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 #include "asterisk.h"
00138
00139 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 262416 $")
00140
00141 #include <ctype.h>
00142 #include <sys/ioctl.h>
00143 #include <fcntl.h>
00144 #include <signal.h>
00145 #include <sys/signal.h>
00146 #include <regex.h>
00147 #include <time.h>
00148
00149 #include "asterisk/network.h"
00150 #include "asterisk/paths.h"
00151
00152 #include "asterisk/lock.h"
00153 #include "asterisk/channel.h"
00154 #include "asterisk/config.h"
00155 #include "asterisk/module.h"
00156 #include "asterisk/pbx.h"
00157 #include "asterisk/sched.h"
00158 #include "asterisk/io.h"
00159 #include "asterisk/rtp.h"
00160 #include "asterisk/udptl.h"
00161 #include "asterisk/acl.h"
00162 #include "asterisk/manager.h"
00163 #include "asterisk/callerid.h"
00164 #include "asterisk/cli.h"
00165 #include "asterisk/app.h"
00166 #include "asterisk/musiconhold.h"
00167 #include "asterisk/dsp.h"
00168 #include "asterisk/features.h"
00169 #include "asterisk/srv.h"
00170 #include "asterisk/astdb.h"
00171 #include "asterisk/causes.h"
00172 #include "asterisk/utils.h"
00173 #include "asterisk/file.h"
00174 #include "asterisk/astobj.h"
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 #include "asterisk/astobj2.h"
00188 #include "asterisk/dnsmgr.h"
00189 #include "asterisk/devicestate.h"
00190 #include "asterisk/linkedlists.h"
00191 #include "asterisk/stringfields.h"
00192 #include "asterisk/monitor.h"
00193 #include "asterisk/netsock.h"
00194 #include "asterisk/localtime.h"
00195 #include "asterisk/abstract_jb.h"
00196 #include "asterisk/threadstorage.h"
00197 #include "asterisk/translate.h"
00198 #include "asterisk/ast_version.h"
00199 #include "asterisk/event.h"
00200 #include "asterisk/tcptls.h"
00201
00202 #ifndef FALSE
00203 #define FALSE 0
00204 #endif
00205
00206 #ifndef TRUE
00207 #define TRUE 1
00208 #endif
00209
00210 #define SIPBUFSIZE 512
00211
00212
00213 #define FINDUSERS (1 << 0)
00214 #define FINDPEERS (1 << 1)
00215 #define FINDALLDEVICES (FINDUSERS | FINDPEERS)
00216
00217 #define XMIT_ERROR -2
00218
00219 #define SIP_RESERVED ";/?:@&=+$,# "
00220
00221
00222
00223 #define DEFAULT_DEFAULT_EXPIRY 120
00224 #define DEFAULT_MIN_EXPIRY 60
00225 #define DEFAULT_MAX_EXPIRY 3600
00226 #define DEFAULT_REGISTRATION_TIMEOUT 20
00227 #define DEFAULT_MAX_FORWARDS "70"
00228
00229
00230
00231 #define EXPIRY_GUARD_SECS 15
00232 #define EXPIRY_GUARD_LIMIT 30
00233
00234 #define EXPIRY_GUARD_MIN 500
00235
00236
00237
00238 #define EXPIRY_GUARD_PCT 0.20
00239
00240 #define DEFAULT_EXPIRY 900
00241
00242 static int min_expiry = DEFAULT_MIN_EXPIRY;
00243 static int max_expiry = DEFAULT_MAX_EXPIRY;
00244 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00245
00246 #define CALLERID_UNKNOWN "Anonymous"
00247 #define FROMDOMAIN_INVALID "anonymous.invalid"
00248
00249 #define DEFAULT_MAXMS 2000
00250 #define DEFAULT_QUALIFYFREQ 60 * 1000
00251 #define DEFAULT_FREQ_NOTOK 10 * 1000
00252
00253 #define DEFAULT_RETRANS 1000
00254 #define MAX_RETRANS 6
00255 #define SIP_TIMER_T1 500
00256 #define SIP_TRANS_TIMEOUT 64 * SIP_TIMER_T1
00257
00258
00259 #define DEFAULT_TRANS_TIMEOUT -1
00260 #define PROVIS_KEEPALIVE_TIMEOUT 60000
00261 #define MAX_AUTHTRIES 3
00262
00263 #define SIP_MAX_HEADERS 64
00264 #define SIP_MAX_LINES 256
00265 #define SIP_MAX_PACKET 4096
00266 #define SIP_MIN_PACKET 1024
00267
00268 #define INITIAL_CSEQ 101
00269
00270 #define DEFAULT_MAX_SE 1800
00271 #define DEFAULT_MIN_SE 90
00272
00273 #define SDP_MAX_RTPMAP_CODECS 32
00274
00275
00276 static struct ast_jb_conf default_jbconf =
00277 {
00278 .flags = 0,
00279 .max_size = -1,
00280 .resync_threshold = -1,
00281 .impl = "",
00282 .target_extra = -1,
00283 };
00284 static struct ast_jb_conf global_jbconf;
00285
00286 static const char config[] = "sip.conf";
00287 static const char notify_config[] = "sip_notify.conf";
00288
00289 #define RTP 1
00290 #define NO_RTP 0
00291
00292
00293
00294
00295 enum transfermodes {
00296 TRANSFER_OPENFORALL,
00297 TRANSFER_CLOSED,
00298 };
00299
00300
00301
00302 enum sip_result {
00303 AST_SUCCESS = 0,
00304 AST_FAILURE = -1,
00305 };
00306
00307
00308
00309
00310 enum invitestates {
00311 INV_NONE = 0,
00312 INV_CALLING = 1,
00313 INV_PROCEEDING = 2,
00314 INV_EARLY_MEDIA = 3,
00315 INV_COMPLETED = 4,
00316 INV_CONFIRMED = 5,
00317 INV_TERMINATED = 6,
00318
00319 INV_CANCELLED = 7,
00320 };
00321
00322
00323
00324 static const struct invstate2stringtable {
00325 const enum invitestates state;
00326 const char *desc;
00327 } invitestate2string[] = {
00328 {INV_NONE, "None" },
00329 {INV_CALLING, "Calling (Trying)"},
00330 {INV_PROCEEDING, "Proceeding "},
00331 {INV_EARLY_MEDIA, "Early media"},
00332 {INV_COMPLETED, "Completed (done)"},
00333 {INV_CONFIRMED, "Confirmed (up)"},
00334 {INV_TERMINATED, "Done"},
00335 {INV_CANCELLED, "Cancelled"}
00336 };
00337
00338
00339
00340
00341 enum xmittype {
00342 XMIT_CRITICAL = 2,
00343
00344 XMIT_RELIABLE = 1,
00345 XMIT_UNRELIABLE = 0,
00346 };
00347
00348 enum parse_register_result {
00349 PARSE_REGISTER_DENIED,
00350 PARSE_REGISTER_FAILED,
00351 PARSE_REGISTER_UPDATE,
00352 PARSE_REGISTER_QUERY,
00353 };
00354
00355
00356 enum subscriptiontype {
00357 NONE = 0,
00358 XPIDF_XML,
00359 DIALOG_INFO_XML,
00360 CPIM_PIDF_XML,
00361 PIDF_XML,
00362 MWI_NOTIFICATION
00363 };
00364
00365
00366
00367
00368
00369
00370 static const struct cfsubscription_types {
00371 enum subscriptiontype type;
00372 const char * const event;
00373 const char * const mediatype;
00374 const char * const text;
00375 } subscription_types[] = {
00376 { NONE, "-", "unknown", "unknown" },
00377
00378 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00379 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00380 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00381 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00382 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00383 };
00384
00385
00386
00387
00388
00389
00390
00391 enum sip_auth_type {
00392 PROXY_AUTH = 407,
00393 WWW_AUTH = 401,
00394 };
00395
00396
00397 enum check_auth_result {
00398 AUTH_DONT_KNOW = -100,
00399
00400
00401 AUTH_SUCCESSFUL = 0,
00402 AUTH_CHALLENGE_SENT = 1,
00403 AUTH_SECRET_FAILED = -1,
00404 AUTH_USERNAME_MISMATCH = -2,
00405 AUTH_NOT_FOUND = -3,
00406 AUTH_FAKE_AUTH = -4,
00407 AUTH_UNKNOWN_DOMAIN = -5,
00408 AUTH_PEER_NOT_DYNAMIC = -6,
00409 AUTH_ACL_FAILED = -7,
00410 AUTH_BAD_TRANSPORT = -8,
00411 };
00412
00413
00414 enum sipregistrystate {
00415 REG_STATE_UNREGISTERED = 0,
00416
00417
00418
00419
00420 REG_STATE_REGSENT,
00421
00422
00423
00424
00425 REG_STATE_AUTHSENT,
00426
00427
00428
00429
00430 REG_STATE_REGISTERED,
00431
00432 REG_STATE_REJECTED,
00433
00434
00435
00436
00437
00438 REG_STATE_TIMEOUT,
00439
00440
00441 REG_STATE_NOAUTH,
00442
00443
00444 REG_STATE_FAILED,
00445
00446 };
00447
00448
00449 enum st_mode {
00450 SESSION_TIMER_MODE_INVALID = 0,
00451 SESSION_TIMER_MODE_ACCEPT,
00452 SESSION_TIMER_MODE_ORIGINATE,
00453 SESSION_TIMER_MODE_REFUSE
00454 };
00455
00456
00457 enum st_refresher {
00458 SESSION_TIMER_REFRESHER_AUTO,
00459 SESSION_TIMER_REFRESHER_UAC,
00460 SESSION_TIMER_REFRESHER_UAS
00461 };
00462
00463
00464
00465
00466 enum sip_transport {
00467 SIP_TRANSPORT_UDP = 1,
00468 SIP_TRANSPORT_TCP = 1 << 1,
00469 SIP_TRANSPORT_TLS = 1 << 2,
00470 };
00471
00472
00473
00474
00475
00476
00477
00478 struct sip_proxy {
00479 char name[MAXHOSTNAMELEN];
00480 struct sockaddr_in ip;
00481 time_t last_dnsupdate;
00482 enum sip_transport transport;
00483 int force;
00484
00485 };
00486
00487
00488 struct __show_chan_arg {
00489 int fd;
00490 int subscriptions;
00491 int numchans;
00492 };
00493
00494
00495
00496 enum can_create_dialog {
00497 CAN_NOT_CREATE_DIALOG,
00498 CAN_CREATE_DIALOG,
00499 CAN_CREATE_DIALOG_UNSUPPORTED_METHOD,
00500 };
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 enum sipmethod {
00513 SIP_UNKNOWN,
00514 SIP_RESPONSE,
00515 SIP_REGISTER,
00516 SIP_OPTIONS,
00517 SIP_NOTIFY,
00518 SIP_INVITE,
00519 SIP_ACK,
00520 SIP_PRACK,
00521 SIP_BYE,
00522 SIP_REFER,
00523 SIP_SUBSCRIBE,
00524 SIP_MESSAGE,
00525 SIP_UPDATE,
00526 SIP_INFO,
00527 SIP_CANCEL,
00528 SIP_PUBLISH,
00529 SIP_PING,
00530 };
00531
00532
00533
00534
00535
00536 static const struct cfsip_methods {
00537 enum sipmethod id;
00538 int need_rtp;
00539 char * const text;
00540 enum can_create_dialog can_create;
00541 } sip_methods[] = {
00542 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00543 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00544 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00545 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00546 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00547 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00548 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00549 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00550 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00551 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00552 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00553 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00554 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00555 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00556 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00557 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00558 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00559 };
00560
00561 static unsigned int chan_idx;
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 #define SUPPORTED 1
00574 #define NOT_SUPPORTED 0
00575
00576
00577 #define SIP_OPT_REPLACES (1 << 0)
00578 #define SIP_OPT_100REL (1 << 1)
00579 #define SIP_OPT_TIMER (1 << 2)
00580 #define SIP_OPT_EARLY_SESSION (1 << 3)
00581 #define SIP_OPT_JOIN (1 << 4)
00582 #define SIP_OPT_PATH (1 << 5)
00583 #define SIP_OPT_PREF (1 << 6)
00584 #define SIP_OPT_PRECONDITION (1 << 7)
00585 #define SIP_OPT_PRIVACY (1 << 8)
00586 #define SIP_OPT_SDP_ANAT (1 << 9)
00587 #define SIP_OPT_SEC_AGREE (1 << 10)
00588 #define SIP_OPT_EVENTLIST (1 << 11)
00589 #define SIP_OPT_GRUU (1 << 12)
00590 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00591 #define SIP_OPT_NOREFERSUB (1 << 14)
00592 #define SIP_OPT_HISTINFO (1 << 15)
00593 #define SIP_OPT_RESPRIORITY (1 << 16)
00594 #define SIP_OPT_FROMCHANGE (1 << 17)
00595 #define SIP_OPT_RECLISTINV (1 << 18)
00596 #define SIP_OPT_RECLISTSUB (1 << 19)
00597 #define SIP_OPT_UNKNOWN (1 << 20)
00598
00599
00600
00601
00602 static const struct cfsip_options {
00603 int id;
00604 int supported;
00605 char * const text;
00606 } sip_options[] = {
00607
00608 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00609
00610 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00611
00612 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00613
00614 { SIP_OPT_FROMCHANGE, NOT_SUPPORTED, "from-change" },
00615
00616 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00617
00618 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00619
00620 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00621
00622 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00623
00624 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00625
00626 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00627
00628 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00629
00630 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00631
00632 { SIP_OPT_RECLISTINV, NOT_SUPPORTED, "recipient-list-invite" },
00633
00634 { SIP_OPT_RECLISTSUB, NOT_SUPPORTED, "recipient-list-subscribe" },
00635
00636 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00637
00638 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00639
00640 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00641
00642 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00643
00644 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00645
00646 { SIP_OPT_TIMER, SUPPORTED, "timer" },
00647
00648 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00649 };
00650
00651
00652
00653
00654
00655
00656 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO"
00657
00658
00659
00660
00661
00662
00663 #define SUPPORTED_EXTENSIONS "replaces, timer"
00664
00665
00666 #define STANDARD_SIP_PORT 5060
00667
00668 #define STANDARD_TLS_PORT 5061
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 #define DEFAULT_CONTEXT "default"
00687 #define DEFAULT_MOHINTERPRET "default"
00688 #define DEFAULT_MOHSUGGEST ""
00689 #define DEFAULT_VMEXTEN "asterisk"
00690 #define DEFAULT_CALLERID "asterisk"
00691 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00692 #define DEFAULT_ALLOWGUEST TRUE
00693 #define DEFAULT_CALLCOUNTER FALSE
00694 #define DEFAULT_SRVLOOKUP TRUE
00695 #define DEFAULT_COMPACTHEADERS FALSE
00696 #define DEFAULT_TOS_SIP 0
00697 #define DEFAULT_TOS_AUDIO 0
00698 #define DEFAULT_TOS_VIDEO 0
00699 #define DEFAULT_TOS_TEXT 0
00700 #define DEFAULT_COS_SIP 4
00701 #define DEFAULT_COS_AUDIO 5
00702 #define DEFAULT_COS_VIDEO 6
00703 #define DEFAULT_COS_TEXT 5
00704 #define DEFAULT_ALLOW_EXT_DOM TRUE
00705 #define DEFAULT_REALM "asterisk"
00706 #define DEFAULT_NOTIFYRINGING TRUE
00707 #define DEFAULT_PEDANTIC FALSE
00708 #define DEFAULT_AUTOCREATEPEER FALSE
00709 #define DEFAULT_QUALIFY FALSE
00710 #define DEFAULT_REGEXTENONQUALIFY FALSE
00711 #define DEFAULT_T1MIN 100
00712 #define DEFAULT_MAX_CALL_BITRATE (384)
00713 #ifndef DEFAULT_USERAGENT
00714 #define DEFAULT_USERAGENT "Asterisk PBX"
00715 #define DEFAULT_SDPSESSION "Asterisk PBX"
00716 #define DEFAULT_SDPOWNER "root"
00717 #endif
00718
00719
00720
00721
00722
00723
00724
00725 static char default_context[AST_MAX_CONTEXT];
00726 static char default_subscribecontext[AST_MAX_CONTEXT];
00727 static char default_language[MAX_LANGUAGE];
00728 static char default_callerid[AST_MAX_EXTENSION];
00729 static char default_fromdomain[AST_MAX_EXTENSION];
00730 static char default_notifymime[AST_MAX_EXTENSION];
00731 static int default_qualify;
00732 static char default_vmexten[AST_MAX_EXTENSION];
00733 static char default_mohinterpret[MAX_MUSICCLASS];
00734 static char default_mohsuggest[MAX_MUSICCLASS];
00735
00736 static char default_parkinglot[AST_MAX_CONTEXT];
00737 static int default_maxcallbitrate;
00738 static struct ast_codec_pref default_prefs;
00739
00740
00741 struct sip_settings {
00742 int peer_rtupdate;
00743 int rtsave_sysname;
00744 int ignore_regexpire;
00745 };
00746
00747 static struct sip_settings sip_cfg;
00748
00749
00750
00751
00752
00753
00754
00755 static int global_directrtpsetup;
00756 static int global_rtautoclear;
00757 static int global_notifyringing;
00758 static int global_notifyhold;
00759 static int global_alwaysauthreject;
00760 static int global_srvlookup;
00761 static int pedanticsipchecking;
00762 static int autocreatepeer;
00763 static int global_match_auth_username;
00764 static int global_relaxdtmf;
00765 static int global_prematuremediafilter;
00766 static int global_relaxdtmf;
00767 static int global_rtptimeout;
00768 static int global_rtpholdtimeout;
00769 static int global_rtpkeepalive;
00770 static int global_reg_timeout;
00771 static int global_regattempts_max;
00772 static int global_shrinkcallerid;
00773 static int global_allowguest;
00774 static int global_callcounter;
00775
00776
00777 static int global_allowsubscribe;
00778
00779 static unsigned int global_tos_sip;
00780 static unsigned int global_tos_audio;
00781 static unsigned int global_tos_video;
00782 static unsigned int global_tos_text;
00783 static unsigned int global_cos_sip;
00784 static unsigned int global_cos_audio;
00785 static unsigned int global_cos_video;
00786 static unsigned int global_cos_text;
00787 static int compactheaders;
00788 static int recordhistory;
00789 static int dumphistory;
00790 static char global_realm[MAXHOSTNAMELEN];
00791 static char global_regcontext[AST_MAX_CONTEXT];
00792 static char global_useragent[AST_MAX_EXTENSION];
00793 static char global_sdpsession[AST_MAX_EXTENSION];
00794 static char global_sdpowner[AST_MAX_EXTENSION];
00795 static int allow_external_domains;
00796 static int global_callevents;
00797 static int global_authfailureevents;
00798 static int global_t1;
00799 static int global_t1min;
00800 static int global_timer_b;
00801 static int global_regextenonqualify;
00802 static int global_autoframing;
00803 static enum transfermodes global_allowtransfer;
00804 static struct sip_proxy global_outboundproxy;
00805 static int global_matchexterniplocally;
00806 static int global_qualifyfreq;
00807
00808
00809
00810 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00811
00812 static enum st_mode global_st_mode;
00813 static enum st_refresher global_st_refresher;
00814 static int global_min_se;
00815 static int global_max_se;
00816
00817 static int global_dynamic_exclude_static = 0;
00818
00819
00820
00821 static struct ast_ha *global_contact_ha = NULL;
00822
00823
00824
00825
00826 static int speerobjs = 0;
00827 static int rpeerobjs = 0;
00828 static int apeerobjs = 0;
00829 static int regobjs = 0;
00830
00831
00832 static struct ast_flags global_flags[2] = {{0}};
00833 static int global_t38_maxdatagram;
00834
00835 static char used_context[AST_MAX_CONTEXT];
00836
00837
00838 AST_MUTEX_DEFINE_STATIC(netlock);
00839
00840
00841
00842 AST_MUTEX_DEFINE_STATIC(monlock);
00843
00844 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00845
00846
00847
00848 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00849
00850 static int sip_reloading = FALSE;
00851 static enum channelreloadreason sip_reloadreason;
00852
00853 static struct sched_context *sched;
00854 static struct io_context *io;
00855 static int *sipsock_read_id;
00856
00857 #define DEC_CALL_LIMIT 0
00858 #define INC_CALL_LIMIT 1
00859 #define DEC_CALL_RINGING 2
00860 #define INC_CALL_RINGING 3
00861
00862
00863 struct sip_socket {
00864 enum sip_transport type;
00865 int fd;
00866 uint16_t port;
00867 struct ast_tcptls_session_instance *tcptls_session;
00868 };
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894 struct sip_request {
00895 ptrdiff_t rlPart1;
00896 ptrdiff_t rlPart2;
00897 int len;
00898 int headers;
00899 int method;
00900 int lines;
00901 unsigned int sdp_start;
00902 unsigned int sdp_count;
00903 char debug;
00904 char has_to_tag;
00905 char ignore;
00906
00907 ptrdiff_t header[SIP_MAX_HEADERS];
00908
00909 ptrdiff_t line[SIP_MAX_LINES];
00910 struct ast_str *data;
00911
00912 struct sip_socket socket;
00913 AST_LIST_ENTRY(sip_request) next;
00914 };
00915
00916
00917
00918
00919
00920
00921
00922
00923 #define REQ_OFFSET_TO_STR(req,offset) ((req)->data->str + ((req)->offset))
00924
00925
00926 struct sip_dual {
00927 struct ast_channel *chan1;
00928 struct ast_channel *chan2;
00929 struct sip_request req;
00930 int seqno;
00931 };
00932
00933 struct sip_pkt;
00934
00935
00936 struct sip_invite_param {
00937 int addsipheaders;
00938 const char *uri_options;
00939 const char *vxml_url;
00940 char *auth;
00941 char *authheader;
00942 enum sip_auth_type auth_type;
00943 const char *replaces;
00944 int transfer;
00945 };
00946
00947
00948 struct sip_route {
00949 struct sip_route *next;
00950 char hop[0];
00951 };
00952
00953
00954 enum domain_mode {
00955 SIP_DOMAIN_AUTO,
00956 SIP_DOMAIN_CONFIG,
00957 };
00958
00959
00960
00961
00962
00963 struct domain {
00964 char domain[MAXHOSTNAMELEN];
00965 char context[AST_MAX_EXTENSION];
00966 enum domain_mode mode;
00967 AST_LIST_ENTRY(domain) list;
00968 };
00969
00970 static AST_LIST_HEAD_STATIC(domain_list, domain);
00971
00972
00973
00974 struct sip_history {
00975 AST_LIST_ENTRY(sip_history) list;
00976 char event[0];
00977 };
00978
00979 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00980
00981
00982 struct sip_auth {
00983 char realm[AST_MAX_EXTENSION];
00984 char username[256];
00985 char secret[256];
00986 char md5secret[256];
00987 struct sip_auth *next;
00988 };
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000 #define SIP_OUTGOING (1 << 0)
01001 #define SIP_RINGING (1 << 2)
01002 #define SIP_PROGRESS_SENT (1 << 3)
01003 #define SIP_NEEDREINVITE (1 << 4)
01004 #define SIP_PENDINGBYE (1 << 5)
01005 #define SIP_GOTREFER (1 << 6)
01006 #define SIP_CALL_LIMIT (1 << 7)
01007 #define SIP_INC_COUNT (1 << 8)
01008 #define SIP_INC_RINGING (1 << 9)
01009 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 10)
01010
01011 #define SIP_PROMISCREDIR (1 << 11)
01012 #define SIP_TRUSTRPID (1 << 12)
01013 #define SIP_USEREQPHONE (1 << 13)
01014 #define SIP_USECLIENTCODE (1 << 14)
01015
01016
01017 #define SIP_DTMF (7 << 15)
01018 #define SIP_DTMF_RFC2833 (0 << 15)
01019 #define SIP_DTMF_INBAND (1 << 15)
01020 #define SIP_DTMF_INFO (2 << 15)
01021 #define SIP_DTMF_AUTO (3 << 15)
01022 #define SIP_DTMF_SHORTINFO (4 << 15)
01023
01024
01025 #define SIP_NAT (3 << 18)
01026 #define SIP_NAT_NEVER (0 << 18)
01027 #define SIP_NAT_RFC3581 (1 << 18)
01028 #define SIP_NAT_ROUTE (2 << 18)
01029 #define SIP_NAT_ALWAYS (3 << 18)
01030
01031
01032 #define SIP_REINVITE (7 << 20)
01033 #define SIP_REINVITE_NONE (0 << 20)
01034 #define SIP_CAN_REINVITE (1 << 20)
01035 #define SIP_CAN_REINVITE_NAT (2 << 20)
01036 #define SIP_REINVITE_UPDATE (4 << 20)
01037
01038
01039 #define SIP_INSECURE (3 << 23)
01040 #define SIP_INSECURE_NONE (0 << 23)
01041 #define SIP_INSECURE_PORT (1 << 23)
01042 #define SIP_INSECURE_INVITE (1 << 24)
01043
01044
01045 #define SIP_PROG_INBAND (3 << 25)
01046 #define SIP_PROG_INBAND_NEVER (0 << 25)
01047 #define SIP_PROG_INBAND_NO (1 << 25)
01048 #define SIP_PROG_INBAND_YES (2 << 25)
01049
01050 #define SIP_SENDRPID (1 << 29)
01051 #define SIP_G726_NONSTANDARD (1 << 31)
01052
01053
01054 #define SIP_FLAGS_TO_COPY \
01055 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
01056 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
01057 SIP_USEREQPHONE | SIP_INSECURE)
01058
01059
01060
01061
01062
01063
01064 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
01065 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
01066 #define SIP_PAGE2_HAVEPEERCONTEXT (1 << 3)
01067
01068 #define SIP_PAGE2_STATECHANGEQUEUE (1 << 9)
01069
01070 #define SIP_PAGE2_RPORT_PRESENT (1 << 10)
01071 #define SIP_PAGE2_VIDEOSUPPORT (1 << 14)
01072 #define SIP_PAGE2_TEXTSUPPORT (1 << 15)
01073 #define SIP_PAGE2_ALLOWSUBSCRIBE (1 << 16)
01074 #define SIP_PAGE2_ALLOWOVERLAP (1 << 17)
01075 #define SIP_PAGE2_SUBSCRIBEMWIONLY (1 << 18)
01076 #define SIP_PAGE2_IGNORESDPVERSION (1 << 19)
01077
01078 #define SIP_PAGE2_T38SUPPORT (3 << 20)
01079 #define SIP_PAGE2_T38SUPPORT_UDPTL (1 << 20)
01080 #define SIP_PAGE2_T38SUPPORT_UDPTL_FEC (2 << 20)
01081 #define SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY (3 << 20)
01082
01083 #define SIP_PAGE2_CALL_ONHOLD (3 << 23)
01084 #define SIP_PAGE2_CALL_ONHOLD_ACTIVE (1 << 23)
01085 #define SIP_PAGE2_CALL_ONHOLD_ONEDIR (2 << 23)
01086 #define SIP_PAGE2_CALL_ONHOLD_INACTIVE (3 << 23)
01087
01088 #define SIP_PAGE2_RFC2833_COMPENSATE (1 << 25)
01089 #define SIP_PAGE2_BUGGY_MWI (1 << 26)
01090 #define SIP_PAGE2_DIALOG_ESTABLISHED (1 << 27)
01091 #define SIP_PAGE2_FAX_DETECT (1 << 28)
01092 #define SIP_PAGE2_REGISTERTRYING (1 << 29)
01093 #define SIP_PAGE2_UDPTL_DESTINATION (1 << 30)
01094 #define SIP_PAGE2_VIDEOSUPPORT_ALWAYS (1 << 31)
01095
01096 #define SIP_PAGE2_FLAGS_TO_COPY \
01097 (SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_IGNORESDPVERSION | \
01098 SIP_PAGE2_VIDEOSUPPORT | SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | \
01099 SIP_PAGE2_BUGGY_MWI | SIP_PAGE2_TEXTSUPPORT | \
01100 SIP_PAGE2_UDPTL_DESTINATION | SIP_PAGE2_VIDEOSUPPORT_ALWAYS | SIP_PAGE2_FAX_DETECT | \
01101 SIP_PAGE2_HAVEPEERCONTEXT)
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111 enum sip_debug_e {
01112 sip_debug_none = 0,
01113 sip_debug_config = 1,
01114 sip_debug_console = 2,
01115 };
01116
01117 static enum sip_debug_e sipdebug;
01118
01119
01120
01121
01122
01123 static int sipdebug_text;
01124
01125
01126 enum t38state {
01127 T38_DISABLED = 0,
01128 T38_LOCAL_REINVITE,
01129 T38_PEER_REINVITE,
01130 T38_ENABLED
01131 };
01132
01133
01134 struct t38properties {
01135 enum t38state state;
01136 struct ast_control_t38_parameters our_parms;
01137 struct ast_control_t38_parameters their_parms;
01138 };
01139
01140
01141 enum referstatus {
01142 REFER_IDLE,
01143 REFER_SENT,
01144 REFER_RECEIVED,
01145 REFER_CONFIRMED,
01146 REFER_ACCEPTED,
01147 REFER_RINGING,
01148 REFER_200OK,
01149 REFER_FAILED,
01150 REFER_NOAUTH
01151 };
01152
01153
01154
01155
01156
01157
01158 struct _map_x_s {
01159 int x;
01160 const char *s;
01161 };
01162
01163 static const struct _map_x_s referstatusstrings[] = {
01164 { REFER_IDLE, "<none>" },
01165 { REFER_SENT, "Request sent" },
01166 { REFER_RECEIVED, "Request received" },
01167 { REFER_CONFIRMED, "Confirmed" },
01168 { REFER_ACCEPTED, "Accepted" },
01169 { REFER_RINGING, "Target ringing" },
01170 { REFER_200OK, "Done" },
01171 { REFER_FAILED, "Failed" },
01172 { REFER_NOAUTH, "Failed - auth failure" },
01173 { -1, NULL}
01174 };
01175
01176
01177
01178 struct sip_refer {
01179 char refer_to[AST_MAX_EXTENSION];
01180 char refer_to_domain[AST_MAX_EXTENSION];
01181 char refer_to_urioption[AST_MAX_EXTENSION];
01182 char refer_to_context[AST_MAX_EXTENSION];
01183 char referred_by[AST_MAX_EXTENSION];
01184 char referred_by_name[AST_MAX_EXTENSION];
01185 char refer_contact[AST_MAX_EXTENSION];
01186 char replaces_callid[SIPBUFSIZE];
01187 char replaces_callid_totag[SIPBUFSIZE/2];
01188 char replaces_callid_fromtag[SIPBUFSIZE/2];
01189 struct sip_pvt *refer_call;
01190
01191
01192
01193 int attendedtransfer;
01194 int localtransfer;
01195 enum referstatus status;
01196 };
01197
01198
01199
01200
01201
01202 struct sip_st_dlg {
01203 int st_active;
01204 int st_interval;
01205 int st_schedid;
01206 enum st_refresher st_ref;
01207 int st_expirys;
01208 int st_active_peer_ua;
01209 int st_cached_min_se;
01210 int st_cached_max_se;
01211 enum st_mode st_cached_mode;
01212 enum st_refresher st_cached_ref;
01213 unsigned char quit_flag:1;
01214 };
01215
01216
01217
01218
01219
01220 struct sip_st_cfg {
01221 enum st_mode st_mode_oper;
01222 enum st_refresher st_ref;
01223 int st_min_se;
01224 int st_max_se;
01225 };
01226
01227 struct offered_media {
01228 int offered;
01229 char text[128];
01230 };
01231
01232
01233
01234
01235
01236 struct sip_pvt {
01237 struct sip_pvt *next;
01238 enum invitestates invitestate;
01239 int method;
01240 AST_DECLARE_STRING_FIELDS(
01241 AST_STRING_FIELD(callid);
01242 AST_STRING_FIELD(randdata);
01243 AST_STRING_FIELD(accountcode);
01244 AST_STRING_FIELD(realm);
01245 AST_STRING_FIELD(nonce);
01246 AST_STRING_FIELD(opaque);
01247 AST_STRING_FIELD(qop);
01248 AST_STRING_FIELD(domain);
01249 AST_STRING_FIELD(from);
01250 AST_STRING_FIELD(useragent);
01251 AST_STRING_FIELD(exten);
01252 AST_STRING_FIELD(context);
01253 AST_STRING_FIELD(subscribecontext);
01254 AST_STRING_FIELD(subscribeuri);
01255 AST_STRING_FIELD(fromdomain);
01256 AST_STRING_FIELD(fromuser);
01257 AST_STRING_FIELD(fromname);
01258 AST_STRING_FIELD(tohost);
01259 AST_STRING_FIELD(todnid);
01260 AST_STRING_FIELD(language);
01261 AST_STRING_FIELD(mohinterpret);
01262 AST_STRING_FIELD(mohsuggest);
01263 AST_STRING_FIELD(rdnis);
01264 AST_STRING_FIELD(redircause);
01265 AST_STRING_FIELD(theirtag);
01266 AST_STRING_FIELD(username);
01267 AST_STRING_FIELD(peername);
01268 AST_STRING_FIELD(authname);
01269 AST_STRING_FIELD(uri);
01270 AST_STRING_FIELD(okcontacturi);
01271 AST_STRING_FIELD(peersecret);
01272 AST_STRING_FIELD(peermd5secret);
01273 AST_STRING_FIELD(cid_num);
01274 AST_STRING_FIELD(cid_name);
01275 AST_STRING_FIELD(fullcontact);
01276
01277 AST_STRING_FIELD(our_contact);
01278 AST_STRING_FIELD(rpid);
01279 AST_STRING_FIELD(rpid_from);
01280 AST_STRING_FIELD(url);
01281 AST_STRING_FIELD(parkinglot);
01282 );
01283 char via[128];
01284 struct sip_socket socket;
01285 unsigned int ocseq;
01286 unsigned int icseq;
01287 ast_group_t callgroup;
01288 ast_group_t pickupgroup;
01289 int lastinvite;
01290 struct ast_flags flags[2];
01291
01292
01293 char do_history;
01294 char alreadygone;
01295 char needdestroy;
01296 char outgoing_call;
01297 char answered_elsewhere;
01298 char novideo;
01299 char notext;
01300
01301 int timer_t1;
01302 int timer_b;
01303 unsigned int sipoptions;
01304 unsigned int reqsipoptions;
01305 struct ast_codec_pref prefs;
01306 int capability;
01307 int jointcapability;
01308 int peercapability;
01309 int prefcodec;
01310 int noncodeccapability;
01311 int jointnoncodeccapability;
01312 int redircodecs;
01313 int maxcallbitrate;
01314 struct sip_proxy *outboundproxy;
01315 int t38_maxdatagram;
01316 struct t38properties t38;
01317 struct sockaddr_in udptlredirip;
01318 struct ast_udptl *udptl;
01319 int callingpres;
01320 int authtries;
01321 int expiry;
01322 long branch;
01323 long invite_branch;
01324 char tag[11];
01325 int sessionid;
01326 int sessionversion;
01327 int64_t sessionversion_remote;
01328 int session_modify;
01329 unsigned int portinuri:1;
01330 struct sockaddr_in sa;
01331 struct sockaddr_in redirip;
01332 struct sockaddr_in vredirip;
01333 struct sockaddr_in tredirip;
01334 time_t lastrtprx;
01335 time_t lastrtptx;
01336 int rtptimeout;
01337 struct sockaddr_in recv;
01338 struct sockaddr_in ourip;
01339 struct ast_channel *owner;
01340 struct sip_route *route;
01341 int route_persistant;
01342 struct ast_variable *notify_headers;
01343 struct sip_auth *peerauth;
01344 int noncecount;
01345 unsigned int stalenonce:1;
01346 char lastmsg[256];
01347 int amaflags;
01348 int pendinginvite;
01349 int glareinvite;
01350
01351
01352 struct sip_request initreq;
01353
01354
01355
01356 int initid;
01357 int waitid;
01358 int autokillid;
01359 int t38id;
01360 enum transfermodes allowtransfer;
01361 struct sip_refer *refer;
01362 enum subscriptiontype subscribed;
01363 int stateid;
01364 int laststate;
01365 int dialogver;
01366
01367 struct ast_dsp *dsp;
01368
01369 struct sip_peer *relatedpeer;
01370
01371 struct sip_registry *registry;
01372 struct ast_rtp *rtp;
01373 struct ast_rtp *vrtp;
01374 struct ast_rtp *trtp;
01375 struct sip_pkt *packets;
01376 struct sip_history_head *history;
01377 size_t history_entries;
01378 struct ast_variable *chanvars;
01379 AST_LIST_HEAD_NOLOCK(request_queue, sip_request) request_queue;
01380 int request_queue_sched_id;
01381 int provisional_keepalive_sched_id;
01382 const char *last_provisional;
01383 struct sip_invite_param *options;
01384 int autoframing;
01385
01386
01387
01388 struct sip_st_dlg *stimer;
01389 int red;
01390 int hangupcause;
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405 struct offered_media offered_media[4];
01406 };
01407
01408
01409 #define MAX_HISTORY_ENTRIES 50
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419 struct ao2_container *dialogs;
01420
01421 #define sip_pvt_lock(x) ao2_lock(x)
01422 #define sip_pvt_trylock(x) ao2_trylock(x)
01423 #define sip_pvt_unlock(x) ao2_unlock(x)
01424
01425
01426
01427
01428
01429
01430 #ifdef REF_DEBUG
01431 #define dialog_ref(arg1,arg2) dialog_ref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
01432 #define dialog_unref(arg1,arg2) dialog_unref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
01433
01434 static struct sip_pvt *dialog_ref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
01435 {
01436 if (p)
01437 _ao2_ref_debug(p, 1, tag, file, line, func);
01438 else
01439 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
01440 return p;
01441 }
01442
01443 static struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
01444 {
01445 if (p)
01446 _ao2_ref_debug(p, -1, tag, file, line, func);
01447 return NULL;
01448 }
01449 #else
01450 static struct sip_pvt *dialog_ref(struct sip_pvt *p, char *tag)
01451 {
01452 if (p)
01453 ao2_ref(p, 1);
01454 else
01455 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
01456 return p;
01457 }
01458
01459 static struct sip_pvt *dialog_unref(struct sip_pvt *p, char *tag)
01460 {
01461 if (p)
01462 ao2_ref(p, -1);
01463 return NULL;
01464 }
01465 #endif
01466
01467
01468
01469
01470
01471
01472
01473 struct sip_pkt {
01474 struct sip_pkt *next;
01475 int retrans;
01476 int method;
01477 int seqno;
01478 char is_resp;
01479 char is_fatal;
01480 int response_code;
01481 struct sip_pvt *owner;
01482 int retransid;
01483 int timer_a;
01484 int timer_t1;
01485 int packetlen;
01486 struct ast_str *data;
01487 };
01488
01489
01490
01491
01492
01493
01494
01495 struct sip_mailbox {
01496 char *mailbox;
01497 char *context;
01498
01499 struct ast_event_sub *event_sub;
01500 AST_LIST_ENTRY(sip_mailbox) entry;
01501 };
01502
01503 enum sip_peer_type {
01504 SIP_TYPE_PEER = (1 << 0),
01505 SIP_TYPE_USER = (1 << 1),
01506 };
01507
01508
01509
01510
01511 struct sip_peer {
01512 char name[80];
01513 struct sip_socket socket;
01514 enum sip_transport default_outbound_transport;
01515 unsigned int transports:3;
01516 char secret[80];
01517 char md5secret[80];
01518 struct sip_auth *auth;
01519 char context[AST_MAX_CONTEXT];
01520 char subscribecontext[AST_MAX_CONTEXT];
01521 char username[80];
01522 char accountcode[AST_MAX_ACCOUNT_CODE];
01523 int amaflags;
01524 char tohost[MAXHOSTNAMELEN];
01525 char regexten[AST_MAX_EXTENSION];
01526 char fromuser[80];
01527 char fromdomain[MAXHOSTNAMELEN];
01528 char fullcontact[256];
01529 char cid_num[80];
01530 char cid_name[80];
01531 int callingpres;
01532 int inUse;
01533 int inRinging;
01534 int onHold;
01535 int call_limit;
01536 int t38_maxdatagram;
01537 int busy_level;
01538 enum transfermodes allowtransfer;
01539 char vmexten[AST_MAX_EXTENSION];
01540 char language[MAX_LANGUAGE];
01541 char mohinterpret[MAX_MUSICCLASS];
01542 char mohsuggest[MAX_MUSICCLASS];
01543 char parkinglot[AST_MAX_CONTEXT];
01544 char useragent[256];
01545 struct ast_codec_pref prefs;
01546 int lastmsgssent;
01547 unsigned int sipoptions;
01548 struct ast_flags flags[2];
01549
01550
01551 AST_LIST_HEAD_NOLOCK(, sip_mailbox) mailboxes;
01552
01553
01554 char is_realtime;
01555 char rt_fromcontact;
01556 char host_dynamic;
01557 char selfdestruct;
01558 char the_mark;
01559
01560 int expire;
01561 int capability;
01562 int rtptimeout;
01563 int rtpholdtimeout;
01564 int rtpkeepalive;
01565 ast_group_t callgroup;
01566 ast_group_t pickupgroup;
01567 struct sip_proxy *outboundproxy;
01568 struct ast_dnsmgr_entry *dnsmgr;
01569 struct sockaddr_in addr;
01570 int maxcallbitrate;
01571 unsigned int portinuri:1;
01572
01573
01574 struct sip_pvt *call;
01575 int pokeexpire;
01576 int lastms;
01577 int maxms;
01578 int qualifyfreq;
01579 struct timeval ps;
01580 struct sockaddr_in defaddr;
01581 struct ast_ha *ha;
01582 struct ast_ha *contactha;
01583 struct ast_variable *chanvars;
01584 struct sip_pvt *mwipvt;
01585 int autoframing;
01586 struct sip_st_cfg stimer;
01587 int timer_t1;
01588 int timer_b;
01589 int deprecated_username;
01590 enum sip_peer_type type;
01591 };
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606 struct sip_registry {
01607 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
01608 AST_DECLARE_STRING_FIELDS(
01609 AST_STRING_FIELD(callid);
01610 AST_STRING_FIELD(realm);
01611 AST_STRING_FIELD(nonce);
01612 AST_STRING_FIELD(opaque);
01613 AST_STRING_FIELD(qop);
01614 AST_STRING_FIELD(domain);
01615 AST_STRING_FIELD(username);
01616 AST_STRING_FIELD(authuser);
01617 AST_STRING_FIELD(hostname);
01618 AST_STRING_FIELD(secret);
01619 AST_STRING_FIELD(md5secret);
01620 AST_STRING_FIELD(callback);
01621 AST_STRING_FIELD(random);
01622 AST_STRING_FIELD(peername);
01623 );
01624 enum sip_transport transport;
01625 int portno;
01626 int expire;
01627 int configured_expiry;
01628 int expiry;
01629 int regattempts;
01630 int timeout;
01631 int refresh;
01632 struct sip_pvt *call;
01633 enum sipregistrystate regstate;
01634 struct timeval regtime;
01635 int callid_valid;
01636 unsigned int ocseq;
01637 struct ast_dnsmgr_entry *dnsmgr;
01638 struct sockaddr_in us;
01639 int noncecount;
01640 char lastmsg[256];
01641 };
01642
01643 enum sip_tcptls_alert {
01644
01645 TCPTLS_ALERT_DATA,
01646
01647 TCPTLS_ALERT_STOP,
01648 };
01649
01650 struct tcptls_packet {
01651 AST_LIST_ENTRY(tcptls_packet) entry;
01652 struct ast_str *data;
01653 size_t len;
01654 };
01655
01656 struct sip_threadinfo {
01657 int stop;
01658 int alert_pipe[2];
01659 pthread_t threadid;
01660 struct ast_tcptls_session_instance *tcptls_session;
01661 enum sip_transport type;
01662 AST_LIST_HEAD_NOLOCK(, tcptls_packet) packet_q;
01663 };
01664
01665
01666
01667 #ifdef LOW_MEMORY
01668 static int hash_peer_size = 17;
01669 static int hash_dialog_size = 17;
01670 static int hash_user_size = 17;
01671 #else
01672 static int hash_peer_size = 563;
01673 static int hash_dialog_size = 563;
01674 static int hash_user_size = 563;
01675 #endif
01676
01677
01678 static struct ao2_container *threadt;
01679
01680
01681 struct ao2_container *peers;
01682 struct ao2_container *peers_by_ip;
01683
01684
01685 static struct ast_register_list {
01686 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01687 int recheck;
01688 } regl;
01689
01690
01691
01692
01693 static int peer_hash_cb(const void *obj, const int flags)
01694 {
01695 const struct sip_peer *peer = obj;
01696
01697 return ast_str_case_hash(peer->name);
01698 }
01699
01700
01701
01702
01703 static int peer_cmp_cb(void *obj, void *arg, int flags)
01704 {
01705 struct sip_peer *peer = obj, *peer2 = arg;
01706
01707 return !strcasecmp(peer->name, peer2->name) ? CMP_MATCH | CMP_STOP : 0;
01708 }
01709
01710
01711
01712
01713 static int peer_iphash_cb(const void *obj, const int flags)
01714 {
01715 const struct sip_peer *peer = obj;
01716 int ret1 = peer->addr.sin_addr.s_addr;
01717 if (ret1 < 0)
01718 ret1 = -ret1;
01719
01720 return ret1;
01721 }
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741 static int peer_ipcmp_cb(void *obj, void *arg, int flags)
01742 {
01743 struct sip_peer *peer = obj, *peer2 = arg;
01744
01745 if (peer->addr.sin_addr.s_addr != peer2->addr.sin_addr.s_addr) {
01746
01747 return 0;
01748 }
01749
01750
01751 if ((peer->transports & peer2->transports) & (SIP_TRANSPORT_TLS | SIP_TRANSPORT_TCP)) {
01752
01753 return CMP_MATCH | CMP_STOP;
01754 } else if (ast_test_flag(&peer2->flags[0], SIP_INSECURE_PORT)) {
01755
01756
01757 return ast_test_flag(&peer->flags[0], SIP_INSECURE_PORT) ?
01758 (CMP_MATCH | CMP_STOP) : 0;
01759 }
01760
01761
01762 return peer->addr.sin_port == peer2->addr.sin_port ? (CMP_MATCH | CMP_STOP) : 0;
01763 }
01764
01765
01766 static int threadt_hash_cb(const void *obj, const int flags)
01767 {
01768 const struct sip_threadinfo *th = obj;
01769
01770 return (int) th->tcptls_session->remote_address.sin_addr.s_addr;
01771 }
01772
01773 static int threadt_cmp_cb(void *obj, void *arg, int flags)
01774 {
01775 struct sip_threadinfo *th = obj, *th2 = arg;
01776
01777 return (th->tcptls_session == th2->tcptls_session) ? CMP_MATCH | CMP_STOP : 0;
01778 }
01779
01780
01781
01782
01783 static int dialog_hash_cb(const void *obj, const int flags)
01784 {
01785 const struct sip_pvt *pvt = obj;
01786
01787 return ast_str_case_hash(pvt->callid);
01788 }
01789
01790
01791
01792
01793 static int dialog_cmp_cb(void *obj, void *arg, int flags)
01794 {
01795 struct sip_pvt *pvt = obj, *pvt2 = arg;
01796
01797 return !strcasecmp(pvt->callid, pvt2->callid) ? CMP_MATCH | CMP_STOP : 0;
01798 }
01799
01800 static int temp_pvt_init(void *);
01801 static void temp_pvt_cleanup(void *);
01802
01803
01804 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01805
01806 #ifdef LOW_MEMORY
01807 static void ts_ast_rtp_destroy(void *);
01808
01809 AST_THREADSTORAGE_CUSTOM(ts_audio_rtp, NULL, ts_ast_rtp_destroy);
01810 AST_THREADSTORAGE_CUSTOM(ts_video_rtp, NULL, ts_ast_rtp_destroy);
01811 AST_THREADSTORAGE_CUSTOM(ts_text_rtp, NULL, ts_ast_rtp_destroy);
01812 #endif
01813
01814
01815
01816 static struct sip_auth *authl = NULL;
01817
01818
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830
01831
01832
01833
01834
01835 static int sipsock = -1;
01836
01837 static struct sockaddr_in bindaddr;
01838
01839
01840
01841
01842
01843
01844
01845 static struct sockaddr_in internip;
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865 static struct sockaddr_in externip;
01866
01867 static char externhost[MAXHOSTNAMELEN];
01868 static time_t externexpire;
01869 static int externrefresh = 10;
01870 static struct sockaddr_in stunaddr;
01871
01872
01873
01874
01875
01876
01877
01878 static struct ast_ha *localaddr;
01879
01880 static int ourport_tcp;
01881 static int ourport_tls;
01882 static struct sockaddr_in debugaddr;
01883
01884 static struct ast_config *notify_types = NULL;
01885
01886
01887
01888 #define UNLINK(element, head, prev) do { \
01889 if (prev) \
01890 (prev)->next = (element)->next; \
01891 else \
01892 (head) = (element)->next; \
01893 } while (0)
01894
01895 enum t38_action_flag {
01896 SDP_T38_NONE = 0,
01897 SDP_T38_INITIATE,
01898 SDP_T38_ACCEPT,
01899 };
01900
01901
01902
01903
01904
01905
01906 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
01907 static int sip_devicestate(void *data);
01908 static int sip_sendtext(struct ast_channel *ast, const char *text);
01909 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01910 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen);
01911 static int sip_hangup(struct ast_channel *ast);
01912 static int sip_answer(struct ast_channel *ast);
01913 static struct ast_frame *sip_read(struct ast_channel *ast);
01914 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01915 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01916 static int sip_transfer(struct ast_channel *ast, const char *dest);
01917 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01918 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01919 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01920 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen);
01921 static const char *sip_get_callid(struct ast_channel *chan);
01922
01923 static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin);
01924 static int sip_standard_port(enum sip_transport type, int port);
01925 static int sip_prepare_socket(struct sip_pvt *p);
01926 static int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport);
01927
01928
01929 static int sipsock_read(int *id, int fd, short events, void *ignore);
01930 static int __sip_xmit(struct sip_pvt *p, struct ast_str *data, int len);
01931 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, struct ast_str *data, int len, int fatal, int sipmethod);
01932 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01933 static int retrans_pkt(const void *data);
01934 static int transmit_response_using_temp(ast_string_field callid, struct sockaddr_in *sin, int useglobal_nat, const int intended_method, const struct sip_request *req, const char *msg);
01935 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01936 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01937 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01938 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp);
01939 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01940 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);
01941 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp);
01942 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01943 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable);
01944 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01945 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01946 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
01947 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp);
01948 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01949 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01950 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01951 static int transmit_refer(struct sip_pvt *p, const char *dest);
01952 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten);
01953 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01954 static int transmit_notify_custom(struct sip_pvt *p, struct ast_variable *vars);
01955 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01956 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01957 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01958 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01959 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01960 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
01961 static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only);
01962
01963
01964 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
01965 int useglobal_nat, const int intended_method, struct sip_request *req);
01966 static int __sip_autodestruct(const void *data);
01967 static void sip_scheddestroy(struct sip_pvt *p, int ms);
01968 static int sip_cancel_destroy(struct sip_pvt *p);
01969 static struct sip_pvt *sip_destroy(struct sip_pvt *p);
01970 static void *dialog_unlink_all(struct sip_pvt *dialog, int lockowner, int lockdialoglist);
01971 static void *registry_unref(struct sip_registry *reg, char *tag);
01972 static void __sip_destroy(struct sip_pvt *p, int lockowner, int lockdialoglist);
01973 static int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01974 static void __sip_pretend_ack(struct sip_pvt *p);
01975 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01976 static int auto_congest(const void *arg);
01977 static int update_call_counter(struct sip_pvt *fup, int event);
01978 static int hangup_sip2cause(int cause);
01979 static const char *hangup_cause2sip(int cause);
01980 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method);
01981 static void free_old_route(struct sip_route *route);
01982 static void list_route(struct sip_route *route);
01983 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01984 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
01985 struct sip_request *req, char *uri);
01986 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01987 static void check_pendings(struct sip_pvt *p);
01988 static void *sip_park_thread(void *stuff);
01989 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno);
01990 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01991
01992
01993 static void try_suggested_sip_codec(struct sip_pvt *p);
01994 static const char *get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01995 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value);
01996 static int find_sdp(struct sip_request *req);
01997 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action);
01998 static int process_sdp_o(const char *o, struct sip_pvt *p);
01999 static int process_sdp_c(const char *c, struct ast_hostent *hp);
02000 static int process_sdp_a_sendonly(const char *a, int *sendonly);
02001 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp *newaudiortp, int *last_rtpmap_codec);
02002 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp *newvideortp, int *last_rtpmap_codec);
02003 static int process_sdp_a_text(const char *a, struct sip_pvt *p, struct ast_rtp *newtextrtp, char *red_fmtp, int *red_num_gen, int *red_data_pt, int *last_rtpmap_codec);
02004 static int process_sdp_a_image(const char *a, struct sip_pvt *p);
02005 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
02006 struct ast_str **m_buf, struct ast_str **a_buf,
02007 int debug, int *min_packet_size);
02008 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
02009 struct ast_str **m_buf, struct ast_str **a_buf,
02010 int debug);
02011 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38);
02012 static void do_setnat(struct sip_pvt *p, int natflags);
02013 static void stop_media_flows(struct sip_pvt *p);
02014
02015
02016 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
02017 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
02018 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
02019 const char *secret, const char *md5secret, int sipmethod,
02020 char *uri, enum xmittype reliable, int ignore);
02021 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
02022 int sipmethod, char *uri, enum xmittype reliable,
02023 struct sockaddr_in *sin, struct sip_peer **authpeer);
02024 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin);
02025
02026
02027 static int check_sip_domain(const char *domain, char *context, size_t len);
02028 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
02029 static void clear_sip_domains(void);
02030
02031
02032 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, const char *configuration, int lineno);
02033 static int clear_realm_authentication(struct sip_auth *authlist);
02034 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
02035
02036
02037 static void check_rtp_timeout(struct sip_pvt *dialog, time_t t);
02038 static int sip_do_reload(enum channelreloadreason reason);
02039 static int reload_config(enum channelreloadreason reason);
02040 static int expire_register(const void *data);
02041 static void *do_monitor(void *data);
02042 static int restart_monitor(void);
02043 static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer);
02044
02045 static int sip_refer_allocate(struct sip_pvt *p);
02046 static void ast_quiet_chan(struct ast_channel *chan);
02047 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
02048
02049
02050
02051
02052
02053
02054
02055 #define check_request_transport(peer, tmpl) ({ \
02056 int ret = 0; \
02057 if (peer->socket.type == tmpl->socket.type) \
02058 ; \
02059 else if (!(peer->transports & tmpl->socket.type)) {\
02060 ast_log(LOG_ERROR, \
02061 "'%s' is not a valid transport for '%s'. we only use '%s'! ending call.\n", \
02062 get_transport(tmpl->socket.type), peer->name, get_transport_list(peer) \
02063 ); \
02064 ret = 1; \
02065 } else if (peer->socket.type & SIP_TRANSPORT_TLS) { \
02066 ast_log(LOG_WARNING, \
02067 "peer '%s' HAS NOT USED (OR SWITCHED TO) TLS in favor of '%s' (but this was allowed in sip.conf)!\n", \
02068 peer->name, get_transport(tmpl->socket.type) \
02069 ); \
02070 } else { \
02071 ast_debug(1, \
02072 "peer '%s' has contacted us over %s even though we prefer %s.\n", \
02073 peer->name, get_transport(tmpl->socket.type), get_transport(peer->socket.type) \
02074 ); \
02075 }\
02076 (ret); \
02077 })
02078
02079
02080
02081 static int cb_extensionstate(char *context, char* exten, int state, void *data);
02082 static int sip_devicestate(void *data);
02083 static int sip_poke_noanswer(const void *data);
02084 static int sip_poke_peer(struct sip_peer *peer, int force);
02085 static void sip_poke_all_peers(void);
02086 static void sip_peer_hold(struct sip_pvt *p, int hold);
02087 static void mwi_event_cb(const struct ast_event *, void *);
02088
02089
02090 static const char *sip_nat_mode(const struct sip_pvt *p);
02091 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02092 static char *transfermode2str(enum transfermodes mode) attribute_const;
02093 static const char *nat2str(int nat) attribute_const;
02094 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
02095 static char *sip_show_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02096 static char * _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
02097 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02098 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02099 static void print_group(int fd, ast_group_t group, int crlf);
02100 static const char *dtmfmode2str(int mode) attribute_const;
02101 static int str2dtmfmode(const char *str) attribute_unused;
02102 static const char *insecure2str(int mode) attribute_const;
02103 static void cleanup_stale_contexts(char *new, char *old);
02104 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
02105 static const char *domain_mode_to_text(const enum domain_mode mode);
02106 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02107 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
02108 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02109 static char *_sip_qualify_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
02110 static char *sip_qualify_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02111 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02112 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02113 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02114 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
02115 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
02116 static char *complete_sip_peer(const char *word, int state, int flags2);
02117 static char *complete_sip_registered_peer(const char *word, int state, int flags2);
02118 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state);
02119 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
02120 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state);
02121 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
02122 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02123 static char *sip_show_channelstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02124 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02125 static char *sip_do_debug_ip(int fd, char *arg);
02126 static char *sip_do_debug_peer(int fd, char *arg);
02127 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02128 static char *sip_cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02129 static char *sip_do_history_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02130 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02131 static int sip_dtmfmode(struct ast_channel *chan, void *data);
02132 static int sip_addheader(struct ast_channel *chan, void *data);
02133 static int sip_do_reload(enum channelreloadreason reason);
02134 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02135 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen);
02136
02137
02138
02139
02140
02141 static void sip_dump_history(struct sip_pvt *dialog);
02142 static inline int sip_debug_test_addr(const struct sockaddr_in *addr);
02143 static inline int sip_debug_test_pvt(struct sip_pvt *p);
02144
02145
02146
02147
02148 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
02149 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
02150 static void sip_dump_history(struct sip_pvt *dialog);
02151
02152
02153 static struct sip_peer *temp_peer(const char *name);
02154 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only);
02155 static int update_call_counter(struct sip_pvt *fup, int event);
02156 static void sip_destroy_peer(struct sip_peer *peer);
02157 static void sip_destroy_peer_fn(void *peer);
02158 static void set_peer_defaults(struct sip_peer *peer);
02159 static struct sip_peer *temp_peer(const char *name);
02160 static void register_peer_exten(struct sip_peer *peer, int onoff);
02161 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int which_objects, int devstate_only, int transport);
02162 static int sip_poke_peer_s(const void *data);
02163 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
02164 static void reg_source_db(struct sip_peer *peer);
02165 static void destroy_association(struct sip_peer *peer);
02166 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
02167 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
02168 static void set_socket_transport(struct sip_socket *socket, int transport);
02169
02170
02171 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, const char *useragent, int expirey, int deprecated_username, int lastms);
02172 static void update_peer(struct sip_peer *p, int expire);
02173 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *config);
02174 static const char *get_name_from_variable(struct ast_variable *var, const char *newpeername);
02175 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin, int devstate_only);
02176 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
02177
02178
02179 static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us, struct sip_pvt *p);
02180 static void sip_registry_destroy(struct sip_registry *reg);
02181 static int sip_register(const char *value, int lineno);
02182 static const char *regstate2str(enum sipregistrystate regstate) attribute_const;
02183 static int sip_reregister(const void *data);
02184 static int __sip_do_register(struct sip_registry *r);
02185 static int sip_reg_timeout(const void *data);
02186 static void sip_send_all_registers(void);
02187 static int sip_reinvite_retry(const void *data);
02188
02189
02190 static void append_date(struct sip_request *req);
02191 static int determine_firstline_parts(struct sip_request *req);
02192 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
02193 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
02194 static int find_sip_method(const char *msg);
02195 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
02196 static int parse_request(struct sip_request *req);
02197 static const char *get_header(const struct sip_request *req, const char *name);
02198 static const char *referstatus2str(enum referstatus rstatus) attribute_pure;
02199 static int method_match(enum sipmethod id, const char *name);
02200 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
02201 static char *get_in_brackets(char *tmp);
02202 static const char *find_alias(const char *name, const char *_default);
02203 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
02204 static int lws2sws(char *msgbuf, int len);
02205 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
02206 static char *remove_uri_parameters(char *uri);
02207 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
02208 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
02209 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
02210 static int set_address_from_contact(struct sip_pvt *pvt);
02211 static void check_via(struct sip_pvt *p, struct sip_request *req);
02212 static char *get_calleridname(const char *input, char *output, size_t outputsize);
02213 static int get_rpid_num(const char *input, char *output, int maxlen);
02214 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq);
02215 static int get_destination(struct sip_pvt *p, struct sip_request *oreq);
02216 static int get_msg_text(char *buf, int len, struct sip_request *req, int addnewline);
02217 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
02218
02219
02220 static void *_sip_tcp_helper_thread(struct sip_pvt *pvt, struct ast_tcptls_session_instance *tcptls_session);
02221 static void *sip_tcp_worker_fn(void *);
02222
02223
02224 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
02225 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
02226 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
02227 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod);
02228 static int init_resp(struct sip_request *resp, const char *msg);
02229 static inline int resp_needs_contact(const char *msg, enum sipmethod method);
02230 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
02231 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p);
02232 static void build_via(struct sip_pvt *p);
02233 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
02234 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct sockaddr_in *sin, int newdialog);
02235 static char *generate_random_string(char *buf, size_t size);
02236 static void build_callid_pvt(struct sip_pvt *pvt);
02237 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain);
02238 static void make_our_tag(char *tagbuf, size_t len);
02239 static int add_header(struct sip_request *req, const char *var, const char *value);
02240 static int add_header_contentLength(struct sip_request *req, int len);
02241 static int add_line(struct sip_request *req, const char *line);
02242 static int add_text(struct sip_request *req, const char *text);
02243 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode);
02244 static int add_vidupdate(struct sip_request *req);
02245 static void add_route(struct sip_request *req, struct sip_route *route);
02246 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
02247 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
02248 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
02249 static void set_destination(struct sip_pvt *p, char *uri);
02250 static void append_date(struct sip_request *req);
02251 static void build_contact(struct sip_pvt *p);
02252 static void build_rpid(struct sip_pvt *p);
02253
02254
02255 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock);
02256 static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct sockaddr_in *sin, int *recount, char *e, int *nounlock);
02257 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock);
02258 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
02259 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e);
02260 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
02261 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
02262 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
02263 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
02264 static int handle_request_options(struct sip_pvt *p, struct sip_request *req);
02265 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct sockaddr_in *sin, int *nounlock);
02266 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
02267 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno);
02268
02269
02270 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02271 static void handle_response_notify(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02272 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02273 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02274 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02275
02276
02277 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, struct ast_rtp *trtp, int codecs, int nat_active);
02278 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
02279 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
02280 static enum ast_rtp_get_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
02281 static int sip_get_codec(struct ast_channel *chan);
02282 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect);
02283
02284
02285 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
02286 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
02287 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
02288 static void change_t38_state(struct sip_pvt *p, int state);
02289
02290
02291 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp);
02292 static int proc_session_timer(const void *vp);
02293 static void stop_session_timer(struct sip_pvt *p);
02294 static void start_session_timer(struct sip_pvt *p);
02295 static void restart_session_timer(struct sip_pvt *p);
02296 static const char *strefresher2str(enum st_refresher r);
02297 static int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher *const p_ref);
02298 static int parse_minse(const char *p_hdrval, int *const p_interval);
02299 static int st_get_se(struct sip_pvt *, int max);
02300 static enum st_refresher st_get_refresher(struct sip_pvt *);
02301 static enum st_mode st_get_mode(struct sip_pvt *);
02302 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p);
02303
02304
02305
02306 static const struct ast_channel_tech sip_tech = {
02307 .type = "SIP",
02308 .description = "Session Initiation Protocol (SIP)",
02309 .capabilities = AST_FORMAT_AUDIO_MASK,
02310 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
02311 .requester = sip_request_call,
02312 .devicestate = sip_devicestate,
02313 .call = sip_call,
02314 .send_html = sip_sendhtml,
02315 .hangup = sip_hangup,
02316 .answer = sip_answer,
02317 .read = sip_read,
02318 .write = sip_write,
02319 .write_video = sip_write,
02320 .write_text = sip_write,
02321 .indicate = sip_indicate,
02322 .transfer = sip_transfer,
02323 .fixup = sip_fixup,
02324 .send_digit_begin = sip_senddigit_begin,
02325 .send_digit_end = sip_senddigit_end,
02326 .bridge = ast_rtp_bridge,
02327 .early_bridge = ast_rtp_early_bridge,
02328 .send_text = sip_sendtext,
02329 .func_channel_read = acf_channel_read,
02330 .queryoption = sip_queryoption,
02331 .get_pvt_uniqueid = sip_get_callid,
02332 };
02333
02334
02335
02336
02337
02338
02339
02340 static struct ast_channel_tech sip_tech_info;
02341
02342
02343
02344 static struct ast_tls_config sip_tls_cfg;
02345
02346
02347 static struct ast_tls_config default_tls_cfg;
02348
02349
02350 static struct ast_tcptls_session_args sip_tcp_desc = {
02351 .accept_fd = -1,
02352 .master = AST_PTHREADT_NULL,
02353 .tls_cfg = NULL,
02354 .poll_timeout = -1,
02355 .name = "sip tcp server",
02356 .accept_fn = ast_tcptls_server_root,
02357 .worker_fn = sip_tcp_worker_fn,
02358 };
02359
02360
02361 static struct ast_tcptls_session_args sip_tls_desc = {
02362 .accept_fd = -1,
02363 .master = AST_PTHREADT_NULL,
02364 .tls_cfg = &sip_tls_cfg,
02365 .poll_timeout = -1,
02366 .name = "sip tls server",
02367 .accept_fn = ast_tcptls_server_root,
02368 .worker_fn = sip_tcp_worker_fn,
02369 };
02370
02371
02372 #define IS_SIP_TECH(t) ((t) == &sip_tech || (t) == &sip_tech_info)
02373
02374
02375
02376
02377 static const char *map_x_s(const struct _map_x_s *table, int x, const char *errorstring)
02378 {
02379 const struct _map_x_s *cur;
02380
02381 for (cur = table; cur->s; cur++)
02382 if (cur->x == x)
02383 return cur->s;
02384 return errorstring;
02385 }
02386
02387
02388
02389
02390 static int map_s_x(const struct _map_x_s *table, const char *s, int errorvalue)
02391 {
02392 const struct _map_x_s *cur;
02393
02394 for (cur = table; cur->s; cur++)
02395 if (!strcasecmp(cur->s, s))
02396 return cur->x;
02397 return errorvalue;
02398 }
02399
02400
02401
02402 static struct ast_rtp_protocol sip_rtp = {
02403 .type = "SIP",
02404 .get_rtp_info = sip_get_rtp_peer,
02405 .get_vrtp_info = sip_get_vrtp_peer,
02406 .get_trtp_info = sip_get_trtp_peer,
02407 .set_rtp_peer = sip_set_rtp_peer,
02408 .get_codec = sip_get_codec,
02409 };
02410
02411
02412 static void tcptls_packet_destructor(void *obj)
02413 {
02414 struct tcptls_packet *packet = obj;
02415
02416 ast_free(packet->data);
02417 }
02418
02419 static void sip_tcptls_client_args_destructor(void *obj)
02420 {
02421 struct ast_tcptls_session_args *args = obj;
02422 if (args->tls_cfg) {
02423 ast_free(args->tls_cfg->certfile);
02424 ast_free(args->tls_cfg->cipher);
02425 ast_free(args->tls_cfg->cafile);
02426 ast_free(args->tls_cfg->capath);
02427 }
02428 ast_free(args->tls_cfg);
02429 ast_free((char *) args->name);
02430 }
02431
02432 static void sip_threadinfo_destructor(void *obj)
02433 {
02434 struct sip_threadinfo *th = obj;
02435 struct tcptls_packet *packet;
02436 if (th->alert_pipe[1] > -1) {
02437 close(th->alert_pipe[0]);
02438 }
02439 if (th->alert_pipe[1] > -1) {
02440 close(th->alert_pipe[1]);
02441 }
02442 th->alert_pipe[0] = th->alert_pipe[1] = -1;
02443
02444 while ((packet = AST_LIST_REMOVE_HEAD(&th->packet_q, entry))) {
02445 ao2_t_ref(packet, -1, "thread destruction, removing packet from frame queue");
02446 }
02447
02448 if (th->tcptls_session) {
02449 ao2_t_ref(th->tcptls_session, -1, "remove tcptls_session for sip_threadinfo object");
02450 }
02451 }
02452
02453
02454 static struct sip_threadinfo *sip_threadinfo_create(struct ast_tcptls_session_instance *tcptls_session, int transport)
02455 {
02456 struct sip_threadinfo *th;
02457
02458 if (!tcptls_session || !(th = ao2_alloc(sizeof(*th), sip_threadinfo_destructor))) {
02459 return NULL;
02460 }
02461
02462 th->alert_pipe[0] = th->alert_pipe[1] = -1;
02463
02464 if (pipe(th->alert_pipe) == -1) {
02465 ao2_t_ref(th, -1, "Failed to open alert pipe on sip_threadinfo");
02466 ast_log(LOG_ERROR, "Could not create sip alert pipe in tcptls thread, error %s\n", strerror(errno));
02467 return NULL;
02468 }
02469 ao2_t_ref(tcptls_session, +1, "tcptls_session ref for sip_threadinfo object");
02470 th->tcptls_session = tcptls_session;
02471 th->type = transport ? transport : (tcptls_session->ssl ? SIP_TRANSPORT_TLS: SIP_TRANSPORT_TCP);
02472 ao2_t_link(threadt, th, "Adding new tcptls helper thread");
02473 ao2_t_ref(th, -1, "Decrementing threadinfo ref from alloc, only table ref remains");
02474 return th;
02475 }
02476
02477
02478 static int sip_tcptls_write(struct ast_tcptls_session_instance *tcptls_session, const void *buf, size_t len)
02479 {
02480 int res = len;
02481 struct sip_threadinfo *th = NULL;
02482 struct tcptls_packet *packet = NULL;
02483 struct sip_threadinfo tmp = {
02484 .tcptls_session = tcptls_session,
02485 };
02486 enum sip_tcptls_alert alert = TCPTLS_ALERT_DATA;
02487
02488 if (!tcptls_session) {
02489 return XMIT_ERROR;
02490 }
02491
02492 ast_mutex_lock(&tcptls_session->lock);
02493
02494 if ((tcptls_session->fd == -1) ||
02495 !(th = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread")) ||
02496 !(packet = ao2_alloc(sizeof(*packet), tcptls_packet_destructor)) ||
02497 !(packet->data = ast_str_create(len))) {
02498 goto tcptls_write_setup_error;
02499 }
02500
02501
02502 ast_str_set(&packet->data, 0, "%s", (char *) buf);
02503 packet->len = len;
02504
02505
02506
02507
02508 ao2_lock(th);
02509 if (write(th->alert_pipe[1], &alert, sizeof(alert)) == -1) {
02510 ast_log(LOG_ERROR, "write() to alert pipe failed: %s\n", strerror(errno));
02511 ao2_t_ref(packet, -1, "could not write to alert pipe, remove packet");
02512 packet = NULL;
02513 res = XMIT_ERROR;
02514 } else {
02515 AST_LIST_INSERT_TAIL(&th->packet_q, packet, entry);
02516 }
02517 ao2_unlock(th);
02518
02519 ast_mutex_unlock(&tcptls_session->lock);
02520 ao2_t_ref(th, -1, "In sip_tcptls_write, unref threadinfo object after finding it");
02521 return res;
02522
02523 tcptls_write_setup_error:
02524 if (th) {
02525 ao2_t_ref(th, -1, "In sip_tcptls_write, unref threadinfo obj, could not create packet");
02526 }
02527 if (packet) {
02528 ao2_t_ref(packet, -1, "could not allocate packet's data");
02529 }
02530 ast_mutex_unlock(&tcptls_session->lock);
02531
02532 return XMIT_ERROR;
02533 }
02534
02535
02536 static void *sip_tcp_worker_fn(void *data)
02537 {
02538 struct ast_tcptls_session_instance *tcptls_session = data;
02539
02540 return _sip_tcp_helper_thread(NULL, tcptls_session);
02541 }
02542
02543
02544 static void *_sip_tcp_helper_thread(struct sip_pvt *pvt, struct ast_tcptls_session_instance *tcptls_session)
02545 {
02546 int res, cl;
02547 struct sip_request req = { 0, } , reqcpy = { 0, };
02548 struct sip_threadinfo *me = NULL;
02549 char buf[1024] = "";
02550 struct pollfd fds[2] = { { 0 }, { 0 }, };
02551 struct ast_tcptls_session_args *ca = NULL;
02552
02553
02554
02555
02556
02557
02558
02559
02560
02561
02562
02563 if (!tcptls_session->client) {
02564 if (!(me = sip_threadinfo_create(tcptls_session, tcptls_session->ssl ? SIP_TRANSPORT_TLS : SIP_TRANSPORT_TCP))) {
02565 goto cleanup;
02566 }
02567 ao2_t_ref(me, +1, "Adding threadinfo ref for tcp_helper_thread");
02568 } else {
02569 struct sip_threadinfo tmp = {
02570 .tcptls_session = tcptls_session,
02571 };
02572
02573 if ((!(ca = tcptls_session->parent)) ||
02574 (!(me = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread"))) ||
02575 (!(tcptls_session = ast_tcptls_client_start(tcptls_session)))) {
02576 goto cleanup;
02577 }
02578 }
02579
02580 me->threadid = pthread_self();
02581 ast_debug(2, "Starting thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
02582
02583
02584 fds[0].fd = tcptls_session->fd;
02585 fds[1].fd = me->alert_pipe[0];
02586 fds[0].events = fds[1].events = POLLIN | POLLPRI;
02587
02588 if (!(req.data = ast_str_create(SIP_MIN_PACKET)))
02589 goto cleanup;
02590 if (!(reqcpy.data = ast_str_create(SIP_MIN_PACKET)))
02591 goto cleanup;
02592
02593 for (;;) {
02594 struct ast_str *str_save;
02595
02596 res = ast_poll(fds, 2, -1);
02597 if (res < 0) {
02598 ast_debug(2, "SIP %s server :: ast_wait_for_input returned %d\n", tcptls_session->ssl ? "SSL": "TCP", res);
02599 goto cleanup;
02600 }
02601
02602
02603
02604 if (fds[0].revents) {
02605
02606 fds[0].revents = 0;
02607
02608
02609 str_save = req.data;
02610 memset(&req, 0, sizeof(req));
02611 req.data = str_save;
02612 ast_str_reset(req.data);
02613
02614 str_save = reqcpy.data;
02615 memset(&reqcpy, 0, sizeof(reqcpy));
02616 reqcpy.data = str_save;
02617 ast_str_reset(reqcpy.data);
02618
02619 memset(buf, 0, sizeof(buf));
02620
02621 if (tcptls_session->ssl) {
02622 set_socket_transport(&req.socket, SIP_TRANSPORT_TLS);
02623 req.socket.port = htons(ourport_tls);
02624 } else {
02625 set_socket_transport(&req.socket, SIP_TRANSPORT_TCP);
02626 req.socket.port = htons(ourport_tcp);
02627 }
02628 req.socket.fd = tcptls_session->fd;
02629
02630
02631 while (req.len < 4 || strncmp(REQ_OFFSET_TO_STR(&req, len - 4), "\r\n\r\n", 4)) {
02632 ast_mutex_lock(&tcptls_session->lock);
02633 if (!fgets(buf, sizeof(buf), tcptls_session->f)) {
02634 ast_mutex_unlock(&tcptls_session->lock);
02635 goto cleanup;
02636 }
02637 ast_mutex_unlock(&tcptls_session->lock);
02638 if (me->stop)
02639 goto cleanup;
02640 ast_str_append(&req.data, 0, "%s", buf);
02641 req.len = req.data->used;
02642 }
02643 copy_request(&reqcpy, &req);
02644 parse_request(&reqcpy);
02645
02646 if (sscanf(get_header(&reqcpy, "Content-Length"), "%30d", &cl)) {
02647 while (cl > 0) {
02648 size_t bytes_read;
02649 ast_mutex_lock(&tcptls_session->lock);
02650 if (!(bytes_read = fread(buf, 1, MIN(sizeof(buf) - 1, cl), tcptls_session->f))) {
02651 ast_mutex_unlock(&tcptls_session->lock);
02652 goto cleanup;
02653 }
02654 buf[bytes_read] = '\0';
02655 ast_mutex_unlock(&tcptls_session->lock);
02656 if (me->stop)
02657 goto cleanup;
02658 cl -= strlen(buf);
02659 ast_str_append(&req.data, 0, "%s", buf);
02660 req.len = req.data->used;
02661 }
02662 }
02663
02664
02665
02666 req.socket.tcptls_session = tcptls_session;
02667 handle_request_do(&req, &tcptls_session->remote_address);
02668 }
02669 if (fds[1].revents) {
02670 enum sip_tcptls_alert alert;
02671 struct tcptls_packet *packet;
02672
02673 fds[1].revents = 0;
02674
02675 if (read(me->alert_pipe[0], &alert, sizeof(alert)) == -1) {
02676 ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
02677 continue;
02678 }
02679
02680 switch (alert) {
02681 case TCPTLS_ALERT_STOP:
02682 goto cleanup;
02683 case TCPTLS_ALERT_DATA:
02684 ao2_lock(me);
02685 if (!(packet = AST_LIST_REMOVE_HEAD(&me->packet_q, entry))) {
02686 ast_log(LOG_WARNING, "TCPTLS thread alert_pipe indicated packet should be sent, but frame_q is empty");
02687 } else if (ast_tcptls_server_write(tcptls_session, ast_str_buffer(packet->data), packet->len) == -1) {
02688 ast_log(LOG_WARNING, "Failure to write to tcp/tls socket\n");
02689 }
02690
02691 if (packet) {
02692 ao2_t_ref(packet, -1, "tcptls packet sent, this is no longer needed");
02693 }
02694 ao2_unlock(me);
02695 break;
02696 default:
02697 ast_log(LOG_ERROR, "Unknown tcptls thread alert '%d'\n", alert);
02698 }
02699 }
02700 }
02701
02702 ast_debug(2, "Shutting down thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
02703
02704 cleanup:
02705 if (me) {
02706 ao2_t_unlink(threadt, me, "Removing tcptls helper thread, thread is closing");
02707 ao2_t_ref(me, -1, "Removing tcp_helper_threads threadinfo ref");
02708 }
02709 if (reqcpy.data) {
02710 ast_free(reqcpy.data);
02711 }
02712
02713 if (req.data) {
02714 ast_free(req.data);
02715 req.data = NULL;
02716 }
02717
02718
02719 if (ca) {
02720 ao2_t_ref(ca, -1, "closing tcptls thread, getting rid of client tcptls_session arguments");
02721 }
02722
02723 if (tcptls_session) {
02724 ast_mutex_lock(&tcptls_session->lock);
02725 if (tcptls_session->f) {
02726 fclose(tcptls_session->f);
02727 tcptls_session->f = NULL;
02728 }
02729 if (tcptls_session->fd != -1) {
02730 close(tcptls_session->fd);
02731 tcptls_session->fd = -1;
02732 }
02733 tcptls_session->parent = NULL;
02734 ast_mutex_unlock(&tcptls_session->lock);
02735
02736 ao2_ref(tcptls_session, -1);
02737 tcptls_session = NULL;
02738 }
02739
02740 return NULL;
02741 }
02742
02743
02744
02745
02746
02747
02748
02749 static void *unref_peer(struct sip_peer *peer, char *tag)
02750 {
02751 ao2_t_ref(peer, -1, tag);
02752 return NULL;
02753 }
02754
02755 static struct sip_peer *ref_peer(struct sip_peer *peer, char *tag)
02756 {
02757 ao2_t_ref(peer, 1, tag);
02758 return peer;
02759 }
02760
02761
02762
02763
02764
02765
02766
02767
02768
02769
02770
02771
02772 static struct sip_proxy *ref_proxy(struct sip_pvt *pvt, struct sip_proxy *proxy)
02773 {
02774 struct sip_proxy *old_obproxy = pvt->outboundproxy;
02775
02776 if (proxy && proxy != &global_outboundproxy) {
02777 ao2_ref(proxy, +1);
02778 }
02779 pvt->outboundproxy = proxy;
02780 if (old_obproxy && old_obproxy != &global_outboundproxy) {
02781 ao2_ref(old_obproxy, -1);
02782 }
02783 return proxy;
02784 }
02785
02786
02787
02788
02789
02790
02791
02792
02793 static void *dialog_unlink_all(struct sip_pvt *dialog, int lockowner, int lockdialoglist)
02794 {
02795 struct sip_pkt *cp;
02796
02797 dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
02798
02799 ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
02800
02801
02802 if (dialog->owner) {
02803 if (lockowner)
02804 ast_channel_lock(dialog->owner);
02805 ast_debug(1, "Detaching from channel %s\n", dialog->owner->name);
02806 dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
02807 if (lockowner)
02808 ast_channel_unlock(dialog->owner);
02809 }
02810 if (dialog->registry) {
02811 if (dialog->registry->call == dialog)
02812 dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
02813 dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
02814 }
02815 if (dialog->stateid > -1) {
02816 ast_extension_state_del(dialog->stateid, NULL);
02817 dialog_unref(dialog, "removing extension_state, should unref the associated dialog ptr that was stored there.");
02818 dialog->stateid = -1;
02819 }
02820
02821 if (dialog->relatedpeer && dialog->relatedpeer->mwipvt == dialog)
02822 dialog->relatedpeer->mwipvt = dialog_unref(dialog->relatedpeer->mwipvt, "delete ->relatedpeer->mwipvt");
02823 if (dialog->relatedpeer && dialog->relatedpeer->call == dialog)
02824 dialog->relatedpeer->call = dialog_unref(dialog->relatedpeer->call, "unset the relatedpeer->call field in tandem with relatedpeer field itself");
02825
02826
02827 while((cp = dialog->packets)) {
02828 dialog->packets = dialog->packets->next;
02829 AST_SCHED_DEL(sched, cp->retransid);
02830 dialog_unref(cp->owner, "remove all current packets in this dialog, and the pointer to the dialog too as part of __sip_destroy");
02831 if (cp->data) {
02832 ast_free(cp->data);
02833 }
02834 ast_free(cp);
02835 }
02836
02837 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"));
02838
02839 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"));
02840
02841 if (dialog->autokillid > -1)
02842 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"));
02843
02844 if (dialog->request_queue_sched_id > -1) {
02845 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"));
02846 }
02847
02848 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"));
02849
02850 if (dialog->t38id > -1) {
02851 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"));
02852 }
02853
02854 dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
02855 return NULL;
02856 }
02857
02858 static void *registry_unref(struct sip_registry *reg, char *tag)
02859 {
02860 ast_debug(3, "SIP Registry %s: refcount now %d\n", reg->hostname, reg->refcount - 1);
02861 ASTOBJ_UNREF(reg, sip_registry_destroy);
02862 return NULL;
02863 }
02864
02865
02866 static struct sip_registry *registry_addref(struct sip_registry *reg, char *tag)
02867 {
02868 ast_debug(3, "SIP Registry %s: refcount now %d\n", reg->hostname, reg->refcount + 1);
02869 return ASTOBJ_REF(reg);
02870 }
02871
02872
02873 static struct ast_udptl_protocol sip_udptl = {
02874 type: "SIP",
02875 get_udptl_info: sip_get_udptl_peer,
02876 set_udptl_peer: sip_set_udptl_peer,
02877 };
02878
02879 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
02880 __attribute__((format(printf, 2, 3)));
02881
02882
02883
02884 static const char *referstatus2str(enum referstatus rstatus)
02885 {
02886 return map_x_s(referstatusstrings, rstatus, "");
02887 }
02888
02889
02890
02891
02892 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
02893 {
02894 if (p->initreq.headers)
02895 ast_debug(1, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
02896 else
02897 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
02898
02899 copy_request(&p->initreq, req);
02900 parse_request(&p->initreq);
02901 if (req->debug)
02902 ast_verbose("Initreq: %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
02903 }
02904
02905
02906 static void sip_alreadygone(struct sip_pvt *dialog)
02907 {
02908 ast_debug(3, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
02909 dialog->alreadygone = 1;
02910 }
02911
02912
02913 static int proxy_update(struct sip_proxy *proxy)
02914 {
02915
02916
02917 if (!inet_aton(proxy->name, &proxy->ip.sin_addr)) {
02918
02919
02920 if (ast_get_ip_or_srv(&proxy->ip, proxy->name, global_srvlookup ? "_sip._udp" : NULL) < 0) {
02921 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", proxy->name);
02922 return FALSE;
02923 }
02924 }
02925 proxy->last_dnsupdate = time(NULL);
02926 return TRUE;
02927 }
02928
02929
02930
02931
02932
02933 static int port_str2int(const char *pt, unsigned int standard)
02934 {
02935 int port = standard;
02936 if (ast_strlen_zero(pt) || (sscanf(pt, "%30d", &port) != 1) || (port < 1) || (port > 65535)) {
02937 port = standard;
02938 }
02939
02940 return port;
02941 }
02942
02943
02944 static struct sip_proxy *proxy_allocate(char *name, char *port, int force)
02945 {
02946 struct sip_proxy *proxy;
02947
02948 if (ast_strlen_zero(name)) {
02949 return NULL;
02950 }
02951
02952 proxy = ao2_alloc(sizeof(*proxy), NULL);
02953 if (!proxy)
02954 return NULL;
02955 proxy->force = force;
02956 ast_copy_string(proxy->name, name, sizeof(proxy->name));
02957 proxy->ip.sin_port = htons(port_str2int(port, STANDARD_SIP_PORT));
02958 proxy_update(proxy);
02959 return proxy;
02960 }
02961
02962
02963 static struct sip_proxy *obproxy_get(struct sip_pvt *dialog, struct sip_peer *peer)
02964 {
02965 if (peer && peer->outboundproxy) {
02966 if (sipdebug)
02967 ast_debug(1, "OBPROXY: Applying peer OBproxy to this call\n");
02968 append_history(dialog, "OBproxy", "Using peer obproxy %s", peer->outboundproxy->name);
02969 return peer->outboundproxy;
02970 }
02971 if (global_outboundproxy.name[0]) {
02972 if (sipdebug)
02973 ast_debug(1, "OBPROXY: Applying global OBproxy to this call\n");
02974 append_history(dialog, "OBproxy", "Using global obproxy %s", global_outboundproxy.name);
02975 return &global_outboundproxy;
02976 }
02977 if (sipdebug)
02978 ast_debug(1, "OBPROXY: Not applying OBproxy to this call\n");
02979 return NULL;
02980 }
02981
02982
02983
02984
02985
02986
02987
02988 static int method_match(enum sipmethod id, const char *name)
02989 {
02990 int len = strlen(sip_methods[id].text);
02991 int l_name = name ? strlen(name) : 0;
02992
02993 return (l_name >= len && name[len] < 33 &&
02994 !strncasecmp(sip_methods[id].text, name, len));
02995 }
02996
02997
02998 static int find_sip_method(const char *msg)
02999 {
03000 int i, res = 0;
03001
03002 if (ast_strlen_zero(msg))
03003 return 0;
03004 for (i = 1; i < ARRAY_LEN(sip_methods) && !res; i++) {
03005 if (method_match(i, msg))
03006 res = sip_methods[i].id;
03007 }
03008 return res;
03009 }
03010
03011
03012 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
03013 {
03014 char *next, *sep;
03015 char *temp;
03016 unsigned int profile = 0;
03017 int i, found;
03018
03019 if (ast_strlen_zero(supported) )
03020 return 0;
03021 temp = ast_strdupa(supported);
03022
03023 if (sipdebug)
03024 ast_debug(3, "Begin: parsing SIP \"Supported: %s\"\n", supported);
03025
03026 for (next = temp; next; next = sep) {
03027 found = FALSE;
03028 if ( (sep = strchr(next, ',')) != NULL)
03029 *sep++ = '\0';
03030 next = ast_skip_blanks(next);
03031 if (sipdebug)
03032 ast_debug(3, "Found SIP option: -%s-\n", next);
03033 for (i = 0; i < ARRAY_LEN(sip_options); i++) {
03034 if (!strcasecmp(next, sip_options[i].text)) {
03035 profile |= sip_options[i].id;
03036 found = TRUE;
03037 if (sipdebug)
03038 ast_debug(3, "Matched SIP option: %s\n", next);
03039 break;
03040 }
03041 }
03042
03043
03044
03045
03046
03047 if (!found)
03048 profile |= SIP_OPT_UNKNOWN;
03049
03050 if (!found && sipdebug) {
03051 if (!strncasecmp(next, "x-", 2))
03052 ast_debug(3, "Found private SIP option, not supported: %s\n", next);
03053 else
03054 ast_debug(3, "Found no match for SIP option: %s (Please file bug report!)\n", next);
03055 }
03056 }
03057
03058 if (pvt)
03059 pvt->sipoptions = profile;
03060 return profile;
03061 }
03062
03063
03064 static inline int sip_debug_test_addr(const struct sockaddr_in *addr)
03065 {
03066 if (!sipdebug)
03067 return 0;
03068 if (debugaddr.sin_addr.s_addr) {
03069 if (((ntohs(debugaddr.sin_port) != 0)
03070 && (debugaddr.sin_port != addr->sin_port))
03071 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
03072 return 0;
03073 }
03074 return 1;
03075 }
03076
03077
03078 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
03079 {
03080 if (p->outboundproxy)
03081 return &p->outboundproxy->ip;
03082
03083 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
03084 }
03085
03086
03087 static const char *sip_nat_mode(const struct sip_pvt *p)
03088 {
03089 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
03090 }
03091
03092
03093 static inline int sip_debug_test_pvt(struct sip_pvt *p)
03094 {
03095 if (!sipdebug)
03096 return 0;
03097 return sip_debug_test_addr(sip_real_dst(p));
03098 }
03099
03100 static int get_transport_str2enum(const char *transport)
03101 {
03102 int res = 0;
03103
03104 if (ast_strlen_zero(transport)) {
03105 return res;
03106 }
03107
03108 if (!strcasecmp(transport, "udp")) {
03109 res |= SIP_TRANSPORT_UDP;
03110 }
03111 if (!strcasecmp(transport, "tcp")) {
03112 res |= SIP_TRANSPORT_TCP;
03113 }
03114 if (!strcasecmp(transport, "tls")) {
03115 res |= SIP_TRANSPORT_TLS;
03116 }
03117
03118 return res;
03119 }
03120
03121 static inline const char *get_transport_list(struct sip_peer *peer) {
03122 switch (peer->transports) {
03123 case SIP_TRANSPORT_UDP:
03124 return "UDP";
03125 case SIP_TRANSPORT_TCP:
03126 return "TCP";
03127 case SIP_TRANSPORT_TLS:
03128 return "TLS";
03129 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TCP:
03130 return "TCP,UDP";
03131 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TLS:
03132 return "TLS,UDP";
03133 case SIP_TRANSPORT_TCP | SIP_TRANSPORT_TLS:
03134 return "TLS,TCP";
03135 default:
03136 return peer->transports ?
03137 "TLS,TCP,UDP" : "UNKNOWN";
03138 }
03139 }
03140
03141 static inline const char *get_transport(enum sip_transport t)
03142 {
03143 switch (t) {
03144 case SIP_TRANSPORT_UDP:
03145 return "UDP";
03146 case SIP_TRANSPORT_TCP:
03147 return "TCP";
03148 case SIP_TRANSPORT_TLS:
03149 return "TLS";
03150 }
03151
03152 return "UNKNOWN";
03153 }
03154
03155 static inline const char *get_transport_pvt(struct sip_pvt *p)
03156 {
03157 if (p->outboundproxy && p->outboundproxy->transport) {
03158 set_socket_transport(&p->socket, p->outboundproxy->transport);
03159 }
03160
03161 return get_transport(p->socket.type);
03162 }
03163
03164
03165
03166
03167
03168
03169 static int __sip_xmit(struct sip_pvt *p, struct ast_str *data, int len)
03170 {
03171 int res = 0;
03172 const struct sockaddr_in *dst = sip_real_dst(p);
03173
03174 ast_debug(1, "Trying to put '%.11s' onto %s socket destined for %s:%d\n", data->str, get_transport_pvt(p), ast_inet_ntoa(dst->sin_addr), htons(dst->sin_port));
03175
03176 if (sip_prepare_socket(p) < 0)
03177 return XMIT_ERROR;
03178
03179 if (p->socket.type == SIP_TRANSPORT_UDP) {
03180 res = sendto(p->socket.fd, data->str, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
03181 } else if (p->socket.tcptls_session) {
03182 res = sip_tcptls_write(p->socket.tcptls_session, data->str, len);
03183 } else {
03184 ast_debug(2, "Socket type is TCP but no tcptls_session is present to write to\n");
03185 return XMIT_ERROR;
03186 }
03187
03188 if (res == -1) {
03189 switch (errno) {
03190 case EBADF:
03191 case EHOSTUNREACH:
03192 case ENETDOWN:
03193 case ENETUNREACH:
03194 case ECONNREFUSED:
03195 res = XMIT_ERROR;
03196 }
03197 }
03198 if (res != len)
03199 ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s:%d returned %d: %s\n", data, len, ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), res, strerror(errno));
03200
03201 return res;
03202 }
03203
03204
03205 static void build_via(struct sip_pvt *p)
03206 {
03207
03208 const char *rport = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_RFC3581 ? ";rport" : "";
03209
03210
03211 snprintf(p->via, sizeof(p->via), "SIP/2.0/%s %s:%d;branch=z9hG4bK%08x%s",
03212 get_transport_pvt(p),
03213 ast_inet_ntoa(p->ourip.sin_addr),
03214 ntohs(p->ourip.sin_port), (int) p->branch, rport);
03215 }
03216
03217
03218
03219
03220
03221
03222
03223
03224 static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us, struct sip_pvt *p)
03225 {
03226 struct sockaddr_in theirs;
03227
03228
03229
03230
03231
03232
03233
03234
03235
03236
03237
03238
03239 int want_remap;
03240
03241 *us = internip;
03242
03243 ast_ouraddrfor(them, &us->sin_addr);
03244 theirs.sin_addr = *them;
03245
03246 want_remap = localaddr &&
03247 (externip.sin_addr.s_addr || stunaddr.sin_addr.s_addr) &&
03248 ast_apply_ha(localaddr, &theirs) == AST_SENSE_ALLOW ;
03249
03250 if (want_remap &&
03251 (!global_matchexterniplocally || !ast_apply_ha(localaddr, us)) ) {
03252
03253 if (externexpire && time(NULL) >= externexpire) {
03254 if (stunaddr.sin_addr.s_addr) {
03255 ast_stun_request(sipsock, &stunaddr, NULL, &externip);
03256 } else {
03257 if (ast_parse_arg(externhost, PARSE_INADDR, &externip))
03258 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
03259 }
03260 externexpire = time(NULL) + externrefresh;
03261 }
03262 if (externip.sin_addr.s_addr)
03263 *us = externip;
03264 else
03265 ast_log(LOG_WARNING, "stun failed\n");
03266 ast_debug(1, "Target address %s is not local, substituting externip\n",
03267 ast_inet_ntoa(*(struct in_addr *)&them->s_addr));
03268 } else if (p) {
03269
03270 switch (p->socket.type) {
03271 case SIP_TRANSPORT_TCP:
03272 if (sip_tcp_desc.local_address.sin_addr.s_addr) {
03273 *us = sip_tcp_desc.local_address;
03274 } else {
03275 us->sin_port = sip_tcp_desc.local_address.sin_port;
03276 }
03277 break;
03278 case SIP_TRANSPORT_TLS:
03279 if (sip_tls_desc.local_address.sin_addr.s_addr) {
03280 *us = sip_tls_desc.local_address;
03281 } else {
03282 us->sin_port = sip_tls_desc.local_address.sin_port;
03283 }
03284 break;
03285 case SIP_TRANSPORT_UDP:
03286
03287 default:
03288 if (bindaddr.sin_addr.s_addr) {
03289 *us = bindaddr;
03290 }
03291 }
03292 } else if (bindaddr.sin_addr.s_addr) {
03293 *us = bindaddr;
03294 }
03295 ast_debug(3, "Setting SIP_TRANSPORT_%s with address %s:%d\n", get_transport(p->socket.type), ast_inet_ntoa(us->sin_addr), ntohs(us->sin_port));
03296 }
03297
03298
03299 static __attribute__((format(printf, 2, 0))) void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
03300 {
03301 char buf[80], *c = buf;
03302 struct sip_history *hist;
03303 int l;
03304
03305 vsnprintf(buf, sizeof(buf), fmt, ap);
03306 strsep(&c, "\r\n");
03307 l = strlen(buf) + 1;
03308 if (!(hist = ast_calloc(1, sizeof(*hist) + l)))
03309 return;
03310 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
03311 ast_free(hist);
03312 return;
03313 }
03314 memcpy(hist->event, buf, l);
03315 if (p->history_entries == MAX_HISTORY_ENTRIES) {
03316 struct sip_history *oldest;
03317 oldest = AST_LIST_REMOVE_HEAD(p->history, list);
03318 p->history_entries--;
03319 ast_free(oldest);
03320 }
03321 AST_LIST_INSERT_TAIL(p->history, hist, list);
03322 p->history_entries++;
03323 }
03324
03325
03326 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
03327 {
03328 va_list ap;
03329
03330 if (!p)
03331 return;
03332
03333 if (!p->do_history && !recordhistory && !dumphistory)
03334 return;
03335
03336 va_start(ap, fmt);
03337 append_history_va(p, fmt, ap);
03338 va_end(ap);
03339
03340 return;
03341 }
03342
03343
03344 static int retrans_pkt(const void *data)
03345 {
03346 struct sip_pkt *pkt = (struct sip_pkt *)data, *prev, *cur = NULL;
03347 int reschedule = DEFAULT_RETRANS;
03348 int xmitres = 0;
03349
03350
03351 sip_pvt_lock(pkt->owner);
03352
03353 if (pkt->retrans < MAX_RETRANS) {
03354 pkt->retrans++;
03355 if (!pkt->timer_t1) {
03356 if (sipdebug)
03357 ast_debug(4, "SIP TIMER: Not rescheduling id #%d:%s (Method %d) (No timer T1)\n", pkt->retransid, sip_methods[pkt->method].text, pkt->method);
03358 } else {
03359 int siptimer_a;
03360
03361 if (sipdebug)
03362 ast_debug(4, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
03363 if (!pkt->timer_a)
03364 pkt->timer_a = 2 ;
03365 else
03366 pkt->timer_a = 2 * pkt->timer_a;
03367
03368
03369 siptimer_a = pkt->timer_t1 * pkt->timer_a;
03370 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
03371 siptimer_a = 4000;
03372
03373
03374 reschedule = siptimer_a;
03375 ast_debug(4, "** SIP timers: Rescheduling retransmission %d to %d ms (t1 %d ms (Retrans id #%d)) \n", pkt->retrans +1, siptimer_a, pkt->timer_t1, pkt->retransid);
03376 }
03377
03378 if (sip_debug_test_pvt(pkt->owner)) {
03379 const struct sockaddr_in *dst = sip_real_dst(pkt->owner);
03380 ast_verbose("Retransmitting #%d (%s) to %s:%d:\n%s\n---\n",
03381 pkt->retrans, sip_nat_mode(pkt->owner),
03382 ast_inet_ntoa(dst->sin_addr),
03383 ntohs(dst->sin_port), pkt->data->str);
03384 }
03385
03386 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data->str);
03387 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
03388 sip_pvt_unlock(pkt->owner);
03389 if (xmitres == XMIT_ERROR)
03390 ast_log(LOG_WARNING, "Network error on retransmit in dialog %s\n", pkt->owner->callid);
03391 else
03392 return reschedule;
03393 }
03394
03395 if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
03396 if (pkt->is_fatal || sipdebug)
03397 ast_log(LOG_WARNING, "Maximum retries exceeded on transmission %s for seqno %d (%s %s) -- See doc/sip-retransmit.txt.\n",
03398 pkt->owner->callid, pkt->seqno,
03399 pkt->is_fatal ? "Critical" : "Non-critical", pkt->is_resp ? "Response" : "Request");
03400 } else if (pkt->method == SIP_OPTIONS && sipdebug) {
03401 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) -- See doc/sip-retransmit.txt.\n", pkt->owner->callid);
03402
03403 }
03404 if (xmitres == XMIT_ERROR) {
03405 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission on Call ID %s\n", pkt->owner->callid);
03406 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03407 } else
03408 append_history(pkt->owner, "MaxRetries", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03409
03410 pkt->retransid = -1;
03411
03412 if (pkt->is_fatal) {
03413 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
03414 sip_pvt_unlock(pkt->owner);
03415 usleep(1);
03416 sip_pvt_lock(pkt->owner);
03417 }
03418
03419 if (pkt->owner->owner && !pkt->owner->owner->hangupcause)
03420 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
03421
03422 if (pkt->owner->owner) {
03423 sip_alreadygone(pkt->owner);
03424 ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet (see doc/sip-retransmit.txt).\n", pkt->owner->callid);
03425 ast_queue_hangup_with_cause(pkt->owner->owner, AST_CAUSE_PROTOCOL_ERROR);
03426 ast_channel_unlock(pkt->owner->owner);
03427 } else {
03428
03429
03430
03431 if (pkt->method != SIP_OPTIONS && pkt->method != SIP_REGISTER) {
03432 pkt->owner->needdestroy = 1;
03433 sip_alreadygone(pkt->owner);
03434 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
03435 }
03436 }
03437 }
03438
03439 if (pkt->method == SIP_BYE) {
03440
03441 if (pkt->owner->owner)
03442 ast_channel_unlock(pkt->owner->owner);
03443 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
03444 pkt->owner->needdestroy = 1;
03445 }
03446
03447
03448 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
03449 if (cur == pkt) {
03450 UNLINK(cur, pkt->owner->packets, prev);
03451 sip_pvt_unlock(pkt->owner);
03452 if (pkt->owner)
03453 pkt->owner = dialog_unref(pkt->owner,"pkt is being freed, its dialog ref is dead now");
03454 if (pkt->data)
03455 ast_free(pkt->data);
03456 pkt->data = NULL;
03457 ast_free(pkt);
03458 return 0;
03459 }
03460 }
03461
03462 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
03463 sip_pvt_unlock(pkt->owner);
03464 return 0;
03465 }
03466
03467
03468
03469
03470 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)
03471 {
03472 struct sip_pkt *pkt = NULL;
03473 int siptimer_a = DEFAULT_RETRANS;
03474 int xmitres = 0;
03475 int respid;
03476
03477 if (sipmethod == SIP_INVITE) {
03478
03479 p->pendinginvite = seqno;
03480 }
03481
03482
03483
03484
03485 if (!(p->socket.type & SIP_TRANSPORT_UDP)) {
03486 xmitres = __sip_xmit(p, data, len);
03487 if (xmitres == XMIT_ERROR) {
03488 append_history(p, "XmitErr", "%s", fatal ? "(Critical)" : "(Non-critical)");
03489 return AST_FAILURE;
03490 } else
03491 return AST_SUCCESS;
03492 }
03493
03494 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1)))
03495 return AST_FAILURE;
03496
03497 if (!(pkt->data = ast_str_create(len))) {
03498 ast_free(pkt);
03499 return AST_FAILURE;
03500 }
03501 ast_str_set(&pkt->data, 0, "%s%s", data->str, "\0");
03502 pkt->packetlen = len;
03503
03504 pkt->method = sipmethod;
03505 pkt->seqno = seqno;
03506 pkt->is_resp = resp;
03507 pkt->is_fatal = fatal;
03508 pkt->owner = dialog_ref(p, "__sip_reliable_xmit: setting pkt->owner");
03509 pkt->next = p->packets;
03510 p->packets = pkt;
03511 if (resp) {
03512
03513 if (sscanf(ast_str_buffer(pkt->data), "SIP/2.0 %30u", &respid) == 1) {
03514 pkt->response_code = respid;
03515 }
03516 }
03517 pkt->timer_t1 = p->timer_t1;
03518 pkt->retransid = -1;
03519 if (pkt->timer_t1)
03520 siptimer_a = pkt->timer_t1 * 2;
03521
03522
03523 AST_SCHED_REPLACE_VARIABLE(pkt->retransid, sched, siptimer_a, retrans_pkt, pkt, 1);
03524 if (sipdebug)
03525 ast_debug(4, "*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->retransid);
03526
03527 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
03528
03529 if (xmitres == XMIT_ERROR) {
03530 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03531 ast_log(LOG_ERROR, "Serious Network Trouble; __sip_xmit returns error for pkt data\n");
03532 AST_SCHED_DEL(sched, pkt->retransid);
03533 p->packets = pkt->next;
03534 pkt->owner = dialog_unref(pkt->owner,"pkt is being freed, its dialog ref is dead now");
03535 ast_free(pkt->data);
03536 ast_free(pkt);
03537 return AST_FAILURE;
03538 } else {
03539 return AST_SUCCESS;
03540 }
03541 }
03542
03543
03544
03545
03546
03547
03548 static int __sip_autodestruct(const void *data)
03549 {
03550 struct sip_pvt *p = (struct sip_pvt *)data;
03551
03552
03553 if (p->subscribed) {
03554 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
03555 p->subscribed = NONE;
03556 append_history(p, "Subscribestatus", "timeout");
03557 ast_debug(3, "Re-scheduled destruction of SIP subscription %s\n", p->callid ? p->callid : "<unknown>");
03558 return 10000;
03559 }
03560
03561
03562 if (p->packets) {
03563 if (!p->needdestroy) {
03564 char method_str[31];
03565 ast_debug(3, "Re-scheduled destruction of SIP call %s\n", p->callid ? p->callid : "<unknown>");
03566 append_history(p, "ReliableXmit", "timeout");
03567 if (sscanf(p->lastmsg, "Tx: %30s", method_str) == 1 || sscanf(p->lastmsg, "Rx: %30s", method_str) == 1) {
03568 if (method_match(SIP_CANCEL, method_str) || method_match(SIP_BYE, method_str)) {
03569 p->needdestroy = 1;
03570 }
03571 }
03572 return 10000;
03573 } else {
03574
03575 __sip_pretend_ack(p);
03576 }
03577 }
03578
03579 if (p->subscribed == MWI_NOTIFICATION)
03580 if (p->relatedpeer)
03581 p->relatedpeer = unref_peer(p->relatedpeer, "__sip_autodestruct: unref peer p->relatedpeer");
03582
03583
03584 p->autokillid = -1;
03585
03586 if (p->owner) {
03587 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
03588 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
03589 } else if (p->refer && !p->alreadygone) {
03590 ast_debug(3, "Finally hanging up channel after transfer: %s\n", p->callid);
03591 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03592 append_history(p, "ReferBYE", "Sending BYE on transferer call leg %s", p->callid);
03593 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03594 } else {
03595 append_history(p, "AutoDestroy", "%s", p->callid);
03596 ast_debug(3, "Auto destroying SIP dialog '%s'\n", p->callid);
03597 dialog_unlink_all(p, TRUE, TRUE);
03598
03599
03600
03601 }
03602 dialog_unref(p, "The ref to a dialog passed to this sched callback is going out of scope; unref it.");
03603 return 0;
03604 }
03605
03606
03607 static void sip_scheddestroy(struct sip_pvt *p, int ms)
03608 {
03609 if (ms < 0) {
03610 if (p->timer_t1 == 0) {
03611 p->timer_t1 = global_t1;
03612 p->timer_b = global_timer_b;
03613 }
03614 ms = p->timer_t1 * 64;
03615 }
03616 if (sip_debug_test_pvt(p))
03617 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
03618 if (sip_cancel_destroy(p))
03619 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03620
03621 if (p->do_history)
03622 append_history(p, "SchedDestroy", "%d ms", ms);
03623 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, dialog_ref(p, "setting ref as passing into ast_sched_add for __sip_autodestruct"));
03624
03625 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_schedid > 0)
03626 stop_session_timer(p);
03627 }
03628
03629
03630
03631
03632
03633 static int sip_cancel_destroy(struct sip_pvt *p)
03634 {
03635 int res = 0;
03636 if (p->autokillid > -1) {
03637 int res3;
03638
03639 if (!(res3 = ast_sched_del(sched, p->autokillid))) {
03640 append_history(p, "CancelDestroy", "");
03641 p->autokillid = -1;
03642 dialog_unref(p, "dialog unrefd because autokillid is de-sched'd");
03643 }
03644 }
03645 return res;
03646 }
03647
03648
03649
03650 static int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
03651 {
03652 struct sip_pkt *cur, *prev = NULL;
03653 const char *msg = "Not Found";
03654 int res = FALSE;
03655
03656
03657
03658
03659
03660
03661 if (p->outboundproxy && !p->outboundproxy->force){
03662 ref_proxy(p, NULL);
03663 }
03664
03665 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
03666 if (cur->seqno != seqno || cur->is_resp != resp)
03667 continue;
03668 if (cur->is_resp || cur->method == sipmethod) {
03669 res = TRUE;
03670 msg = "Found";
03671 if (!resp && (seqno == p->pendinginvite)) {
03672 ast_debug(1, "Acked pending invite %d\n", p->pendinginvite);
03673 p->pendinginvite = 0;
03674 }
03675 if (cur->retransid > -1) {
03676 if (sipdebug)
03677 ast_debug(4, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
03678 }
03679
03680
03681
03682
03683
03684
03685
03686
03687
03688
03689
03690
03691
03692
03693
03694
03695 while (cur->retransid > -1 && ast_sched_del(sched, cur->retransid)) {
03696 sip_pvt_unlock(p);
03697 usleep(1);
03698 sip_pvt_lock(p);
03699 }
03700 UNLINK(cur, p->packets, prev);
03701 dialog_unref(cur->owner, "unref pkt cur->owner dialog from sip ack before freeing pkt");
03702 if (cur->data)
03703 ast_free(cur->data);
03704 ast_free(cur);
03705 break;
03706 }
03707 }
03708 ast_debug(1, "Stopping retransmission on '%s' of %s %d: Match %s\n",
03709 p->callid, resp ? "Response" : "Request", seqno, msg);
03710 return res;
03711 }
03712
03713
03714
03715 static void __sip_pretend_ack(struct sip_pvt *p)
03716 {
03717 struct sip_pkt *cur = NULL;
03718
03719 while (p->packets) {
03720 int method;
03721 if (cur == p->packets) {
03722 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
03723 return;
03724 }
03725 cur = p->packets;
03726 method = (cur->method) ? cur->method : find_sip_method(cur->data->str);
03727 __sip_ack(p, cur->seqno, cur->is_resp, method);
03728 }
03729 }
03730
03731
03732 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
03733 {
03734 struct sip_pkt *cur;
03735 int res = FALSE;
03736
03737 for (cur = p->packets; cur; cur = cur->next) {
03738 if (cur->seqno == seqno && cur->is_resp == resp &&
03739 (cur->is_resp || method_match(sipmethod, cur->data->str))) {
03740
03741 if (cur->retransid > -1) {
03742 if (sipdebug)
03743 ast_debug(4, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
03744 }
03745 AST_SCHED_DEL(sched, cur->retransid);
03746 res = TRUE;
03747 break;
03748 }
03749 }
03750 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");
03751 return res;
03752 }
03753
03754
03755
03756 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
03757 {
03758 copy_request(dst, src);
03759 parse_request(dst);
03760 }
03761
03762
03763 static void add_blank(struct sip_request *req)
03764 {
03765 if (!req->lines) {
03766
03767 ast_str_append(&req->data, 0, "\r\n");
03768 req->len = req->data->used;
03769 }
03770 }
03771
03772 static int send_provisional_keepalive_full(struct sip_pvt *pvt, int with_sdp)
03773 {
03774 const char *msg = NULL;
03775
03776 if (!pvt->last_provisional || !strncasecmp(pvt->last_provisional, "100", 3)) {
03777 msg = "183 Session Progress";
03778 }
03779
03780 if (pvt->invitestate < INV_COMPLETED) {
03781 if (with_sdp) {
03782 transmit_response_with_sdp(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq, XMIT_UNRELIABLE, FALSE);
03783 } else {
03784 transmit_response(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq);
03785 }
03786 return PROVIS_KEEPALIVE_TIMEOUT;
03787 }
03788
03789 return 0;
03790 }
03791
03792 static int send_provisional_keepalive(const void *data) {
03793 struct sip_pvt *pvt = (struct sip_pvt *) data;
03794
03795 return send_provisional_keepalive_full(pvt, 0);
03796 }
03797
03798 static int send_provisional_keepalive_with_sdp(const void *data) {
03799 struct sip_pvt *pvt = (void *)data;
03800
03801 return send_provisional_keepalive_full(pvt, 1);
03802 }
03803
03804 static void update_provisional_keepalive(struct sip_pvt *pvt, int with_sdp)
03805 {
03806 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"));
03807
03808 pvt->provisional_keepalive_sched_id = ast_sched_add(sched, PROVIS_KEEPALIVE_TIMEOUT,
03809 with_sdp ? send_provisional_keepalive_with_sdp : send_provisional_keepalive, dialog_ref(pvt, "Increment refcount to pass dialog pointer to sched callback"));
03810 }
03811
03812
03813 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
03814 {
03815 int res;
03816
03817 add_blank(req);
03818 if (sip_debug_test_pvt(p)) {
03819 const struct sockaddr_in *dst = sip_real_dst(p);
03820
03821 ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
03822 reliable ? "Reliably " : "", sip_nat_mode(p),
03823 ast_inet_ntoa(dst->sin_addr),
03824 ntohs(dst->sin_port), req->data->str);
03825 }
03826 if (p->do_history) {
03827 struct sip_request tmp = { .rlPart1 = 0, };
03828 parse_copy(&tmp, req);
03829 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data->str, get_header(&tmp, "CSeq"),
03830 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? REQ_OFFSET_TO_STR(&tmp, rlPart2) : sip_methods[tmp.method].text);
03831 ast_free(tmp.data);
03832 }
03833
03834
03835 if (p->initreq.method == SIP_INVITE && reliable == XMIT_CRITICAL) {
03836 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"));
03837 }
03838
03839 res = (reliable) ?
03840 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
03841 __sip_xmit(p, req->data, req->len);
03842 ast_free(req->data);
03843 req->data = NULL;
03844 if (res > 0)
03845 return 0;
03846 return res;
03847 }
03848
03849
03850 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
03851 {
03852 int res;
03853
03854
03855
03856
03857 if (p->outboundproxy) {
03858 p->sa = p->outboundproxy->ip;
03859 }
03860
03861 add_blank(req);
03862 if (sip_debug_test_pvt(p)) {
03863 if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
03864 ast_verbose("%sTransmitting (NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port), req->data->str);
03865 else
03866 ast_verbose("%sTransmitting (no NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(p->sa.sin_addr), ntohs(p->sa.sin_port), req->data->str);
03867 }
03868 if (p->do_history) {
03869 struct sip_request tmp = { .rlPart1 = 0, };
03870 parse_copy(&tmp, req);
03871 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data->str, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
03872 ast_free(tmp.data);
03873 }
03874 res = (reliable) ?
03875 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
03876 __sip_xmit(p, req->data, req->len);
03877 if (req->data) {
03878 ast_free(req->data);
03879 req->data = NULL;
03880 }
03881 return res;
03882 }
03883
03884
03885 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen)
03886 {
03887 int res = -1;
03888 enum ast_t38_state state = T38_STATE_UNAVAILABLE;
03889 struct sip_pvt *p = (struct sip_pvt *) chan->tech_pvt;
03890
03891 switch (option) {
03892 case AST_OPTION_T38_STATE:
03893
03894 if (*datalen != sizeof(enum ast_t38_state)) {
03895 ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option. Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
03896 return -1;
03897 }
03898
03899 sip_pvt_lock(p);
03900
03901
03902 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT)) {
03903 switch (p->t38.state) {
03904 case T38_LOCAL_REINVITE:
03905 case T38_PEER_REINVITE:
03906 state = T38_STATE_NEGOTIATING;
03907 break;
03908 case T38_ENABLED:
03909 state = T38_STATE_NEGOTIATED;
03910 break;
03911 default:
03912 state = T38_STATE_UNKNOWN;
03913 }
03914 }
03915
03916 sip_pvt_unlock(p);
03917
03918 *((enum ast_t38_state *) data) = state;
03919 res = 0;
03920
03921 break;
03922 default:
03923 break;
03924 }
03925
03926 return res;
03927 }
03928
03929
03930
03931
03932
03933 static const char *find_closing_quote(const char *start, const char *lim)
03934 {
03935 char last_char = '\0';
03936 const char *s;
03937 for (s = start; *s && s != lim; last_char = *s++) {
03938 if (*s == '"' && last_char != '\\')
03939 break;
03940 }
03941 return s;
03942 }
03943
03944
03945
03946
03947
03948
03949
03950
03951
03952
03953
03954
03955
03956 static char *get_in_brackets(char *tmp)
03957 {
03958 const char *parse = tmp;
03959 char *first_bracket;
03960
03961
03962
03963
03964
03965 while ( (first_bracket = strchr(parse, '<')) ) {
03966 char *first_quote = strchr(parse, '"');
03967
03968 if (!first_quote || first_quote > first_bracket)
03969 break;
03970
03971 parse = find_closing_quote(first_quote + 1, NULL);
03972 if (!*parse) {
03973
03974 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
03975 break;
03976 }
03977 parse++;
03978 }
03979 if (first_bracket) {
03980 char *second_bracket = strchr(first_bracket + 1, '>');
03981 if (second_bracket) {
03982 *second_bracket = '\0';
03983 tmp = first_bracket + 1;
03984 } else {
03985 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
03986 }
03987 }
03988
03989 return tmp;
03990 }
03991
03992
03993
03994
03995
03996
03997
03998
03999
04000
04001
04002
04003
04004
04005
04006
04007
04008
04009
04010 static int parse_uri(char *uri, const char *scheme,
04011 char **ret_name, char **pass, char **domain, char **port, char **options, char **transport)
04012 {
04013 char *name = NULL;
04014 int error = 0;
04015
04016
04017 if (pass)
04018 *pass = "";
04019 if (port)
04020 *port = "";
04021 if (scheme) {
04022 int l;
04023 char *scheme2 = ast_strdupa(scheme);
04024 char *cur = strsep(&scheme2, ",");
04025 for (; !ast_strlen_zero(cur); cur = strsep(&scheme2, ",")) {
04026 l = strlen(cur);
04027 if (!strncasecmp(uri, cur, l)) {
04028 uri += l;
04029 break;
04030 }
04031 }
04032 if (ast_strlen_zero(cur)) {
04033 ast_debug(1, "No supported scheme found in '%s' using the scheme[s] %s\n", uri, scheme);
04034 error = -1;
04035 }
04036 }
04037 if (transport) {
04038 char *t, *type = "";
04039 *transport = "";
04040 if ((t = strstr(uri, "transport="))) {
04041 strsep(&t, "=");
04042 if ((type = strsep(&t, ";"))) {
04043 *transport = type;
04044 }
04045 }
04046 }
04047
04048 if (!domain) {
04049
04050
04051
04052 } else {
04053
04054
04055
04056 char *c, *dom = "";
04057
04058 if ((c = strchr(uri, '@')) == NULL) {
04059
04060 dom = uri;
04061 name = "";
04062 } else {
04063 *c++ = '\0';
04064 dom = c;
04065 name = uri;
04066 }
04067
04068
04069 dom = strsep(&dom, ";");
04070 name = strsep(&name, ";");
04071
04072 if (port && (c = strchr(dom, ':'))) {
04073 *c++ = '\0';
04074 *port = c;
04075 }
04076 if (pass && (c = strchr(name, ':'))) {
04077 *c++ = '\0';
04078 *pass = c;
04079 }
04080 *domain = dom;
04081 }
04082 if (ret_name)
04083 *ret_name = name;
04084 if (options)
04085 *options = uri ? uri : "";
04086
04087 return error;
04088 }
04089
04090
04091 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
04092 {
04093 struct sip_pvt *p = chan->tech_pvt;
04094
04095 if (subclass != AST_HTML_URL)
04096 return -1;
04097
04098 ast_string_field_build(p, url, "<%s>;mode=active", data);
04099
04100 if (sip_debug_test_pvt(p))
04101 ast_debug(1, "Send URL %s, state = %d!\n", data, chan->_state);
04102
04103 switch (chan->_state) {
04104 case AST_STATE_RING:
04105 transmit_response(p, "100 Trying", &p->initreq);
04106 break;
04107 case AST_STATE_RINGING:
04108 transmit_response(p, "180 Ringing", &p->initreq);
04109 break;
04110 case AST_STATE_UP:
04111 if (!p->pendinginvite) {
04112 transmit_reinvite_with_sdp(p, FALSE, FALSE);
04113 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04114 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04115 }
04116 break;
04117 default:
04118 ast_log(LOG_WARNING, "Don't know how to send URI when state is %d!\n", chan->_state);
04119 }
04120
04121 return 0;
04122 }
04123
04124
04125 static const char *sip_get_callid(struct ast_channel *chan)
04126 {
04127 return chan->tech_pvt ? ((struct sip_pvt *) chan->tech_pvt)->callid : "";
04128 }
04129
04130
04131
04132 static int sip_sendtext(struct ast_channel *ast, const char *text)
04133 {
04134 struct sip_pvt *p = ast->tech_pvt;
04135 int debug = sip_debug_test_pvt(p);
04136
04137 if (debug)
04138 ast_verbose("Sending text %s on %s\n", text, ast->name);
04139 if (!p)
04140 return -1;
04141
04142
04143 if (!text)
04144 return 0;
04145 if (debug)
04146 ast_verbose("Really sending text %s on %s\n", text, ast->name);
04147 transmit_message_with_text(p, text);
04148 return 0;
04149 }
04150
04151
04152
04153
04154
04155
04156 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *defaultuser, const char *fullcontact, const char *useragent, int expirey, int deprecated_username, int lastms)
04157 {
04158 char port[10];
04159 char ipaddr[INET_ADDRSTRLEN];
04160 char regseconds[20];
04161 char *tablename = NULL;
04162 char str_lastms[20];
04163
04164 const char *sysname = ast_config_AST_SYSTEM_NAME;
04165 char *syslabel = NULL;
04166
04167 time_t nowtime = time(NULL) + expirey;
04168 const char *fc = fullcontact ? "fullcontact" : NULL;
04169
04170 int realtimeregs = ast_check_realtime("sipregs");
04171
04172 tablename = realtimeregs ? "sipregs" : "sippeers";
04173
04174
04175 snprintf(str_lastms, sizeof(str_lastms), "%d", lastms);
04176 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
04177 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
04178 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
04179
04180 if (ast_strlen_zero(sysname))
04181 sysname = NULL;
04182 else if (sip_cfg.rtsave_sysname)
04183 syslabel = "regserver";
04184
04185 if (fc) {
04186 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
04187 "port", port, "regseconds", regseconds,
04188 deprecated_username ? "username" : "defaultuser", defaultuser,
04189 "useragent", useragent, "lastms", str_lastms,
04190 fc, fullcontact, syslabel, sysname, SENTINEL);
04191 } else {
04192 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
04193 "port", port, "regseconds", regseconds,
04194 "useragent", useragent, "lastms", str_lastms,
04195 deprecated_username ? "username" : "defaultuser", defaultuser,
04196 syslabel, sysname, SENTINEL);
04197 }
04198 }
04199
04200
04201 static void register_peer_exten(struct sip_peer *peer, int onoff)
04202 {
04203 char multi[256];
04204 char *stringp, *ext, *context;
04205 struct pbx_find_info q = { .stacklen = 0 };
04206
04207
04208
04209
04210
04211 if (ast_strlen_zero(global_regcontext))
04212 return;
04213
04214 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
04215 stringp = multi;
04216 while ((ext = strsep(&stringp, "&"))) {
04217 if ((context = strchr(ext, '@'))) {
04218 *context++ = '\0';
04219 if (!ast_context_find(context)) {
04220 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
04221 continue;
04222 }
04223 } else {
04224 context = global_regcontext;
04225 }
04226 if (onoff) {
04227 if (!ast_exists_extension(NULL, context, ext, 1, NULL)) {
04228 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
04229 ast_strdup(peer->name), ast_free_ptr, "SIP");
04230 }
04231 } else if (pbx_find_extension(NULL, NULL, &q, context, ext, 1, NULL, "", E_MATCH)) {
04232 ast_context_remove_extension(context, ext, 1, NULL);
04233 }
04234 }
04235 }
04236
04237
04238 static void destroy_mailbox(struct sip_mailbox *mailbox)
04239 {
04240 if (mailbox->mailbox)
04241 ast_free(mailbox->mailbox);
04242 if (mailbox->context)
04243 ast_free(mailbox->context);
04244 if (mailbox->event_sub)
04245 ast_event_unsubscribe(mailbox->event_sub);
04246 ast_free(mailbox);
04247 }
04248
04249
04250 static void clear_peer_mailboxes(struct sip_peer *peer)
04251 {
04252 struct sip_mailbox *mailbox;
04253
04254 while ((mailbox = AST_LIST_REMOVE_HEAD(&peer->mailboxes, entry)))
04255 destroy_mailbox(mailbox);
04256 }
04257
04258 static void sip_destroy_peer_fn(void *peer)
04259 {
04260 sip_destroy_peer(peer);
04261 }
04262
04263
04264 static void sip_destroy_peer(struct sip_peer *peer)
04265 {
04266 ast_debug(3, "Destroying SIP peer %s\n", peer->name);
04267 if (peer->outboundproxy)
04268 ao2_ref(peer->outboundproxy, -1);
04269 peer->outboundproxy = NULL;
04270
04271
04272 if (peer->call) {
04273 dialog_unlink_all(peer->call, TRUE, TRUE);
04274 peer->call = dialog_unref(peer->call, "peer->call is being unset");
04275 }
04276
04277
04278 if (peer->mwipvt) {
04279 dialog_unlink_all(peer->mwipvt, TRUE, TRUE);
04280 peer->mwipvt = dialog_unref(peer->mwipvt, "unreffing peer->mwipvt");
04281 }
04282
04283 if (peer->chanvars) {
04284 ast_variables_destroy(peer->chanvars);
04285 peer->chanvars = NULL;
04286 }
04287
04288 register_peer_exten(peer, FALSE);
04289 ast_free_ha(peer->ha);
04290 if (peer->selfdestruct)
04291 ast_atomic_fetchadd_int(&apeerobjs, -1);
04292 else if (peer->is_realtime) {
04293 ast_atomic_fetchadd_int(&rpeerobjs, -1);
04294 ast_debug(3, "-REALTIME- peer Destroyed. Name: %s. Realtime Peer objects: %d\n", peer->name, rpeerobjs);
04295 } else
04296 ast_atomic_fetchadd_int(&speerobjs, -1);
04297 clear_realm_authentication(peer->auth);
04298 peer->auth = NULL;
04299 if (peer->dnsmgr)
04300 ast_dnsmgr_release(peer->dnsmgr);
04301 clear_peer_mailboxes(peer);
04302
04303 if (peer->socket.tcptls_session) {
04304 ao2_ref(peer->socket.tcptls_session, -1);
04305 peer->socket.tcptls_session = NULL;
04306 }
04307 }
04308
04309
04310 static void update_peer(struct sip_peer *p, int expire)
04311 {
04312 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
04313 if (sip_cfg.peer_rtupdate &&
04314 (p->is_realtime || rtcachefriends)) {
04315 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, p->useragent, expire, p->deprecated_username, p->lastms);
04316 }
04317 }
04318
04319 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *cfg)
04320 {
04321 struct ast_variable *var = NULL;
04322 struct ast_flags flags = {0};
04323 char *cat = NULL;
04324 const char *insecure;
04325 while ((cat = ast_category_browse(cfg, cat))) {
04326 insecure = ast_variable_retrieve(cfg, cat, "insecure");
04327 set_insecure_flags(&flags, insecure, -1);
04328 if (ast_test_flag(&flags, SIP_INSECURE_PORT)) {
04329 var = ast_category_root(cfg, cat);
04330 break;
04331 }
04332 }
04333 return var;
04334 }
04335
04336 static const char *get_name_from_variable(struct ast_variable *var, const char *newpeername)
04337 {
04338 struct ast_variable *tmp;
04339 for (tmp = var; tmp; tmp = tmp->next) {
04340 if (!newpeername && !strcasecmp(tmp->name, "name"))
04341 newpeername = tmp->value;
04342 }
04343 return newpeername;
04344 }
04345
04346
04347
04348
04349
04350
04351
04352 static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_in *sin, int devstate_only)
04353 {
04354 struct sip_peer *peer;
04355 struct ast_variable *var = NULL;
04356 struct ast_variable *varregs = NULL;
04357 struct ast_variable *tmp;
04358 struct ast_config *peerlist = NULL;
04359 char ipaddr[INET_ADDRSTRLEN];
04360 char portstring[6];
04361 char *cat = NULL;
04362 unsigned short portnum;
04363 int realtimeregs = ast_check_realtime("sipregs");
04364
04365
04366 if (newpeername) {
04367 if (realtimeregs)
04368 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04369
04370 var = ast_load_realtime("sippeers", "name", newpeername, "host", "dynamic", SENTINEL);
04371 if (!var && sin)
04372 var = ast_load_realtime("sippeers", "name", newpeername, "host", ast_inet_ntoa(sin->sin_addr), SENTINEL);
04373 if (!var) {
04374 var = ast_load_realtime("sippeers", "name", newpeername, SENTINEL);
04375
04376
04377
04378
04379
04380
04381 if (var && sin) {
04382 for (tmp = var; tmp; tmp = tmp->next) {
04383 if (!strcasecmp(tmp->name, "host")) {
04384 struct hostent *hp;
04385 struct ast_hostent ahp;
04386 if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
04387
04388 ast_variables_destroy(var);
04389 var = NULL;
04390 }
04391 break;
04392 }
04393 }
04394 }
04395 }
04396 }
04397
04398 if (!var && sin) {
04399 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
04400 portnum = ntohs(sin->sin_port);
04401 sprintf(portstring, "%u", portnum);
04402 var = ast_load_realtime("sippeers", "host", ipaddr, "port", portstring, SENTINEL);
04403 if (var) {
04404 if (realtimeregs) {
04405 newpeername = get_name_from_variable(var, newpeername);
04406 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04407 }
04408 } else {
04409 if (realtimeregs)
04410 varregs = ast_load_realtime("sipregs", "ipaddr", ipaddr, "port", portstring, SENTINEL);
04411 else
04412 var = ast_load_realtime("sippeers", "ipaddr", ipaddr, "port", portstring, SENTINEL);
04413 if (varregs) {
04414 newpeername = get_name_from_variable(varregs, newpeername);
04415 var = ast_load_realtime("sippeers", "name", newpeername, SENTINEL);
04416 }
04417 }
04418 if (!var) {
04419 peerlist = ast_load_realtime_multientry("sippeers", "host", ipaddr, SENTINEL);
04420 if (peerlist) {
04421 var = get_insecure_variable_from_config(peerlist);
04422 if(var) {
04423 if (realtimeregs) {
04424 newpeername = get_name_from_variable(var, newpeername);
04425 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04426 }
04427 } else {
04428 peerlist = NULL;
04429 cat = NULL;
04430 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", ipaddr, SENTINEL);
04431 if(peerlist) {
04432 var = get_insecure_variable_from_config(peerlist);
04433 if(var) {
04434 if (realtimeregs) {
04435 newpeername = get_name_from_variable(var, newpeername);
04436 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04437 }
04438 }
04439 }
04440 }
04441 } else {
04442 if (realtimeregs) {
04443 peerlist = ast_load_realtime_multientry("sipregs", "ipaddr", ipaddr, SENTINEL);
04444 if (peerlist) {
04445 varregs = get_insecure_variable_from_config(peerlist);
04446 if (varregs) {
04447 newpeername = get_name_from_variable(varregs, newpeername);
04448 var = ast_load_realtime("sippeers", "name", newpeername, SENTINEL);
04449 }
04450 }
04451 } else {
04452 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", ipaddr, SENTINEL);
04453 if (peerlist) {
04454 var = get_insecure_variable_from_config(peerlist);
04455 if (var) {
04456 newpeername = get_name_from_variable(var, newpeername);
04457 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04458 }
04459 }
04460 }
04461 }
04462 }
04463 }
04464
04465 if (!var) {
04466 if (peerlist)
04467 ast_config_destroy(peerlist);
04468 return NULL;
04469 }
04470
04471 for (tmp = var; tmp; tmp = tmp->next) {
04472
04473 if (!strcasecmp(tmp->name, "type") &&
04474 !strcasecmp(tmp->value, "user")) {
04475 if(peerlist)
04476 ast_config_destroy(peerlist);
04477 else {
04478 ast_variables_destroy(var);
04479 ast_variables_destroy(varregs);
04480 }
04481 return NULL;
04482 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
04483 newpeername = tmp->value;
04484 }
04485 }
04486
04487 if (!newpeername) {
04488 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", ipaddr);
04489 if(peerlist)
04490 ast_config_destroy(peerlist);
04491 else
04492 ast_variables_destroy(var);
04493 return NULL;
04494 }
04495
04496
04497
04498 peer = build_peer(newpeername, var, varregs, TRUE, devstate_only);
04499 if (!peer) {
04500 if(peerlist)
04501 ast_config_destroy(peerlist);
04502 else {
04503 ast_variables_destroy(var);
04504 ast_variables_destroy(varregs);
04505 }
04506 return NULL;
04507 }
04508
04509 ast_debug(3, "-REALTIME- loading peer from database to memory. Name: %s. Peer objects: %d\n", peer->name, rpeerobjs);
04510
04511 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && !devstate_only) {
04512
04513 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
04514 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
04515 AST_SCHED_REPLACE_UNREF(peer->expire, sched, global_rtautoclear * 1000, expire_register, peer,
04516 unref_peer(_data, "remove registration ref"),
04517 unref_peer(peer, "remove registration ref"),
04518 ref_peer(peer, "add registration ref"));
04519 }
04520 ao2_t_link(peers, peer, "link peer into peers table");
04521 if (peer->addr.sin_addr.s_addr) {
04522 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
04523 }
04524 }
04525 peer->is_realtime = 1;
04526 if (peerlist)
04527 ast_config_destroy(peerlist);
04528 else {
04529 ast_variables_destroy(var);
04530 ast_variables_destroy(varregs);
04531 }
04532
04533 return peer;
04534 }
04535
04536
04537
04538
04539
04540
04541 struct peer_finding_info {
04542 struct sip_peer tmp_peer;
04543 int which_objects;
04544 };
04545
04546
04547 static int find_by_name(void *obj, void *arg, int flags)
04548 {
04549 struct sip_peer *search = obj;
04550 struct peer_finding_info *pfi = arg;
04551
04552
04553 if (strcmp(search->name, pfi->tmp_peer.name)) {
04554 return 0;
04555 }
04556
04557 switch (pfi->which_objects) {
04558 case FINDUSERS:
04559 if (!(search->type & SIP_TYPE_USER)) {
04560 return 0;
04561 }
04562 break;
04563 case FINDPEERS:
04564 if (!(search->type & SIP_TYPE_PEER)) {
04565 return 0;
04566 }
04567 break;
04568 case FINDALLDEVICES:
04569 break;
04570 }
04571
04572 return CMP_MATCH | CMP_STOP;
04573 }
04574
04575
04576
04577
04578
04579
04580
04581
04582
04583
04584
04585
04586
04587
04588 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int which_objects, int devstate_only, int transport)
04589 {
04590 struct sip_peer *p = NULL;
04591
04592 if (peer) {
04593 struct peer_finding_info pfi = {
04594 .which_objects = which_objects,
04595 };
04596 ast_copy_string(pfi.tmp_peer.name, peer, sizeof(pfi.tmp_peer.name));
04597 p = ao2_t_callback(peers, OBJ_POINTER, find_by_name, &pfi, "ao2_callback in peers table");
04598 } else if (sin) {
04599 struct sip_peer tmp_peer;
04600 tmp_peer.addr.sin_addr.s_addr = sin->sin_addr.s_addr;
04601 tmp_peer.addr.sin_port = sin->sin_port;
04602 tmp_peer.flags[0].flags = 0;
04603 tmp_peer.transports = transport;
04604 p = ao2_t_find(peers_by_ip, &tmp_peer, OBJ_POINTER, "ao2_find in peers_by_ip table");
04605 if (!p) {
04606 ast_set_flag(&tmp_peer.flags[0], SIP_INSECURE_PORT);
04607 p = ao2_t_find(peers_by_ip, &tmp_peer, OBJ_POINTER, "ao2_find in peers_by_ip table 2");
04608 if (p) {
04609 return p;
04610 }
04611 }
04612 }
04613
04614 if (!p && (realtime || devstate_only))
04615 p = realtime_peer(peer, sin, devstate_only);
04616
04617 return p;
04618 }
04619
04620
04621 static void do_setnat(struct sip_pvt *p, int natflags)
04622 {
04623 const char *mode = natflags ? "On" : "Off";
04624
04625 if (p->rtp) {
04626 ast_debug(1, "Setting NAT on RTP to %s\n", mode);
04627 ast_rtp_setnat(p->rtp, natflags);
04628 }
04629 if (p->vrtp) {
04630 ast_debug(1, "Setting NAT on VRTP to %s\n", mode);
04631 ast_rtp_setnat(p->vrtp, natflags);
04632 }
04633 if (p->udptl) {
04634 ast_debug(1, "Setting NAT on UDPTL to %s\n", mode);
04635 ast_udptl_setnat(p->udptl, natflags);
04636 }
04637 if (p->trtp) {
04638 ast_debug(1, "Setting NAT on TRTP to %s\n", mode);
04639 ast_rtp_setnat(p->trtp, natflags);
04640 }
04641 }
04642
04643
04644 static void change_t38_state(struct sip_pvt *p, int state)
04645 {
04646 int old = p->t38.state;
04647 struct ast_channel *chan = p->owner;
04648 struct ast_control_t38_parameters parameters = { .request_response = 0 };
04649
04650
04651 if (old == state)
04652 return;
04653
04654 p->t38.state = state;
04655 ast_debug(2, "T38 state changed to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
04656
04657
04658 if (!chan)
04659 return;
04660
04661
04662 switch (state) {
04663 case T38_PEER_REINVITE:
04664 parameters = p->t38.their_parms;
04665 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
04666 parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
04667 ast_udptl_set_tag(p->udptl, "SIP/%s", p->username);
04668 break;
04669 case T38_ENABLED:
04670 parameters = p->t38.their_parms;
04671 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
04672 parameters.request_response = AST_T38_NEGOTIATED;
04673 ast_udptl_set_tag(p->udptl, "SIP/%s", p->username);
04674 break;
04675 case T38_DISABLED:
04676 if (old == T38_ENABLED) {
04677 parameters.request_response = AST_T38_TERMINATED;
04678 } else if (old == T38_LOCAL_REINVITE) {
04679 parameters.request_response = AST_T38_REFUSED;
04680 }
04681 break;
04682 case T38_LOCAL_REINVITE:
04683
04684 break;
04685 }
04686
04687
04688 if (parameters.request_response)
04689 ast_queue_control_data(chan, AST_CONTROL_T38_PARAMETERS, ¶meters, sizeof(parameters));
04690 }
04691
04692
04693 static void set_t38_capabilities(struct sip_pvt *p)
04694 {
04695 if (p->udptl) {
04696 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY) {
04697 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
04698 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL_FEC) {
04699 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
04700 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL) {
04701 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
04702 }
04703 }
04704 }
04705
04706 static void copy_socket_data(struct sip_socket *to_sock, const struct sip_socket *from_sock)
04707 {
04708 if (to_sock->tcptls_session) {
04709 ao2_ref(to_sock->tcptls_session, -1);
04710 to_sock->tcptls_session = NULL;
04711 }
04712
04713 if (from_sock->tcptls_session) {
04714 ao2_ref(from_sock->tcptls_session, +1);
04715 }
04716
04717 *to_sock = *from_sock;
04718 }
04719
04720
04721
04722
04723
04724
04725
04726
04727 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
04728 {
04729
04730
04731
04732 if (dialog->socket.type && check_request_transport(peer, dialog))
04733 return -1;
04734 copy_socket_data(&dialog->socket, &peer->socket);
04735
04736 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
04737 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
04738 dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
04739 dialog->recv = dialog->sa;
04740 } else
04741 return -1;
04742
04743 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
04744 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
04745 dialog->capability = peer->capability;
04746 if (!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS) &&
04747 (!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) ||
04748 !(dialog->capability & AST_FORMAT_VIDEO_MASK)) &&
04749 dialog->vrtp) {
04750 ast_rtp_destroy(dialog->vrtp);
04751 dialog->vrtp = NULL;
04752 }
04753 if (!ast_test_flag(&dialog->flags[1], SIP_PAGE2_TEXTSUPPORT) && dialog->trtp) {
04754 ast_rtp_destroy(dialog->trtp);
04755 dialog->trtp = NULL;
04756 }
04757 dialog->prefs = peer->prefs;
04758 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
04759 if (!dialog->udptl) {
04760
04761 dialog->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
04762 }
04763 dialog->t38_maxdatagram = peer->t38_maxdatagram;
04764 set_t38_capabilities(dialog);
04765 } else if (dialog->udptl) {
04766 ast_udptl_destroy(dialog->udptl);
04767 dialog->udptl = NULL;
04768 }
04769 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04770
04771 if (dialog->rtp) {
04772 ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04773 ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04774 ast_rtp_set_rtptimeout(dialog->rtp, peer->rtptimeout);
04775 ast_rtp_set_rtpholdtimeout(dialog->rtp, peer->rtpholdtimeout);
04776 ast_rtp_set_rtpkeepalive(dialog->rtp, peer->rtpkeepalive);
04777
04778 ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
04779 dialog->autoframing = peer->autoframing;
04780 }
04781 if (dialog->vrtp) {
04782 ast_rtp_setdtmf(dialog->vrtp, 0);
04783 ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
04784 ast_rtp_set_rtptimeout(dialog->vrtp, peer->rtptimeout);
04785 ast_rtp_set_rtpholdtimeout(dialog->vrtp, peer->rtpholdtimeout);
04786 ast_rtp_set_rtpkeepalive(dialog->vrtp, peer->rtpkeepalive);
04787 }
04788 if (dialog->trtp) {
04789 ast_rtp_setdtmf(dialog->trtp, 0);
04790 ast_rtp_setdtmfcompensate(dialog->trtp, 0);
04791 ast_rtp_set_rtptimeout(dialog->trtp, peer->rtptimeout);
04792 ast_rtp_set_rtpholdtimeout(dialog->trtp, peer->rtpholdtimeout);
04793 ast_rtp_set_rtpkeepalive(dialog->trtp, peer->rtpkeepalive);
04794 }
04795
04796 ast_string_field_set(dialog, peername, peer->name);
04797 ast_string_field_set(dialog, authname, peer->username);
04798 ast_string_field_set(dialog, username, peer->username);
04799 ast_string_field_set(dialog, peersecret, peer->secret);
04800 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
04801 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
04802 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
04803 ast_string_field_set(dialog, tohost, peer->tohost);
04804 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
04805 ast_string_field_set(dialog, context, peer->context);
04806 ast_string_field_set(dialog, parkinglot, peer->parkinglot);
04807 ref_proxy(dialog, obproxy_get(dialog, peer));
04808 dialog->callgroup = peer->callgroup;
04809 dialog->pickupgroup = peer->pickupgroup;
04810 dialog->allowtransfer = peer->allowtransfer;
04811 dialog->jointnoncodeccapability = dialog->noncodeccapability;
04812 dialog->rtptimeout = peer->rtptimeout;
04813 dialog->peerauth = peer->auth;
04814 dialog->maxcallbitrate = peer->maxcallbitrate;
04815 if (ast_strlen_zero(dialog->tohost))
04816 ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
04817 if (!ast_strlen_zero(peer->fromdomain)) {
04818 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
04819 if (!dialog->initreq.headers) {
04820 char *c;
04821 char *tmpcall = ast_strdupa(dialog->callid);
04822
04823 c = strchr(tmpcall, '@');
04824 if (c) {
04825 *c = '\0';
04826 ao2_t_unlink(dialogs, dialog, "About to change the callid -- remove the old name");
04827 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
04828 ao2_t_link(dialogs, dialog, "New dialog callid -- inserted back into table");
04829 }
04830 }
04831 }
04832 if (!ast_strlen_zero(peer->fromuser))
04833 ast_string_field_set(dialog, fromuser, peer->fromuser);
04834 if (!ast_strlen_zero(peer->language))
04835 ast_string_field_set(dialog, language, peer->language);
04836
04837
04838
04839
04840 if (peer->maxms && peer->lastms)
04841 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
04842 else
04843 dialog->timer_t1 = peer->timer_t1;
04844
04845
04846
04847 if (peer->timer_b)
04848 dialog->timer_b = peer->timer_b;
04849 else
04850 dialog->timer_b = 64 * dialog->timer_t1;
04851
04852 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
04853 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
04854 dialog->noncodeccapability |= AST_RTP_DTMF;
04855 else
04856 dialog->noncodeccapability &= ~AST_RTP_DTMF;
04857 if (peer->call_limit)
04858 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
04859 if (!dialog->portinuri)
04860 dialog->portinuri = peer->portinuri;
04861
04862 return 0;
04863 }
04864
04865
04866
04867
04868 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct sockaddr_in *sin, int newdialog)
04869 {
04870 struct hostent *hp;
04871 struct ast_hostent ahp;
04872 struct sip_peer *peer;
04873 char *port;
04874 int portno = 0;
04875 char host[MAXHOSTNAMELEN], *hostn;
04876 char peername[256];
04877 int srv_ret = 0;
04878
04879 ast_copy_string(peername, opeer, sizeof(peername));
04880 port = strchr(peername, ':');
04881 if (port) {
04882 *port++ = '\0';
04883 dialog->portinuri = 1;
04884 }
04885 dialog->sa.sin_family = AF_INET;
04886 dialog->timer_t1 = global_t1;
04887 dialog->timer_b = global_timer_b;
04888 peer = find_peer(peername, NULL, TRUE, FINDPEERS, FALSE, 0);
04889
04890 if (peer) {
04891 int res;
04892 if (newdialog) {
04893 set_socket_transport(&dialog->socket, 0);
04894 }
04895 res = create_addr_from_peer(dialog, peer);
04896 if (!ast_strlen_zero(port)) {
04897 if ((portno = atoi(port))) {
04898 dialog->sa.sin_port = dialog->recv.sin_port = htons(portno);
04899 }
04900 }
04901 unref_peer(peer, "create_addr: unref peer from find_peer hashtab lookup");
04902 return res;
04903 }
04904
04905 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04906
04907 ast_string_field_set(dialog, tohost, peername);
04908
04909
04910 ref_proxy(dialog, obproxy_get(dialog, NULL));
04911
04912 if (sin) {
04913
04914 memcpy(&dialog->sa.sin_addr, &sin->sin_addr, sizeof(dialog->sa.sin_addr));
04915 if (!sin->sin_port) {
04916 portno = port_str2int(port, (dialog->socket.type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT);
04917 } else {
04918 portno = ntohs(sin->sin_port);
04919 }
04920 } else {
04921
04922
04923
04924
04925
04926
04927 hostn = peername;
04928
04929
04930
04931 if (!port && global_srvlookup) {
04932 char service[MAXHOSTNAMELEN];
04933 int tportno;
04934
04935 snprintf(service, sizeof(service), "_sip._%s.%s", get_transport(dialog->socket.type), peername);
04936 srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
04937 if (srv_ret > 0) {
04938 hostn = host;
04939 portno = tportno;
04940 }
04941 }
04942 if (!portno)
04943 portno = port_str2int(port, (dialog->socket.type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT);
04944 hp = ast_gethostbyname(hostn, &ahp);
04945 if (!hp) {
04946 ast_log(LOG_WARNING, "No such host: %s\n", peername);
04947 return -1;
04948 }
04949 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
04950 }
04951
04952 if (!dialog->socket.type)
04953 set_socket_transport(&dialog->socket, SIP_TRANSPORT_UDP);
04954 if (!dialog->socket.port)
04955 dialog->socket.port = bindaddr.sin_port;
04956 dialog->sa.sin_port = htons(portno);
04957 dialog->recv = dialog->sa;
04958 return 0;
04959 }
04960
04961
04962
04963
04964 static int auto_congest(const void *arg)
04965 {
04966 struct sip_pvt *p = (struct sip_pvt *)arg;
04967
04968 sip_pvt_lock(p);
04969 p->initid = -1;
04970 if (p->owner) {
04971
04972 if (!ast_channel_trylock(p->owner)) {
04973 append_history(p, "Cong", "Auto-congesting (timer)");
04974 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
04975 ast_channel_unlock(p->owner);
04976 }
04977
04978
04979 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
04980 }
04981 sip_pvt_unlock(p);
04982 dialog_unref(p, "unreffing arg passed into auto_congest callback (p->initid)");
04983 return 0;
04984 }
04985
04986
04987
04988
04989 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
04990 {
04991 int res;
04992 struct sip_pvt *p = ast->tech_pvt;
04993 struct varshead *headp;
04994 struct ast_var_t *current;
04995 const char *referer = NULL;
04996
04997 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
04998 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
04999 return -1;
05000 }
05001
05002
05003 headp=&ast->varshead;
05004 AST_LIST_TRAVERSE(headp, current, entries) {
05005
05006 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
05007 p->options->vxml_url = ast_var_value(current);
05008 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
05009 p->options->uri_options = ast_var_value(current);
05010 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
05011
05012 p->options->addsipheaders = 1;
05013 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
05014
05015 p->options->transfer = 1;
05016 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
05017
05018 referer = ast_var_value(current);
05019 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
05020
05021 p->options->replaces = ast_var_value(current);
05022 }
05023 }
05024
05025 res = 0;
05026 ast_set_flag(&p->flags[0], SIP_OUTGOING);
05027
05028 if (p->options->transfer) {
05029 char buf[SIPBUFSIZE/2];
05030
05031 if (referer) {
05032 if (sipdebug)
05033 ast_debug(3, "Call for %s transfered by %s\n", p->username, referer);
05034 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
05035 } else
05036 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
05037 ast_string_field_set(p, cid_name, buf);
05038 }
05039 ast_debug(1, "Outgoing Call for %s\n", p->username);
05040
05041 res = update_call_counter(p, INC_CALL_RINGING);
05042
05043 if (res == -1) {
05044 ast->hangupcause = AST_CAUSE_USER_BUSY;
05045 return res;
05046 }
05047 p->callingpres = ast->cid.cid_pres;
05048 p->jointcapability = ast_translate_available_formats(p->capability, p->prefcodec);
05049 p->jointnoncodeccapability = p->noncodeccapability;
05050
05051
05052 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
05053 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
05054 res = -1;
05055 } else {
05056 int xmitres;
05057
05058 xmitres = transmit_invite(p, SIP_INVITE, 1, 2);
05059 if (xmitres == XMIT_ERROR)
05060 return -1;
05061 p->invitestate = INV_CALLING;
05062
05063
05064 AST_SCHED_REPLACE_UNREF(p->initid, sched, p->timer_b, auto_congest, p,
05065 dialog_unref(_data, "dialog ptr dec when SCHED_REPLACE del op succeeded"),
05066 dialog_unref(p, "dialog ptr dec when SCHED_REPLACE add failed"),
05067 dialog_ref(p, "dialog ptr inc when SCHED_REPLACE add succeeded") );
05068 }
05069 return res;
05070 }
05071
05072
05073
05074 static void sip_registry_destroy(struct sip_registry *reg)
05075 {
05076
05077 ast_debug(3, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
05078
05079 if (reg->call) {
05080
05081
05082 reg->call->registry = registry_unref(reg->call->registry, "destroy reg->call->registry");
05083 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
05084 dialog_unlink_all(reg->call, TRUE, TRUE);
05085 reg->call = dialog_unref(reg->call, "unref reg->call");
05086
05087 }
05088 AST_SCHED_DEL(sched, reg->expire);
05089 AST_SCHED_DEL(sched, reg->timeout);
05090
05091 ast_string_field_free_memory(reg);
05092 ast_atomic_fetchadd_int(®objs, -1);
05093 ast_dnsmgr_release(reg->dnsmgr);
05094 ast_free(reg);
05095 }
05096
05097
05098 static void __sip_destroy(struct sip_pvt *p, int lockowner, int lockdialoglist)
05099 {
05100 struct sip_request *req;
05101
05102 if (p->stimer) {
05103 ast_free(p->stimer);
05104 p->stimer = NULL;
05105 }
05106
05107 if (sip_debug_test_pvt(p))
05108 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
05109
05110 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05111 update_call_counter(p, DEC_CALL_LIMIT);
05112 ast_debug(2, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
05113 }
05114
05115
05116 if (p->owner) {
05117 if (lockowner)
05118 ast_channel_lock(p->owner);
05119 if (option_debug)
05120 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
05121 p->owner->tech_pvt = NULL;
05122
05123 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05124 if (lockowner)
05125 ast_channel_unlock(p->owner);
05126
05127 usleep(1);
05128 }
05129
05130
05131 if (p->relatedpeer && p->relatedpeer->mwipvt)
05132 p->relatedpeer->mwipvt = dialog_unref(p->relatedpeer->mwipvt, "delete ->relatedpeer->mwipvt");
05133 if (p->relatedpeer && p->relatedpeer->call == p)
05134 p->relatedpeer->call = dialog_unref(p->relatedpeer->call, "unset the relatedpeer->call field in tandem with relatedpeer field itself");
05135
05136 if (p->relatedpeer)
05137 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting a dialog relatedpeer field in sip_destroy");
05138
05139 if (p->registry) {
05140 if (p->registry->call == p)
05141 p->registry->call = dialog_unref(p->registry->call, "nulling out the registry's call dialog field in unlink_all");
05142 p->registry = registry_unref(p->registry, "delete p->registry");
05143 }
05144
05145 if (dumphistory)
05146 sip_dump_history(p);
05147
05148 if (p->options)
05149 ast_free(p->options);
05150
05151 if (p->notify_headers) {
05152 ast_variables_destroy(p->notify_headers);
05153 p->notify_headers = NULL;
05154 }
05155 if (p->rtp) {
05156 ast_rtp_destroy(p->rtp);
05157 }
05158 if (p->vrtp) {
05159 ast_rtp_destroy(p->vrtp);
05160 }
05161 if (p->trtp) {
05162 while (ast_rtp_get_bridged(p->trtp))
05163 usleep(1);
05164 ast_rtp_destroy(p->trtp);
05165 }
05166 if (p->udptl)
05167 ast_udptl_destroy(p->udptl);
05168 if (p->refer)
05169 ast_free(p->refer);
05170 if (p->route) {
05171 free_old_route(p->route);
05172 p->route = NULL;
05173 }
05174 if (p->initreq.data)
05175 ast_free(p->initreq.data);
05176
05177
05178 if (p->stimer) {
05179 p->stimer->quit_flag = 1;
05180 if (p->stimer->st_active == TRUE && p->stimer->st_schedid > -1) {
05181 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
05182 dialog_unref(p, "removing session timer ref"));
05183 }
05184 ast_free(p->stimer);
05185 p->stimer = NULL;
05186 }
05187
05188
05189 if (p->history) {
05190 struct sip_history *hist;
05191 while ( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) ) {
05192 ast_free(hist);
05193 p->history_entries--;
05194 }
05195 ast_free(p->history);
05196 p->history = NULL;
05197 }
05198
05199 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
05200 ast_free(req);
05201 }
05202
05203 if (p->chanvars) {
05204 ast_variables_destroy(p->chanvars);
05205 p->chanvars = NULL;
05206 }
05207
05208 ast_string_field_free_memory(p);
05209
05210 if (p->socket.tcptls_session) {
05211 ao2_ref(p->socket.tcptls_session, -1);
05212 p->socket.tcptls_session = NULL;
05213 }
05214 }
05215
05216
05217
05218
05219
05220
05221
05222
05223
05224
05225
05226
05227
05228
05229
05230 static int update_call_counter(struct sip_pvt *fup, int event)
05231 {
05232 char name[256];
05233 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
05234 int outgoing = fup->outgoing_call;
05235 struct sip_peer *p = NULL;
05236
05237 ast_debug(3, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
05238
05239
05240
05241
05242 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
05243 return 0;
05244
05245 ast_copy_string(name, fup->username, sizeof(name));
05246
05247
05248 if ((p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, TRUE, FINDALLDEVICES, FALSE, 0))) {
05249 inuse = &p->inUse;
05250 call_limit = &p->call_limit;
05251 inringing = &p->inRinging;
05252 ast_copy_string(name, fup->peername, sizeof(name));
05253 }
05254 if (!p) {
05255 ast_debug(2, "%s is not a local device, no call limit\n", name);
05256 return 0;
05257 }
05258
05259 switch(event) {
05260
05261 case DEC_CALL_LIMIT:
05262
05263 if (inuse) {
05264 sip_pvt_lock(fup);
05265 ao2_lock(p);
05266 if (*inuse > 0) {
05267 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
05268 (*inuse)--;
05269 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
05270 }
05271 } else {
05272 *inuse = 0;
05273 }
05274 ao2_unlock(p);
05275 sip_pvt_unlock(fup);
05276 }
05277
05278
05279 if (inringing) {
05280 sip_pvt_lock(fup);
05281 ao2_lock(p);
05282 if (*inringing > 0) {
05283 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
05284 (*inringing)--;
05285 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
05286 }
05287 } else {
05288 *inringing = 0;
05289 }
05290 ao2_unlock(p);
05291 sip_pvt_unlock(fup);
05292 }
05293
05294
05295 sip_pvt_lock(fup);
05296 ao2_lock(p);
05297 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold) {
05298 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
05299 ao2_unlock(p);
05300 sip_pvt_unlock(fup);
05301 sip_peer_hold(fup, FALSE);
05302 } else {
05303 ao2_unlock(p);
05304 sip_pvt_unlock(fup);
05305 }
05306 if (sipdebug)
05307 ast_debug(2, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", "peer", name, *call_limit);
05308 break;
05309
05310 case INC_CALL_RINGING:
05311 case INC_CALL_LIMIT:
05312
05313 if (*call_limit > 0 ) {
05314 if (*inuse >= *call_limit) {
05315 ast_log(LOG_ERROR, "Call %s %s '%s' rejected due to usage limit of %d\n", outgoing ? "to" : "from", "peer", name, *call_limit);
05316 unref_peer(p, "update_call_counter: unref peer p, call limit exceeded");
05317 return -1;
05318 }
05319 }
05320 if (inringing && (event == INC_CALL_RINGING)) {
05321 sip_pvt_lock(fup);
05322 ao2_lock(p);
05323 if (!ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
05324 (*inringing)++;
05325 ast_set_flag(&fup->flags[0], SIP_INC_RINGING);
05326 }
05327 ao2_unlock(p);
05328 sip_pvt_unlock(fup);
05329 }
05330 if (inuse) {
05331 sip_pvt_lock(fup);
05332 ao2_lock(p);
05333 if (!ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
05334 (*inuse)++;
05335 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
05336 }
05337 ao2_unlock(p);
05338 sip_pvt_unlock(fup);
05339 }
05340 if (sipdebug) {
05341 ast_debug(2, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", "peer", name, *inuse, *call_limit);
05342 }
05343 break;
05344
05345 case DEC_CALL_RINGING:
05346 if (inringing) {
05347 sip_pvt_lock(fup);
05348 ao2_lock(p);
05349 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
05350 if (*inringing > 0) {
05351 (*inringing)--;
05352 }
05353 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
05354 }
05355 ao2_unlock(p);
05356 sip_pvt_unlock(fup);
05357 }
05358 break;
05359
05360 default:
05361 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
05362 }
05363
05364 if (p) {
05365 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", p->name);
05366 unref_peer(p, "update_call_counter: unref_peer from call counter");
05367 }
05368 return 0;
05369 }
05370
05371
05372 static void sip_destroy_fn(void *p)
05373 {
05374 sip_destroy(p);
05375 }
05376
05377
05378
05379
05380
05381
05382 static struct sip_pvt * sip_destroy(struct sip_pvt *p)
05383 {
05384 ast_debug(3, "Destroying SIP dialog %s\n", p->callid);
05385 __sip_destroy(p, TRUE, TRUE);
05386 return NULL;
05387 }
05388
05389
05390 static int hangup_sip2cause(int cause)
05391 {
05392
05393
05394 switch(cause) {
05395 case 401:
05396 return AST_CAUSE_CALL_REJECTED;
05397 case 403:
05398 return AST_CAUSE_CALL_REJECTED;
05399 case 404:
05400 return AST_CAUSE_UNALLOCATED;
05401 case 405:
05402 return AST_CAUSE_INTERWORKING;
05403 case 407:
05404 return AST_CAUSE_CALL_REJECTED;
05405 case 408:
05406 return AST_CAUSE_NO_USER_RESPONSE;
05407 case 409:
05408 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
05409 case 410:
05410 return AST_CAUSE_NUMBER_CHANGED;
05411 case 411:
05412 return AST_CAUSE_INTERWORKING;
05413 case 413:
05414 return AST_CAUSE_INTERWORKING;
05415 case 414:
05416 return AST_CAUSE_INTERWORKING;
05417 case 415:
05418 return AST_CAUSE_INTERWORKING;
05419 case 420:
05420 return AST_CAUSE_NO_ROUTE_DESTINATION;
05421 case 480:
05422 return AST_CAUSE_NO_ANSWER;
05423 case 481:
05424 return AST_CAUSE_INTERWORKING;
05425 case 482:
05426 return AST_CAUSE_INTERWORKING;
05427 case 483:
05428 return AST_CAUSE_NO_ANSWER;
05429 case 484:
05430 return AST_CAUSE_INVALID_NUMBER_FORMAT;
05431 case 485:
05432 return AST_CAUSE_UNALLOCATED;
05433 case 486:
05434 return AST_CAUSE_BUSY;
05435 case 487:
05436 return AST_CAUSE_INTERWORKING;
05437 case 488:
05438 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05439 case 491:
05440 return AST_CAUSE_INTERWORKING;
05441 case 493:
05442 return AST_CAUSE_INTERWORKING;
05443 case 500:
05444 return AST_CAUSE_FAILURE;
05445 case 501:
05446 return AST_CAUSE_FACILITY_REJECTED;
05447 case 502:
05448 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
05449 case 503:
05450 return AST_CAUSE_CONGESTION;
05451 case 504:
05452 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
05453 case 505:
05454 return AST_CAUSE_INTERWORKING;
05455 case 600:
05456 return AST_CAUSE_USER_BUSY;
05457 case 603:
05458 return AST_CAUSE_CALL_REJECTED;
05459 case 604:
05460 return AST_CAUSE_UNALLOCATED;
05461 case 606:
05462 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05463 default:
05464 return AST_CAUSE_NORMAL;
05465 }
05466
05467 return 0;
05468 }
05469
05470
05471
05472
05473
05474
05475
05476
05477
05478
05479
05480
05481
05482
05483
05484
05485
05486
05487
05488
05489
05490
05491
05492
05493
05494
05495
05496
05497
05498
05499
05500
05501
05502 static const char *hangup_cause2sip(int cause)
05503 {
05504 switch (cause) {
05505 case AST_CAUSE_UNALLOCATED:
05506 case AST_CAUSE_NO_ROUTE_DESTINATION:
05507 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
05508 return "404 Not Found";
05509 case AST_CAUSE_CONGESTION:
05510 case AST_CAUSE_SWITCH_CONGESTION:
05511 return "503 Service Unavailable";
05512 case AST_CAUSE_NO_USER_RESPONSE:
05513 return "408 Request Timeout";
05514 case AST_CAUSE_NO_ANSWER:
05515 case AST_CAUSE_UNREGISTERED:
05516 return "480 Temporarily unavailable";
05517 case AST_CAUSE_CALL_REJECTED:
05518 return "403 Forbidden";
05519 case AST_CAUSE_NUMBER_CHANGED:
05520 return "410 Gone";
05521 case AST_CAUSE_NORMAL_UNSPECIFIED:
05522 return "480 Temporarily unavailable";
05523 case AST_CAUSE_INVALID_NUMBER_FORMAT:
05524 return "484 Address incomplete";
05525 case AST_CAUSE_USER_BUSY:
05526 return "486 Busy here";
05527 case AST_CAUSE_FAILURE:
05528 return "500 Server internal failure";
05529 case AST_CAUSE_FACILITY_REJECTED:
05530 return "501 Not Implemented";
05531 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
05532 return "503 Service Unavailable";
05533
05534 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
05535 return "502 Bad Gateway";
05536 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
05537 return "488 Not Acceptable Here";
05538
05539 case AST_CAUSE_NOTDEFINED:
05540 default:
05541 ast_debug(1, "AST hangup cause %d (no match found in SIP)\n", cause);
05542 return NULL;
05543 }
05544
05545
05546 return 0;
05547 }
05548
05549
05550
05551
05552 static int sip_hangup(struct ast_channel *ast)
05553 {
05554 struct sip_pvt *p = ast->tech_pvt;
05555 int needcancel = FALSE;
05556 int needdestroy = 0;
05557 struct ast_channel *oldowner = ast;
05558
05559 if (!p) {
05560 ast_debug(1, "Asked to hangup channel that was not connected\n");
05561 return 0;
05562 }
05563 if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
05564 ast_debug(1, "This call was answered elsewhere");
05565 append_history(p, "Cancel", "Call answered elsewhere");
05566 p->answered_elsewhere = TRUE;
05567 }
05568
05569
05570 if (p->owner)
05571 p->hangupcause = p->owner->hangupcause;
05572
05573 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
05574 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05575 if (sipdebug)
05576 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
05577 update_call_counter(p, DEC_CALL_LIMIT);
05578 }
05579 ast_debug(4, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
05580 if (p->autokillid > -1 && sip_cancel_destroy(p))
05581 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
05582 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05583 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
05584 p->needdestroy = 0;
05585 p->owner->tech_pvt = dialog_unref(p->owner->tech_pvt, "unref p->owner->tech_pvt");
05586 sip_pvt_lock(p);
05587 p->owner = NULL;
05588 sip_pvt_unlock(p);
05589 return 0;
05590 }
05591
05592 if (ast_test_flag(ast, AST_FLAG_ZOMBIE)) {
05593 if (p->refer)
05594 ast_debug(1, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
05595 else
05596 ast_debug(1, "Hanging up zombie call. Be scared.\n");
05597 } else
05598 ast_debug(1, "Hangup call %s, SIP callid %s\n", ast->name, p->callid);
05599
05600 sip_pvt_lock(p);
05601 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05602 if (sipdebug)
05603 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
05604 update_call_counter(p, DEC_CALL_LIMIT);
05605 }
05606
05607
05608 if (p->owner != ast) {
05609 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
05610 sip_pvt_unlock(p);
05611 return 0;
05612 }
05613
05614
05615 if (p->invitestate < INV_COMPLETED && p->owner->_state != AST_STATE_UP) {
05616 needcancel = TRUE;
05617 ast_debug(4, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
05618 }
05619
05620 stop_media_flows(p);
05621
05622 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", p->owner ? ast_cause2str(p->hangupcause) : "Unknown");
05623
05624
05625 if (p->dsp)
05626 ast_dsp_free(p->dsp);
05627
05628 p->owner = NULL;
05629 ast->tech_pvt = dialog_unref(ast->tech_pvt, "unref ast->tech_pvt");
05630
05631 ast_module_unref(ast_module_info->self);
05632
05633
05634
05635
05636
05637
05638 if (p->alreadygone)
05639 needdestroy = 1;
05640 else if (p->invitestate != INV_CALLING)
05641 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05642
05643
05644 if (!p->alreadygone && p->initreq.data && !ast_strlen_zero(p->initreq.data->str)) {
05645 if (needcancel) {
05646 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05647
05648
05649 if (p->invitestate == INV_CALLING) {
05650
05651 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
05652 __sip_pretend_ack(p);
05653
05654 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05655 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
05656 } else {
05657 struct sip_pkt *cur;
05658
05659 for (cur = p->packets; cur; cur = cur->next) {
05660 __sip_semi_ack(p, cur->seqno, cur->is_resp, cur->method ? cur->method : find_sip_method(cur->data->str));
05661 }
05662 p->invitestate = INV_CANCELLED;
05663
05664 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
05665
05666
05667 needdestroy = 0;
05668 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05669 }
05670 } else {
05671 const char *res;
05672 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"));
05673 if (p->hangupcause && (res = hangup_cause2sip(p->hangupcause)))
05674 transmit_response_reliable(p, res, &p->initreq);
05675 else
05676 transmit_response_reliable(p, "603 Declined", &p->initreq);
05677 p->invitestate = INV_TERMINATED;
05678 }
05679 } else {
05680 if (p->stimer->st_active == TRUE) {
05681 stop_session_timer(p);
05682 }
05683
05684 if (!p->pendinginvite) {
05685 struct ast_channel *bridge = ast_bridged_channel(oldowner);
05686 char *audioqos = "";
05687 char *videoqos = "";
05688 char *textqos = "";
05689
05690
05691
05692
05693 while (bridge && ast_channel_trylock(bridge)) {
05694 sip_pvt_unlock(p);
05695 do {
05696
05697 CHANNEL_DEADLOCK_AVOIDANCE(oldowner);
05698 } while (sip_pvt_trylock(p));
05699 bridge = ast_bridged_channel(oldowner);
05700 }
05701
05702 if (p->rtp)
05703 ast_rtp_set_vars(oldowner, p->rtp);
05704
05705 if (bridge) {
05706 struct sip_pvt *q = bridge->tech_pvt;
05707
05708 if (IS_SIP_TECH(bridge->tech) && q && q->rtp)
05709 ast_rtp_set_vars(bridge, q->rtp);
05710 ast_channel_unlock(bridge);
05711 }
05712
05713 if (p->vrtp)
05714 videoqos = ast_rtp_get_quality(p->vrtp, NULL, RTPQOS_SUMMARY);
05715 if (p->trtp)
05716 textqos = ast_rtp_get_quality(p->trtp, NULL, RTPQOS_SUMMARY);
05717
05718 if (oldowner->_state == AST_STATE_UP) {
05719 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
05720 }
05721
05722
05723 if (p->do_history) {
05724 if (p->rtp)
05725 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
05726 if (p->vrtp)
05727 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
05728 if (p->trtp)
05729 append_history(p, "RTCPtext", "Quality:%s", textqos);
05730 }
05731 if (p->rtp && oldowner)
05732 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", audioqos);
05733 if (p->vrtp && oldowner)
05734 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", videoqos);
05735 if (p->trtp && oldowner)
05736 pbx_builtin_setvar_helper(oldowner, "RTPTEXTQOS", textqos);
05737 } else {
05738
05739
05740 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
05741 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
05742 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"));
05743 if (sip_cancel_destroy(p))
05744 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
05745 }
05746 }
05747 }
05748 if (needdestroy)
05749 p->needdestroy = 1;
05750 sip_pvt_unlock(p);
05751 return 0;
05752 }
05753
05754
05755 static void try_suggested_sip_codec(struct sip_pvt *p)
05756 {
05757 int fmt;
05758 const char *codec;
05759
05760 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
05761 if (!codec)
05762 return;
05763
05764 fmt = ast_getformatbyname(codec);
05765 if (fmt) {
05766 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
05767 if (p->jointcapability & fmt) {
05768 p->jointcapability &= fmt;
05769 p->capability &= fmt;
05770 } else
05771 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
05772 } else
05773 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
05774 return;
05775 }
05776
05777
05778
05779 static int sip_answer(struct ast_channel *ast)
05780 {
05781 int res = 0;
05782 struct sip_pvt *p = ast->tech_pvt;
05783
05784 sip_pvt_lock(p);
05785 if (ast->_state != AST_STATE_UP) {
05786 try_suggested_sip_codec(p);
05787
05788 ast_setstate(ast, AST_STATE_UP);
05789 ast_debug(1, "SIP answering channel: %s\n", ast->name);
05790 ast_rtp_new_source(p->rtp);
05791 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL, FALSE);
05792 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
05793 }
05794 sip_pvt_unlock(p);
05795 return res;
05796 }
05797
05798
05799 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
05800 {
05801 struct sip_pvt *p = ast->tech_pvt;
05802 int res = 0;
05803
05804 switch (frame->frametype) {
05805 case AST_FRAME_VOICE:
05806 if (!(frame->subclass & ast->nativeformats)) {
05807 char s1[512], s2[512], s3[512];
05808 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %s(%d) read/write = %s(%d)/%s(%d)\n",
05809 frame->subclass,
05810 ast_getformatname_multiple(s1, sizeof(s1) - 1, ast->nativeformats & AST_FORMAT_AUDIO_MASK),
05811 ast->nativeformats & AST_FORMAT_AUDIO_MASK,
05812 ast_getformatname_multiple(s2, sizeof(s2) - 1, ast->readformat),
05813 ast->readformat,
05814 ast_getformatname_multiple(s3, sizeof(s3) - 1, ast->writeformat),
05815 ast->writeformat);
05816 return 0;
05817 }
05818 if (p) {
05819 sip_pvt_lock(p);
05820 if (p->rtp) {
05821
05822 if ((ast->_state != AST_STATE_UP) &&
05823 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05824 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05825 ast_rtp_new_source(p->rtp);
05826 if (!global_prematuremediafilter) {
05827 p->invitestate = INV_EARLY_MEDIA;
05828 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
05829 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
05830 }
05831 } else if (p->t38.state == T38_ENABLED) {
05832
05833 } else {
05834 p->lastrtptx = time(NULL);
05835 res = ast_rtp_write(p->rtp, frame);
05836 }
05837 }
05838 sip_pvt_unlock(p);
05839 }
05840 break;
05841 case AST_FRAME_VIDEO:
05842 if (p) {
05843 sip_pvt_lock(p);
05844 if (p->vrtp) {
05845
05846 if ((ast->_state != AST_STATE_UP) &&
05847 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05848 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05849 p->invitestate = INV_EARLY_MEDIA;
05850 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
05851 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
05852 }
05853 p->lastrtptx = time(NULL);
05854 res = ast_rtp_write(p->vrtp, frame);
05855 }
05856 sip_pvt_unlock(p);
05857 }
05858 break;
05859 case AST_FRAME_TEXT:
05860 if (p) {
05861 sip_pvt_lock(p);
05862 if (p->red) {
05863 red_buffer_t140(p->trtp, frame);
05864 } else {
05865 if (p->trtp) {
05866
05867 if ((ast->_state != AST_STATE_UP) &&
05868 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05869 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05870 p->invitestate = INV_EARLY_MEDIA;
05871 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
05872 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
05873 }
05874 p->lastrtptx = time(NULL);
05875 res = ast_rtp_write(p->trtp, frame);
05876 }
05877 }
05878 sip_pvt_unlock(p);
05879 }
05880 break;
05881 case AST_FRAME_IMAGE:
05882 return 0;
05883 break;
05884 case AST_FRAME_MODEM:
05885 if (p) {
05886 sip_pvt_lock(p);
05887
05888
05889
05890
05891 if ((ast->_state == AST_STATE_UP) &&
05892 p->udptl &&
05893 (p->t38.state == T38_ENABLED)) {
05894 res = ast_udptl_write(p->udptl, frame);
05895 }
05896 sip_pvt_unlock(p);
05897 }
05898 break;
05899 default:
05900 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
05901 return 0;
05902 }
05903
05904 return res;
05905 }
05906
05907
05908
05909 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
05910 {
05911 int ret = -1;
05912 struct sip_pvt *p;
05913
05914 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE))
05915 ast_debug(1, "New channel is zombie\n");
05916 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE))
05917 ast_debug(1, "Old channel is zombie\n");
05918
05919 if (!newchan || !newchan->tech_pvt) {
05920 if (!newchan)
05921 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
05922 else
05923 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
05924 return -1;
05925 }
05926 p = newchan->tech_pvt;
05927
05928 sip_pvt_lock(p);
05929 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
05930 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
05931 if (p->owner != oldchan)
05932 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
05933 else {
05934 p->owner = newchan;
05935
05936
05937
05938
05939
05940
05941 sip_set_rtp_peer(newchan, NULL, NULL, 0, 0, 0);
05942 ret = 0;
05943 }
05944 ast_debug(3, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
05945
05946 sip_pvt_unlock(p);
05947 return ret;
05948 }
05949
05950 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
05951 {
05952 struct sip_pvt *p = ast->tech_pvt;
05953 int res = 0;
05954
05955 sip_pvt_lock(p);
05956 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
05957 case SIP_DTMF_INBAND:
05958 res = -1;
05959 break;
05960 case SIP_DTMF_RFC2833:
05961 if (p->rtp)
05962 ast_rtp_senddigit_begin(p->rtp, digit);
05963 break;
05964 default:
05965 break;
05966 }
05967 sip_pvt_unlock(p);
05968
05969 return res;
05970 }
05971
05972
05973
05974 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
05975 {
05976 struct sip_pvt *p = ast->tech_pvt;
05977 int res = 0;
05978
05979 sip_pvt_lock(p);
05980 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
05981 case SIP_DTMF_INFO:
05982 case SIP_DTMF_SHORTINFO:
05983 transmit_info_with_digit(p, digit, duration);
05984 break;
05985 case SIP_DTMF_RFC2833:
05986 if (p->rtp)
05987 ast_rtp_senddigit_end(p->rtp, digit);
05988 break;
05989 case SIP_DTMF_INBAND:
05990 res = -1;
05991 break;
05992 }
05993 sip_pvt_unlock(p);
05994
05995 return res;
05996 }
05997
05998
05999 static int sip_transfer(struct ast_channel *ast, const char *dest)
06000 {
06001 struct sip_pvt *p = ast->tech_pvt;
06002 int res;
06003
06004 if (dest == NULL)
06005 dest = "";
06006 sip_pvt_lock(p);
06007 if (ast->_state == AST_STATE_RING)
06008 res = sip_sipredirect(p, dest);
06009 else
06010 res = transmit_refer(p, dest);
06011 sip_pvt_unlock(p);
06012 return res;
06013 }
06014
06015
06016 static void interpret_t38_parameters(struct sip_pvt *p, const struct ast_control_t38_parameters *parameters)
06017 {
06018 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT)) {
06019 return;
06020 }
06021 switch (parameters->request_response) {
06022 case AST_T38_NEGOTIATED:
06023 case AST_T38_REQUEST_NEGOTIATE:
06024
06025 if (!parameters->max_ifp) {
06026 change_t38_state(p, T38_DISABLED);
06027 if (p->t38.state == T38_PEER_REINVITE) {
06028 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"));
06029 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
06030 }
06031 break;
06032 } else if (p->t38.state == T38_PEER_REINVITE) {
06033 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"));
06034 p->t38.our_parms = *parameters;
06035
06036
06037
06038 if (!p->t38.their_parms.fill_bit_removal) {
06039 p->t38.our_parms.fill_bit_removal = FALSE;
06040 }
06041 if (!p->t38.their_parms.transcoding_mmr) {
06042 p->t38.our_parms.transcoding_mmr = FALSE;
06043 }
06044 if (!p->t38.their_parms.transcoding_jbig) {
06045 p->t38.our_parms.transcoding_jbig = FALSE;
06046 }
06047 p->t38.our_parms.version = MIN(p->t38.our_parms.version, p->t38.their_parms.version);
06048 p->t38.our_parms.rate_management = p->t38.their_parms.rate_management;
06049 ast_udptl_set_local_max_ifp(p->udptl, p->t38.our_parms.max_ifp);
06050 change_t38_state(p, T38_ENABLED);
06051 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
06052 } else if (p->t38.state != T38_ENABLED) {
06053 p->t38.our_parms = *parameters;
06054 ast_udptl_set_local_max_ifp(p->udptl, p->t38.our_parms.max_ifp);
06055 change_t38_state(p, T38_LOCAL_REINVITE);
06056 if (!p->pendinginvite) {
06057 transmit_reinvite_with_sdp(p, TRUE, FALSE);
06058 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
06059 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
06060 }
06061 }
06062 break;
06063 case AST_T38_TERMINATED:
06064 case AST_T38_REFUSED:
06065 case AST_T38_REQUEST_TERMINATE:
06066 if (p->t38.state == T38_PEER_REINVITE) {
06067 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"));
06068 change_t38_state(p, T38_DISABLED);
06069 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
06070 } else if (p->t38.state == T38_ENABLED)
06071 transmit_reinvite_with_sdp(p, FALSE, FALSE);
06072 break;
06073 default:
06074 break;
06075 }
06076 }
06077
06078
06079
06080
06081
06082
06083 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
06084 {
06085 struct sip_pvt *p = ast->tech_pvt;
06086 int res = 0;
06087
06088 sip_pvt_lock(p);
06089 switch(condition) {
06090 case AST_CONTROL_RINGING:
06091 if (ast->_state == AST_STATE_RING) {
06092 p->invitestate = INV_EARLY_MEDIA;
06093 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
06094 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
06095
06096 transmit_provisional_response(p, "180 Ringing", &p->initreq, 0);
06097 ast_set_flag(&p->flags[0], SIP_RINGING);
06098 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
06099 break;
06100 } else {
06101
06102 }
06103 }
06104 res = -1;
06105 break;
06106 case AST_CONTROL_BUSY:
06107 if (ast->_state != AST_STATE_UP) {
06108 transmit_response_reliable(p, "486 Busy Here", &p->initreq);
06109 p->invitestate = INV_COMPLETED;
06110 sip_alreadygone(p);
06111 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
06112 break;
06113 }
06114 res = -1;
06115 break;
06116 case AST_CONTROL_CONGESTION:
06117 if (ast->_state != AST_STATE_UP) {
06118 transmit_response_reliable(p, "503 Service Unavailable", &p->initreq);
06119 p->invitestate = INV_COMPLETED;
06120 sip_alreadygone(p);
06121 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
06122 break;
06123 }
06124 res = -1;
06125 break;
06126 case AST_CONTROL_PROCEEDING:
06127 if ((ast->_state != AST_STATE_UP) &&
06128 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06129 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06130 transmit_response(p, "100 Trying", &p->initreq);
06131 p->invitestate = INV_PROCEEDING;
06132 break;
06133 }
06134 res = -1;
06135 break;
06136 case AST_CONTROL_PROGRESS:
06137 if ((ast->_state != AST_STATE_UP) &&
06138 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06139 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06140 p->invitestate = INV_EARLY_MEDIA;
06141 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06142 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06143 break;
06144 }
06145 res = -1;
06146 break;
06147 case AST_CONTROL_HOLD:
06148 ast_rtp_new_source(p->rtp);
06149 ast_moh_start(ast, data, p->mohinterpret);
06150 break;
06151 case AST_CONTROL_UNHOLD:
06152 ast_rtp_new_source(p->rtp);
06153 ast_moh_stop(ast);
06154 break;
06155 case AST_CONTROL_VIDUPDATE:
06156 if (p->vrtp && !p->novideo) {
06157 transmit_info_with_vidupdate(p);
06158
06159 } else
06160 res = -1;
06161 break;
06162 case AST_CONTROL_T38_PARAMETERS:
06163 if (datalen != sizeof(struct ast_control_t38_parameters)) {
06164 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);
06165 } else {
06166 const struct ast_control_t38_parameters *parameters = data;
06167 interpret_t38_parameters(p, parameters);
06168 }
06169 break;
06170 case AST_CONTROL_SRCUPDATE:
06171 ast_rtp_new_source(p->rtp);
06172 break;
06173 case AST_CONTROL_SRCCHANGE:
06174 ast_rtp_change_source(p->rtp);
06175 break;
06176 case -1:
06177 res = -1;
06178 break;
06179 default:
06180 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
06181 res = -1;
06182 break;
06183 }
06184 sip_pvt_unlock(p);
06185 return res;
06186 }
06187
06188
06189
06190
06191
06192
06193
06194 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
06195 {
06196 struct ast_channel *tmp;
06197 struct ast_variable *v = NULL;
06198 int fmt;
06199 int what;
06200 int video;
06201 int text;
06202 int needvideo = 0;
06203 int needtext = 0;
06204 char buf[SIPBUFSIZE];
06205 char *decoded_exten;
06206
06207 {
06208 const char *my_name;
06209
06210 if (title)
06211 my_name = title;
06212 else if ( (my_name = strchr(i->fromdomain, ':')) )
06213 my_name++;
06214 else
06215 my_name = i->fromdomain;
06216
06217 sip_pvt_unlock(i);
06218
06219 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, "SIP/%s-%08x", my_name, ast_atomic_fetchadd_int((int *)&chan_idx, +1));
06220
06221 }
06222 if (!tmp) {
06223 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
06224 sip_pvt_lock(i);
06225 return NULL;
06226 }
06227 sip_pvt_lock(i);
06228
06229 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;
06230
06231
06232
06233 if (i->jointcapability) {
06234 what = i->jointcapability;
06235 video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
06236 text = i->jointcapability & AST_FORMAT_TEXT_MASK;
06237 } else if (i->capability) {
06238 what = i->capability;
06239 video = i->capability & AST_FORMAT_VIDEO_MASK;
06240 text = i->capability & AST_FORMAT_TEXT_MASK;
06241 } else {
06242 what = global_capability;
06243 video = global_capability & AST_FORMAT_VIDEO_MASK;
06244 text = global_capability & AST_FORMAT_TEXT_MASK;
06245 }
06246
06247
06248 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video | text;
06249 ast_debug(3, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
06250 ast_debug(3, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
06251 ast_debug(3, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
06252 ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
06253 if (i->prefcodec)
06254 ast_debug(3, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
06255
06256
06257 fmt = ast_best_codec(tmp->nativeformats);
06258
06259
06260
06261
06262
06263 if (i->vrtp) {
06264 if (ast_test_flag(&i->flags[1], SIP_PAGE2_VIDEOSUPPORT))
06265 needvideo = AST_FORMAT_VIDEO_MASK;
06266 else if (i->prefcodec)
06267 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
06268 else
06269 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
06270 }
06271
06272 if (i->trtp) {
06273 if (i->prefcodec)
06274 needtext = i->prefcodec & AST_FORMAT_TEXT_MASK;
06275 else
06276 needtext = i->jointcapability & AST_FORMAT_TEXT_MASK;
06277 }
06278
06279 if (needvideo)
06280 ast_debug(3, "This channel can handle video! HOLLYWOOD next!\n");
06281 else
06282 ast_debug(3, "This channel will not be able to handle video.\n");
06283
06284 if ((ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) || (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) ||
06285 (ast_test_flag(&i->flags[1], SIP_PAGE2_FAX_DETECT))) {
06286 int features = DSP_FEATURE_DIGIT_DETECT;
06287
06288 if (ast_test_flag(&i->flags[1], SIP_PAGE2_FAX_DETECT)) {
06289 features |= DSP_FEATURE_FAX_DETECT;
06290 }
06291
06292 i->dsp = ast_dsp_new();
06293 ast_dsp_set_features(i->dsp, features);
06294 if (global_relaxdtmf)
06295 ast_dsp_set_digitmode(i->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
06296 }
06297
06298
06299 if (i->rtp) {
06300 ast_channel_set_fd(tmp, 0, ast_rtp_fd(i->rtp));
06301 ast_channel_set_fd(tmp, 1, ast_rtcp_fd(i->rtp));
06302 }
06303 if (needvideo && i->vrtp) {
06304 ast_channel_set_fd(tmp, 2, ast_rtp_fd(i->vrtp));
06305 ast_channel_set_fd(tmp, 3, ast_rtcp_fd(i->vrtp));
06306 }
06307 if (needtext && i->trtp)
06308 ast_channel_set_fd(tmp, 4, ast_rtp_fd(i->trtp));
06309 if (i->udptl)
06310 ast_channel_set_fd(tmp, 5, ast_udptl_fd(i->udptl));
06311
06312 if (state == AST_STATE_RING)
06313 tmp->rings = 1;
06314 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
06315 tmp->writeformat = fmt;
06316 tmp->rawwriteformat = fmt;
06317 tmp->readformat = fmt;
06318 tmp->rawreadformat = fmt;
06319 tmp->tech_pvt = dialog_ref(i, "sip_new: set chan->tech_pvt to i");
06320
06321 tmp->callgroup = i->callgroup;
06322 tmp->pickupgroup = i->pickupgroup;
06323 tmp->cid.cid_pres = i->callingpres;
06324 if (!ast_strlen_zero(i->parkinglot))
06325 ast_string_field_set(tmp, parkinglot, i->parkinglot);
06326 if (!ast_strlen_zero(i->accountcode))
06327 ast_string_field_set(tmp, accountcode, i->accountcode);
06328 if (i->amaflags)
06329 tmp->amaflags = i->amaflags;
06330 if (!ast_strlen_zero(i->language))
06331 ast_string_field_set(tmp, language, i->language);
06332 i->owner = tmp;
06333 ast_module_ref(ast_module_info->self);
06334 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
06335
06336
06337
06338
06339 decoded_exten = ast_strdupa(i->exten);
06340 ast_uri_decode(decoded_exten);
06341 ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
06342
06343
06344
06345 tmp->cid.cid_ani = ast_strdup(i->cid_num);
06346 if (!ast_strlen_zero(i->rdnis))
06347 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
06348
06349 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
06350 tmp->cid.cid_dnid = ast_strdup(i->exten);
06351
06352 tmp->priority = 1;
06353 if (!ast_strlen_zero(i->uri))
06354 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
06355 if (!ast_strlen_zero(i->domain))
06356 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
06357 if (!ast_strlen_zero(i->callid))
06358 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
06359 if (i->rtp)
06360 ast_jb_configure(tmp, &global_jbconf);
06361
06362
06363 for (v = i->chanvars ; v ; v = v->next)
06364 pbx_builtin_setvar_helper(tmp, v->name, v->value);
06365
06366 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
06367 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
06368 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
06369 ast_hangup(tmp);
06370 tmp = NULL;
06371 }
06372
06373 if (i->do_history)
06374 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
06375
06376
06377 if (global_callevents)
06378 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
06379 "Channel: %s\r\nUniqueid: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\n",
06380 tmp->name, tmp->uniqueid, "SIP", i->callid, i->fullcontact);
06381
06382 return tmp;
06383 }
06384
06385
06386 static char *get_body_by_line(const char *line, const char *name, int nameLen)
06387 {
06388 if (!strncasecmp(line, name, nameLen) && line[nameLen] == '=')
06389 return ast_skip_blanks(line + nameLen + 1);
06390
06391 return "";
06392 }
06393
06394
06395
06396
06397
06398 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
06399 {
06400 int len = strlen(name);
06401
06402 while (*start < (req->sdp_start + req->sdp_count)) {
06403 const char *r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[(*start)++]), name, len);
06404 if (r[0] != '\0')
06405 return r;
06406 }
06407
06408
06409 (*start)++;
06410
06411 return "";
06412 }
06413
06414
06415
06416
06417
06418
06419 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value)
06420 {
06421 char type = '\0';
06422 const char *line = NULL;
06423
06424 if (stop > (req->sdp_start + req->sdp_count)) {
06425 stop = req->sdp_start + req->sdp_count;
06426 }
06427
06428 while (*start < stop) {
06429 line = REQ_OFFSET_TO_STR(req, line[(*start)++]);
06430 if (line[1] == '=') {
06431 type = line[0];
06432 *value = ast_skip_blanks(line + 2);
06433 break;
06434 }
06435 }
06436
06437 return type;
06438 }
06439
06440
06441 static char *get_body(struct sip_request *req, char *name)
06442 {
06443 int x;
06444 int len = strlen(name);
06445 char *r;
06446
06447 for (x = 0; x < req->lines; x++) {
06448 r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[x]), name, len);
06449 if (r[0] != '\0')
06450 return r;
06451 }
06452
06453 return "";
06454 }
06455
06456
06457 static const char *find_alias(const char *name, const char *_default)
06458 {
06459
06460 static const struct cfalias {
06461 char * const fullname;
06462 char * const shortname;
06463 } aliases[] = {
06464 { "Content-Type", "c" },
06465 { "Content-Encoding", "e" },
06466 { "From", "f" },
06467 { "Call-ID", "i" },
06468 { "Contact", "m" },
06469 { "Content-Length", "l" },
06470 { "Subject", "s" },
06471 { "To", "t" },
06472 { "Supported", "k" },
06473 { "Refer-To", "r" },
06474 { "Referred-By", "b" },
06475 { "Allow-Events", "u" },
06476 { "Event", "o" },
06477 { "Via", "v" },
06478 { "Accept-Contact", "a" },
06479 { "Reject-Contact", "j" },
06480 { "Request-Disposition", "d" },
06481 { "Session-Expires", "x" },
06482 { "Identity", "y" },
06483 { "Identity-Info", "n" },
06484 };
06485 int x;
06486
06487 for (x = 0; x < ARRAY_LEN(aliases); x++) {
06488 if (!strcasecmp(aliases[x].fullname, name))
06489 return aliases[x].shortname;
06490 }
06491
06492 return _default;
06493 }
06494
06495 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
06496 {
06497 int pass;
06498
06499
06500
06501
06502
06503
06504
06505
06506
06507
06508 for (pass = 0; name && pass < 2;pass++) {
06509 int x, len = strlen(name);
06510 for (x = *start; x < req->headers; x++) {
06511 char *header = REQ_OFFSET_TO_STR(req, header[x]);
06512 if (!strncasecmp(header, name, len)) {
06513 char *r = header + len;
06514 if (pedanticsipchecking)
06515 r = ast_skip_blanks(r);
06516
06517 if (*r == ':') {
06518 *start = x+1;
06519 return ast_skip_blanks(r+1);
06520 }
06521 }
06522 }
06523 if (pass == 0)
06524 name = find_alias(name, NULL);
06525 }
06526
06527
06528 return "";
06529 }
06530
06531
06532
06533
06534 static const char *get_header(const struct sip_request *req, const char *name)
06535 {
06536 int start = 0;
06537 return __get_header(req, name, &start);
06538 }
06539
06540
06541 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
06542 {
06543
06544 struct ast_frame *f;
06545
06546 if (!p->rtp) {
06547
06548 return &ast_null_frame;
06549 }
06550
06551 switch(ast->fdno) {
06552 case 0:
06553 f = ast_rtp_read(p->rtp);
06554 break;
06555 case 1:
06556 f = ast_rtcp_read(p->rtp);
06557 break;
06558 case 2:
06559 f = ast_rtp_read(p->vrtp);
06560 break;
06561 case 3:
06562 f = ast_rtcp_read(p->vrtp);
06563 break;
06564 case 4:
06565 f = ast_rtp_read(p->trtp);
06566 if (sipdebug_text) {
06567 int i;
06568 unsigned char* arr = f->data.ptr;
06569 for (i=0; i < f->datalen; i++)
06570 ast_verbose("%c", (arr[i] > ' ' && arr[i] < '}') ? arr[i] : '.');
06571 ast_verbose(" -> ");
06572 for (i=0; i < f->datalen; i++)
06573 ast_verbose("%02X ", arr[i]);
06574 ast_verbose("\n");
06575 }
06576 break;
06577 case 5:
06578 f = ast_udptl_read(p->udptl);
06579 break;
06580 default:
06581 f = &ast_null_frame;
06582 }
06583
06584 if (f && (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) &&
06585 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833)) {
06586 ast_debug(1, "Ignoring DTMF (%c) RTP frame because dtmfmode is not RFC2833\n", f->subclass);
06587 return &ast_null_frame;
06588 }
06589
06590
06591 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
06592 return f;
06593
06594 if (f && f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
06595 if (!(f->subclass & p->jointcapability)) {
06596 ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
06597 ast_getformatname(f->subclass), p->owner->name);
06598 return &ast_null_frame;
06599 }
06600 ast_debug(1, "Oooh, format changed to %d %s\n",
06601 f->subclass, ast_getformatname(f->subclass));
06602 p->owner->nativeformats = (p->owner->nativeformats & (AST_FORMAT_VIDEO_MASK | AST_FORMAT_TEXT_MASK)) | f->subclass;
06603 ast_set_read_format(p->owner, p->owner->readformat);
06604 ast_set_write_format(p->owner, p->owner->writeformat);
06605 }
06606
06607 if (f && ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) || ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT)) && p->dsp) {
06608 f = ast_dsp_process(p->owner, p->dsp, f);
06609 if (f && f->frametype == AST_FRAME_DTMF) {
06610 if (f->subclass == 'f') {
06611 if (option_debug)
06612 ast_log(LOG_DEBUG, "Fax CNG detected on %s\n", ast->name);
06613 *faxdetect = 1;
06614
06615 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
06616 ast_dsp_set_features(p->dsp, DSP_FEATURE_DIGIT_DETECT);
06617 } else {
06618 ast_dsp_free(p->dsp);
06619 p->dsp = NULL;
06620 }
06621 } else {
06622 ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
06623 }
06624 }
06625 }
06626
06627 return f;
06628 }
06629
06630
06631 static struct ast_frame *sip_read(struct ast_channel *ast)
06632 {
06633 struct ast_frame *fr;
06634 struct sip_pvt *p = ast->tech_pvt;
06635 int faxdetected = FALSE;
06636
06637 sip_pvt_lock(p);
06638 fr = sip_rtp_read(ast, p, &faxdetected);
06639 p->lastrtprx = time(NULL);
06640
06641
06642 if (faxdetected && ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT)) {
06643 ast_channel_lock(ast);
06644 if (strcmp(ast->exten, "fax")) {
06645 const char *target_context = S_OR(ast->macrocontext, ast->context);
06646 ast_channel_unlock(ast);
06647 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
06648 ast_verbose(VERBOSE_PREFIX_2 "Redirecting '%s' to fax extension\n", ast->name);
06649 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
06650 if (ast_async_goto(ast, target_context, "fax", 1)) {
06651 ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
06652 }
06653 fr = &ast_null_frame;
06654 } else {
06655 ast_log(LOG_NOTICE, "Fax detected but no fax extension\n");
06656 }
06657 } else {
06658 ast_channel_unlock(ast);
06659 }
06660 }
06661
06662
06663 if (fr && fr->frametype == AST_FRAME_VOICE && p->invitestate != INV_EARLY_MEDIA && ast->_state != AST_STATE_UP) {
06664 fr = &ast_null_frame;
06665 }
06666
06667 sip_pvt_unlock(p);
06668
06669 return fr;
06670 }
06671
06672
06673
06674 static char *generate_random_string(char *buf, size_t size)
06675 {
06676 long val[4];
06677 int x;
06678
06679 for (x=0; x<4; x++)
06680 val[x] = ast_random();
06681 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
06682
06683 return buf;
06684 }
06685
06686
06687 static void build_callid_pvt(struct sip_pvt *pvt)
06688 {
06689 char buf[33];
06690
06691 const char *host = S_OR(pvt->fromdomain, ast_inet_ntoa(pvt->ourip.sin_addr));
06692
06693 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
06694
06695 }
06696
06697
06698 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain)
06699 {
06700 char buf[33];
06701
06702 const char *host = S_OR(fromdomain, ast_inet_ntoa(ourip));
06703
06704 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
06705 }
06706
06707
06708 static void make_our_tag(char *tagbuf, size_t len)
06709 {
06710 snprintf(tagbuf, len, "as%08lx", ast_random());
06711 }
06712
06713
06714 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p)
06715 {
06716 struct sip_st_dlg *stp;
06717
06718 if (p->stimer) {
06719 ast_log(LOG_ERROR, "Session-Timer struct already allocated\n");
06720 return p->stimer;
06721 }
06722
06723 if (!(stp = ast_calloc(1, sizeof(struct sip_st_dlg))))
06724 return NULL;
06725
06726 p->stimer = stp;
06727
06728 stp->st_schedid = -1;
06729
06730 return p->stimer;
06731 }
06732
06733
06734
06735
06736
06737 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
06738 int useglobal_nat, const int intended_method, struct sip_request *req)
06739 {
06740 struct sip_pvt *p;
06741
06742 if (!(p = ao2_t_alloc(sizeof(*p), sip_destroy_fn, "allocate a dialog(pvt) struct")))
06743 return NULL;
06744
06745 if (ast_string_field_init(p, 512)) {
06746 ao2_t_ref(p, -1, "failed to string_field_init, drop p");
06747 return NULL;
06748 }
06749
06750 if (req) {
06751 set_socket_transport(&p->socket, req->socket.type);
06752 } else {
06753 set_socket_transport(&p->socket, SIP_TRANSPORT_UDP);
06754 }
06755
06756 p->socket.fd = -1;
06757 p->method = intended_method;
06758 p->initid = -1;
06759 p->waitid = -1;
06760 p->autokillid = -1;
06761 p->request_queue_sched_id = -1;
06762 p->provisional_keepalive_sched_id = -1;
06763 p->t38id = -1;
06764 p->subscribed = NONE;
06765 p->stateid = -1;
06766 p->sessionversion_remote = -1;
06767 p->session_modify = TRUE;
06768 p->stimer = NULL;
06769 p->prefs = default_prefs;
06770
06771 if (intended_method != SIP_OPTIONS) {
06772 p->timer_t1 = global_t1;
06773 p->timer_b = global_timer_b;
06774 }
06775
06776 if (!sin)
06777 p->ourip = internip;
06778 else {
06779 p->sa = *sin;
06780 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
06781 }
06782
06783
06784 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
06785 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
06786
06787 p->do_history = recordhistory;
06788
06789 p->branch = ast_random();
06790 make_our_tag(p->tag, sizeof(p->tag));
06791 p->ocseq = INITIAL_CSEQ;
06792
06793 if (sip_methods[intended_method].need_rtp) {
06794 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
06795
06796 if (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT))
06797 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
06798 if (ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT))
06799 p->trtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
06800 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT))
06801 p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
06802 if (!p->rtp|| (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) && !p->vrtp)
06803 || (ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT) && !p->trtp)) {
06804 ast_log(LOG_WARNING, "Unable to create RTP audio %s%ssession: %s\n",
06805 ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "and video " : "",
06806 ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT) ? "and text " : "", strerror(errno));
06807 if (p->chanvars) {
06808 ast_variables_destroy(p->chanvars);
06809 p->chanvars = NULL;
06810 }
06811 ao2_t_ref(p, -1, "failed to create RTP audio session, drop p");
06812 return NULL;
06813 p->t38_maxdatagram = global_t38_maxdatagram;
06814 }
06815 ast_rtp_setqos(p->rtp, global_tos_audio, global_cos_audio, "SIP RTP");
06816 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
06817 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
06818 ast_rtp_set_rtptimeout(p->rtp, global_rtptimeout);
06819 ast_rtp_set_rtpholdtimeout(p->rtp, global_rtpholdtimeout);
06820 ast_rtp_set_rtpkeepalive(p->rtp, global_rtpkeepalive);
06821 if (p->vrtp) {
06822 ast_rtp_setqos(p->vrtp, global_tos_video, global_cos_video, "SIP VRTP");
06823 ast_rtp_setdtmf(p->vrtp, 0);
06824 ast_rtp_setdtmfcompensate(p->vrtp, 0);
06825 ast_rtp_set_rtptimeout(p->vrtp, global_rtptimeout);
06826 ast_rtp_set_rtpholdtimeout(p->vrtp, global_rtpholdtimeout);
06827 ast_rtp_set_rtpkeepalive(p->vrtp, global_rtpkeepalive);
06828 }
06829 if (p->trtp) {
06830 ast_rtp_setqos(p->trtp, global_tos_text, global_cos_text, "SIP TRTP");
06831 ast_rtp_setdtmf(p->trtp, 0);
06832 ast_rtp_setdtmfcompensate(p->trtp, 0);
06833 }
06834 if (p->udptl)
06835 ast_udptl_setqos(p->udptl, global_tos_audio, global_cos_audio);
06836 p->maxcallbitrate = default_maxcallbitrate;
06837 p->autoframing = global_autoframing;
06838 ast_rtp_codec_setpref(p->rtp, &p->prefs);
06839 }
06840
06841 if (useglobal_nat && sin) {
06842
06843 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
06844 p->recv = *sin;
06845 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
06846 }
06847
06848 if (p->method != SIP_REGISTER)
06849 ast_string_field_set(p, fromdomain, default_fromdomain);
06850 build_via(p);
06851 if (!callid)
06852 build_callid_pvt(p);
06853 else
06854 ast_string_field_set(p, callid, callid);
06855
06856 ast_string_field_set(p, mohinterpret, default_mohinterpret);
06857 ast_string_field_set(p, mohsuggest, default_mohsuggest);
06858 p->capability = global_capability;
06859 p->allowtransfer = global_allowtransfer;
06860 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
06861 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
06862 p->noncodeccapability |= AST_RTP_DTMF;
06863 if (p->udptl) {
06864 p->t38_maxdatagram = global_t38_maxdatagram;
06865 set_t38_capabilities(p);
06866 }
06867 ast_string_field_set(p, context, default_context);
06868 ast_string_field_set(p, parkinglot, default_parkinglot);
06869
06870 AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
06871
06872
06873
06874 ao2_t_link(dialogs, p, "link pvt into dialogs table");
06875
06876 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");
06877 return p;
06878 }
06879
06880
06881 struct find_call_cb_arg {
06882 enum sipmethod method;
06883 const char *callid;
06884 const char *fromtag;
06885 const char *totag;
06886 const char *tag;
06887 };
06888
06889
06890
06891
06892
06893 static int find_call_cb(void *__pvt, void *__arg, int flags)
06894 {
06895 struct sip_pvt *p = __pvt;
06896 struct find_call_cb_arg *arg = __arg;
06897
06898 int found = FALSE;
06899
06900 if (!ast_strlen_zero(p->callid)) {
06901 if (arg->method == SIP_REGISTER)
06902 found = (!strcmp(p->callid, arg->callid));
06903 else {
06904 found = !strcmp(p->callid, arg->callid);
06905 if (pedanticsipchecking && found) {
06906 found = ast_strlen_zero(arg->tag) || ast_strlen_zero(p->theirtag) || !ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED) || !strcmp(p->theirtag, arg->tag);
06907 }
06908 }
06909
06910 ast_debug(5, "= %s Their Call ID: %s Their Tag %s Our tag: %s\n", found ? "Found" : "No match", p->callid, p->theirtag, p->tag);
06911
06912
06913 if (pedanticsipchecking && found && arg->method != SIP_RESPONSE) {
06914 if (p->tag[0] == '\0' && arg->totag[0]) {
06915
06916 found = FALSE;
06917 } else if (arg->totag[0]) {
06918 if (strcmp(arg->totag, p->tag)) {
06919 found = FALSE;
06920 }
06921 }
06922 if (!found)
06923 ast_debug(5, "= Being pedantic: This is not our match on request: Call ID: %s Ourtag <null> Totag %s Method %s\n", p->callid, arg->totag, sip_methods[arg->method].text);
06924 }
06925 }
06926 return found;
06927 }
06928
06929
06930
06931
06932
06933
06934 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
06935 {
06936 struct sip_pvt *p = NULL;
06937 char *tag = "";
06938 char totag[128];
06939 char fromtag[128];
06940 struct find_call_cb_arg arg;
06941 const char *callid = get_header(req, "Call-ID");
06942 const char *from = get_header(req, "From");
06943 const char *to = get_header(req, "To");
06944 const char *cseq = get_header(req, "Cseq");
06945 struct sip_pvt *sip_pvt_ptr;
06946
06947
06948
06949 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
06950 ast_strlen_zero(from) || ast_strlen_zero(cseq))
06951 return NULL;
06952
06953 arg.method = req->method;
06954 arg.callid = callid;
06955 arg.fromtag = fromtag;
06956 arg.totag = totag;
06957 arg.tag = "";
06958
06959 if (pedanticsipchecking) {
06960
06961
06962
06963
06964
06965
06966 if (gettag(req, "To", totag, sizeof(totag)))
06967 req->has_to_tag = 1;
06968 gettag(req, "From", fromtag, sizeof(fromtag));
06969
06970 tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
06971
06972 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);
06973
06974
06975 if (ast_strlen_zero(fromtag)) {
06976 ast_debug(5, "%s request has no from tag, dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
06977 return NULL;
06978 }
06979
06980 if (ast_strlen_zero(totag) && (req->method == SIP_ACK || req->method == SIP_BYE || req->method == SIP_INFO )) {
06981 ast_debug(5, "%s must have a to tag. dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
06982 return NULL;
06983 }
06984 }
06985
06986 restartsearch:
06987 if (!pedanticsipchecking) {
06988 struct sip_pvt tmp_dialog = {
06989 .callid = callid,
06990 };
06991 sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find in dialogs");
06992 if (sip_pvt_ptr) {
06993
06994 sip_pvt_lock(sip_pvt_ptr);
06995 return sip_pvt_ptr;
06996 }
06997 } else {
06998 ao2_lock(dialogs);
06999 p = ao2_t_callback(dialogs, 0 , find_call_cb, &arg, "pedantic linear search for dialog");
07000 if (p) {
07001 if (sip_pvt_trylock(p)) {
07002 ao2_unlock(dialogs);
07003 usleep(1);
07004 goto restartsearch;
07005 }
07006 ao2_unlock(dialogs);
07007 return p;
07008 }
07009 ao2_unlock(dialogs);
07010 }
07011
07012
07013 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
07014 if (intended_method == SIP_REFER) {
07015
07016 transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
07017 } else if (intended_method == SIP_NOTIFY) {
07018
07019
07020 transmit_response_using_temp(callid, sin, 1, intended_method, req, "489 Bad event");
07021 } else {
07022
07023 if ((p = sip_alloc(callid, sin, 1, intended_method, req))) {
07024
07025 sip_pvt_lock(p);
07026 } else {
07027
07028
07029
07030
07031
07032
07033
07034
07035 transmit_response_using_temp(callid, sin, 1, intended_method, req, "500 Server internal error");
07036 ast_debug(4, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
07037 }
07038 }
07039 return p;
07040 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
07041
07042 transmit_response_using_temp(callid, sin, 1, intended_method, req, "501 Method Not Implemented");
07043 ast_debug(2, "Got a request with unsupported SIP method.\n");
07044 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
07045
07046 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 Call leg/transaction does not exist");
07047 ast_debug(2, "That's odd... Got a request in unknown dialog. Callid %s\n", callid ? callid : "<unknown>");
07048 }
07049
07050
07051 if (intended_method == SIP_RESPONSE)
07052 ast_debug(2, "That's odd... Got a response on a call we dont know about. Callid %s\n", callid ? callid : "<unknown>");
07053
07054 return NULL;
07055 }
07056
07057
07058 static int sip_register(const char *value, int lineno)
07059 {
07060 struct sip_registry *reg;
07061 int portnum = 0;
07062 enum sip_transport transport = SIP_TRANSPORT_UDP;
07063 char buf[256] = "";
07064 char *username = NULL;
07065 char *hostname=NULL, *secret=NULL, *authuser=NULL, *expire=NULL, *tmp=NULL;
07066 char *callback=NULL, *peername=NULL;
07067
07068 if (!value)
07069 return -1;
07070 ast_copy_string(buf, value, sizeof(buf));
07071 tmp = strrchr(buf, '@');
07072
07073
07074 expire = strchr(tmp, '~');
07075 if (expire)
07076 *expire++ = '\0';
07077 callback = strrchr(tmp, '/');
07078 if (callback)
07079 *callback++ = '\0';
07080 if (ast_strlen_zero(callback))
07081 callback = "s";
07082
07083
07084 tmp = strchr(buf, '?');
07085 if (tmp) {
07086 *tmp++ = '\0';
07087 peername = buf;
07088 } else {
07089 tmp = buf;
07090 }
07091
07092 sip_parse_host(tmp, lineno, &username, &portnum, &transport);
07093
07094
07095 hostname = strrchr(username, '@');
07096 if (hostname)
07097 *hostname++ = '\0';
07098 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
07099 ast_log(LOG_WARNING, "Format for registration is [transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry] at line %d\n", lineno);
07100 return -1;
07101 }
07102
07103 secret = strchr(username, ':');
07104 if (secret) {
07105 *secret++ = '\0';
07106 authuser = strchr(secret, ':');
07107 if (authuser)
07108 *authuser++ = '\0';
07109 }
07110
07111 if (!(reg = ast_calloc(1, sizeof(*reg)))) {
07112 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
07113 return -1;
07114 }
07115
07116 if (ast_string_field_init(reg, 256)) {
07117 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry strings\n");
07118 ast_free(reg);
07119 return -1;
07120 }
07121
07122 ast_atomic_fetchadd_int(®objs, 1);
07123 ASTOBJ_INIT(reg);
07124 ast_string_field_set(reg, callback, callback);
07125 if (!ast_strlen_zero(username))
07126 ast_string_field_set(reg, username, username);
07127 if (hostname)
07128 ast_string_field_set(reg, hostname, hostname);
07129 if (authuser)
07130 ast_string_field_set(reg, authuser, authuser);
07131 if (secret)
07132 ast_string_field_set(reg, secret, secret);
07133 if (peername) {
07134 ast_string_field_set(reg, peername, peername);
07135 }
07136 reg->transport = transport;
07137 reg->expire = -1;
07138 reg->configured_expiry = (expire ? atoi(expire) : default_expiry);
07139 reg->expiry = reg->configured_expiry;
07140 reg->timeout = -1;
07141 reg->refresh = reg->expiry;
07142 reg->portno = portnum;
07143 reg->callid_valid = FALSE;
07144 reg->ocseq = INITIAL_CSEQ;
07145 ASTOBJ_CONTAINER_LINK(®l, reg);
07146 registry_unref(reg, "unref the reg pointer");
07147 return 0;
07148 }
07149
07150
07151
07152 static int lws2sws(char *msgbuf, int len)
07153 {
07154 int h = 0, t = 0;
07155 int lws = 0;
07156
07157 for (; h < len;) {
07158
07159 if (msgbuf[h] == '\r') {
07160 h++;
07161 continue;
07162 }
07163
07164 if (msgbuf[h] == '\n') {
07165
07166 if (h + 1 == len)
07167 break;
07168
07169 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
07170
07171 h++;
07172 continue;
07173 }
07174
07175 msgbuf[t++] = msgbuf[h++];
07176 lws = 0;
07177 continue;
07178 }
07179 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
07180 if (lws) {
07181 h++;
07182 continue;
07183 }
07184 msgbuf[t++] = msgbuf[h++];
07185 lws = 1;
07186 continue;
07187 }
07188 msgbuf[t++] = msgbuf[h++];
07189 if (lws)
07190 lws = 0;
07191 }
07192 msgbuf[t] = '\0';
07193 return t;
07194 }
07195
07196
07197
07198
07199 static int parse_request(struct sip_request *req)
07200 {
07201 char *c = req->data->str;
07202 ptrdiff_t *dst = req->header;
07203 int i = 0, lim = SIP_MAX_HEADERS - 1;
07204 unsigned int skipping_headers = 0;
07205 ptrdiff_t current_header_offset = 0;
07206 char *previous_header = "";
07207
07208 req->header[0] = 0;
07209 req->headers = -1;
07210 for (; *c; c++) {
07211 if (*c == '\r') {
07212 *c = '\0';
07213 } else if (*c == '\n') {
07214 *c = '\0';
07215 current_header_offset = (c + 1) - req->data->str;
07216 previous_header = req->data->str + dst[i];
07217 if (skipping_headers) {
07218
07219
07220
07221 if (ast_strlen_zero(previous_header)) {
07222 skipping_headers = 0;
07223 }
07224 dst[i] = current_header_offset;
07225 continue;
07226 }
07227 if (sipdebug) {
07228 ast_debug(4, "%7s %2d [%3d]: %s\n",
07229 req->headers < 0 ? "Header" : "Body",
07230 i, (int) strlen(previous_header), previous_header);
07231 }
07232 if (ast_strlen_zero(previous_header) && req->headers < 0) {
07233 req->headers = i;
07234 dst = req->line;
07235 i = 0;
07236 lim = SIP_MAX_LINES - 1;
07237 } else {
07238 if (i++ == lim) {
07239
07240
07241
07242 if (req->headers != -1) {
07243 break;
07244 } else {
07245 req->headers = i;
07246 dst = req->line;
07247 i = 0;
07248 lim = SIP_MAX_LINES - 1;
07249 skipping_headers = 1;
07250 }
07251 }
07252 }
07253 dst[i] = current_header_offset;
07254 }
07255 }
07256
07257
07258
07259
07260
07261
07262 previous_header = req->data->str + dst[i];
07263 if ((i < lim) && !ast_strlen_zero(previous_header)) {
07264 if (sipdebug) {
07265 ast_debug(4, "%7s %2d [%3d]: %s\n",
07266 req->headers < 0 ? "Header" : "Body",
07267 i, (int) strlen(previous_header), previous_header );
07268 }
07269 i++;
07270 }
07271
07272
07273 if (req->headers >= 0) {
07274 req->lines = i;
07275 } else {
07276 req->headers = i;
07277 req->lines = 0;
07278
07279 req->line[0] = req->data->used;
07280 }
07281
07282 if (*c) {
07283 ast_log(LOG_WARNING, "Too many lines, skipping <%s>\n", c);
07284 }
07285
07286
07287 return determine_firstline_parts(req);
07288 }
07289
07290
07291
07292
07293
07294
07295
07296
07297
07298 static int find_sdp(struct sip_request *req)
07299 {
07300 const char *content_type;
07301 const char *content_length;
07302 const char *search;
07303 char *boundary;
07304 unsigned int x;
07305 int boundaryisquoted = FALSE;
07306 int found_application_sdp = FALSE;
07307 int found_end_of_headers = FALSE;
07308
07309 content_length = get_header(req, "Content-Length");
07310
07311 if (!ast_strlen_zero(content_length)) {
07312 if (sscanf(content_length, "%30u", &x) != 1) {
07313 ast_log(LOG_WARNING, "Invalid Content-Length: %s\n", content_length);
07314 return 0;
07315 }
07316
07317
07318
07319 if (x == 0)
07320 return 0;
07321 }
07322
07323 content_type = get_header(req, "Content-Type");
07324
07325
07326 if (!strncasecmp(content_type, "application/sdp", 15)) {
07327 req->sdp_start = 0;
07328 req->sdp_count = req->lines;
07329 return req->lines ? 1 : 0;
07330 }
07331
07332
07333 if (strncasecmp(content_type, "multipart/mixed", 15))
07334 return 0;
07335
07336
07337 if ((search = strcasestr(content_type, ";boundary=")))
07338 search += 10;
07339 else if ((search = strcasestr(content_type, "; boundary=")))
07340 search += 11;
07341 else
07342 return 0;
07343
07344 if (ast_strlen_zero(search))
07345 return 0;
07346
07347
07348 if (*search == '\"') {
07349 search++;
07350 boundaryisquoted = TRUE;
07351 }
07352
07353
07354
07355 boundary = ast_strdupa(search - 2);
07356 boundary[0] = boundary[1] = '-';
07357
07358 if (boundaryisquoted)
07359 boundary[strlen(boundary) - 1] = '\0';
07360
07361
07362
07363
07364 for (x = 0; x < (req->lines); x++) {
07365 char *line = REQ_OFFSET_TO_STR(req, line[x]);
07366 if (!strncasecmp(line, boundary, strlen(boundary))){
07367 if (found_application_sdp && found_end_of_headers) {
07368 req->sdp_count = (x - 1) - req->sdp_start;
07369 return 1;
07370 }
07371 found_application_sdp = FALSE;
07372 }
07373 if (!strcasecmp(line, "Content-Type: application/sdp"))
07374 found_application_sdp = TRUE;
07375
07376 if (ast_strlen_zero(line)) {
07377 if (found_application_sdp && !found_end_of_headers){
07378 req->sdp_start = x;
07379 found_end_of_headers = TRUE;
07380 }
07381 }
07382 }
07383 if (found_application_sdp && found_end_of_headers) {
07384 req->sdp_count = x - req->sdp_start;
07385 return TRUE;
07386 }
07387 return FALSE;
07388 }
07389
07390 enum media_type {
07391 SDP_AUDIO,
07392 SDP_VIDEO,
07393 SDP_IMAGE,
07394 SDP_TEXT,
07395 };
07396
07397 static int get_ip_and_port_from_sdp(struct sip_request *req, const enum media_type media, struct sockaddr_in *sin)
07398 {
07399 const char *m;
07400 const char *c;
07401 int miterator = req->sdp_start;
07402 int citerator = req->sdp_start;
07403 int x = 0;
07404 int numberofports;
07405 int len;
07406 char host[258] = "";
07407 struct ast_hostent audiohp;
07408 struct hostent *hp;
07409
07410 c = get_sdp_iterate(&citerator, req, "c");
07411 if (sscanf(c, "IN IP4 %256s", host) != 1) {
07412 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
07413
07414 }
07415
07416 for (m = get_sdp_iterate(&miterator, req, "m"); !ast_strlen_zero(m); m = get_sdp_iterate(&miterator, req, "m")) {
07417 if ((media == SDP_AUDIO && ((sscanf(m, "audio %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
07418 (sscanf(m, "audio %30u RTP/AVP %n", &x, &len) == 1 && len > 0))) ||
07419 (media == SDP_VIDEO && ((sscanf(m, "video %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
07420 (sscanf(m, "video %30u RTP/AVP %n", &x, &len) == 1 && len > 0)))) {
07421
07422
07423
07424
07425 c = get_sdp_iterate(&citerator, req, "c");
07426 if (!ast_strlen_zero(c)) {
07427 sscanf(c, "IN IP4 %256s", host);
07428 }
07429 break;
07430 }
07431 }
07432
07433 if (ast_strlen_zero(host) || x == 0) {
07434 ast_log(LOG_WARNING, "Failed to read an alternate host or port in SDP. Expect %s problems\n", media == SDP_AUDIO ? "audio" : "video");
07435 return -1;
07436 }
07437
07438 hp = ast_gethostbyname(host, &audiohp);
07439 if (!hp) {
07440 ast_log(LOG_WARNING, "Could not look up IP address of alternate hostname. Expect %s problems\n", media == SDP_AUDIO? "audio" : "video");
07441 return -1;
07442 }
07443
07444 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
07445 sin->sin_port = htons(x);
07446 return 0;
07447 }
07448
07449
07450
07451
07452
07453
07454 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
07455 {
07456
07457 int start = req->sdp_start;
07458 int next = start;
07459 int iterator = start;
07460
07461
07462 char type = '\0';
07463 const char *value = NULL;
07464 const char *m = NULL;
07465 const char *nextm = NULL;
07466 int len = -1;
07467
07468
07469 struct ast_hostent sessionhp;
07470 struct ast_hostent audiohp;
07471 struct ast_hostent videohp;
07472 struct ast_hostent texthp;
07473 struct ast_hostent imagehp;
07474 struct hostent *hp = NULL;
07475 struct hostent *vhp = NULL;
07476 struct hostent *thp = NULL;
07477 struct hostent *ihp = NULL;
07478 int portno = -1;
07479 int vportno = -1;
07480 int tportno = -1;
07481 int udptlportno = -1;
07482 struct sockaddr_in sin;
07483 struct sockaddr_in vsin;
07484 struct sockaddr_in isin;
07485 struct sockaddr_in tsin;
07486
07487
07488 int peercapability = 0, peernoncodeccapability = 0;
07489 int vpeercapability = 0, vpeernoncodeccapability = 0;
07490 int tpeercapability = 0, tpeernoncodeccapability = 0;
07491
07492 struct ast_rtp *newaudiortp, *newvideortp, *newtextrtp;
07493 int newjointcapability;
07494 int newpeercapability;
07495 int newnoncodeccapability;
07496
07497 const char *codecs;
07498 int codec;
07499
07500
07501 int sendonly = -1;
07502 int vsendonly = -1;
07503 int numberofports;
07504 int numberofmediastreams = 0;
07505 int last_rtpmap_codec = 0;
07506 int red_data_pt[10];
07507 int red_num_gen = 0;
07508 char red_fmtp[100] = "empty";
07509 int debug = sip_debug_test_pvt(p);
07510
07511
07512 char buf[SIPBUFSIZE];
07513
07514
07515
07516 if (!p->rtp) {
07517 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
07518 return -1;
07519 }
07520
07521
07522 #ifdef LOW_MEMORY
07523 newaudiortp = ast_threadstorage_get(&ts_audio_rtp, ast_rtp_alloc_size());
07524 #else
07525 newaudiortp = alloca(ast_rtp_alloc_size());
07526 #endif
07527 memset(newaudiortp, 0, ast_rtp_alloc_size());
07528 ast_rtp_new_init(newaudiortp);
07529 ast_rtp_pt_clear(newaudiortp);
07530
07531 #ifdef LOW_MEMORY
07532 newvideortp = ast_threadstorage_get(&ts_video_rtp, ast_rtp_alloc_size());
07533 #else
07534 newvideortp = alloca(ast_rtp_alloc_size());
07535 #endif
07536 memset(newvideortp, 0, ast_rtp_alloc_size());
07537 ast_rtp_new_init(newvideortp);
07538 ast_rtp_pt_clear(newvideortp);
07539
07540 #ifdef LOW_MEMORY
07541 newtextrtp = ast_threadstorage_get(&ts_text_rtp, ast_rtp_alloc_size());
07542 #else
07543 newtextrtp = alloca(ast_rtp_alloc_size());
07544 #endif
07545 memset(newtextrtp, 0, ast_rtp_alloc_size());
07546 ast_rtp_new_init(newtextrtp);
07547 ast_rtp_pt_clear(newtextrtp);
07548
07549
07550 p->lastrtprx = p->lastrtptx = time(NULL);
07551
07552 memset(p->offered_media, 0, sizeof(p->offered_media));
07553
07554
07555
07556 p->novideo = TRUE;
07557 p->notext = TRUE;
07558
07559 if (p->vrtp)
07560 ast_rtp_pt_clear(newvideortp);
07561
07562 if (p->trtp)
07563 ast_rtp_pt_clear(newtextrtp);
07564
07565
07566 nextm = get_sdp_iterate(&next, req, "m");
07567 if (ast_strlen_zero(nextm)) {
07568 ast_log(LOG_WARNING, "Insufficient information for SDP (m= not found)\n");
07569 return -1;
07570 }
07571
07572
07573 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
07574 int processed = FALSE;
07575 switch (type) {
07576 case 'o':
07577
07578
07579
07580 if (!process_sdp_o(value, p))
07581 return (p->session_modify == FALSE) ? 0 : -1;
07582 break;
07583 case 'c':
07584 if (process_sdp_c(value, &sessionhp)) {
07585 processed = TRUE;
07586 hp = &sessionhp.hp;
07587 vhp = hp;
07588 thp = hp;
07589 ihp = hp;
07590 }
07591 break;
07592 case 'a':
07593 if (process_sdp_a_sendonly(value, &sendonly)) {
07594 processed = TRUE;
07595 vsendonly = sendonly;
07596 }
07597 else if (process_sdp_a_audio(value, p, newaudiortp, &last_rtpmap_codec))
07598 processed = TRUE;
07599 else if (process_sdp_a_video(value, p, newvideortp, &last_rtpmap_codec))
07600 processed = TRUE;
07601 else if (process_sdp_a_text(value, p, newtextrtp, red_fmtp, &red_num_gen, red_data_pt, &last_rtpmap_codec))
07602 processed = TRUE;
07603 else if (process_sdp_a_image(value, p))
07604 processed = TRUE;
07605 break;
07606 }
07607
07608 if (option_debug > 2)
07609 ast_log(LOG_DEBUG, "Processing session-level SDP %c=%s... %s\n", type, value, (processed == TRUE)? "OK." : "UNSUPPORTED.");
07610 }
07611
07612
07613
07614
07615 while (!ast_strlen_zero(nextm)) {
07616 int audio = FALSE;
07617 int video = FALSE;
07618 int image = FALSE;
07619 int text = FALSE;
07620 int x;
07621
07622 numberofports = 1;
07623 len = -1;
07624 start = next;
07625 m = nextm;
07626 iterator = next;
07627 nextm = get_sdp_iterate(&next, req, "m");
07628
07629
07630 if ((sscanf(m, "audio %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
07631 (sscanf(m, "audio %30u RTP/AVP %n", &x, &len) == 1 && len > 0)) {
07632 audio = TRUE;
07633 p->offered_media[SDP_AUDIO].offered = TRUE;
07634 numberofmediastreams++;
07635 portno = x;
07636
07637
07638 codecs = m + len;
07639 ast_copy_string(p->offered_media[SDP_AUDIO].text, codecs, sizeof(p->offered_media[SDP_AUDIO].text));
07640 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
07641 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
07642 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
07643 return -1;
07644 }
07645 if (debug)
07646 ast_verbose("Found RTP audio format %d\n", codec);
07647
07648 ast_rtp_set_m_type(newaudiortp, codec);
07649 }
07650
07651 } else if ((sscanf(m, "video %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
07652 (sscanf(m, "video %30u RTP/AVP %n", &x, &len) == 1 && len >= 0)) {
07653 video = TRUE;
07654 p->novideo = FALSE;
07655 p->offered_media[SDP_VIDEO].offered = TRUE;
07656 numberofmediastreams++;
07657 vportno = x;
07658
07659
07660 codecs = m + len;
07661 ast_copy_string(p->offered_media[SDP_VIDEO].text, codecs, sizeof(p->offered_media[SDP_VIDEO].text));
07662 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
07663 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
07664 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
07665 return -1;
07666 }
07667 if (debug)
07668 ast_verbose("Found RTP video format %d\n", codec);
07669 ast_rtp_set_m_type(newvideortp, codec);
07670 }
07671
07672 } else if ((sscanf(m, "text %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
07673 (sscanf(m, "text %30u RTP/AVP %n", &x, &len) == 1 && len > 0)) {
07674 text = TRUE;
07675 p->notext = FALSE;
07676 p->offered_media[SDP_TEXT].offered = TRUE;
07677 numberofmediastreams++;
07678 tportno = x;
07679
07680
07681 codecs = m + len;
07682 ast_copy_string(p->offered_media[SDP_TEXT].text, codecs, sizeof(p->offered_media[SDP_TEXT].text));
07683 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
07684 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
07685 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
07686 return -1;
07687 }
07688 if (debug)
07689 ast_verbose("Found RTP text format %d\n", codec);
07690 ast_rtp_set_m_type(newtextrtp, codec);
07691 }
07692
07693 } else if (p->udptl && ((sscanf(m, "image %30u udptl t38%n", &x, &len) == 1 && len > 0) ||
07694 (sscanf(m, "image %30u UDPTL t38%n", &x, &len) == 1 && len > 0) )) {
07695 image = TRUE;
07696 if (debug)
07697 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
07698 p->offered_media[SDP_IMAGE].offered = TRUE;
07699 udptlportno = x;
07700 numberofmediastreams++;
07701
07702 if (p->t38.state != T38_ENABLED) {
07703 memset(&p->t38.their_parms, 0, sizeof(p->t38.their_parms));
07704 }
07705 } else {
07706 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
07707 continue;
07708 }
07709
07710
07711 if (numberofports > 1)
07712 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
07713
07714
07715
07716
07717 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
07718 int processed = FALSE;
07719
07720 switch (type) {
07721 case 'c':
07722 if (audio) {
07723 if (process_sdp_c(value, &audiohp)) {
07724 processed = TRUE;
07725 hp = &audiohp.hp;
07726 }
07727 } else if (video) {
07728 if (process_sdp_c(value, &videohp)) {
07729 processed = TRUE;
07730 vhp = &videohp.hp;
07731 }
07732 } else if (text) {
07733 if (process_sdp_c(value, &texthp)) {
07734 processed = TRUE;
07735 thp = &texthp.hp;
07736 }
07737 } else if (image) {
07738 if (process_sdp_c(value, &imagehp)) {
07739 processed = TRUE;
07740 ihp = &imagehp.hp;
07741 }
07742 }
07743 break;
07744 case 'a':
07745
07746 if (audio) {
07747 if (process_sdp_a_sendonly(value, &sendonly))
07748 processed = TRUE;
07749 else if (process_sdp_a_audio(value, p, newaudiortp, &last_rtpmap_codec))
07750 processed = TRUE;
07751 }
07752
07753 else if (video) {
07754 if (process_sdp_a_sendonly(value, &vsendonly))
07755 processed = TRUE;
07756 else if (process_sdp_a_video(value, p, newvideortp, &last_rtpmap_codec))
07757 processed = TRUE;
07758 }
07759
07760 else if (text) {
07761 if (process_sdp_a_text(value, p, newtextrtp, red_fmtp, &red_num_gen, red_data_pt, &last_rtpmap_codec))
07762 processed = TRUE;
07763 }
07764
07765 else if (image) {
07766 if (process_sdp_a_image(value, p))
07767 processed = TRUE;
07768 }
07769 break;
07770 }
07771
07772 if (option_debug > 2)
07773 ast_log(LOG_DEBUG, "Processing media-level (%s) SDP %c=%s... %s\n",
07774 (audio == TRUE)? "audio" : (video == TRUE)? "video" : "image",
07775 type, value,
07776 (processed == TRUE)? "OK." : "UNSUPPORTED.");
07777 }
07778 }
07779
07780
07781
07782 if (!hp && !vhp && !thp && !ihp) {
07783 ast_log(LOG_WARNING, "Insufficient information in SDP (c=)...\n");
07784 return -1;
07785 }
07786
07787 if (portno == -1 && vportno == -1 && udptlportno == -1 && tportno == -1)
07788
07789
07790 return -2;
07791
07792 if (numberofmediastreams > 3)
07793
07794 return -3;
07795
07796 if (udptlportno == -1) {
07797 change_t38_state(p, T38_DISABLED);
07798 }
07799
07800
07801 ast_rtp_get_current_formats(newaudiortp, &peercapability, &peernoncodeccapability);
07802 ast_rtp_get_current_formats(newvideortp, &vpeercapability, &vpeernoncodeccapability);
07803 ast_rtp_get_current_formats(newtextrtp, &tpeercapability, &tpeernoncodeccapability);
07804
07805 newjointcapability = p->capability & (peercapability | vpeercapability | tpeercapability);
07806 newpeercapability = (peercapability | vpeercapability | tpeercapability);
07807 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
07808
07809
07810 if (debug) {
07811
07812 char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE], s5[SIPBUFSIZE];
07813
07814 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s/text=%s, combined - %s\n",
07815 ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
07816 ast_getformatname_multiple(s2, SIPBUFSIZE, peercapability),
07817 ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
07818 ast_getformatname_multiple(s4, SIPBUFSIZE, tpeercapability),
07819 ast_getformatname_multiple(s5, SIPBUFSIZE, newjointcapability));
07820
07821 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
07822 ast_rtp_lookup_mime_multiple(s1, SIPBUFSIZE, p->noncodeccapability, 0, 0),
07823 ast_rtp_lookup_mime_multiple(s2, SIPBUFSIZE, peernoncodeccapability, 0, 0),
07824 ast_rtp_lookup_mime_multiple(s3, SIPBUFSIZE, newnoncodeccapability, 0, 0));
07825 }
07826 if (!newjointcapability && (portno != -1)) {
07827 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
07828
07829 return -1;
07830 }
07831
07832
07833 if (p->rtp) {
07834 if (portno > 0) {
07835 sin.sin_family = AF_INET;
07836 sin.sin_port = htons(portno);
07837 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
07838 ast_rtp_set_peer(p->rtp, &sin);
07839 if (debug)
07840 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
07841
07842
07843 p->jointcapability = newjointcapability;
07844 p->peercapability = newpeercapability;
07845 p->jointnoncodeccapability = newnoncodeccapability;
07846
07847 ast_rtp_pt_copy(p->rtp, newaudiortp);
07848
07849 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
07850 ast_clear_flag(&p->flags[0], SIP_DTMF);
07851 if (newnoncodeccapability & AST_RTP_DTMF) {
07852
07853 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
07854
07855 ast_rtp_setdtmf(p->rtp, 1);
07856 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
07857 } else {
07858 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
07859 }
07860 }
07861 } else if (udptlportno > 0) {
07862 if (debug)
07863 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session.\n");
07864 } else {
07865 ast_rtp_stop(p->rtp);
07866 if (debug)
07867 ast_verbose("Peer doesn't provide audio\n");
07868 }
07869 }
07870
07871
07872 if (p->vrtp) {
07873 if (vportno > 0) {
07874 vsin.sin_family = AF_INET;
07875 vsin.sin_port = htons(vportno);
07876 memcpy(&vsin.sin_addr, vhp->h_addr, sizeof(vsin.sin_addr));
07877 ast_rtp_set_peer(p->vrtp, &vsin);
07878 if (debug)
07879 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
07880 ast_rtp_pt_copy(p->vrtp, newvideortp);
07881 } else {
07882 ast_rtp_stop(p->vrtp);
07883 if (debug)
07884 ast_verbose("Peer doesn't provide video\n");
07885 }
07886 }
07887
07888
07889 if (p->trtp) {
07890 if (tportno > 0) {
07891 tsin.sin_family = AF_INET;
07892 tsin.sin_port = htons(tportno);
07893 memcpy(&tsin.sin_addr, thp->h_addr, sizeof(tsin.sin_addr));
07894 ast_rtp_set_peer(p->trtp, &tsin);
07895 if (debug)
07896 ast_verbose("Peer T.140 RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
07897 if ((p->jointcapability & AST_FORMAT_T140RED)) {
07898 p->red = 1;
07899 rtp_red_init(p->trtp, 300, red_data_pt, 2);
07900 } else {
07901 p->red = 0;
07902 }
07903 ast_rtp_pt_copy(p->trtp, newtextrtp);
07904 } else {
07905 ast_rtp_stop(p->trtp);
07906 if (debug)
07907 ast_verbose("Peer doesn't provide T.140\n");
07908 }
07909 }
07910
07911 if (p->udptl) {
07912 if (udptlportno > 0) {
07913 isin.sin_family = AF_INET;
07914 isin.sin_port = htons(udptlportno);
07915 if (ast_test_flag(&p->flags[0], SIP_NAT) && ast_test_flag(&p->flags[1], SIP_PAGE2_UDPTL_DESTINATION)) {
07916 struct sockaddr_in remote_address = { 0, };
07917 ast_rtp_get_peer(p->rtp, &remote_address);
07918 if (remote_address.sin_addr.s_addr) {
07919 memcpy(&isin, &remote_address, sizeof(isin));
07920 if (debug) {
07921 ast_log(LOG_DEBUG, "Peer T.38 UDPTL is set behind NAT and with destination, destination address now %s\n", ast_inet_ntoa(isin.sin_addr));
07922 }
07923 }
07924 } else {
07925 memcpy(&isin.sin_addr, ihp->h_addr, sizeof(sin.sin_addr));
07926 }
07927 ast_udptl_set_peer(p->udptl, &isin);
07928 if (debug)
07929 ast_debug(1,"Peer T.38 UDPTL is at port %s:%d\n", ast_inet_ntoa(isin.sin_addr), ntohs(isin.sin_port));
07930
07931
07932 if (!ast_udptl_get_far_max_datagram(p->udptl)) {
07933
07934 ast_udptl_set_far_max_datagram(p->udptl, 0);
07935 }
07936
07937
07938 if ((t38action == SDP_T38_ACCEPT) &&
07939 (p->t38.state == T38_LOCAL_REINVITE)) {
07940 change_t38_state(p, T38_ENABLED);
07941 } else if ((t38action == SDP_T38_INITIATE) &&
07942 p->owner && p->lastinvite) {
07943 change_t38_state(p, T38_PEER_REINVITE);
07944 }
07945 } else {
07946 ast_udptl_stop(p->udptl);
07947 if (debug)
07948 ast_debug(1, "Peer doesn't provide T.38 UDPTL\n");
07949 }
07950 }
07951
07952 if ((portno == -1) && (p->t38.state != T38_DISABLED)) {
07953 ast_debug(3, "Have T.38 but no audio, accepting offer anyway\n");
07954 return 0;
07955 }
07956
07957
07958 ast_debug(2, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
07959
07960 if (!p->owner)
07961 return 0;
07962
07963 ast_debug(4, "We have an owner, now see if we need to change this call\n");
07964
07965 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
07966 if (debug) {
07967 char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
07968 ast_debug(1, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
07969 ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
07970 ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
07971 }
07972 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability) | (p->capability & tpeercapability);
07973 ast_set_read_format(p->owner, p->owner->readformat);
07974 ast_set_write_format(p->owner, p->owner->writeformat);
07975 }
07976
07977 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1)) {
07978 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
07979
07980 ast_queue_frame(p->owner, &ast_null_frame);
07981
07982 append_history(p, "Unhold", "%s", req->data->str);
07983 if (global_callevents)
07984 manager_event(EVENT_FLAG_CALL, "Hold",
07985 "Status: Off\r\n"
07986 "Channel: %s\r\n"
07987 "Uniqueid: %s\r\n",
07988 p->owner->name,
07989 p->owner->uniqueid);
07990 if (global_notifyhold)
07991 sip_peer_hold(p, FALSE);
07992 ast_clear_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD);
07993 } else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1)) {
07994 int already_on_hold = ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD);
07995 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
07996 S_OR(p->mohsuggest, NULL),
07997 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
07998 if (sendonly)
07999 ast_rtp_stop(p->rtp);
08000
08001
08002 ast_queue_frame(p->owner, &ast_null_frame);
08003
08004 append_history(p, "Hold", "%s", req->data->str);
08005 if (global_callevents && !ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
08006 manager_event(EVENT_FLAG_CALL, "Hold",
08007 "Status: On\r\n"
08008 "Channel: %s\r\n"
08009 "Uniqueid: %s\r\n",
08010 p->owner->name,
08011 p->owner->uniqueid);
08012 }
08013 if (sendonly == 1)
08014 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
08015 else if (sendonly == 2)
08016 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
08017 else
08018 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
08019 if (global_notifyhold && !already_on_hold)
08020 sip_peer_hold(p, TRUE);
08021 }
08022
08023 return 0;
08024 }
08025
08026 static int process_sdp_o(const char *o, struct sip_pvt *p)
08027 {
08028 char *o_copy;
08029 char *token;
08030 int64_t rua_version;
08031
08032
08033
08034
08035
08036
08037
08038
08039
08040
08041 p->session_modify = TRUE;
08042
08043 if (ast_strlen_zero(o)) {
08044 ast_log(LOG_WARNING, "SDP syntax error. SDP without an o= line\n");
08045 return FALSE;
08046 }
08047
08048 o_copy = ast_strdupa(o);
08049 token = strsep(&o_copy, " ");
08050 if (!o_copy) {
08051 ast_log(LOG_WARNING, "SDP syntax error in o= line username\n");
08052 return FALSE;
08053 }
08054 token = strsep(&o_copy, " ");
08055 if (!o_copy) {
08056 ast_log(LOG_WARNING, "SDP syntax error in o= line session-id\n");
08057 return FALSE;
08058 }
08059 token = strsep(&o_copy, " ");
08060 if (!o_copy) {
08061 ast_log(LOG_WARNING, "SDP syntax error in o= line\n");
08062 return FALSE;
08063 }
08064 if (!sscanf(token, "%30" SCNd64, &rua_version)) {
08065 ast_log(LOG_WARNING, "SDP syntax error in o= line version\n");
08066 return FALSE;
08067 }
08068
08069
08070
08071
08072
08073
08074
08075
08076
08077
08078
08079
08080
08081
08082
08083
08084
08085
08086
08087 if (ast_test_flag(&p->flags[1], SIP_PAGE2_IGNORESDPVERSION) ||
08088 (p->sessionversion_remote < 0) ||
08089 (p->sessionversion_remote < rua_version)) {
08090 p->sessionversion_remote = rua_version;
08091 } else {
08092 if (p->t38.state == T38_LOCAL_REINVITE) {
08093 p->sessionversion_remote = rua_version;
08094 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);
08095 } else {
08096 p->session_modify = FALSE;
08097 ast_debug(2, "Call %s responded to our reinvite without changing SDP version; ignoring SDP.\n", p->callid);
08098 return FALSE;
08099 }
08100 }
08101
08102 return TRUE;
08103 }
08104
08105 static int process_sdp_c(const char *c, struct ast_hostent *ast_hp)
08106 {
08107 char host[258];
08108 struct hostent *hp;
08109
08110
08111 if (sscanf(c, "IN IP4 %255s", host) != 1) {
08112 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
08113 return FALSE;
08114 } else {
08115 if (!(hp = ast_gethostbyname(host, ast_hp))) {
08116 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in c= line, '%s'\n", c);
08117 return FALSE;
08118 }
08119 return TRUE;
08120 }
08121 return FALSE;
08122 }
08123
08124 static int process_sdp_a_sendonly(const char *a, int *sendonly)
08125 {
08126 int found = FALSE;
08127
08128 if (!strcasecmp(a, "sendonly")) {
08129 if (*sendonly == -1)
08130 *sendonly = 1;
08131 found = TRUE;
08132 } else if (!strcasecmp(a, "inactive")) {
08133 if (*sendonly == -1)
08134 *sendonly = 2;
08135 found = TRUE;
08136 } else if (!strcasecmp(a, "sendrecv")) {
08137 if (*sendonly == -1)
08138 *sendonly = 0;
08139 found = TRUE;
08140 }
08141 return found;
08142 }
08143
08144 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp *newaudiortp, int *last_rtpmap_codec)
08145 {
08146 int found = FALSE;
08147 int codec;
08148 char mimeSubtype[128];
08149 int debug = sip_debug_test_pvt(p);
08150
08151 if (!strncasecmp(a, "ptime", 5)) {
08152 char *tmp = strrchr(a, ':');
08153 long int framing = 0;
08154 if (tmp) {
08155 tmp++;
08156 framing = strtol(tmp, NULL, 10);
08157 if (framing == LONG_MIN || framing == LONG_MAX) {
08158 framing = 0;
08159 ast_debug(1, "Can't read framing from SDP: %s\n", a);
08160 }
08161 }
08162 if (framing && p->autoframing) {
08163 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
08164 int codec_n;
08165 int format = 0;
08166 for (codec_n = 0; codec_n < MAX_RTP_PT; codec_n++) {
08167 format = ast_rtp_codec_getformat(codec_n);
08168 if (!format)
08169 continue;
08170 if (option_debug)
08171 ast_log(LOG_DEBUG, "Setting framing for %d to %ld\n", format, framing);
08172 ast_codec_pref_setsize(pref, format, framing);
08173 }
08174 ast_rtp_codec_setpref(p->rtp, pref);
08175 }
08176 found = TRUE;
08177 } else if (sscanf(a, "rtpmap: %30u %127[^/]/", &codec, mimeSubtype) == 2) {
08178
08179 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
08180 if (ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
08181 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0) != -1) {
08182 if (debug)
08183 ast_verbose("Found audio description format %s for ID %d\n", mimeSubtype, codec);
08184
08185 (*last_rtpmap_codec)++;
08186 found = TRUE;
08187 } else {
08188 ast_rtp_unset_m_type(newaudiortp, codec);
08189 if (debug)
08190 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
08191 }
08192 } else {
08193 if (debug)
08194 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
08195 }
08196 }
08197
08198 return found;
08199 }
08200
08201 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp *newvideortp, int *last_rtpmap_codec)
08202 {
08203 int found = FALSE;
08204 int codec;
08205 char mimeSubtype[128];
08206 int debug = sip_debug_test_pvt(p);
08207
08208 if (sscanf(a, "rtpmap: %30u %127[^/]/", &codec, mimeSubtype) == 2) {
08209
08210 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
08211
08212 if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3)) {
08213 if (ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0) != -1) {
08214 if (debug)
08215 ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec);
08216
08217 (*last_rtpmap_codec)++;
08218 found = TRUE;
08219 } else {
08220 ast_rtp_unset_m_type(newvideortp, codec);
08221 if (debug)
08222 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
08223 }
08224 }
08225 } else {
08226 if (debug)
08227 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
08228 }
08229 }
08230
08231 return found;
08232 }
08233
08234 static int process_sdp_a_text(const char *a, struct sip_pvt *p, struct ast_rtp *newtextrtp, char *red_fmtp, int *red_num_gen, int *red_data_pt, int *last_rtpmap_codec)
08235 {
08236 int found = FALSE;
08237 int codec;
08238 char mimeSubtype[128];
08239 char *red_cp;
08240 int debug = sip_debug_test_pvt(p);
08241
08242 if (sscanf(a, "rtpmap: %30u %127[^/]/", &codec, mimeSubtype) == 2) {
08243
08244 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
08245 if (!strncasecmp(mimeSubtype, "T140", 4)) {
08246 if (p->trtp) {
08247
08248 ast_rtp_set_rtpmap_type(newtextrtp, codec, "text", mimeSubtype, 0);
08249 found = TRUE;
08250 }
08251 } else if (!strncasecmp(mimeSubtype, "RED", 3)) {
08252 if (p->trtp) {
08253 ast_rtp_set_rtpmap_type(newtextrtp, codec, "text", mimeSubtype, 0);
08254 sprintf(red_fmtp, "fmtp:%d ", codec);
08255 if (debug)
08256 ast_verbose("RED submimetype has payload type: %d\n", codec);
08257 found = TRUE;
08258 }
08259 }
08260 } else {
08261 if (debug)
08262 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
08263 }
08264 } else if (!strncmp(a, red_fmtp, strlen(red_fmtp))) {
08265
08266 red_cp = &red_fmtp[strlen(red_fmtp)];
08267 strncpy(red_fmtp, a, 100);
08268
08269 sscanf(red_cp, "%30u", &red_data_pt[*red_num_gen]);
08270 red_cp = strtok(red_cp, "/");
08271 while (red_cp && (*red_num_gen)++ < RED_MAX_GENERATION) {
08272 sscanf(red_cp, "%30u", &red_data_pt[*red_num_gen]);
08273 red_cp = strtok(NULL, "/");
08274 }
08275 red_cp = red_fmtp;
08276 found = TRUE;
08277 }
08278
08279 return found;
08280 }
08281
08282 static int process_sdp_a_image(const char *a, struct sip_pvt *p)
08283 {
08284 int found = FALSE;
08285 char s[256];
08286 unsigned int x;
08287
08288 if ((sscanf(a, "T38FaxMaxBuffer:%30u", &x) == 1)) {
08289 ast_debug(3, "MaxBufferSize:%d\n", x);
08290 found = TRUE;
08291 } else if ((sscanf(a, "T38MaxBitRate:%30u", &x) == 1) || (sscanf(a, "T38FaxMaxRate:%30u", &x) == 1)) {
08292 ast_debug(3, "T38MaxBitRate: %d\n", x);
08293 switch (x) {
08294 case 14400:
08295 p->t38.their_parms.rate = AST_T38_RATE_14400;
08296 break;
08297 case 12000:
08298 p->t38.their_parms.rate = AST_T38_RATE_12000;
08299 break;
08300 case 9600:
08301 p->t38.their_parms.rate = AST_T38_RATE_9600;
08302 break;
08303 case 7200:
08304 p->t38.their_parms.rate = AST_T38_RATE_7200;
08305 break;
08306 case 4800:
08307 p->t38.their_parms.rate = AST_T38_RATE_4800;
08308 break;
08309 case 2400:
08310 p->t38.their_parms.rate = AST_T38_RATE_2400;
08311 break;
08312 }
08313 found = TRUE;
08314 } else if ((sscanf(a, "T38FaxVersion:%30u", &x) == 1)) {
08315 ast_debug(3, "FaxVersion: %u\n", x);
08316 p->t38.their_parms.version = x;
08317 found = TRUE;
08318 } else if ((sscanf(a, "T38FaxMaxDatagram:%30u", &x) == 1) || (sscanf(a, "T38MaxDatagram:%30u", &x) == 1)) {
08319
08320 if (((signed int) p->t38_maxdatagram >= 0) && ((unsigned int) p->t38_maxdatagram > x)) {
08321 ast_debug(1, "Overriding T38FaxMaxDatagram '%d' with '%d'\n", x, p->t38_maxdatagram);
08322 x = p->t38_maxdatagram;
08323 }
08324 ast_debug(3, "FaxMaxDatagram: %u\n", x);
08325 ast_udptl_set_far_max_datagram(p->udptl, x);
08326 found = TRUE;
08327 } else if ((strncmp(a, "T38FaxFillBitRemoval", 20) == 0)) {
08328 if (sscanf(a, "T38FaxFillBitRemoval:%30u", &x) == 1) {
08329 ast_debug(3, "FillBitRemoval: %d\n", x);
08330 if (x == 1) {
08331 p->t38.their_parms.fill_bit_removal = TRUE;
08332 }
08333 } else {
08334 ast_debug(3, "FillBitRemoval\n");
08335 p->t38.their_parms.fill_bit_removal = TRUE;
08336 }
08337 found = TRUE;
08338 } else if ((strncmp(a, "T38FaxTranscodingMMR", 20) == 0)) {
08339 if (sscanf(a, "T38FaxTranscodingMMR:%30u", &x) == 1) {
08340 ast_debug(3, "Transcoding MMR: %d\n", x);
08341 if (x == 1) {
08342 p->t38.their_parms.transcoding_mmr = TRUE;
08343 }
08344 } else {
08345 ast_debug(3, "Transcoding MMR\n");
08346 p->t38.their_parms.transcoding_mmr = TRUE;
08347 }
08348 found = TRUE;
08349 } else if ((strncmp(a, "T38FaxTranscodingJBIG", 21) == 0)) {
08350 if (sscanf(a, "T38FaxTranscodingJBIG:%30u", &x) == 1) {
08351 ast_debug(3, "Transcoding JBIG: %d\n", x);
08352 if (x == 1) {
08353 p->t38.their_parms.transcoding_jbig = TRUE;
08354 }
08355 } else {
08356 ast_debug(3, "Transcoding JBIG\n");
08357 p->t38.their_parms.transcoding_jbig = TRUE;
08358 }
08359 found = TRUE;
08360 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
08361 ast_debug(3, "RateManagement: %s\n", s);
08362 if (!strcasecmp(s, "localTCF"))
08363 p->t38.their_parms.rate_management = AST_T38_RATE_MANAGEMENT_LOCAL_TCF;
08364 else if (!strcasecmp(s, "transferredTCF"))
08365 p->t38.their_parms.rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF;
08366 found = TRUE;
08367 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
08368 ast_debug(3, "UDP EC: %s\n", s);
08369 if (!strcasecmp(s, "t38UDPRedundancy")) {
08370 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
08371 } else if (!strcasecmp(s, "t38UDPFEC")) {
08372 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
08373 } else {
08374 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
08375 }
08376 found = TRUE;
08377 }
08378
08379 return found;
08380 }
08381
08382
08383 #ifdef LOW_MEMORY
08384 static void ts_ast_rtp_destroy(void *data)
08385 {
08386 struct ast_rtp *tmp = data;
08387 ast_rtp_destroy(tmp);
08388 }
08389 #endif
08390
08391
08392 static int add_header(struct sip_request *req, const char *var, const char *value)
08393 {
08394 if (req->headers == SIP_MAX_HEADERS) {
08395 ast_log(LOG_WARNING, "Out of SIP header space\n");
08396 return -1;
08397 }
08398
08399 if (req->lines) {
08400 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
08401 return -1;
08402 }
08403
08404 if (compactheaders)
08405 var = find_alias(var, var);
08406
08407 ast_str_append(&req->data, 0, "%s: %s\r\n", var, value);
08408 req->header[req->headers] = req->len;
08409
08410 req->len = req->data->used;
08411 req->headers++;
08412
08413 return 0;
08414 }
08415
08416
08417 static int add_header_contentLength(struct sip_request *req, int len)
08418 {
08419 char clen[10];
08420
08421 snprintf(clen, sizeof(clen), "%d", len);
08422 return add_header(req, "Content-Length", clen);
08423 }
08424
08425
08426 static int add_line(struct sip_request *req, const char *line)
08427 {
08428 if (req->lines == SIP_MAX_LINES) {
08429 ast_log(LOG_WARNING, "Out of SIP line space\n");
08430 return -1;
08431 }
08432 if (!req->lines)
08433
08434 req->len += ast_str_append(&req->data, 0, "\r\n");
08435 req->line[req->lines] = req->len;
08436 ast_str_append(&req->data, 0, "%s", line);
08437 req->len = req->data->used;
08438 req->lines++;
08439 return 0;
08440 }
08441
08442
08443 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
08444 {
08445 const char *tmp = get_header(orig, field);
08446
08447 if (!ast_strlen_zero(tmp))
08448 return add_header(req, field, tmp);
08449 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
08450 return -1;
08451 }
08452
08453
08454 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
08455 {
08456 int start = 0;
08457 int copied = 0;
08458 for (;;) {
08459 const char *tmp = __get_header(orig, field, &start);
08460
08461 if (ast_strlen_zero(tmp))
08462 break;
08463
08464 add_header(req, field, tmp);
08465 copied++;
08466 }
08467 return copied ? 0 : -1;
08468 }
08469
08470
08471
08472
08473
08474
08475
08476
08477
08478 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
08479 {
08480 int copied = 0;
08481 int start = 0;
08482
08483 for (;;) {
08484 char new[512];
08485 const char *oh = __get_header(orig, field, &start);
08486
08487 if (ast_strlen_zero(oh))
08488 break;
08489
08490 if (!copied) {
08491 char leftmost[512], *others, *rport;
08492
08493
08494 ast_copy_string(leftmost, oh, sizeof(leftmost));
08495 others = strchr(leftmost, ',');
08496 if (others)
08497 *others++ = '\0';
08498
08499
08500 rport = strstr(leftmost, ";rport");
08501 if (rport && *(rport+6) == '=')
08502 rport = NULL;
08503
08504
08505 if (rport && ((ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_ALWAYS) || (ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_RFC3581))) {
08506
08507 char *end;
08508
08509 rport = strstr(leftmost, ";rport");
08510
08511 if (rport) {
08512 end = strchr(rport + 1, ';');
08513 if (end)
08514 memmove(rport, end, strlen(end) + 1);
08515 else
08516 *rport = '\0';
08517 }
08518
08519
08520 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
08521 leftmost, ast_inet_ntoa(p->recv.sin_addr),
08522 ntohs(p->recv.sin_port),
08523 others ? "," : "", others ? others : "");
08524 } else {
08525
08526 snprintf(new, sizeof(new), "%s;received=%s%s%s",
08527 leftmost, ast_inet_ntoa(p->recv.sin_addr),
08528 others ? "," : "", others ? others : "");
08529 }
08530 oh = new;
08531 }
08532 add_header(req, field, oh);
08533 copied++;
08534 }
08535 if (!copied) {
08536 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
08537 return -1;
08538 }
08539 return 0;
08540 }
08541
08542
08543 static void add_route(struct sip_request *req, struct sip_route *route)
08544 {
08545 char r[SIPBUFSIZE*2], *p;
08546 int n, rem = sizeof(r);
08547
08548 if (!route)
08549 return;
08550
08551 p = r;
08552 for (;route ; route = route->next) {
08553 n = strlen(route->hop);
08554 if (rem < n+3)
08555 break;
08556 if (p != r) {
08557 *p++ = ',';
08558 --rem;
08559 }
08560 *p++ = '<';
08561 ast_copy_string(p, route->hop, rem);
08562 p += n;
08563 *p++ = '>';
08564 rem -= (n+2);
08565 }
08566 *p = '\0';
08567 add_header(req, "Route", r);
08568 }
08569
08570
08571 static void set_destination(struct sip_pvt *p, char *uri)
08572 {
08573 char *h, *maddr, hostname[256];
08574 int port, hn;
08575 struct hostent *hp;
08576 struct ast_hostent ahp;
08577 int debug=sip_debug_test_pvt(p);
08578
08579
08580
08581
08582 if (debug)
08583 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
08584
08585
08586 h = strchr(uri, '@');
08587 if (h)
08588 ++h;
08589 else {
08590 h = uri;
08591 if (!strncasecmp(h, "sip:", 4))
08592 h += 4;
08593 else if (!strncasecmp(h, "sips:", 5))
08594 h += 5;
08595 }
08596 hn = strcspn(h, ":;>") + 1;
08597 if (hn > sizeof(hostname))
08598 hn = sizeof(hostname);
08599 ast_copy_string(hostname, h, hn);
08600
08601 h += hn - 1;
08602
08603
08604 if (*h == ':') {
08605
08606 ++h;
08607 port = strtol(h, &h, 10);
08608 }
08609 else
08610 port = STANDARD_SIP_PORT;
08611
08612
08613 maddr = strstr(h, "maddr=");
08614 if (maddr) {
08615 maddr += 6;
08616 hn = strspn(maddr, "0123456789.") + 1;
08617 if (hn > sizeof(hostname))
08618 hn = sizeof(hostname);
08619 ast_copy_string(hostname, maddr, hn);
08620 }
08621
08622 hp = ast_gethostbyname(hostname, &ahp);
08623 if (hp == NULL) {
08624 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
08625 return;
08626 }
08627 p->sa.sin_family = AF_INET;
08628 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
08629 p->sa.sin_port = htons(port);
08630 if (debug)
08631 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(p->sa.sin_addr), port);
08632 }
08633
08634
08635 static int init_resp(struct sip_request *resp, const char *msg)
08636 {
08637
08638 memset(resp, 0, sizeof(*resp));
08639 resp->method = SIP_RESPONSE;
08640 if (!(resp->data = ast_str_create(SIP_MIN_PACKET)))
08641 return -1;
08642 resp->header[0] = 0;
08643 ast_str_set(&resp->data, 0, "SIP/2.0 %s\r\n", msg);
08644 resp->len = resp->data->used;
08645 resp->headers++;
08646 return 0;
08647 }
08648
08649
08650 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
08651 {
08652
08653 memset(req, 0, sizeof(*req));
08654 if (!(req->data = ast_str_create(SIP_MIN_PACKET)))
08655 return -1;
08656 req->method = sipmethod;
08657 req->header[0] = 0;
08658 ast_str_set(&req->data, 0, "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
08659 req->len = req->data->used;
08660 req->headers++;
08661 return 0;
08662 }
08663
08664
08665 static inline int resp_needs_contact(const char *msg, enum sipmethod method) {
08666
08667
08668
08669
08670
08671
08672
08673
08674
08675
08676
08677
08678
08679
08680
08681 switch (method) {
08682
08683 case SIP_INVITE:
08684 case SIP_UPDATE:
08685 case SIP_SUBSCRIBE:
08686 case SIP_NOTIFY:
08687 if ((msg[0] >= '1' && msg[0] <= '3') || !strncmp(msg, "485", 3))
08688 return 1;
08689 break;
08690
08691
08692 case SIP_REGISTER:
08693 case SIP_OPTIONS:
08694 if (msg[0] == '2' || msg[0] == '3' || !strncmp(msg, "485", 3))
08695 return 1;
08696 break;
08697
08698
08699 case SIP_BYE:
08700 case SIP_PRACK:
08701 case SIP_MESSAGE:
08702 case SIP_PUBLISH:
08703 if (msg[0] == '3' || !strncmp(msg, "485", 3))
08704 return 1;
08705 break;
08706
08707
08708 case SIP_REFER:
08709 if (msg[0] >= '2' && msg[0] <= '6')
08710 return 1;
08711 break;
08712
08713
08714 case SIP_ACK:
08715 case SIP_CANCEL:
08716 case SIP_INFO:
08717 case SIP_PING:
08718 default:
08719 return 0;
08720 }
08721 return 0;
08722 }
08723
08724
08725 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
08726 {
08727 char newto[256];
08728 const char *ot;
08729
08730 init_resp(resp, msg);
08731 copy_via_headers(p, resp, req, "Via");
08732 if (msg[0] == '1' || msg[0] == '2')
08733 copy_all_header(resp, req, "Record-Route");
08734 copy_header(resp, req, "From");
08735 ot = get_header(req, "To");
08736 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
08737
08738
08739 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
08740 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
08741 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
08742 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
08743 else
08744 ast_copy_string(newto, ot, sizeof(newto));
08745 ot = newto;
08746 }
08747 add_header(resp, "To", ot);
08748 copy_header(resp, req, "Call-ID");
08749 copy_header(resp, req, "CSeq");
08750 if (!ast_strlen_zero(global_useragent))
08751 add_header(resp, "Server", global_useragent);
08752 add_header(resp, "Allow", ALLOWED_METHODS);
08753 add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
08754
08755
08756 if (p->method == SIP_INVITE && p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE) {
08757 char se_hdr[256];
08758 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
08759 strefresher2str(p->stimer->st_ref));
08760 add_header(resp, "Require", "timer");
08761 add_header(resp, "Session-Expires", se_hdr);
08762 }
08763
08764 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
08765
08766
08767 char tmp[256];
08768
08769 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
08770 add_header(resp, "Expires", tmp);
08771 if (p->expiry) {
08772 char contact[SIPBUFSIZE];
08773 const char *contact_uri = p->method == SIP_SUBSCRIBE ? p->our_contact : p->fullcontact;
08774 char *brackets = strchr(contact_uri, '<');
08775 snprintf(contact, sizeof(contact), "%s%s%s;expires=%d", brackets ? "" : "<", contact_uri, brackets ? "" : ">", p->expiry);
08776 add_header(resp, "Contact", contact);
08777 }
08778 } else if (!ast_strlen_zero(p->our_contact) && resp_needs_contact(msg, p->method)) {
08779 add_header(resp, "Contact", p->our_contact);
08780 }
08781
08782 if (!ast_strlen_zero(p->url)) {
08783 add_header(resp, "Access-URL", p->url);
08784 ast_string_field_set(p, url, NULL);
08785 }
08786
08787 return 0;
08788 }
08789
08790
08791 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
08792 {
08793 struct sip_request *orig = &p->initreq;
08794 char stripped[80];
08795 char tmp[80];
08796 char newto[256];
08797 const char *c;
08798 const char *ot, *of;
08799 int is_strict = FALSE;
08800 int is_outbound = ast_test_flag(&p->flags[0], SIP_OUTGOING);
08801
08802 memset(req, 0, sizeof(struct sip_request));
08803
08804 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
08805
08806 if (!seqno) {
08807 p->ocseq++;
08808 seqno = p->ocseq;
08809 }
08810
08811
08812 if (sipmethod == SIP_CANCEL) {
08813 p->branch = p->invite_branch;
08814 build_via(p);
08815 } else if (newbranch && (sipmethod == SIP_INVITE)) {
08816 p->branch ^= ast_random();
08817 p->invite_branch = p->branch;
08818 build_via(p);
08819 } else if (newbranch) {
08820 p->branch ^= ast_random();
08821 build_via(p);
08822 }
08823
08824
08825 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop, ";lr") == NULL) {
08826 is_strict = TRUE;
08827 if (sipdebug)
08828 ast_debug(1, "Strict routing enforced for session %s\n", p->callid);
08829 }
08830
08831 if (sipmethod == SIP_CANCEL)
08832 c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
08833 else if (sipmethod == SIP_ACK) {
08834
08835
08836 if (!ast_strlen_zero(p->okcontacturi))
08837 c = is_strict ? p->route->hop : p->okcontacturi;
08838 else
08839 c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
08840 } else if (!ast_strlen_zero(p->okcontacturi))
08841 c = is_strict ? p->route->hop : p->okcontacturi;
08842 else if (!ast_strlen_zero(p->uri))
08843 c = p->uri;
08844 else {
08845 char *n;
08846
08847 ast_copy_string(stripped, get_header(orig, is_outbound ? "To" : "From"),
08848 sizeof(stripped));
08849 n = get_in_brackets(stripped);
08850 c = remove_uri_parameters(n);
08851 }
08852 init_req(req, sipmethod, c);
08853
08854 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
08855
08856 add_header(req, "Via", p->via);
08857 if (p->route) {
08858 set_destination(p, p->route->hop);
08859 add_route(req, is_strict ? p->route->next : p->route);
08860 }
08861 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
08862
08863 ot = get_header(orig, "To");
08864 of = get_header(orig, "From");
08865
08866
08867
08868 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
08869
08870
08871 if (is_outbound && !ast_strlen_zero(p->theirtag))
08872 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
08873 else if (!is_outbound)
08874 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
08875 else
08876 snprintf(newto, sizeof(newto), "%s", ot);
08877 ot = newto;
08878 }
08879
08880 if (is_outbound) {
08881 add_header(req, "From", of);
08882 add_header(req, "To", ot);
08883 } else {
08884 add_header(req, "From", ot);
08885 add_header(req, "To", of);
08886 }
08887
08888 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
08889 add_header(req, "Contact", p->our_contact);
08890
08891 copy_header(req, orig, "Call-ID");
08892 add_header(req, "CSeq", tmp);
08893
08894 if (!ast_strlen_zero(global_useragent))
08895 add_header(req, "User-Agent", global_useragent);
08896
08897 if (!ast_strlen_zero(p->rpid))
08898 add_header(req, "Remote-Party-ID", p->rpid);
08899
08900 if (!ast_strlen_zero(p->url)) {
08901 add_header(req, "Access-URL", p->url);
08902 ast_string_field_set(p, url, NULL);
08903 }
08904
08905
08906
08907
08908
08909
08910
08911
08912 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE
08913 && sipmethod == SIP_INVITE) {
08914 char se_hdr[256];
08915 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
08916 strefresher2str(p->stimer->st_ref));
08917 add_header(req, "Require", "timer");
08918 add_header(req, "Session-Expires", se_hdr);
08919 snprintf(se_hdr, sizeof(se_hdr), "%d", st_get_se(p, FALSE));
08920 add_header(req, "Min-SE", se_hdr);
08921 }
08922
08923 return 0;
08924 }
08925
08926
08927 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
08928 {
08929 struct sip_request resp;
08930 int seqno = 0;
08931
08932 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
08933 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
08934 return -1;
08935 }
08936 respprep(&resp, p, msg, req);
08937 add_header_contentLength(&resp, 0);
08938
08939
08940 if (p->method == SIP_INVITE && msg[0] != '1' && p->owner && p->owner->hangupcause) {
08941 char buf[10];
08942
08943 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
08944 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
08945 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
08946 }
08947 return send_response(p, &resp, reliable, seqno);
08948 }
08949
08950 static int temp_pvt_init(void *data)
08951 {
08952 struct sip_pvt *p = data;
08953
08954 p->do_history = 0;
08955 return ast_string_field_init(p, 512);
08956 }
08957
08958 static void temp_pvt_cleanup(void *data)
08959 {
08960 struct sip_pvt *p = data;
08961
08962 ast_string_field_free_memory(p);
08963
08964 ast_free(data);
08965 }
08966
08967
08968 static int transmit_response_using_temp(ast_string_field callid, struct sockaddr_in *sin, int useglobal_nat, const int intended_method, const struct sip_request *req, const char *msg)
08969 {
08970 struct sip_pvt *p = NULL;
08971
08972 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
08973 ast_log(LOG_ERROR, "Failed to get temporary pvt\n");
08974 return -1;
08975 }
08976
08977
08978
08979
08980
08981
08982
08983
08984
08985 p->method = intended_method;
08986
08987 if (!sin)
08988 p->ourip = internip;
08989 else {
08990 p->sa = *sin;
08991 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
08992 }
08993
08994 p->branch = ast_random();
08995 make_our_tag(p->tag, sizeof(p->tag));
08996 p->ocseq = INITIAL_CSEQ;
08997
08998 if (useglobal_nat && sin) {
08999 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
09000 p->recv = *sin;
09001 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
09002 }
09003
09004 ast_string_field_set(p, fromdomain, default_fromdomain);
09005 build_via(p);
09006 ast_string_field_set(p, callid, callid);
09007
09008 copy_socket_data(&p->socket, &req->socket);
09009
09010
09011 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
09012
09013
09014 ast_string_field_init(p, 0);
09015
09016 return 0;
09017 }
09018
09019
09020 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
09021 {
09022 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
09023 }
09024
09025
09026 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
09027 {
09028 struct sip_request resp;
09029 respprep(&resp, p, msg, req);
09030 append_date(&resp);
09031 add_header(&resp, "Unsupported", unsupported);
09032 add_header_contentLength(&resp, 0);
09033 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
09034 }
09035
09036
09037 static int transmit_response_with_minse(struct sip_pvt *p, const char *msg, const struct sip_request *req, int minse_int)
09038 {
09039 struct sip_request resp;
09040 char minse_str[20];
09041
09042 respprep(&resp, p, msg, req);
09043 append_date(&resp);
09044
09045 snprintf(minse_str, sizeof(minse_str), "%d", minse_int);
09046 add_header(&resp, "Min-SE", minse_str);
09047
09048 add_header_contentLength(&resp, 0);
09049 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
09050 }
09051
09052
09053
09054
09055
09056 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
09057 {
09058 return __transmit_response(p, msg, req, req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL);
09059 }
09060
09061
09062 static void append_date(struct sip_request *req)
09063 {
09064 char tmpdat[256];
09065 struct tm tm;
09066 time_t t = time(NULL);
09067
09068 gmtime_r(&t, &tm);
09069 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
09070 add_header(req, "Date", tmpdat);
09071 }
09072
09073
09074 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
09075 {
09076 struct sip_request resp;
09077 respprep(&resp, p, msg, req);
09078 append_date(&resp);
09079 add_header_contentLength(&resp, 0);
09080 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
09081 }
09082
09083
09084 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
09085 {
09086 struct sip_request resp;
09087 respprep(&resp, p, msg, req);
09088 add_header(&resp, "Accept", "application/sdp");
09089 add_header_contentLength(&resp, 0);
09090 return send_response(p, &resp, reliable, 0);
09091 }
09092
09093
09094 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)
09095 {
09096 struct sip_request resp;
09097 char tmp[512];
09098 int seqno = 0;
09099
09100 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
09101 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
09102 return -1;
09103 }
09104
09105
09106 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
09107 respprep(&resp, p, msg, req);
09108 add_header(&resp, header, tmp);
09109 add_header_contentLength(&resp, 0);
09110 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
09111 return send_response(p, &resp, reliable, seqno);
09112 }
09113
09114
09115 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp)
09116 {
09117 int res;
09118
09119 if (!(res = with_sdp ? transmit_response_with_sdp(p, msg, req, XMIT_UNRELIABLE, FALSE) : transmit_response(p, msg, req))) {
09120 p->last_provisional = msg;
09121 update_provisional_keepalive(p, with_sdp);
09122 }
09123
09124 return res;
09125 }
09126
09127
09128 static int add_text(struct sip_request *req, const char *text)
09129 {
09130
09131 add_header(req, "Content-Type", "text/plain;charset=UTF-8");
09132 add_header_contentLength(req, strlen(text));
09133 add_line(req, text);
09134 return 0;
09135 }
09136
09137
09138
09139
09140
09141 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode)
09142 {
09143 char tmp[256];
09144 int event;
09145 if (mode) {
09146
09147 if (digit == '*')
09148 event = 10;
09149 else if (digit == '#')
09150 event = 11;
09151 else if ((digit >= 'A') && (digit <= 'D'))
09152 event = 12 + digit - 'A';
09153 else
09154 event = atoi(&digit);
09155 snprintf(tmp, sizeof(tmp), "%d\r\n", event);
09156 add_header(req, "Content-Type", "application/dtmf");
09157 add_header_contentLength(req, strlen(tmp));
09158 add_line(req, tmp);
09159 } else {
09160
09161 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
09162 add_header(req, "Content-Type", "application/dtmf-relay");
09163 add_header_contentLength(req, strlen(tmp));
09164 add_line(req, tmp);
09165 }
09166 return 0;
09167 }
09168
09169
09170
09171 static int add_vidupdate(struct sip_request *req)
09172 {
09173 const char *xml_is_a_huge_waste_of_space =
09174 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
09175 " <media_control>\r\n"
09176 " <vc_primitive>\r\n"
09177 " <to_encoder>\r\n"
09178 " <picture_fast_update>\r\n"
09179 " </picture_fast_update>\r\n"
09180 " </to_encoder>\r\n"
09181 " </vc_primitive>\r\n"
09182 " </media_control>\r\n";
09183 add_header(req, "Content-Type", "application/media_control+xml");
09184 add_header_contentLength(req, strlen(xml_is_a_huge_waste_of_space));
09185 add_line(req, xml_is_a_huge_waste_of_space);
09186 return 0;
09187 }
09188
09189
09190 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
09191 struct ast_str **m_buf, struct ast_str **a_buf,
09192 int debug, int *min_packet_size)
09193 {
09194 int rtp_code;
09195 struct ast_format_list fmt;
09196
09197
09198 if (debug)
09199 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
09200 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
09201 return;
09202
09203 if (p->rtp) {
09204 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
09205 fmt = ast_codec_pref_getsize(pref, codec);
09206 } else
09207 return;
09208 ast_str_append(m_buf, 0, " %d", rtp_code);
09209 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
09210 ast_rtp_lookup_mime_subtype(1, codec,
09211 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
09212 sample_rate);
09213 if (codec == AST_FORMAT_G729A) {
09214
09215 ast_str_append(a_buf, 0, "a=fmtp:%d annexb=no\r\n", rtp_code);
09216 } else if (codec == AST_FORMAT_G723_1) {
09217
09218 ast_str_append(a_buf, 0, "a=fmtp:%d annexa=no\r\n", rtp_code);
09219 } else if (codec == AST_FORMAT_ILBC) {
09220
09221 ast_str_append(a_buf, 0, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
09222 }
09223
09224 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
09225 *min_packet_size = fmt.cur_ms;
09226
09227
09228 if ((*min_packet_size)==0 && fmt.cur_ms)
09229 *min_packet_size = fmt.cur_ms;
09230 }
09231
09232
09233
09234 static void add_vcodec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
09235 struct ast_str **m_buf, struct ast_str **a_buf,
09236 int debug, int *min_packet_size)
09237 {
09238 int rtp_code;
09239
09240 if (!p->vrtp)
09241 return;
09242
09243 if (debug)
09244 ast_verbose("Adding video codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
09245
09246 if ((rtp_code = ast_rtp_lookup_code(p->vrtp, 1, codec)) == -1)
09247 return;
09248
09249 ast_str_append(m_buf, 0, " %d", rtp_code);
09250 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
09251 ast_rtp_lookup_mime_subtype(1, codec, 0), sample_rate);
09252
09253 }
09254
09255
09256 static void add_tcodec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
09257 struct ast_str **m_buf, struct ast_str **a_buf,
09258 int debug, int *min_packet_size)
09259 {
09260 int rtp_code;
09261
09262 if (!p->trtp)
09263 return;
09264
09265 if (debug)
09266 ast_verbose("Adding text codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
09267
09268 if ((rtp_code = ast_rtp_lookup_code(p->trtp, 1, codec)) == -1)
09269 return;
09270
09271 ast_str_append(m_buf, 0, " %d", rtp_code);
09272 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
09273 ast_rtp_lookup_mime_subtype(1, codec, 0), sample_rate);
09274
09275
09276 if (codec == AST_FORMAT_T140RED) {
09277 ast_str_append(a_buf, 0, "a=fmtp:%d %d/%d/%d\r\n", rtp_code,
09278 ast_rtp_lookup_code(p->trtp, 1, AST_FORMAT_T140),
09279 ast_rtp_lookup_code(p->trtp, 1, AST_FORMAT_T140),
09280 ast_rtp_lookup_code(p->trtp, 1, AST_FORMAT_T140));
09281
09282 }
09283 }
09284
09285
09286
09287 static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
09288 {
09289 switch (rate) {
09290 case AST_T38_RATE_2400:
09291 return 2400;
09292 case AST_T38_RATE_4800:
09293 return 4800;
09294 case AST_T38_RATE_7200:
09295 return 7200;
09296 case AST_T38_RATE_9600:
09297 return 9600;
09298 case AST_T38_RATE_12000:
09299 return 12000;
09300 case AST_T38_RATE_14400:
09301 return 14400;
09302 default:
09303 return 0;
09304 }
09305 }
09306
09307
09308 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
09309 struct ast_str **m_buf, struct ast_str **a_buf,
09310 int debug)
09311 {
09312 int rtp_code;
09313
09314 if (debug)
09315 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format, 0));
09316 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
09317 return;
09318
09319 ast_str_append(m_buf, 0, " %d", rtp_code);
09320 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
09321 ast_rtp_lookup_mime_subtype(0, format, 0),
09322 sample_rate);
09323 if (format == AST_RTP_DTMF)
09324 ast_str_append(a_buf, 0, "a=fmtp:%d 0-16\r\n", rtp_code);
09325 }
09326
09327
09328
09329
09330 static void get_our_media_address(struct sip_pvt *p, int needvideo,
09331 struct sockaddr_in *sin, struct sockaddr_in *vsin, struct sockaddr_in *tsin,
09332 struct sockaddr_in *dest, struct sockaddr_in *vdest)
09333 {
09334
09335 ast_rtp_get_us(p->rtp, sin);
09336 if (p->vrtp)
09337 ast_rtp_get_us(p->vrtp, vsin);
09338 if (p->trtp)
09339 ast_rtp_get_us(p->trtp, tsin);
09340
09341
09342
09343 if (p->redirip.sin_addr.s_addr) {
09344 dest->sin_port = p->redirip.sin_port;
09345 dest->sin_addr = p->redirip.sin_addr;
09346 } else {
09347 dest->sin_addr = p->ourip.sin_addr;
09348 dest->sin_port = sin->sin_port;
09349 }
09350 if (needvideo) {
09351
09352 if (p->vredirip.sin_addr.s_addr) {
09353 vdest->sin_addr = p->vredirip.sin_addr;
09354 vdest->sin_port = p->vredirip.sin_port;
09355 } else {
09356 vdest->sin_addr = p->ourip.sin_addr;
09357 vdest->sin_port = vsin->sin_port;
09358 }
09359 }
09360
09361 }
09362
09363
09364
09365
09366
09367
09368 #define SDP_SAMPLE_RATE(x) 8000
09369
09370
09371
09372
09373
09374
09375
09376 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
09377 {
09378 int len = 0;
09379 int alreadysent = 0;
09380
09381 struct sockaddr_in sin;
09382 struct sockaddr_in vsin;
09383 struct sockaddr_in tsin;
09384 struct sockaddr_in dest;
09385 struct sockaddr_in udptlsin;
09386 struct sockaddr_in vdest = { 0, };
09387 struct sockaddr_in tdest = { 0, };
09388 struct sockaddr_in udptldest = { 0, };
09389
09390
09391 char *version = "v=0\r\n";
09392 char subject[256];
09393 char owner[256];
09394 char connection[256];
09395 char *session_time = "t=0 0\r\n";
09396 char bandwidth[256] = "";
09397 char *hold = "";
09398 struct ast_str *m_audio = ast_str_alloca(256);
09399 struct ast_str *m_video = ast_str_alloca(256);
09400 struct ast_str *m_text = ast_str_alloca(256);
09401 struct ast_str *m_modem = ast_str_alloca(256);
09402 struct ast_str *a_audio = ast_str_alloca(1024);
09403 struct ast_str *a_video = ast_str_alloca(1024);
09404 struct ast_str *a_text = ast_str_alloca(1024);
09405 struct ast_str *a_modem = ast_str_alloca(1024);
09406
09407 int x;
09408 int capability = 0;
09409 int needaudio = FALSE;
09410 int needvideo = FALSE;
09411 int needtext = FALSE;
09412 int debug = sip_debug_test_pvt(p);
09413 int min_audio_packet_size = 0;
09414 int min_video_packet_size = 0;
09415 int min_text_packet_size = 0;
09416
09417 char codecbuf[SIPBUFSIZE];
09418 char buf[SIPBUFSIZE];
09419 char dummy_answer[256];
09420
09421
09422 snprintf(subject, sizeof(subject), "s=%s\r\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
09423
09424 if (!p->rtp) {
09425 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
09426 return AST_FAILURE;
09427 }
09428
09429
09430
09431
09432 if (!p->sessionid) {
09433 p->sessionid = (int)ast_random();
09434 p->sessionversion = p->sessionid;
09435 } else {
09436 if (oldsdp == FALSE)
09437 p->sessionversion++;
09438 }
09439
09440 if (add_audio) {
09441
09442 if ((p->jointcapability & AST_FORMAT_VIDEO_MASK) && !p->novideo) {
09443 if (p->vrtp) {
09444 needvideo = TRUE;
09445 ast_debug(2, "This call needs video offers!\n");
09446 } else
09447 ast_debug(2, "This call needs video offers, but there's no video support enabled!\n");
09448 }
09449
09450 if ((p->jointcapability & AST_FORMAT_TEXT_MASK) && !p->notext) {
09451 if (sipdebug_text)
09452 ast_verbose("We think we can do text\n");
09453 if (p->trtp) {
09454 if (sipdebug_text) {
09455 ast_verbose("And we have a text rtp object\n");
09456 }
09457 needtext = TRUE;
09458 ast_debug(2, "This call needs text offers! \n");
09459 } else {
09460 ast_debug(2, "This call needs text offers, but there's no text support enabled ! \n");
09461 }
09462 }
09463 }
09464
09465 get_our_media_address(p, needvideo, &sin, &vsin, &tsin, &dest, &vdest);
09466
09467 snprintf(owner, sizeof(owner), "o=%s %d %d IN IP4 %s\r\n", ast_strlen_zero(global_sdpowner) ? "-" : global_sdpowner, p->sessionid, p->sessionversion, ast_inet_ntoa(dest.sin_addr));
09468 snprintf(connection, sizeof(connection), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
09469
09470 if (add_audio) {
09471 capability = p->jointcapability;
09472
09473
09474 ast_debug(1, "** Our capability: %s Video flag: %s Text flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability),
09475 p->novideo ? "True" : "False", p->notext ? "True" : "False");
09476 ast_debug(1, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
09477
09478
09479 if (capability & AST_FORMAT_AUDIO_MASK)
09480 needaudio = TRUE;
09481
09482 if (debug)
09483 ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(sin.sin_port));
09484
09485
09486
09487 if (needvideo) {
09488 ast_str_append(&m_video, 0, "m=video %d RTP/AVP", ntohs(vsin.sin_port));
09489
09490
09491 if (p->maxcallbitrate)
09492 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
09493 if (debug)
09494 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(vdest.sin_port));
09495 }
09496
09497
09498
09499 if (needtext) {
09500 if (sipdebug_text)
09501 ast_verbose("Lets set up the text sdp\n");
09502
09503 if (p->tredirip.sin_addr.s_addr) {
09504 tdest.sin_addr = p->tredirip.sin_addr;
09505 tdest.sin_port = p->tredirip.sin_port;
09506 } else {
09507 tdest.sin_addr = p->ourip.sin_addr;
09508 tdest.sin_port = tsin.sin_port;
09509 }
09510 ast_str_append(&m_text, 0, "m=text %d RTP/AVP", ntohs(tdest.sin_port));
09511
09512 if (debug)
09513 ast_verbose("Text is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(tsin.sin_port));
09514
09515 }
09516
09517
09518
09519
09520
09521
09522 ast_str_append(&m_audio, 0, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
09523
09524 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR)
09525 hold = "a=recvonly\r\n";
09526 else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE)
09527 hold = "a=inactive\r\n";
09528 else
09529 hold = "a=sendrecv\r\n";
09530
09531
09532
09533
09534
09535
09536
09537
09538
09539
09540 if (capability & p->prefcodec) {
09541 int codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
09542
09543 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
09544 &m_audio, &a_audio,
09545 debug, &min_audio_packet_size);
09546 alreadysent |= codec;
09547 }
09548
09549
09550 for (x = 0; x < 32; x++) {
09551 int codec;
09552
09553 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
09554 break;
09555
09556 if (!(capability & codec))
09557 continue;
09558
09559 if (alreadysent & codec)
09560 continue;
09561
09562 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
09563 &m_audio, &a_audio,
09564 debug, &min_audio_packet_size);
09565 alreadysent |= codec;
09566 }
09567
09568
09569 for (x = 1; x <= (needtext ? AST_FORMAT_TEXT_MASK : (needvideo ? AST_FORMAT_VIDEO_MASK : AST_FORMAT_AUDIO_MASK)); x <<= 1) {
09570 if (!(capability & x))
09571 continue;
09572
09573 if (alreadysent & x)
09574 continue;
09575
09576 if (x & AST_FORMAT_AUDIO_MASK)
09577 add_codec_to_sdp(p, x, SDP_SAMPLE_RATE(x),
09578 &m_audio, &a_audio, debug, &min_audio_packet_size);
09579 else if (x & AST_FORMAT_VIDEO_MASK)
09580 add_vcodec_to_sdp(p, x, 90000,
09581 &m_video, &a_video, debug, &min_video_packet_size);
09582 else if (x & AST_FORMAT_TEXT_MASK)
09583 add_tcodec_to_sdp(p, x, 1000,
09584 &m_text, &a_text, debug, &min_text_packet_size);
09585 }
09586
09587
09588 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
09589 if (!(p->jointnoncodeccapability & x))
09590 continue;
09591
09592 add_noncodec_to_sdp(p, x, 8000, &m_audio, &a_audio, debug);
09593 }
09594
09595 ast_debug(3, "-- Done with adding codecs to SDP\n");
09596
09597 if (!p->owner || !ast_internal_timing_enabled(p->owner))
09598 ast_str_append(&a_audio, 0, "a=silenceSupp:off - - - -\r\n");
09599
09600 if (min_audio_packet_size)
09601 ast_str_append(&a_audio, 0, "a=ptime:%d\r\n", min_audio_packet_size);
09602
09603
09604 if (min_video_packet_size)
09605 ast_str_append(&a_video, 0, "a=ptime:%d\r\n", min_video_packet_size);
09606
09607
09608 if (min_text_packet_size)
09609 ast_str_append(&a_text, 0, "a=ptime:%d\r\n", min_text_packet_size);
09610 }
09611
09612 if (add_t38) {
09613 ast_udptl_get_us(p->udptl, &udptlsin);
09614
09615
09616 if (p->udptlredirip.sin_addr.s_addr) {
09617 udptldest.sin_port = p->udptlredirip.sin_port;
09618 udptldest.sin_addr = p->udptlredirip.sin_addr;
09619 } else {
09620 udptldest.sin_addr = p->ourip.sin_addr;
09621 udptldest.sin_port = udptlsin.sin_port;
09622 }
09623
09624 if (debug)
09625 ast_debug(1, "T.38 UDPTL is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(udptlsin.sin_port));
09626
09627
09628
09629
09630 ast_str_append(&m_modem, 0, "m=image %d udptl t38\r\n", ntohs(udptldest.sin_port));
09631
09632 ast_str_append(&a_modem, 0, "a=T38FaxVersion:%d\r\n", p->t38.our_parms.version);
09633 ast_str_append(&a_modem, 0, "a=T38MaxBitRate:%d\r\n", t38_get_rate(p->t38.our_parms.rate));
09634 if (p->t38.our_parms.fill_bit_removal) {
09635 ast_str_append(&a_modem, 0, "a=T38FaxFillBitRemoval\r\n");
09636 }
09637 if (p->t38.our_parms.transcoding_mmr) {
09638 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingMMR\r\n");
09639 }
09640 if (p->t38.our_parms.transcoding_jbig) {
09641 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingJBIG\r\n");
09642 }
09643 switch (p->t38.our_parms.rate_management) {
09644 case AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF:
09645 ast_str_append(&a_modem, 0, "a=T38FaxRateManagement:transferredTCF\r\n");
09646 break;
09647 case AST_T38_RATE_MANAGEMENT_LOCAL_TCF:
09648 ast_str_append(&a_modem, 0, "a=T38FaxRateManagement:localTCF\r\n");
09649 break;
09650 }
09651 ast_str_append(&a_modem, 0, "a=T38FaxMaxDatagram:%u\r\n", ast_udptl_get_local_max_datagram(p->udptl));
09652 switch (ast_udptl_get_error_correction_scheme(p->udptl)) {
09653 case UDPTL_ERROR_CORRECTION_NONE:
09654 break;
09655 case UDPTL_ERROR_CORRECTION_FEC:
09656 ast_str_append(&a_modem, 0, "a=T38FaxUdpEC:t38UDPFEC\r\n");
09657 break;
09658 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
09659 ast_str_append(&a_modem, 0, "a=T38FaxUdpEC:t38UDPRedundancy\r\n");
09660 break;
09661 }
09662 }
09663
09664 if (m_audio->len - m_audio->used < 2 || m_video->len - m_video->used < 2 ||
09665 m_text->len - m_text->used < 2 || a_text->len - a_text->used < 2 ||
09666 a_audio->len - a_audio->used < 2 || a_video->len - a_video->used < 2)
09667 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
09668
09669 if (needaudio)
09670 ast_str_append(&m_audio, 0, "\r\n");
09671 if (needvideo)
09672 ast_str_append(&m_video, 0, "\r\n");
09673 if (needtext)
09674 ast_str_append(&m_text, 0, "\r\n");
09675
09676 len = strlen(version) + strlen(subject) + strlen(owner) +
09677 strlen(connection) + strlen(session_time);
09678 if (needaudio)
09679 len += m_audio->used + a_audio->used + strlen(hold);
09680 if (needvideo)
09681 len += m_video->used + a_video->used + strlen(bandwidth) + strlen(hold);
09682 if (needtext)
09683 len += m_text->used + a_text->used + strlen(hold);
09684 if (add_t38)
09685 len += m_modem->used + a_modem->used;
09686
09687 add_header(resp, "Content-Type", "application/sdp");
09688 add_header_contentLength(resp, len);
09689 add_line(resp, version);
09690 add_line(resp, owner);
09691 add_line(resp, subject);
09692 add_line(resp, connection);
09693 if (needvideo)
09694 add_line(resp, bandwidth);
09695 add_line(resp, session_time);
09696 if (needaudio) {
09697 add_line(resp, m_audio->str);
09698 add_line(resp, a_audio->str);
09699 add_line(resp, hold);
09700 } else if (p->offered_media[SDP_AUDIO].offered) {
09701 snprintf(dummy_answer, sizeof(dummy_answer), "m=audio 0 RTP/AVP %s\r\n", p->offered_media[SDP_AUDIO].text);
09702 add_line(resp, dummy_answer);
09703 }
09704 if (needvideo) {
09705 add_line(resp, m_video->str);
09706 add_line(resp, a_video->str);
09707 add_line(resp, hold);
09708 } else if (p->offered_media[SDP_VIDEO].offered) {
09709 snprintf(dummy_answer, sizeof(dummy_answer), "m=video 0 RTP/AVP %s\r\n", p->offered_media[SDP_VIDEO].text);
09710 add_line(resp, dummy_answer);
09711 }
09712 if (needtext) {
09713 add_line(resp, m_text->str);
09714 add_line(resp, a_text->str);
09715 add_line(resp, hold);
09716 } else if (p->offered_media[SDP_TEXT].offered) {
09717 snprintf(dummy_answer, sizeof(dummy_answer), "m=text 0 RTP/AVP %s\r\n", p->offered_media[SDP_TEXT].text);
09718 add_line(resp, dummy_answer);
09719 }
09720 if (add_t38) {
09721 add_line(resp, m_modem->str);
09722 add_line(resp, a_modem->str);
09723 } else if (p->offered_media[SDP_IMAGE].offered) {
09724 add_line(resp, "m=image 0 udptl t38\r\n");
09725 }
09726
09727
09728 p->lastrtprx = p->lastrtptx = time(NULL);
09729
09730 ast_debug(3, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
09731
09732 return AST_SUCCESS;
09733 }
09734
09735
09736 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
09737 {
09738 struct sip_request resp;
09739 int seqno;
09740
09741 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
09742 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
09743 return -1;
09744 }
09745 respprep(&resp, p, msg, req);
09746 if (p->udptl) {
09747 add_sdp(&resp, p, 0, 0, 1);
09748 } else
09749 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
09750 if (retrans && !p->pendinginvite)
09751 p->pendinginvite = seqno;
09752 return send_response(p, &resp, retrans, seqno);
09753 }
09754
09755
09756 static void copy_request(struct sip_request *dst, const struct sip_request *src)
09757 {
09758 struct ast_str *duplicate = dst->data;
09759
09760
09761 memcpy(dst, src, sizeof(*dst));
09762 dst->data = duplicate;
09763
09764
09765
09766
09767
09768
09769
09770 if (!dst->data && !(dst->data = ast_str_create(src->data->used + 1)))
09771 return;
09772 else if (dst->data->len < src->data->used + 1)
09773 ast_str_make_space(&dst->data, src->data->used + 1);
09774
09775 memcpy(dst->data->str, src->data->str, src->data->used + 1);
09776 dst->data->used = src->data->used;
09777 }
09778
09779
09780
09781
09782 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp)
09783 {
09784 struct sip_request resp;
09785 int seqno;
09786 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
09787 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
09788 return -1;
09789 }
09790 respprep(&resp, p, msg, req);
09791 if (p->rtp) {
09792 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
09793 ast_debug(1, "Setting framing from config on incoming call\n");
09794 ast_rtp_codec_setpref(p->rtp, &p->prefs);
09795 }
09796 try_suggested_sip_codec(p);
09797 if (p->t38.state == T38_ENABLED) {
09798 add_sdp(&resp, p, oldsdp, TRUE, TRUE);
09799 } else {
09800 add_sdp(&resp, p, oldsdp, TRUE, FALSE);
09801 }
09802 } else
09803 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
09804 if (reliable && !p->pendinginvite)
09805 p->pendinginvite = seqno;
09806 return send_response(p, &resp, reliable, seqno);
09807 }
09808
09809
09810 static int determine_firstline_parts(struct sip_request *req)
09811 {
09812 char *e = ast_skip_blanks(req->data->str);
09813 char *local_rlPart1;
09814
09815 if (!*e)
09816 return -1;
09817 req->rlPart1 = e - req->data->str;
09818 local_rlPart1 = e;
09819 e = ast_skip_nonblanks(e);
09820 if (*e)
09821 *e++ = '\0';
09822
09823 e = ast_skip_blanks(e);
09824 if ( !*e )
09825 return -1;
09826 ast_trim_blanks(e);
09827
09828 if (!strcasecmp(local_rlPart1, "SIP/2.0") ) {
09829 if (strlen(e) < 3)
09830 return -1;
09831 req->rlPart2 = e - req->data->str;
09832 } else {
09833 if ( *e == '<' ) {
09834 ast_debug(3, "Oops. Bogus uri in <> %s\n", e);
09835 e++;
09836 if (!*e)
09837 return -1;
09838 }
09839 req->rlPart2 = e - req->data->str;
09840 e = ast_skip_nonblanks(e);
09841 if (*e)
09842 *e++ = '\0';
09843 e = ast_skip_blanks(e);
09844 if (strcasecmp(e, "SIP/2.0") ) {
09845 ast_debug(3, "Skipping packet - Bad request protocol %s\n", e);
09846 return -1;
09847 }
09848 }
09849 return 1;
09850 }
09851
09852
09853
09854
09855
09856
09857
09858
09859
09860
09861
09862
09863
09864
09865 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp)
09866 {
09867 struct sip_request req;
09868
09869 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
09870
09871 add_header(&req, "Allow", ALLOWED_METHODS);
09872 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
09873 if (sipdebug) {
09874 if (oldsdp == TRUE)
09875 add_header(&req, "X-asterisk-Info", "SIP re-invite (Session-Timers)");
09876 else
09877 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
09878 }
09879
09880 if (p->do_history)
09881 append_history(p, "ReInv", "Re-invite sent");
09882 memset(p->offered_media, 0, sizeof(p->offered_media));
09883
09884 if (t38version)
09885 add_sdp(&req, p, oldsdp, FALSE, TRUE);
09886 else
09887 add_sdp(&req, p, oldsdp, TRUE, FALSE);
09888
09889
09890 initialize_initreq(p, &req);
09891 p->lastinvite = p->ocseq;
09892 ast_set_flag(&p->flags[0], SIP_OUTGOING);
09893
09894 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
09895 }
09896
09897
09898 static char *remove_uri_parameters(char *uri)
09899 {
09900 char *atsign;
09901 atsign = strchr(uri, '@');
09902 if (!atsign)
09903 atsign = uri;
09904 atsign = strchr(atsign, ';');
09905 if (atsign)
09906 *atsign = '\0';
09907 return uri;
09908 }
09909
09910
09911 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
09912 {
09913 char stripped[SIPBUFSIZE];
09914 char *c;
09915
09916 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
09917 c = get_in_brackets(stripped);
09918
09919 c = remove_uri_parameters(c);
09920 if (!ast_strlen_zero(c))
09921 ast_string_field_set(p, uri, c);
09922
09923 }
09924
09925
09926 static void build_contact(struct sip_pvt *p)
09927 {
09928 int ourport = ntohs(p->ourip.sin_port);
09929
09930 if (!sip_standard_port(p->socket.type, ourport)) {
09931 if (p->socket.type == SIP_TRANSPORT_UDP)
09932 ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr), ourport);
09933 else
09934 ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d;transport=%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr), ourport, get_transport(p->socket.type));
09935 } else {
09936 if (p->socket.type == SIP_TRANSPORT_UDP)
09937 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr));
09938 else
09939 ast_string_field_build(p, our_contact, "<sip:%s%s%s;transport=%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr), get_transport(p->socket.type));
09940 }
09941 }
09942
09943
09944 static void build_rpid(struct sip_pvt *p)
09945 {
09946 int send_pres_tags = TRUE;
09947 const char *privacy=NULL;
09948 const char *screen=NULL;
09949 char buf[256];
09950 const char *clid = default_callerid;
09951 const char *clin = NULL;
09952 const char *fromdomain;
09953
09954 if (!ast_strlen_zero(p->rpid) || !ast_strlen_zero(p->rpid_from))
09955 return;
09956
09957 if (p->owner && !ast_strlen_zero(p->owner->cid.cid_num))
09958 clid = p->owner->cid.cid_num;
09959 if (p->owner && p->owner->cid.cid_name)
09960 clin = p->owner->cid.cid_name;
09961 if (ast_strlen_zero(clin))
09962 clin = clid;
09963
09964 switch (p->callingpres) {
09965 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
09966 privacy = "off";
09967 screen = "no";
09968 break;
09969 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
09970 privacy = "off";
09971 screen = "yes";
09972 break;
09973 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
09974 privacy = "off";
09975 screen = "no";
09976 break;
09977 case AST_PRES_ALLOWED_NETWORK_NUMBER:
09978 privacy = "off";
09979 screen = "yes";
09980 break;
09981 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
09982 privacy = "full";
09983 screen = "no";
09984 break;
09985 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
09986 privacy = "full";
09987 screen = "yes";
09988 break;
09989 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
09990 privacy = "full";
09991 screen = "no";
09992 break;
09993 case AST_PRES_PROHIB_NETWORK_NUMBER:
09994 privacy = "full";
09995 screen = "yes";
09996 break;
09997 case AST_PRES_NUMBER_NOT_AVAILABLE:
09998 send_pres_tags = FALSE;
09999 break;
10000 default:
10001 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
10002 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
10003 privacy = "full";
10004 else
10005 privacy = "off";
10006 screen = "no";
10007 break;
10008 }
10009
10010 fromdomain = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip.sin_addr));
10011
10012 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
10013 if (send_pres_tags)
10014 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
10015 ast_string_field_set(p, rpid, buf);
10016
10017 ast_string_field_build(p, rpid_from, "\"%s\" <sip:%s@%s>;tag=%s", clin,
10018 S_OR(p->fromuser, clid),
10019 fromdomain, p->tag);
10020 }
10021
10022
10023 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
10024 {
10025 struct ast_str *invite = ast_str_alloca(256);
10026 char from[256];
10027 char to[256];
10028 char tmp_n[SIPBUFSIZE/2];
10029 char tmp_l[SIPBUFSIZE/2];
10030 const char *l = NULL;
10031 const char *n = NULL;
10032 const char *d = NULL;
10033 const char *urioptions = "";
10034 int ourport;
10035
10036 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
10037 const char *s = p->username;
10038
10039
10040
10041
10042
10043
10044 if (*s == '+')
10045 s++;
10046 for (; *s; s++) {
10047 if (!strchr(AST_DIGIT_ANYNUM, *s) )
10048 break;
10049 }
10050
10051 if (!*s)
10052 urioptions = ";user=phone";
10053 }
10054
10055
10056 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
10057
10058 d = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip.sin_addr));
10059 if (p->owner) {
10060 l = p->owner->cid.cid_num;
10061 n = p->owner->cid.cid_name;
10062 }
10063
10064 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID) &&
10065 ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
10066 l = CALLERID_UNKNOWN;
10067 n = l;
10068 d = FROMDOMAIN_INVALID;
10069 }
10070 if (ast_strlen_zero(l))
10071 l = default_callerid;
10072 if (ast_strlen_zero(n))
10073 n = l;
10074
10075 if (!ast_strlen_zero(p->fromuser))
10076 l = p->fromuser;
10077 else
10078 ast_string_field_set(p, fromuser, l);
10079
10080
10081 if (!ast_strlen_zero(p->fromname))
10082 n = p->fromname;
10083 else
10084 ast_string_field_set(p, fromname, n);
10085
10086 if (pedanticsipchecking) {
10087 ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
10088 n = tmp_n;
10089 ast_uri_encode(l, tmp_l, sizeof(tmp_l), 0);
10090 l = tmp_l;
10091 }
10092
10093 ourport = ntohs(p->ourip.sin_port);
10094 if (!sip_standard_port(p->socket.type, ourport) && ast_strlen_zero(p->fromdomain))
10095 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, d, ourport, p->tag);
10096 else
10097 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, d, p->tag);
10098
10099
10100 if (!ast_strlen_zero(p->fullcontact)) {
10101
10102 ast_str_append(&invite, 0, "%s", p->fullcontact);
10103 } else {
10104
10105 ast_str_append(&invite, 0, "sip:");
10106 if (!ast_strlen_zero(p->username)) {
10107 n = p->username;
10108 if (pedanticsipchecking) {
10109 ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
10110 n = tmp_n;
10111 }
10112 ast_str_append(&invite, 0, "%s@", n);
10113 }
10114 ast_str_append(&invite, 0, "%s", p->tohost);
10115 if (p->portinuri)
10116 ast_str_append(&invite, 0, ":%d", ntohs(p->sa.sin_port));
10117 ast_str_append(&invite, 0, "%s", urioptions);
10118 }
10119
10120
10121 if (p->options && !ast_strlen_zero(p->options->uri_options))
10122 ast_str_append(&invite, 0, ";%s", p->options->uri_options);
10123
10124
10125
10126
10127 ast_string_field_set(p, uri, invite->str);
10128
10129 if (!ast_strlen_zero(p->todnid)) {
10130
10131 if (!strchr(p->todnid, '@')) {
10132
10133 snprintf(to, sizeof(to), "<sip:%s@%s>%s%s", p->todnid, p->tohost, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
10134 } else {
10135 snprintf(to, sizeof(to), "<sip:%s>%s%s", p->todnid, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
10136 }
10137 } else {
10138 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
10139
10140 snprintf(to, sizeof(to), "<%s%s>;tag=%s", (strncasecmp(p->uri, "sip:", 4) ? "sip:" : ""), p->uri, p->theirtag);
10141 } else if (p->options && p->options->vxml_url) {
10142
10143 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
10144 } else
10145 snprintf(to, sizeof(to), "<%s>", p->uri);
10146 }
10147
10148 init_req(req, sipmethod, p->uri);
10149
10150 snprintf(tmp_n, sizeof(tmp_n), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
10151
10152 add_header(req, "Via", p->via);
10153 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
10154
10155
10156
10157
10158 add_route(req, p->route);
10159
10160
10161 if (ast_test_flag(&p->flags[0], SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
10162 build_rpid(p);
10163 add_header(req, "From", p->rpid_from);
10164 } else
10165 add_header(req, "From", from);
10166 add_header(req, "To", to);
10167 ast_string_field_set(p, exten, l);
10168 build_contact(p);
10169 add_header(req, "Contact", p->our_contact);
10170 add_header(req, "Call-ID", p->callid);
10171 add_header(req, "CSeq", tmp_n);
10172 if (!ast_strlen_zero(global_useragent))
10173 add_header(req, "User-Agent", global_useragent);
10174 if (!ast_strlen_zero(p->rpid))
10175 add_header(req, "Remote-Party-ID", p->rpid);
10176 }
10177
10178
10179
10180
10181
10182
10183
10184
10185 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
10186 {
10187 struct sip_request req;
10188 struct ast_variable *var;
10189
10190 req.method = sipmethod;
10191 if (init) {
10192 p->branch ^= ast_random();
10193 p->invite_branch = p->branch;
10194 build_via(p);
10195 }
10196 if (init > 1)
10197 initreqprep(&req, p, sipmethod);
10198 else
10199
10200 reqprep(&req, p, sipmethod, 0, init ? 0 : 1);
10201
10202 if (p->options && p->options->auth)
10203 add_header(&req, p->options->authheader, p->options->auth);
10204 append_date(&req);
10205 if (sipmethod == SIP_REFER) {
10206 if (p->refer) {
10207 char buf[SIPBUFSIZE];
10208 if (!ast_strlen_zero(p->refer->refer_to))
10209 add_header(&req, "Refer-To", p->refer->refer_to);
10210 if (!ast_strlen_zero(p->refer->referred_by)) {
10211 snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
10212 add_header(&req, "Referred-By", buf);
10213 }
10214 }
10215 }
10216
10217
10218 if (p->options && !ast_strlen_zero(p->options->replaces)) {
10219 add_header(&req, "Replaces", p->options->replaces);
10220 add_header(&req, "Require", "replaces");
10221 }
10222
10223
10224 if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
10225 char i2astr[10];
10226
10227 if (!p->stimer->st_interval)
10228 p->stimer->st_interval = st_get_se(p, TRUE);
10229
10230 p->stimer->st_active = TRUE;
10231
10232 snprintf(i2astr, sizeof(i2astr), "%d", p->stimer->st_interval);
10233 add_header(&req, "Session-Expires", i2astr);
10234 snprintf(i2astr, sizeof(i2astr), "%d", st_get_se(p, FALSE));
10235 add_header(&req, "Min-SE", i2astr);
10236 }
10237
10238 add_header(&req, "Allow", ALLOWED_METHODS);
10239 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
10240
10241 if(p->notify_headers) {
10242 char buf[512];
10243 for (var = p->notify_headers; var; var = var->next) {
10244 ast_copy_string(buf, var->value, sizeof(buf));
10245 add_header(&req, var->name, ast_unescape_semicolon(buf));
10246 }
10247 }
10248 if (p->options && p->options->addsipheaders && p->owner) {
10249 struct ast_channel *chan = p->owner;
10250 struct varshead *headp;
10251
10252 ast_channel_lock(chan);
10253
10254 headp = &chan->varshead;
10255
10256 if (!headp)
10257 ast_log(LOG_WARNING, "No Headp for the channel...ooops!\n");
10258 else {
10259 const struct ast_var_t *current;
10260 AST_LIST_TRAVERSE(headp, current, entries) {
10261
10262 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
10263 char *content, *end;
10264 const char *header = ast_var_value(current);
10265 char *headdup = ast_strdupa(header);
10266
10267
10268 if (*headdup == '"')
10269 headdup++;
10270 if ((content = strchr(headdup, ':'))) {
10271 *content++ = '\0';
10272 content = ast_skip_blanks(content);
10273
10274 end = content + strlen(content) -1;
10275 if (*end == '"')
10276 *end = '\0';
10277
10278 add_header(&req, headdup, content);
10279 if (sipdebug)
10280 ast_debug(1, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
10281 }
10282 }
10283 }
10284 }
10285
10286 ast_channel_unlock(chan);
10287 }
10288 if (sdp) {
10289 memset(p->offered_media, 0, sizeof(p->offered_media));
10290 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
10291 ast_debug(1, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
10292 add_sdp(&req, p, FALSE, FALSE, TRUE);
10293 } else if (p->rtp)
10294 add_sdp(&req, p, FALSE, TRUE, FALSE);
10295 } else {
10296 if (!p->notify_headers) {
10297 add_header_contentLength(&req, 0);
10298 }
10299 }
10300
10301 if (!p->initreq.headers || init > 2)
10302 initialize_initreq(p, &req);
10303 p->lastinvite = p->ocseq;
10304 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
10305 }
10306
10307
10308 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
10309 {
10310 struct ast_str *tmp = ast_str_alloca(4000);
10311 char from[256], to[256];
10312 char *c, *mfrom, *mto;
10313 struct sip_request req;
10314 char hint[AST_MAX_EXTENSION];
10315 char *statestring = "terminated";
10316 const struct cfsubscription_types *subscriptiontype;
10317 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
10318 char *pidfstate = "--";
10319 char *pidfnote= "Ready";
10320
10321 memset(from, 0, sizeof(from));
10322 memset(to, 0, sizeof(to));
10323
10324 switch (state) {
10325 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
10326 statestring = (global_notifyringing) ? "early" : "confirmed";
10327 local_state = NOTIFY_INUSE;
10328 pidfstate = "busy";
10329 pidfnote = "Ringing";
10330 break;
10331 case AST_EXTENSION_RINGING:
10332 statestring = "early";
10333 local_state = NOTIFY_INUSE;
10334 pidfstate = "busy";
10335 pidfnote = "Ringing";
10336 break;
10337 case AST_EXTENSION_INUSE:
10338 statestring = "confirmed";
10339 local_state = NOTIFY_INUSE;
10340 pidfstate = "busy";
10341 pidfnote = "On the phone";
10342 break;
10343 case AST_EXTENSION_BUSY:
10344 statestring = "confirmed";
10345 local_state = NOTIFY_CLOSED;
10346 pidfstate = "busy";
10347 pidfnote = "On the phone";
10348 break;
10349 case AST_EXTENSION_UNAVAILABLE:
10350 statestring = "terminated";
10351 local_state = NOTIFY_CLOSED;
10352 pidfstate = "away";
10353 pidfnote = "Unavailable";
10354 break;
10355 case AST_EXTENSION_ONHOLD:
10356 statestring = "confirmed";
10357 local_state = NOTIFY_CLOSED;
10358 pidfstate = "busy";
10359 pidfnote = "On hold";
10360 break;
10361 case AST_EXTENSION_NOT_INUSE:
10362 default:
10363
10364 break;
10365 }
10366
10367 subscriptiontype = find_subscription_type(p->subscribed);
10368
10369
10370 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
10371 char *hint2 = hint, *individual_hint = NULL;
10372 int hint_count = 0, unavailable_count = 0;
10373
10374 while ((individual_hint = strsep(&hint2, "&"))) {
10375 hint_count++;
10376
10377 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE)
10378 unavailable_count++;
10379 }
10380
10381
10382
10383
10384 if (hint_count > 0 && hint_count == unavailable_count) {
10385 local_state = NOTIFY_CLOSED;
10386 pidfstate = "away";
10387 pidfnote = "Not online";
10388 }
10389 }
10390
10391 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
10392 c = get_in_brackets(from);
10393 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
10394 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
10395 return -1;
10396 }
10397
10398 mfrom = remove_uri_parameters(c);
10399
10400 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
10401 c = get_in_brackets(to);
10402 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
10403 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
10404 return -1;
10405 }
10406 mto = remove_uri_parameters(c);
10407
10408 reqprep(&req, p, SIP_NOTIFY, 0, 1);
10409
10410
10411 add_header(&req, "Event", subscriptiontype->event);
10412 add_header(&req, "Content-Type", subscriptiontype->mediatype);
10413 switch(state) {
10414 case AST_EXTENSION_DEACTIVATED:
10415 if (timeout)
10416 add_header(&req, "Subscription-State", "terminated;reason=timeout");
10417 else {
10418 add_header(&req, "Subscription-State", "terminated;reason=probation");
10419 add_header(&req, "Retry-After", "60");
10420 }
10421 break;
10422 case AST_EXTENSION_REMOVED:
10423 add_header(&req, "Subscription-State", "terminated;reason=noresource");
10424 break;
10425 default:
10426 if (p->expiry)
10427 add_header(&req, "Subscription-State", "active");
10428 else
10429 add_header(&req, "Subscription-State", "terminated;reason=timeout");
10430 }
10431 switch (p->subscribed) {
10432 case XPIDF_XML:
10433 case CPIM_PIDF_XML:
10434 ast_str_append(&tmp, 0,
10435 "<?xml version=\"1.0\"?>\n"
10436 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
10437 "<presence>\n");
10438 ast_str_append(&tmp, 0, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
10439 ast_str_append(&tmp, 0, "<atom id=\"%s\">\n", p->exten);
10440 ast_str_append(&tmp, 0, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
10441 ast_str_append(&tmp, 0, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
10442 ast_str_append(&tmp, 0, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
10443 ast_str_append(&tmp, 0, "</address>\n</atom>\n</presence>\n");
10444 break;
10445 case PIDF_XML:
10446 ast_str_append(&tmp, 0,
10447 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
10448 "<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);
10449 ast_str_append(&tmp, 0, "<pp:person><status>\n");
10450 if (pidfstate[0] != '-')
10451 ast_str_append(&tmp, 0, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
10452 ast_str_append(&tmp, 0, "</status></pp:person>\n");
10453 ast_str_append(&tmp, 0, "<note>%s</note>\n", pidfnote);
10454 ast_str_append(&tmp, 0, "<tuple id=\"%s\">\n", p->exten);
10455 ast_str_append(&tmp, 0, "<contact priority=\"1\">%s</contact>\n", mto);
10456 if (pidfstate[0] == 'b')
10457 ast_str_append(&tmp, 0, "<status><basic>open</basic></status>\n");
10458 else
10459 ast_str_append(&tmp, 0, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
10460 ast_str_append(&tmp, 0, "</tuple>\n</presence>\n");
10461 break;
10462 case DIALOG_INFO_XML:
10463 ast_str_append(&tmp, 0, "<?xml version=\"1.0\"?>\n");
10464 ast_str_append(&tmp, 0, "<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" version=\"%d\" state=\"%s\" entity=\"%s\">\n", p->dialogver++, full ? "full":"partial", mto);
10465 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
10466 ast_str_append(&tmp, 0, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
10467 else
10468 ast_str_append(&tmp, 0, "<dialog id=\"%s\">\n", p->exten);
10469 ast_str_append(&tmp, 0, "<state>%s</state>\n", statestring);
10470 if (state == AST_EXTENSION_ONHOLD) {
10471 ast_str_append(&tmp, 0, "<local>\n<target uri=\"%s\">\n"
10472 "<param pname=\"+sip.rendering\" pvalue=\"no\"/>\n"
10473 "</target>\n</local>\n", mto);
10474 }
10475 ast_str_append(&tmp, 0, "</dialog>\n</dialog-info>\n");
10476 break;
10477 case NONE:
10478 default:
10479 break;
10480 }
10481
10482 add_header_contentLength(&req, tmp->used);
10483 add_line(&req, tmp->str);
10484
10485 p->pendinginvite = p->ocseq;
10486
10487 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
10488 }
10489
10490
10491
10492
10493
10494
10495
10496 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
10497 {
10498 struct sip_request req;
10499 struct ast_str *out = ast_str_alloca(500);
10500 int ourport = ntohs(p->ourip.sin_port);
10501 const char *exten = S_OR(vmexten, default_vmexten);
10502
10503 initreqprep(&req, p, SIP_NOTIFY);
10504 add_header(&req, "Event", "message-summary");
10505 add_header(&req, "Content-Type", default_notifymime);
10506 ast_str_append(&out, 0, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
10507
10508 if (!ast_strlen_zero(p->fromdomain)) {
10509 ast_str_append(&out, 0, "Message-Account: sip:%s@%s\r\n", exten, p->fromdomain);
10510 } else if (!sip_standard_port(p->socket.type, ourport)) {
10511 if (p->socket.type == SIP_TRANSPORT_UDP) {
10512 ast_str_append(&out, 0, "Message-Account: sip:%s@%s:%d\r\n", exten, ast_inet_ntoa(p->ourip.sin_addr), ourport);
10513 } else {
10514 ast_str_append(&out, 0, "Message-Account: sip:%s@%s:%d;transport=%s\r\n", exten, ast_inet_ntoa(p->ourip.sin_addr), ourport, get_transport(p->socket.type));
10515 }
10516 } else {
10517 if (p->socket.type == SIP_TRANSPORT_UDP) {
10518 ast_str_append(&out, 0, "Message-Account: sip:%s@%s\r\n", exten, ast_inet_ntoa(p->ourip.sin_addr));
10519 } else {
10520 ast_str_append(&out, 0, "Message-Account: sip:%s@%s;transport=%s\r\n", exten, ast_inet_ntoa(p->ourip.sin_addr), get_transport(p->socket.type));
10521 }
10522 }
10523
10524
10525
10526 ast_str_append(&out, 0, "Voice-Message: %d/%d%s\r\n",
10527 newmsgs, oldmsgs, (ast_test_flag(&p->flags[1], SIP_PAGE2_BUGGY_MWI) ? "" : " (0/0)"));
10528
10529 if (p->subscribed) {
10530 if (p->expiry)
10531 add_header(&req, "Subscription-State", "active");
10532 else
10533 add_header(&req, "Subscription-State", "terminated;reason=timeout");
10534 }
10535
10536 add_header_contentLength(&req, out->used);
10537 add_line(&req, out->str);
10538
10539 if (!p->initreq.headers)
10540 initialize_initreq(p, &req);
10541 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
10542 }
10543
10544
10545 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
10546 {
10547 struct sip_request req;
10548 char tmp[SIPBUFSIZE/2];
10549
10550 reqprep(&req, p, SIP_NOTIFY, 0, 1);
10551 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
10552 add_header(&req, "Event", tmp);
10553 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
10554 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
10555 add_header(&req, "Allow", ALLOWED_METHODS);
10556 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
10557
10558 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
10559 add_header_contentLength(&req, strlen(tmp));
10560 add_line(&req, tmp);
10561
10562 if (!p->initreq.headers)
10563 initialize_initreq(p, &req);
10564
10565 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
10566 }
10567
10568
10569 static int transmit_notify_custom(struct sip_pvt *p, struct ast_variable *vars) {
10570 struct sip_request req;
10571 struct ast_variable *var, *newvar;
10572
10573 initreqprep(&req, p, SIP_NOTIFY);
10574
10575
10576 p->notify_headers = newvar = ast_variable_new("Subscription-State", "terminated", "");
10577 add_header(&req, newvar->name, newvar->value);
10578 for (var = vars; var; var = var->next) {
10579 char buf[512];
10580 ast_debug(2, " Adding pair %s=%s\n", var->name, var->value);
10581 ast_copy_string(buf, var->value, sizeof(buf));
10582 add_header(&req, var->name, ast_unescape_semicolon(buf));
10583 newvar->next = ast_variable_new(var->name, var->value, "");
10584 newvar = newvar->next;
10585 }
10586
10587 if (!p->initreq.headers) {
10588 initialize_initreq(p, &req);
10589 }
10590
10591 return send_request(p, &req, XMIT_UNRELIABLE, p->ocseq);
10592 }
10593
10594 static int manager_sipnotify(struct mansession *s, const struct message *m)
10595 {
10596 const char *channame = astman_get_header(m, "Channel");
10597 struct ast_variable *vars = astman_get_variables(m);
10598 struct sip_pvt *p;
10599
10600 if (ast_strlen_zero(channame)) {
10601 astman_send_error(s, m, "SIPNotify requires a channel name");
10602 return 0;
10603 }
10604
10605 if (!strncasecmp(channame, "sip/", 4)) {
10606 channame += 4;
10607 }
10608
10609 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
10610 astman_send_error(s, m, "Unable to build sip pvt data for notify (memory/socket error)");
10611 return 0;
10612 }
10613
10614 if (create_addr(p, channame, NULL, 0)) {
10615
10616 dialog_unlink_all(p, TRUE, TRUE);
10617 dialog_unref(p, "unref dialog inside for loop" );
10618
10619 astman_send_error(s, m, "Could not create address");
10620 return 0;
10621 }
10622
10623
10624 ast_set_flag(&p->flags[0], SIP_OUTGOING);
10625
10626
10627 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
10628 build_via(p);
10629 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
10630 build_callid_pvt(p);
10631 ao2_t_link(dialogs, p, "Linking in new name");
10632 dialog_ref(p, "bump the count of p, which transmit_sip_request will decrement.");
10633 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
10634
10635 if (!transmit_notify_custom(p, vars)) {
10636 astman_send_ack(s, m, "Notify Sent");
10637 } else {
10638 astman_send_error(s, m, "Unable to send notify");
10639 }
10640 ast_variables_destroy(vars);
10641 return 0;
10642 }
10643
10644 static char mandescr_sipnotify[] =
10645 "Description: Sends a SIP Notify event\n"
10646 "All parameters for this event must be specified in the body of this request\n"
10647 "via multiple Variable: name=value sequences.\n"
10648 "Variables: \n"
10649 " *Channel: <peername> Peer to receive the notify. Required.\n"
10650 " *Variable: <name>=<value> At least one variable pair must be specified.\n"
10651 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
10652
10653 static const struct _map_x_s regstatestrings[] = {
10654 { REG_STATE_FAILED, "Failed" },
10655 { REG_STATE_UNREGISTERED, "Unregistered"},
10656 { REG_STATE_REGSENT, "Request Sent"},
10657 { REG_STATE_AUTHSENT, "Auth. Sent"},
10658 { REG_STATE_REGISTERED, "Registered"},
10659 { REG_STATE_REJECTED, "Rejected"},
10660 { REG_STATE_TIMEOUT, "Timeout"},
10661 { REG_STATE_NOAUTH, "No Authentication"},
10662 { -1, NULL }
10663 };
10664
10665
10666 static const char *regstate2str(enum sipregistrystate regstate)
10667 {
10668 return map_x_s(regstatestrings, regstate, "Unknown");
10669 }
10670
10671
10672
10673
10674
10675
10676
10677 static int sip_reregister(const void *data)
10678 {
10679
10680 struct sip_registry *r= (struct sip_registry *) data;
10681
10682
10683 if (!r)
10684 return 0;
10685
10686 if (r->call && r->call->do_history)
10687 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
10688
10689
10690 if (sipdebug)
10691 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
10692
10693 r->expire = -1;
10694 r->expiry = r->configured_expiry;
10695 __sip_do_register(r);
10696 registry_unref(r, "unref the re-register scheduled event");
10697 return 0;
10698 }
10699
10700
10701 static int __sip_do_register(struct sip_registry *r)
10702 {
10703 int res;
10704
10705 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
10706 return res;
10707 }
10708
10709
10710
10711
10712
10713
10714
10715 static int sip_reg_timeout(const void *data)
10716 {
10717
10718
10719 struct sip_registry *r = (struct sip_registry *)data;
10720 struct sip_pvt *p;
10721 int res;
10722
10723
10724 if (!r)
10725 return 0;
10726
10727 if (r->dnsmgr) {
10728
10729 ast_dnsmgr_refresh(r->dnsmgr);
10730 }
10731
10732 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
10733
10734
10735
10736
10737 if (r->call) {
10738
10739
10740 p = r->call;
10741 sip_pvt_lock(p);
10742 p->needdestroy = 1;
10743
10744 __sip_pretend_ack(p);
10745 sip_pvt_unlock(p);
10746
10747
10748
10749 if (p->registry)
10750 p->registry = registry_unref(p->registry, "p->registry unreffed");
10751 r->call = dialog_unref(r->call, "unrefing r->call");
10752 }
10753
10754 r->timeout = -1;
10755 if (global_regattempts_max && r->regattempts > global_regattempts_max) {
10756
10757
10758
10759 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
10760 r->regstate = REG_STATE_FAILED;
10761 } else {
10762 r->regstate = REG_STATE_UNREGISTERED;
10763 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
10764 }
10765 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));
10766 registry_unref(r, "unreffing registry_unref r");
10767 return 0;
10768 }
10769
10770
10771
10772
10773 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
10774 {
10775 struct sip_request req;
10776 char from[256];
10777 char to[256];
10778 char tmp[80];
10779 char addr[80];
10780 struct sip_pvt *p;
10781 struct sip_peer *peer = NULL;
10782 int res;
10783 char *fromdomain;
10784
10785
10786 if (r == NULL || ((auth == NULL) && (r->regstate == REG_STATE_REGSENT || r->regstate == REG_STATE_AUTHSENT))) {
10787 if (r) {
10788 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
10789 }
10790 return 0;
10791 }
10792
10793 if (r->dnsmgr == NULL) {
10794 char transport[MAXHOSTNAMELEN];
10795 peer = find_peer(r->hostname, NULL, TRUE, FINDPEERS, FALSE, 0);
10796 snprintf(transport, sizeof(transport), "_sip._%s", get_transport(r->transport));
10797 ast_dnsmgr_lookup(peer ? peer->tohost : r->hostname, &r->us, &r->dnsmgr, global_srvlookup ? transport : NULL);
10798 if (peer) {
10799 peer = unref_peer(peer, "removing peer ref for dnsmgr_lookup");
10800 }
10801 }
10802
10803 if (r->call) {
10804 if (!auth) {
10805 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
10806 return 0;
10807 } else {
10808 p = dialog_ref(r->call, "getting a copy of the r->call dialog in transmit_register");
10809 make_our_tag(p->tag, sizeof(p->tag));
10810 ast_string_field_set(p, theirtag, NULL);
10811 }
10812 } else {
10813
10814 if (!r->callid_valid) {
10815 build_callid_registry(r, internip.sin_addr, default_fromdomain);
10816 r->callid_valid = TRUE;
10817 }
10818
10819 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER, NULL))) {
10820 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
10821 return 0;
10822 }
10823
10824 if (p->do_history)
10825 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
10826
10827 if (!ast_strlen_zero(r->peername)) {
10828 if (!(peer = find_peer(r->peername, NULL, 1, FINDPEERS, FALSE, 0))) {
10829 ast_log(LOG_WARNING, "Could not find peer %s in transmit_register\n", r->peername);
10830 } else {
10831 p->peerauth = peer->auth;
10832 }
10833 }
10834 ref_proxy(p, obproxy_get(p, peer));
10835 if (peer) {
10836 unref_peer(peer, "transmit_registration: from find_peer operation");
10837 }
10838
10839 if (!r->us.sin_port && r->portno)
10840 r->us.sin_port = htons(r->portno);
10841
10842
10843 if (create_addr(p, r->hostname, &r->us, 0)) {
10844
10845
10846 dialog_unlink_all(p, TRUE, TRUE);
10847 p = dialog_unref(p, "unref dialog after unlink_all");
10848 if (r->timeout > -1) {
10849 AST_SCHED_REPLACE_UNREF(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r,
10850 registry_unref(_data, "del for REPLACE of registry ptr"),
10851 registry_unref(r, "object ptr dec when SCHED_REPLACE add failed"),
10852 registry_addref(r,"add for REPLACE registry ptr"));
10853 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
10854 } else {
10855 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, registry_addref(r, "add for REPLACE registry ptr"));
10856 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);
10857 }
10858 r->regattempts++;
10859 return 0;
10860 }
10861
10862
10863 ast_string_field_set(r, callid, p->callid);
10864 if (!r->dnsmgr && r->portno) {
10865 p->sa.sin_port = htons(r->portno);
10866 p->recv.sin_port = htons(r->portno);
10867 } else {
10868 r->portno = ntohs(p->sa.sin_port);
10869 }
10870 ast_set_flag(&p->flags[0], SIP_OUTGOING);
10871 r->call = dialog_ref(p, "copying dialog into registry r->call");
10872 p->registry = registry_addref(r, "transmit_register: addref to p->registry in transmit_register");
10873 if (!ast_strlen_zero(r->secret))
10874 ast_string_field_set(p, peersecret, r->secret);
10875 if (!ast_strlen_zero(r->md5secret))
10876 ast_string_field_set(p, peermd5secret, r->md5secret);
10877
10878
10879 if (!ast_strlen_zero(r->authuser)) {
10880 ast_string_field_set(p, peername, r->authuser);
10881 ast_string_field_set(p, authname, r->authuser);
10882 } else if (!ast_strlen_zero(r->username)) {
10883 ast_string_field_set(p, peername, r->username);
10884 ast_string_field_set(p, authname, r->username);
10885 ast_string_field_set(p, fromuser, r->username);
10886 }
10887 if (!ast_strlen_zero(r->username))
10888 ast_string_field_set(p, username, r->username);
10889
10890 if (!ast_strlen_zero(r->callback))
10891 ast_string_field_set(p, exten, r->callback);
10892
10893
10894 set_socket_transport(&p->socket, r->transport);
10895 if (r->transport == SIP_TRANSPORT_TLS || r->transport == SIP_TRANSPORT_TCP) {
10896 p->socket.port = sip_tcp_desc.local_address.sin_port;
10897 }
10898
10899
10900
10901
10902
10903
10904 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
10905 build_contact(p);
10906 }
10907
10908
10909 if (auth == NULL) {
10910 if (r->timeout > -1)
10911 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
10912 AST_SCHED_REPLACE_UNREF(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r,
10913 registry_unref(_data,"reg ptr unrefed from del in SCHED_REPLACE"),
10914 registry_unref(r,"reg ptr unrefed from add failure in SCHED_REPLACE"),
10915 registry_addref(r,"reg ptr reffed from add in SCHED_REPLACE"));
10916 ast_debug(1, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
10917 }
10918
10919 if ((fromdomain = strchr(r->username, '@'))) {
10920
10921 fromdomain++ ;
10922
10923 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
10924 if (!ast_strlen_zero(p->theirtag))
10925 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
10926 else
10927 snprintf(to, sizeof(to), "<sip:%s>", r->username);
10928
10929
10930
10931 if (ast_strlen_zero(p->fromdomain)) {
10932 ast_string_field_set(p, fromdomain, fromdomain);
10933 }
10934 } else {
10935 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
10936 if (!ast_strlen_zero(p->theirtag))
10937 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
10938 else
10939 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
10940 }
10941
10942
10943
10944 if (!ast_strlen_zero(p->fromdomain)) {
10945 if (r->portno && r->portno != STANDARD_SIP_PORT)
10946 snprintf(addr, sizeof(addr), "sip:%s:%d", p->fromdomain, r->portno);
10947 else
10948 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
10949 } else {
10950 if (r->portno && r->portno != STANDARD_SIP_PORT)
10951 snprintf(addr, sizeof(addr), "sip:%s:%d", r->hostname, r->portno);
10952 else
10953 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
10954 }
10955 ast_string_field_set(p, uri, addr);
10956
10957 p->branch ^= ast_random();
10958
10959 init_req(&req, sipmethod, addr);
10960
10961
10962 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
10963 p->ocseq = r->ocseq;
10964
10965 build_via(p);
10966 add_header(&req, "Via", p->via);
10967 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
10968 add_header(&req, "From", from);
10969 add_header(&req, "To", to);
10970 add_header(&req, "Call-ID", p->callid);
10971 add_header(&req, "CSeq", tmp);
10972 if (!ast_strlen_zero(global_useragent))
10973 add_header(&req, "User-Agent", global_useragent);
10974
10975
10976 if (auth)
10977 add_header(&req, authheader, auth);
10978 else if (!ast_strlen_zero(r->nonce)) {
10979 char digest[1024];
10980
10981
10982
10983
10984
10985
10986 if (sipdebug)
10987 ast_debug(1, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
10988 ast_string_field_set(p, realm, r->realm);
10989 ast_string_field_set(p, nonce, r->nonce);
10990 ast_string_field_set(p, domain, r->domain);
10991 ast_string_field_set(p, opaque, r->opaque);
10992 ast_string_field_set(p, qop, r->qop);
10993 p->noncecount = ++r->noncecount;
10994
10995 memset(digest, 0, sizeof(digest));
10996 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
10997 add_header(&req, "Authorization", digest);
10998 else
10999 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
11000
11001 }
11002
11003 snprintf(tmp, sizeof(tmp), "%d", r->expiry);
11004 add_header(&req, "Expires", tmp);
11005 add_header(&req, "Contact", p->our_contact);
11006 add_header_contentLength(&req, 0);
11007
11008 initialize_initreq(p, &req);
11009 if (sip_debug_test_pvt(p)) {
11010 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
11011 }
11012 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
11013 r->regattempts++;
11014 ast_debug(4, "REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
11015 res = send_request(p, &req, XMIT_CRITICAL, p->ocseq);
11016 dialog_unref(p, "p is finished here at the end of transmit_register");
11017 return res;
11018 }
11019
11020
11021 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
11022 {
11023 struct sip_request req;
11024
11025 reqprep(&req, p, SIP_MESSAGE, 0, 1);
11026 add_text(&req, text);
11027 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
11028 }
11029
11030
11031 static int sip_refer_allocate(struct sip_pvt *p)
11032 {
11033 p->refer = ast_calloc(1, sizeof(struct sip_refer));
11034 return p->refer ? 1 : 0;
11035 }
11036
11037
11038
11039
11040
11041
11042 static int transmit_refer(struct sip_pvt *p, const char *dest)
11043 {
11044 struct sip_request req = {
11045 .headers = 0,
11046 };
11047 char from[256];
11048 const char *of;
11049 char *c;
11050 char referto[256];
11051 char *ttag, *ftag;
11052 char *theirtag = ast_strdupa(p->theirtag);
11053
11054 if (sipdebug)
11055 ast_debug(1, "SIP transfer of %s to %s\n", p->callid, dest);
11056
11057
11058 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
11059 of = get_header(&p->initreq, "To");
11060 ttag = theirtag;
11061 ftag = p->tag;
11062 } else {
11063 of = get_header(&p->initreq, "From");
11064 ftag = theirtag;
11065 ttag = p->tag;
11066 }
11067
11068 ast_copy_string(from, of, sizeof(from));
11069 of = get_in_brackets(from);
11070 ast_string_field_set(p, from, of);
11071 if (!strncasecmp(of, "sip:", 4))
11072 of += 4;
11073 else if (!strncasecmp(of, "sips:", 5))
11074 of += 5;
11075 else
11076 ast_log(LOG_NOTICE, "From address missing 'sip(s):', using it anyway\n");
11077
11078 if ((c = strchr(dest, '@')))
11079 c = NULL;
11080 else if ((c = strchr(of, '@')))
11081 *c++ = '\0';
11082 if (c)
11083 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
11084 else
11085 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
11086
11087
11088 sip_refer_allocate(p);
11089 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
11090 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
11091 p->refer->status = REFER_SENT;
11092
11093 reqprep(&req, p, SIP_REFER, 0, 1);
11094
11095 add_header(&req, "Refer-To", referto);
11096 add_header(&req, "Allow", ALLOWED_METHODS);
11097 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
11098 if (!ast_strlen_zero(p->our_contact))
11099 add_header(&req, "Referred-By", p->our_contact);
11100
11101 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111 }
11112
11113
11114
11115 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
11116 {
11117 struct sip_request req;
11118
11119 reqprep(&req, p, SIP_INFO, 0, 1);
11120 add_digit(&req, digit, duration, (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_SHORTINFO));
11121 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
11122 }
11123
11124
11125 static int transmit_info_with_vidupdate(struct sip_pvt *p)
11126 {
11127 struct sip_request req;
11128
11129 reqprep(&req, p, SIP_INFO, 0, 1);
11130 add_vidupdate(&req);
11131 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
11132 }
11133
11134
11135
11136
11137 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
11138 {
11139 struct sip_request resp;
11140
11141 if (sipmethod == SIP_ACK)
11142 p->invitestate = INV_CONFIRMED;
11143
11144 reqprep(&resp, p, sipmethod, seqno, newbranch);
11145 if (sipmethod == SIP_CANCEL && p->answered_elsewhere)
11146 add_header(&resp, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
11147
11148 add_header_contentLength(&resp, 0);
11149 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
11150 }
11151
11152
11153 static void auth_headers(enum sip_auth_type code, char **header, char **respheader)
11154 {
11155 if (code == WWW_AUTH) {
11156 *header = "WWW-Authenticate";
11157 *respheader = "Authorization";
11158 } else if (code == PROXY_AUTH) {
11159 *header = "Proxy-Authenticate";
11160 *respheader = "Proxy-Authorization";
11161 } else {
11162 ast_verbose("-- wrong response code %d\n", code);
11163 *header = *respheader = "Invalid";
11164 }
11165 }
11166
11167
11168 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
11169 {
11170 struct sip_request resp;
11171
11172 reqprep(&resp, p, sipmethod, seqno, newbranch);
11173 if (!ast_strlen_zero(p->realm)) {
11174 char digest[1024];
11175
11176 memset(digest, 0, sizeof(digest));
11177 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
11178 char *dummy, *response;
11179 enum sip_auth_type code = p->options ? p->options->auth_type : PROXY_AUTH;
11180 auth_headers(code, &dummy, &response);
11181 add_header(&resp, response, digest);
11182 } else
11183 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
11184 }
11185
11186
11187 if (sipmethod == SIP_BYE) {
11188 char buf[10];
11189
11190 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->hangupcause));
11191 snprintf(buf, sizeof(buf), "%d", p->hangupcause);
11192 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
11193 }
11194
11195 add_header_contentLength(&resp, 0);
11196 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
11197 }
11198
11199
11200 static void destroy_association(struct sip_peer *peer)
11201 {
11202 int realtimeregs = ast_check_realtime("sipregs");
11203 char *tablename = (realtimeregs) ? "sipregs" : "sippeers";
11204
11205 if (!sip_cfg.ignore_regexpire) {
11206 if (peer->rt_fromcontact && sip_cfg.peer_rtupdate) {
11207 ast_update_realtime(tablename, "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", peer->deprecated_username ? "username" : "defaultuser", "", "regserver", "", "useragent", "", "lastms", "", SENTINEL);
11208 } else {
11209 ast_db_del("SIP/Registry", peer->name);
11210 }
11211 }
11212 }
11213
11214 static void set_socket_transport(struct sip_socket *socket, int transport)
11215 {
11216
11217 if (socket->type != transport) {
11218 socket->fd = -1;
11219 socket->type = transport;
11220 if (socket->tcptls_session) {
11221 ao2_ref(socket->tcptls_session, -1);
11222 socket->tcptls_session = NULL;
11223 }
11224 }
11225 }
11226
11227
11228 static int expire_register(const void *data)
11229 {
11230 struct sip_peer *peer = (struct sip_peer *)data;
11231
11232 if (!peer)
11233 return 0;
11234
11235 peer->expire = -1;
11236 peer->portinuri = 0;
11237 memset(&peer->addr, 0, sizeof(peer->addr));
11238
11239 destroy_association(peer);
11240 set_socket_transport(&peer->socket, peer->default_outbound_transport);
11241
11242 if (peer->socket.tcptls_session) {
11243 ao2_ref(peer->socket.tcptls_session, -1);
11244 peer->socket.tcptls_session = NULL;
11245 }
11246
11247 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
11248 register_peer_exten(peer, FALSE);
11249 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
11250
11251
11252
11253
11254 if (peer->is_realtime)
11255 ast_debug(3, "-REALTIME- peer expired registration. Name: %s. Realtime peer objects now %d\n", peer->name, rpeerobjs);
11256
11257 if (peer->selfdestruct ||
11258 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
11259 ao2_t_unlink(peers, peer, "ao2_unlink of peer from peers table");
11260 if (peer->addr.sin_addr.s_addr) {
11261 ao2_t_unlink(peers_by_ip, peer, "ao2_unlink of peer from peers_by_ip table");
11262 }
11263 }
11264
11265 unref_peer(peer, "removing peer ref for expire_register");
11266
11267 return 0;
11268 }
11269
11270
11271 static int sip_poke_peer_s(const void *data)
11272 {
11273 struct sip_peer *peer = (struct sip_peer *)data;
11274
11275 peer->pokeexpire = -1;
11276
11277 sip_poke_peer(peer, 0);
11278
11279 unref_peer(peer, "removing poke peer ref");
11280
11281 return 0;
11282 }
11283
11284
11285 static void reg_source_db(struct sip_peer *peer)
11286 {
11287 char data[256];
11288 struct in_addr in;
11289 int expire;
11290 int port;
11291 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
11292
11293 if (peer->rt_fromcontact)
11294 return;
11295 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
11296 return;
11297
11298 scan = data;
11299 addr = strsep(&scan, ":");
11300 port_str = strsep(&scan, ":");
11301 expiry_str = strsep(&scan, ":");
11302 username = strsep(&scan, ":");
11303 contact = scan;
11304
11305 if (!inet_aton(addr, &in))
11306 return;
11307
11308 if (port_str)
11309 port = atoi(port_str);
11310 else
11311 return;
11312
11313 if (expiry_str)
11314 expire = atoi(expiry_str);
11315 else
11316 return;
11317
11318 if (username)
11319 ast_copy_string(peer->username, username, sizeof(peer->username));
11320 if (contact)
11321 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
11322
11323 ast_debug(2, "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
11324 peer->name, peer->username, ast_inet_ntoa(in), port, expire);
11325
11326 memset(&peer->addr, 0, sizeof(peer->addr));
11327 peer->addr.sin_family = AF_INET;
11328 peer->addr.sin_addr = in;
11329 peer->addr.sin_port = htons(port);
11330 if (sipsock < 0) {
11331
11332 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, ast_random() % 5000 + 1, sip_poke_peer_s, peer,
11333 unref_peer(_data, "removing poke peer ref"),
11334 unref_peer(peer, "removing poke peer ref"),
11335 ref_peer(peer, "adding poke peer ref"));
11336 } else {
11337 sip_poke_peer(peer, 0);
11338 }
11339 AST_SCHED_REPLACE_UNREF(peer->expire, sched, (expire + 10) * 1000, expire_register, peer,
11340 unref_peer(_data, "remove registration ref"),
11341 unref_peer(peer, "remove registration ref"),
11342 ref_peer(peer, "add registration ref"));
11343 register_peer_exten(peer, TRUE);
11344 }
11345
11346
11347 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
11348 {
11349 char contact[SIPBUFSIZE];
11350 char *c;
11351
11352
11353 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
11354 c = get_in_brackets(contact);
11355
11356
11357 ast_string_field_set(pvt, fullcontact, c);
11358
11359
11360 ast_string_field_set(pvt, okcontacturi, c);
11361
11362
11363
11364 return TRUE;
11365 }
11366
11367 static int __set_address_from_contact(const char *fullcontact, struct sockaddr_in *sin, int tcp)
11368 {
11369 struct hostent *hp;
11370 struct ast_hostent ahp;
11371 int port = STANDARD_SIP_PORT;
11372 char *host, *pt, *transport;
11373 char contact_buf[256];
11374 char *contact;
11375
11376
11377 ast_copy_string(contact_buf, fullcontact, sizeof(contact_buf));
11378 contact = contact_buf;
11379
11380
11381
11382
11383
11384
11385
11386
11387 if (parse_uri(contact, "sip:,sips:", &contact, NULL, &host, &pt, NULL, &transport)) {
11388 ast_log(LOG_WARNING, "Invalid contact uri %s (missing sip: or sips:), attempting to use anyway\n", fullcontact);
11389 }
11390
11391
11392 if (((get_transport_str2enum(transport) == SIP_TRANSPORT_TLS)) || !(strncasecmp(fullcontact, "sips", 4))) {
11393 port = port_str2int(pt, STANDARD_TLS_PORT);
11394 } else {
11395 port = port_str2int(pt, STANDARD_SIP_PORT);
11396 }
11397
11398
11399
11400 hp = ast_gethostbyname(host, &ahp);
11401 if (!hp) {
11402 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't resolve in DNS) : '%s'\n", host);
11403 return -1;
11404 }
11405 sin->sin_family = AF_INET;
11406 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
11407 sin->sin_port = htons(port);
11408
11409 return 0;
11410 }
11411
11412
11413 static int set_address_from_contact(struct sip_pvt *pvt)
11414 {
11415 if (ast_test_flag(&pvt->flags[0], SIP_NAT_ROUTE)) {
11416
11417
11418 pvt->sa = pvt->recv;
11419 return 0;
11420 }
11421
11422 return __set_address_from_contact(pvt->fullcontact, &pvt->sa, pvt->socket.type == SIP_TRANSPORT_TLS ? 1 : 0);
11423 }
11424
11425
11426 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
11427 {
11428 char contact[SIPBUFSIZE];
11429 char data[SIPBUFSIZE];
11430 const char *expires = get_header(req, "Expires");
11431 int expire = atoi(expires);
11432 char *curi, *host, *pt, *transport;
11433 int port;
11434 int transport_type;
11435 const char *useragent;
11436 struct hostent *hp;
11437 struct ast_hostent ahp;
11438 struct sockaddr_in oldsin, testsin;
11439
11440
11441 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
11442
11443 if (ast_strlen_zero(expires)) {
11444 char *s = strcasestr(contact, ";expires=");
11445 if (s) {
11446 expires = strsep(&s, ";");
11447 if (sscanf(expires + 9, "%30d", &expire) != 1)
11448 expire = default_expiry;
11449 } else {
11450
11451 expire = default_expiry;
11452 }
11453 }
11454
11455 copy_socket_data(&pvt->socket, &req->socket);
11456
11457
11458 curi = contact;
11459 if (strchr(contact, '<') == NULL)
11460 strsep(&curi, ";");
11461 curi = get_in_brackets(contact);
11462
11463
11464
11465
11466
11467 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
11468
11469 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact))
11470 pvt->expiry = ast_sched_when(sched, peer->expire);
11471 return PARSE_REGISTER_QUERY;
11472 } else if (!strcasecmp(curi, "*") || !expire) {
11473
11474 memset(&peer->addr, 0, sizeof(peer->addr));
11475 set_socket_transport(&peer->socket, peer->default_outbound_transport);
11476
11477 AST_SCHED_DEL_UNREF(sched, peer->expire,
11478 unref_peer(peer, "remove register expire ref"));
11479
11480 destroy_association(peer);
11481
11482 register_peer_exten(peer, FALSE);
11483 peer->fullcontact[0] = '\0';
11484 peer->useragent[0] = '\0';
11485 peer->sipoptions = 0;
11486 peer->lastms = 0;
11487 peer->portinuri = 0;
11488 pvt->expiry = 0;
11489
11490 ast_verb(3, "Unregistered SIP '%s'\n", peer->name);
11491
11492 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unregistered\r\n", peer->name);
11493 return PARSE_REGISTER_UPDATE;
11494 }
11495
11496
11497 ast_copy_string(peer->fullcontact, curi, sizeof(peer->fullcontact));
11498
11499
11500 ast_string_field_build(pvt, our_contact, "<%s>", curi);
11501
11502
11503 if (parse_uri(curi, "sip:,sips:", &curi, NULL, &host, &pt, NULL, &transport)) {
11504 ast_log(LOG_NOTICE, "Not a valid SIP contact (missing sip:) trying to use anyway\n");
11505 }
11506
11507
11508
11509 peer->portinuri = !ast_strlen_zero(pt) ? TRUE : FALSE;
11510
11511
11512 if ((transport_type = get_transport_str2enum(transport))) {
11513
11514
11515
11516
11517 port = port_str2int(pt, (transport_type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT);
11518 } else {
11519 port = port_str2int(pt, STANDARD_SIP_PORT);
11520 transport_type = pvt->socket.type;
11521 }
11522
11523
11524
11525
11526 if ((peer->socket.type != transport_type) && (peer->transports & transport_type)) {
11527 set_socket_transport(&peer->socket, transport_type);
11528 }
11529
11530 oldsin = peer->addr;
11531
11532
11533 if (peer->addr.sin_addr.s_addr) {
11534 ao2_t_unlink(peers_by_ip, peer, "ao2_unlink of peer from peers_by_ip table");
11535 }
11536
11537
11538
11539 hp = ast_gethostbyname(host, &ahp);
11540 if (!hp) {
11541 ast_log(LOG_WARNING, "Invalid host '%s'\n", host);
11542 *peer->fullcontact = '\0';
11543 ast_string_field_set(pvt, our_contact, "");
11544 return PARSE_REGISTER_FAILED;
11545 }
11546 memcpy(&testsin.sin_addr, hp->h_addr, sizeof(testsin.sin_addr));
11547 if ( ast_apply_ha(global_contact_ha, &testsin) != AST_SENSE_ALLOW ||
11548 ast_apply_ha(peer->contactha, &testsin) != AST_SENSE_ALLOW) {
11549 ast_log(LOG_WARNING, "Host '%s' disallowed by contact ACL (violating IP %s)\n", host, ast_inet_ntoa(testsin.sin_addr));
11550 *peer->fullcontact = '\0';
11551 ast_string_field_set(pvt, our_contact, "");
11552 return PARSE_REGISTER_DENIED;
11553 }
11554
11555 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
11556 peer->addr.sin_family = AF_INET;
11557 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
11558 peer->addr.sin_port = htons(port);
11559 } else {
11560
11561
11562 peer->addr = pvt->recv;
11563 }
11564
11565
11566
11567
11568 if ((peer->socket.type == pvt->socket.type) &&
11569 (peer->addr.sin_addr.s_addr == pvt->recv.sin_addr.s_addr) &&
11570 (peer->addr.sin_port == pvt->recv.sin_port)){
11571
11572 copy_socket_data(&peer->socket, &pvt->socket);
11573 }
11574
11575
11576 ao2_t_link(peers_by_ip, peer, "ao2_link into peers_by_ip table");
11577
11578
11579 peer->sipoptions = pvt->sipoptions;
11580
11581 if (!ast_strlen_zero(curi) && ast_strlen_zero(peer->username))
11582 ast_copy_string(peer->username, curi, sizeof(peer->username));
11583
11584 AST_SCHED_DEL_UNREF(sched, peer->expire,
11585 unref_peer(peer, "remove register expire ref"));
11586
11587 if (expire > max_expiry)
11588 expire = max_expiry;
11589 if (expire < min_expiry)
11590 expire = min_expiry;
11591 if (peer->is_realtime && !ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11592 peer->expire = -1;
11593 } else {
11594 peer->expire = ast_sched_add(sched, (expire + 10) * 1000, expire_register,
11595 ref_peer(peer, "add registration ref"));
11596 if (peer->expire == -1) {
11597 unref_peer(peer, "remote registration ref");
11598 }
11599 }
11600 pvt->expiry = expire;
11601 snprintf(data, sizeof(data), "%s:%d:%d:%s:%s", ast_inet_ntoa(peer->addr.sin_addr), ntohs(peer->addr.sin_port), expire, peer->username, peer->fullcontact);
11602
11603
11604
11605
11606 if (!peer->rt_fromcontact && (peer->socket.type & SIP_TRANSPORT_UDP))
11607 ast_db_put("SIP/Registry", peer->name, data);
11608 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\nAddress: %s\r\nPort: %d\r\n", peer->name, ast_inet_ntoa(peer->addr.sin_addr), ntohs(peer->addr.sin_port));
11609
11610
11611 if (VERBOSITY_ATLEAST(2) && inaddrcmp(&peer->addr, &oldsin)) {
11612 ast_verbose(VERBOSE_PREFIX_3 "Registered SIP '%s' at %s port %d\n", peer->name, ast_inet_ntoa(peer->addr.sin_addr), ntohs(peer->addr.sin_port));
11613 }
11614 sip_poke_peer(peer, 0);
11615 register_peer_exten(peer, 1);
11616
11617
11618 useragent = get_header(req, "User-Agent");
11619 if (strcasecmp(useragent, peer->useragent)) {
11620 ast_copy_string(peer->useragent, useragent, sizeof(peer->useragent));
11621 ast_verb(4, "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
11622 }
11623 return PARSE_REGISTER_UPDATE;
11624 }
11625
11626
11627 static void free_old_route(struct sip_route *route)
11628 {
11629 struct sip_route *next;
11630
11631 while (route) {
11632 next = route->next;
11633 ast_free(route);
11634 route = next;
11635 }
11636 }
11637
11638
11639 static void list_route(struct sip_route *route)
11640 {
11641 if (!route)
11642 ast_verbose("list_route: no route\n");
11643 else {
11644 for (;route; route = route->next)
11645 ast_verbose("list_route: hop: <%s>\n", route->hop);
11646 }
11647 }
11648
11649
11650 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
11651 {
11652 struct sip_route *thishop, *head, *tail;
11653 int start = 0;
11654 int len;
11655 const char *rr, *contact, *c;
11656
11657
11658 if (p->route && p->route_persistant) {
11659 ast_debug(1, "build_route: Retaining previous route: <%s>\n", p->route->hop);
11660 return;
11661 }
11662
11663 if (p->route) {
11664 free_old_route(p->route);
11665 p->route = NULL;
11666 }
11667
11668
11669 p->route_persistant = 1;
11670
11671
11672
11673
11674
11675
11676 head = NULL;
11677 tail = head;
11678
11679 for (;;) {
11680
11681 rr = __get_header(req, "Record-Route", &start);
11682 if (*rr == '\0')
11683 break;
11684 for (; (rr = strchr(rr, '<')) ; rr += len) {
11685 ++rr;
11686 len = strcspn(rr, ">") + 1;
11687
11688 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
11689
11690 ast_copy_string(thishop->hop, rr, len);
11691 ast_debug(2, "build_route: Record-Route hop: <%s>\n", thishop->hop);
11692
11693 if (backwards) {
11694
11695 thishop->next = head;
11696 head = thishop;
11697
11698 if (!tail)
11699 tail = thishop;
11700 } else {
11701 thishop->next = NULL;
11702
11703 if (tail)
11704 tail->next = thishop;
11705 else
11706 head = thishop;
11707 tail = thishop;
11708 }
11709 }
11710 }
11711 }
11712
11713
11714 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop, ";lr") == NULL) ) {
11715
11716
11717 contact = get_header(req, "Contact");
11718 if (!ast_strlen_zero(contact)) {
11719 ast_debug(2, "build_route: Contact hop: %s\n", contact);
11720
11721 c = strchr(contact, '<');
11722 if (c) {
11723
11724 ++c;
11725 len = strcspn(c, ">") + 1;
11726 } else {
11727
11728 c = contact;
11729 len = strlen(contact) + 1;
11730 }
11731 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
11732
11733 ast_copy_string(thishop->hop, c, len);
11734 thishop->next = NULL;
11735
11736 if (tail)
11737 tail->next = thishop;
11738 else
11739 head = thishop;
11740 }
11741 }
11742 }
11743
11744
11745 p->route = head;
11746
11747
11748 if (sip_debug_test_pvt(p))
11749 list_route(p->route);
11750 }
11751
11752
11753
11754
11755
11756
11757
11758 static void set_nonce_randdata(struct sip_pvt *p, int forceupdate)
11759 {
11760 if (p->stalenonce || forceupdate || ast_strlen_zero(p->randdata)) {
11761 ast_string_field_build(p, randdata, "%08lx", ast_random());
11762 p->stalenonce = 0;
11763 }
11764 }
11765
11766 AST_THREADSTORAGE(check_auth_buf);
11767 #define CHECK_AUTH_BUF_INITLEN 256
11768
11769
11770
11771
11772
11773
11774 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
11775 const char *secret, const char *md5secret, int sipmethod,
11776 char *uri, enum xmittype reliable, int ignore)
11777 {
11778 const char *response;
11779 char *reqheader, *respheader;
11780 const char *authtoken;
11781 char a1_hash[256];
11782 char resp_hash[256]="";
11783 char *c;
11784 int wrongnonce = FALSE;
11785 int good_response;
11786 const char *usednonce = p->randdata;
11787 struct ast_str *buf;
11788 int res;
11789
11790
11791 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
11792 struct x {
11793 const char *key;
11794 const char *s;
11795 } *i, keys[] = {
11796 [K_RESP] = { "response=", "" },
11797 [K_URI] = { "uri=", "" },
11798 [K_USER] = { "username=", "" },
11799 [K_NONCE] = { "nonce=", "" },
11800 [K_LAST] = { NULL, NULL}
11801 };
11802
11803
11804 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
11805 return AUTH_SUCCESSFUL;
11806
11807
11808
11809 response = "401 Unauthorized";
11810
11811
11812
11813
11814
11815 auth_headers(WWW_AUTH, &respheader, &reqheader);
11816
11817 authtoken = get_header(req, reqheader);
11818 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
11819
11820
11821 if (!reliable) {
11822
11823
11824 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
11825
11826 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11827 }
11828 return AUTH_CHALLENGE_SENT;
11829 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
11830
11831 set_nonce_randdata(p, 1);
11832 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
11833
11834 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11835 return AUTH_CHALLENGE_SENT;
11836 }
11837
11838
11839
11840
11841
11842
11843 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN)))
11844 return AUTH_SECRET_FAILED;
11845
11846
11847 res = ast_str_set(&buf, 0, "%s", authtoken);
11848
11849 if (res == AST_DYNSTR_BUILD_FAILED)
11850 return AUTH_SECRET_FAILED;
11851
11852 c = buf->str;
11853
11854 while(c && *(c = ast_skip_blanks(c)) ) {
11855 for (i = keys; i->key != NULL; i++) {
11856 const char *separator = ",";
11857
11858 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
11859 continue;
11860
11861 c += strlen(i->key);
11862 if (*c == '"') {
11863 c++;
11864 separator = "\"";
11865 }
11866 i->s = c;
11867 strsep(&c, separator);
11868 break;
11869 }
11870 if (i->key == NULL)
11871 strsep(&c, " ,");
11872 }
11873
11874
11875 if (strcmp(username, keys[K_USER].s)) {
11876 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
11877 username, keys[K_USER].s);
11878
11879 return AUTH_USERNAME_MISMATCH;
11880 }
11881
11882
11883
11884 if (strcasecmp(p->randdata, keys[K_NONCE].s) || p->stalenonce) {
11885 wrongnonce = TRUE;
11886 usednonce = keys[K_NONCE].s;
11887 } else {
11888 p->stalenonce = 1;
11889 }
11890
11891 if (!ast_strlen_zero(md5secret))
11892 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
11893 else {
11894 char a1[256];
11895 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
11896 ast_md5_hash(a1_hash, a1);
11897 }
11898
11899
11900 {
11901 char a2[256];
11902 char a2_hash[256];
11903 char resp[256];
11904
11905 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
11906 S_OR(keys[K_URI].s, uri));
11907 ast_md5_hash(a2_hash, a2);
11908 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
11909 ast_md5_hash(resp_hash, resp);
11910 }
11911
11912 good_response = keys[K_RESP].s &&
11913 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
11914 if (wrongnonce) {
11915 if (good_response) {
11916 if (sipdebug)
11917 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "To"));
11918
11919 set_nonce_randdata(p, 0);
11920 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
11921 } else {
11922
11923 if (!req->ignore) {
11924 if (sipdebug)
11925 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
11926 set_nonce_randdata(p, 1);
11927 } else {
11928 if (sipdebug)
11929 ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", get_header(req, "To"));
11930 }
11931 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
11932 }
11933
11934
11935 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11936 return AUTH_CHALLENGE_SENT;
11937 }
11938 if (good_response) {
11939 append_history(p, "AuthOK", "Auth challenge succesful for %s", username);
11940 return AUTH_SUCCESSFUL;
11941 }
11942
11943
11944
11945
11946
11947
11948 return AUTH_SECRET_FAILED;
11949 }
11950
11951
11952 static void sip_peer_hold(struct sip_pvt *p, int hold)
11953 {
11954 struct sip_peer *peer = find_peer(p->peername, NULL, 1, FINDALLDEVICES, FALSE, 0);
11955
11956 if (!peer)
11957 return;
11958
11959
11960 ast_atomic_fetchadd_int(&peer->onHold, (hold ? +1 : -1));
11961
11962
11963 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
11964 unref_peer(peer, "sip_peer_hold: from find_peer operation");
11965
11966 return;
11967 }
11968
11969
11970 static void mwi_event_cb(const struct ast_event *event, void *userdata)
11971 {
11972 struct sip_peer *peer = userdata;
11973
11974 ao2_lock(peer);
11975 sip_send_mwi_to_peer(peer, event, 0);
11976 ao2_unlock(peer);
11977 }
11978
11979
11980
11981
11982 static int cb_extensionstate(char *context, char* exten, int state, void *data)
11983 {
11984 struct sip_pvt *p = data;
11985
11986 sip_pvt_lock(p);
11987
11988 switch(state) {
11989 case AST_EXTENSION_DEACTIVATED:
11990 case AST_EXTENSION_REMOVED:
11991 if (p->autokillid > -1 && sip_cancel_destroy(p))
11992 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
11993 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11994 ast_verb(2, "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
11995 p->stateid = -1;
11996 p->subscribed = NONE;
11997 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
11998 break;
11999 default:
12000 p->laststate = state;
12001 break;
12002 }
12003 if (p->subscribed != NONE) {
12004 if (!p->pendinginvite) {
12005 transmit_state_notify(p, state, 1, FALSE);
12006 } else {
12007
12008
12009 ast_set_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
12010 }
12011 }
12012 ast_verb(2, "Extension Changed %s[%s] new state %s for Notify User %s %s\n", exten, context, ast_extension_state2str(state), p->username,
12013 ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE) ? "(queued)" : "");
12014
12015 sip_pvt_unlock(p);
12016
12017 return 0;
12018 }
12019
12020
12021
12022
12023 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable)
12024 {
12025
12026
12027 const char *response = "407 Proxy Authentication Required";
12028 const char *reqheader = "Proxy-Authorization";
12029 const char *respheader = "Proxy-Authenticate";
12030 const char *authtoken;
12031 struct ast_str *buf;
12032 char *c;
12033
12034
12035 enum keys { K_NONCE, K_LAST };
12036 struct x {
12037 const char *key;
12038 const char *s;
12039 } *i, keys[] = {
12040 [K_NONCE] = { "nonce=", "" },
12041 [K_LAST] = { NULL, NULL}
12042 };
12043
12044 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
12045 response = "401 Unauthorized";
12046 reqheader = "Authorization";
12047 respheader = "WWW-Authenticate";
12048 }
12049 authtoken = get_header(req, reqheader);
12050 if (req->ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
12051
12052
12053 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
12054
12055 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12056 return;
12057 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
12058
12059 set_nonce_randdata(p, 1);
12060 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
12061
12062 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12063 return;
12064 }
12065
12066 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
12067 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
12068 return;
12069 }
12070
12071
12072 if (ast_str_set(&buf, 0, "%s", authtoken) == AST_DYNSTR_BUILD_FAILED) {
12073 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
12074 return;
12075 }
12076
12077 c = buf->str;
12078
12079 while (c && *(c = ast_skip_blanks(c))) {
12080 for (i = keys; i->key != NULL; i++) {
12081 const char *separator = ",";
12082
12083 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
12084 continue;
12085 }
12086
12087 c += strlen(i->key);
12088 if (*c == '"') {
12089 c++;
12090 separator = "\"";
12091 }
12092 i->s = c;
12093 strsep(&c, separator);
12094 break;
12095 }
12096 if (i->key == NULL) {
12097 strsep(&c, " ,");
12098 }
12099 }
12100
12101
12102 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
12103 if (!req->ignore) {
12104 set_nonce_randdata(p, 1);
12105 }
12106 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
12107
12108
12109 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12110 } else {
12111 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
12112 }
12113 }
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126 static char *terminate_uri(char *uri)
12127 {
12128 char *t = uri;
12129 while (*t && *t > ' ' && *t != ';')
12130 t++;
12131 *t = '\0';
12132 return uri;
12133 }
12134
12135
12136
12137
12138
12139
12140 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
12141 struct sip_request *req, char *uri)
12142 {
12143 enum check_auth_result res = AUTH_NOT_FOUND;
12144 struct sip_peer *peer;
12145 char tmp[256];
12146 char *name, *c;
12147 char *domain;
12148
12149 terminate_uri(uri);
12150
12151 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
12152 if (pedanticsipchecking)
12153 ast_uri_decode(tmp);
12154
12155 c = get_in_brackets(tmp);
12156 c = remove_uri_parameters(c);
12157
12158 if (!strncasecmp(c, "sip:", 4)) {
12159 name = c + 4;
12160 } else if (!strncasecmp(c, "sips:", 5)) {
12161 name = c + 5;
12162 } else {
12163 name = c;
12164 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(sin->sin_addr));
12165 }
12166
12167
12168
12169
12170
12171 if ((c = strchr(name, '@'))) {
12172 *c++ = '\0';
12173 domain = c;
12174 if ((c = strchr(domain, ':')))
12175 *c = '\0';
12176 if (!AST_LIST_EMPTY(&domain_list)) {
12177 if (!check_sip_domain(domain, NULL, 0)) {
12178 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
12179 return AUTH_UNKNOWN_DOMAIN;
12180 }
12181 }
12182 }
12183 c = strchr(name, ';');
12184 if (c)
12185 *c = '\0';
12186
12187 ast_string_field_set(p, exten, name);
12188 build_contact(p);
12189 if (req->ignore) {
12190
12191 const char *expires = get_header(req, "Expires");
12192 int expire = atoi(expires);
12193
12194 if (ast_strlen_zero(expires)) {
12195 if ((expires = strcasestr(get_header(req, "Contact"), ";expires="))) {
12196 expire = atoi(expires + 9);
12197 }
12198 }
12199 if (!ast_strlen_zero(expires) && expire == 0) {
12200 transmit_response_with_date(p, "200 OK", req);
12201 return 0;
12202 }
12203 }
12204 peer = find_peer(name, NULL, TRUE, FINDPEERS, FALSE, 0);
12205 if (!(peer && ast_apply_ha(peer->ha, sin))) {
12206
12207 if (peer) {
12208 unref_peer(peer, "register_verify: unref_peer: from find_peer operation");
12209 peer = NULL;
12210 res = AUTH_ACL_FAILED;
12211 } else
12212 res = AUTH_NOT_FOUND;
12213 }
12214
12215 if (peer) {
12216
12217 if (p->rtp) {
12218 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
12219 p->autoframing = peer->autoframing;
12220 }
12221 if (!peer->host_dynamic) {
12222 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
12223 res = AUTH_PEER_NOT_DYNAMIC;
12224 } else {
12225 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
12226 if (ast_test_flag(&p->flags[1], SIP_PAGE2_REGISTERTRYING))
12227 transmit_response(p, "100 Trying", req);
12228 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, req->ignore))) {
12229 if (sip_cancel_destroy(p))
12230 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12231
12232 if (check_request_transport(peer, req)) {
12233 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
12234 transmit_response_with_date(p, "403 Forbidden", req);
12235 res = AUTH_BAD_TRANSPORT;
12236 } else {
12237
12238
12239
12240 switch (parse_register_contact(p, peer, req)) {
12241 case PARSE_REGISTER_DENIED:
12242 transmit_response_with_date(p, "603 Denied", req);
12243 peer->lastmsgssent = -1;
12244 res = 0;
12245 break;
12246 case PARSE_REGISTER_FAILED:
12247 ast_log(LOG_WARNING, "Failed to parse contact info\n");
12248 transmit_response_with_date(p, "400 Bad Request", req);
12249 peer->lastmsgssent = -1;
12250 res = 0;
12251 break;
12252 case PARSE_REGISTER_QUERY:
12253 ast_string_field_set(p, fullcontact, peer->fullcontact);
12254 transmit_response_with_date(p, "200 OK", req);
12255 peer->lastmsgssent = -1;
12256 res = 0;
12257 break;
12258 case PARSE_REGISTER_UPDATE:
12259 ast_string_field_set(p, fullcontact, peer->fullcontact);
12260 update_peer(peer, p->expiry);
12261
12262 transmit_response_with_date(p, "200 OK", req);
12263 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
12264 peer->lastmsgssent = -1;
12265 res = 0;
12266 break;
12267 }
12268 }
12269
12270 }
12271 }
12272 }
12273 if (!peer && autocreatepeer) {
12274
12275 peer = temp_peer(name);
12276 if (peer) {
12277 ao2_t_link(peers, peer, "link peer into peer table");
12278 if (peer->addr.sin_addr.s_addr) {
12279 ao2_t_link(peers_by_ip, peer, "link peer into peers-by-ip table");
12280 }
12281
12282 if (sip_cancel_destroy(p))
12283 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12284 switch (parse_register_contact(p, peer, req)) {
12285 case PARSE_REGISTER_DENIED:
12286 transmit_response_with_date(p, "403 Forbidden (ACL)", req);
12287 peer->lastmsgssent = -1;
12288 res = 0;
12289 break;
12290 case PARSE_REGISTER_FAILED:
12291 ast_log(LOG_WARNING, "Failed to parse contact info\n");
12292 transmit_response_with_date(p, "400 Bad Request", req);
12293 peer->lastmsgssent = -1;
12294 res = 0;
12295 break;
12296 case PARSE_REGISTER_QUERY:
12297 ast_string_field_set(p, fullcontact, peer->fullcontact);
12298 transmit_response_with_date(p, "200 OK", req);
12299 peer->lastmsgssent = -1;
12300 res = 0;
12301 break;
12302 case PARSE_REGISTER_UPDATE:
12303 ast_string_field_set(p, fullcontact, peer->fullcontact);
12304
12305 transmit_response_with_date(p, "200 OK", req);
12306 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\nAddress: %s\r\nPort: %d\r\n", peer->name, ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
12307 peer->lastmsgssent = -1;
12308 res = 0;
12309 break;
12310 }
12311 }
12312 }
12313 if (!peer && global_alwaysauthreject) {
12314
12315
12316
12317 transmit_response(p, "100 Trying", req);
12318
12319 sched_yield();
12320 }
12321 if (!res) {
12322 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
12323 }
12324 if (res < 0) {
12325 switch (res) {
12326 case AUTH_SECRET_FAILED:
12327
12328 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
12329 if (global_authfailureevents)
12330 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Rejected\r\nCause: AUTH_SECRET_FAILED\r\nAddress: %s\r\nPort: %d\r\n",
12331 name, ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
12332 break;
12333 case AUTH_USERNAME_MISMATCH:
12334
12335
12336
12337
12338 case AUTH_NOT_FOUND:
12339 case AUTH_PEER_NOT_DYNAMIC:
12340 case AUTH_ACL_FAILED:
12341 if (global_alwaysauthreject) {
12342 transmit_fake_auth_response(p, SIP_REGISTER, &p->initreq, XMIT_UNRELIABLE);
12343 } else {
12344
12345 if (res == AUTH_PEER_NOT_DYNAMIC) {
12346 transmit_response(p, "403 Forbidden", &p->initreq);
12347 if (global_authfailureevents) {
12348 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
12349 "ChannelType: SIP\r\n"
12350 "Peer: SIP/%s\r\n"
12351 "PeerStatus: Rejected\r\n"
12352 "Cause: AUTH_PEER_NOT_DYNAMIC\r\n"
12353 "Address: %s\r\n"
12354 "Port: %d\r\n",
12355 name, ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
12356 }
12357 } else {
12358 transmit_response(p, "404 Not found", &p->initreq);
12359 if (global_authfailureevents) {
12360 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
12361 "ChannelType: SIP\r\n"
12362 "Peer: SIP/%s\r\n"
12363 "PeerStatus: Rejected\r\n"
12364 "Cause: %s\r\n"
12365 "Address: %s\r\n"
12366 "Port: %d\r\n",
12367 name,
12368 (res == AUTH_USERNAME_MISMATCH) ? "AUTH_USERNAME_MISMATCH" : "URI_NOT_FOUND",
12369 ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
12370 }
12371 }
12372 }
12373 break;
12374 case AUTH_BAD_TRANSPORT:
12375 default:
12376 break;
12377 }
12378 }
12379 if (peer)
12380 unref_peer(peer, "register_verify: unref_peer: tossing stack peer pointer at end of func");
12381
12382 return res;
12383 }
12384
12385
12386 static void sip_set_redirstr(struct sip_pvt *p, char *reason) {
12387
12388 if (!strcmp(reason, "unknown")) {
12389 ast_string_field_set(p, redircause, "UNKNOWN");
12390 } else if (!strcmp(reason, "user-busy")) {
12391 ast_string_field_set(p, redircause, "BUSY");
12392 } else if (!strcmp(reason, "no-answer")) {
12393 ast_string_field_set(p, redircause, "NOANSWER");
12394 } else if (!strcmp(reason, "unavailable")) {
12395 ast_string_field_set(p, redircause, "UNREACHABLE");
12396 } else if (!strcmp(reason, "unconditional")) {
12397 ast_string_field_set(p, redircause, "UNCONDITIONAL");
12398 } else if (!strcmp(reason, "time-of-day")) {
12399 ast_string_field_set(p, redircause, "UNKNOWN");
12400 } else if (!strcmp(reason, "do-not-disturb")) {
12401 ast_string_field_set(p, redircause, "UNKNOWN");
12402 } else if (!strcmp(reason, "deflection")) {
12403 ast_string_field_set(p, redircause, "UNKNOWN");
12404 } else if (!strcmp(reason, "follow-me")) {
12405 ast_string_field_set(p, redircause, "UNKNOWN");
12406 } else if (!strcmp(reason, "out-of-service")) {
12407 ast_string_field_set(p, redircause, "UNREACHABLE");
12408 } else if (!strcmp(reason, "away")) {
12409 ast_string_field_set(p, redircause, "UNREACHABLE");
12410 } else {
12411 ast_string_field_set(p, redircause, "UNKNOWN");
12412 }
12413 }
12414
12415
12416 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
12417 {
12418 char tmp[256], *exten, *rexten, *rdomain;
12419 char *params, *reason = NULL;
12420 struct sip_request *req;
12421
12422 req = oreq ? oreq : &p->initreq;
12423
12424 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
12425 if (ast_strlen_zero(tmp))
12426 return 0;
12427
12428 params = strchr(tmp, ';');
12429
12430 exten = get_in_brackets(tmp);
12431 if (!strncasecmp(exten, "sip:", 4)) {
12432 exten += 4;
12433 } else if (!strncasecmp(exten, "sips:", 5)) {
12434 exten += 5;
12435 } else {
12436 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", exten);
12437 return -1;
12438 }
12439
12440
12441 if (params) {
12442 *params = '\0';
12443 params++;
12444 while (*params == ';' || *params == ' ')
12445 params++;
12446
12447 if ((reason = strcasestr(params, "reason="))) {
12448 reason+=7;
12449
12450 if (*reason == '"')
12451 ast_strip_quoted(reason, "\"", "\"");
12452 if (!ast_strlen_zero(reason)) {
12453 sip_set_redirstr(p, reason);
12454 if (p->owner) {
12455 pbx_builtin_setvar_helper(p->owner, "__PRIREDIRECTREASON", p->redircause);
12456 pbx_builtin_setvar_helper(p->owner, "__SIPREDIRECTREASON", reason);
12457 }
12458 }
12459 }
12460 }
12461
12462 rdomain = exten;
12463 rexten = strsep(&rdomain, "@");
12464 if (p->owner)
12465 pbx_builtin_setvar_helper(p->owner, "__SIPRDNISDOMAIN", rdomain);
12466
12467 if (sip_debug_test_pvt(p))
12468 ast_verbose("RDNIS for this call is is %s (reason %s)\n", exten, reason ? reason : "");
12469
12470 ast_string_field_set(p, rdnis, rexten);
12471
12472 return 0;
12473 }
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
12484 {
12485 char tmp[256] = "", *uri, *a;
12486 char tmpf[256] = "", *from = NULL;
12487 struct sip_request *req;
12488 char *colon;
12489 char *decoded_uri;
12490
12491 req = oreq;
12492 if (!req)
12493 req = &p->initreq;
12494
12495
12496 if (req->rlPart2)
12497 ast_copy_string(tmp, REQ_OFFSET_TO_STR(req, rlPart2), sizeof(tmp));
12498
12499 if (pedanticsipchecking)
12500 ast_uri_decode(tmp);
12501
12502 uri = get_in_brackets(tmp);
12503
12504 if (!strncasecmp(uri, "sip:", 4)) {
12505 uri += 4;
12506 } else if (!strncasecmp(uri, "sips:", 5)) {
12507 uri += 5;
12508 } else {
12509 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
12510 return -1;
12511 }
12512
12513
12514
12515
12516
12517 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
12518 if (!ast_strlen_zero(tmpf)) {
12519 if (pedanticsipchecking)
12520 ast_uri_decode(tmpf);
12521 from = get_in_brackets(tmpf);
12522 }
12523
12524 if (!ast_strlen_zero(from)) {
12525 if (!strncasecmp(from, "sip:", 4)) {
12526 from += 4;
12527 } else if (!strncasecmp(from, "sips:", 5)) {
12528 from += 5;
12529 } else {
12530 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
12531 return -1;
12532 }
12533 if ((a = strchr(from, '@')))
12534 *a++ = '\0';
12535 else
12536 a = from;
12537 from = strsep(&from, ";");
12538 a = strsep(&a, ";");
12539 ast_string_field_set(p, fromdomain, a);
12540 }
12541
12542
12543
12544
12545 if ((a = strchr(uri, '@'))) {
12546 *a++ = '\0';
12547 } else {
12548 a = uri;
12549 uri = "s";
12550 }
12551 colon = strchr(a, ':');
12552 if (colon)
12553 *colon = '\0';
12554
12555 uri = strsep(&uri, ";");
12556 a = strsep(&a, ";");
12557
12558 ast_string_field_set(p, domain, a);
12559
12560 if (!AST_LIST_EMPTY(&domain_list)) {
12561 char domain_context[AST_MAX_EXTENSION];
12562
12563 domain_context[0] = '\0';
12564 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
12565 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
12566 ast_debug(1, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
12567 return -2;
12568 }
12569 }
12570
12571
12572 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_HAVEPEERCONTEXT) && !ast_strlen_zero(domain_context))
12573 ast_string_field_set(p, context, domain_context);
12574 }
12575
12576
12577 if (req->method == SIP_SUBSCRIBE && !ast_strlen_zero(p->subscribecontext))
12578 ast_string_field_set(p, context, p->subscribecontext);
12579
12580 if (sip_debug_test_pvt(p))
12581 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
12582
12583
12584 if (req->method == SIP_SUBSCRIBE) {
12585 char hint[AST_MAX_EXTENSION];
12586 return (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten) ? 0 : -1);
12587 } else {
12588 decoded_uri = ast_strdupa(uri);
12589 ast_uri_decode(decoded_uri);
12590
12591
12592
12593
12594
12595 if (ast_exists_extension(NULL, p->context, uri, 1, S_OR(p->cid_num, from)) || ast_exists_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from)) ||
12596 !strcmp(decoded_uri, ast_pickup_ext())) {
12597 if (!oreq)
12598 ast_string_field_set(p, exten, decoded_uri);
12599 return 0;
12600 }
12601 }
12602
12603
12604 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
12605 ast_canmatch_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from))) ||
12606 !strncmp(decoded_uri, ast_pickup_ext(), strlen(decoded_uri))) {
12607 return 1;
12608 }
12609
12610 return -1;
12611 }
12612
12613
12614
12615
12616 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
12617 {
12618 struct sip_pvt *sip_pvt_ptr;
12619 struct sip_pvt tmp_dialog = {
12620 .callid = callid,
12621 };
12622
12623 if (totag)
12624 ast_debug(4, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
12625
12626
12627
12628 sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find of dialog in dialogs table");
12629 if (sip_pvt_ptr) {
12630
12631 sip_pvt_lock(sip_pvt_ptr);
12632 if (pedanticsipchecking) {
12633 unsigned char frommismatch = 0, tomismatch = 0;
12634
12635 if (ast_strlen_zero(fromtag)) {
12636 sip_pvt_unlock(sip_pvt_ptr);
12637 ast_debug(4, "Matched %s call for callid=%s - no from tag specified, pedantic check fails\n",
12638 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid);
12639 return NULL;
12640 }
12641
12642 if (ast_strlen_zero(totag)) {
12643 sip_pvt_unlock(sip_pvt_ptr);
12644 ast_debug(4, "Matched %s call for callid=%s - no to tag specified, pedantic check fails\n",
12645 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid);
12646 return NULL;
12647 }
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662 frommismatch = !!strcmp(fromtag, sip_pvt_ptr->theirtag);
12663 tomismatch = !!strcmp(totag, sip_pvt_ptr->tag);
12664 if (frommismatch || tomismatch) {
12665 sip_pvt_unlock(sip_pvt_ptr);
12666 if (frommismatch) {
12667 ast_debug(4, "Matched %s call for callid=%s - But the pedantic check rejected the match; their tag is %s Our tag is %s\n",
12668 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid,
12669 fromtag, sip_pvt_ptr->theirtag);
12670 }
12671 if (tomismatch) {
12672 ast_debug(4, "Matched %s call for callid=%s - pedantic to tag check fails; their tag is %s our tag is %s\n",
12673 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid,
12674 totag, sip_pvt_ptr->tag);
12675 }
12676 return NULL;
12677 }
12678 }
12679
12680 if (totag)
12681 ast_debug(4, "Matched %s call - their tag is %s Our tag is %s\n",
12682 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING",
12683 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
12684
12685
12686 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
12687 sip_pvt_unlock(sip_pvt_ptr);
12688 usleep(1);
12689 sip_pvt_lock(sip_pvt_ptr);
12690 }
12691 }
12692
12693 return sip_pvt_ptr;
12694 }
12695
12696
12697
12698 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
12699 {
12700
12701 const char *p_referred_by = NULL;
12702 char *h_refer_to = NULL;
12703 char *h_referred_by = NULL;
12704 char *refer_to;
12705 const char *p_refer_to;
12706 char *referred_by_uri = NULL;
12707 char *ptr;
12708 struct sip_request *req = NULL;
12709 const char *transfer_context = NULL;
12710 struct sip_refer *referdata;
12711
12712
12713 req = outgoing_req;
12714 referdata = transferer->refer;
12715
12716 if (!req)
12717 req = &transferer->initreq;
12718
12719 p_refer_to = get_header(req, "Refer-To");
12720 if (ast_strlen_zero(p_refer_to)) {
12721 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
12722 return -2;
12723 }
12724 h_refer_to = ast_strdupa(p_refer_to);
12725 refer_to = get_in_brackets(h_refer_to);
12726 if (pedanticsipchecking)
12727 ast_uri_decode(refer_to);
12728
12729 if (!strncasecmp(refer_to, "sip:", 4)) {
12730 refer_to += 4;
12731 } else if (!strncasecmp(refer_to, "sips:", 5)) {
12732 refer_to += 5;
12733 } else {
12734 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
12735 return -3;
12736 }
12737
12738
12739 p_referred_by = get_header(req, "Referred-By");
12740
12741
12742 if (transferer->owner) {
12743 struct ast_channel *peer = ast_bridged_channel(transferer->owner);
12744 if (peer) {
12745 pbx_builtin_setvar_helper(peer, "SIPREFERRINGCONTEXT", transferer->context);
12746 pbx_builtin_setvar_helper(peer, "SIPREFERREDBYHDR", p_referred_by);
12747 }
12748 }
12749
12750 if (!ast_strlen_zero(p_referred_by)) {
12751 char *lessthan;
12752 h_referred_by = ast_strdupa(p_referred_by);
12753 if (pedanticsipchecking)
12754 ast_uri_decode(h_referred_by);
12755
12756
12757 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
12758 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
12759 *(lessthan - 1) = '\0';
12760 }
12761
12762 referred_by_uri = get_in_brackets(h_referred_by);
12763 if (!strncasecmp(referred_by_uri, "sip:", 4)) {
12764 referred_by_uri += 4;
12765 } else if (!strncasecmp(referred_by_uri, "sips:", 5)) {
12766 referred_by_uri += 5;
12767 } else {
12768 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
12769 referred_by_uri = NULL;
12770 }
12771 }
12772
12773
12774 if ((ptr = strcasestr(refer_to, "replaces="))) {
12775 char *to = NULL, *from = NULL;
12776
12777
12778 referdata->attendedtransfer = 1;
12779 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
12780 ast_uri_decode(referdata->replaces_callid);
12781 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
12782 *ptr++ = '\0';
12783 }
12784
12785 if (ptr) {
12786
12787 to = strcasestr(ptr, "to-tag=");
12788 from = strcasestr(ptr, "from-tag=");
12789 }
12790
12791
12792 if (to) {
12793 ptr = to + 7;
12794 if ((to = strchr(ptr, '&')))
12795 *to = '\0';
12796 if ((to = strchr(ptr, ';')))
12797 *to = '\0';
12798 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
12799 }
12800
12801 if (from) {
12802 ptr = from + 9;
12803 if ((to = strchr(ptr, '&')))
12804 *to = '\0';
12805 if ((to = strchr(ptr, ';')))
12806 *to = '\0';
12807 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
12808 }
12809
12810 if (!pedanticsipchecking)
12811 ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
12812 else
12813 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>" );
12814 }
12815
12816 if ((ptr = strchr(refer_to, '@'))) {
12817 char *urioption = NULL, *domain;
12818 *ptr++ = '\0';
12819
12820 if ((urioption = strchr(ptr, ';')))
12821 *urioption++ = '\0';
12822
12823 domain = ptr;
12824 if ((ptr = strchr(domain, ':')))
12825 *ptr = '\0';
12826
12827
12828 ast_copy_string(referdata->refer_to_domain, domain, sizeof(referdata->refer_to_domain));
12829 if (urioption)
12830 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
12831 }
12832
12833 if ((ptr = strchr(refer_to, ';')))
12834 *ptr = '\0';
12835 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
12836
12837 if (referred_by_uri) {
12838 if ((ptr = strchr(referred_by_uri, ';')))
12839 *ptr = '\0';
12840 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
12841 } else {
12842 referdata->referred_by[0] = '\0';
12843 }
12844
12845
12846 if (transferer->owner)
12847 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
12848
12849
12850 if (ast_strlen_zero(transfer_context)) {
12851 transfer_context = S_OR(transferer->owner->macrocontext,
12852 S_OR(transferer->context, default_context));
12853 }
12854
12855 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
12856
12857
12858 if (referdata->attendedtransfer || ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
12859 if (sip_debug_test_pvt(transferer)) {
12860 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
12861 }
12862
12863 return 0;
12864 }
12865 if (sip_debug_test_pvt(transferer))
12866 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
12867
12868
12869 return -1;
12870 }
12871
12872
12873
12874 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
12875 {
12876 char tmp[256] = "", *c, *a;
12877 struct sip_request *req = oreq ? oreq : &p->initreq;
12878 struct sip_refer *referdata = NULL;
12879 const char *transfer_context = NULL;
12880
12881 if (!p->refer && !sip_refer_allocate(p))
12882 return -1;
12883
12884 referdata = p->refer;
12885
12886 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
12887 c = get_in_brackets(tmp);
12888
12889 if (pedanticsipchecking)
12890 ast_uri_decode(c);
12891
12892 if (!strncasecmp(c, "sip:", 4)) {
12893 c += 4;
12894 } else if (!strncasecmp(c, "sips:", 5)) {
12895 c += 5;
12896 } else {
12897 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
12898 return -1;
12899 }
12900
12901 if ((a = strchr(c, ';')))
12902 *a = '\0';
12903
12904 if ((a = strchr(c, '@'))) {
12905 *a++ = '\0';
12906 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
12907 }
12908
12909 if (sip_debug_test_pvt(p))
12910 ast_verbose("Looking for %s in %s\n", c, p->context);
12911
12912 if (p->owner)
12913 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
12914
12915
12916 if (ast_strlen_zero(transfer_context)) {
12917 transfer_context = S_OR(p->owner->macrocontext,
12918 S_OR(p->context, default_context));
12919 }
12920 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
12921
12922 ast_debug(1, "SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
12923 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
12924 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
12925 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
12926 referdata->refer_call = dialog_unref(referdata->refer_call, "unreffing referdata->refer_call");
12927
12928 ast_string_field_set(p, context, transfer_context);
12929 return 0;
12930 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
12931 return 1;
12932 }
12933
12934 return -1;
12935 }
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947 static attribute_unused void check_via_response(struct sip_pvt *p, struct sip_request *req)
12948 {
12949 char via[256];
12950 char *cur, *opts;
12951
12952 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
12953
12954
12955 opts = strchr(via, ',');
12956 if (opts)
12957 *opts = '\0';
12958
12959
12960 opts = strchr(via, ';');
12961 if (!opts)
12962 return;
12963 *opts++ = '\0';
12964 while ( (cur = strsep(&opts, ";")) ) {
12965 if (!strncmp(cur, "rport=", 6)) {
12966 int port = strtol(cur+6, NULL, 10);
12967
12968 p->ourip.sin_port = ntohs(port);
12969 } else if (!strncmp(cur, "received=", 9)) {
12970 if (ast_parse_arg(cur+9, PARSE_INADDR, &p->ourip))
12971 ;
12972 }
12973 }
12974 }
12975
12976
12977 static void check_via(struct sip_pvt *p, struct sip_request *req)
12978 {
12979 char via[512];
12980 char *c, *pt, *maddr;
12981 struct hostent *hp;
12982 struct ast_hostent ahp;
12983
12984 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
12985
12986
12987 c = strchr(via, ',');
12988 if (c)
12989 *c = '\0';
12990
12991
12992 c = strstr(via, ";rport");
12993 if (c && (c[6] != '='))
12994 ast_set_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT);
12995
12996
12997 maddr = strstr(via, "maddr=");
12998 if (maddr) {
12999 maddr += 6;
13000 c = maddr + strspn(maddr, "0123456789.");
13001 *c = '\0';
13002 }
13003
13004 c = strchr(via, ';');
13005 if (c)
13006 *c = '\0';
13007
13008 c = strchr(via, ' ');
13009 if (c) {
13010 *c = '\0';
13011 c = ast_skip_blanks(c+1);
13012 if (strcasecmp(via, "SIP/2.0/UDP") && strcasecmp(via, "SIP/2.0/TCP") && strcasecmp(via, "SIP/2.0/TLS")) {
13013 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
13014 return;
13015 }
13016 pt = strchr(c, ':');
13017 if (pt)
13018 *pt++ = '\0';
13019
13020 if (maddr)
13021 c = maddr;
13022 hp = ast_gethostbyname(c, &ahp);
13023 if (!hp) {
13024 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
13025 return;
13026 }
13027 memset(&p->sa, 0, sizeof(p->sa));
13028 p->sa.sin_family = AF_INET;
13029 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
13030 p->sa.sin_port = htons(port_str2int(pt, STANDARD_SIP_PORT));
13031
13032 if (sip_debug_test_pvt(p)) {
13033 const struct sockaddr_in *dst = sip_real_dst(p);
13034 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
13035 }
13036 }
13037 }
13038
13039
13040 static char *get_calleridname(const char *input, char *output, size_t outputsize)
13041 {
13042 const char *end = strchr(input, '<');
13043 const char *tmp = strchr(input, '"');
13044 int bytes = 0;
13045 int maxbytes = outputsize - 1;
13046
13047 if (!end || end == input)
13048 return NULL;
13049
13050 end--;
13051
13052 if (tmp && tmp <= end) {
13053
13054
13055
13056 end = strchr(tmp+1, '"');
13057 if (!end)
13058 return NULL;
13059 bytes = (int) (end - tmp);
13060
13061 if (bytes > maxbytes)
13062 bytes = maxbytes;
13063 ast_copy_string(output, tmp + 1, bytes);
13064 } else {
13065
13066
13067 input = ast_skip_blanks(input);
13068
13069 while(*end && *end < 33 && end > input)
13070 end--;
13071 if (end >= input) {
13072 bytes = (int) (end - input) + 2;
13073
13074 if (bytes > maxbytes)
13075 bytes = maxbytes;
13076 ast_copy_string(output, input, bytes);
13077 } else
13078 return NULL;
13079 }
13080 return output;
13081 }
13082
13083
13084
13085
13086
13087 static int get_rpid_num(const char *input, char *output, int maxlen)
13088 {
13089 char *start;
13090 char *end;
13091
13092 start = strchr(input, ':');
13093 if (!start) {
13094 output[0] = '\0';
13095 return 0;
13096 }
13097 start++;
13098
13099
13100 ast_copy_string(output, start, maxlen);
13101 output[maxlen-1] = '\0';
13102
13103 end = strchr(output, '@');
13104 if (end)
13105 *end = '\0';
13106 else
13107 output[0] = '\0';
13108 if (strstr(input, "privacy=full") || strstr(input, "privacy=uri"))
13109 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
13110
13111 return 0;
13112 }
13113
13114
13115
13116
13117 static struct ast_variable *copy_vars(struct ast_variable *src)
13118 {
13119 struct ast_variable *res = NULL, *tmp, *v = NULL;
13120
13121 for (v = src ; v ; v = v->next) {
13122 if ((tmp = ast_variable_new(v->name, v->value, v->file))) {
13123 tmp->next = res;
13124 res = tmp;
13125 }
13126 }
13127 return res;
13128 }
13129
13130
13131 static void replace_cid(struct sip_pvt *p, const char *rpid_num, const char *calleridname)
13132 {
13133
13134 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
13135 char *tmp = ast_strdupa(rpid_num);
13136 if (!ast_strlen_zero(calleridname))
13137 ast_string_field_set(p, cid_name, calleridname);
13138 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
13139 ast_shrink_phone_number(tmp);
13140 ast_string_field_set(p, cid_num, tmp);
13141 }
13142 }
13143
13144
13145 static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
13146 struct sip_request *req, int sipmethod, struct sockaddr_in *sin,
13147 struct sip_peer **authpeer,
13148 enum xmittype reliable,
13149 char *rpid_num, char *calleridname, char *uri2)
13150 {
13151 enum check_auth_result res;
13152 int debug=sip_debug_test_addr(sin);
13153 struct sip_peer *peer;
13154
13155 if (sipmethod == SIP_SUBSCRIBE) {
13156
13157
13158
13159 peer = find_peer(of, NULL, TRUE, FINDALLDEVICES, FALSE, 0);
13160 } else {
13161
13162 peer = find_peer(of, NULL, TRUE, FINDUSERS, FALSE, 0);
13163
13164
13165 if (!peer) {
13166 peer = find_peer(NULL, &p->recv, TRUE, FINDPEERS, FALSE, p->socket.type);
13167 }
13168 }
13169
13170 if (!peer) {
13171 if (debug)
13172 ast_verbose("No matching peer for '%s' from '%s:%d'\n",
13173 of, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
13174 return AUTH_DONT_KNOW;
13175 }
13176 if (!ast_apply_ha(peer->ha, sin)) {
13177 ast_debug(2, "Found peer '%s' for '%s', but fails host access\n", peer->name, of);
13178 unref_peer(peer, "unref_peer: check_peer_ok: from find_peer call, early return of AUTH_ACL_FAILED");
13179 return AUTH_ACL_FAILED;
13180 }
13181 if (debug)
13182 ast_verbose("Found peer '%s' for '%s' from %s:%d\n",
13183 peer->name, of, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
13184
13185
13186
13187 if (p->rtp) {
13188 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
13189 p->autoframing = peer->autoframing;
13190 }
13191
13192
13193 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
13194 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
13195
13196 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && p->udptl) {
13197 p->t38_maxdatagram = peer->t38_maxdatagram;
13198 set_t38_capabilities(p);
13199 }
13200
13201
13202
13203 if (p->sipoptions)
13204 peer->sipoptions = p->sipoptions;
13205
13206 replace_cid(p, rpid_num, calleridname);
13207 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
13208
13209 ast_string_field_set(p, peersecret, peer->secret);
13210 ast_string_field_set(p, peermd5secret, peer->md5secret);
13211 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
13212 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
13213 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
13214 ast_string_field_set(p, parkinglot, peer->parkinglot);
13215 if (peer->callingpres)
13216 p->callingpres = peer->callingpres;
13217 if (peer->maxms && peer->lastms)
13218 p->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
13219 else
13220 p->timer_t1 = peer->timer_t1;
13221
13222
13223 if (peer->timer_b)
13224 p->timer_b = peer->timer_b;
13225 else
13226 p->timer_b = 64 * p->timer_t1;
13227
13228 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
13229
13230 ast_string_field_set(p, peersecret, NULL);
13231 ast_string_field_set(p, peermd5secret, NULL);
13232 }
13233 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, req->ignore))) {
13234 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
13235 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
13236
13237 if (peer->call_limit)
13238 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
13239 ast_string_field_set(p, peername, peer->name);
13240 ast_string_field_set(p, authname, peer->name);
13241
13242 if (sipmethod == SIP_INVITE) {
13243
13244 p->chanvars = copy_vars(peer->chanvars);
13245 }
13246
13247 if (authpeer) {
13248 ao2_t_ref(peer, 1, "copy pointer into (*authpeer)");
13249 (*authpeer) = peer;
13250 }
13251
13252 if (!ast_strlen_zero(peer->username)) {
13253 ast_string_field_set(p, username, peer->username);
13254
13255
13256 ast_string_field_set(p, authname, peer->username);
13257 }
13258 if (!ast_strlen_zero(peer->cid_num)) {
13259 char *tmp = ast_strdupa(peer->cid_num);
13260 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
13261 ast_shrink_phone_number(tmp);
13262 ast_string_field_set(p, cid_num, tmp);
13263 }
13264 if (!ast_strlen_zero(peer->cid_name))
13265 ast_string_field_set(p, cid_name, peer->cid_name);
13266 ast_string_field_set(p, fullcontact, peer->fullcontact);
13267 if (!ast_strlen_zero(peer->context))
13268 ast_string_field_set(p, context, peer->context);
13269 ast_string_field_set(p, peersecret, peer->secret);
13270 ast_string_field_set(p, peermd5secret, peer->md5secret);
13271 ast_string_field_set(p, language, peer->language);
13272 ast_string_field_set(p, accountcode, peer->accountcode);
13273 p->amaflags = peer->amaflags;
13274 p->callgroup = peer->callgroup;
13275 p->pickupgroup = peer->pickupgroup;
13276 p->capability = peer->capability;
13277 p->prefs = peer->prefs;
13278 p->jointcapability = peer->capability;
13279 if (p->peercapability)
13280 p->jointcapability &= p->peercapability;
13281 p->maxcallbitrate = peer->maxcallbitrate;
13282 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS) &&
13283 (!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ||
13284 !(p->capability & AST_FORMAT_VIDEO_MASK)) &&
13285 p->vrtp) {
13286 ast_rtp_destroy(p->vrtp);
13287 p->vrtp = NULL;
13288 }
13289 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT) || !(p->capability & AST_FORMAT_TEXT_MASK)) && p->trtp) {
13290 ast_rtp_destroy(p->trtp);
13291 p->trtp = NULL;
13292 }
13293 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
13294 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
13295 p->noncodeccapability |= AST_RTP_DTMF;
13296 else
13297 p->noncodeccapability &= ~AST_RTP_DTMF;
13298 p->jointnoncodeccapability = p->noncodeccapability;
13299 }
13300 unref_peer(peer, "check_peer_ok: unref_peer: tossing temp ptr to peer from find_peer");
13301 return res;
13302 }
13303
13304
13305
13306
13307
13308
13309
13310 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
13311 int sipmethod, char *uri, enum xmittype reliable,
13312 struct sockaddr_in *sin, struct sip_peer **authpeer)
13313 {
13314 char from[256];
13315 char *dummy;
13316 char *domain;
13317 char *of;
13318 char rpid_num[50];
13319 const char *rpid;
13320 enum check_auth_result res;
13321 char calleridname[50];
13322 char *uri2 = ast_strdupa(uri);
13323
13324 terminate_uri(uri2);
13325
13326 ast_copy_string(from, get_header(req, "From"), sizeof(from));
13327 if (pedanticsipchecking)
13328 ast_uri_decode(from);
13329
13330 memset(calleridname, 0, sizeof(calleridname));
13331 get_calleridname(from, calleridname, sizeof(calleridname));
13332 if (calleridname[0])
13333 ast_string_field_set(p, cid_name, calleridname);
13334
13335 rpid = get_header(req, "Remote-Party-ID");
13336 memset(rpid_num, 0, sizeof(rpid_num));
13337 if (!ast_strlen_zero(rpid))
13338 p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
13339
13340 of = get_in_brackets(from);
13341 if (ast_strlen_zero(p->exten)) {
13342 char *t = uri2;
13343 if (!strncasecmp(t, "sip:", 4))
13344 t+= 4;
13345 else if (!strncasecmp(t, "sips:", 5))
13346 t += 5;
13347 ast_string_field_set(p, exten, t);
13348 t = strchr(p->exten, '@');
13349 if (t)
13350 *t = '\0';
13351 if (ast_strlen_zero(p->our_contact))
13352 build_contact(p);
13353 }
13354
13355 ast_string_field_set(p, from, of);
13356
13357
13358
13359 if (parse_uri(of, "sip:,sips:", &of, &dummy, &domain, &dummy, &dummy, NULL)) {
13360 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
13361 }
13362
13363 if (ast_strlen_zero(of)) {
13364
13365
13366
13367
13368
13369
13370 of = domain;
13371 } else {
13372 char *tmp = ast_strdupa(of);
13373
13374
13375
13376 tmp = strsep(&tmp, ";");
13377 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
13378 ast_shrink_phone_number(tmp);
13379 ast_string_field_set(p, cid_num, tmp);
13380 }
13381
13382 if (global_match_auth_username) {
13383
13384
13385
13386
13387
13388
13389
13390
13391 const char *hdr = get_header(req, "Authorization");
13392 if (ast_strlen_zero(hdr))
13393 hdr = get_header(req, "Proxy-Authorization");
13394
13395 if ( !ast_strlen_zero(hdr) && (hdr = strstr(hdr, "username=\"")) ) {
13396 ast_copy_string(from, hdr + strlen("username=\""), sizeof(from));
13397 of = from;
13398 of = strsep(&of, "\"");
13399 }
13400 }
13401
13402 res = check_peer_ok(p, of, req, sipmethod, sin,
13403 authpeer, reliable, rpid_num, calleridname, uri2);
13404 if (res != AUTH_DONT_KNOW)
13405 return res;
13406
13407
13408 if (global_allowguest) {
13409 replace_cid(p, rpid_num, calleridname);
13410 res = AUTH_SUCCESSFUL;
13411 } else if (global_alwaysauthreject)
13412 res = AUTH_FAKE_AUTH;
13413 else
13414 res = AUTH_SECRET_FAILED;
13415
13416
13417 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT)) {
13418 ast_set_flag(&p->flags[0], SIP_NAT_ROUTE);
13419 }
13420
13421 return res;
13422 }
13423
13424
13425
13426
13427 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin)
13428 {
13429 return check_user_full(p, req, sipmethod, uri, reliable, sin, NULL);
13430 }
13431
13432
13433 static int get_msg_text(char *buf, int len, struct sip_request *req, int addnewline)
13434 {
13435 int x;
13436 int y;
13437
13438 buf[0] = '\0';
13439
13440 y = len - strlen(buf) - 5;
13441 if (y < 0)
13442 y = 0;
13443 for (x = 0; x < req->lines; x++) {
13444 char *line = REQ_OFFSET_TO_STR(req, line[x]);
13445 strncat(buf, line, y);
13446 y -= strlen(line) + 1;
13447 if (y < 0)
13448 y = 0;
13449 if (y != 0 && addnewline)
13450 strcat(buf, "\n");
13451 }
13452 return 0;
13453 }
13454
13455
13456
13457
13458
13459 static void receive_message(struct sip_pvt *p, struct sip_request *req)
13460 {
13461 char buf[1400];
13462 struct ast_frame f;
13463 const char *content_type = get_header(req, "Content-Type");
13464
13465 if (strncmp(content_type, "text/plain", strlen("text/plain"))) {
13466 transmit_response(p, "415 Unsupported Media Type", req);
13467 if (!p->owner)
13468 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13469 return;
13470 }
13471
13472 if (get_msg_text(buf, sizeof(buf), req, FALSE)) {
13473 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
13474 transmit_response(p, "202 Accepted", req);
13475 if (!p->owner)
13476 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13477 return;
13478 }
13479
13480 if (p->owner) {
13481 if (sip_debug_test_pvt(p))
13482 ast_verbose("SIP Text message received: '%s'\n", buf);
13483 memset(&f, 0, sizeof(f));
13484 f.frametype = AST_FRAME_TEXT;
13485 f.subclass = 0;
13486 f.offset = 0;
13487 f.data.ptr = buf;
13488 f.datalen = strlen(buf);
13489 ast_queue_frame(p->owner, &f);
13490 transmit_response(p, "202 Accepted", req);
13491 return;
13492 }
13493
13494
13495 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);
13496 transmit_response(p, "405 Method Not Allowed", req);
13497 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13498 return;
13499 }
13500
13501
13502 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13503 {
13504 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
13505 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
13506 char ilimits[40];
13507 char iused[40];
13508 int showall = FALSE;
13509 struct ao2_iterator i;
13510 struct sip_peer *peer;
13511
13512 switch (cmd) {
13513 case CLI_INIT:
13514 e->command = "sip show inuse";
13515 e->usage =
13516 "Usage: sip show inuse [all]\n"
13517 " List all SIP devices usage counters and limits.\n"
13518 " Add option \"all\" to show all devices, not only those with a limit.\n";
13519 return NULL;
13520 case CLI_GENERATE:
13521 return NULL;
13522 }
13523
13524 if (a->argc < 3)
13525 return CLI_SHOWUSAGE;
13526
13527 if (a->argc == 4 && !strcmp(a->argv[3], "all"))
13528 showall = TRUE;
13529
13530 ast_cli(a->fd, FORMAT, "* Peer name", "In use", "Limit");
13531
13532 i = ao2_iterator_init(peers, 0);
13533 while ((peer = ao2_t_iterator_next(&i, "iterate thru peer table"))) {
13534 ao2_lock(peer);
13535 if (peer->call_limit)
13536 snprintf(ilimits, sizeof(ilimits), "%d", peer->call_limit);
13537 else
13538 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
13539 snprintf(iused, sizeof(iused), "%d/%d/%d", peer->inUse, peer->inRinging, peer->onHold);
13540 if (showall || peer->call_limit)
13541 ast_cli(a->fd, FORMAT2, peer->name, iused, ilimits);
13542 ao2_unlock(peer);
13543 unref_peer(peer, "toss iterator pointer");
13544 }
13545 ao2_iterator_destroy(&i);
13546
13547 return CLI_SUCCESS;
13548 #undef FORMAT
13549 #undef FORMAT2
13550 }
13551
13552
13553
13554 static char *transfermode2str(enum transfermodes mode)
13555 {
13556 if (mode == TRANSFER_OPENFORALL)
13557 return "open";
13558 else if (mode == TRANSFER_CLOSED)
13559 return "closed";
13560 return "strict";
13561 }
13562
13563 static struct _map_x_s natmodes[] = {
13564 { SIP_NAT_NEVER, "No"},
13565 { SIP_NAT_ROUTE, "Route"},
13566 { SIP_NAT_ALWAYS, "Always"},
13567 { SIP_NAT_RFC3581, "RFC3581"},
13568 { -1, NULL},
13569 };
13570
13571
13572 static const char *nat2str(int nat)
13573 {
13574 return map_x_s(natmodes, nat, "Unknown");
13575 }
13576
13577 #ifdef NOTUSED
13578
13579
13580
13581
13582 static struct _map_x_s natcfgmodes[] = {
13583 { SIP_NAT_NEVER, "never"},
13584 { SIP_NAT_ROUTE, "route"},
13585 { SIP_NAT_ALWAYS, "yes"},
13586 { SIP_NAT_RFC3581, "no"},
13587 { -1, NULL},
13588 };
13589
13590
13591 static const char *nat2strconfig(int nat)
13592 {
13593 return map_x_s(natcfgmodes, nat, "Unknown");
13594 }
13595 #endif
13596
13597
13598
13599
13600
13601
13602
13603 static struct _map_x_s stmodes[] = {
13604 { SESSION_TIMER_MODE_ACCEPT, "Accept"},
13605 { SESSION_TIMER_MODE_ORIGINATE, "Originate"},
13606 { SESSION_TIMER_MODE_REFUSE, "Refuse"},
13607 { -1, NULL},
13608 };
13609
13610 static const char *stmode2str(enum st_mode m)
13611 {
13612 return map_x_s(stmodes, m, "Unknown");
13613 }
13614
13615 static enum st_mode str2stmode(const char *s)
13616 {
13617 return map_s_x(stmodes, s, -1);
13618 }
13619
13620
13621 static struct _map_x_s strefreshers[] = {
13622 { SESSION_TIMER_REFRESHER_AUTO, "auto"},
13623 { SESSION_TIMER_REFRESHER_UAC, "uac"},
13624 { SESSION_TIMER_REFRESHER_UAS, "uas"},
13625 { -1, NULL},
13626 };
13627
13628 static const char *strefresher2str(enum st_refresher r)
13629 {
13630 return map_x_s(strefreshers, r, "Unknown");
13631 }
13632
13633 static enum st_refresher str2strefresher(const char *s)
13634 {
13635 return map_s_x(strefreshers, s, -1);
13636 }
13637
13638
13639 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
13640 {
13641 int res = 0;
13642 if (peer->maxms) {
13643 if (peer->lastms < 0) {
13644 ast_copy_string(status, "UNREACHABLE", statuslen);
13645 } else if (peer->lastms > peer->maxms) {
13646 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
13647 res = 1;
13648 } else if (peer->lastms) {
13649 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
13650 res = 1;
13651 } else {
13652 ast_copy_string(status, "UNKNOWN", statuslen);
13653 }
13654 } else {
13655 ast_copy_string(status, "Unmonitored", statuslen);
13656
13657 res = -1;
13658 }
13659 return res;
13660 }
13661
13662
13663
13664
13665
13666
13667 static const char *cli_yesno(int x)
13668 {
13669 return x ? "Yes" : "No";
13670 }
13671
13672
13673 static char *sip_show_tcp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13674 {
13675 struct sip_threadinfo *th;
13676 struct ao2_iterator i;
13677
13678 #define FORMAT2 "%-30.30s %3.6s %9.9s %6.6s\n"
13679 #define FORMAT "%-30.30s %-6d %-9.9s %-6.6s\n"
13680
13681 switch (cmd) {
13682 case CLI_INIT:
13683 e->command = "sip show tcp";
13684 e->usage =
13685 "Usage: sip show tcp\n"
13686 " Lists all active TCP/TLS sessions.\n";
13687 return NULL;
13688 case CLI_GENERATE:
13689 return NULL;
13690 }
13691
13692 if (a->argc != 3)
13693 return CLI_SHOWUSAGE;
13694
13695 ast_cli(a->fd, FORMAT2, "Host", "Port", "Transport", "Type");
13696 i = ao2_iterator_init(threadt, 0);
13697 while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
13698 ast_cli(a->fd, FORMAT, ast_inet_ntoa(th->tcptls_session->remote_address.sin_addr),
13699 ntohs(th->tcptls_session->remote_address.sin_port),
13700 get_transport(th->type),
13701 (th->tcptls_session->client ? "Client" : "Server"));
13702 ao2_t_ref(th, -1, "decrement ref from iterator");
13703 }
13704 ao2_iterator_destroy(&i);
13705 return CLI_SUCCESS;
13706 #undef FORMAT
13707 #undef FORMAT2
13708 }
13709
13710
13711 static char *sip_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13712 {
13713 regex_t regexbuf;
13714 int havepattern = FALSE;
13715 struct ao2_iterator user_iter;
13716 struct sip_peer *user;
13717
13718 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
13719
13720 switch (cmd) {
13721 case CLI_INIT:
13722 e->command = "sip show users";
13723 e->usage =
13724 "Usage: sip show users [like <pattern>]\n"
13725 " Lists all known SIP users.\n"
13726 " Optional regular expression pattern is used to filter the user list.\n";
13727 return NULL;
13728 case CLI_GENERATE:
13729 return NULL;
13730 }
13731
13732 switch (a->argc) {
13733 case 5:
13734 if (!strcasecmp(a->argv[3], "like")) {
13735 if (regcomp(®exbuf, a->argv[4], REG_EXTENDED | REG_NOSUB))
13736 return CLI_SHOWUSAGE;
13737 havepattern = TRUE;
13738 } else
13739 return CLI_SHOWUSAGE;
13740 case 3:
13741 break;
13742 default:
13743 return CLI_SHOWUSAGE;
13744 }
13745
13746 ast_cli(a->fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
13747
13748 user_iter = ao2_iterator_init(peers, 0);
13749 while ((user = ao2_iterator_next(&user_iter))) {
13750 ao2_lock(user);
13751 if (!(user->type & SIP_TYPE_USER)) {
13752 ao2_unlock(user);
13753 unref_peer(user, "sip show users");
13754 continue;
13755 }
13756
13757 if (havepattern && regexec(®exbuf, user->name, 0, NULL, 0)) {
13758 ao2_unlock(user);
13759 unref_peer(user, "sip show users");
13760 continue;
13761 }
13762
13763 ast_cli(a->fd, FORMAT, user->name,
13764 user->secret,
13765 user->accountcode,
13766 user->context,
13767 cli_yesno(user->ha != NULL),
13768 nat2str(ast_test_flag(&user->flags[0], SIP_NAT)));
13769 ao2_unlock(user);
13770 unref_peer(user, "sip show users");
13771 }
13772 ao2_iterator_destroy(&user_iter);
13773
13774 if (havepattern)
13775 regfree(®exbuf);
13776
13777 return CLI_SUCCESS;
13778 #undef FORMAT
13779 }
13780
13781
13782 static char mandescr_show_registry[] =
13783 "Description: Lists all registration requests and status\n"
13784 "Registrations will follow as separate events. followed by a final event called\n"
13785 "RegistrationsComplete.\n"
13786 "Variables: \n"
13787 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
13788
13789
13790 static int manager_show_registry(struct mansession *s, const struct message *m)
13791 {
13792 const char *id = astman_get_header(m, "ActionID");
13793 char idtext[256] = "";
13794 int total = 0;
13795
13796 if (!ast_strlen_zero(id))
13797 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
13798
13799 astman_send_listack(s, m, "Registrations will follow", "start");
13800
13801 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
13802 ASTOBJ_RDLOCK(iterator);
13803 astman_append(s,
13804 "Event: RegistryEntry\r\n"
13805 "%s"
13806 "Host: %s\r\n"
13807 "Port: %d\r\n"
13808 "Username: %s\r\n"
13809 "Refresh: %d\r\n"
13810 "State: %s\r\n"
13811 "RegistrationTime: %ld\r\n"
13812 "\r\n", idtext, iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT,
13813 iterator->username, iterator->refresh, regstate2str(iterator->regstate), (long) iterator->regtime.tv_sec);
13814 ASTOBJ_UNLOCK(iterator);
13815 total++;
13816 } while(0));
13817
13818 astman_append(s,
13819 "Event: RegistrationsComplete\r\n"
13820 "EventList: Complete\r\n"
13821 "ListItems: %d\r\n"
13822 "%s"
13823 "\r\n", total, idtext);
13824
13825 return 0;
13826 }
13827
13828 static char mandescr_show_peers[] =
13829 "Description: Lists SIP peers in text format with details on current status.\n"
13830 "Peerlist will follow as separate events, followed by a final event called\n"
13831 "PeerlistComplete.\n"
13832 "Variables: \n"
13833 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
13834
13835
13836
13837 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
13838 {
13839 const char *id = astman_get_header(m, "ActionID");
13840 const char *a[] = {"sip", "show", "peers"};
13841 char idtext[256] = "";
13842 int total = 0;
13843
13844 if (!ast_strlen_zero(id))
13845 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
13846
13847 astman_send_listack(s, m, "Peer status list will follow", "start");
13848
13849 _sip_show_peers(-1, &total, s, m, 3, a);
13850
13851 astman_append(s,
13852 "Event: PeerlistComplete\r\n"
13853 "EventList: Complete\r\n"
13854 "ListItems: %d\r\n"
13855 "%s"
13856 "\r\n", total, idtext);
13857 return 0;
13858 }
13859
13860
13861 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13862 {
13863 switch (cmd) {
13864 case CLI_INIT:
13865 e->command = "sip show peers";
13866 e->usage =
13867 "Usage: sip show peers [like <pattern>]\n"
13868 " Lists all known SIP peers.\n"
13869 " Optional regular expression pattern is used to filter the peer list.\n";
13870 return NULL;
13871 case CLI_GENERATE:
13872 return NULL;
13873 }
13874
13875 return _sip_show_peers(a->fd, NULL, NULL, NULL, a->argc, (const char **) a->argv);
13876 }
13877
13878 int peercomparefunc(const void *a, const void *b);
13879
13880 int peercomparefunc(const void *a, const void *b)
13881 {
13882 struct sip_peer **ap = (struct sip_peer **)a;
13883 struct sip_peer **bp = (struct sip_peer **)b;
13884 return strcmp((*ap)->name, (*bp)->name);
13885 }
13886
13887
13888
13889 static char *_sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
13890 {
13891 regex_t regexbuf;
13892 int havepattern = FALSE;
13893 struct sip_peer *peer;
13894 struct ao2_iterator i;
13895
13896
13897 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s %s\n"
13898 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s %s\n"
13899
13900 char name[256];
13901 int total_peers = 0;
13902 int peers_mon_online = 0;
13903 int peers_mon_offline = 0;
13904 int peers_unmon_offline = 0;
13905 int peers_unmon_online = 0;
13906 const char *id;
13907 char idtext[256] = "";
13908 int realtimepeers;
13909 int objcount = ao2_container_count(peers);
13910 struct sip_peer **peerarray;
13911 int k;
13912
13913
13914 realtimepeers = ast_check_realtime("sippeers");
13915 peerarray = ast_calloc(sizeof(struct sip_peer *), objcount);
13916
13917 if (s) {
13918 id = astman_get_header(m, "ActionID");
13919 if (!ast_strlen_zero(id))
13920 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
13921 }
13922
13923 switch (argc) {
13924 case 5:
13925 if (!strcasecmp(argv[3], "like")) {
13926 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
13927 return CLI_SHOWUSAGE;
13928 havepattern = TRUE;
13929 } else
13930 return CLI_SHOWUSAGE;
13931 case 3:
13932 break;
13933 default:
13934 return CLI_SHOWUSAGE;
13935 }
13936
13937 if (!s)
13938 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
13939
13940
13941 i = ao2_iterator_init(peers, 0);
13942 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
13943 ao2_lock(peer);
13944
13945 if (!(peer->type & SIP_TYPE_PEER)) {
13946 ao2_unlock(peer);
13947 unref_peer(peer, "unref peer because it's actually a user");
13948 continue;
13949 }
13950
13951 if (havepattern && regexec(®exbuf, peer->name, 0, NULL, 0)) {
13952 objcount--;
13953 ao2_unlock(peer);
13954 unref_peer(peer, "toss iterator peer ptr before continue");
13955 continue;
13956 }
13957
13958 peerarray[total_peers++] = peer;
13959 ao2_unlock(peer);
13960 }
13961 ao2_iterator_destroy(&i);
13962
13963 qsort(peerarray, total_peers, sizeof(struct sip_peer *), peercomparefunc);
13964
13965 for(k=0; k < total_peers; k++) {
13966 char status[20] = "";
13967 char srch[2000];
13968 char pstatus;
13969 peer = peerarray[k];
13970
13971 ao2_lock(peer);
13972 if (havepattern && regexec(®exbuf, peer->name, 0, NULL, 0)) {
13973 ao2_unlock(peer);
13974 unref_peer(peer, "toss iterator peer ptr before continue");
13975 continue;
13976 }
13977
13978 if (!ast_strlen_zero(peer->username) && !s)
13979 snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
13980 else
13981 ast_copy_string(name, peer->name, sizeof(name));
13982
13983 pstatus = peer_status(peer, status, sizeof(status));
13984 if (pstatus == 1)
13985 peers_mon_online++;
13986 else if (pstatus == 0)
13987 peers_mon_offline++;
13988 else {
13989 if (peer->addr.sin_port == 0)
13990 peers_unmon_offline++;
13991 else
13992 peers_unmon_online++;
13993 }
13994
13995 snprintf(srch, sizeof(srch), FORMAT, name,
13996 peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
13997 peer->host_dynamic ? " D " : " ",
13998 ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
13999 peer->ha ? " A " : " ",
14000 ntohs(peer->addr.sin_port), status,
14001 realtimepeers ? (peer->is_realtime ? "Cached RT":"") : "");
14002
14003 if (!s) {
14004 ast_cli(fd, FORMAT, name,
14005 peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
14006 peer->host_dynamic ? " D " : " ",
14007 ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
14008 peer->ha ? " A " : " ",
14009
14010 ntohs(peer->addr.sin_port), status,
14011 realtimepeers ? (peer->is_realtime ? "Cached RT":"") : "");
14012 } else {
14013
14014 astman_append(s,
14015 "Event: PeerEntry\r\n%s"
14016 "Channeltype: SIP\r\n"
14017 "ObjectName: %s\r\n"
14018 "ChanObjectType: peer\r\n"
14019 "IPaddress: %s\r\n"
14020 "IPport: %d\r\n"
14021 "Dynamic: %s\r\n"
14022 "Natsupport: %s\r\n"
14023 "VideoSupport: %s\r\n"
14024 "TextSupport: %s\r\n"
14025 "ACL: %s\r\n"
14026 "Status: %s\r\n"
14027 "RealtimeDevice: %s\r\n\r\n",
14028 idtext,
14029 peer->name,
14030 peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "-none-",
14031 ntohs(peer->addr.sin_port),
14032 peer->host_dynamic ? "yes" : "no",
14033 ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE) ? "yes" : "no",
14034 ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
14035 ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT) ? "yes" : "no",
14036 peer->ha ? "yes" : "no",
14037 status,
14038 realtimepeers ? (peer->is_realtime ? "yes":"no") : "no");
14039 }
14040 ao2_unlock(peer);
14041 unref_peer(peer, "toss iterator peer ptr");
14042 }
14043
14044 if (!s)
14045 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
14046 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
14047
14048 if (havepattern)
14049 regfree(®exbuf);
14050
14051 if (total)
14052 *total = total_peers;
14053
14054 ast_free(peerarray);
14055
14056 return CLI_SUCCESS;
14057 #undef FORMAT
14058 #undef FORMAT2
14059 }
14060
14061 static int peer_dump_func(void *userobj, void *arg, int flags)
14062 {
14063 struct sip_peer *peer = userobj;
14064 int refc = ao2_t_ref(userobj, 0, "");
14065 int *fd = arg;
14066
14067 ast_cli(*fd, "name: %s\ntype: peer\nobjflags: %d\nrefcount: %d\n\n",
14068 peer->name, 0, refc);
14069 return 0;
14070 }
14071
14072 static int dialog_dump_func(void *userobj, void *arg, int flags)
14073 {
14074 struct sip_pvt *pvt = userobj;
14075 int refc = ao2_t_ref(userobj, 0, "");
14076 int *fd = arg;
14077
14078 ast_cli(*fd, "name: %s\ntype: dialog\nobjflags: %d\nrefcount: %d\n\n",
14079 pvt->callid, 0, refc);
14080 return 0;
14081 }
14082
14083
14084
14085 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14086 {
14087 char tmp[256];
14088
14089 switch (cmd) {
14090 case CLI_INIT:
14091 e->command = "sip show objects";
14092 e->usage =
14093 "Usage: sip show objects\n"
14094 " Lists status of known SIP objects\n";
14095 return NULL;
14096 case CLI_GENERATE:
14097 return NULL;
14098 }
14099
14100 if (a->argc != 3)
14101 return CLI_SHOWUSAGE;
14102 ast_cli(a->fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
14103 ao2_t_callback(peers, OBJ_NODATA, peer_dump_func, &a->fd, "initiate ao2_callback to dump peers");
14104 ast_cli(a->fd, "-= Registry objects: %d =-\n\n", regobjs);
14105 ASTOBJ_CONTAINER_DUMP(a->fd, tmp, sizeof(tmp), ®l);
14106 ast_cli(a->fd, "-= Dialog objects:\n\n");
14107 ao2_t_callback(dialogs, OBJ_NODATA, dialog_dump_func, &a->fd, "initiate ao2_callback to dump dialogs");
14108 return CLI_SUCCESS;
14109 }
14110
14111 static void print_group(int fd, ast_group_t group, int crlf)
14112 {
14113 char buf[256];
14114 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
14115 }
14116
14117
14118 static struct _map_x_s dtmfstr[] = {
14119 { SIP_DTMF_RFC2833, "rfc2833" },
14120 { SIP_DTMF_INFO, "info" },
14121 { SIP_DTMF_SHORTINFO, "shortinfo" },
14122 { SIP_DTMF_INBAND, "inband" },
14123 { SIP_DTMF_AUTO, "auto" },
14124 { -1, NULL },
14125 };
14126
14127
14128 static const char *dtmfmode2str(int mode)
14129 {
14130 return map_x_s(dtmfstr, mode, "<error>");
14131 }
14132
14133
14134 static int str2dtmfmode(const char *str)
14135 {
14136 return map_s_x(dtmfstr, str, -1);
14137 }
14138
14139 static struct _map_x_s insecurestr[] = {
14140 { SIP_INSECURE_PORT, "port" },
14141 { SIP_INSECURE_INVITE, "invite" },
14142 { SIP_INSECURE_PORT | SIP_INSECURE_INVITE, "port,invite" },
14143 { 0, "no" },
14144 { -1, NULL },
14145 };
14146
14147
14148 static const char *insecure2str(int mode)
14149 {
14150 return map_x_s(insecurestr, mode, "<error>");
14151 }
14152
14153
14154
14155
14156 static void cleanup_stale_contexts(char *new, char *old)
14157 {
14158 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
14159
14160 while ((oldcontext = strsep(&old, "&"))) {
14161 stalecontext = '\0';
14162 ast_copy_string(newlist, new, sizeof(newlist));
14163 stringp = newlist;
14164 while ((newcontext = strsep(&stringp, "&"))) {
14165 if (!strcmp(newcontext, oldcontext)) {
14166
14167 stalecontext = '\0';
14168 break;
14169 } else if (strcmp(newcontext, oldcontext)) {
14170 stalecontext = oldcontext;
14171 }
14172
14173 }
14174 if (stalecontext)
14175 ast_context_destroy(ast_context_find(stalecontext), "SIP");
14176 }
14177 }
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187 static int dialog_needdestroy(void *dialogobj, void *arg, int flags)
14188 {
14189 struct sip_pvt *dialog = dialogobj;
14190 time_t *t = arg;
14191
14192
14193
14194 if (sip_pvt_trylock(dialog)) {
14195
14196
14197
14198
14199
14200
14201 ao2_unlock(dialogs);
14202 usleep(1);
14203 ao2_lock(dialogs);
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214 return 0;
14215 }
14216
14217
14218 check_rtp_timeout(dialog, *t);
14219
14220
14221
14222
14223 if (dialog->needdestroy && !dialog->packets && !dialog->owner) {
14224
14225 if (dialog->rtp && ast_rtp_get_bridged(dialog->rtp)) {
14226 ast_debug(2, "Bridge still active. Delaying destruction of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
14227 sip_pvt_unlock(dialog);
14228 return 0;
14229 }
14230
14231 if (dialog->vrtp && ast_rtp_get_bridged(dialog->vrtp)) {
14232 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
14233 sip_pvt_unlock(dialog);
14234 return 0;
14235 }
14236
14237 sip_pvt_unlock(dialog);
14238
14239
14240 dialog_unlink_all(dialog, TRUE, FALSE);
14241 return 0;
14242 }
14243
14244 sip_pvt_unlock(dialog);
14245
14246 return 0;
14247 }
14248
14249
14250
14251 static int peer_is_marked(void *peerobj, void *arg, int flags)
14252 {
14253 struct sip_peer *peer = peerobj;
14254 return peer->the_mark ? CMP_MATCH : 0;
14255 }
14256
14257
14258
14259 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14260 {
14261 struct sip_peer *peer, *pi;
14262 int prunepeer = FALSE;
14263 int multi = FALSE;
14264 char *name = NULL;
14265 regex_t regexbuf;
14266 struct ao2_iterator i;
14267
14268 if (cmd == CLI_INIT) {
14269 e->command = "sip prune realtime [peer|all] [all|like]";
14270 e->usage =
14271 "Usage: sip prune realtime [peer [<name>|all|like <pattern>]|all]\n"
14272 " Prunes object(s) from the cache.\n"
14273 " Optional regular expression pattern is used to filter the objects.\n";
14274 return NULL;
14275 } else if (cmd == CLI_GENERATE) {
14276 if (a->pos == 4) {
14277 if (strcasestr(a->line, "realtime peer"))
14278 return complete_sip_peer(a->word, a->n, SIP_PAGE2_RTCACHEFRIENDS);
14279 }
14280 return NULL;
14281 }
14282 switch (a->argc) {
14283 case 4:
14284 name = a->argv[3];
14285
14286 if (!strcasecmp(name, "peer") || !strcasecmp(name, "like"))
14287 return CLI_SHOWUSAGE;
14288 prunepeer = TRUE;
14289 if (!strcasecmp(name, "all")) {
14290 multi = TRUE;
14291 name = NULL;
14292 }
14293
14294 break;
14295 case 5:
14296
14297 name = a->argv[4];
14298 if (!strcasecmp(a->argv[3], "peer"))
14299 prunepeer = TRUE;
14300 else if (!strcasecmp(a->argv[3], "like")) {
14301 prunepeer = TRUE;
14302 multi = TRUE;
14303 } else
14304 return CLI_SHOWUSAGE;
14305 if (!strcasecmp(a->argv[4], "like"))
14306 return CLI_SHOWUSAGE;
14307 if (!multi && !strcasecmp(a->argv[4], "all")) {
14308 multi = TRUE;
14309 name = NULL;
14310 }
14311 break;
14312 case 6:
14313 name = a->argv[5];
14314 multi = TRUE;
14315
14316 if (strcasecmp(a->argv[4], "like"))
14317 return CLI_SHOWUSAGE;
14318 if (!strcasecmp(a->argv[3], "peer")) {
14319 prunepeer = TRUE;
14320 } else
14321 return CLI_SHOWUSAGE;
14322 break;
14323 default:
14324 return CLI_SHOWUSAGE;
14325 }
14326
14327 if (multi && name) {
14328 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
14329 return CLI_SHOWUSAGE;
14330 }
14331
14332 if (multi) {
14333 if (prunepeer) {
14334 int pruned = 0;
14335
14336 i = ao2_iterator_init(peers, 0);
14337 while ((pi = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
14338 ao2_lock(pi);
14339 if (name && regexec(®exbuf, pi->name, 0, NULL, 0)) {
14340 unref_peer(pi, "toss iterator peer ptr before continue");
14341 ao2_unlock(pi);
14342 continue;
14343 };
14344 if (ast_test_flag(&pi->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
14345 pi->the_mark = 1;
14346 pruned++;
14347 }
14348 ao2_unlock(pi);
14349 unref_peer(pi, "toss iterator peer ptr");
14350 }
14351 ao2_iterator_destroy(&i);
14352 if (pruned) {
14353 ao2_t_callback(peers, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, peer_is_marked, 0,
14354 "initiating callback to remove marked peers");
14355 ast_cli(a->fd, "%d peers pruned.\n", pruned);
14356 } else
14357 ast_cli(a->fd, "No peers found to prune.\n");
14358 }
14359 } else {
14360 if (prunepeer) {
14361 struct sip_peer tmp;
14362 ast_copy_string(tmp.name, name, sizeof(tmp.name));
14363 if ((peer = ao2_t_find(peers, &tmp, OBJ_POINTER | OBJ_UNLINK, "finding to unlink from peers"))) {
14364 if (peer->addr.sin_addr.s_addr) {
14365 ao2_t_unlink(peers_by_ip, peer, "unlinking peer from peers_by_ip also");
14366 }
14367 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
14368 ast_cli(a->fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
14369
14370 ao2_t_link(peers, peer, "link peer into peer table");
14371 if (peer->addr.sin_addr.s_addr) {
14372 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
14373 }
14374
14375 } else
14376 ast_cli(a->fd, "Peer '%s' pruned.\n", name);
14377 unref_peer(peer, "sip_prune_realtime: unref_peer: tossing temp peer ptr");
14378 } else
14379 ast_cli(a->fd, "Peer '%s' not found.\n", name);
14380 }
14381 }
14382
14383 return CLI_SUCCESS;
14384 }
14385
14386
14387 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
14388 {
14389 int x, codec;
14390
14391 for(x = 0; x < 32 ; x++) {
14392 codec = ast_codec_pref_index(pref, x);
14393 if (!codec)
14394 break;
14395 ast_cli(fd, "%s", ast_getformatname(codec));
14396 ast_cli(fd, ":%d", pref->framing[x]);
14397 if (x < 31 && ast_codec_pref_index(pref, x + 1))
14398 ast_cli(fd, ",");
14399 }
14400 if (!x)
14401 ast_cli(fd, "none");
14402 }
14403
14404
14405 static const char *domain_mode_to_text(const enum domain_mode mode)
14406 {
14407 switch (mode) {
14408 case SIP_DOMAIN_AUTO:
14409 return "[Automatic]";
14410 case SIP_DOMAIN_CONFIG:
14411 return "[Configured]";
14412 }
14413
14414 return "";
14415 }
14416
14417
14418 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14419 {
14420 struct domain *d;
14421 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
14422
14423 switch (cmd) {
14424 case CLI_INIT:
14425 e->command = "sip show domains";
14426 e->usage =
14427 "Usage: sip show domains\n"
14428 " Lists all configured SIP local domains.\n"
14429 " Asterisk only responds to SIP messages to local domains.\n";
14430 return NULL;
14431 case CLI_GENERATE:
14432 return NULL;
14433 }
14434
14435 if (AST_LIST_EMPTY(&domain_list)) {
14436 ast_cli(a->fd, "SIP Domain support not enabled.\n\n");
14437 return CLI_SUCCESS;
14438 } else {
14439 ast_cli(a->fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
14440 AST_LIST_LOCK(&domain_list);
14441 AST_LIST_TRAVERSE(&domain_list, d, list)
14442 ast_cli(a->fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
14443 domain_mode_to_text(d->mode));
14444 AST_LIST_UNLOCK(&domain_list);
14445 ast_cli(a->fd, "\n");
14446 return CLI_SUCCESS;
14447 }
14448 }
14449 #undef FORMAT
14450
14451 static char mandescr_show_peer[] =
14452 "Description: Show one SIP peer with details on current status.\n"
14453 "Variables: \n"
14454 " Peer: <name> The peer name you want to check.\n"
14455 " ActionID: <id> Optional action ID for this AMI transaction.\n";
14456
14457
14458 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
14459 {
14460 const char *a[4];
14461 const char *peer;
14462
14463 peer = astman_get_header(m, "Peer");
14464 if (ast_strlen_zero(peer)) {
14465 astman_send_error(s, m, "Peer: <name> missing.");
14466 return 0;
14467 }
14468 a[0] = "sip";
14469 a[1] = "show";
14470 a[2] = "peer";
14471 a[3] = peer;
14472
14473 _sip_show_peer(1, -1, s, m, 4, a);
14474 astman_append(s, "\r\n\r\n" );
14475 return 0;
14476 }
14477
14478
14479 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14480 {
14481 switch (cmd) {
14482 case CLI_INIT:
14483 e->command = "sip show peer";
14484 e->usage =
14485 "Usage: sip show peer <name> [load]\n"
14486 " Shows all details on one SIP peer and the current status.\n"
14487 " Option \"load\" forces lookup of peer in realtime storage.\n";
14488 return NULL;
14489 case CLI_GENERATE:
14490 return complete_sip_show_peer(a->line, a->word, a->pos, a->n);
14491 }
14492 return _sip_show_peer(0, a->fd, NULL, NULL, a->argc, (const char **) a->argv);
14493 }
14494
14495
14496 static char *_sip_qualify_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
14497 {
14498 struct sip_peer *peer;
14499 int load_realtime;
14500
14501 if (argc < 4)
14502 return CLI_SHOWUSAGE;
14503
14504 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
14505 if ((peer = find_peer(argv[3], NULL, load_realtime, FINDPEERS, FALSE, 0))) {
14506 sip_poke_peer(peer, 1);
14507 unref_peer(peer, "qualify: done with peer");
14508 } else if (type == 0) {
14509 ast_cli(fd, "Peer '%s' not found\n", argv[3]);
14510 } else {
14511 astman_send_error(s, m, "Peer not found");
14512 }
14513 return CLI_SUCCESS;
14514 }
14515
14516
14517 static int manager_sip_qualify_peer(struct mansession *s, const struct message *m)
14518 {
14519 const char *a[4];
14520 const char *peer;
14521
14522 peer = astman_get_header(m, "Peer");
14523 if (ast_strlen_zero(peer)) {
14524 astman_send_error(s, m, "Peer: <name> missing.");
14525 return 0;
14526 }
14527 a[0] = "sip";
14528 a[1] = "qualify";
14529 a[2] = "peer";
14530 a[3] = peer;
14531
14532 _sip_qualify_peer(1, -1, s, m, 4, a);
14533 astman_append(s, "\r\n\r\n" );
14534 return 0;
14535 }
14536
14537
14538 static char *sip_qualify_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14539 {
14540 switch (cmd) {
14541 case CLI_INIT:
14542 e->command = "sip qualify peer";
14543 e->usage =
14544 "Usage: sip qualify peer <name> [load]\n"
14545 " Requests a response from one SIP peer and the current status.\n"
14546 " Option \"load\" forces lookup of peer in realtime storage.\n";
14547 return NULL;
14548 case CLI_GENERATE:
14549 return complete_sip_show_peer(a->line, a->word, a->pos, a->n);
14550 }
14551 return _sip_qualify_peer(0, a->fd, NULL, NULL, a->argc, (const char **) a->argv);
14552 }
14553
14554
14555 static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer)
14556 {
14557 struct sip_mailbox *mailbox;
14558
14559 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
14560 ast_str_append(mailbox_str, 0, "%s%s%s%s",
14561 mailbox->mailbox,
14562 ast_strlen_zero(mailbox->context) ? "" : "@",
14563 S_OR(mailbox->context, ""),
14564 AST_LIST_NEXT(mailbox, entry) ? "," : "");
14565 }
14566 }
14567
14568 static struct _map_x_s faxecmodes[] = {
14569 { SIP_PAGE2_T38SUPPORT_UDPTL, "None"},
14570 { SIP_PAGE2_T38SUPPORT_UDPTL_FEC, "FEC"},
14571 { SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY, "Redundancy"},
14572 { -1, NULL},
14573 };
14574
14575 static const char *faxec2str(int faxec)
14576 {
14577 return map_x_s(faxecmodes, faxec, "Unknown");
14578 }
14579
14580
14581 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
14582 {
14583 char status[30] = "";
14584 char cbuf[256];
14585 struct sip_peer *peer;
14586 char codec_buf[512];
14587 struct ast_codec_pref *pref;
14588 struct ast_variable *v;
14589 struct sip_auth *auth;
14590 int x = 0, codec = 0, load_realtime;
14591 int realtimepeers;
14592
14593 realtimepeers = ast_check_realtime("sippeers");
14594
14595 if (argc < 4)
14596 return CLI_SHOWUSAGE;
14597
14598 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
14599 peer = find_peer(argv[3], NULL, load_realtime, FINDPEERS, FALSE, 0);
14600
14601 if (s) {
14602 if (peer) {
14603 const char *id = astman_get_header(m, "ActionID");
14604
14605 astman_append(s, "Response: Success\r\n");
14606 if (!ast_strlen_zero(id))
14607 astman_append(s, "ActionID: %s\r\n", id);
14608 } else {
14609 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.", argv[3]);
14610 astman_send_error(s, m, cbuf);
14611 return CLI_SUCCESS;
14612 }
14613 }
14614 if (peer && type==0 ) {
14615 struct ast_str *mailbox_str = ast_str_alloca(512);
14616 ast_cli(fd, "\n\n");
14617 ast_cli(fd, " * Name : %s\n", peer->name);
14618 if (realtimepeers) {
14619 ast_cli(fd, " Realtime peer: %s\n", peer->is_realtime ? "Yes, cached" : "No");
14620 }
14621 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
14622 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
14623 for (auth = peer->auth; auth; auth = auth->next) {
14624 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
14625 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
14626 }
14627 ast_cli(fd, " Context : %s\n", peer->context);
14628 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
14629 ast_cli(fd, " Language : %s\n", peer->language);
14630 if (!ast_strlen_zero(peer->accountcode))
14631 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
14632 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
14633 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
14634 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
14635 if (!ast_strlen_zero(peer->fromuser))
14636 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
14637 if (!ast_strlen_zero(peer->fromdomain))
14638 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
14639 ast_cli(fd, " Callgroup : ");
14640 print_group(fd, peer->callgroup, 0);
14641 ast_cli(fd, " Pickupgroup : ");
14642 print_group(fd, peer->pickupgroup, 0);
14643 peer_mailboxes_to_str(&mailbox_str, peer);
14644 ast_cli(fd, " Mailbox : %s\n", mailbox_str->str);
14645 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
14646 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
14647 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
14648 if (peer->busy_level)
14649 ast_cli(fd, " Busy level : %d\n", peer->busy_level);
14650 ast_cli(fd, " Dynamic : %s\n", cli_yesno(peer->host_dynamic));
14651 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
14652 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
14653 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
14654 ast_cli(fd, " Insecure : %s\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
14655 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
14656 ast_cli(fd, " ACL : %s\n", cli_yesno(peer->ha != NULL));
14657 ast_cli(fd, " T.38 support : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
14658 ast_cli(fd, " T.38 EC mode : %s\n", faxec2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
14659 ast_cli(fd, " T.38 MaxDtgrm: %d\n", peer->t38_maxdatagram);
14660 ast_cli(fd, " CanReinvite : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)));
14661 ast_cli(fd, " PromiscRedir : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)));
14662 ast_cli(fd, " User=Phone : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)));
14663 ast_cli(fd, " Video Support: %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)));
14664 ast_cli(fd, " Text Support : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)));
14665 ast_cli(fd, " Ign SDP ver : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_IGNORESDPVERSION)));
14666 ast_cli(fd, " Trust RPID : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_TRUSTRPID)));
14667 ast_cli(fd, " Send RPID : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_SENDRPID)));
14668 ast_cli(fd, " Subscriptions: %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
14669 ast_cli(fd, " Overlap dial : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP)));
14670 if (peer->outboundproxy)
14671 ast_cli(fd, " Outb. proxy : %s %s\n", ast_strlen_zero(peer->outboundproxy->name) ? "<not set>" : peer->outboundproxy->name,
14672 peer->outboundproxy->force ? "(forced)" : "");
14673
14674
14675 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
14676 ast_cli(fd, " Timer T1 : %d\n", peer->timer_t1);
14677 ast_cli(fd, " Timer B : %d\n", peer->timer_b);
14678 ast_cli(fd, " ToHost : %s\n", peer->tohost);
14679 ast_cli(fd, " Addr->IP : %s Port %d\n", peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "(Unspecified)", ntohs(peer->addr.sin_port));
14680 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
14681 ast_cli(fd, " Transport : %s\n", get_transport(peer->socket.type));
14682 if (!ast_strlen_zero(global_regcontext))
14683 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
14684 ast_cli(fd, " Def. Username: %s\n", peer->username);
14685 ast_cli(fd, " SIP Options : ");
14686 if (peer->sipoptions) {
14687 int lastoption = -1;
14688 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
14689 if (sip_options[x].id != lastoption) {
14690 if (peer->sipoptions & sip_options[x].id)
14691 ast_cli(fd, "%s ", sip_options[x].text);
14692 lastoption = x;
14693 }
14694 }
14695 } else
14696 ast_cli(fd, "(none)");
14697
14698 ast_cli(fd, "\n");
14699 ast_cli(fd, " Codecs : ");
14700 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
14701 ast_cli(fd, "%s\n", codec_buf);
14702 ast_cli(fd, " Codec Order : (");
14703 print_codec_to_cli(fd, &peer->prefs);
14704 ast_cli(fd, ")\n");
14705
14706 ast_cli(fd, " Auto-Framing : %s \n", cli_yesno(peer->autoframing));
14707 ast_cli(fd, " 100 on REG : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_REGISTERTRYING) ? "Yes" : "No");
14708 ast_cli(fd, " Status : ");
14709 peer_status(peer, status, sizeof(status));
14710 ast_cli(fd, "%s\n", status);
14711 ast_cli(fd, " Useragent : %s\n", peer->useragent);
14712 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
14713 ast_cli(fd, " Qualify Freq : %d ms\n", peer->qualifyfreq);
14714 if (peer->chanvars) {
14715 ast_cli(fd, " Variables :\n");
14716 for (v = peer->chanvars ; v ; v = v->next)
14717 ast_cli(fd, " %s = %s\n", v->name, v->value);
14718 }
14719
14720 ast_cli(fd, " Sess-Timers : %s\n", stmode2str(peer->stimer.st_mode_oper));
14721 ast_cli(fd, " Sess-Refresh : %s\n", strefresher2str(peer->stimer.st_ref));
14722 ast_cli(fd, " Sess-Expires : %d secs\n", peer->stimer.st_max_se);
14723 ast_cli(fd, " Min-Sess : %d secs\n", peer->stimer.st_min_se);
14724 ast_cli(fd, " Parkinglot : %s\n", peer->parkinglot);
14725 ast_cli(fd, "\n");
14726 peer = unref_peer(peer, "sip_show_peer: unref_peer: done with peer ptr");
14727 } else if (peer && type == 1) {
14728 char buffer[256];
14729 struct ast_str *mailbox_str = ast_str_alloca(512);
14730 astman_append(s, "Channeltype: SIP\r\n");
14731 astman_append(s, "ObjectName: %s\r\n", peer->name);
14732 astman_append(s, "ChanObjectType: peer\r\n");
14733 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
14734 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
14735 astman_append(s, "Context: %s\r\n", peer->context);
14736 astman_append(s, "Language: %s\r\n", peer->language);
14737 if (!ast_strlen_zero(peer->accountcode))
14738 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
14739 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
14740 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
14741 if (!ast_strlen_zero(peer->fromuser))
14742 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
14743 if (!ast_strlen_zero(peer->fromdomain))
14744 astman_append(s, "SIP-FromDomain: %s\r\n", peer->fromdomain);
14745 astman_append(s, "Callgroup: ");
14746 astman_append(s, "%s\r\n", ast_print_group(buffer, sizeof(buffer), peer->callgroup));
14747 astman_append(s, "Pickupgroup: ");
14748 astman_append(s, "%s\r\n", ast_print_group(buffer, sizeof(buffer), peer->pickupgroup));
14749 peer_mailboxes_to_str(&mailbox_str, peer);
14750 astman_append(s, "VoiceMailbox: %s\r\n", mailbox_str->str);
14751 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
14752 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
14753 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
14754 astman_append(s, "Busy-level: %d\r\n", peer->busy_level);
14755 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
14756 astman_append(s, "Dynamic: %s\r\n", peer->host_dynamic?"Y":"N");
14757 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
14758 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched, peer->expire));
14759 astman_append(s, "SIP-AuthInsecure: %s\r\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
14760 astman_append(s, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
14761 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
14762 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Y":"N"));
14763 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
14764 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
14765 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
14766 astman_append(s, "SIP-TextSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)?"Y":"N"));
14767 astman_append(s, "SIP-T.38Support: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)?"Y":"N"));
14768 astman_append(s, "SIP-T.38EC: %s\r\n", faxec2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
14769 astman_append(s, "SIP-T.38MaxDtgrm: %d\r\n", peer->t38_maxdatagram);
14770 astman_append(s, "SIP-Sess-Timers: %s\r\n", stmode2str(peer->stimer.st_mode_oper));
14771 astman_append(s, "SIP-Sess-Refresh: %s\r\n", strefresher2str(peer->stimer.st_ref));
14772 astman_append(s, "SIP-Sess-Expires: %d\r\n", peer->stimer.st_max_se);
14773 astman_append(s, "SIP-Sess-Min: %d\r\n", peer->stimer.st_min_se);
14774
14775
14776 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
14777 astman_append(s, "ToHost: %s\r\n", peer->tohost);
14778 astman_append(s, "Address-IP: %s\r\nAddress-Port: %d\r\n", peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", ntohs(peer->addr.sin_port));
14779 astman_append(s, "Default-addr-IP: %s\r\nDefault-addr-port: %d\r\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
14780 astman_append(s, "Default-Username: %s\r\n", peer->username);
14781 if (!ast_strlen_zero(global_regcontext))
14782 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
14783 astman_append(s, "Codecs: ");
14784 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
14785 astman_append(s, "%s\r\n", codec_buf);
14786 astman_append(s, "CodecOrder: ");
14787 pref = &peer->prefs;
14788 for(x = 0; x < 32 ; x++) {
14789 codec = ast_codec_pref_index(pref, x);
14790 if (!codec)
14791 break;
14792 astman_append(s, "%s", ast_getformatname(codec));
14793 if (x < 31 && ast_codec_pref_index(pref, x+1))
14794 astman_append(s, ",");
14795 }
14796
14797 astman_append(s, "\r\n");
14798 astman_append(s, "Status: ");
14799 peer_status(peer, status, sizeof(status));
14800 astman_append(s, "%s\r\n", status);
14801 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
14802 astman_append(s, "Reg-Contact: %s\r\n", peer->fullcontact);
14803 astman_append(s, "QualifyFreq: %d ms\r\n", peer->qualifyfreq);
14804 astman_append(s, "Parkinglot: %s\r\n", peer->parkinglot);
14805 if (peer->chanvars) {
14806 for (v = peer->chanvars ; v ; v = v->next) {
14807 astman_append(s, "ChanVariable: %s=%s\r\n", v->name, v->value);
14808 }
14809 }
14810
14811 peer = unref_peer(peer, "sip_show_peer: unref_peer: done with peer");
14812
14813 } else {
14814 ast_cli(fd, "Peer %s not found.\n", argv[3]);
14815 ast_cli(fd, "\n");
14816 }
14817
14818 return CLI_SUCCESS;
14819 }
14820
14821
14822 static char *complete_sip_user(const char *word, int state)
14823 {
14824 char *result = NULL;
14825 int wordlen = strlen(word);
14826 int which = 0;
14827 struct ao2_iterator user_iter;
14828 struct sip_peer *user;
14829
14830 user_iter = ao2_iterator_init(peers, 0);
14831 while ((user = ao2_iterator_next(&user_iter))) {
14832 ao2_lock(user);
14833 if (!(user->type & SIP_TYPE_USER)) {
14834 ao2_unlock(user);
14835 unref_peer(user, "complete sip user");
14836 continue;
14837 }
14838
14839 if (!strncasecmp(word, user->name, wordlen) && ++which > state) {
14840 result = ast_strdup(user->name);
14841 }
14842 ao2_unlock(user);
14843 unref_peer(user, "complete sip user");
14844 }
14845 ao2_iterator_destroy(&user_iter);
14846 return result;
14847 }
14848
14849 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
14850 {
14851 if (pos == 3)
14852 return complete_sip_user(word, state);
14853
14854 return NULL;
14855 }
14856
14857
14858 static char *sip_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14859 {
14860 char cbuf[256];
14861 struct sip_peer *user;
14862 struct ast_variable *v;
14863 int load_realtime;
14864
14865 switch (cmd) {
14866 case CLI_INIT:
14867 e->command = "sip show user";
14868 e->usage =
14869 "Usage: sip show user <name> [load]\n"
14870 " Shows all details on one SIP user and the current status.\n"
14871 " Option \"load\" forces lookup of peer in realtime storage.\n";
14872 return NULL;
14873 case CLI_GENERATE:
14874 return complete_sip_show_user(a->line, a->word, a->pos, a->n);
14875 }
14876
14877 if (a->argc < 4)
14878 return CLI_SHOWUSAGE;
14879
14880
14881 load_realtime = (a->argc == 5 && !strcmp(a->argv[4], "load")) ? TRUE : FALSE;
14882
14883 if ((user = find_peer(a->argv[3], NULL, load_realtime, FINDUSERS, FALSE, 0))) {
14884 ao2_lock(user);
14885 ast_cli(a->fd, "\n\n");
14886 ast_cli(a->fd, " * Name : %s\n", user->name);
14887 ast_cli(a->fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
14888 ast_cli(a->fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
14889 ast_cli(a->fd, " Context : %s\n", user->context);
14890 ast_cli(a->fd, " Language : %s\n", user->language);
14891 if (!ast_strlen_zero(user->accountcode))
14892 ast_cli(a->fd, " Accountcode : %s\n", user->accountcode);
14893 ast_cli(a->fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
14894 ast_cli(a->fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
14895 ast_cli(a->fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
14896 ast_cli(a->fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
14897 ast_cli(a->fd, " Call limit : %d\n", user->call_limit);
14898 ast_cli(a->fd, " Callgroup : ");
14899 print_group(a->fd, user->callgroup, 0);
14900 ast_cli(a->fd, " Pickupgroup : ");
14901 print_group(a->fd, user->pickupgroup, 0);
14902 ast_cli(a->fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
14903 ast_cli(a->fd, " ACL : %s\n", cli_yesno(user->ha != NULL));
14904 ast_cli(a->fd, " Sess-Timers : %s\n", stmode2str(user->stimer.st_mode_oper));
14905 ast_cli(a->fd, " Sess-Refresh : %s\n", strefresher2str(user->stimer.st_ref));
14906 ast_cli(a->fd, " Sess-Expires : %d secs\n", user->stimer.st_max_se);
14907 ast_cli(a->fd, " Sess-Min-SE : %d secs\n", user->stimer.st_min_se);
14908 ast_cli(a->fd, " Ign SDP ver : %s\n", cli_yesno(ast_test_flag(&user->flags[1], SIP_PAGE2_IGNORESDPVERSION)));
14909
14910 ast_cli(a->fd, " Codec Order : (");
14911 print_codec_to_cli(a->fd, &user->prefs);
14912 ast_cli(a->fd, ")\n");
14913
14914 ast_cli(a->fd, " Auto-Framing: %s \n", cli_yesno(user->autoframing));
14915 if (user->chanvars) {
14916 ast_cli(a->fd, " Variables :\n");
14917 for (v = user->chanvars ; v ; v = v->next)
14918 ast_cli(a->fd, " %s = %s\n", v->name, v->value);
14919 }
14920
14921 ast_cli(a->fd, "\n");
14922
14923 ao2_unlock(user);
14924 unref_peer(user, "sip show user");
14925 } else {
14926 ast_cli(a->fd, "User %s not found.\n", a->argv[3]);
14927 ast_cli(a->fd, "\n");
14928 }
14929
14930 return CLI_SUCCESS;
14931 }
14932
14933
14934 static char *sip_show_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14935 {
14936 struct ast_str *cbuf;
14937 struct ast_cb_names cbnames = {9, { "retrans_pkt",
14938 "__sip_autodestruct",
14939 "expire_register",
14940 "auto_congest",
14941 "sip_reg_timeout",
14942 "sip_poke_peer_s",
14943 "sip_poke_noanswer",
14944 "sip_reregister",
14945 "sip_reinvite_retry"},
14946 { retrans_pkt,
14947 __sip_autodestruct,
14948 expire_register,
14949 auto_congest,
14950 sip_reg_timeout,
14951 sip_poke_peer_s,
14952 sip_poke_noanswer,
14953 sip_reregister,
14954 sip_reinvite_retry}};
14955
14956 switch (cmd) {
14957 case CLI_INIT:
14958 e->command = "sip show sched";
14959 e->usage =
14960 "Usage: sip show sched\n"
14961 " Shows stats on what's in the sched queue at the moment\n";
14962 return NULL;
14963 case CLI_GENERATE:
14964 return NULL;
14965 }
14966
14967 cbuf = ast_str_alloca(2048);
14968
14969 ast_cli(a->fd, "\n");
14970 ast_sched_report(sched, &cbuf, &cbnames);
14971 ast_cli(a->fd, "%s", cbuf->str);
14972
14973 return CLI_SUCCESS;
14974 }
14975
14976
14977 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14978 {
14979 #define FORMAT2 "%-30.30s %-6.6s %-12.12s %8.8s %-20.20s %-25.25s\n"
14980 #define FORMAT "%-30.30s %-6.6s %-12.12s %8d %-20.20s %-25.25s\n"
14981 char host[80];
14982 char tmpdat[256];
14983 struct ast_tm tm;
14984 int counter = 0;
14985
14986 switch (cmd) {
14987 case CLI_INIT:
14988 e->command = "sip show registry";
14989 e->usage =
14990 "Usage: sip show registry\n"
14991 " Lists all registration requests and status.\n";
14992 return NULL;
14993 case CLI_GENERATE:
14994 return NULL;
14995 }
14996
14997 if (a->argc != 3)
14998 return CLI_SHOWUSAGE;
14999 ast_cli(a->fd, FORMAT2, "Host", "dnsmgr", "Username", "Refresh", "State", "Reg.Time");
15000
15001 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
15002 ASTOBJ_RDLOCK(iterator);
15003 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
15004 if (iterator->regtime.tv_sec) {
15005 ast_localtime(&iterator->regtime, &tm, NULL);
15006 ast_strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
15007 } else
15008 tmpdat[0] = '\0';
15009 ast_cli(a->fd, FORMAT, host, (iterator->dnsmgr) ? "Y" : "N", iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
15010 ASTOBJ_UNLOCK(iterator);
15011 counter++;
15012 } while(0));
15013 ast_cli(a->fd, "%d SIP registrations.\n", counter);
15014 return CLI_SUCCESS;
15015 #undef FORMAT
15016 #undef FORMAT2
15017 }
15018
15019
15020
15021
15022
15023 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15024 {
15025 struct sip_peer *peer;
15026 int load_realtime = 0;
15027
15028 switch (cmd) {
15029 case CLI_INIT:
15030 e->command = "sip unregister";
15031 e->usage =
15032 "Usage: sip unregister <peer>\n"
15033 " Unregister (force expiration) a SIP peer from the registry\n";
15034 return NULL;
15035 case CLI_GENERATE:
15036 return complete_sip_unregister(a->line, a->word, a->pos, a->n);
15037 }
15038
15039 if (a->argc != 3)
15040 return CLI_SHOWUSAGE;
15041
15042 if ((peer = find_peer(a->argv[2], NULL, load_realtime, FINDPEERS, TRUE, 0))) {
15043 if (peer->expire > 0) {
15044 AST_SCHED_DEL_UNREF(sched, peer->expire,
15045 unref_peer(peer, "remove register expire ref"));
15046 expire_register(ref_peer(peer, "ref for expire_register"));
15047 ast_cli(a->fd, "Unregistered peer \'%s\'\n\n", a->argv[2]);
15048 } else {
15049 ast_cli(a->fd, "Peer %s not registered\n", a->argv[2]);
15050 }
15051 unref_peer(peer, "sip_unregister: unref_peer via sip_unregister: done with peer from find_peer call");
15052 } else {
15053 ast_cli(a->fd, "Peer unknown: \'%s\'. Not unregistered.\n", a->argv[2]);
15054 }
15055
15056 return CLI_SUCCESS;
15057 }
15058
15059
15060 static int show_chanstats_cb(void *__cur, void *__arg, int flags)
15061 {
15062 #define FORMAT2 "%-15.15s %-11.11s %-8.8s %-10.10s %-10.10s ( %%) %-6.6s %-10.10s %-10.10s ( %%) %-6.6s\n"
15063 #define FORMAT "%-15.15s %-11.11s %-8.8s %-10.10u%-1.1s %-10.10u (%5.2f%%) %-6.6u %-10.10u%-1.1s %-10.10u (%5.2f%%) %-6.6u\n"
15064 struct sip_pvt *cur = __cur;
15065 unsigned int rxcount, txcount, rxploss, txploss;
15066 char durbuf[10];
15067 int duration;
15068 int durh, durm, durs;
15069 struct ast_channel *c = cur->owner;
15070 struct __show_chan_arg *arg = __arg;
15071 int fd = arg->fd;
15072
15073
15074 if (cur->subscribed != NONE)
15075 return 0;
15076
15077 if (!cur->rtp) {
15078 if (sipdebug)
15079 ast_cli(fd, "%-15.15s %-11.11s (inv state: %s) -- %s\n", ast_inet_ntoa(cur->sa.sin_addr), cur->callid, invitestate2string[cur->invitestate].desc, "-- No RTP active");
15080 return 0;
15081 }
15082 rxcount = ast_rtp_get_qosvalue(cur->rtp, AST_RTP_RXCOUNT);
15083 txcount = ast_rtp_get_qosvalue(cur->rtp, AST_RTP_TXCOUNT);
15084 rxploss = ast_rtp_get_qosvalue(cur->rtp, AST_RTP_RXPLOSS);
15085 txploss = ast_rtp_get_qosvalue(cur->rtp, AST_RTP_TXPLOSS);
15086
15087
15088 if (c && c->cdr && !ast_tvzero(c->cdr->start)) {
15089 duration = (int)(ast_tvdiff_ms(ast_tvnow(), c->cdr->start) / 1000);
15090 durh = duration / 3600;
15091 durm = (duration % 3600) / 60;
15092 durs = duration % 60;
15093 snprintf(durbuf, sizeof(durbuf), "%02d:%02d:%02d", durh, durm, durs);
15094 } else {
15095 durbuf[0] = '\0';
15096 }
15097
15098 ast_cli(fd, FORMAT,
15099 ast_inet_ntoa(cur->sa.sin_addr),
15100 cur->callid,
15101 durbuf,
15102 rxcount > (unsigned int) 100000 ? (unsigned int) (rxcount)/(unsigned int) 1000 : rxcount,
15103 rxcount > (unsigned int) 100000 ? "K":" ",
15104 rxploss,
15105 (rxcount + rxploss) > 0 ? (double) rxploss / (rxcount + rxploss) * 100 : 0,
15106 ast_rtp_get_qosvalue(cur->rtp, AST_RTP_RXJITTER),
15107 txcount > (unsigned int) 100000 ? (unsigned int) (txcount)/(unsigned int) 1000 : txcount,
15108 txcount > (unsigned int) 100000 ? "K":" ",
15109 txploss,
15110 txcount > 0 ? (double) txploss / txcount * 100 : 0,
15111 ast_rtp_get_qosvalue(cur->rtp, AST_RTP_TXJITTER)
15112 );
15113 arg->numchans++;
15114
15115 return 0;
15116 }
15117
15118
15119 static char *sip_show_channelstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15120 {
15121 struct __show_chan_arg arg = { .fd = a->fd, .numchans = 0 };
15122
15123 switch (cmd) {
15124 case CLI_INIT:
15125 e->command = "sip show channelstats";
15126 e->usage =
15127 "Usage: sip show channelstats\n"
15128 " Lists all currently active SIP channel's RTCP statistics.\n"
15129 " Note that calls in the much optimized RTP P2P bridge mode will not show any packets here.";
15130 return NULL;
15131 case CLI_GENERATE:
15132 return NULL;
15133 }
15134
15135 if (a->argc != 3)
15136 return CLI_SHOWUSAGE;
15137
15138 ast_cli(a->fd, FORMAT2, "Peer", "Call ID", "Duration", "Recv: Pack", "Lost", "Jitter", "Send: Pack", "Lost", "Jitter");
15139
15140 ao2_t_callback(dialogs, OBJ_NODATA, show_chanstats_cb, &arg, "callback to sip show chanstats");
15141 ast_cli(a->fd, "%d active SIP channel%s\n", arg.numchans, (arg.numchans != 1) ? "s" : "");
15142 return CLI_SUCCESS;
15143 }
15144 #undef FORMAT
15145 #undef FORMAT2
15146
15147
15148 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15149 {
15150 int realtimepeers;
15151 int realtimeregs;
15152 char codec_buf[SIPBUFSIZE];
15153 const char *msg;
15154
15155 switch (cmd) {
15156 case CLI_INIT:
15157 e->command = "sip show settings";
15158 e->usage =
15159 "Usage: sip show settings\n"
15160 " Provides detailed list of the configuration of the SIP channel.\n";
15161 return NULL;
15162 case CLI_GENERATE:
15163 return NULL;
15164 }
15165
15166
15167 realtimepeers = ast_check_realtime("sippeers");
15168 realtimeregs = ast_check_realtime("sipregs");
15169
15170 if (a->argc != 3)
15171 return CLI_SHOWUSAGE;
15172 ast_cli(a->fd, "\n\nGlobal Settings:\n");
15173 ast_cli(a->fd, "----------------\n");
15174 ast_cli(a->fd, " UDP SIP Port: %d\n", ntohs(bindaddr.sin_port));
15175 ast_cli(a->fd, " UDP Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
15176 ast_cli(a->fd, " TCP SIP Port: ");
15177 if (sip_tcp_desc.local_address.sin_family == AF_INET) {
15178 ast_cli(a->fd, "%d\n", ntohs(sip_tcp_desc.local_address.sin_port));
15179 ast_cli(a->fd, " TCP Bindaddress: %s\n", ast_inet_ntoa(sip_tcp_desc.local_address.sin_addr));
15180 } else {
15181 ast_cli(a->fd, "Disabled\n");
15182 }
15183 ast_cli(a->fd, " TLS SIP Port: ");
15184 if (default_tls_cfg.enabled != FALSE) {
15185 ast_cli(a->fd, "%d\n", ntohs(sip_tls_desc.local_address.sin_port));
15186 ast_cli(a->fd, " TLS Bindaddress: %s\n", ast_inet_ntoa(sip_tls_desc.local_address.sin_addr));
15187 } else {
15188 ast_cli(a->fd, "Disabled\n");
15189 }
15190 ast_cli(a->fd, " Videosupport: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT)));
15191 ast_cli(a->fd, " Textsupport: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT)));
15192 ast_cli(a->fd, " AutoCreate Peer: %s\n", cli_yesno(autocreatepeer));
15193 ast_cli(a->fd, " Ignore SDP sess. ver.: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION)));
15194 ast_cli(a->fd, " Match Auth Username: %s\n", cli_yesno(global_match_auth_username));
15195 ast_cli(a->fd, " Allow unknown access: %s\n", cli_yesno(global_allowguest));
15196 ast_cli(a->fd, " Allow subscriptions: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
15197 ast_cli(a->fd, " Allow overlap dialing: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP)));
15198 ast_cli(a->fd, " Allow promsic. redir: %s\n", cli_yesno(ast_test_flag(&global_flags[0], SIP_PROMISCREDIR)));
15199 ast_cli(a->fd, " Enable call counters: %s\n", cli_yesno(global_callcounter));
15200 ast_cli(a->fd, " SIP domain support: %s\n", cli_yesno(!AST_LIST_EMPTY(&domain_list)));
15201 ast_cli(a->fd, " Realm. auth: %s\n", cli_yesno(authl != NULL));
15202 ast_cli(a->fd, " Our auth realm %s\n", global_realm);
15203 ast_cli(a->fd, " Call to non-local dom.: %s\n", cli_yesno(allow_external_domains));
15204 ast_cli(a->fd, " URI user is phone no: %s\n", cli_yesno(ast_test_flag(&global_flags[0], SIP_USEREQPHONE)));
15205 ast_cli(a->fd, " Always auth rejects: %s\n", cli_yesno(global_alwaysauthreject));
15206 ast_cli(a->fd, " Direct RTP setup: %s\n", cli_yesno(global_directrtpsetup));
15207 ast_cli(a->fd, " User Agent: %s\n", global_useragent);
15208 ast_cli(a->fd, " SDP Session Name: %s\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
15209 ast_cli(a->fd, " SDP Owner Name: %s\n", ast_strlen_zero(global_sdpowner) ? "-" : global_sdpowner);
15210 ast_cli(a->fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
15211 ast_cli(a->fd, " Regexten on Qualify: %s\n", cli_yesno(global_regextenonqualify));
15212 ast_cli(a->fd, " Caller ID: %s\n", default_callerid);
15213 ast_cli(a->fd, " From: Domain: %s\n", default_fromdomain);
15214 ast_cli(a->fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
15215 ast_cli(a->fd, " Call Events: %s\n", global_callevents ? "On" : "Off");
15216 ast_cli(a->fd, " Auth. Failure Events: %s\n", global_authfailureevents ? "On" : "Off");
15217
15218 ast_cli(a->fd, " T.38 support: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT)));
15219 ast_cli(a->fd, " T.38 EC mode: %s\n", faxec2str(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT)));
15220 ast_cli(a->fd, " T.38 MaxDtgrm: %d\n", global_t38_maxdatagram);
15221
15222 if (!realtimepeers && !realtimeregs)
15223 ast_cli(a->fd, " SIP realtime: Disabled\n" );
15224 else
15225 ast_cli(a->fd, " SIP realtime: Enabled\n" );
15226 ast_cli(a->fd, " Qualify Freq : %d ms\n", global_qualifyfreq);
15227 ast_cli(a->fd, "\nNetwork QoS Settings:\n");
15228 ast_cli(a->fd, "---------------------------\n");
15229 ast_cli(a->fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
15230 ast_cli(a->fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
15231 ast_cli(a->fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
15232 ast_cli(a->fd, " IP ToS RTP text: %s\n", ast_tos2str(global_tos_text));
15233 ast_cli(a->fd, " 802.1p CoS SIP: %d\n", global_cos_sip);
15234 ast_cli(a->fd, " 802.1p CoS RTP audio: %d\n", global_cos_audio);
15235 ast_cli(a->fd, " 802.1p CoS RTP video: %d\n", global_cos_video);
15236 ast_cli(a->fd, " 802.1p CoS RTP text: %d\n", global_cos_text);
15237 ast_cli(a->fd, " Jitterbuffer enabled: %s\n", cli_yesno(ast_test_flag(&global_jbconf, AST_JB_ENABLED)));
15238 ast_cli(a->fd, " Jitterbuffer forced: %s\n", cli_yesno(ast_test_flag(&global_jbconf, AST_JB_FORCED)));
15239 ast_cli(a->fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
15240 ast_cli(a->fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
15241 ast_cli(a->fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
15242 ast_cli(a->fd, " Jitterbuffer log: %s\n", cli_yesno(ast_test_flag(&global_jbconf, AST_JB_LOG)));
15243
15244 ast_cli(a->fd, "\nNetwork Settings:\n");
15245 ast_cli(a->fd, "---------------------------\n");
15246
15247 if (localaddr == NULL)
15248 msg = "Disabled, no localnet list";
15249 else if (externip.sin_addr.s_addr == 0)
15250 msg = "Disabled, externip is 0.0.0.0";
15251 else if (stunaddr.sin_addr.s_addr != 0)
15252 msg = "Enabled using STUN";
15253 else if (!ast_strlen_zero(externhost))
15254 msg = "Enabled using externhost";
15255 else
15256 msg = "Enabled using externip";
15257 ast_cli(a->fd, " SIP address remapping: %s\n", msg);
15258 ast_cli(a->fd, " Externhost: %s\n", S_OR(externhost, "<none>"));
15259 ast_cli(a->fd, " Externip: %s:%d\n", ast_inet_ntoa(externip.sin_addr), ntohs(externip.sin_port));
15260 ast_cli(a->fd, " Externrefresh: %d\n", externrefresh);
15261 ast_cli(a->fd, " Internal IP: %s:%d\n", ast_inet_ntoa(internip.sin_addr), ntohs(internip.sin_port));
15262 {
15263 struct ast_ha *d;
15264 const char *prefix = "Localnet:";
15265 char buf[INET_ADDRSTRLEN];
15266
15267 for (d = localaddr; d ; prefix = "", d = d->next) {
15268 ast_cli(a->fd, " %-24s%s/%s\n",
15269 prefix, ast_inet_ntoa(d->netaddr),
15270 inet_ntop(AF_INET, &d->netmask, buf, sizeof(buf)) );
15271 }
15272 }
15273 ast_cli(a->fd, " STUN server: %s:%d\n", ast_inet_ntoa(stunaddr.sin_addr), ntohs(stunaddr.sin_port));
15274
15275 ast_cli(a->fd, "\nGlobal Signalling Settings:\n");
15276 ast_cli(a->fd, "---------------------------\n");
15277 ast_cli(a->fd, " Codecs: ");
15278 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, global_capability);
15279 ast_cli(a->fd, "%s\n", codec_buf);
15280 ast_cli(a->fd, " Codec Order: ");
15281 print_codec_to_cli(a->fd, &default_prefs);
15282 ast_cli(a->fd, "\n");
15283 ast_cli(a->fd, " Relax DTMF: %s\n", cli_yesno(global_relaxdtmf));
15284 ast_cli(a->fd, " RFC2833 Compensation: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE)));
15285 ast_cli(a->fd, " Compact SIP headers: %s\n", cli_yesno(compactheaders));
15286 ast_cli(a->fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
15287 ast_cli(a->fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
15288 ast_cli(a->fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
15289 ast_cli(a->fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
15290 ast_cli(a->fd, " DNS SRV lookup: %s\n", cli_yesno(global_srvlookup));
15291 ast_cli(a->fd, " Pedantic SIP support: %s\n", cli_yesno(pedanticsipchecking));
15292 ast_cli(a->fd, " Reg. min duration %d secs\n", min_expiry);
15293 ast_cli(a->fd, " Reg. max duration: %d secs\n", max_expiry);
15294 ast_cli(a->fd, " Reg. default duration: %d secs\n", default_expiry);
15295 ast_cli(a->fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
15296 ast_cli(a->fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
15297 ast_cli(a->fd, " Notify ringing state: %s\n", cli_yesno(global_notifyringing));
15298 ast_cli(a->fd, " Notify hold state: %s\n", cli_yesno(global_notifyhold));
15299 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(global_allowtransfer));
15300 ast_cli(a->fd, " Max Call Bitrate: %d kbps\n", default_maxcallbitrate);
15301 ast_cli(a->fd, " Auto-Framing: %s\n", cli_yesno(global_autoframing));
15302 ast_cli(a->fd, " Outb. proxy: %s %s\n", ast_strlen_zero(global_outboundproxy.name) ? "<not set>" : global_outboundproxy.name,
15303 global_outboundproxy.force ? "(forced)" : "");
15304 ast_cli(a->fd, " Session Timers: %s\n", stmode2str(global_st_mode));
15305 ast_cli(a->fd, " Session Refresher: %s\n", strefresher2str (global_st_refresher));
15306 ast_cli(a->fd, " Session Expires: %d secs\n", global_max_se);
15307 ast_cli(a->fd, " Session Min-SE: %d secs\n", global_min_se);
15308 ast_cli(a->fd, " Timer T1: %d\n", global_t1);
15309 ast_cli(a->fd, " Timer T1 minimum: %d\n", global_t1min);
15310 ast_cli(a->fd, " Timer B: %d\n", global_timer_b);
15311 ast_cli(a->fd, " No premature media: %s\n", global_prematuremediafilter ? "Yes" : "No");
15312
15313 ast_cli(a->fd, "\nDefault Settings:\n");
15314 ast_cli(a->fd, "-----------------\n");
15315 ast_cli(a->fd, " Context: %s\n", default_context);
15316 ast_cli(a->fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags[0], SIP_NAT)));
15317 ast_cli(a->fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
15318 ast_cli(a->fd, " Qualify: %d\n", default_qualify);
15319 ast_cli(a->fd, " Use ClientCode: %s\n", cli_yesno(ast_test_flag(&global_flags[0], SIP_USECLIENTCODE)));
15320 ast_cli(a->fd, " Progress inband: %s\n", (ast_test_flag(&global_flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER) ? "Never" : (ast_test_flag(&global_flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NO) ? "No" : "Yes" );
15321 ast_cli(a->fd, " Language: %s\n", default_language);
15322 ast_cli(a->fd, " MOH Interpret: %s\n", default_mohinterpret);
15323 ast_cli(a->fd, " MOH Suggest: %s\n", default_mohsuggest);
15324 ast_cli(a->fd, " Voice Mail Extension: %s\n", default_vmexten);
15325
15326
15327 if (realtimepeers || realtimeregs) {
15328 ast_cli(a->fd, "\nRealtime SIP Settings:\n");
15329 ast_cli(a->fd, "----------------------\n");
15330 ast_cli(a->fd, " Realtime Peers: %s\n", cli_yesno(realtimepeers));
15331 ast_cli(a->fd, " Realtime Regs: %s\n", cli_yesno(realtimeregs));
15332 ast_cli(a->fd, " Cache Friends: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)));
15333 ast_cli(a->fd, " Update: %s\n", cli_yesno(sip_cfg.peer_rtupdate));
15334 ast_cli(a->fd, " Ignore Reg. Expire: %s\n", cli_yesno(sip_cfg.ignore_regexpire));
15335 ast_cli(a->fd, " Save sys. name: %s\n", cli_yesno(sip_cfg.rtsave_sysname));
15336 ast_cli(a->fd, " Auto Clear: %d\n", global_rtautoclear);
15337 }
15338 ast_cli(a->fd, "\n----\n");
15339 return CLI_SUCCESS;
15340 }
15341
15342
15343 static const char *subscription_type2str(enum subscriptiontype subtype)
15344 {
15345 int i;
15346
15347 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
15348 if (subscription_types[i].type == subtype) {
15349 return subscription_types[i].text;
15350 }
15351 }
15352 return subscription_types[0].text;
15353 }
15354
15355
15356 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
15357 {
15358 int i;
15359
15360 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
15361 if (subscription_types[i].type == subtype) {
15362 return &subscription_types[i];
15363 }
15364 }
15365 return &subscription_types[0];
15366 }
15367
15368
15369
15370
15371
15372
15373
15374
15375 #define FORMAT4 "%-15.15s %-15.15s %-15.15s %-15.15s %-13.13s %-15.15s %-10.10s %-6.6d\n"
15376 #define FORMAT3 "%-15.15s %-15.15s %-15.15s %-15.15s %-13.13s %-15.15s %-10.10s %-6.6s\n"
15377 #define FORMAT2 "%-15.15s %-15.15s %-15.15s %-15.15s %-7.7s %-15.15s %-6.6s\n"
15378 #define FORMAT "%-15.15s %-15.15s %-15.15s %-15.15s %-3.3s %-3.3s %-15.15s %-10.10s\n"
15379
15380
15381 static int show_channels_cb(void *__cur, void *__arg, int flags)
15382 {
15383 struct sip_pvt *cur = __cur;
15384 struct __show_chan_arg *arg = __arg;
15385 const struct sockaddr_in *dst = sip_real_dst(cur);
15386
15387
15388 if (cur->subscribed == NONE && !arg->subscriptions) {
15389
15390 const char *referstatus = cur->refer ? referstatus2str(cur->refer->status) : "";
15391 char formatbuf[SIPBUFSIZE/2];
15392
15393 ast_cli(arg->fd, FORMAT, ast_inet_ntoa(dst->sin_addr),
15394 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
15395 cur->callid,
15396 ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
15397 cli_yesno(ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD)),
15398 cur->needdestroy ? "(d)" : "",
15399 cur->lastmsg ,
15400 referstatus
15401 );
15402 arg->numchans++;
15403 }
15404 if (cur->subscribed != NONE && arg->subscriptions) {
15405 struct ast_str *mailbox_str = ast_str_alloca(512);
15406 if (cur->subscribed == MWI_NOTIFICATION && cur->relatedpeer)
15407 peer_mailboxes_to_str(&mailbox_str, cur->relatedpeer);
15408 ast_cli(arg->fd, FORMAT4, ast_inet_ntoa(dst->sin_addr),
15409 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
15410 cur->callid,
15411
15412 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
15413 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
15414 subscription_type2str(cur->subscribed),
15415 cur->subscribed == MWI_NOTIFICATION ? S_OR(mailbox_str->str, "<none>") : "<none>",
15416 cur->expiry
15417 );
15418 arg->numchans++;
15419 }
15420 return 0;
15421 }
15422
15423
15424
15425
15426
15427
15428 static char *sip_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15429 {
15430 struct __show_chan_arg arg = { .fd = a->fd, .numchans = 0 };
15431
15432
15433 if (cmd == CLI_INIT) {
15434 e->command = "sip show {channels|subscriptions}";
15435 e->usage =
15436 "Usage: sip show channels\n"
15437 " Lists all currently active SIP calls (dialogs).\n"
15438 "Usage: sip show subscriptions\n"
15439 " Lists active SIP subscriptions.\n";
15440 return NULL;
15441 } else if (cmd == CLI_GENERATE)
15442 return NULL;
15443
15444 if (a->argc != e->args)
15445 return CLI_SHOWUSAGE;
15446 arg.subscriptions = !strcasecmp(a->argv[e->args - 1], "subscriptions");
15447 if (!arg.subscriptions)
15448 ast_cli(arg.fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Format", "Hold", "Last Message", "Expiry");
15449 else
15450 ast_cli(arg.fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox", "Expiry");
15451
15452
15453 ao2_t_callback(dialogs, OBJ_NODATA, show_channels_cb, &arg, "callback to show channels");
15454
15455
15456 ast_cli(arg.fd, "%d active SIP %s%s\n", arg.numchans,
15457 (arg.subscriptions ? "subscription" : "dialog"),
15458 ESS(arg.numchans));
15459 return CLI_SUCCESS;
15460 #undef FORMAT
15461 #undef FORMAT2
15462 #undef FORMAT3
15463 }
15464
15465
15466
15467
15468
15469
15470 static char *complete_sipch(const char *line, const char *word, int pos, int state)
15471 {
15472 int which=0;
15473 struct sip_pvt *cur;
15474 char *c = NULL;
15475 int wordlen = strlen(word);
15476 struct ao2_iterator i;
15477
15478 if (pos != 3) {
15479 return NULL;
15480 }
15481
15482 i = ao2_iterator_init(dialogs, 0);
15483 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
15484 sip_pvt_lock(cur);
15485 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
15486 c = ast_strdup(cur->callid);
15487 sip_pvt_unlock(cur);
15488 dialog_unref(cur, "drop ref in iterator loop break");
15489 break;
15490 }
15491 sip_pvt_unlock(cur);
15492 dialog_unref(cur, "drop ref in iterator loop");
15493 }
15494 ao2_iterator_destroy(&i);
15495 return c;
15496 }
15497
15498
15499
15500 static char *complete_sip_peer(const char *word, int state, int flags2)
15501 {
15502 char *result = NULL;
15503 int wordlen = strlen(word);
15504 int which = 0;
15505 struct ao2_iterator i = ao2_iterator_init(peers, 0);
15506 struct sip_peer *peer;
15507
15508 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
15509
15510 if (!strncasecmp(word, peer->name, wordlen) &&
15511 (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
15512 ++which > state)
15513 result = ast_strdup(peer->name);
15514 unref_peer(peer, "toss iterator peer ptr before break");
15515 if (result) {
15516 break;
15517 }
15518 }
15519 ao2_iterator_destroy(&i);
15520 return result;
15521 }
15522
15523
15524 static char *complete_sip_registered_peer(const char *word, int state, int flags2)
15525 {
15526 char *result = NULL;
15527 int wordlen = strlen(word);
15528 int which = 0;
15529 struct ao2_iterator i;
15530 struct sip_peer *peer;
15531
15532 i = ao2_iterator_init(peers, 0);
15533 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
15534 if (!strncasecmp(word, peer->name, wordlen) &&
15535 (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
15536 ++which > state && peer->expire > 0)
15537 result = ast_strdup(peer->name);
15538 if (result) {
15539 unref_peer(peer, "toss iterator peer ptr before break");
15540 break;
15541 }
15542 unref_peer(peer, "toss iterator peer ptr");
15543 }
15544 ao2_iterator_destroy(&i);
15545 return result;
15546 }
15547
15548
15549 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state)
15550 {
15551 if (pos == 3)
15552 return complete_sipch(line, word, pos, state);
15553
15554 return NULL;
15555 }
15556
15557
15558 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
15559 {
15560 if (pos == 3) {
15561 return complete_sip_peer(word, state, 0);
15562 }
15563
15564 return NULL;
15565 }
15566
15567
15568 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state)
15569 {
15570 if (pos == 2)
15571 return complete_sip_registered_peer(word, state, 0);
15572
15573 return NULL;
15574 }
15575
15576
15577 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
15578 {
15579 char *c = NULL;
15580
15581 if (pos == 2) {
15582 int which = 0;
15583 char *cat = NULL;
15584 int wordlen = strlen(word);
15585
15586
15587
15588 if (!notify_types)
15589 return NULL;
15590
15591 while ( (cat = ast_category_browse(notify_types, cat)) ) {
15592 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
15593 c = ast_strdup(cat);
15594 break;
15595 }
15596 }
15597 return c;
15598 }
15599
15600 if (pos > 2)
15601 return complete_sip_peer(word, state, 0);
15602
15603 return NULL;
15604 }
15605
15606
15607 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15608 {
15609 struct sip_pvt *cur;
15610 size_t len;
15611 int found = 0;
15612 struct ao2_iterator i;
15613
15614 switch (cmd) {
15615 case CLI_INIT:
15616 e->command = "sip show channel";
15617 e->usage =
15618 "Usage: sip show channel <call-id>\n"
15619 " Provides detailed status on a given SIP dialog (identified by SIP call-id).\n";
15620 return NULL;
15621 case CLI_GENERATE:
15622 return complete_sipch(a->line, a->word, a->pos, a->n);
15623 }
15624
15625 if (a->argc != 4)
15626 return CLI_SHOWUSAGE;
15627 len = strlen(a->argv[3]);
15628
15629 i = ao2_iterator_init(dialogs, 0);
15630 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
15631 sip_pvt_lock(cur);
15632
15633 if (!strncasecmp(cur->callid, a->argv[3], len)) {
15634 char formatbuf[SIPBUFSIZE/2];
15635 ast_cli(a->fd, "\n");
15636 if (cur->subscribed != NONE)
15637 ast_cli(a->fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
15638 else
15639 ast_cli(a->fd, " * SIP Call\n");
15640 ast_cli(a->fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
15641 ast_cli(a->fd, " Call-ID: %s\n", cur->callid);
15642 ast_cli(a->fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
15643 ast_cli(a->fd, " Our Codec Capability: %d\n", cur->capability);
15644 ast_cli(a->fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
15645 ast_cli(a->fd, " Their Codec Capability: %d\n", cur->peercapability);
15646 ast_cli(a->fd, " Joint Codec Capability: %d\n", cur->jointcapability);
15647 ast_cli(a->fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
15648 ast_cli(a->fd, " T.38 support %s\n", cli_yesno(cur->udptl != NULL));
15649 ast_cli(a->fd, " Video support %s\n", cli_yesno(cur->vrtp != NULL));
15650 ast_cli(a->fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
15651 ast_cli(a->fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(cur->sa.sin_addr), ntohs(cur->sa.sin_port));
15652 ast_cli(a->fd, " Received Address: %s:%d\n", ast_inet_ntoa(cur->recv.sin_addr), ntohs(cur->recv.sin_port));
15653 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
15654 ast_cli(a->fd, " NAT Support: %s\n", nat2str(ast_test_flag(&cur->flags[0], SIP_NAT)));
15655 ast_cli(a->fd, " Audio IP: %s %s\n", ast_inet_ntoa(cur->redirip.sin_addr.s_addr ? cur->redirip.sin_addr : cur->ourip.sin_addr), cur->redirip.sin_addr.s_addr ? "(Outside bridge)" : "(local)" );
15656 ast_cli(a->fd, " Our Tag: %s\n", cur->tag);
15657 ast_cli(a->fd, " Their Tag: %s\n", cur->theirtag);
15658 ast_cli(a->fd, " SIP User agent: %s\n", cur->useragent);
15659 if (!ast_strlen_zero(cur->username))
15660 ast_cli(a->fd, " Username: %s\n", cur->username);
15661 if (!ast_strlen_zero(cur->peername))
15662 ast_cli(a->fd, " Peername: %s\n", cur->peername);
15663 if (!ast_strlen_zero(cur->uri))
15664 ast_cli(a->fd, " Original uri: %s\n", cur->uri);
15665 if (!ast_strlen_zero(cur->cid_num))
15666 ast_cli(a->fd, " Caller-ID: %s\n", cur->cid_num);
15667 ast_cli(a->fd, " Need Destroy: %s\n", cli_yesno(cur->needdestroy));
15668 ast_cli(a->fd, " Last Message: %s\n", cur->lastmsg);
15669 ast_cli(a->fd, " Promiscuous Redir: %s\n", cli_yesno(ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR)));
15670 ast_cli(a->fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
15671 ast_cli(a->fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
15672 ast_cli(a->fd, " SIP Options: ");
15673 if (cur->sipoptions) {
15674 int x;
15675 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
15676 if (cur->sipoptions & sip_options[x].id)
15677 ast_cli(a->fd, "%s ", sip_options[x].text);
15678 }
15679 ast_cli(a->fd, "\n");
15680 } else
15681 ast_cli(a->fd, "(none)\n");
15682
15683 if (!cur->stimer)
15684 ast_cli(a->fd, " Session-Timer: Uninitiallized\n");
15685 else {
15686 ast_cli(a->fd, " Session-Timer: %s\n", cur->stimer->st_active ? "Active" : "Inactive");
15687 if (cur->stimer->st_active == TRUE) {
15688 ast_cli(a->fd, " S-Timer Interval: %d\n", cur->stimer->st_interval);
15689 ast_cli(a->fd, " S-Timer Refresher: %s\n", strefresher2str(cur->stimer->st_ref));
15690 ast_cli(a->fd, " S-Timer Expirys: %d\n", cur->stimer->st_expirys);
15691 ast_cli(a->fd, " S-Timer Sched Id: %d\n", cur->stimer->st_schedid);
15692 ast_cli(a->fd, " S-Timer Peer Sts: %s\n", cur->stimer->st_active_peer_ua ? "Active" : "Inactive");
15693 ast_cli(a->fd, " S-Timer Cached Min-SE: %d\n", cur->stimer->st_cached_min_se);
15694 ast_cli(a->fd, " S-Timer Cached SE: %d\n", cur->stimer->st_cached_max_se);
15695 ast_cli(a->fd, " S-Timer Cached Ref: %s\n", strefresher2str(cur->stimer->st_cached_ref));
15696 ast_cli(a->fd, " S-Timer Cached Mode: %s\n", stmode2str(cur->stimer->st_cached_mode));
15697 }
15698 }
15699
15700 ast_cli(a->fd, "\n\n");
15701
15702 found++;
15703 }
15704
15705 sip_pvt_unlock(cur);
15706
15707 ao2_t_ref(cur, -1, "toss dialog ptr set by iterator_next");
15708 }
15709 ao2_iterator_destroy(&i);
15710
15711 if (!found)
15712 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
15713
15714 return CLI_SUCCESS;
15715 }
15716
15717
15718 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15719 {
15720 struct sip_pvt *cur;
15721 size_t len;
15722 int found = 0;
15723 struct ao2_iterator i;
15724
15725 switch (cmd) {
15726 case CLI_INIT:
15727 e->command = "sip show history";
15728 e->usage =
15729 "Usage: sip show history <call-id>\n"
15730 " Provides detailed dialog history on a given SIP call (specified by call-id).\n";
15731 return NULL;
15732 case CLI_GENERATE:
15733 return complete_sip_show_history(a->line, a->word, a->pos, a->n);
15734 }
15735
15736 if (a->argc != 4)
15737 return CLI_SHOWUSAGE;
15738
15739 if (!recordhistory)
15740 ast_cli(a->fd, "\n***Note: History recording is currently DISABLED. Use 'sip set history on' to ENABLE.\n");
15741
15742 len = strlen(a->argv[3]);
15743
15744 i = ao2_iterator_init(dialogs, 0);
15745 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
15746 sip_pvt_lock(cur);
15747 if (!strncasecmp(cur->callid, a->argv[3], len)) {
15748 struct sip_history *hist;
15749 int x = 0;
15750
15751 ast_cli(a->fd, "\n");
15752 if (cur->subscribed != NONE)
15753 ast_cli(a->fd, " * Subscription\n");
15754 else
15755 ast_cli(a->fd, " * SIP Call\n");
15756 if (cur->history)
15757 AST_LIST_TRAVERSE(cur->history, hist, list)
15758 ast_cli(a->fd, "%d. %s\n", ++x, hist->event);
15759 if (x == 0)
15760 ast_cli(a->fd, "Call '%s' has no history\n", cur->callid);
15761 found++;
15762 }
15763 sip_pvt_unlock(cur);
15764 ao2_t_ref(cur, -1, "toss dialog ptr from iterator_next");
15765 }
15766 ao2_iterator_destroy(&i);
15767
15768 if (!found)
15769 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
15770
15771 return CLI_SUCCESS;
15772 }
15773
15774
15775 static void sip_dump_history(struct sip_pvt *dialog)
15776 {
15777 int x = 0;
15778 struct sip_history *hist;
15779 static int errmsg = 0;
15780
15781 if (!dialog)
15782 return;
15783
15784 if (!option_debug && !sipdebug) {
15785 if (!errmsg) {
15786 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
15787 errmsg = 1;
15788 }
15789 return;
15790 }
15791
15792 ast_debug(1, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
15793 if (dialog->subscribed)
15794 ast_debug(1, " * Subscription\n");
15795 else
15796 ast_debug(1, " * SIP Call\n");
15797 if (dialog->history)
15798 AST_LIST_TRAVERSE(dialog->history, hist, list)
15799 ast_debug(1, " %-3.3d. %s\n", ++x, hist->event);
15800 if (!x)
15801 ast_debug(1, "Call '%s' has no history\n", dialog->callid);
15802 ast_debug(1, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
15803 }
15804
15805
15806
15807 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
15808 {
15809 char buf[1024];
15810 unsigned int event;
15811 const char *c = get_header(req, "Content-Type");
15812
15813
15814 if (!strcasecmp(c, "application/dtmf-relay") ||
15815 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
15816 unsigned int duration = 0;
15817
15818 if (!p->owner) {
15819 transmit_response(p, "481 Call leg/transaction does not exist", req);
15820 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15821 return;
15822 }
15823
15824
15825 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
15826 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
15827 transmit_response(p, "200 OK", req);
15828 return;
15829 } else {
15830 ast_copy_string(buf, c, sizeof(buf));
15831 }
15832
15833 if (!ast_strlen_zero((c = get_body(req, "Duration"))))
15834 duration = atoi(c);
15835 if (!duration)
15836 duration = 100;
15837
15838
15839 if (ast_strlen_zero(buf)) {
15840 transmit_response(p, "200 OK", req);
15841 return;
15842 }
15843
15844 if (buf[0] == '*')
15845 event = 10;
15846 else if (buf[0] == '#')
15847 event = 11;
15848 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
15849 event = 12 + buf[0] - 'A';
15850 else if (buf[0] == '!')
15851 event = 16;
15852 else
15853 event = atoi(buf);
15854 if (event == 16) {
15855
15856 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
15857 ast_queue_frame(p->owner, &f);
15858 if (sipdebug)
15859 ast_verbose("* DTMF-relay event received: FLASH\n");
15860 } else {
15861
15862 struct ast_frame f = { AST_FRAME_DTMF, };
15863 if (event < 10) {
15864 f.subclass = '0' + event;
15865 } else if (event < 11) {
15866 f.subclass = '*';
15867 } else if (event < 12) {
15868 f.subclass = '#';
15869 } else if (event < 16) {
15870 f.subclass = 'A' + (event - 12);
15871 }
15872 f.len = duration;
15873 ast_queue_frame(p->owner, &f);
15874 if (sipdebug)
15875 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
15876 }
15877 transmit_response(p, "200 OK", req);
15878 return;
15879 } else if (!strcasecmp(c, "application/dtmf")) {
15880
15881 unsigned int duration = 0;
15882
15883 if (!p->owner) {
15884 transmit_response(p, "481 Call leg/transaction does not exist", req);
15885 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15886 return;
15887 }
15888
15889 get_msg_text(buf, sizeof(buf), req, TRUE);
15890 duration = 100;
15891
15892 if (ast_strlen_zero(buf)) {
15893 transmit_response(p, "200 OK", req);
15894 return;
15895 }
15896 event = atoi(buf);
15897 if (event == 16) {
15898
15899 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
15900 ast_queue_frame(p->owner, &f);
15901 if (sipdebug)
15902 ast_verbose("* DTMF-relay event received: FLASH\n");
15903 } else {
15904
15905 struct ast_frame f = { AST_FRAME_DTMF, };
15906 if (event < 10) {
15907 f.subclass = '0' + event;
15908 } else if (event < 11) {
15909 f.subclass = '*';
15910 } else if (event < 12) {
15911 f.subclass = '#';
15912 } else if (event < 16) {
15913 f.subclass = 'A' + (event - 12);
15914 }
15915 f.len = duration;
15916 ast_queue_frame(p->owner, &f);
15917 if (sipdebug)
15918 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
15919 }
15920 transmit_response(p, "200 OK", req);
15921 return;
15922
15923 } else if (!strcasecmp(c, "application/media_control+xml")) {
15924
15925 if (p->owner)
15926 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
15927 transmit_response(p, "200 OK", req);
15928 return;
15929 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
15930
15931 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
15932 if (p->owner && p->owner->cdr)
15933 ast_cdr_setuserfield(p->owner, c);
15934 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
15935 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
15936 transmit_response(p, "200 OK", req);
15937 } else {
15938 transmit_response(p, "403 Forbidden", req);
15939 }
15940 return;
15941 } else if (!ast_strlen_zero(c = get_header(req, "Record"))) {
15942
15943
15944
15945
15946
15947
15948
15949 struct ast_call_feature *feat;
15950 int j;
15951 struct ast_frame f = { AST_FRAME_DTMF, };
15952
15953 ast_rdlock_call_features();
15954 feat = ast_find_call_feature("automon");
15955 if (!feat || ast_strlen_zero(feat->exten)) {
15956 ast_log(LOG_WARNING, "Recording requested, but no One Touch Monitor registered. (See features.conf)\n");
15957
15958 transmit_response(p, "403 Forbidden", req);
15959 ast_unlock_call_features();
15960 return;
15961 }
15962
15963 f.len = 100;
15964 for (j=0; j < strlen(feat->exten); j++) {
15965 f.subclass = feat->exten[j];
15966 ast_queue_frame(p->owner, &f);
15967 if (sipdebug)
15968 ast_verbose("* DTMF-relay event faked: %c\n", f.subclass);
15969 }
15970 ast_unlock_call_features();
15971
15972 ast_debug(1, "Got a Request to Record the channel, state %s\n", c);
15973 transmit_response(p, "200 OK", req);
15974 return;
15975 } else if (ast_strlen_zero(c = get_header(req, "Content-Length")) || !strcasecmp(c, "0")) {
15976
15977 transmit_response(p, "200 OK", req);
15978 return;
15979 }
15980
15981
15982
15983
15984 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
15985 transmit_response(p, "415 Unsupported media type", req);
15986 return;
15987 }
15988
15989
15990 static char *sip_do_debug_ip(int fd, char *arg)
15991 {
15992 struct hostent *hp;
15993 struct ast_hostent ahp;
15994 int port = 0;
15995 char *p;
15996
15997 p = arg;
15998 strsep(&p, ":");
15999 if (p)
16000 port = atoi(p);
16001 hp = ast_gethostbyname(arg, &ahp);
16002 if (hp == NULL)
16003 return CLI_SHOWUSAGE;
16004
16005 debugaddr.sin_family = AF_INET;
16006 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
16007 debugaddr.sin_port = htons(port);
16008 if (port == 0)
16009 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(debugaddr.sin_addr));
16010 else
16011 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), port);
16012
16013 sipdebug |= sip_debug_console;
16014
16015 return CLI_SUCCESS;
16016 }
16017
16018
16019 static char *sip_do_debug_peer(int fd, char *arg)
16020 {
16021 struct sip_peer *peer = find_peer(arg, NULL, TRUE, FINDPEERS, FALSE, 0);
16022 if (!peer)
16023 ast_cli(fd, "No such peer '%s'\n", arg);
16024 else if (peer->addr.sin_addr.s_addr == 0)
16025 ast_cli(fd, "Unable to get IP address of peer '%s'\n", arg);
16026 else {
16027 debugaddr.sin_family = AF_INET;
16028 debugaddr.sin_addr = peer->addr.sin_addr;
16029 debugaddr.sin_port = peer->addr.sin_port;
16030 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n",
16031 ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
16032 sipdebug |= sip_debug_console;
16033 }
16034 if (peer)
16035 unref_peer(peer, "sip_do_debug_peer: unref_peer, from find_peer call");
16036 return CLI_SUCCESS;
16037 }
16038
16039
16040 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16041 {
16042 int oldsipdebug = sipdebug & sip_debug_console;
16043 char *what;
16044
16045 if (cmd == CLI_INIT) {
16046 e->command = "sip set debug {on|off|ip|peer}";
16047 e->usage =
16048 "Usage: sip set debug {off|on|ip addr[:port]|peer peername}\n"
16049 " Globally disables dumping of SIP packets,\n"
16050 " or enables it either globally or for a (single)\n"
16051 " IP address or registered peer.\n";
16052 return NULL;
16053 } else if (cmd == CLI_GENERATE) {
16054 if (a->pos == 4 && strcasestr(a->line, " peer"))
16055 return complete_sip_peer(a->word, a->n, 0);
16056 return NULL;
16057 }
16058
16059 what = a->argv[e->args-1];
16060 if (a->argc == e->args) {
16061 if (!strcasecmp(what, "on")) {
16062 sipdebug |= sip_debug_console;
16063 sipdebug_text = 1;
16064 memset(&debugaddr, 0, sizeof(debugaddr));
16065 ast_cli(a->fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
16066 return CLI_SUCCESS;
16067 } else if (!strcasecmp(what, "off")) {
16068 sipdebug &= ~sip_debug_console;
16069 sipdebug_text = 0;
16070 ast_cli(a->fd, "SIP Debugging Disabled\n");
16071 return CLI_SUCCESS;
16072 }
16073 } else if (a->argc == e->args +1) {
16074 if (!strcasecmp(what, "ip"))
16075 return sip_do_debug_ip(a->fd, a->argv[e->args]);
16076 else if (!strcasecmp(what, "peer"))
16077 return sip_do_debug_peer(a->fd, a->argv[e->args]);
16078 }
16079 return CLI_SHOWUSAGE;
16080 }
16081
16082
16083 static char *sip_cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16084 {
16085 struct ast_variable *varlist;
16086 int i;
16087
16088 switch (cmd) {
16089 case CLI_INIT:
16090 e->command = "sip notify";
16091 e->usage =
16092 "Usage: sip notify <type> <peer> [<peer>...]\n"
16093 " Send a NOTIFY message to a SIP peer or peers\n"
16094 " Message types are defined in sip_notify.conf\n";
16095 return NULL;
16096 case CLI_GENERATE:
16097 return complete_sipnotify(a->line, a->word, a->pos, a->n);
16098 }
16099
16100 if (a->argc < 4)
16101 return CLI_SHOWUSAGE;
16102
16103 if (!notify_types) {
16104 ast_cli(a->fd, "No %s file found, or no types listed there\n", notify_config);
16105 return CLI_FAILURE;
16106 }
16107
16108 varlist = ast_variable_browse(notify_types, a->argv[2]);
16109
16110 if (!varlist) {
16111 ast_cli(a->fd, "Unable to find notify type '%s'\n", a->argv[2]);
16112 return CLI_FAILURE;
16113 }
16114
16115 for (i = 3; i < a->argc; i++) {
16116 struct sip_pvt *p;
16117
16118 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
16119 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
16120 return CLI_FAILURE;
16121 }
16122
16123 if (create_addr(p, a->argv[i], NULL, 1)) {
16124
16125 dialog_unlink_all(p, TRUE, TRUE);
16126 dialog_unref(p, "unref dialog inside for loop" );
16127
16128 ast_cli(a->fd, "Could not create address for '%s'\n", a->argv[i]);
16129 continue;
16130 }
16131
16132
16133 ast_set_flag(&p->flags[0], SIP_OUTGOING);
16134
16135
16136 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
16137 build_via(p);
16138 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
16139 build_callid_pvt(p);
16140 ao2_t_link(dialogs, p, "Linking in new name");
16141 ast_cli(a->fd, "Sending NOTIFY of type '%s' to '%s'\n", a->argv[2], a->argv[i]);
16142 dialog_ref(p, "bump the count of p, which transmit_sip_request will decrement.");
16143 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
16144 transmit_notify_custom(p, varlist);
16145 }
16146
16147 return CLI_SUCCESS;
16148 }
16149
16150
16151 static char *sip_do_history_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16152 {
16153 switch (cmd) {
16154 case CLI_INIT:
16155 e->command = "sip history [off]";
16156 e->usage =
16157 "Usage: sip history [off]\n"
16158 " Enables/Disables recording of SIP dialog history for debugging purposes.\n"
16159 " Use 'sip show history' to view the history of a call number.\n";
16160 return NULL;
16161 case CLI_GENERATE:
16162 return NULL;
16163 }
16164
16165 if (a->argc < 2 || a->argc > 3) {
16166 return CLI_SHOWUSAGE;
16167 }
16168 if (a->argc == 2) {
16169 recordhistory = TRUE;
16170 ast_cli(a->fd, "SIP History Recording Enabled (use 'sip show history')\n");
16171 } else {
16172 if (strncasecmp(a->argv[2], "off", 3))
16173 return CLI_SHOWUSAGE;
16174 recordhistory = FALSE;
16175 ast_cli(a->fd, "SIP History Recording Disabled\n");
16176 }
16177 return CLI_SUCCESS;
16178 }
16179
16180
16181 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16182 {
16183 switch (cmd) {
16184 case CLI_INIT:
16185 e->command = "sip set history {on|off}";
16186 e->usage =
16187 "Usage: sip set history {on|off}\n"
16188 " Enables/Disables recording of SIP dialog history for debugging purposes.\n"
16189 " Use 'sip show history' to view the history of a call number.\n";
16190 return NULL;
16191 case CLI_GENERATE:
16192 return NULL;
16193 }
16194
16195 if (a->argc != e->args)
16196 return CLI_SHOWUSAGE;
16197
16198 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
16199 recordhistory = TRUE;
16200 ast_cli(a->fd, "SIP History Recording Enabled (use 'sip show history')\n");
16201 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
16202 recordhistory = FALSE;
16203 ast_cli(a->fd, "SIP History Recording Disabled\n");
16204 } else {
16205 return CLI_SHOWUSAGE;
16206 }
16207 return CLI_SUCCESS;
16208 }
16209
16210
16211 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code)
16212 {
16213 char *header, *respheader;
16214 char digest[1024];
16215
16216 p->authtries++;
16217 auth_headers(code, &header, &respheader);
16218 memset(digest, 0, sizeof(digest));
16219 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
16220
16221
16222 if (sip_debug_test_pvt(p) && p->registry)
16223 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
16224
16225 return -1;
16226 }
16227 if (p->do_history)
16228 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
16229 if (sip_debug_test_pvt(p) && p->registry)
16230 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
16231 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
16232 }
16233
16234
16235 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code, int sipmethod, int init)
16236 {
16237 char *header, *respheader;
16238 char digest[1024];
16239
16240 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
16241 return -2;
16242
16243 p->authtries++;
16244 auth_headers(code, &header, &respheader);
16245 ast_debug(2, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
16246 memset(digest, 0, sizeof(digest));
16247 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
16248
16249 return -1;
16250 }
16251
16252 p->options->auth = digest;
16253 p->options->authheader = respheader;
16254 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
16255 }
16256
16257
16258
16259
16260
16261 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
16262 {
16263 char tmp[512];
16264 char *c;
16265 char oldnonce[256];
16266
16267
16268 const struct x {
16269 const char *key;
16270 const ast_string_field *field;
16271 } *i, keys[] = {
16272 { "realm=", &p->realm },
16273 { "nonce=", &p->nonce },
16274 { "opaque=", &p->opaque },
16275 { "qop=", &p->qop },
16276 { "domain=", &p->domain },
16277 { NULL, 0 },
16278 };
16279
16280 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
16281 if (ast_strlen_zero(tmp))
16282 return -1;
16283 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
16284 ast_log(LOG_WARNING, "missing Digest.\n");
16285 return -1;
16286 }
16287 c = tmp + strlen("Digest ");
16288 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
16289 while (c && *(c = ast_skip_blanks(c))) {
16290 for (i = keys; i->key != NULL; i++) {
16291 char *src, *separator;
16292 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
16293 continue;
16294
16295 c += strlen(i->key);
16296 if (*c == '"') {
16297 src = ++c;
16298 separator = "\"";
16299 } else {
16300 src = c;
16301 separator = ",";
16302 }
16303 strsep(&c, separator);
16304 ast_string_field_ptr_set(p, i->field, src);
16305 break;
16306 }
16307 if (i->key == NULL)
16308 strsep(&c, ",");
16309 }
16310
16311 if (strcmp(p->nonce, oldnonce))
16312 p->noncecount = 0;
16313
16314
16315 if (p->registry) {
16316 struct sip_registry *r = p->registry;
16317
16318 if (strcmp(r->nonce, p->nonce)) {
16319 ast_string_field_set(r, realm, p->realm);
16320 ast_string_field_set(r, nonce, p->nonce);
16321 ast_string_field_set(r, domain, p->domain);
16322 ast_string_field_set(r, opaque, p->opaque);
16323 ast_string_field_set(r, qop, p->qop);
16324 r->noncecount = 0;
16325 }
16326 }
16327 return build_reply_digest(p, sipmethod, digest, digest_len);
16328 }
16329
16330
16331
16332
16333
16334
16335 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
16336 {
16337 char a1[256];
16338 char a2[256];
16339 char a1_hash[256];
16340 char a2_hash[256];
16341 char resp[256];
16342 char resp_hash[256];
16343 char uri[256];
16344 char opaque[256] = "";
16345 char cnonce[80];
16346 const char *username;
16347 const char *secret;
16348 const char *md5secret;
16349 struct sip_auth *auth = NULL;
16350
16351 if (!ast_strlen_zero(p->domain))
16352 ast_copy_string(uri, p->domain, sizeof(uri));
16353 else if (!ast_strlen_zero(p->uri))
16354 ast_copy_string(uri, p->uri, sizeof(uri));
16355 else
16356 snprintf(uri, sizeof(uri), "sip:%s@%s", p->username, ast_inet_ntoa(p->sa.sin_addr));
16357
16358 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
16359
16360
16361 if(!(auth = find_realm_authentication(p->peerauth, p->realm)))
16362 auth = find_realm_authentication(authl, p->realm);
16363
16364 if (auth) {
16365 ast_log(LOG_DEBUG, "use realm [%s] from peer [%s][%s]\n", auth->username, p->peername, p->username);
16366 username = auth->username;
16367 secret = auth->secret;
16368 md5secret = auth->md5secret;
16369 if (sipdebug)
16370 ast_debug(1, "Using realm %s authentication for call %s\n", p->realm, p->callid);
16371 } else {
16372
16373 username = p->authname;
16374 secret = p->peersecret;
16375 md5secret = p->peermd5secret;
16376 }
16377 if (ast_strlen_zero(username))
16378 return -1;
16379
16380
16381 snprintf(a1, sizeof(a1), "%s:%s:%s", username, p->realm, secret);
16382 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[method].text, uri);
16383 if (!ast_strlen_zero(md5secret))
16384 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
16385 else
16386 ast_md5_hash(a1_hash, a1);
16387 ast_md5_hash(a2_hash, a2);
16388
16389 p->noncecount++;
16390 if (!ast_strlen_zero(p->qop))
16391 snprintf(resp, sizeof(resp), "%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
16392 else
16393 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, p->nonce, a2_hash);
16394 ast_md5_hash(resp_hash, resp);
16395
16396
16397 if (!ast_strlen_zero(p->opaque)) {
16398 snprintf(opaque, sizeof(opaque), ", opaque=\"%s\"", p->opaque);
16399 }
16400
16401
16402 if (!ast_strlen_zero(p->qop))
16403 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);
16404 else
16405 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);
16406
16407 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
16408
16409 return 0;
16410 }
16411
16412
16413 static int func_header_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
16414 {
16415 struct sip_pvt *p;
16416 const char *content = NULL;
16417 AST_DECLARE_APP_ARGS(args,
16418 AST_APP_ARG(header);
16419 AST_APP_ARG(number);
16420 );
16421 int i, number, start = 0;
16422
16423 if (ast_strlen_zero(data)) {
16424 ast_log(LOG_WARNING, "This function requires a header name.\n");
16425 return -1;
16426 }
16427
16428 ast_channel_lock(chan);
16429 if (!IS_SIP_TECH(chan->tech)) {
16430 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
16431 ast_channel_unlock(chan);
16432 return -1;
16433 }
16434
16435 AST_STANDARD_APP_ARGS(args, data);
16436 if (!args.number) {
16437 number = 1;
16438 } else {
16439 sscanf(args.number, "%30d", &number);
16440 if (number < 1)
16441 number = 1;
16442 }
16443
16444 p = chan->tech_pvt;
16445
16446
16447 if (!p) {
16448 ast_channel_unlock(chan);
16449 return -1;
16450 }
16451
16452 for (i = 0; i < number; i++)
16453 content = __get_header(&p->initreq, args.header, &start);
16454
16455 if (ast_strlen_zero(content)) {
16456 ast_channel_unlock(chan);
16457 return -1;
16458 }
16459
16460 ast_copy_string(buf, content, len);
16461 ast_channel_unlock(chan);
16462
16463 return 0;
16464 }
16465
16466 static struct ast_custom_function sip_header_function = {
16467 .name = "SIP_HEADER",
16468 .synopsis = "Gets the specified SIP header",
16469 .syntax = "SIP_HEADER(<name>[,<number>])",
16470 .desc = "Since there are several headers (such as Via) which can occur multiple\n"
16471 "times, SIP_HEADER takes an optional second argument to specify which header with\n"
16472 "that name to retrieve. Headers start at offset 1.\n",
16473 .read = func_header_read,
16474 };
16475
16476
16477 static int func_check_sipdomain(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
16478 {
16479 if (ast_strlen_zero(data)) {
16480 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
16481 return -1;
16482 }
16483 if (check_sip_domain(data, NULL, 0))
16484 ast_copy_string(buf, data, len);
16485 else
16486 buf[0] = '\0';
16487 return 0;
16488 }
16489
16490 static struct ast_custom_function checksipdomain_function = {
16491 .name = "CHECKSIPDOMAIN",
16492 .synopsis = "Checks if domain is a local domain",
16493 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
16494 .read = func_check_sipdomain,
16495 .desc = "This function checks if the domain in the argument is configured\n"
16496 "as a local SIP domain that this Asterisk server is configured to handle.\n"
16497 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
16498 "Check the domain= configuration in sip.conf\n",
16499 };
16500
16501
16502 static int function_sippeer(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
16503 {
16504 struct sip_peer *peer;
16505 char *colname;
16506
16507 if ((colname = strchr(data, ':'))) {
16508 static int deprecation_warning = 0;
16509 *colname++ = '\0';
16510 if (deprecation_warning++ % 10 == 0)
16511 ast_log(LOG_WARNING, "SIPPEER(): usage of ':' to separate arguments is deprecated. Please use ',' instead.\n");
16512 } else if ((colname = strchr(data, ',')))
16513 *colname++ = '\0';
16514 else
16515 colname = "ip";
16516
16517 if (!(peer = find_peer(data, NULL, TRUE, FINDPEERS, FALSE, 0)))
16518 return -1;
16519
16520 if (!strcasecmp(colname, "ip")) {
16521 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
16522 } else if (!strcasecmp(colname, "port")) {
16523 snprintf(buf, len, "%d", ntohs(peer->addr.sin_port));
16524 } else if (!strcasecmp(colname, "status")) {
16525 peer_status(peer, buf, len);
16526 } else if (!strcasecmp(colname, "language")) {
16527 ast_copy_string(buf, peer->language, len);
16528 } else if (!strcasecmp(colname, "regexten")) {
16529 ast_copy_string(buf, peer->regexten, len);
16530 } else if (!strcasecmp(colname, "limit")) {
16531 snprintf(buf, len, "%d", peer->call_limit);
16532 } else if (!strcasecmp(colname, "busylevel")) {
16533 snprintf(buf, len, "%d", peer->busy_level);
16534 } else if (!strcasecmp(colname, "curcalls")) {
16535 snprintf(buf, len, "%d", peer->inUse);
16536 } else if (!strcasecmp(colname, "accountcode")) {
16537 ast_copy_string(buf, peer->accountcode, len);
16538 } else if (!strcasecmp(colname, "callgroup")) {
16539 ast_print_group(buf, len, peer->callgroup);
16540 } else if (!strcasecmp(colname, "pickupgroup")) {
16541 ast_print_group(buf, len, peer->pickupgroup);
16542 } else if (!strcasecmp(colname, "useragent")) {
16543 ast_copy_string(buf, peer->useragent, len);
16544 } else if (!strcasecmp(colname, "mailbox")) {
16545 struct ast_str *mailbox_str = ast_str_alloca(512);
16546 peer_mailboxes_to_str(&mailbox_str, peer);
16547 ast_copy_string(buf, mailbox_str->str, len);
16548 } else if (!strcasecmp(colname, "context")) {
16549 ast_copy_string(buf, peer->context, len);
16550 } else if (!strcasecmp(colname, "expire")) {
16551 snprintf(buf, len, "%d", peer->expire);
16552 } else if (!strcasecmp(colname, "dynamic")) {
16553 ast_copy_string(buf, peer->host_dynamic ? "yes" : "no", len);
16554 } else if (!strcasecmp(colname, "callerid_name")) {
16555 ast_copy_string(buf, peer->cid_name, len);
16556 } else if (!strcasecmp(colname, "callerid_num")) {
16557 ast_copy_string(buf, peer->cid_num, len);
16558 } else if (!strcasecmp(colname, "codecs")) {
16559 ast_getformatname_multiple(buf, len -1, peer->capability);
16560 } else if (!strncasecmp(colname, "chanvar[", 8)) {
16561 char *chanvar=colname + 8;
16562 struct ast_variable *v;
16563
16564 chanvar = strsep(&chanvar, "]");
16565 for (v = peer->chanvars ; v ; v = v->next)
16566 if (!strcasecmp(v->name, chanvar))
16567 ast_copy_string(buf, v->value, len);
16568 } else if (!strncasecmp(colname, "codec[", 6)) {
16569 char *codecnum;
16570 int codec = 0;
16571
16572 codecnum = colname + 6;
16573 codecnum = strsep(&codecnum, "]");
16574 if((codec = ast_codec_pref_index(&peer->prefs, atoi(codecnum)))) {
16575 ast_copy_string(buf, ast_getformatname(codec), len);
16576 } else {
16577 buf[0] = '\0';
16578 }
16579 } else {
16580 buf[0] = '\0';
16581 }
16582
16583 unref_peer(peer, "unref_peer from function_sippeer, just before return");
16584
16585 return 0;
16586 }
16587
16588
16589 struct ast_custom_function sippeer_function = {
16590 .name = "SIPPEER",
16591 .synopsis = "Gets SIP peer information",
16592 .syntax = "SIPPEER(<peername>[,item])",
16593 .read = function_sippeer,
16594 .desc = "Valid items are:\n"
16595 "- ip (default) The IP address.\n"
16596 "- port The port number\n"
16597 "- mailbox The configured mailbox.\n"
16598 "- context The configured context.\n"
16599 "- expire The epoch time of the next expire.\n"
16600 "- dynamic Is it dynamic? (yes/no).\n"
16601 "- callerid_name The configured Caller ID name.\n"
16602 "- callerid_num The configured Caller ID number.\n"
16603 "- callgroup The configured Callgroup.\n"
16604 "- pickupgroup The configured Pickupgroup.\n"
16605 "- codecs The configured codecs.\n"
16606 "- status Status (if qualify=yes).\n"
16607 "- regexten Registration extension\n"
16608 "- limit Call limit (call-limit)\n"
16609 "- busylevel Configured call level for signalling busy\n"
16610 "- curcalls Current amount of calls \n"
16611 " Only available if call-limit is set\n"
16612 "- language Default language for peer\n"
16613 "- accountcode Account code for this peer\n"
16614 "- useragent Current user agent id for peer\n"
16615 "- chanvar[name] A channel variable configured with setvar for this peer.\n"
16616 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
16617 "\n"
16618 };
16619
16620
16621 static int function_sipchaninfo_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
16622 {
16623 struct sip_pvt *p;
16624 static int deprecated = 0;
16625
16626 *buf = 0;
16627
16628 if (!data) {
16629 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
16630 return -1;
16631 }
16632
16633 ast_channel_lock(chan);
16634 if (!IS_SIP_TECH(chan->tech)) {
16635 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
16636 ast_channel_unlock(chan);
16637 return -1;
16638 }
16639
16640 if (deprecated++ % 20 == 0) {
16641
16642 ast_log(LOG_WARNING, "SIPCHANINFO() is deprecated. Please transition to using CHANNEL().\n");
16643 }
16644
16645 p = chan->tech_pvt;
16646
16647
16648 if (!p) {
16649 ast_channel_unlock(chan);
16650 return -1;
16651 }
16652
16653 if (!strcasecmp(data, "peerip")) {
16654 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", len);
16655 } else if (!strcasecmp(data, "recvip")) {
16656 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", len);
16657 } else if (!strcasecmp(data, "from")) {
16658 ast_copy_string(buf, p->from, len);
16659 } else if (!strcasecmp(data, "uri")) {
16660 ast_copy_string(buf, p->uri, len);
16661 } else if (!strcasecmp(data, "useragent")) {
16662 ast_copy_string(buf, p->useragent, len);
16663 } else if (!strcasecmp(data, "peername")) {
16664 ast_copy_string(buf, p->peername, len);
16665 } else if (!strcasecmp(data, "t38passthrough")) {
16666 if (p->t38.state == T38_DISABLED) {
16667 ast_copy_string(buf, "0", len);
16668 } else {
16669 ast_copy_string(buf, "1", len);
16670 }
16671 } else {
16672 ast_channel_unlock(chan);
16673 return -1;
16674 }
16675 ast_channel_unlock(chan);
16676
16677 return 0;
16678 }
16679
16680
16681 static struct ast_custom_function sipchaninfo_function = {
16682 .name = "SIPCHANINFO",
16683 .synopsis = "Gets the specified SIP parameter from the current channel",
16684 .syntax = "SIPCHANINFO(item)",
16685 .read = function_sipchaninfo_read,
16686 .desc = "Valid items are:\n"
16687 "- peerip The IP address of the peer.\n"
16688 "- recvip The source IP address of the peer.\n"
16689 "- from The URI from the From: header.\n"
16690 "- uri The URI from the Contact: header.\n"
16691 "- useragent The useragent.\n"
16692 "- peername The name of the peer.\n"
16693 "- t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
16694 };
16695
16696
16697 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
16698 {
16699 char tmp[SIPBUFSIZE];
16700 char *s, *e, *t, *trans;
16701 char *domain;
16702 enum sip_transport transport = SIP_TRANSPORT_UDP;
16703
16704 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
16705 if ((t = strchr(tmp, ',')))
16706 *t = '\0';
16707
16708 s = get_in_brackets(tmp);
16709 if ((trans = strcasestr(s, ";transport="))) do {
16710 trans += 11;
16711
16712 if ((e = strchr(trans, ';')))
16713 *e = '\0';
16714
16715 if (!strncasecmp(trans, "tcp", 3))
16716 transport = SIP_TRANSPORT_TCP;
16717 else if (!strncasecmp(trans, "tls", 3))
16718 transport = SIP_TRANSPORT_TLS;
16719 else {
16720 if (strncasecmp(trans, "udp", 3))
16721 ast_debug(1, "received contact with an invalid transport, '%s'\n", s);
16722 transport = SIP_TRANSPORT_UDP;
16723 }
16724 } while(0);
16725 s = remove_uri_parameters(s);
16726
16727 if (p->socket.tcptls_session) {
16728 ao2_ref(p->socket.tcptls_session, -1);
16729 p->socket.tcptls_session = NULL;
16730 }
16731
16732 set_socket_transport(&p->socket, transport);
16733
16734 if (ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
16735 char *host = NULL;
16736 if (!strncasecmp(s, "sip:", 4))
16737 s += 4;
16738 else if (!strncasecmp(s, "sips:", 5))
16739 s += 5;
16740 e = strchr(s, '/');
16741 if (e)
16742 *e = '\0';
16743 if ((host = strchr(s, '@'))) {
16744 *host++ = '\0';
16745 ast_debug(2, "Found promiscuous redirection to 'SIP/%s::::%s@%s'\n", s, get_transport(transport), host);
16746 if (p->owner)
16747 ast_string_field_build(p->owner, call_forward, "SIP/%s::::%s@%s", s, get_transport(transport), host);
16748 } else {
16749 ast_debug(2, "Found promiscuous redirection to 'SIP/::::%s@%s'\n", get_transport(transport), s);
16750 if (p->owner)
16751 ast_string_field_build(p->owner, call_forward, "SIP/::::%s@%s", get_transport(transport), s);
16752 }
16753 } else {
16754 e = strchr(tmp, '@');
16755 if (e) {
16756 *e++ = '\0';
16757 domain = e;
16758 } else {
16759
16760 domain = tmp;
16761 }
16762 e = strchr(tmp, '/');
16763 if (e)
16764 *e = '\0';
16765
16766 if (!strncasecmp(s, "sip:", 4))
16767 s += 4;
16768 else if (!strncasecmp(s, "sips:", 5))
16769 s += 5;
16770 e = strchr(s, ';');
16771 if (e)
16772 *e = '\0';
16773 ast_debug(2, "Received 302 Redirect to extension '%s' (domain %s)\n", s, domain);
16774 if (p->owner) {
16775 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
16776 ast_string_field_set(p->owner, call_forward, s);
16777 }
16778 }
16779 }
16780
16781
16782 static void check_pendings(struct sip_pvt *p)
16783 {
16784 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
16785
16786 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA)
16787 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
16788
16789
16790 else {
16791
16792
16793 if (p->pendinginvite)
16794 return;
16795
16796
16797 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
16798 }
16799 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
16800 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16801 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
16802
16803 if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
16804 ast_debug(2, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
16805 } else {
16806 ast_debug(2, "Sending pending reinvite on '%s'\n", p->callid);
16807
16808 transmit_reinvite_with_sdp(p, (p->t38.state == T38_LOCAL_REINVITE ? TRUE : FALSE), FALSE);
16809 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
16810 }
16811 }
16812 }
16813
16814
16815
16816
16817
16818 static int sip_reinvite_retry(const void *data)
16819 {
16820 struct sip_pvt *p = (struct sip_pvt *) data;
16821
16822 sip_pvt_lock(p);
16823 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
16824 p->waitid = -1;
16825 check_pendings(p);
16826 sip_pvt_unlock(p);
16827 dialog_unref(p, "unref the dialog ptr from sip_reinvite_retry, because it held a dialog ptr");
16828 return 0;
16829 }
16830
16831
16832
16833 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
16834 {
16835 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
16836 int res = 0;
16837 int xmitres = 0;
16838 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
16839 char *p_hdrval;
16840 int rtn;
16841
16842 if (reinvite)
16843 ast_debug(4, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
16844 else
16845 ast_debug(4, "SIP response %d to standard invite\n", resp);
16846
16847 if (p->alreadygone) {
16848 ast_debug(1, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
16849 return;
16850 }
16851
16852
16853
16854 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"));
16855
16856
16857
16858
16859 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 182 && resp != 183)
16860 resp = 183;
16861
16862
16863 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
16864 p->invitestate = INV_PROCEEDING;
16865
16866
16867 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
16868 p->invitestate = INV_COMPLETED;
16869
16870
16871 if ((resp == 200 || resp >= 300) && p->pendinginvite && seqno == p->pendinginvite)
16872 p->pendinginvite = 0;
16873
16874 switch (resp) {
16875 case 100:
16876 case 101:
16877 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
16878 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16879 check_pendings(p);
16880 break;
16881
16882 case 180:
16883 case 182:
16884 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
16885 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16886 if (!req->ignore && p->owner) {
16887 ast_queue_control(p->owner, AST_CONTROL_RINGING);
16888 if (p->owner->_state != AST_STATE_UP) {
16889 ast_setstate(p->owner, AST_STATE_RINGING);
16890 }
16891 }
16892 if (find_sdp(req)) {
16893 if (p->invitestate != INV_CANCELLED)
16894 p->invitestate = INV_EARLY_MEDIA;
16895 res = process_sdp(p, req, SDP_T38_NONE);
16896 if (!req->ignore && p->owner) {
16897
16898 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
16899 }
16900 }
16901 check_pendings(p);
16902 break;
16903
16904 case 183:
16905 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
16906 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16907 if (find_sdp(req)) {
16908 if (p->invitestate != INV_CANCELLED)
16909 p->invitestate = INV_EARLY_MEDIA;
16910 res = process_sdp(p, req, SDP_T38_NONE);
16911 if (!req->ignore && p->owner) {
16912
16913 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
16914 }
16915 } else {
16916
16917
16918
16919
16920 if (!req->ignore && p->owner) {
16921 ast_queue_control(p->owner, AST_CONTROL_RINGING);
16922 }
16923 }
16924 check_pendings(p);
16925 break;
16926
16927 case 200:
16928 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
16929 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16930 p->authtries = 0;
16931 if (find_sdp(req)) {
16932 if ((res = process_sdp(p, req, SDP_T38_ACCEPT)) && !req->ignore)
16933 if (!reinvite)
16934
16935
16936 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
16937 }
16938
16939
16940
16941
16942 if (outgoing) {
16943 update_call_counter(p, DEC_CALL_RINGING);
16944 parse_ok_contact(p, req);
16945
16946 if (!reinvite)
16947 build_route(p, req, 1);
16948
16949 if(set_address_from_contact(p)) {
16950
16951
16952 if (!p->route && !req->ignore)
16953 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
16954 }
16955
16956 }
16957
16958 if (!req->ignore && p->owner) {
16959 if (!reinvite) {
16960 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
16961 if (global_callevents)
16962 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
16963 "Channel: %s\r\nChanneltype: %s\r\nUniqueid: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
16964 p->owner->name, "SIP", p->owner->uniqueid, p->callid, p->fullcontact, p->peername);
16965 } else {
16966 ast_queue_frame(p->owner, &ast_null_frame);
16967 }
16968 } else {
16969
16970
16971
16972 if (!req->ignore)
16973 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
16974 }
16975
16976
16977 if (st_get_mode(p) != SESSION_TIMER_MODE_REFUSE && p->outgoing_call == TRUE && !reinvite) {
16978 p_hdrval = (char*)get_header(req, "Session-Expires");
16979 if (!ast_strlen_zero(p_hdrval)) {
16980
16981 enum st_refresher tmp_st_ref = SESSION_TIMER_REFRESHER_AUTO;
16982 int tmp_st_interval = 0;
16983 rtn = parse_session_expires(p_hdrval, &tmp_st_interval, &tmp_st_ref);
16984 if (rtn != 0) {
16985 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
16986 }
16987 if (tmp_st_ref == SESSION_TIMER_REFRESHER_UAC ||
16988 tmp_st_ref == SESSION_TIMER_REFRESHER_UAS) {
16989 p->stimer->st_ref = tmp_st_ref;
16990 }
16991 if (tmp_st_interval) {
16992 p->stimer->st_interval = tmp_st_interval;
16993 }
16994 p->stimer->st_active = TRUE;
16995 p->stimer->st_active_peer_ua = TRUE;
16996 start_session_timer(p);
16997 } else {
16998
16999 if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
17000 p->stimer->st_ref = SESSION_TIMER_REFRESHER_UAC;
17001 p->stimer->st_active_peer_ua = FALSE;
17002 start_session_timer(p);
17003 }
17004 }
17005 }
17006
17007
17008
17009 p->invitestate = INV_TERMINATED;
17010 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
17011 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
17012 check_pendings(p);
17013 break;
17014
17015 case 407:
17016 case 401:
17017
17018 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17019 if (p->options)
17020 p->options->auth_type = resp;
17021
17022
17023 ast_string_field_set(p, theirtag, NULL);
17024 if (!req->ignore) {
17025 if (p->authtries < MAX_AUTHTRIES)
17026 p->invitestate = INV_CALLING;
17027 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, SIP_INVITE, 1)) {
17028 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
17029 p->needdestroy = 1;
17030 sip_alreadygone(p);
17031 if (p->owner)
17032 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17033 }
17034 }
17035 break;
17036
17037 case 403:
17038
17039 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17040 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
17041 if (!req->ignore && p->owner)
17042 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17043 p->needdestroy = 1;
17044 sip_alreadygone(p);
17045 break;
17046
17047 case 404:
17048 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17049 if (p->owner && !req->ignore)
17050 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17051 sip_alreadygone(p);
17052 break;
17053
17054 case 408:
17055 case 481:
17056
17057 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
17058 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17059 if (p->owner)
17060 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17061 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17062 break;
17063
17064 case 422:
17065 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17066 ast_string_field_set(p, theirtag, NULL);
17067 proc_422_rsp(p, req);
17068 break;
17069
17070 case 487:
17071
17072
17073
17074 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17075 if (p->owner && !req->ignore) {
17076 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_NORMAL_CLEARING);
17077 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
17078 } else if (!req->ignore) {
17079 update_call_counter(p, DEC_CALL_LIMIT);
17080 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
17081 p->needdestroy = 1;
17082 sip_alreadygone(p);
17083 }
17084 break;
17085 case 415:
17086 case 488:
17087 case 606:
17088 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17089 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
17090 change_t38_state(p, T38_DISABLED);
17091
17092 ast_rtp_set_rtptimers_onhold(p->rtp);
17093
17094
17095 transmit_reinvite_with_sdp(p, FALSE, FALSE);
17096 } else {
17097
17098 if (p->owner && !req->ignore)
17099 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17100 p->needdestroy = 1;
17101
17102 if (!reinvite)
17103 sip_alreadygone(p);
17104 }
17105 break;
17106 case 491:
17107 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17108 if (p->owner && !req->ignore) {
17109 if (p->owner->_state != AST_STATE_UP) {
17110 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17111 p->needdestroy = 1;
17112 } else {
17113
17114
17115
17116 int wait;
17117
17118
17119 if (p->outgoing_call) {
17120 wait = 2100 + ast_random() % 2000;
17121 } else {
17122 wait = ast_random() % 2000;
17123 }
17124 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."));
17125 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);
17126 ast_debug(2, "Reinvite race. Waiting %d secs before retry\n", wait);
17127 }
17128 }
17129 break;
17130
17131 case 501:
17132 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17133 if (p->owner)
17134 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17135 break;
17136 }
17137 if (xmitres == XMIT_ERROR)
17138 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
17139 }
17140
17141
17142
17143
17144 static void handle_response_notify(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
17145 {
17146 switch (resp) {
17147 case 200:
17148
17149 if (p->owner) {
17150 if (!p->refer) {
17151 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
17152 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_NORMAL_UNSPECIFIED);
17153 } else {
17154 ast_debug(4, "Got OK on REFER Notify message\n");
17155 }
17156 } else {
17157 if (p->subscribed == NONE) {
17158 ast_debug(4, "Got 200 accepted on NOTIFY\n");
17159 p->needdestroy = 1;
17160 }
17161 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
17162
17163 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
17164 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
17165 }
17166 }
17167 break;
17168 case 401:
17169 case 407:
17170 if (!p->notify_headers) {
17171 break;
17172 }
17173 ast_string_field_set(p, theirtag, NULL);
17174 if (ast_strlen_zero(p->authname)) {
17175 ast_log(LOG_WARNING, "Asked to authenticate NOTIFY to %s:%d but we have no matching peer or realm auth!\n", ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
17176 p->needdestroy = 1;
17177 }
17178 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_NOTIFY, 0)) {
17179 ast_log(LOG_NOTICE, "Failed to authenticate on NOTYFY to '%s'\n", get_header(&p->initreq, "From"));
17180 p->needdestroy = 1;
17181 }
17182 break;
17183 }
17184 }
17185
17186
17187
17188
17189 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
17190 {
17191
17192 if (!p->refer)
17193 return;
17194
17195 switch (resp) {
17196 case 202:
17197
17198
17199 p->refer->status = REFER_ACCEPTED;
17200
17201 ast_debug(3, "Got 202 accepted on transfer\n");
17202
17203 break;
17204
17205 case 401:
17206 case 407:
17207 if (ast_strlen_zero(p->authname)) {
17208 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s:%d but we have no matching peer or realm auth!\n",
17209 ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
17210 p->needdestroy = 1;
17211 }
17212 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_REFER, 0)) {
17213 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
17214 p->refer->status = REFER_NOAUTH;
17215 p->needdestroy = 1;
17216 }
17217 break;
17218 case 481:
17219
17220
17221
17222
17223 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
17224 if (p->owner)
17225 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17226 p->needdestroy = 1;
17227 break;
17228
17229 case 500:
17230 case 501:
17231
17232
17233 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
17234 p->needdestroy = 1;
17235 p->refer->status = REFER_FAILED;
17236 break;
17237 case 603:
17238 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
17239 p->refer->status = REFER_FAILED;
17240 p->needdestroy = 1;
17241 break;
17242 }
17243 }
17244
17245
17246 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
17247 {
17248 int expires, expires_ms;
17249 struct sip_registry *r;
17250 r=p->registry;
17251
17252 switch (resp) {
17253 case 401:
17254 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
17255 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
17256 p->needdestroy = 1;
17257 }
17258 break;
17259 case 403:
17260 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
17261 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 403"));
17262 r->regstate = REG_STATE_NOAUTH;
17263 p->needdestroy = 1;
17264 break;
17265 case 404:
17266 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username, p->registry->hostname);
17267 p->needdestroy = 1;
17268 if (r->call)
17269 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 404");
17270 r->regstate = REG_STATE_REJECTED;
17271 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 404"));
17272 break;
17273 case 407:
17274 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
17275 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
17276 p->needdestroy = 1;
17277 }
17278 break;
17279 case 408:
17280
17281 if (r) {
17282 r->regattempts = 0;
17283 } else {
17284 ast_log(LOG_WARNING, "Got a 408 response to our REGISTER on call %s after we had destroyed the registry object\n", p->callid);
17285 }
17286 break;
17287 case 423:
17288 r->expiry = atoi(get_header(req, "Min-Expires"));
17289 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);
17290 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 423"));
17291 if (r->call) {
17292 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 423");
17293 p->needdestroy = 1;
17294 }
17295 if (r->expiry > max_expiry) {
17296 ast_log(LOG_WARNING, "Required expiration time from %s@%s is too high, giving up\n", p->registry->username, p->registry->hostname);
17297 r->expiry = r->configured_expiry;
17298 r->regstate = REG_STATE_REJECTED;
17299 } else {
17300 r->regstate = REG_STATE_UNREGISTERED;
17301 transmit_register(r, SIP_REGISTER, NULL, NULL);
17302 }
17303 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));
17304 break;
17305 case 479:
17306 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username, p->registry->hostname);
17307 p->needdestroy = 1;
17308 if (r->call)
17309 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 479");
17310 r->regstate = REG_STATE_REJECTED;
17311 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 479"));
17312 break;
17313 case 200:
17314 if (!r) {
17315 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));
17316 p->needdestroy = 1;
17317 return 0;
17318 }
17319
17320 r->regstate = REG_STATE_REGISTERED;
17321 r->regtime = ast_tvnow();
17322 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
17323 r->regattempts = 0;
17324 ast_debug(1, "Registration successful\n");
17325 if (r->timeout > -1) {
17326 ast_debug(1, "Cancelling timeout %d\n", r->timeout);
17327 }
17328 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 200"));
17329 if (r->call)
17330 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 200");
17331 p->registry = registry_unref(p->registry, "unref registry entry p->registry");
17332
17333 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17334
17335
17336
17337
17338
17339
17340 expires = 0;
17341
17342
17343 if (!ast_strlen_zero(get_header(req, "Contact"))) {
17344 const char *contact = NULL;
17345 const char *tmptmp = NULL;
17346 int start = 0;
17347 for(;;) {
17348 contact = __get_header(req, "Contact", &start);
17349
17350 if(!ast_strlen_zero(contact)) {
17351 if( (tmptmp=strstr(contact, p->our_contact))) {
17352 contact=tmptmp;
17353 break;
17354 }
17355 } else
17356 break;
17357 }
17358 tmptmp = strcasestr(contact, "expires=");
17359 if (tmptmp) {
17360 if (sscanf(tmptmp + 8, "%30d;", &expires) != 1)
17361 expires = 0;
17362 }
17363
17364 }
17365 if (!expires)
17366 expires=atoi(get_header(req, "expires"));
17367 if (!expires)
17368 expires=default_expiry;
17369
17370 expires_ms = expires * 1000;
17371 if (expires <= EXPIRY_GUARD_LIMIT)
17372 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT), EXPIRY_GUARD_MIN);
17373 else
17374 expires_ms -= EXPIRY_GUARD_SECS * 1000;
17375 if (sipdebug)
17376 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
17377
17378 r->refresh= (int) expires_ms / 1000;
17379
17380
17381 AST_SCHED_REPLACE_UNREF(r->expire, sched, expires_ms, sip_reregister, r,
17382 registry_unref(_data,"unref in REPLACE del fail"),
17383 registry_unref(r,"unref in REPLACE add fail"),
17384 registry_addref(r,"The Addition side of REPLACE"));
17385 }
17386 return 1;
17387 }
17388
17389
17390 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
17391 {
17392 struct sip_peer *peer = p->relatedpeer ;
17393 int statechanged, is_reachable, was_reachable;
17394 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
17395
17396
17397
17398
17399
17400
17401 if (pingtime < 1)
17402 pingtime = 1;
17403
17404
17405
17406
17407
17408 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
17409 is_reachable = pingtime <= peer->maxms;
17410 statechanged = peer->lastms == 0
17411 || was_reachable != is_reachable;
17412
17413 peer->lastms = pingtime;
17414 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
17415 if (statechanged) {
17416 const char *s = is_reachable ? "Reachable" : "Lagged";
17417 char str_lastms[20];
17418 snprintf(str_lastms, sizeof(str_lastms), "%d", pingtime);
17419
17420 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
17421 peer->name, s, pingtime, peer->maxms);
17422 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
17423 if (sip_cfg.peer_rtupdate) {
17424 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", str_lastms, SENTINEL);
17425 }
17426 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
17427 "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
17428 peer->name, s, pingtime);
17429 if (is_reachable && global_regextenonqualify)
17430 register_peer_exten(peer, TRUE);
17431 }
17432
17433 p->needdestroy = 1;
17434
17435
17436 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched,
17437 is_reachable ? peer->qualifyfreq : DEFAULT_FREQ_NOTOK,
17438 sip_poke_peer_s, peer,
17439 unref_peer(_data, "removing poke peer ref"),
17440 unref_peer(peer, "removing poke peer ref"),
17441 ref_peer(peer, "adding poke peer ref"));
17442 }
17443
17444
17445 static void stop_media_flows(struct sip_pvt *p)
17446 {
17447
17448 if (p->rtp)
17449 ast_rtp_stop(p->rtp);
17450 if (p->vrtp)
17451 ast_rtp_stop(p->vrtp);
17452 if (p->trtp)
17453 ast_rtp_stop(p->trtp);
17454 if (p->udptl)
17455 ast_udptl_stop(p->udptl);
17456 }
17457
17458
17459
17460 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
17461 {
17462 struct ast_channel *owner;
17463 int sipmethod;
17464 int res = 1;
17465 const char *c = get_header(req, "Cseq");
17466
17467 char *c_copy = ast_strdupa(c);
17468
17469 const char *msg = ast_skip_blanks(ast_skip_nonblanks(c_copy));
17470
17471 if (!msg)
17472 msg = "";
17473
17474 sipmethod = find_sip_method(msg);
17475
17476 owner = p->owner;
17477 if (owner)
17478 owner->hangupcause = hangup_sip2cause(resp);
17479
17480 if (p->socket.type == SIP_TRANSPORT_UDP) {
17481 int ack_res;
17482
17483
17484 if ((resp >= 100) && (resp <= 199)) {
17485 ack_res = __sip_semi_ack(p, seqno, 0, sipmethod);
17486 } else {
17487 ack_res = __sip_ack(p, seqno, 0, sipmethod);
17488 }
17489
17490 if (ack_res == FALSE) {
17491 append_history(p, "Ignore", "Ignoring this retransmit\n");
17492 return;
17493 }
17494 }
17495
17496
17497 if (!p->owner && sipmethod == SIP_NOTIFY && p->pendinginvite)
17498 p->pendinginvite = 0;
17499
17500
17501 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
17502 char tag[128];
17503
17504 gettag(req, "To", tag, sizeof(tag));
17505 ast_string_field_set(p, theirtag, tag);
17506 }
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522 if ((resp == 404 || resp == 408 || resp == 481) && sipmethod == SIP_BYE) {
17523 p->needdestroy = 1;
17524 return;
17525 }
17526
17527 if (p->relatedpeer && p->method == SIP_OPTIONS) {
17528
17529
17530
17531 if (resp != 100)
17532 handle_response_peerpoke(p, resp, req);
17533 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
17534 switch(resp) {
17535 case 100:
17536 case 101:
17537 if (sipmethod == SIP_INVITE)
17538 handle_response_invite(p, resp, rest, req, seqno);
17539 break;
17540 case 183:
17541 if (sipmethod == SIP_INVITE)
17542 handle_response_invite(p, resp, rest, req, seqno);
17543 break;
17544 case 180:
17545 if (sipmethod == SIP_INVITE)
17546 handle_response_invite(p, resp, rest, req, seqno);
17547 break;
17548 case 182:
17549 if (sipmethod == SIP_INVITE)
17550 handle_response_invite(p, resp, rest, req, seqno);
17551 break;
17552 case 200:
17553 p->authtries = 0;
17554 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
17555
17556
17557
17558 } else if (sipmethod == SIP_INVITE) {
17559 handle_response_invite(p, resp, rest, req, seqno);
17560 } else if (sipmethod == SIP_NOTIFY) {
17561 handle_response_notify(p, resp, rest, req, seqno);
17562 } else if (sipmethod == SIP_REGISTER)
17563 res = handle_response_register(p, resp, rest, req, seqno);
17564 else if (sipmethod == SIP_BYE) {
17565 p->needdestroy = 1;
17566 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
17567 } else if (sipmethod == SIP_SUBSCRIBE) {
17568 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
17569 }
17570 break;
17571 case 202:
17572 if (sipmethod == SIP_REFER)
17573 handle_response_refer(p, resp, rest, req, seqno);
17574 break;
17575 case 401:
17576 case 407:
17577 if (sipmethod == SIP_INVITE)
17578 handle_response_invite(p, resp, rest, req, seqno);
17579 else if (sipmethod == SIP_NOTIFY)
17580 handle_response_notify(p, resp, rest, req, seqno);
17581 else if (sipmethod == SIP_REFER)
17582 handle_response_refer(p, resp, rest, req, seqno);
17583 else if (p->registry && sipmethod == SIP_REGISTER)
17584 res = handle_response_register(p, resp, rest, req, seqno);
17585 else if (sipmethod == SIP_BYE) {
17586 if (p->options)
17587 p->options->auth_type = resp;
17588 if (ast_strlen_zero(p->authname)) {
17589 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
17590 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
17591 p->needdestroy = 1;
17592 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, sipmethod, 0)) {
17593 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
17594 p->needdestroy = 1;
17595 }
17596 } else {
17597 ast_log(LOG_WARNING, "Got authentication request (%d) on %s to '%s'\n", resp, sip_methods[sipmethod].text, get_header(req, "To"));
17598 p->needdestroy = 1;
17599 }
17600 break;
17601 case 403:
17602 if (sipmethod == SIP_INVITE)
17603 handle_response_invite(p, resp, rest, req, seqno);
17604 else if (p->registry && sipmethod == SIP_REGISTER)
17605 res = handle_response_register(p, resp, rest, req, seqno);
17606 else {
17607 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
17608 p->needdestroy = 1;
17609 }
17610 break;
17611 case 404:
17612 if (p->registry && sipmethod == SIP_REGISTER)
17613 res = handle_response_register(p, resp, rest, req, seqno);
17614 else if (sipmethod == SIP_INVITE)
17615 handle_response_invite(p, resp, rest, req, seqno);
17616 else if (owner)
17617 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17618 break;
17619 case 423:
17620 if (sipmethod == SIP_REGISTER)
17621 res = handle_response_register(p, resp, rest, req, seqno);
17622 break;
17623 case 408:
17624 if (sipmethod == SIP_INVITE)
17625 handle_response_invite(p, resp, rest, req, seqno);
17626 else if (sipmethod == SIP_REGISTER)
17627 res = handle_response_register(p, resp, rest, req, seqno);
17628 else if (sipmethod == SIP_BYE) {
17629 p->needdestroy = 1;
17630 ast_debug(4, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
17631 } else {
17632 if (owner)
17633 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17634 p->needdestroy = 1;
17635 }
17636 break;
17637
17638 case 422:
17639 if (sipmethod == SIP_INVITE) {
17640 handle_response_invite(p, resp, rest, req, seqno);
17641 }
17642 break;
17643
17644 case 481:
17645 if (sipmethod == SIP_INVITE) {
17646 handle_response_invite(p, resp, rest, req, seqno);
17647 } else if (sipmethod == SIP_REFER) {
17648 handle_response_refer(p, resp, rest, req, seqno);
17649 } else if (sipmethod == SIP_BYE) {
17650
17651
17652 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
17653 } else if (sipmethod == SIP_CANCEL) {
17654
17655
17656 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
17657 } else {
17658 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
17659
17660 }
17661 break;
17662 case 487:
17663 if (sipmethod == SIP_INVITE)
17664 handle_response_invite(p, resp, rest, req, seqno);
17665 break;
17666 case 415:
17667 case 488:
17668 case 606:
17669 if (sipmethod == SIP_INVITE)
17670 handle_response_invite(p, resp, rest, req, seqno);
17671 break;
17672 case 491:
17673 if (sipmethod == SIP_INVITE)
17674 handle_response_invite(p, resp, rest, req, seqno);
17675 else {
17676 ast_debug(1, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
17677 p->needdestroy = 1;
17678 }
17679 break;
17680 case 501:
17681 if (sipmethod == SIP_INVITE)
17682 handle_response_invite(p, resp, rest, req, seqno);
17683 else if (sipmethod == SIP_REFER)
17684 handle_response_refer(p, resp, rest, req, seqno);
17685 else
17686 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(p->sa.sin_addr), msg);
17687 break;
17688 case 603:
17689 if (sipmethod == SIP_REFER) {
17690 handle_response_refer(p, resp, rest, req, seqno);
17691 break;
17692 }
17693
17694 default:
17695 if ((resp >= 300) && (resp < 700)) {
17696
17697 if ((resp != 487))
17698 ast_verb(3, "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
17699
17700 if (sipmethod == SIP_INVITE)
17701 stop_media_flows(p);
17702
17703
17704 switch(resp) {
17705 case 300:
17706 case 301:
17707 case 302:
17708 case 305:
17709 parse_moved_contact(p, req);
17710
17711 case 486:
17712 case 600:
17713 case 603:
17714 if (p->owner)
17715 ast_queue_control(p->owner, AST_CONTROL_BUSY);
17716 break;
17717 case 482:
17718
17719
17720
17721
17722 ast_debug(1, "Hairpin detected, setting up call forward for what it's worth\n");
17723 if (p->owner)
17724 ast_string_field_build(p->owner, call_forward,
17725 "Local/%s@%s", p->username, p->context);
17726
17727 case 480:
17728 case 404:
17729 case 410:
17730 case 400:
17731 case 500:
17732 if (sipmethod == SIP_REFER) {
17733 handle_response_refer(p, resp, rest, req, seqno);
17734 break;
17735 }
17736
17737 case 502:
17738 case 503:
17739 case 504:
17740 if (owner)
17741 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
17742 break;
17743 default:
17744
17745 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO && sipmethod != SIP_BYE)
17746 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
17747 break;
17748 }
17749
17750 if (sipmethod == SIP_INVITE)
17751 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
17752 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
17753 sip_alreadygone(p);
17754 if (!p->owner)
17755 p->needdestroy = 1;
17756 } else if ((resp >= 100) && (resp < 200)) {
17757 if (sipmethod == SIP_INVITE) {
17758 if (!req->ignore && sip_cancel_destroy(p))
17759 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
17760 if (find_sdp(req))
17761 process_sdp(p, req, SDP_T38_NONE);
17762 if (p->owner) {
17763
17764 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
17765 }
17766 }
17767 } else
17768 ast_log(LOG_NOTICE, "Dont know how to handle a %d %s response from %s\n", resp, rest, p->owner ? p->owner->name : ast_inet_ntoa(p->sa.sin_addr));
17769 }
17770 } else {
17771
17772
17773 if (req->debug)
17774 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
17775
17776 if (sipmethod == SIP_INVITE && resp == 200) {
17777
17778
17779 char tag[128];
17780
17781 gettag(req, "To", tag, sizeof(tag));
17782 ast_string_field_set(p, theirtag, tag);
17783 }
17784
17785 switch(resp) {
17786 case 200:
17787 if (sipmethod == SIP_INVITE) {
17788 handle_response_invite(p, resp, rest, req, seqno);
17789 } else if (sipmethod == SIP_CANCEL) {
17790 ast_debug(1, "Got 200 OK on CANCEL\n");
17791
17792
17793 } else if (sipmethod == SIP_NOTIFY) {
17794
17795 if (p->owner) {
17796 if (p->refer) {
17797 ast_debug(1, "Got 200 OK on NOTIFY for transfer\n");
17798 } else
17799 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
17800
17801 } else {
17802 if (!p->subscribed && !p->refer)
17803 p->needdestroy = 1;
17804 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
17805
17806 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
17807 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
17808 }
17809 }
17810 } else if (sipmethod == SIP_BYE)
17811 p->needdestroy = 1;
17812 else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO)
17813
17814
17815 ;
17816 else if (sipmethod == SIP_BYE)
17817
17818 p->needdestroy = 1;
17819 break;
17820 case 202:
17821 if (sipmethod == SIP_REFER)
17822 handle_response_refer(p, resp, rest, req, seqno);
17823 break;
17824 case 401:
17825 case 407:
17826 if (sipmethod == SIP_REFER)
17827 handle_response_refer(p, resp, rest, req, seqno);
17828 else if (sipmethod == SIP_INVITE)
17829 handle_response_invite(p, resp, rest, req, seqno);
17830 else if (sipmethod == SIP_BYE) {
17831 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, sipmethod, 0)) {
17832 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
17833 p->needdestroy = 1;
17834 }
17835 }
17836 break;
17837 case 481:
17838 if (sipmethod == SIP_INVITE) {
17839
17840 handle_response_invite(p, resp, rest, req, seqno);
17841 } else if (sipmethod == SIP_BYE) {
17842 p->needdestroy = 1;
17843 } else if (sipdebug) {
17844 ast_debug(1, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
17845 }
17846 break;
17847 case 501:
17848 if (sipmethod == SIP_INVITE)
17849 handle_response_invite(p, resp, rest, req, seqno);
17850 else if (sipmethod == SIP_REFER)
17851 handle_response_refer(p, resp, rest, req, seqno);
17852 break;
17853 case 603:
17854 if (sipmethod == SIP_REFER) {
17855 handle_response_refer(p, resp, rest, req, seqno);
17856 break;
17857 }
17858
17859 default:
17860 if ((resp >= 100) && (resp < 200)) {
17861 if (sipmethod == SIP_INVITE) {
17862 if (!req->ignore && sip_cancel_destroy(p))
17863 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
17864 }
17865 }
17866 if ((resp >= 300) && (resp < 700)) {
17867 if ((resp != 487))
17868 ast_verb(3, "Incoming call: Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
17869 switch(resp) {
17870 case 415:
17871 case 488:
17872 case 603:
17873 case 500:
17874 case 502:
17875 case 503:
17876 case 504:
17877
17878
17879 if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
17880 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
17881 break;
17882 }
17883 }
17884 break;
17885 }
17886 }
17887 }
17888
17889
17890
17891
17892
17893
17894
17895 static void *sip_park_thread(void *stuff)
17896 {
17897 struct ast_channel *transferee, *transferer;
17898 struct sip_dual *d;
17899 struct sip_request req = {0,};
17900 int ext;
17901 int res;
17902
17903 d = stuff;
17904 transferee = d->chan1;
17905 transferer = d->chan2;
17906 copy_request(&req, &d->req);
17907
17908 if (!transferee || !transferer) {
17909 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
17910 if (d->req.data)
17911 ast_free(d->req.data);
17912 free(d);
17913 return NULL;
17914 }
17915 ast_debug(4, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
17916
17917 ast_channel_lock(transferee);
17918 if (ast_do_masquerade(transferee)) {
17919 ast_log(LOG_WARNING, "Masquerade failed.\n");
17920 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
17921 ast_channel_unlock(transferee);
17922 if (d->req.data)
17923 ast_free(d->req.data);
17924 free(d);
17925 return NULL;
17926 }
17927 ast_channel_unlock(transferee);
17928
17929 res = ast_park_call(transferee, transferer, 0, &ext);
17930
17931
17932 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
17933 if (!res) {
17934 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
17935 } else {
17936
17937 sprintf(buf, "Call parked on extension '%d'", ext);
17938 transmit_message_with_text(transferer->tech_pvt, buf);
17939 }
17940 #endif
17941
17942
17943
17944 transmit_response(transferer->tech_pvt, "202 Accepted", &req);
17945 if (!res) {
17946
17947 append_history(transferer->tech_pvt, "SIPpark", "Parked call on %d", ext);
17948 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
17949 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
17950 ast_hangup(transferer);
17951 ast_debug(1, "SIP Call parked on extension '%d'\n", ext);
17952 } else {
17953 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
17954 append_history(transferer->tech_pvt, "SIPpark", "Parking failed\n");
17955 ast_debug(1, "SIP Call parked failed \n");
17956
17957 }
17958 if (d->req.data)
17959 ast_free(d->req.data);
17960 free(d);
17961 return NULL;
17962 }
17963
17964
17965
17966
17967 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno)
17968 {
17969 struct sip_dual *d;
17970 struct ast_channel *transferee, *transferer;
17971
17972 pthread_t th;
17973
17974 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
17975 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "SIPPeer/%s", chan2->name);
17976 if ((!transferer) || (!transferee)) {
17977 if (transferee) {
17978 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
17979 ast_hangup(transferee);
17980 }
17981 if (transferer) {
17982 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
17983 ast_hangup(transferer);
17984 }
17985 return -1;
17986 }
17987
17988
17989 transferee->readformat = chan1->readformat;
17990 transferee->writeformat = chan1->writeformat;
17991
17992
17993 ast_channel_masquerade(transferee, chan1);
17994
17995
17996 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
17997 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
17998 transferee->priority = chan1->priority;
17999
18000
18001
18002
18003
18004 transferer->readformat = chan2->readformat;
18005 transferer->writeformat = chan2->writeformat;
18006 if (!ast_strlen_zero(chan2->parkinglot))
18007 ast_string_field_set(transferer, parkinglot, chan2->parkinglot);
18008
18009
18010
18011
18012 while (ast_channel_trylock(chan2)) {
18013 struct sip_pvt *pvt = chan2->tech_pvt;
18014 sip_pvt_unlock(pvt);
18015 usleep(1);
18016 sip_pvt_lock(pvt);
18017 }
18018 ast_channel_masquerade(transferer, chan2);
18019 ast_channel_unlock(chan2);
18020
18021
18022 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
18023 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
18024 transferer->priority = chan2->priority;
18025
18026 ast_channel_lock(transferer);
18027 if (ast_do_masquerade(transferer)) {
18028 ast_log(LOG_WARNING, "Masquerade failed :(\n");
18029 ast_channel_unlock(transferer);
18030 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
18031 ast_hangup(transferer);
18032 return -1;
18033 }
18034 ast_channel_unlock(transferer);
18035 if (!transferer || !transferee) {
18036 if (!transferer) {
18037 ast_debug(1, "No transferer channel, giving up parking\n");
18038 }
18039 if (!transferee) {
18040 ast_debug(1, "No transferee channel, giving up parking\n");
18041 }
18042 return -1;
18043 }
18044 if ((d = ast_calloc(1, sizeof(*d)))) {
18045
18046
18047 copy_request(&d->req, req);
18048 d->chan1 = transferee;
18049 d->chan2 = transferer;
18050 d->seqno = seqno;
18051 if (ast_pthread_create_detached_background(&th, NULL, sip_park_thread, d) < 0) {
18052
18053 if (d->req.data)
18054 ast_free(d->req.data);
18055 ast_free(d);
18056
18057 return 0;
18058 }
18059 }
18060 return -1;
18061 }
18062
18063
18064
18065
18066 static void ast_quiet_chan(struct ast_channel *chan)
18067 {
18068 if (chan && chan->_state == AST_STATE_UP) {
18069 if (ast_test_flag(chan, AST_FLAG_MOH))
18070 ast_moh_stop(chan);
18071 else if (chan->generatordata)
18072 ast_deactivate_generator(chan);
18073 }
18074 }
18075
18076
18077
18078 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
18079 {
18080 int res = 0;
18081 struct ast_channel *peera = NULL,
18082 *peerb = NULL,
18083 *peerc = NULL,
18084 *peerd = NULL;
18085
18086
18087
18088
18089 ast_debug(4, "Sip transfer:--------------------\n");
18090 if (transferer->chan1)
18091 ast_debug(4, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
18092 else
18093 ast_debug(4, "-- No transferer first channel - odd??? \n");
18094 if (target->chan1)
18095 ast_debug(4, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
18096 else
18097 ast_debug(4, "-- No target first channel ---\n");
18098 if (transferer->chan2)
18099 ast_debug(4, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
18100 else
18101 ast_debug(4, "-- No bridged call to transferee\n");
18102 if (target->chan2)
18103 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)");
18104 else
18105 ast_debug(4, "-- No target second channel ---\n");
18106 ast_debug(4, "-- END Sip transfer:--------------------\n");
18107 if (transferer->chan2) {
18108 peera = transferer->chan1;
18109 peerb = target->chan1;
18110 peerc = transferer->chan2;
18111 peerd = target->chan2;
18112 ast_debug(3, "SIP transfer: Four channels to handle\n");
18113 } else if (target->chan2) {
18114 peera = target->chan1;
18115 peerb = transferer->chan1;
18116 peerc = target->chan2;
18117 peerd = transferer->chan2;
18118 ast_debug(3, "SIP transfer: Three channels to handle\n");
18119 }
18120
18121 if (peera && peerb && peerc && (peerb != peerc)) {
18122 ast_quiet_chan(peera);
18123 ast_quiet_chan(peerb);
18124 ast_quiet_chan(peerc);
18125 if (peerd)
18126 ast_quiet_chan(peerd);
18127
18128 ast_debug(4, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
18129 if (ast_channel_masquerade(peerb, peerc)) {
18130 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
18131 res = -1;
18132 } else
18133 ast_debug(4, "SIP transfer: Succeeded to masquerade channels.\n");
18134 return res;
18135 } else {
18136 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
18137 if (transferer->chan1)
18138 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
18139 if (target->chan1)
18140 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
18141 return -1;
18142 }
18143 return 0;
18144 }
18145
18146
18147
18148
18149
18150
18151 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
18152 {
18153 const char *thetag;
18154
18155 if (!tagbuf)
18156 return NULL;
18157 tagbuf[0] = '\0';
18158 thetag = get_header(req, header);
18159 thetag = strcasestr(thetag, ";tag=");
18160 if (thetag) {
18161 thetag += 5;
18162 ast_copy_string(tagbuf, thetag, tagbufsize);
18163 return strsep(&tagbuf, ";");
18164 }
18165 return NULL;
18166 }
18167
18168
18169 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
18170 {
18171
18172
18173 int res = 0;
18174 const char *event = get_header(req, "Event");
18175 char *eventid = NULL;
18176 char *sep;
18177
18178 if( (sep = strchr(event, ';')) ) {
18179 *sep++ = '\0';
18180 eventid = sep;
18181 }
18182
18183 if (sipdebug)
18184 ast_debug(2, "Got NOTIFY Event: %s\n", event);
18185
18186 if (strcmp(event, "refer")) {
18187
18188
18189 transmit_response(p, "489 Bad event", req);
18190 res = -1;
18191 } else {
18192
18193
18194
18195
18196
18197 char buf[1024];
18198 char *cmd, *code;
18199 int respcode;
18200 int success = TRUE;
18201
18202
18203
18204
18205
18206
18207
18208
18209 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
18210
18211 transmit_response(p, "400 Bad request", req);
18212 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18213 return -1;
18214 }
18215
18216
18217 if (get_msg_text(buf, sizeof(buf), req, TRUE)) {
18218 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
18219 transmit_response(p, "400 Bad request", req);
18220 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18221 return -1;
18222 }
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244 ast_debug(3, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
18245 cmd = ast_skip_blanks(buf);
18246 code = cmd;
18247
18248 while(*code && (*code > 32)) {
18249 code++;
18250 }
18251 *code++ = '\0';
18252 code = ast_skip_blanks(code);
18253 sep = code;
18254 sep++;
18255 while(*sep && (*sep > 32)) {
18256 sep++;
18257 }
18258 *sep++ = '\0';
18259 respcode = atoi(code);
18260 switch (respcode) {
18261 case 100:
18262 case 101:
18263
18264 break;
18265 case 183:
18266
18267 break;
18268 case 200:
18269
18270 break;
18271 case 301:
18272 case 302:
18273
18274 success = FALSE;
18275 break;
18276 case 503:
18277
18278 success = FALSE;
18279 break;
18280 case 603:
18281
18282 success = FALSE;
18283 break;
18284 }
18285 if (!success) {
18286 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
18287 }
18288
18289
18290 transmit_response(p, "200 OK", req);
18291 };
18292
18293 if (!p->lastinvite)
18294 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18295
18296 return res;
18297 }
18298
18299
18300
18301
18302 static int handle_request_options(struct sip_pvt *p, struct sip_request *req)
18303 {
18304 int res;
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316 if (p->lastinvite) {
18317
18318 transmit_response_with_allow(p, "200 OK", req, 0);
18319 return 0;
18320 }
18321
18322 res = get_destination(p, req);
18323 build_contact(p);
18324
18325 if (ast_strlen_zero(p->context))
18326 ast_string_field_set(p, context, default_context);
18327
18328 if (ast_shutting_down())
18329 transmit_response_with_allow(p, "503 Unavailable", req, 0);
18330 else if (res < 0)
18331 transmit_response_with_allow(p, "404 Not Found", req, 0);
18332 else
18333 transmit_response_with_allow(p, "200 OK", req, 0);
18334
18335
18336
18337 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18338
18339 return res;
18340 }
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct sockaddr_in *sin, int *nounlock)
18352 {
18353 int earlyreplace = 0;
18354 int oneleggedreplace = 0;
18355 struct ast_channel *c = p->owner;
18356 struct ast_channel *replacecall = p->refer->refer_call->owner;
18357 struct ast_channel *targetcall;
18358
18359 struct ast_channel *test;
18360
18361
18362 if (replacecall->_state == AST_STATE_RING)
18363 earlyreplace = 1;
18364
18365
18366 if (!(targetcall = ast_bridged_channel(replacecall))) {
18367
18368 if (!earlyreplace) {
18369 ast_debug(2, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
18370 oneleggedreplace = 1;
18371 }
18372 }
18373 if (targetcall && targetcall->_state == AST_STATE_RINGING)
18374 ast_debug(4, "SIP transfer: Target channel is in ringing state\n");
18375
18376 if (targetcall)
18377 ast_debug(4, "SIP transfer: Invite Replace incoming channel should bridge to channel %s while hanging up channel %s\n", targetcall->name, replacecall->name);
18378 else
18379 ast_debug(4, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
18380
18381 if (req->ignore) {
18382 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
18383
18384
18385
18386 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE);
18387
18388 if (c) {
18389 *nounlock = 1;
18390 ast_channel_unlock(c);
18391 }
18392 ast_channel_unlock(replacecall);
18393 sip_pvt_unlock(p->refer->refer_call);
18394 return 1;
18395 }
18396 if (!c) {
18397
18398 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
18399 transmit_response_reliable(p, "503 Service Unavailable", req);
18400 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
18401 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18402 ast_channel_unlock(replacecall);
18403 sip_pvt_unlock(p->refer->refer_call);
18404 return 1;
18405 }
18406 append_history(p, "Xfer", "INVITE/Replace received");
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418 transmit_response(p, "100 Trying", req);
18419 ast_setstate(c, AST_STATE_RING);
18420
18421
18422
18423
18424
18425 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE);
18426
18427 ast_setstate(c, AST_STATE_UP);
18428
18429
18430 ast_quiet_chan(replacecall);
18431 ast_quiet_chan(targetcall);
18432 ast_debug(4, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
18433
18434
18435 if (! earlyreplace && ! oneleggedreplace )
18436 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
18437
18438
18439 if(ast_channel_masquerade(replacecall, c))
18440 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
18441 else
18442 ast_debug(4, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
18443
18444
18445 if (ast_do_masquerade(replacecall)) {
18446 ast_log(LOG_WARNING, "Failed to perform masquerade with INVITE replaces\n");
18447 }
18448
18449 if (earlyreplace || oneleggedreplace ) {
18450 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
18451 }
18452
18453 ast_setstate(c, AST_STATE_DOWN);
18454 ast_debug(4, "After transfer:----------------------------\n");
18455 ast_debug(4, " -- C: %s State %s\n", c->name, ast_state2str(c->_state));
18456 if (replacecall)
18457 ast_debug(4, " -- replacecall: %s State %s\n", replacecall->name, ast_state2str(replacecall->_state));
18458 if (p->owner) {
18459 ast_debug(4, " -- P->owner: %s State %s\n", p->owner->name, ast_state2str(p->owner->_state));
18460 test = ast_bridged_channel(p->owner);
18461 if (test)
18462 ast_debug(4, " -- Call bridged to P->owner: %s State %s\n", test->name, ast_state2str(test->_state));
18463 else
18464 ast_debug(4, " -- No call bridged to C->owner \n");
18465 } else
18466 ast_debug(4, " -- No channel yet \n");
18467 ast_debug(4, "End After transfer:----------------------------\n");
18468
18469
18470 ast_channel_unlock(replacecall);
18471 ast_channel_unlock(c);
18472 sip_pvt_unlock(p->refer->refer_call);
18473 sip_pvt_unlock(p);
18474 *nounlock = 1;
18475
18476
18477 c->tech_pvt = dialog_unref(c->tech_pvt, "unref dialog c->tech_pvt");
18478 ast_hangup(c);
18479 sip_pvt_lock(p);
18480
18481 return 0;
18482 }
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500 static int sip_uri_params_cmp(const char *input1, const char *input2)
18501 {
18502 char *params1 = NULL;
18503 char *params2 = NULL;
18504 char *pos1;
18505 char *pos2;
18506 int zerolength1 = 0;
18507 int zerolength2 = 0;
18508 int maddrmatch = 0;
18509 int ttlmatch = 0;
18510 int usermatch = 0;
18511 int methodmatch = 0;
18512
18513 if (ast_strlen_zero(input1)) {
18514 zerolength1 = 1;
18515 } else {
18516 params1 = ast_strdupa(input1);
18517 }
18518 if (ast_strlen_zero(input2)) {
18519 zerolength2 = 1;
18520 } else {
18521 params2 = ast_strdupa(input2);
18522 }
18523
18524
18525
18526
18527 if (zerolength1 && zerolength2) {
18528 return 0;
18529 }
18530
18531 pos1 = params1;
18532 while (!ast_strlen_zero(pos1)) {
18533 char *name1 = pos1;
18534 char *value1 = strchr(pos1, '=');
18535 char *semicolon1 = strchr(pos1, ';');
18536 int matched = 0;
18537 if (semicolon1) {
18538 *semicolon1++ = '\0';
18539 }
18540 if (!value1) {
18541 goto fail;
18542 }
18543 *value1++ = '\0';
18544
18545
18546
18547
18548
18549 pos2 = ast_strdupa(params2);
18550 while (!ast_strlen_zero(pos2)) {
18551 char *name2 = pos2;
18552 char *value2 = strchr(pos2, '=');
18553 char *semicolon2 = strchr(pos2, ';');
18554 if (semicolon2) {
18555 *semicolon2++ = '\0';
18556 }
18557 if (!value2) {
18558 goto fail;
18559 }
18560 *value2++ = '\0';
18561 if (!strcasecmp(name1, name2)) {
18562 if (strcasecmp(value1, value2)) {
18563 goto fail;
18564 } else {
18565 matched = 1;
18566 break;
18567 }
18568 }
18569 pos2 = semicolon2;
18570 }
18571
18572 if (!strcasecmp(name1, "maddr")) {
18573 if (matched) {
18574 maddrmatch = 1;
18575 } else {
18576 goto fail;
18577 }
18578 } else if (!strcasecmp(name1, "ttl")) {
18579 if (matched) {
18580 ttlmatch = 1;
18581 } else {
18582 goto fail;
18583 }
18584 } else if (!strcasecmp(name1, "user")) {
18585 if (matched) {
18586 usermatch = 1;
18587 } else {
18588 goto fail;
18589 }
18590 } else if (!strcasecmp(name1, "method")) {
18591 if (matched) {
18592 methodmatch = 1;
18593 } else {
18594 goto fail;
18595 }
18596 }
18597 pos1 = semicolon1;
18598 }
18599
18600
18601
18602
18603
18604 pos2 = params2;
18605 while (!ast_strlen_zero(pos2)) {
18606 char *name2 = pos2;
18607 char *value2 = strchr(pos2, '=');
18608 char *semicolon2 = strchr(pos2, ';');
18609 if (semicolon2) {
18610 *semicolon2++ = '\0';
18611 }
18612 if (!value2) {
18613 goto fail;
18614 }
18615 *value2++ = '\0';
18616 if ((!strcasecmp(name2, "maddr") && !maddrmatch) ||
18617 (!strcasecmp(name2, "ttl") && !ttlmatch) ||
18618 (!strcasecmp(name2, "user") && !usermatch) ||
18619 (!strcasecmp(name2, "method") && !methodmatch)) {
18620 goto fail;
18621 }
18622 }
18623 return 0;
18624
18625 fail:
18626 return 1;
18627 }
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640 static int sip_uri_headers_cmp(const char *input1, const char *input2)
18641 {
18642 char *headers1 = NULL;
18643 char *headers2 = NULL;
18644 int zerolength1 = 0;
18645 int zerolength2 = 0;
18646 int different = 0;
18647 char *header1;
18648
18649 if (ast_strlen_zero(input1)) {
18650 zerolength1 = 1;
18651 } else {
18652 headers1 = ast_strdupa(input1);
18653 }
18654
18655 if (ast_strlen_zero(input2)) {
18656 zerolength2 = 1;
18657 } else {
18658 headers2 = ast_strdupa(input2);
18659 }
18660
18661 if ((zerolength1 && !zerolength2) ||
18662 (zerolength2 && !zerolength1))
18663 return 1;
18664
18665 if (zerolength1 && zerolength2)
18666 return 0;
18667
18668
18669
18670
18671
18672 if (strlen(headers1) != strlen(headers2)) {
18673 return 1;
18674 }
18675
18676 for (header1 = strsep(&headers1, "&"); header1; header1 = strsep(&headers1, "&")) {
18677 if (!strcasestr(headers2, header1)) {
18678 different = 1;
18679 break;
18680 }
18681 }
18682
18683 return different;
18684 }
18685
18686 static int sip_uri_cmp(const char *input1, const char *input2)
18687 {
18688 char *uri1 = ast_strdupa(input1);
18689 char *uri2 = ast_strdupa(input2);
18690 char *host1;
18691 char *host2;
18692 char *params1;
18693 char *params2;
18694 char *headers1;
18695 char *headers2;
18696
18697
18698
18699
18700 strsep(&uri1, ":");
18701 strsep(&uri2, ":");
18702
18703 if ((host1 = strchr(uri1, '@'))) {
18704 *host1++ = '\0';
18705 }
18706 if ((host2 = strchr(uri2, '@'))) {
18707 *host2++ = '\0';
18708 }
18709
18710
18711
18712
18713 if ((host1 && !host2) ||
18714 (host2 && !host1) ||
18715 (host1 && host2 && strcmp(uri1, uri2))) {
18716 return 1;
18717 }
18718
18719 if (!host1)
18720 host1 = uri1;
18721 if (!host2)
18722 host2 = uri2;
18723
18724
18725
18726
18727
18728 if ((params1 = strchr(host1, ';'))) {
18729 *params1++ = '\0';
18730 }
18731 if ((params2 = strchr(host2, ';'))) {
18732 *params2++ = '\0';
18733 }
18734
18735
18736
18737
18738 if ((headers1 = strchr(S_OR(params1, host1), '?'))) {
18739 *headers1++ = '\0';
18740 }
18741 if ((headers2 = strchr(S_OR(params2, host2), '?'))) {
18742 *headers2++ = '\0';
18743 }
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755 if (strcasecmp(host1, host2)) {
18756 return 1;
18757 }
18758
18759
18760 if (sip_uri_headers_cmp(headers1, headers2)) {
18761 return 1;
18762 }
18763
18764
18765 return sip_uri_params_cmp(params1, params2);
18766 }
18767
18768
18769 static int sip_t38_abort(const void *data)
18770 {
18771 struct sip_pvt *p = (struct sip_pvt *) data;
18772
18773 change_t38_state(p, T38_DISABLED);
18774 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
18775 p->t38id = -1;
18776 dialog_unref(p, "unref the dialog ptr from sip_t38_abort, because it held a dialog ptr");
18777
18778 return 0;
18779 }
18780
18781
18782
18783
18784
18785
18786
18787 static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct sockaddr_in *sin, int *recount, char *e, int *nounlock)
18788 {
18789 int res = 1;
18790 int gotdest;
18791 const char *p_replaces;
18792 char *replace_id = NULL;
18793 int refer_locked = 0;
18794 const char *required;
18795 unsigned int required_profile = 0;
18796 struct ast_channel *c = NULL;
18797 int reinvite = 0;
18798 int rtn;
18799
18800 const char *p_uac_se_hdr;
18801 const char *p_uac_min_se;
18802 int uac_max_se = -1;
18803 int uac_min_se = -1;
18804 int st_active = FALSE;
18805 int st_interval = 0;
18806 enum st_refresher st_ref;
18807 int dlg_min_se = -1;
18808 st_ref = SESSION_TIMER_REFRESHER_AUTO;
18809
18810
18811 if (!p->sipoptions) {
18812 const char *supported = get_header(req, "Supported");
18813 if (!ast_strlen_zero(supported))
18814 parse_sip_options(p, supported);
18815 }
18816
18817
18818 required = get_header(req, "Require");
18819 if (!ast_strlen_zero(required)) {
18820 required_profile = parse_sip_options(NULL, required);
18821 if (required_profile && !(required_profile & (SIP_OPT_REPLACES | SIP_OPT_TIMER))) {
18822
18823 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
18824 ast_log(LOG_WARNING, "Received SIP INVITE with unsupported required extension: %s\n", required);
18825 p->invitestate = INV_COMPLETED;
18826 if (!p->lastinvite)
18827 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18828 res = -1;
18829 goto request_invite_cleanup;
18830 }
18831 }
18832
18833
18834
18835 p->sipoptions |= required_profile;
18836 p->reqsipoptions = required_profile;
18837
18838
18839 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->invitestate != INV_TERMINATED && p->invitestate != INV_CONFIRMED)) {
18840
18841
18842
18843
18844
18845 int different;
18846 char *initial_rlPart2 = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
18847 char *this_rlPart2 = REQ_OFFSET_TO_STR(req, rlPart2);
18848 if (pedanticsipchecking)
18849 different = sip_uri_cmp(initial_rlPart2, this_rlPart2);
18850 else
18851 different = strcmp(initial_rlPart2, this_rlPart2);
18852 if (!different) {
18853 transmit_response(p, "482 Loop Detected", req);
18854 p->invitestate = INV_COMPLETED;
18855 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18856 res = 0;
18857 goto request_invite_cleanup;
18858 } else {
18859
18860
18861
18862
18863
18864
18865
18866 char *uri = ast_strdupa(this_rlPart2);
18867 char *at = strchr(uri, '@');
18868 char *peerorhost;
18869 ast_debug(2, "Potential spiral detected. Original RURI was %s, new RURI is %s\n", initial_rlPart2, this_rlPart2);
18870 transmit_response(p, "100 Trying", req);
18871 if (at) {
18872 *at = '\0';
18873 }
18874
18875 if ((peerorhost = strchr(uri, ':'))) {
18876 *peerorhost++ = '\0';
18877 }
18878 ast_string_field_set(p, theirtag, NULL);
18879
18880
18881 ast_string_field_set(p->owner, call_forward, peerorhost);
18882 ast_queue_control(p->owner, AST_CONTROL_BUSY);
18883 res = 0;
18884 goto request_invite_cleanup;
18885 }
18886 }
18887
18888 if (!req->ignore && p->pendinginvite) {
18889 if (!ast_test_flag(&p->flags[0], SIP_OUTGOING) && (p->invitestate == INV_COMPLETED || p->invitestate == INV_TERMINATED)) {
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900 __sip_ack(p, p->pendinginvite, 1, 0);
18901 } else {
18902
18903 p->glareinvite = seqno;
18904 if (p->rtp && find_sdp(req)) {
18905 struct sockaddr_in sin;
18906 if (get_ip_and_port_from_sdp(req, SDP_AUDIO, &sin)) {
18907 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Audio may not work properly on this call.\n");
18908 } else {
18909 ast_rtp_set_alt_peer(p->rtp, &sin);
18910 }
18911 if (p->vrtp) {
18912 if (get_ip_and_port_from_sdp(req, SDP_VIDEO, &sin)) {
18913 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Video may not work properly on this call.\n");
18914 } else {
18915 ast_rtp_set_alt_peer(p->vrtp, &sin);
18916 }
18917 }
18918 }
18919 transmit_response_reliable(p, "491 Request Pending", req);
18920 ast_debug(1, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
18921
18922 res = 0;
18923 goto request_invite_cleanup;
18924 }
18925 }
18926
18927 p_replaces = get_header(req, "Replaces");
18928 if (!ast_strlen_zero(p_replaces)) {
18929
18930 char *ptr;
18931 char *fromtag = NULL;
18932 char *totag = NULL;
18933 char *start, *to;
18934 int error = 0;
18935
18936 if (p->owner) {
18937 ast_debug(3, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
18938 transmit_response_reliable(p, "400 Bad request", req);
18939
18940 res = -1;
18941 goto request_invite_cleanup;
18942 }
18943
18944 if (sipdebug)
18945 ast_debug(3, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
18946
18947 replace_id = ast_strdupa(p_replaces);
18948 ast_uri_decode(replace_id);
18949
18950 if (!p->refer && !sip_refer_allocate(p)) {
18951 transmit_response_reliable(p, "500 Server Internal Error", req);
18952 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
18953 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18954 p->invitestate = INV_COMPLETED;
18955 res = -1;
18956 goto request_invite_cleanup;
18957 }
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968 replace_id = ast_skip_blanks(replace_id);
18969
18970 start = replace_id;
18971 while ( (ptr = strsep(&start, ";")) ) {
18972 ptr = ast_skip_blanks(ptr);
18973 if ( (to = strcasestr(ptr, "to-tag=") ) )
18974 totag = to + 7;
18975 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
18976 fromtag = to + 9;
18977 fromtag = strsep(&fromtag, "&");
18978 }
18979 }
18980
18981 if (sipdebug)
18982 ast_debug(4, "Invite/replaces: Will use Replace-Call-ID : %s Fromtag: %s Totag: %s\n", replace_id, fromtag ? fromtag : "<no from tag>", totag ? totag : "<no to tag>");
18983
18984
18985
18986
18987
18988 if ((p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
18989 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
18990 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
18991 error = 1;
18992 } else {
18993 refer_locked = 1;
18994 }
18995
18996
18997
18998
18999
19000
19001
19002 if (p->refer->refer_call == p) {
19003 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
19004 p->refer->refer_call = dialog_unref(p->refer->refer_call, "unref dialog p->refer->refer_call");
19005 transmit_response_reliable(p, "400 Bad request", req);
19006 error = 1;
19007 }
19008
19009 if (!error && !p->refer->refer_call->owner) {
19010
19011 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
19012
19013 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replace)", req);
19014 error = 1;
19015 }
19016
19017 if (!error && 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 ) {
19018 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
19019 transmit_response_reliable(p, "603 Declined (Replaces)", req);
19020 error = 1;
19021 }
19022
19023 if (error) {
19024 append_history(p, "Xfer", "INVITE/Replace Failed.");
19025 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19026 sip_pvt_unlock(p);
19027 if (p->refer->refer_call) {
19028 sip_pvt_unlock(p->refer->refer_call);
19029 if (p->refer->refer_call->owner) {
19030 ast_channel_unlock(p->refer->refer_call->owner);
19031 }
19032 }
19033 refer_locked = 0;
19034 p->invitestate = INV_COMPLETED;
19035 res = -1;
19036 goto request_invite_cleanup;
19037 }
19038 }
19039
19040
19041
19042
19043 if (!req->ignore) {
19044 int newcall = (p->initreq.headers ? TRUE : FALSE);
19045
19046 if (sip_cancel_destroy(p))
19047 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
19048
19049 p->pendinginvite = seqno;
19050 check_via(p, req);
19051
19052 copy_request(&p->initreq, req);
19053 if (sipdebug)
19054 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
19055 if (!p->owner) {
19056 if (debug)
19057 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
19058 if (newcall)
19059 append_history(p, "Invite", "New call: %s", p->callid);
19060 parse_ok_contact(p, req);
19061 } else {
19062 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
19063
19064 if (find_sdp(req)) {
19065 if (process_sdp(p, req, SDP_T38_INITIATE)) {
19066 transmit_response_reliable(p, "488 Not acceptable here", req);
19067 if (!p->lastinvite)
19068 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19069 res = -1;
19070 goto request_invite_cleanup;
19071 }
19072 ast_queue_control(p->owner, AST_CONTROL_SRCUPDATE);
19073 } else {
19074 p->jointcapability = p->capability;
19075 ast_debug(1, "Hm.... No sdp for the moment\n");
19076 }
19077 if (p->do_history)
19078 append_history(p, "ReInv", "Re-invite received");
19079 }
19080 } else if (debug)
19081 ast_verbose("Ignoring this INVITE request\n");
19082
19083
19084 if (!p->lastinvite && !req->ignore && !p->owner) {
19085
19086
19087 res = check_user(p, req, SIP_INVITE, e, XMIT_RELIABLE, sin);
19088 if (res == AUTH_CHALLENGE_SENT) {
19089 p->invitestate = INV_COMPLETED;
19090 res = 0;
19091 goto request_invite_cleanup;
19092 }
19093 if (res < 0) {
19094 if (res == AUTH_FAKE_AUTH) {
19095 ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
19096 transmit_fake_auth_response(p, SIP_INVITE, req, XMIT_RELIABLE);
19097 } else {
19098 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
19099 transmit_response_reliable(p, "403 Forbidden", req);
19100 }
19101 p->invitestate = INV_COMPLETED;
19102 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19103 ast_string_field_set(p, theirtag, NULL);
19104 res = 0;
19105 goto request_invite_cleanup;
19106 }
19107
19108
19109 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && !p->udptl && (p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr))) {
19110 p->t38_maxdatagram = global_t38_maxdatagram;
19111 set_t38_capabilities(p);
19112 }
19113
19114
19115 if (find_sdp(req)) {
19116 if (process_sdp(p, req, SDP_T38_INITIATE)) {
19117
19118 transmit_response_reliable(p, "488 Not acceptable here", req);
19119 p->invitestate = INV_COMPLETED;
19120 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19121 ast_debug(1, "No compatible codecs for this SIP call.\n");
19122 res = -1;
19123 goto request_invite_cleanup;
19124 }
19125 } else {
19126 p->jointcapability = p->capability;
19127 ast_debug(2, "No SDP in Invite, third party call control\n");
19128 }
19129
19130
19131
19132 if (p->owner)
19133 ast_queue_frame(p->owner, &ast_null_frame);
19134
19135
19136
19137 if (ast_strlen_zero(p->context))
19138 ast_string_field_set(p, context, default_context);
19139
19140
19141
19142 ast_debug(1, "Checking SIP call limits for device %s\n", p->username);
19143 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
19144 if (res < 0) {
19145 ast_log(LOG_NOTICE, "Failed to place call for device %s, too many calls\n", p->username);
19146 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
19147 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19148 p->invitestate = INV_COMPLETED;
19149 }
19150 res = 0;
19151 goto request_invite_cleanup;
19152 }
19153 gotdest = get_destination(p, NULL);
19154 get_rdnis(p, NULL);
19155 extract_uri(p, req);
19156 build_contact(p);
19157
19158 if (p->rtp) {
19159 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
19160 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
19161 }
19162
19163 if (!replace_id && gotdest) {
19164 if (gotdest == 1 && ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP))
19165 transmit_response_reliable(p, "484 Address Incomplete", req);
19166 else {
19167 char *decoded_exten = ast_strdupa(p->exten);
19168
19169 transmit_response_reliable(p, "404 Not Found", req);
19170 ast_uri_decode(decoded_exten);
19171 ast_log(LOG_NOTICE, "Call from '%s' to extension"
19172 " '%s' rejected because extension not found in context '%s'.\n",
19173 S_OR(p->username, p->peername), decoded_exten, p->context);
19174 }
19175 p->invitestate = INV_COMPLETED;
19176 update_call_counter(p, DEC_CALL_LIMIT);
19177 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19178 res = 0;
19179 goto request_invite_cleanup;
19180 } else {
19181
19182
19183
19184 if (ast_strlen_zero(p->exten))
19185 ast_string_field_set(p, exten, "s");
19186
19187
19188 make_our_tag(p->tag, sizeof(p->tag));
19189
19190 c = sip_new(p, AST_STATE_DOWN, S_OR(p->peername, NULL));
19191 *recount = 1;
19192
19193
19194 build_route(p, req, 0);
19195
19196 if (c) {
19197
19198 ast_channel_lock(c);
19199 }
19200 }
19201 } else {
19202 if (sipdebug) {
19203 if (!req->ignore)
19204 ast_debug(2, "Got a SIP re-invite for call %s\n", p->callid);
19205 else
19206 ast_debug(2, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
19207 }
19208 if (!req->ignore)
19209 reinvite = 1;
19210 c = p->owner;
19211 }
19212
19213
19214 if (p->sipoptions & SIP_OPT_TIMER) {
19215
19216
19217 ast_debug(2, "Incoming INVITE with 'timer' option enabled\n");
19218
19219
19220 if (!p->stimer)
19221 sip_st_alloc(p);
19222
19223
19224 p_uac_se_hdr = get_header(req, "Session-Expires");
19225 if (!ast_strlen_zero(p_uac_se_hdr)) {
19226 rtn = parse_session_expires(p_uac_se_hdr, &uac_max_se, &st_ref);
19227 if (rtn != 0) {
19228 transmit_response_reliable(p, "400 Session-Expires Invalid Syntax", req);
19229 p->invitestate = INV_COMPLETED;
19230 if (!p->lastinvite) {
19231 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19232 }
19233 res = -1;
19234 goto request_invite_cleanup;
19235 }
19236 }
19237
19238
19239 p_uac_min_se = get_header(req, "Min-SE");
19240 if (!ast_strlen_zero(p_uac_min_se)) {
19241 rtn = parse_minse(p_uac_min_se, &uac_min_se);
19242 if (rtn != 0) {
19243 transmit_response_reliable(p, "400 Min-SE Invalid Syntax", req);
19244 p->invitestate = INV_COMPLETED;
19245 if (!p->lastinvite) {
19246 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19247 }
19248 res = -1;
19249 goto request_invite_cleanup;
19250 }
19251 }
19252
19253 dlg_min_se = st_get_se(p, FALSE);
19254 switch (st_get_mode(p)) {
19255 case SESSION_TIMER_MODE_ACCEPT:
19256 case SESSION_TIMER_MODE_ORIGINATE:
19257 if (uac_max_se > 0 && uac_max_se < dlg_min_se) {
19258 transmit_response_with_minse(p, "422 Session Interval Too Small", req, dlg_min_se);
19259 p->invitestate = INV_COMPLETED;
19260 if (!p->lastinvite) {
19261 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19262 }
19263 res = -1;
19264 goto request_invite_cleanup;
19265 }
19266
19267 p->stimer->st_active_peer_ua = TRUE;
19268 st_active = TRUE;
19269 if (st_ref == SESSION_TIMER_REFRESHER_AUTO) {
19270 st_ref = st_get_refresher(p);
19271 }
19272
19273 if (uac_max_se > 0) {
19274 int dlg_max_se = st_get_se(p, TRUE);
19275 if (dlg_max_se >= uac_min_se) {
19276 st_interval = (uac_max_se < dlg_max_se) ? uac_max_se : dlg_max_se;
19277 } else {
19278 st_interval = uac_max_se;
19279 }
19280 } else {
19281
19282 st_interval = global_max_se;
19283 }
19284 break;
19285
19286 case SESSION_TIMER_MODE_REFUSE:
19287 if (p->reqsipoptions & SIP_OPT_TIMER) {
19288 transmit_response_with_unsupported(p, "420 Option Disabled", req, required);
19289 ast_log(LOG_WARNING, "Received SIP INVITE with supported but disabled option: %s\n", required);
19290 p->invitestate = INV_COMPLETED;
19291 if (!p->lastinvite) {
19292 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19293 }
19294 res = -1;
19295 goto request_invite_cleanup;
19296 }
19297 break;
19298
19299 default:
19300 ast_log(LOG_ERROR, "Internal Error %d at %s:%d\n", st_get_mode(p), __FILE__, __LINE__);
19301 break;
19302 }
19303 } else {
19304
19305
19306
19307 switch (st_get_mode(p)) {
19308 case SESSION_TIMER_MODE_ORIGINATE:
19309 st_active = TRUE;
19310 st_interval = st_get_se(p, TRUE);
19311 st_ref = SESSION_TIMER_REFRESHER_UAS;
19312 p->stimer->st_active_peer_ua = FALSE;
19313 break;
19314
19315 default:
19316 break;
19317 }
19318 }
19319
19320 if (reinvite == 0) {
19321
19322 if (st_active == TRUE) {
19323 p->stimer->st_active = TRUE;
19324 p->stimer->st_interval = st_interval;
19325 p->stimer->st_ref = st_ref;
19326 start_session_timer(p);
19327 }
19328 } else {
19329 if (p->stimer->st_active == TRUE) {
19330
19331
19332
19333 ast_debug (2, "Restarting session-timers on a refresh - %s\n", p->callid);
19334
19335
19336 if (st_interval > 0) {
19337 p->stimer->st_interval = st_interval;
19338 p->stimer->st_ref = st_ref;
19339 }
19340
19341 restart_session_timer(p);
19342 if (p->stimer->st_expirys > 0) {
19343 p->stimer->st_expirys--;
19344 }
19345 }
19346 }
19347
19348 if (!req->ignore && p)
19349 p->lastinvite = seqno;
19350
19351 if (replace_id) {
19352
19353 if (sipdebug)
19354 ast_debug(4, "Sending this call to the invite/replcaes handler %s\n", p->callid);
19355 res = handle_invite_replaces(p, req, debug, seqno, sin, nounlock);
19356 refer_locked = 0;
19357 goto request_invite_cleanup;
19358
19359 }
19360
19361
19362 if (c) {
19363 enum ast_channel_state c_state = c->_state;
19364
19365 if (c_state != AST_STATE_UP && reinvite &&
19366 (p->invitestate == INV_TERMINATED || p->invitestate == INV_CONFIRMED)) {
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376 c_state = AST_STATE_UP;
19377 }
19378
19379 switch(c_state) {
19380 case AST_STATE_DOWN:
19381 ast_debug(2, "%s: New call is still down.... Trying... \n", c->name);
19382 transmit_provisional_response(p, "100 Trying", req, 0);
19383 p->invitestate = INV_PROCEEDING;
19384 ast_setstate(c, AST_STATE_RING);
19385 if (strcmp(p->exten, ast_pickup_ext())) {
19386 enum ast_pbx_result result;
19387
19388 result = ast_pbx_start(c);
19389
19390 switch(result) {
19391 case AST_PBX_FAILED:
19392 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
19393 p->invitestate = INV_COMPLETED;
19394 transmit_response_reliable(p, "503 Unavailable", req);
19395 break;
19396 case AST_PBX_CALL_LIMIT:
19397 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
19398 p->invitestate = INV_COMPLETED;
19399 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
19400 break;
19401 case AST_PBX_SUCCESS:
19402
19403 break;
19404 }
19405
19406 if (result) {
19407
19408
19409 ast_channel_unlock(c);
19410 sip_pvt_unlock(p);
19411 ast_hangup(c);
19412 sip_pvt_lock(p);
19413 c = NULL;
19414 }
19415 } else {
19416 ast_channel_unlock(c);
19417 *nounlock = 1;
19418 if (ast_pickup_call(c)) {
19419 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
19420 transmit_response_reliable(p, "503 Unavailable", req);
19421 sip_alreadygone(p);
19422
19423 sip_pvt_unlock(p);
19424 c->hangupcause = AST_CAUSE_CALL_REJECTED;
19425 } else {
19426 sip_pvt_unlock(p);
19427 ast_setstate(c, AST_STATE_DOWN);
19428 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
19429 }
19430 p->invitestate = INV_COMPLETED;
19431 ast_hangup(c);
19432 sip_pvt_lock(p);
19433 c = NULL;
19434 }
19435 break;
19436 case AST_STATE_RING:
19437 transmit_provisional_response(p, "100 Trying", req, 0);
19438 p->invitestate = INV_PROCEEDING;
19439 break;
19440 case AST_STATE_RINGING:
19441 transmit_provisional_response(p, "180 Ringing", req, 0);
19442 p->invitestate = INV_PROCEEDING;
19443 break;
19444 case AST_STATE_UP:
19445 ast_debug(2, "%s: This call is UP.... \n", c->name);
19446
19447 transmit_response(p, "100 Trying", req);
19448
19449 if (p->t38.state == T38_PEER_REINVITE) {
19450 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."));
19451 } else if (p->t38.state == T38_ENABLED) {
19452 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
19453 transmit_response_with_t38_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)));
19454 } else if (p->t38.state == T38_DISABLED) {
19455
19456 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
19457 transmit_response_with_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)), p->session_modify == TRUE ? FALSE:TRUE);
19458 }
19459
19460 p->invitestate = INV_TERMINATED;
19461 break;
19462 default:
19463 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
19464 transmit_response(p, "100 Trying", req);
19465 break;
19466 }
19467 } else {
19468 if (p && (p->autokillid == -1)) {
19469 const char *msg;
19470
19471 if (!p->jointcapability)
19472 msg = "488 Not Acceptable Here (codec error)";
19473 else {
19474 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
19475 msg = "503 Unavailable";
19476 }
19477 transmit_response_reliable(p, msg, req);
19478 p->invitestate = INV_COMPLETED;
19479 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19480 }
19481 }
19482
19483 request_invite_cleanup:
19484
19485 if (refer_locked && p->refer && p->refer->refer_call) {
19486 sip_pvt_unlock(p->refer->refer_call);
19487 if (p->refer->refer_call->owner) {
19488 ast_channel_unlock(p->refer->refer_call->owner);
19489 }
19490 }
19491
19492 return res;
19493 }
19494
19495
19496
19497 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno)
19498 {
19499 struct sip_dual target;
19500
19501 int res = 0;
19502 struct sip_pvt *targetcall_pvt;
19503
19504
19505 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
19506 transferer->refer->replaces_callid_fromtag))) {
19507 if (transferer->refer->localtransfer) {
19508
19509 transmit_response(transferer, "202 Accepted", req);
19510
19511
19512 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
19513 append_history(transferer, "Xfer", "Refer failed");
19514 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
19515 transferer->refer->status = REFER_FAILED;
19516 return -1;
19517 }
19518
19519 ast_debug(3, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
19520 return 0;
19521 }
19522
19523
19524 transmit_response(transferer, "202 Accepted", req);
19525 append_history(transferer, "Xfer", "Refer accepted");
19526 if (!targetcall_pvt->owner) {
19527 ast_debug(4, "SIP attended transfer: Error: No owner of target call\n");
19528
19529 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
19530 append_history(transferer, "Xfer", "Refer failed");
19531 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
19532 transferer->refer->status = REFER_FAILED;
19533 sip_pvt_unlock(targetcall_pvt);
19534 if (targetcall_pvt)
19535 ao2_t_ref(targetcall_pvt, -1, "Drop targetcall_pvt pointer");
19536 return -1;
19537 }
19538
19539
19540 target.chan1 = targetcall_pvt->owner;
19541 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
19542
19543 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
19544
19545 if (target.chan2)
19546 ast_debug(4, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
19547 else if (target.chan1->_state != AST_STATE_RING)
19548 ast_debug(4, "SIP attended transfer: Error: No target channel\n");
19549 else
19550 ast_debug(4, "SIP attended transfer: Attempting transfer in ringing state\n");
19551 }
19552
19553
19554 if (sipdebug) {
19555 if (current->chan2)
19556 ast_debug(4, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
19557 else
19558 ast_debug(4, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
19559 }
19560
19561 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
19562
19563
19564 manager_event(EVENT_FLAG_CALL, "Transfer", "TransferMethod: SIP\r\nTransferType: Attended\r\nChannel: %s\r\nUniqueid: %s\r\nSIP-Callid: %s\r\nTargetChannel: %s\r\nTargetUniqueid: %s\r\n",
19565 transferer->owner->name,
19566 transferer->owner->uniqueid,
19567 transferer->callid,
19568 target.chan1->name,
19569 target.chan1->uniqueid);
19570 res = attempt_transfer(current, &target);
19571 sip_pvt_unlock(targetcall_pvt);
19572 if (res) {
19573
19574 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
19575 append_history(transferer, "Xfer", "Refer failed");
19576 if (targetcall_pvt->owner)
19577 ast_channel_unlock(targetcall_pvt->owner);
19578 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
19579 } else {
19580
19581
19582
19583 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
19584 append_history(transferer, "Xfer", "Refer succeeded");
19585 transferer->refer->status = REFER_200OK;
19586 if (targetcall_pvt->owner) {
19587 ast_debug(1, "SIP attended transfer: Unlocking channel %s\n", targetcall_pvt->owner->name);
19588 ast_channel_unlock(targetcall_pvt->owner);
19589 }
19590 }
19591 if (targetcall_pvt)
19592 ao2_t_ref(targetcall_pvt, -1, "drop targetcall_pvt");
19593 return 1;
19594 }
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
19661 {
19662 struct sip_dual current;
19663
19664
19665 int res = 0;
19666 current.req.data = NULL;
19667
19668 if (req->debug)
19669 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");
19670
19671 if (!p->owner) {
19672
19673
19674 ast_debug(3, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
19675 transmit_response(p, "603 Declined (No dialog)", req);
19676 if (!req->ignore) {
19677 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
19678 sip_alreadygone(p);
19679 p->needdestroy = 1;
19680 }
19681 return 0;
19682 }
19683
19684
19685
19686 if (p->allowtransfer == TRANSFER_CLOSED ) {
19687
19688 transmit_response(p, "603 Declined (policy)", req);
19689 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
19690
19691 return 0;
19692 }
19693
19694 if (!req->ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
19695
19696 transmit_response(p, "491 Request pending", req);
19697 append_history(p, "Xfer", "Refer failed. Request pending.");
19698 return 0;
19699 }
19700
19701
19702 if (!p->refer && !sip_refer_allocate(p)) {
19703 transmit_response(p, "500 Internal Server Error", req);
19704 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
19705 return -3;
19706 }
19707
19708 res = get_refer_info(p, req);
19709
19710 p->refer->status = REFER_SENT;
19711
19712 if (res != 0) {
19713 switch (res) {
19714 case -2:
19715 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
19716 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
19717 if (req->debug)
19718 ast_debug(1, "SIP transfer to black hole can't be handled (no refer-to: )\n");
19719 break;
19720 case -3:
19721 transmit_response(p, "603 Declined (Non sip: uri)", req);
19722 append_history(p, "Xfer", "Refer failed. Non SIP uri");
19723 if (req->debug)
19724 ast_debug(1, "SIP transfer to non-SIP uri denied\n");
19725 break;
19726 default:
19727
19728 transmit_response(p, "202 Accepted", req);
19729 append_history(p, "Xfer", "Refer failed. Bad extension.");
19730 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
19731 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
19732 if (req->debug)
19733 ast_debug(1, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
19734 break;
19735 }
19736 return 0;
19737 }
19738 if (ast_strlen_zero(p->context))
19739 ast_string_field_set(p, context, default_context);
19740
19741
19742 if (allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
19743 p->refer->localtransfer = 1;
19744 if (sipdebug)
19745 ast_debug(3, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
19746 } else if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
19747
19748 p->refer->localtransfer = 1;
19749 } else if (sipdebug)
19750 ast_debug(3, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
19751
19752
19753
19754 if (req->ignore)
19755 return res;
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783 current.chan1 = p->owner;
19784
19785
19786 current.chan2 = ast_bridged_channel(current.chan1);
19787
19788 if (sipdebug)
19789 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>");
19790
19791 if (!current.chan2 && !p->refer->attendedtransfer) {
19792
19793
19794
19795 if (sipdebug)
19796 ast_debug(3, "Refused SIP transfer on non-bridged channel.\n");
19797 p->refer->status = REFER_FAILED;
19798 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
19799 transmit_response(p, "603 Declined", req);
19800 return -1;
19801 }
19802
19803 if (current.chan2) {
19804 if (sipdebug)
19805 ast_debug(4, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
19806
19807 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
19808 }
19809
19810 ast_set_flag(&p->flags[0], SIP_GOTREFER);
19811
19812
19813 if (p->refer->attendedtransfer) {
19814 if ((res = local_attended_transfer(p, ¤t, req, seqno)))
19815 return res;
19816
19817 if (sipdebug)
19818 ast_debug(4, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
19819
19820 }
19821
19822
19823
19824 if (p->refer->localtransfer && !strcmp(p->refer->refer_to, ast_parking_ext())) {
19825
19826 *nounlock = 1;
19827 ast_channel_unlock(current.chan1);
19828 copy_request(¤t.req, req);
19829 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
19830 p->refer->status = REFER_200OK;
19831 append_history(p, "Xfer", "REFER to call parking.");
19832 manager_event(EVENT_FLAG_CALL, "Transfer", "TransferMethod: SIP\r\nTransferType: Blind\r\nChannel: %s\r\nUniqueid: %s\r\nSIP-Callid: %s\r\nTargetChannel: %s\r\nTargetUniqueid: %s\r\nTransferExten: %s\r\nTransfer2Parking: Yes\r\n",
19833 current.chan1->name,
19834 current.chan1->uniqueid,
19835 p->callid,
19836 current.chan2->name,
19837 current.chan2->uniqueid,
19838 p->refer->refer_to);
19839 if (sipdebug)
19840 ast_debug(4, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
19841 sip_park(current.chan2, current.chan1, req, seqno);
19842 return res;
19843 }
19844
19845
19846 transmit_response(p, "202 Accepted", req);
19847
19848 if (current.chan1 && current.chan2) {
19849 ast_debug(3, "chan1->name: %s\n", current.chan1->name);
19850 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
19851 }
19852 if (current.chan2) {
19853 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
19854 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
19855 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
19856
19857 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
19858
19859 if (p->refer->referred_by)
19860 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
19861 }
19862
19863 if (!ast_strlen_zero(p->refer->replaces_callid)) {
19864 char tempheader[SIPBUFSIZE];
19865 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
19866 p->refer->replaces_callid_totag ? ";to-tag=" : "",
19867 p->refer->replaces_callid_totag,
19868 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
19869 p->refer->replaces_callid_fromtag);
19870 if (current.chan2)
19871 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
19872 }
19873
19874
19875 *nounlock = 1;
19876 ast_channel_unlock(current.chan1);
19877
19878
19879
19880
19881 if (!p->refer->attendedtransfer)
19882 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
19883
19884
19885
19886
19887
19888 if (!current.chan2) {
19889
19890
19891
19892
19893
19894
19895
19896 p->refer->status = REFER_FAILED;
19897 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
19898 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
19899 append_history(p, "Xfer", "Refer failed (only bridged calls).");
19900 return -1;
19901 }
19902 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
19903
19904
19905
19906
19907 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
19908
19909 if (!res) {
19910 manager_event(EVENT_FLAG_CALL, "Transfer", "TransferMethod: SIP\r\nTransferType: Blind\r\nChannel: %s\r\nUniqueid: %s\r\nSIP-Callid: %s\r\nTargetChannel: %s\r\nTargetUniqueid: %s\r\nTransferExten: %s\r\nTransferContext: %s\r\n",
19911 current.chan1->name,
19912 current.chan1->uniqueid,
19913 p->callid,
19914 current.chan2->name,
19915 current.chan2->uniqueid,
19916 p->refer->refer_to, p->refer->refer_to_context);
19917
19918 ast_debug(3, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
19919 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
19920 if (p->refer->localtransfer)
19921 p->refer->status = REFER_200OK;
19922 if (p->owner)
19923 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
19924 append_history(p, "Xfer", "Refer succeeded.");
19925 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
19926
19927
19928 res = 0;
19929 } else {
19930 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
19931 ast_debug(3, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
19932 append_history(p, "Xfer", "Refer failed.");
19933
19934 p->refer->status = REFER_FAILED;
19935 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
19936 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
19937 res = -1;
19938 }
19939 return res;
19940 }
19941
19942
19943 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
19944 {
19945
19946 check_via(p, req);
19947 sip_alreadygone(p);
19948
19949
19950
19951
19952
19953
19954 if (p->invitestate == INV_TERMINATED)
19955 __sip_pretend_ack(p);
19956 else
19957 p->invitestate = INV_CANCELLED;
19958
19959 if (p->owner && p->owner->_state == AST_STATE_UP) {
19960
19961 transmit_response(p, "200 OK", req);
19962 ast_debug(1, "Got CANCEL on an answered call. Ignoring... \n");
19963 return 0;
19964 }
19965
19966 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
19967 update_call_counter(p, DEC_CALL_LIMIT);
19968
19969 stop_media_flows(p);
19970 if (p->owner)
19971 ast_queue_hangup(p->owner);
19972 else
19973 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19974 if (p->initreq.len > 0) {
19975 struct sip_pkt *pkt, *prev_pkt;
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987 for (pkt = p->packets, prev_pkt = NULL; pkt; prev_pkt = pkt, pkt = pkt->next) {
19988 if (pkt->seqno == p->lastinvite && pkt->response_code == 487) {
19989 AST_SCHED_DEL(sched, pkt->retransid);
19990 UNLINK(pkt, p->packets, prev_pkt);
19991 ast_free(pkt);
19992 break;
19993 }
19994 }
19995 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
19996 transmit_response(p, "200 OK", req);
19997 return 1;
19998 } else {
19999 transmit_response(p, "481 Call Leg Does Not Exist", req);
20000 return 0;
20001 }
20002 }
20003
20004 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
20005 {
20006 struct sip_pvt *p = chan->tech_pvt;
20007 char *all = "", *parse = ast_strdupa(preparse);
20008 int res = 0;
20009 AST_DECLARE_APP_ARGS(args,
20010 AST_APP_ARG(param);
20011 AST_APP_ARG(type);
20012 AST_APP_ARG(field);
20013 );
20014 AST_STANDARD_APP_ARGS(args, parse);
20015
20016
20017 if (!IS_SIP_TECH(chan->tech)) {
20018 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
20019 return 0;
20020 }
20021
20022 memset(buf, 0, buflen);
20023
20024 if (p == NULL) {
20025 return -1;
20026 }
20027
20028 if (!strcasecmp(args.param, "peerip")) {
20029 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", buflen);
20030 } else if (!strcasecmp(args.param, "recvip")) {
20031 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", buflen);
20032 } else if (!strcasecmp(args.param, "from")) {
20033 ast_copy_string(buf, p->from, buflen);
20034 } else if (!strcasecmp(args.param, "uri")) {
20035 ast_copy_string(buf, p->uri, buflen);
20036 } else if (!strcasecmp(args.param, "useragent")) {
20037 ast_copy_string(buf, p->useragent, buflen);
20038 } else if (!strcasecmp(args.param, "peername")) {
20039 ast_copy_string(buf, p->peername, buflen);
20040 } else if (!strcasecmp(args.param, "t38passthrough")) {
20041 ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
20042 } else if (!strcasecmp(args.param, "rtpdest")) {
20043 struct sockaddr_in sin;
20044 struct ast_rtp *stream = NULL;
20045
20046 if (ast_strlen_zero(args.type))
20047 args.type = "audio";
20048
20049 if (!strcasecmp(args.type, "audio")) {
20050 stream = p->rtp;
20051 } else if (!strcasecmp(args.type, "video")) {
20052 stream = p->vrtp;
20053 } else if (!strcasecmp(args.type, "text")) {
20054 stream = p->trtp;
20055 } else {
20056 return -1;
20057 }
20058
20059 if (!stream) {
20060 return -1;
20061 }
20062
20063 ast_rtp_get_peer(stream, &sin);
20064 snprintf(buf, buflen, "%s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
20065 } else if (!strcasecmp(args.param, "rtpqos")) {
20066 struct ast_rtp_quality qos;
20067 struct ast_rtp *rtp = p->rtp;
20068
20069 memset(&qos, 0, sizeof(qos));
20070
20071 if (ast_strlen_zero(args.type))
20072 args.type = "audio";
20073 if (ast_strlen_zero(args.field))
20074 args.field = "all";
20075
20076 if (!strcasecmp(args.type, "AUDIO")) {
20077 all = ast_rtp_get_quality(rtp = p->rtp, &qos, RTPQOS_SUMMARY);
20078 } else if (!strcasecmp(args.type, "VIDEO")) {
20079 all = ast_rtp_get_quality(rtp = p->vrtp, &qos, RTPQOS_SUMMARY);
20080 } else if (!strcasecmp(args.type, "TEXT")) {
20081 all = ast_rtp_get_quality(rtp = p->trtp, &qos, RTPQOS_SUMMARY);
20082 } else {
20083 return -1;
20084 }
20085
20086 if (!strcasecmp(args.field, "local_ssrc"))
20087 snprintf(buf, buflen, "%u", qos.local_ssrc);
20088 else if (!strcasecmp(args.field, "local_lostpackets"))
20089 snprintf(buf, buflen, "%u", qos.local_lostpackets);
20090 else if (!strcasecmp(args.field, "local_jitter"))
20091 snprintf(buf, buflen, "%.0f", qos.local_jitter * 1000.0);
20092 else if (!strcasecmp(args.field, "local_count"))
20093 snprintf(buf, buflen, "%u", qos.local_count);
20094 else if (!strcasecmp(args.field, "remote_ssrc"))
20095 snprintf(buf, buflen, "%u", qos.remote_ssrc);
20096 else if (!strcasecmp(args.field, "remote_lostpackets"))
20097 snprintf(buf, buflen, "%u", qos.remote_lostpackets);
20098 else if (!strcasecmp(args.field, "remote_jitter"))
20099 snprintf(buf, buflen, "%.0f", qos.remote_jitter * 1000.0);
20100 else if (!strcasecmp(args.field, "remote_count"))
20101 snprintf(buf, buflen, "%u", qos.remote_count);
20102 else if (!strcasecmp(args.field, "rtt"))
20103 snprintf(buf, buflen, "%.0f", qos.rtt * 1000.0);
20104 else if (!strcasecmp(args.field, "all"))
20105 ast_copy_string(buf, all, buflen);
20106 else if (!ast_rtp_get_qos(rtp, args.field, buf, buflen))
20107 ;
20108 else {
20109 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
20110 return -1;
20111 }
20112 } else {
20113 res = -1;
20114 }
20115 return res;
20116 }
20117
20118
20119 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
20120 {
20121 struct ast_channel *c=NULL;
20122 int res;
20123 struct ast_channel *bridged_to;
20124
20125
20126 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !req->ignore) {
20127 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
20128 }
20129
20130 __sip_pretend_ack(p);
20131
20132 p->invitestate = INV_TERMINATED;
20133
20134 copy_request(&p->initreq, req);
20135 if (sipdebug)
20136 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
20137 check_via(p, req);
20138 sip_alreadygone(p);
20139
20140
20141 if (p->do_history || p->owner) {
20142 struct ast_channel *bridge = p->owner ? ast_bridged_channel(p->owner) : NULL;
20143 char *videoqos, *textqos;
20144
20145
20146
20147
20148 while (bridge && ast_channel_trylock(bridge)) {
20149 ast_channel_unlock(p->owner);
20150 do {
20151
20152 sip_pvt_unlock(p);
20153 usleep(1);
20154 sip_pvt_lock(p);
20155 } while (p->owner && ast_channel_trylock(p->owner));
20156 bridge = p->owner ? ast_bridged_channel(p->owner) : NULL;
20157 }
20158
20159 if (p->rtp) {
20160 if (p->do_history) {
20161 char *audioqos,
20162 *audioqos_jitter,
20163 *audioqos_loss,
20164 *audioqos_rtt;
20165
20166 audioqos = ast_rtp_get_quality(p->rtp, NULL, RTPQOS_SUMMARY);
20167 audioqos_jitter = ast_rtp_get_quality(p->rtp, NULL, RTPQOS_JITTER);
20168 audioqos_loss = ast_rtp_get_quality(p->rtp, NULL, RTPQOS_LOSS);
20169 audioqos_rtt = ast_rtp_get_quality(p->rtp, NULL, RTPQOS_RTT);
20170
20171 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
20172 append_history(p, "RTCPaudioJitter", "Quality:%s", audioqos_jitter);
20173 append_history(p, "RTCPaudioLoss", "Quality:%s", audioqos_loss);
20174 append_history(p, "RTCPaudioRTT", "Quality:%s", audioqos_rtt);
20175 }
20176
20177 if (p->owner) {
20178 ast_rtp_set_vars(p->owner, p->rtp);
20179 }
20180 }
20181
20182 if (bridge) {
20183 struct sip_pvt *q = bridge->tech_pvt;
20184
20185 if (IS_SIP_TECH(bridge->tech) && q && q->rtp)
20186 ast_rtp_set_vars(bridge, q->rtp);
20187 ast_channel_unlock(bridge);
20188 }
20189
20190 if (p->vrtp) {
20191 videoqos = ast_rtp_get_quality(p->vrtp, NULL, RTPQOS_SUMMARY);
20192 if (p->do_history)
20193 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
20194 if (p->owner)
20195 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", videoqos);
20196 }
20197
20198 if (p->trtp) {
20199 textqos = ast_rtp_get_quality(p->trtp, NULL, RTPQOS_SUMMARY);
20200 if (p->do_history)
20201 append_history(p, "RTCPtext", "Quality:%s", textqos);
20202 if (p->owner)
20203 pbx_builtin_setvar_helper(p->owner, "RTPTEXTQOS", textqos);
20204 }
20205 }
20206
20207 stop_media_flows(p);
20208 stop_session_timer(p);
20209
20210 if (!ast_strlen_zero(get_header(req, "Also"))) {
20211 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
20212 ast_inet_ntoa(p->recv.sin_addr));
20213 if (ast_strlen_zero(p->context))
20214 ast_string_field_set(p, context, default_context);
20215 res = get_also_info(p, req);
20216 if (!res) {
20217 c = p->owner;
20218 if (c) {
20219 bridged_to = ast_bridged_channel(c);
20220 if (bridged_to) {
20221
20222 ast_queue_control(c, AST_CONTROL_UNHOLD);
20223 ast_async_goto(bridged_to, p->context, p->refer->refer_to, 1);
20224 } else
20225 ast_queue_hangup(p->owner);
20226 }
20227 } else {
20228 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(p->recv.sin_addr));
20229 if (p->owner)
20230 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
20231 }
20232 } else if (p->owner) {
20233 ast_queue_hangup(p->owner);
20234 ast_debug(3, "Received bye, issuing owner hangup\n");
20235 } else {
20236 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20237 ast_debug(3, "Received bye, no owner, selfdestruct soon.\n");
20238 }
20239 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
20240 transmit_response(p, "200 OK", req);
20241
20242 return 1;
20243 }
20244
20245
20246 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
20247 {
20248 if (!req->ignore) {
20249 if (req->debug)
20250 ast_verbose("Receiving message!\n");
20251 receive_message(p, req);
20252 } else
20253 transmit_response(p, "202 Accepted", req);
20254 return 1;
20255 }
20256
20257 static void add_peer_mwi_subs(struct sip_peer *peer)
20258 {
20259 struct sip_mailbox *mailbox;
20260
20261 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
20262 mailbox->event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, peer,
20263 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
20264 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
20265 AST_EVENT_IE_END);
20266 }
20267 }
20268
20269
20270 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
20271 {
20272 int gotdest = 0;
20273 int res = 0;
20274 int firststate = AST_EXTENSION_REMOVED;
20275 struct sip_peer *authpeer = NULL;
20276 const char *eventheader = get_header(req, "Event");
20277 const char *acceptheader = get_header(req, "Accept");
20278 int resubscribe = (p->subscribed != NONE);
20279 char *temp, *event;
20280 struct ao2_iterator i;
20281
20282 if (p->initreq.headers) {
20283
20284 if (p->initreq.method != SIP_SUBSCRIBE) {
20285
20286
20287 transmit_response(p, "403 Forbidden (within dialog)", req);
20288
20289 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);
20290 return 0;
20291 } else if (req->debug) {
20292 if (resubscribe)
20293 ast_debug(1, "Got a re-subscribe on existing subscription %s\n", p->callid);
20294 else
20295 ast_debug(1, "Got a new subscription %s (possibly with auth)\n", p->callid);
20296 }
20297 }
20298
20299
20300
20301
20302 if (!global_allowsubscribe) {
20303 transmit_response(p, "403 Forbidden (policy)", req);
20304 p->needdestroy = 1;
20305 return 0;
20306 }
20307
20308 if (!req->ignore && !resubscribe) {
20309 const char *to = get_header(req, "To");
20310 char totag[128];
20311
20312
20313 if (!ast_strlen_zero(to) && gettag(req, "To", totag, sizeof(totag))) {
20314 if (req->debug)
20315 ast_verbose("Received resubscription for a dialog we no longer know about. Telling remote side to subscribe again.\n");
20316 transmit_response(p, "481 Subscription does not exist", req);
20317 p->needdestroy = 1;
20318 return 0;
20319 }
20320
20321
20322 if (req->debug)
20323 ast_verbose("Creating new subscription\n");
20324
20325 copy_request(&p->initreq, req);
20326 if (sipdebug)
20327 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
20328 check_via(p, req);
20329 build_route(p, req, 0);
20330 } else if (req->debug && req->ignore)
20331 ast_verbose("Ignoring this SUBSCRIBE request\n");
20332
20333
20334 if (ast_strlen_zero(eventheader)) {
20335 transmit_response(p, "489 Bad Event", req);
20336 ast_debug(2, "Received SIP subscribe for unknown event package: <none>\n");
20337 p->needdestroy = 1;
20338 return 0;
20339 }
20340
20341 if ( (strchr(eventheader, ';'))) {
20342 event = ast_strdupa(eventheader);
20343 temp = strchr(event, ';');
20344 *temp = '\0';
20345
20346 } else
20347 event = (char *) eventheader;
20348
20349
20350 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, &authpeer);
20351
20352 if (res == AUTH_CHALLENGE_SENT)
20353 return 0;
20354 if (res < 0) {
20355 if (res == AUTH_FAKE_AUTH) {
20356 ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
20357 transmit_fake_auth_response(p, SIP_SUBSCRIBE, req, XMIT_UNRELIABLE);
20358 } else {
20359 ast_log(LOG_NOTICE, "Failed to authenticate device %s for SUBSCRIBE\n", get_header(req, "From"));
20360 transmit_response_reliable(p, "403 Forbidden", req);
20361 }
20362 p->needdestroy = 1;
20363 return 0;
20364 }
20365
20366
20367
20368
20369
20370
20371
20372 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
20373 transmit_response(p, "403 Forbidden (policy)", req);
20374 p->needdestroy = 1;
20375 if (authpeer)
20376 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 1)");
20377 return 0;
20378 }
20379
20380 if (strcmp(event, "message-summary")) {
20381
20382 gotdest = get_destination(p, NULL);
20383 }
20384
20385
20386 parse_ok_contact(p, req);
20387
20388 build_contact(p);
20389 if (gotdest) {
20390 transmit_response(p, "404 Not Found", req);
20391 p->needdestroy = 1;
20392 if (authpeer)
20393 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 2)");
20394 return 0;
20395 }
20396
20397
20398 if (ast_strlen_zero(p->tag))
20399 make_our_tag(p->tag, sizeof(p->tag));
20400
20401 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
20402 unsigned int pidf_xml;
20403
20404 if (authpeer)
20405 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 2)");
20406
20407
20408
20409 pidf_xml = strstr(acceptheader, "application/pidf+xml") ? 1 : 0;
20410
20411
20412
20413 if (pidf_xml && strstr(p->useragent, "Polycom")) {
20414 p->subscribed = XPIDF_XML;
20415 } else if (pidf_xml) {
20416 p->subscribed = PIDF_XML;
20417 } else if (strstr(acceptheader, "application/dialog-info+xml")) {
20418 p->subscribed = DIALOG_INFO_XML;
20419
20420 } else if (strstr(acceptheader, "application/cpim-pidf+xml")) {
20421 p->subscribed = CPIM_PIDF_XML;
20422 } else if (strstr(acceptheader, "application/xpidf+xml")) {
20423 p->subscribed = XPIDF_XML;
20424 } else if (ast_strlen_zero(acceptheader)) {
20425 if (p->subscribed == NONE) {
20426 transmit_response(p, "489 Bad Event", req);
20427
20428 ast_log(LOG_WARNING, "SUBSCRIBE failure: no Accept header: pvt: stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
20429 p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
20430 p->needdestroy = 1;
20431 return 0;
20432 }
20433
20434
20435 } else {
20436
20437 char mybuf[200];
20438 snprintf(mybuf, sizeof(mybuf), "489 Bad Event (format %s)", acceptheader);
20439 transmit_response(p, mybuf, req);
20440
20441 ast_log(LOG_WARNING, "SUBSCRIBE failure: unrecognized format: '%s' pvt: subscribed: %d, stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
20442 acceptheader, (int)p->subscribed, p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
20443 p->needdestroy = 1;
20444 return 0;
20445 }
20446 } else if (!strcmp(event, "message-summary")) {
20447 if (!ast_strlen_zero(acceptheader) && strcmp(acceptheader, "application/simple-message-summary")) {
20448
20449 transmit_response(p, "406 Not Acceptable", req);
20450 ast_debug(2, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
20451 p->needdestroy = 1;
20452 if (authpeer)
20453 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 3)");
20454 return 0;
20455 }
20456
20457
20458
20459
20460
20461 if (!authpeer || AST_LIST_EMPTY(&authpeer->mailboxes)) {
20462 transmit_response(p, "404 Not found (no mailbox)", req);
20463 p->needdestroy = 1;
20464 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
20465 if (authpeer)
20466 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 4)");
20467 return 0;
20468 }
20469
20470 p->subscribed = MWI_NOTIFICATION;
20471 if (ast_test_flag(&authpeer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY)) {
20472 add_peer_mwi_subs(authpeer);
20473 }
20474 if (authpeer->mwipvt && authpeer->mwipvt != p) {
20475
20476 dialog_unlink_all(authpeer->mwipvt, TRUE, TRUE);
20477 authpeer->mwipvt = dialog_unref(authpeer->mwipvt, "unref dialog authpeer->mwipvt");
20478
20479 }
20480 if (authpeer->mwipvt)
20481 dialog_unref(authpeer->mwipvt, "Unref previously stored mwipvt dialog pointer");
20482 authpeer->mwipvt = dialog_ref(p, "setting peers' mwipvt to p");
20483 if (p->relatedpeer)
20484 unref_peer(p->relatedpeer, "Unref previously stored relatedpeer ptr");
20485 p->relatedpeer = ref_peer(authpeer, "setting dialog's relatedpeer pointer");
20486
20487 } else {
20488 transmit_response(p, "489 Bad Event", req);
20489 ast_debug(2, "Received SIP subscribe for unknown event package: %s\n", event);
20490 p->needdestroy = 1;
20491 if (authpeer)
20492 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 5)");
20493 return 0;
20494 }
20495
20496
20497 if (p->subscribed != MWI_NOTIFICATION && !resubscribe) {
20498 if (p->stateid > -1) {
20499 ast_extension_state_del(p->stateid, cb_extensionstate);
20500
20501 dialog_unref(p, "the extensionstate containing this dialog ptr was deleted");
20502 }
20503 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, dialog_ref(p,"copying dialog ptr into extension state struct"));
20504 }
20505
20506 if (!req->ignore && p)
20507 p->lastinvite = seqno;
20508 if (p && !p->needdestroy) {
20509 p->expiry = atoi(get_header(req, "Expires"));
20510
20511
20512 if (p->expiry > max_expiry)
20513 p->expiry = max_expiry;
20514 if (p->expiry < min_expiry && p->expiry > 0)
20515 p->expiry = min_expiry;
20516
20517 if (sipdebug) {
20518 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
20519 ast_debug(2, "Adding subscription for mailbox notification - peer %s\n", p->relatedpeer->name);
20520 else
20521 ast_debug(2, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
20522 }
20523 if (p->autokillid > -1 && sip_cancel_destroy(p))
20524 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20525 if (p->expiry > 0)
20526 sip_scheddestroy(p, (p->expiry + 10) * 1000);
20527
20528 if (p->subscribed == MWI_NOTIFICATION) {
20529 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
20530 transmit_response(p, "200 OK", req);
20531 if (p->relatedpeer) {
20532 ao2_lock(p->relatedpeer);
20533 sip_send_mwi_to_peer(p->relatedpeer, NULL, 0);
20534 ao2_unlock(p->relatedpeer);
20535 }
20536 } else {
20537 struct sip_pvt *p_old;
20538
20539 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
20540
20541 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_inet_ntoa(p->sa.sin_addr));
20542 transmit_response(p, "404 Not found", req);
20543 p->needdestroy = 1;
20544 return 0;
20545 }
20546 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
20547 transmit_response(p, "200 OK", req);
20548 transmit_state_notify(p, firststate, 1, FALSE);
20549 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
20550
20551 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
20552
20553
20554
20555
20556
20557
20558 i = ao2_iterator_init(dialogs, 0);
20559 while ((p_old = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
20560 if (p_old == p) {
20561 ao2_t_ref(p_old, -1, "toss dialog ptr from iterator_next before continue");
20562 continue;
20563 }
20564 if (p_old->initreq.method != SIP_SUBSCRIBE) {
20565 ao2_t_ref(p_old, -1, "toss dialog ptr from iterator_next before continue");
20566 continue;
20567 }
20568 if (p_old->subscribed == NONE) {
20569 ao2_t_ref(p_old, -1, "toss dialog ptr from iterator_next before continue");
20570 continue;
20571 }
20572 sip_pvt_lock(p_old);
20573 if (!strcmp(p_old->username, p->username)) {
20574 if (!strcmp(p_old->exten, p->exten) &&
20575 !strcmp(p_old->context, p->context)) {
20576 p_old->needdestroy = 1;
20577 sip_pvt_unlock(p_old);
20578 ao2_t_ref(p_old, -1, "toss dialog ptr from iterator_next before break");
20579 break;
20580 }
20581 }
20582 sip_pvt_unlock(p_old);
20583 ao2_t_ref(p_old, -1, "toss dialog ptr from iterator_next");
20584 }
20585 ao2_iterator_destroy(&i);
20586 }
20587 if (!p->expiry)
20588 p->needdestroy = 1;
20589 }
20590 return 1;
20591 }
20592
20593
20594 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e)
20595 {
20596 enum check_auth_result res;
20597
20598
20599 copy_request(&p->initreq, req);
20600 if (sipdebug)
20601 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
20602 check_via(p, req);
20603 if ((res = register_verify(p, sin, req, e)) < 0) {
20604 const char *reason;
20605
20606 switch (res) {
20607 case AUTH_SECRET_FAILED:
20608 reason = "Wrong password";
20609 break;
20610 case AUTH_USERNAME_MISMATCH:
20611 reason = "Username/auth name mismatch";
20612 break;
20613 case AUTH_NOT_FOUND:
20614 reason = "No matching peer found";
20615 break;
20616 case AUTH_UNKNOWN_DOMAIN:
20617 reason = "Not a local domain";
20618 break;
20619 case AUTH_PEER_NOT_DYNAMIC:
20620 reason = "Peer is not supposed to register";
20621 break;
20622 case AUTH_ACL_FAILED:
20623 reason = "Device does not match ACL";
20624 break;
20625 case AUTH_BAD_TRANSPORT:
20626 reason = "Device not configured to use this transport type";
20627 break;
20628 default:
20629 reason = "Unknown failure";
20630 break;
20631 }
20632 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
20633 get_header(req, "To"), ast_inet_ntoa(sin->sin_addr),
20634 reason);
20635 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
20636 } else
20637 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
20638
20639 if (res < 1) {
20640
20641
20642 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20643 }
20644 return res;
20645 }
20646
20647
20648
20649
20650 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
20651 {
20652
20653
20654 const char *cmd;
20655 const char *cseq;
20656 const char *useragent;
20657 int seqno;
20658 int len;
20659 int respid;
20660 int res = 0;
20661 int debug = sip_debug_test_pvt(p);
20662 char *e;
20663 int error = 0;
20664 int oldmethod = p->method;
20665 int acked = 0;
20666
20667
20668 cseq = get_header(req, "Cseq");
20669 cmd = REQ_OFFSET_TO_STR(req, header[0]);
20670
20671
20672 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq)) {
20673 ast_log(LOG_ERROR, "Missing Cseq. Dropping this SIP message, it's incomplete.\n");
20674 error = 1;
20675 }
20676 if (!error && sscanf(cseq, "%30d%n", &seqno, &len) != 1) {
20677 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
20678 error = 1;
20679 }
20680 if (error) {
20681 if (!p->initreq.headers)
20682 p->needdestroy = 1;
20683 return -1;
20684 }
20685
20686
20687 cmd = REQ_OFFSET_TO_STR(req, rlPart1);
20688 e = ast_skip_blanks(REQ_OFFSET_TO_STR(req, rlPart2));
20689
20690
20691 useragent = get_header(req, "User-Agent");
20692 if (!ast_strlen_zero(useragent))
20693 ast_string_field_set(p, useragent, useragent);
20694
20695
20696 if (req->method == SIP_RESPONSE) {
20697
20698
20699
20700
20701
20702
20703 if (ast_strlen_zero(e)) {
20704 return 0;
20705 }
20706 if (sscanf(e, "%30d %n", &respid, &len) != 1) {
20707 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
20708 return 0;
20709 }
20710 if (respid <= 0) {
20711 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
20712 return 0;
20713 }
20714 if (p->ocseq && (p->ocseq < seqno)) {
20715 if (option_debug)
20716 ast_log(LOG_DEBUG, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
20717 return -1;
20718 } else {
20719 if ((respid == 200) || ((respid >= 300) && (respid <= 399))) {
20720 extract_uri(p, req);
20721 }
20722 handle_response(p, respid, e + len, req, seqno);
20723 }
20724 return 0;
20725 }
20726
20727
20728
20729
20730
20731 p->method = req->method;
20732 ast_debug(4, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
20733
20734 if (p->icseq && (p->icseq > seqno) ) {
20735 if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
20736 ast_debug(2, "Got CANCEL or ACK on INVITE with transactions in between.\n");
20737 } else {
20738 ast_debug(1, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
20739 if (req->method != SIP_ACK)
20740 transmit_response(p, "503 Server error", req);
20741 return -1;
20742 }
20743 } else if (p->icseq &&
20744 p->icseq == seqno &&
20745 req->method != SIP_ACK &&
20746 (p->method != SIP_CANCEL || p->alreadygone)) {
20747
20748
20749
20750 req->ignore = 1;
20751 ast_debug(3, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
20752 }
20753
20754 if (seqno >= p->icseq)
20755
20756
20757
20758 p->icseq = seqno;
20759
20760
20761 if (ast_strlen_zero(p->theirtag)) {
20762 char tag[128];
20763
20764 gettag(req, "From", tag, sizeof(tag));
20765 ast_string_field_set(p, theirtag, tag);
20766 }
20767 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
20768
20769 if (pedanticsipchecking) {
20770
20771
20772
20773
20774 if (!p->initreq.headers && req->has_to_tag) {
20775
20776 if (!req->ignore && req->method == SIP_INVITE) {
20777 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
20778
20779 } else if (req->method != SIP_ACK) {
20780 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
20781 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20782 } else {
20783 ast_debug(1, "Got ACK for unknown dialog... strange.\n");
20784 }
20785 return res;
20786 }
20787 }
20788
20789 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY)) {
20790 transmit_response(p, "400 Bad request", req);
20791 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20792 return -1;
20793 }
20794
20795
20796 switch (p->method) {
20797 case SIP_OPTIONS:
20798 res = handle_request_options(p, req);
20799 break;
20800 case SIP_INVITE:
20801 res = handle_request_invite(p, req, debug, seqno, sin, recount, e, nounlock);
20802 break;
20803 case SIP_REFER:
20804 res = handle_request_refer(p, req, debug, seqno, nounlock);
20805 break;
20806 case SIP_CANCEL:
20807 res = handle_request_cancel(p, req);
20808 break;
20809 case SIP_BYE:
20810 res = handle_request_bye(p, req);
20811 break;
20812 case SIP_MESSAGE:
20813 res = handle_request_message(p, req);
20814 break;
20815 case SIP_SUBSCRIBE:
20816 res = handle_request_subscribe(p, req, sin, seqno, e);
20817 break;
20818 case SIP_REGISTER:
20819 res = handle_request_register(p, req, sin, e);
20820 break;
20821 case SIP_INFO:
20822 if (req->debug)
20823 ast_verbose("Receiving INFO!\n");
20824 if (!req->ignore)
20825 handle_request_info(p, req);
20826 else
20827 transmit_response(p, "200 OK", req);
20828 break;
20829 case SIP_NOTIFY:
20830 res = handle_request_notify(p, req, sin, seqno, e);
20831 break;
20832 case SIP_ACK:
20833
20834 if (seqno == p->pendinginvite) {
20835 p->invitestate = INV_TERMINATED;
20836 p->pendinginvite = 0;
20837 acked = __sip_ack(p, seqno, 1 , 0);
20838 if (find_sdp(req)) {
20839 if (process_sdp(p, req, SDP_T38_NONE))
20840 return -1;
20841 }
20842 check_pendings(p);
20843 } else if (p->glareinvite == seqno) {
20844
20845 p->glareinvite = 0;
20846 acked = __sip_ack(p, seqno, 1, 0);
20847 }
20848 if (!acked) {
20849
20850
20851 p->method = oldmethod;
20852 }
20853 if (!p->lastinvite && ast_strlen_zero(p->randdata))
20854 p->needdestroy = 1;
20855 break;
20856 default:
20857 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
20858 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
20859 cmd, ast_inet_ntoa(p->sa.sin_addr));
20860
20861 if (!p->initreq.headers)
20862 p->needdestroy = 1;
20863 break;
20864 }
20865 return res;
20866 }
20867
20868 static void process_request_queue(struct sip_pvt *p, int *recount, int *nounlock)
20869 {
20870 struct sip_request *req;
20871
20872 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
20873 if (handle_incoming(p, req, &p->recv, recount, nounlock) == -1) {
20874
20875 if (option_debug) {
20876 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
20877 }
20878 }
20879 ast_free(req);
20880 }
20881 }
20882
20883 static int scheduler_process_request_queue(const void *data)
20884 {
20885 struct sip_pvt *p = (struct sip_pvt *) data;
20886 int recount = 0;
20887 int nounlock = 0;
20888 int lockretry;
20889
20890 for (lockretry = 10; lockretry > 0; lockretry--) {
20891 sip_pvt_lock(p);
20892
20893
20894
20895 if (!p->owner || !ast_channel_trylock(p->owner)) {
20896 break;
20897 }
20898
20899 if (lockretry != 1) {
20900 sip_pvt_unlock(p);
20901
20902 usleep(1);
20903 }
20904 }
20905
20906 if (!lockretry) {
20907 int retry = !AST_LIST_EMPTY(&p->request_queue);
20908
20909
20910
20911
20912
20913
20914 sip_pvt_unlock(p);
20915 if (!retry) {
20916 dialog_unref(p, "The ref to a dialog passed to this sched callback is going out of scope; unref it.");
20917 }
20918 return retry;
20919 };
20920
20921 process_request_queue(p, &recount, &nounlock);
20922 p->request_queue_sched_id = -1;
20923
20924 if (p->owner && !nounlock) {
20925 ast_channel_unlock(p->owner);
20926 }
20927 sip_pvt_unlock(p);
20928
20929 if (recount) {
20930 ast_update_use_count();
20931 }
20932
20933 dialog_unref(p, "The ref to a dialog passed to this sched callback is going out of scope; unref it.");
20934
20935 return 0;
20936 }
20937
20938 static int queue_request(struct sip_pvt *p, const struct sip_request *req)
20939 {
20940 struct sip_request *newreq;
20941
20942 if (!(newreq = ast_calloc(1, sizeof(*newreq)))) {
20943 return -1;
20944 }
20945
20946 copy_request(newreq, req);
20947 AST_LIST_INSERT_TAIL(&p->request_queue, newreq, next);
20948 if (p->request_queue_sched_id == -1) {
20949 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) {
20950 dialog_unref(p, "Decrement refcount due to sched_add failure");
20951 }
20952 }
20953
20954 return 0;
20955 }
20956
20957
20958
20959
20960
20961
20962 static int sipsock_read(int *id, int fd, short events, void *ignore)
20963 {
20964 struct sip_request req;
20965 struct sockaddr_in sin = { 0, };
20966 int res;
20967 socklen_t len = sizeof(sin);
20968 static char readbuf[65535];
20969
20970 memset(&req, 0, sizeof(req));
20971 res = recvfrom(fd, readbuf, sizeof(readbuf) - 1, 0, (struct sockaddr *)&sin, &len);
20972 if (res < 0) {
20973 #if !defined(__FreeBSD__)
20974 if (errno == EAGAIN)
20975 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
20976 else
20977 #endif
20978 if (errno != ECONNREFUSED)
20979 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
20980 return 1;
20981 }
20982
20983 readbuf[res] = '\0';
20984
20985 if (!(req.data = ast_str_create(SIP_MIN_PACKET))) {
20986 return 1;
20987 }
20988
20989 if (ast_str_set(&req.data, 0, "%s", readbuf) == AST_DYNSTR_BUILD_FAILED) {
20990 return -1;
20991 }
20992
20993 req.len = res;
20994 req.socket.fd = sipsock;
20995 set_socket_transport(&req.socket, SIP_TRANSPORT_UDP);
20996 req.socket.tcptls_session = NULL;
20997 req.socket.port = bindaddr.sin_port;
20998
20999 handle_request_do(&req, &sin);
21000 if (req.data) {
21001 ast_free(req.data);
21002 req.data = NULL;
21003 }
21004
21005 return 1;
21006 }
21007
21008 static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin)
21009 {
21010 struct sip_pvt *p;
21011 int recount = 0;
21012 int nounlock = 0;
21013 int lockretry;
21014
21015 if (sip_debug_test_addr(sin))
21016 req->debug = 1;
21017 if (pedanticsipchecking)
21018 req->len = lws2sws(req->data->str, req->len);
21019 if (req->debug) {
21020 ast_verbose("\n<--- SIP read from %s://%s:%d --->\n%s\n<------------->\n",
21021 get_transport(req->socket.type), ast_inet_ntoa(sin->sin_addr),
21022 ntohs(sin->sin_port), req->data->str);
21023 }
21024
21025 if (parse_request(req) == -1) {
21026 ast_str_reset(req->data);
21027 return 1;
21028 }
21029 req->method = find_sip_method(REQ_OFFSET_TO_STR(req, rlPart1));
21030
21031 if (req->debug)
21032 ast_verbose("--- (%d headers %d lines)%s ---\n", req->headers, req->lines, (req->headers + req->lines == 0) ? " Nat keepalive" : "");
21033
21034 if (req->headers < 2) {
21035 ast_str_reset(req->data);
21036 return 1;
21037 }
21038
21039
21040 for (lockretry = 10; lockretry > 0; lockretry--) {
21041 ast_mutex_lock(&netlock);
21042
21043
21044 p = find_call(req, sin, req->method);
21045 if (p == NULL) {
21046 ast_debug(1, "Invalid SIP message - rejected , no callid, len %d\n", req->len);
21047 ast_mutex_unlock(&netlock);
21048 return 1;
21049 }
21050
21051 copy_socket_data(&p->socket, &req->socket);
21052
21053
21054
21055 if (!p->owner || !ast_channel_trylock(p->owner))
21056 break;
21057
21058 if (lockretry != 1) {
21059 sip_pvt_unlock(p);
21060 ao2_t_ref(p, -1, "release p (from find_call) inside lockretry loop");
21061 ast_mutex_unlock(&netlock);
21062
21063 usleep(1);
21064 }
21065 }
21066 p->recv = *sin;
21067
21068 if (p->do_history)
21069 append_history(p, "Rx", "%s / %s / %s", req->data->str, get_header(req, "CSeq"), REQ_OFFSET_TO_STR(req, rlPart2));
21070
21071 if (!lockretry) {
21072 if (!queue_request(p, req)) {
21073
21074 sip_pvt_unlock(p);
21075 ao2_t_ref(p, -1, "release p (from find_call) after queueing request");
21076 ast_mutex_unlock(&netlock);
21077 return 1;
21078 }
21079
21080 if (p->owner)
21081 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 ??? - "));
21082 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
21083 if (req->method != SIP_ACK)
21084 transmit_response(p, "503 Server error", req);
21085
21086 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
21087 sip_pvt_unlock(p);
21088 ao2_t_ref(p, -1, "release p (from find_call) at end of lockretry");
21089 ast_mutex_unlock(&netlock);
21090 return 1;
21091 }
21092
21093
21094
21095
21096 if (!AST_LIST_EMPTY(&p->request_queue)) {
21097 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"));
21098 process_request_queue(p, &recount, &nounlock);
21099 }
21100
21101 if (handle_incoming(p, req, sin, &recount, &nounlock) == -1) {
21102
21103 ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
21104 }
21105
21106 if (recount)
21107 ast_update_use_count();
21108
21109 if (p->owner && !nounlock)
21110 ast_channel_unlock(p->owner);
21111 sip_pvt_unlock(p);
21112 ast_mutex_unlock(&netlock);
21113 ao2_t_ref(p, -1, "throw away dialog ptr from find_call at end of routine");
21114 return 1;
21115 }
21116
21117
21118
21119
21120
21121
21122
21123 static int sip_standard_port(enum sip_transport type, int port)
21124 {
21125 if (type & SIP_TRANSPORT_TLS)
21126 return port == STANDARD_TLS_PORT;
21127 else
21128 return port == STANDARD_SIP_PORT;
21129 }
21130
21131 static int threadinfo_locate_cb(void *obj, void *arg, int flags)
21132 {
21133 struct sip_threadinfo *th = obj;
21134 struct sockaddr_in *s = arg;
21135
21136 if (!inaddrcmp(&th->tcptls_session->remote_address, s)) {
21137 return CMP_MATCH | CMP_STOP;
21138 }
21139
21140 return 0;
21141 }
21142
21143
21144
21145
21146
21147
21148 static struct ast_tcptls_session_instance *sip_tcp_locate(struct sockaddr_in *s)
21149 {
21150 struct sip_threadinfo *th;
21151 struct ast_tcptls_session_instance *tcptls_instance = NULL;
21152
21153 if ((th = ao2_callback(threadt, 0, threadinfo_locate_cb, s))) {
21154 tcptls_instance = (ao2_ref(th->tcptls_session, +1), th->tcptls_session);
21155 ao2_t_ref(th, -1, "decrement ref from callback");
21156 }
21157 return tcptls_instance;
21158 }
21159
21160
21161 static int sip_prepare_socket(struct sip_pvt *p)
21162 {
21163 struct sip_socket *s = &p->socket;
21164 static const char name[] = "SIP socket";
21165 struct sip_threadinfo *th = NULL;
21166 struct ast_tcptls_session_instance *tcptls_session;
21167 struct ast_tcptls_session_args tmp_ca = {
21168 .name = name,
21169 .accept_fd = -1,
21170 };
21171 struct ast_tcptls_session_args *ca;
21172
21173
21174 if ((s->fd != -1) && (s->type == SIP_TRANSPORT_UDP)) {
21175 return s->fd;
21176 }
21177 if ((s->type & (SIP_TRANSPORT_TCP | SIP_TRANSPORT_TLS)) &&
21178 (s->tcptls_session) &&
21179 (s->tcptls_session->fd != -1)) {
21180 return s->tcptls_session->fd;
21181 }
21182
21183 if (p->outboundproxy && p->outboundproxy->transport) {
21184 s->type = p->outboundproxy->transport;
21185 }
21186
21187 if (s->type == SIP_TRANSPORT_UDP) {
21188 s->fd = sipsock;
21189 return s->fd;
21190 }
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202 tmp_ca.remote_address = *(sip_real_dst(p));
21203 if ((tcptls_session = sip_tcp_locate(&tmp_ca.remote_address))) {
21204 s->fd = tcptls_session->fd;
21205 if (s->tcptls_session) {
21206 ao2_ref(s->tcptls_session, -1);
21207 s->tcptls_session = NULL;
21208 }
21209 s->tcptls_session = tcptls_session;
21210 return s->fd;
21211
21212 } else if (s->tcptls_session) {
21213 return s->fd;
21214 }
21215
21216
21217
21218 if (!(ca = ao2_alloc(sizeof(*ca), sip_tcptls_client_args_destructor)) ||
21219 !(ca->name = ast_strdup(name))) {
21220 goto create_tcptls_session_fail;
21221 }
21222 ca->accept_fd = -1;
21223 ca->remote_address = *(sip_real_dst(p));
21224
21225 if (s->type == SIP_TRANSPORT_TLS) {
21226 if (!(ca->tls_cfg = ast_calloc(1, sizeof(*ca->tls_cfg)))) {
21227 goto create_tcptls_session_fail;
21228 }
21229 memcpy(ca->tls_cfg, &default_tls_cfg, sizeof(*ca->tls_cfg));
21230
21231 if (!(ca->tls_cfg->certfile = ast_strdup(default_tls_cfg.certfile)) ||
21232 !(ca->tls_cfg->cipher = ast_strdup(default_tls_cfg.cipher)) ||
21233 !(ca->tls_cfg->cafile = ast_strdup(default_tls_cfg.cafile)) ||
21234 !(ca->tls_cfg->capath = ast_strdup(default_tls_cfg.capath))) {
21235
21236 goto create_tcptls_session_fail;
21237 }
21238
21239
21240 if (!ast_strlen_zero(p->tohost)) {
21241 ast_copy_string(ca->hostname, p->tohost, sizeof(ca->hostname));
21242 }
21243 }
21244
21245 if (!(s->tcptls_session = ast_tcptls_client_create(ca))) {
21246 goto create_tcptls_session_fail;
21247 }
21248
21249 s->fd = s->tcptls_session->fd;
21250
21251
21252
21253
21254
21255 if (!(th = sip_threadinfo_create(s->tcptls_session, s->type))) {
21256 goto create_tcptls_session_fail;
21257
21258 }
21259
21260
21261 ao2_ref(s->tcptls_session, +1);
21262
21263 if (ast_pthread_create_background(&ca->master, NULL, sip_tcp_worker_fn, s->tcptls_session)) {
21264 ast_debug(1, "Unable to launch '%s'.", ca->name);
21265 ao2_ref(s->tcptls_session, -1);
21266 goto create_tcptls_session_fail;
21267 }
21268
21269 return s->fd;
21270
21271 create_tcptls_session_fail:
21272 if (ca) {
21273 ao2_t_ref(ca, -1, "failed to create client, getting rid of client tcptls_session arguments");
21274 }
21275 if (s->tcptls_session) {
21276 close(tcptls_session->fd);
21277 s->fd = tcptls_session->fd = -1;
21278 ao2_ref(s->tcptls_session, -1);
21279 s->tcptls_session = NULL;
21280 }
21281 if (th) {
21282 ao2_t_unlink(threadt, th, "Removing tcptls thread info object, thread failed to open");
21283 }
21284
21285 return -1;
21286 }
21287
21288
21289
21290
21291
21292 static int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport)
21293 {
21294 char *port;
21295
21296 if ((*hostname = strstr(line, "://"))) {
21297 *hostname += 3;
21298
21299 if (!strncasecmp(line, "tcp", 3))
21300 *transport = SIP_TRANSPORT_TCP;
21301 else if (!strncasecmp(line, "tls", 3))
21302 *transport = SIP_TRANSPORT_TLS;
21303 else if (!strncasecmp(line, "udp", 3))
21304 *transport = SIP_TRANSPORT_UDP;
21305 else
21306 ast_log(LOG_NOTICE, "'%.3s' is not a valid transport type on line %d of sip.conf. defaulting to udp.\n", line, lineno);
21307 } else {
21308 *hostname = line;
21309 *transport = SIP_TRANSPORT_UDP;
21310 }
21311
21312 if ((line = strrchr(*hostname, '@')))
21313 line++;
21314 else
21315 line = *hostname;
21316
21317 if ((port = strrchr(line, ':'))) {
21318 *port++ = '\0';
21319
21320 if (!sscanf(port, "%5u", portnum)) {
21321 ast_log(LOG_NOTICE, "'%s' is not a valid port number on line %d of sip.conf. using default.\n", port, lineno);
21322 port = NULL;
21323 }
21324 }
21325
21326 if (!port) {
21327 if (*transport & SIP_TRANSPORT_TLS) {
21328 *portnum = STANDARD_TLS_PORT;
21329 } else {
21330 *portnum = STANDARD_SIP_PORT;
21331 }
21332 }
21333
21334 return 0;
21335 }
21336
21337
21338
21339
21340
21341
21342 static int get_cached_mwi(struct sip_peer *peer, int *new, int *old)
21343 {
21344 struct sip_mailbox *mailbox;
21345
21346 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
21347 struct ast_event *event;
21348 event = ast_event_get_cached(AST_EVENT_MWI,
21349 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
21350 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
21351 AST_EVENT_IE_END);
21352 if (!event)
21353 continue;
21354 *new += ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
21355 *old += ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
21356 ast_event_destroy(event);
21357 }
21358
21359 return (*new || *old) ? 0 : 1;
21360 }
21361
21362
21363 static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only)
21364 {
21365
21366 struct sip_pvt *p;
21367 int newmsgs = 0, oldmsgs = 0;
21368
21369 if (ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY) && !peer->mwipvt)
21370 return 0;
21371
21372
21373 if (!peer->addr.sin_addr.s_addr && !peer->defaddr.sin_addr.s_addr)
21374 return 0;
21375
21376 if (event) {
21377 newmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
21378 oldmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
21379 } else if (!get_cached_mwi(peer, &newmsgs, &oldmsgs)) {
21380
21381 } else if (cache_only) {
21382 return 0;
21383 } else {
21384 struct ast_str *mailbox_str = ast_str_alloca(512);
21385 peer_mailboxes_to_str(&mailbox_str, peer);
21386 ast_app_inboxcount(mailbox_str->str, &newmsgs, &oldmsgs);
21387 }
21388
21389 if (peer->mwipvt) {
21390
21391 p = dialog_ref(peer->mwipvt, "sip_send_mwi_to_peer: Setting dialog ptr p from peer->mwipvt-- should this be done?");
21392 } else {
21393
21394 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL)))
21395 return -1;
21396
21397
21398
21399
21400 set_socket_transport(&p->socket, 0);
21401 if (create_addr_from_peer(p, peer)) {
21402
21403 dialog_unlink_all(p, TRUE, TRUE);
21404 dialog_unref(p, "unref dialog p just created via sip_alloc");
21405
21406 return 0;
21407 }
21408
21409 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
21410 build_via(p);
21411 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
21412 build_callid_pvt(p);
21413 ao2_t_link(dialogs, p, "Linking in under new name");
21414
21415 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21416 }
21417
21418
21419 ast_set_flag(&p->flags[0], SIP_OUTGOING);
21420
21421 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
21422 dialog_unref(p, "unref dialog ptr p just before it goes out of scope at the end of sip_send_mwi_to_peer.");
21423 return 0;
21424 }
21425
21426
21427 static void check_rtp_timeout(struct sip_pvt *dialog, time_t t)
21428 {
21429
21430 if (!dialog->rtp || !dialog->owner)
21431 return;
21432
21433
21434 if (dialog->owner->_state != AST_STATE_UP || dialog->redirip.sin_addr.s_addr)
21435 return;
21436
21437
21438 if (dialog->t38.state == T38_ENABLED)
21439 return;
21440
21441
21442 if ((ast_rtp_get_rtpkeepalive(dialog->rtp) == 0) && (ast_rtp_get_rtptimeout(dialog->rtp) == 0) && (ast_rtp_get_rtpholdtimeout(dialog->rtp) == 0))
21443 return;
21444
21445
21446 if (dialog->lastrtptx && ast_rtp_get_rtpkeepalive(dialog->rtp) &&
21447 (t > dialog->lastrtptx + ast_rtp_get_rtpkeepalive(dialog->rtp))) {
21448
21449 dialog->lastrtptx = time(NULL);
21450 ast_rtp_sendcng(dialog->rtp, 0);
21451 }
21452
21453
21454
21455
21456
21457
21458
21459
21460 if (dialog->lastrtprx && (ast_rtp_get_rtptimeout(dialog->rtp) || ast_rtp_get_rtpholdtimeout(dialog->rtp)) &&
21461 (t > dialog->lastrtprx + ast_rtp_get_rtptimeout(dialog->rtp))) {
21462
21463
21464 struct sockaddr_in sin;
21465 ast_rtp_get_peer(dialog->rtp, &sin);
21466 if (!ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD) || (ast_rtp_get_rtpholdtimeout(dialog->rtp) &&
21467 (t > dialog->lastrtprx + ast_rtp_get_rtpholdtimeout(dialog->rtp)))) {
21468
21469 if (ast_rtp_get_rtptimeout(dialog->rtp)) {
21470 while (dialog->owner && ast_channel_trylock(dialog->owner)) {
21471 sip_pvt_unlock(dialog);
21472 usleep(1);
21473 sip_pvt_lock(dialog);
21474 }
21475 ast_log(LOG_NOTICE, "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
21476 dialog->owner->name, (long) (t - dialog->lastrtprx));
21477
21478 ast_softhangup_nolock(dialog->owner, AST_SOFTHANGUP_DEV);
21479 ast_channel_unlock(dialog->owner);
21480
21481
21482
21483
21484 ast_rtp_set_rtptimeout(dialog->rtp, 0);
21485 ast_rtp_set_rtpholdtimeout(dialog->rtp, 0);
21486 if (dialog->vrtp) {
21487 ast_rtp_set_rtptimeout(dialog->vrtp, 0);
21488 ast_rtp_set_rtpholdtimeout(dialog->vrtp, 0);
21489 }
21490 }
21491 }
21492 }
21493 }
21494
21495
21496
21497
21498
21499 static void *do_monitor(void *data)
21500 {
21501 int res;
21502 time_t t;
21503 int reloading;
21504
21505
21506 if (sipsock > -1)
21507 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
21508
21509
21510 for(;;) {
21511
21512 ast_mutex_lock(&sip_reload_lock);
21513 reloading = sip_reloading;
21514 sip_reloading = FALSE;
21515 ast_mutex_unlock(&sip_reload_lock);
21516 if (reloading) {
21517 ast_verb(1, "Reloading SIP\n");
21518 sip_do_reload(sip_reloadreason);
21519
21520
21521 if (sipsock > -1) {
21522 if (sipsock_read_id)
21523 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
21524 else
21525 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
21526 } else if (sipsock_read_id) {
21527 ast_io_remove(io, sipsock_read_id);
21528 sipsock_read_id = NULL;
21529 }
21530 }
21531
21532
21533 t = time(NULL);
21534
21535
21536
21537
21538 ao2_t_callback(dialogs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, dialog_needdestroy, &t,
21539 "callback to remove dialogs w/needdestroy");
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551 pthread_testcancel();
21552
21553 res = ast_sched_wait(sched);
21554 if ((res < 0) || (res > 1000))
21555 res = 1000;
21556 res = ast_io_wait(io, res);
21557 if (res > 20)
21558 ast_debug(1, "chan_sip: ast_io_wait ran %d all at once\n", res);
21559 ast_mutex_lock(&monlock);
21560 res = ast_sched_runq(sched);
21561 if (res >= 20)
21562 ast_debug(1, "chan_sip: ast_sched_runq ran %d all at once\n", res);
21563 ast_mutex_unlock(&monlock);
21564 }
21565
21566
21567 return NULL;
21568 }
21569
21570
21571 static int restart_monitor(void)
21572 {
21573
21574 if (monitor_thread == AST_PTHREADT_STOP)
21575 return 0;
21576 ast_mutex_lock(&monlock);
21577 if (monitor_thread == pthread_self()) {
21578 ast_mutex_unlock(&monlock);
21579 ast_log(LOG_WARNING, "Cannot kill myself\n");
21580 return -1;
21581 }
21582 if (monitor_thread != AST_PTHREADT_NULL) {
21583
21584 pthread_kill(monitor_thread, SIGURG);
21585 } else {
21586
21587 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
21588 ast_mutex_unlock(&monlock);
21589 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
21590 return -1;
21591 }
21592 }
21593 ast_mutex_unlock(&monlock);
21594 return 0;
21595 }
21596
21597
21598
21599 static void restart_session_timer(struct sip_pvt *p)
21600 {
21601 if (!p->stimer) {
21602 ast_log(LOG_WARNING, "Null stimer in restart_session_timer - %s\n", p->callid);
21603 return;
21604 }
21605
21606 if (p->stimer->st_active == TRUE) {
21607 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
21608 dialog_unref(p, "Removing session timer ref"));
21609 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
21610 start_session_timer(p);
21611 }
21612 }
21613
21614
21615
21616 static void stop_session_timer(struct sip_pvt *p)
21617 {
21618 if (!p->stimer) {
21619 ast_log(LOG_WARNING, "Null stimer in stop_session_timer - %s\n", p->callid);
21620 return;
21621 }
21622
21623 if (p->stimer->st_active == TRUE) {
21624 p->stimer->st_active = FALSE;
21625 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
21626 dialog_unref(p, "removing session timer ref"));
21627 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
21628 }
21629 }
21630
21631
21632
21633 static void start_session_timer(struct sip_pvt *p)
21634 {
21635 if (!p->stimer) {
21636 ast_log(LOG_WARNING, "Null stimer in start_session_timer - %s\n", p->callid);
21637 return;
21638 }
21639
21640 p->stimer->st_schedid = ast_sched_add(sched, p->stimer->st_interval * 1000 / 2, proc_session_timer,
21641 dialog_ref(p, "adding session timer ref"));
21642 if (p->stimer->st_schedid < 0) {
21643 dialog_unref(p, "removing session timer ref");
21644 ast_log(LOG_ERROR, "ast_sched_add failed.\n");
21645 }
21646 ast_debug(2, "Session timer started: %d - %s\n", p->stimer->st_schedid, p->callid);
21647 }
21648
21649
21650
21651 static int proc_session_timer(const void *vp)
21652 {
21653 struct sip_pvt *p = (struct sip_pvt *) vp;
21654 int sendreinv = FALSE;
21655 int res = 0;
21656
21657 if (!p->stimer) {
21658 ast_log(LOG_WARNING, "Null stimer in proc_session_timer - %s\n", p->callid);
21659 goto return_unref;
21660 }
21661
21662 ast_debug(2, "Session timer expired: %d - %s\n", p->stimer->st_schedid, p->callid);
21663
21664 if (!p->owner) {
21665 goto return_unref;
21666 }
21667
21668 if ((p->stimer->st_active != TRUE) || (p->owner->_state != AST_STATE_UP)) {
21669 goto return_unref;
21670 }
21671
21672 switch (p->stimer->st_ref) {
21673 case SESSION_TIMER_REFRESHER_UAC:
21674 if (p->outgoing_call == TRUE) {
21675 sendreinv = TRUE;
21676 }
21677 break;
21678 case SESSION_TIMER_REFRESHER_UAS:
21679 if (p->outgoing_call != TRUE) {
21680 sendreinv = TRUE;
21681 }
21682 break;
21683 default:
21684 ast_log(LOG_ERROR, "Unknown session refresher %d\n", p->stimer->st_ref);
21685 goto return_unref;
21686 }
21687
21688 if (sendreinv == TRUE) {
21689 res = 1;
21690 transmit_reinvite_with_sdp(p, FALSE, TRUE);
21691 } else {
21692 p->stimer->st_expirys++;
21693 if (p->stimer->st_expirys >= 2) {
21694 if (p->stimer->quit_flag) {
21695 goto return_unref;
21696 }
21697 ast_log(LOG_WARNING, "Session-Timer expired - %s\n", p->callid);
21698 sip_pvt_lock(p);
21699 while (p->owner && ast_channel_trylock(p->owner)) {
21700 sip_pvt_unlock(p);
21701 usleep(1);
21702 if (p->stimer && p->stimer->quit_flag) {
21703 goto return_unref;
21704 }
21705 sip_pvt_lock(p);
21706 }
21707
21708 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
21709 ast_channel_unlock(p->owner);
21710 sip_pvt_unlock(p);
21711 }
21712 }
21713
21714 return_unref:
21715 if (!res) {
21716
21717 if (p->stimer) {
21718 p->stimer->st_schedid = -1;
21719 stop_session_timer(p);
21720 }
21721
21722
21723
21724 dialog_unref(p, "removing session timer ref");
21725 }
21726
21727 return res;
21728 }
21729
21730
21731
21732 int parse_minse (const char *p_hdrval, int *const p_interval)
21733 {
21734 if (ast_strlen_zero(p_hdrval)) {
21735 ast_log(LOG_WARNING, "Null Min-SE header\n");
21736 return -1;
21737 }
21738
21739 *p_interval = 0;
21740 p_hdrval = ast_skip_blanks(p_hdrval);
21741 if (!sscanf(p_hdrval, "%30d", p_interval)) {
21742 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
21743 return -1;
21744 }
21745
21746 ast_debug(2, "Received Min-SE: %d\n", *p_interval);
21747 return 0;
21748 }
21749
21750
21751
21752 int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher *const p_ref)
21753 {
21754 char *p_token;
21755 int ref_idx;
21756 char *p_se_hdr;
21757
21758 if (ast_strlen_zero(p_hdrval)) {
21759 ast_log(LOG_WARNING, "Null Session-Expires header\n");
21760 return -1;
21761 }
21762
21763 *p_ref = SESSION_TIMER_REFRESHER_AUTO;
21764 *p_interval = 0;
21765
21766 p_se_hdr = ast_strdupa(p_hdrval);
21767 p_se_hdr = ast_skip_blanks(p_se_hdr);
21768
21769 while ((p_token = strsep(&p_se_hdr, ";"))) {
21770 p_token = ast_skip_blanks(p_token);
21771 if (!sscanf(p_token, "%30d", p_interval)) {
21772 ast_log(LOG_WARNING, "Parsing of Session-Expires failed\n");
21773 return -1;
21774 }
21775
21776 ast_debug(2, "Session-Expires: %d\n", *p_interval);
21777
21778 if (!p_se_hdr)
21779 continue;
21780
21781 p_se_hdr = ast_skip_blanks(p_se_hdr);
21782 ref_idx = strlen("refresher=");
21783 if (!strncasecmp(p_se_hdr, "refresher=", ref_idx)) {
21784 p_se_hdr += ref_idx;
21785 p_se_hdr = ast_skip_blanks(p_se_hdr);
21786
21787 if (!strncasecmp(p_se_hdr, "uac", strlen("uac"))) {
21788 *p_ref = SESSION_TIMER_REFRESHER_UAC;
21789 ast_debug(2, "Refresher: UAC\n");
21790 } else if (!strncasecmp(p_se_hdr, "uas", strlen("uas"))) {
21791 *p_ref = SESSION_TIMER_REFRESHER_UAS;
21792 ast_debug(2, "Refresher: UAS\n");
21793 } else {
21794 ast_log(LOG_WARNING, "Invalid refresher value %s\n", p_se_hdr);
21795 return -1;
21796 }
21797 break;
21798 }
21799 }
21800 return 0;
21801 }
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp)
21812 {
21813 int rtn;
21814 const char *p_hdrval;
21815 int minse;
21816
21817 p_hdrval = get_header(rsp, "Min-SE");
21818 if (ast_strlen_zero(p_hdrval)) {
21819 ast_log(LOG_WARNING, "422 response without a Min-SE header %s\n", p_hdrval);
21820 return;
21821 }
21822 rtn = parse_minse(p_hdrval, &minse);
21823 if (rtn != 0) {
21824 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
21825 return;
21826 }
21827 p->stimer->st_interval = minse;
21828 transmit_invite(p, SIP_INVITE, 1, 2);
21829 }
21830
21831
21832
21833
21834
21835
21836 int st_get_se(struct sip_pvt *p, int max)
21837 {
21838 if (max == TRUE) {
21839 if (p->stimer->st_cached_max_se) {
21840 return p->stimer->st_cached_max_se;
21841 } else if (p->peername) {
21842 struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDPEERS, FALSE, 0);
21843 if (pp) {
21844 p->stimer->st_cached_max_se = pp->stimer.st_max_se;
21845 unref_peer(pp, "unref peer pointer from find_peer call in st_get_se");
21846 return (p->stimer->st_cached_max_se);
21847 }
21848 }
21849 p->stimer->st_cached_max_se = global_max_se;
21850 return (p->stimer->st_cached_max_se);
21851 } else {
21852 if (p->stimer->st_cached_min_se) {
21853 return p->stimer->st_cached_min_se;
21854 } else if (p->peername) {
21855 struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDPEERS, FALSE, 0);
21856 if (pp) {
21857 p->stimer->st_cached_min_se = pp->stimer.st_min_se;
21858 unref_peer(pp, "unref peer pointer from find_peer call in st_get_se (2)");
21859 return (p->stimer->st_cached_min_se);
21860 }
21861 }
21862 p->stimer->st_cached_min_se = global_min_se;
21863 return (p->stimer->st_cached_min_se);
21864 }
21865 }
21866
21867
21868
21869
21870
21871 enum st_refresher st_get_refresher(struct sip_pvt *p)
21872 {
21873 if (p->stimer->st_cached_ref != SESSION_TIMER_REFRESHER_AUTO)
21874 return p->stimer->st_cached_ref;
21875
21876 if (p->peername) {
21877 struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDPEERS, FALSE, 0);
21878 if (pp) {
21879 p->stimer->st_cached_ref = pp->stimer.st_ref;
21880 unref_peer(pp, "unref peer pointer from find_peer call in st_get_refresher");
21881 return pp->stimer.st_ref;
21882 }
21883 }
21884
21885 p->stimer->st_cached_ref = global_st_refresher;
21886 return global_st_refresher;
21887 }
21888
21889
21890
21891
21892
21893 enum st_mode st_get_mode(struct sip_pvt *p)
21894 {
21895 if (!p->stimer)
21896 sip_st_alloc(p);
21897
21898 if (p->stimer->st_cached_mode != SESSION_TIMER_MODE_INVALID)
21899 return p->stimer->st_cached_mode;
21900
21901 if (p->peername) {
21902 struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, FINDPEERS, FALSE, 0);
21903 if (pp) {
21904 p->stimer->st_cached_mode = pp->stimer.st_mode_oper;
21905 unref_peer(pp, "unref peer pointer from find_peer call in st_get_mode");
21906 return pp->stimer.st_mode_oper;
21907 }
21908 }
21909
21910 p->stimer->st_cached_mode = global_st_mode;
21911 return global_st_mode;
21912 }
21913
21914
21915
21916 static int sip_poke_noanswer(const void *data)
21917 {
21918 struct sip_peer *peer = (struct sip_peer *)data;
21919
21920 peer->pokeexpire = -1;
21921
21922 if (peer->lastms > -1) {
21923 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
21924 if (sip_cfg.peer_rtupdate) {
21925 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", "-1", SENTINEL);
21926 }
21927 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
21928 if (global_regextenonqualify) {
21929 register_peer_exten(peer, FALSE);
21930 }
21931 }
21932
21933 if (peer->call) {
21934 dialog_unlink_all(peer->call, TRUE, TRUE);
21935 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
21936
21937 }
21938
21939 peer->lastms = -1;
21940 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
21941
21942
21943 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched,
21944 DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer,
21945 unref_peer(_data, "removing poke peer ref"),
21946 unref_peer(peer, "removing poke peer ref"),
21947 ref_peer(peer, "adding poke peer ref"));
21948
21949
21950 unref_peer(peer, "release peer poke noanswer ref");
21951
21952 return 0;
21953 }
21954
21955
21956
21957
21958
21959
21960 static int sip_poke_peer(struct sip_peer *peer, int force)
21961 {
21962 struct sip_pvt *p;
21963 int xmitres = 0;
21964
21965 if ((!peer->maxms && !force) || !peer->addr.sin_addr.s_addr) {
21966
21967
21968 AST_SCHED_DEL_UNREF(sched, peer->pokeexpire,
21969 unref_peer(peer, "removing poke peer ref"));
21970
21971 peer->lastms = 0;
21972 if (peer->call) {
21973 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
21974 }
21975 return 0;
21976 }
21977 if (peer->call) {
21978 if (sipdebug) {
21979 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
21980 }
21981 dialog_unlink_all(peer->call, TRUE, TRUE);
21982 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
21983
21984 }
21985 if (!(p = sip_alloc(NULL, NULL, 0, SIP_OPTIONS, NULL))) {
21986 return -1;
21987 }
21988 peer->call = dialog_ref(p, "copy sip alloc from p to peer->call");
21989
21990 p->sa = peer->addr;
21991 p->recv = peer->addr;
21992 copy_socket_data(&p->socket, &peer->socket);
21993 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
21994 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
21995
21996
21997 if (!ast_strlen_zero(peer->fullcontact))
21998 ast_string_field_set(p, fullcontact, peer->fullcontact);
21999
22000 if (!ast_strlen_zero(peer->tohost))
22001 ast_string_field_set(p, tohost, peer->tohost);
22002 else
22003 ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr));
22004
22005
22006 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
22007 build_via(p);
22008 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
22009 build_callid_pvt(p);
22010 ao2_t_link(dialogs, p, "Linking in under new name");
22011
22012 AST_SCHED_DEL_UNREF(sched, peer->pokeexpire,
22013 unref_peer(peer, "removing poke peer ref"));
22014
22015 if (p->relatedpeer)
22016 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting the relatedpeer field in the dialog, before it is set to something else.");
22017 p->relatedpeer = ref_peer(peer, "setting the relatedpeer field in the dialog");
22018 ast_set_flag(&p->flags[0], SIP_OUTGOING);
22019 #ifdef VOCAL_DATA_HACK
22020 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
22021 xmitres = transmit_invite(p, SIP_INVITE, 0, 2);
22022 #else
22023 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2);
22024 #endif
22025 peer->ps = ast_tvnow();
22026 if (xmitres == XMIT_ERROR) {
22027 sip_poke_noanswer(peer);
22028 } else if (!force) {
22029 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, peer->maxms * 2, sip_poke_noanswer, peer,
22030 unref_peer(_data, "removing poke peer ref"),
22031 unref_peer(peer, "removing poke peer ref"),
22032 ref_peer(peer, "adding poke peer ref"));
22033 }
22034 dialog_unref(p, "unref dialog at end of sip_poke_peer, obtained from sip_alloc, just before it goes out of scope");
22035 return 0;
22036 }
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071 static int sip_devicestate(void *data)
22072 {
22073 char *host;
22074 char *tmp;
22075 struct sip_peer *p;
22076
22077 int res = AST_DEVICE_INVALID;
22078
22079
22080 host = ast_strdupa(data ? data : "");
22081 if ((tmp = strchr(host, '@')))
22082 host = tmp + 1;
22083
22084 ast_debug(3, "Checking device state for peer %s\n", host);
22085
22086
22087
22088
22089
22090
22091
22092
22093 if ((p = find_peer(host, NULL, FALSE, FINDALLDEVICES, TRUE, 0))) {
22094 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106 if (p->onHold)
22107
22108 res = AST_DEVICE_ONHOLD;
22109 else if (p->inRinging) {
22110 if (p->inRinging == p->inUse)
22111 res = AST_DEVICE_RINGING;
22112 else
22113 res = AST_DEVICE_RINGINUSE;
22114 } else if (p->call_limit && (p->inUse == p->call_limit))
22115
22116 res = AST_DEVICE_BUSY;
22117 else if (p->call_limit && p->busy_level && p->inUse >= p->busy_level)
22118
22119 res = AST_DEVICE_BUSY;
22120 else if (p->call_limit && p->inUse)
22121
22122 res = AST_DEVICE_INUSE;
22123 else if (p->maxms && ((p->lastms > p->maxms) || (p->lastms < 0)))
22124
22125 res = AST_DEVICE_UNAVAILABLE;
22126 else
22127 res = AST_DEVICE_NOT_INUSE;
22128 } else {
22129
22130 res = AST_DEVICE_UNAVAILABLE;
22131 }
22132 unref_peer(p, "unref_peer, from sip_devicestate, release ref from find_peer");
22133 } else {
22134 res = AST_DEVICE_UNKNOWN;
22135 }
22136
22137 return res;
22138 }
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
22151 {
22152 struct sip_pvt *p;
22153 struct ast_channel *tmpc = NULL;
22154 char *ext = NULL, *host;
22155 char tmp[256];
22156 char *dest = data;
22157 char *dnid;
22158 char *secret = NULL;
22159 char *md5secret = NULL;
22160 char *authname = NULL;
22161 char *trans = NULL;
22162 enum sip_transport transport = 0;
22163 int oldformat = format;
22164
22165
22166
22167
22168
22169
22170
22171
22172 format &= AST_FORMAT_AUDIO_MASK;
22173 if (!format) {
22174 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format %s while capability is %s\n", ast_getformatname(oldformat), ast_getformatname(global_capability));
22175 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
22176 return NULL;
22177 }
22178 ast_debug(1, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
22179
22180 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE, NULL))) {
22181 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", dest);
22182 *cause = AST_CAUSE_SWITCH_CONGESTION;
22183 return NULL;
22184 }
22185
22186 p->outgoing_call = TRUE;
22187
22188 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
22189 dialog_unlink_all(p, TRUE, TRUE);
22190 dialog_unref(p, "unref dialog p from mem fail");
22191
22192 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
22193 *cause = AST_CAUSE_SWITCH_CONGESTION;
22194 return NULL;
22195 }
22196
22197
22198 ast_copy_string(tmp, dest, sizeof(tmp));
22199
22200
22201
22202 dnid = strchr(tmp, '!');
22203 if (dnid != NULL) {
22204 *dnid++ = '\0';
22205 ast_string_field_set(p, todnid, dnid);
22206 }
22207
22208
22209 host = strchr(tmp, '@');
22210 if (host) {
22211 *host++ = '\0';
22212 ext = tmp;
22213 secret = strchr(ext, ':');
22214 }
22215 if (secret) {
22216 *secret++ = '\0';
22217 md5secret = strchr(secret, ':');
22218 }
22219 if (md5secret) {
22220 *md5secret++ = '\0';
22221 authname = strchr(md5secret, ':');
22222 }
22223 if (authname) {
22224 *authname++ = '\0';
22225 trans = strchr(authname, ':');
22226 }
22227 if (trans) {
22228 *trans++ = '\0';
22229 if (!strcasecmp(trans, "tcp"))
22230 transport = SIP_TRANSPORT_TCP;
22231 else if (!strcasecmp(trans, "tls"))
22232 transport = SIP_TRANSPORT_TLS;
22233 else {
22234 if (strcasecmp(trans, "udp"))
22235 ast_log(LOG_WARNING, "'%s' is not a valid transport option to Dial() for SIP calls, using udp by default.\n", trans);
22236 transport = SIP_TRANSPORT_UDP;
22237 }
22238 } else {
22239 transport = SIP_TRANSPORT_UDP;
22240 }
22241
22242 if (!host) {
22243 ext = strchr(tmp, '/');
22244 if (ext)
22245 *ext++ = '\0';
22246 host = tmp;
22247 }
22248
22249 set_socket_transport(&p->socket, transport);
22250
22251
22252
22253
22254
22255
22256 if (create_addr(p, host, NULL, 1)) {
22257 *cause = AST_CAUSE_UNREGISTERED;
22258 ast_debug(3, "Cant create SIP call - target device not registered\n");
22259 dialog_unlink_all(p, TRUE, TRUE);
22260 dialog_unref(p, "unref dialog p UNREGISTERED");
22261
22262 return NULL;
22263 }
22264 if (ast_strlen_zero(p->peername) && ext)
22265 ast_string_field_set(p, peername, ext);
22266
22267 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
22268 build_via(p);
22269 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
22270 build_callid_pvt(p);
22271 ao2_t_link(dialogs, p, "Linking in under new name");
22272
22273
22274
22275
22276
22277
22278 if (ext) {
22279 ast_string_field_set(p, username, ext);
22280 ast_string_field_set(p, fullcontact, NULL);
22281 }
22282 if (secret && !ast_strlen_zero(secret))
22283 ast_string_field_set(p, peersecret, secret);
22284
22285 if (md5secret && !ast_strlen_zero(md5secret))
22286 ast_string_field_set(p, peermd5secret, md5secret);
22287
22288 if (authname && !ast_strlen_zero(authname))
22289 ast_string_field_set(p, authname, authname);
22290 #if 0
22291 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
22292 #endif
22293 p->prefcodec = oldformat;
22294 p->jointcapability = oldformat;
22295 sip_pvt_lock(p);
22296 tmpc = sip_new(p, AST_STATE_DOWN, host);
22297 if (global_callevents)
22298 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
22299 "Channel: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
22300 p->owner? p->owner->name : "", "SIP", p->callid, p->fullcontact, p->peername);
22301 sip_pvt_unlock(p);
22302 if (!tmpc) {
22303 dialog_unlink_all(p, TRUE, TRUE);
22304
22305 }
22306 dialog_unref(p, "toss pvt ptr at end of sip_request_call");
22307 ast_update_use_count();
22308 restart_monitor();
22309 return tmpc;
22310 }
22311
22312
22313 static void set_insecure_flags (struct ast_flags *flags, const char *value, int lineno)
22314 {
22315 if (ast_strlen_zero(value))
22316 return;
22317
22318 if (!ast_false(value)) {
22319 char buf[64];
22320 char *word, *next;
22321
22322 ast_copy_string(buf, value, sizeof(buf));
22323 next = buf;
22324 while ((word = strsep(&next, ","))) {
22325 if (!strcasecmp(word, "port"))
22326 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
22327 else if (!strcasecmp(word, "invite"))
22328 ast_set_flag(&flags[0], SIP_INSECURE_INVITE);
22329 else
22330 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
22331 }
22332 }
22333 }
22334
22335
22336
22337
22338
22339 static int handle_t38_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v,
22340 int *maxdatagram)
22341 {
22342 int res = 1;
22343
22344 if (!strcasecmp(v->name, "t38pt_udptl")) {
22345 char *buf = ast_strdupa(v->value);
22346 char *word, *next = buf;
22347
22348 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT);
22349
22350 while ((word = strsep(&next, ","))) {
22351 if (ast_true(word) || !strcasecmp(word, "fec")) {
22352 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
22353 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL_FEC);
22354 } else if (!strcasecmp(word, "redundancy")) {
22355 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
22356 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY);
22357 } else if (!strcasecmp(word, "none")) {
22358 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
22359 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL);
22360 } else if (!strncasecmp(word, "maxdatagram=", 12)) {
22361 if (sscanf(&word[12], "%30u", maxdatagram) != 1) {
22362 ast_log(LOG_WARNING, "Invalid maxdatagram '%s' at line %d of %s\n", v->value, v->lineno, config);
22363 *maxdatagram = global_t38_maxdatagram;
22364 }
22365 }
22366 }
22367 } else if (!strcasecmp(v->name, "t38pt_usertpsource")) {
22368 ast_set_flag(&mask[1], SIP_PAGE2_UDPTL_DESTINATION);
22369 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_UDPTL_DESTINATION);
22370 } else {
22371 res = 0;
22372 }
22373
22374 return res;
22375 }
22376
22377
22378
22379
22380
22381
22382
22383
22384 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
22385 {
22386 int res = 1;
22387
22388 if (!strcasecmp(v->name, "trustrpid")) {
22389 ast_set_flag(&mask[0], SIP_TRUSTRPID);
22390 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
22391 } else if (!strcasecmp(v->name, "sendrpid")) {
22392 ast_set_flag(&mask[0], SIP_SENDRPID);
22393 ast_set2_flag(&flags[0], ast_true(v->value), SIP_SENDRPID);
22394 } else if (!strcasecmp(v->name, "g726nonstandard")) {
22395 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
22396 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
22397 } else if (!strcasecmp(v->name, "useclientcode")) {
22398 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
22399 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
22400 } else if (!strcasecmp(v->name, "dtmfmode")) {
22401 ast_set_flag(&mask[0], SIP_DTMF);
22402 ast_clear_flag(&flags[0], SIP_DTMF);
22403 if (!strcasecmp(v->value, "inband"))
22404 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
22405 else if (!strcasecmp(v->value, "rfc2833"))
22406 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
22407 else if (!strcasecmp(v->value, "info"))
22408 ast_set_flag(&flags[0], SIP_DTMF_INFO);
22409 else if (!strcasecmp(v->value, "shortinfo"))
22410 ast_set_flag(&flags[0], SIP_DTMF_SHORTINFO);
22411 else if (!strcasecmp(v->value, "auto"))
22412 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
22413 else {
22414 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
22415 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
22416 }
22417 } else if (!strcasecmp(v->name, "nat")) {
22418 ast_set_flag(&mask[0], SIP_NAT);
22419 ast_clear_flag(&flags[0], SIP_NAT);
22420 if (!strcasecmp(v->value, "never"))
22421 ast_set_flag(&flags[0], SIP_NAT_NEVER);
22422 else if (!strcasecmp(v->value, "route"))
22423 ast_set_flag(&flags[0], SIP_NAT_ROUTE);
22424 else if (ast_true(v->value))
22425 ast_set_flag(&flags[0], SIP_NAT_ALWAYS);
22426 else
22427 ast_set_flag(&flags[0], SIP_NAT_RFC3581);
22428 } else if (!strcasecmp(v->name, "canreinvite")) {
22429 ast_set_flag(&mask[0], SIP_REINVITE);
22430 ast_clear_flag(&flags[0], SIP_REINVITE);
22431 if (ast_true(v->value)) {
22432 ast_set_flag(&flags[0], SIP_CAN_REINVITE | SIP_CAN_REINVITE_NAT);
22433 } else if (!ast_false(v->value)) {
22434 char buf[64];
22435 char *word, *next = buf;
22436
22437 ast_copy_string(buf, v->value, sizeof(buf));
22438 while ((word = strsep(&next, ","))) {
22439 if (!strcasecmp(word, "update")) {
22440 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_CAN_REINVITE);
22441 } else if (!strcasecmp(word, "nonat")) {
22442 ast_set_flag(&flags[0], SIP_CAN_REINVITE);
22443 ast_clear_flag(&flags[0], SIP_CAN_REINVITE_NAT);
22444 } else {
22445 ast_log(LOG_WARNING, "Unknown canreinvite mode '%s' on line %d\n", v->value, v->lineno);
22446 }
22447 }
22448 }
22449 } else if (!strcasecmp(v->name, "insecure")) {
22450 ast_set_flag(&mask[0], SIP_INSECURE);
22451 ast_clear_flag(&flags[0], SIP_INSECURE);
22452 set_insecure_flags(&flags[0], v->value, v->lineno);
22453 } else if (!strcasecmp(v->name, "progressinband")) {
22454 ast_set_flag(&mask[0], SIP_PROG_INBAND);
22455 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
22456 if (ast_true(v->value))
22457 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
22458 else if (strcasecmp(v->value, "never"))
22459 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
22460 } else if (!strcasecmp(v->name, "promiscredir")) {
22461 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
22462 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
22463 } else if (!strcasecmp(v->name, "videosupport")) {
22464 if (!strcasecmp(v->value, "always")) {
22465 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
22466 ast_set_flag(&flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
22467 } else {
22468 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
22469 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
22470 }
22471 } else if (!strcasecmp(v->name, "textsupport")) {
22472 ast_set_flag(&mask[1], SIP_PAGE2_TEXTSUPPORT);
22473 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_TEXTSUPPORT);
22474 res = 1;
22475 } else if (!strcasecmp(v->name, "allowoverlap")) {
22476 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
22477 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
22478 } else if (!strcasecmp(v->name, "allowsubscribe")) {
22479 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
22480 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
22481 } else if (!strcasecmp(v->name, "ignoresdpversion")) {
22482 ast_set_flag(&mask[1], SIP_PAGE2_IGNORESDPVERSION);
22483 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_IGNORESDPVERSION);
22484 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
22485 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
22486 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
22487 } else if (!strcasecmp(v->name, "buggymwi")) {
22488 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
22489 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
22490 } else if (!strcasecmp(v->name, "faxdetect")) {
22491 ast_set_flag(&mask[1], SIP_PAGE2_FAX_DETECT);
22492 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_FAX_DETECT);
22493 } else
22494 res = 0;
22495
22496 return res;
22497 }
22498
22499
22500 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
22501 {
22502 struct domain *d;
22503
22504 if (ast_strlen_zero(domain)) {
22505 ast_log(LOG_WARNING, "Zero length domain.\n");
22506 return 1;
22507 }
22508
22509 if (!(d = ast_calloc(1, sizeof(*d))))
22510 return 0;
22511
22512 ast_copy_string(d->domain, domain, sizeof(d->domain));
22513
22514 if (!ast_strlen_zero(context))
22515 ast_copy_string(d->context, context, sizeof(d->context));
22516
22517 d->mode = mode;
22518
22519 AST_LIST_LOCK(&domain_list);
22520 AST_LIST_INSERT_TAIL(&domain_list, d, list);
22521 AST_LIST_UNLOCK(&domain_list);
22522
22523 if (sipdebug)
22524 ast_debug(1, "Added local SIP domain '%s'\n", domain);
22525
22526 return 1;
22527 }
22528
22529
22530 static int check_sip_domain(const char *domain, char *context, size_t len)
22531 {
22532 struct domain *d;
22533 int result = 0;
22534
22535 AST_LIST_LOCK(&domain_list);
22536 AST_LIST_TRAVERSE(&domain_list, d, list) {
22537 if (strcasecmp(d->domain, domain))
22538 continue;
22539
22540 if (len && !ast_strlen_zero(d->context))
22541 ast_copy_string(context, d->context, len);
22542
22543 result = 1;
22544 break;
22545 }
22546 AST_LIST_UNLOCK(&domain_list);
22547
22548 return result;
22549 }
22550
22551
22552 static void clear_sip_domains(void)
22553 {
22554 struct domain *d;
22555
22556 AST_LIST_LOCK(&domain_list);
22557 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
22558 ast_free(d);
22559 AST_LIST_UNLOCK(&domain_list);
22560 }
22561
22562
22563
22564 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, const char *configuration, int lineno)
22565 {
22566 char authcopy[256];
22567 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
22568 struct sip_auth *a, *b, *auth;
22569
22570 if (ast_strlen_zero(configuration))
22571 return authlist;
22572
22573 ast_debug(1, "Auth config :: %s\n", configuration);
22574
22575 ast_copy_string(authcopy, configuration, sizeof(authcopy));
22576 username = authcopy;
22577
22578
22579 realm = strrchr(username, '@');
22580 if (realm)
22581 *realm++ = '\0';
22582 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
22583 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
22584 return authlist;
22585 }
22586
22587
22588 if ((secret = strchr(username, ':'))) {
22589 *secret++ = '\0';
22590 } else if ((md5secret = strchr(username, '#'))) {
22591 *md5secret++ = '\0';
22592 }
22593
22594 if (!(auth = ast_calloc(1, sizeof(*auth))))
22595 return authlist;
22596
22597 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
22598 ast_copy_string(auth->username, username, sizeof(auth->username));
22599 if (secret)
22600 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
22601 if (md5secret)
22602 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
22603
22604
22605 for (b = NULL, a = authlist; a ; b = a, a = a->next)
22606 ;
22607 if (b)
22608 b->next = auth;
22609 else
22610 authlist = auth;
22611
22612 ast_verb(3, "Added authentication for realm %s\n", realm);
22613
22614 return authlist;
22615
22616 }
22617
22618
22619 static int clear_realm_authentication(struct sip_auth *authlist)
22620 {
22621 struct sip_auth *a = authlist;
22622 struct sip_auth *b;
22623
22624 while (a) {
22625 b = a;
22626 a = a->next;
22627 ast_free(b);
22628 }
22629
22630 return 1;
22631 }
22632
22633
22634 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
22635 {
22636 struct sip_auth *a;
22637
22638 for (a = authlist; a; a = a->next) {
22639 if (!strcasecmp(a->realm, realm))
22640 break;
22641 }
22642
22643 return a;
22644 }
22645
22646
22647
22648
22649 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
22650 {
22651 struct ast_variable *tmpvar = NULL;
22652 char *varname = ast_strdupa(buf), *varval = NULL;
22653
22654 if ((varval = strchr(varname, '='))) {
22655 *varval++ = '\0';
22656 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
22657 tmpvar->next = list;
22658 list = tmpvar;
22659 }
22660 }
22661 return list;
22662 }
22663
22664
22665 static void set_peer_defaults(struct sip_peer *peer)
22666 {
22667 if (peer->expire == 0) {
22668
22669
22670
22671 peer->expire = -1;
22672 peer->pokeexpire = -1;
22673 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
22674 set_socket_transport(&peer->socket, SIP_TRANSPORT_UDP);
22675 }
22676 peer->type = SIP_TYPE_PEER;
22677 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
22678 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
22679 strcpy(peer->context, default_context);
22680 strcpy(peer->subscribecontext, default_subscribecontext);
22681 strcpy(peer->language, default_language);
22682 strcpy(peer->mohinterpret, default_mohinterpret);
22683 strcpy(peer->mohsuggest, default_mohsuggest);
22684 peer->addr.sin_family = AF_INET;
22685 peer->defaddr.sin_family = AF_INET;
22686 peer->capability = global_capability;
22687 peer->maxcallbitrate = default_maxcallbitrate;
22688 peer->rtptimeout = global_rtptimeout;
22689 peer->rtpholdtimeout = global_rtpholdtimeout;
22690 peer->rtpkeepalive = global_rtpkeepalive;
22691 peer->allowtransfer = global_allowtransfer;
22692 peer->autoframing = global_autoframing;
22693 peer->t38_maxdatagram = global_t38_maxdatagram;
22694 peer->qualifyfreq = global_qualifyfreq;
22695 if (global_callcounter)
22696 peer->call_limit=999;
22697 strcpy(peer->vmexten, default_vmexten);
22698 peer->secret[0] = '\0';
22699 peer->md5secret[0] = '\0';
22700 peer->cid_num[0] = '\0';
22701 peer->cid_name[0] = '\0';
22702 peer->fromdomain[0] = '\0';
22703 peer->fromuser[0] = '\0';
22704 peer->regexten[0] = '\0';
22705 peer->callgroup = 0;
22706 peer->pickupgroup = 0;
22707 peer->maxms = default_qualify;
22708 peer->prefs = default_prefs;
22709 peer->stimer.st_mode_oper = global_st_mode;
22710 peer->stimer.st_ref = global_st_refresher;
22711 peer->stimer.st_min_se = global_min_se;
22712 peer->stimer.st_max_se = global_max_se;
22713 peer->timer_t1 = global_t1;
22714 peer->timer_b = global_timer_b;
22715 clear_peer_mailboxes(peer);
22716 }
22717
22718
22719 static struct sip_peer *temp_peer(const char *name)
22720 {
22721 struct sip_peer *peer;
22722
22723 if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct")))
22724 return NULL;
22725
22726 ast_atomic_fetchadd_int(&apeerobjs, 1);
22727 set_peer_defaults(peer);
22728
22729 ast_copy_string(peer->name, name, sizeof(peer->name));
22730
22731 peer->selfdestruct = TRUE;
22732 peer->host_dynamic = TRUE;
22733 peer->prefs = default_prefs;
22734 reg_source_db(peer);
22735
22736 return peer;
22737 }
22738
22739
22740 static void add_peer_mailboxes(struct sip_peer *peer, const char *value)
22741 {
22742 char *next, *mbox, *context;
22743
22744 next = ast_strdupa(value);
22745
22746 while ((mbox = context = strsep(&next, ","))) {
22747 struct sip_mailbox *mailbox;
22748
22749 if (!(mailbox = ast_calloc(1, sizeof(*mailbox))))
22750 continue;
22751
22752 strsep(&context, "@");
22753 if (ast_strlen_zero(mbox)) {
22754 ast_free(mailbox);
22755 continue;
22756 }
22757 mailbox->mailbox = ast_strdup(mbox);
22758 mailbox->context = ast_strdup(context);
22759
22760 AST_LIST_INSERT_TAIL(&peer->mailboxes, mailbox, entry);
22761 }
22762 }
22763
22764
22765 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only)
22766 {
22767 struct sip_peer *peer = NULL;
22768 struct ast_ha *oldha = NULL;
22769 int found = 0;
22770 int firstpass = 1;
22771 uint16_t port = 0;
22772 int format = 0;
22773 time_t regseconds = 0;
22774 struct ast_flags peerflags[2] = {{(0)}};
22775 struct ast_flags mask[2] = {{(0)}};
22776 char callback[256] = "";
22777 struct sip_peer tmp_peer;
22778 const char *srvlookup = NULL;
22779 static int deprecation_warning = 1;
22780 int alt_fullcontact = alt ? 1 : 0;
22781 struct ast_str *fullcontact = ast_str_alloca(512);
22782
22783 if (!realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
22784
22785
22786
22787
22788
22789 ast_copy_string(tmp_peer.name, name, sizeof(tmp_peer.name));
22790 peer = ao2_t_find(peers, &tmp_peer, OBJ_POINTER | OBJ_UNLINK, "find and unlink peer from peers table");
22791 }
22792
22793 if (peer) {
22794
22795 found++;
22796 if (!(peer->the_mark))
22797 firstpass = 0;
22798 } else {
22799 if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct")))
22800 return NULL;
22801
22802 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
22803 ast_atomic_fetchadd_int(&rpeerobjs, 1);
22804 ast_debug(3, "-REALTIME- peer built. Name: %s. Peer objects: %d\n", name, rpeerobjs);
22805 } else
22806 ast_atomic_fetchadd_int(&speerobjs, 1);
22807 }
22808
22809
22810 if (firstpass) {
22811 peer->lastmsgssent = -1;
22812 oldha = peer->ha;
22813 peer->ha = NULL;
22814 set_peer_defaults(peer);
22815 peer->type = 0;
22816 }
22817 if (!found && name)
22818 ast_copy_string(peer->name, name, sizeof(peer->name));
22819
22820
22821 if (peer->chanvars) {
22822 ast_variables_destroy(peer->chanvars);
22823 peer->chanvars = NULL;
22824
22825 }
22826
22827 if (found)
22828 peer->portinuri = 0;
22829
22830
22831 clear_realm_authentication(peer->auth);
22832 peer->auth = NULL;
22833 peer->default_outbound_transport = 0;
22834 peer->transports = 0;
22835
22836 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
22837 if (!devstate_only) {
22838 if (handle_common_options(&peerflags[0], &mask[0], v)) {
22839 continue;
22840 }
22841 if (handle_t38_options(&peerflags[0], &mask[0], v, &peer->t38_maxdatagram)) {
22842 continue;
22843 }
22844 if (!strcasecmp(v->name, "transport") && !ast_strlen_zero(v->value)) {
22845 char *val = ast_strdupa(v->value);
22846 char *trans;
22847
22848 while ((trans = strsep(&val, ","))) {
22849 trans = ast_skip_blanks(trans);
22850
22851 if (!strncasecmp(trans, "udp", 3)) {
22852 peer->transports |= SIP_TRANSPORT_UDP;
22853 } else if (!strncasecmp(trans, "tcp", 3)) {
22854 peer->transports |= SIP_TRANSPORT_TCP;
22855 } else if (!strncasecmp(trans, "tls", 3)) {
22856 peer->transports |= SIP_TRANSPORT_TLS;
22857 } else {
22858 ast_log(LOG_NOTICE, "'%s' is not a valid transport type. if no other is specified, udp will be used.\n", trans);
22859 }
22860
22861 if (!peer->default_outbound_transport) {
22862 peer->default_outbound_transport = peer->transports;
22863 }
22864 }
22865 } else if (realtime && !strcasecmp(v->name, "regseconds")) {
22866 ast_get_time_t(v->value, ®seconds, 0, NULL);
22867 } else if (realtime && !strcasecmp(v->name, "name")) {
22868 ast_copy_string(peer->name, v->value, sizeof(peer->name));
22869 } else if (!strcasecmp(v->name, "type")) {
22870 if (!strcasecmp(v->value, "peer")) {
22871 peer->type |= SIP_TYPE_PEER;
22872 } else if (!strcasecmp(v->value, "user")) {
22873 peer->type |= SIP_TYPE_USER;
22874 } else if (!strcasecmp(v->value, "friend")) {
22875 peer->type = SIP_TYPE_USER | SIP_TYPE_PEER;
22876 }
22877 } else if (!strcasecmp(v->name, "secret")) {
22878 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
22879 } else if (!strcasecmp(v->name, "md5secret")) {
22880 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
22881 } else if (!strcasecmp(v->name, "auth")) {
22882 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
22883 } else if (!strcasecmp(v->name, "callerid")) {
22884 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
22885 } else if (!strcasecmp(v->name, "fullname")) {
22886 ast_copy_string(peer->cid_name, v->value, sizeof(peer->cid_name));
22887 } else if (!strcasecmp(v->name, "trunkname")) {
22888
22889 ast_copy_string(peer->cid_name, "", sizeof(peer->cid_name));
22890 } else if (!strcasecmp(v->name, "cid_number")) {
22891 ast_copy_string(peer->cid_num, v->value, sizeof(peer->cid_num));
22892 } else if (!strcasecmp(v->name, "context")) {
22893 ast_copy_string(peer->context, v->value, sizeof(peer->context));
22894 ast_set_flag(&peer->flags[1], SIP_PAGE2_HAVEPEERCONTEXT);
22895 } else if (!strcasecmp(v->name, "subscribecontext")) {
22896 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
22897 } else if (!strcasecmp(v->name, "fromdomain")) {
22898 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
22899 } else if (!strcasecmp(v->name, "usereqphone")) {
22900 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
22901 } else if (!strcasecmp(v->name, "fromuser")) {
22902 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
22903 } else if (!strcasecmp(v->name, "outboundproxy")) {
22904 char *port, *next, *force, *proxyname;
22905 int forceopt = FALSE;
22906
22907 next = proxyname = ast_strdupa(v->value);
22908 if ((port = strchr(proxyname, ':'))) {
22909 *port++ = '\0';
22910 next = port;
22911 }
22912 if ((force = strchr(next, ','))) {
22913 *force++ = '\0';
22914 forceopt = strcmp(force, "force");
22915 }
22916
22917 peer->outboundproxy = proxy_allocate(proxyname, port, forceopt);
22918 } else if (!strcasecmp(v->name, "host")) {
22919 if (!strcasecmp(v->value, "dynamic")) {
22920
22921 if (!found || !peer->host_dynamic) {
22922
22923
22924 memset(&peer->addr.sin_addr, 0, 4);
22925 peer->addr.sin_port = 0;
22926 }
22927 peer->host_dynamic = TRUE;
22928 } else {
22929
22930 AST_SCHED_DEL_UNREF(sched, peer->expire,
22931 unref_peer(peer, "removing register expire ref"));
22932
22933 peer->addr.sin_port = 0;
22934 peer->host_dynamic = FALSE;
22935 srvlookup = v->value;
22936 if (global_dynamic_exclude_static) {
22937 int err = 0;
22938 global_contact_ha = ast_append_ha("deny", (char *)ast_inet_ntoa(peer->addr.sin_addr), global_contact_ha, &err);
22939 if (err) {
22940 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
22941 }
22942 }
22943 }
22944 } else if (!strcasecmp(v->name, "defaultip")) {
22945 if (!ast_strlen_zero(v->value) && ast_get_ip(&peer->defaddr, v->value)) {
22946 unref_peer(peer, "unref_peer: from build_peer defaultip");
22947 return NULL;
22948 }
22949 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
22950 int ha_error = 0;
22951 if (!ast_strlen_zero(v->value)) {
22952 peer->ha = ast_append_ha(v->name, v->value, peer->ha, &ha_error);
22953 }
22954 if (ha_error) {
22955 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
22956 }
22957 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
22958 int ha_error = 0;
22959 if (!ast_strlen_zero(v->value)) {
22960 peer->contactha = ast_append_ha(v->name + 7, v->value, peer->contactha, &ha_error);
22961 }
22962 if (ha_error) {
22963 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
22964 }
22965 } else if (!strcasecmp(v->name, "port")) {
22966 peer->portinuri = 1;
22967 if (!(port = port_str2int(v->value, 0))) {
22968 if (realtime) {
22969
22970 peer->portinuri = 0;
22971 } else {
22972 ast_log(LOG_WARNING, "Invalid peer port configuration at line %d : %s\n", v->lineno, v->value);
22973 }
22974 }
22975 } else if (!strcasecmp(v->name, "callingpres")) {
22976 peer->callingpres = ast_parse_caller_presentation(v->value);
22977 if (peer->callingpres == -1) {
22978 peer->callingpres = atoi(v->value);
22979 }
22980 } else if (!strcasecmp(v->name, "username") || !strcmp(v->name, "defaultuser")) {
22981 ast_copy_string(peer->username, v->value, sizeof(peer->username));
22982 if (!strcasecmp(v->name, "username")) {
22983 if (deprecation_warning) {
22984 ast_log(LOG_NOTICE, "The 'username' field for sip peers has been deprecated in favor of the term 'defaultuser'\n");
22985 deprecation_warning = 0;
22986 }
22987 peer->deprecated_username = 1;
22988 }
22989 } else if (!strcasecmp(v->name, "language")) {
22990 ast_copy_string(peer->language, v->value, sizeof(peer->language));
22991 } else if (!strcasecmp(v->name, "regexten")) {
22992 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
22993 } else if (!strcasecmp(v->name, "callbackextension")) {
22994 ast_copy_string(callback, v->value, sizeof(callback));
22995 } else if (!strcasecmp(v->name, "amaflags")) {
22996 format = ast_cdr_amaflags2int(v->value);
22997 if (format < 0) {
22998 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
22999 } else {
23000 peer->amaflags = format;
23001 }
23002 } else if (!strcasecmp(v->name, "accountcode")) {
23003 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
23004 } else if (!strcasecmp(v->name, "mohinterpret")) {
23005 ast_copy_string(peer->mohinterpret, v->value, sizeof(peer->mohinterpret));
23006 } else if (!strcasecmp(v->name, "mohsuggest")) {
23007 ast_copy_string(peer->mohsuggest, v->value, sizeof(peer->mohsuggest));
23008 } else if (!strcasecmp(v->name, "parkinglot")) {
23009 ast_copy_string(peer->parkinglot, v->value, sizeof(peer->parkinglot));
23010 } else if (!strcasecmp(v->name, "mailbox")) {
23011 add_peer_mailboxes(peer, v->value);
23012 } else if (!strcasecmp(v->name, "hasvoicemail")) {
23013
23014
23015 if (ast_true(v->value) && AST_LIST_EMPTY(&peer->mailboxes)) {
23016 add_peer_mailboxes(peer, name);
23017 }
23018 } else if (!strcasecmp(v->name, "subscribemwi")) {
23019 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
23020 } else if (!strcasecmp(v->name, "vmexten")) {
23021 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
23022 } else if (!strcasecmp(v->name, "callgroup")) {
23023 peer->callgroup = ast_get_group(v->value);
23024 } else if (!strcasecmp(v->name, "allowtransfer")) {
23025 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
23026 } else if (!strcasecmp(v->name, "pickupgroup")) {
23027 peer->pickupgroup = ast_get_group(v->value);
23028 } else if (!strcasecmp(v->name, "allow")) {
23029 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, TRUE);
23030 if (error) {
23031 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
23032 }
23033 } else if (!strcasecmp(v->name, "disallow")) {
23034 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, FALSE);
23035 if (error) {
23036 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
23037 }
23038 } else if (!strcasecmp(v->name, "registertrying")) {
23039 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_REGISTERTRYING);
23040 } else if (!strcasecmp(v->name, "autoframing")) {
23041 peer->autoframing = ast_true(v->value);
23042 } else if (!strcasecmp(v->name, "rtptimeout")) {
23043 if ((sscanf(v->value, "%30d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
23044 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
23045 peer->rtptimeout = global_rtptimeout;
23046 }
23047 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
23048 if ((sscanf(v->value, "%30d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
23049 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
23050 peer->rtpholdtimeout = global_rtpholdtimeout;
23051 }
23052 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
23053 if ((sscanf(v->value, "%30d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
23054 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
23055 peer->rtpkeepalive = global_rtpkeepalive;
23056 }
23057 } else if (!strcasecmp(v->name, "timert1")) {
23058 if ((sscanf(v->value, "%30d", &peer->timer_t1) != 1) || (peer->timer_t1 < 0)) {
23059 ast_log(LOG_WARNING, "'%s' is not a valid T1 time at line %d. Using default.\n", v->value, v->lineno);
23060 peer->timer_t1 = global_t1;
23061 }
23062
23063
23064 if (peer->timer_b < peer->timer_t1 * 64) {
23065 peer->timer_b = peer->timer_t1 * 64;
23066 }
23067 } else if (!strcasecmp(v->name, "timerb")) {
23068 if ((sscanf(v->value, "%30d", &peer->timer_b) != 1) || (peer->timer_b < 0)) {
23069 ast_log(LOG_WARNING, "'%s' is not a valid Timer B time at line %d. Using default.\n", v->value, v->lineno);
23070 peer->timer_b = global_timer_b;
23071 }
23072 if (peer->timer_b < peer->timer_t1 * 64) {
23073 static int warning = 0;
23074 if (warning++ % 20 == 0) {
23075 ast_log(LOG_WARNING, "Timer B has been set lower than recommended. (RFC 3261, 17.1.1.2)\n");
23076 }
23077 }
23078 } else if (!strcasecmp(v->name, "setvar")) {
23079 peer->chanvars = add_var(v->value, peer->chanvars);
23080 } else if (!strcasecmp(v->name, "qualifyfreq")) {
23081 int i;
23082 if (sscanf(v->value, "%30d", &i) == 1) {
23083 peer->qualifyfreq = i * 1000;
23084 } else {
23085 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
23086 peer->qualifyfreq = global_qualifyfreq;
23087 }
23088 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
23089 peer->maxcallbitrate = atoi(v->value);
23090 if (peer->maxcallbitrate < 0) {
23091 peer->maxcallbitrate = default_maxcallbitrate;
23092 }
23093 } else if (!strcasecmp(v->name, "session-timers")) {
23094 int i = (int) str2stmode(v->value);
23095 if (i < 0) {
23096 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
23097 peer->stimer.st_mode_oper = global_st_mode;
23098 } else {
23099 peer->stimer.st_mode_oper = i;
23100 }
23101 } else if (!strcasecmp(v->name, "session-expires")) {
23102 if (sscanf(v->value, "%30d", &peer->stimer.st_max_se) != 1) {
23103 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
23104 peer->stimer.st_max_se = global_max_se;
23105 }
23106 } else if (!strcasecmp(v->name, "session-minse")) {
23107 if (sscanf(v->value, "%30d", &peer->stimer.st_min_se) != 1) {
23108 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
23109 peer->stimer.st_min_se = global_min_se;
23110 }
23111 if (peer->stimer.st_min_se < 90) {
23112 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);
23113 peer->stimer.st_min_se = global_min_se;
23114 }
23115 } else if (!strcasecmp(v->name, "session-refresher")) {
23116 int i = (int) str2strefresher(v->value);
23117 if (i < 0) {
23118 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
23119 peer->stimer.st_ref = global_st_refresher;
23120 } else {
23121 peer->stimer.st_ref = i;
23122 }
23123 }
23124 }
23125
23126
23127 if (realtime && !strcasecmp(v->name, "lastms")) {
23128 sscanf(v->value, "%30d", &peer->lastms);
23129 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
23130 inet_aton(v->value, &(peer->addr.sin_addr));
23131 } else if (realtime && !strcasecmp(v->name, "fullcontact")) {
23132 if (alt_fullcontact && !alt) {
23133
23134
23135
23136
23137
23138 alt_fullcontact = 0;
23139 ast_str_reset(fullcontact);
23140 }
23141
23142 if (fullcontact->used > 0) {
23143 ast_str_append(&fullcontact, 0, ";%s", v->value);
23144 } else {
23145 ast_str_set(&fullcontact, 0, "%s", v->value);
23146 }
23147 } else if (!strcasecmp(v->name, "qualify")) {
23148 if (!strcasecmp(v->value, "no")) {
23149 peer->maxms = 0;
23150 } else if (!strcasecmp(v->value, "yes")) {
23151 peer->maxms = default_qualify ? default_qualify : DEFAULT_MAXMS;
23152 } else if (sscanf(v->value, "%30d", &peer->maxms) != 1) {
23153 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);
23154 peer->maxms = 0;
23155 }
23156 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && peer->maxms > 0) {
23157
23158
23159
23160
23161 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);
23162 peer->maxms = 0;
23163 }
23164 } else if (!strcasecmp(v->name, "callcounter")) {
23165 peer->call_limit = ast_true(v->value) ? INT_MAX : 0;
23166 } else if (!strcasecmp(v->name, "call-limit")) {
23167 peer->call_limit = atoi(v->value);
23168 if (peer->call_limit < 0) {
23169 peer->call_limit = 0;
23170 }
23171 } else if (!strcasecmp(v->name, "busylevel")) {
23172 peer->busy_level = atoi(v->value);
23173 if (peer->busy_level < 0) {
23174 peer->busy_level = 0;
23175 }
23176 }
23177 }
23178
23179 if (!peer->default_outbound_transport) {
23180 peer->transports = SIP_TRANSPORT_UDP;
23181 peer->default_outbound_transport = SIP_TRANSPORT_UDP;
23182 }
23183
23184
23185
23186
23187
23188 if (((peer->socket.type != peer->default_outbound_transport) && (peer->expire == -1)) ||
23189 !(peer->socket.type & peer->transports) || !(peer->socket.type)) {
23190
23191 set_socket_transport(&peer->socket, peer->default_outbound_transport);
23192 }
23193
23194 if (port && !realtime && peer->host_dynamic) {
23195 peer->defaddr.sin_port = htons(port);
23196 } else if (port) {
23197 peer->addr.sin_port = htons(port);
23198 }
23199
23200 if (ast_str_strlen(fullcontact)) {
23201 ast_copy_string(peer->fullcontact, fullcontact->str, sizeof(peer->fullcontact));
23202 peer->rt_fromcontact = TRUE;
23203
23204
23205
23206
23207
23208
23209
23210
23211 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE) || !peer->addr.sin_addr.s_addr) {
23212 __set_address_from_contact(fullcontact->str, &peer->addr, 0);
23213 }
23214 }
23215
23216 if (srvlookup && peer->dnsmgr == NULL) {
23217 char transport[MAXHOSTNAMELEN];
23218 char _srvlookup[MAXHOSTNAMELEN];
23219 char *params;
23220
23221 ast_copy_string(_srvlookup, srvlookup, sizeof(_srvlookup));
23222 if ((params = strchr(_srvlookup, ';'))) {
23223 *params++ = '\0';
23224 }
23225
23226 snprintf(transport, sizeof(transport), "_sip._%s", get_transport(peer->socket.type));
23227
23228 if (ast_dnsmgr_lookup(_srvlookup, &peer->addr, &peer->dnsmgr, global_srvlookup && !peer->portinuri ? transport : NULL)) {
23229 ast_log(LOG_ERROR, "srvlookup failed for host: %s, on peer %s, removing peer\n", _srvlookup, peer->name);
23230 unref_peer(peer, "getting rid of a peer pointer");
23231 return NULL;
23232 }
23233
23234 ast_copy_string(peer->tohost, srvlookup, sizeof(peer->tohost));
23235 }
23236
23237 if (!peer->addr.sin_port) {
23238 peer->addr.sin_port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
23239 }
23240 if (!peer->defaddr.sin_port) {
23241 peer->defaddr.sin_port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
23242 }
23243 if (!peer->socket.port) {
23244 peer->socket.port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
23245 }
23246
23247 if (!sip_cfg.ignore_regexpire && peer->host_dynamic && realtime) {
23248 time_t nowtime = time(NULL);
23249
23250 if ((nowtime - regseconds) > 0) {
23251 destroy_association(peer);
23252 memset(&peer->addr, 0, sizeof(peer->addr));
23253 peer->lastms = -1;
23254 ast_debug(1, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
23255 }
23256 }
23257
23258
23259 if (!devstate_only && realtime && peer->lastms > 0) {
23260 ref_peer(peer, "schedule qualify");
23261 sip_poke_peer(peer, 0);
23262 }
23263
23264 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
23265 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
23266 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
23267 global_allowsubscribe = TRUE;
23268 }
23269 if (!found && peer->host_dynamic && !peer->is_realtime) {
23270 reg_source_db(peer);
23271 }
23272
23273
23274
23275 if (!devstate_only && !ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
23276 !AST_LIST_EMPTY(&peer->mailboxes)) {
23277 add_peer_mwi_subs(peer);
23278
23279
23280
23281 sip_send_mwi_to_peer(peer, NULL, 1);
23282 }
23283
23284 peer->the_mark = 0;
23285
23286 ast_free_ha(oldha);
23287 if (!ast_strlen_zero(callback)) {
23288 char *reg_string;
23289
23290 if (asprintf(®_string, "%s?%s:%s@%s/%s", peer->name, peer->username, peer->secret, peer->tohost, callback) < 0) {
23291 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
23292 } else if (reg_string) {
23293 sip_register(reg_string, 0);
23294 ast_free(reg_string);
23295 }
23296 }
23297 return peer;
23298 }
23299
23300 static int peer_markall_func(void *device, void *arg, int flags)
23301 {
23302 struct sip_peer *peer = device;
23303 peer->the_mark = 1;
23304 return 0;
23305 }
23306
23307
23308
23309
23310
23311
23312
23313 static int reload_config(enum channelreloadreason reason)
23314 {
23315 struct ast_config *cfg, *ucfg;
23316 struct ast_variable *v;
23317 struct sip_peer *peer;
23318 char *cat, *stringp, *context, *oldregcontext;
23319 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
23320 struct ast_flags dummy[2];
23321 struct ast_flags config_flags = { reason == CHANNEL_MODULE_LOAD ? 0 : ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? 0 : CONFIG_FLAG_FILEUNCHANGED };
23322 int auto_sip_domains = FALSE;
23323 struct sockaddr_in old_bindaddr = bindaddr;
23324 int registry_count = 0, peer_count = 0;
23325 time_t run_start, run_end;
23326
23327 run_start = time(0);
23328 ast_unload_realtime("sipregs");
23329 ast_unload_realtime("sippeers");
23330 cfg = ast_config_load(config, config_flags);
23331
23332
23333 if (!cfg) {
23334 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
23335 return -1;
23336 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
23337 ucfg = ast_config_load("users.conf", config_flags);
23338 if (ucfg == CONFIG_STATUS_FILEUNCHANGED)
23339 return 1;
23340
23341 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
23342 cfg = ast_config_load(config, config_flags);
23343 } else {
23344 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
23345 ucfg = ast_config_load("users.conf", config_flags);
23346 }
23347
23348
23349 memset(&sip_tcp_desc.local_address, 0, sizeof(sip_tcp_desc.local_address));
23350 memset(&sip_tls_desc.local_address, 0, sizeof(sip_tls_desc.local_address));
23351
23352 ast_free_ha(global_contact_ha);
23353 global_contact_ha = NULL;
23354
23355 default_tls_cfg.enabled = FALSE;
23356
23357 sip_tcp_desc.local_address.sin_port = htons(STANDARD_SIP_PORT);
23358 sip_tls_desc.local_address.sin_port = htons(STANDARD_TLS_PORT);
23359
23360 if (reason != CHANNEL_MODULE_LOAD) {
23361 ast_debug(4, "--------------- SIP reload started\n");
23362
23363 clear_realm_authentication(authl);
23364 clear_sip_domains();
23365 authl = NULL;
23366
23367
23368
23369 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
23370
23371 ASTOBJ_RDLOCK(iterator);
23372 if (iterator->call) {
23373 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
23374
23375 dialog_unlink_all(iterator->call, TRUE, TRUE);
23376 iterator->call = dialog_unref(iterator->call, "remove iterator->call from registry traversal");
23377 }
23378 if (iterator->expire > -1) {
23379 AST_SCHED_DEL_UNREF(sched, iterator->expire, registry_unref(iterator, "reg ptr unref from reload config"));
23380 }
23381 if (iterator->timeout > -1) {
23382 AST_SCHED_DEL_UNREF(sched, iterator->timeout, registry_unref(iterator, "reg ptr unref from reload config"));
23383 }
23384 ASTOBJ_UNLOCK(iterator);
23385
23386 } while(0));
23387
23388
23389 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
23390 ast_debug(4, "--------------- Done destroying registry list\n");
23391 ao2_t_callback(peers, OBJ_NODATA, peer_markall_func, 0, "callback to mark all peers");
23392 }
23393
23394
23395 if (reason != CHANNEL_MODULE_LOAD) {
23396 ast_free(default_tls_cfg.certfile);
23397 ast_free(default_tls_cfg.cipher);
23398 ast_free(default_tls_cfg.cafile);
23399 ast_free(default_tls_cfg.capath);
23400 }
23401 default_tls_cfg.certfile = ast_strdup(AST_CERTFILE);
23402 default_tls_cfg.cipher = ast_strdup("");
23403 default_tls_cfg.cafile = ast_strdup("");
23404 default_tls_cfg.capath = ast_strdup("");
23405
23406
23407 ast_copy_string(oldcontexts, global_regcontext, sizeof(oldcontexts));
23408 oldregcontext = oldcontexts;
23409
23410
23411
23412 sipdebug &= sip_debug_console;
23413 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
23414 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
23415
23416
23417 memset(&bindaddr, 0, sizeof(bindaddr));
23418 memset(&stunaddr, 0, sizeof(stunaddr));
23419 memset(&internip, 0, sizeof(internip));
23420
23421
23422 ast_free_ha(localaddr);
23423 memset(&localaddr, 0, sizeof(localaddr));
23424 memset(&externip, 0, sizeof(externip));
23425 memset(&default_prefs, 0 , sizeof(default_prefs));
23426 memset(&global_outboundproxy, 0, sizeof(struct sip_proxy));
23427 global_outboundproxy.ip.sin_port = htons(STANDARD_SIP_PORT);
23428 global_outboundproxy.ip.sin_family = AF_INET;
23429 global_outboundproxy.force = FALSE;
23430 ourport_tcp = STANDARD_SIP_PORT;
23431 ourport_tls = STANDARD_TLS_PORT;
23432 bindaddr.sin_port = htons(STANDARD_SIP_PORT);
23433 global_srvlookup = DEFAULT_SRVLOOKUP;
23434 global_tos_sip = DEFAULT_TOS_SIP;
23435 global_tos_audio = DEFAULT_TOS_AUDIO;
23436 global_tos_video = DEFAULT_TOS_VIDEO;
23437 global_tos_text = DEFAULT_TOS_TEXT;
23438 global_cos_sip = DEFAULT_COS_SIP;
23439 global_cos_audio = DEFAULT_COS_AUDIO;
23440 global_cos_video = DEFAULT_COS_VIDEO;
23441 global_cos_text = DEFAULT_COS_TEXT;
23442
23443 externhost[0] = '\0';
23444 externexpire = 0;
23445 externrefresh = 10;
23446
23447
23448 allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
23449 global_regcontext[0] = '\0';
23450 global_regextenonqualify = DEFAULT_REGEXTENONQUALIFY;
23451 global_notifyringing = DEFAULT_NOTIFYRINGING;
23452 global_notifyhold = FALSE;
23453 global_directrtpsetup = FALSE;
23454 global_alwaysauthreject = 0;
23455 global_allowsubscribe = FALSE;
23456 snprintf(global_useragent, sizeof(global_useragent), "%s %s", DEFAULT_USERAGENT, ast_get_version());
23457 snprintf(global_sdpsession, sizeof(global_sdpsession), "%s %s", DEFAULT_SDPSESSION, ast_get_version());
23458 snprintf(global_sdpowner, sizeof(global_sdpowner), "%s", DEFAULT_SDPOWNER);
23459 global_prematuremediafilter = FALSE;
23460 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
23461 ast_copy_string(global_realm, S_OR(ast_config_AST_SYSTEM_NAME, DEFAULT_REALM), sizeof(global_realm));
23462 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
23463 compactheaders = DEFAULT_COMPACTHEADERS;
23464 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
23465 global_regattempts_max = 0;
23466 pedanticsipchecking = DEFAULT_PEDANTIC;
23467 autocreatepeer = DEFAULT_AUTOCREATEPEER;
23468 global_autoframing = 0;
23469 global_allowguest = DEFAULT_ALLOWGUEST;
23470 global_callcounter = DEFAULT_CALLCOUNTER;
23471 global_match_auth_username = FALSE;
23472 global_rtptimeout = 0;
23473 global_rtpholdtimeout = 0;
23474 global_rtpkeepalive = 0;
23475 global_allowtransfer = TRANSFER_OPENFORALL;
23476 global_rtautoclear = 120;
23477 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
23478 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
23479 sip_cfg.peer_rtupdate = TRUE;
23480 global_dynamic_exclude_static = 0;
23481
23482
23483 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
23484 global_st_refresher = SESSION_TIMER_REFRESHER_UAS;
23485 global_min_se = DEFAULT_MIN_SE;
23486 global_max_se = DEFAULT_MAX_SE;
23487
23488
23489 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
23490 default_subscribecontext[0] = '\0';
23491 default_language[0] = '\0';
23492 default_fromdomain[0] = '\0';
23493 default_qualify = DEFAULT_QUALIFY;
23494 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
23495 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
23496 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
23497 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
23498 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
23499 ast_set_flag(&global_flags[0], SIP_NAT_RFC3581);
23500 ast_set_flag(&global_flags[0], SIP_CAN_REINVITE);
23501
23502
23503 dumphistory = FALSE;
23504 recordhistory = FALSE;
23505 sipdebug &= ~sip_debug_config;
23506
23507
23508 global_relaxdtmf = FALSE;
23509 global_callevents = FALSE;
23510 global_authfailureevents = FALSE;
23511 global_t1 = SIP_TIMER_T1;
23512 global_timer_b = 64 * SIP_TIMER_T1;
23513 global_t1min = DEFAULT_T1MIN;
23514 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
23515 global_t38_maxdatagram = -1;
23516 global_shrinkcallerid = 1;
23517
23518 global_matchexterniplocally = FALSE;
23519
23520
23521 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
23522
23523 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT | SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
23524 ast_clear_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT);
23525 ast_clear_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION);
23526 ast_clear_flag(&global_flags[1], SIP_PAGE2_FAX_DETECT);
23527
23528
23529
23530 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
23531 if (handle_common_options(&global_flags[0], &dummy[0], v))
23532 continue;
23533 if (handle_t38_options(&global_flags[0], &dummy[0], v, &global_t38_maxdatagram)) {
23534 continue;
23535 }
23536
23537 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
23538 continue;
23539
23540 if (!strcasecmp(v->name, "context")) {
23541 ast_copy_string(default_context, v->value, sizeof(default_context));
23542 } else if (!strcasecmp(v->name, "subscribecontext")) {
23543 ast_copy_string(default_subscribecontext, v->value, sizeof(default_subscribecontext));
23544 } else if (!strcasecmp(v->name, "callcounter")) {
23545 global_callcounter = ast_true(v->value) ? 1 : 0;
23546 } else if (!strcasecmp(v->name, "allowguest")) {
23547 global_allowguest = ast_true(v->value) ? 1 : 0;
23548 } else if (!strcasecmp(v->name, "realm")) {
23549 ast_copy_string(global_realm, v->value, sizeof(global_realm));
23550 } else if (!strcasecmp(v->name, "useragent")) {
23551 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
23552 ast_debug(1, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
23553 } else if (!strcasecmp(v->name, "sdpsession")) {
23554 ast_copy_string(global_sdpsession, v->value, sizeof(global_sdpsession));
23555 } else if (!strcasecmp(v->name, "sdpowner")) {
23556
23557 if (!strstr(v->value, " "))
23558 ast_copy_string(global_sdpowner, v->value, sizeof(global_sdpowner));
23559 else
23560 ast_log(LOG_WARNING, "'%s' must not contain spaces at line %d. Using default.\n", v->value, v->lineno);
23561 } else if (!strcasecmp(v->name, "allowtransfer")) {
23562 global_allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
23563 } else if (!strcasecmp(v->name, "rtcachefriends")) {
23564 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
23565 } else if (!strcasecmp(v->name, "rtsavesysname")) {
23566 sip_cfg.rtsave_sysname = ast_true(v->value);
23567 } else if (!strcasecmp(v->name, "rtupdate")) {
23568 sip_cfg.peer_rtupdate = ast_true(v->value);
23569 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
23570 sip_cfg.ignore_regexpire = ast_true(v->value);
23571 } else if (!strcasecmp(v->name, "timert1")) {
23572
23573
23574
23575 global_t1 = atoi(v->value);
23576
23577 global_timer_b = global_t1 * 64;
23578 } else if (!strcasecmp(v->name, "t1min")) {
23579 global_t1min = atoi(v->value);
23580 } else if (!strcasecmp(v->name, "tcpenable")) {
23581 sip_tcp_desc.local_address.sin_family = ast_false(v->value) ? 0 : AF_INET;
23582 ast_debug(2, "Enabling TCP socket for listening\n");
23583 } else if (!strcasecmp(v->name, "tcpbindaddr")) {
23584 int family = sip_tcp_desc.local_address.sin_family;
23585 if (ast_parse_arg(v->value, PARSE_INADDR, &sip_tcp_desc.local_address))
23586 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n", v->name, v->value, v->lineno, config);
23587 sip_tcp_desc.local_address.sin_family = family;
23588 ast_debug(2, "Setting TCP socket address to %s\n", v->value);
23589 } else if (!strcasecmp(v->name, "tlsenable")) {
23590 default_tls_cfg.enabled = ast_true(v->value) ? TRUE : FALSE;
23591 sip_tls_desc.local_address.sin_family = AF_INET;
23592 } else if (!strcasecmp(v->name, "tlscertfile")) {
23593 ast_free(default_tls_cfg.certfile);
23594 default_tls_cfg.certfile = ast_strdup(v->value);
23595 } else if (!strcasecmp(v->name, "tlscipher")) {
23596 ast_free(default_tls_cfg.cipher);
23597 default_tls_cfg.cipher = ast_strdup(v->value);
23598 } else if (!strcasecmp(v->name, "tlscafile")) {
23599 ast_free(default_tls_cfg.cafile);
23600 default_tls_cfg.cafile = ast_strdup(v->value);
23601 } else if (!strcasecmp(v->name, "tlscapath")) {
23602 ast_free(default_tls_cfg.capath);
23603 default_tls_cfg.capath = ast_strdup(v->value);
23604 } else if (!strcasecmp(v->name, "tlsverifyclient")) {
23605 ast_set2_flag(&default_tls_cfg.flags, ast_true(v->value), AST_SSL_VERIFY_CLIENT);
23606 } else if (!strcasecmp(v->name, "tlsdontverifyserver")) {
23607 ast_set2_flag(&default_tls_cfg.flags, ast_true(v->value), AST_SSL_DONT_VERIFY_SERVER);
23608 } else if (!strcasecmp(v->name, "tlsbindaddr")) {
23609 if (ast_parse_arg(v->value, PARSE_INADDR, &sip_tls_desc.local_address))
23610 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n", v->name, v->value, v->lineno, config);
23611 } else if (!strcasecmp(v->name, "dynamic_exclude_static") || !strcasecmp(v->name, "dynamic_excludes_static")) {
23612 global_dynamic_exclude_static = ast_true(v->value);
23613 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
23614 int ha_error = 0;
23615 global_contact_ha = ast_append_ha(v->name + 7, v->value, global_contact_ha, &ha_error);
23616 if (ha_error) {
23617 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
23618 }
23619 } else if (!strcasecmp(v->name, "rtautoclear")) {
23620 int i = atoi(v->value);
23621 if (i > 0)
23622 global_rtautoclear = i;
23623 else
23624 i = 0;
23625 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
23626 } else if (!strcasecmp(v->name, "usereqphone")) {
23627 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
23628 } else if (!strcasecmp(v->name, "prematuremedia")) {
23629 global_prematuremediafilter = ast_true(v->value);
23630 } else if (!strcasecmp(v->name, "relaxdtmf")) {
23631 global_relaxdtmf = ast_true(v->value);
23632 } else if (!strcasecmp(v->name, "vmexten")) {
23633 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
23634 } else if (!strcasecmp(v->name, "rtptimeout")) {
23635 if ((sscanf(v->value, "%30d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
23636 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
23637 global_rtptimeout = 0;
23638 }
23639 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
23640 if ((sscanf(v->value, "%30d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
23641 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
23642 global_rtpholdtimeout = 0;
23643 }
23644 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
23645 if ((sscanf(v->value, "%30d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
23646 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
23647 global_rtpkeepalive = 0;
23648 }
23649 } else if (!strcasecmp(v->name, "compactheaders")) {
23650 compactheaders = ast_true(v->value);
23651 } else if (!strcasecmp(v->name, "notifymimetype")) {
23652 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
23653 } else if (!strcasecmp(v->name, "directrtpsetup")) {
23654 global_directrtpsetup = ast_true(v->value);
23655 } else if (!strcasecmp(v->name, "notifyringing")) {
23656 global_notifyringing = ast_true(v->value);
23657 } else if (!strcasecmp(v->name, "notifyhold")) {
23658 global_notifyhold = ast_true(v->value);
23659 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
23660 global_alwaysauthreject = ast_true(v->value);
23661 } else if (!strcasecmp(v->name, "mohinterpret")) {
23662 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
23663 } else if (!strcasecmp(v->name, "mohsuggest")) {
23664 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
23665 } else if (!strcasecmp(v->name, "language")) {
23666 ast_copy_string(default_language, v->value, sizeof(default_language));
23667 } else if (!strcasecmp(v->name, "regcontext")) {
23668 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
23669 stringp = newcontexts;
23670
23671 cleanup_stale_contexts(stringp, oldregcontext);
23672
23673 while ((context = strsep(&stringp, "&"))) {
23674 ast_copy_string(used_context, context, sizeof(used_context));
23675 ast_context_find_or_create(NULL, NULL, context, "SIP");
23676 }
23677 ast_copy_string(global_regcontext, v->value, sizeof(global_regcontext));
23678 } else if (!strcasecmp(v->name, "regextenonqualify")) {
23679 global_regextenonqualify = ast_true(v->value);
23680 } else if (!strcasecmp(v->name, "callerid")) {
23681 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
23682 } else if (!strcasecmp(v->name, "fromdomain")) {
23683 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
23684 } else if (!strcasecmp(v->name, "outboundproxy")) {
23685 int portnum;
23686 char *tok, *proxyname;
23687
23688 if (ast_strlen_zero(v->value)) {
23689 ast_log(LOG_WARNING, "no value given for outbound proxy on line %d of sip.conf.", v->lineno);
23690 continue;
23691 }
23692
23693 tok = ast_skip_blanks(strtok(ast_strdupa(v->value), ","));
23694
23695 sip_parse_host(tok, v->lineno, &proxyname, &portnum, &global_outboundproxy.transport);
23696
23697 global_outboundproxy.ip.sin_port = htons(portnum);
23698
23699 if ((tok = strtok(NULL, ","))) {
23700 global_outboundproxy.force = !strncasecmp(ast_skip_blanks(tok), "force", 5);
23701 } else {
23702 global_outboundproxy.force = FALSE;
23703 }
23704
23705 if (ast_strlen_zero(proxyname)) {
23706 ast_log(LOG_WARNING, "you must specify a name for the outboundproxy on line %d of sip.conf.", v->lineno);
23707 global_outboundproxy.name[0] = '\0';
23708 continue;
23709 }
23710
23711 ast_copy_string(global_outboundproxy.name, proxyname, sizeof(global_outboundproxy.name));
23712
23713 proxy_update(&global_outboundproxy);
23714 } else if (!strcasecmp(v->name, "autocreatepeer")) {
23715 autocreatepeer = ast_true(v->value);
23716 } else if (!strcasecmp(v->name, "match_auth_username")) {
23717 global_match_auth_username = ast_true(v->value);
23718 } else if (!strcasecmp(v->name, "srvlookup")) {
23719 global_srvlookup = ast_true(v->value);
23720 } else if (!strcasecmp(v->name, "pedantic")) {
23721 pedanticsipchecking = ast_true(v->value);
23722 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
23723 max_expiry = atoi(v->value);
23724 if (max_expiry < 1)
23725 max_expiry = DEFAULT_MAX_EXPIRY;
23726 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
23727 min_expiry = atoi(v->value);
23728 if (min_expiry < 1)
23729 min_expiry = DEFAULT_MIN_EXPIRY;
23730 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
23731 default_expiry = atoi(v->value);
23732 if (default_expiry < 1)
23733 default_expiry = DEFAULT_DEFAULT_EXPIRY;
23734 } else if (!strcasecmp(v->name, "sipdebug")) {
23735 if (ast_true(v->value))
23736 sipdebug |= sip_debug_config;
23737 } else if (!strcasecmp(v->name, "dumphistory")) {
23738 dumphistory = ast_true(v->value);
23739 } else if (!strcasecmp(v->name, "recordhistory")) {
23740 recordhistory = ast_true(v->value);
23741 } else if (!strcasecmp(v->name, "registertimeout")) {
23742 global_reg_timeout = atoi(v->value);
23743 if (global_reg_timeout < 1)
23744 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
23745 } else if (!strcasecmp(v->name, "registerattempts")) {
23746 global_regattempts_max = atoi(v->value);
23747 } else if (!strcasecmp(v->name, "stunaddr")) {
23748 stunaddr.sin_port = htons(3478);
23749 if (ast_parse_arg(v->value, PARSE_INADDR, &stunaddr))
23750 ast_log(LOG_WARNING, "Invalid STUN server address: %s\n", v->value);
23751 externexpire = time(NULL);
23752 } else if (!strcasecmp(v->name, "bindaddr") || !strcasecmp(v->name, "udpbindaddr")) {
23753 if (ast_parse_arg(v->value, PARSE_INADDR, &bindaddr))
23754 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
23755 } else if (!strcasecmp(v->name, "localnet")) {
23756 struct ast_ha *na;
23757 int ha_error = 0;
23758
23759 if (!(na = ast_append_ha("d", v->value, localaddr, &ha_error)))
23760 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
23761 else
23762 localaddr = na;
23763 if (ha_error)
23764 ast_log(LOG_ERROR, "Bad localnet configuration value line %d : %s\n", v->lineno, v->value);
23765 } else if (!strcasecmp(v->name, "externip")) {
23766 if (ast_parse_arg(v->value, PARSE_INADDR, &externip))
23767 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
23768 externexpire = 0;
23769
23770 if (!externip.sin_port)
23771 externip.sin_port = bindaddr.sin_port;
23772 } else if (!strcasecmp(v->name, "externhost")) {
23773 ast_copy_string(externhost, v->value, sizeof(externhost));
23774 if (ast_parse_arg(externhost, PARSE_INADDR, &externip))
23775 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
23776 externexpire = time(NULL);
23777
23778 if (!externip.sin_port)
23779 externip.sin_port = bindaddr.sin_port;
23780 } else if (!strcasecmp(v->name, "externrefresh")) {
23781 if (sscanf(v->value, "%30d", &externrefresh) != 1) {
23782 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
23783 externrefresh = 10;
23784 }
23785 } else if (!strcasecmp(v->name, "allow")) {
23786 int error = ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, TRUE);
23787 if (error)
23788 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
23789 } else if (!strcasecmp(v->name, "disallow")) {
23790 int error = ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, FALSE);
23791 if (error)
23792 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
23793 } else if (!strcasecmp(v->name, "autoframing")) {
23794 global_autoframing = ast_true(v->value);
23795 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
23796 allow_external_domains = ast_true(v->value);
23797 } else if (!strcasecmp(v->name, "autodomain")) {
23798 auto_sip_domains = ast_true(v->value);
23799 } else if (!strcasecmp(v->name, "domain")) {
23800 char *domain = ast_strdupa(v->value);
23801 char *cntx = strchr(domain, ',');
23802
23803 if (cntx)
23804 *cntx++ = '\0';
23805
23806 if (ast_strlen_zero(cntx))
23807 ast_debug(1, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
23808 if (ast_strlen_zero(domain))
23809 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
23810 else
23811 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, cntx ? ast_strip(cntx) : "");
23812 } else if (!strcasecmp(v->name, "register")) {
23813 if (sip_register(v->value, v->lineno) == 0)
23814 registry_count++;
23815 } else if (!strcasecmp(v->name, "tos_sip")) {
23816 if (ast_str2tos(v->value, &global_tos_sip))
23817 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, refer to QoS documentation\n", v->lineno);
23818 } else if (!strcasecmp(v->name, "tos_audio")) {
23819 if (ast_str2tos(v->value, &global_tos_audio))
23820 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
23821 } else if (!strcasecmp(v->name, "tos_video")) {
23822 if (ast_str2tos(v->value, &global_tos_video))
23823 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, refer to QoS documentation\n", v->lineno);
23824 } else if (!strcasecmp(v->name, "tos_text")) {
23825 if (ast_str2tos(v->value, &global_tos_text))
23826 ast_log(LOG_WARNING, "Invalid tos_text value at line %d, refer to QoS documentation\n", v->lineno);
23827 } else if (!strcasecmp(v->name, "cos_sip")) {
23828 if (ast_str2cos(v->value, &global_cos_sip))
23829 ast_log(LOG_WARNING, "Invalid cos_sip value at line %d, refer to QoS documentation\n", v->lineno);
23830 } else if (!strcasecmp(v->name, "cos_audio")) {
23831 if (ast_str2cos(v->value, &global_cos_audio))
23832 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
23833 } else if (!strcasecmp(v->name, "cos_video")) {
23834 if (ast_str2cos(v->value, &global_cos_video))
23835 ast_log(LOG_WARNING, "Invalid cos_video value at line %d, refer to QoS documentation\n", v->lineno);
23836 } else if (!strcasecmp(v->name, "cos_text")) {
23837 if (ast_str2cos(v->value, &global_cos_text))
23838 ast_log(LOG_WARNING, "Invalid cos_text value at line %d, refer to QoS documentation\n", v->lineno);
23839 } else if (!strcasecmp(v->name, "bindport")) {
23840 int i;
23841 if (sscanf(v->value, "%5d", &i) == 1) {
23842 bindaddr.sin_port = htons(i);
23843 } else {
23844 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
23845 }
23846 } else if (!strcasecmp(v->name, "hash_user")) {
23847 int i;
23848 if (sscanf(v->value, "%30d", &i) == 1 && i > 2) {
23849 hash_user_size = i;
23850 } else {
23851 ast_log(LOG_WARNING, "Invalid hash_user size '%s' at line %d of %s -- should be much larger than 2\n", v->value, v->lineno, config);
23852 }
23853 } else if (!strcasecmp(v->name, "hash_peer")) {
23854 int i;
23855 if (sscanf(v->value, "%30d", &i) == 1 && i > 2) {
23856 hash_peer_size = i;
23857 } else {
23858 ast_log(LOG_WARNING, "Invalid hash_peer size '%s' at line %d of %s -- should be much larger than 2\n", v->value, v->lineno, config);
23859 }
23860 } else if (!strcasecmp(v->name, "hash_dialog")) {
23861 int i;
23862 if (sscanf(v->value, "%30d", &i) == 1 && i > 2) {
23863 hash_dialog_size = i;
23864 } else {
23865 ast_log(LOG_WARNING, "Invalid hash_dialog size '%s' at line %d of %s -- should be much larger than 2\n", v->value, v->lineno, config);
23866 }
23867 } else if (!strcasecmp(v->name, "qualify")) {
23868 if (!strcasecmp(v->value, "no")) {
23869 default_qualify = 0;
23870 } else if (!strcasecmp(v->value, "yes")) {
23871 default_qualify = DEFAULT_MAXMS;
23872 } else if (sscanf(v->value, "%30d", &default_qualify) != 1) {
23873 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
23874 default_qualify = 0;
23875 }
23876 } else if (!strcasecmp(v->name, "qualifyfreq")) {
23877 int i;
23878 if (sscanf(v->value, "%30d", &i) == 1)
23879 global_qualifyfreq = i * 1000;
23880 else {
23881 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
23882 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
23883 }
23884 } else if (!strcasecmp(v->name, "callevents")) {
23885 global_callevents = ast_true(v->value);
23886 } else if (!strcasecmp(v->name, "authfailureevents")) {
23887 global_authfailureevents = ast_true(v->value);
23888 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
23889 default_maxcallbitrate = atoi(v->value);
23890 if (default_maxcallbitrate < 0)
23891 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
23892 } else if (!strcasecmp(v->name, "matchexterniplocally")) {
23893 global_matchexterniplocally = ast_true(v->value);
23894 } else if (!strcasecmp(v->name, "session-timers")) {
23895 int i = (int) str2stmode(v->value);
23896 if (i < 0) {
23897 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
23898 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
23899 } else {
23900 global_st_mode = i;
23901 }
23902 } else if (!strcasecmp(v->name, "session-expires")) {
23903 if (sscanf(v->value, "%30d", &global_max_se) != 1) {
23904 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
23905 global_max_se = DEFAULT_MAX_SE;
23906 }
23907 } else if (!strcasecmp(v->name, "session-minse")) {
23908 if (sscanf(v->value, "%30d", &global_min_se) != 1) {
23909 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
23910 global_min_se = DEFAULT_MIN_SE;
23911 }
23912 if (global_min_se < 90) {
23913 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);
23914 global_min_se = DEFAULT_MIN_SE;
23915 }
23916 } else if (!strcasecmp(v->name, "session-refresher")) {
23917 int i = (int) str2strefresher(v->value);
23918 if (i < 0) {
23919 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
23920 global_st_refresher = SESSION_TIMER_REFRESHER_UAS;
23921 } else {
23922 global_st_refresher = i;
23923 }
23924 } else if (!strcasecmp(v->name, "shrinkcallerid")) {
23925 if (ast_true(v->value)) {
23926 global_shrinkcallerid = 1;
23927 } else if (ast_false(v->value)) {
23928 global_shrinkcallerid = 0;
23929 } else {
23930 ast_log(LOG_WARNING, "shrinkcallerid value %s is not valid at line %d.\n", v->value, v->lineno);
23931 }
23932 }
23933 }
23934
23935 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
23936 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
23937 allow_external_domains = 1;
23938 }
23939
23940
23941 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
23942
23943 if (!strcasecmp(v->name, "auth"))
23944 authl = add_realm_authentication(authl, v->value, v->lineno);
23945 }
23946
23947 if (ucfg) {
23948 struct ast_variable *gen;
23949 int genhassip, genregistersip;
23950 const char *hassip, *registersip;
23951
23952 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
23953 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
23954 gen = ast_variable_browse(ucfg, "general");
23955 cat = ast_category_browse(ucfg, NULL);
23956 while (cat) {
23957 if (strcasecmp(cat, "general")) {
23958 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
23959 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
23960 if (ast_true(hassip) || (!hassip && genhassip)) {
23961 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0, 0);
23962 if (peer) {
23963
23964 peer->type = SIP_TYPE_USER | SIP_TYPE_PEER;
23965 ao2_t_link(peers, peer, "link peer into peer table");
23966 if ((peer->type & SIP_TYPE_PEER) && peer->addr.sin_addr.s_addr) {
23967 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
23968 }
23969
23970 unref_peer(peer, "unref_peer: from reload_config");
23971 peer_count++;
23972 }
23973 }
23974 if (ast_true(registersip) || (!registersip && genregistersip)) {
23975 char tmp[256];
23976 const char *host = ast_variable_retrieve(ucfg, cat, "host");
23977 const char *username = ast_variable_retrieve(ucfg, cat, "username");
23978 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
23979 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
23980 const char *authuser = ast_variable_retrieve(ucfg, cat, "authuser");
23981 if (!host)
23982 host = ast_variable_retrieve(ucfg, "general", "host");
23983 if (!username)
23984 username = ast_variable_retrieve(ucfg, "general", "username");
23985 if (!secret)
23986 secret = ast_variable_retrieve(ucfg, "general", "secret");
23987 if (!contact)
23988 contact = "s";
23989 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
23990 if (!ast_strlen_zero(secret)) {
23991 if (!ast_strlen_zero(authuser)) {
23992 snprintf(tmp, sizeof(tmp), "%s:%s:%s@%s/%s", username, secret, authuser, host, contact);
23993 } else {
23994 snprintf(tmp, sizeof(tmp), "%s:%s@%s/%s", username, secret, host, contact);
23995 }
23996 } else if (!ast_strlen_zero(authuser)) {
23997 snprintf(tmp, sizeof(tmp), "%s::%s@%s/%s", username, authuser, host, contact);
23998 } else {
23999 snprintf(tmp, sizeof(tmp), "%s@%s/%s", username, host, contact);
24000 }
24001 if (sip_register(tmp, 0) == 0)
24002 registry_count++;
24003 }
24004 }
24005 }
24006 cat = ast_category_browse(ucfg, cat);
24007 }
24008 ast_config_destroy(ucfg);
24009 }
24010
24011
24012
24013 cat = NULL;
24014 while ( (cat = ast_category_browse(cfg, cat)) ) {
24015 const char *utype;
24016 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
24017 continue;
24018 utype = ast_variable_retrieve(cfg, cat, "type");
24019 if (!utype) {
24020 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
24021 continue;
24022 } else {
24023 if (!strcasecmp(utype, "user")) {
24024 ;
24025 } else if (!strcasecmp(utype, "friend")) {
24026 ;
24027 } else if (!strcasecmp(utype, "peer")) {
24028 ;
24029 } else {
24030 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
24031 continue;
24032 }
24033 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0, 0);
24034 if (peer) {
24035 ao2_t_link(peers, peer, "link peer into peers table");
24036 if ((peer->type & SIP_TYPE_PEER) && peer->addr.sin_addr.s_addr) {
24037 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
24038 }
24039 unref_peer(peer, "unref the result of the build_peer call. Now, the links from the tables are the only ones left.");
24040 peer_count++;
24041 }
24042 }
24043 }
24044
24045
24046 bindaddr.sin_family = AF_INET;
24047 internip = bindaddr;
24048 if (ast_find_ourip(&internip.sin_addr, bindaddr)) {
24049 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
24050 ast_config_destroy(cfg);
24051 return 0;
24052 }
24053 ast_mutex_lock(&netlock);
24054 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
24055 close(sipsock);
24056 sipsock = -1;
24057 }
24058 if (sipsock < 0) {
24059 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
24060 if (sipsock < 0) {
24061 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
24062 ast_config_destroy(cfg);
24063 return -1;
24064 } else {
24065
24066 const int reuseFlag = 1;
24067
24068 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
24069 (const char*)&reuseFlag,
24070 sizeof reuseFlag);
24071
24072 ast_enable_packet_fragmentation(sipsock);
24073
24074 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
24075 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
24076 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
24077 strerror(errno));
24078 close(sipsock);
24079 sipsock = -1;
24080 } else {
24081 ast_verb(2, "SIP Listening on %s:%d\n",
24082 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
24083 ast_netsock_set_qos(sipsock, global_tos_sip, global_cos_sip, "SIP");
24084 }
24085 }
24086 }
24087 if (stunaddr.sin_addr.s_addr != 0) {
24088 ast_debug(1, "stun to %s:%d\n",
24089 ast_inet_ntoa(stunaddr.sin_addr) , ntohs(stunaddr.sin_port));
24090 ast_stun_request(sipsock, &stunaddr,
24091 NULL, &externip);
24092 ast_debug(1, "STUN sees us at %s:%d\n",
24093 ast_inet_ntoa(externip.sin_addr) , ntohs(externip.sin_port));
24094 }
24095 ast_mutex_unlock(&netlock);
24096
24097
24098 ast_tcptls_server_start(&sip_tcp_desc);
24099
24100
24101 memcpy(sip_tls_desc.tls_cfg, &default_tls_cfg, sizeof(default_tls_cfg));
24102
24103 if (ast_ssl_setup(sip_tls_desc.tls_cfg))
24104 ast_tcptls_server_start(&sip_tls_desc);
24105 else if (sip_tls_desc.tls_cfg->enabled) {
24106 sip_tls_desc.tls_cfg = NULL;
24107 ast_log(LOG_WARNING, "SIP TLS server did not load because of errors.\n");
24108 }
24109
24110
24111
24112
24113
24114
24115
24116 if (auto_sip_domains) {
24117 char temp[MAXHOSTNAMELEN];
24118
24119
24120 if (bindaddr.sin_addr.s_addr) {
24121 add_sip_domain(ast_inet_ntoa(bindaddr.sin_addr), SIP_DOMAIN_AUTO, NULL);
24122 } else if (internip.sin_addr.s_addr) {
24123
24124 add_sip_domain(ast_inet_ntoa(internip.sin_addr), SIP_DOMAIN_AUTO, NULL);
24125 } else {
24126 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
24127 }
24128
24129
24130 if (sip_tcp_desc.local_address.sin_addr.s_addr && !inaddrcmp(&bindaddr, &sip_tcp_desc.local_address))
24131 add_sip_domain(ast_inet_ntoa(sip_tcp_desc.local_address.sin_addr), SIP_DOMAIN_AUTO, NULL);
24132
24133
24134 if (sip_tls_desc.local_address.sin_addr.s_addr && !inaddrcmp(&bindaddr, &sip_tls_desc.local_address) && inaddrcmp(&sip_tcp_desc.local_address, &sip_tls_desc.local_address))
24135 add_sip_domain(ast_inet_ntoa(sip_tls_desc.local_address.sin_addr), SIP_DOMAIN_AUTO, NULL);
24136
24137
24138 if (externip.sin_addr.s_addr)
24139 add_sip_domain(ast_inet_ntoa(externip.sin_addr), SIP_DOMAIN_AUTO, NULL);
24140
24141
24142 if (!ast_strlen_zero(externhost))
24143 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
24144
24145
24146 if (!gethostname(temp, sizeof(temp)))
24147 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
24148 }
24149
24150
24151 ast_config_destroy(cfg);
24152
24153
24154 if (notify_types)
24155 ast_config_destroy(notify_types);
24156 notify_types = ast_config_load(notify_config, config_flags);
24157
24158
24159 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);
24160 run_end = time(0);
24161 ast_debug(4, "SIP reload_config done...Runtime= %d sec\n", (int)(run_end-run_start));
24162
24163 return 0;
24164 }
24165
24166 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
24167 {
24168 struct sip_pvt *p;
24169 struct ast_udptl *udptl = NULL;
24170
24171 p = chan->tech_pvt;
24172 if (!p)
24173 return NULL;
24174
24175 sip_pvt_lock(p);
24176 if (p->udptl && ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
24177 udptl = p->udptl;
24178 sip_pvt_unlock(p);
24179 return udptl;
24180 }
24181
24182 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
24183 {
24184 struct sip_pvt *p;
24185
24186 p = chan->tech_pvt;
24187 if (!p)
24188 return -1;
24189 sip_pvt_lock(p);
24190 if (udptl)
24191 ast_udptl_get_peer(udptl, &p->udptlredirip);
24192 else
24193 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
24194 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
24195 if (!p->pendinginvite) {
24196 ast_debug(3, "Sending reinvite on SIP '%s' - It's UDPTL soon redirected to IP %s:%d\n", p->callid, ast_inet_ntoa(udptl ? p->udptlredirip.sin_addr : p->ourip.sin_addr), udptl ? ntohs(p->udptlredirip.sin_port) : 0);
24197 transmit_reinvite_with_sdp(p, TRUE, FALSE);
24198 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
24199 ast_debug(3, "Deferring reinvite on SIP '%s' - It's UDPTL will be redirected to IP %s:%d\n", p->callid, ast_inet_ntoa(udptl ? p->udptlredirip.sin_addr : p->ourip.sin_addr), udptl ? ntohs(p->udptlredirip.sin_port) : 0);
24200 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
24201 }
24202 }
24203
24204 p->lastrtprx = p->lastrtptx = time(NULL);
24205 sip_pvt_unlock(p);
24206 return 0;
24207 }
24208
24209
24210 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
24211 {
24212 struct sip_pvt *p = NULL;
24213 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
24214
24215 if (!(p = chan->tech_pvt))
24216 return AST_RTP_GET_FAILED;
24217
24218 sip_pvt_lock(p);
24219 if (!(p->rtp)) {
24220 sip_pvt_unlock(p);
24221 return AST_RTP_GET_FAILED;
24222 }
24223
24224 *rtp = p->rtp;
24225
24226 if (ast_rtp_getnat(*rtp) && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT))
24227 res = AST_RTP_TRY_PARTIAL;
24228 else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
24229 res = AST_RTP_TRY_NATIVE;
24230 else if (ast_test_flag(&global_jbconf, AST_JB_FORCED))
24231 res = AST_RTP_GET_FAILED;
24232
24233 sip_pvt_unlock(p);
24234
24235 return res;
24236 }
24237
24238
24239 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
24240 {
24241 struct sip_pvt *p = NULL;
24242 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
24243
24244 if (!(p = chan->tech_pvt))
24245 return AST_RTP_GET_FAILED;
24246
24247 sip_pvt_lock(p);
24248 if (!(p->vrtp)) {
24249 sip_pvt_unlock(p);
24250 return AST_RTP_GET_FAILED;
24251 }
24252
24253 *rtp = p->vrtp;
24254
24255 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
24256 res = AST_RTP_TRY_NATIVE;
24257
24258 sip_pvt_unlock(p);
24259
24260 return res;
24261 }
24262
24263
24264 static enum ast_rtp_get_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
24265 {
24266 struct sip_pvt *p = NULL;
24267 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
24268
24269 if (!(p = chan->tech_pvt))
24270 return AST_RTP_GET_FAILED;
24271
24272 sip_pvt_lock(p);
24273 if (!(p->trtp)) {
24274 sip_pvt_unlock(p);
24275 return AST_RTP_GET_FAILED;
24276 }
24277
24278 *rtp = p->trtp;
24279
24280 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
24281 res = AST_RTP_TRY_NATIVE;
24282
24283 sip_pvt_unlock(p);
24284
24285 return res;
24286 }
24287
24288
24289 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, struct ast_rtp *trtp, int codecs, int nat_active)
24290 {
24291 struct sip_pvt *p;
24292 int changed = 0;
24293
24294 p = chan->tech_pvt;
24295 if (!p)
24296 return -1;
24297
24298
24299 if (!ast_bridged_channel(chan) && !global_directrtpsetup)
24300 return 0;
24301
24302 sip_pvt_lock(p);
24303 if (p->alreadygone) {
24304
24305 sip_pvt_unlock(p);
24306 return 0;
24307 }
24308
24309
24310
24311
24312 if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
24313 sip_pvt_unlock(p);
24314 return 0;
24315 }
24316
24317 if (rtp) {
24318 changed |= ast_rtp_get_peer(rtp, &p->redirip);
24319 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
24320 memset(&p->redirip, 0, sizeof(p->redirip));
24321 changed = 1;
24322 }
24323 if (vrtp) {
24324 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
24325 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
24326 memset(&p->vredirip, 0, sizeof(p->vredirip));
24327 changed = 1;
24328 }
24329 if (trtp) {
24330 changed |= ast_rtp_get_peer(trtp, &p->tredirip);
24331 } else if (p->tredirip.sin_addr.s_addr || ntohs(p->tredirip.sin_port) != 0) {
24332 memset(&p->tredirip, 0, sizeof(p->tredirip));
24333 changed = 1;
24334 }
24335 if (codecs && (p->redircodecs != codecs)) {
24336 p->redircodecs = codecs;
24337 changed = 1;
24338 }
24339 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
24340 if (chan->_state != AST_STATE_UP) {
24341 if (p->do_history)
24342 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
24343 ast_debug(1, "Early remote bridge setting SIP '%s' - Sending media to %s\n", p->callid, ast_inet_ntoa(rtp ? p->redirip.sin_addr : p->ourip.sin_addr));
24344 } else if (!p->pendinginvite) {
24345 ast_debug(3, "Sending reinvite on SIP '%s' - It's audio soon redirected to IP %s\n", p->callid, ast_inet_ntoa(rtp ? p->redirip.sin_addr : p->ourip.sin_addr));
24346 transmit_reinvite_with_sdp(p, FALSE, FALSE);
24347 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
24348 ast_debug(3, "Deferring reinvite on SIP '%s' - It's audio will be redirected to IP %s\n", p->callid, ast_inet_ntoa(rtp ? p->redirip.sin_addr : p->ourip.sin_addr));
24349
24350 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
24351 }
24352 }
24353
24354 p->lastrtprx = p->lastrtptx = time(NULL);
24355 sip_pvt_unlock(p);
24356 return 0;
24357 }
24358
24359 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
24360 static char *descrip_dtmfmode = " SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
24361 static char *app_dtmfmode = "SIPDtmfMode";
24362
24363 static char *app_sipaddheader = "SIPAddHeader";
24364 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
24365
24366 static char *descrip_sipaddheader = ""
24367 " SIPAddHeader(Header: Content):\n"
24368 "Adds a header to a SIP call placed with DIAL.\n"
24369 "Remember to user the X-header if you are adding non-standard SIP\n"
24370 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
24371 "Adding the wrong headers may jeopardize the SIP dialog.\n"
24372 "Always returns 0\n";
24373
24374
24375
24376 static int sip_dtmfmode(struct ast_channel *chan, void *data)
24377 {
24378 struct sip_pvt *p;
24379 char *mode = data;
24380
24381 if (!data) {
24382 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
24383 return 0;
24384 }
24385 ast_channel_lock(chan);
24386 if (!IS_SIP_TECH(chan->tech)) {
24387 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
24388 ast_channel_unlock(chan);
24389 return 0;
24390 }
24391 p = chan->tech_pvt;
24392 if (!p) {
24393 ast_channel_unlock(chan);
24394 return 0;
24395 }
24396 sip_pvt_lock(p);
24397 if (!strcasecmp(mode, "info")) {
24398 ast_clear_flag(&p->flags[0], SIP_DTMF);
24399 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
24400 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
24401 } else if (!strcasecmp(mode, "shortinfo")) {
24402 ast_clear_flag(&p->flags[0], SIP_DTMF);
24403 ast_set_flag(&p->flags[0], SIP_DTMF_SHORTINFO);
24404 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
24405 } else if (!strcasecmp(mode, "rfc2833")) {
24406 ast_clear_flag(&p->flags[0], SIP_DTMF);
24407 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
24408 p->jointnoncodeccapability |= AST_RTP_DTMF;
24409 } else if (!strcasecmp(mode, "inband")) {
24410 ast_clear_flag(&p->flags[0], SIP_DTMF);
24411 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
24412 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
24413 } else
24414 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n", mode);
24415 if (p->rtp)
24416 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
24417 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
24418 if (!p->dsp) {
24419 p->dsp = ast_dsp_new();
24420 ast_dsp_set_features(p->dsp, DSP_FEATURE_DIGIT_DETECT);
24421 }
24422 } else {
24423 if (p->dsp) {
24424 ast_dsp_free(p->dsp);
24425 p->dsp = NULL;
24426 }
24427 }
24428 sip_pvt_unlock(p);
24429 ast_channel_unlock(chan);
24430 return 0;
24431 }
24432
24433
24434 static int sip_addheader(struct ast_channel *chan, void *data)
24435 {
24436 int no = 0;
24437 int ok = FALSE;
24438 char varbuf[30];
24439 char *inbuf = data, *subbuf;
24440
24441 if (ast_strlen_zero(inbuf)) {
24442 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
24443 return 0;
24444 }
24445 ast_channel_lock(chan);
24446
24447
24448 while (!ok && no <= 50) {
24449 no++;
24450 snprintf(varbuf, sizeof(varbuf), "__SIPADDHEADER%.2d", no);
24451
24452
24453 if ((pbx_builtin_getvar_helper(chan, (const char *) varbuf + 2) == (const char *) NULL)) {
24454 ok = TRUE;
24455 }
24456 }
24457 if (ok) {
24458 size_t len = strlen(inbuf);
24459 subbuf = alloca(len + 1);
24460 ast_get_encoded_str(inbuf, subbuf, len + 1);
24461 pbx_builtin_setvar_helper(chan, varbuf, subbuf);
24462 if (sipdebug) {
24463 ast_debug(1, "SIP Header added \"%s\" as %s\n", inbuf, varbuf);
24464 }
24465 } else {
24466 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
24467 }
24468 ast_channel_unlock(chan);
24469 return 0;
24470 }
24471
24472
24473
24474
24475
24476
24477
24478 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
24479 {
24480 char *cdest;
24481 char *extension, *host, *port;
24482 char tmp[80];
24483
24484 cdest = ast_strdupa(dest);
24485
24486 extension = strsep(&cdest, "@");
24487 host = strsep(&cdest, ":");
24488 port = strsep(&cdest, ":");
24489 if (ast_strlen_zero(extension)) {
24490 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
24491 return 0;
24492 }
24493
24494
24495 if (!host) {
24496 char *localtmp;
24497
24498 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
24499 if (ast_strlen_zero(tmp)) {
24500 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
24501 return 0;
24502 }
24503 if ( ( (localtmp = strcasestr(tmp, "sip:")) || (localtmp = strcasestr(tmp, "sips:")) )
24504 && (localtmp = strchr(localtmp, '@'))) {
24505 char lhost[80], lport[80];
24506
24507 memset(lhost, 0, sizeof(lhost));
24508 memset(lport, 0, sizeof(lport));
24509 localtmp++;
24510
24511 sscanf(localtmp, "%80[^<>:; ]:%80[^<>:; ]", lhost, lport);
24512 if (ast_strlen_zero(lhost)) {
24513 ast_log(LOG_ERROR, "Can't find the host address\n");
24514 return 0;
24515 }
24516 host = ast_strdupa(lhost);
24517 if (!ast_strlen_zero(lport)) {
24518 port = ast_strdupa(lport);
24519 }
24520 }
24521 }
24522
24523 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
24524 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
24525
24526 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
24527 sip_alreadygone(p);
24528
24529 return 0;
24530 }
24531
24532
24533 static int sip_get_codec(struct ast_channel *chan)
24534 {
24535 struct sip_pvt *p = chan->tech_pvt;
24536 return p->jointcapability ? p->jointcapability : p->capability;
24537 }
24538
24539
24540
24541
24542
24543 static void sip_poke_all_peers(void)
24544 {
24545 int ms = 0;
24546 struct ao2_iterator i;
24547 struct sip_peer *peer;
24548
24549 if (!speerobjs)
24550 return;
24551
24552 i = ao2_iterator_init(peers, 0);
24553 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
24554 ao2_lock(peer);
24555 ms += 100;
24556 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, ms, sip_poke_peer_s, peer,
24557 unref_peer(_data, "removing poke peer ref"),
24558 unref_peer(peer, "removing poke peer ref"),
24559 ref_peer(peer, "adding poke peer ref"));
24560 ao2_unlock(peer);
24561 unref_peer(peer, "toss iterator peer ptr");
24562 }
24563 ao2_iterator_destroy(&i);
24564 }
24565
24566
24567 static void sip_send_all_registers(void)
24568 {
24569 int ms;
24570 int regspacing;
24571 if (!regobjs)
24572 return;
24573 regspacing = default_expiry * 1000/regobjs;
24574 if (regspacing > 100)
24575 regspacing = 100;
24576 ms = regspacing;
24577 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
24578 ASTOBJ_WRLOCK(iterator);
24579 ms += regspacing;
24580 AST_SCHED_REPLACE_UNREF(iterator->expire, sched, ms, sip_reregister, iterator,
24581 registry_unref(_data, "REPLACE sched del decs the refcount"),
24582 registry_unref(iterator, "REPLACE sched add failure decs the refcount"),
24583 registry_addref(iterator, "REPLACE sched add incs the refcount"));
24584 ASTOBJ_UNLOCK(iterator);
24585 } while (0)
24586 );
24587 }
24588
24589
24590 static int sip_do_reload(enum channelreloadreason reason)
24591 {
24592 time_t start_poke, end_poke;
24593
24594 reload_config(reason);
24595 ast_sched_dump(sched);
24596
24597 start_poke = time(0);
24598
24599 ao2_t_callback(peers, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, peer_is_marked, 0,
24600 "callback to remove marked peers");
24601
24602 ast_debug(4, "--------------- Done destroying pruned peers\n");
24603
24604
24605 sip_poke_all_peers();
24606
24607
24608 sip_send_all_registers();
24609 end_poke = time(0);
24610
24611 ast_debug(4, "do_reload finished. peer poke/prune reg contact time = %d sec.\n", (int)(end_poke-start_poke));
24612
24613 ast_debug(4, "--------------- SIP reload done\n");
24614
24615 return 0;
24616 }
24617
24618
24619 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
24620 {
24621
24622 switch (cmd) {
24623 case CLI_INIT:
24624 e->command = "sip reload";
24625 e->usage =
24626 "Usage: sip reload\n"
24627 " Reloads SIP configuration from sip.conf\n";
24628 return NULL;
24629 case CLI_GENERATE:
24630 return NULL;
24631 }
24632
24633 ast_mutex_lock(&sip_reload_lock);
24634 if (sip_reloading)
24635 ast_verbose("Previous SIP reload not yet done\n");
24636 else {
24637 sip_reloading = TRUE;
24638 sip_reloadreason = (a && a->fd) ? CHANNEL_CLI_RELOAD : CHANNEL_MODULE_RELOAD;
24639 }
24640 ast_mutex_unlock(&sip_reload_lock);
24641 restart_monitor();
24642
24643 return CLI_SUCCESS;
24644 }
24645
24646
24647 static int reload(void)
24648 {
24649 if (sip_reload(0, 0, NULL))
24650 return 0;
24651 return 1;
24652 }
24653
24654 static struct ast_cli_entry cli_sip_do_history_deprecated = AST_CLI_DEFINE(sip_do_history_deprecated, "Enable/Disable SIP history");
24655
24656 static struct ast_cli_entry cli_sip[] = {
24657 AST_CLI_DEFINE(sip_show_channels, "List active SIP channels or subscriptions"),
24658 AST_CLI_DEFINE(sip_show_channelstats, "List statistics for active SIP channels"),
24659 AST_CLI_DEFINE(sip_show_domains, "List our local SIP domains"),
24660 AST_CLI_DEFINE(sip_show_inuse, "List all inuse/limits"),
24661 AST_CLI_DEFINE(sip_show_objects, "List all SIP object allocations"),
24662 AST_CLI_DEFINE(sip_show_peers, "List defined SIP peers"),
24663 AST_CLI_DEFINE(sip_show_registry, "List SIP registration status"),
24664 AST_CLI_DEFINE(sip_unregister, "Unregister (force expiration) a SIP peer from the registry"),
24665 AST_CLI_DEFINE(sip_show_settings, "Show SIP global settings"),
24666 AST_CLI_DEFINE(sip_cli_notify, "Send a notify packet to a SIP peer"),
24667 AST_CLI_DEFINE(sip_show_channel, "Show detailed SIP channel info"),
24668 AST_CLI_DEFINE(sip_show_history, "Show SIP dialog history"),
24669 AST_CLI_DEFINE(sip_show_peer, "Show details on specific SIP peer"),
24670 AST_CLI_DEFINE(sip_show_users, "List defined SIP users"),
24671 AST_CLI_DEFINE(sip_show_user, "Show details on specific SIP user"),
24672 AST_CLI_DEFINE(sip_qualify_peer, "Send an OPTIONS packet to a peer"),
24673 AST_CLI_DEFINE(sip_show_sched, "Present a report on the status of the sched queue"),
24674 AST_CLI_DEFINE(sip_prune_realtime, "Prune cached Realtime users/peers"),
24675 AST_CLI_DEFINE(sip_do_debug, "Enable/Disable SIP debugging"),
24676 AST_CLI_DEFINE(sip_set_history, "Enable/Disable SIP history", .deprecate_cmd = &cli_sip_do_history_deprecated),
24677 AST_CLI_DEFINE(sip_reload, "Reload SIP configuration"),
24678 AST_CLI_DEFINE(sip_show_tcp, "List TCP Connections")
24679 };
24680
24681
24682 static int load_module(void)
24683 {
24684 ast_verbose("SIP channel loading...\n");
24685
24686
24687 peers = ao2_t_container_alloc(hash_peer_size, peer_hash_cb, peer_cmp_cb, "allocate peers");
24688 peers_by_ip = ao2_t_container_alloc(hash_peer_size, peer_iphash_cb, peer_ipcmp_cb, "allocate peers_by_ip");
24689 dialogs = ao2_t_container_alloc(hash_dialog_size, dialog_hash_cb, dialog_cmp_cb, "allocate dialogs");
24690 threadt = ao2_t_container_alloc(hash_dialog_size, threadt_hash_cb, threadt_cmp_cb, "allocate threadt table");
24691
24692 ASTOBJ_CONTAINER_INIT(®l);
24693
24694 if (!(sched = sched_context_create())) {
24695 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
24696 return AST_MODULE_LOAD_FAILURE;
24697 }
24698
24699 if (!(io = io_context_create())) {
24700 ast_log(LOG_ERROR, "Unable to create I/O context\n");
24701 sched_context_destroy(sched);
24702 return AST_MODULE_LOAD_FAILURE;
24703 }
24704
24705 sip_reloadreason = CHANNEL_MODULE_LOAD;
24706
24707 if(reload_config(sip_reloadreason))
24708 return AST_MODULE_LOAD_DECLINE;
24709
24710
24711
24712
24713
24714 memcpy(&sip_tech_info, &sip_tech, sizeof(sip_tech));
24715 memset((void *) &sip_tech_info.send_digit_begin, 0, sizeof(sip_tech_info.send_digit_begin));
24716
24717
24718 if (ast_channel_register(&sip_tech)) {
24719 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
24720 io_context_destroy(io);
24721 sched_context_destroy(sched);
24722 return AST_MODULE_LOAD_FAILURE;
24723 }
24724
24725
24726 ast_cli_register_multiple(cli_sip, sizeof(cli_sip)/ sizeof(struct ast_cli_entry));
24727
24728
24729 ast_rtp_proto_register(&sip_rtp);
24730
24731
24732 ast_udptl_proto_register(&sip_udptl);
24733
24734
24735 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
24736 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
24737
24738
24739 ast_custom_function_register(&sip_header_function);
24740 ast_custom_function_register(&sippeer_function);
24741 ast_custom_function_register(&sipchaninfo_function);
24742 ast_custom_function_register(&checksipdomain_function);
24743
24744
24745 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peers,
24746 "List SIP peers (text format)", mandescr_show_peers);
24747 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peer,
24748 "Show SIP peer (text format)", mandescr_show_peer);
24749 ast_manager_register2("SIPqualifypeer", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_qualify_peer,
24750 "Show SIP peer (text format)", mandescr_show_peer);
24751 ast_manager_register2("SIPshowregistry", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_show_registry,
24752 "Show SIP registrations (text format)", mandescr_show_registry);
24753 ast_manager_register2("SIPnotify", EVENT_FLAG_SYSTEM, manager_sipnotify,
24754 "Send a SIP notify", mandescr_sipnotify);
24755 sip_poke_all_peers();
24756 sip_send_all_registers();
24757
24758
24759 restart_monitor();
24760
24761 ast_realtime_require_field(ast_check_realtime("sipregs") ? "sipregs" : "sippeers",
24762 "name", RQ_CHAR, 10,
24763 "ipaddr", RQ_CHAR, 15,
24764 "port", RQ_UINTEGER2, 5,
24765 "regseconds", RQ_INTEGER4, 11,
24766 "defaultuser", RQ_CHAR, 10,
24767 "fullcontact", RQ_CHAR, 35,
24768 "regserver", RQ_CHAR, 20,
24769 "useragent", RQ_CHAR, 20,
24770 "lastms", RQ_INTEGER4, 11,
24771 SENTINEL);
24772
24773 return AST_MODULE_LOAD_SUCCESS;
24774 }
24775
24776
24777 static int unload_module(void)
24778 {
24779 struct sip_pvt *p;
24780 struct sip_threadinfo *th;
24781 struct ast_context *con;
24782 struct ao2_iterator i;
24783
24784 ast_sched_dump(sched);
24785
24786
24787 ast_channel_unregister(&sip_tech);
24788
24789
24790 ast_custom_function_unregister(&sipchaninfo_function);
24791 ast_custom_function_unregister(&sippeer_function);
24792 ast_custom_function_unregister(&sip_header_function);
24793 ast_custom_function_unregister(&checksipdomain_function);
24794
24795
24796 ast_unregister_application(app_dtmfmode);
24797 ast_unregister_application(app_sipaddheader);
24798
24799
24800 ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
24801
24802
24803 ast_rtp_proto_unregister(&sip_rtp);
24804
24805
24806 ast_udptl_proto_unregister(&sip_udptl);
24807
24808
24809 ast_manager_unregister("SIPpeers");
24810 ast_manager_unregister("SIPshowpeer");
24811 ast_manager_unregister("SIPqualifypeer");
24812 ast_manager_unregister("SIPshowregistry");
24813 ast_manager_unregister("SIPnotify");
24814
24815
24816 if (sip_tcp_desc.master)
24817 ast_tcptls_server_stop(&sip_tcp_desc);
24818 if (sip_tls_desc.master)
24819 ast_tcptls_server_stop(&sip_tls_desc);
24820
24821
24822 i = ao2_iterator_init(threadt, 0);
24823 while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
24824 pthread_t thread = th->threadid;
24825 th->stop = 1;
24826 pthread_kill(thread, SIGURG);
24827 pthread_join(thread, NULL);
24828 ao2_t_ref(th, -1, "decrement ref from iterator");
24829 }
24830 ao2_iterator_destroy(&i);
24831
24832
24833 i = ao2_iterator_init(dialogs, 0);
24834 while ((p = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
24835 if (p->owner)
24836 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
24837 ao2_t_ref(p, -1, "toss dialog ptr from iterator_next");
24838 }
24839 ao2_iterator_destroy(&i);
24840
24841 ast_mutex_lock(&monlock);
24842 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
24843 pthread_cancel(monitor_thread);
24844 pthread_kill(monitor_thread, SIGURG);
24845 pthread_join(monitor_thread, NULL);
24846 }
24847 monitor_thread = AST_PTHREADT_STOP;
24848 ast_mutex_unlock(&monlock);
24849
24850
24851 i = ao2_iterator_init(dialogs, 0);
24852 while ((p = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
24853 dialog_unlink_all(p, TRUE, TRUE);
24854 ao2_t_ref(p, -1, "throw away iterator result");
24855 }
24856 ao2_iterator_destroy(&i);
24857
24858
24859 ast_free_ha(localaddr);
24860
24861 clear_realm_authentication(authl);
24862
24863
24864 if (default_tls_cfg.certfile)
24865 ast_free(default_tls_cfg.certfile);
24866 if (default_tls_cfg.cipher)
24867 ast_free(default_tls_cfg.cipher);
24868 if (default_tls_cfg.cafile)
24869 ast_free(default_tls_cfg.cafile);
24870 if (default_tls_cfg.capath)
24871 ast_free(default_tls_cfg.capath);
24872
24873 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
24874 ASTOBJ_CONTAINER_DESTROY(®l);
24875
24876 ao2_t_ref(peers, -1, "unref the peers table");
24877 ao2_t_ref(peers_by_ip, -1, "unref the peers_by_ip table");
24878 ao2_t_ref(dialogs, -1, "unref the dialogs table");
24879 ao2_t_ref(threadt, -1, "unref the thread table");
24880
24881 clear_sip_domains();
24882 ast_free_ha(global_contact_ha);
24883 close(sipsock);
24884 sched_context_destroy(sched);
24885 con = ast_context_find(used_context);
24886 if (con)
24887 ast_context_destroy(con, "SIP");
24888 ast_unload_realtime("sipregs");
24889 ast_unload_realtime("sippeers");
24890
24891 return 0;
24892 }
24893
24894 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)",
24895 .load = load_module,
24896 .unload = unload_module,
24897 .reload = reload,
24898 );