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 #include "asterisk.h"
00130
00131 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 202926 $")
00132
00133 #include <ctype.h>
00134 #include <sys/ioctl.h>
00135 #include <fcntl.h>
00136 #include <signal.h>
00137 #include <sys/signal.h>
00138 #include <regex.h>
00139
00140 #include "asterisk/network.h"
00141 #include "asterisk/paths.h"
00142
00143 #include "asterisk/lock.h"
00144 #include "asterisk/channel.h"
00145 #include "asterisk/config.h"
00146 #include "asterisk/module.h"
00147 #include "asterisk/pbx.h"
00148 #include "asterisk/sched.h"
00149 #include "asterisk/io.h"
00150 #include "asterisk/rtp.h"
00151 #include "asterisk/udptl.h"
00152 #include "asterisk/acl.h"
00153 #include "asterisk/manager.h"
00154 #include "asterisk/callerid.h"
00155 #include "asterisk/cli.h"
00156 #include "asterisk/app.h"
00157 #include "asterisk/musiconhold.h"
00158 #include "asterisk/dsp.h"
00159 #include "asterisk/features.h"
00160 #include "asterisk/srv.h"
00161 #include "asterisk/astdb.h"
00162 #include "asterisk/causes.h"
00163 #include "asterisk/utils.h"
00164 #include "asterisk/file.h"
00165 #include "asterisk/astobj.h"
00166 #include "asterisk/dnsmgr.h"
00167 #include "asterisk/devicestate.h"
00168 #include "asterisk/linkedlists.h"
00169 #include "asterisk/stringfields.h"
00170 #include "asterisk/monitor.h"
00171 #include "asterisk/netsock.h"
00172 #include "asterisk/localtime.h"
00173 #include "asterisk/abstract_jb.h"
00174 #include "asterisk/threadstorage.h"
00175 #include "asterisk/translate.h"
00176 #include "asterisk/ast_version.h"
00177 #include "asterisk/event.h"
00178 #include "asterisk/tcptls.h"
00179
00180 #ifndef FALSE
00181 #define FALSE 0
00182 #endif
00183
00184 #ifndef TRUE
00185 #define TRUE 1
00186 #endif
00187
00188 #define SIPBUFSIZE 512
00189
00190 #define XMIT_ERROR -2
00191
00192 #define SIP_RESERVED ";/?:@&=+$,# "
00193
00194
00195
00196 #define DEFAULT_DEFAULT_EXPIRY 120
00197 #define DEFAULT_MIN_EXPIRY 60
00198 #define DEFAULT_MAX_EXPIRY 3600
00199 #define DEFAULT_REGISTRATION_TIMEOUT 20
00200 #define DEFAULT_MAX_FORWARDS "70"
00201
00202
00203
00204 #define EXPIRY_GUARD_SECS 15
00205 #define EXPIRY_GUARD_LIMIT 30
00206
00207 #define EXPIRY_GUARD_MIN 500
00208
00209
00210
00211 #define EXPIRY_GUARD_PCT 0.20
00212
00213 #define DEFAULT_EXPIRY 900
00214
00215 static int min_expiry = DEFAULT_MIN_EXPIRY;
00216 static int max_expiry = DEFAULT_MAX_EXPIRY;
00217 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00218
00219 #ifndef MAX
00220 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00221 #endif
00222
00223 #define CALLERID_UNKNOWN "Unknown"
00224
00225 #define DEFAULT_MAXMS 2000
00226 #define DEFAULT_QUALIFYFREQ 60 * 1000
00227 #define DEFAULT_FREQ_NOTOK 10 * 1000
00228
00229 #define DEFAULT_RETRANS 1000
00230 #define MAX_RETRANS 6
00231 #define SIP_TIMER_T1 500
00232 #define SIP_TRANS_TIMEOUT 64 * SIP_TIMER_T1
00233
00234
00235 #define DEFAULT_TRANS_TIMEOUT -1
00236 #define MAX_AUTHTRIES 3
00237
00238 #define SIP_MAX_HEADERS 64
00239 #define SIP_MAX_LINES 64
00240 #define SIP_MAX_PACKET 4096
00241
00242 #define INITIAL_CSEQ 101
00243
00244 #define DEFAULT_MAX_SE 1800
00245 #define DEFAULT_MIN_SE 90
00246
00247 #define SDP_MAX_RTPMAP_CODECS 32
00248
00249
00250 static struct ast_jb_conf default_jbconf =
00251 {
00252 .flags = 0,
00253 .max_size = -1,
00254 .resync_threshold = -1,
00255 .impl = ""
00256 };
00257 static struct ast_jb_conf global_jbconf;
00258
00259 static const char config[] = "sip.conf";
00260 static const char notify_config[] = "sip_notify.conf";
00261
00262 #define RTP 1
00263 #define NO_RTP 0
00264
00265
00266
00267
00268 enum transfermodes {
00269 TRANSFER_OPENFORALL,
00270 TRANSFER_CLOSED,
00271 };
00272
00273
00274 enum sip_result {
00275 AST_SUCCESS = 0,
00276 AST_FAILURE = -1,
00277 };
00278
00279
00280
00281
00282 enum invitestates {
00283 INV_NONE = 0,
00284 INV_CALLING = 1,
00285 INV_PROCEEDING = 2,
00286 INV_EARLY_MEDIA = 3,
00287 INV_COMPLETED = 4,
00288 INV_CONFIRMED = 5,
00289 INV_TERMINATED = 6,
00290
00291 INV_CANCELLED = 7,
00292 };
00293
00294 enum xmittype {
00295 XMIT_CRITICAL = 2,
00296
00297 XMIT_RELIABLE = 1,
00298 XMIT_UNRELIABLE = 0,
00299 };
00300
00301 enum parse_register_result {
00302 PARSE_REGISTER_FAILED,
00303 PARSE_REGISTER_UPDATE,
00304 PARSE_REGISTER_QUERY,
00305 };
00306
00307 enum subscriptiontype {
00308 NONE = 0,
00309 XPIDF_XML,
00310 DIALOG_INFO_XML,
00311 CPIM_PIDF_XML,
00312 PIDF_XML,
00313 MWI_NOTIFICATION
00314 };
00315
00316
00317
00318
00319
00320
00321 static const struct cfsubscription_types {
00322 enum subscriptiontype type;
00323 const char * const event;
00324 const char * const mediatype;
00325 const char * const text;
00326 } subscription_types[] = {
00327 { NONE, "-", "unknown", "unknown" },
00328
00329 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00330 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00331 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00332 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00333 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00334 };
00335
00336
00337
00338
00339
00340
00341
00342 enum sip_auth_type {
00343 PROXY_AUTH = 407,
00344 WWW_AUTH = 401,
00345 };
00346
00347
00348 enum check_auth_result {
00349 AUTH_DONT_KNOW = -100,
00350
00351
00352 AUTH_SUCCESSFUL = 0,
00353 AUTH_CHALLENGE_SENT = 1,
00354 AUTH_SECRET_FAILED = -1,
00355 AUTH_USERNAME_MISMATCH = -2,
00356 AUTH_NOT_FOUND = -3,
00357 AUTH_FAKE_AUTH = -4,
00358 AUTH_UNKNOWN_DOMAIN = -5,
00359 AUTH_PEER_NOT_DYNAMIC = -6,
00360 AUTH_ACL_FAILED = -7,
00361 AUTH_BAD_TRANSPORT = -8,
00362 };
00363
00364
00365 enum sipregistrystate {
00366 REG_STATE_UNREGISTERED = 0,
00367
00368
00369
00370
00371 REG_STATE_REGSENT,
00372
00373
00374
00375
00376 REG_STATE_AUTHSENT,
00377
00378
00379
00380
00381 REG_STATE_REGISTERED,
00382
00383 REG_STATE_REJECTED,
00384
00385
00386
00387
00388
00389 REG_STATE_TIMEOUT,
00390
00391
00392 REG_STATE_NOAUTH,
00393
00394
00395 REG_STATE_FAILED,
00396
00397 };
00398
00399
00400 enum st_mode {
00401 SESSION_TIMER_MODE_INVALID = 0,
00402 SESSION_TIMER_MODE_ACCEPT,
00403 SESSION_TIMER_MODE_ORIGINATE,
00404 SESSION_TIMER_MODE_REFUSE
00405 };
00406
00407
00408 enum st_refresher {
00409 SESSION_TIMER_REFRESHER_AUTO,
00410 SESSION_TIMER_REFRESHER_UAC,
00411 SESSION_TIMER_REFRESHER_UAS
00412 };
00413
00414
00415 enum sip_transport {
00416 SIP_TRANSPORT_UDP = 1,
00417 SIP_TRANSPORT_TCP = 1 << 1,
00418 SIP_TRANSPORT_TLS = 1 << 2,
00419 };
00420
00421
00422
00423
00424
00425
00426
00427 struct sip_proxy {
00428 char name[MAXHOSTNAMELEN];
00429 struct sockaddr_in ip;
00430 time_t last_dnsupdate;
00431 enum sip_transport transport;
00432 int force;
00433
00434 };
00435
00436
00437 enum can_create_dialog {
00438 CAN_NOT_CREATE_DIALOG,
00439 CAN_CREATE_DIALOG,
00440 CAN_CREATE_DIALOG_UNSUPPORTED_METHOD,
00441 };
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 enum sipmethod {
00454 SIP_UNKNOWN,
00455 SIP_RESPONSE,
00456 SIP_REGISTER,
00457 SIP_OPTIONS,
00458 SIP_NOTIFY,
00459 SIP_INVITE,
00460 SIP_ACK,
00461 SIP_PRACK,
00462 SIP_BYE,
00463 SIP_REFER,
00464 SIP_SUBSCRIBE,
00465 SIP_MESSAGE,
00466 SIP_UPDATE,
00467 SIP_INFO,
00468 SIP_CANCEL,
00469 SIP_PUBLISH,
00470 SIP_PING,
00471 };
00472
00473
00474
00475
00476
00477 static const struct cfsip_methods {
00478 enum sipmethod id;
00479 int need_rtp;
00480 char * const text;
00481 enum can_create_dialog can_create;
00482 } sip_methods[] = {
00483 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00484 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00485 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00486 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00487 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00488 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00489 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00490 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00491 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00492 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00493 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00494 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00495 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00496 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00497 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00498 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00499 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00500 };
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 #define SUPPORTED 1
00513 #define NOT_SUPPORTED 0
00514
00515
00516 #define SIP_OPT_REPLACES (1 << 0)
00517 #define SIP_OPT_100REL (1 << 1)
00518 #define SIP_OPT_TIMER (1 << 2)
00519 #define SIP_OPT_EARLY_SESSION (1 << 3)
00520 #define SIP_OPT_JOIN (1 << 4)
00521 #define SIP_OPT_PATH (1 << 5)
00522 #define SIP_OPT_PREF (1 << 6)
00523 #define SIP_OPT_PRECONDITION (1 << 7)
00524 #define SIP_OPT_PRIVACY (1 << 8)
00525 #define SIP_OPT_SDP_ANAT (1 << 9)
00526 #define SIP_OPT_SEC_AGREE (1 << 10)
00527 #define SIP_OPT_EVENTLIST (1 << 11)
00528 #define SIP_OPT_GRUU (1 << 12)
00529 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00530 #define SIP_OPT_NOREFERSUB (1 << 14)
00531 #define SIP_OPT_HISTINFO (1 << 15)
00532 #define SIP_OPT_RESPRIORITY (1 << 16)
00533 #define SIP_OPT_UNKNOWN (1 << 17)
00534
00535
00536
00537
00538 static const struct cfsip_options {
00539 int id;
00540 int supported;
00541 char * const text;
00542 } sip_options[] = {
00543
00544 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00545
00546 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00547
00548 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00549
00550 { SIP_OPT_TIMER, SUPPORTED, "timer" },
00551
00552 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00553
00554 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00555
00556 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00557
00558 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00559
00560 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00561
00562 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00563
00564 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00565
00566 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00567
00568 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00569
00570 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00571
00572 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00573
00574 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00575
00576 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00577
00578 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00579 };
00580
00581
00582
00583
00584
00585
00586 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO"
00587
00588
00589 #define SUPPORTED_EXTENSIONS "replaces, timer"
00590
00591
00592 #define STANDARD_SIP_PORT 5060
00593 #define STANDARD_TLS_PORT 5061
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610 #define DEFAULT_CONTEXT "default"
00611 #define DEFAULT_MOHINTERPRET "default"
00612 #define DEFAULT_MOHSUGGEST ""
00613 #define DEFAULT_VMEXTEN "asterisk"
00614 #define DEFAULT_CALLERID "asterisk"
00615 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00616 #define DEFAULT_ALLOWGUEST TRUE
00617 #define DEFAULT_CALLCOUNTER FALSE
00618 #define DEFAULT_SRVLOOKUP TRUE
00619 #define DEFAULT_COMPACTHEADERS FALSE
00620 #define DEFAULT_TOS_SIP 0
00621 #define DEFAULT_TOS_AUDIO 0
00622 #define DEFAULT_TOS_VIDEO 0
00623 #define DEFAULT_TOS_TEXT 0
00624 #define DEFAULT_COS_SIP 4
00625 #define DEFAULT_COS_AUDIO 5
00626 #define DEFAULT_COS_VIDEO 6
00627 #define DEFAULT_COS_TEXT 5
00628 #define DEFAULT_ALLOW_EXT_DOM TRUE
00629 #define DEFAULT_REALM "asterisk"
00630 #define DEFAULT_NOTIFYRINGING TRUE
00631 #define DEFAULT_PEDANTIC FALSE
00632 #define DEFAULT_AUTOCREATEPEER FALSE
00633 #define DEFAULT_QUALIFY FALSE
00634 #define DEFAULT_REGEXTENONQUALIFY FALSE
00635 #define DEFAULT_T1MIN 100
00636 #define DEFAULT_MAX_CALL_BITRATE (384)
00637 #ifndef DEFAULT_USERAGENT
00638 #define DEFAULT_USERAGENT "Asterisk PBX"
00639 #define DEFAULT_SDPSESSION "Asterisk PBX"
00640 #define DEFAULT_SDPOWNER "root"
00641 #endif
00642
00643
00644
00645
00646
00647
00648
00649 static char default_context[AST_MAX_CONTEXT];
00650 static char default_subscribecontext[AST_MAX_CONTEXT];
00651 static char default_language[MAX_LANGUAGE];
00652 static char default_callerid[AST_MAX_EXTENSION];
00653 static char default_fromdomain[AST_MAX_EXTENSION];
00654 static char default_notifymime[AST_MAX_EXTENSION];
00655 static int default_qualify;
00656 static char default_vmexten[AST_MAX_EXTENSION];
00657 static char default_mohinterpret[MAX_MUSICCLASS];
00658 static char default_mohsuggest[MAX_MUSICCLASS];
00659
00660 static int default_maxcallbitrate;
00661 static struct ast_codec_pref default_prefs;
00662
00663
00664 struct sip_settings {
00665 int peer_rtupdate;
00666 int rtsave_sysname;
00667 int ignore_regexpire;
00668 };
00669
00670 static struct sip_settings sip_cfg;
00671
00672
00673
00674
00675
00676
00677
00678 static int global_directrtpsetup;
00679 static int global_limitonpeers;
00680 static int global_rtautoclear;
00681 static int global_notifyringing;
00682 static int global_notifyhold;
00683 static int global_alwaysauthreject;
00684 static int global_srvlookup;
00685 static int pedanticsipchecking;
00686 static int autocreatepeer;
00687 static int global_match_auth_username;
00688 static int global_relaxdtmf;
00689 static int global_rtptimeout;
00690 static int global_rtpholdtimeout;
00691 static int global_rtpkeepalive;
00692 static int global_reg_timeout;
00693 static int global_regattempts_max;
00694 static int global_allowguest;
00695 static int global_callcounter;
00696
00697
00698 static int global_allowsubscribe;
00699
00700 static unsigned int global_tos_sip;
00701 static unsigned int global_tos_audio;
00702 static unsigned int global_tos_video;
00703 static unsigned int global_tos_text;
00704 static unsigned int global_cos_sip;
00705 static unsigned int global_cos_audio;
00706 static unsigned int global_cos_video;
00707 static unsigned int global_cos_text;
00708 static int compactheaders;
00709 static int recordhistory;
00710 static int dumphistory;
00711 static char global_realm[MAXHOSTNAMELEN];
00712 static char global_regcontext[AST_MAX_CONTEXT];
00713 static char global_useragent[AST_MAX_EXTENSION];
00714 static char global_sdpsession[AST_MAX_EXTENSION];
00715 static char global_sdpowner[AST_MAX_EXTENSION];
00716 static int allow_external_domains;
00717 static int global_callevents;
00718 static int global_t1;
00719 static int global_t1min;
00720 static int global_timer_b;
00721 static int global_regextenonqualify;
00722 static int global_autoframing;
00723 static enum transfermodes global_allowtransfer;
00724 static struct sip_proxy global_outboundproxy;
00725 static int global_matchexterniplocally;
00726 static int global_qualifyfreq;
00727
00728
00729
00730 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00731 static enum st_mode global_st_mode;
00732 static enum st_refresher global_st_refresher;
00733 static int global_min_se;
00734 static int global_max_se;
00735
00736
00737
00738
00739 static struct ast_ha *global_contact_ha = NULL;
00740 static int global_dynamic_exclude_static = 0;
00741
00742
00743
00744
00745 static int suserobjs = 0;
00746 static int ruserobjs = 0;
00747 static int speerobjs = 0;
00748 static int rpeerobjs = 0;
00749 static int apeerobjs = 0;
00750 static int regobjs = 0;
00751
00752
00753 static struct ast_flags global_flags[2] = {{0}};
00754 static char used_context[AST_MAX_CONTEXT];
00755
00756
00757 AST_MUTEX_DEFINE_STATIC(netlock);
00758
00759
00760
00761
00762 AST_MUTEX_DEFINE_STATIC(monlock);
00763
00764 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00765
00766
00767
00768 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00769
00770 static int sip_reloading = FALSE;
00771 static enum channelreloadreason sip_reloadreason;
00772
00773 static struct sched_context *sched;
00774 static struct io_context *io;
00775 static int *sipsock_read_id;
00776
00777 #define DEC_CALL_LIMIT 0
00778 #define INC_CALL_LIMIT 1
00779 #define DEC_CALL_RINGING 2
00780 #define INC_CALL_RINGING 3
00781
00782
00783 struct sip_socket {
00784 enum sip_transport type;
00785 int fd;
00786 uint16_t port;
00787 struct ast_tcptls_session_instance *tcptls_session;
00788 };
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814 struct sip_request {
00815 char *rlPart1;
00816 char *rlPart2;
00817 int len;
00818 int headers;
00819 int method;
00820 int lines;
00821 unsigned int sdp_start;
00822 unsigned int sdp_end;
00823 char debug;
00824 char has_to_tag;
00825 char ignore;
00826 char *header[SIP_MAX_HEADERS];
00827 char *line[SIP_MAX_LINES];
00828 char data[SIP_MAX_PACKET];
00829
00830 struct sip_socket socket;
00831 AST_LIST_ENTRY(sip_request) next;
00832 };
00833
00834
00835 struct sip_dual {
00836 struct ast_channel *chan1;
00837 struct ast_channel *chan2;
00838 struct sip_request req;
00839 int seqno;
00840 };
00841
00842 struct sip_pkt;
00843
00844
00845 struct sip_invite_param {
00846 int addsipheaders;
00847 const char *uri_options;
00848 const char *vxml_url;
00849 char *auth;
00850 char *authheader;
00851 enum sip_auth_type auth_type;
00852 const char *replaces;
00853 int transfer;
00854 };
00855
00856
00857 struct sip_route {
00858 struct sip_route *next;
00859 char hop[0];
00860 };
00861
00862
00863 enum domain_mode {
00864 SIP_DOMAIN_AUTO,
00865 SIP_DOMAIN_CONFIG,
00866 };
00867
00868
00869
00870
00871
00872 struct domain {
00873 char domain[MAXHOSTNAMELEN];
00874 char context[AST_MAX_EXTENSION];
00875 enum domain_mode mode;
00876 AST_LIST_ENTRY(domain) list;
00877 };
00878
00879 static AST_LIST_HEAD_STATIC(domain_list, domain);
00880
00881
00882
00883 struct sip_history {
00884 AST_LIST_ENTRY(sip_history) list;
00885 char event[0];
00886 };
00887
00888 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00889
00890
00891 struct sip_auth {
00892 char realm[AST_MAX_EXTENSION];
00893 char username[256];
00894 char secret[256];
00895 char md5secret[256];
00896 struct sip_auth *next;
00897 };
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909 #define SIP_OUTGOING (1 << 0)
00910 #define SIP_RINGING (1 << 2)
00911 #define SIP_PROGRESS_SENT (1 << 3)
00912 #define SIP_NEEDREINVITE (1 << 4)
00913 #define SIP_PENDINGBYE (1 << 5)
00914 #define SIP_GOTREFER (1 << 6)
00915 #define SIP_CALL_LIMIT (1 << 7)
00916 #define SIP_INC_COUNT (1 << 8)
00917 #define SIP_INC_RINGING (1 << 9)
00918 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 10)
00919
00920 #define SIP_PROMISCREDIR (1 << 11)
00921 #define SIP_TRUSTRPID (1 << 12)
00922 #define SIP_USEREQPHONE (1 << 13)
00923 #define SIP_USECLIENTCODE (1 << 14)
00924
00925
00926 #define SIP_DTMF (7 << 15)
00927 #define SIP_DTMF_RFC2833 (0 << 15)
00928 #define SIP_DTMF_INBAND (1 << 15)
00929 #define SIP_DTMF_INFO (2 << 15)
00930 #define SIP_DTMF_AUTO (3 << 15)
00931 #define SIP_DTMF_SHORTINFO (4 << 15)
00932
00933
00934 #define SIP_NAT (3 << 18)
00935 #define SIP_NAT_NEVER (0 << 18)
00936 #define SIP_NAT_RFC3581 (1 << 18)
00937 #define SIP_NAT_ROUTE (2 << 18)
00938 #define SIP_NAT_ALWAYS (3 << 18)
00939
00940
00941 #define SIP_REINVITE (7 << 20)
00942 #define SIP_REINVITE_NONE (0 << 20)
00943 #define SIP_CAN_REINVITE (1 << 20)
00944 #define SIP_CAN_REINVITE_NAT (2 << 20)
00945 #define SIP_REINVITE_UPDATE (4 << 20)
00946
00947
00948 #define SIP_INSECURE (3 << 23)
00949 #define SIP_INSECURE_NONE (0 << 23)
00950 #define SIP_INSECURE_PORT (1 << 23)
00951 #define SIP_INSECURE_INVITE (1 << 24)
00952
00953
00954 #define SIP_PROG_INBAND (3 << 25)
00955 #define SIP_PROG_INBAND_NEVER (0 << 25)
00956 #define SIP_PROG_INBAND_NO (1 << 25)
00957 #define SIP_PROG_INBAND_YES (2 << 25)
00958
00959 #define SIP_SENDRPID (1 << 29)
00960 #define SIP_G726_NONSTANDARD (1 << 31)
00961
00962
00963 #define SIP_FLAGS_TO_COPY \
00964 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
00965 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
00966 SIP_USEREQPHONE | SIP_INSECURE)
00967
00968
00969
00970
00971
00972
00973 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
00974 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
00975
00976 #define SIP_PAGE2_STATECHANGEQUEUE (1 << 9)
00977
00978 #define SIP_PAGE2_RPORT_PRESENT (1 << 10)
00979 #define SIP_PAGE2_VIDEOSUPPORT (1 << 14)
00980 #define SIP_PAGE2_TEXTSUPPORT (1 << 15)
00981 #define SIP_PAGE2_ALLOWSUBSCRIBE (1 << 16)
00982 #define SIP_PAGE2_ALLOWOVERLAP (1 << 17)
00983 #define SIP_PAGE2_SUBSCRIBEMWIONLY (1 << 18)
00984 #define SIP_PAGE2_IGNORESDPVERSION (1 << 19)
00985
00986 #define SIP_PAGE2_T38SUPPORT (7 << 20)
00987 #define SIP_PAGE2_T38SUPPORT_UDPTL (1 << 20)
00988 #define SIP_PAGE2_T38SUPPORT_RTP (2 << 20)
00989 #define SIP_PAGE2_T38SUPPORT_TCP (4 << 20)
00990
00991 #define SIP_PAGE2_CALL_ONHOLD (3 << 23)
00992 #define SIP_PAGE2_CALL_ONHOLD_ACTIVE (1 << 23)
00993 #define SIP_PAGE2_CALL_ONHOLD_ONEDIR (2 << 23)
00994 #define SIP_PAGE2_CALL_ONHOLD_INACTIVE (3 << 23)
00995
00996 #define SIP_PAGE2_RFC2833_COMPENSATE (1 << 25)
00997 #define SIP_PAGE2_BUGGY_MWI (1 << 26)
00998 #define SIP_PAGE2_DIALOG_ESTABLISHED (1 << 27)
00999 #define SIP_PAGE2_REGISTERTRYING (1 << 29)
01000 #define SIP_PAGE2_UDPTL_DESTINATION (1 << 30)
01001
01002 #define SIP_PAGE2_FLAGS_TO_COPY \
01003 (SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_IGNORESDPVERSION | \
01004 SIP_PAGE2_VIDEOSUPPORT | SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | \
01005 SIP_PAGE2_BUGGY_MWI | SIP_PAGE2_TEXTSUPPORT | SIP_PAGE2_UDPTL_DESTINATION)
01006
01007
01008
01009
01010
01011
01012
01013 #define T38FAX_FILL_BIT_REMOVAL (1 << 0)
01014 #define T38FAX_TRANSCODING_MMR (1 << 1)
01015 #define T38FAX_TRANSCODING_JBIG (1 << 2)
01016
01017 #define T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF (0 << 3)
01018 #define T38FAX_RATE_MANAGEMENT_LOCAL_TCF (1 << 3)
01019
01020 #define T38FAX_UDP_EC_NONE (0 << 4)
01021 #define T38FAX_UDP_EC_FEC (1 << 4)
01022 #define T38FAX_UDP_EC_REDUNDANCY (2 << 4)
01023
01024 #define T38FAX_VERSION (3 << 6)
01025 #define T38FAX_VERSION_0 (0 << 6)
01026 #define T38FAX_VERSION_1 (1 << 6)
01027
01028 #define T38FAX_RATE_2400 (1 << 8)
01029 #define T38FAX_RATE_4800 (1 << 9)
01030 #define T38FAX_RATE_7200 (1 << 10)
01031 #define T38FAX_RATE_9600 (1 << 11)
01032 #define T38FAX_RATE_12000 (1 << 12)
01033 #define T38FAX_RATE_14400 (1 << 13)
01034
01035
01036 static int global_t38_capability = T38FAX_VERSION_0 | T38FAX_RATE_2400 | T38FAX_RATE_4800 | T38FAX_RATE_7200 | T38FAX_RATE_9600;
01037
01038
01039
01040
01041
01042
01043
01044
01045 enum sip_debug_e {
01046 sip_debug_none = 0,
01047 sip_debug_config = 1,
01048 sip_debug_console = 2,
01049 };
01050
01051 static enum sip_debug_e sipdebug;
01052
01053
01054
01055
01056
01057 static int sipdebug_text;
01058
01059
01060 enum t38state {
01061 T38_DISABLED = 0,
01062 T38_LOCAL_REINVITE,
01063 T38_PEER_DIRECT,
01064 T38_PEER_REINVITE,
01065 T38_ENABLED
01066 };
01067
01068
01069 struct t38properties {
01070 struct ast_flags t38support;
01071 int capability;
01072 int peercapability;
01073 int jointcapability;
01074 enum t38state state;
01075 unsigned int direct:1;
01076 };
01077
01078
01079 enum referstatus {
01080 REFER_IDLE,
01081 REFER_SENT,
01082 REFER_RECEIVED,
01083 REFER_CONFIRMED,
01084 REFER_ACCEPTED,
01085 REFER_RINGING,
01086 REFER_200OK,
01087 REFER_FAILED,
01088 REFER_NOAUTH
01089 };
01090
01091
01092
01093
01094
01095
01096 struct _map_x_s {
01097 int x;
01098 const char *s;
01099 };
01100
01101 static const struct _map_x_s referstatusstrings[] = {
01102 { REFER_IDLE, "<none>" },
01103 { REFER_SENT, "Request sent" },
01104 { REFER_RECEIVED, "Request received" },
01105 { REFER_CONFIRMED, "Confirmed" },
01106 { REFER_ACCEPTED, "Accepted" },
01107 { REFER_RINGING, "Target ringing" },
01108 { REFER_200OK, "Done" },
01109 { REFER_FAILED, "Failed" },
01110 { REFER_NOAUTH, "Failed - auth failure" },
01111 { -1, NULL}
01112 };
01113
01114
01115
01116 struct sip_refer {
01117 char refer_to[AST_MAX_EXTENSION];
01118 char refer_to_domain[AST_MAX_EXTENSION];
01119 char refer_to_urioption[AST_MAX_EXTENSION];
01120 char refer_to_context[AST_MAX_EXTENSION];
01121 char referred_by[AST_MAX_EXTENSION];
01122 char referred_by_name[AST_MAX_EXTENSION];
01123 char refer_contact[AST_MAX_EXTENSION];
01124 char replaces_callid[SIPBUFSIZE];
01125 char replaces_callid_totag[SIPBUFSIZE/2];
01126 char replaces_callid_fromtag[SIPBUFSIZE/2];
01127 struct sip_pvt *refer_call;
01128
01129
01130
01131 int attendedtransfer;
01132 int localtransfer;
01133 enum referstatus status;
01134 };
01135
01136
01137
01138
01139
01140 struct sip_st_dlg {
01141 int st_active;
01142 int st_interval;
01143 int st_schedid;
01144 enum st_refresher st_ref;
01145 int st_expirys;
01146 int st_active_peer_ua;
01147 int st_cached_min_se;
01148 int st_cached_max_se;
01149 enum st_mode st_cached_mode;
01150 enum st_refresher st_cached_ref;
01151 };
01152
01153
01154
01155
01156
01157 struct sip_st_cfg {
01158 enum st_mode st_mode_oper;
01159 enum st_refresher st_ref;
01160 int st_min_se;
01161 int st_max_se;
01162 };
01163
01164
01165
01166
01167
01168
01169
01170
01171 struct sip_pvt {
01172 struct sip_pvt *next;
01173 ast_mutex_t pvt_lock;
01174 enum invitestates invitestate;
01175 int method;
01176 AST_DECLARE_STRING_FIELDS(
01177 AST_STRING_FIELD(callid);
01178 AST_STRING_FIELD(randdata);
01179 AST_STRING_FIELD(accountcode);
01180 AST_STRING_FIELD(realm);
01181 AST_STRING_FIELD(nonce);
01182 AST_STRING_FIELD(opaque);
01183 AST_STRING_FIELD(qop);
01184 AST_STRING_FIELD(domain);
01185 AST_STRING_FIELD(from);
01186 AST_STRING_FIELD(useragent);
01187 AST_STRING_FIELD(exten);
01188 AST_STRING_FIELD(context);
01189 AST_STRING_FIELD(subscribecontext);
01190 AST_STRING_FIELD(subscribeuri);
01191 AST_STRING_FIELD(fromdomain);
01192 AST_STRING_FIELD(fromuser);
01193 AST_STRING_FIELD(fromname);
01194 AST_STRING_FIELD(tohost);
01195 AST_STRING_FIELD(todnid);
01196 AST_STRING_FIELD(language);
01197 AST_STRING_FIELD(mohinterpret);
01198 AST_STRING_FIELD(mohsuggest);
01199 AST_STRING_FIELD(rdnis);
01200 AST_STRING_FIELD(redircause);
01201 AST_STRING_FIELD(theirtag);
01202 AST_STRING_FIELD(username);
01203 AST_STRING_FIELD(peername);
01204 AST_STRING_FIELD(authname);
01205 AST_STRING_FIELD(uri);
01206 AST_STRING_FIELD(okcontacturi);
01207 AST_STRING_FIELD(peersecret);
01208 AST_STRING_FIELD(peermd5secret);
01209 AST_STRING_FIELD(cid_num);
01210 AST_STRING_FIELD(cid_name);
01211 AST_STRING_FIELD(fullcontact);
01212
01213 AST_STRING_FIELD(our_contact);
01214 AST_STRING_FIELD(rpid);
01215 AST_STRING_FIELD(rpid_from);
01216 AST_STRING_FIELD(url);
01217 );
01218 char via[128];
01219 struct sip_socket socket;
01220 unsigned int ocseq;
01221 unsigned int icseq;
01222 ast_group_t callgroup;
01223 ast_group_t pickupgroup;
01224 int lastinvite;
01225 int lastnoninvite;
01226 struct ast_flags flags[2];
01227
01228
01229 char do_history;
01230 char alreadygone;
01231 char needdestroy;
01232 char outgoing_call;
01233 char answered_elsewhere;
01234 char novideo;
01235 char notext;
01236
01237 int timer_t1;
01238 int timer_b;
01239 unsigned int sipoptions;
01240 unsigned int reqsipoptions;
01241 struct ast_codec_pref prefs;
01242 int capability;
01243 int jointcapability;
01244 int peercapability;
01245 int prefcodec;
01246 int noncodeccapability;
01247 int jointnoncodeccapability;
01248 int redircodecs;
01249 int maxcallbitrate;
01250 struct sip_proxy *outboundproxy;
01251 struct t38properties t38;
01252 struct sockaddr_in udptlredirip;
01253 struct ast_udptl *udptl;
01254 int callingpres;
01255 int authtries;
01256 int expiry;
01257 long branch;
01258 long invite_branch;
01259 char tag[11];
01260 int sessionid;
01261 int sessionversion;
01262 int64_t sessionversion_remote;
01263 int session_modify;
01264 struct sockaddr_in sa;
01265 struct sockaddr_in redirip;
01266 struct sockaddr_in vredirip;
01267 struct sockaddr_in tredirip;
01268 time_t lastrtprx;
01269 time_t lastrtptx;
01270 int rtptimeout;
01271 struct sockaddr_in recv;
01272 struct sockaddr_in ourip;
01273 struct ast_channel *owner;
01274 struct sip_route *route;
01275 int route_persistant;
01276 struct sip_auth *peerauth;
01277 int noncecount;
01278 char lastmsg[256];
01279 int amaflags;
01280 int pendinginvite;
01281 int glareinvite;
01282
01283
01284
01285 struct sip_request initreq;
01286
01287
01288
01289 int initid;
01290 int waitid;
01291 int autokillid;
01292 int t38id;
01293 enum transfermodes allowtransfer;
01294 struct sip_refer *refer;
01295 enum subscriptiontype subscribed;
01296 int stateid;
01297 int laststate;
01298 int dialogver;
01299
01300 struct ast_dsp *vad;
01301
01302 struct sip_peer *relatedpeer;
01303
01304 struct sip_registry *registry;
01305 struct ast_rtp *rtp;
01306 struct ast_rtp *vrtp;
01307 struct ast_rtp *trtp;
01308 struct sip_pkt *packets;
01309 struct sip_history_head *history;
01310 size_t history_entries;
01311 struct ast_variable *chanvars;
01312 AST_LIST_HEAD_NOLOCK(request_queue, sip_request) request_queue;
01313 int request_queue_sched_id;
01314 struct sip_invite_param *options;
01315 int autoframing;
01316
01317
01318
01319 struct sip_st_dlg *stimer;
01320 int hangupcause;
01321 };
01322
01323
01324
01325 #define MAX_HISTORY_ENTRIES 50
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335 static struct sip_pvt *dialoglist = NULL;
01336
01337
01338 AST_MUTEX_DEFINE_STATIC(dialoglock);
01339
01340 #ifndef DETECT_DEADLOCKS
01341
01342 static void dialoglist_lock(void)
01343 {
01344 ast_mutex_lock(&dialoglock);
01345 }
01346
01347 static void dialoglist_unlock(void)
01348 {
01349 ast_mutex_unlock(&dialoglock);
01350 }
01351 #else
01352
01353
01354 #define dialoglist_lock(x) ast_mutex_lock(&dialoglock)
01355 #define dialoglist_unlock(x) ast_mutex_unlock(&dialoglock)
01356 #endif
01357
01358 #define sip_pvt_lock(x) ast_mutex_lock(&x->pvt_lock)
01359 #define sip_pvt_trylock(x) ast_mutex_trylock(&x->pvt_lock)
01360 #define sip_pvt_unlock(x) ast_mutex_unlock(&x->pvt_lock)
01361
01362
01363
01364
01365
01366
01367 static struct sip_pvt *dialog_ref(struct sip_pvt *p)
01368 {
01369 return p;
01370 }
01371
01372 static struct sip_pvt *dialog_unref(struct sip_pvt *p)
01373 {
01374 return NULL;
01375 }
01376
01377
01378
01379
01380
01381
01382
01383 struct sip_pkt {
01384 struct sip_pkt *next;
01385 int retrans;
01386 int method;
01387 int seqno;
01388 char is_resp;
01389 char is_fatal;
01390 int response_code;
01391 struct sip_pvt *owner;
01392 int retransid;
01393 int timer_a;
01394 int timer_t1;
01395 int packetlen;
01396 char data[0];
01397 };
01398
01399
01400 struct sip_user {
01401
01402 ASTOBJ_COMPONENTS(struct sip_user);
01403 char secret[80];
01404 char md5secret[80];
01405 char context[AST_MAX_CONTEXT];
01406 char subscribecontext[AST_MAX_CONTEXT];
01407 char cid_num[80];
01408 char cid_name[80];
01409 char accountcode[AST_MAX_ACCOUNT_CODE];
01410 char language[MAX_LANGUAGE];
01411 char mohinterpret[MAX_MUSICCLASS];
01412 char mohsuggest[MAX_MUSICCLASS];
01413 char useragent[256];
01414 struct ast_codec_pref prefs;
01415 ast_group_t callgroup;
01416 ast_group_t pickupgroup;
01417 unsigned int sipoptions;
01418 struct ast_flags flags[2];
01419
01420
01421 char is_realtime;
01422
01423 int amaflags;
01424 int callingpres;
01425 int capability;
01426 int inUse;
01427 int call_limit;
01428 enum transfermodes allowtransfer;
01429 struct ast_ha *ha;
01430 struct ast_variable *chanvars;
01431 int maxcallbitrate;
01432 int autoframing;
01433 struct sip_st_cfg stimer;
01434 };
01435
01436
01437
01438
01439
01440
01441
01442 struct sip_mailbox {
01443 char *mailbox;
01444 char *context;
01445
01446 struct ast_event_sub *event_sub;
01447 AST_LIST_ENTRY(sip_mailbox) entry;
01448 };
01449
01450
01451
01452 struct sip_peer {
01453 ASTOBJ_COMPONENTS(struct sip_peer);
01454
01455 struct sip_socket socket;
01456 enum sip_transport default_outbound_transport;
01457 unsigned int transports:3;
01458 char secret[80];
01459 char md5secret[80];
01460 struct sip_auth *auth;
01461 char context[AST_MAX_CONTEXT];
01462 char subscribecontext[AST_MAX_CONTEXT];
01463 char username[80];
01464 char accountcode[AST_MAX_ACCOUNT_CODE];
01465 int amaflags;
01466 char tohost[MAXHOSTNAMELEN];
01467 char regexten[AST_MAX_EXTENSION];
01468 char fromuser[80];
01469 char fromdomain[MAXHOSTNAMELEN];
01470 char fullcontact[256];
01471 char cid_num[80];
01472 char cid_name[80];
01473 int callingpres;
01474 int inUse;
01475 int inRinging;
01476 int onHold;
01477 int call_limit;
01478 int busy_level;
01479 enum transfermodes allowtransfer;
01480 char vmexten[AST_MAX_EXTENSION];
01481 char language[MAX_LANGUAGE];
01482 char mohinterpret[MAX_MUSICCLASS];
01483 char mohsuggest[MAX_MUSICCLASS];
01484 char useragent[256];
01485 struct ast_codec_pref prefs;
01486 int lastmsgssent;
01487 unsigned int sipoptions;
01488 struct ast_flags flags[2];
01489
01490
01491 AST_LIST_HEAD_NOLOCK(, sip_mailbox) mailboxes;
01492
01493
01494 char is_realtime;
01495 char rt_fromcontact;
01496 char host_dynamic;
01497 char selfdestruct;
01498
01499 int expire;
01500 int capability;
01501 int rtptimeout;
01502 int rtpholdtimeout;
01503 int rtpkeepalive;
01504 ast_group_t callgroup;
01505 ast_group_t pickupgroup;
01506 struct sip_proxy *outboundproxy;
01507 struct ast_dnsmgr_entry *dnsmgr;
01508 struct sockaddr_in addr;
01509 int maxcallbitrate;
01510
01511
01512 struct sip_pvt *call;
01513 int pokeexpire;
01514 int lastms;
01515 int maxms;
01516 int qualifyfreq;
01517 struct timeval ps;
01518 struct sockaddr_in defaddr;
01519 struct ast_ha *ha;
01520 struct ast_ha *contactha;
01521 struct ast_variable *chanvars;
01522 struct sip_pvt *mwipvt;
01523 int autoframing;
01524 struct sip_st_cfg stimer;
01525 int timer_t1;
01526 int timer_b;
01527 int deprecated_username;
01528 };
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546 struct sip_registry {
01547 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
01548 AST_DECLARE_STRING_FIELDS(
01549 AST_STRING_FIELD(callid);
01550 AST_STRING_FIELD(realm);
01551 AST_STRING_FIELD(nonce);
01552 AST_STRING_FIELD(opaque);
01553 AST_STRING_FIELD(qop);
01554 AST_STRING_FIELD(domain);
01555 AST_STRING_FIELD(username);
01556 AST_STRING_FIELD(authuser);
01557 AST_STRING_FIELD(hostname);
01558 AST_STRING_FIELD(secret);
01559 AST_STRING_FIELD(md5secret);
01560 AST_STRING_FIELD(callback);
01561 AST_STRING_FIELD(random);
01562 );
01563 enum sip_transport transport;
01564 int portno;
01565 int expire;
01566 int expiry;
01567 int regattempts;
01568 int timeout;
01569 int refresh;
01570 struct sip_pvt *call;
01571 enum sipregistrystate regstate;
01572 struct timeval regtime;
01573 int callid_valid;
01574 unsigned int ocseq;
01575 struct sockaddr_in us;
01576 int noncecount;
01577 char lastmsg[256];
01578 };
01579
01580 struct sip_threadinfo {
01581 int stop;
01582 pthread_t threadid;
01583 struct ast_tcptls_session_instance *tcptls_session;
01584 enum sip_transport type;
01585 AST_LIST_ENTRY(sip_threadinfo) list;
01586 };
01587
01588
01589
01590
01591 static AST_LIST_HEAD_STATIC(threadl, sip_threadinfo);
01592
01593
01594 static struct ast_user_list {
01595 ASTOBJ_CONTAINER_COMPONENTS(struct sip_user);
01596 } userl;
01597
01598
01599 static struct ast_peer_list {
01600 ASTOBJ_CONTAINER_COMPONENTS(struct sip_peer);
01601 } peerl;
01602
01603
01604 static struct ast_register_list {
01605 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01606 int recheck;
01607 } regl;
01608
01609 static int temp_pvt_init(void *);
01610 static void temp_pvt_cleanup(void *);
01611
01612
01613 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01614
01615 #ifdef LOW_MEMORY
01616 static void ts_ast_rtp_destroy(void *);
01617
01618 AST_THREADSTORAGE_CUSTOM(ts_audio_rtp, NULL, ts_ast_rtp_destroy);
01619 AST_THREADSTORAGE_CUSTOM(ts_video_rtp, NULL, ts_ast_rtp_destroy);
01620 AST_THREADSTORAGE_CUSTOM(ts_text_rtp, NULL, ts_ast_rtp_destroy);
01621 #endif
01622
01623
01624
01625 static struct sip_auth *authl = NULL;
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644 static int sipsock = -1;
01645
01646 static struct sockaddr_in bindaddr;
01647
01648
01649
01650
01651
01652
01653
01654 static struct sockaddr_in internip;
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674 static struct sockaddr_in externip;
01675
01676 static char externhost[MAXHOSTNAMELEN];
01677 static time_t externexpire;
01678 static int externrefresh = 10;
01679 static struct sockaddr_in stunaddr;
01680
01681
01682
01683
01684
01685
01686
01687 static struct ast_ha *localaddr;
01688
01689 static int ourport_tcp;
01690 static int ourport_tls;
01691 static struct sockaddr_in debugaddr;
01692
01693 static struct ast_config *notify_types;
01694
01695
01696
01697 #define UNLINK(element, head, prev) do { \
01698 if (prev) \
01699 (prev)->next = (element)->next; \
01700 else \
01701 (head) = (element)->next; \
01702 } while (0)
01703
01704 enum t38_action_flag {
01705 SDP_T38_NONE = 0,
01706 SDP_T38_INITIATE,
01707 SDP_T38_ACCEPT,
01708 };
01709
01710
01711
01712
01713
01714
01715 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
01716 static int sip_devicestate(void *data);
01717 static int sip_sendtext(struct ast_channel *ast, const char *text);
01718 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01719 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen);
01720 static int sip_hangup(struct ast_channel *ast);
01721 static int sip_answer(struct ast_channel *ast);
01722 static struct ast_frame *sip_read(struct ast_channel *ast);
01723 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01724 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01725 static int sip_transfer(struct ast_channel *ast, const char *dest);
01726 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01727 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01728 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01729 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen);
01730 static const char *sip_get_callid(struct ast_channel *chan);
01731
01732 static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin);
01733 static int sip_standard_port(enum sip_transport type, int port);
01734 static int sip_prepare_socket(struct sip_pvt *p);
01735 static int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport);
01736
01737
01738 static int sipsock_read(int *id, int fd, short events, void *ignore);
01739 static int __sip_xmit(struct sip_pvt *p, char *data, int len);
01740 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod);
01741 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01742 static int retrans_pkt(const void *data);
01743 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req);
01744 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);
01745 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01746 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01747 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01748 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp);
01749 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01750 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);
01751 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01752 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable);
01753 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01754 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01755 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
01756 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp);
01757 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01758 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01759 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01760 static int transmit_refer(struct sip_pvt *p, const char *dest);
01761 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten);
01762 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01763 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01764 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01765 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01766 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01767 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01768 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
01769 static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only);
01770
01771
01772 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
01773 int useglobal_nat, const int intended_method, struct sip_request *req);
01774 static int __sip_autodestruct(const void *data);
01775 static void sip_scheddestroy(struct sip_pvt *p, int ms);
01776 static int sip_cancel_destroy(struct sip_pvt *p);
01777 static struct sip_pvt *sip_destroy(struct sip_pvt *p);
01778 static int __sip_destroy(struct sip_pvt *p, int lockowner, int lockdialoglist);
01779 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01780 static void __sip_pretend_ack(struct sip_pvt *p);
01781 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01782 static int auto_congest(const void *arg);
01783 static int update_call_counter(struct sip_pvt *fup, int event);
01784 static int hangup_sip2cause(int cause);
01785 static const char *hangup_cause2sip(int cause);
01786 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method);
01787 static void free_old_route(struct sip_route *route);
01788 static void list_route(struct sip_route *route);
01789 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01790 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
01791 struct sip_request *req, char *uri);
01792 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01793 static void check_pendings(struct sip_pvt *p);
01794 static void *sip_park_thread(void *stuff);
01795 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno);
01796 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01797
01798
01799 static void try_suggested_sip_codec(struct sip_pvt *p);
01800 static const char* get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01801 static const char *get_sdp(struct sip_request *req, const char *name);
01802 static int find_sdp(struct sip_request *req);
01803 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action);
01804 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
01805 struct ast_str **m_buf, struct ast_str **a_buf,
01806 int debug, int *min_packet_size);
01807 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
01808 struct ast_str **m_buf, struct ast_str **a_buf,
01809 int debug);
01810 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38);
01811 static void do_setnat(struct sip_pvt *p, int natflags);
01812 static void stop_media_flows(struct sip_pvt *p);
01813
01814
01815 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01816 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01817 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01818 const char *secret, const char *md5secret, int sipmethod,
01819 char *uri, enum xmittype reliable, int ignore);
01820 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01821 int sipmethod, char *uri, enum xmittype reliable,
01822 struct sockaddr_in *sin, struct sip_peer **authpeer);
01823 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin);
01824
01825
01826 static int check_sip_domain(const char *domain, char *context, size_t len);
01827 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01828 static void clear_sip_domains(void);
01829
01830
01831 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, const char *configuration, int lineno);
01832 static int clear_realm_authentication(struct sip_auth *authlist);
01833 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
01834
01835
01836 static int sip_do_reload(enum channelreloadreason reason);
01837 static int reload_config(enum channelreloadreason reason);
01838 static int expire_register(const void *data);
01839 static void *do_monitor(void *data);
01840 static int restart_monitor(void);
01841 static int sip_addrcmp(char *name, struct sockaddr_in *sin);
01842 static int sip_refer_allocate(struct sip_pvt *p);
01843 static void ast_quiet_chan(struct ast_channel *chan);
01844 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01845
01846
01847
01848
01849
01850
01851
01852 #define check_request_transport(peer, tmpl) ({ \
01853 int ret = 0; \
01854 if (peer->socket.type == tmpl->socket.type) \
01855 ; \
01856 else if (!(peer->transports & tmpl->socket.type)) {\
01857 ast_log(LOG_ERROR, \
01858 "'%s' is not a valid transport for '%s'. we only use '%s'! ending call.\n", \
01859 get_transport(tmpl->socket.type), peer->name, get_transport_list(peer) \
01860 ); \
01861 ret = 1; \
01862 } else if (peer->socket.type & SIP_TRANSPORT_TLS) { \
01863 ast_log(LOG_WARNING, \
01864 "peer '%s' HAS NOT USED (OR SWITCHED TO) TLS in favor of '%s' (but this was allowed in sip.conf)!\n", \
01865 peer->name, get_transport(tmpl->socket.type) \
01866 ); \
01867 } else { \
01868 ast_debug(1, \
01869 "peer '%s' has contacted us over %s even though we prefer %s.\n", \
01870 peer->name, get_transport(tmpl->socket.type), get_transport(peer->socket.type) \
01871 ); \
01872 }\
01873 (ret); \
01874 })
01875
01876
01877
01878 static int cb_extensionstate(char *context, char* exten, int state, void *data);
01879 static int sip_devicestate(void *data);
01880 static int sip_poke_noanswer(const void *data);
01881 static int sip_poke_peer(struct sip_peer *peer);
01882 static void sip_poke_all_peers(void);
01883 static void sip_peer_hold(struct sip_pvt *p, int hold);
01884 static void mwi_event_cb(const struct ast_event *, void *);
01885
01886
01887 static const char *sip_nat_mode(const struct sip_pvt *p);
01888 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01889 static char *transfermode2str(enum transfermodes mode) attribute_const;
01890 static const char *nat2str(int nat) attribute_const;
01891 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01892 static char *sip_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01893 static char * _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01894 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01895 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01896 static void print_group(int fd, ast_group_t group, int crlf);
01897 static const char *dtmfmode2str(int mode) attribute_const;
01898 static int str2dtmfmode(const char *str) attribute_unused;
01899 static const char *insecure2str(int mode) attribute_const;
01900 static void cleanup_stale_contexts(char *new, char *old);
01901 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01902 static const char *domain_mode_to_text(const enum domain_mode mode);
01903 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01904 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01905 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01906 static char *sip_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01907 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01908 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01909 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01910 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01911 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01912 static char *complete_sip_peer(const char *word, int state, int flags2);
01913 static char *complete_sip_registered_peer(const char *word, int state, int flags2);
01914 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state);
01915 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01916 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state);
01917 static char *complete_sip_user(const char *word, int state, int flags2);
01918 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
01919 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01920 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01921 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01922 static char *sip_do_debug_ip(int fd, char *arg);
01923 static char *sip_do_debug_peer(int fd, char *arg);
01924 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01925 static char *sip_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01926 static char *sip_do_history_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01927 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01928 static int sip_dtmfmode(struct ast_channel *chan, void *data);
01929 static int sip_addheader(struct ast_channel *chan, void *data);
01930 static int sip_do_reload(enum channelreloadreason reason);
01931 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01932 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen);
01933
01934
01935
01936
01937
01938 static void sip_dump_history(struct sip_pvt *dialog);
01939 static inline int sip_debug_test_addr(const struct sockaddr_in *addr);
01940 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01941
01942
01943
01944
01945 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
01946 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01947 static void sip_dump_history(struct sip_pvt *dialog);
01948
01949
01950 static struct sip_peer *temp_peer(const char *name);
01951 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01952 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01953 static int update_call_counter(struct sip_pvt *fup, int event);
01954 static void sip_destroy_peer(struct sip_peer *peer);
01955 static void sip_destroy_user(struct sip_user *user);
01956 static int sip_poke_peer(struct sip_peer *peer);
01957 static void set_peer_defaults(struct sip_peer *peer);
01958 static struct sip_peer *temp_peer(const char *name);
01959 static void register_peer_exten(struct sip_peer *peer, int onoff);
01960 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int devstate_only);
01961 static struct sip_user *find_user(const char *name, int realtime);
01962 static int sip_poke_peer_s(const void *data);
01963 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01964 static void reg_source_db(struct sip_peer *peer);
01965 static void destroy_association(struct sip_peer *peer);
01966 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
01967 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01968 static void set_socket_transport(struct sip_socket *socket, int transport);
01969
01970
01971 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey, int deprecated_username, int lastms);
01972 static struct sip_user *realtime_user(const char *username);
01973 static void update_peer(struct sip_peer *p, int expiry);
01974 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *config);
01975 static const char *get_name_from_variable(struct ast_variable *var, const char *newpeername);
01976 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin, int devstate_only);
01977 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01978
01979
01980 static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us, struct sip_pvt *p);
01981 static void sip_registry_destroy(struct sip_registry *reg);
01982 static int sip_register(const char *value, int lineno);
01983 static const char *regstate2str(enum sipregistrystate regstate) attribute_const;
01984 static int sip_reregister(const void *data);
01985 static int __sip_do_register(struct sip_registry *r);
01986 static int sip_reg_timeout(const void *data);
01987 static void sip_send_all_registers(void);
01988
01989
01990 static void append_date(struct sip_request *req);
01991 static int determine_firstline_parts(struct sip_request *req);
01992 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01993 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01994 static int find_sip_method(const char *msg);
01995 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
01996 static int parse_request(struct sip_request *req);
01997 static const char *get_header(const struct sip_request *req, const char *name);
01998 static const char *referstatus2str(enum referstatus rstatus) attribute_pure;
01999 static int method_match(enum sipmethod id, const char *name);
02000 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
02001 static char *get_in_brackets(char *tmp);
02002 static const char *find_alias(const char *name, const char *_default);
02003 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
02004 static int lws2sws(char *msgbuf, int len);
02005 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
02006 static char *remove_uri_parameters(char *uri);
02007 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
02008 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
02009 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
02010 static int set_address_from_contact(struct sip_pvt *pvt);
02011 static void check_via(struct sip_pvt *p, struct sip_request *req);
02012 static char *get_calleridname(const char *input, char *output, size_t outputsize);
02013 static int get_rpid_num(const char *input, char *output, int maxlen);
02014 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq);
02015 static int get_destination(struct sip_pvt *p, struct sip_request *oreq);
02016 static int get_msg_text(char *buf, int len, struct sip_request *req);
02017 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
02018
02019
02020 static void *_sip_tcp_helper_thread(struct sip_pvt *pvt, struct ast_tcptls_session_instance *tcptls_session);
02021 static void *sip_tcp_worker_fn(void *);
02022
02023
02024 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
02025 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
02026 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
02027 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod);
02028 static int init_resp(struct sip_request *resp, const char *msg);
02029 static inline int resp_needs_contact(const char *msg, enum sipmethod method);
02030 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
02031 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p);
02032 static void build_via(struct sip_pvt *p);
02033 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
02034 static int create_addr(struct sip_pvt *dialog, const char *opeer, int newdialog);
02035 static char *generate_random_string(char *buf, size_t size);
02036 static void build_callid_pvt(struct sip_pvt *pvt);
02037 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain);
02038 static void make_our_tag(char *tagbuf, size_t len);
02039 static int add_header(struct sip_request *req, const char *var, const char *value);
02040 static int add_header_contentLength(struct sip_request *req, int len);
02041 static int add_line(struct sip_request *req, const char *line);
02042 static int add_text(struct sip_request *req, const char *text);
02043 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode);
02044 static int add_vidupdate(struct sip_request *req);
02045 static void add_route(struct sip_request *req, struct sip_route *route);
02046 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
02047 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
02048 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
02049 static void set_destination(struct sip_pvt *p, char *uri);
02050 static void append_date(struct sip_request *req);
02051 static void build_contact(struct sip_pvt *p);
02052 static void build_rpid(struct sip_pvt *p);
02053
02054
02055 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock);
02056 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);
02057 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock);
02058 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
02059 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e);
02060 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
02061 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
02062 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
02063 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
02064 static int handle_request_options(struct sip_pvt *p, struct sip_request *req);
02065 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct sockaddr_in *sin);
02066 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
02067 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno);
02068
02069
02070 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02071 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02072 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02073 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
02074
02075
02076 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);
02077 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
02078 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
02079 static enum ast_rtp_get_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
02080 static int sip_get_codec(struct ast_channel *chan);
02081 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect);
02082
02083
02084 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
02085 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
02086 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
02087 static void change_t38_state(struct sip_pvt *p, int state);
02088
02089
02090 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp);
02091 static int proc_session_timer(const void *vp);
02092 static void stop_session_timer(struct sip_pvt *p);
02093 static void start_session_timer(struct sip_pvt *p);
02094 static void restart_session_timer(struct sip_pvt *p);
02095 static const char *strefresher2str(enum st_refresher r);
02096 static int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher *const p_ref);
02097 static int parse_minse(const char *p_hdrval, int *const p_interval);
02098 static int st_get_se(struct sip_pvt *, int max);
02099 static enum st_refresher st_get_refresher(struct sip_pvt *);
02100 static enum st_mode st_get_mode(struct sip_pvt *);
02101 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p);
02102
02103
02104
02105 static const struct ast_channel_tech sip_tech = {
02106 .type = "SIP",
02107 .description = "Session Initiation Protocol (SIP)",
02108 .capabilities = AST_FORMAT_AUDIO_MASK,
02109 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
02110 .requester = sip_request_call,
02111 .devicestate = sip_devicestate,
02112 .call = sip_call,
02113 .send_html = sip_sendhtml,
02114 .hangup = sip_hangup,
02115 .answer = sip_answer,
02116 .read = sip_read,
02117 .write = sip_write,
02118 .write_video = sip_write,
02119 .write_text = sip_write,
02120 .indicate = sip_indicate,
02121 .transfer = sip_transfer,
02122 .fixup = sip_fixup,
02123 .send_digit_begin = sip_senddigit_begin,
02124 .send_digit_end = sip_senddigit_end,
02125 .bridge = ast_rtp_bridge,
02126 .early_bridge = ast_rtp_early_bridge,
02127 .send_text = sip_sendtext,
02128 .func_channel_read = acf_channel_read,
02129 .queryoption = sip_queryoption,
02130 .get_pvt_uniqueid = sip_get_callid,
02131 };
02132
02133
02134
02135
02136
02137
02138
02139 static struct ast_channel_tech sip_tech_info;
02140
02141
02142
02143 static struct ast_tls_config sip_tls_cfg;
02144
02145
02146 static struct ast_tls_config default_tls_cfg;
02147
02148
02149 static struct server_args sip_tcp_desc = {
02150 .accept_fd = -1,
02151 .master = AST_PTHREADT_NULL,
02152 .tls_cfg = NULL,
02153 .poll_timeout = -1,
02154 .name = "sip tcp server",
02155 .accept_fn = ast_tcptls_server_root,
02156 .worker_fn = sip_tcp_worker_fn,
02157 };
02158
02159
02160 static struct server_args sip_tls_desc = {
02161 .accept_fd = -1,
02162 .master = AST_PTHREADT_NULL,
02163 .tls_cfg = &sip_tls_cfg,
02164 .poll_timeout = -1,
02165 .name = "sip tls server",
02166 .accept_fn = ast_tcptls_server_root,
02167 .worker_fn = sip_tcp_worker_fn,
02168 };
02169
02170
02171 #define IS_SIP_TECH(t) ((t) == &sip_tech || (t) == &sip_tech_info)
02172
02173
02174
02175
02176 static const char *map_x_s(const struct _map_x_s *table, int x, const char *errorstring)
02177 {
02178 const struct _map_x_s *cur;
02179
02180 for (cur = table; cur->s; cur++)
02181 if (cur->x == x)
02182 return cur->s;
02183 return errorstring;
02184 }
02185
02186
02187
02188
02189 static int map_s_x(const struct _map_x_s *table, const char *s, int errorvalue)
02190 {
02191 const struct _map_x_s *cur;
02192
02193 for (cur = table; cur->s; cur++)
02194 if (!strcasecmp(cur->s, s))
02195 return cur->x;
02196 return errorvalue;
02197 }
02198
02199
02200
02201 static struct ast_rtp_protocol sip_rtp = {
02202 .type = "SIP",
02203 .get_rtp_info = sip_get_rtp_peer,
02204 .get_vrtp_info = sip_get_vrtp_peer,
02205 .get_trtp_info = sip_get_trtp_peer,
02206 .set_rtp_peer = sip_set_rtp_peer,
02207 .get_codec = sip_get_codec,
02208 };
02209
02210
02211
02212 static void *sip_tcp_worker_fn(void *data)
02213 {
02214 struct ast_tcptls_session_instance *tcptls_session = data;
02215
02216 return _sip_tcp_helper_thread(NULL, tcptls_session);
02217 }
02218
02219
02220 static void *_sip_tcp_helper_thread(struct sip_pvt *pvt, struct ast_tcptls_session_instance *tcptls_session)
02221 {
02222 int res, cl;
02223 struct sip_request req = { 0, } , reqcpy = { 0, };
02224 struct sip_threadinfo *me;
02225 char buf[1024] = "";
02226
02227 me = ast_calloc(1, sizeof(*me));
02228
02229 if (!me)
02230 goto cleanup2;
02231
02232 me->threadid = pthread_self();
02233 me->tcptls_session = tcptls_session;
02234 if (tcptls_session->ssl)
02235 me->type = SIP_TRANSPORT_TLS;
02236 else
02237 me->type = SIP_TRANSPORT_TCP;
02238
02239 ast_debug(2, "Starting thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
02240
02241 AST_LIST_LOCK(&threadl);
02242 AST_LIST_INSERT_TAIL(&threadl, me, list);
02243 AST_LIST_UNLOCK(&threadl);
02244
02245
02246 for (;;) {
02247 memset(req.data, 0, sizeof(req.data));
02248 req.len = 0;
02249 req.ignore = 0;
02250 req.debug = 0;
02251
02252 if (tcptls_session->ssl) {
02253 set_socket_transport(&req.socket, SIP_TRANSPORT_TLS);
02254 req.socket.port = htons(ourport_tls);
02255 } else {
02256 set_socket_transport(&req.socket, SIP_TRANSPORT_TCP);
02257 req.socket.port = htons(ourport_tcp);
02258 }
02259 req.socket.fd = tcptls_session->fd;
02260 res = ast_wait_for_input(tcptls_session->fd, -1);
02261 if (res < 0) {
02262 ast_debug(2, "SIP %s server :: ast_wait_for_input returned %d\n", tcptls_session->ssl ? "SSL": "TCP", res);
02263 goto cleanup;
02264 }
02265
02266
02267 while (req.len < 4 || strncmp((char *)&req.data + req.len - 4, "\r\n\r\n", 4)) {
02268 ast_mutex_lock(&tcptls_session->lock);
02269 if (!fgets(buf, sizeof(buf), tcptls_session->f)) {
02270 ast_mutex_unlock(&tcptls_session->lock);
02271 goto cleanup;
02272 }
02273 ast_mutex_unlock(&tcptls_session->lock);
02274 if (me->stop)
02275 goto cleanup;
02276 strncat(req.data, buf, sizeof(req.data) - req.len - 1);
02277 req.len = strlen(req.data);
02278 }
02279 parse_copy(&reqcpy, &req);
02280 if (sscanf(get_header(&reqcpy, "Content-Length"), "%d", &cl)) {
02281 while (cl > 0) {
02282 ast_mutex_lock(&tcptls_session->lock);
02283 if (!fread(buf, (cl < sizeof(buf)) ? cl : sizeof(buf), 1, tcptls_session->f)) {
02284 ast_mutex_unlock(&tcptls_session->lock);
02285 goto cleanup;
02286 }
02287 ast_mutex_unlock(&tcptls_session->lock);
02288 if (me->stop)
02289 goto cleanup;
02290 cl -= strlen(buf);
02291 strncat(req.data, buf, sizeof(req.data) - req.len - 1);
02292 req.len = strlen(req.data);
02293 }
02294 }
02295 req.socket.tcptls_session = tcptls_session;
02296 handle_request_do(&req, &tcptls_session->requestor);
02297 }
02298
02299 cleanup:
02300 AST_LIST_LOCK(&threadl);
02301 AST_LIST_REMOVE(&threadl, me, list);
02302 AST_LIST_UNLOCK(&threadl);
02303 ast_free(me);
02304 cleanup2:
02305 fclose(tcptls_session->f);
02306 tcptls_session->f = NULL;
02307 tcptls_session->fd = -1;
02308
02309 ast_debug(2, "Shutting down thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
02310
02311 ao2_ref(tcptls_session, -1);
02312 tcptls_session = NULL;
02313
02314 return NULL;
02315 }
02316
02317
02318
02319
02320
02321
02322
02323 static void unref_peer(struct sip_peer *peer)
02324 {
02325 ASTOBJ_UNREF(peer, sip_destroy_peer);
02326 }
02327
02328 static void unref_user(struct sip_user *user)
02329 {
02330 ASTOBJ_UNREF(user, sip_destroy_user);
02331 }
02332
02333 static void *registry_unref(struct sip_registry *reg)
02334 {
02335 ast_debug(3, "SIP Registry %s: refcount now %d\n", reg->hostname, reg->refcount - 1);
02336 ASTOBJ_UNREF(reg, sip_registry_destroy);
02337 return NULL;
02338 }
02339
02340
02341 static struct sip_registry *registry_addref(struct sip_registry *reg)
02342 {
02343 ast_debug(3, "SIP Registry %s: refcount now %d\n", reg->hostname, reg->refcount + 1);
02344 return ASTOBJ_REF(reg);
02345 }
02346
02347
02348 static struct ast_udptl_protocol sip_udptl = {
02349 type: "SIP",
02350 get_udptl_info: sip_get_udptl_peer,
02351 set_udptl_peer: sip_set_udptl_peer,
02352 };
02353
02354 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
02355 __attribute__((format(printf, 2, 3)));
02356
02357
02358
02359 static const char *referstatus2str(enum referstatus rstatus)
02360 {
02361 return map_x_s(referstatusstrings, rstatus, "");
02362 }
02363
02364
02365
02366
02367 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
02368 {
02369 if (p->initreq.headers)
02370 ast_debug(1, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
02371 else
02372 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
02373
02374 copy_request(&p->initreq, req);
02375 parse_request(&p->initreq);
02376 if (req->debug)
02377 ast_verbose("Initreq: %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
02378 }
02379
02380
02381 static void sip_alreadygone(struct sip_pvt *dialog)
02382 {
02383 ast_debug(3, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
02384 dialog->alreadygone = 1;
02385 }
02386
02387
02388 static int proxy_update(struct sip_proxy *proxy)
02389 {
02390
02391
02392 if (!inet_aton(proxy->name, &proxy->ip.sin_addr)) {
02393
02394
02395 if (ast_get_ip_or_srv(&proxy->ip, proxy->name, global_srvlookup ? "_sip._udp" : NULL) < 0) {
02396 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", proxy->name);
02397 return FALSE;
02398 }
02399 }
02400 proxy->last_dnsupdate = time(NULL);
02401 return TRUE;
02402 }
02403
02404
02405 static struct sip_proxy *proxy_allocate(char *name, char *port, int force)
02406 {
02407 struct sip_proxy *proxy;
02408
02409 if (ast_strlen_zero(name)) {
02410 return NULL;
02411 }
02412
02413 proxy = ast_calloc(1, sizeof(*proxy));
02414 if (!proxy)
02415 return NULL;
02416 proxy->force = force;
02417 ast_copy_string(proxy->name, name, sizeof(proxy->name));
02418 proxy->ip.sin_port = htons((!ast_strlen_zero(port) ? atoi(port) : STANDARD_SIP_PORT));
02419 proxy_update(proxy);
02420 return proxy;
02421 }
02422
02423
02424 static struct sip_proxy *obproxy_get(struct sip_pvt *dialog, struct sip_peer *peer)
02425 {
02426 if (peer && peer->outboundproxy) {
02427 if (sipdebug)
02428 ast_debug(1, "OBPROXY: Applying peer OBproxy to this call\n");
02429 append_history(dialog, "OBproxy", "Using peer obproxy %s", peer->outboundproxy->name);
02430 return peer->outboundproxy;
02431 }
02432 if (global_outboundproxy.name[0]) {
02433 if (sipdebug)
02434 ast_debug(1, "OBPROXY: Applying global OBproxy to this call\n");
02435 append_history(dialog, "OBproxy", "Using global obproxy %s", global_outboundproxy.name);
02436 return &global_outboundproxy;
02437 }
02438 if (sipdebug)
02439 ast_debug(1, "OBPROXY: Not applying OBproxy to this call\n");
02440 return NULL;
02441 }
02442
02443
02444
02445
02446
02447
02448
02449 static int method_match(enum sipmethod id, const char *name)
02450 {
02451 int len = strlen(sip_methods[id].text);
02452 int l_name = name ? strlen(name) : 0;
02453
02454 return (l_name >= len && name[len] < 33 &&
02455 !strncasecmp(sip_methods[id].text, name, len));
02456 }
02457
02458
02459 static int find_sip_method(const char *msg)
02460 {
02461 int i, res = 0;
02462
02463 if (ast_strlen_zero(msg))
02464 return 0;
02465 for (i = 1; i < ARRAY_LEN(sip_methods) && !res; i++) {
02466 if (method_match(i, msg))
02467 res = sip_methods[i].id;
02468 }
02469 return res;
02470 }
02471
02472
02473 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
02474 {
02475 char *next, *sep;
02476 char *temp;
02477 unsigned int profile = 0;
02478 int i, found;
02479
02480 if (ast_strlen_zero(supported) )
02481 return 0;
02482 temp = ast_strdupa(supported);
02483
02484 if (sipdebug)
02485 ast_debug(3, "Begin: parsing SIP \"Supported: %s\"\n", supported);
02486
02487 for (next = temp; next; next = sep) {
02488 found = FALSE;
02489 if ( (sep = strchr(next, ',')) != NULL)
02490 *sep++ = '\0';
02491 next = ast_skip_blanks(next);
02492 if (sipdebug)
02493 ast_debug(3, "Found SIP option: -%s-\n", next);
02494 for (i = 0; i < ARRAY_LEN(sip_options); i++) {
02495 if (!strcasecmp(next, sip_options[i].text)) {
02496 profile |= sip_options[i].id;
02497 found = TRUE;
02498 if (sipdebug)
02499 ast_debug(3, "Matched SIP option: %s\n", next);
02500 break;
02501 }
02502 }
02503
02504
02505
02506
02507
02508 if (!found)
02509 profile |= SIP_OPT_UNKNOWN;
02510
02511 if (!found && sipdebug) {
02512 if (!strncasecmp(next, "x-", 2))
02513 ast_debug(3, "Found private SIP option, not supported: %s\n", next);
02514 else
02515 ast_debug(3, "Found no match for SIP option: %s (Please file bug report!)\n", next);
02516 }
02517 }
02518
02519 if (pvt)
02520 pvt->sipoptions = profile;
02521 return profile;
02522 }
02523
02524
02525 static inline int sip_debug_test_addr(const struct sockaddr_in *addr)
02526 {
02527 if (!sipdebug)
02528 return 0;
02529 if (debugaddr.sin_addr.s_addr) {
02530 if (((ntohs(debugaddr.sin_port) != 0)
02531 && (debugaddr.sin_port != addr->sin_port))
02532 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
02533 return 0;
02534 }
02535 return 1;
02536 }
02537
02538
02539 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
02540 {
02541 if (p->outboundproxy)
02542 return &p->outboundproxy->ip;
02543
02544 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
02545 }
02546
02547
02548 static const char *sip_nat_mode(const struct sip_pvt *p)
02549 {
02550 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
02551 }
02552
02553
02554 static inline int sip_debug_test_pvt(struct sip_pvt *p)
02555 {
02556 if (!sipdebug)
02557 return 0;
02558 return sip_debug_test_addr(sip_real_dst(p));
02559 }
02560
02561 static int get_transport_str2enum(const char *transport)
02562 {
02563 int res = 0;
02564
02565 if (ast_strlen_zero(transport)) {
02566 return res;
02567 }
02568
02569 if (!strcasecmp(transport, "udp")) {
02570 res |= SIP_TRANSPORT_UDP;
02571 }
02572 if (!strcasecmp(transport, "tcp")) {
02573 res |= SIP_TRANSPORT_TCP;
02574 }
02575 if (!strcasecmp(transport, "tls")) {
02576 res |= SIP_TRANSPORT_TLS;
02577 }
02578
02579 return res;
02580 }
02581
02582 static inline const char *get_transport_list(struct sip_peer *peer) {
02583 switch (peer->transports) {
02584 case SIP_TRANSPORT_UDP:
02585 return "UDP";
02586 case SIP_TRANSPORT_TCP:
02587 return "TCP";
02588 case SIP_TRANSPORT_TLS:
02589 return "TLS";
02590 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TCP:
02591 return "TCP,UDP";
02592 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TLS:
02593 return "TLS,UDP";
02594 case SIP_TRANSPORT_TCP | SIP_TRANSPORT_TLS:
02595 return "TLS,TCP";
02596 default:
02597 return peer->transports ?
02598 "TLS,TCP,UDP" : "UNKNOWN";
02599 }
02600 }
02601
02602 static inline const char *get_transport(enum sip_transport t)
02603 {
02604 switch (t) {
02605 case SIP_TRANSPORT_UDP:
02606 return "UDP";
02607 case SIP_TRANSPORT_TCP:
02608 return "TCP";
02609 case SIP_TRANSPORT_TLS:
02610 return "TLS";
02611 }
02612
02613 return "UNKNOWN";
02614 }
02615
02616 static inline const char *get_transport_pvt(struct sip_pvt *p)
02617 {
02618 if (p->outboundproxy && p->outboundproxy->transport) {
02619 set_socket_transport(&p->socket, p->outboundproxy->transport);
02620 }
02621
02622 return get_transport(p->socket.type);
02623 }
02624
02625
02626
02627
02628
02629
02630 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
02631 {
02632 int res = 0;
02633 const struct sockaddr_in *dst = sip_real_dst(p);
02634
02635 ast_debug(1, "Trying to put '%.10s' onto %s socket destined for %s:%d\n", data, get_transport_pvt(p), ast_inet_ntoa(dst->sin_addr), htons(dst->sin_port));
02636
02637 if (sip_prepare_socket(p) < 0)
02638 return XMIT_ERROR;
02639
02640 if (p->socket.tcptls_session)
02641 ast_mutex_lock(&p->socket.tcptls_session->lock);
02642
02643 if (p->socket.type & SIP_TRANSPORT_UDP) {
02644 res = sendto(p->socket.fd, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
02645 } else if (p->socket.tcptls_session) {
02646 if (p->socket.tcptls_session->f) {
02647 res = ast_tcptls_server_write(p->socket.tcptls_session, data, len);
02648 } else {
02649 ast_debug(2, "No p->socket.tcptls_session->f len=%d\n", len);
02650 }
02651 } else {
02652 ast_debug(2, "Socket type is TCP but no tcptls_session is present to write to\n");
02653 return XMIT_ERROR;
02654 }
02655
02656 if (p->socket.tcptls_session)
02657 ast_mutex_unlock(&p->socket.tcptls_session->lock);
02658
02659 if (res == -1) {
02660 switch (errno) {
02661 case EBADF:
02662 case EHOSTUNREACH:
02663 case ENETDOWN:
02664 case ENETUNREACH:
02665 case ECONNREFUSED:
02666 res = XMIT_ERROR;
02667 }
02668 }
02669 if (res != len)
02670 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));
02671
02672 return res;
02673 }
02674
02675
02676 static void build_via(struct sip_pvt *p)
02677 {
02678
02679 const char *rport = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_RFC3581 ? ";rport" : "";
02680
02681
02682 snprintf(p->via, sizeof(p->via), "SIP/2.0/%s %s:%d;branch=z9hG4bK%08x%s",
02683 get_transport_pvt(p),
02684 ast_inet_ntoa(p->ourip.sin_addr),
02685 ntohs(p->ourip.sin_port), (int) p->branch, rport);
02686 }
02687
02688
02689
02690
02691
02692
02693
02694
02695 static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us, struct sip_pvt *p)
02696 {
02697 struct sockaddr_in theirs;
02698
02699
02700
02701
02702
02703
02704
02705
02706
02707
02708
02709
02710 int want_remap;
02711
02712 *us = internip;
02713
02714 ast_ouraddrfor(them, &us->sin_addr);
02715 theirs.sin_addr = *them;
02716
02717 want_remap = localaddr &&
02718 (externip.sin_addr.s_addr || stunaddr.sin_addr.s_addr) &&
02719 ast_apply_ha(localaddr, &theirs) == AST_SENSE_ALLOW ;
02720
02721 if (want_remap &&
02722 (!global_matchexterniplocally || !ast_apply_ha(localaddr, us)) ) {
02723
02724 if (externexpire && time(NULL) >= externexpire) {
02725 if (stunaddr.sin_addr.s_addr) {
02726 ast_stun_request(sipsock, &stunaddr, NULL, &externip);
02727 } else {
02728 if (ast_parse_arg(externhost, PARSE_INADDR, &externip))
02729 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
02730 }
02731 externexpire = time(NULL) + externrefresh;
02732 }
02733 if (externip.sin_addr.s_addr)
02734 *us = externip;
02735 else
02736 ast_log(LOG_WARNING, "stun failed\n");
02737 ast_debug(1, "Target address %s is not local, substituting externip\n",
02738 ast_inet_ntoa(*(struct in_addr *)&them->s_addr));
02739 } else if (p) {
02740
02741 switch (p->socket.type) {
02742 case SIP_TRANSPORT_TCP:
02743 if (sip_tcp_desc.sin.sin_addr.s_addr) {
02744 *us = sip_tcp_desc.sin;
02745 } else {
02746 us->sin_port = sip_tcp_desc.sin.sin_port;
02747 }
02748 break;
02749 case SIP_TRANSPORT_TLS:
02750 if (sip_tls_desc.sin.sin_addr.s_addr) {
02751 *us = sip_tls_desc.sin;
02752 } else {
02753 us->sin_port = sip_tls_desc.sin.sin_port;
02754 }
02755 break;
02756 case SIP_TRANSPORT_UDP:
02757
02758 default:
02759 if (bindaddr.sin_addr.s_addr) {
02760 *us = bindaddr;
02761 }
02762 }
02763 } else if (bindaddr.sin_addr.s_addr) {
02764 *us = bindaddr;
02765 }
02766 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));
02767 }
02768
02769
02770 static __attribute__((format(printf, 2, 0))) void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
02771 {
02772 char buf[80], *c = buf;
02773 struct sip_history *hist;
02774 int l;
02775
02776 vsnprintf(buf, sizeof(buf), fmt, ap);
02777 strsep(&c, "\r\n");
02778 l = strlen(buf) + 1;
02779 if (!(hist = ast_calloc(1, sizeof(*hist) + l)))
02780 return;
02781 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
02782 ast_free(hist);
02783 return;
02784 }
02785 memcpy(hist->event, buf, l);
02786 if (p->history_entries == MAX_HISTORY_ENTRIES) {
02787 struct sip_history *oldest;
02788 oldest = AST_LIST_REMOVE_HEAD(p->history, list);
02789 p->history_entries--;
02790 ast_free(oldest);
02791 }
02792 AST_LIST_INSERT_TAIL(p->history, hist, list);
02793 p->history_entries++;
02794 }
02795
02796
02797 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
02798 {
02799 va_list ap;
02800
02801 if (!p)
02802 return;
02803
02804 if (!p->do_history && !recordhistory && !dumphistory)
02805 return;
02806
02807 va_start(ap, fmt);
02808 append_history_va(p, fmt, ap);
02809 va_end(ap);
02810
02811 return;
02812 }
02813
02814
02815 static int retrans_pkt(const void *data)
02816 {
02817 struct sip_pkt *pkt = (struct sip_pkt *)data, *prev, *cur = NULL;
02818 int reschedule = DEFAULT_RETRANS;
02819 int xmitres = 0;
02820
02821
02822 sip_pvt_lock(pkt->owner);
02823
02824 if (pkt->retrans < MAX_RETRANS) {
02825 pkt->retrans++;
02826 if (!pkt->timer_t1) {
02827 if (sipdebug)
02828 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);
02829 } else {
02830 int siptimer_a;
02831
02832 if (sipdebug)
02833 ast_debug(4, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
02834 if (!pkt->timer_a)
02835 pkt->timer_a = 2 ;
02836 else
02837 pkt->timer_a = 2 * pkt->timer_a;
02838
02839
02840 siptimer_a = pkt->timer_t1 * pkt->timer_a;
02841 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
02842 siptimer_a = 4000;
02843
02844
02845 reschedule = siptimer_a;
02846 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);
02847 }
02848
02849 if (sip_debug_test_pvt(pkt->owner)) {
02850 const struct sockaddr_in *dst = sip_real_dst(pkt->owner);
02851 ast_verbose("Retransmitting #%d (%s) to %s:%d:\n%s\n---\n",
02852 pkt->retrans, sip_nat_mode(pkt->owner),
02853 ast_inet_ntoa(dst->sin_addr),
02854 ntohs(dst->sin_port), pkt->data);
02855 }
02856
02857 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data);
02858 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02859 sip_pvt_unlock(pkt->owner);
02860 if (xmitres == XMIT_ERROR)
02861 ast_log(LOG_WARNING, "Network error on retransmit in dialog %s\n", pkt->owner->callid);
02862 else
02863 return reschedule;
02864 }
02865
02866 if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
02867 if (pkt->is_fatal || sipdebug)
02868 ast_log(LOG_WARNING, "Maximum retries exceeded on transmission %s for seqno %d (%s %s) -- See doc/sip-retransmit.txt.\n",
02869 pkt->owner->callid, pkt->seqno,
02870 pkt->is_fatal ? "Critical" : "Non-critical", pkt->is_resp ? "Response" : "Request");
02871 } else if (pkt->method == SIP_OPTIONS && sipdebug) {
02872 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) -- See doc/sip-retransmit.txt.\n", pkt->owner->callid);
02873
02874 }
02875 if (xmitres == XMIT_ERROR) {
02876 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission on Call ID %s\n", pkt->owner->callid);
02877 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
02878 } else
02879 append_history(pkt->owner, "MaxRetries", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
02880
02881 pkt->retransid = -1;
02882
02883 if (pkt->is_fatal) {
02884 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
02885 sip_pvt_unlock(pkt->owner);
02886 usleep(1);
02887 sip_pvt_lock(pkt->owner);
02888 }
02889
02890 if (pkt->owner->owner && !pkt->owner->owner->hangupcause)
02891 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
02892
02893 if (pkt->owner->owner) {
02894 sip_alreadygone(pkt->owner);
02895 ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet (see doc/sip-retransmit.txt).\n", pkt->owner->callid);
02896 ast_queue_hangup(pkt->owner->owner);
02897 ast_channel_unlock(pkt->owner->owner);
02898 } else {
02899
02900
02901
02902 if (pkt->method != SIP_OPTIONS && pkt->method != SIP_REGISTER) {
02903 pkt->owner->needdestroy = 1;
02904 sip_alreadygone(pkt->owner);
02905 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
02906 }
02907 }
02908 }
02909
02910 if (pkt->method == SIP_BYE) {
02911
02912 if (pkt->owner->owner)
02913 ast_channel_unlock(pkt->owner->owner);
02914 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
02915 pkt->owner->needdestroy = 1;
02916 }
02917
02918
02919 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
02920 if (cur == pkt) {
02921 UNLINK(cur, pkt->owner->packets, prev);
02922 sip_pvt_unlock(pkt->owner);
02923 ast_free(pkt);
02924 return 0;
02925 }
02926 }
02927
02928 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
02929 sip_pvt_unlock(pkt->owner);
02930 return 0;
02931 }
02932
02933
02934
02935
02936 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod)
02937 {
02938 struct sip_pkt *pkt = NULL;
02939 int siptimer_a = DEFAULT_RETRANS;
02940 int xmitres = 0;
02941 int respid;
02942
02943 if (sipmethod == SIP_INVITE) {
02944
02945 p->pendinginvite = seqno;
02946 }
02947
02948
02949
02950
02951 if (!(p->socket.type & SIP_TRANSPORT_UDP)) {
02952 xmitres = __sip_xmit(dialog_ref(p), data, len);
02953 if (xmitres == XMIT_ERROR) {
02954 append_history(p, "XmitErr", "%s", fatal ? "(Critical)" : "(Non-critical)");
02955 return AST_FAILURE;
02956 } else
02957 return AST_SUCCESS;
02958 }
02959
02960 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1)))
02961 return AST_FAILURE;
02962
02963 memcpy(pkt->data, data, len);
02964 pkt->data[len] = '\0';
02965 pkt->packetlen = len;
02966
02967 pkt->method = sipmethod;
02968 pkt->seqno = seqno;
02969 pkt->is_resp = resp;
02970 pkt->is_fatal = fatal;
02971 pkt->owner = dialog_ref(p);
02972 pkt->next = p->packets;
02973 p->packets = pkt;
02974 if (resp) {
02975
02976 if (sscanf(pkt->data, "SIP/2.0 %d", &respid) == 1) {
02977 pkt->response_code = respid;
02978 }
02979 }
02980 pkt->timer_t1 = p->timer_t1;
02981 pkt->retransid = -1;
02982 if (pkt->timer_t1)
02983 siptimer_a = pkt->timer_t1 * 2;
02984
02985
02986 AST_SCHED_REPLACE_VARIABLE(pkt->retransid, sched, siptimer_a, retrans_pkt, pkt, 1);
02987 if (sipdebug)
02988 ast_debug(4, "*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->retransid);
02989
02990 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02991
02992 if (xmitres == XMIT_ERROR) {
02993 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
02994 ast_log(LOG_ERROR, "Serious Network Trouble; __sip_xmit returns error for pkt data\n");
02995 AST_SCHED_DEL(sched, pkt->retransid);
02996 p->packets = pkt->next;
02997 ast_free(pkt);
02998 return AST_FAILURE;
02999 } else {
03000 return AST_SUCCESS;
03001 }
03002 }
03003
03004
03005
03006
03007
03008
03009 static int __sip_autodestruct(const void *data)
03010 {
03011 struct sip_pvt *p = (struct sip_pvt *)data;
03012
03013
03014 if (p->subscribed) {
03015 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
03016 p->subscribed = NONE;
03017 append_history(p, "Subscribestatus", "timeout");
03018 ast_debug(3, "Re-scheduled destruction of SIP subscription %s\n", p->callid ? p->callid : "<unknown>");
03019 return 10000;
03020 }
03021
03022
03023 if (p->packets) {
03024 ast_debug(3, "Re-scheduled destruction of SIP call %s\n", p->callid ? p->callid : "<unknown>");
03025 append_history(p, "ReliableXmit", "timeout");
03026 return 10000;
03027 }
03028
03029 if (p->subscribed == MWI_NOTIFICATION)
03030 if (p->relatedpeer)
03031 unref_peer(p->relatedpeer);
03032
03033
03034 p->autokillid = -1;
03035
03036 if (p->owner) {
03037 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
03038 ast_queue_hangup(p->owner);
03039 dialog_unref(p);
03040 } else if (p->refer && !p->alreadygone) {
03041 ast_debug(3, "Finally hanging up channel after transfer: %s\n", p->callid);
03042 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03043 append_history(p, "ReferBYE", "Sending BYE on transferer call leg %s", p->callid);
03044 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03045 dialog_unref(p);
03046 } else {
03047 append_history(p, "AutoDestroy", "%s", p->callid);
03048 ast_debug(3, "Auto destroying SIP dialog '%s'\n", p->callid);
03049 sip_destroy(p);
03050
03051 }
03052 return 0;
03053 }
03054
03055
03056 static void sip_scheddestroy(struct sip_pvt *p, int ms)
03057 {
03058 if (ms < 0) {
03059 if (p->timer_t1 == 0) {
03060 p->timer_t1 = global_t1;
03061 p->timer_b = global_timer_b;
03062 }
03063 ms = p->timer_t1 * 64;
03064 }
03065 if (sip_debug_test_pvt(p))
03066 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
03067 if (sip_cancel_destroy(p))
03068 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03069
03070 if (p->do_history)
03071 append_history(p, "SchedDestroy", "%d ms", ms);
03072 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, dialog_ref(p));
03073
03074 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_schedid > 0)
03075 stop_session_timer(p);
03076 }
03077
03078
03079
03080
03081
03082 static int sip_cancel_destroy(struct sip_pvt *p)
03083 {
03084 int res = 0;
03085 if (p->autokillid > -1) {
03086 if (!(res = ast_sched_del(sched, p->autokillid))) {
03087 append_history(p, "CancelDestroy", "");
03088 p->autokillid = -1;
03089 }
03090 dialog_unref(p);
03091 }
03092 return res;
03093 }
03094
03095
03096
03097 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
03098 {
03099 struct sip_pkt *cur, *prev = NULL;
03100 const char *msg = "Not Found";
03101
03102
03103
03104
03105
03106
03107 if (p->outboundproxy && !p->outboundproxy->force)
03108 p->outboundproxy = NULL;
03109
03110 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
03111 if (cur->seqno != seqno || cur->is_resp != resp)
03112 continue;
03113 if (cur->is_resp || cur->method == sipmethod) {
03114 msg = "Found";
03115 if (!resp && (seqno == p->pendinginvite)) {
03116 ast_debug(1, "Acked pending invite %d\n", p->pendinginvite);
03117 p->pendinginvite = 0;
03118 }
03119 if (cur->retransid > -1) {
03120 if (sipdebug)
03121 ast_debug(4, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
03122 }
03123
03124
03125
03126
03127
03128
03129
03130
03131
03132
03133
03134
03135
03136
03137
03138
03139 while (cur->retransid > -1 && ast_sched_del(sched, cur->retransid)) {
03140 sip_pvt_unlock(p);
03141 usleep(1);
03142 sip_pvt_lock(p);
03143 }
03144 UNLINK(cur, p->packets, prev);
03145 dialog_unref(cur->owner);
03146 ast_free(cur);
03147 break;
03148 }
03149 }
03150 ast_debug(1, "Stopping retransmission on '%s' of %s %d: Match %s\n",
03151 p->callid, resp ? "Response" : "Request", seqno, msg);
03152 }
03153
03154
03155
03156 static void __sip_pretend_ack(struct sip_pvt *p)
03157 {
03158 struct sip_pkt *cur = NULL;
03159
03160 while (p->packets) {
03161 int method;
03162 if (cur == p->packets) {
03163 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
03164 return;
03165 }
03166 cur = p->packets;
03167 method = (cur->method) ? cur->method : find_sip_method(cur->data);
03168 __sip_ack(p, cur->seqno, cur->is_resp, method);
03169 }
03170 }
03171
03172
03173 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
03174 {
03175 struct sip_pkt *cur;
03176 int res = -1;
03177
03178 for (cur = p->packets; cur; cur = cur->next) {
03179 if (cur->seqno == seqno && cur->is_resp == resp &&
03180 (cur->is_resp || method_match(sipmethod, cur->data))) {
03181
03182 if (cur->retransid > -1) {
03183 if (sipdebug)
03184 ast_debug(4, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
03185 }
03186 AST_SCHED_DEL(sched, cur->retransid);
03187 res = 0;
03188 break;
03189 }
03190 }
03191 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");
03192 return res;
03193 }
03194
03195
03196
03197 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
03198 {
03199 memset(dst, 0, sizeof(*dst));
03200 memcpy(dst->data, src->data, sizeof(dst->data));
03201 dst->len = src->len;
03202 parse_request(dst);
03203 }
03204
03205
03206 static void add_blank(struct sip_request *req)
03207 {
03208 if (!req->lines) {
03209
03210 ast_copy_string(req->data + req->len, "\r\n", sizeof(req->data) - req->len);
03211 req->len += strlen(req->data + req->len);
03212 }
03213 }
03214
03215
03216 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
03217 {
03218 int res;
03219
03220 add_blank(req);
03221 if (sip_debug_test_pvt(p)) {
03222 const struct sockaddr_in *dst = sip_real_dst(p);
03223
03224 ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
03225 reliable ? "Reliably " : "", sip_nat_mode(p),
03226 ast_inet_ntoa(dst->sin_addr),
03227 ntohs(dst->sin_port), req->data);
03228 }
03229 if (p->do_history) {
03230 struct sip_request tmp;
03231 parse_copy(&tmp, req);
03232 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"),
03233 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? tmp.rlPart2 : sip_methods[tmp.method].text);
03234 }
03235 res = (reliable) ?
03236 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
03237 __sip_xmit(p, req->data, req->len);
03238 if (res > 0)
03239 return 0;
03240 return res;
03241 }
03242
03243
03244 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
03245 {
03246 int res;
03247
03248
03249
03250
03251 if (p->outboundproxy) {
03252 p->sa = p->outboundproxy->ip;
03253 }
03254
03255 add_blank(req);
03256 if (sip_debug_test_pvt(p)) {
03257 if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
03258 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);
03259 else
03260 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);
03261 }
03262 if (p->do_history) {
03263 struct sip_request tmp;
03264 parse_copy(&tmp, req);
03265 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
03266 }
03267 res = (reliable) ?
03268 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
03269 __sip_xmit(p, req->data, req->len);
03270 return res;
03271 }
03272
03273
03274 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen)
03275 {
03276 int res = -1;
03277 enum ast_t38_state state = T38_STATE_UNAVAILABLE;
03278 struct sip_pvt *p = (struct sip_pvt *) chan->tech_pvt;
03279
03280 switch (option) {
03281 case AST_OPTION_T38_STATE:
03282
03283 if (*datalen != sizeof(enum ast_t38_state)) {
03284 ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option. Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
03285 return -1;
03286 }
03287
03288 sip_pvt_lock(p);
03289
03290
03291 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT)) {
03292 switch (p->t38.state) {
03293 case T38_LOCAL_REINVITE:
03294 case T38_PEER_DIRECT:
03295 case T38_PEER_REINVITE:
03296 state = T38_STATE_NEGOTIATING;
03297 break;
03298 case T38_ENABLED:
03299 state = T38_STATE_NEGOTIATED;
03300 break;
03301 default:
03302 state = T38_STATE_UNKNOWN;
03303 }
03304 }
03305
03306 sip_pvt_unlock(p);
03307
03308 *((enum ast_t38_state *) data) = state;
03309 res = 0;
03310
03311 break;
03312 default:
03313 break;
03314 }
03315
03316 return res;
03317 }
03318
03319
03320
03321
03322
03323 static const char *find_closing_quote(const char *start, const char *lim)
03324 {
03325 char last_char = '\0';
03326 const char *s;
03327 for (s = start; *s && s != lim; last_char = *s++) {
03328 if (*s == '"' && last_char != '\\')
03329 break;
03330 }
03331 return s;
03332 }
03333
03334
03335
03336
03337
03338
03339
03340
03341
03342
03343
03344
03345
03346 static char *get_in_brackets(char *tmp)
03347 {
03348 const char *parse = tmp;
03349 char *first_bracket;
03350
03351
03352
03353
03354
03355 while ( (first_bracket = strchr(parse, '<')) ) {
03356 char *first_quote = strchr(parse, '"');
03357
03358 if (!first_quote || first_quote > first_bracket)
03359 break;
03360
03361 parse = find_closing_quote(first_quote + 1, NULL);
03362 if (!*parse) {
03363
03364 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
03365 break;
03366 }
03367 parse++;
03368 }
03369 if (first_bracket) {
03370 char *second_bracket = strchr(first_bracket + 1, '>');
03371 if (second_bracket) {
03372 *second_bracket = '\0';
03373 tmp = first_bracket + 1;
03374 } else {
03375 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
03376 }
03377 }
03378
03379 return tmp;
03380 }
03381
03382
03383
03384
03385
03386
03387
03388
03389
03390
03391
03392
03393
03394
03395
03396
03397
03398
03399 static int parse_uri(char *uri, char *scheme,
03400 char **ret_name, char **pass, char **domain, char **port, char **options, char **transport)
03401 {
03402 char *name = NULL;
03403 int error = 0;
03404
03405
03406 if (pass)
03407 *pass = "";
03408 if (port)
03409 *port = "";
03410 if (scheme) {
03411 int l = strlen(scheme);
03412 if (!strncasecmp(uri, scheme, l))
03413 uri += l;
03414 else {
03415 ast_debug(1, "Missing scheme '%s' in '%s'\n", scheme, uri);
03416 error = -1;
03417 }
03418 }
03419 if (transport) {
03420 char *t, *type = "";
03421 *transport = "";
03422 if ((t = strstr(uri, "transport="))) {
03423 strsep(&t, "=");
03424 if ((type = strsep(&t, ";"))) {
03425 *transport = type;
03426 }
03427 }
03428 }
03429
03430 if (!domain) {
03431
03432
03433
03434 } else {
03435
03436
03437
03438 char *c, *dom = "";
03439
03440 if ((c = strchr(uri, '@')) == NULL) {
03441
03442 dom = uri;
03443 name = "";
03444 } else {
03445 *c++ = '\0';
03446 dom = c;
03447 name = uri;
03448 }
03449
03450
03451 dom = strsep(&dom, ";");
03452 name = strsep(&name, ";");
03453
03454 if (port && (c = strchr(dom, ':'))) {
03455 *c++ = '\0';
03456 *port = c;
03457 }
03458 if (pass && (c = strchr(name, ':'))) {
03459 *c++ = '\0';
03460 *pass = c;
03461 }
03462 *domain = dom;
03463 }
03464 if (ret_name)
03465 *ret_name = name;
03466 if (options)
03467 *options = uri ? uri : "";
03468
03469 return error;
03470 }
03471
03472
03473 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
03474 {
03475 struct sip_pvt *p = chan->tech_pvt;
03476
03477 if (subclass != AST_HTML_URL)
03478 return -1;
03479
03480 ast_string_field_build(p, url, "<%s>;mode=active", data);
03481
03482 if (sip_debug_test_pvt(p))
03483 ast_debug(1, "Send URL %s, state = %d!\n", data, chan->_state);
03484
03485 switch (chan->_state) {
03486 case AST_STATE_RING:
03487 transmit_response(p, "100 Trying", &p->initreq);
03488 break;
03489 case AST_STATE_RINGING:
03490 transmit_response(p, "180 Ringing", &p->initreq);
03491 break;
03492 case AST_STATE_UP:
03493 if (!p->pendinginvite) {
03494 transmit_reinvite_with_sdp(p, FALSE, FALSE);
03495 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
03496 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
03497 }
03498 break;
03499 default:
03500 ast_log(LOG_WARNING, "Don't know how to send URI when state is %d!\n", chan->_state);
03501 }
03502
03503 return 0;
03504 }
03505
03506
03507 static const char *sip_get_callid(struct ast_channel *chan)
03508 {
03509 return chan->tech_pvt ? ((struct sip_pvt *) chan->tech_pvt)->callid : "";
03510 }
03511
03512
03513
03514 static int sip_sendtext(struct ast_channel *ast, const char *text)
03515 {
03516 struct sip_pvt *p = ast->tech_pvt;
03517 int debug = sip_debug_test_pvt(p);
03518
03519 if (debug)
03520 ast_verbose("Sending text %s on %s\n", text, ast->name);
03521 if (!p)
03522 return -1;
03523
03524
03525 if (!text)
03526 return 0;
03527 if (debug)
03528 ast_verbose("Really sending text %s on %s\n", text, ast->name);
03529 transmit_message_with_text(p, text);
03530 return 0;
03531 }
03532
03533
03534
03535
03536
03537
03538 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *defaultuser, const char *fullcontact, int expirey, int deprecated_username, int lastms)
03539 {
03540 char port[10];
03541 char ipaddr[INET_ADDRSTRLEN];
03542 char regseconds[20];
03543 char *tablename = NULL;
03544 char str_lastms[20];
03545
03546 const char *sysname = ast_config_AST_SYSTEM_NAME;
03547 char *syslabel = NULL;
03548
03549 time_t nowtime = time(NULL) + expirey;
03550 const char *fc = fullcontact ? "fullcontact" : NULL;
03551
03552 int realtimeregs = ast_check_realtime("sipregs");
03553
03554 tablename = realtimeregs ? "sipregs" : "sippeers";
03555
03556
03557 snprintf(str_lastms, sizeof(str_lastms), "%d", lastms);
03558 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
03559 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
03560 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
03561
03562 if (ast_strlen_zero(sysname))
03563 sysname = NULL;
03564 else if (sip_cfg.rtsave_sysname)
03565 syslabel = "regserver";
03566
03567 if (fc)
03568 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
03569 "port", port, "regseconds", regseconds,
03570 deprecated_username ? "username" : "defaultuser", defaultuser, fc, fullcontact, syslabel, sysname, NULL);
03571 else
03572 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
03573 "port", port, "regseconds", regseconds,
03574 deprecated_username ? "username" : "defaultuser", defaultuser, syslabel, sysname, NULL);
03575
03576
03577 ast_update_realtime(tablename, "name", peername, "lastms", str_lastms, NULL);
03578 }
03579
03580
03581 static void register_peer_exten(struct sip_peer *peer, int onoff)
03582 {
03583 char multi[256];
03584 char *stringp, *ext, *context;
03585 struct pbx_find_info q = { .stacklen = 0 };
03586
03587
03588
03589
03590
03591 if (ast_strlen_zero(global_regcontext))
03592 return;
03593
03594 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
03595 stringp = multi;
03596 while ((ext = strsep(&stringp, "&"))) {
03597 if ((context = strchr(ext, '@'))) {
03598 *context++ = '\0';
03599 if (!ast_context_find(context)) {
03600 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
03601 continue;
03602 }
03603 } else {
03604 context = global_regcontext;
03605 }
03606 if (onoff) {
03607 if (!ast_exists_extension(NULL, context, ext, 1, NULL)) {
03608 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
03609 ast_strdup(peer->name), ast_free_ptr, "SIP");
03610 }
03611 } else if (pbx_find_extension(NULL, NULL, &q, context, ext, 1, NULL, "", E_MATCH)) {
03612 ast_context_remove_extension(context, ext, 1, NULL);
03613 }
03614 }
03615 }
03616
03617
03618 static void destroy_mailbox(struct sip_mailbox *mailbox)
03619 {
03620 if (mailbox->mailbox)
03621 ast_free(mailbox->mailbox);
03622 if (mailbox->context)
03623 ast_free(mailbox->context);
03624 if (mailbox->event_sub)
03625 ast_event_unsubscribe(mailbox->event_sub);
03626 ast_free(mailbox);
03627 }
03628
03629
03630 static void clear_peer_mailboxes(struct sip_peer *peer)
03631 {
03632 struct sip_mailbox *mailbox;
03633
03634 while ((mailbox = AST_LIST_REMOVE_HEAD(&peer->mailboxes, entry)))
03635 destroy_mailbox(mailbox);
03636 }
03637
03638
03639 static void sip_destroy_peer(struct sip_peer *peer)
03640 {
03641 ast_debug(3, "Destroying SIP peer %s\n", peer->name);
03642
03643 if (peer->outboundproxy)
03644 ast_free(peer->outboundproxy);
03645 peer->outboundproxy = NULL;
03646
03647
03648 if (peer->call)
03649 peer->call = sip_destroy(peer->call);
03650
03651 if (peer->mwipvt)
03652 peer->mwipvt = sip_destroy(peer->mwipvt);
03653
03654 if (peer->chanvars) {
03655 ast_variables_destroy(peer->chanvars);
03656 peer->chanvars = NULL;
03657 }
03658
03659
03660
03661
03662
03663
03664
03665 AST_SCHED_DEL(sched, peer->expire);
03666 AST_SCHED_DEL(sched, peer->pokeexpire);
03667
03668 register_peer_exten(peer, FALSE);
03669 ast_free_ha(peer->ha);
03670 if (peer->selfdestruct)
03671 apeerobjs--;
03672 else if (peer->is_realtime) {
03673 rpeerobjs--;
03674 ast_debug(3, "-REALTIME- peer Destroyed. Name: %s. Realtime Peer objects: %d\n", peer->name, rpeerobjs);
03675 } else
03676 speerobjs--;
03677 clear_realm_authentication(peer->auth);
03678 peer->auth = NULL;
03679 if (peer->dnsmgr)
03680 ast_dnsmgr_release(peer->dnsmgr);
03681 clear_peer_mailboxes(peer);
03682
03683 if (peer->socket.tcptls_session) {
03684 ao2_ref(peer->socket.tcptls_session, -1);
03685 peer->socket.tcptls_session = NULL;
03686 }
03687 ast_free(peer);
03688 }
03689
03690
03691 static void update_peer(struct sip_peer *p, int expiry)
03692 {
03693 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
03694 if (sip_cfg.peer_rtupdate &&
03695 (p->is_realtime || rtcachefriends)) {
03696 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, expiry, p->deprecated_username, p->lastms);
03697 }
03698 }
03699
03700 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *config)
03701 {
03702 struct ast_variable *var = NULL;
03703 struct ast_flags flags = {0};
03704 char *cat = NULL;
03705 const char *insecure;
03706 while ((cat = ast_category_browse(config, cat))) {
03707 insecure = ast_variable_retrieve(config, cat, "insecure");
03708 set_insecure_flags(&flags, insecure, -1);
03709 if (ast_test_flag(&flags, SIP_INSECURE_PORT)) {
03710 var = ast_category_root(config, cat);
03711 break;
03712 }
03713 }
03714 return var;
03715 }
03716
03717 static const char *get_name_from_variable(struct ast_variable *var, const char *newpeername)
03718 {
03719 struct ast_variable *tmp;
03720 for (tmp = var; tmp; tmp = tmp->next) {
03721 if (!newpeername && !strcasecmp(tmp->name, "name"))
03722 newpeername = tmp->value;
03723 }
03724 return newpeername;
03725 }
03726
03727
03728
03729
03730
03731 static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_in *sin, int devstate_only)
03732 {
03733 struct sip_peer *peer;
03734 struct ast_variable *var = NULL;
03735 struct ast_variable *varregs = NULL;
03736 struct ast_variable *tmp;
03737 struct ast_config *peerlist = NULL;
03738 char ipaddr[INET_ADDRSTRLEN];
03739 char portstring[6];
03740 char *cat = NULL;
03741 unsigned short portnum;
03742 int realtimeregs = ast_check_realtime("sipregs");
03743
03744
03745 if (newpeername) {
03746 if (realtimeregs)
03747 varregs = ast_load_realtime("sipregs", "name", newpeername, NULL);
03748
03749 var = ast_load_realtime("sippeers", "name", newpeername, "host", "dynamic", NULL);
03750 if (!var && sin)
03751 var = ast_load_realtime("sippeers", "name", newpeername, "host", ast_inet_ntoa(sin->sin_addr), NULL);
03752 if (!var) {
03753 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
03754
03755
03756
03757
03758
03759
03760 if (var && sin) {
03761 for (tmp = var; tmp; tmp = tmp->next) {
03762 if (!strcasecmp(tmp->name, "host")) {
03763 struct hostent *hp;
03764 struct ast_hostent ahp;
03765 if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(&hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
03766
03767 ast_variables_destroy(var);
03768 var = NULL;
03769 }
03770 break;
03771 }
03772 }
03773 }
03774 }
03775 }
03776
03777 if (!var && sin) {
03778 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
03779 portnum = ntohs(sin->sin_port);
03780 sprintf(portstring, "%u", portnum);
03781 var = ast_load_realtime("sippeers", "host", ipaddr, "port", portstring, NULL);
03782 if (var) {
03783 if (realtimeregs) {
03784 newpeername = get_name_from_variable(var, newpeername);
03785 varregs = ast_load_realtime("sipregs", "name", newpeername, NULL);
03786 }
03787 } else {
03788 if (realtimeregs)
03789 varregs = ast_load_realtime("sipregs", "ipaddr", ipaddr, "port", portstring, NULL);
03790 else
03791 var = ast_load_realtime("sippeers", "ipaddr", ipaddr, "port", portstring, NULL);
03792 if (varregs) {
03793 newpeername = get_name_from_variable(varregs, newpeername);
03794 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
03795 }
03796 }
03797 if (!var) {
03798 peerlist = ast_load_realtime_multientry("sippeers", "host", ipaddr, NULL);
03799 if (peerlist) {
03800 var = get_insecure_variable_from_config(peerlist);
03801 if(var) {
03802 if (realtimeregs) {
03803 newpeername = get_name_from_variable(var, newpeername);
03804 varregs = ast_load_realtime("sipregs", "name", newpeername, NULL);
03805 }
03806 } else {
03807 peerlist = NULL;
03808 cat = NULL;
03809 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", ipaddr, NULL);
03810 if(peerlist) {
03811 var = get_insecure_variable_from_config(peerlist);
03812 if(var) {
03813 if (realtimeregs) {
03814 newpeername = get_name_from_variable(var, newpeername);
03815 varregs = ast_load_realtime("sipregs", "name", newpeername, NULL);
03816 }
03817 }
03818 }
03819 }
03820 } else {
03821 if (realtimeregs) {
03822 peerlist = ast_load_realtime_multientry("sipregs", "ipaddr", ipaddr, NULL);
03823 if (peerlist) {
03824 varregs = get_insecure_variable_from_config(peerlist);
03825 if (varregs) {
03826 newpeername = get_name_from_variable(varregs, newpeername);
03827 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
03828 }
03829 }
03830 } else {
03831 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", ipaddr, NULL);
03832 if (peerlist) {
03833 var = get_insecure_variable_from_config(peerlist);
03834 if (var) {
03835 newpeername = get_name_from_variable(var, newpeername);
03836 varregs = ast_load_realtime("sipregs", "name", newpeername, NULL);
03837 }
03838 }
03839 }
03840 }
03841 }
03842 }
03843
03844 if (!var) {
03845 if (peerlist)
03846 ast_config_destroy(peerlist);
03847 return NULL;
03848 }
03849
03850 for (tmp = var; tmp; tmp = tmp->next) {
03851
03852 if (!strcasecmp(tmp->name, "type") &&
03853 !strcasecmp(tmp->value, "user")) {
03854 if(peerlist)
03855 ast_config_destroy(peerlist);
03856 else {
03857 ast_variables_destroy(var);
03858 ast_variables_destroy(varregs);
03859 }
03860 return NULL;
03861 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
03862 newpeername = tmp->value;
03863 }
03864 }
03865
03866 if (!newpeername) {
03867 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", ipaddr);
03868 if(peerlist)
03869 ast_config_destroy(peerlist);
03870 else
03871 ast_variables_destroy(var);
03872 return NULL;
03873 }
03874
03875
03876
03877 peer = build_peer(newpeername, var, varregs, 1);
03878 if (!peer) {
03879 if(peerlist)
03880 ast_config_destroy(peerlist);
03881 else {
03882 ast_variables_destroy(var);
03883 ast_variables_destroy(varregs);
03884 }
03885 return NULL;
03886 }
03887
03888 ast_debug(3, "-REALTIME- loading peer from database to memory. Name: %s. Peer objects: %d\n", peer->name, rpeerobjs);
03889
03890 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && !devstate_only) {
03891
03892 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
03893 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
03894 AST_SCHED_REPLACE(peer->expire, sched, global_rtautoclear * 1000, expire_register, (void *) peer);
03895 }
03896 ASTOBJ_CONTAINER_LINK(&peerl, peer);
03897 }
03898 peer->is_realtime = 1;
03899 if (peerlist)
03900 ast_config_destroy(peerlist);
03901 else {
03902 ast_variables_destroy(var);
03903 ast_variables_destroy(varregs);
03904 }
03905
03906 return peer;
03907 }
03908
03909
03910 static int sip_addrcmp(char *name, struct sockaddr_in *sin)
03911 {
03912
03913 struct sip_peer *p = (struct sip_peer *) name;
03914 return !(!inaddrcmp(&p->addr, sin) ||
03915 (ast_test_flag(&p->flags[0], SIP_INSECURE_PORT) &&
03916 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
03917 }
03918
03919
03920
03921
03922
03923
03924
03925 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int devstate_only)
03926 {
03927 struct sip_peer *p = NULL;
03928
03929 if (peer)
03930 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
03931 else
03932 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, name, sip_addr_hashfunc, 1, sip_addrcmp);
03933
03934 if (!p && realtime)
03935 p = realtime_peer(peer, sin, devstate_only);
03936
03937 return p;
03938 }
03939
03940
03941 static void sip_destroy_user(struct sip_user *user)
03942 {
03943 ast_debug(3, "Destroying user object from memory: %s\n", user->name);
03944
03945 ast_free_ha(user->ha);
03946 if (user->chanvars) {
03947 ast_variables_destroy(user->chanvars);
03948 user->chanvars = NULL;
03949 }
03950 if (user->is_realtime)
03951 ruserobjs--;
03952 else
03953 suserobjs--;
03954 ast_free(user);
03955 }
03956
03957
03958
03959
03960 static struct sip_user *realtime_user(const char *username)
03961 {
03962 struct ast_variable *var;
03963 struct ast_variable *tmp;
03964 struct sip_user *user = NULL;
03965
03966 var = ast_load_realtime("sipusers", "name", username, NULL);
03967
03968 if (!var)
03969 return NULL;
03970
03971 for (tmp = var; tmp; tmp = tmp->next) {
03972 if (!strcasecmp(tmp->name, "type") &&
03973 !strcasecmp(tmp->value, "peer")) {
03974 ast_variables_destroy(var);
03975 return NULL;
03976 }
03977 }
03978
03979 user = build_user(username, var, NULL, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
03980
03981 if (!user) {
03982 ast_variables_destroy(var);
03983 return NULL;
03984 }
03985
03986 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
03987 ast_set_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
03988 suserobjs++;
03989 ASTOBJ_CONTAINER_LINK(&userl, user);
03990 } else {
03991
03992 suserobjs--;
03993 ruserobjs++;
03994 user->is_realtime = 1;
03995 }
03996 ast_variables_destroy(var);
03997 return user;
03998 }
03999
04000
04001
04002
04003
04004 static struct sip_user *find_user(const char *name, int realtime)
04005 {
04006 struct sip_user *u = ASTOBJ_CONTAINER_FIND(&userl, name);
04007 if (!u && realtime)
04008 u = realtime_user(name);
04009 return u;
04010 }
04011
04012
04013 static void do_setnat(struct sip_pvt *p, int natflags)
04014 {
04015 const char *mode = natflags ? "On" : "Off";
04016
04017 if (p->rtp) {
04018 ast_debug(1, "Setting NAT on RTP to %s\n", mode);
04019 ast_rtp_setnat(p->rtp, natflags);
04020 }
04021 if (p->vrtp) {
04022 ast_debug(1, "Setting NAT on VRTP to %s\n", mode);
04023 ast_rtp_setnat(p->vrtp, natflags);
04024 }
04025 if (p->udptl) {
04026 ast_debug(1, "Setting NAT on UDPTL to %s\n", mode);
04027 ast_udptl_setnat(p->udptl, natflags);
04028 }
04029 if (p->trtp) {
04030 ast_debug(1, "Setting NAT on TRTP to %s\n", mode);
04031 ast_rtp_setnat(p->trtp, natflags);
04032 }
04033 }
04034
04035
04036 static void change_t38_state(struct sip_pvt *p, int state)
04037 {
04038 int old = p->t38.state;
04039 struct ast_channel *chan = p->owner;
04040 enum ast_control_t38 message = 0;
04041
04042
04043 if (old == state)
04044 return;
04045
04046 if (state == T38_PEER_DIRECT) {
04047 p->t38.direct = 1;
04048 }
04049
04050 p->t38.state = state;
04051 ast_debug(2, "T38 state changed to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
04052
04053
04054 if (!chan)
04055 return;
04056
04057
04058 if (state == T38_PEER_REINVITE)
04059 message = AST_T38_REQUEST_NEGOTIATE;
04060 else if (state == T38_ENABLED)
04061 message = AST_T38_NEGOTIATED;
04062 else if (state == T38_DISABLED && old == T38_ENABLED)
04063 message = AST_T38_TERMINATED;
04064 else if (state == T38_DISABLED && old == T38_LOCAL_REINVITE)
04065 message = AST_T38_REFUSED;
04066
04067
04068 if (message)
04069 ast_queue_control_data(chan, AST_CONTROL_T38, &message, sizeof(message));
04070 }
04071
04072
04073 static void set_t38_capabilities(struct sip_pvt *p)
04074 {
04075 p->t38.capability = global_t38_capability;
04076 if (p->udptl) {
04077 if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_FEC )
04078 p->t38.capability |= T38FAX_UDP_EC_FEC;
04079 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY )
04080 p->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
04081 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_NONE )
04082 p->t38.capability |= T38FAX_UDP_EC_NONE;
04083 p->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
04084 }
04085 }
04086
04087 static void copy_socket_data(struct sip_socket *to_sock, const struct sip_socket *from_sock)
04088 {
04089 if (to_sock->tcptls_session) {
04090 ao2_ref(to_sock->tcptls_session, -1);
04091 to_sock->tcptls_session = NULL;
04092 }
04093
04094 if (from_sock->tcptls_session) {
04095 ao2_ref(from_sock->tcptls_session, +1);
04096 }
04097
04098 *to_sock = *from_sock;
04099 }
04100
04101
04102
04103
04104
04105
04106
04107 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
04108 {
04109
04110
04111
04112 if (dialog->socket.type && check_request_transport(peer, dialog))
04113 return -1;
04114 copy_socket_data(&dialog->socket, &peer->socket);
04115
04116 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
04117 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
04118 dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
04119 dialog->recv = dialog->sa;
04120 } else
04121 return -1;
04122
04123 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
04124 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
04125 dialog->capability = peer->capability;
04126 if ((!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(dialog->capability & AST_FORMAT_VIDEO_MASK)) && dialog->vrtp) {
04127 ast_rtp_destroy(dialog->vrtp);
04128 dialog->vrtp = NULL;
04129 }
04130 if (!ast_test_flag(&dialog->flags[1], SIP_PAGE2_TEXTSUPPORT) && dialog->trtp) {
04131 ast_rtp_destroy(dialog->trtp);
04132 dialog->trtp = NULL;
04133 }
04134 dialog->prefs = peer->prefs;
04135 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
04136 if (!dialog->udptl) {
04137
04138 dialog->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
04139 }
04140 ast_copy_flags(&dialog->t38.t38support, &peer->flags[1], SIP_PAGE2_T38SUPPORT);
04141 set_t38_capabilities(dialog);
04142 dialog->t38.jointcapability = dialog->t38.capability;
04143 } else if (dialog->udptl) {
04144 ast_udptl_destroy(dialog->udptl);
04145 dialog->udptl = NULL;
04146 }
04147 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04148
04149 if (dialog->rtp) {
04150 ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04151 ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04152 ast_rtp_set_rtptimeout(dialog->rtp, peer->rtptimeout);
04153 ast_rtp_set_rtpholdtimeout(dialog->rtp, peer->rtpholdtimeout);
04154 ast_rtp_set_rtpkeepalive(dialog->rtp, peer->rtpkeepalive);
04155
04156 ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
04157 dialog->autoframing = peer->autoframing;
04158 }
04159 if (dialog->vrtp) {
04160 ast_rtp_setdtmf(dialog->vrtp, 0);
04161 ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
04162 ast_rtp_set_rtptimeout(dialog->vrtp, peer->rtptimeout);
04163 ast_rtp_set_rtpholdtimeout(dialog->vrtp, peer->rtpholdtimeout);
04164 ast_rtp_set_rtpkeepalive(dialog->vrtp, peer->rtpkeepalive);
04165 }
04166 if (dialog->trtp) {
04167 ast_rtp_setdtmf(dialog->trtp, 0);
04168 ast_rtp_setdtmfcompensate(dialog->trtp, 0);
04169 ast_rtp_set_rtptimeout(dialog->trtp, peer->rtptimeout);
04170 ast_rtp_set_rtpholdtimeout(dialog->trtp, peer->rtpholdtimeout);
04171 ast_rtp_set_rtpkeepalive(dialog->trtp, peer->rtpkeepalive);
04172 }
04173
04174 ast_string_field_set(dialog, peername, peer->name);
04175 ast_string_field_set(dialog, authname, peer->username);
04176 ast_string_field_set(dialog, username, peer->username);
04177 ast_string_field_set(dialog, peersecret, peer->secret);
04178 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
04179 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
04180 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
04181 ast_string_field_set(dialog, tohost, peer->tohost);
04182 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
04183 ast_string_field_set(dialog, context, peer->context);
04184 dialog->outboundproxy = obproxy_get(dialog, peer);
04185 dialog->callgroup = peer->callgroup;
04186 dialog->pickupgroup = peer->pickupgroup;
04187 dialog->allowtransfer = peer->allowtransfer;
04188 dialog->jointnoncodeccapability = dialog->noncodeccapability;
04189 dialog->rtptimeout = peer->rtptimeout;
04190 dialog->peerauth = peer->auth;
04191 dialog->maxcallbitrate = peer->maxcallbitrate;
04192 if (ast_strlen_zero(dialog->tohost))
04193 ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
04194 if (!ast_strlen_zero(peer->fromdomain)) {
04195 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
04196 if (!dialog->initreq.headers) {
04197 char *c;
04198 char *tmpcall = ast_strdupa(dialog->callid);
04199
04200 c = strchr(tmpcall, '@');
04201 if (c) {
04202 *c = '\0';
04203 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
04204 }
04205 }
04206 }
04207 if (!ast_strlen_zero(peer->fromuser))
04208 ast_string_field_set(dialog, fromuser, peer->fromuser);
04209 if (!ast_strlen_zero(peer->language))
04210 ast_string_field_set(dialog, language, peer->language);
04211
04212
04213
04214
04215 if (peer->maxms && peer->lastms)
04216 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
04217 else
04218 dialog->timer_t1 = peer->timer_t1;
04219
04220
04221
04222 if (peer->timer_b)
04223 dialog->timer_b = peer->timer_b;
04224 else
04225 dialog->timer_b = 64 * dialog->timer_t1;
04226
04227 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
04228 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
04229 dialog->noncodeccapability |= AST_RTP_DTMF;
04230 else
04231 dialog->noncodeccapability &= ~AST_RTP_DTMF;
04232 if (peer->call_limit)
04233 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
04234
04235 return 0;
04236 }
04237
04238
04239
04240
04241 static int create_addr(struct sip_pvt *dialog, const char *opeer, int newdialog)
04242 {
04243 struct hostent *hp;
04244 struct ast_hostent ahp;
04245 struct sip_peer *peer;
04246 char *port;
04247 int portno = 0;
04248 char host[MAXHOSTNAMELEN], *hostn;
04249 char peername[256];
04250
04251 ast_copy_string(peername, opeer, sizeof(peername));
04252 port = strchr(peername, ':');
04253 if (port)
04254 *port++ = '\0';
04255 dialog->sa.sin_family = AF_INET;
04256 dialog->timer_t1 = global_t1;
04257 dialog->timer_b = global_timer_b;
04258 peer = find_peer(peername, NULL, 1, 0);
04259
04260 if (peer) {
04261 int res;
04262 if (newdialog) {
04263 set_socket_transport(&dialog->socket, 0);
04264 }
04265 res = create_addr_from_peer(dialog, peer);
04266 unref_peer(peer);
04267 if (!ast_strlen_zero(port)) {
04268 if ((portno = atoi(port))) {
04269 dialog->sa.sin_port = dialog->recv.sin_port = htons(portno);
04270 }
04271 }
04272 return res;
04273 }
04274
04275 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04276
04277 ast_string_field_set(dialog, tohost, peername);
04278
04279
04280 dialog->outboundproxy = obproxy_get(dialog, NULL);
04281
04282
04283
04284
04285
04286
04287
04288 hostn = peername;
04289
04290
04291
04292 if (!port && global_srvlookup) {
04293 char service[MAXHOSTNAMELEN];
04294 int tportno;
04295 int ret;
04296
04297 snprintf(service, sizeof(service), "_sip._%s.%s", get_transport(dialog->socket.type), peername);
04298 ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
04299 if (ret > 0) {
04300 hostn = host;
04301 portno = tportno;
04302 }
04303 }
04304 if (!portno)
04305 portno = port ? atoi(port) : (dialog->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT;
04306 hp = ast_gethostbyname(hostn, &ahp);
04307 if (!hp) {
04308 ast_log(LOG_WARNING, "No such host: %s\n", peername);
04309 return -1;
04310 }
04311 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
04312 if (ast_strlen_zero(port) || sscanf(port, "%u", &portno) != 1) {
04313 portno = (dialog->socket.type & SIP_TRANSPORT_TLS) ?
04314 STANDARD_TLS_PORT : STANDARD_SIP_PORT;
04315 }
04316
04317 if (!dialog->socket.type)
04318 set_socket_transport(&dialog->socket, SIP_TRANSPORT_UDP);
04319 if (!dialog->socket.port)
04320 dialog->socket.port = bindaddr.sin_port;
04321
04322 dialog->sa.sin_port = htons(portno);
04323 dialog->recv = dialog->sa;
04324 return 0;
04325 }
04326
04327
04328
04329
04330 static int auto_congest(const void *arg)
04331 {
04332 struct sip_pvt *p = (struct sip_pvt *)arg;
04333
04334 sip_pvt_lock(p);
04335 p->initid = -1;
04336 if (p->owner) {
04337
04338 if (!ast_channel_trylock(p->owner)) {
04339 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
04340 append_history(p, "Cong", "Auto-congesting (timer)");
04341 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
04342 ast_channel_unlock(p->owner);
04343 }
04344 }
04345 sip_pvt_unlock(p);
04346 dialog_unref(p);
04347 return 0;
04348 }
04349
04350
04351
04352
04353 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
04354 {
04355 int res;
04356 struct sip_pvt *p = ast->tech_pvt;
04357 struct varshead *headp;
04358 struct ast_var_t *current;
04359 const char *referer = NULL;
04360
04361 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
04362 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
04363 return -1;
04364 }
04365
04366
04367 headp=&ast->varshead;
04368 AST_LIST_TRAVERSE(headp, current, entries) {
04369
04370 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
04371 p->options->vxml_url = ast_var_value(current);
04372 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
04373 p->options->uri_options = ast_var_value(current);
04374 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
04375
04376 p->options->addsipheaders = 1;
04377 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
04378
04379 p->options->transfer = 1;
04380 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
04381
04382 referer = ast_var_value(current);
04383 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
04384
04385 p->options->replaces = ast_var_value(current);
04386 }
04387 }
04388
04389 res = 0;
04390 ast_set_flag(&p->flags[0], SIP_OUTGOING);
04391
04392 if (p->options->transfer) {
04393 char buf[SIPBUFSIZE/2];
04394
04395 if (referer) {
04396 if (sipdebug)
04397 ast_debug(3, "Call for %s transfered by %s\n", p->username, referer);
04398 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
04399 } else
04400 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
04401 ast_string_field_set(p, cid_name, buf);
04402 }
04403 ast_debug(1, "Outgoing Call for %s\n", p->username);
04404
04405 res = update_call_counter(p, INC_CALL_RINGING);
04406
04407 if (res == -1) {
04408 ast->hangupcause = AST_CAUSE_USER_BUSY;
04409 return res;
04410 }
04411 p->callingpres = ast->cid.cid_pres;
04412 p->jointcapability = ast_translate_available_formats(p->capability, p->prefcodec);
04413 p->jointnoncodeccapability = p->noncodeccapability;
04414
04415
04416 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
04417 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
04418 res = -1;
04419 } else {
04420 int xmitres;
04421
04422 p->t38.jointcapability = p->t38.capability;
04423 ast_debug(2, "Our T38 capability (%d), joint T38 capability (%d)\n", p->t38.capability, p->t38.jointcapability);
04424
04425 xmitres = transmit_invite(p, SIP_INVITE, 1, 2);
04426 if (xmitres == XMIT_ERROR)
04427 return -1;
04428 p->invitestate = INV_CALLING;
04429
04430
04431 AST_SCHED_REPLACE(p->initid, sched, p->timer_b, auto_congest, dialog_ref(p));
04432 }
04433
04434 return res;
04435 }
04436
04437
04438
04439 static void sip_registry_destroy(struct sip_registry *reg)
04440 {
04441
04442 ast_debug(3, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
04443
04444 if (reg->call) {
04445
04446
04447 reg->call->registry = NULL;
04448 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
04449 reg->call = sip_destroy(reg->call);
04450 }
04451 AST_SCHED_DEL(sched, reg->expire);
04452 AST_SCHED_DEL(sched, reg->timeout);
04453 ast_string_field_free_memory(reg);
04454 regobjs--;
04455 ast_free(reg);
04456
04457 }
04458
04459
04460 static int __sip_destroy(struct sip_pvt *p, int lockowner, int lockdialoglist)
04461 {
04462 struct sip_pvt *cur, *prev = NULL;
04463 struct sip_pkt *cp;
04464 struct sip_request *req;
04465
04466
04467 if (p->rtp && ast_rtp_get_bridged(p->rtp)) {
04468 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
04469 return -1;
04470 }
04471
04472 if (p->vrtp && ast_rtp_get_bridged(p->vrtp)) {
04473 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
04474 return -1;
04475 }
04476
04477 if (sip_debug_test_pvt(p))
04478 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
04479
04480 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
04481 update_call_counter(p, DEC_CALL_LIMIT);
04482 ast_debug(2, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
04483 }
04484
04485
04486 if (p->owner) {
04487 if (lockowner)
04488 ast_channel_lock(p->owner);
04489 if (option_debug)
04490 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
04491 p->owner->tech_pvt = NULL;
04492
04493 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
04494 if (lockowner)
04495 ast_channel_unlock(p->owner);
04496
04497 usleep(1);
04498 }
04499
04500
04501 if (p->relatedpeer && p->relatedpeer->mwipvt == p)
04502 p->relatedpeer->mwipvt = dialog_unref(p->relatedpeer->mwipvt);
04503
04504 if (dumphistory)
04505 sip_dump_history(p);
04506
04507 if (p->options)
04508 ast_free(p->options);
04509
04510 if (p->stateid > -1)
04511 ast_extension_state_del(p->stateid, NULL);
04512 AST_SCHED_DEL(sched, p->initid);
04513 AST_SCHED_DEL(sched, p->waitid);
04514 AST_SCHED_DEL(sched, p->autokillid);
04515 AST_SCHED_DEL(sched, p->request_queue_sched_id);
04516 AST_SCHED_DEL(sched, p->t38id);
04517
04518 if (p->rtp) {
04519 ast_rtp_destroy(p->rtp);
04520 }
04521 if (p->vrtp) {
04522 ast_rtp_destroy(p->vrtp);
04523 }
04524 if (p->trtp) {
04525 while (ast_rtp_get_bridged(p->trtp))
04526 usleep(1);
04527 ast_rtp_destroy(p->trtp);
04528 }
04529 if (p->udptl)
04530 ast_udptl_destroy(p->udptl);
04531 if (p->refer)
04532 ast_free(p->refer);
04533 if (p->route) {
04534 free_old_route(p->route);
04535 p->route = NULL;
04536 }
04537 if (p->registry) {
04538 if (p->registry->call == p)
04539 p->registry->call = NULL;
04540 p->registry = registry_unref(p->registry);
04541 }
04542
04543
04544 if (p->stimer) {
04545 if (p->stimer->st_active == TRUE && p->stimer->st_schedid > -1)
04546 ast_sched_del(sched, p->stimer->st_schedid);
04547 ast_free(p->stimer);
04548 }
04549
04550
04551 if (p->history) {
04552 struct sip_history *hist;
04553 while ( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) ) {
04554 ast_free(hist);
04555 p->history_entries--;
04556 }
04557 ast_free(p->history);
04558 p->history = NULL;
04559 }
04560
04561 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
04562 ast_free(req);
04563 }
04564
04565
04566 if (lockdialoglist)
04567 dialoglist_lock();
04568 for (prev = NULL, cur = dialoglist; cur; prev = cur, cur = cur->next) {
04569 if (cur == p) {
04570 UNLINK(cur, dialoglist, prev);
04571 break;
04572 }
04573 }
04574 if (lockdialoglist)
04575 dialoglist_unlock();
04576 if (!cur) {
04577 ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
04578 return 0;
04579 }
04580
04581
04582 while((cp = p->packets)) {
04583 p->packets = p->packets->next;
04584 AST_SCHED_DEL(sched, cp->retransid);
04585 dialog_unref(cp->owner);
04586 ast_free(cp);
04587 }
04588 if (p->chanvars) {
04589 ast_variables_destroy(p->chanvars);
04590 p->chanvars = NULL;
04591 }
04592 ast_mutex_destroy(&p->pvt_lock);
04593
04594 ast_string_field_free_memory(p);
04595
04596 if (p->socket.tcptls_session) {
04597 ao2_ref(p->socket.tcptls_session, -1);
04598 p->socket.tcptls_session = NULL;
04599 }
04600
04601 ast_free(p);
04602 return 0;
04603 }
04604
04605
04606
04607
04608
04609
04610
04611
04612
04613
04614
04615
04616
04617
04618
04619 static int update_call_counter(struct sip_pvt *fup, int event)
04620 {
04621 char name[256];
04622 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
04623 int outgoing = fup->outgoing_call;
04624 struct sip_user *u = NULL;
04625 struct sip_peer *p = NULL;
04626 ast_mutex_t *pu_lock = NULL;
04627
04628 ast_debug(3, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
04629
04630
04631
04632
04633 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
04634 return 0;
04635
04636 ast_copy_string(name, fup->username, sizeof(name));
04637
04638
04639 if (global_limitonpeers == FALSE && !outgoing && (u = find_user(name, 1))) {
04640 inuse = &u->inUse;
04641 call_limit = &u->call_limit;
04642 inringing = NULL;
04643 pu_lock = &u->_lock;
04644 } else if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, 1, 0) ) ) {
04645 inuse = &p->inUse;
04646 call_limit = &p->call_limit;
04647 inringing = &p->inRinging;
04648 ast_copy_string(name, fup->peername, sizeof(name));
04649 pu_lock = &p->_lock;
04650 }
04651 if (!p && !u) {
04652 ast_debug(2, "%s is not a local device, no call limit\n", name);
04653 return 0;
04654 }
04655
04656 switch(event) {
04657
04658 case DEC_CALL_LIMIT:
04659
04660 if (inuse) {
04661 sip_pvt_lock(fup);
04662 ast_mutex_lock(pu_lock);
04663 if (*inuse > 0) {
04664 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
04665 (*inuse)--;
04666 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
04667 }
04668 } else {
04669 *inuse = 0;
04670 }
04671 ast_mutex_unlock(pu_lock);
04672 sip_pvt_unlock(fup);
04673 }
04674
04675
04676 if (inringing) {
04677 sip_pvt_lock(fup);
04678 ast_mutex_lock(pu_lock);
04679 if (*inringing > 0) {
04680 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
04681 (*inringing)--;
04682 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
04683 }
04684 } else {
04685 *inringing = 0;
04686 }
04687 ast_mutex_unlock(pu_lock);
04688 sip_pvt_unlock(fup);
04689 }
04690
04691
04692 sip_pvt_lock(fup);
04693 ast_mutex_lock(pu_lock);
04694 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold) {
04695 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
04696 ast_mutex_unlock(pu_lock);
04697 sip_pvt_unlock(fup);
04698 sip_peer_hold(fup, FALSE);
04699 } else {
04700 ast_mutex_unlock(pu_lock);
04701 sip_pvt_unlock(fup);
04702 }
04703 if (sipdebug)
04704 ast_debug(2, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
04705 break;
04706
04707 case INC_CALL_RINGING:
04708 case INC_CALL_LIMIT:
04709
04710 if (*call_limit > 0 ) {
04711 if (*inuse >= *call_limit) {
04712 ast_log(LOG_ERROR, "Call %s %s '%s' rejected due to usage limit of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
04713 if (u)
04714 unref_user(u);
04715 else
04716 unref_peer(p);
04717 return -1;
04718 }
04719 }
04720 if (inringing && (event == INC_CALL_RINGING)) {
04721 sip_pvt_lock(fup);
04722 ast_mutex_lock(pu_lock);
04723 if (!ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
04724 (*inringing)++;
04725 ast_set_flag(&fup->flags[0], SIP_INC_RINGING);
04726 }
04727 ast_mutex_unlock(pu_lock);
04728 sip_pvt_unlock(fup);
04729 }
04730 if (inuse) {
04731 sip_pvt_lock(fup);
04732 ast_mutex_lock(pu_lock);
04733 if (!ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
04734 (*inuse)++;
04735 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
04736 }
04737 ast_mutex_unlock(pu_lock);
04738 sip_pvt_unlock(fup);
04739 }
04740 if (sipdebug) {
04741 ast_debug(2, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
04742 }
04743 break;
04744
04745 case DEC_CALL_RINGING:
04746 if (inringing) {
04747 sip_pvt_lock(fup);
04748 ast_mutex_lock(pu_lock);
04749 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
04750 if (*inringing > 0) {
04751 (*inringing)--;
04752 }
04753 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
04754 }
04755 ast_mutex_unlock(pu_lock);
04756 sip_pvt_unlock(fup);
04757 }
04758 break;
04759
04760 default:
04761 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
04762 }
04763
04764 if (p) {
04765 ast_device_state_changed("SIP/%s", p->name);
04766 unref_peer(p);
04767 } else
04768 unref_user(u);
04769 return 0;
04770 }
04771
04772
04773
04774
04775
04776
04777 static struct sip_pvt * sip_destroy(struct sip_pvt *p)
04778 {
04779 ast_debug(3, "Destroying SIP dialog %s\n", p->callid);
04780 __sip_destroy(p, TRUE, TRUE);
04781 return NULL;
04782 }
04783
04784
04785 static int hangup_sip2cause(int cause)
04786 {
04787
04788
04789 switch(cause) {
04790 case 401:
04791 return AST_CAUSE_CALL_REJECTED;
04792 case 403:
04793 return AST_CAUSE_CALL_REJECTED;
04794 case 404:
04795 return AST_CAUSE_UNALLOCATED;
04796 case 405:
04797 return AST_CAUSE_INTERWORKING;
04798 case 407:
04799 return AST_CAUSE_CALL_REJECTED;
04800 case 408:
04801 return AST_CAUSE_NO_USER_RESPONSE;
04802 case 409:
04803 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
04804 case 410:
04805 return AST_CAUSE_NUMBER_CHANGED;
04806 case 411:
04807 return AST_CAUSE_INTERWORKING;
04808 case 413:
04809 return AST_CAUSE_INTERWORKING;
04810 case 414:
04811 return AST_CAUSE_INTERWORKING;
04812 case 415:
04813 return AST_CAUSE_INTERWORKING;
04814 case 420:
04815 return AST_CAUSE_NO_ROUTE_DESTINATION;
04816 case 480:
04817 return AST_CAUSE_NO_ANSWER;
04818 case 481:
04819 return AST_CAUSE_INTERWORKING;
04820 case 482:
04821 return AST_CAUSE_INTERWORKING;
04822 case 483:
04823 return AST_CAUSE_NO_ANSWER;
04824 case 484:
04825 return AST_CAUSE_INVALID_NUMBER_FORMAT;
04826 case 485:
04827 return AST_CAUSE_UNALLOCATED;
04828 case 486:
04829 return AST_CAUSE_BUSY;
04830 case 487:
04831 return AST_CAUSE_INTERWORKING;
04832 case 488:
04833 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
04834 case 491:
04835 return AST_CAUSE_INTERWORKING;
04836 case 493:
04837 return AST_CAUSE_INTERWORKING;
04838 case 500:
04839 return AST_CAUSE_FAILURE;
04840 case 501:
04841 return AST_CAUSE_FACILITY_REJECTED;
04842 case 502:
04843 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
04844 case 503:
04845 return AST_CAUSE_CONGESTION;
04846 case 504:
04847 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
04848 case 505:
04849 return AST_CAUSE_INTERWORKING;
04850 case 600:
04851 return AST_CAUSE_USER_BUSY;
04852 case 603:
04853 return AST_CAUSE_CALL_REJECTED;
04854 case 604:
04855 return AST_CAUSE_UNALLOCATED;
04856 case 606:
04857 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
04858 default:
04859 return AST_CAUSE_NORMAL;
04860 }
04861
04862 return 0;
04863 }
04864
04865
04866
04867
04868
04869
04870
04871
04872
04873
04874
04875
04876
04877
04878
04879
04880
04881
04882
04883
04884
04885
04886
04887
04888
04889
04890
04891
04892
04893
04894
04895
04896
04897 static const char *hangup_cause2sip(int cause)
04898 {
04899 switch (cause) {
04900 case AST_CAUSE_UNALLOCATED:
04901 case AST_CAUSE_NO_ROUTE_DESTINATION:
04902 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
04903 return "404 Not Found";
04904 case AST_CAUSE_CONGESTION:
04905 case AST_CAUSE_SWITCH_CONGESTION:
04906 return "503 Service Unavailable";
04907 case AST_CAUSE_NO_USER_RESPONSE:
04908 return "408 Request Timeout";
04909 case AST_CAUSE_NO_ANSWER:
04910 case AST_CAUSE_UNREGISTERED:
04911 return "480 Temporarily unavailable";
04912 case AST_CAUSE_CALL_REJECTED:
04913 return "403 Forbidden";
04914 case AST_CAUSE_NUMBER_CHANGED:
04915 return "410 Gone";
04916 case AST_CAUSE_NORMAL_UNSPECIFIED:
04917 return "480 Temporarily unavailable";
04918 case AST_CAUSE_INVALID_NUMBER_FORMAT:
04919 return "484 Address incomplete";
04920 case AST_CAUSE_USER_BUSY:
04921 return "486 Busy here";
04922 case AST_CAUSE_FAILURE:
04923 return "500 Server internal failure";
04924 case AST_CAUSE_FACILITY_REJECTED:
04925 return "501 Not Implemented";
04926 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
04927 return "503 Service Unavailable";
04928
04929 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
04930 return "502 Bad Gateway";
04931 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
04932 return "488 Not Acceptable Here";
04933
04934 case AST_CAUSE_NOTDEFINED:
04935 default:
04936 ast_debug(1, "AST hangup cause %d (no match found in SIP)\n", cause);
04937 return NULL;
04938 }
04939
04940
04941 return 0;
04942 }
04943
04944
04945
04946
04947 static int sip_hangup(struct ast_channel *ast)
04948 {
04949 struct sip_pvt *p = ast->tech_pvt;
04950 int needcancel = FALSE;
04951 int needdestroy = 0;
04952 struct ast_channel *oldowner = ast;
04953
04954 if (!p) {
04955 ast_debug(1, "Asked to hangup channel that was not connected\n");
04956 return 0;
04957 }
04958 if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
04959 ast_debug(1, "This call was answered elsewhere");
04960 append_history(p, "Cancel", "Call answered elsewhere");
04961 p->answered_elsewhere = TRUE;
04962 }
04963
04964
04965 if (p->owner)
04966 p->hangupcause = p->owner->hangupcause;
04967
04968 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
04969 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
04970 if (sipdebug)
04971 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
04972 update_call_counter(p, DEC_CALL_LIMIT);
04973 }
04974 ast_debug(4, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
04975 if (p->autokillid > -1 && sip_cancel_destroy(p))
04976 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
04977 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
04978 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
04979 p->needdestroy = 0;
04980 p->owner->tech_pvt = dialog_unref(p->owner->tech_pvt);
04981 p->owner = NULL;
04982 return 0;
04983 }
04984
04985 if (ast_test_flag(ast, AST_FLAG_ZOMBIE)) {
04986 if (p->refer)
04987 ast_debug(1, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
04988 else
04989 ast_debug(1, "Hanging up zombie call. Be scared.\n");
04990 } else
04991 ast_debug(1, "Hangup call %s, SIP callid %s\n", ast->name, p->callid);
04992
04993 sip_pvt_lock(p);
04994 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
04995 if (sipdebug)
04996 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
04997 update_call_counter(p, DEC_CALL_LIMIT);
04998 }
04999
05000
05001 if (p->owner != ast) {
05002 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
05003 sip_pvt_unlock(p);
05004 return 0;
05005 }
05006
05007
05008 if (p->invitestate < INV_COMPLETED && p->owner->_state != AST_STATE_UP) {
05009 needcancel = TRUE;
05010 ast_debug(4, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
05011 }
05012
05013 stop_media_flows(p);
05014
05015 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", p->owner ? ast_cause2str(p->hangupcause) : "Unknown");
05016
05017
05018 if (p->vad)
05019 ast_dsp_free(p->vad);
05020
05021 p->owner = NULL;
05022 ast->tech_pvt = dialog_unref(ast->tech_pvt);
05023
05024 ast_module_unref(ast_module_info->self);
05025
05026
05027
05028
05029
05030
05031 if (p->alreadygone)
05032 needdestroy = 1;
05033 else if (p->invitestate != INV_CALLING)
05034 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05035
05036
05037 if (!p->alreadygone && !ast_strlen_zero(p->initreq.data)) {
05038 if (needcancel) {
05039 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05040
05041 __sip_pretend_ack(p);
05042
05043
05044 if (p->invitestate == INV_CALLING) {
05045
05046 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
05047
05048 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05049 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
05050 } else {
05051 p->invitestate = INV_CANCELLED;
05052
05053 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
05054
05055
05056 needdestroy = 0;
05057 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05058 }
05059 } else {
05060 const char *res;
05061 if (p->hangupcause && (res = hangup_cause2sip(p->hangupcause)))
05062 transmit_response_reliable(p, res, &p->initreq);
05063 else
05064 transmit_response_reliable(p, "603 Declined", &p->initreq);
05065 p->invitestate = INV_TERMINATED;
05066 }
05067 } else {
05068 if (p->stimer->st_active == TRUE) {
05069 stop_session_timer(p);
05070 }
05071
05072 if (!p->pendinginvite) {
05073 char *audioqos = "";
05074 char *videoqos = "";
05075 char *textqos = "";
05076 if (p->rtp)
05077 audioqos = ast_rtp_get_quality(p->rtp, NULL);
05078 if (p->vrtp)
05079 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
05080 if (p->trtp)
05081 textqos = ast_rtp_get_quality(p->trtp, NULL);
05082
05083 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
05084
05085
05086 if (p->do_history) {
05087 if (p->rtp)
05088 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
05089 if (p->vrtp)
05090 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
05091 if (p->trtp)
05092 append_history(p, "RTCPtext", "Quality:%s", textqos);
05093 }
05094 if (p->rtp && oldowner)
05095 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", audioqos);
05096 if (p->vrtp && oldowner)
05097 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", videoqos);
05098 if (p->trtp && oldowner)
05099 pbx_builtin_setvar_helper(oldowner, "RTPTEXTQOS", textqos);
05100 } else {
05101
05102
05103 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
05104 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
05105 AST_SCHED_DEL(sched, p->waitid);
05106 if (sip_cancel_destroy(p))
05107 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
05108 }
05109 }
05110 }
05111 if (needdestroy)
05112 p->needdestroy = 1;
05113 sip_pvt_unlock(p);
05114 return 0;
05115 }
05116
05117
05118 static void try_suggested_sip_codec(struct sip_pvt *p)
05119 {
05120 int fmt;
05121 const char *codec;
05122
05123 while (p->owner && ast_channel_trylock(p->owner)) {
05124 sip_pvt_unlock(p);
05125 sched_yield();
05126 sip_pvt_lock(p);
05127 }
05128
05129 if (!p->owner)
05130 return;
05131
05132 codec = ast_strdupa(S_OR(pbx_builtin_getvar_helper(p->owner, "SIP_CODEC"), ""));
05133
05134 ast_channel_unlock(p->owner);
05135
05136 if (ast_strlen_zero(codec))
05137 return;
05138
05139 fmt = ast_getformatbyname(codec);
05140 if (fmt) {
05141 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
05142 if (p->jointcapability & fmt) {
05143 p->jointcapability &= fmt;
05144 p->capability &= fmt;
05145 } else
05146 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
05147 } else
05148 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
05149 return;
05150 }
05151
05152
05153
05154 static int sip_answer(struct ast_channel *ast)
05155 {
05156 int res = 0;
05157 struct sip_pvt *p = ast->tech_pvt;
05158
05159 sip_pvt_lock(p);
05160 if (ast->_state != AST_STATE_UP) {
05161 try_suggested_sip_codec(p);
05162
05163 ast_setstate(ast, AST_STATE_UP);
05164 ast_debug(1, "SIP answering channel: %s\n", ast->name);
05165 if (p->t38.state == T38_PEER_DIRECT) {
05166 change_t38_state(p, T38_ENABLED);
05167 }
05168 ast_rtp_new_source(p->rtp);
05169 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
05170 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL, FALSE);
05171 }
05172 sip_pvt_unlock(p);
05173 return res;
05174 }
05175
05176
05177 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
05178 {
05179 struct sip_pvt *p = ast->tech_pvt;
05180 int res = 0;
05181
05182 switch (frame->frametype) {
05183 case AST_FRAME_VOICE:
05184 if (!(frame->subclass & ast->nativeformats)) {
05185 char s1[512], s2[512], s3[512];
05186 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %s(%d) read/write = %s(%d)/%s(%d)\n",
05187 frame->subclass,
05188 ast_getformatname_multiple(s1, sizeof(s1) - 1, ast->nativeformats & AST_FORMAT_AUDIO_MASK),
05189 ast->nativeformats & AST_FORMAT_AUDIO_MASK,
05190 ast_getformatname_multiple(s2, sizeof(s2) - 1, ast->readformat),
05191 ast->readformat,
05192 ast_getformatname_multiple(s3, sizeof(s3) - 1, ast->writeformat),
05193 ast->writeformat);
05194 return 0;
05195 }
05196 if (p) {
05197 sip_pvt_lock(p);
05198 if (p->rtp) {
05199
05200 if ((ast->_state != AST_STATE_UP) &&
05201 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05202 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05203 ast_rtp_new_source(p->rtp);
05204 p->invitestate = INV_EARLY_MEDIA;
05205 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
05206 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
05207 } else if (p->t38.state == T38_ENABLED && !p->t38.direct) {
05208 change_t38_state(p, T38_DISABLED);
05209 transmit_reinvite_with_sdp(p, FALSE, FALSE);
05210 } else {
05211 p->lastrtptx = time(NULL);
05212 res = ast_rtp_write(p->rtp, frame);
05213 }
05214 }
05215 sip_pvt_unlock(p);
05216 }
05217 break;
05218 case AST_FRAME_VIDEO:
05219 if (p) {
05220 sip_pvt_lock(p);
05221 if (p->vrtp) {
05222
05223 if ((ast->_state != AST_STATE_UP) &&
05224 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05225 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05226 p->invitestate = INV_EARLY_MEDIA;
05227 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
05228 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
05229 }
05230 p->lastrtptx = time(NULL);
05231 res = ast_rtp_write(p->vrtp, frame);
05232 }
05233 sip_pvt_unlock(p);
05234 }
05235 break;
05236 case AST_FRAME_TEXT:
05237 if (p) {
05238 sip_pvt_lock(p);
05239 if (p->trtp) {
05240
05241 if ((ast->_state != AST_STATE_UP) &&
05242 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05243 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05244 p->invitestate = INV_EARLY_MEDIA;
05245 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
05246 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
05247 }
05248 p->lastrtptx = time(NULL);
05249 res = ast_rtp_write(p->trtp, frame);
05250 }
05251 sip_pvt_unlock(p);
05252 }
05253 break;
05254 case AST_FRAME_IMAGE:
05255 return 0;
05256 break;
05257 case AST_FRAME_MODEM:
05258 if (p) {
05259 sip_pvt_lock(p);
05260
05261
05262
05263
05264 if (ast->_state == AST_STATE_UP) {
05265 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && p->t38.state == T38_DISABLED) {
05266 if (!p->pendinginvite) {
05267 change_t38_state(p, T38_LOCAL_REINVITE);
05268 transmit_reinvite_with_sdp(p, TRUE, FALSE);
05269 }
05270 } else if (p->udptl && p->t38.state == T38_ENABLED) {
05271 res = ast_udptl_write(p->udptl, frame);
05272 }
05273 }
05274 sip_pvt_unlock(p);
05275 }
05276 break;
05277 default:
05278 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
05279 return 0;
05280 }
05281
05282 return res;
05283 }
05284
05285
05286
05287 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
05288 {
05289 int ret = -1;
05290 struct sip_pvt *p;
05291
05292 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE))
05293 ast_debug(1, "New channel is zombie\n");
05294 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE))
05295 ast_debug(1, "Old channel is zombie\n");
05296
05297 if (!newchan || !newchan->tech_pvt) {
05298 if (!newchan)
05299 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
05300 else
05301 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
05302 return -1;
05303 }
05304 p = newchan->tech_pvt;
05305
05306 sip_pvt_lock(p);
05307 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
05308 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
05309 if (p->owner != oldchan)
05310 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
05311 else {
05312 p->owner = newchan;
05313
05314
05315
05316
05317
05318
05319 sip_set_rtp_peer(newchan, NULL, NULL, 0, 0, 0);
05320 ret = 0;
05321 }
05322 ast_debug(3, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
05323
05324 sip_pvt_unlock(p);
05325 return ret;
05326 }
05327
05328 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
05329 {
05330 struct sip_pvt *p = ast->tech_pvt;
05331 int res = 0;
05332
05333 sip_pvt_lock(p);
05334 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
05335 case SIP_DTMF_INBAND:
05336 res = -1;
05337 break;
05338 case SIP_DTMF_RFC2833:
05339 if (p->rtp)
05340 ast_rtp_senddigit_begin(p->rtp, digit);
05341 break;
05342 default:
05343 break;
05344 }
05345 sip_pvt_unlock(p);
05346
05347 return res;
05348 }
05349
05350
05351
05352 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
05353 {
05354 struct sip_pvt *p = ast->tech_pvt;
05355 int res = 0;
05356
05357 sip_pvt_lock(p);
05358 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
05359 case SIP_DTMF_INFO:
05360 case SIP_DTMF_SHORTINFO:
05361 transmit_info_with_digit(p, digit, duration);
05362 break;
05363 case SIP_DTMF_RFC2833:
05364 if (p->rtp)
05365 ast_rtp_senddigit_end(p->rtp, digit);
05366 break;
05367 case SIP_DTMF_INBAND:
05368 res = -1;
05369 break;
05370 }
05371 sip_pvt_unlock(p);
05372
05373 return res;
05374 }
05375
05376
05377 static int sip_transfer(struct ast_channel *ast, const char *dest)
05378 {
05379 struct sip_pvt *p = ast->tech_pvt;
05380 int res;
05381
05382 if (dest == NULL)
05383 dest = "";
05384 sip_pvt_lock(p);
05385 if (ast->_state == AST_STATE_RING)
05386 res = sip_sipredirect(p, dest);
05387 else
05388 res = transmit_refer(p, dest);
05389 sip_pvt_unlock(p);
05390 return res;
05391 }
05392
05393
05394
05395
05396
05397
05398 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
05399 {
05400 struct sip_pvt *p = ast->tech_pvt;
05401 int res = 0;
05402
05403 sip_pvt_lock(p);
05404 switch(condition) {
05405 case AST_CONTROL_RINGING:
05406 if (ast->_state == AST_STATE_RING) {
05407 p->invitestate = INV_EARLY_MEDIA;
05408 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
05409 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
05410
05411 transmit_response(p, "180 Ringing", &p->initreq);
05412 ast_set_flag(&p->flags[0], SIP_RINGING);
05413 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
05414 break;
05415 } else {
05416
05417 }
05418 }
05419 res = -1;
05420 break;
05421 case AST_CONTROL_BUSY:
05422 if (ast->_state != AST_STATE_UP) {
05423 transmit_response_reliable(p, "486 Busy Here", &p->initreq);
05424 p->invitestate = INV_COMPLETED;
05425 sip_alreadygone(p);
05426 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
05427 break;
05428 }
05429 res = -1;
05430 break;
05431 case AST_CONTROL_CONGESTION:
05432 if (ast->_state != AST_STATE_UP) {
05433 transmit_response_reliable(p, "503 Service Unavailable", &p->initreq);
05434 p->invitestate = INV_COMPLETED;
05435 sip_alreadygone(p);
05436 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
05437 break;
05438 }
05439 res = -1;
05440 break;
05441 case AST_CONTROL_PROCEEDING:
05442 if ((ast->_state != AST_STATE_UP) &&
05443 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05444 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05445 transmit_response(p, "100 Trying", &p->initreq);
05446 p->invitestate = INV_PROCEEDING;
05447 break;
05448 }
05449 res = -1;
05450 break;
05451 case AST_CONTROL_PROGRESS:
05452 if ((ast->_state != AST_STATE_UP) &&
05453 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
05454 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05455 p->invitestate = INV_EARLY_MEDIA;
05456 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
05457 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
05458 break;
05459 }
05460 res = -1;
05461 break;
05462 case AST_CONTROL_HOLD:
05463 ast_rtp_new_source(p->rtp);
05464 ast_moh_start(ast, data, p->mohinterpret);
05465 break;
05466 case AST_CONTROL_UNHOLD:
05467 ast_rtp_new_source(p->rtp);
05468 ast_moh_stop(ast);
05469 break;
05470 case AST_CONTROL_VIDUPDATE:
05471 if (p->vrtp && !p->novideo) {
05472 transmit_info_with_vidupdate(p);
05473
05474 } else
05475 res = -1;
05476 break;
05477 case AST_CONTROL_T38:
05478 if (datalen != sizeof(enum ast_control_t38)) {
05479 ast_log(LOG_ERROR, "Invalid datalen for AST_CONTROL_T38. Expected %d, got %d\n", (int)sizeof(enum ast_control_t38), (int)datalen);
05480 } else {
05481 switch (*((enum ast_control_t38 *) data)) {
05482 case AST_T38_NEGOTIATED:
05483 case AST_T38_REQUEST_NEGOTIATE:
05484 if (p->t38.state == T38_PEER_REINVITE) {
05485 AST_SCHED_DEL(sched, p->t38id);
05486 change_t38_state(p, T38_ENABLED);
05487 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
05488 } else if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT) && p->t38.state != T38_ENABLED) {
05489 change_t38_state(p, T38_LOCAL_REINVITE);
05490 if (!p->pendinginvite) {
05491 transmit_reinvite_with_sdp(p, TRUE, FALSE);
05492 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
05493 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
05494 }
05495 }
05496 break;
05497 case AST_T38_TERMINATED:
05498 case AST_T38_REFUSED:
05499 case AST_T38_REQUEST_TERMINATE:
05500 if (p->t38.state == T38_PEER_REINVITE) {
05501 AST_SCHED_DEL(sched, p->t38id);
05502 change_t38_state(p, T38_DISABLED);
05503 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
05504 } else if (p->t38.state == T38_ENABLED)
05505 transmit_reinvite_with_sdp(p, FALSE, FALSE);
05506 break;
05507 default:
05508 break;
05509 }
05510 }
05511 break;
05512 case AST_CONTROL_SRCUPDATE:
05513 ast_rtp_new_source(p->rtp);
05514 break;
05515 case -1:
05516 res = -1;
05517 break;
05518 default:
05519 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
05520 res = -1;
05521 break;
05522 }
05523 sip_pvt_unlock(p);
05524 return res;
05525 }
05526
05527
05528
05529
05530
05531
05532
05533 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
05534 {
05535 struct ast_channel *tmp;
05536 struct ast_variable *v = NULL;
05537 int fmt;
05538 int what;
05539 int video;
05540 int text;
05541 int needvideo = 0;
05542 int needtext = 0;
05543 char buf[SIPBUFSIZE];
05544 char *decoded_exten;
05545
05546 {
05547 const char *my_name;
05548
05549 if (title)
05550 my_name = title;
05551 else if ( (my_name = strchr(i->fromdomain, ':')) )
05552 my_name++;
05553 else
05554 my_name = i->fromdomain;
05555
05556 sip_pvt_unlock(i);
05557
05558 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, (int)(long) i);
05559
05560 }
05561 if (!tmp) {
05562 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
05563 sip_pvt_lock(i);
05564 return NULL;
05565 }
05566 sip_pvt_lock(i);
05567
05568 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;
05569
05570
05571
05572 if (i->jointcapability) {
05573 what = i->jointcapability;
05574 video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
05575 text = i->jointcapability & AST_FORMAT_TEXT_MASK;
05576 } else if (i->capability) {
05577 what = i->capability;
05578 video = i->capability & AST_FORMAT_VIDEO_MASK;
05579 text = i->capability & AST_FORMAT_TEXT_MASK;
05580 } else {
05581 what = global_capability;
05582 video = global_capability & AST_FORMAT_VIDEO_MASK;
05583 text = global_capability & AST_FORMAT_TEXT_MASK;
05584 }
05585
05586
05587 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video | text;
05588 ast_debug(3, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
05589 ast_debug(3, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
05590 ast_debug(3, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
05591 ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
05592 if (i->prefcodec)
05593 ast_debug(3, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
05594
05595
05596 fmt = ast_best_codec(tmp->nativeformats);
05597
05598
05599
05600
05601
05602 if (i->vrtp) {
05603 if (i->prefcodec)
05604 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
05605 else
05606 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
05607 }
05608
05609 if (i->trtp) {
05610 if (i->prefcodec)
05611 needtext = i->prefcodec & AST_FORMAT_TEXT_MASK;
05612 else
05613 needtext = i->jointcapability & AST_FORMAT_TEXT_MASK;
05614 }
05615
05616 if (needvideo)
05617 ast_debug(3, "This channel can handle video! HOLLYWOOD next!\n");
05618 else
05619 ast_debug(3, "This channel will not be able to handle video.\n");
05620
05621 if ((ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) || (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
05622 i->vad = ast_dsp_new();
05623 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
05624 if (global_relaxdtmf)
05625 ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
05626 }
05627
05628
05629 if (i->rtp) {
05630 ast_channel_set_fd(tmp, 0, ast_rtp_fd(i->rtp));
05631 ast_channel_set_fd(tmp, 1, ast_rtcp_fd(i->rtp));
05632 }
05633 if (needvideo && i->vrtp) {
05634 ast_channel_set_fd(tmp, 2, ast_rtp_fd(i->vrtp));
05635 ast_channel_set_fd(tmp, 3, ast_rtcp_fd(i->vrtp));
05636 }
05637 if (needtext && i->trtp)
05638 ast_channel_set_fd(tmp, 4, ast_rtp_fd(i->trtp));
05639 if (i->udptl)
05640 ast_channel_set_fd(tmp, 5, ast_udptl_fd(i->udptl));
05641
05642 if (state == AST_STATE_RING)
05643 tmp->rings = 1;
05644 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
05645 tmp->writeformat = fmt;
05646 tmp->rawwriteformat = fmt;
05647 tmp->readformat = fmt;
05648 tmp->rawreadformat = fmt;
05649 tmp->tech_pvt = dialog_ref(i);
05650
05651 tmp->callgroup = i->callgroup;
05652 tmp->pickupgroup = i->pickupgroup;
05653 tmp->cid.cid_pres = i->callingpres;
05654 if (!ast_strlen_zero(i->accountcode))
05655 ast_string_field_set(tmp, accountcode, i->accountcode);
05656 if (i->amaflags)
05657 tmp->amaflags = i->amaflags;
05658 if (!ast_strlen_zero(i->language))
05659 ast_string_field_set(tmp, language, i->language);
05660 i->owner = tmp;
05661 ast_module_ref(ast_module_info->self);
05662 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
05663
05664
05665
05666
05667 decoded_exten = ast_strdupa(i->exten);
05668 ast_uri_decode(decoded_exten);
05669 ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
05670
05671
05672
05673 tmp->cid.cid_ani = ast_strdup(i->cid_num);
05674 if (!ast_strlen_zero(i->rdnis))
05675 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
05676
05677 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
05678 tmp->cid.cid_dnid = ast_strdup(i->exten);
05679
05680 tmp->priority = 1;
05681 if (!ast_strlen_zero(i->uri))
05682 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
05683 if (!ast_strlen_zero(i->domain))
05684 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
05685 if (!ast_strlen_zero(i->callid))
05686 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
05687 if (i->rtp)
05688 ast_jb_configure(tmp, &global_jbconf);
05689
05690
05691 for (v = i->chanvars ; v ; v = v->next)
05692 pbx_builtin_setvar_helper(tmp, v->name, v->value);
05693
05694 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
05695 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05696 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
05697 ast_hangup(tmp);
05698 tmp = NULL;
05699 }
05700
05701 if (i->do_history)
05702 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
05703
05704
05705 if (global_callevents)
05706 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
05707 "Channel: %s\r\nUniqueid: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\n",
05708 tmp->name, tmp->uniqueid, "SIP", i->callid, i->fullcontact);
05709
05710 return tmp;
05711 }
05712
05713
05714 static char *get_body_by_line(const char *line, const char *name, int nameLen)
05715 {
05716 if (!strncasecmp(line, name, nameLen) && line[nameLen] == '=')
05717 return ast_skip_blanks(line + nameLen + 1);
05718
05719 return "";
05720 }
05721
05722
05723
05724
05725
05726 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
05727 {
05728 int len = strlen(name);
05729
05730 while (*start < req->sdp_end) {
05731 const char *r = get_body_by_line(req->line[(*start)++], name, len);
05732 if (r[0] != '\0')
05733 return r;
05734 }
05735
05736 return "";
05737 }
05738
05739
05740 static const char *get_sdp(struct sip_request *req, const char *name)
05741 {
05742 int dummy = 0;
05743
05744 return get_sdp_iterate(&dummy, req, name);
05745 }
05746
05747
05748 static char *get_body(struct sip_request *req, char *name)
05749 {
05750 int x;
05751 int len = strlen(name);
05752 char *r;
05753
05754 for (x = 0; x < req->lines; x++) {
05755 r = get_body_by_line(req->line[x], name, len);
05756 if (r[0] != '\0')
05757 return r;
05758 }
05759
05760 return "";
05761 }
05762
05763
05764 static const char *find_alias(const char *name, const char *_default)
05765 {
05766
05767 static const struct cfalias {
05768 char * const fullname;
05769 char * const shortname;
05770 } aliases[] = {
05771 { "Content-Type", "c" },
05772 { "Content-Encoding", "e" },
05773 { "From", "f" },
05774 { "Call-ID", "i" },
05775 { "Contact", "m" },
05776 { "Content-Length", "l" },
05777 { "Subject", "s" },
05778 { "To", "t" },
05779 { "Supported", "k" },
05780 { "Refer-To", "r" },
05781 { "Referred-By", "b" },
05782 { "Allow-Events", "u" },
05783 { "Event", "o" },
05784 { "Via", "v" },
05785 { "Accept-Contact", "a" },
05786 { "Reject-Contact", "j" },
05787 { "Request-Disposition", "d" },
05788 { "Session-Expires", "x" },
05789 { "Identity", "y" },
05790 { "Identity-Info", "n" },
05791 };
05792 int x;
05793
05794 for (x = 0; x < ARRAY_LEN(aliases); x++) {
05795 if (!strcasecmp(aliases[x].fullname, name))
05796 return aliases[x].shortname;
05797 }
05798
05799 return _default;
05800 }
05801
05802 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
05803 {
05804 int pass;
05805
05806
05807
05808
05809
05810
05811
05812
05813
05814
05815 for (pass = 0; name && pass < 2;pass++) {
05816 int x, len = strlen(name);
05817 for (x=*start; x<req->headers; x++) {
05818 if (!strncasecmp(req->header[x], name, len)) {
05819 char *r = req->header[x] + len;
05820 if (pedanticsipchecking)
05821 r = ast_skip_blanks(r);
05822
05823 if (*r == ':') {
05824 *start = x+1;
05825 return ast_skip_blanks(r+1);
05826 }
05827 }
05828 }
05829 if (pass == 0)
05830 name = find_alias(name, NULL);
05831 }
05832
05833
05834 return "";
05835 }
05836
05837
05838
05839
05840 static const char *get_header(const struct sip_request *req, const char *name)
05841 {
05842 int start = 0;
05843 return __get_header(req, name, &start);
05844 }
05845
05846
05847 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
05848 {
05849
05850 struct ast_frame *f;
05851
05852 if (!p->rtp) {
05853
05854 return &ast_null_frame;
05855 }
05856
05857 switch(ast->fdno) {
05858 case 0:
05859 f = ast_rtp_read(p->rtp);
05860 break;
05861 case 1:
05862 f = ast_rtcp_read(p->rtp);
05863 break;
05864 case 2:
05865 f = ast_rtp_read(p->vrtp);
05866 break;
05867 case 3:
05868 f = ast_rtcp_read(p->vrtp);
05869 break;
05870 case 4:
05871 f = ast_rtp_read(p->trtp);
05872 if (sipdebug_text) {
05873 int i;
05874 unsigned char* arr = f->data;
05875 for (i=0; i < f->datalen; i++)
05876 ast_verbose("%c", (arr[i] > ' ' && arr[i] < '}') ? arr[i] : '.');
05877 ast_verbose(" -> ");
05878 for (i=0; i < f->datalen; i++)
05879 ast_verbose("%02X ", arr[i]);
05880 ast_verbose("\n");
05881 }
05882 break;
05883 case 5:
05884 f = ast_udptl_read(p->udptl);
05885 break;
05886 default:
05887 f = &ast_null_frame;
05888 }
05889
05890 if (f && (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) &&
05891 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833)) {
05892 ast_debug(1, "Ignoring DTMF (%c) RTP frame because dtmfmode is not RFC2833\n", f->subclass);
05893 return &ast_null_frame;
05894 }
05895
05896
05897 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
05898 return f;
05899
05900 if (f && f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
05901 if (!(f->subclass & p->jointcapability)) {
05902 ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
05903 ast_getformatname(f->subclass), p->owner->name);
05904 return &ast_null_frame;
05905 }
05906 ast_debug(1, "Oooh, format changed to %d %s\n",
05907 f->subclass, ast_getformatname(f->subclass));
05908 p->owner->nativeformats = (p->owner->nativeformats & (AST_FORMAT_VIDEO_MASK | AST_FORMAT_TEXT_MASK)) | f->subclass;
05909 ast_set_read_format(p->owner, p->owner->readformat);
05910 ast_set_write_format(p->owner, p->owner->writeformat);
05911 }
05912
05913 if (f && (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
05914 f = ast_dsp_process(p->owner, p->vad, f);
05915 if (f && f->frametype == AST_FRAME_DTMF) {
05916 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && f->subclass == 'f') {
05917 ast_debug(1, "Fax CNG detected on %s\n", ast->name);
05918 *faxdetect = 1;
05919 } else {
05920 ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
05921 }
05922 }
05923 }
05924
05925 return f;
05926 }
05927
05928
05929 static struct ast_frame *sip_read(struct ast_channel *ast)
05930 {
05931 struct ast_frame *fr;
05932 struct sip_pvt *p = ast->tech_pvt;
05933 int faxdetected = FALSE;
05934
05935 sip_pvt_lock(p);
05936 fr = sip_rtp_read(ast, p, &faxdetected);
05937 p->lastrtprx = time(NULL);
05938
05939
05940
05941 if (faxdetected && ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && (p->t38.state == T38_DISABLED) && !(ast_bridged_channel(ast))) {
05942 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
05943 if (!p->pendinginvite) {
05944 ast_debug(3, "Sending reinvite on SIP (%s) for T.38 negotiation.\n", ast->name);
05945 change_t38_state(p, T38_LOCAL_REINVITE);
05946 transmit_reinvite_with_sdp(p, TRUE, FALSE);
05947 }
05948 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
05949 ast_debug(3, "Deferring reinvite on SIP (%s) - it will be re-negotiated for T.38\n", ast->name);
05950 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
05951 }
05952 }
05953
05954
05955 if (fr && fr->frametype == AST_FRAME_VOICE && p->invitestate != INV_EARLY_MEDIA && ast->_state != AST_STATE_UP) {
05956 fr = &ast_null_frame;
05957 }
05958
05959 sip_pvt_unlock(p);
05960
05961 return fr;
05962 }
05963
05964
05965
05966 static char *generate_random_string(char *buf, size_t size)
05967 {
05968 long val[4];
05969 int x;
05970
05971 for (x=0; x<4; x++)
05972 val[x] = ast_random();
05973 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
05974
05975 return buf;
05976 }
05977
05978
05979 static void build_callid_pvt(struct sip_pvt *pvt)
05980 {
05981 char buf[33];
05982
05983 const char *host = S_OR(pvt->fromdomain, ast_inet_ntoa(pvt->ourip.sin_addr));
05984
05985 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
05986
05987 }
05988
05989
05990 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain)
05991 {
05992 char buf[33];
05993
05994 const char *host = S_OR(fromdomain, ast_inet_ntoa(ourip));
05995
05996 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
05997 }
05998
05999
06000 static void make_our_tag(char *tagbuf, size_t len)
06001 {
06002 snprintf(tagbuf, len, "as%08lx", ast_random());
06003 }
06004
06005
06006 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p)
06007 {
06008 struct sip_st_dlg *stp;
06009
06010 if (p->stimer) {
06011 ast_log(LOG_ERROR, "Session-Timer struct already allocated\n");
06012 return p->stimer;
06013 }
06014
06015 if (!(stp = ast_calloc(1, sizeof(struct sip_st_dlg))))
06016 return NULL;
06017
06018 p->stimer = stp;
06019
06020 stp->st_schedid = -1;
06021
06022 return p->stimer;
06023 }
06024
06025
06026
06027
06028
06029 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
06030 int useglobal_nat, const int intended_method, struct sip_request *req)
06031 {
06032 struct sip_pvt *p;
06033
06034 if (!(p = ast_calloc(1, sizeof(*p))))
06035 return NULL;
06036
06037 if (ast_string_field_init(p, 512)) {
06038 ast_free(p);
06039 return NULL;
06040 }
06041
06042 ast_mutex_init(&p->pvt_lock);
06043
06044 if (req) {
06045 set_socket_transport(&p->socket, req->socket.type);
06046 } else {
06047 set_socket_transport(&p->socket, SIP_TRANSPORT_UDP);
06048 }
06049
06050 p->socket.fd = -1;
06051 p->method = intended_method;
06052 p->initid = -1;
06053 p->waitid = -1;
06054 p->autokillid = -1;
06055 p->request_queue_sched_id = -1;
06056 p->t38id = -1;
06057 p->subscribed = NONE;
06058 p->stateid = -1;
06059 p->sessionversion_remote = -1;
06060 p->session_modify = TRUE;
06061 p->stimer = NULL;
06062 p->prefs = default_prefs;
06063
06064 if (intended_method != SIP_OPTIONS) {
06065 p->timer_t1 = global_t1;
06066 p->timer_b = global_timer_b;
06067 }
06068
06069 if (!sin)
06070 p->ourip = internip;
06071 else {
06072 p->sa = *sin;
06073 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
06074 }
06075
06076
06077 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
06078 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
06079
06080 p->do_history = recordhistory;
06081
06082 p->branch = ast_random();
06083 make_our_tag(p->tag, sizeof(p->tag));
06084 p->ocseq = INITIAL_CSEQ;
06085
06086 if (sip_methods[intended_method].need_rtp) {
06087 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
06088
06089 if (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT))
06090 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
06091 if (ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT))
06092 p->trtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
06093 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT))
06094 p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
06095 if (!p->rtp|| (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) && !p->vrtp)
06096 || (ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT) && !p->trtp)) {
06097 ast_log(LOG_WARNING, "Unable to create RTP audio %s%ssession: %s\n",
06098 ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "and video " : "",
06099 ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT) ? "and text " : "", strerror(errno));
06100 if (p->rtp) {
06101 ast_rtp_destroy(p->rtp);
06102 }
06103 if (p->vrtp) {
06104 ast_rtp_destroy(p->vrtp);
06105 }
06106 if (p->udptl) {
06107 ast_udptl_destroy(p->udptl);
06108 }
06109 ast_mutex_destroy(&p->pvt_lock);
06110 if (p->chanvars) {
06111 ast_variables_destroy(p->chanvars);
06112 p->chanvars = NULL;
06113 }
06114 ast_string_field_free_memory(p);
06115 ast_free(p);
06116 return NULL;
06117 }
06118 ast_rtp_setqos(p->rtp, global_tos_audio, global_cos_audio, "SIP RTP");
06119 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
06120 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
06121 ast_rtp_set_rtptimeout(p->rtp, global_rtptimeout);
06122 ast_rtp_set_rtpholdtimeout(p->rtp, global_rtpholdtimeout);
06123 ast_rtp_set_rtpkeepalive(p->rtp, global_rtpkeepalive);
06124 if (p->vrtp) {
06125 ast_rtp_setqos(p->vrtp, global_tos_video, global_cos_video, "SIP VRTP");
06126 ast_rtp_setdtmf(p->vrtp, 0);
06127 ast_rtp_setdtmfcompensate(p->vrtp, 0);
06128 ast_rtp_set_rtptimeout(p->vrtp, global_rtptimeout);
06129 ast_rtp_set_rtpholdtimeout(p->vrtp, global_rtpholdtimeout);
06130 ast_rtp_set_rtpkeepalive(p->vrtp, global_rtpkeepalive);
06131 }
06132 if (p->trtp) {
06133 ast_rtp_setqos(p->trtp, global_tos_text, global_cos_text, "SIP TRTP");
06134 ast_rtp_setdtmf(p->trtp, 0);
06135 ast_rtp_setdtmfcompensate(p->trtp, 0);
06136 }
06137 if (p->udptl)
06138 ast_udptl_setqos(p->udptl, global_tos_audio, global_cos_audio);
06139 p->maxcallbitrate = default_maxcallbitrate;
06140 p->autoframing = global_autoframing;
06141 ast_rtp_codec_setpref(p->rtp, &p->prefs);
06142 }
06143
06144 if (useglobal_nat && sin) {
06145
06146 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
06147 p->recv = *sin;
06148 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
06149 }
06150
06151 if (p->method != SIP_REGISTER)
06152 ast_string_field_set(p, fromdomain, default_fromdomain);
06153 build_via(p);
06154 if (!callid)
06155 build_callid_pvt(p);
06156 else
06157 ast_string_field_set(p, callid, callid);
06158
06159 ast_string_field_set(p, mohinterpret, default_mohinterpret);
06160 ast_string_field_set(p, mohsuggest, default_mohsuggest);
06161 p->capability = global_capability;
06162 p->allowtransfer = global_allowtransfer;
06163 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
06164 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
06165 p->noncodeccapability |= AST_RTP_DTMF;
06166 if (p->udptl) {
06167 ast_copy_flags(&p->t38.t38support, &p->flags[1], SIP_PAGE2_T38SUPPORT);
06168 set_t38_capabilities(p);
06169 p->t38.jointcapability = p->t38.capability;
06170 }
06171 ast_string_field_set(p, context, default_context);
06172
06173 AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
06174
06175
06176 dialoglist_lock();
06177 p->next = dialoglist;
06178 dialoglist = dialog_ref(p);
06179 dialoglist_unlock();
06180 ast_debug(1, "Allocating new SIP dialog for %s - %s (%s)\n", callid ? callid : "(No Call-ID)", sip_methods[intended_method].text, p->rtp ? "With RTP" : "No RTP");
06181 return p;
06182 }
06183
06184
06185
06186
06187
06188
06189 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
06190 {
06191 struct sip_pvt *p = NULL;
06192 char *tag = "";
06193 char totag[128];
06194 char fromtag[128];
06195 const char *callid = get_header(req, "Call-ID");
06196 const char *from = get_header(req, "From");
06197 const char *to = get_header(req, "To");
06198 const char *cseq = get_header(req, "Cseq");
06199
06200
06201
06202 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
06203 ast_strlen_zero(from) || ast_strlen_zero(cseq))
06204 return NULL;
06205
06206 if (pedanticsipchecking) {
06207
06208
06209
06210
06211
06212
06213 if (gettag(req, "To", totag, sizeof(totag)))
06214 req->has_to_tag = 1;
06215 gettag(req, "From", fromtag, sizeof(fromtag));
06216
06217 tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
06218
06219 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);
06220
06221
06222 if (ast_strlen_zero(fromtag)) {
06223 ast_debug(5, "%s request has no from tag, dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
06224 return NULL;
06225 }
06226
06227 if (ast_strlen_zero(totag) && (req->method == SIP_ACK || req->method == SIP_BYE || req->method == SIP_INFO )) {
06228 ast_debug(5, "%s must have a to tag. dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
06229 return NULL;
06230 }
06231 }
06232
06233 dialoglist_lock();
06234 restartsearch:
06235 for (p = dialoglist; p; p = p->next) {
06236
06237 int found = FALSE;
06238 if (ast_strlen_zero(p->callid))
06239 continue;
06240 if (req->method == SIP_REGISTER)
06241 found = (!strcmp(p->callid, callid));
06242 else
06243 found = (!strcmp(p->callid, callid) &&
06244 (!pedanticsipchecking || ast_strlen_zero(tag) || ast_strlen_zero(p->theirtag) || !ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED) || !strcmp(p->theirtag, tag))) ;
06245
06246 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);
06247
06248
06249 if (pedanticsipchecking && found && req->method != SIP_RESPONSE) {
06250 if (p->tag[0] == '\0' && totag[0]) {
06251
06252 found = FALSE;
06253 } else if (totag[0]) {
06254 if (strcmp(totag, p->tag)) {
06255 found = FALSE;
06256 }
06257 }
06258 if (!found)
06259 ast_debug(5, "= Being pedantic: This is not our match on request: Call ID: %s Ourtag <null> Totag %s Method %s\n", p->callid, totag, sip_methods[req->method].text);
06260 }
06261
06262
06263 if (found) {
06264
06265 if (sip_pvt_trylock(p)) {
06266 dialoglist_unlock();
06267 usleep(1);
06268 dialoglist_lock();
06269 goto restartsearch;
06270 }
06271 dialoglist_unlock();
06272 return p;
06273 }
06274 }
06275 dialoglist_unlock();
06276
06277
06278 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
06279 if (intended_method == SIP_REFER) {
06280
06281 transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
06282 } else if (intended_method == SIP_NOTIFY) {
06283
06284
06285 transmit_response_using_temp(callid, sin, 1, intended_method, req, "489 Bad event");
06286 } else {
06287
06288 if ((p = sip_alloc(callid, sin, 1, intended_method, req))) {
06289
06290 sip_pvt_lock(p);
06291 } else {
06292
06293
06294
06295
06296
06297
06298
06299
06300 transmit_response_using_temp(callid, sin, 1, intended_method, req, "500 Server internal error");
06301 ast_debug(4, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
06302 }
06303 }
06304 return p;
06305 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
06306
06307 transmit_response_using_temp(callid, sin, 1, intended_method, req, "501 Method Not Implemented");
06308 ast_debug(2, "Got a request with unsupported SIP method.\n");
06309 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
06310
06311 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 Call leg/transaction does not exist");
06312 ast_debug(2, "That's odd... Got a request in unknown dialog. Callid %s\n", callid ? callid : "<unknown>");
06313 }
06314
06315
06316 if (intended_method == SIP_RESPONSE)
06317 ast_debug(2, "That's odd... Got a response on a call we dont know about. Callid %s\n", callid ? callid : "<unknown>");
06318
06319 return p;
06320 }
06321
06322
06323 static int sip_register(const char *value, int lineno)
06324 {
06325 struct sip_registry *reg;
06326 int portnum = 0;
06327 enum sip_transport transport = SIP_TRANSPORT_UDP;
06328 char buf[256] = "";
06329 char *username = NULL;
06330 char *port = NULL;
06331 char *hostname=NULL, *secret=NULL, *authuser=NULL;
06332 char *callback=NULL;
06333
06334 if (!value)
06335 return -1;
06336
06337 ast_copy_string(buf, value, sizeof(buf));
06338
06339 sip_parse_host(buf, lineno, &username, &portnum, &transport);
06340
06341
06342 hostname = strrchr(username, '@');
06343 if (hostname)
06344 *hostname++ = '\0';
06345 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
06346 ast_log(LOG_WARNING, "Format for registration is [transport://]user[@domain][:secret[:authuser]]@host[:port][/extension] at line %d\n", lineno);
06347 return -1;
06348 }
06349
06350 secret = strchr(username, ':');
06351 if (secret) {
06352 *secret++ = '\0';
06353 authuser = strchr(secret, ':');
06354 if (authuser)
06355 *authuser++ = '\0';
06356 }
06357
06358
06359 callback = strchr(hostname, '/');
06360 if (callback)
06361 *callback++ = '\0';
06362 if (ast_strlen_zero(callback))
06363 callback = "s";
06364
06365
06366 if ((port = strchr(hostname, ':'))) {
06367 *port = '\0';
06368 }
06369
06370
06371 if (port) {
06372 *port = ':';
06373 }
06374 if (!(reg = ast_calloc(1, sizeof(*reg)))) {
06375 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
06376 return -1;
06377 }
06378
06379 if (ast_string_field_init(reg, 256)) {
06380 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry strings\n");
06381 ast_free(reg);
06382 return -1;
06383 }
06384
06385 regobjs++;
06386 ASTOBJ_INIT(reg);
06387 ast_string_field_set(reg, callback, callback);
06388 if (!ast_strlen_zero(username))
06389 ast_string_field_set(reg, username, username);
06390 if (hostname)
06391 ast_string_field_set(reg, hostname, hostname);
06392 if (authuser)
06393 ast_string_field_set(reg, authuser, authuser);
06394 if (secret)
06395 ast_string_field_set(reg, secret, secret);
06396 reg->transport = transport;
06397 reg->expire = -1;
06398 reg->expiry = default_expiry;
06399 reg->timeout = -1;
06400 reg->refresh = default_expiry;
06401 reg->portno = portnum;
06402 reg->callid_valid = FALSE;
06403 reg->ocseq = INITIAL_CSEQ;
06404 ASTOBJ_CONTAINER_LINK(®l, reg);
06405 registry_unref(reg);
06406 return 0;
06407 }
06408
06409
06410
06411 static int lws2sws(char *msgbuf, int len)
06412 {
06413 int h = 0, t = 0;
06414 int lws = 0;
06415
06416 for (; h < len;) {
06417
06418 if (msgbuf[h] == '\r') {
06419 h++;
06420 continue;
06421 }
06422
06423 if (msgbuf[h] == '\n') {
06424
06425 if (h + 1 == len)
06426 break;
06427
06428 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
06429
06430 h++;
06431 continue;
06432 }
06433
06434 msgbuf[t++] = msgbuf[h++];
06435 lws = 0;
06436 continue;
06437 }
06438 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
06439 if (lws) {
06440 h++;
06441 continue;
06442 }
06443 msgbuf[t++] = msgbuf[h++];
06444 lws = 1;
06445 continue;
06446 }
06447 msgbuf[t++] = msgbuf[h++];
06448 if (lws)
06449 lws = 0;
06450 }
06451 msgbuf[t] = '\0';
06452 return t;
06453 }
06454
06455
06456
06457
06458 static int parse_request(struct sip_request *req)
06459 {
06460 char *c = req->data, **dst = req->header;
06461 int i = 0, lim = SIP_MAX_HEADERS - 1;
06462 unsigned int skipping_headers = 0;
06463
06464 req->header[0] = c;
06465 req->headers = -1;
06466 for (; *c; c++) {
06467 if (*c == '\r') {
06468 *c = '\0';
06469 } else if (*c == '\n') {
06470 *c = '\0';
06471 if (skipping_headers) {
06472
06473
06474
06475 if (ast_strlen_zero(dst[i])) {
06476 skipping_headers = 0;
06477 }
06478 dst[i] = c + 1;
06479 continue;
06480 }
06481 if (sipdebug) {
06482 ast_debug(4, "%7s %2d [%3d]: %s\n",
06483 req->headers < 0 ? "Header" : "Body",
06484 i, (int) strlen(dst[i]), dst[i]);
06485 }
06486 if (ast_strlen_zero(dst[i]) && req->headers < 0) {
06487 req->headers = i;
06488 dst = req->line;
06489 i = 0;
06490 lim = SIP_MAX_LINES - 1;
06491 } else {
06492 if (i++ == lim) {
06493
06494
06495
06496 if (req->headers != -1) {
06497 break;
06498 } else {
06499 req->headers = i;
06500 dst = req->line;
06501 i = 0;
06502 lim = SIP_MAX_LINES - 1;
06503 skipping_headers = 1;
06504 }
06505 }
06506 }
06507 dst[i] = c + 1;
06508 }
06509 }
06510
06511
06512
06513
06514
06515
06516 if ((i < lim) && !ast_strlen_zero(dst[i])) {
06517 if (sipdebug) {
06518 ast_debug(4, "%7s %2d [%3d]: %s\n",
06519 req->headers < 0 ? "Header" : "Body",
06520 i, (int) strlen(dst[i]), dst[i]);
06521 }
06522 i++;
06523 }
06524
06525
06526 if (req->headers >= 0) {
06527 req->lines = i;
06528 } else {
06529 req->headers = i;
06530 req->lines = 0;
06531 req->line[0] = "";
06532 }
06533
06534 if (*c) {
06535 ast_log(LOG_WARNING, "Too many lines, skipping <%s>\n", c);
06536 }
06537
06538
06539 return determine_firstline_parts(req);
06540 }
06541
06542
06543
06544
06545
06546
06547
06548
06549
06550 static int find_sdp(struct sip_request *req)
06551 {
06552 const char *content_type;
06553 const char *content_length;
06554 const char *search;
06555 char *boundary;
06556 unsigned int x;
06557 int boundaryisquoted = FALSE;
06558 int found_application_sdp = FALSE;
06559 int found_end_of_headers = FALSE;
06560
06561 content_length = get_header(req, "Content-Length");
06562
06563 if (!ast_strlen_zero(content_length)) {
06564 if (sscanf(content_length, "%ud", &x) != 1) {
06565 ast_log(LOG_WARNING, "Invalid Content-Length: %s\n", content_length);
06566 return 0;
06567 }
06568
06569
06570
06571 if (x == 0)
06572 return 0;
06573 }
06574
06575 content_type = get_header(req, "Content-Type");
06576
06577
06578 if (!strncasecmp(content_type, "application/sdp", 15)) {
06579 req->sdp_start = 0;
06580 req->sdp_end = req->lines;
06581 return req->lines ? 1 : 0;
06582 }
06583
06584
06585 if (strncasecmp(content_type, "multipart/mixed", 15))
06586 return 0;
06587
06588
06589 if ((search = strcasestr(content_type, ";boundary=")))
06590 search += 10;
06591 else if ((search = strcasestr(content_type, "; boundary=")))
06592 search += 11;
06593 else
06594 return 0;
06595
06596 if (ast_strlen_zero(search))
06597 return 0;
06598
06599
06600 if (*search == '\"') {
06601 search++;
06602 boundaryisquoted = TRUE;
06603 }
06604
06605
06606
06607 boundary = ast_strdupa(search - 2);
06608 boundary[0] = boundary[1] = '-';
06609
06610 if (boundaryisquoted)
06611 boundary[strlen(boundary) - 1] = '\0';
06612
06613
06614
06615
06616 for (x = 0; x < (req->lines ); x++) {
06617 if(!strncasecmp(req->line[x], boundary, strlen(boundary))){
06618 if(found_application_sdp && found_end_of_headers){
06619 req->sdp_end = x-1;
06620 return 1;
06621 }
06622 found_application_sdp = FALSE;
06623 }
06624 if(!strcasecmp(req->line[x], "Content-Type: application/sdp"))
06625 found_application_sdp = TRUE;
06626
06627 if (ast_strlen_zero(req->line[x])) {
06628 if(found_application_sdp && !found_end_of_headers){
06629 req->sdp_start = x;
06630 found_end_of_headers = TRUE;
06631 }
06632 }
06633 }
06634 if(found_application_sdp && found_end_of_headers) {
06635 req->sdp_end = x;
06636 return TRUE;
06637 }
06638 return FALSE;
06639 }
06640
06641 enum media_type {
06642 SDP_AUDIO,
06643 SDP_VIDEO,
06644 };
06645
06646 static int get_ip_and_port_from_sdp(struct sip_request *req, const enum media_type media, struct sockaddr_in *sin)
06647 {
06648 const char *m;
06649 const char *c;
06650 int miterator = req->sdp_start;
06651 int citerator = req->sdp_start;
06652 int x = 0;
06653 int numberofports;
06654 int len;
06655 char host[258] = "";
06656 struct ast_hostent audiohp;
06657 struct hostent *hp;
06658
06659 c = get_sdp_iterate(&citerator, req, "c");
06660 if (sscanf(c, "IN IP4 %256s", host) != 1) {
06661 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
06662
06663 }
06664
06665 for (m = get_sdp_iterate(&miterator, req, "m"); !ast_strlen_zero(m); m = get_sdp_iterate(&miterator, req, "m")) {
06666 if ((media == SDP_AUDIO && ((sscanf(m, "audio %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
06667 (sscanf(m, "audio %d RTP/AVP %n", &x, &len) == 1 && len > 0))) ||
06668 (media == SDP_VIDEO && ((sscanf(m, "video %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
06669 (sscanf(m, "video %d RTP/AVP %n", &x, &len) == 1 && len > 0)))) {
06670
06671
06672
06673
06674 c = get_sdp_iterate(&citerator, req, "c");
06675 if (!ast_strlen_zero(c)) {
06676 sscanf(c, "IN IP4 %256s", host);
06677 }
06678 break;
06679 }
06680 }
06681
06682 if (ast_strlen_zero(host) || x == 0) {
06683 ast_log(LOG_WARNING, "Failed to read an alternate host or port in SDP. Expect %s problems\n", media == SDP_AUDIO ? "audio" : "video");
06684 return -1;
06685 }
06686
06687 hp = ast_gethostbyname(host, &audiohp);
06688 if (!hp) {
06689 ast_log(LOG_WARNING, "Could not look up IP address of alternate hostname. Expect %s problems\n", media == SDP_AUDIO? "audio" : "video");
06690 return -1;
06691 }
06692
06693 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
06694 sin->sin_port = htons(x);
06695 return 0;
06696 }
06697
06698
06699
06700
06701
06702
06703 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
06704 {
06705 const char *m;
06706 const char *c;
06707 const char *a;
06708 const char *o;
06709 char *o_copy;
06710 char *token;
06711 char host[258];
06712 int len = -1;
06713 int portno = -1;
06714 int vportno = -1;
06715 int tportno = -1;
06716 int udptlportno = -1;
06717 int peert38capability = 0;
06718 char s[256];
06719 int old = 0;
06720
06721
06722 int peercapability = 0, peernoncodeccapability = 0;
06723 int vpeercapability = 0, vpeernoncodeccapability = 0;
06724 int tpeercapability = 0, tpeernoncodeccapability = 0;
06725 struct sockaddr_in sin;
06726 struct sockaddr_in vsin;
06727 struct sockaddr_in tsin;
06728
06729 const char *codecs;
06730 struct hostent *hp;
06731 struct hostent *vhp = NULL;
06732 struct hostent *thp = NULL;
06733 struct ast_hostent audiohp;
06734 struct ast_hostent videohp;
06735 struct ast_hostent texthp;
06736 int codec;
06737 int destiterator = 0;
06738 int iterator;
06739 int sendonly = -1;
06740 int numberofports;
06741 struct ast_rtp *newaudiortp, *newvideortp, *newtextrtp;
06742 int newjointcapability;
06743 int newpeercapability;
06744 int newnoncodeccapability;
06745 int numberofmediastreams = 0;
06746 int debug = sip_debug_test_pvt(p);
06747
06748 int found_rtpmap_codecs[SDP_MAX_RTPMAP_CODECS];
06749 int last_rtpmap_codec=0;
06750
06751 char buf[SIPBUFSIZE];
06752 int64_t rua_version;
06753
06754 if (!p->rtp) {
06755 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
06756 return -1;
06757 }
06758
06759
06760 #ifdef LOW_MEMORY
06761 newaudiortp = ast_threadstorage_get(&ts_audio_rtp, ast_rtp_alloc_size());
06762 #else
06763 newaudiortp = alloca(ast_rtp_alloc_size());
06764 #endif
06765 memset(newaudiortp, 0, ast_rtp_alloc_size());
06766 ast_rtp_new_init(newaudiortp);
06767 ast_rtp_pt_clear(newaudiortp);
06768
06769 #ifdef LOW_MEMORY
06770 newvideortp = ast_threadstorage_get(&ts_video_rtp, ast_rtp_alloc_size());
06771 #else
06772 newvideortp = alloca(ast_rtp_alloc_size());
06773 #endif
06774 memset(newvideortp, 0, ast_rtp_alloc_size());
06775 ast_rtp_new_init(newvideortp);
06776 ast_rtp_pt_clear(newvideortp);
06777
06778 #ifdef LOW_MEMORY
06779 newtextrtp = ast_threadstorage_get(&ts_text_rtp, ast_rtp_alloc_size());
06780 #else
06781 newtextrtp = alloca(ast_rtp_alloc_size());
06782 #endif
06783 memset(newtextrtp, 0, ast_rtp_alloc_size());
06784 ast_rtp_new_init(newtextrtp);
06785 ast_rtp_pt_clear(newtextrtp);
06786
06787
06788 p->lastrtprx = p->lastrtptx = time(NULL);
06789
06790
06791
06792
06793
06794
06795
06796
06797
06798
06799 o = get_sdp(req, "o");
06800 if (ast_strlen_zero(o)) {
06801 ast_log(LOG_WARNING, "SDP sytax error. SDP without an o= line\n");
06802 return -1;
06803 }
06804
06805 o_copy = ast_strdupa(o);
06806 token = strsep(&o_copy, " ");
06807 if (!o_copy) {
06808 ast_log(LOG_WARNING, "SDP sytax error in o= line username\n");
06809 return -1;
06810 }
06811 token = strsep(&o_copy, " ");
06812 if (!o_copy) {
06813 ast_log(LOG_WARNING, "SDP sytax error in o= line session-id\n");
06814 return -1;
06815 }
06816 token = strsep(&o_copy, " ");
06817 if (!o_copy) {
06818 ast_log(LOG_WARNING, "SDP sytax error in o= line\n");
06819 return -1;
06820 }
06821 if (!sscanf(token, "%" SCNd64, &rua_version)) {
06822 ast_log(LOG_WARNING, "SDP sytax error in o= line version\n");
06823 return -1;
06824 }
06825
06826
06827
06828
06829
06830
06831
06832
06833
06834
06835
06836
06837
06838
06839
06840
06841
06842
06843
06844 if (ast_test_flag(&p->flags[1], SIP_PAGE2_IGNORESDPVERSION) ||
06845 (p->sessionversion_remote < 0) ||
06846 (p->sessionversion_remote < rua_version)) {
06847 p->sessionversion_remote = rua_version;
06848 p->session_modify = TRUE;
06849 } else {
06850 if (p->t38.state == T38_LOCAL_REINVITE) {
06851 p->sessionversion_remote = rua_version;
06852 p->session_modify = TRUE;
06853 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);
06854 } else {
06855 p->session_modify = FALSE;
06856 ast_debug(2, "Call %s responded to our reinvite without changing SDP version; ignoring SDP.\n", p->callid);
06857 return 0;
06858 }
06859 }
06860
06861
06862 m = get_sdp(req, "m");
06863 destiterator = req->sdp_start;
06864 c = get_sdp_iterate(&destiterator, req, "c");
06865 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
06866 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
06867 return -1;
06868 }
06869
06870
06871 if (sscanf(c, "IN IP4 %256s", host) != 1) {
06872 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
06873 return -1;
06874 }
06875
06876
06877 hp = ast_gethostbyname(host, &audiohp);
06878 if (!hp) {
06879 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
06880 return -1;
06881 }
06882 vhp = hp;
06883 thp = hp;
06884
06885 iterator = req->sdp_start;
06886
06887 p->novideo = TRUE;
06888 p->notext = TRUE;
06889
06890 if (p->vrtp)
06891 ast_rtp_pt_clear(newvideortp);
06892
06893 if (p->trtp)
06894 ast_rtp_pt_clear(newtextrtp);
06895
06896
06897 while ((m = get_sdp_iterate(&iterator, req, "m"))[0] != '\0') {
06898 int x;
06899 int audio = FALSE;
06900 int video = FALSE;
06901 int text = FALSE;
06902
06903 numberofports = 1;
06904 len = -1;
06905 if ((sscanf(m, "audio %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
06906 (sscanf(m, "audio %d RTP/AVP %n", &x, &len) == 1 && len > 0)) {
06907 audio = TRUE;
06908 numberofmediastreams++;
06909
06910 portno = x;
06911
06912 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
06913 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
06914 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
06915 return -1;
06916 }
06917 if (debug)
06918 ast_verbose("Found RTP audio format %d\n", codec);
06919 ast_rtp_set_m_type(newaudiortp, codec);
06920 }
06921 } else if ((sscanf(m, "video %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
06922 (sscanf(m, "video %d RTP/AVP %n", &x, &len) == 1 && len >= 0)) {
06923 video = TRUE;
06924 p->novideo = FALSE;
06925 numberofmediastreams++;
06926 vportno = x;
06927
06928 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
06929 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
06930 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
06931 return -1;
06932 }
06933 if (debug)
06934 ast_verbose("Found RTP video format %d\n", codec);
06935 ast_rtp_set_m_type(newvideortp, codec);
06936 }
06937 } else if ((sscanf(m, "text %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
06938 (sscanf(m, "text %d RTP/AVP %n", &x, &len) == 1 && len > 0)) {
06939 text = TRUE;
06940 p->notext = FALSE;
06941 numberofmediastreams++;
06942 tportno = x;
06943
06944 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
06945 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
06946 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
06947 return -1;
06948 }
06949 if (debug)
06950 ast_verbose("Found RTP text format %d\n", codec);
06951 ast_rtp_set_m_type(newtextrtp, codec);
06952 }
06953 } else if (p->udptl && ( (sscanf(m, "image %d udptl t38%n", &x, &len) == 1 && len > 0) ||
06954 (sscanf(m, "image %d UDPTL t38%n", &x, &len) == 1 && len > 0) )) {
06955 if (debug)
06956 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
06957 udptlportno = x;
06958 numberofmediastreams++;
06959 } else
06960 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
06961 if (numberofports > 1)
06962 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
06963
06964
06965
06966 c = get_sdp_iterate(&destiterator, req, "c");
06967 if (!ast_strlen_zero(c)) {
06968 if (sscanf(c, "IN IP4 %256s", host) != 1) {
06969 ast_log(LOG_WARNING, "Invalid secondary host in c= line, '%s'\n", c);
06970 } else {
06971
06972 if (audio) {
06973 if ( !(hp = ast_gethostbyname(host, &audiohp))) {
06974 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in secondary c= line, '%s'\n", c);
06975 return -2;
06976 }
06977 } else if (video) {
06978 if (!(vhp = ast_gethostbyname(host, &videohp))) {
06979 ast_log(LOG_WARNING, "Unable to lookup RTP video host in secondary c= line, '%s'\n", c);
06980 return -2;
06981 }
06982 } else if (text) {
06983 if (!(thp = ast_gethostbyname(host, &texthp))) {
06984 ast_log(LOG_WARNING, "Unable to lookup RTP text host in secondary c= line, '%s'\n", c);
06985 return -2;
06986 }
06987 }
06988 }
06989
06990 }
06991 }
06992 if (portno == -1 && vportno == -1 && udptlportno == -1 && tportno == -1)
06993
06994
06995 return -2;
06996
06997 if (numberofmediastreams > 3)
06998
06999 return -3;
07000
07001
07002 sin.sin_family = AF_INET;
07003 vsin.sin_family = AF_INET;
07004 tsin.sin_family = AF_INET;
07005 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
07006 if (vhp)
07007 memcpy(&vsin.sin_addr, vhp->h_addr, sizeof(vsin.sin_addr));
07008 if (thp)
07009 memcpy(&tsin.sin_addr, thp->h_addr, sizeof(tsin.sin_addr));
07010
07011
07012 if (p->udptl) {
07013 if (udptlportno > 0) {
07014 sin.sin_port = htons(udptlportno);
07015 if (ast_test_flag(&p->flags[0], SIP_NAT) && ast_test_flag(&p->flags[1], SIP_PAGE2_UDPTL_DESTINATION)) {
07016 struct sockaddr_in peer;
07017 ast_rtp_get_peer(p->rtp, &peer);
07018 if (peer.sin_addr.s_addr) {
07019 memcpy(&sin.sin_addr, &peer.sin_addr, sizeof(sin.sin_addr));
07020 if (debug) {
07021 ast_log(LOG_DEBUG, "Peer T.38 UDPTL is set behind NAT and with destination, destination address now %s\n", ast_inet_ntoa(sin.sin_addr));
07022 }
07023 }
07024 }
07025 ast_udptl_set_peer(p->udptl, &sin);
07026 if (debug)
07027 ast_debug(1, "Peer T.38 UDPTL is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
07028 } else {
07029 ast_udptl_stop(p->udptl);
07030 if (debug)
07031 ast_debug(1, "Peer doesn't provide T.38 UDPTL\n");
07032 }
07033 }
07034
07035
07036 if (p->rtp) {
07037 if (portno > 0) {
07038 sin.sin_port = htons(portno);
07039 ast_rtp_set_peer(p->rtp, &sin);
07040 if (debug)
07041 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
07042 } else {
07043 if (udptlportno > 0) {
07044 if (debug)
07045 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session. Callid %s\n", p->callid);
07046 } else {
07047 ast_rtp_stop(p->rtp);
07048 if (debug)
07049 ast_verbose("Peer doesn't provide audio. Callid %s\n", p->callid);
07050 }
07051 }
07052 }
07053
07054 if (vportno != -1)
07055 vsin.sin_port = htons(vportno);
07056
07057
07058 if (tportno != -1)
07059 tsin.sin_port = htons(tportno);
07060
07061
07062
07063
07064
07065 iterator = req->sdp_start;
07066 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
07067 char mimeSubtype[128];
07068 if (option_debug > 1) {
07069 int breakout = FALSE;
07070
07071
07072 if (!strncasecmp(a, "rtcp:", (size_t) 5)) {
07073 if (debug)
07074 ast_verbose("Got unsupported a:rtcp in SDP offer \n");
07075 breakout = TRUE;
07076 } else if (!strncasecmp(a, "fmtp:", (size_t) 5)) {
07077
07078
07079
07080
07081 if (debug)
07082 ast_verbose("Got unsupported a:fmtp in SDP offer \n");
07083 breakout = TRUE;
07084 } else if (!strncasecmp(a, "framerate:", (size_t) 10)) {
07085
07086 if (debug)
07087 ast_verbose("Got unsupported a:framerate in SDP offer \n");
07088 breakout = TRUE;
07089 } else if (!strncasecmp(a, "maxprate:", (size_t) 9)) {
07090
07091 if (debug)
07092 ast_verbose("Got unsupported a:maxprate in SDP offer \n");
07093 breakout = TRUE;
07094 } else if (!strncasecmp(a, "crypto:", (size_t) 7)) {
07095
07096 if (debug)
07097 ast_verbose("Got unsupported a:crypto in SDP offer \n");
07098 breakout = TRUE;
07099 }
07100 if (breakout)
07101 continue;
07102 }
07103 if (!strcasecmp(a, "sendonly")) {
07104 if (sendonly == -1)
07105 sendonly = 1;
07106 continue;
07107 } else if (!strcasecmp(a, "inactive")) {
07108 if (sendonly == -1)
07109 sendonly = 2;
07110 continue;
07111 } else if (!strcasecmp(a, "sendrecv")) {
07112 if (sendonly == -1)
07113 sendonly = 0;
07114 continue;
07115 } else if (strlen(a) > 5 && !strncasecmp(a, "ptime", 5)) {
07116 char *tmp = strrchr(a, ':');
07117 long int framing = 0;
07118 if (tmp) {
07119 tmp++;
07120 framing = strtol(tmp, NULL, 10);
07121 if (framing == LONG_MIN || framing == LONG_MAX) {
07122 framing = 0;
07123 ast_debug(1, "Can't read framing from SDP: %s\n", a);
07124 }
07125 }
07126 if (framing && p->autoframing) {
07127 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
07128 int codec_n;
07129 int format = 0;
07130 for (codec_n = 0; codec_n < MAX_RTP_PT; codec_n++) {
07131 format = ast_rtp_codec_getformat(codec_n);
07132 if (!format)
07133 continue;
07134 if (option_debug)
07135 ast_log(LOG_DEBUG, "Setting framing for %d to %ld\n", format, framing);
07136 ast_codec_pref_setsize(pref, format, framing);
07137 }
07138 ast_rtp_codec_setpref(p->rtp, pref);
07139 }
07140 continue;
07141 } else if (sscanf(a, "rtpmap: %u %127[^/]/", &codec, mimeSubtype) == 2) {
07142
07143
07144 if (last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
07145
07146
07147 if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3)) {
07148 if(ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0) != -1) {
07149 if (debug)
07150 ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec);
07151 found_rtpmap_codecs[last_rtpmap_codec] = codec;
07152 last_rtpmap_codec++;
07153 } else {
07154 ast_rtp_unset_m_type(newvideortp, codec);
07155 if (debug)
07156 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
07157 }
07158 } else if (!strncasecmp(mimeSubtype, "T140", 4)) {
07159 if (p->trtp) {
07160
07161 ast_rtp_set_rtpmap_type(newtextrtp, codec, "text", mimeSubtype, 0);
07162 }
07163 } else {
07164 if(ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
07165 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0) != -1) {
07166 if (debug)
07167 ast_verbose("Found audio description format %s for ID %d\n", mimeSubtype, codec);
07168 found_rtpmap_codecs[last_rtpmap_codec] = codec;
07169 last_rtpmap_codec++;
07170 } else {
07171 ast_rtp_unset_m_type(newaudiortp, codec);
07172 if (debug)
07173 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
07174 }
07175 }
07176 } else {
07177 if (debug)
07178 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
07179 }
07180
07181 }
07182 }
07183
07184 if (udptlportno != -1) {
07185 int found = 0, x;
07186
07187 old = 0;
07188
07189
07190 iterator = req->sdp_start;
07191 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
07192 if ((sscanf(a, "T38FaxMaxBuffer:%d", &x) == 1)) {
07193 found = 1;
07194 ast_debug(3, "MaxBufferSize:%d\n", x);
07195 } else if ((sscanf(a, "T38MaxBitRate:%d", &x) == 1) || (sscanf(a, "T38FaxMaxRate:%d", &x) == 1)) {
07196 found = 1;
07197 ast_debug(3, "T38MaxBitRate: %d\n", x);
07198 switch (x) {
07199 case 14400:
07200 peert38capability |= T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
07201 break;
07202 case 12000:
07203 peert38capability |= T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
07204 break;
07205 case 9600:
07206 peert38capability |= T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
07207 break;
07208 case 7200:
07209 peert38capability |= T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
07210 break;
07211 case 4800:
07212 peert38capability |= T38FAX_RATE_4800 | T38FAX_RATE_2400;
07213 break;
07214 case 2400:
07215 peert38capability |= T38FAX_RATE_2400;
07216 break;
07217 }
07218 } else if ((sscanf(a, "T38FaxVersion:%d", &x) == 1)) {
07219 found = 1;
07220 ast_debug(3, "FaxVersion: %d\n", x);
07221 if (x == 0)
07222 peert38capability |= T38FAX_VERSION_0;
07223 else if (x == 1)
07224 peert38capability |= T38FAX_VERSION_1;
07225 } else if ((sscanf(a, "T38FaxMaxDatagram:%d", &x) == 1) || (sscanf(a, "T38MaxDatagram:%d", &x) == 1)) {
07226 found = 1;
07227 ast_debug(3, "FaxMaxDatagram: %d\n", x);
07228 ast_udptl_set_far_max_datagram(p->udptl, x);
07229 ast_udptl_set_local_max_datagram(p->udptl, x);
07230 } else if ((strncmp(a, "T38FaxFillBitRemoval", 20) == 0)) {
07231 found = 1;
07232 if(sscanf(a, "T38FaxFillBitRemoval:%d", &x) == 1) {
07233 ast_debug(3, "FillBitRemoval: %d\n", x);
07234 if(x == 1)
07235 peert38capability |= T38FAX_FILL_BIT_REMOVAL;
07236 } else {
07237 ast_debug(3, "FillBitRemoval\n");
07238 peert38capability |= T38FAX_FILL_BIT_REMOVAL;
07239 }
07240 } else if ((strncmp(a, "T38FaxTranscodingMMR", 20) == 0)) {
07241 found = 1;
07242 if(sscanf(a, "T38FaxTranscodingMMR:%d", &x) == 1) {
07243 ast_debug(3, "Transcoding MMR: %d\n", x);
07244 if(x == 1)
07245 peert38capability |= T38FAX_TRANSCODING_MMR;
07246 } else {
07247 ast_debug(3, "Transcoding MMR\n");
07248 peert38capability |= T38FAX_TRANSCODING_MMR;
07249 }
07250 } else if ((strncmp(a, "T38FaxTranscodingJBIG", 21) == 0)) {
07251 found = 1;
07252 if(sscanf(a, "T38FaxTranscodingJBIG:%d", &x) == 1) {
07253 ast_debug(3, "Transcoding JBIG: %d\n", x);
07254 if(x == 1)
07255 peert38capability |= T38FAX_TRANSCODING_JBIG;
07256 } else {
07257 ast_debug(3, "Transcoding JBIG\n");
07258 peert38capability |= T38FAX_TRANSCODING_JBIG;
07259 }
07260 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
07261 found = 1;
07262 ast_debug(3, "RateManagement: %s\n", s);
07263 if (!strcasecmp(s, "localTCF"))
07264 peert38capability |= T38FAX_RATE_MANAGEMENT_LOCAL_TCF;
07265 else if (!strcasecmp(s, "transferredTCF"))
07266 peert38capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
07267 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
07268 found = 1;
07269 ast_debug(3, "UDP EC: %s\n", s);
07270 if (!strcasecmp(s, "t38UDPRedundancy")) {
07271 peert38capability |= T38FAX_UDP_EC_REDUNDANCY;
07272 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
07273 } else if (!strcasecmp(s, "t38UDPFEC")) {
07274 peert38capability |= T38FAX_UDP_EC_FEC;
07275 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
07276 } else {
07277 peert38capability |= T38FAX_UDP_EC_NONE;
07278 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
07279 }
07280 }
07281 }
07282 if (found) {
07283 p->t38.peercapability = peert38capability;
07284 p->t38.jointcapability = (peert38capability & 255);
07285 peert38capability &= (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400);
07286 p->t38.jointcapability |= (peert38capability & p->t38.capability);
07287 }
07288 if (debug)
07289 ast_debug(1, "Our T38 capability = (%d), peer T38 capability (%d), joint T38 capability (%d)\n",
07290 p->t38.capability,
07291 p->t38.peercapability,
07292 p->t38.jointcapability);
07293
07294
07295 if (t38action == SDP_T38_ACCEPT) {
07296 if (p->t38.state == T38_LOCAL_REINVITE)
07297 change_t38_state(p, T38_ENABLED);
07298 } else if (t38action == SDP_T38_INITIATE) {
07299 if (p->owner && p->lastinvite) {
07300 change_t38_state(p, T38_PEER_REINVITE);
07301 } else {
07302 change_t38_state(p, T38_PEER_DIRECT);
07303 }
07304 }
07305 } else {
07306 change_t38_state(p, T38_DISABLED);
07307 }
07308
07309
07310 ast_rtp_get_current_formats(newaudiortp, &peercapability, &peernoncodeccapability);
07311 ast_rtp_get_current_formats(newvideortp, &vpeercapability, &vpeernoncodeccapability);
07312 ast_rtp_get_current_formats(newtextrtp, &tpeercapability, &tpeernoncodeccapability);
07313
07314 newjointcapability = p->capability & (peercapability | vpeercapability | tpeercapability);
07315 newpeercapability = (peercapability | vpeercapability | tpeercapability);
07316 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
07317
07318
07319 if (debug) {
07320
07321 char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE], s5[SIPBUFSIZE];
07322
07323 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s/text=%s, combined - %s\n",
07324 ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
07325 ast_getformatname_multiple(s2, SIPBUFSIZE, peercapability),
07326 ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
07327 ast_getformatname_multiple(s4, SIPBUFSIZE, tpeercapability),
07328 ast_getformatname_multiple(s5, SIPBUFSIZE, newjointcapability));
07329
07330 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
07331 ast_rtp_lookup_mime_multiple(s1, SIPBUFSIZE, p->noncodeccapability, 0, 0),
07332 ast_rtp_lookup_mime_multiple(s2, SIPBUFSIZE, peernoncodeccapability, 0, 0),
07333 ast_rtp_lookup_mime_multiple(s3, SIPBUFSIZE, newnoncodeccapability, 0, 0));
07334 }
07335 if (!newjointcapability) {
07336
07337 if (!p->t38.jointcapability || !udptlportno) {
07338 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
07339
07340 return -1;
07341 } else {
07342 ast_debug(3, "Have T.38 but no audio codecs, accepting offer anyway\n");
07343 return 0;
07344 }
07345 }
07346
07347
07348
07349 p->jointcapability = newjointcapability;
07350 p->peercapability = newpeercapability;
07351 p->jointnoncodeccapability = newnoncodeccapability;
07352
07353 ast_rtp_pt_copy(p->rtp, newaudiortp);
07354 if (p->vrtp)
07355 ast_rtp_pt_copy(p->vrtp, newvideortp);
07356 if (p->trtp)
07357 ast_rtp_pt_copy(p->trtp, newtextrtp);
07358
07359 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
07360 ast_clear_flag(&p->flags[0], SIP_DTMF);
07361 if (newnoncodeccapability & AST_RTP_DTMF) {
07362
07363 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
07364
07365 ast_rtp_setdtmf(p->rtp, 1);
07366 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
07367 } else {
07368 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
07369 }
07370 }
07371
07372
07373 if (p->rtp && sin.sin_port) {
07374 ast_rtp_set_peer(p->rtp, &sin);
07375 if (debug)
07376 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
07377 }
07378
07379
07380 if (p->vrtp && vsin.sin_port) {
07381 ast_rtp_set_peer(p->vrtp, &vsin);
07382 if (debug)
07383 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
07384 }
07385
07386
07387 if (p->trtp && tsin.sin_port) {
07388 ast_rtp_set_peer(p->trtp, &tsin);
07389 if (debug)
07390 ast_verbose("Peer text RTP is at port %s:%d\n", ast_inet_ntoa(tsin.sin_addr), ntohs(tsin.sin_port));
07391 }
07392
07393
07394 ast_debug(2, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
07395
07396 if (!p->owner)
07397 return 0;
07398
07399 ast_debug(4, "We have an owner, now see if we need to change this call\n");
07400
07401 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
07402 if (debug) {
07403 char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
07404 ast_debug(1, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
07405 ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
07406 ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
07407 }
07408 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability) | (p->capability & tpeercapability);
07409 ast_set_read_format(p->owner, p->owner->readformat);
07410 ast_set_write_format(p->owner, p->owner->writeformat);
07411 }
07412
07413 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1)) {
07414 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
07415
07416 ast_queue_frame(p->owner, &ast_null_frame);
07417
07418 append_history(p, "Unhold", "%s", req->data);
07419 if (global_callevents)
07420 manager_event(EVENT_FLAG_CALL, "Hold",
07421 "Status: Off\r\n"
07422 "Channel: %s\r\n"
07423 "Uniqueid: %s\r\n",
07424 p->owner->name,
07425 p->owner->uniqueid);
07426 if (global_notifyhold)
07427 sip_peer_hold(p, FALSE);
07428 ast_clear_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD);
07429 } else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1)) {
07430 int already_on_hold = ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD);
07431 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
07432 S_OR(p->mohsuggest, NULL),
07433 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
07434 if (sendonly)
07435 ast_rtp_stop(p->rtp);
07436
07437
07438 ast_queue_frame(p->owner, &ast_null_frame);
07439
07440 append_history(p, "Hold", "%s", req->data);
07441 if (global_callevents && !ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
07442 manager_event(EVENT_FLAG_CALL, "Hold",
07443 "Status: On\r\n"
07444 "Channel: %s\r\n"
07445 "Uniqueid: %s\r\n",
07446 p->owner->name,
07447 p->owner->uniqueid);
07448 }
07449 if (sendonly == 1)
07450 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
07451 else if (sendonly == 2)
07452 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
07453 else
07454 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
07455 if (global_notifyhold && !already_on_hold)
07456 sip_peer_hold(p, TRUE);
07457 }
07458
07459 return 0;
07460 }
07461
07462 #ifdef LOW_MEMORY
07463 static void ts_ast_rtp_destroy(void *data)
07464 {
07465 struct ast_rtp *tmp = data;
07466 ast_rtp_destroy(tmp);
07467 }
07468 #endif
07469
07470
07471 static int add_header(struct sip_request *req, const char *var, const char *value)
07472 {
07473 int maxlen = sizeof(req->data) - 4 - req->len;
07474
07475 if (req->headers == SIP_MAX_HEADERS) {
07476 ast_log(LOG_WARNING, "Out of SIP header space\n");
07477 return -1;
07478 }
07479
07480 if (req->lines) {
07481 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
07482 return -1;
07483 }
07484
07485 if (maxlen <= 0) {
07486 ast_log(LOG_WARNING, "Out of space, can't add anymore (%s:%s)\n", var, value);
07487 return -1;
07488 }
07489
07490 req->header[req->headers] = req->data + req->len;
07491
07492 if (compactheaders)
07493 var = find_alias(var, var);
07494
07495 snprintf(req->header[req->headers], maxlen, "%s: %s\r\n", var, value);
07496 req->len += strlen(req->header[req->headers]);
07497 req->headers++;
07498
07499 return 0;
07500 }
07501
07502
07503 static int add_header_contentLength(struct sip_request *req, int len)
07504 {
07505 char clen[10];
07506
07507 snprintf(clen, sizeof(clen), "%d", len);
07508 return add_header(req, "Content-Length", clen);
07509 }
07510
07511
07512 static int add_line(struct sip_request *req, const char *line)
07513 {
07514 if (req->lines == SIP_MAX_LINES) {
07515 ast_log(LOG_WARNING, "Out of SIP line space\n");
07516 return -1;
07517 }
07518 if (!req->lines) {
07519
07520 ast_copy_string(req->data + req->len, "\r\n", sizeof(req->data) - req->len);
07521 req->len += strlen(req->data + req->len);
07522 }
07523 if (req->len >= sizeof(req->data) - 4) {
07524 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
07525 return -1;
07526 }
07527 req->line[req->lines] = req->data + req->len;
07528 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
07529 req->len += strlen(req->line[req->lines]);
07530 req->lines++;
07531 return 0;
07532 }
07533
07534
07535 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
07536 {
07537 const char *tmp = get_header(orig, field);
07538
07539 if (!ast_strlen_zero(tmp))
07540 return add_header(req, field, tmp);
07541 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
07542 return -1;
07543 }
07544
07545
07546 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
07547 {
07548 int start = 0;
07549 int copied = 0;
07550 for (;;) {
07551 const char *tmp = __get_header(orig, field, &start);
07552
07553 if (ast_strlen_zero(tmp))
07554 break;
07555
07556 add_header(req, field, tmp);
07557 copied++;
07558 }
07559 return copied ? 0 : -1;
07560 }
07561
07562
07563
07564
07565
07566
07567
07568
07569
07570 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
07571 {
07572 int copied = 0;
07573 int start = 0;
07574
07575 for (;;) {
07576 char new[512];
07577 const char *oh = __get_header(orig, field, &start);
07578
07579 if (ast_strlen_zero(oh))
07580 break;
07581
07582 if (!copied) {
07583 char leftmost[512], *others, *rport;
07584
07585
07586 ast_copy_string(leftmost, oh, sizeof(leftmost));
07587 others = strchr(leftmost, ',');
07588 if (others)
07589 *others++ = '\0';
07590
07591
07592 rport = strstr(leftmost, ";rport");
07593 if (rport && *(rport+6) == '=')
07594 rport = NULL;
07595
07596
07597 if (rport && ((ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_ALWAYS) || (ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_RFC3581))) {
07598
07599 char *end;
07600
07601 rport = strstr(leftmost, ";rport");
07602
07603 if (rport) {
07604 end = strchr(rport + 1, ';');
07605 if (end)
07606 memmove(rport, end, strlen(end) + 1);
07607 else
07608 *rport = '\0';
07609 }
07610
07611
07612 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
07613 leftmost, ast_inet_ntoa(p->recv.sin_addr),
07614 ntohs(p->recv.sin_port),
07615 others ? "," : "", others ? others : "");
07616 } else {
07617
07618 snprintf(new, sizeof(new), "%s;received=%s%s%s",
07619 leftmost, ast_inet_ntoa(p->recv.sin_addr),
07620 others ? "," : "", others ? others : "");
07621 }
07622 oh = new;
07623 }
07624 add_header(req, field, oh);
07625 copied++;
07626 }
07627 if (!copied) {
07628 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
07629 return -1;
07630 }
07631 return 0;
07632 }
07633
07634
07635 static void add_route(struct sip_request *req, struct sip_route *route)
07636 {
07637 char r[SIPBUFSIZE*2], *p;
07638 int n, rem = sizeof(r);
07639
07640 if (!route)
07641 return;
07642
07643 p = r;
07644 for (;route ; route = route->next) {
07645 n = strlen(route->hop);
07646 if (rem < n+3)
07647 break;
07648 if (p != r) {
07649 *p++ = ',';
07650 --rem;
07651 }
07652 *p++ = '<';
07653 ast_copy_string(p, route->hop, rem);
07654 p += n;
07655 *p++ = '>';
07656 rem -= (n+2);
07657 }
07658 *p = '\0';
07659 add_header(req, "Route", r);
07660 }
07661
07662
07663 static void set_destination(struct sip_pvt *p, char *uri)
07664 {
07665 char *h, *maddr, hostname[256];
07666 int port, hn;
07667 struct hostent *hp;
07668 struct ast_hostent ahp;
07669 int debug=sip_debug_test_pvt(p);
07670
07671
07672
07673
07674 if (debug)
07675 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
07676
07677
07678 h = strchr(uri, '@');
07679 if (h)
07680 ++h;
07681 else {
07682 h = uri;
07683 if (!strncasecmp(h, "sip:", 4))
07684 h += 4;
07685 else if (!strncasecmp(h, "sips:", 5))
07686 h += 5;
07687 }
07688 hn = strcspn(h, ":;>") + 1;
07689 if (hn > sizeof(hostname))
07690 hn = sizeof(hostname);
07691 ast_copy_string(hostname, h, hn);
07692
07693 h += hn - 1;
07694
07695
07696 if (*h == ':') {
07697
07698 ++h;
07699 port = strtol(h, &h, 10);
07700 }
07701 else
07702 port = STANDARD_SIP_PORT;
07703
07704
07705 maddr = strstr(h, "maddr=");
07706 if (maddr) {
07707 maddr += 6;
07708 hn = strspn(maddr, "0123456789.") + 1;
07709 if (hn > sizeof(hostname))
07710 hn = sizeof(hostname);
07711 ast_copy_string(hostname, maddr, hn);
07712 }
07713
07714 hp = ast_gethostbyname(hostname, &ahp);
07715 if (hp == NULL) {
07716 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
07717 return;
07718 }
07719 p->sa.sin_family = AF_INET;
07720 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
07721 p->sa.sin_port = htons(port);
07722 if (debug)
07723 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(p->sa.sin_addr), port);
07724 }
07725
07726
07727 static int init_resp(struct sip_request *resp, const char *msg)
07728 {
07729
07730 memset(resp, 0, sizeof(*resp));
07731 resp->method = SIP_RESPONSE;
07732 resp->header[0] = resp->data;
07733 snprintf(resp->header[0], sizeof(resp->data), "SIP/2.0 %s\r\n", msg);
07734 resp->len = strlen(resp->header[0]);
07735 resp->headers++;
07736 return 0;
07737 }
07738
07739
07740 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
07741 {
07742
07743 memset(req, 0, sizeof(*req));
07744 req->method = sipmethod;
07745 req->header[0] = req->data;
07746 snprintf(req->header[0], sizeof(req->data), "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
07747 req->len = strlen(req->header[0]);
07748 req->headers++;
07749 return 0;
07750 }
07751
07752
07753 static inline int resp_needs_contact(const char *msg, enum sipmethod method) {
07754
07755
07756
07757
07758
07759
07760
07761
07762
07763
07764
07765
07766
07767
07768
07769 switch (method) {
07770
07771 case SIP_INVITE:
07772 case SIP_UPDATE:
07773 case SIP_SUBSCRIBE:
07774 case SIP_NOTIFY:
07775 if ((msg[0] >= '1' && msg[0] <= '3') || !strncmp(msg, "485", 3))
07776 return 1;
07777 break;
07778
07779
07780 case SIP_REGISTER:
07781 case SIP_OPTIONS:
07782 if (msg[0] == '2' || msg[0] == '3' || !strncmp(msg, "485", 3))
07783 return 1;
07784 break;
07785
07786
07787 case SIP_BYE:
07788 case SIP_PRACK:
07789 case SIP_MESSAGE:
07790 case SIP_PUBLISH:
07791 if (msg[0] == '3' || !strncmp(msg, "485", 3))
07792 return 1;
07793 break;
07794
07795
07796 case SIP_REFER:
07797 if (msg[0] >= '2' && msg[0] <= '6')
07798 return 1;
07799 break;
07800
07801
07802 case SIP_ACK:
07803 case SIP_CANCEL:
07804 case SIP_INFO:
07805 case SIP_PING:
07806 default:
07807 return 0;
07808 }
07809 return 0;
07810 }
07811
07812
07813
07814 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
07815 {
07816 char newto[256];
07817 const char *ot;
07818
07819 init_resp(resp, msg);
07820 copy_via_headers(p, resp, req, "Via");
07821 if (msg[0] == '1' || msg[0] == '2')
07822 copy_all_header(resp, req, "Record-Route");
07823 copy_header(resp, req, "From");
07824 ot = get_header(req, "To");
07825 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
07826
07827
07828 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
07829 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
07830 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
07831 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
07832 else
07833 ast_copy_string(newto, ot, sizeof(newto));
07834 ot = newto;
07835 }
07836 add_header(resp, "To", ot);
07837 copy_header(resp, req, "Call-ID");
07838 copy_header(resp, req, "CSeq");
07839 if (!ast_strlen_zero(global_useragent))
07840 add_header(resp, "User-Agent", global_useragent);
07841 add_header(resp, "Allow", ALLOWED_METHODS);
07842 add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
07843
07844
07845 if (p->method == SIP_INVITE && p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE) {
07846 char se_hdr[256];
07847 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
07848 strefresher2str(p->stimer->st_ref));
07849 add_header(resp, "Require", "timer");
07850 add_header(resp, "Session-Expires", se_hdr);
07851 }
07852
07853 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
07854
07855
07856 char tmp[256];
07857
07858 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
07859 add_header(resp, "Expires", tmp);
07860 if (p->expiry) {
07861 char contact[SIPBUFSIZE];
07862 snprintf(contact, sizeof(contact), "%s;expires=%d", p->our_contact, p->expiry);
07863 add_header(resp, "Contact", contact);
07864 }
07865 } else if (!ast_strlen_zero(p->our_contact) && resp_needs_contact(msg, p->method)) {
07866 add_header(resp, "Contact", p->our_contact);
07867 }
07868
07869 if (!ast_strlen_zero(p->url)) {
07870 add_header(resp, "Access-URL", p->url);
07871 ast_string_field_set(p, url, NULL);
07872 }
07873
07874 return 0;
07875 }
07876
07877
07878 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
07879 {
07880 struct sip_request *orig = &p->initreq;
07881 char stripped[80];
07882 char tmp[80];
07883 char newto[256];
07884 const char *c;
07885 const char *ot, *of;
07886 int is_strict = FALSE;
07887 int is_outbound = ast_test_flag(&p->flags[0], SIP_OUTGOING);
07888
07889 memset(req, 0, sizeof(struct sip_request));
07890
07891 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
07892
07893 if (!seqno) {
07894 p->ocseq++;
07895 seqno = p->ocseq;
07896 }
07897
07898
07899
07900
07901
07902 if (sipmethod == SIP_CANCEL || (sipmethod == SIP_INVITE && p->options && !ast_strlen_zero(p->options->auth))) {
07903 p->branch = p->invite_branch;
07904 build_via(p);
07905 } else if (newbranch) {
07906 p->branch ^= ast_random();
07907 build_via(p);
07908 }
07909
07910
07911 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop, ";lr") == NULL) {
07912 is_strict = TRUE;
07913 if (sipdebug)
07914 ast_debug(1, "Strict routing enforced for session %s\n", p->callid);
07915 }
07916
07917 if (sipmethod == SIP_CANCEL)
07918 c = p->initreq.rlPart2;
07919 else if (sipmethod == SIP_ACK) {
07920
07921
07922 if (!ast_strlen_zero(p->okcontacturi))
07923 c = is_strict ? p->route->hop : p->okcontacturi;
07924 else
07925 c = p->initreq.rlPart2;
07926 } else if (!ast_strlen_zero(p->okcontacturi))
07927 c = is_strict ? p->route->hop : p->okcontacturi;
07928 else if (!ast_strlen_zero(p->uri))
07929 c = p->uri;
07930 else {
07931 char *n;
07932
07933 ast_copy_string(stripped, get_header(orig, is_outbound ? "To" : "From"),
07934 sizeof(stripped));
07935 n = get_in_brackets(stripped);
07936 c = remove_uri_parameters(n);
07937 }
07938 init_req(req, sipmethod, c);
07939
07940 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
07941
07942 add_header(req, "Via", p->via);
07943 if (p->route) {
07944 set_destination(p, p->route->hop);
07945 add_route(req, is_strict ? p->route->next : p->route);
07946 }
07947 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07948
07949 ot = get_header(orig, "To");
07950 of = get_header(orig, "From");
07951
07952
07953
07954 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
07955
07956
07957 if (is_outbound && !ast_strlen_zero(p->theirtag))
07958 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
07959 else if (!is_outbound)
07960 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
07961 else
07962 snprintf(newto, sizeof(newto), "%s", ot);
07963 ot = newto;
07964 }
07965
07966 if (is_outbound) {
07967 add_header(req, "From", of);
07968 add_header(req, "To", ot);
07969 } else {
07970 add_header(req, "From", ot);
07971 add_header(req, "To", of);
07972 }
07973
07974 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
07975 add_header(req, "Contact", p->our_contact);
07976
07977 copy_header(req, orig, "Call-ID");
07978 add_header(req, "CSeq", tmp);
07979
07980 if (!ast_strlen_zero(global_useragent))
07981 add_header(req, "User-Agent", global_useragent);
07982
07983 if (!ast_strlen_zero(p->rpid))
07984 add_header(req, "Remote-Party-ID", p->rpid);
07985
07986 if (!ast_strlen_zero(p->url)) {
07987 add_header(req, "Access-URL", p->url);
07988 ast_string_field_set(p, url, NULL);
07989 }
07990
07991
07992
07993
07994
07995
07996
07997
07998 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE
07999 && sipmethod == SIP_INVITE) {
08000 char se_hdr[256];
08001 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
08002 strefresher2str(p->stimer->st_ref));
08003 add_header(req, "Require", "timer");
08004 add_header(req, "Session-Expires", se_hdr);
08005 snprintf(se_hdr, sizeof(se_hdr), "%d", st_get_se(p, FALSE));
08006 add_header(req, "Min-SE", se_hdr);
08007 }
08008
08009 return 0;
08010 }
08011
08012
08013 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
08014 {
08015 struct sip_request resp;
08016 int seqno = 0;
08017
08018 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
08019 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
08020 return -1;
08021 }
08022 respprep(&resp, p, msg, req);
08023 add_header_contentLength(&resp, 0);
08024
08025
08026 if (p->method == SIP_INVITE && msg[0] != '1' && p->owner && p->owner->hangupcause) {
08027 char buf[10];
08028
08029 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
08030 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
08031 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
08032 }
08033 return send_response(p, &resp, reliable, seqno);
08034 }
08035
08036 static int temp_pvt_init(void *data)
08037 {
08038 struct sip_pvt *p = data;
08039
08040 p->do_history = 0;
08041 return ast_string_field_init(p, 512);
08042 }
08043
08044 static void temp_pvt_cleanup(void *data)
08045 {
08046 struct sip_pvt *p = data;
08047
08048 ast_string_field_free_memory(p);
08049
08050 ast_free(data);
08051 }
08052
08053
08054 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)
08055 {
08056 struct sip_pvt *p = NULL;
08057
08058 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
08059 ast_log(LOG_NOTICE, "Failed to get temporary pvt\n");
08060 return -1;
08061 }
08062
08063
08064
08065
08066
08067
08068
08069
08070
08071 p->method = intended_method;
08072
08073 if (!sin)
08074 p->ourip = internip;
08075 else {
08076 p->sa = *sin;
08077 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
08078 }
08079
08080 p->branch = ast_random();
08081 make_our_tag(p->tag, sizeof(p->tag));
08082 p->ocseq = INITIAL_CSEQ;
08083
08084 if (useglobal_nat && sin) {
08085 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
08086 p->recv = *sin;
08087 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
08088 }
08089
08090 ast_string_field_set(p, fromdomain, default_fromdomain);
08091 build_via(p);
08092 ast_string_field_set(p, callid, callid);
08093
08094 copy_socket_data(&p->socket, &req->socket);
08095
08096
08097 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
08098
08099
08100 ast_string_field_init(p, 0);
08101
08102 return 0;
08103 }
08104
08105
08106 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
08107 {
08108 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
08109 }
08110
08111
08112 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
08113 {
08114 struct sip_request resp;
08115 respprep(&resp, p, msg, req);
08116 append_date(&resp);
08117 add_header(&resp, "Unsupported", unsupported);
08118 add_header_contentLength(&resp, 0);
08119 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
08120 }
08121
08122
08123 static int transmit_response_with_minse(struct sip_pvt *p, const char *msg, const struct sip_request *req, int minse_int)
08124 {
08125 struct sip_request resp;
08126 char minse_str[20];
08127
08128 respprep(&resp, p, msg, req);
08129 append_date(&resp);
08130
08131 snprintf(minse_str, sizeof(minse_str), "%d", minse_int);
08132 add_header(&resp, "Min-SE", minse_str);
08133
08134 add_header_contentLength(&resp, 0);
08135 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
08136 }
08137
08138
08139
08140
08141
08142 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
08143 {
08144 return __transmit_response(p, msg, req, req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL);
08145 }
08146
08147
08148 static void append_date(struct sip_request *req)
08149 {
08150 char tmpdat[256];
08151 struct tm tm;
08152 time_t t = time(NULL);
08153
08154 gmtime_r(&t, &tm);
08155 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
08156 add_header(req, "Date", tmpdat);
08157 }
08158
08159
08160 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
08161 {
08162 struct sip_request resp;
08163 respprep(&resp, p, msg, req);
08164 append_date(&resp);
08165 add_header_contentLength(&resp, 0);
08166 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
08167 }
08168
08169
08170 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
08171 {
08172 struct sip_request resp;
08173 respprep(&resp, p, msg, req);
08174 add_header(&resp, "Accept", "application/sdp");
08175 add_header_contentLength(&resp, 0);
08176 return send_response(p, &resp, reliable, 0);
08177 }
08178
08179
08180 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)
08181 {
08182 struct sip_request resp;
08183 char tmp[512];
08184 int seqno = 0;
08185
08186 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
08187 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
08188 return -1;
08189 }
08190
08191
08192 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
08193 respprep(&resp, p, msg, req);
08194 add_header(&resp, header, tmp);
08195 add_header_contentLength(&resp, 0);
08196 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
08197 return send_response(p, &resp, reliable, seqno);
08198 }
08199
08200
08201 static int add_text(struct sip_request *req, const char *text)
08202 {
08203
08204 add_header(req, "Content-Type", "text/plain");
08205 add_header_contentLength(req, strlen(text));
08206 add_line(req, text);
08207 return 0;
08208 }
08209
08210
08211
08212
08213
08214 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode)
08215 {
08216 char tmp[256];
08217 int event;
08218 if (mode) {
08219
08220 if (digit == '*')
08221 event = 10;
08222 else if (digit == '#')
08223 event = 11;
08224 else if ((digit >= 'A') && (digit <= 'D'))
08225 event = 12 + digit - 'A';
08226 else
08227 event = atoi(&digit);
08228 snprintf(tmp, sizeof(tmp), "%d\r\n", event);
08229 add_header(req, "Content-Type", "application/dtmf");
08230 add_header_contentLength(req, strlen(tmp));
08231 add_line(req, tmp);
08232 } else {
08233
08234 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
08235 add_header(req, "Content-Type", "application/dtmf-relay");
08236 add_header_contentLength(req, strlen(tmp));
08237 add_line(req, tmp);
08238 }
08239 return 0;
08240 }
08241
08242
08243
08244 static int add_vidupdate(struct sip_request *req)
08245 {
08246 const char *xml_is_a_huge_waste_of_space =
08247 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
08248 " <media_control>\r\n"
08249 " <vc_primitive>\r\n"
08250 " <to_encoder>\r\n"
08251 " <picture_fast_update>\r\n"
08252 " </picture_fast_update>\r\n"
08253 " </to_encoder>\r\n"
08254 " </vc_primitive>\r\n"
08255 " </media_control>\r\n";
08256 add_header(req, "Content-Type", "application/media_control+xml");
08257 add_header_contentLength(req, strlen(xml_is_a_huge_waste_of_space));
08258 add_line(req, xml_is_a_huge_waste_of_space);
08259 return 0;
08260 }
08261
08262
08263 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
08264 struct ast_str **m_buf, struct ast_str **a_buf,
08265 int debug, int *min_packet_size)
08266 {
08267 int rtp_code;
08268 struct ast_format_list fmt;
08269
08270
08271 if (debug)
08272 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
08273 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
08274 return;
08275
08276 if (p->rtp) {
08277 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
08278 fmt = ast_codec_pref_getsize(pref, codec);
08279 } else
08280 return;
08281 ast_str_append(m_buf, 0, " %d", rtp_code);
08282 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
08283 ast_rtp_lookup_mime_subtype(1, codec,
08284 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
08285 sample_rate);
08286 if (codec == AST_FORMAT_G729A) {
08287
08288 ast_str_append(a_buf, 0, "a=fmtp:%d annexb=no\r\n", rtp_code);
08289 } else if (codec == AST_FORMAT_G723_1) {
08290
08291 ast_str_append(a_buf, 0, "a=fmtp:%d annexa=no\r\n", rtp_code);
08292 } else if (codec == AST_FORMAT_ILBC) {
08293
08294 ast_str_append(a_buf, 0, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
08295 }
08296
08297 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
08298 *min_packet_size = fmt.cur_ms;
08299
08300
08301 if ((*min_packet_size)==0 && fmt.cur_ms)
08302 *min_packet_size = fmt.cur_ms;
08303 }
08304
08305
08306
08307 static void add_vcodec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
08308 struct ast_str **m_buf, struct ast_str **a_buf,
08309 int debug, int *min_packet_size)
08310 {
08311 int rtp_code;
08312
08313 if (!p->vrtp)
08314 return;
08315
08316 if (debug)
08317 ast_verbose("Adding video codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
08318
08319 if ((rtp_code = ast_rtp_lookup_code(p->vrtp, 1, codec)) == -1)
08320 return;
08321
08322 ast_str_append(m_buf, 0, " %d", rtp_code);
08323 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
08324 ast_rtp_lookup_mime_subtype(1, codec, 0), sample_rate);
08325
08326 }
08327
08328
08329 static void add_tcodec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
08330 struct ast_str **m_buf, struct ast_str **a_buf,
08331 int debug, int *min_packet_size)
08332 {
08333 int rtp_code;
08334
08335 if (!p->trtp)
08336 return;
08337
08338 if (debug)
08339 ast_verbose("Adding text codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
08340
08341 if ((rtp_code = ast_rtp_lookup_code(p->trtp, 1, codec)) == -1)
08342 return;
08343
08344 ast_str_append(m_buf, 0, " %d", rtp_code);
08345 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
08346 ast_rtp_lookup_mime_subtype(1, codec, 0), sample_rate);
08347
08348 }
08349
08350
08351
08352 static int t38_get_rate(int t38cap)
08353 {
08354 int maxrate = (t38cap & (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400));
08355
08356 if (maxrate & T38FAX_RATE_14400) {
08357 ast_debug(2, "T38MaxBitRate 14400 found\n");
08358 return 14400;
08359 } else if (maxrate & T38FAX_RATE_12000) {
08360 ast_debug(2, "T38MaxBitRate 12000 found\n");
08361 return 12000;
08362 } else if (maxrate & T38FAX_RATE_9600) {
08363 ast_debug(2, "T38MaxBitRate 9600 found\n");
08364 return 9600;
08365 } else if (maxrate & T38FAX_RATE_7200) {
08366 ast_debug(2, "T38MaxBitRate 7200 found\n");
08367 return 7200;
08368 } else if (maxrate & T38FAX_RATE_4800) {
08369 ast_debug(2, "T38MaxBitRate 4800 found\n");
08370 return 4800;
08371 } else if (maxrate & T38FAX_RATE_2400) {
08372 ast_debug(2, "T38MaxBitRate 2400 found\n");
08373 return 2400;
08374 } else {
08375 ast_debug(2, "Strange, T38MaxBitRate NOT found in peers T38 SDP.\n");
08376 return 0;
08377 }
08378 }
08379
08380
08381 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
08382 struct ast_str **m_buf, struct ast_str **a_buf,
08383 int debug)
08384 {
08385 int rtp_code;
08386
08387 if (debug)
08388 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format, 0));
08389 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
08390 return;
08391
08392 ast_str_append(m_buf, 0, " %d", rtp_code);
08393 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
08394 ast_rtp_lookup_mime_subtype(0, format, 0),
08395 sample_rate);
08396 if (format == AST_RTP_DTMF)
08397 ast_str_append(a_buf, 0, "a=fmtp:%d 0-16\r\n", rtp_code);
08398 }
08399
08400
08401
08402
08403 static void get_our_media_address(struct sip_pvt *p, int needvideo,
08404 struct sockaddr_in *sin, struct sockaddr_in *vsin, struct sockaddr_in *tsin,
08405 struct sockaddr_in *dest, struct sockaddr_in *vdest)
08406 {
08407
08408 ast_rtp_get_us(p->rtp, sin);
08409 if (p->vrtp)
08410 ast_rtp_get_us(p->vrtp, vsin);
08411 if (p->trtp)
08412 ast_rtp_get_us(p->trtp, tsin);
08413
08414
08415
08416 if (p->redirip.sin_addr.s_addr) {
08417 dest->sin_port = p->redirip.sin_port;
08418 dest->sin_addr = p->redirip.sin_addr;
08419 } else {
08420 dest->sin_addr = p->ourip.sin_addr;
08421 dest->sin_port = sin->sin_port;
08422 }
08423 if (needvideo) {
08424
08425 if (p->vredirip.sin_addr.s_addr) {
08426 vdest->sin_addr = p->vredirip.sin_addr;
08427 vdest->sin_port = p->vredirip.sin_port;
08428 } else {
08429 vdest->sin_addr = p->ourip.sin_addr;
08430 vdest->sin_port = vsin->sin_port;
08431 }
08432 }
08433
08434 }
08435
08436
08437
08438
08439
08440
08441 #define SDP_SAMPLE_RATE(x) 8000
08442
08443
08444
08445
08446
08447
08448
08449 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
08450 {
08451 int len = 0;
08452 int alreadysent = 0;
08453
08454 struct sockaddr_in sin;
08455 struct sockaddr_in vsin;
08456 struct sockaddr_in tsin;
08457 struct sockaddr_in dest;
08458 struct sockaddr_in udptlsin;
08459 struct sockaddr_in vdest = { 0, };
08460 struct sockaddr_in tdest = { 0, };
08461 struct sockaddr_in udptldest = { 0, };
08462
08463
08464 char *version = "v=0\r\n";
08465 char subject[256];
08466 char owner[256];
08467 char connection[256];
08468 char *stime = "t=0 0\r\n";
08469 char bandwidth[256] = "";
08470 char *hold = "";
08471 struct ast_str *m_audio = ast_str_alloca(256);
08472 struct ast_str *m_video = ast_str_alloca(256);
08473 struct ast_str *m_text = ast_str_alloca(256);
08474 struct ast_str *m_modem = ast_str_alloca(256);
08475 struct ast_str *a_audio = ast_str_alloca(1024);
08476 struct ast_str *a_video = ast_str_alloca(1024);
08477 struct ast_str *a_text = ast_str_alloca(1024);
08478 struct ast_str *a_modem = ast_str_alloca(1024);
08479
08480 int x;
08481 int capability = 0;
08482 int needaudio = FALSE;
08483 int needvideo = FALSE;
08484 int needtext = FALSE;
08485 int debug = sip_debug_test_pvt(p);
08486 int min_audio_packet_size = 0;
08487 int min_video_packet_size = 0;
08488 int min_text_packet_size = 0;
08489
08490 char codecbuf[SIPBUFSIZE];
08491 char buf[SIPBUFSIZE];
08492
08493
08494 snprintf(subject, sizeof(subject), "s=%s\r\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
08495
08496 if (!p->rtp) {
08497 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
08498 return AST_FAILURE;
08499 }
08500
08501
08502
08503
08504 if (!p->sessionid) {
08505 p->sessionid = (int)ast_random();
08506 p->sessionversion = p->sessionid;
08507 } else {
08508 if (oldsdp == FALSE)
08509 p->sessionversion++;
08510 }
08511
08512 get_our_media_address(p, needvideo, &sin, &vsin, &tsin, &dest, &vdest);
08513
08514 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));
08515 snprintf(connection, sizeof(connection), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
08516
08517 if (add_audio) {
08518 capability = p->jointcapability;
08519
08520
08521 ast_debug(1, "** Our capability: %s Video flag: %s Text flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability),
08522 p->novideo ? "True" : "False", p->notext ? "True" : "False");
08523 ast_debug(1, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
08524
08525 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
08526 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_RTP)) {
08527 ast_str_append(&m_audio, 0, " %d", 191);
08528 ast_str_append(&a_audio, 0, "a=rtpmap:%d %s/%d\r\n", 191, "t38", 8000);
08529 }
08530 #endif
08531
08532
08533 if (capability & AST_FORMAT_AUDIO_MASK)
08534 needaudio = TRUE;
08535
08536
08537 if ((capability & AST_FORMAT_VIDEO_MASK) && !p->novideo) {
08538 if (p->vrtp) {
08539 needvideo = TRUE;
08540 ast_debug(2, "This call needs video offers!\n");
08541 } else
08542 ast_debug(2, "This call needs video offers, but there's no video support enabled!\n");
08543 }
08544
08545 if (debug)
08546 ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(sin.sin_port));
08547
08548
08549
08550 if (needvideo) {
08551 ast_str_append(&m_video, 0, "m=video %d RTP/AVP", ntohs(vdest.sin_port));
08552
08553
08554 if (p->maxcallbitrate)
08555 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
08556 if (debug)
08557 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(vsin.sin_port));
08558 }
08559
08560
08561 if((capability & AST_FORMAT_TEXT_MASK) && !p->notext) {
08562 if (sipdebug_text)
08563 ast_verbose("We think we can do text\n");
08564 if (p->trtp) {
08565 if (sipdebug_text)
08566 ast_verbose("And we have a text rtp object\n");
08567 needtext = TRUE;
08568 ast_debug(2, "This call needs text offers! \n");
08569 } else
08570 ast_debug(2, "This call needs text offers, but there's no text support enabled ! \n");
08571 }
08572
08573
08574
08575 if (needtext) {
08576 if (sipdebug_text)
08577 ast_verbose("Lets set up the text sdp\n");
08578
08579 if (p->tredirip.sin_addr.s_addr) {
08580 tdest.sin_addr = p->tredirip.sin_addr;
08581 tdest.sin_port = p->tredirip.sin_port;
08582 } else {
08583 tdest.sin_addr = p->ourip.sin_addr;
08584 tdest.sin_port = tsin.sin_port;
08585 }
08586 ast_str_append(&m_text, 0, "m=text %d RTP/AVP", ntohs(tdest.sin_port));
08587
08588 if (debug)
08589 ast_verbose("Text is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(tsin.sin_port));
08590
08591 }
08592
08593
08594
08595
08596
08597
08598 ast_str_append(&m_audio, 0, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
08599
08600 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR)
08601 hold = "a=recvonly\r\n";
08602 else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE)
08603 hold = "a=inactive\r\n";
08604 else
08605 hold = "a=sendrecv\r\n";
08606
08607
08608
08609
08610
08611
08612
08613
08614
08615
08616 if (capability & p->prefcodec) {
08617 int codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
08618
08619 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
08620 &m_audio, &a_audio,
08621 debug, &min_audio_packet_size);
08622 alreadysent |= codec;
08623 }
08624
08625
08626 for (x = 0; x < 32; x++) {
08627 int codec;
08628
08629 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
08630 break;
08631
08632 if (!(capability & codec))
08633 continue;
08634
08635 if (alreadysent & codec)
08636 continue;
08637
08638 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
08639 &m_audio, &a_audio,
08640 debug, &min_audio_packet_size);
08641 alreadysent |= codec;
08642 }
08643
08644
08645 for (x = 1; x <= (needtext ? AST_FORMAT_TEXT_MASK : (needvideo ? AST_FORMAT_VIDEO_MASK : AST_FORMAT_AUDIO_MASK)); x <<= 1) {
08646 if (!(capability & x))
08647 continue;
08648
08649 if (alreadysent & x)
08650 continue;
08651
08652 if (x & AST_FORMAT_AUDIO_MASK)
08653 add_codec_to_sdp(p, x, SDP_SAMPLE_RATE(x),
08654 &m_audio, &a_audio, debug, &min_audio_packet_size);
08655 else if (x & AST_FORMAT_VIDEO_MASK)
08656 add_vcodec_to_sdp(p, x, 90000,
08657 &m_video, &a_video, debug, &min_video_packet_size);
08658 else if (x & AST_FORMAT_TEXT_MASK)
08659 add_tcodec_to_sdp(p, x, 1000,
08660 &m_text, &a_text, debug, &min_text_packet_size);
08661 }
08662
08663
08664 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
08665 if (!(p->jointnoncodeccapability & x))
08666 continue;
08667
08668 add_noncodec_to_sdp(p, x, 8000, &m_audio, &a_audio, debug);
08669 }
08670
08671 ast_debug(3, "-- Done with adding codecs to SDP\n");
08672
08673 if (!p->owner || !ast_internal_timing_enabled(p->owner))
08674 ast_str_append(&a_audio, 0, "a=silenceSupp:off - - - -\r\n");
08675
08676 if (min_audio_packet_size)
08677 ast_str_append(&a_audio, 0, "a=ptime:%d\r\n", min_audio_packet_size);
08678
08679
08680 if (min_video_packet_size)
08681 ast_str_append(&a_video, 0, "a=ptime:%d\r\n", min_video_packet_size);
08682
08683
08684 if (min_text_packet_size)
08685 ast_str_append(&a_text, 0, "a=ptime:%d\r\n", min_text_packet_size);
08686 }
08687
08688 if (add_t38) {
08689 ast_udptl_get_us(p->udptl, &udptlsin);
08690
08691
08692 if (p->udptlredirip.sin_addr.s_addr) {
08693 udptldest.sin_port = p->udptlredirip.sin_port;
08694 udptldest.sin_addr = p->udptlredirip.sin_addr;
08695 } else {
08696 udptldest.sin_addr = p->ourip.sin_addr;
08697 udptldest.sin_port = udptlsin.sin_port;
08698 }
08699
08700 if (debug)
08701 ast_debug(1, "T.38 UDPTL is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(udptlsin.sin_port));
08702
08703
08704
08705
08706 if (debug) {
08707 ast_debug(1, "Our T38 capability (%d), peer T38 capability (%d), joint capability (%d)\n",
08708 p->t38.capability,
08709 p->t38.peercapability,
08710 p->t38.jointcapability);
08711 }
08712
08713 ast_str_append(&m_modem, 0, "m=image %d udptl t38\r\n", ntohs(udptldest.sin_port));
08714
08715 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_0)
08716 ast_str_append(&a_modem, 0, "a=T38FaxVersion:0\r\n");
08717 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_1)
08718 ast_str_append(&a_modem, 0, "a=T38FaxVersion:1\r\n");
08719 if ((x = t38_get_rate(p->t38.jointcapability)))
08720 ast_str_append(&a_modem, 0, "a=T38MaxBitRate:%d\r\n", x);
08721 if ((p->t38.jointcapability & T38FAX_FILL_BIT_REMOVAL) == T38FAX_FILL_BIT_REMOVAL)
08722 ast_str_append(&a_modem, 0, "a=T38FaxFillBitRemoval\r\n");
08723 if ((p->t38.jointcapability & T38FAX_TRANSCODING_MMR) == T38FAX_TRANSCODING_MMR)
08724 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingMMR\r\n");
08725 if ((p->t38.jointcapability & T38FAX_TRANSCODING_JBIG) == T38FAX_TRANSCODING_JBIG)
08726 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingJBIG\r\n");
08727 ast_str_append(&a_modem, 0, "a=T38FaxRateManagement:%s\r\n", (p->t38.jointcapability & T38FAX_RATE_MANAGEMENT_LOCAL_TCF) ? "localTCF" : "transferredTCF");
08728 x = ast_udptl_get_local_max_datagram(p->udptl);
08729 ast_str_append(&a_modem, 0, "a=T38FaxMaxBuffer:%d\r\n", x);
08730 ast_str_append(&a_modem, 0, "a=T38FaxMaxDatagram:%d\r\n", x);
08731 if (p->t38.jointcapability != T38FAX_UDP_EC_NONE)
08732 ast_str_append(&a_modem, 0, "a=T38FaxUdpEC:%s\r\n", (p->t38.jointcapability & T38FAX_UDP_EC_REDUNDANCY) ? "t38UDPRedundancy" : "t38UDPFEC");
08733 }
08734
08735 if (m_audio->len - m_audio->used < 2 || m_video->len - m_video->used < 2 ||
08736 m_text->len - m_text->used < 2 || a_text->len - a_text->used < 2 ||
08737 a_audio->len - a_audio->used < 2 || a_video->len - a_video->used < 2)
08738 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
08739
08740 if (needaudio)
08741 ast_str_append(&m_audio, 0, "\r\n");
08742 if (needvideo)
08743 ast_str_append(&m_video, 0, "\r\n");
08744 if (needtext)
08745 ast_str_append(&m_text, 0, "\r\n");
08746
08747 len = strlen(version) + strlen(subject) + strlen(owner) +
08748 strlen(connection) + strlen(stime);
08749 if (needaudio)
08750 len += m_audio->used + a_audio->used + strlen(hold);
08751 if (needvideo)
08752 len += m_video->used + a_video->used + strlen(bandwidth) + strlen(hold);
08753 if (needtext)
08754 len += m_text->used + a_text->used + strlen(hold);
08755 if (add_t38)
08756 len += m_modem->used + a_modem->used;
08757
08758 add_header(resp, "Content-Type", "application/sdp");
08759 add_header_contentLength(resp, len);
08760 add_line(resp, version);
08761 add_line(resp, owner);
08762 add_line(resp, subject);
08763 add_line(resp, connection);
08764 if (needvideo)
08765 add_line(resp, bandwidth);
08766 add_line(resp, stime);
08767 if (needaudio) {
08768 add_line(resp, m_audio->str);
08769 add_line(resp, a_audio->str);
08770 add_line(resp, hold);
08771 }
08772 if (needvideo) {
08773 add_line(resp, m_video->str);
08774 add_line(resp, a_video->str);
08775 add_line(resp, hold);
08776 }
08777 if (needtext) {
08778 add_line(resp, m_text->str);
08779 add_line(resp, a_text->str);
08780 add_line(resp, hold);
08781 }
08782 if (add_t38) {
08783 add_line(resp, m_modem->str);
08784 add_line(resp, a_modem->str);
08785 }
08786
08787
08788 p->lastrtprx = p->lastrtptx = time(NULL);
08789
08790 ast_debug(3, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
08791
08792 return AST_SUCCESS;
08793 }
08794
08795
08796 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
08797 {
08798 struct sip_request resp;
08799 int seqno;
08800
08801 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
08802 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
08803 return -1;
08804 }
08805 respprep(&resp, p, msg, req);
08806 if (p->udptl) {
08807 ast_udptl_offered_from_local(p->udptl, 0);
08808 add_sdp(&resp, p, 0, 0, 1);
08809 } else
08810 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
08811 if (retrans && !p->pendinginvite)
08812 p->pendinginvite = seqno;
08813 return send_response(p, &resp, retrans, seqno);
08814 }
08815
08816
08817 static void copy_request(struct sip_request *dst, const struct sip_request *src)
08818 {
08819 long offset;
08820 int x;
08821 offset = ((void *)dst) - ((void *)src);
08822
08823 memcpy(dst, src, sizeof(*dst));
08824
08825 for (x=0; x < src->headers; x++)
08826 dst->header[x] += offset;
08827 for (x=0; x < src->lines; x++)
08828 dst->line[x] += offset;
08829 dst->rlPart1 += offset;
08830 dst->rlPart2 += offset;
08831 }
08832
08833
08834
08835
08836 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp)
08837 {
08838 struct sip_request resp;
08839 int seqno;
08840 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
08841 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
08842 return -1;
08843 }
08844 respprep(&resp, p, msg, req);
08845 if (p->rtp) {
08846 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
08847 ast_debug(1, "Setting framing from config on incoming call\n");
08848 ast_rtp_codec_setpref(p->rtp, &p->prefs);
08849 }
08850 try_suggested_sip_codec(p);
08851 if (p->t38.state == T38_PEER_DIRECT || p->t38.state == T38_ENABLED) {
08852 add_sdp(&resp, p, oldsdp, TRUE, TRUE);
08853 } else {
08854 add_sdp(&resp, p, oldsdp, TRUE, FALSE);
08855 }
08856 } else
08857 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
08858 if (reliable && !p->pendinginvite)
08859 p->pendinginvite = seqno;
08860 return send_response(p, &resp, reliable, seqno);
08861 }
08862
08863
08864 static int determine_firstline_parts(struct sip_request *req)
08865 {
08866 char *e = ast_skip_blanks(req->header[0]);
08867
08868 if (!*e)
08869 return -1;
08870 req->rlPart1 = e;
08871 e = ast_skip_nonblanks(e);
08872 if (*e)
08873 *e++ = '\0';
08874
08875 e = ast_skip_blanks(e);
08876 if ( !*e )
08877 return -1;
08878 ast_trim_blanks(e);
08879
08880 if (!strcasecmp(req->rlPart1, "SIP/2.0") ) {
08881 if (strlen(e) < 3)
08882 return -1;
08883 req->rlPart2 = e;
08884 } else {
08885 if ( *e == '<' ) {
08886 ast_debug(3, "Oops. Bogus uri in <> %s\n", e);
08887 e++;
08888 if (!*e)
08889 return -1;
08890 }
08891 req->rlPart2 = e;
08892 e = ast_skip_nonblanks(e);
08893 if (*e)
08894 *e++ = '\0';
08895 e = ast_skip_blanks(e);
08896 if (strcasecmp(e, "SIP/2.0") ) {
08897 ast_debug(3, "Skipping packet - Bad request protocol %s\n", e);
08898 return -1;
08899 }
08900 }
08901 return 1;
08902 }
08903
08904
08905
08906
08907
08908
08909
08910
08911
08912
08913
08914
08915
08916
08917 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp)
08918 {
08919 struct sip_request req;
08920
08921 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
08922
08923 add_header(&req, "Allow", ALLOWED_METHODS);
08924 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
08925 if (sipdebug) {
08926 if (oldsdp == TRUE)
08927 add_header(&req, "X-asterisk-Info", "SIP re-invite (Session-Timers)");
08928 else
08929 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
08930 }
08931
08932 if (p->do_history)
08933 append_history(p, "ReInv", "Re-invite sent");
08934 if (t38version)
08935 add_sdp(&req, p, oldsdp, FALSE, TRUE);
08936 else
08937 add_sdp(&req, p, oldsdp, TRUE, FALSE);
08938
08939
08940 initialize_initreq(p, &req);
08941 p->lastinvite = p->ocseq;
08942 ast_set_flag(&p->flags[0], SIP_OUTGOING);
08943
08944 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
08945 }
08946
08947
08948 static char *remove_uri_parameters(char *uri)
08949 {
08950 char *atsign;
08951 atsign = strchr(uri, '@');
08952 if (!atsign)
08953 atsign = uri;
08954 atsign = strchr(atsign, ';');
08955 if (atsign)
08956 *atsign = '\0';
08957 return uri;
08958 }
08959
08960
08961 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
08962 {
08963 char stripped[SIPBUFSIZE];
08964 char *c;
08965
08966 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
08967 c = get_in_brackets(stripped);
08968
08969 c = remove_uri_parameters(c);
08970 if (!ast_strlen_zero(c))
08971 ast_string_field_set(p, uri, c);
08972
08973 }
08974
08975
08976 static void build_contact(struct sip_pvt *p)
08977 {
08978 int ourport = ntohs(p->ourip.sin_port);
08979
08980 if (!sip_standard_port(p->socket.type, ourport)) {
08981 if (p->socket.type == SIP_TRANSPORT_UDP)
08982 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);
08983 else
08984 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));
08985 } else {
08986 if (p->socket.type == SIP_TRANSPORT_UDP)
08987 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));
08988 else
08989 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));
08990 }
08991 }
08992
08993
08994 static void build_rpid(struct sip_pvt *p)
08995 {
08996 int send_pres_tags = TRUE;
08997 const char *privacy=NULL;
08998 const char *screen=NULL;
08999 char buf[256];
09000 const char *clid = default_callerid;
09001 const char *clin = NULL;
09002 const char *fromdomain;
09003
09004 if (!ast_strlen_zero(p->rpid) || !ast_strlen_zero(p->rpid_from))
09005 return;
09006
09007 if (p->owner && p->owner->cid.cid_num)
09008 clid = p->owner->cid.cid_num;
09009 if (p->owner && p->owner->cid.cid_name)
09010 clin = p->owner->cid.cid_name;
09011 if (ast_strlen_zero(clin))
09012 clin = clid;
09013
09014 switch (p->callingpres) {
09015 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
09016 privacy = "off";
09017 screen = "no";
09018 break;
09019 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
09020 privacy = "off";
09021 screen = "yes";
09022 break;
09023 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
09024 privacy = "off";
09025 screen = "no";
09026 break;
09027 case AST_PRES_ALLOWED_NETWORK_NUMBER:
09028 privacy = "off";
09029 screen = "yes";
09030 break;
09031 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
09032 privacy = "full";
09033 screen = "no";
09034 break;
09035 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
09036 privacy = "full";
09037 screen = "yes";
09038 break;
09039 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
09040 privacy = "full";
09041 screen = "no";
09042 break;
09043 case AST_PRES_PROHIB_NETWORK_NUMBER:
09044 privacy = "full";
09045 screen = "yes";
09046 break;
09047 case AST_PRES_NUMBER_NOT_AVAILABLE:
09048 send_pres_tags = FALSE;
09049 break;
09050 default:
09051 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
09052 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
09053 privacy = "full";
09054 else
09055 privacy = "off";
09056 screen = "no";
09057 break;
09058 }
09059
09060 fromdomain = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip.sin_addr));
09061
09062 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
09063 if (send_pres_tags)
09064 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
09065 ast_string_field_set(p, rpid, buf);
09066
09067 ast_string_field_build(p, rpid_from, "\"%s\" <sip:%s@%s>;tag=%s", clin,
09068 S_OR(p->fromuser, clid),
09069 fromdomain, p->tag);
09070 }
09071
09072
09073 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
09074 {
09075 struct ast_str *invite = ast_str_alloca(256);
09076 char from[256];
09077 char to[256];
09078 char tmp_n[SIPBUFSIZE/2];
09079 char tmp_l[SIPBUFSIZE/2];
09080 const char *l = NULL;
09081 const char *n = NULL;
09082 const char *urioptions = "";
09083 int ourport;
09084
09085 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
09086 const char *s = p->username;
09087
09088
09089
09090
09091
09092
09093 if (*s == '+')
09094 s++;
09095 for (; *s; s++) {
09096 if (!strchr(AST_DIGIT_ANYNUM, *s) )
09097 break;
09098 }
09099
09100 if (!*s)
09101 urioptions = ";user=phone";
09102 }
09103
09104
09105 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
09106
09107 if (p->owner) {
09108 l = p->owner->cid.cid_num;
09109 n = p->owner->cid.cid_name;
09110 }
09111
09112 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID) &&
09113 ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
09114 l = CALLERID_UNKNOWN;
09115 n = l;
09116 }
09117 if (ast_strlen_zero(l))
09118 l = default_callerid;
09119 if (ast_strlen_zero(n))
09120 n = l;
09121
09122 if (!ast_strlen_zero(p->fromuser))
09123 l = p->fromuser;
09124 else
09125 ast_string_field_set(p, fromuser, l);
09126
09127
09128 if (!ast_strlen_zero(p->fromname))
09129 n = p->fromname;
09130 else
09131 ast_string_field_set(p, fromname, n);
09132
09133 if (pedanticsipchecking) {
09134 ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
09135 n = tmp_n;
09136 ast_uri_encode(l, tmp_l, sizeof(tmp_l), 0);
09137 l = tmp_l;
09138 }
09139
09140 ourport = ntohs(p->ourip.sin_port);
09141 if (!sip_standard_port(p->socket.type, ourport) && ast_strlen_zero(p->fromdomain))
09142 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, ast_inet_ntoa(p->ourip.sin_addr), ourport, p->tag);
09143 else
09144 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip.sin_addr)), p->tag);
09145
09146
09147 if (!ast_strlen_zero(p->fullcontact)) {
09148
09149 ast_str_append(&invite, 0, "%s", p->fullcontact);
09150 } else {
09151
09152 ast_str_append(&invite, 0, "sip:");
09153 if (!ast_strlen_zero(p->username)) {
09154 n = p->username;
09155 if (pedanticsipchecking) {
09156 ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
09157 n = tmp_n;
09158 }
09159 ast_str_append(&invite, 0, "%s@", n);
09160 }
09161 ast_str_append(&invite, 0, "%s", p->tohost);
09162 if (ntohs(p->sa.sin_port) != STANDARD_SIP_PORT)
09163 ast_str_append(&invite, 0, ":%d", ntohs(p->sa.sin_port));
09164 ast_str_append(&invite, 0, "%s", urioptions);
09165 }
09166
09167
09168 if (p->options && !ast_strlen_zero(p->options->uri_options))
09169 ast_str_append(&invite, 0, ";%s", p->options->uri_options);
09170
09171
09172
09173
09174 ast_string_field_set(p, uri, invite->str);
09175
09176 if (!ast_strlen_zero(p->todnid)) {
09177
09178 if (!strchr(p->todnid, '@')) {
09179
09180 snprintf(to, sizeof(to), "<sip:%s@%s>%s%s", p->todnid, p->tohost, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
09181 } else {
09182 snprintf(to, sizeof(to), "<sip:%s>%s%s", p->todnid, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
09183 }
09184 } else {
09185 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
09186
09187 snprintf(to, sizeof(to), "<%s%s>;tag=%s", (!strncasecmp(p->uri, "sip:", 4) ? "" : "sip:"), p->uri, p->theirtag);
09188 } else if (p->options && p->options->vxml_url) {
09189
09190 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
09191 } else
09192 snprintf(to, sizeof(to), "<%s>", p->uri);
09193 }
09194
09195 init_req(req, sipmethod, p->uri);
09196
09197 snprintf(tmp_n, sizeof(tmp_n), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
09198
09199 add_header(req, "Via", p->via);
09200 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
09201
09202
09203
09204
09205 if (ast_test_flag(&p->flags[0], SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
09206 build_rpid(p);
09207 add_header(req, "From", p->rpid_from);
09208 } else
09209 add_header(req, "From", from);
09210 add_header(req, "To", to);
09211 ast_string_field_set(p, exten, l);
09212 build_contact(p);
09213 add_header(req, "Contact", p->our_contact);
09214 add_header(req, "Call-ID", p->callid);
09215 add_header(req, "CSeq", tmp_n);
09216 if (!ast_strlen_zero(global_useragent))
09217 add_header(req, "User-Agent", global_useragent);
09218 if (!ast_strlen_zero(p->rpid))
09219 add_header(req, "Remote-Party-ID", p->rpid);
09220 }
09221
09222
09223
09224
09225
09226
09227
09228
09229 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
09230 {
09231 struct sip_request req;
09232
09233 req.method = sipmethod;
09234 if (init) {
09235 p->branch ^= ast_random();
09236 p->invite_branch = p->branch;
09237 build_via(p);
09238 }
09239 if (init > 1)
09240 initreqprep(&req, p, sipmethod);
09241 else
09242
09243 reqprep(&req, p, sipmethod, 0, init ? 0 : 1);
09244
09245 if (p->options && p->options->auth)
09246 add_header(&req, p->options->authheader, p->options->auth);
09247 append_date(&req);
09248 if (sipmethod == SIP_REFER) {
09249 if (p->refer) {
09250 char buf[SIPBUFSIZE];
09251 if (!ast_strlen_zero(p->refer->refer_to))
09252 add_header(&req, "Refer-To", p->refer->refer_to);
09253 if (!ast_strlen_zero(p->refer->referred_by)) {
09254 snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
09255 add_header(&req, "Referred-By", buf);
09256 }
09257 }
09258 }
09259
09260
09261 if (p->options && !ast_strlen_zero(p->options->replaces)) {
09262 add_header(&req, "Replaces", p->options->replaces);
09263 add_header(&req, "Require", "replaces");
09264 }
09265
09266
09267 if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
09268 char i2astr[10];
09269
09270 if (!p->stimer->st_interval)
09271 p->stimer->st_interval = st_get_se(p, TRUE);
09272
09273 p->stimer->st_active = TRUE;
09274
09275 snprintf(i2astr, sizeof(i2astr), "%d", p->stimer->st_interval);
09276 add_header(&req, "Session-Expires", i2astr);
09277 snprintf(i2astr, sizeof(i2astr), "%d", st_get_se(p, FALSE));
09278 add_header(&req, "Min-SE", i2astr);
09279 }
09280
09281 add_header(&req, "Allow", ALLOWED_METHODS);
09282 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
09283 if (p->options && p->options->addsipheaders && p->owner) {
09284 struct ast_channel *chan = p->owner;
09285 struct varshead *headp;
09286
09287 ast_channel_lock(chan);
09288
09289 headp = &chan->varshead;
09290
09291 if (!headp)
09292 ast_log(LOG_WARNING, "No Headp for the channel...ooops!\n");
09293 else {
09294 const struct ast_var_t *current;
09295 AST_LIST_TRAVERSE(headp, current, entries) {
09296
09297 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
09298 char *content, *end;
09299 const char *header = ast_var_value(current);
09300 char *headdup = ast_strdupa(header);
09301
09302
09303 if (*headdup == '"')
09304 headdup++;
09305 if ((content = strchr(headdup, ':'))) {
09306 *content++ = '\0';
09307 content = ast_skip_blanks(content);
09308
09309 end = content + strlen(content) -1;
09310 if (*end == '"')
09311 *end = '\0';
09312
09313 add_header(&req, headdup, content);
09314 if (sipdebug)
09315 ast_debug(1, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
09316 }
09317 }
09318 }
09319 }
09320
09321 ast_channel_unlock(chan);
09322 }
09323 if (sdp) {
09324 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
09325 ast_udptl_offered_from_local(p->udptl, 1);
09326 ast_debug(1, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
09327 add_sdp(&req, p, FALSE, FALSE, TRUE);
09328 } else if (p->rtp)
09329 add_sdp(&req, p, FALSE, TRUE, FALSE);
09330 } else {
09331 add_header_contentLength(&req, 0);
09332 }
09333
09334 if (!p->initreq.headers || init > 2)
09335 initialize_initreq(p, &req);
09336 p->lastinvite = p->ocseq;
09337 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
09338 }
09339
09340
09341 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
09342 {
09343 struct ast_str *tmp = ast_str_alloca(4000);
09344 char from[256], to[256];
09345 char *c, *mfrom, *mto;
09346 struct sip_request req;
09347 char hint[AST_MAX_EXTENSION];
09348 char *statestring = "terminated";
09349 const struct cfsubscription_types *subscriptiontype;
09350 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
09351 char *pidfstate = "--";
09352 char *pidfnote= "Ready";
09353
09354 memset(from, 0, sizeof(from));
09355 memset(to, 0, sizeof(to));
09356
09357 switch (state) {
09358 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
09359 statestring = (global_notifyringing) ? "early" : "confirmed";
09360 local_state = NOTIFY_INUSE;
09361 pidfstate = "busy";
09362 pidfnote = "Ringing";
09363 break;
09364 case AST_EXTENSION_RINGING:
09365 statestring = "early";
09366 local_state = NOTIFY_INUSE;
09367 pidfstate = "busy";
09368 pidfnote = "Ringing";
09369 break;
09370 case AST_EXTENSION_INUSE:
09371 statestring = "confirmed";
09372 local_state = NOTIFY_INUSE;
09373 pidfstate = "busy";
09374 pidfnote = "On the phone";
09375 break;
09376 case AST_EXTENSION_BUSY:
09377 statestring = "confirmed";
09378 local_state = NOTIFY_CLOSED;
09379 pidfstate = "busy";
09380 pidfnote = "On the phone";
09381 break;
09382 case AST_EXTENSION_UNAVAILABLE:
09383 statestring = "terminated";
09384 local_state = NOTIFY_CLOSED;
09385 pidfstate = "away";
09386 pidfnote = "Unavailable";
09387 break;
09388 case AST_EXTENSION_ONHOLD:
09389 statestring = "confirmed";
09390 local_state = NOTIFY_CLOSED;
09391 pidfstate = "busy";
09392 pidfnote = "On hold";
09393 break;
09394 case AST_EXTENSION_NOT_INUSE:
09395 default:
09396
09397 break;
09398 }
09399
09400 subscriptiontype = find_subscription_type(p->subscribed);
09401
09402
09403 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
09404 char *hint2 = hint, *individual_hint = NULL;
09405 int hint_count = 0, unavailable_count = 0;
09406
09407 while ((individual_hint = strsep(&hint2, "&"))) {
09408 hint_count++;
09409
09410 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE)
09411 unavailable_count++;
09412 }
09413
09414
09415
09416
09417 if (hint_count > 0 && hint_count == unavailable_count) {
09418 local_state = NOTIFY_CLOSED;
09419 pidfstate = "away";
09420 pidfnote = "Not online";
09421 }
09422 }
09423
09424 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
09425 c = get_in_brackets(from);
09426 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
09427 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
09428 return -1;
09429 }
09430
09431 mfrom = remove_uri_parameters(c);
09432
09433 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
09434 c = get_in_brackets(to);
09435 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
09436 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
09437 return -1;
09438 }
09439 mto = remove_uri_parameters(c);
09440
09441 reqprep(&req, p, SIP_NOTIFY, 0, 1);
09442
09443
09444 add_header(&req, "Event", subscriptiontype->event);
09445 add_header(&req, "Content-Type", subscriptiontype->mediatype);
09446 switch(state) {
09447 case AST_EXTENSION_DEACTIVATED:
09448 if (timeout)
09449 add_header(&req, "Subscription-State", "terminated;reason=timeout");
09450 else {
09451 add_header(&req, "Subscription-State", "terminated;reason=probation");
09452 add_header(&req, "Retry-After", "60");
09453 }
09454 break;
09455 case AST_EXTENSION_REMOVED:
09456 add_header(&req, "Subscription-State", "terminated;reason=noresource");
09457 break;
09458 default:
09459 if (p->expiry)
09460 add_header(&req, "Subscription-State", "active");
09461 else
09462 add_header(&req, "Subscription-State", "terminated;reason=timeout");
09463 }
09464 switch (p->subscribed) {
09465 case XPIDF_XML:
09466 case CPIM_PIDF_XML:
09467 ast_str_append(&tmp, 0,
09468 "<?xml version=\"1.0\"?>\n"
09469 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
09470 "<presence>\n");
09471 ast_str_append(&tmp, 0, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
09472 ast_str_append(&tmp, 0, "<atom id=\"%s\">\n", p->exten);
09473 ast_str_append(&tmp, 0, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
09474 ast_str_append(&tmp, 0, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
09475 ast_str_append(&tmp, 0, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
09476 ast_str_append(&tmp, 0, "</address>\n</atom>\n</presence>\n");
09477 break;
09478 case PIDF_XML:
09479 ast_str_append(&tmp, 0,
09480 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
09481 "<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);
09482 ast_str_append(&tmp, 0, "<pp:person><status>\n");
09483 if (pidfstate[0] != '-')
09484 ast_str_append(&tmp, 0, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
09485 ast_str_append(&tmp, 0, "</status></pp:person>\n");
09486 ast_str_append(&tmp, 0, "<note>%s</note>\n", pidfnote);
09487 ast_str_append(&tmp, 0, "<tuple id=\"%s\">\n", p->exten);
09488 ast_str_append(&tmp, 0, "<contact priority=\"1\">%s</contact>\n", mto);
09489 if (pidfstate[0] == 'b')
09490 ast_str_append(&tmp, 0, "<status><basic>open</basic></status>\n");
09491 else
09492 ast_str_append(&tmp, 0, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
09493 ast_str_append(&tmp, 0, "</tuple>\n</presence>\n");
09494 break;
09495 case DIALOG_INFO_XML:
09496 ast_str_append(&tmp, 0, "<?xml version=\"1.0\"?>\n");
09497 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);
09498 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
09499 ast_str_append(&tmp, 0, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
09500 else
09501 ast_str_append(&tmp, 0, "<dialog id=\"%s\">\n", p->exten);
09502 ast_str_append(&tmp, 0, "<state>%s</state>\n", statestring);
09503 if (state == AST_EXTENSION_ONHOLD) {
09504 ast_str_append(&tmp, 0, "<local>\n<target uri=\"%s\">\n"
09505 "<param pname=\"+sip.rendering\" pvalue=\"no\"/>\n"
09506 "</target>\n</local>\n", mto);
09507 }
09508 ast_str_append(&tmp, 0, "</dialog>\n</dialog-info>\n");
09509 break;
09510 case NONE:
09511 default:
09512 break;
09513 }
09514
09515 add_header_contentLength(&req, tmp->used);
09516 add_line(&req, tmp->str);
09517
09518 p->pendinginvite = p->ocseq;
09519
09520 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
09521 }
09522
09523
09524
09525
09526
09527
09528
09529 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
09530 {
09531 struct sip_request req;
09532 struct ast_str *out = ast_str_alloca(500);
09533 int ourport = ntohs(p->ourip.sin_port);
09534 const char *exten = S_OR(vmexten, default_vmexten);
09535
09536 initreqprep(&req, p, SIP_NOTIFY);
09537 add_header(&req, "Event", "message-summary");
09538 add_header(&req, "Content-Type", default_notifymime);
09539 ast_str_append(&out, 0, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
09540
09541 if (!ast_strlen_zero(p->fromdomain)) {
09542 ast_str_append(&out, 0, "Message-Account: sip:%s@%s\r\n", exten, p->fromdomain);
09543 } else if (!sip_standard_port(p->socket.type, ourport)) {
09544 if (p->socket.type == SIP_TRANSPORT_UDP) {
09545 ast_str_append(&out, 0, "Message-Account: sip:%s@%s:%d\r\n", exten, ast_inet_ntoa(p->ourip.sin_addr), ourport);
09546 } else {
09547 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));
09548 }
09549 } else {
09550 if (p->socket.type == SIP_TRANSPORT_UDP) {
09551 ast_str_append(&out, 0, "Message-Account: sip:%s@%s\r\n", exten, ast_inet_ntoa(p->ourip.sin_addr));
09552 } else {
09553 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));
09554 }
09555 }
09556
09557
09558
09559 ast_str_append(&out, 0, "Voice-Message: %d/%d%s\r\n",
09560 newmsgs, oldmsgs, (ast_test_flag(&p->flags[1], SIP_PAGE2_BUGGY_MWI) ? "" : " (0/0)"));
09561
09562 if (p->subscribed) {
09563 if (p->expiry)
09564 add_header(&req, "Subscription-State", "active");
09565 else
09566 add_header(&req, "Subscription-State", "terminated;reason=timeout");
09567 }
09568
09569 add_header_contentLength(&req, out->used);
09570 add_line(&req, out->str);
09571
09572 if (!p->initreq.headers)
09573 initialize_initreq(p, &req);
09574 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
09575 }
09576
09577
09578 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req)
09579 {
09580 if (!p->initreq.headers)
09581 initialize_initreq(p, req);
09582 return send_request(p, req, XMIT_UNRELIABLE, p->ocseq);
09583 }
09584
09585
09586 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
09587 {
09588 struct sip_request req;
09589 char tmp[SIPBUFSIZE/2];
09590
09591 reqprep(&req, p, SIP_NOTIFY, 0, 1);
09592 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
09593 add_header(&req, "Event", tmp);
09594 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
09595 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
09596 add_header(&req, "Allow", ALLOWED_METHODS);
09597 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
09598
09599 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
09600 add_header_contentLength(&req, strlen(tmp));
09601 add_line(&req, tmp);
09602
09603 if (!p->initreq.headers)
09604 initialize_initreq(p, &req);
09605
09606 p->lastnoninvite = p->ocseq;
09607
09608 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
09609 }
09610
09611 static const struct _map_x_s regstatestrings[] = {
09612 { REG_STATE_FAILED, "Failed" },
09613 { REG_STATE_UNREGISTERED, "Unregistered"},
09614 { REG_STATE_REGSENT, "Request Sent"},
09615 { REG_STATE_AUTHSENT, "Auth. Sent"},
09616 { REG_STATE_REGISTERED, "Registered"},
09617 { REG_STATE_REJECTED, "Rejected"},
09618 { REG_STATE_TIMEOUT, "Timeout"},
09619 { REG_STATE_NOAUTH, "No Authentication"},
09620 { -1, NULL }
09621 };
09622
09623
09624 static const char *regstate2str(enum sipregistrystate regstate)
09625 {
09626 return map_x_s(regstatestrings, regstate, "Unknown");
09627 }
09628
09629
09630
09631
09632
09633
09634
09635 static int sip_reregister(const void *data)
09636 {
09637
09638 struct sip_registry *r= registry_addref((struct sip_registry *) data);
09639
09640
09641 if (!r)
09642 return 0;
09643
09644 if (r->call && r->call->do_history)
09645 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
09646
09647
09648 if (sipdebug)
09649 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
09650
09651 r->expire = -1;
09652 __sip_do_register(r);
09653 registry_unref(r);
09654 return 0;
09655 }
09656
09657
09658 static int __sip_do_register(struct sip_registry *r)
09659 {
09660 int res;
09661
09662 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
09663 return res;
09664 }
09665
09666
09667
09668
09669
09670
09671
09672 static int sip_reg_timeout(const void *data)
09673 {
09674
09675
09676 struct sip_registry *r = registry_addref((struct sip_registry *) data);
09677 struct sip_pvt *p;
09678 int res;
09679
09680
09681 if (!r)
09682 return 0;
09683
09684 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
09685
09686
09687
09688
09689 if (r->call) {
09690
09691
09692 p = r->call;
09693 sip_pvt_lock(p);
09694 p->needdestroy = 1;
09695
09696 __sip_pretend_ack(p);
09697 sip_pvt_unlock(p);
09698
09699
09700
09701 if (p->registry)
09702 p->registry = registry_unref(p->registry);
09703 r->call = dialog_unref(r->call);
09704 }
09705
09706 if (global_regattempts_max && r->regattempts > global_regattempts_max) {
09707
09708
09709
09710 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
09711 r->regstate = REG_STATE_FAILED;
09712 } else {
09713 r->regstate = REG_STATE_UNREGISTERED;
09714 r->timeout = -1;
09715 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
09716 }
09717 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));
09718 registry_unref(r);
09719 return 0;
09720 }
09721
09722
09723
09724
09725 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
09726 {
09727 struct sip_request req;
09728 char from[256];
09729 char to[256];
09730 char tmp[80];
09731 char addr[80];
09732 struct sip_pvt *p;
09733 char *fromdomain;
09734
09735
09736 if ( r == NULL || ((auth==NULL) && (r->regstate==REG_STATE_REGSENT || r->regstate==REG_STATE_AUTHSENT))) {
09737 if (r) {
09738 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
09739 }
09740 return 0;
09741 }
09742
09743 if (r->call) {
09744 if (!auth) {
09745 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
09746 return 0;
09747 } else {
09748 p = r->call;
09749 make_our_tag(p->tag, sizeof(p->tag));
09750 ast_string_field_set(p, theirtag, NULL);
09751 }
09752 } else {
09753
09754 if (!r->callid_valid) {
09755 build_callid_registry(r, internip.sin_addr, default_fromdomain);
09756 r->callid_valid = TRUE;
09757 }
09758
09759 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER, NULL))) {
09760 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
09761 return 0;
09762 }
09763
09764 if (p->do_history)
09765 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
09766
09767 p->outboundproxy = obproxy_get(p, NULL);
09768
09769
09770 if (create_addr(p, r->hostname, 0)) {
09771
09772
09773 sip_destroy(p);
09774 if (r->timeout > -1) {
09775 AST_SCHED_REPLACE(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r);
09776 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
09777 } else {
09778 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
09779 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);
09780 }
09781 r->regattempts++;
09782 return 0;
09783 }
09784
09785 ast_string_field_set(r, callid, p->callid);
09786 if (r->portno) {
09787 p->sa.sin_port = htons(r->portno);
09788 p->recv.sin_port = htons(r->portno);
09789 } else
09790 r->portno = ntohs(p->sa.sin_port);
09791 ast_set_flag(&p->flags[0], SIP_OUTGOING);
09792 r->call = dialog_ref(p);
09793 p->registry = registry_addref(r);
09794 if (!ast_strlen_zero(r->secret))
09795 ast_string_field_set(p, peersecret, r->secret);
09796 if (!ast_strlen_zero(r->md5secret))
09797 ast_string_field_set(p, peermd5secret, r->md5secret);
09798
09799
09800 if (!ast_strlen_zero(r->authuser)) {
09801 ast_string_field_set(p, peername, r->authuser);
09802 ast_string_field_set(p, authname, r->authuser);
09803 } else if (!ast_strlen_zero(r->username)) {
09804 ast_string_field_set(p, peername, r->username);
09805 ast_string_field_set(p, authname, r->username);
09806 ast_string_field_set(p, fromuser, r->username);
09807 }
09808 if (!ast_strlen_zero(r->username))
09809 ast_string_field_set(p, username, r->username);
09810
09811 if (!ast_strlen_zero(r->callback))
09812 ast_string_field_set(p, exten, r->callback);
09813
09814
09815 set_socket_transport(&p->socket, r->transport);
09816 if (r->transport == SIP_TRANSPORT_TLS || r->transport == SIP_TRANSPORT_TCP) {
09817 p->socket.port = sip_tcp_desc.sin.sin_port;
09818 }
09819
09820
09821
09822
09823
09824 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
09825 build_contact(p);
09826 }
09827
09828
09829 if (auth == NULL) {
09830 if (r->timeout > -1)
09831 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
09832 AST_SCHED_REPLACE(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r);
09833 ast_debug(1, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
09834 }
09835
09836 if ((fromdomain = strchr(r->username, '@'))) {
09837
09838 fromdomain++ ;
09839
09840 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
09841 if (!ast_strlen_zero(p->theirtag))
09842 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
09843 else
09844 snprintf(to, sizeof(to), "<sip:%s>", r->username);
09845
09846
09847
09848 if (ast_strlen_zero(p->fromdomain)) {
09849 ast_string_field_set(p, fromdomain, fromdomain);
09850 }
09851 } else {
09852 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
09853 if (!ast_strlen_zero(p->theirtag))
09854 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
09855 else
09856 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
09857 }
09858
09859
09860
09861 if (!ast_strlen_zero(p->fromdomain)) {
09862 if (r->portno && r->portno != STANDARD_SIP_PORT)
09863 snprintf(addr, sizeof(addr), "sip:%s:%d", p->fromdomain, r->portno);
09864 else
09865 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
09866 } else {
09867 if (r->portno && r->portno != STANDARD_SIP_PORT)
09868 snprintf(addr, sizeof(addr), "sip:%s:%d", r->hostname, r->portno);
09869 else
09870 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
09871 }
09872 ast_string_field_set(p, uri, addr);
09873
09874 p->branch ^= ast_random();
09875
09876 init_req(&req, sipmethod, addr);
09877
09878
09879 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
09880 p->ocseq = r->ocseq;
09881
09882 build_via(p);
09883 add_header(&req, "Via", p->via);
09884 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
09885 add_header(&req, "From", from);
09886 add_header(&req, "To", to);
09887 add_header(&req, "Call-ID", p->callid);
09888 add_header(&req, "CSeq", tmp);
09889 if (!ast_strlen_zero(global_useragent))
09890 add_header(&req, "User-Agent", global_useragent);
09891
09892
09893 if (auth)
09894 add_header(&req, authheader, auth);
09895 else if (!ast_strlen_zero(r->nonce)) {
09896 char digest[1024];
09897
09898
09899
09900
09901
09902
09903 if (sipdebug)
09904 ast_debug(1, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
09905 ast_string_field_set(p, realm, r->realm);
09906 ast_string_field_set(p, nonce, r->nonce);
09907 ast_string_field_set(p, domain, r->domain);
09908 ast_string_field_set(p, opaque, r->opaque);
09909 ast_string_field_set(p, qop, r->qop);
09910 p->noncecount = ++r->noncecount;
09911
09912 memset(digest, 0, sizeof(digest));
09913 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
09914 add_header(&req, "Authorization", digest);
09915 else
09916 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
09917
09918 }
09919
09920 snprintf(tmp, sizeof(tmp), "%d", r->expiry);
09921 add_header(&req, "Expires", tmp);
09922 add_header(&req, "Contact", p->our_contact);
09923 add_header(&req, "Event", "registration");
09924 add_header_contentLength(&req, 0);
09925
09926 initialize_initreq(p, &req);
09927 if (sip_debug_test_pvt(p)) {
09928 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
09929 }
09930 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
09931 r->regattempts++;
09932 ast_debug(4, "REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
09933
09934 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
09935 }
09936
09937
09938 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
09939 {
09940 struct sip_request req;
09941
09942 reqprep(&req, p, SIP_MESSAGE, 0, 1);
09943 add_text(&req, text);
09944 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
09945 }
09946
09947
09948 static int sip_refer_allocate(struct sip_pvt *p)
09949 {
09950 p->refer = ast_calloc(1, sizeof(struct sip_refer));
09951 return p->refer ? 1 : 0;
09952 }
09953
09954
09955
09956
09957
09958
09959 static int transmit_refer(struct sip_pvt *p, const char *dest)
09960 {
09961 struct sip_request req = {
09962 .headers = 0,
09963 };
09964 char from[256];
09965 const char *of;
09966 char *c;
09967 char referto[256];
09968 char *ttag, *ftag;
09969 char *theirtag = ast_strdupa(p->theirtag);
09970
09971 if (sipdebug)
09972 ast_debug(1, "SIP transfer of %s to %s\n", p->callid, dest);
09973
09974
09975 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
09976 of = get_header(&p->initreq, "To");
09977 ttag = theirtag;
09978 ftag = p->tag;
09979 } else {
09980 of = get_header(&p->initreq, "From");
09981 ftag = theirtag;
09982 ttag = p->tag;
09983 }
09984
09985 ast_copy_string(from, of, sizeof(from));
09986 of = get_in_brackets(from);
09987 ast_string_field_set(p, from, of);
09988 if (!strncasecmp(of, "sip:", 4))
09989 of += 4;
09990 else if (!strncasecmp(of, "sips:", 5))
09991 of += 5;
09992 else
09993 ast_log(LOG_NOTICE, "From address missing 'sip(s):', using it anyway\n");
09994
09995 if ((c = strchr(dest, '@')))
09996 c = NULL;
09997 else if ((c = strchr(of, '@')))
09998 *c++ = '\0';
09999 if (c)
10000 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
10001 else
10002 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
10003
10004
10005 sip_refer_allocate(p);
10006 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
10007 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
10008 p->refer->status = REFER_SENT;
10009
10010 reqprep(&req, p, SIP_REFER, 0, 1);
10011
10012 add_header(&req, "Refer-To", referto);
10013 add_header(&req, "Allow", ALLOWED_METHODS);
10014 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
10015 if (!ast_strlen_zero(p->our_contact))
10016 add_header(&req, "Referred-By", p->our_contact);
10017
10018 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
10019
10020
10021
10022
10023
10024
10025
10026
10027 }
10028
10029
10030
10031 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
10032 {
10033 struct sip_request req;
10034
10035 reqprep(&req, p, SIP_INFO, 0, 1);
10036 add_digit(&req, digit, duration, (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_SHORTINFO));
10037 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
10038 }
10039
10040
10041 static int transmit_info_with_vidupdate(struct sip_pvt *p)
10042 {
10043 struct sip_request req;
10044
10045 reqprep(&req, p, SIP_INFO, 0, 1);
10046 add_vidupdate(&req);
10047 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
10048 }
10049
10050
10051
10052
10053 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
10054 {
10055 struct sip_request resp;
10056
10057 if (sipmethod == SIP_ACK)
10058 p->invitestate = INV_CONFIRMED;
10059
10060 reqprep(&resp, p, sipmethod, seqno, newbranch);
10061 if (sipmethod == SIP_CANCEL && p->answered_elsewhere)
10062 add_header(&resp, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
10063
10064 add_header_contentLength(&resp, 0);
10065 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
10066 }
10067
10068
10069 static void auth_headers(enum sip_auth_type code, char **header, char **respheader)
10070 {
10071 if (code == WWW_AUTH) {
10072 *header = "WWW-Authenticate";
10073 *respheader = "Authorization";
10074 } else if (code == PROXY_AUTH) {
10075 *header = "Proxy-Authenticate";
10076 *respheader = "Proxy-Authorization";
10077 } else {
10078 ast_verbose("-- wrong response code %d\n", code);
10079 *header = *respheader = "Invalid";
10080 }
10081 }
10082
10083
10084 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
10085 {
10086 struct sip_request resp;
10087
10088 reqprep(&resp, p, sipmethod, seqno, newbranch);
10089 if (!ast_strlen_zero(p->realm)) {
10090 char digest[1024];
10091
10092 memset(digest, 0, sizeof(digest));
10093 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
10094 char *dummy, *response;
10095 enum sip_auth_type code = p->options ? p->options->auth_type : PROXY_AUTH;
10096 auth_headers(code, &dummy, &response);
10097 add_header(&resp, response, digest);
10098 } else
10099 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
10100 }
10101
10102
10103 if (sipmethod == SIP_BYE) {
10104 char buf[10];
10105
10106 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->hangupcause));
10107 snprintf(buf, sizeof(buf), "%d", p->hangupcause);
10108 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
10109 }
10110
10111 add_header_contentLength(&resp, 0);
10112 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
10113 }
10114
10115
10116 static void destroy_association(struct sip_peer *peer)
10117 {
10118 int realtimeregs = ast_check_realtime("sipregs");
10119 char *tablename = (realtimeregs) ? "sipregs" : "sippeers";
10120
10121 if (!sip_cfg.ignore_regexpire) {
10122 if (peer->rt_fromcontact && sip_cfg.peer_rtupdate) {
10123 ast_update_realtime(tablename, "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", peer->deprecated_username ? "username" : "defaultuser", "", "regserver", "", NULL);
10124 ast_update_realtime(tablename, "name", peer->name, "lastms", "", NULL);
10125 } else {
10126 ast_db_del("SIP/Registry", peer->name);
10127 }
10128 }
10129 }
10130 static void set_socket_transport(struct sip_socket *socket, int transport)
10131 {
10132
10133 if (socket->type != transport) {
10134 socket->fd = -1;
10135 socket->type = transport;
10136 if (socket->tcptls_session) {
10137 ao2_ref(socket->tcptls_session, -1);
10138 socket->tcptls_session = NULL;
10139 }
10140 }
10141 }
10142
10143
10144 static int expire_register(const void *data)
10145 {
10146 struct sip_peer *peer = (struct sip_peer *)data;
10147
10148 if (!peer)
10149 return 0;
10150
10151 memset(&peer->addr, 0, sizeof(peer->addr));
10152
10153 destroy_association(peer);
10154 set_socket_transport(&peer->socket, peer->default_outbound_transport);
10155
10156 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
10157 register_peer_exten(peer, FALSE);
10158 peer->expire = -1;
10159 ast_device_state_changed("SIP/%s", peer->name);
10160
10161
10162
10163
10164 if (peer->is_realtime)
10165 ast_debug(3, "-REALTIME- peer expired registration. Name: %s. Realtime peer objects now %d\n", peer->name, rpeerobjs);
10166
10167 if (peer->selfdestruct ||
10168 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
10169 peer = ASTOBJ_CONTAINER_UNLINK(&peerl, peer);
10170 unref_peer(peer);
10171 }
10172
10173 return 0;
10174 }
10175
10176
10177 static int sip_poke_peer_s(const void *data)
10178 {
10179 struct sip_peer *peer = (struct sip_peer *)data;
10180
10181 peer->pokeexpire = -1;
10182 sip_poke_peer(peer);
10183 return 0;
10184 }
10185
10186
10187 static void reg_source_db(struct sip_peer *peer)
10188 {
10189 char data[256];
10190 struct in_addr in;
10191 int expiry;
10192 int port;
10193 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
10194
10195 if (peer->rt_fromcontact)
10196 return;
10197 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
10198 return;
10199
10200 scan = data;
10201 addr = strsep(&scan, ":");
10202 port_str = strsep(&scan, ":");
10203 expiry_str = strsep(&scan, ":");
10204 username = strsep(&scan, ":");
10205 contact = scan;
10206
10207 if (!inet_aton(addr, &in))
10208 return;
10209
10210 if (port_str)
10211 port = atoi(port_str);
10212 else
10213 return;
10214
10215 if (expiry_str)
10216 expiry = atoi(expiry_str);
10217 else
10218 return;
10219
10220 if (username)
10221 ast_copy_string(peer->username, username, sizeof(peer->username));
10222 if (contact)
10223 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
10224
10225 ast_debug(2, "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
10226 peer->name, peer->username, ast_inet_ntoa(in), port, expiry);
10227
10228 memset(&peer->addr, 0, sizeof(peer->addr));
10229 peer->addr.sin_family = AF_INET;
10230 peer->addr.sin_addr = in;
10231 peer->addr.sin_port = htons(port);
10232 if (sipsock < 0) {
10233
10234 AST_SCHED_REPLACE(peer->pokeexpire, sched, ast_random() % 5000 + 1, sip_poke_peer_s, peer);
10235 } else
10236 sip_poke_peer(peer);
10237 AST_SCHED_REPLACE(peer->expire, sched, (expiry + 10) * 1000, expire_register, peer);
10238 register_peer_exten(peer, TRUE);
10239 }
10240
10241
10242 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
10243 {
10244 char contact[SIPBUFSIZE];
10245 char *c;
10246
10247
10248 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
10249 c = get_in_brackets(contact);
10250
10251
10252 ast_string_field_set(pvt, fullcontact, c);
10253
10254
10255 ast_string_field_set(pvt, okcontacturi, c);
10256
10257
10258
10259 return TRUE;
10260 }
10261
10262 static int __set_address_from_contact(const char *fullcontact, struct sockaddr_in *sin, int tcp)
10263 {
10264 struct hostent *hp;
10265 struct ast_hostent ahp;
10266 int port;
10267 char *host, *pt;
10268 char contact_buf[256];
10269 char contact2_buf[256];
10270 char *contact, *contact2;
10271
10272
10273 ast_copy_string(contact_buf, fullcontact, sizeof(contact_buf));
10274 ast_copy_string(contact2_buf, fullcontact, sizeof(contact2_buf));
10275 contact = contact_buf;
10276 contact2 = contact2_buf;
10277
10278
10279 if (tcp) {
10280 if (parse_uri(contact, "sips:", &contact, NULL, &host, &pt, NULL, NULL)) {
10281 if (parse_uri(contact2, "sip:", &contact, NULL, &host, &pt, NULL, NULL))
10282 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
10283 }
10284 port = !ast_strlen_zero(pt) ? atoi(pt) : STANDARD_TLS_PORT;
10285 } else {
10286 if (parse_uri(contact, "sip:", &contact, NULL, &host, &pt, NULL, NULL))
10287 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
10288 port = !ast_strlen_zero(pt) ? atoi(pt) : STANDARD_SIP_PORT;
10289 }
10290
10291
10292
10293 hp = ast_gethostbyname(host, &ahp);
10294 if (!hp) {
10295 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't resolve in DNS) : '%s'\n", host);
10296 return -1;
10297 }
10298 sin->sin_family = AF_INET;
10299 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
10300 sin->sin_port = htons(port);
10301
10302 return 0;
10303 }
10304
10305
10306 static int set_address_from_contact(struct sip_pvt *pvt)
10307 {
10308 if (ast_test_flag(&pvt->flags[0], SIP_NAT_ROUTE)) {
10309
10310
10311 pvt->sa = pvt->recv;
10312 return 0;
10313 }
10314
10315 return __set_address_from_contact(pvt->fullcontact, &pvt->sa, pvt->socket.type == SIP_TRANSPORT_TLS ? 1 : 0);
10316 }
10317
10318
10319
10320 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
10321 {
10322 char contact[SIPBUFSIZE];
10323 char data[SIPBUFSIZE];
10324 const char *expires = get_header(req, "Expires");
10325 int expiry = atoi(expires);
10326 char *curi, *host, *pt, *curi2, *transport;
10327 int port;
10328 int transport_type;
10329 const char *useragent;
10330 struct hostent *hp;
10331 struct ast_hostent ahp;
10332 struct sockaddr_in oldsin, testsin;
10333
10334 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
10335
10336 if (ast_strlen_zero(expires)) {
10337 char *s = strcasestr(contact, ";expires=");
10338 if (s) {
10339 expires = strsep(&s, ";");
10340 if (sscanf(expires + 9, "%d", &expiry) != 1)
10341 expiry = default_expiry;
10342 } else {
10343
10344 expiry = default_expiry;
10345 }
10346 }
10347
10348 copy_socket_data(&pvt->socket, &req->socket);
10349
10350
10351 curi = contact;
10352 if (strchr(contact, '<') == NULL)
10353 strsep(&curi, ";");
10354 curi = get_in_brackets(contact);
10355 curi2 = ast_strdupa(curi);
10356
10357
10358
10359
10360
10361 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
10362
10363 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact))
10364 pvt->expiry = ast_sched_when(sched, peer->expire);
10365 return PARSE_REGISTER_QUERY;
10366 } else if (!strcasecmp(curi, "*") || !expiry) {
10367
10368 memset(&peer->addr, 0, sizeof(peer->addr));
10369 set_socket_transport(&peer->socket, peer->default_outbound_transport);
10370 AST_SCHED_DEL(sched, peer->expire);
10371
10372 destroy_association(peer);
10373
10374 register_peer_exten(peer, FALSE);
10375 peer->fullcontact[0] = '\0';
10376 peer->useragent[0] = '\0';
10377 peer->sipoptions = 0;
10378 peer->lastms = 0;
10379 pvt->expiry = 0;
10380
10381 ast_verb(3, "Unregistered SIP '%s'\n", peer->name);
10382 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unregistered\r\n", peer->name);
10383 return PARSE_REGISTER_UPDATE;
10384 }
10385
10386
10387 ast_copy_string(peer->fullcontact, curi, sizeof(peer->fullcontact));
10388
10389
10390 ast_string_field_build(pvt, our_contact, "<%s>", curi);
10391
10392
10393 if (pvt->socket.type == SIP_TRANSPORT_TLS) {
10394 if (parse_uri(curi, "sips:", &curi, NULL, &host, &pt, NULL, &transport)) {
10395 if (parse_uri(curi2, "sip:", &curi, NULL, &host, &pt, NULL, &transport))
10396 ast_log(LOG_NOTICE, "Not a valid SIP contact (missing sip:) trying to use anyway\n");
10397 }
10398 port = !ast_strlen_zero(pt) ? atoi(pt) : STANDARD_TLS_PORT;
10399 } else {
10400 if (parse_uri(curi, "sip:", &curi, NULL, &host, &pt, NULL, &transport))
10401 ast_log(LOG_NOTICE, "Not a valid SIP contact (missing sip:) trying to use anyway\n");
10402 port = !ast_strlen_zero(pt) ? atoi(pt) : STANDARD_SIP_PORT;
10403 }
10404
10405
10406 if ((transport_type = get_transport_str2enum(transport))) {
10407
10408
10409
10410 if (ast_strlen_zero(pt)) {
10411 port = (transport_type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT;
10412 }
10413 } else {
10414 transport_type = pvt->socket.type;
10415 }
10416
10417
10418
10419
10420 if ((peer->socket.type != transport_type) && (peer->transports & transport_type)) {
10421 set_socket_transport(&peer->socket, transport_type);
10422 }
10423
10424 oldsin = peer->addr;
10425
10426
10427
10428 hp = ast_gethostbyname(host, &ahp);
10429 if (!hp) {
10430 ast_log(LOG_WARNING, "Invalid host '%s'\n", host);
10431 *peer->fullcontact = '\0';
10432 ast_string_field_set(pvt, our_contact, "");
10433 return PARSE_REGISTER_FAILED;
10434 }
10435 memcpy(&testsin.sin_addr, hp->h_addr, sizeof(testsin.sin_addr));
10436 if ( ast_apply_ha(global_contact_ha, &testsin) != AST_SENSE_ALLOW ||
10437 ast_apply_ha(peer->contactha, &testsin) != AST_SENSE_ALLOW) {
10438 ast_log(LOG_WARNING, "Host '%s' disallowed by rule\n", host);
10439 *peer->fullcontact = '\0';
10440 ast_string_field_set(pvt, our_contact, "");
10441 return PARSE_REGISTER_FAILED;
10442 }
10443
10444 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
10445 peer->addr.sin_family = AF_INET;
10446 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
10447 peer->addr.sin_port = htons(port);
10448 } else {
10449
10450
10451 peer->addr = pvt->recv;
10452 }
10453
10454
10455
10456
10457 if ((peer->socket.type == pvt->socket.type) &&
10458 (peer->addr.sin_addr.s_addr == pvt->recv.sin_addr.s_addr) &&
10459 (peer->addr.sin_port == pvt->recv.sin_port)){
10460
10461 copy_socket_data(&peer->socket, &pvt->socket);
10462 }
10463
10464
10465 peer->sipoptions = pvt->sipoptions;
10466
10467 if (!ast_strlen_zero(curi) && ast_strlen_zero(peer->username))
10468 ast_copy_string(peer->username, curi, sizeof(peer->username));
10469
10470 AST_SCHED_DEL(sched, peer->expire);
10471 if (expiry > max_expiry)
10472 expiry = max_expiry;
10473 if (expiry < min_expiry)
10474 expiry = min_expiry;
10475 peer->expire = peer->is_realtime && !ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? -1 :
10476 ast_sched_add(sched, (expiry + 10) * 1000, expire_register, peer);
10477 pvt->expiry = expiry;
10478 snprintf(data, sizeof(data), "%s:%d:%d:%s:%s", ast_inet_ntoa(peer->addr.sin_addr), ntohs(peer->addr.sin_port), expiry, peer->username, peer->fullcontact);
10479
10480
10481
10482
10483 if (!peer->rt_fromcontact && (peer->socket.type & SIP_TRANSPORT_UDP))
10484 ast_db_put("SIP/Registry", peer->name, data);
10485 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
10486
10487
10488 if (VERBOSITY_ATLEAST(2) && inaddrcmp(&peer->addr, &oldsin)) {
10489 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));
10490 }
10491 sip_poke_peer(peer);
10492 register_peer_exten(peer, 1);
10493
10494
10495 useragent = get_header(req, "User-Agent");
10496 if (strcasecmp(useragent, peer->useragent)) {
10497 ast_copy_string(peer->useragent, useragent, sizeof(peer->useragent));
10498 ast_verb(4, "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
10499 }
10500 return PARSE_REGISTER_UPDATE;
10501 }
10502
10503
10504 static void free_old_route(struct sip_route *route)
10505 {
10506 struct sip_route *next;
10507
10508 while (route) {
10509 next = route->next;
10510 ast_free(route);
10511 route = next;
10512 }
10513 }
10514
10515
10516 static void list_route(struct sip_route *route)
10517 {
10518 if (!route)
10519 ast_verbose("list_route: no route\n");
10520 else {
10521 for (;route; route = route->next)
10522 ast_verbose("list_route: hop: <%s>\n", route->hop);
10523 }
10524 }
10525
10526
10527 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
10528 {
10529 struct sip_route *thishop, *head, *tail;
10530 int start = 0;
10531 int len;
10532 const char *rr, *contact, *c;
10533
10534
10535 if (p->route && p->route_persistant) {
10536 ast_debug(1, "build_route: Retaining previous route: <%s>\n", p->route->hop);
10537 return;
10538 }
10539
10540 if (p->route) {
10541 free_old_route(p->route);
10542 p->route = NULL;
10543 }
10544
10545
10546 p->route_persistant = 1;
10547
10548
10549
10550
10551
10552
10553 head = NULL;
10554 tail = head;
10555
10556 for (;;) {
10557
10558 rr = __get_header(req, "Record-Route", &start);
10559 if (*rr == '\0')
10560 break;
10561 for (; (rr = strchr(rr, '<')) ; rr += len) {
10562 ++rr;
10563 len = strcspn(rr, ">") + 1;
10564
10565 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
10566
10567 ast_copy_string(thishop->hop, rr, len);
10568 ast_debug(2, "build_route: Record-Route hop: <%s>\n", thishop->hop);
10569
10570 if (backwards) {
10571
10572 thishop->next = head;
10573 head = thishop;
10574
10575 if (!tail)
10576 tail = thishop;
10577 } else {
10578 thishop->next = NULL;
10579
10580 if (tail)
10581 tail->next = thishop;
10582 else
10583 head = thishop;
10584 tail = thishop;
10585 }
10586 }
10587 }
10588 }
10589
10590
10591 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop, ";lr") == NULL) ) {
10592
10593
10594 contact = get_header(req, "Contact");
10595 if (!ast_strlen_zero(contact)) {
10596 ast_debug(2, "build_route: Contact hop: %s\n", contact);
10597
10598 c = strchr(contact, '<');
10599 if (c) {
10600
10601 ++c;
10602 len = strcspn(c, ">") + 1;
10603 } else {
10604
10605 c = contact;
10606 len = strlen(contact) + 1;
10607 }
10608 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
10609
10610 ast_copy_string(thishop->hop, c, len);
10611 thishop->next = NULL;
10612
10613 if (tail)
10614 tail->next = thishop;
10615 else
10616 head = thishop;
10617 }
10618 }
10619 }
10620
10621
10622 p->route = head;
10623
10624
10625 if (sip_debug_test_pvt(p))
10626 list_route(p->route);
10627 }
10628
10629 AST_THREADSTORAGE(check_auth_buf);
10630 #define CHECK_AUTH_BUF_INITLEN 256
10631
10632
10633
10634
10635
10636
10637 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
10638 const char *secret, const char *md5secret, int sipmethod,
10639 char *uri, enum xmittype reliable, int ignore)
10640 {
10641 const char *response;
10642 char *reqheader, *respheader;
10643 const char *authtoken;
10644 char a1_hash[256];
10645 char resp_hash[256]="";
10646 char *c;
10647 int wrongnonce = FALSE;
10648 int good_response;
10649 const char *usednonce = p->randdata;
10650 struct ast_str *buf;
10651 int res;
10652
10653
10654 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
10655 struct x {
10656 const char *key;
10657 const char *s;
10658 } *i, keys[] = {
10659 [K_RESP] = { "response=", "" },
10660 [K_URI] = { "uri=", "" },
10661 [K_USER] = { "username=", "" },
10662 [K_NONCE] = { "nonce=", "" },
10663 [K_LAST] = { NULL, NULL}
10664 };
10665
10666
10667 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
10668 return AUTH_SUCCESSFUL;
10669
10670
10671
10672 response = "401 Unauthorized";
10673
10674
10675
10676
10677
10678 auth_headers(WWW_AUTH, &respheader, &reqheader);
10679
10680 authtoken = get_header(req, reqheader);
10681 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
10682
10683
10684 if (!reliable) {
10685
10686
10687 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
10688
10689 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10690 }
10691 return AUTH_CHALLENGE_SENT;
10692 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
10693
10694 ast_string_field_build(p, randdata, "%08lx", ast_random());
10695 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
10696
10697 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10698 return AUTH_CHALLENGE_SENT;
10699 }
10700
10701
10702
10703
10704
10705
10706 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN)))
10707 return AUTH_SECRET_FAILED;
10708
10709
10710 res = ast_str_set(&buf, 0, "%s", authtoken);
10711
10712 if (res == AST_DYNSTR_BUILD_FAILED)
10713 return AUTH_SECRET_FAILED;
10714
10715 c = buf->str;
10716
10717 while(c && *(c = ast_skip_blanks(c)) ) {
10718 for (i = keys; i->key != NULL; i++) {
10719 const char *separator = ",";
10720
10721 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
10722 continue;
10723
10724 c += strlen(i->key);
10725 if (*c == '"') {
10726 c++;
10727 separator = "\"";
10728 }
10729 i->s = c;
10730 strsep(&c, separator);
10731 break;
10732 }
10733 if (i->key == NULL)
10734 strsep(&c, " ,");
10735 }
10736
10737
10738 if (strcmp(username, keys[K_USER].s)) {
10739 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
10740 username, keys[K_USER].s);
10741
10742 return AUTH_USERNAME_MISMATCH;
10743 }
10744
10745
10746 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
10747 wrongnonce = TRUE;
10748 usednonce = keys[K_NONCE].s;
10749 }
10750
10751 if (!ast_strlen_zero(md5secret))
10752 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
10753 else {
10754 char a1[256];
10755 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
10756 ast_md5_hash(a1_hash, a1);
10757 }
10758
10759
10760 {
10761 char a2[256];
10762 char a2_hash[256];
10763 char resp[256];
10764
10765 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
10766 S_OR(keys[K_URI].s, uri));
10767 ast_md5_hash(a2_hash, a2);
10768 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
10769 ast_md5_hash(resp_hash, resp);
10770 }
10771
10772 good_response = keys[K_RESP].s &&
10773 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
10774 if (wrongnonce) {
10775 if (good_response) {
10776 if (sipdebug)
10777 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "To"));
10778
10779 ast_string_field_build(p, randdata, "%08lx", ast_random());
10780 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
10781 } else {
10782
10783 if (!req->ignore) {
10784 if (sipdebug)
10785 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
10786 ast_string_field_build(p, randdata, "%08lx", ast_random());
10787 } else {
10788 if (sipdebug)
10789 ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", get_header(req, "To"));
10790 }
10791 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
10792 }
10793
10794
10795 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10796 return AUTH_CHALLENGE_SENT;
10797 }
10798 if (good_response) {
10799 append_history(p, "AuthOK", "Auth challenge succesful for %s", username);
10800 return AUTH_SUCCESSFUL;
10801 }
10802
10803
10804
10805
10806
10807
10808 return AUTH_SECRET_FAILED;
10809 }
10810
10811
10812 static void sip_peer_hold(struct sip_pvt *p, int hold)
10813 {
10814 struct sip_peer *peer = find_peer(p->peername, NULL, 1, 0);
10815
10816 if (!peer)
10817 return;
10818
10819
10820 ast_atomic_fetchadd_int(&peer->onHold, (hold ? +1 : -1));
10821
10822
10823 ast_device_state_changed("SIP/%s", peer->name);
10824
10825 return;
10826 }
10827
10828
10829 static void mwi_event_cb(const struct ast_event *event, void *userdata)
10830 {
10831 struct sip_peer *peer = userdata;
10832
10833 ASTOBJ_RDLOCK(peer);
10834 sip_send_mwi_to_peer(peer, event, 0);
10835 ASTOBJ_UNLOCK(peer);
10836 }
10837
10838
10839
10840
10841 static int cb_extensionstate(char *context, char* exten, int state, void *data)
10842 {
10843 struct sip_pvt *p = data;
10844
10845 sip_pvt_lock(p);
10846
10847 switch(state) {
10848 case AST_EXTENSION_DEACTIVATED:
10849 case AST_EXTENSION_REMOVED:
10850 if (p->autokillid > -1 && sip_cancel_destroy(p))
10851 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
10852 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10853 ast_verb(2, "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
10854 p->stateid = -1;
10855 p->subscribed = NONE;
10856 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
10857 break;
10858 default:
10859 p->laststate = state;
10860 break;
10861 }
10862 if (p->subscribed != NONE) {
10863 if (!p->pendinginvite) {
10864 transmit_state_notify(p, state, 1, FALSE);
10865 } else {
10866
10867
10868 ast_set_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
10869 }
10870 }
10871 ast_verb(2, "Extension Changed %s[%s] new state %s for Notify User %s %s\n", exten, context, ast_extension_state2str(state), p->username,
10872 ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE) ? "(queued)" : "");
10873
10874 sip_pvt_unlock(p);
10875
10876 return 0;
10877 }
10878
10879
10880
10881
10882 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable)
10883 {
10884
10885
10886 const char *response = "407 Proxy Authentication Required";
10887 const char *reqheader = "Proxy-Authorization";
10888 const char *respheader = "Proxy-Authenticate";
10889 const char *authtoken;
10890 struct ast_str *buf;
10891 char *c;
10892
10893
10894 enum keys { K_NONCE, K_LAST };
10895 struct x {
10896 const char *key;
10897 const char *s;
10898 } *i, keys[] = {
10899 [K_NONCE] = { "nonce=", "" },
10900 [K_LAST] = { NULL, NULL}
10901 };
10902
10903 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
10904 response = "401 Unauthorized";
10905 reqheader = "Authorization";
10906 respheader = "WWW-Authenticate";
10907 }
10908 authtoken = get_header(req, reqheader);
10909 if (req->ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
10910
10911
10912 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
10913
10914 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10915 return;
10916 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
10917
10918 ast_string_field_build(p, randdata, "%08lx", ast_random());
10919 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
10920
10921 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10922 return;
10923 }
10924
10925 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
10926 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
10927 return;
10928 }
10929
10930
10931 if (ast_str_set(&buf, 0, "%s", authtoken) == AST_DYNSTR_BUILD_FAILED) {
10932 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
10933 return;
10934 }
10935
10936 c = buf->str;
10937
10938 while (c && *(c = ast_skip_blanks(c))) {
10939 for (i = keys; i->key != NULL; i++) {
10940 const char *separator = ",";
10941
10942 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
10943 continue;
10944 }
10945
10946 c += strlen(i->key);
10947 if (*c == '"') {
10948 c++;
10949 separator = "\"";
10950 }
10951 i->s = c;
10952 strsep(&c, separator);
10953 break;
10954 }
10955 if (i->key == NULL) {
10956 strsep(&c, " ,");
10957 }
10958 }
10959
10960
10961 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
10962 if (!req->ignore) {
10963 ast_string_field_build(p, randdata, "%08lx", ast_random());
10964 }
10965 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
10966
10967
10968 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10969 } else {
10970 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
10971 }
10972 }
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985 static char *terminate_uri(char *uri)
10986 {
10987 char *t = uri;
10988 while (*t && *t > ' ' && *t != ';')
10989 t++;
10990 *t = '\0';
10991 return uri;
10992 }
10993
10994
10995
10996
10997
10998
10999 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
11000 struct sip_request *req, char *uri)
11001 {
11002 enum check_auth_result res = AUTH_NOT_FOUND;
11003 struct sip_peer *peer;
11004 char tmp[256];
11005 char *name, *c;
11006 char *domain;
11007
11008 terminate_uri(uri);
11009
11010 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
11011 if (pedanticsipchecking)
11012 ast_uri_decode(tmp);
11013
11014 c = get_in_brackets(tmp);
11015 c = remove_uri_parameters(c);
11016
11017 if (!strncasecmp(c, "sip:", 4)) {
11018 name = c + 4;
11019 } else if (!strncasecmp(c, "sips:", 5)) {
11020 name = c + 5;
11021 } else {
11022 name = c;
11023 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(sin->sin_addr));
11024 }
11025
11026
11027
11028
11029
11030 if ((c = strchr(name, '@'))) {
11031 *c++ = '\0';
11032 domain = c;
11033 if ((c = strchr(domain, ':')))
11034 *c = '\0';
11035 if (!AST_LIST_EMPTY(&domain_list)) {
11036 if (!check_sip_domain(domain, NULL, 0)) {
11037 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
11038 return AUTH_UNKNOWN_DOMAIN;
11039 }
11040 }
11041 }
11042 c = strchr(name, ';');
11043 if (c)
11044 *c = '\0';
11045
11046 ast_string_field_set(p, exten, name);
11047 build_contact(p);
11048 peer = find_peer(name, NULL, 1, 0);
11049 if (!(peer && ast_apply_ha(peer->ha, sin))) {
11050
11051 if (peer) {
11052 unref_peer(peer);
11053 peer = NULL;
11054 res = AUTH_ACL_FAILED;
11055 } else
11056 res = AUTH_NOT_FOUND;
11057 }
11058
11059 if (peer) {
11060
11061 if (p->rtp) {
11062 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
11063 p->autoframing = peer->autoframing;
11064 }
11065 if (!peer->host_dynamic) {
11066 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
11067 res = AUTH_PEER_NOT_DYNAMIC;
11068 } else {
11069 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
11070 if (ast_test_flag(&p->flags[1], SIP_PAGE2_REGISTERTRYING))
11071 transmit_response(p, "100 Trying", req);
11072 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, req->ignore))) {
11073 if (sip_cancel_destroy(p))
11074 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
11075
11076
11077
11078 switch (parse_register_contact(p, peer, req)) {
11079 case PARSE_REGISTER_FAILED:
11080 ast_log(LOG_WARNING, "Failed to parse contact info\n");
11081 transmit_response_with_date(p, "400 Bad Request", req);
11082 peer->lastmsgssent = -1;
11083 res = 0;
11084 break;
11085 case PARSE_REGISTER_QUERY:
11086 transmit_response_with_date(p, "200 OK", req);
11087 peer->lastmsgssent = -1;
11088 res = 0;
11089 break;
11090 case PARSE_REGISTER_UPDATE:
11091 update_peer(peer, p->expiry);
11092
11093 transmit_response_with_date(p, "200 OK", req);
11094 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
11095 peer->lastmsgssent = -1;
11096 res = 0;
11097 break;
11098 }
11099
11100 if (check_request_transport(peer, req)) {
11101 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
11102 transmit_response_with_date(p, "403 Forbidden", req);
11103 res = AUTH_BAD_TRANSPORT;
11104 }
11105 }
11106 }
11107 }
11108 if (!peer && autocreatepeer) {
11109
11110 peer = temp_peer(name);
11111 if (peer) {
11112 ASTOBJ_CONTAINER_LINK(&peerl, peer);
11113 if (sip_cancel_destroy(p))
11114 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
11115 switch (parse_register_contact(p, peer, req)) {
11116 case PARSE_REGISTER_FAILED:
11117 ast_log(LOG_WARNING, "Failed to parse contact info\n");
11118 transmit_response_with_date(p, "400 Bad Request", req);
11119 peer->lastmsgssent = -1;
11120 res = 0;
11121 break;
11122 case PARSE_REGISTER_QUERY:
11123 transmit_response_with_date(p, "200 OK", req);
11124 peer->lastmsgssent = -1;
11125 res = 0;
11126 break;
11127 case PARSE_REGISTER_UPDATE:
11128
11129 transmit_response_with_date(p, "200 OK", req);
11130 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
11131 peer->lastmsgssent = -1;
11132 res = 0;
11133 break;
11134 }
11135 }
11136 }
11137 if (!peer && global_alwaysauthreject) {
11138
11139
11140
11141 transmit_response(p, "100 Trying", req);
11142
11143 sched_yield();
11144 }
11145 if (!res) {
11146 ast_device_state_changed("SIP/%s", peer->name);
11147 }
11148 if (res < 0) {
11149 switch (res) {
11150 case AUTH_SECRET_FAILED:
11151
11152 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
11153 break;
11154 case AUTH_USERNAME_MISMATCH:
11155
11156
11157
11158
11159 transmit_response(p, "403 Authentication user name does not match account name", &p->initreq);
11160 break;
11161 case AUTH_NOT_FOUND:
11162 case AUTH_PEER_NOT_DYNAMIC:
11163 case AUTH_ACL_FAILED:
11164 if (global_alwaysauthreject) {
11165 transmit_fake_auth_response(p, SIP_REGISTER, &p->initreq, XMIT_UNRELIABLE);
11166 } else {
11167
11168 if (res == AUTH_PEER_NOT_DYNAMIC)
11169 transmit_response(p, "403 Forbidden", &p->initreq);
11170 else
11171 transmit_response(p, "404 Not found", &p->initreq);
11172 }
11173 break;
11174 case AUTH_BAD_TRANSPORT:
11175 default:
11176 break;
11177 }
11178 }
11179 if (peer)
11180 unref_peer(peer);
11181
11182 return res;
11183 }
11184
11185
11186 static void sip_set_redirstr(struct sip_pvt *p, char *reason) {
11187
11188 if (!strcmp(reason, "unknown")) {
11189 ast_string_field_set(p, redircause, "UNKNOWN");
11190 } else if (!strcmp(reason, "user-busy")) {
11191 ast_string_field_set(p, redircause, "BUSY");
11192 } else if (!strcmp(reason, "no-answer")) {
11193 ast_string_field_set(p, redircause, "NOANSWER");
11194 } else if (!strcmp(reason, "unavailable")) {
11195 ast_string_field_set(p, redircause, "UNREACHABLE");
11196 } else if (!strcmp(reason, "unconditional")) {
11197 ast_string_field_set(p, redircause, "UNCONDITIONAL");
11198 } else if (!strcmp(reason, "time-of-day")) {
11199 ast_string_field_set(p, redircause, "UNKNOWN");
11200 } else if (!strcmp(reason, "do-not-disturb")) {
11201 ast_string_field_set(p, redircause, "UNKNOWN");
11202 } else if (!strcmp(reason, "deflection")) {
11203 ast_string_field_set(p, redircause, "UNKNOWN");
11204 } else if (!strcmp(reason, "follow-me")) {
11205 ast_string_field_set(p, redircause, "UNKNOWN");
11206 } else if (!strcmp(reason, "out-of-service")) {
11207 ast_string_field_set(p, redircause, "UNREACHABLE");
11208 } else if (!strcmp(reason, "away")) {
11209 ast_string_field_set(p, redircause, "UNREACHABLE");
11210 } else {
11211 ast_string_field_set(p, redircause, "UNKNOWN");
11212 }
11213 }
11214
11215
11216 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
11217 {
11218 char tmp[256], *exten, *rexten, *rdomain;
11219 char *params, *reason = NULL;
11220 struct sip_request *req;
11221
11222 req = oreq ? oreq : &p->initreq;
11223
11224 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
11225 if (ast_strlen_zero(tmp))
11226 return 0;
11227
11228 params = strchr(tmp, ';');
11229
11230 exten = get_in_brackets(tmp);
11231 if (!strncasecmp(exten, "sip:", 4)) {
11232 exten += 4;
11233 } else if (!strncasecmp(exten, "sips:", 5)) {
11234 exten += 5;
11235 } else {
11236 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", exten);
11237 return -1;
11238 }
11239
11240
11241 if (params) {
11242 *params = '\0';
11243 params++;
11244 while (*params == ';' || *params == ' ')
11245 params++;
11246
11247 if ((reason = strcasestr(params, "reason="))) {
11248 reason+=7;
11249
11250 if (*reason == '"')
11251 ast_strip_quoted(reason, "\"", "\"");
11252 if (!ast_strlen_zero(reason)) {
11253 sip_set_redirstr(p, reason);
11254 if (p->owner) {
11255 pbx_builtin_setvar_helper(p->owner, "__PRIREDIRECTREASON", p->redircause);
11256 pbx_builtin_setvar_helper(p->owner, "__SIPREDIRECTREASON", reason);
11257 }
11258 }
11259 }
11260 }
11261
11262 rdomain = exten;
11263 rexten = strsep(&rdomain, "@");
11264 if (p->owner)
11265 pbx_builtin_setvar_helper(p->owner, "__SIPRDNISDOMAIN", rdomain);
11266
11267 if (sip_debug_test_pvt(p))
11268 ast_verbose("RDNIS for this call is is %s (reason %s)\n", exten, reason ? reason : "");
11269
11270 ast_string_field_set(p, rdnis, rexten);
11271
11272 return 0;
11273 }
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
11284 {
11285 char tmp[256] = "", *uri, *a;
11286 char tmpf[256] = "", *from = NULL;
11287 struct sip_request *req;
11288 char *colon;
11289 char *decoded_uri;
11290
11291 req = oreq;
11292 if (!req)
11293 req = &p->initreq;
11294
11295
11296 if (req->rlPart2)
11297 ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
11298
11299 if (pedanticsipchecking)
11300 ast_uri_decode(tmp);
11301
11302 uri = get_in_brackets(tmp);
11303
11304 if (!strncasecmp(uri, "sip:", 4)) {
11305 uri += 4;
11306 } else if (!strncasecmp(uri, "sips:", 5)) {
11307 uri += 5;
11308 } else {
11309 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
11310 return -1;
11311 }
11312
11313
11314
11315
11316
11317 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
11318 if (!ast_strlen_zero(tmpf)) {
11319 if (pedanticsipchecking)
11320 ast_uri_decode(tmpf);
11321 from = get_in_brackets(tmpf);
11322 }
11323
11324 if (!ast_strlen_zero(from)) {
11325 if (!strncasecmp(from, "sip:", 4)) {
11326 from += 4;
11327 } else if (!strncasecmp(from, "sips:", 5)) {
11328 from += 5;
11329 } else {
11330 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
11331 return -1;
11332 }
11333 if ((a = strchr(from, '@')))
11334 *a++ = '\0';
11335 else
11336 a = from;
11337 from = strsep(&from, ";");
11338 a = strsep(&a, ";");
11339 ast_string_field_set(p, fromdomain, a);
11340 }
11341
11342
11343
11344
11345 if ((a = strchr(uri, '@'))) {
11346 *a++ = '\0';
11347 } else {
11348 a = uri;
11349 uri = "s";
11350 }
11351 colon = strchr(a, ':');
11352 if (colon)
11353 *colon = '\0';
11354
11355 uri = strsep(&uri, ";");
11356 a = strsep(&a, ";");
11357
11358 ast_string_field_set(p, domain, a);
11359
11360 if (!AST_LIST_EMPTY(&domain_list)) {
11361 char domain_context[AST_MAX_EXTENSION];
11362
11363 domain_context[0] = '\0';
11364 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
11365 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
11366 ast_debug(1, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
11367 return -2;
11368 }
11369 }
11370
11371 if (!ast_strlen_zero(domain_context))
11372 ast_string_field_set(p, context, domain_context);
11373 }
11374
11375
11376 if (req->method == SIP_SUBSCRIBE && !ast_strlen_zero(p->subscribecontext))
11377 ast_string_field_set(p, context, p->subscribecontext);
11378
11379 if (sip_debug_test_pvt(p))
11380 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
11381
11382
11383 if (req->method == SIP_SUBSCRIBE) {
11384 char hint[AST_MAX_EXTENSION];
11385 return (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten) ? 0 : -1);
11386 } else {
11387 decoded_uri = ast_strdupa(uri);
11388 ast_uri_decode(decoded_uri);
11389
11390
11391
11392
11393
11394 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)) ||
11395 !strcmp(decoded_uri, ast_pickup_ext())) {
11396 if (!oreq)
11397 ast_string_field_set(p, exten, decoded_uri);
11398 return 0;
11399 }
11400 }
11401
11402
11403 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
11404 ast_canmatch_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from))) ||
11405 !strncmp(decoded_uri, ast_pickup_ext(), strlen(decoded_uri))) {
11406 return 1;
11407 }
11408
11409 return -1;
11410 }
11411
11412
11413
11414
11415
11416
11417
11418 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
11419 {
11420 struct sip_pvt *sip_pvt_ptr;
11421
11422
11423 if (totag)
11424 ast_debug(4, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
11425
11426
11427 dialoglist_lock();
11428 for (sip_pvt_ptr = dialoglist; sip_pvt_ptr; sip_pvt_ptr = sip_pvt_ptr->next) {
11429 if (!strcmp(sip_pvt_ptr->callid, callid)) {
11430 int match = 1;
11431 char *ourtag = sip_pvt_ptr->tag;
11432
11433
11434 sip_pvt_lock(sip_pvt_ptr);
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455 if (pedanticsipchecking && (strcmp(fromtag, sip_pvt_ptr->theirtag) || (!ast_strlen_zero(totag) && strcmp(totag, ourtag))))
11456 match = 0;
11457
11458 if (!match) {
11459 sip_pvt_unlock(sip_pvt_ptr);
11460 continue;
11461 }
11462
11463 if (totag)
11464 ast_debug(4, "Matched %s call - their tag is %s Our tag is %s\n",
11465 ast_test_flag(&sip_pvt_ptr->flags[0], SIP_OUTGOING) ? "OUTGOING": "INCOMING",
11466 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
11467
11468
11469 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
11470 sip_pvt_unlock(sip_pvt_ptr);
11471 usleep(1);
11472 sip_pvt_lock(sip_pvt_ptr);
11473 }
11474 break;
11475 }
11476 }
11477 dialoglist_unlock();
11478 if (!sip_pvt_ptr)
11479 ast_debug(4, "Found no match for callid %s to-tag %s from-tag %s\n", callid, totag, fromtag);
11480 return sip_pvt_ptr;
11481 }
11482
11483
11484
11485 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
11486 {
11487
11488 const char *p_referred_by = NULL;
11489 char *h_refer_to = NULL;
11490 char *h_referred_by = NULL;
11491 char *refer_to;
11492 const char *p_refer_to;
11493 char *referred_by_uri = NULL;
11494 char *ptr;
11495 struct sip_request *req = NULL;
11496 const char *transfer_context = NULL;
11497 struct sip_refer *referdata;
11498
11499
11500 req = outgoing_req;
11501 referdata = transferer->refer;
11502
11503 if (!req)
11504 req = &transferer->initreq;
11505
11506 p_refer_to = get_header(req, "Refer-To");
11507 if (ast_strlen_zero(p_refer_to)) {
11508 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
11509 return -2;
11510 }
11511 h_refer_to = ast_strdupa(p_refer_to);
11512 refer_to = get_in_brackets(h_refer_to);
11513 if (pedanticsipchecking)
11514 ast_uri_decode(refer_to);
11515
11516 if (!strncasecmp(refer_to, "sip:", 4)) {
11517 refer_to += 4;
11518 } else if (!strncasecmp(refer_to, "sips:", 5)) {
11519 refer_to += 5;
11520 } else {
11521 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
11522 return -3;
11523 }
11524
11525
11526 p_referred_by = get_header(req, "Referred-By");
11527
11528
11529 if (transferer->owner) {
11530 struct ast_channel *peer = ast_bridged_channel(transferer->owner);
11531 if (peer) {
11532 pbx_builtin_setvar_helper(peer, "SIPREFERRINGCONTEXT", transferer->context);
11533 pbx_builtin_setvar_helper(peer, "SIPREFERREDBYHDR", p_referred_by);
11534 }
11535 }
11536
11537 if (!ast_strlen_zero(p_referred_by)) {
11538 char *lessthan;
11539 h_referred_by = ast_strdupa(p_referred_by);
11540 if (pedanticsipchecking)
11541 ast_uri_decode(h_referred_by);
11542
11543
11544 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
11545 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
11546 *(lessthan - 1) = '\0';
11547 }
11548
11549 referred_by_uri = get_in_brackets(h_referred_by);
11550 if (!strncasecmp(referred_by_uri, "sip:", 4)) {
11551 referred_by_uri += 4;
11552 } else if (!strncasecmp(referred_by_uri, "sips:", 5)) {
11553 referred_by_uri += 5;
11554 } else {
11555 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
11556 referred_by_uri = NULL;
11557 }
11558 }
11559
11560
11561 if ((ptr = strcasestr(refer_to, "replaces="))) {
11562 char *to = NULL, *from = NULL;
11563
11564
11565 referdata->attendedtransfer = 1;
11566 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
11567 ast_uri_decode(referdata->replaces_callid);
11568 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
11569 *ptr++ = '\0';
11570 }
11571
11572 if (ptr) {
11573
11574 to = strcasestr(ptr, "to-tag=");
11575 from = strcasestr(ptr, "from-tag=");
11576 }
11577
11578
11579 if (to) {
11580 ptr = to + 7;
11581 if ((to = strchr(ptr, '&')))
11582 *to = '\0';
11583 if ((to = strchr(ptr, ';')))
11584 *to = '\0';
11585 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
11586 }
11587
11588 if (from) {
11589 ptr = from + 9;
11590 if ((to = strchr(ptr, '&')))
11591 *to = '\0';
11592 if ((to = strchr(ptr, ';')))
11593 *to = '\0';
11594 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
11595 }
11596
11597 if (!pedanticsipchecking)
11598 ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
11599 else
11600 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>" );
11601 }
11602
11603 if ((ptr = strchr(refer_to, '@'))) {
11604 char *urioption = NULL, *domain;
11605 *ptr++ = '\0';
11606
11607 if ((urioption = strchr(ptr, ';')))
11608 *urioption++ = '\0';
11609
11610 domain = ptr;
11611 if ((ptr = strchr(domain, ':')))
11612 *ptr = '\0';
11613
11614
11615 ast_copy_string(referdata->refer_to_domain, domain, sizeof(referdata->refer_to_domain));
11616 if (urioption)
11617 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
11618 }
11619
11620 if ((ptr = strchr(refer_to, ';')))
11621 *ptr = '\0';
11622 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
11623
11624 if (referred_by_uri) {
11625 if ((ptr = strchr(referred_by_uri, ';')))
11626 *ptr = '\0';
11627 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
11628 } else {
11629 referdata->referred_by[0] = '\0';
11630 }
11631
11632
11633 if (transferer->owner)
11634 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
11635
11636
11637 if (ast_strlen_zero(transfer_context)) {
11638 transfer_context = S_OR(transferer->owner->macrocontext,
11639 S_OR(transferer->context, default_context));
11640 }
11641
11642 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
11643
11644
11645 if (referdata->attendedtransfer || ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
11646 if (sip_debug_test_pvt(transferer)) {
11647 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
11648 }
11649
11650 return 0;
11651 }
11652 if (sip_debug_test_pvt(transferer))
11653 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
11654
11655
11656 return -1;
11657 }
11658
11659
11660
11661 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
11662 {
11663 char tmp[256] = "", *c, *a;
11664 struct sip_request *req = oreq ? oreq : &p->initreq;
11665 struct sip_refer *referdata = NULL;
11666 const char *transfer_context = NULL;
11667
11668 if (!p->refer && !sip_refer_allocate(p))
11669 return -1;
11670
11671 referdata = p->refer;
11672
11673 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
11674 c = get_in_brackets(tmp);
11675
11676 if (pedanticsipchecking)
11677 ast_uri_decode(c);
11678
11679 if (!strncasecmp(c, "sip:", 4)) {
11680 c += 4;
11681 } else if (!strncasecmp(c, "sips:", 5)) {
11682 c += 5;
11683 } else {
11684 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
11685 return -1;
11686 }
11687
11688 if ((a = strchr(c, ';')))
11689 *a = '\0';
11690
11691 if ((a = strchr(c, '@'))) {
11692 *a++ = '\0';
11693 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
11694 }
11695
11696 if (sip_debug_test_pvt(p))
11697 ast_verbose("Looking for %s in %s\n", c, p->context);
11698
11699 if (p->owner)
11700 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
11701
11702
11703 if (ast_strlen_zero(transfer_context)) {
11704 transfer_context = S_OR(p->owner->macrocontext,
11705 S_OR(p->context, default_context));
11706 }
11707 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
11708
11709 ast_debug(1, "SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
11710 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
11711 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
11712 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
11713 referdata->refer_call = dialog_unref(referdata->refer_call);
11714
11715 ast_string_field_set(p, context, transfer_context);
11716 return 0;
11717 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
11718 return 1;
11719 }
11720
11721 return -1;
11722 }
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734 static attribute_unused void check_via_response(struct sip_pvt *p, struct sip_request *req)
11735 {
11736 char via[256];
11737 char *cur, *opts;
11738
11739 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
11740
11741
11742 opts = strchr(via, ',');
11743 if (opts)
11744 *opts = '\0';
11745
11746
11747 opts = strchr(via, ';');
11748 if (!opts)
11749 return;
11750 *opts++ = '\0';
11751 while ( (cur = strsep(&opts, ";")) ) {
11752 if (!strncmp(cur, "rport=", 6)) {
11753 int port = strtol(cur+6, NULL, 10);
11754
11755 p->ourip.sin_port = ntohs(port);
11756 } else if (!strncmp(cur, "received=", 9)) {
11757 if (ast_parse_arg(cur+9, PARSE_INADDR, &p->ourip))
11758 ;
11759 }
11760 }
11761 }
11762
11763
11764 static void check_via(struct sip_pvt *p, struct sip_request *req)
11765 {
11766 char via[512];
11767 char *c, *pt;
11768 struct hostent *hp;
11769 struct ast_hostent ahp;
11770
11771 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
11772
11773
11774 c = strchr(via, ',');
11775 if (c)
11776 *c = '\0';
11777
11778
11779 c = strstr(via, ";rport");
11780 if (c && (c[6] != '='))
11781 ast_set_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT);
11782
11783 c = strchr(via, ';');
11784 if (c)
11785 *c = '\0';
11786
11787 c = strchr(via, ' ');
11788 if (c) {
11789 *c = '\0';
11790 c = ast_skip_blanks(c+1);
11791 if (strcasecmp(via, "SIP/2.0/UDP") && strcasecmp(via, "SIP/2.0/TCP") && strcasecmp(via, "SIP/2.0/TLS")) {
11792 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
11793 return;
11794 }
11795 pt = strchr(c, ':');
11796 if (pt)
11797 *pt++ = '\0';
11798 hp = ast_gethostbyname(c, &ahp);
11799 if (!hp) {
11800 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
11801 return;
11802 }
11803 memset(&p->sa, 0, sizeof(p->sa));
11804 p->sa.sin_family = AF_INET;
11805 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
11806 p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
11807
11808 if (sip_debug_test_pvt(p)) {
11809 const struct sockaddr_in *dst = sip_real_dst(p);
11810 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
11811 }
11812 }
11813 }
11814
11815
11816 static char *get_calleridname(const char *input, char *output, size_t outputsize)
11817 {
11818 const char *end = strchr(input, '<');
11819 const char *tmp = strchr(input, '"');
11820 int bytes = 0;
11821 int maxbytes = outputsize - 1;
11822
11823 if (!end || end == input)
11824 return NULL;
11825
11826 end--;
11827
11828 if (tmp && tmp <= end) {
11829
11830
11831
11832 end = strchr(tmp+1, '"');
11833 if (!end)
11834 return NULL;
11835 bytes = (int) (end - tmp);
11836
11837 if (bytes > maxbytes)
11838 bytes = maxbytes;
11839 ast_copy_string(output, tmp + 1, bytes);
11840 } else {
11841
11842
11843 input = ast_skip_blanks(input);
11844
11845 while(*end && *end < 33 && end > input)
11846 end--;
11847 if (end >= input) {
11848 bytes = (int) (end - input) + 2;
11849
11850 if (bytes > maxbytes)
11851 bytes = maxbytes;
11852 ast_copy_string(output, input, bytes);
11853 } else
11854 return NULL;
11855 }
11856 return output;
11857 }
11858
11859
11860
11861
11862
11863 static int get_rpid_num(const char *input, char *output, int maxlen)
11864 {
11865 char *start;
11866 char *end;
11867
11868 start = strchr(input, ':');
11869 if (!start) {
11870 output[0] = '\0';
11871 return 0;
11872 }
11873 start++;
11874
11875
11876 ast_copy_string(output, start, maxlen);
11877 output[maxlen-1] = '\0';
11878
11879 end = strchr(output, '@');
11880 if (end)
11881 *end = '\0';
11882 else
11883 output[0] = '\0';
11884 if (strstr(input, "privacy=full") || strstr(input, "privacy=uri"))
11885 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
11886
11887 return 0;
11888 }
11889
11890
11891
11892
11893 static struct ast_variable *copy_vars(struct ast_variable *src)
11894 {
11895 struct ast_variable *res = NULL, *tmp, *v = NULL;
11896
11897 for (v = src ; v ; v = v->next) {
11898 if ((tmp = ast_variable_new(v->name, v->value, v->file))) {
11899 tmp->next = res;
11900 res = tmp;
11901 }
11902 }
11903 return res;
11904 }
11905
11906
11907 static void replace_cid(struct sip_pvt *p, const char *rpid_num, const char *calleridname)
11908 {
11909
11910 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
11911 char *tmp = ast_strdupa(rpid_num);
11912 if (!ast_strlen_zero(calleridname))
11913 ast_string_field_set(p, cid_name, calleridname);
11914 if (ast_is_shrinkable_phonenumber(tmp))
11915 ast_shrink_phone_number(tmp);
11916 ast_string_field_set(p, cid_num, tmp);
11917 }
11918 }
11919
11920
11921 static enum check_auth_result check_user_ok(struct sip_pvt *p, char *of,
11922 struct sip_request *req, int sipmethod, struct sockaddr_in *sin,
11923 enum xmittype reliable,
11924 char *rpid_num, char *calleridname, char *uri2)
11925 {
11926 enum check_auth_result res;
11927 struct sip_user *user = find_user(of, 1);
11928 int debug=sip_debug_test_addr(sin);
11929
11930
11931 if (!user) {
11932 if (debug)
11933 ast_verbose("No user '%s' in SIP users list\n", of);
11934 return AUTH_DONT_KNOW;
11935 }
11936 if (!ast_apply_ha(user->ha, sin)) {
11937 if (debug)
11938 ast_verbose("Found user '%s' for '%s', but fails host access\n",
11939 user->name, of);
11940 unref_user(user);
11941 return AUTH_DONT_KNOW;
11942 }
11943 if (debug)
11944 ast_verbose("Found user '%s' for '%s'\n", user->name, of);
11945
11946 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
11947 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
11948 if (sipmethod == SIP_INVITE) {
11949
11950 p->chanvars = copy_vars(user->chanvars);
11951 }
11952 p->prefs = user->prefs;
11953
11954 if (p->rtp) {
11955 ast_rtp_codec_setpref(p->rtp, &p->prefs);
11956 p->autoframing = user->autoframing;
11957 }
11958
11959 replace_cid(p, rpid_num, calleridname);
11960 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
11961
11962 if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, req->ignore))) {
11963 sip_cancel_destroy(p);
11964 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
11965 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
11966
11967 if (p->sipoptions)
11968 user->sipoptions = p->sipoptions;
11969
11970
11971 if (user->call_limit)
11972 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
11973 if (!ast_strlen_zero(user->context))
11974 ast_string_field_set(p, context, user->context);
11975 if (!ast_strlen_zero(user->cid_num)) {
11976 char *tmp = ast_strdupa(user->cid_num);
11977 if (ast_is_shrinkable_phonenumber(tmp))
11978 ast_shrink_phone_number(tmp);
11979 ast_string_field_set(p, cid_num, tmp);
11980 }
11981 if (!ast_strlen_zero(user->cid_name))
11982 ast_string_field_set(p, cid_name, user->cid_name);
11983 ast_string_field_set(p, username, user->name);
11984 ast_string_field_set(p, peername, user->name);
11985 ast_string_field_set(p, peersecret, user->secret);
11986 ast_string_field_set(p, peermd5secret, user->md5secret);
11987 ast_string_field_set(p, subscribecontext, user->subscribecontext);
11988 ast_string_field_set(p, accountcode, user->accountcode);
11989 ast_string_field_set(p, language, user->language);
11990 ast_string_field_set(p, mohsuggest, user->mohsuggest);
11991 ast_string_field_set(p, mohinterpret, user->mohinterpret);
11992 p->allowtransfer = user->allowtransfer;
11993 p->amaflags = user->amaflags;
11994 p->callgroup = user->callgroup;
11995 p->pickupgroup = user->pickupgroup;
11996 if (user->callingpres)
11997 p->callingpres = user->callingpres;
11998
11999
12000 p->capability = user->capability;
12001 p->jointcapability = user->capability;
12002 if (p->peercapability)
12003 p->jointcapability &= p->peercapability;
12004 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
12005 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
12006 p->noncodeccapability |= AST_RTP_DTMF;
12007 else
12008 p->noncodeccapability &= ~AST_RTP_DTMF;
12009 p->jointnoncodeccapability = p->noncodeccapability;
12010 if (p->t38.peercapability)
12011 p->t38.jointcapability &= p->t38.peercapability;
12012 p->maxcallbitrate = user->maxcallbitrate;
12013
12014 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
12015 ast_rtp_destroy(p->vrtp);
12016 p->vrtp = NULL;
12017 }
12018
12019 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT) && p->trtp) {
12020 ast_rtp_destroy(p->trtp);
12021 p->trtp = NULL;
12022 }
12023 }
12024 unref_user(user);
12025 return res;
12026 }
12027
12028
12029 static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
12030 struct sip_request *req, int sipmethod, struct sockaddr_in *sin,
12031 struct sip_peer **authpeer,
12032 enum xmittype reliable,
12033 char *rpid_num, char *calleridname, char *uri2)
12034 {
12035 enum check_auth_result res;
12036 int debug=sip_debug_test_addr(sin);
12037 struct sip_peer *peer;
12038
12039
12040
12041
12042 peer = (sipmethod == SIP_SUBSCRIBE) ? find_peer(of, NULL, 1, 0) : find_peer(NULL, &p->recv, 1, 0);
12043
12044
12045
12046
12047
12048
12049 if (!peer && (p->socket.type != SIP_TRANSPORT_UDP)) {
12050 struct sockaddr_in tmpsin;
12051 char contact[SIPBUFSIZE];
12052 char *tmp;
12053 memcpy(&tmpsin, &p->recv, sizeof(tmpsin));
12054 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
12055 tmp = get_in_brackets(contact);
12056 __set_address_from_contact(tmp, &tmpsin, 1);
12057 peer = find_peer(NULL, &tmpsin, 1, 0);
12058 }
12059 if (!peer) {
12060 if (debug)
12061 ast_verbose("No matching peer for '%s' from '%s:%d'\n",
12062 of, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12063 return AUTH_DONT_KNOW;
12064 }
12065
12066 if (debug)
12067 ast_verbose("Found peer '%s' for '%s' from %s:%d\n",
12068 peer->name, of, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12069
12070
12071
12072 if (p->rtp) {
12073 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
12074 p->autoframing = peer->autoframing;
12075 }
12076
12077
12078 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
12079 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
12080
12081
12082
12083 if (p->sipoptions)
12084 peer->sipoptions = p->sipoptions;
12085
12086 replace_cid(p, rpid_num, calleridname);
12087 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
12088
12089 ast_string_field_set(p, peersecret, peer->secret);
12090 ast_string_field_set(p, peermd5secret, peer->md5secret);
12091 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
12092 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
12093 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
12094 if (peer->callingpres)
12095 p->callingpres = peer->callingpres;
12096 if (peer->maxms && peer->lastms)
12097 p->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
12098 else
12099 p->timer_t1 = peer->timer_t1;
12100
12101
12102 if (peer->timer_b)
12103 p->timer_b = peer->timer_b;
12104 else
12105 p->timer_b = 64 * p->timer_t1;
12106
12107 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
12108
12109 ast_string_field_set(p, peersecret, NULL);
12110 ast_string_field_set(p, peermd5secret, NULL);
12111 }
12112 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, req->ignore))) {
12113 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
12114 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
12115
12116 if (peer->call_limit)
12117 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
12118 ast_string_field_set(p, peername, peer->name);
12119 ast_string_field_set(p, authname, peer->name);
12120
12121 if (sipmethod == SIP_INVITE) {
12122
12123 p->chanvars = copy_vars(peer->chanvars);
12124 }
12125
12126 if (authpeer) {
12127 (*authpeer) = ASTOBJ_REF(peer);
12128 }
12129
12130 if (!ast_strlen_zero(peer->username)) {
12131 ast_string_field_set(p, username, peer->username);
12132
12133
12134 ast_string_field_set(p, authname, peer->username);
12135 }
12136 if (!ast_strlen_zero(peer->cid_num)) {
12137 char *tmp = ast_strdupa(peer->cid_num);
12138 if (ast_is_shrinkable_phonenumber(tmp))
12139 ast_shrink_phone_number(tmp);
12140 ast_string_field_set(p, cid_num, tmp);
12141 }
12142 if (!ast_strlen_zero(peer->cid_name))
12143 ast_string_field_set(p, cid_name, peer->cid_name);
12144 ast_string_field_set(p, fullcontact, peer->fullcontact);
12145 if (!ast_strlen_zero(peer->context))
12146 ast_string_field_set(p, context, peer->context);
12147 ast_string_field_set(p, peersecret, peer->secret);
12148 ast_string_field_set(p, peermd5secret, peer->md5secret);
12149 ast_string_field_set(p, language, peer->language);
12150 ast_string_field_set(p, accountcode, peer->accountcode);
12151 p->amaflags = peer->amaflags;
12152 p->callgroup = peer->callgroup;
12153 p->pickupgroup = peer->pickupgroup;
12154 p->capability = peer->capability;
12155 p->prefs = peer->prefs;
12156 p->jointcapability = peer->capability;
12157 if (p->peercapability)
12158 p->jointcapability &= p->peercapability;
12159 p->maxcallbitrate = peer->maxcallbitrate;
12160 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
12161 ast_rtp_destroy(p->vrtp);
12162 p->vrtp = NULL;
12163 }
12164 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_TEXTSUPPORT) || !(p->capability & AST_FORMAT_TEXT_MASK)) && p->trtp) {
12165 ast_rtp_destroy(p->trtp);
12166 p->trtp = NULL;
12167 }
12168 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
12169 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
12170 p->noncodeccapability |= AST_RTP_DTMF;
12171 else
12172 p->noncodeccapability &= ~AST_RTP_DTMF;
12173 p->jointnoncodeccapability = p->noncodeccapability;
12174 if (p->t38.peercapability)
12175 p->t38.jointcapability &= p->t38.peercapability;
12176 }
12177 unref_peer(peer);
12178 return res;
12179 }
12180
12181
12182
12183
12184
12185
12186
12187 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
12188 int sipmethod, char *uri, enum xmittype reliable,
12189 struct sockaddr_in *sin, struct sip_peer **authpeer)
12190 {
12191 char from[256];
12192 char *dummy;
12193 char *domain;
12194 char *of, *of2;
12195 char rpid_num[50];
12196 const char *rpid;
12197 enum check_auth_result res;
12198 char calleridname[50];
12199 char *uri2 = ast_strdupa(uri);
12200
12201 terminate_uri(uri2);
12202
12203 ast_copy_string(from, get_header(req, "From"), sizeof(from));
12204 if (pedanticsipchecking)
12205 ast_uri_decode(from);
12206
12207 memset(calleridname, 0, sizeof(calleridname));
12208 get_calleridname(from, calleridname, sizeof(calleridname));
12209 if (calleridname[0])
12210 ast_string_field_set(p, cid_name, calleridname);
12211
12212 rpid = get_header(req, "Remote-Party-ID");
12213 memset(rpid_num, 0, sizeof(rpid_num));
12214 if (!ast_strlen_zero(rpid))
12215 p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
12216
12217 of = get_in_brackets(from);
12218 if (ast_strlen_zero(p->exten)) {
12219 char *t = uri2;
12220 if (!strncasecmp(t, "sip:", 4))
12221 t+= 4;
12222 else if (!strncasecmp(t, "sips:", 5))
12223 t += 5;
12224 ast_string_field_set(p, exten, t);
12225 t = strchr(p->exten, '@');
12226 if (t)
12227 *t = '\0';
12228 if (ast_strlen_zero(p->our_contact))
12229 build_contact(p);
12230 }
12231
12232 ast_string_field_set(p, from, of);
12233
12234 of2 = ast_strdupa(of);
12235
12236
12237 if (p->socket.type == SIP_TRANSPORT_TLS) {
12238 if (parse_uri(of, "sips:", &of, &dummy, &domain, &dummy, &dummy, NULL)) {
12239 if (parse_uri(of2, "sip:", &of, &dummy, &domain, &dummy, &dummy, NULL))
12240 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
12241 }
12242 } else {
12243 if (parse_uri(of, "sip:", &of, &dummy, &domain, &dummy, &dummy, NULL))
12244 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
12245 }
12246
12247 if (ast_strlen_zero(of)) {
12248
12249
12250
12251
12252
12253
12254 of = domain;
12255 }
12256 {
12257 char *tmp = ast_strdupa(of);
12258
12259
12260
12261 tmp = strsep(&tmp, ";");
12262 if (ast_is_shrinkable_phonenumber(tmp))
12263 ast_shrink_phone_number(tmp);
12264 ast_string_field_set(p, cid_num, tmp);
12265 }
12266
12267 if (global_match_auth_username) {
12268
12269
12270
12271
12272
12273
12274
12275
12276 const char *hdr = get_header(req, "Authorization");
12277 if (ast_strlen_zero(hdr))
12278 hdr = get_header(req, "Proxy-Authorization");
12279
12280 if ( !ast_strlen_zero(hdr) && (hdr = strstr(hdr, "username=\"")) ) {
12281 ast_copy_string(from, hdr + strlen("username=\""), sizeof(from));
12282 of = from;
12283 of = strsep(&of, "\"");
12284 }
12285 }
12286
12287 if (!authpeer) {
12288
12289
12290 res = check_user_ok(p, of, req, sipmethod, sin,
12291 reliable, rpid_num, calleridname, uri2);
12292 if (res != AUTH_DONT_KNOW)
12293 return res;
12294 }
12295
12296 res = check_peer_ok(p, of, req, sipmethod, sin,
12297 authpeer, reliable, rpid_num, calleridname, uri2);
12298 if (res != AUTH_DONT_KNOW)
12299 return res;
12300
12301
12302 if (global_allowguest) {
12303 replace_cid(p, rpid_num, calleridname);
12304 res = AUTH_SUCCESSFUL;
12305 } else if (global_alwaysauthreject)
12306 res = AUTH_FAKE_AUTH;
12307 else
12308 res = AUTH_SECRET_FAILED;
12309
12310
12311 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT)) {
12312 ast_set_flag(&p->flags[0], SIP_NAT_ROUTE);
12313 }
12314
12315 return res;
12316 }
12317
12318
12319
12320
12321 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin)
12322 {
12323 return check_user_full(p, req, sipmethod, uri, reliable, sin, NULL);
12324 }
12325
12326
12327 static int get_msg_text(char *buf, int len, struct sip_request *req)
12328 {
12329 int x;
12330 int y;
12331
12332 buf[0] = '\0';
12333 y = len - strlen(buf) - 5;
12334 if (y < 0)
12335 y = 0;
12336 for (x=0;x<req->lines;x++) {
12337 strncat(buf, req->line[x], y);
12338 y -= strlen(req->line[x]) + 1;
12339 if (y < 0)
12340 y = 0;
12341 if (y != 0)
12342 strcat(buf, "\n");
12343 }
12344 return 0;
12345 }
12346
12347
12348
12349
12350
12351 static void receive_message(struct sip_pvt *p, struct sip_request *req)
12352 {
12353 char buf[1024];
12354 struct ast_frame f;
12355 const char *content_type = get_header(req, "Content-Type");
12356
12357 if (strncmp(content_type, "text/plain", strlen("text/plain"))) {
12358 transmit_response(p, "415 Unsupported Media Type", req);
12359 if (!p->owner)
12360 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12361 return;
12362 }
12363
12364 if (get_msg_text(buf, sizeof(buf), req)) {
12365 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
12366 transmit_response(p, "202 Accepted", req);
12367 if (!p->owner)
12368 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12369 return;
12370 }
12371
12372 if (p->owner) {
12373 if (sip_debug_test_pvt(p))
12374 ast_verbose("Message received: '%s'\n", buf);
12375 memset(&f, 0, sizeof(f));
12376 f.frametype = AST_FRAME_TEXT;
12377 f.subclass = 0;
12378 f.offset = 0;
12379 f.data = buf;
12380 f.datalen = strlen(buf);
12381 ast_queue_frame(p->owner, &f);
12382 transmit_response(p, "202 Accepted", req);
12383 } else {
12384 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);
12385 transmit_response(p, "405 Method Not Allowed", req);
12386 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12387 }
12388 return;
12389 }
12390
12391
12392 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
12393 {
12394 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
12395 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
12396 char ilimits[40];
12397 char iused[40];
12398 int showall = FALSE;
12399
12400 switch (cmd) {
12401 case CLI_INIT:
12402 e->command = "sip show inuse";
12403 e->usage =
12404 "Usage: sip show inuse [all]\n"
12405 " List all SIP users and peers usage counters and limits.\n"
12406 " Add option \"all\" to show all devices, not only those with a limit.\n";
12407 return NULL;
12408 case CLI_GENERATE:
12409 return NULL;
12410 }
12411
12412 if (a->argc < 3)
12413 return CLI_SHOWUSAGE;
12414
12415 if (a->argc == 4 && !strcmp(a->argv[3], "all"))
12416 showall = TRUE;
12417
12418 ast_cli(a->fd, FORMAT, "* User name", "In use", "Limit");
12419 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
12420 ASTOBJ_RDLOCK(iterator);
12421 if (iterator->call_limit)
12422 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
12423 else
12424 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
12425 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
12426 if (showall || iterator->call_limit)
12427 ast_cli(a->fd, FORMAT2, iterator->name, iused, ilimits);
12428 ASTOBJ_UNLOCK(iterator);
12429 } while (0) );
12430
12431 ast_cli(a->fd, FORMAT, "* Peer name", "In use", "Limit");
12432
12433 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
12434 ASTOBJ_RDLOCK(iterator);
12435 if (iterator->call_limit)
12436 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
12437 else
12438 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
12439 snprintf(iused, sizeof(iused), "%d/%d/%d", iterator->inUse, iterator->inRinging, iterator->onHold);
12440 if (showall || iterator->call_limit)
12441 ast_cli(a->fd, FORMAT2, iterator->name, iused, ilimits);
12442 ASTOBJ_UNLOCK(iterator);
12443 } while (0) );
12444
12445 return CLI_SUCCESS;
12446 #undef FORMAT
12447 #undef FORMAT2
12448 }
12449
12450
12451
12452 static char *transfermode2str(enum transfermodes mode)
12453 {
12454 if (mode == TRANSFER_OPENFORALL)
12455 return "open";
12456 else if (mode == TRANSFER_CLOSED)
12457 return "closed";
12458 return "strict";
12459 }
12460
12461 static struct _map_x_s natmodes[] = {
12462 { SIP_NAT_NEVER, "No"},
12463 { SIP_NAT_ROUTE, "Route"},
12464 { SIP_NAT_ALWAYS, "Always"},
12465 { SIP_NAT_RFC3581, "RFC3581"},
12466 { -1, NULL},
12467 };
12468
12469
12470 static const char *nat2str(int nat)
12471 {
12472 return map_x_s(natmodes, nat, "Unknown");
12473 }
12474
12475
12476
12477
12478
12479
12480
12481 static struct _map_x_s stmodes[] = {
12482 { SESSION_TIMER_MODE_ACCEPT, "Accept"},
12483 { SESSION_TIMER_MODE_ORIGINATE, "Originate"},
12484 { SESSION_TIMER_MODE_REFUSE, "Refuse"},
12485 { -1, NULL},
12486 };
12487
12488 static const char *stmode2str(enum st_mode m)
12489 {
12490 return map_x_s(stmodes, m, "Unknown");
12491 }
12492
12493 static enum st_mode str2stmode(const char *s)
12494 {
12495 return map_s_x(stmodes, s, -1);
12496 }
12497
12498
12499 static struct _map_x_s strefreshers[] = {
12500 { SESSION_TIMER_REFRESHER_AUTO, "auto"},
12501 { SESSION_TIMER_REFRESHER_UAC, "uac"},
12502 { SESSION_TIMER_REFRESHER_UAS, "uas"},
12503 { -1, NULL},
12504 };
12505
12506 static const char *strefresher2str(enum st_refresher r)
12507 {
12508 return map_x_s(strefreshers, r, "Unknown");
12509 }
12510
12511 static enum st_refresher str2strefresher(const char *s)
12512 {
12513 return map_s_x(strefreshers, s, -1);
12514 }
12515
12516
12517 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
12518 {
12519 int res = 0;
12520 if (peer->maxms) {
12521 if (peer->lastms < 0) {
12522 ast_copy_string(status, "UNREACHABLE", statuslen);
12523 } else if (peer->lastms > peer->maxms) {
12524 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
12525 res = 1;
12526 } else if (peer->lastms) {
12527 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
12528 res = 1;
12529 } else {
12530 ast_copy_string(status, "UNKNOWN", statuslen);
12531 }
12532 } else {
12533 ast_copy_string(status, "Unmonitored", statuslen);
12534
12535 res = -1;
12536 }
12537 return res;
12538 }
12539
12540
12541
12542
12543
12544
12545 static const char *cli_yesno(int x)
12546 {
12547 return x ? "Yes" : "No";
12548 }
12549
12550
12551 static char *sip_show_tcp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
12552 {
12553 struct sip_threadinfo *th;
12554
12555 #define FORMAT2 "%-30.30s %3.6s %9.9s %6.6s\n"
12556 #define FORMAT "%-30.30s %-6d %-9.9s %-6.6s\n"
12557
12558 switch (cmd) {
12559 case CLI_INIT:
12560 e->command = "sip show tcp";
12561 e->usage =
12562 "Usage: sip show tcp\n"
12563 " Lists all active TCP/TLS sessions.\n";
12564 return NULL;
12565 case CLI_GENERATE:
12566 return NULL;
12567 }
12568
12569 if (a->argc != 3)
12570 return CLI_SHOWUSAGE;
12571
12572 ast_cli(a->fd, FORMAT2, "Host", "Port", "Transport", "Type");
12573 AST_LIST_LOCK(&threadl);
12574 AST_LIST_TRAVERSE(&threadl, th, list) {
12575 ast_cli(a->fd, FORMAT, ast_inet_ntoa(th->tcptls_session->requestor.sin_addr),
12576 ntohs(th->tcptls_session->requestor.sin_port),
12577 get_transport(th->type),
12578 (th->tcptls_session->client ? "Client" : "Server"));
12579
12580 }
12581 AST_LIST_UNLOCK(&threadl);
12582 return CLI_SUCCESS;
12583 #undef FORMAT
12584 #undef FORMAT2
12585 }
12586
12587
12588 static char *sip_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
12589 {
12590 regex_t regexbuf;
12591 int havepattern = FALSE;
12592
12593 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
12594
12595 switch (cmd) {
12596 case CLI_INIT:
12597 e->command = "sip show users";
12598 e->usage =
12599 "Usage: sip show users [like <pattern>]\n"
12600 " Lists all known SIP users.\n"
12601 " Optional regular expression pattern is used to filter the user list.\n";
12602 return NULL;
12603 case CLI_GENERATE:
12604 return NULL;
12605 }
12606
12607 switch (a->argc) {
12608 case 5:
12609 if (!strcasecmp(a->argv[3], "like")) {
12610 if (regcomp(®exbuf, a->argv[4], REG_EXTENDED | REG_NOSUB))
12611 return CLI_SHOWUSAGE;
12612 havepattern = TRUE;
12613 } else
12614 return CLI_SHOWUSAGE;
12615 case 3:
12616 break;
12617 default:
12618 return CLI_SHOWUSAGE;
12619 }
12620
12621 ast_cli(a->fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
12622 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
12623 ASTOBJ_RDLOCK(iterator);
12624
12625 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
12626 ASTOBJ_UNLOCK(iterator);
12627 continue;
12628 }
12629
12630 ast_cli(a->fd, FORMAT, iterator->name,
12631 iterator->secret,
12632 iterator->accountcode,
12633 iterator->context,
12634 cli_yesno(iterator->ha != NULL),
12635 nat2str(ast_test_flag(&iterator->flags[0], SIP_NAT)));
12636 ASTOBJ_UNLOCK(iterator);
12637 } while (0)
12638 );
12639
12640 if (havepattern)
12641 regfree(®exbuf);
12642
12643 return CLI_SUCCESS;
12644 #undef FORMAT
12645 }
12646
12647
12648 static char mandescr_show_registry[] =
12649 "Description: Lists all registration requests and status\n"
12650 "Registrations will follow as separate events. followed by a final event called\n"
12651 "RegistrationsComplete.\n"
12652 "Variables: \n"
12653 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
12654
12655
12656 static int manager_show_registry(struct mansession *s, const struct message *m)
12657 {
12658 const char *id = astman_get_header(m, "ActionID");
12659 char idtext[256] = "";
12660 int total = 0;
12661
12662 if (!ast_strlen_zero(id))
12663 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
12664
12665 astman_send_listack(s, m, "Registrations will follow", "start");
12666
12667 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
12668 ASTOBJ_RDLOCK(iterator);
12669 astman_append(s,
12670 "Event: RegistryEntry\r\n"
12671 "Host: %s\r\n"
12672 "Port: %d\r\n"
12673 "Username: %s\r\n"
12674 "Refresh: %d\r\n"
12675 "State: %s\r\n"
12676 "RegistrationTime: %ld\r\n"
12677 "\r\n", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT,
12678 iterator->username, iterator->refresh, regstate2str(iterator->regstate), (long) iterator->regtime.tv_sec);
12679 ASTOBJ_UNLOCK(iterator);
12680 total++;
12681 } while(0));
12682
12683 astman_append(s,
12684 "Event: RegistrationsComplete\r\n"
12685 "EventList: Complete\r\n"
12686 "ListItems: %d\r\n"
12687 "%s"
12688 "\r\n", total, idtext);
12689
12690 return 0;
12691 }
12692
12693 static char mandescr_show_peers[] =
12694 "Description: Lists SIP peers in text format with details on current status.\n"
12695 "Peerlist will follow as separate events, followed by a final event called\n"
12696 "PeerlistComplete.\n"
12697 "Variables: \n"
12698 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
12699
12700
12701
12702 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
12703 {
12704 const char *id = astman_get_header(m, "ActionID");
12705 const char *a[] = {"sip", "show", "peers"};
12706 char idtext[256] = "";
12707 int total = 0;
12708
12709 if (!ast_strlen_zero(id))
12710 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
12711
12712 astman_send_listack(s, m, "Peer status list will follow", "start");
12713
12714 _sip_show_peers(-1, &total, s, m, 3, a);
12715
12716 astman_append(s,
12717 "Event: PeerlistComplete\r\n"
12718 "EventList: Complete\r\n"
12719 "ListItems: %d\r\n"
12720 "%s"
12721 "\r\n", total, idtext);
12722 return 0;
12723 }
12724
12725
12726 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
12727 {
12728 switch (cmd) {
12729 case CLI_INIT:
12730 e->command = "sip show peers";
12731 e->usage =
12732 "Usage: sip show peers [like <pattern>]\n"
12733 " Lists all known SIP peers.\n"
12734 " Optional regular expression pattern is used to filter the peer list.\n";
12735 return NULL;
12736 case CLI_GENERATE:
12737 return NULL;
12738 }
12739
12740 return _sip_show_peers(a->fd, NULL, NULL, NULL, a->argc, (const char **) a->argv);
12741 }
12742
12743
12744 static char *_sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
12745 {
12746 regex_t regexbuf;
12747 int havepattern = FALSE;
12748
12749
12750 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s %s\n"
12751 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s %s\n"
12752
12753 char name[256];
12754 int total_peers = 0;
12755 int peers_mon_online = 0;
12756 int peers_mon_offline = 0;
12757 int peers_unmon_offline = 0;
12758 int peers_unmon_online = 0;
12759 const char *id;
12760 char idtext[256] = "";
12761 int realtimepeers;
12762
12763 realtimepeers = ast_check_realtime("sippeers");
12764
12765 if (s) {
12766 id = astman_get_header(m, "ActionID");
12767 if (!ast_strlen_zero(id))
12768 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
12769 }
12770
12771 switch (argc) {
12772 case 5:
12773 if (!strcasecmp(argv[3], "like")) {
12774 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
12775 return CLI_SHOWUSAGE;
12776 havepattern = TRUE;
12777 } else
12778 return CLI_SHOWUSAGE;
12779 case 3:
12780 break;
12781 default:
12782 return CLI_SHOWUSAGE;
12783 }
12784
12785 if (!s)
12786 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
12787
12788 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
12789 char status[20] = "";
12790 char srch[2000];
12791 char pstatus;
12792
12793 ASTOBJ_RDLOCK(iterator);
12794
12795 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
12796 ASTOBJ_UNLOCK(iterator);
12797 continue;
12798 }
12799
12800 if (!ast_strlen_zero(iterator->username) && !s)
12801 snprintf(name, sizeof(name), "%s/%s", iterator->name, iterator->username);
12802 else
12803 ast_copy_string(name, iterator->name, sizeof(name));
12804
12805 pstatus = peer_status(iterator, status, sizeof(status));
12806 if (pstatus == 1)
12807 peers_mon_online++;
12808 else if (pstatus == 0)
12809 peers_mon_offline++;
12810 else {
12811 if (iterator->addr.sin_port == 0)
12812 peers_unmon_offline++;
12813 else
12814 peers_unmon_online++;
12815 }
12816
12817 snprintf(srch, sizeof(srch), FORMAT, name,
12818 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
12819 iterator->host_dynamic ? " D " : " ",
12820 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
12821 iterator->ha ? " A " : " ",
12822 ntohs(iterator->addr.sin_port), status,
12823 realtimepeers ? (iterator->is_realtime ? "Cached RT":"") : "");
12824
12825 if (!s) {
12826 ast_cli(fd, FORMAT, name,
12827 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
12828 iterator->host_dynamic ? " D " : " ",
12829 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
12830 iterator->ha ? " A " : " ",
12831
12832 ntohs(iterator->addr.sin_port), status,
12833 realtimepeers ? (iterator->is_realtime ? "Cached RT":"") : "");
12834 } else {
12835
12836 astman_append(s,
12837 "Event: PeerEntry\r\n%s"
12838 "Channeltype: SIP\r\n"
12839 "ObjectName: %s\r\n"
12840 "ChanObjectType: peer\r\n"
12841 "IPaddress: %s\r\n"
12842 "IPport: %d\r\n"
12843 "Dynamic: %s\r\n"
12844 "Natsupport: %s\r\n"
12845 "VideoSupport: %s\r\n"
12846 "TextSupport: %s\r\n"
12847 "ACL: %s\r\n"
12848 "Status: %s\r\n"
12849 "RealtimeDevice: %s\r\n\r\n",
12850 idtext,
12851 iterator->name,
12852 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "-none-",
12853 ntohs(iterator->addr.sin_port),
12854 iterator->host_dynamic ? "yes" : "no",
12855 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? "yes" : "no",
12856 ast_test_flag(&iterator->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
12857 ast_test_flag(&iterator->flags[1], SIP_PAGE2_TEXTSUPPORT) ? "yes" : "no",
12858 iterator->ha ? "yes" : "no",
12859 status,
12860 realtimepeers ? (iterator->is_realtime ? "yes":"no") : "no");
12861 }
12862
12863 ASTOBJ_UNLOCK(iterator);
12864
12865 total_peers++;
12866 } while(0) );
12867
12868 if (!s)
12869 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
12870 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
12871
12872 if (havepattern)
12873 regfree(®exbuf);
12874
12875 if (total)
12876 *total = total_peers;
12877
12878
12879 return CLI_SUCCESS;
12880 #undef FORMAT
12881 #undef FORMAT2
12882 }
12883
12884
12885 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
12886 {
12887 char tmp[256];
12888
12889 switch (cmd) {
12890 case CLI_INIT:
12891 e->command = "sip show objects";
12892 e->usage =
12893 "Usage: sip show objects\n"
12894 " Lists status of known SIP objects\n";
12895 return NULL;
12896 case CLI_GENERATE:
12897 return NULL;
12898 }
12899
12900 if (a->argc != 3)
12901 return CLI_SHOWUSAGE;
12902 ast_cli(a->fd, "-= User objects: %d static, %d realtime =-\n\n", suserobjs, ruserobjs);
12903 ASTOBJ_CONTAINER_DUMP(a->fd, tmp, sizeof(tmp), &userl);
12904 ast_cli(a->fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
12905 ASTOBJ_CONTAINER_DUMP(a->fd, tmp, sizeof(tmp), &peerl);
12906 ast_cli(a->fd, "-= Registry objects: %d =-\n\n", regobjs);
12907 ASTOBJ_CONTAINER_DUMP(a->fd, tmp, sizeof(tmp), ®l);
12908 return CLI_SUCCESS;
12909 }
12910
12911 static void print_group(int fd, ast_group_t group, int crlf)
12912 {
12913 char buf[256];
12914 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
12915 }
12916
12917
12918 static struct _map_x_s dtmfstr[] = {
12919 { SIP_DTMF_RFC2833, "rfc2833" },
12920 { SIP_DTMF_INFO, "info" },
12921 { SIP_DTMF_SHORTINFO, "shortinfo" },
12922 { SIP_DTMF_INBAND, "inband" },
12923 { SIP_DTMF_AUTO, "auto" },
12924 { -1, NULL },
12925 };
12926
12927
12928 static const char *dtmfmode2str(int mode)
12929 {
12930 return map_x_s(dtmfstr, mode, "<error>");
12931 }
12932
12933
12934 static int str2dtmfmode(const char *str)
12935 {
12936 return map_s_x(dtmfstr, str, -1);
12937 }
12938
12939 static struct _map_x_s insecurestr[] = {
12940 { SIP_INSECURE_PORT, "port" },
12941 { SIP_INSECURE_INVITE, "invite" },
12942 { SIP_INSECURE_PORT | SIP_INSECURE_INVITE, "port,invite" },
12943 { 0, "no" },
12944 { -1, NULL },
12945 };
12946
12947
12948 static const char *insecure2str(int mode)
12949 {
12950 return map_x_s(insecurestr, mode, "<error>");
12951 }
12952
12953
12954
12955
12956 static void cleanup_stale_contexts(char *new, char *old)
12957 {
12958 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
12959
12960 while ((oldcontext = strsep(&old, "&"))) {
12961 stalecontext = '\0';
12962 ast_copy_string(newlist, new, sizeof(newlist));
12963 stringp = newlist;
12964 while ((newcontext = strsep(&stringp, "&"))) {
12965 if (!strcmp(newcontext, oldcontext)) {
12966
12967 stalecontext = '\0';
12968 break;
12969 } else if (strcmp(newcontext, oldcontext)) {
12970 stalecontext = oldcontext;
12971 }
12972
12973 }
12974 if (stalecontext)
12975 ast_context_destroy(ast_context_find(stalecontext), "SIP");
12976 }
12977 }
12978
12979
12980 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
12981 {
12982 struct sip_peer *peer;
12983 struct sip_user *user;
12984 int pruneuser = FALSE;
12985 int prunepeer = FALSE;
12986 int multi = FALSE;
12987 char *name = NULL;
12988 regex_t regexbuf;
12989
12990 if (cmd == CLI_INIT) {
12991 e->command = "sip prune realtime [peer|user|all] [all|like]";
12992 e->usage =
12993 "Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
12994 " Prunes object(s) from the cache.\n"
12995 " Optional regular expression pattern is used to filter the objects.\n";
12996 return NULL;
12997 } else if (cmd == CLI_GENERATE) {
12998 if (a->pos == 4) {
12999 if (strcasestr(a->line, "realtime peer"))
13000 return complete_sip_peer(a->word, a->n, SIP_PAGE2_RTCACHEFRIENDS);
13001 else if (strcasestr(a->line, "realtime user"))
13002 return complete_sip_user(a->word, a->n, SIP_PAGE2_RTCACHEFRIENDS);
13003 }
13004 return NULL;
13005 }
13006 switch (a->argc) {
13007 case 4:
13008 name = a->argv[3];
13009
13010 if (!strcasecmp(name, "user") || !strcasecmp(name, "peer") ||
13011 !strcasecmp(name, "like"))
13012 return CLI_SHOWUSAGE;
13013 pruneuser = prunepeer = TRUE;
13014 if (!strcasecmp(name, "all")) {
13015 multi = TRUE;
13016 name = NULL;
13017 }
13018
13019 break;
13020 case 5:
13021
13022 name = a->argv[4];
13023 if (!strcasecmp(a->argv[3], "user"))
13024 pruneuser = TRUE;
13025 else if (!strcasecmp(a->argv[3], "peer"))
13026 prunepeer = TRUE;
13027 else if (!strcasecmp(a->argv[3], "like")) {
13028 pruneuser = prunepeer = TRUE;
13029 multi = TRUE;
13030 } else
13031 return CLI_SHOWUSAGE;
13032 if (!strcasecmp(a->argv[4], "like"))
13033 return CLI_SHOWUSAGE;
13034 if (!multi && !strcasecmp(a->argv[4], "all")) {
13035 multi = TRUE;
13036 name = NULL;
13037 }
13038 break;
13039 case 6:
13040 name = a->argv[5];
13041 multi = TRUE;
13042
13043 if (strcasecmp(a->argv[4], "like"))
13044 return CLI_SHOWUSAGE;
13045 if (!strcasecmp(a->argv[3], "user")) {
13046 pruneuser = TRUE;
13047 } else if (!strcasecmp(a->argv[3], "peer")) {
13048 prunepeer = TRUE;
13049 } else
13050 return CLI_SHOWUSAGE;
13051 break;
13052 default:
13053 return CLI_SHOWUSAGE;
13054 }
13055
13056 if (multi && name) {
13057 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
13058 return CLI_SHOWUSAGE;
13059 }
13060
13061 if (multi) {
13062 if (prunepeer) {
13063 int pruned = 0;
13064
13065 ASTOBJ_CONTAINER_WRLOCK(&peerl);
13066 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
13067 ASTOBJ_RDLOCK(iterator);
13068 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
13069 ASTOBJ_UNLOCK(iterator);
13070 continue;
13071 };
13072 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
13073 ASTOBJ_MARK(iterator);
13074 pruned++;
13075 }
13076 ASTOBJ_UNLOCK(iterator);
13077 } while (0) );
13078 if (pruned) {
13079 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
13080 ast_cli(a->fd, "%d peers pruned.\n", pruned);
13081 } else
13082 ast_cli(a->fd, "No peers found to prune.\n");
13083 ASTOBJ_CONTAINER_UNLOCK(&peerl);
13084 }
13085 if (pruneuser) {
13086 int pruned = 0;
13087
13088 ASTOBJ_CONTAINER_WRLOCK(&userl);
13089 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
13090 ASTOBJ_RDLOCK(iterator);
13091 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
13092 ASTOBJ_UNLOCK(iterator);
13093 continue;
13094 };
13095 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
13096 ASTOBJ_MARK(iterator);
13097 pruned++;
13098 }
13099 ASTOBJ_UNLOCK(iterator);
13100 } while (0) );
13101 if (pruned) {
13102 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
13103 ast_cli(a->fd, "%d users pruned.\n", pruned);
13104 } else
13105 ast_cli(a->fd, "No users found to prune.\n");
13106 ASTOBJ_CONTAINER_UNLOCK(&userl);
13107 }
13108 } else {
13109 if (prunepeer) {
13110 if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
13111 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
13112 ast_cli(a->fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
13113 ASTOBJ_CONTAINER_LINK(&peerl, peer);
13114 } else
13115 ast_cli(a->fd, "Peer '%s' pruned.\n", name);
13116 unref_peer(peer);
13117 } else
13118 ast_cli(a->fd, "Peer '%s' not found.\n", name);
13119 }
13120 if (pruneuser) {
13121 if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
13122 if (!ast_test_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
13123 ast_cli(a->fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
13124 ASTOBJ_CONTAINER_LINK(&userl, user);
13125 } else
13126 ast_cli(a->fd, "User '%s' pruned.\n", name);
13127 unref_user(user);
13128 } else
13129 ast_cli(a->fd, "User '%s' not found.\n", name);
13130 }
13131 }
13132
13133 return CLI_SUCCESS;
13134 }
13135
13136
13137 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
13138 {
13139 int x, codec;
13140
13141 for(x = 0; x < 32 ; x++) {
13142 codec = ast_codec_pref_index(pref, x);
13143 if (!codec)
13144 break;
13145 ast_cli(fd, "%s", ast_getformatname(codec));
13146 ast_cli(fd, ":%d", pref->framing[x]);
13147 if (x < 31 && ast_codec_pref_index(pref, x + 1))
13148 ast_cli(fd, ",");
13149 }
13150 if (!x)
13151 ast_cli(fd, "none");
13152 }
13153
13154
13155 static const char *domain_mode_to_text(const enum domain_mode mode)
13156 {
13157 switch (mode) {
13158 case SIP_DOMAIN_AUTO:
13159 return "[Automatic]";
13160 case SIP_DOMAIN_CONFIG:
13161 return "[Configured]";
13162 }
13163
13164 return "";
13165 }
13166
13167
13168 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13169 {
13170 struct domain *d;
13171 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
13172
13173 switch (cmd) {
13174 case CLI_INIT:
13175 e->command = "sip show domains";
13176 e->usage =
13177 "Usage: sip show domains\n"
13178 " Lists all configured SIP local domains.\n"
13179 " Asterisk only responds to SIP messages to local domains.\n";
13180 return NULL;
13181 case CLI_GENERATE:
13182 return NULL;
13183 }
13184
13185 if (AST_LIST_EMPTY(&domain_list)) {
13186 ast_cli(a->fd, "SIP Domain support not enabled.\n\n");
13187 return CLI_SUCCESS;
13188 } else {
13189 ast_cli(a->fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
13190 AST_LIST_LOCK(&domain_list);
13191 AST_LIST_TRAVERSE(&domain_list, d, list)
13192 ast_cli(a->fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
13193 domain_mode_to_text(d->mode));
13194 AST_LIST_UNLOCK(&domain_list);
13195 ast_cli(a->fd, "\n");
13196 return CLI_SUCCESS;
13197 }
13198 }
13199 #undef FORMAT
13200
13201 static char mandescr_show_peer[] =
13202 "Description: Show one SIP peer with details on current status.\n"
13203 "Variables: \n"
13204 " Peer: <name> The peer name you want to check.\n"
13205 " ActionID: <id> Optional action ID for this AMI transaction.\n";
13206
13207
13208 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
13209 {
13210 const char *a[4];
13211 const char *peer;
13212
13213 peer = astman_get_header(m, "Peer");
13214 if (ast_strlen_zero(peer)) {
13215 astman_send_error(s, m, "Peer: <name> missing.");
13216 return 0;
13217 }
13218 a[0] = "sip";
13219 a[1] = "show";
13220 a[2] = "peer";
13221 a[3] = peer;
13222
13223 _sip_show_peer(1, -1, s, m, 4, a);
13224 astman_append(s, "\r\n\r\n" );
13225 return 0;
13226 }
13227
13228
13229 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13230 {
13231 switch (cmd) {
13232 case CLI_INIT:
13233 e->command = "sip show peer";
13234 e->usage =
13235 "Usage: sip show peer <name> [load]\n"
13236 " Shows all details on one SIP peer and the current status.\n"
13237 " Option \"load\" forces lookup of peer in realtime storage.\n";
13238 return NULL;
13239 case CLI_GENERATE:
13240 return complete_sip_show_peer(a->line, a->word, a->pos, a->n);
13241 }
13242 return _sip_show_peer(0, a->fd, NULL, NULL, a->argc, (const char **) a->argv);
13243 }
13244
13245
13246 static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer)
13247 {
13248 struct sip_mailbox *mailbox;
13249
13250 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
13251 ast_str_append(mailbox_str, 0, "%s%s%s%s",
13252 mailbox->mailbox,
13253 ast_strlen_zero(mailbox->context) ? "" : "@",
13254 S_OR(mailbox->context, ""),
13255 AST_LIST_NEXT(mailbox, entry) ? "," : "");
13256 }
13257 }
13258
13259
13260 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
13261 {
13262 char status[30] = "";
13263 char cbuf[256];
13264 struct sip_peer *peer;
13265 char codec_buf[512];
13266 struct ast_codec_pref *pref;
13267 struct ast_variable *v;
13268 struct sip_auth *auth;
13269 int x = 0, codec = 0, load_realtime;
13270 int realtimepeers;
13271
13272 realtimepeers = ast_check_realtime("sippeers");
13273
13274 if (argc < 4)
13275 return CLI_SHOWUSAGE;
13276
13277 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
13278 peer = find_peer(argv[3], NULL, load_realtime, 0);
13279 if (s) {
13280 if (peer) {
13281 const char *id = astman_get_header(m, "ActionID");
13282
13283 astman_append(s, "Response: Success\r\n");
13284 if (!ast_strlen_zero(id))
13285 astman_append(s, "ActionID: %s\r\n", id);
13286 } else {
13287 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.", argv[3]);
13288 astman_send_error(s, m, cbuf);
13289 return CLI_SUCCESS;
13290 }
13291 }
13292 if (peer && type==0 ) {
13293 struct ast_str *mailbox_str = ast_str_alloca(512);
13294 ast_cli(fd, "\n\n");
13295 ast_cli(fd, " * Name : %s\n", peer->name);
13296 if (realtimepeers) {
13297 ast_cli(fd, " Realtime peer: %s\n", peer->is_realtime ? "Yes, cached" : "No");
13298 }
13299 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
13300 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
13301 for (auth = peer->auth; auth; auth = auth->next) {
13302 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
13303 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
13304 }
13305 ast_cli(fd, " Context : %s\n", peer->context);
13306 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
13307 ast_cli(fd, " Language : %s\n", peer->language);
13308 if (!ast_strlen_zero(peer->accountcode))
13309 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
13310 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
13311 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
13312 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
13313 if (!ast_strlen_zero(peer->fromuser))
13314 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
13315 if (!ast_strlen_zero(peer->fromdomain))
13316 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
13317 ast_cli(fd, " Callgroup : ");
13318 print_group(fd, peer->callgroup, 0);
13319 ast_cli(fd, " Pickupgroup : ");
13320 print_group(fd, peer->pickupgroup, 0);
13321 peer_mailboxes_to_str(&mailbox_str, peer);
13322 ast_cli(fd, " Mailbox : %s\n", mailbox_str->str);
13323 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
13324 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
13325 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
13326 if (peer->busy_level)
13327 ast_cli(fd, " Busy level : %d\n", peer->busy_level);
13328 ast_cli(fd, " Dynamic : %s\n", cli_yesno(peer->host_dynamic));
13329 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
13330 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
13331 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
13332 ast_cli(fd, " Insecure : %s\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
13333 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
13334 ast_cli(fd, " ACL : %s\n", cli_yesno(peer->ha != NULL));
13335 ast_cli(fd, " T38 pt UDPTL : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)));
13336 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
13337 ast_cli(fd, " T38 pt RTP : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_RTP)));
13338 ast_cli(fd, " T38 pt TCP : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_TCP)));
13339 #endif
13340 ast_cli(fd, " CanReinvite : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)));
13341 ast_cli(fd, " PromiscRedir : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)));
13342 ast_cli(fd, " User=Phone : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)));
13343 ast_cli(fd, " Video Support: %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)));
13344 ast_cli(fd, " Text Support : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)));
13345 ast_cli(fd, " Ign SDP ver : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_IGNORESDPVERSION)));
13346 ast_cli(fd, " Trust RPID : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_TRUSTRPID)));
13347 ast_cli(fd, " Send RPID : %s\n", cli_yesno(ast_test_flag(&peer->flags[0], SIP_SENDRPID)));
13348 ast_cli(fd, " Subscriptions: %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
13349 ast_cli(fd, " Overlap dial : %s\n", cli_yesno(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP)));
13350 if (peer->outboundproxy)
13351 ast_cli(fd, " Outb. proxy : %s %s\n", ast_strlen_zero(peer->outboundproxy->name) ? "<not set>" : peer->outboundproxy->name,
13352 peer->outboundproxy->force ? "(forced)" : "");
13353
13354
13355 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
13356 ast_cli(fd, " Timer T1 : %d\n", peer->timer_t1);
13357 ast_cli(fd, " Timer B : %d\n", peer->timer_b);
13358 ast_cli(fd, " ToHost : %s\n", peer->tohost);
13359 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));
13360 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
13361 ast_cli(fd, " Transport : %s\n", get_transport(peer->socket.type));
13362 if (!ast_strlen_zero(global_regcontext))
13363 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
13364 ast_cli(fd, " Def. Username: %s\n", peer->username);
13365 ast_cli(fd, " SIP Options : ");
13366 if (peer->sipoptions) {
13367 int lastoption = -1;
13368 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
13369 if (sip_options[x].id != lastoption) {
13370 if (peer->sipoptions & sip_options[x].id)
13371 ast_cli(fd, "%s ", sip_options[x].text);
13372 lastoption = x;
13373 }
13374 }
13375 } else
13376 ast_cli(fd, "(none)");
13377
13378 ast_cli(fd, "\n");
13379 ast_cli(fd, " Codecs : ");
13380 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
13381 ast_cli(fd, "%s\n", codec_buf);
13382 ast_cli(fd, " Codec Order : (");
13383 print_codec_to_cli(fd, &peer->prefs);
13384 ast_cli(fd, ")\n");
13385
13386 ast_cli(fd, " Auto-Framing : %s \n", cli_yesno(peer->autoframing));
13387 ast_cli(fd, " 100 on REG : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_REGISTERTRYING) ? "Yes" : "No");
13388 ast_cli(fd, " Status : ");
13389 peer_status(peer, status, sizeof(status));
13390 ast_cli(fd, "%s\n", status);
13391 ast_cli(fd, " Useragent : %s\n", peer->useragent);
13392 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
13393 ast_cli(fd, " Qualify Freq : %d ms\n", peer->qualifyfreq);
13394 if (peer->chanvars) {
13395 ast_cli(fd, " Variables :\n");
13396 for (v = peer->chanvars ; v ; v = v->next)
13397 ast_cli(fd, " %s = %s\n", v->name, v->value);
13398 }
13399
13400 ast_cli(fd, " Sess-Timers : %s\n", stmode2str(peer->stimer.st_mode_oper));
13401 ast_cli(fd, " Sess-Refresh : %s\n", strefresher2str(peer->stimer.st_ref));
13402 ast_cli(fd, " Sess-Expires : %d secs\n", peer->stimer.st_max_se);
13403 ast_cli(fd, " Min-Sess : %d secs\n", peer->stimer.st_min_se);
13404 ast_cli(fd, "\n");
13405 unref_peer(peer);
13406 } else if (peer && type == 1) {
13407 char buf[256];
13408 struct ast_str *mailbox_str = ast_str_alloca(512);
13409 astman_append(s, "Channeltype: SIP\r\n");
13410 astman_append(s, "ObjectName: %s\r\n", peer->name);
13411 astman_append(s, "ChanObjectType: peer\r\n");
13412 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
13413 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
13414 astman_append(s, "Context: %s\r\n", peer->context);
13415 astman_append(s, "Language: %s\r\n", peer->language);
13416 if (!ast_strlen_zero(peer->accountcode))
13417 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
13418 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
13419 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
13420 if (!ast_strlen_zero(peer->fromuser))
13421 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
13422 if (!ast_strlen_zero(peer->fromdomain))
13423 astman_append(s, "SIP-FromDomain: %s\r\n", peer->fromdomain);
13424 astman_append(s, "Callgroup: ");
13425 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->callgroup));
13426 astman_append(s, "Pickupgroup: ");
13427 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->pickupgroup));
13428 peer_mailboxes_to_str(&mailbox_str, peer);
13429 astman_append(s, "VoiceMailbox: %s\r\n", mailbox_str->str);
13430 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
13431 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
13432 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
13433 astman_append(s, "Busy-level: %d\r\n", peer->busy_level);
13434 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
13435 astman_append(s, "Dynamic: %s\r\n", peer->host_dynamic?"Y":"N");
13436 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
13437 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched, peer->expire));
13438 astman_append(s, "SIP-AuthInsecure: %s\r\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
13439 astman_append(s, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
13440 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
13441 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Y":"N"));
13442 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
13443 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
13444 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
13445 astman_append(s, "SIP-TextSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)?"Y":"N"));
13446 astman_append(s, "SIP-Sess-Timers: %s\r\n", stmode2str(peer->stimer.st_mode_oper));
13447 astman_append(s, "SIP-Sess-Refresh: %s\r\n", strefresher2str(peer->stimer.st_ref));
13448 astman_append(s, "SIP-Sess-Expires: %d\r\n", peer->stimer.st_max_se);
13449 astman_append(s, "SIP-Sess-Min: %d\r\n", peer->stimer.st_min_se);
13450
13451
13452 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
13453 astman_append(s, "ToHost: %s\r\n", peer->tohost);
13454 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));
13455 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));
13456 astman_append(s, "Default-Username: %s\r\n", peer->username);
13457 if (!ast_strlen_zero(global_regcontext))
13458 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
13459 astman_append(s, "Codecs: ");
13460 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
13461 astman_append(s, "%s\r\n", codec_buf);
13462 astman_append(s, "CodecOrder: ");
13463 pref = &peer->prefs;
13464 for(x = 0; x < 32 ; x++) {
13465 codec = ast_codec_pref_index(pref, x);
13466 if (!codec)
13467 break;
13468 astman_append(s, "%s", ast_getformatname(codec));
13469 if (x < 31 && ast_codec_pref_index(pref, x+1))
13470 astman_append(s, ",");
13471 }
13472
13473 astman_append(s, "\r\n");
13474 astman_append(s, "Status: ");
13475 peer_status(peer, status, sizeof(status));
13476 astman_append(s, "%s\r\n", status);
13477 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
13478 astman_append(s, "Reg-Contact : %s\r\n", peer->fullcontact);
13479 astman_append(s, "QualifyFreq : %d ms\n", peer->qualifyfreq);
13480 if (peer->chanvars) {
13481 for (v = peer->chanvars ; v ; v = v->next) {
13482 astman_append(s, "ChanVariable:\n");
13483 astman_append(s, " %s,%s\r\n", v->name, v->value);
13484 }
13485 }
13486
13487 unref_peer(peer);
13488
13489 } else {
13490 ast_cli(fd, "Peer %s not found.\n", argv[3]);
13491 ast_cli(fd, "\n");
13492 }
13493
13494 return CLI_SUCCESS;
13495 }
13496
13497
13498 static char *sip_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13499 {
13500 char cbuf[256];
13501 struct sip_user *user;
13502 struct ast_variable *v;
13503 int load_realtime;
13504
13505 switch (cmd) {
13506 case CLI_INIT:
13507 e->command = "sip show user";
13508 e->usage =
13509 "Usage: sip show user <name> [load]\n"
13510 " Shows all details on one SIP user and the current status.\n"
13511 " Option \"load\" forces lookup of peer in realtime storage.\n";
13512 return NULL;
13513 case CLI_GENERATE:
13514 return complete_sip_show_user(a->line, a->word, a->pos, a->n);
13515 }
13516
13517 if (a->argc < 4)
13518 return CLI_SHOWUSAGE;
13519
13520
13521 load_realtime = (a->argc == 5 && !strcmp(a->argv[4], "load")) ? TRUE : FALSE;
13522
13523 user = find_user(a->argv[3], load_realtime);
13524 if (user) {
13525 ast_cli(a->fd, "\n\n");
13526 ast_cli(a->fd, " * Name : %s\n", user->name);
13527 ast_cli(a->fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
13528 ast_cli(a->fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
13529 ast_cli(a->fd, " Context : %s\n", user->context);
13530 ast_cli(a->fd, " Language : %s\n", user->language);
13531 if (!ast_strlen_zero(user->accountcode))
13532 ast_cli(a->fd, " Accountcode : %s\n", user->accountcode);
13533 ast_cli(a->fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
13534 ast_cli(a->fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
13535 ast_cli(a->fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
13536 ast_cli(a->fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
13537 ast_cli(a->fd, " Call limit : %d\n", user->call_limit);
13538 ast_cli(a->fd, " Callgroup : ");
13539 print_group(a->fd, user->callgroup, 0);
13540 ast_cli(a->fd, " Pickupgroup : ");
13541 print_group(a->fd, user->pickupgroup, 0);
13542 ast_cli(a->fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
13543 ast_cli(a->fd, " ACL : %s\n", cli_yesno(user->ha != NULL));
13544 ast_cli(a->fd, " Sess-Timers : %s\n", stmode2str(user->stimer.st_mode_oper));
13545 ast_cli(a->fd, " Sess-Refresh : %s\n", strefresher2str(user->stimer.st_ref));
13546 ast_cli(a->fd, " Sess-Expires : %d secs\n", user->stimer.st_max_se);
13547 ast_cli(a->fd, " Sess-Min-SE : %d secs\n", user->stimer.st_min_se);
13548 ast_cli(a->fd, " Ign SDP ver : %s\n", cli_yesno(ast_test_flag(&user->flags[1], SIP_PAGE2_IGNORESDPVERSION)));
13549
13550 ast_cli(a->fd, " Codec Order : (");
13551 print_codec_to_cli(a->fd, &user->prefs);
13552 ast_cli(a->fd, ")\n");
13553
13554 ast_cli(a->fd, " Auto-Framing: %s \n", cli_yesno(user->autoframing));
13555 if (user->chanvars) {
13556 ast_cli(a->fd, " Variables :\n");
13557 for (v = user->chanvars ; v ; v = v->next)
13558 ast_cli(a->fd, " %s = %s\n", v->name, v->value);
13559 }
13560
13561 ast_cli(a->fd, "\n");
13562
13563 unref_user(user);
13564 } else {
13565 ast_cli(a->fd, "User %s not found.\n", a->argv[3]);
13566 ast_cli(a->fd, "\n");
13567 }
13568
13569 return CLI_SUCCESS;
13570 }
13571
13572
13573 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13574 {
13575 #define FORMAT2 "%-30.30s %-12.12s %8.8s %-20.20s %-25.25s\n"
13576 #define FORMAT "%-30.30s %-12.12s %8d %-20.20s %-25.25s\n"
13577 char host[80];
13578 char tmpdat[256];
13579 struct ast_tm tm;
13580 int counter = 0;
13581
13582 switch (cmd) {
13583 case CLI_INIT:
13584 e->command = "sip show registry";
13585 e->usage =
13586 "Usage: sip show registry\n"
13587 " Lists all registration requests and status.\n";
13588 return NULL;
13589 case CLI_GENERATE:
13590 return NULL;
13591 }
13592
13593 if (a->argc != 3)
13594 return CLI_SHOWUSAGE;
13595 ast_cli(a->fd, FORMAT2, "Host", "Username", "Refresh", "State", "Reg.Time");
13596 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
13597 ASTOBJ_RDLOCK(iterator);
13598 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
13599 if (iterator->regtime.tv_sec) {
13600 ast_localtime(&iterator->regtime, &tm, NULL);
13601 ast_strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
13602 } else
13603 tmpdat[0] = '\0';
13604 ast_cli(a->fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
13605 ASTOBJ_UNLOCK(iterator);
13606 counter++;
13607 } while(0));
13608 ast_cli(a->fd, "%d SIP registrations.\n", counter);
13609 return CLI_SUCCESS;
13610 #undef FORMAT
13611 #undef FORMAT2
13612 }
13613
13614
13615
13616
13617
13618 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13619 {
13620 struct sip_peer *peer;
13621 int load_realtime = 0;
13622
13623 switch (cmd) {
13624 case CLI_INIT:
13625 e->command = "sip unregister";
13626 e->usage =
13627 "Usage: sip unregister <peer>\n"
13628 " Unregister (force expiration) a SIP peer from the registry\n";
13629 return NULL;
13630 case CLI_GENERATE:
13631 return complete_sip_unregister(a->line, a->word, a->pos, a->n);
13632 }
13633
13634 if (a->argc != 3)
13635 return CLI_SHOWUSAGE;
13636
13637 if ((peer = find_peer(a->argv[2], NULL, load_realtime, 0))) {
13638 if (peer->expire > 0) {
13639 AST_SCHED_DEL(sched, peer->expire);
13640 expire_register(peer);
13641 ast_cli(a->fd, "Unregistered peer \'%s\'\n\n", a->argv[2]);
13642 } else {
13643 ast_cli(a->fd, "Peer %s not registered\n", a->argv[2]);
13644 }
13645 } else {
13646 ast_cli(a->fd, "Peer unknown: \'%s\'. Not unregistered.\n", a->argv[2]);
13647 }
13648
13649 return CLI_SUCCESS;
13650 }
13651
13652
13653 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13654 {
13655 int realtimepeers;
13656 int realtimeusers;
13657 int realtimeregs;
13658 char codec_buf[SIPBUFSIZE];
13659 const char *msg;
13660
13661 switch (cmd) {
13662 case CLI_INIT:
13663 e->command = "sip show settings";
13664 e->usage =
13665 "Usage: sip show settings\n"
13666 " Provides detailed list of the configuration of the SIP channel.\n";
13667 return NULL;
13668 case CLI_GENERATE:
13669 return NULL;
13670 }
13671
13672
13673 realtimepeers = ast_check_realtime("sippeers");
13674 realtimeusers = ast_check_realtime("sipusers");
13675 realtimeregs = ast_check_realtime("sipregs");
13676
13677 if (a->argc != 3)
13678 return CLI_SHOWUSAGE;
13679 ast_cli(a->fd, "\n\nGlobal Settings:\n");
13680 ast_cli(a->fd, "----------------\n");
13681 ast_cli(a->fd, " UDP SIP Port: %d\n", ntohs(bindaddr.sin_port));
13682 ast_cli(a->fd, " UDP Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
13683 ast_cli(a->fd, " TCP SIP Port: ");
13684 if (sip_tcp_desc.sin.sin_family == AF_INET) {
13685 ast_cli(a->fd, "%d\n", ntohs(sip_tcp_desc.sin.sin_port));
13686 ast_cli(a->fd, " TCP Bindaddress: %s\n", ast_inet_ntoa(sip_tcp_desc.sin.sin_addr));
13687 } else {
13688 ast_cli(a->fd, "Disabled\n");
13689 }
13690 ast_cli(a->fd, " TLS SIP Port: ");
13691 if (default_tls_cfg.enabled != FALSE) {
13692 ast_cli(a->fd, "%d\n", ntohs(sip_tls_desc.sin.sin_port));
13693 ast_cli(a->fd, " TLS Bindaddress: %s\n", ast_inet_ntoa(sip_tls_desc.sin.sin_addr));
13694 } else {
13695 ast_cli(a->fd, "Disabled\n");
13696 }
13697 ast_cli(a->fd, " Videosupport: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT)));
13698 ast_cli(a->fd, " Textsupport: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT)));
13699 ast_cli(a->fd, " AutoCreate Peer: %s\n", cli_yesno(autocreatepeer));
13700 ast_cli(a->fd, " Ignore SDP sess. ver.: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION)));
13701 ast_cli(a->fd, " Match Auth Username: %s\n", cli_yesno(global_match_auth_username));
13702 ast_cli(a->fd, " Allow unknown access: %s\n", cli_yesno(global_allowguest));
13703 ast_cli(a->fd, " Allow subscriptions: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
13704 ast_cli(a->fd, " Allow overlap dialing: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP)));
13705 ast_cli(a->fd, " Allow promsic. redir: %s\n", cli_yesno(ast_test_flag(&global_flags[0], SIP_PROMISCREDIR)));
13706 ast_cli(a->fd, " Enable call counters: %s\n", cli_yesno(global_callcounter));
13707 ast_cli(a->fd, " SIP domain support: %s\n", cli_yesno(!AST_LIST_EMPTY(&domain_list)));
13708 ast_cli(a->fd, " Realm. auth: %s\n", cli_yesno(authl != NULL));
13709 ast_cli(a->fd, " Our auth realm %s\n", global_realm);
13710 ast_cli(a->fd, " Call to non-local dom.: %s\n", cli_yesno(allow_external_domains));
13711 ast_cli(a->fd, " URI user is phone no: %s\n", cli_yesno(ast_test_flag(&global_flags[0], SIP_USEREQPHONE)));
13712 ast_cli(a->fd, " Always auth rejects: %s\n", cli_yesno(global_alwaysauthreject));
13713 ast_cli(a->fd, " Call limit peers only: %s\n", cli_yesno(global_limitonpeers));
13714 ast_cli(a->fd, " Direct RTP setup: %s\n", cli_yesno(global_directrtpsetup));
13715 ast_cli(a->fd, " User Agent: %s\n", global_useragent);
13716 ast_cli(a->fd, " SDP Session Name: %s\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
13717 ast_cli(a->fd, " SDP Owner Name: %s\n", ast_strlen_zero(global_sdpowner) ? "-" : global_sdpowner);
13718 ast_cli(a->fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
13719 ast_cli(a->fd, " Regexten on Qualify: %s\n", cli_yesno(global_regextenonqualify));
13720 ast_cli(a->fd, " Caller ID: %s\n", default_callerid);
13721 ast_cli(a->fd, " From: Domain: %s\n", default_fromdomain);
13722 ast_cli(a->fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
13723 ast_cli(a->fd, " Call Events: %s\n", global_callevents ? "On" : "Off");
13724
13725 ast_cli(a->fd, " T38 fax pt UDPTL: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)));
13726 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
13727 ast_cli(a->fd, " T38 fax pt RTP: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_RTP)));
13728 ast_cli(a->fd, " T38 fax pt TCP: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_TCP)));
13729 #endif
13730 if (!realtimepeers && !realtimeusers && !realtimeregs)
13731 ast_cli(a->fd, " SIP realtime: Disabled\n" );
13732 else
13733 ast_cli(a->fd, " SIP realtime: Enabled\n" );
13734 ast_cli(a->fd, " Qualify Freq : %d ms\n", global_qualifyfreq);
13735 ast_cli(a->fd, "\nNetwork QoS Settings:\n");
13736 ast_cli(a->fd, "---------------------------\n");
13737 ast_cli(a->fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
13738 ast_cli(a->fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
13739 ast_cli(a->fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
13740 ast_cli(a->fd, " IP ToS RTP text: %s\n", ast_tos2str(global_tos_text));
13741 ast_cli(a->fd, " 802.1p CoS SIP: %d\n", global_cos_sip);
13742 ast_cli(a->fd, " 802.1p CoS RTP audio: %d\n", global_cos_audio);
13743 ast_cli(a->fd, " 802.1p CoS RTP video: %d\n", global_cos_video);
13744 ast_cli(a->fd, " 802.1p CoS RTP text: %d\n", global_cos_text);
13745 ast_cli(a->fd, " Jitterbuffer enabled: %s\n", cli_yesno(ast_test_flag(&global_jbconf, AST_JB_ENABLED)));
13746 ast_cli(a->fd, " Jitterbuffer forced: %s\n", cli_yesno(ast_test_flag(&global_jbconf, AST_JB_FORCED)));
13747 ast_cli(a->fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
13748 ast_cli(a->fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
13749 ast_cli(a->fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
13750 ast_cli(a->fd, " Jitterbuffer log: %s\n", cli_yesno(ast_test_flag(&global_jbconf, AST_JB_LOG)));
13751
13752 ast_cli(a->fd, "\nNetwork Settings:\n");
13753 ast_cli(a->fd, "---------------------------\n");
13754
13755 if (localaddr == NULL)
13756 msg = "Disabled, no localnet list";
13757 else if (externip.sin_addr.s_addr == 0)
13758 msg = "Disabled, externip is 0.0.0.0";
13759 else if (stunaddr.sin_addr.s_addr != 0)
13760 msg = "Enabled using STUN";
13761 else if (!ast_strlen_zero(externhost))
13762 msg = "Enabled using externhost";
13763 else
13764 msg = "Enabled using externip";
13765 ast_cli(a->fd, " SIP address remapping: %s\n", msg);
13766 ast_cli(a->fd, " Externhost: %s\n", S_OR(externhost, "<none>"));
13767 ast_cli(a->fd, " Externip: %s:%d\n", ast_inet_ntoa(externip.sin_addr), ntohs(externip.sin_port));
13768 ast_cli(a->fd, " Externrefresh: %d\n", externrefresh);
13769 ast_cli(a->fd, " Internal IP: %s:%d\n", ast_inet_ntoa(internip.sin_addr), ntohs(internip.sin_port));
13770 {
13771 struct ast_ha *d;
13772 const char *prefix = "Localnet:";
13773 char buf[INET_ADDRSTRLEN];
13774
13775 for (d = localaddr; d ; prefix = "", d = d->next) {
13776 ast_cli(a->fd, " %-24s%s/%s\n",
13777 prefix, ast_inet_ntoa(d->netaddr),
13778 inet_ntop(AF_INET, &d->netmask, buf, sizeof(buf)) );
13779 }
13780 }
13781 ast_cli(a->fd, " STUN server: %s:%d\n", ast_inet_ntoa(stunaddr.sin_addr), ntohs(stunaddr.sin_port));
13782
13783 ast_cli(a->fd, "\nGlobal Signalling Settings:\n");
13784 ast_cli(a->fd, "---------------------------\n");
13785 ast_cli(a->fd, " Codecs: ");
13786 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, global_capability);
13787 ast_cli(a->fd, "%s\n", codec_buf);
13788 ast_cli(a->fd, " Codec Order: ");
13789 print_codec_to_cli(a->fd, &default_prefs);
13790 ast_cli(a->fd, "\n");
13791 ast_cli(a->fd, " Relax DTMF: %s\n", cli_yesno(global_relaxdtmf));
13792 ast_cli(a->fd, " RFC2833 Compensation: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE)));
13793 ast_cli(a->fd, " Compact SIP headers: %s\n", cli_yesno(compactheaders));
13794 ast_cli(a->fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
13795 ast_cli(a->fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
13796 ast_cli(a->fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
13797 ast_cli(a->fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
13798 ast_cli(a->fd, " DNS SRV lookup: %s\n", cli_yesno(global_srvlookup));
13799 ast_cli(a->fd, " Pedantic SIP support: %s\n", cli_yesno(pedanticsipchecking));
13800 ast_cli(a->fd, " Reg. min duration %d secs\n", min_expiry);
13801 ast_cli(a->fd, " Reg. max duration: %d secs\n", max_expiry);
13802 ast_cli(a->fd, " Reg. default duration: %d secs\n", default_expiry);
13803 ast_cli(a->fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
13804 ast_cli(a->fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
13805 ast_cli(a->fd, " Notify ringing state: %s\n", cli_yesno(global_notifyringing));
13806 ast_cli(a->fd, " Notify hold state: %s\n", cli_yesno(global_notifyhold));
13807 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(global_allowtransfer));
13808 ast_cli(a->fd, " Max Call Bitrate: %d kbps\n", default_maxcallbitrate);
13809 ast_cli(a->fd, " Auto-Framing: %s\n", cli_yesno(global_autoframing));
13810 ast_cli(a->fd, " Outb. proxy: %s %s\n", ast_strlen_zero(global_outboundproxy.name) ? "<not set>" : global_outboundproxy.name,
13811 global_outboundproxy.force ? "(forced)" : "");
13812 ast_cli(a->fd, " Session Timers: %s\n", stmode2str(global_st_mode));
13813 ast_cli(a->fd, " Session Refresher: %s\n", strefresher2str (global_st_refresher));
13814 ast_cli(a->fd, " Session Expires: %d secs\n", global_max_se);
13815 ast_cli(a->fd, " Session Min-SE: %d secs\n", global_min_se);
13816 ast_cli(a->fd, " Timer T1: %d\n", global_t1);
13817 ast_cli(a->fd, " Timer T1 minimum: %d\n", global_t1min);
13818 ast_cli(a->fd, " Timer B: %d\n", global_timer_b);
13819
13820 ast_cli(a->fd, "\nDefault Settings:\n");
13821 ast_cli(a->fd, "-----------------\n");
13822 ast_cli(a->fd, " Context: %s\n", default_context);
13823 ast_cli(a->fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags[0], SIP_NAT)));
13824 ast_cli(a->fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
13825 ast_cli(a->fd, " Qualify: %d\n", default_qualify);
13826 ast_cli(a->fd, " Use ClientCode: %s\n", cli_yesno(ast_test_flag(&global_flags[0], SIP_USECLIENTCODE)));
13827 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" );
13828 ast_cli(a->fd, " Language: %s\n", default_language);
13829 ast_cli(a->fd, " MOH Interpret: %s\n", default_mohinterpret);
13830 ast_cli(a->fd, " MOH Suggest: %s\n", default_mohsuggest);
13831 ast_cli(a->fd, " Voice Mail Extension: %s\n", default_vmexten);
13832
13833
13834 if (realtimepeers || realtimeusers || realtimeregs) {
13835 ast_cli(a->fd, "\nRealtime SIP Settings:\n");
13836 ast_cli(a->fd, "----------------------\n");
13837 ast_cli(a->fd, " Realtime Peers: %s\n", cli_yesno(realtimepeers));
13838 ast_cli(a->fd, " Realtime Users: %s\n", cli_yesno(realtimeusers));
13839 ast_cli(a->fd, " Realtime Regs: %s\n", cli_yesno(realtimeregs));
13840 ast_cli(a->fd, " Cache Friends: %s\n", cli_yesno(ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)));
13841 ast_cli(a->fd, " Update: %s\n", cli_yesno(sip_cfg.peer_rtupdate));
13842 ast_cli(a->fd, " Ignore Reg. Expire: %s\n", cli_yesno(sip_cfg.ignore_regexpire));
13843 ast_cli(a->fd, " Save sys. name: %s\n", cli_yesno(sip_cfg.rtsave_sysname));
13844 ast_cli(a->fd, " Auto Clear: %d\n", global_rtautoclear);
13845 }
13846 ast_cli(a->fd, "\n----\n");
13847 return CLI_SUCCESS;
13848 }
13849
13850
13851 static const char *subscription_type2str(enum subscriptiontype subtype)
13852 {
13853 int i;
13854
13855 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
13856 if (subscription_types[i].type == subtype) {
13857 return subscription_types[i].text;
13858 }
13859 }
13860 return subscription_types[0].text;
13861 }
13862
13863
13864 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
13865 {
13866 int i;
13867
13868 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
13869 if (subscription_types[i].type == subtype) {
13870 return &subscription_types[i];
13871 }
13872 }
13873 return &subscription_types[0];
13874 }
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884 struct __show_chan_arg {
13885 int fd;
13886 int subscriptions;
13887 int numchans;
13888 };
13889
13890 #define FORMAT3 "%-15.15s %-10.10s %-15.15s %-15.15s %-13.13s %-15.15s %-10.10s\n"
13891 #define FORMAT2 "%-15.15s %-10.10s %-15.15s %-15.15s %-7.7s %-15.15s\n"
13892 #define FORMAT "%-15.15s %-10.10s %-15.15s %-15.15s %-3.3s %-3.3s %-15.15s %-10.10s\n"
13893
13894
13895 static int show_channels_cb(void *__cur, void *__arg, int flags)
13896 {
13897 struct sip_pvt *cur = __cur;
13898 struct __show_chan_arg *arg = __arg;
13899 const struct sockaddr_in *dst = sip_real_dst(cur);
13900
13901
13902 if (cur->subscribed == NONE && !arg->subscriptions) {
13903
13904 const char *referstatus = cur->refer ? referstatus2str(cur->refer->status) : "";
13905 char formatbuf[SIPBUFSIZE/2];
13906
13907 ast_cli(arg->fd, FORMAT, ast_inet_ntoa(dst->sin_addr),
13908 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
13909 cur->callid,
13910 ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
13911 cli_yesno(ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD)),
13912 cur->needdestroy ? "(d)" : "",
13913 cur->lastmsg ,
13914 referstatus
13915 );
13916 arg->numchans++;
13917 }
13918 if (cur->subscribed != NONE && arg->subscriptions) {
13919 struct ast_str *mailbox_str = ast_str_alloca(512);
13920 if (cur->subscribed == MWI_NOTIFICATION && cur->relatedpeer)
13921 peer_mailboxes_to_str(&mailbox_str, cur->relatedpeer);
13922 ast_cli(arg->fd, FORMAT3, ast_inet_ntoa(dst->sin_addr),
13923 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
13924 cur->callid,
13925
13926 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
13927 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
13928 subscription_type2str(cur->subscribed),
13929 cur->subscribed == MWI_NOTIFICATION ? S_OR(mailbox_str->str, "<none>") : "<none>"
13930 );
13931 arg->numchans++;
13932 }
13933
13934 return 0;
13935 }
13936
13937
13938
13939
13940
13941
13942 static char *sip_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
13943 {
13944 struct sip_pvt *cur;
13945 struct __show_chan_arg arg = { .fd = a->fd, .numchans = 0 };
13946
13947 if (cmd == CLI_INIT) {
13948 e->command = "sip show {channels|subscriptions}";
13949 e->usage =
13950 "Usage: sip show channels\n"
13951 " Lists all currently active SIP calls (dialogs).\n"
13952 "Usage: sip show subscriptions\n"
13953 " Lists active SIP subscriptions.\n";
13954 return NULL;
13955 } else if (cmd == CLI_GENERATE)
13956 return NULL;
13957
13958 if (a->argc != e->args)
13959 return CLI_SHOWUSAGE;
13960 arg.subscriptions = !strcasecmp(a->argv[e->args - 1], "subscriptions");
13961 if (!arg.subscriptions)
13962 ast_cli(arg.fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Format", "Hold", "Last Message");
13963 else
13964 ast_cli(arg.fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox");
13965
13966
13967 dialoglist_lock();
13968 for (cur = dialoglist; cur; cur = cur->next) {
13969 show_channels_cb(cur, &arg, 0);
13970 }
13971 dialoglist_unlock();
13972
13973
13974 ast_cli(arg.fd, "%d active SIP %s%s\n", arg.numchans,
13975 (arg.subscriptions ? "subscription" : "dialog"),
13976 ESS(arg.numchans));
13977 return CLI_SUCCESS;
13978 #undef FORMAT
13979 #undef FORMAT2
13980 #undef FORMAT3
13981 }
13982
13983
13984
13985
13986
13987
13988 static char *complete_sipch(const char *line, const char *word, int pos, int state)
13989 {
13990 int which=0;
13991 struct sip_pvt *cur;
13992 char *c = NULL;
13993 int wordlen = strlen(word);
13994
13995 if (pos != 3) {
13996 return NULL;
13997 }
13998
13999 dialoglist_lock();
14000 for (cur = dialoglist; cur; cur = cur->next) {
14001 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
14002 c = ast_strdup(cur->callid);
14003 break;
14004 }
14005 }
14006 dialoglist_unlock();
14007 return c;
14008 }
14009
14010
14011 static char *complete_sip_peer(const char *word, int state, int flags2)
14012 {
14013 char *result = NULL;
14014 int wordlen = strlen(word);
14015 int which = 0;
14016
14017 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
14018
14019 if (!strncasecmp(word, iterator->name, wordlen) &&
14020 (!flags2 || ast_test_flag(&iterator->flags[1], flags2)) &&
14021 ++which > state)
14022 result = ast_strdup(iterator->name);
14023 } while(0) );
14024 return result;
14025 }
14026
14027
14028 static char *complete_sip_registered_peer(const char *word, int state, int flags2)
14029 {
14030 char *result = NULL;
14031 int wordlen = strlen(word);
14032 int which = 0;
14033
14034 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
14035 ASTOBJ_WRLOCK(iterator);
14036 if (!strncasecmp(word, iterator->name, wordlen) &&
14037 (!flags2 || ast_test_flag(&iterator->flags[1], flags2)) &&
14038 ++which > state && iterator->expire > 0)
14039 result = ast_strdup(iterator->name);
14040 ASTOBJ_UNLOCK(iterator);
14041 } while(0) );
14042 return result;
14043 }
14044
14045
14046 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state)
14047 {
14048 if (pos == 3)
14049 return complete_sipch(line, word, pos, state);
14050
14051 return NULL;
14052 }
14053
14054
14055 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
14056 {
14057 if (pos == 3)
14058 return complete_sip_peer(word, state, 0);
14059
14060 return NULL;
14061 }
14062
14063
14064 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state)
14065 {
14066 if (pos == 2)
14067 return complete_sip_registered_peer(word, state, 0);
14068
14069 return NULL;
14070 }
14071
14072
14073 static char *complete_sip_user(const char *word, int state, int flags2)
14074 {
14075 char *result = NULL;
14076 int wordlen = strlen(word);
14077 int which = 0;
14078
14079 ASTOBJ_CONTAINER_TRAVERSE(&userl, !result, do {
14080
14081 if (!strncasecmp(word, iterator->name, wordlen)) {
14082 if (flags2 && !ast_test_flag(&iterator->flags[1], flags2))
14083 continue;
14084 if (++which > state) {
14085 result = ast_strdup(iterator->name);
14086 }
14087 }
14088 } while(0) );
14089 return result;
14090 }
14091
14092
14093 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
14094 {
14095 if (pos == 3)
14096 return complete_sip_user(word, state, 0);
14097
14098 return NULL;
14099 }
14100
14101
14102 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
14103 {
14104 char *c = NULL;
14105
14106 if (pos == 2) {
14107 int which = 0;
14108 char *cat = NULL;
14109 int wordlen = strlen(word);
14110
14111
14112
14113 if (!notify_types)
14114 return NULL;
14115
14116 while ( (cat = ast_category_browse(notify_types, cat)) ) {
14117 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
14118 c = ast_strdup(cat);
14119 break;
14120 }
14121 }
14122 return c;
14123 }
14124
14125 if (pos > 2)
14126 return complete_sip_peer(word, state, 0);
14127
14128 return NULL;
14129 }
14130
14131
14132 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14133 {
14134 struct sip_pvt *cur;
14135 size_t len;
14136 int found = 0;
14137
14138 switch (cmd) {
14139 case CLI_INIT:
14140 e->command = "sip show channel";
14141 e->usage =
14142 "Usage: sip show channel <call-id>\n"
14143 " Provides detailed status on a given SIP dialog (identified by SIP call-id).\n";
14144 return NULL;
14145 case CLI_GENERATE:
14146 return complete_sipch(a->line, a->word, a->pos, a->n);
14147 }
14148
14149 if (a->argc != 4)
14150 return CLI_SHOWUSAGE;
14151 len = strlen(a->argv[3]);
14152 dialoglist_lock();
14153 for (cur = dialoglist; cur; cur = cur->next) {
14154 if (!strncasecmp(cur->callid, a->argv[3], len)) {
14155 char formatbuf[SIPBUFSIZE/2];
14156 ast_cli(a->fd, "\n");
14157 if (cur->subscribed != NONE)
14158 ast_cli(a->fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
14159 else
14160 ast_cli(a->fd, " * SIP Call\n");
14161 ast_cli(a->fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
14162 ast_cli(a->fd, " Call-ID: %s\n", cur->callid);
14163 ast_cli(a->fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
14164 ast_cli(a->fd, " Our Codec Capability: %d\n", cur->capability);
14165 ast_cli(a->fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
14166 ast_cli(a->fd, " Their Codec Capability: %d\n", cur->peercapability);
14167 ast_cli(a->fd, " Joint Codec Capability: %d\n", cur->jointcapability);
14168 ast_cli(a->fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
14169 ast_cli(a->fd, " T.38 support %s\n", cli_yesno(cur->udptl != NULL));
14170 ast_cli(a->fd, " Video support %s\n", cli_yesno(cur->vrtp != NULL));
14171 ast_cli(a->fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
14172 ast_cli(a->fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(cur->sa.sin_addr), ntohs(cur->sa.sin_port));
14173 ast_cli(a->fd, " Received Address: %s:%d\n", ast_inet_ntoa(cur->recv.sin_addr), ntohs(cur->recv.sin_port));
14174 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
14175 ast_cli(a->fd, " NAT Support: %s\n", nat2str(ast_test_flag(&cur->flags[0], SIP_NAT)));
14176 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)" );
14177 ast_cli(a->fd, " Our Tag: %s\n", cur->tag);
14178 ast_cli(a->fd, " Their Tag: %s\n", cur->theirtag);
14179 ast_cli(a->fd, " SIP User agent: %s\n", cur->useragent);
14180 if (!ast_strlen_zero(cur->username))
14181 ast_cli(a->fd, " Username: %s\n", cur->username);
14182 if (!ast_strlen_zero(cur->peername))
14183 ast_cli(a->fd, " Peername: %s\n", cur->peername);
14184 if (!ast_strlen_zero(cur->uri))
14185 ast_cli(a->fd, " Original uri: %s\n", cur->uri);
14186 if (!ast_strlen_zero(cur->cid_num))
14187 ast_cli(a->fd, " Caller-ID: %s\n", cur->cid_num);
14188 ast_cli(a->fd, " Need Destroy: %s\n", cli_yesno(cur->needdestroy));
14189 ast_cli(a->fd, " Last Message: %s\n", cur->lastmsg);
14190 ast_cli(a->fd, " Promiscuous Redir: %s\n", cli_yesno(ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR)));
14191 ast_cli(a->fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
14192 ast_cli(a->fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
14193 ast_cli(a->fd, " SIP Options: ");
14194 if (cur->sipoptions) {
14195 int x;
14196 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
14197 if (cur->sipoptions & sip_options[x].id)
14198 ast_cli(a->fd, "%s ", sip_options[x].text);
14199 }
14200 ast_cli(a->fd, "\n");
14201 } else
14202 ast_cli(a->fd, "(none)\n");
14203
14204 if (!cur->stimer)
14205 ast_cli(a->fd, " Session-Timer: Uninitiallized\n");
14206 else {
14207 ast_cli(a->fd, " Session-Timer: %s\n", cur->stimer->st_active ? "Active" : "Inactive");
14208 if (cur->stimer->st_active == TRUE) {
14209 ast_cli(a->fd, " S-Timer Interval: %d\n", cur->stimer->st_interval);
14210 ast_cli(a->fd, " S-Timer Refresher: %s\n", strefresher2str(cur->stimer->st_ref));
14211 ast_cli(a->fd, " S-Timer Expirys: %d\n", cur->stimer->st_expirys);
14212 ast_cli(a->fd, " S-Timer Sched Id: %d\n", cur->stimer->st_schedid);
14213 ast_cli(a->fd, " S-Timer Peer Sts: %s\n", cur->stimer->st_active_peer_ua ? "Active" : "Inactive");
14214 ast_cli(a->fd, " S-Timer Cached Min-SE: %d\n", cur->stimer->st_cached_min_se);
14215 ast_cli(a->fd, " S-Timer Cached SE: %d\n", cur->stimer->st_cached_max_se);
14216 ast_cli(a->fd, " S-Timer Cached Ref: %s\n", strefresher2str(cur->stimer->st_cached_ref));
14217 ast_cli(a->fd, " S-Timer Cached Mode: %s\n", stmode2str(cur->stimer->st_cached_mode));
14218 }
14219 }
14220
14221 ast_cli(a->fd, "\n\n");
14222
14223 found++;
14224 }
14225 }
14226 dialoglist_unlock();
14227 if (!found)
14228 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
14229 return CLI_SUCCESS;
14230 }
14231
14232
14233 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14234 {
14235 struct sip_pvt *cur;
14236 size_t len;
14237 int found = 0;
14238
14239 switch (cmd) {
14240 case CLI_INIT:
14241 e->command = "sip show history";
14242 e->usage =
14243 "Usage: sip show history <call-id>\n"
14244 " Provides detailed dialog history on a given SIP call (specified by call-id).\n";
14245 return NULL;
14246 case CLI_GENERATE:
14247 return complete_sip_show_history(a->line, a->word, a->pos, a->n);
14248 }
14249
14250 if (a->argc != 4)
14251 return CLI_SHOWUSAGE;
14252 if (!recordhistory)
14253 ast_cli(a->fd, "\n***Note: History recording is currently DISABLED. Use 'sip set history on' to ENABLE.\n");
14254 len = strlen(a->argv[3]);
14255 dialoglist_lock();
14256 for (cur = dialoglist; cur; cur = cur->next) {
14257 if (!strncasecmp(cur->callid, a->argv[3], len)) {
14258 struct sip_history *hist;
14259 int x = 0;
14260
14261 ast_cli(a->fd, "\n");
14262 if (cur->subscribed != NONE)
14263 ast_cli(a->fd, " * Subscription\n");
14264 else
14265 ast_cli(a->fd, " * SIP Call\n");
14266 if (cur->history)
14267 AST_LIST_TRAVERSE(cur->history, hist, list)
14268 ast_cli(a->fd, "%d. %s\n", ++x, hist->event);
14269 if (x == 0)
14270 ast_cli(a->fd, "Call '%s' has no history\n", cur->callid);
14271 found++;
14272 }
14273 }
14274 dialoglist_unlock();
14275 if (!found)
14276 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
14277 return CLI_SUCCESS;
14278 }
14279
14280
14281 static void sip_dump_history(struct sip_pvt *dialog)
14282 {
14283 int x = 0;
14284 struct sip_history *hist;
14285 static int errmsg = 0;
14286
14287 if (!dialog)
14288 return;
14289
14290 if (!option_debug && !sipdebug) {
14291 if (!errmsg) {
14292 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
14293 errmsg = 1;
14294 }
14295 return;
14296 }
14297
14298 ast_debug(1, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
14299 if (dialog->subscribed)
14300 ast_debug(1, " * Subscription\n");
14301 else
14302 ast_debug(1, " * SIP Call\n");
14303 if (dialog->history)
14304 AST_LIST_TRAVERSE(dialog->history, hist, list)
14305 ast_debug(1, " %-3.3d. %s\n", ++x, hist->event);
14306 if (!x)
14307 ast_debug(1, "Call '%s' has no history\n", dialog->callid);
14308 ast_debug(1, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
14309 }
14310
14311
14312
14313 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
14314 {
14315 char buf[1024];
14316 unsigned int event;
14317 const char *c = get_header(req, "Content-Type");
14318
14319
14320 if (!strcasecmp(c, "application/dtmf-relay") ||
14321 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
14322 unsigned int duration = 0;
14323
14324 if (!p->owner) {
14325 transmit_response(p, "481 Call leg/transaction does not exist", req);
14326 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14327 return;
14328 }
14329
14330
14331 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
14332 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
14333 transmit_response(p, "200 OK", req);
14334 return;
14335 } else {
14336 ast_copy_string(buf, c, sizeof(buf));
14337 }
14338
14339 if (!ast_strlen_zero((c = get_body(req, "Duration"))))
14340 duration = atoi(c);
14341 if (!duration)
14342 duration = 100;
14343
14344
14345 if (ast_strlen_zero(buf)) {
14346 transmit_response(p, "200 OK", req);
14347 return;
14348 }
14349
14350 if (buf[0] == '*')
14351 event = 10;
14352 else if (buf[0] == '#')
14353 event = 11;
14354 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
14355 event = 12 + buf[0] - 'A';
14356 else if (buf[0] == '!')
14357 event = 16;
14358 else
14359 event = atoi(buf);
14360 if (event == 16) {
14361
14362 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
14363 ast_queue_frame(p->owner, &f);
14364 if (sipdebug)
14365 ast_verbose("* DTMF-relay event received: FLASH\n");
14366 } else {
14367
14368 struct ast_frame f = { AST_FRAME_DTMF, };
14369 if (event < 10) {
14370 f.subclass = '0' + event;
14371 } else if (event < 11) {
14372 f.subclass = '*';
14373 } else if (event < 12) {
14374 f.subclass = '#';
14375 } else if (event < 16) {
14376 f.subclass = 'A' + (event - 12);
14377 }
14378 f.len = duration;
14379 ast_queue_frame(p->owner, &f);
14380 if (sipdebug)
14381 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
14382 }
14383 transmit_response(p, "200 OK", req);
14384 return;
14385 } else if (!strcasecmp(c, "application/dtmf")) {
14386
14387 unsigned int duration = 0;
14388
14389 if (!p->owner) {
14390 transmit_response(p, "481 Call leg/transaction does not exist", req);
14391 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14392 return;
14393 }
14394
14395 get_msg_text(buf, sizeof(buf), req);
14396 duration = 100;
14397
14398 if (ast_strlen_zero(buf)) {
14399 transmit_response(p, "200 OK", req);
14400 return;
14401 }
14402 event = atoi(buf);
14403 if (event == 16) {
14404
14405 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
14406 ast_queue_frame(p->owner, &f);
14407 if (sipdebug)
14408 ast_verbose("* DTMF-relay event received: FLASH\n");
14409 } else {
14410
14411 struct ast_frame f = { AST_FRAME_DTMF, };
14412 if (event < 10) {
14413 f.subclass = '0' + event;
14414 } else if (event < 11) {
14415 f.subclass = '*';
14416 } else if (event < 12) {
14417 f.subclass = '#';
14418 } else if (event < 16) {
14419 f.subclass = 'A' + (event - 12);
14420 }
14421 f.len = duration;
14422 ast_queue_frame(p->owner, &f);
14423 if (sipdebug)
14424 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
14425 }
14426 transmit_response(p, "200 OK", req);
14427 return;
14428
14429 } else if (!strcasecmp(c, "application/media_control+xml")) {
14430
14431 if (p->owner)
14432 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
14433 transmit_response(p, "200 OK", req);
14434 return;
14435 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
14436
14437 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
14438 if (p->owner && p->owner->cdr)
14439 ast_cdr_setuserfield(p->owner, c);
14440 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
14441 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
14442 transmit_response(p, "200 OK", req);
14443 } else {
14444 transmit_response(p, "403 Forbidden", req);
14445 }
14446 return;
14447 } else if (!ast_strlen_zero(c = get_header(req, "Record"))) {
14448
14449
14450
14451
14452
14453
14454
14455 struct ast_call_feature *feat;
14456 int j;
14457 struct ast_frame f = { AST_FRAME_DTMF, };
14458
14459 ast_rdlock_call_features();
14460 feat = ast_find_call_feature("automon");
14461 if (!feat || ast_strlen_zero(feat->exten)) {
14462 ast_log(LOG_WARNING, "Recording requested, but no One Touch Monitor registered. (See features.conf)\n");
14463
14464 transmit_response(p, "403 Forbidden", req);
14465 ast_unlock_call_features();
14466 return;
14467 }
14468
14469 f.len = 100;
14470 for (j=0; j < strlen(feat->exten); j++) {
14471 f.subclass = feat->exten[j];
14472 ast_queue_frame(p->owner, &f);
14473 if (sipdebug)
14474 ast_verbose("* DTMF-relay event faked: %c\n", f.subclass);
14475 }
14476 ast_unlock_call_features();
14477
14478 ast_debug(1, "Got a Request to Record the channel, state %s\n", c);
14479 transmit_response(p, "200 OK", req);
14480 return;
14481 } else if (ast_strlen_zero(c = get_header(req, "Content-Length")) || !strcasecmp(c, "0")) {
14482
14483 transmit_response(p, "200 OK", req);
14484 return;
14485 }
14486
14487
14488
14489
14490 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
14491 transmit_response(p, "415 Unsupported media type", req);
14492 return;
14493 }
14494
14495
14496 static char *sip_do_debug_ip(int fd, char *arg)
14497 {
14498 struct hostent *hp;
14499 struct ast_hostent ahp;
14500 int port = 0;
14501 char *p;
14502
14503 p = arg;
14504 strsep(&p, ":");
14505 if (p)
14506 port = atoi(p);
14507 hp = ast_gethostbyname(arg, &ahp);
14508 if (hp == NULL)
14509 return CLI_SHOWUSAGE;
14510
14511 debugaddr.sin_family = AF_INET;
14512 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
14513 debugaddr.sin_port = htons(port);
14514 if (port == 0)
14515 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(debugaddr.sin_addr));
14516 else
14517 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), port);
14518
14519 sipdebug |= sip_debug_console;
14520
14521 return CLI_SUCCESS;
14522 }
14523
14524
14525 static char *sip_do_debug_peer(int fd, char *arg)
14526 {
14527 struct sip_peer *peer = find_peer(arg, NULL, 1, 0);
14528 if (!peer)
14529 ast_cli(fd, "No such peer '%s'\n", arg);
14530 else if (peer->addr.sin_addr.s_addr == 0)
14531 ast_cli(fd, "Unable to get IP address of peer '%s'\n", arg);
14532 else {
14533 debugaddr.sin_family = AF_INET;
14534 debugaddr.sin_addr = peer->addr.sin_addr;
14535 debugaddr.sin_port = peer->addr.sin_port;
14536 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n",
14537 ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
14538 sipdebug |= sip_debug_console;
14539 }
14540 if (peer)
14541 unref_peer(peer);
14542 return CLI_SUCCESS;
14543 }
14544
14545
14546 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14547 {
14548 int oldsipdebug = sipdebug & sip_debug_console;
14549 char *what;
14550
14551 if (cmd == CLI_INIT) {
14552 e->command = "sip set debug {on|off|ip|peer}";
14553 e->usage =
14554 "Usage: sip set debug {off|on|ip addr[:port]|peer peername}\n"
14555 " Globally disables dumping of SIP packets,\n"
14556 " or enables it either globally or for a (single)\n"
14557 " IP address or registered peer.\n";
14558 return NULL;
14559 } else if (cmd == CLI_GENERATE) {
14560 if (a->pos == 4 && strcasestr(a->line, " peer"))
14561 return complete_sip_peer(a->word, a->n, 0);
14562 return NULL;
14563 }
14564
14565 what = a->argv[e->args-1];
14566 if (a->argc == e->args) {
14567 if (!strcasecmp(what, "on")) {
14568 sipdebug |= sip_debug_console;
14569 sipdebug_text = 1;
14570 memset(&debugaddr, 0, sizeof(debugaddr));
14571 ast_cli(a->fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
14572 return CLI_SUCCESS;
14573 } else if (!strcasecmp(what, "off")) {
14574 sipdebug &= ~sip_debug_console;
14575 sipdebug_text = 0;
14576 ast_cli(a->fd, "SIP Debugging Disabled\n");
14577 return CLI_SUCCESS;
14578 }
14579 } else if (a->argc == e->args +1) {
14580 if (!strcasecmp(what, "ip"))
14581 return sip_do_debug_ip(a->fd, a->argv[e->args]);
14582 else if (!strcasecmp(what, "peer"))
14583 return sip_do_debug_peer(a->fd, a->argv[e->args]);
14584 }
14585 return CLI_SHOWUSAGE;
14586 }
14587
14588
14589 static char *sip_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14590 {
14591 struct ast_variable *varlist;
14592 int i;
14593
14594 switch (cmd) {
14595 case CLI_INIT:
14596 e->command = "sip notify";
14597 e->usage =
14598 "Usage: sip notify <type> <peer> [<peer>...]\n"
14599 " Send a NOTIFY message to a SIP peer or peers\n"
14600 " Message types are defined in sip_notify.conf\n";
14601 return NULL;
14602 case CLI_GENERATE:
14603 return complete_sipnotify(a->line, a->word, a->pos, a->n);
14604 }
14605
14606
14607 if (a->argc < 4)
14608 return CLI_SHOWUSAGE;
14609
14610 if (!notify_types) {
14611 ast_cli(a->fd, "No %s file found, or no types listed there\n", notify_config);
14612 return CLI_FAILURE;
14613 }
14614
14615 varlist = ast_variable_browse(notify_types, a->argv[2]);
14616
14617 if (!varlist) {
14618 ast_cli(a->fd, "Unable to find notify type '%s'\n", a->argv[2]);
14619 return CLI_FAILURE;
14620 }
14621
14622 for (i = 3; i < a->argc; i++) {
14623 struct sip_pvt *p;
14624 struct sip_request req;
14625 struct ast_variable *var;
14626
14627 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
14628 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
14629 return CLI_FAILURE;
14630 }
14631
14632 if (create_addr(p, a->argv[i], 1)) {
14633
14634 sip_destroy(p);
14635 ast_cli(a->fd, "Could not create address for '%s'\n", a->argv[i]);
14636 continue;
14637 }
14638
14639 initreqprep(&req, p, SIP_NOTIFY);
14640
14641 for (var = varlist; var; var = var->next) {
14642 char buf[512];
14643 ast_copy_string(buf, var->value, sizeof(buf));
14644 add_header(&req, var->name, ast_unescape_semicolon(buf));
14645 }
14646
14647
14648 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
14649 build_via(p);
14650 build_callid_pvt(p);
14651 ast_cli(a->fd, "Sending NOTIFY of type '%s' to '%s'\n", a->argv[2], a->argv[i]);
14652 transmit_sip_request(p, &req);
14653 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14654 dialog_unref(p);
14655 }
14656
14657 return CLI_SUCCESS;
14658 }
14659
14660
14661 static char *sip_do_history_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14662 {
14663 switch (cmd) {
14664 case CLI_INIT:
14665 e->command = "sip history [off]";
14666 e->usage =
14667 "Usage: sip history [off]\n"
14668 " Enables/Disables recording of SIP dialog history for debugging purposes.\n"
14669 " Use 'sip show history' to view the history of a call number.\n";
14670 return NULL;
14671 case CLI_GENERATE:
14672 return NULL;
14673 }
14674
14675 if (a->argc < 2 || a->argc > 3) {
14676 return CLI_SHOWUSAGE;
14677 }
14678 if (a->argc == 2) {
14679 recordhistory = TRUE;
14680 ast_cli(a->fd, "SIP History Recording Enabled (use 'sip show history')\n");
14681 } else {
14682 if (strncasecmp(a->argv[2], "off", 3))
14683 return CLI_SHOWUSAGE;
14684 recordhistory = FALSE;
14685 ast_cli(a->fd, "SIP History Recording Disabled\n");
14686 }
14687 return CLI_SUCCESS;
14688 }
14689
14690
14691 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14692 {
14693 switch (cmd) {
14694 case CLI_INIT:
14695 e->command = "sip set history {on|off}";
14696 e->usage =
14697 "Usage: sip history {on|off}\n"
14698 " Enables/Disables recording of SIP dialog history for debugging purposes.\n"
14699 " Use 'sip show history' to view the history of a call number.\n";
14700 return NULL;
14701 case CLI_GENERATE:
14702 return NULL;
14703 }
14704
14705 if (a->argc != e->args)
14706 return CLI_SHOWUSAGE;
14707
14708 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
14709 recordhistory = TRUE;
14710 ast_cli(a->fd, "SIP History Recording Enabled (use 'sip show history')\n");
14711 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
14712 recordhistory = FALSE;
14713 ast_cli(a->fd, "SIP History Recording Disabled\n");
14714 } else {
14715 return CLI_SHOWUSAGE;
14716 }
14717 return CLI_SUCCESS;
14718 }
14719
14720
14721 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code)
14722 {
14723 char *header, *respheader;
14724 char digest[1024];
14725
14726 p->authtries++;
14727 auth_headers(code, &header, &respheader);
14728 memset(digest, 0, sizeof(digest));
14729 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
14730
14731
14732 if (sip_debug_test_pvt(p) && p->registry)
14733 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
14734
14735 return -1;
14736 }
14737 if (p->do_history)
14738 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
14739 if (sip_debug_test_pvt(p) && p->registry)
14740 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
14741 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
14742 }
14743
14744
14745 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code, int sipmethod, int init)
14746 {
14747 char *header, *respheader;
14748 char digest[1024];
14749
14750 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
14751 return -2;
14752
14753 p->authtries++;
14754 auth_headers(code, &header, &respheader);
14755 ast_debug(2, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
14756 memset(digest, 0, sizeof(digest));
14757 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
14758
14759 return -1;
14760 }
14761
14762 p->options->auth = digest;
14763 p->options->authheader = respheader;
14764 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
14765 }
14766
14767
14768
14769
14770
14771 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
14772 {
14773 char tmp[512];
14774 char *c;
14775 char oldnonce[256];
14776
14777
14778 const struct x {
14779 const char *key;
14780 const ast_string_field *field;
14781 } *i, keys[] = {
14782 { "realm=", &p->realm },
14783 { "nonce=", &p->nonce },
14784 { "opaque=", &p->opaque },
14785 { "qop=", &p->qop },
14786 { "domain=", &p->domain },
14787 { NULL, 0 },
14788 };
14789
14790 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
14791 if (ast_strlen_zero(tmp))
14792 return -1;
14793 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
14794 ast_log(LOG_WARNING, "missing Digest.\n");
14795 return -1;
14796 }
14797 c = tmp + strlen("Digest ");
14798 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
14799 while (c && *(c = ast_skip_blanks(c))) {
14800 for (i = keys; i->key != NULL; i++) {
14801 char *src, *separator;
14802 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
14803 continue;
14804
14805 c += strlen(i->key);
14806 if (*c == '"') {
14807 src = ++c;
14808 separator = "\"";
14809 } else {
14810 src = c;
14811 separator = ",";
14812 }
14813 strsep(&c, separator);
14814 ast_string_field_ptr_set(p, i->field, src);
14815 break;
14816 }
14817 if (i->key == NULL)
14818 strsep(&c, ",");
14819 }
14820
14821 if (strcmp(p->nonce, oldnonce))
14822 p->noncecount = 0;
14823
14824
14825 if (p->registry) {
14826 struct sip_registry *r = p->registry;
14827
14828 if (strcmp(r->nonce, p->nonce)) {
14829 ast_string_field_set(r, realm, p->realm);
14830 ast_string_field_set(r, nonce, p->nonce);
14831 ast_string_field_set(r, domain, p->domain);
14832 ast_string_field_set(r, opaque, p->opaque);
14833 ast_string_field_set(r, qop, p->qop);
14834 r->noncecount = 0;
14835 }
14836 }
14837 return build_reply_digest(p, sipmethod, digest, digest_len);
14838 }
14839
14840
14841
14842
14843
14844
14845 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
14846 {
14847 char a1[256];
14848 char a2[256];
14849 char a1_hash[256];
14850 char a2_hash[256];
14851 char resp[256];
14852 char resp_hash[256];
14853 char uri[256];
14854 char opaque[256] = "";
14855 char cnonce[80];
14856 const char *username;
14857 const char *secret;
14858 const char *md5secret;
14859 struct sip_auth *auth = NULL;
14860
14861 if (!ast_strlen_zero(p->domain))
14862 ast_copy_string(uri, p->domain, sizeof(uri));
14863 else if (!ast_strlen_zero(p->uri))
14864 ast_copy_string(uri, p->uri, sizeof(uri));
14865 else
14866 snprintf(uri, sizeof(uri), "sip:%s@%s", p->username, ast_inet_ntoa(p->sa.sin_addr));
14867
14868 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
14869
14870
14871 if(!(auth = find_realm_authentication(p->peerauth, p->realm)))
14872 auth = find_realm_authentication(authl, p->realm);
14873
14874 if (auth) {
14875 ast_log(LOG_DEBUG, "use realm [%s] from peer [%s][%s]\n", auth->username, p->peername, p->username);
14876 username = auth->username;
14877 secret = auth->secret;
14878 md5secret = auth->md5secret;
14879 if (sipdebug)
14880 ast_debug(1, "Using realm %s authentication for call %s\n", p->realm, p->callid);
14881 } else {
14882
14883 username = p->authname;
14884 secret = p->peersecret;
14885 md5secret = p->peermd5secret;
14886 }
14887 if (ast_strlen_zero(username))
14888 return -1;
14889
14890
14891 snprintf(a1, sizeof(a1), "%s:%s:%s", username, p->realm, secret);
14892 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[method].text, uri);
14893 if (!ast_strlen_zero(md5secret))
14894 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
14895 else
14896 ast_md5_hash(a1_hash, a1);
14897 ast_md5_hash(a2_hash, a2);
14898
14899 p->noncecount++;
14900 if (!ast_strlen_zero(p->qop))
14901 snprintf(resp, sizeof(resp), "%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
14902 else
14903 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, p->nonce, a2_hash);
14904 ast_md5_hash(resp_hash, resp);
14905
14906
14907 if (!ast_strlen_zero(p->opaque)) {
14908 snprintf(opaque, sizeof(opaque), ", opaque=\"%s\"", p->opaque);
14909 }
14910
14911
14912 if (!ast_strlen_zero(p->qop))
14913 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);
14914 else
14915 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);
14916
14917 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
14918
14919 return 0;
14920 }
14921
14922
14923 static int func_header_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
14924 {
14925 struct sip_pvt *p;
14926 const char *content = NULL;
14927 AST_DECLARE_APP_ARGS(args,
14928 AST_APP_ARG(header);
14929 AST_APP_ARG(number);
14930 );
14931 int i, number, start = 0;
14932
14933 if (ast_strlen_zero(data)) {
14934 ast_log(LOG_WARNING, "This function requires a header name.\n");
14935 return -1;
14936 }
14937
14938 ast_channel_lock(chan);
14939 if (!IS_SIP_TECH(chan->tech)) {
14940 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
14941 ast_channel_unlock(chan);
14942 return -1;
14943 }
14944
14945 AST_STANDARD_APP_ARGS(args, data);
14946 if (!args.number) {
14947 number = 1;
14948 } else {
14949 sscanf(args.number, "%d", &number);
14950 if (number < 1)
14951 number = 1;
14952 }
14953
14954 p = chan->tech_pvt;
14955
14956
14957 if (!p) {
14958 ast_channel_unlock(chan);
14959 return -1;
14960 }
14961
14962 for (i = 0; i < number; i++)
14963 content = __get_header(&p->initreq, args.header, &start);
14964
14965 if (ast_strlen_zero(content)) {
14966 ast_channel_unlock(chan);
14967 return -1;
14968 }
14969
14970 ast_copy_string(buf, content, len);
14971 ast_channel_unlock(chan);
14972
14973 return 0;
14974 }
14975
14976 static struct ast_custom_function sip_header_function = {
14977 .name = "SIP_HEADER",
14978 .synopsis = "Gets the specified SIP header",
14979 .syntax = "SIP_HEADER(<name>[,<number>])",
14980 .desc = "Since there are several headers (such as Via) which can occur multiple\n"
14981 "times, SIP_HEADER takes an optional second argument to specify which header with\n"
14982 "that name to retrieve. Headers start at offset 1.\n",
14983 .read = func_header_read,
14984 };
14985
14986
14987 static int func_check_sipdomain(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
14988 {
14989 if (ast_strlen_zero(data)) {
14990 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
14991 return -1;
14992 }
14993 if (check_sip_domain(data, NULL, 0))
14994 ast_copy_string(buf, data, len);
14995 else
14996 buf[0] = '\0';
14997 return 0;
14998 }
14999
15000 static struct ast_custom_function checksipdomain_function = {
15001 .name = "CHECKSIPDOMAIN",
15002 .synopsis = "Checks if domain is a local domain",
15003 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
15004 .read = func_check_sipdomain,
15005 .desc = "This function checks if the domain in the argument is configured\n"
15006 "as a local SIP domain that this Asterisk server is configured to handle.\n"
15007 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
15008 "Check the domain= configuration in sip.conf\n",
15009 };
15010
15011
15012 static int function_sippeer(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
15013 {
15014 struct sip_peer *peer;
15015 char *colname;
15016
15017 if ((colname = strchr(data, ':'))) {
15018 static int deprecation_warning = 0;
15019 *colname++ = '\0';
15020 if (deprecation_warning++ % 10 == 0)
15021 ast_log(LOG_WARNING, "SIPPEER(): usage of ':' to separate arguments is deprecated. Please use ',' instead.\n");
15022 } else if ((colname = strchr(data, ',')))
15023 *colname++ = '\0';
15024 else
15025 colname = "ip";
15026
15027 if (!(peer = find_peer(data, NULL, 1, 0)))
15028 return -1;
15029
15030 if (!strcasecmp(colname, "ip")) {
15031 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
15032 } else if (!strcasecmp(colname, "port")) {
15033 snprintf(buf, len, "%d", ntohs(peer->addr.sin_port));
15034 } else if (!strcasecmp(colname, "status")) {
15035 peer_status(peer, buf, len);
15036 } else if (!strcasecmp(colname, "language")) {
15037 ast_copy_string(buf, peer->language, len);
15038 } else if (!strcasecmp(colname, "regexten")) {
15039 ast_copy_string(buf, peer->regexten, len);
15040 } else if (!strcasecmp(colname, "limit")) {
15041 snprintf(buf, len, "%d", peer->call_limit);
15042 } else if (!strcasecmp(colname, "busylevel")) {
15043 snprintf(buf, len, "%d", peer->busy_level);
15044 } else if (!strcasecmp(colname, "curcalls")) {
15045 snprintf(buf, len, "%d", peer->inUse);
15046 } else if (!strcasecmp(colname, "accountcode")) {
15047 ast_copy_string(buf, peer->accountcode, len);
15048 } else if (!strcasecmp(colname, "callgroup")) {
15049 ast_print_group(buf, len, peer->callgroup);
15050 } else if (!strcasecmp(colname, "pickupgroup")) {
15051 ast_print_group(buf, len, peer->pickupgroup);
15052 } else if (!strcasecmp(colname, "useragent")) {
15053 ast_copy_string(buf, peer->useragent, len);
15054 } else if (!strcasecmp(colname, "mailbox")) {
15055 struct ast_str *mailbox_str = ast_str_alloca(512);
15056 peer_mailboxes_to_str(&mailbox_str, peer);
15057 ast_copy_string(buf, mailbox_str->str, len);
15058 } else if (!strcasecmp(colname, "context")) {
15059 ast_copy_string(buf, peer->context, len);
15060 } else if (!strcasecmp(colname, "expire")) {
15061 snprintf(buf, len, "%d", peer->expire);
15062 } else if (!strcasecmp(colname, "dynamic")) {
15063 ast_copy_string(buf, peer->host_dynamic ? "yes" : "no", len);
15064 } else if (!strcasecmp(colname, "callerid_name")) {
15065 ast_copy_string(buf, peer->cid_name, len);
15066 } else if (!strcasecmp(colname, "callerid_num")) {
15067 ast_copy_string(buf, peer->cid_num, len);
15068 } else if (!strcasecmp(colname, "codecs")) {
15069 ast_getformatname_multiple(buf, len -1, peer->capability);
15070 } else if (!strncasecmp(colname, "chanvar[", 8)) {
15071 char *chanvar=colname + 8;
15072 struct ast_variable *v;
15073
15074 chanvar = strsep(&chanvar, "]");
15075 for (v = peer->chanvars ; v ; v = v->next)
15076 if (!strcasecmp(v->name, chanvar))
15077 ast_copy_string(buf, v->value, len);
15078 } else if (!strncasecmp(colname, "codec[", 6)) {
15079 char *codecnum;
15080 int index = 0, codec = 0;
15081
15082 codecnum = colname + 6;
15083 codecnum = strsep(&codecnum, "]");
15084 index = atoi(codecnum);
15085 if((codec = ast_codec_pref_index(&peer->prefs, index))) {
15086 ast_copy_string(buf, ast_getformatname(codec), len);
15087 }
15088 }
15089
15090 unref_peer(peer);
15091
15092 return 0;
15093 }
15094
15095
15096 struct ast_custom_function sippeer_function = {
15097 .name = "SIPPEER",
15098 .synopsis = "Gets SIP peer information",
15099 .syntax = "SIPPEER(<peername>[,item])",
15100 .read = function_sippeer,
15101 .desc = "Valid items are:\n"
15102 "- ip (default) The IP address.\n"
15103 "- port The port number\n"
15104 "- mailbox The configured mailbox.\n"
15105 "- context The configured context.\n"
15106 "- expire The epoch time of the next expire.\n"
15107 "- dynamic Is it dynamic? (yes/no).\n"
15108 "- callerid_name The configured Caller ID name.\n"
15109 "- callerid_num The configured Caller ID number.\n"
15110 "- callgroup The configured Callgroup.\n"
15111 "- pickupgroup The configured Pickupgroup.\n"
15112 "- codecs The configured codecs.\n"
15113 "- status Status (if qualify=yes).\n"
15114 "- regexten Registration extension\n"
15115 "- limit Call limit (call-limit)\n"
15116 "- busylevel Configured call level for signalling busy\n"
15117 "- curcalls Current amount of calls \n"
15118 " Only available if call-limit is set\n"
15119 "- language Default language for peer\n"
15120 "- accountcode Account code for this peer\n"
15121 "- useragent Current user agent id for peer\n"
15122 "- chanvar[name] A channel variable configured with setvar for this peer.\n"
15123 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
15124 "\n"
15125 };
15126
15127
15128 static int function_sipchaninfo_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
15129 {
15130 struct sip_pvt *p;
15131
15132 *buf = 0;
15133
15134 if (!data) {
15135 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
15136 return -1;
15137 }
15138
15139 ast_channel_lock(chan);
15140 if (!IS_SIP_TECH(chan->tech)) {
15141 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
15142 ast_channel_unlock(chan);
15143 return -1;
15144 }
15145
15146 p = chan->tech_pvt;
15147
15148
15149 if (!p) {
15150 ast_channel_unlock(chan);
15151 return -1;
15152 }
15153
15154 if (!strcasecmp(data, "peerip")) {
15155 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", len);
15156 } else if (!strcasecmp(data, "recvip")) {
15157 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", len);
15158 } else if (!strcasecmp(data, "from")) {
15159 ast_copy_string(buf, p->from, len);
15160 } else if (!strcasecmp(data, "uri")) {
15161 ast_copy_string(buf, p->uri, len);
15162 } else if (!strcasecmp(data, "useragent")) {
15163 ast_copy_string(buf, p->useragent, len);
15164 } else if (!strcasecmp(data, "peername")) {
15165 ast_copy_string(buf, p->peername, len);
15166 } else if (!strcasecmp(data, "t38passthrough")) {
15167 if (p->t38.state == T38_DISABLED)
15168 ast_copy_string(buf, "0", sizeof("0"));
15169 else
15170 ast_copy_string(buf, "1", sizeof("1"));
15171 } else {
15172 ast_channel_unlock(chan);
15173 return -1;
15174 }
15175 ast_channel_unlock(chan);
15176
15177 return 0;
15178 }
15179
15180
15181 static struct ast_custom_function sipchaninfo_function = {
15182 .name = "SIPCHANINFO",
15183 .synopsis = "Gets the specified SIP parameter from the current channel",
15184 .syntax = "SIPCHANINFO(item)",
15185 .read = function_sipchaninfo_read,
15186 .desc = "Valid items are:\n"
15187 "- peerip The IP address of the peer.\n"
15188 "- recvip The source IP address of the peer.\n"
15189 "- from The URI from the From: header.\n"
15190 "- uri The URI from the Contact: header.\n"
15191 "- useragent The useragent.\n"
15192 "- peername The name of the peer.\n"
15193 "- t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
15194 };
15195
15196
15197 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
15198 {
15199 char tmp[SIPBUFSIZE];
15200 char *s, *e, *t, *trans;
15201 char *domain;
15202 enum sip_transport transport = SIP_TRANSPORT_UDP;
15203
15204 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
15205 if ((t = strchr(tmp, ',')))
15206 *t = '\0';
15207
15208 s = get_in_brackets(tmp);
15209 if ((trans = strcasestr(s, ";transport="))) do {
15210 trans += 11;
15211
15212 if ((e = strchr(trans, ';')))
15213 *e = '\0';
15214
15215 if (!strncasecmp(trans, "tcp", 3))
15216 transport = SIP_TRANSPORT_TCP;
15217 else if (!strncasecmp(trans, "tls", 3))
15218 transport = SIP_TRANSPORT_TLS;
15219 else {
15220 if (strncasecmp(trans, "udp", 3))
15221 ast_debug(1, "received contact with an invalid transport, '%s'\n", s);
15222 transport = SIP_TRANSPORT_UDP;
15223 }
15224 } while(0);
15225 s = remove_uri_parameters(s);
15226
15227 if (p->socket.tcptls_session) {
15228 ao2_ref(p->socket.tcptls_session, -1);
15229 p->socket.tcptls_session = NULL;
15230 }
15231
15232 p->socket.fd = -1;
15233 p->socket.type = transport;
15234
15235 if (ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
15236 char *host = NULL;
15237 if (!strncasecmp(s, "sip:", 4))
15238 s += 4;
15239 else if (!strncasecmp(s, "sips:", 5))
15240 s += 5;
15241 e = strchr(s, '/');
15242 if (e)
15243 *e = '\0';
15244 if ((host = strchr(s, '@'))) {
15245 *host++ = '\0';
15246 ast_debug(2, "Found promiscuous redirection to 'SIP/%s::::%s@%s'\n", s, get_transport(transport), host);
15247 if (p->owner)
15248 ast_string_field_build(p->owner, call_forward, "SIP/%s::::%s@%s", s, get_transport(transport), host);
15249 } else {
15250 ast_debug(2, "Found promiscuous redirection to 'SIP/::::%s@%s'\n", get_transport(transport), s);
15251 if (p->owner)
15252 ast_string_field_build(p->owner, call_forward, "SIP/::::%s@%s", get_transport(transport), s);
15253 }
15254 } else {
15255 e = strchr(tmp, '@');
15256 if (e) {
15257 *e++ = '\0';
15258 domain = e;
15259 } else {
15260
15261 domain = tmp;
15262 }
15263 e = strchr(tmp, '/');
15264 if (e)
15265 *e = '\0';
15266
15267 if (!strncasecmp(s, "sip:", 4))
15268 s += 4;
15269 else if (!strncasecmp(s, "sips:", 5))
15270 s += 5;
15271 e = strchr(s, ';');
15272 if (e)
15273 *e = '\0';
15274 ast_debug(2, "Received 302 Redirect to extension '%s' (domain %s)\n", s, domain);
15275 if (p->owner) {
15276 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
15277 ast_string_field_set(p->owner, call_forward, s);
15278 }
15279 }
15280 }
15281
15282
15283 static void check_pendings(struct sip_pvt *p)
15284 {
15285 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
15286
15287 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA)
15288 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
15289
15290
15291 else {
15292
15293
15294 if (p->pendinginvite)
15295 return;
15296
15297
15298 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
15299 }
15300 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
15301 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15302 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
15303
15304 if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
15305 ast_debug(2, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
15306 } else {
15307 ast_debug(2, "Sending pending reinvite on '%s'\n", p->callid);
15308
15309 transmit_reinvite_with_sdp(p, (p->t38.state == T38_LOCAL_REINVITE ? TRUE : FALSE), FALSE);
15310 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
15311 }
15312 }
15313 }
15314
15315
15316
15317
15318
15319 static int sip_reinvite_retry(const void *data)
15320 {
15321 struct sip_pvt *p = (struct sip_pvt *) data;
15322 sip_pvt_lock(p);
15323 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
15324 p->waitid = -1;
15325 check_pendings(p);
15326 sip_pvt_unlock(p);
15327 dialog_unref(p);
15328 return 0;
15329 }
15330
15331
15332
15333 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
15334 {
15335 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
15336 int res = 0;
15337 int xmitres = 0;
15338 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
15339 char *p_hdrval;
15340 int rtn;
15341
15342 if (reinvite)
15343 ast_debug(4, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
15344 else
15345 ast_debug(4, "SIP response %d to standard invite\n", resp);
15346
15347 if (p->alreadygone) {
15348 ast_debug(1, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
15349 return;
15350 }
15351
15352
15353
15354 AST_SCHED_DEL(sched, p->initid);
15355
15356
15357
15358
15359 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 182 && resp != 183)
15360 resp = 183;
15361
15362
15363 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
15364 p->invitestate = INV_PROCEEDING;
15365
15366
15367 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
15368 p->invitestate = INV_COMPLETED;
15369
15370
15371 if ((resp == 200 || resp >= 300) && p->pendinginvite && seqno == p->pendinginvite)
15372 p->pendinginvite = 0;
15373
15374 switch (resp) {
15375 case 100:
15376 case 101:
15377 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
15378 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15379 check_pendings(p);
15380 break;
15381
15382 case 180:
15383 case 182:
15384 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
15385 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15386 if (!req->ignore && p->owner) {
15387 ast_queue_control(p->owner, AST_CONTROL_RINGING);
15388 if (p->owner->_state != AST_STATE_UP) {
15389 ast_setstate(p->owner, AST_STATE_RINGING);
15390 }
15391 }
15392 if (find_sdp(req)) {
15393 if (p->invitestate != INV_CANCELLED)
15394 p->invitestate = INV_EARLY_MEDIA;
15395 res = process_sdp(p, req, SDP_T38_NONE);
15396 if (!req->ignore && p->owner) {
15397
15398 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
15399 }
15400 }
15401 check_pendings(p);
15402 break;
15403
15404 case 183:
15405 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
15406 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15407
15408 if (find_sdp(req)) {
15409 if (p->invitestate != INV_CANCELLED)
15410 p->invitestate = INV_EARLY_MEDIA;
15411 res = process_sdp(p, req, SDP_T38_NONE);
15412 if (!req->ignore && p->owner) {
15413
15414 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
15415 }
15416 }
15417 check_pendings(p);
15418 break;
15419
15420 case 200:
15421 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
15422 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15423 p->authtries = 0;
15424 if (find_sdp(req)) {
15425 if ((res = process_sdp(p, req, SDP_T38_ACCEPT)) && !req->ignore)
15426 if (!reinvite)
15427
15428
15429 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
15430 }
15431
15432
15433
15434
15435 if (outgoing) {
15436 update_call_counter(p, DEC_CALL_RINGING);
15437 parse_ok_contact(p, req);
15438
15439 if (!reinvite)
15440 build_route(p, req, 1);
15441
15442 if(set_address_from_contact(p)) {
15443
15444
15445 if (!p->route && !req->ignore)
15446 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
15447 }
15448
15449 }
15450
15451 if (!req->ignore && p->owner) {
15452 if (!reinvite) {
15453 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
15454 if (global_callevents)
15455 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
15456 "Channel: %s\r\nChanneltype: %s\r\nUniqueid: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
15457 p->owner->name, "SIP", p->owner->uniqueid, p->callid, p->fullcontact, p->peername);
15458 } else {
15459 ast_queue_frame(p->owner, &ast_null_frame);
15460 }
15461 } else {
15462
15463
15464
15465 if (!req->ignore)
15466 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
15467 }
15468
15469
15470 if (st_get_mode(p) != SESSION_TIMER_MODE_REFUSE && p->outgoing_call == TRUE && !reinvite) {
15471 p_hdrval = (char*)get_header(req, "Session-Expires");
15472 if (!ast_strlen_zero(p_hdrval)) {
15473
15474 enum st_refresher tmp_st_ref = SESSION_TIMER_REFRESHER_AUTO;
15475 int tmp_st_interval = 0;
15476 rtn = parse_session_expires(p_hdrval, &tmp_st_interval, &tmp_st_ref);
15477 if (rtn != 0) {
15478 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
15479 }
15480 if (tmp_st_ref == SESSION_TIMER_REFRESHER_UAC ||
15481 tmp_st_ref == SESSION_TIMER_REFRESHER_UAS) {
15482 p->stimer->st_ref = tmp_st_ref;
15483 }
15484 if (tmp_st_interval) {
15485 p->stimer->st_interval = tmp_st_interval;
15486 }
15487 p->stimer->st_active = TRUE;
15488 p->stimer->st_active_peer_ua = TRUE;
15489 start_session_timer(p);
15490 } else {
15491
15492 if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
15493 p->stimer->st_ref = SESSION_TIMER_REFRESHER_UAC;
15494 p->stimer->st_active_peer_ua = FALSE;
15495 start_session_timer(p);
15496 }
15497 }
15498 }
15499
15500
15501
15502 p->invitestate = INV_TERMINATED;
15503 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
15504 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
15505 check_pendings(p);
15506 break;
15507
15508 case 407:
15509 case 401:
15510
15511 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15512 if (p->options)
15513 p->options->auth_type = resp;
15514
15515
15516 ast_string_field_set(p, theirtag, NULL);
15517 if (!req->ignore) {
15518 if (p->authtries < MAX_AUTHTRIES)
15519 p->invitestate = INV_CALLING;
15520 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, SIP_INVITE, 1)) {
15521 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
15522 p->needdestroy = 1;
15523 sip_alreadygone(p);
15524 if (p->owner)
15525 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15526 }
15527 }
15528 break;
15529
15530 case 403:
15531
15532 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15533 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
15534 if (!req->ignore && p->owner)
15535 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15536 p->needdestroy = 1;
15537 sip_alreadygone(p);
15538 break;
15539
15540 case 404:
15541 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15542 if (p->owner && !req->ignore)
15543 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15544 sip_alreadygone(p);
15545 break;
15546
15547 case 408:
15548 case 481:
15549
15550 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
15551 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15552 if (p->owner)
15553 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15554 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15555 break;
15556
15557 case 422:
15558 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15559 ast_string_field_set(p, theirtag, NULL);
15560 proc_422_rsp(p, req);
15561 break;
15562
15563 case 487:
15564
15565
15566
15567 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15568 if (p->owner && !req->ignore) {
15569 ast_queue_hangup(p->owner);
15570 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
15571 } else if (!req->ignore) {
15572 update_call_counter(p, DEC_CALL_LIMIT);
15573 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
15574 p->needdestroy = 1;
15575 sip_alreadygone(p);
15576 }
15577 break;
15578 case 488:
15579 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15580 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
15581 change_t38_state(p, T38_DISABLED);
15582
15583 ast_rtp_set_rtptimers_onhold(p->rtp);
15584
15585
15586 transmit_reinvite_with_sdp(p, FALSE, FALSE);
15587 } else {
15588
15589 if (p->owner && !req->ignore)
15590 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15591 p->needdestroy = 1;
15592
15593 if (!reinvite)
15594 sip_alreadygone(p);
15595 }
15596 break;
15597 case 491:
15598 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15599 if (p->owner && !req->ignore) {
15600 if (p->owner->_state != AST_STATE_UP) {
15601 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15602 p->needdestroy = 1;
15603 } else {
15604
15605
15606
15607 int wait;
15608
15609
15610 if (p->outgoing_call) {
15611 wait = 2100 + ast_random() % 2000;
15612 } else {
15613 wait = ast_random() % 2000;
15614 }
15615 p->waitid = ast_sched_add(sched, wait, sip_reinvite_retry, dialog_ref(p));
15616 ast_debug(2, "Reinvite race. Waiting %d secs before retry\n", wait);
15617 }
15618 }
15619 break;
15620
15621 case 501:
15622 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
15623 if (p->owner)
15624 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15625 break;
15626 }
15627 if (xmitres == XMIT_ERROR)
15628 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
15629 }
15630
15631
15632
15633
15634 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
15635 {
15636
15637 if (!p->refer)
15638 return;
15639
15640 switch (resp) {
15641 case 202:
15642
15643
15644 p->refer->status = REFER_ACCEPTED;
15645
15646 ast_debug(3, "Got 202 accepted on transfer\n");
15647
15648 break;
15649
15650 case 401:
15651 case 407:
15652 if (ast_strlen_zero(p->authname)) {
15653 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s:%d but we have no matching peer or realm auth!\n",
15654 ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
15655 p->needdestroy = 1;
15656 }
15657 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_REFER, 0)) {
15658 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
15659 p->refer->status = REFER_NOAUTH;
15660 p->needdestroy = 1;
15661 }
15662 break;
15663 case 481:
15664
15665
15666
15667
15668 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
15669 if (p->owner)
15670 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
15671 p->needdestroy = 1;
15672 break;
15673
15674 case 500:
15675 case 501:
15676
15677
15678 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
15679 p->needdestroy = 1;
15680 p->refer->status = REFER_FAILED;
15681 break;
15682 case 603:
15683 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
15684 p->refer->status = REFER_FAILED;
15685 p->needdestroy = 1;
15686 break;
15687 }
15688 }
15689
15690
15691 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
15692 {
15693 int expires, expires_ms;
15694 struct sip_registry *r;
15695 r=p->registry;
15696
15697 switch (resp) {
15698 case 401:
15699 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
15700 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
15701 p->needdestroy = 1;
15702 }
15703 break;
15704 case 403:
15705 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
15706 AST_SCHED_DEL(sched, r->timeout);
15707 r->regstate = REG_STATE_NOAUTH;
15708 p->needdestroy = 1;
15709 break;
15710 case 404:
15711 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username, p->registry->hostname);
15712 p->needdestroy = 1;
15713 r->call = NULL;
15714 r->regstate = REG_STATE_REJECTED;
15715 AST_SCHED_DEL(sched, r->timeout);
15716 break;
15717 case 407:
15718 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
15719 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
15720 p->needdestroy = 1;
15721 }
15722 break;
15723 case 408:
15724
15725 if (r) {
15726 r->regattempts = 0;
15727 } else {
15728 ast_log(LOG_WARNING, "Got a 408 response to our REGISTER on call %s after we had destroyed the registry object\n", p->callid);
15729 }
15730 break;
15731 case 423:
15732 r->expiry = atoi(get_header(req, "Min-Expires"));
15733 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);
15734 ast_sched_del(sched, r->timeout);
15735 r->timeout = -1;
15736 if (r->call) {
15737 r->call = NULL;
15738 p->needdestroy = 1;
15739 }
15740 if (r->expiry > max_expiry) {
15741 ast_log(LOG_WARNING, "Required expiration time from %s@%s is too high, giving up\n", p->registry->username, p->registry->hostname);
15742 r->expiry = default_expiry;
15743 r->regstate = REG_STATE_REJECTED;
15744 } else {
15745 r->regstate = REG_STATE_UNREGISTERED;
15746 transmit_register(r, SIP_REGISTER, NULL, NULL);
15747 }
15748 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));
15749 break;
15750 case 479:
15751 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username, p->registry->hostname);
15752 p->needdestroy = 1;
15753 r->call = NULL;
15754 r->regstate = REG_STATE_REJECTED;
15755 AST_SCHED_DEL(sched, r->timeout);
15756 break;
15757 case 200:
15758 if (!r) {
15759 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));
15760 p->needdestroy = 1;
15761 return 0;
15762 }
15763
15764 r->regstate = REG_STATE_REGISTERED;
15765 r->regtime = ast_tvnow();
15766 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
15767 r->regattempts = 0;
15768 ast_debug(1, "Registration successful\n");
15769 if (r->timeout > -1) {
15770 ast_debug(1, "Cancelling timeout %d\n", r->timeout);
15771 }
15772 AST_SCHED_DEL(sched, r->timeout);
15773 r->call = NULL;
15774 p->registry = NULL;
15775
15776 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15777
15778
15779
15780
15781 AST_SCHED_DEL(sched, r->expire);
15782
15783
15784 expires = 0;
15785
15786
15787 if (!ast_strlen_zero(get_header(req, "Contact"))) {
15788 const char *contact = NULL;
15789 const char *tmptmp = NULL;
15790 int start = 0;
15791 for(;;) {
15792 contact = __get_header(req, "Contact", &start);
15793
15794 if(!ast_strlen_zero(contact)) {
15795 if( (tmptmp=strstr(contact, p->our_contact))) {
15796 contact=tmptmp;
15797 break;
15798 }
15799 } else
15800 break;
15801 }
15802 tmptmp = strcasestr(contact, "expires=");
15803 if (tmptmp) {
15804 if (sscanf(tmptmp + 8, "%d;", &expires) != 1)
15805 expires = 0;
15806 }
15807
15808 }
15809 if (!expires)
15810 expires=atoi(get_header(req, "expires"));
15811 if (!expires)
15812 expires=default_expiry;
15813
15814 expires_ms = expires * 1000;
15815 if (expires <= EXPIRY_GUARD_LIMIT)
15816 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT), EXPIRY_GUARD_MIN);
15817 else
15818 expires_ms -= EXPIRY_GUARD_SECS * 1000;
15819 if (sipdebug)
15820 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
15821
15822 r->refresh= (int) expires_ms / 1000;
15823
15824
15825 AST_SCHED_REPLACE(r->expire, sched, expires_ms, sip_reregister, r);
15826 registry_unref(r);
15827 }
15828 return 1;
15829 }
15830
15831
15832 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
15833 {
15834 struct sip_peer *peer = p->relatedpeer;
15835 int statechanged, is_reachable, was_reachable;
15836 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
15837
15838
15839
15840
15841
15842
15843 if (pingtime < 1)
15844 pingtime = 1;
15845
15846
15847
15848
15849
15850 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
15851 is_reachable = pingtime <= peer->maxms;
15852 statechanged = peer->lastms == 0
15853 || was_reachable != is_reachable;
15854
15855 peer->lastms = pingtime;
15856 peer->call = dialog_unref(peer->call);
15857 if (statechanged) {
15858 const char *s = is_reachable ? "Reachable" : "Lagged";
15859 char str_lastms[20];
15860 snprintf(str_lastms, sizeof(str_lastms), "%d", pingtime);
15861
15862 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
15863 peer->name, s, pingtime, peer->maxms);
15864 ast_device_state_changed("SIP/%s", peer->name);
15865 if (sip_cfg.peer_rtupdate) {
15866 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", str_lastms, NULL);
15867 }
15868 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
15869 "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
15870 peer->name, s, pingtime);
15871 if (is_reachable && global_regextenonqualify)
15872 register_peer_exten(peer, TRUE);
15873 }
15874
15875 p->needdestroy = 1;
15876
15877
15878 AST_SCHED_REPLACE(peer->pokeexpire, sched,
15879 is_reachable ? peer->qualifyfreq : DEFAULT_FREQ_NOTOK,
15880 sip_poke_peer_s, peer);
15881 }
15882
15883
15884 static void stop_media_flows(struct sip_pvt *p)
15885 {
15886
15887 if (p->rtp)
15888 ast_rtp_stop(p->rtp);
15889 if (p->vrtp)
15890 ast_rtp_stop(p->vrtp);
15891 if (p->trtp)
15892 ast_rtp_stop(p->trtp);
15893 if (p->udptl)
15894 ast_udptl_stop(p->udptl);
15895 }
15896
15897
15898
15899 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
15900 {
15901 struct ast_channel *owner;
15902 int sipmethod;
15903 int res = 1;
15904 const char *c = get_header(req, "Cseq");
15905
15906 char *c_copy = ast_strdupa(c);
15907
15908 const char *msg = ast_skip_blanks(ast_skip_nonblanks(c_copy));
15909
15910 if (!msg)
15911 msg = "";
15912
15913 sipmethod = find_sip_method(msg);
15914
15915 owner = p->owner;
15916 if (owner)
15917 owner->hangupcause = hangup_sip2cause(resp);
15918
15919
15920 if ((resp >= 100) && (resp <= 199))
15921 __sip_semi_ack(p, seqno, 0, sipmethod);
15922 else
15923 __sip_ack(p, seqno, 0, sipmethod);
15924
15925
15926 if (!p->owner && sipmethod == SIP_NOTIFY && p->pendinginvite)
15927 p->pendinginvite = 0;
15928
15929
15930 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
15931 char tag[128];
15932
15933 gettag(req, "To", tag, sizeof(tag));
15934 ast_string_field_set(p, theirtag, tag);
15935 }
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951 if ((resp == 404 || resp == 408 || resp == 481) && sipmethod == SIP_BYE) {
15952 p->needdestroy = 1;
15953 return;
15954 }
15955
15956 if (p->relatedpeer && p->method == SIP_OPTIONS) {
15957
15958
15959
15960 if (resp != 100)
15961 handle_response_peerpoke(p, resp, req);
15962 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
15963 switch(resp) {
15964 case 100:
15965 case 101:
15966 if (sipmethod == SIP_INVITE)
15967 handle_response_invite(p, resp, rest, req, seqno);
15968 break;
15969 case 183:
15970 if (sipmethod == SIP_INVITE)
15971 handle_response_invite(p, resp, rest, req, seqno);
15972 break;
15973 case 180:
15974 if (sipmethod == SIP_INVITE)
15975 handle_response_invite(p, resp, rest, req, seqno);
15976 break;
15977 case 182:
15978 if (sipmethod == SIP_INVITE)
15979 handle_response_invite(p, resp, rest, req, seqno);
15980 break;
15981 case 200:
15982 p->authtries = 0;
15983 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
15984
15985
15986
15987 } else if (sipmethod == SIP_INVITE) {
15988 handle_response_invite(p, resp, rest, req, seqno);
15989 } else if (sipmethod == SIP_NOTIFY) {
15990
15991 if (p->owner) {
15992 if (!p->refer) {
15993 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
15994 ast_queue_hangup(p->owner);
15995 } else
15996 ast_debug(4, "Got OK on REFER Notify message\n");
15997 } else {
15998 if (p->subscribed == NONE)
15999 p->needdestroy = 1;
16000 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
16001
16002 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
16003 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
16004 }
16005 }
16006 } else if (sipmethod == SIP_REGISTER)
16007 res = handle_response_register(p, resp, rest, req, seqno);
16008 else if (sipmethod == SIP_BYE) {
16009 p->needdestroy = 1;
16010 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16011 } else if (sipmethod == SIP_SUBSCRIBE) {
16012 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16013 }
16014 break;
16015 case 202:
16016 if (sipmethod == SIP_REFER)
16017 handle_response_refer(p, resp, rest, req, seqno);
16018 break;
16019 case 401:
16020 case 407:
16021 if (sipmethod == SIP_INVITE)
16022 handle_response_invite(p, resp, rest, req, seqno);
16023 else if (sipmethod == SIP_REFER)
16024 handle_response_refer(p, resp, rest, req, seqno);
16025 else if (p->registry && sipmethod == SIP_REGISTER)
16026 res = handle_response_register(p, resp, rest, req, seqno);
16027 else if (sipmethod == SIP_BYE) {
16028 if (p->options)
16029 p->options->auth_type = resp;
16030 if (ast_strlen_zero(p->authname)) {
16031 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
16032 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
16033 p->needdestroy = 1;
16034 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, sipmethod, 0)) {
16035 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
16036 p->needdestroy = 1;
16037 }
16038 } else {
16039 ast_log(LOG_WARNING, "Got authentication request (%d) on %s to '%s'\n", resp, sip_methods[sipmethod].text, get_header(req, "To"));
16040 p->needdestroy = 1;
16041 }
16042 break;
16043 case 403:
16044 if (sipmethod == SIP_INVITE)
16045 handle_response_invite(p, resp, rest, req, seqno);
16046 else if (p->registry && sipmethod == SIP_REGISTER)
16047 res = handle_response_register(p, resp, rest, req, seqno);
16048 else {
16049 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
16050 p->needdestroy = 1;
16051 }
16052 break;
16053 case 404:
16054 if (p->registry && sipmethod == SIP_REGISTER)
16055 res = handle_response_register(p, resp, rest, req, seqno);
16056 else if (sipmethod == SIP_INVITE)
16057 handle_response_invite(p, resp, rest, req, seqno);
16058 else if (owner)
16059 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
16060 break;
16061 case 423:
16062 if (sipmethod == SIP_REGISTER)
16063 res = handle_response_register(p, resp, rest, req, seqno);
16064 break;
16065 case 408:
16066 if (sipmethod == SIP_INVITE)
16067 handle_response_invite(p, resp, rest, req, seqno);
16068 else if (sipmethod == SIP_REGISTER)
16069 res = handle_response_register(p, resp, rest, req, seqno);
16070 else if (sipmethod == SIP_BYE) {
16071 p->needdestroy = 1;
16072 ast_debug(4, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
16073 } else {
16074 if (owner)
16075 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
16076 p->needdestroy = 1;
16077 }
16078 break;
16079
16080 case 422:
16081 if (sipmethod == SIP_INVITE) {
16082 handle_response_invite(p, resp, rest, req, seqno);
16083 }
16084 break;
16085
16086 case 481:
16087 if (sipmethod == SIP_INVITE) {
16088 handle_response_invite(p, resp, rest, req, seqno);
16089 } else if (sipmethod == SIP_REFER) {
16090 handle_response_refer(p, resp, rest, req, seqno);
16091 } else if (sipmethod == SIP_BYE) {
16092
16093
16094 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
16095 } else if (sipmethod == SIP_CANCEL) {
16096
16097
16098 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
16099 } else {
16100 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
16101
16102 }
16103 break;
16104 case 487:
16105 if (sipmethod == SIP_INVITE)
16106 handle_response_invite(p, resp, rest, req, seqno);
16107 break;
16108 case 488:
16109 if (sipmethod == SIP_INVITE)
16110 handle_response_invite(p, resp, rest, req, seqno);
16111 break;
16112 case 491:
16113 if (sipmethod == SIP_INVITE)
16114 handle_response_invite(p, resp, rest, req, seqno);
16115 else {
16116 ast_debug(1, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
16117 p->needdestroy = 1;
16118 }
16119 break;
16120 case 501:
16121 if (sipmethod == SIP_INVITE)
16122 handle_response_invite(p, resp, rest, req, seqno);
16123 else if (sipmethod == SIP_REFER)
16124 handle_response_refer(p, resp, rest, req, seqno);
16125 else
16126 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(p->sa.sin_addr), msg);
16127 break;
16128 case 603:
16129 if (sipmethod == SIP_REFER) {
16130 handle_response_refer(p, resp, rest, req, seqno);
16131 break;
16132 }
16133
16134 default:
16135 if ((resp >= 300) && (resp < 700)) {
16136
16137 if ((resp != 487))
16138 ast_verb(3, "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
16139
16140 if (sipmethod == SIP_INVITE)
16141 stop_media_flows(p);
16142
16143
16144 switch(resp) {
16145 case 300:
16146 case 301:
16147 case 302:
16148 case 305:
16149 parse_moved_contact(p, req);
16150
16151 case 486:
16152 case 600:
16153 case 603:
16154 if (p->owner)
16155 ast_queue_control(p->owner, AST_CONTROL_BUSY);
16156 break;
16157 case 482:
16158
16159
16160
16161
16162 ast_debug(1, "Hairpin detected, setting up call forward for what it's worth\n");
16163 if (p->owner)
16164 ast_string_field_build(p->owner, call_forward,
16165 "Local/%s@%s", p->username, p->context);
16166
16167 case 480:
16168 case 404:
16169 case 410:
16170 case 400:
16171 case 500:
16172 if (sipmethod == SIP_REFER) {
16173 handle_response_refer(p, resp, rest, req, seqno);
16174 break;
16175 }
16176
16177 case 502:
16178 case 503:
16179 case 504:
16180 if (owner)
16181 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
16182 break;
16183 default:
16184
16185 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO && sipmethod != SIP_BYE)
16186 ast_queue_hangup(p->owner);
16187 break;
16188 }
16189
16190 if (sipmethod == SIP_INVITE)
16191 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
16192 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
16193 sip_alreadygone(p);
16194 if (!p->owner)
16195 p->needdestroy = 1;
16196 } else if ((resp >= 100) && (resp < 200)) {
16197 if (sipmethod == SIP_INVITE) {
16198 if (!req->ignore && sip_cancel_destroy(p))
16199 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16200 if (find_sdp(req))
16201 process_sdp(p, req, SDP_T38_NONE);
16202 if (p->owner) {
16203
16204 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
16205 }
16206 }
16207 } else
16208 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));
16209 }
16210 } else {
16211
16212
16213 if (req->debug)
16214 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
16215
16216 if (sipmethod == SIP_INVITE && resp == 200) {
16217
16218
16219 char tag[128];
16220
16221 gettag(req, "To", tag, sizeof(tag));
16222 ast_string_field_set(p, theirtag, tag);
16223 }
16224
16225 switch(resp) {
16226 case 200:
16227 if (sipmethod == SIP_INVITE) {
16228 handle_response_invite(p, resp, rest, req, seqno);
16229 } else if (sipmethod == SIP_CANCEL) {
16230 ast_debug(1, "Got 200 OK on CANCEL\n");
16231
16232
16233 } else if (sipmethod == SIP_NOTIFY) {
16234
16235 if (p->owner) {
16236 if (p->refer) {
16237 ast_debug(1, "Got 200 OK on NOTIFY for transfer\n");
16238 } else
16239 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
16240
16241 } else {
16242 if (!p->subscribed && !p->refer)
16243 p->needdestroy = 1;
16244 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
16245
16246 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
16247 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
16248 }
16249 }
16250 } else if (sipmethod == SIP_BYE)
16251 p->needdestroy = 1;
16252 else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO)
16253
16254
16255 ;
16256 else if (sipmethod == SIP_BYE)
16257
16258 p->needdestroy = 1;
16259 break;
16260 case 202:
16261 if (sipmethod == SIP_REFER)
16262 handle_response_refer(p, resp, rest, req, seqno);
16263 break;
16264 case 401:
16265 case 407:
16266 if (sipmethod == SIP_REFER)
16267 handle_response_refer(p, resp, rest, req, seqno);
16268 else if (sipmethod == SIP_INVITE)
16269 handle_response_invite(p, resp, rest, req, seqno);
16270 else if (sipmethod == SIP_BYE) {
16271 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, sipmethod, 0)) {
16272 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
16273 p->needdestroy = 1;
16274 }
16275 }
16276 break;
16277 case 481:
16278 if (sipmethod == SIP_INVITE) {
16279
16280 handle_response_invite(p, resp, rest, req, seqno);
16281 } else if (sipmethod == SIP_BYE) {
16282 p->needdestroy = 1;
16283 } else if (sipdebug) {
16284 ast_debug(1, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
16285 }
16286 break;
16287 case 501:
16288 if (sipmethod == SIP_INVITE)
16289 handle_response_invite(p, resp, rest, req, seqno);
16290 else if (sipmethod == SIP_REFER)
16291 handle_response_refer(p, resp, rest, req, seqno);
16292 break;
16293 case 603:
16294 if (sipmethod == SIP_REFER) {
16295 handle_response_refer(p, resp, rest, req, seqno);
16296 break;
16297 }
16298
16299 default:
16300 if ((resp >= 100) && (resp < 200)) {
16301 if (sipmethod == SIP_INVITE) {
16302 if (!req->ignore && sip_cancel_destroy(p))
16303 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16304 }
16305 }
16306 if ((resp >= 300) && (resp < 700)) {
16307 if ((resp != 487))
16308 ast_verb(3, "Incoming call: Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
16309 switch(resp) {
16310 case 488:
16311 case 603:
16312 case 500:
16313 case 502:
16314 case 503:
16315 case 504:
16316
16317
16318 if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
16319 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16320 break;
16321 }
16322 }
16323 break;
16324 }
16325 }
16326 }
16327
16328
16329
16330
16331
16332
16333
16334 static void *sip_park_thread(void *stuff)
16335 {
16336 struct ast_channel *transferee, *transferer;
16337 struct sip_dual *d;
16338 struct sip_request req;
16339 int ext;
16340 int res;
16341
16342 d = stuff;
16343 transferee = d->chan1;
16344 transferer = d->chan2;
16345 copy_request(&req, &d->req);
16346
16347 if (!transferee || !transferer) {
16348 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
16349 free(d);
16350 return NULL;
16351 }
16352 ast_debug(4, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
16353
16354 ast_channel_lock(transferee);
16355 if (ast_do_masquerade(transferee)) {
16356 ast_log(LOG_WARNING, "Masquerade failed.\n");
16357 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
16358 ast_channel_unlock(transferee);
16359 free(d);
16360 return NULL;
16361 }
16362 ast_channel_unlock(transferee);
16363
16364 res = ast_park_call(transferee, transferer, 0, &ext);
16365
16366
16367 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
16368 if (!res) {
16369 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
16370 } else {
16371
16372 sprintf(buf, "Call parked on extension '%d'", ext);
16373 transmit_message_with_text(transferer->tech_pvt, buf);
16374 }
16375 #endif
16376
16377
16378
16379 transmit_response(transferer->tech_pvt, "202 Accepted", &req);
16380 if (!res) {
16381
16382 append_history(transferer->tech_pvt, "SIPpark", "Parked call on %d", ext);
16383 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
16384 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
16385 ast_hangup(transferer);
16386 ast_debug(1, "SIP Call parked on extension '%d'\n", ext);
16387 } else {
16388 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
16389 append_history(transferer->tech_pvt, "SIPpark", "Parking failed\n");
16390 ast_debug(1, "SIP Call parked failed \n");
16391
16392 }
16393 free(d);
16394 return NULL;
16395 }
16396
16397
16398
16399
16400 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno)
16401 {
16402 struct sip_dual *d;
16403 struct ast_channel *transferee, *transferer;
16404
16405 pthread_t th;
16406
16407 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
16408 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "SIPPeer/%s", chan2->name);
16409 if ((!transferer) || (!transferee)) {
16410 if (transferee) {
16411 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
16412 ast_hangup(transferee);
16413 }
16414 if (transferer) {
16415 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
16416 ast_hangup(transferer);
16417 }
16418 return -1;
16419 }
16420
16421
16422 transferee->readformat = chan1->readformat;
16423 transferee->writeformat = chan1->writeformat;
16424
16425
16426 ast_channel_masquerade(transferee, chan1);
16427
16428
16429 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
16430 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
16431 transferee->priority = chan1->priority;
16432
16433
16434
16435
16436
16437 transferer->readformat = chan2->readformat;
16438 transferer->writeformat = chan2->writeformat;
16439
16440
16441
16442
16443 while (ast_channel_trylock(chan2)) {
16444 struct sip_pvt *pvt = chan2->tech_pvt;
16445 sip_pvt_unlock(pvt);
16446 usleep(1);
16447 sip_pvt_lock(pvt);
16448 }
16449 ast_channel_masquerade(transferer, chan2);
16450 ast_channel_unlock(chan2);
16451
16452
16453 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
16454 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
16455 transferer->priority = chan2->priority;
16456
16457 ast_channel_lock(transferer);
16458 if (ast_do_masquerade(transferer)) {
16459 ast_log(LOG_WARNING, "Masquerade failed :(\n");
16460 ast_channel_unlock(transferer);
16461 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
16462 ast_hangup(transferer);
16463 return -1;
16464 }
16465 ast_channel_unlock(transferer);
16466 if (!transferer || !transferee) {
16467 if (!transferer) {
16468 ast_debug(1, "No transferer channel, giving up parking\n");
16469 }
16470 if (!transferee) {
16471 ast_debug(1, "No transferee channel, giving up parking\n");
16472 }
16473 return -1;
16474 }
16475 if ((d = ast_calloc(1, sizeof(*d)))) {
16476
16477
16478 copy_request(&d->req, req);
16479 d->chan1 = transferee;
16480 d->chan2 = transferer;
16481 d->seqno = seqno;
16482 if (ast_pthread_create_detached_background(&th, NULL, sip_park_thread, d) < 0) {
16483
16484 ast_free(d);
16485
16486 return 0;
16487 }
16488 }
16489 return -1;
16490 }
16491
16492
16493
16494
16495 static void ast_quiet_chan(struct ast_channel *chan)
16496 {
16497 if (chan && chan->_state == AST_STATE_UP) {
16498 if (ast_test_flag(chan, AST_FLAG_MOH))
16499 ast_moh_stop(chan);
16500 else if (chan->generatordata)
16501 ast_deactivate_generator(chan);
16502 }
16503 }
16504
16505
16506
16507 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
16508 {
16509 int res = 0;
16510 struct ast_channel *peera = NULL,
16511 *peerb = NULL,
16512 *peerc = NULL,
16513 *peerd = NULL;
16514
16515
16516
16517
16518 ast_debug(4, "Sip transfer:--------------------\n");
16519 if (transferer->chan1)
16520 ast_debug(4, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
16521 else
16522 ast_debug(4, "-- No transferer first channel - odd??? \n");
16523 if (target->chan1)
16524 ast_debug(4, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
16525 else
16526 ast_debug(4, "-- No target first channel ---\n");
16527 if (transferer->chan2)
16528 ast_debug(4, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
16529 else
16530 ast_debug(4, "-- No bridged call to transferee\n");
16531 if (target->chan2)
16532 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)");
16533 else
16534 ast_debug(4, "-- No target second channel ---\n");
16535 ast_debug(4, "-- END Sip transfer:--------------------\n");
16536 if (transferer->chan2) {
16537 peera = transferer->chan1;
16538 peerb = target->chan1;
16539 peerc = transferer->chan2;
16540 peerd = target->chan2;
16541 ast_debug(3, "SIP transfer: Four channels to handle\n");
16542 } else if (target->chan2) {
16543 peera = target->chan1;
16544 peerb = transferer->chan1;
16545 peerc = target->chan2;
16546 peerd = transferer->chan2;
16547 ast_debug(3, "SIP transfer: Three channels to handle\n");
16548 }
16549
16550 if (peera && peerb && peerc && (peerb != peerc)) {
16551 ast_quiet_chan(peera);
16552 ast_quiet_chan(peerb);
16553 ast_quiet_chan(peerc);
16554 if (peerd)
16555 ast_quiet_chan(peerd);
16556
16557 ast_debug(4, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
16558 if (ast_channel_masquerade(peerb, peerc)) {
16559 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
16560 res = -1;
16561 } else
16562 ast_debug(4, "SIP transfer: Succeeded to masquerade channels.\n");
16563 return res;
16564 } else {
16565 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
16566 if (transferer->chan1)
16567 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
16568 if (target->chan1)
16569 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
16570 return -1;
16571 }
16572 return 0;
16573 }
16574
16575
16576
16577
16578
16579
16580 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
16581 {
16582 const char *thetag;
16583
16584 if (!tagbuf)
16585 return NULL;
16586 tagbuf[0] = '\0';
16587 thetag = get_header(req, header);
16588 thetag = strcasestr(thetag, ";tag=");
16589 if (thetag) {
16590 thetag += 5;
16591 ast_copy_string(tagbuf, thetag, tagbufsize);
16592 return strsep(&tagbuf, ";");
16593 }
16594 return NULL;
16595 }
16596
16597
16598 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
16599 {
16600
16601
16602 int res = 0;
16603 const char *event = get_header(req, "Event");
16604 char *eventid = NULL;
16605 char *sep;
16606
16607 if( (sep = strchr(event, ';')) ) {
16608 *sep++ = '\0';
16609 eventid = sep;
16610 }
16611
16612 if (sipdebug)
16613 ast_debug(2, "Got NOTIFY Event: %s\n", event);
16614
16615 if (strcmp(event, "refer")) {
16616
16617
16618 transmit_response(p, "489 Bad event", req);
16619 res = -1;
16620 } else {
16621
16622
16623
16624
16625
16626 char buf[1024];
16627 char *cmd, *code;
16628 int respcode;
16629 int success = TRUE;
16630
16631
16632
16633
16634
16635
16636
16637
16638 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
16639
16640 transmit_response(p, "400 Bad request", req);
16641 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16642 return -1;
16643 }
16644
16645
16646 if (get_msg_text(buf, sizeof(buf), req)) {
16647 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
16648 transmit_response(p, "400 Bad request", req);
16649 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16650 return -1;
16651 }
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673 ast_debug(3, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
16674 cmd = ast_skip_blanks(buf);
16675 code = cmd;
16676
16677 while(*code && (*code > 32)) {
16678 code++;
16679 }
16680 *code++ = '\0';
16681 code = ast_skip_blanks(code);
16682 sep = code;
16683 sep++;
16684 while(*sep && (*sep > 32)) {
16685 sep++;
16686 }
16687 *sep++ = '\0';
16688 respcode = atoi(code);
16689 switch (respcode) {
16690 case 100:
16691 case 101:
16692
16693 break;
16694 case 183:
16695
16696 break;
16697 case 200:
16698
16699 break;
16700 case 301:
16701 case 302:
16702
16703 success = FALSE;
16704 break;
16705 case 503:
16706
16707 success = FALSE;
16708 break;
16709 case 603:
16710
16711 success = FALSE;
16712 break;
16713 }
16714 if (!success) {
16715 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
16716 }
16717
16718
16719 transmit_response(p, "200 OK", req);
16720 };
16721
16722 if (!p->lastinvite)
16723 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16724
16725 return res;
16726 }
16727
16728
16729
16730
16731 static int handle_request_options(struct sip_pvt *p, struct sip_request *req)
16732 {
16733 int res;
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745 if (p->lastinvite) {
16746
16747 transmit_response_with_allow(p, "200 OK", req, 0);
16748 return 0;
16749 }
16750
16751 res = get_destination(p, req);
16752 build_contact(p);
16753
16754 if (ast_strlen_zero(p->context))
16755 ast_string_field_set(p, context, default_context);
16756
16757 if (ast_shutting_down())
16758 transmit_response_with_allow(p, "503 Unavailable", req, 0);
16759 else if (res < 0)
16760 transmit_response_with_allow(p, "404 Not Found", req, 0);
16761 else
16762 transmit_response_with_allow(p, "200 OK", req, 0);
16763
16764
16765
16766 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16767
16768 return res;
16769 }
16770
16771
16772
16773
16774
16775
16776 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct sockaddr_in *sin)
16777 {
16778 struct ast_frame *f;
16779 int earlyreplace = 0;
16780 int oneleggedreplace = 0;
16781 struct ast_channel *c = p->owner;
16782 struct ast_channel *replacecall = p->refer->refer_call->owner;
16783 struct ast_channel *targetcall;
16784
16785 struct ast_channel *test;
16786
16787
16788 if (replacecall->_state == AST_STATE_RING)
16789 earlyreplace = 1;
16790
16791
16792 if (!(targetcall = ast_bridged_channel(replacecall))) {
16793
16794 if (!earlyreplace) {
16795 ast_debug(2, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
16796 oneleggedreplace = 1;
16797 }
16798 }
16799 if (targetcall && targetcall->_state == AST_STATE_RINGING)
16800 ast_debug(4, "SIP transfer: Target channel is in ringing state\n");
16801
16802 if (targetcall)
16803 ast_debug(4, "SIP transfer: Invite Replace incoming channel should bridge to channel %s while hanging up channel %s\n", targetcall->name, replacecall->name);
16804 else
16805 ast_debug(4, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
16806
16807 if (req->ignore) {
16808 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
16809
16810
16811
16812 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE);
16813
16814 ast_channel_unlock(c);
16815 sip_pvt_unlock(p->refer->refer_call);
16816 return 1;
16817 }
16818 if (!c) {
16819
16820 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
16821 transmit_response_reliable(p, "503 Service Unavailable", req);
16822 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
16823 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16824 sip_pvt_unlock(p->refer->refer_call);
16825 return 1;
16826 }
16827 append_history(p, "Xfer", "INVITE/Replace received");
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839 transmit_response(p, "100 Trying", req);
16840 ast_setstate(c, AST_STATE_RING);
16841
16842
16843
16844
16845
16846 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE);
16847
16848 ast_setstate(c, AST_STATE_UP);
16849
16850
16851 ast_quiet_chan(replacecall);
16852 ast_quiet_chan(targetcall);
16853 ast_debug(4, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
16854
16855 if (!oneleggedreplace)
16856 ast_channel_unlock(c);
16857
16858
16859 sip_pvt_unlock(p->refer->refer_call);
16860
16861
16862 if (! earlyreplace && ! oneleggedreplace )
16863 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
16864
16865
16866 if(ast_channel_masquerade(replacecall, c))
16867 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
16868 else
16869 ast_debug(4, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
16870
16871
16872
16873
16874
16875 ast_channel_unlock(c);
16876
16877 if (earlyreplace || oneleggedreplace ) {
16878
16879 if ((f = ast_read(replacecall))) {
16880 ast_frfree(f);
16881 f = NULL;
16882 ast_debug(4, "Invite/Replace: Could successfully read frame from RING channel!\n");
16883 } else {
16884 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from RING channel \n");
16885 }
16886 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
16887 if (!oneleggedreplace)
16888 ast_channel_unlock(replacecall);
16889 } else {
16890 if ((f = ast_read(replacecall))) {
16891
16892 ast_frfree(f);
16893 f = NULL;
16894 ast_debug(3, "Invite/Replace: Could successfully read frame from channel! Masq done.\n");
16895 } else {
16896 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from channel. Transfer failed\n");
16897 }
16898 ast_channel_unlock(replacecall);
16899 }
16900 sip_pvt_unlock(p->refer->refer_call);
16901
16902 ast_setstate(c, AST_STATE_DOWN);
16903 ast_debug(4, "After transfer:----------------------------\n");
16904 ast_debug(4, " -- C: %s State %s\n", c->name, ast_state2str(c->_state));
16905 if (replacecall)
16906 ast_debug(4, " -- replacecall: %s State %s\n", replacecall->name, ast_state2str(replacecall->_state));
16907 if (p->owner) {
16908 ast_debug(4, " -- P->owner: %s State %s\n", p->owner->name, ast_state2str(p->owner->_state));
16909 test = ast_bridged_channel(p->owner);
16910 if (test)
16911 ast_debug(4, " -- Call bridged to P->owner: %s State %s\n", test->name, ast_state2str(test->_state));
16912 else
16913 ast_debug(4, " -- No call bridged to C->owner \n");
16914 } else
16915 ast_debug(4, " -- No channel yet \n");
16916 ast_debug(4, "End After transfer:----------------------------\n");
16917
16918 ast_channel_unlock(p->owner);
16919 if (!oneleggedreplace)
16920 sip_pvt_unlock(p);
16921
16922
16923 c->tech_pvt = dialog_unref(c->tech_pvt);
16924 ast_hangup(c);
16925 return 0;
16926 }
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944 static int sip_uri_params_cmp(const char *input1, const char *input2)
16945 {
16946 char *params1 = NULL;
16947 char *params2 = NULL;
16948 char *pos1;
16949 char *pos2;
16950 int zerolength1 = 0;
16951 int zerolength2 = 0;
16952 int maddrmatch = 0;
16953 int ttlmatch = 0;
16954 int usermatch = 0;
16955 int methodmatch = 0;
16956
16957 if (ast_strlen_zero(input1)) {
16958 zerolength1 = 1;
16959 } else {
16960 params1 = ast_strdupa(input1);
16961 }
16962 if (ast_strlen_zero(input2)) {
16963 zerolength2 = 1;
16964 } else {
16965 params2 = ast_strdupa(input2);
16966 }
16967
16968
16969
16970
16971 if (zerolength1 && zerolength2) {
16972 return 0;
16973 }
16974
16975 pos1 = params1;
16976 while (!ast_strlen_zero(pos1)) {
16977 char *name1 = pos1;
16978 char *value1 = strchr(pos1, '=');
16979 char *semicolon1 = strchr(pos1, ';');
16980 int matched = 0;
16981 if (semicolon1) {
16982 *semicolon1++ = '\0';
16983 }
16984 if (!value1) {
16985 goto fail;
16986 }
16987 *value1++ = '\0';
16988
16989
16990
16991
16992
16993 pos2 = ast_strdupa(params2);
16994 while (!ast_strlen_zero(pos2)) {
16995 char *name2 = pos2;
16996 char *value2 = strchr(pos2, '=');
16997 char *semicolon2 = strchr(pos2, ';');
16998 if (semicolon2) {
16999 *semicolon2++ = '\0';
17000 }
17001 if (!value2) {
17002 goto fail;
17003 }
17004 *value2++ = '\0';
17005 if (!strcasecmp(name1, name2)) {
17006 if (strcasecmp(value1, value2)) {
17007 goto fail;
17008 } else {
17009 matched = 1;
17010 break;
17011 }
17012 }
17013 pos2 = semicolon2;
17014 }
17015
17016 if (!strcasecmp(name1, "maddr")) {
17017 if (matched) {
17018 maddrmatch = 1;
17019 } else {
17020 goto fail;
17021 }
17022 } else if (!strcasecmp(name1, "ttl")) {
17023 if (matched) {
17024 ttlmatch = 1;
17025 } else {
17026 goto fail;
17027 }
17028 } else if (!strcasecmp(name1, "user")) {
17029 if (matched) {
17030 usermatch = 1;
17031 } else {
17032 goto fail;
17033 }
17034 } else if (!strcasecmp(name1, "method")) {
17035 if (matched) {
17036 methodmatch = 1;
17037 } else {
17038 goto fail;
17039 }
17040 }
17041 pos1 = semicolon1;
17042 }
17043
17044
17045
17046
17047
17048 pos2 = params2;
17049 while (!ast_strlen_zero(pos2)) {
17050 char *name2 = pos2;
17051 char *value2 = strchr(pos2, '=');
17052 char *semicolon2 = strchr(pos2, ';');
17053 if (semicolon2) {
17054 *semicolon2++ = '\0';
17055 }
17056 if (!value2) {
17057 goto fail;
17058 }
17059 *value2++ = '\0';
17060 if ((!strcasecmp(name2, "maddr") && !maddrmatch) ||
17061 (!strcasecmp(name2, "ttl") && !ttlmatch) ||
17062 (!strcasecmp(name2, "user") && !usermatch) ||
17063 (!strcasecmp(name2, "method") && !methodmatch)) {
17064 goto fail;
17065 }
17066 }
17067 return 0;
17068
17069 fail:
17070 return 1;
17071 }
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084 static int sip_uri_headers_cmp(const char *input1, const char *input2)
17085 {
17086 char *headers1 = NULL;
17087 char *headers2 = NULL;
17088 int zerolength1 = 0;
17089 int zerolength2 = 0;
17090 int different = 0;
17091 char *header1;
17092
17093 if (ast_strlen_zero(input1)) {
17094 zerolength1 = 1;
17095 } else {
17096 headers1 = ast_strdupa(input1);
17097 }
17098
17099 if (ast_strlen_zero(input2)) {
17100 zerolength2 = 1;
17101 } else {
17102 headers2 = ast_strdupa(input2);
17103 }
17104
17105 if ((zerolength1 && !zerolength2) ||
17106 (zerolength2 && !zerolength1))
17107 return 1;
17108
17109 if (zerolength1 && zerolength2)
17110 return 0;
17111
17112
17113
17114
17115
17116 if (strlen(headers1) != strlen(headers2)) {
17117 return 1;
17118 }
17119
17120 for (header1 = strsep(&headers1, "&"); header1; header1 = strsep(&headers1, "&")) {
17121 if (!strcasestr(headers2, header1)) {
17122 different = 1;
17123 break;
17124 }
17125 }
17126
17127 return different;
17128 }
17129
17130 static int sip_uri_cmp(const char *input1, const char *input2)
17131 {
17132 char *uri1 = ast_strdupa(input1);
17133 char *uri2 = ast_strdupa(input2);
17134 char *host1;
17135 char *host2;
17136 char *params1;
17137 char *params2;
17138 char *headers1;
17139 char *headers2;
17140
17141
17142
17143
17144 strsep(&uri1, ":");
17145 strsep(&uri2, ":");
17146
17147 if ((host1 = strchr(uri1, '@'))) {
17148 *host1++ = '\0';
17149 }
17150 if ((host2 = strchr(uri2, '@'))) {
17151 *host2++ = '\0';
17152 }
17153
17154
17155
17156
17157 if ((host1 && !host2) ||
17158 (host2 && !host1) ||
17159 (host1 && host2 && strcmp(uri1, uri2))) {
17160 return 1;
17161 }
17162
17163 if (!host1)
17164 host1 = uri1;
17165 if (!host2)
17166 host2 = uri2;
17167
17168
17169
17170
17171
17172 if ((params1 = strchr(host1, ';'))) {
17173 *params1++ = '\0';
17174 }
17175 if ((params2 = strchr(host2, ';'))) {
17176 *params2++ = '\0';
17177 }
17178
17179
17180
17181
17182 if ((headers1 = strchr(S_OR(params1, host1), '?'))) {
17183 *headers1++ = '\0';
17184 }
17185 if ((headers2 = strchr(S_OR(params2, host2), '?'))) {
17186 *headers2++ = '\0';
17187 }
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199 if (strcasecmp(host1, host2)) {
17200 return 1;
17201 }
17202
17203
17204 if (sip_uri_headers_cmp(headers1, headers2)) {
17205 return 1;
17206 }
17207
17208
17209 return sip_uri_params_cmp(params1, params2);
17210 }
17211
17212
17213 static int sip_t38_abort(const void *data)
17214 {
17215 struct sip_pvt *p = (struct sip_pvt *) data;
17216
17217 change_t38_state(p, T38_DISABLED);
17218 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
17219 p->t38id = -1;
17220 return 0;
17221 }
17222
17223
17224
17225
17226
17227
17228
17229 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)
17230 {
17231 int res = 1;
17232 int gotdest;
17233 const char *p_replaces;
17234 char *replace_id = NULL;
17235 const char *required;
17236 unsigned int required_profile = 0;
17237 struct ast_channel *c = NULL;
17238 int reinvite = 0;
17239 int rtn;
17240
17241 const char *p_uac_se_hdr;
17242 const char *p_uac_min_se;
17243 int uac_max_se = -1;
17244 int uac_min_se = -1;
17245 int st_active = FALSE;
17246 int st_interval = 0;
17247 enum st_refresher st_ref;
17248 int dlg_min_se = -1;
17249 st_ref = SESSION_TIMER_REFRESHER_AUTO;
17250
17251
17252 if (!p->sipoptions) {
17253 const char *supported = get_header(req, "Supported");
17254 if (!ast_strlen_zero(supported))
17255 parse_sip_options(p, supported);
17256 }
17257
17258
17259 required = get_header(req, "Require");
17260 if (!ast_strlen_zero(required)) {
17261 required_profile = parse_sip_options(NULL, required);
17262 if (required_profile && required_profile != SIP_OPT_REPLACES && required_profile != SIP_OPT_TIMER) {
17263
17264 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
17265 ast_log(LOG_WARNING, "Received SIP INVITE with unsupported required extension: %s\n", required);
17266 p->invitestate = INV_COMPLETED;
17267 if (!p->lastinvite)
17268 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17269 return -1;
17270 }
17271 }
17272
17273
17274
17275 p->sipoptions |= required_profile;
17276 p->reqsipoptions = required_profile;
17277
17278
17279 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->invitestate != INV_TERMINATED && p->invitestate != INV_CONFIRMED)) {
17280
17281
17282
17283
17284
17285 int different;
17286 if (pedanticsipchecking)
17287 different = sip_uri_cmp(p->initreq.rlPart2, req->rlPart2);
17288 else
17289 different = strcmp(p->initreq.rlPart2, req->rlPart2);
17290 if (!different) {
17291 transmit_response(p, "482 Loop Detected", req);
17292 p->invitestate = INV_COMPLETED;
17293 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17294 return 0;
17295 } else {
17296
17297
17298
17299
17300 char *uri = ast_strdupa(req->rlPart2);
17301 char *at = strchr(uri, '@');
17302 char *peerorhost;
17303 ast_debug(3, "Potential spiral detected. Original RURI was %s, new RURI is %s\n", p->initreq.rlPart2, req->rlPart2);
17304 if (at) {
17305 *at = '\0';
17306 }
17307
17308 if ((peerorhost = strchr(uri, ':'))) {
17309 *peerorhost++ = '\0';
17310 }
17311 ast_string_field_set(p, theirtag, NULL);
17312
17313
17314 ast_string_field_set(p->owner, call_forward, peerorhost);
17315 ast_queue_control(p->owner, AST_CONTROL_BUSY);
17316 return 0;
17317 }
17318 }
17319
17320 if (!req->ignore && p->pendinginvite) {
17321 if (!ast_test_flag(&p->flags[0], SIP_OUTGOING) && ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331 __sip_ack(p, p->lastinvite, 1, 0);
17332 } else {
17333
17334 p->glareinvite = seqno;
17335 if (p->rtp && find_sdp(req)) {
17336 struct sockaddr_in sin;
17337 if (get_ip_and_port_from_sdp(req, SDP_AUDIO, &sin)) {
17338 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Audio may not work properly on this call.\n");
17339 } else {
17340 ast_rtp_set_alt_peer(p->rtp, &sin);
17341 }
17342 if (p->vrtp) {
17343 if (get_ip_and_port_from_sdp(req, SDP_VIDEO, &sin)) {
17344 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Video may not work properly on this call.\n");
17345 } else {
17346 ast_rtp_set_alt_peer(p->vrtp, &sin);
17347 }
17348 }
17349 }
17350 transmit_response_reliable(p, "491 Request Pending", req);
17351 ast_debug(1, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
17352
17353 return 0;
17354 }
17355 }
17356
17357 p_replaces = get_header(req, "Replaces");
17358 if (!ast_strlen_zero(p_replaces)) {
17359
17360 char *ptr;
17361 char *fromtag = NULL;
17362 char *totag = NULL;
17363 char *start, *to;
17364 int error = 0;
17365
17366 if (p->owner) {
17367 ast_debug(3, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
17368 transmit_response_reliable(p, "400 Bad request", req);
17369
17370 return -1;
17371 }
17372
17373 if (sipdebug)
17374 ast_debug(3, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
17375
17376 replace_id = ast_strdupa(p_replaces);
17377 ast_uri_decode(replace_id);
17378
17379 if (!p->refer && !sip_refer_allocate(p)) {
17380 transmit_response_reliable(p, "500 Server Internal Error", req);
17381 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
17382 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17383 p->invitestate = INV_COMPLETED;
17384 return -1;
17385 }
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396 replace_id = ast_skip_blanks(replace_id);
17397
17398 start = replace_id;
17399 while ( (ptr = strsep(&start, ";")) ) {
17400 ptr = ast_skip_blanks(ptr);
17401 if ( (to = strcasestr(ptr, "to-tag=") ) )
17402 totag = to + 7;
17403 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
17404 fromtag = to + 9;
17405 fromtag = strsep(&fromtag, "&");
17406 }
17407 }
17408
17409 if (sipdebug)
17410 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>");
17411
17412
17413
17414
17415
17416 if ((p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
17417 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
17418 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
17419 error = 1;
17420 }
17421
17422
17423
17424
17425
17426
17427
17428 if (p->refer->refer_call == p) {
17429 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
17430 p->refer->refer_call = dialog_unref(p->refer->refer_call);
17431 transmit_response_reliable(p, "400 Bad request", req);
17432 error = 1;
17433 }
17434
17435 if (!error && !p->refer->refer_call->owner) {
17436
17437 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
17438
17439 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replace)", req);
17440 error = 1;
17441 }
17442
17443 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 ) {
17444 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
17445 transmit_response_reliable(p, "603 Declined (Replaces)", req);
17446 error = 1;
17447 }
17448
17449 if (error) {
17450 append_history(p, "Xfer", "INVITE/Replace Failed.");
17451 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17452 sip_pvt_unlock(p);
17453 if (p->refer->refer_call) {
17454 sip_pvt_unlock(p->refer->refer_call);
17455 if (p->refer->refer_call->owner) {
17456 ast_channel_unlock(p->refer->refer_call->owner);
17457 }
17458 }
17459 p->invitestate = INV_COMPLETED;
17460 return -1;
17461 }
17462 }
17463
17464
17465
17466
17467 if (!req->ignore) {
17468 int newcall = (p->initreq.headers ? TRUE : FALSE);
17469
17470 if (sip_cancel_destroy(p))
17471 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
17472
17473 p->pendinginvite = seqno;
17474 check_via(p, req);
17475
17476 copy_request(&p->initreq, req);
17477 if (sipdebug)
17478 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
17479 if (!p->owner) {
17480 if (debug)
17481 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
17482 if (newcall)
17483 append_history(p, "Invite", "New call: %s", p->callid);
17484 parse_ok_contact(p, req);
17485 } else {
17486 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
17487
17488 if (find_sdp(req)) {
17489 if (process_sdp(p, req, SDP_T38_INITIATE)) {
17490 transmit_response_reliable(p, "488 Not acceptable here", req);
17491 if (!p->lastinvite)
17492 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17493 return -1;
17494 }
17495 } else {
17496 p->jointcapability = p->capability;
17497 ast_debug(1, "Hm.... No sdp for the moment\n");
17498 }
17499 if (p->do_history)
17500 append_history(p, "ReInv", "Re-invite received");
17501 }
17502 } else if (debug)
17503 ast_verbose("Ignoring this INVITE request\n");
17504
17505
17506 if (!p->lastinvite && !req->ignore && !p->owner) {
17507
17508
17509 res = check_user(p, req, SIP_INVITE, e, XMIT_RELIABLE, sin);
17510 if (res == AUTH_CHALLENGE_SENT) {
17511 p->invitestate = INV_COMPLETED;
17512 return 0;
17513 }
17514 if (res < 0) {
17515 if (res == AUTH_FAKE_AUTH) {
17516 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
17517 transmit_fake_auth_response(p, SIP_INVITE, req, XMIT_RELIABLE);
17518 } else {
17519 ast_log(LOG_NOTICE, "Failed to authenticate user %s\n", get_header(req, "From"));
17520 transmit_response_reliable(p, "403 Forbidden", req);
17521 }
17522 p->invitestate = INV_COMPLETED;
17523 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17524 ast_string_field_set(p, theirtag, NULL);
17525 return 0;
17526 }
17527
17528
17529 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))) {
17530 set_t38_capabilities(p);
17531 p->t38.jointcapability = p->t38.capability;
17532 }
17533
17534
17535 if (find_sdp(req)) {
17536 if (process_sdp(p, req, SDP_T38_INITIATE)) {
17537
17538 transmit_response_reliable(p, "488 Not acceptable here", req);
17539 p->invitestate = INV_COMPLETED;
17540 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17541 ast_debug(1, "No compatible codecs for this SIP call.\n");
17542 return -1;
17543 }
17544 } else {
17545 p->jointcapability = p->capability;
17546 ast_debug(2, "No SDP in Invite, third party call control\n");
17547 }
17548
17549
17550
17551 if (p->owner)
17552 ast_queue_frame(p->owner, &ast_null_frame);
17553
17554
17555
17556 if (ast_strlen_zero(p->context))
17557 ast_string_field_set(p, context, default_context);
17558
17559
17560
17561 ast_debug(1, "Checking SIP call limits for device %s\n", p->username);
17562 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
17563 if (res < 0) {
17564 ast_log(LOG_NOTICE, "Failed to place call for user %s, too many calls\n", p->username);
17565 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
17566 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17567 p->invitestate = INV_COMPLETED;
17568 }
17569 return 0;
17570 }
17571 gotdest = get_destination(p, NULL);
17572 get_rdnis(p, NULL);
17573 extract_uri(p, req);
17574 build_contact(p);
17575
17576 if (p->rtp) {
17577 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
17578 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
17579 }
17580
17581 if (!replace_id && gotdest) {
17582 if (gotdest == 1 && ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP))
17583 transmit_response_reliable(p, "484 Address Incomplete", req);
17584 else {
17585 char *decoded_exten = ast_strdupa(p->exten);
17586
17587 transmit_response_reliable(p, "404 Not Found", req);
17588 ast_uri_decode(decoded_exten);
17589 ast_log(LOG_NOTICE, "Call from '%s' to extension"
17590 " '%s' rejected because extension not found.\n",
17591 S_OR(p->username, p->peername), decoded_exten);
17592 }
17593 p->invitestate = INV_COMPLETED;
17594 update_call_counter(p, DEC_CALL_LIMIT);
17595 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17596 return 0;
17597 } else {
17598
17599
17600
17601 if (ast_strlen_zero(p->exten))
17602 ast_string_field_set(p, exten, "s");
17603
17604
17605 make_our_tag(p->tag, sizeof(p->tag));
17606
17607 c = sip_new(p, AST_STATE_DOWN, S_OR(p->peername, NULL));
17608 *recount = 1;
17609
17610
17611 build_route(p, req, 0);
17612
17613 if (c) {
17614
17615 ast_channel_lock(c);
17616 }
17617 }
17618 } else {
17619 if (sipdebug) {
17620 if (!req->ignore)
17621 ast_debug(2, "Got a SIP re-invite for call %s\n", p->callid);
17622 else
17623 ast_debug(2, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
17624 }
17625 if (!req->ignore)
17626 reinvite = 1;
17627 c = p->owner;
17628 }
17629
17630
17631 if (p->sipoptions == SIP_OPT_TIMER) {
17632
17633
17634 ast_debug(2, "Incoming INVITE with 'timer' option enabled\n");
17635
17636
17637 if (!p->stimer)
17638 sip_st_alloc(p);
17639
17640
17641 p_uac_se_hdr = get_header(req, "Session-Expires");
17642 if (!ast_strlen_zero(p_uac_se_hdr)) {
17643 rtn = parse_session_expires(p_uac_se_hdr, &uac_max_se, &st_ref);
17644 if (rtn != 0) {
17645 transmit_response_reliable(p, "400 Session-Expires Invalid Syntax", req);
17646 p->invitestate = INV_COMPLETED;
17647 if (!p->lastinvite) {
17648 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17649 }
17650 return -1;
17651 }
17652 }
17653
17654
17655 p_uac_min_se = get_header(req, "Min-SE");
17656 if (!ast_strlen_zero(p_uac_min_se)) {
17657 rtn = parse_minse(p_uac_min_se, &uac_min_se);
17658 if (rtn != 0) {
17659 transmit_response_reliable(p, "400 Min-SE Invalid Syntax", req);
17660 p->invitestate = INV_COMPLETED;
17661 if (!p->lastinvite) {
17662 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17663 }
17664 return -1;
17665 }
17666 }
17667
17668 dlg_min_se = st_get_se(p, FALSE);
17669 switch (st_get_mode(p)) {
17670 case SESSION_TIMER_MODE_ACCEPT:
17671 case SESSION_TIMER_MODE_ORIGINATE:
17672 if (uac_max_se > 0 && uac_max_se < dlg_min_se) {
17673 transmit_response_with_minse(p, "422 Session Interval Too Small", req, dlg_min_se);
17674 p->invitestate = INV_COMPLETED;
17675 if (!p->lastinvite) {
17676 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17677 }
17678 return -1;
17679 }
17680
17681 p->stimer->st_active_peer_ua = TRUE;
17682 st_active = TRUE;
17683 if (st_ref == SESSION_TIMER_REFRESHER_AUTO) {
17684 st_ref = st_get_refresher(p);
17685 }
17686
17687 if (uac_max_se > 0) {
17688 int dlg_max_se = st_get_se(p, TRUE);
17689 if (dlg_max_se >= uac_min_se) {
17690 st_interval = (uac_max_se < dlg_max_se) ? uac_max_se : dlg_max_se;
17691 } else {
17692 st_interval = uac_max_se;
17693 }
17694 } else {
17695 st_interval = uac_min_se;
17696 }
17697 break;
17698
17699 case SESSION_TIMER_MODE_REFUSE:
17700 if (p->reqsipoptions == SIP_OPT_TIMER) {
17701 transmit_response_with_unsupported(p, "420 Option Disabled", req, required);
17702 ast_log(LOG_WARNING, "Received SIP INVITE with supported but disabled option: %s\n", required);
17703 p->invitestate = INV_COMPLETED;
17704 if (!p->lastinvite) {
17705 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17706 }
17707 return -1;
17708 }
17709 break;
17710
17711 default:
17712 ast_log(LOG_ERROR, "Internal Error %d at %s:%d\n", st_get_mode(p), __FILE__, __LINE__);
17713 break;
17714 }
17715 } else {
17716
17717
17718
17719 switch (st_get_mode(p)) {
17720 case SESSION_TIMER_MODE_ORIGINATE:
17721 st_active = TRUE;
17722 st_interval = st_get_se(p, TRUE);
17723 st_ref = SESSION_TIMER_REFRESHER_UAS;
17724 p->stimer->st_active_peer_ua = FALSE;
17725 break;
17726
17727 default:
17728 break;
17729 }
17730 }
17731
17732 if (reinvite == 0) {
17733
17734 if (st_active == TRUE) {
17735 p->stimer->st_active = TRUE;
17736 p->stimer->st_interval = st_interval;
17737 p->stimer->st_ref = st_ref;
17738 start_session_timer(p);
17739 }
17740 } else {
17741 if (p->stimer->st_active == TRUE) {
17742
17743
17744
17745 ast_debug (2, "Restarting session-timers on a refresh - %s\n", p->callid);
17746
17747
17748 if (st_interval > 0) {
17749 p->stimer->st_interval = st_interval;
17750 p->stimer->st_ref = st_ref;
17751 }
17752
17753 restart_session_timer(p);
17754 if (p->stimer->st_expirys > 0) {
17755 p->stimer->st_expirys--;
17756 }
17757 }
17758 }
17759
17760 if (!req->ignore && p)
17761 p->lastinvite = seqno;
17762
17763 if (replace_id) {
17764
17765 if (sipdebug)
17766 ast_debug(4, "Sending this call to the invite/replcaes handler %s\n", p->callid);
17767 return handle_invite_replaces(p, req, debug, seqno, sin);
17768 }
17769
17770
17771 if (c) {
17772 switch(c->_state) {
17773 case AST_STATE_DOWN:
17774 ast_debug(2, "%s: New call is still down.... Trying... \n", c->name);
17775 transmit_response(p, "100 Trying", req);
17776 p->invitestate = INV_PROCEEDING;
17777 ast_setstate(c, AST_STATE_RING);
17778 if (strcmp(p->exten, ast_pickup_ext())) {
17779 enum ast_pbx_result res;
17780
17781 res = ast_pbx_start(c);
17782
17783 switch(res) {
17784 case AST_PBX_FAILED:
17785 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
17786 p->invitestate = INV_COMPLETED;
17787 transmit_response_reliable(p, "503 Unavailable", req);
17788 break;
17789 case AST_PBX_CALL_LIMIT:
17790 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
17791 p->invitestate = INV_COMPLETED;
17792 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
17793 break;
17794 case AST_PBX_SUCCESS:
17795
17796 break;
17797 }
17798
17799 if (res) {
17800
17801
17802 ast_channel_unlock(c);
17803 sip_pvt_unlock(p);
17804 ast_hangup(c);
17805 sip_pvt_lock(p);
17806 c = NULL;
17807 }
17808 } else {
17809 ast_channel_unlock(c);
17810 *nounlock = 1;
17811 if (ast_pickup_call(c)) {
17812 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
17813 transmit_response_reliable(p, "503 Unavailable", req);
17814 sip_alreadygone(p);
17815
17816 sip_pvt_unlock(p);
17817 c->hangupcause = AST_CAUSE_CALL_REJECTED;
17818 } else {
17819 sip_pvt_unlock(p);
17820 ast_setstate(c, AST_STATE_DOWN);
17821 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
17822 }
17823 p->invitestate = INV_COMPLETED;
17824 ast_hangup(c);
17825 sip_pvt_lock(p);
17826 c = NULL;
17827 }
17828 break;
17829 case AST_STATE_RING:
17830 transmit_response(p, "100 Trying", req);
17831 p->invitestate = INV_PROCEEDING;
17832 break;
17833 case AST_STATE_RINGING:
17834 if (reinvite && (p->invitestate == INV_TERMINATED || p->invitestate == INV_CONFIRMED)) {
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844 } else {
17845 transmit_response(p, "180 Ringing", req);
17846 p->invitestate = INV_PROCEEDING;
17847 break;
17848 }
17849 case AST_STATE_UP:
17850 ast_debug(2, "%s: This call is UP.... \n", c->name);
17851
17852 transmit_response(p, "100 Trying", req);
17853
17854 if (p->t38.state == T38_PEER_REINVITE) {
17855 p->t38id = ast_sched_add(sched, 5000, sip_t38_abort, p);
17856 } else if (p->t38.state == T38_ENABLED) {
17857 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
17858 transmit_response_with_t38_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)));
17859 } else if (p->t38.state == T38_DISABLED) {
17860
17861 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
17862 transmit_response_with_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)), p->session_modify == TRUE ? FALSE:TRUE);
17863 }
17864
17865 p->invitestate = INV_TERMINATED;
17866 break;
17867 default:
17868 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
17869 transmit_response(p, "100 Trying", req);
17870 break;
17871 }
17872 } else {
17873 if (p && (p->autokillid == -1)) {
17874 const char *msg;
17875
17876 if (!p->jointcapability)
17877 msg = "488 Not Acceptable Here (codec error)";
17878 else {
17879 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
17880 msg = "503 Unavailable";
17881 }
17882 transmit_response_reliable(p, msg, req);
17883 p->invitestate = INV_COMPLETED;
17884 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17885 }
17886 }
17887 return res;
17888 }
17889
17890
17891
17892 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno)
17893 {
17894 struct sip_dual target;
17895
17896 int res = 0;
17897 struct sip_pvt *targetcall_pvt;
17898
17899
17900 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
17901 transferer->refer->replaces_callid_fromtag))) {
17902 if (transferer->refer->localtransfer) {
17903
17904 transmit_response(transferer, "202 Accepted", req);
17905
17906
17907 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
17908 append_history(transferer, "Xfer", "Refer failed");
17909 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
17910 transferer->refer->status = REFER_FAILED;
17911 return -1;
17912 }
17913
17914 ast_debug(3, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
17915 return 0;
17916 }
17917
17918
17919 transmit_response(transferer, "202 Accepted", req);
17920 append_history(transferer, "Xfer", "Refer accepted");
17921 if (!targetcall_pvt->owner) {
17922 ast_debug(4, "SIP attended transfer: Error: No owner of target call\n");
17923
17924 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
17925 append_history(transferer, "Xfer", "Refer failed");
17926 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
17927 transferer->refer->status = REFER_FAILED;
17928 sip_pvt_unlock(targetcall_pvt);
17929 return -1;
17930 }
17931
17932
17933 target.chan1 = targetcall_pvt->owner;
17934 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
17935
17936 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
17937
17938 if (target.chan2)
17939 ast_debug(4, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
17940 else if (target.chan1->_state != AST_STATE_RING)
17941 ast_debug(4, "SIP attended transfer: Error: No target channel\n");
17942 else
17943 ast_debug(4, "SIP attended transfer: Attempting transfer in ringing state\n");
17944 }
17945
17946
17947 if (sipdebug) {
17948 if (current->chan2)
17949 ast_debug(4, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
17950 else
17951 ast_debug(4, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
17952 }
17953
17954 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
17955
17956
17957 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",
17958 transferer->owner->name,
17959 transferer->owner->uniqueid,
17960 transferer->callid,
17961 target.chan1->name,
17962 target.chan1->uniqueid);
17963 res = attempt_transfer(current, &target);
17964 sip_pvt_unlock(targetcall_pvt);
17965 if (res) {
17966
17967 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
17968 append_history(transferer, "Xfer", "Refer failed");
17969 if (targetcall_pvt->owner)
17970 ast_channel_unlock(targetcall_pvt->owner);
17971 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
17972 } else {
17973
17974
17975
17976 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
17977 append_history(transferer, "Xfer", "Refer succeeded");
17978 transferer->refer->status = REFER_200OK;
17979 if (targetcall_pvt->owner) {
17980 ast_debug(1, "SIP attended transfer: Unlocking channel %s\n", targetcall_pvt->owner->name);
17981 ast_channel_unlock(targetcall_pvt->owner);
17982 }
17983 }
17984 return 1;
17985 }
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
18052 {
18053 struct sip_dual current;
18054
18055
18056 int res = 0;
18057
18058 if (req->debug)
18059 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");
18060
18061 if (!p->owner) {
18062
18063
18064 ast_debug(3, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
18065 transmit_response(p, "603 Declined (No dialog)", req);
18066 if (!req->ignore) {
18067 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
18068 sip_alreadygone(p);
18069 p->needdestroy = 1;
18070 }
18071 return 0;
18072 }
18073
18074
18075
18076 if (p->allowtransfer == TRANSFER_CLOSED ) {
18077
18078 transmit_response(p, "603 Declined (policy)", req);
18079 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
18080
18081 return 0;
18082 }
18083
18084 if (!req->ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
18085
18086 transmit_response(p, "491 Request pending", req);
18087 append_history(p, "Xfer", "Refer failed. Request pending.");
18088 return 0;
18089 }
18090
18091
18092 if (!p->refer && !sip_refer_allocate(p)) {
18093 transmit_response(p, "500 Internal Server Error", req);
18094 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
18095 return -3;
18096 }
18097
18098 res = get_refer_info(p, req);
18099
18100 p->refer->status = REFER_SENT;
18101
18102 if (res != 0) {
18103 switch (res) {
18104 case -2:
18105 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
18106 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
18107 if (req->debug)
18108 ast_debug(1, "SIP transfer to black hole can't be handled (no refer-to: )\n");
18109 break;
18110 case -3:
18111 transmit_response(p, "603 Declined (Non sip: uri)", req);
18112 append_history(p, "Xfer", "Refer failed. Non SIP uri");
18113 if (req->debug)
18114 ast_debug(1, "SIP transfer to non-SIP uri denied\n");
18115 break;
18116 default:
18117
18118 transmit_response(p, "202 Accepted", req);
18119 append_history(p, "Xfer", "Refer failed. Bad extension.");
18120 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
18121 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
18122 if (req->debug)
18123 ast_debug(1, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
18124 break;
18125 }
18126 return 0;
18127 }
18128 if (ast_strlen_zero(p->context))
18129 ast_string_field_set(p, context, default_context);
18130
18131
18132 if (allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
18133 p->refer->localtransfer = 1;
18134 if (sipdebug)
18135 ast_debug(3, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
18136 } else if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
18137
18138 p->refer->localtransfer = 1;
18139 } else if (sipdebug)
18140 ast_debug(3, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
18141
18142
18143
18144 if (req->ignore)
18145 return res;
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173 current.chan1 = p->owner;
18174
18175
18176 current.chan2 = ast_bridged_channel(current.chan1);
18177
18178 if (sipdebug)
18179 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>");
18180
18181 if (!current.chan2 && !p->refer->attendedtransfer) {
18182
18183
18184
18185 if (sipdebug)
18186 ast_debug(3, "Refused SIP transfer on non-bridged channel.\n");
18187 p->refer->status = REFER_FAILED;
18188 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
18189 transmit_response(p, "603 Declined", req);
18190 return -1;
18191 }
18192
18193 if (current.chan2) {
18194 if (sipdebug)
18195 ast_debug(4, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
18196
18197 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
18198 }
18199
18200 ast_set_flag(&p->flags[0], SIP_GOTREFER);
18201
18202
18203 if (p->refer->attendedtransfer) {
18204 if ((res = local_attended_transfer(p, ¤t, req, seqno)))
18205 return res;
18206
18207 if (sipdebug)
18208 ast_debug(4, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
18209
18210 }
18211
18212
18213
18214 if (p->refer->localtransfer && !strcmp(p->refer->refer_to, ast_parking_ext())) {
18215
18216 *nounlock = 1;
18217 ast_channel_unlock(current.chan1);
18218 copy_request(¤t.req, req);
18219 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
18220 p->refer->status = REFER_200OK;
18221 append_history(p, "Xfer", "REFER to call parking.");
18222 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",
18223 current.chan1->name,
18224 current.chan1->uniqueid,
18225 p->callid,
18226 current.chan2->name,
18227 current.chan2->uniqueid,
18228 p->refer->refer_to);
18229 if (sipdebug)
18230 ast_debug(4, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
18231 sip_park(current.chan2, current.chan1, req, seqno);
18232 return res;
18233 }
18234
18235
18236 transmit_response(p, "202 Accepted", req);
18237
18238 if (current.chan1 && current.chan2) {
18239 ast_debug(3, "chan1->name: %s\n", current.chan1->name);
18240 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
18241 }
18242 if (current.chan2) {
18243 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
18244 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
18245 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
18246
18247 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
18248
18249 if (p->refer->referred_by)
18250 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
18251 }
18252
18253 if (!ast_strlen_zero(p->refer->replaces_callid)) {
18254 char tempheader[SIPBUFSIZE];
18255 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
18256 p->refer->replaces_callid_totag ? ";to-tag=" : "",
18257 p->refer->replaces_callid_totag,
18258 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
18259 p->refer->replaces_callid_fromtag);
18260 if (current.chan2)
18261 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
18262 }
18263
18264
18265 *nounlock = 1;
18266 ast_channel_unlock(current.chan1);
18267
18268
18269
18270
18271 if (!p->refer->attendedtransfer)
18272 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
18273
18274
18275
18276
18277
18278 if (!current.chan2) {
18279
18280
18281
18282
18283
18284
18285
18286 p->refer->status = REFER_FAILED;
18287 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
18288 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
18289 append_history(p, "Xfer", "Refer failed (only bridged calls).");
18290 return -1;
18291 }
18292 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
18293
18294
18295
18296
18297 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
18298
18299 if (!res) {
18300 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",
18301 current.chan1->name,
18302 current.chan1->uniqueid,
18303 p->callid,
18304 current.chan2->name,
18305 current.chan2->uniqueid,
18306 p->refer->refer_to, p->refer->refer_to_context);
18307
18308 ast_debug(3, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
18309 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
18310 if (p->refer->localtransfer)
18311 p->refer->status = REFER_200OK;
18312 if (p->owner)
18313 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
18314 append_history(p, "Xfer", "Refer succeeded.");
18315 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
18316
18317
18318 res = 0;
18319 } else {
18320 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
18321 ast_debug(3, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
18322 append_history(p, "Xfer", "Refer failed.");
18323
18324 p->refer->status = REFER_FAILED;
18325 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
18326 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
18327 res = -1;
18328 }
18329 return res;
18330 }
18331
18332
18333 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
18334 {
18335
18336 check_via(p, req);
18337 sip_alreadygone(p);
18338
18339
18340
18341
18342
18343
18344 if (p->invitestate == INV_TERMINATED)
18345 __sip_pretend_ack(p);
18346 else
18347 p->invitestate = INV_CANCELLED;
18348
18349 if (p->owner && p->owner->_state == AST_STATE_UP) {
18350
18351 transmit_response(p, "200 OK", req);
18352 ast_debug(1, "Got CANCEL on an answered call. Ignoring... \n");
18353 return 0;
18354 }
18355
18356 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
18357 update_call_counter(p, DEC_CALL_LIMIT);
18358
18359 stop_media_flows(p);
18360 if (p->owner)
18361 ast_queue_hangup(p->owner);
18362 else
18363 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18364 if (p->initreq.len > 0) {
18365 struct sip_pkt *pkt, *prev_pkt;
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377 for (pkt = p->packets, prev_pkt = NULL; pkt; prev_pkt = pkt, pkt = pkt->next) {
18378 if (pkt->seqno == p->lastinvite && pkt->response_code == 487) {
18379 AST_SCHED_DEL(sched, pkt->retransid);
18380 if (prev_pkt) {
18381 prev_pkt->next = pkt->next;
18382 } else {
18383 p->packets = pkt->next;
18384 }
18385 ast_free(pkt);
18386 break;
18387 }
18388 }
18389 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
18390 transmit_response(p, "200 OK", req);
18391 return 1;
18392 } else {
18393 transmit_response(p, "481 Call Leg Does Not Exist", req);
18394 return 0;
18395 }
18396 }
18397
18398 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
18399 {
18400 struct sip_pvt *p = chan->tech_pvt;
18401 char *all = "", *parse = ast_strdupa(preparse);
18402 int res = 0;
18403 AST_DECLARE_APP_ARGS(args,
18404 AST_APP_ARG(param);
18405 AST_APP_ARG(type);
18406 AST_APP_ARG(field);
18407 );
18408 AST_STANDARD_APP_ARGS(args, parse);
18409
18410
18411 if (!IS_SIP_TECH(chan->tech)) {
18412 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
18413 return 0;
18414 }
18415
18416 memset(buf, 0, buflen);
18417
18418 if (p == NULL) {
18419 return -1;
18420 }
18421
18422 if (!strcasecmp(args.param, "rtpdest")) {
18423 struct sockaddr_in sin;
18424
18425 if (ast_strlen_zero(args.type))
18426 args.type = "audio";
18427
18428 if (!strcasecmp(args.type, "audio"))
18429 ast_rtp_get_peer(p->rtp, &sin);
18430 else if (!strcasecmp(args.type, "video"))
18431 ast_rtp_get_peer(p->vrtp, &sin);
18432 else if (!strcasecmp(args.type, "text"))
18433 ast_rtp_get_peer(p->trtp, &sin);
18434
18435 snprintf(buf, buflen, "%s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
18436 } else if (!strcasecmp(args.param, "rtpqos")) {
18437 struct ast_rtp_quality qos;
18438
18439 memset(&qos, 0, sizeof(qos));
18440
18441 if (ast_strlen_zero(args.type))
18442 args.type = "audio";
18443 if (ast_strlen_zero(args.field))
18444 args.field = "all";
18445
18446 if (!strcasecmp(args.type, "AUDIO")) {
18447 all = ast_rtp_get_quality(p->rtp, &qos);
18448 } else if (!strcasecmp(args.type, "VIDEO")) {
18449 all = ast_rtp_get_quality(p->vrtp, &qos);
18450 } else if (!strcasecmp(args.type, "TEXT")) {
18451 all = ast_rtp_get_quality(p->trtp, &qos);
18452 }
18453
18454 if (!strcasecmp(args.field, "local_ssrc"))
18455 snprintf(buf, buflen, "%u", qos.local_ssrc);
18456 else if (!strcasecmp(args.field, "local_lostpackets"))
18457 snprintf(buf, buflen, "%u", qos.local_lostpackets);
18458 else if (!strcasecmp(args.field, "local_jitter"))
18459 snprintf(buf, buflen, "%.0f", qos.local_jitter * 1000.0);
18460 else if (!strcasecmp(args.field, "local_count"))
18461 snprintf(buf, buflen, "%u", qos.local_count);
18462 else if (!strcasecmp(args.field, "remote_ssrc"))
18463 snprintf(buf, buflen, "%u", qos.remote_ssrc);
18464 else if (!strcasecmp(args.field, "remote_lostpackets"))
18465 snprintf(buf, buflen, "%u", qos.remote_lostpackets);
18466 else if (!strcasecmp(args.field, "remote_jitter"))
18467 snprintf(buf, buflen, "%.0f", qos.remote_jitter * 1000.0);
18468 else if (!strcasecmp(args.field, "remote_count"))
18469 snprintf(buf, buflen, "%u", qos.remote_count);
18470 else if (!strcasecmp(args.field, "rtt"))
18471 snprintf(buf, buflen, "%.0f", qos.rtt * 1000.0);
18472 else if (!strcasecmp(args.field, "all"))
18473 ast_copy_string(buf, all, buflen);
18474 else {
18475 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
18476 return -1;
18477 }
18478 } else {
18479 res = -1;
18480 }
18481 return res;
18482 }
18483
18484
18485 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
18486 {
18487 struct ast_channel *c=NULL;
18488 int res;
18489 struct ast_channel *bridged_to;
18490
18491
18492 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !req->ignore) {
18493 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
18494 }
18495
18496 __sip_pretend_ack(p);
18497
18498 p->invitestate = INV_TERMINATED;
18499
18500 copy_request(&p->initreq, req);
18501 if (sipdebug)
18502 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
18503 check_via(p, req);
18504 sip_alreadygone(p);
18505
18506
18507 if (p->do_history || p->owner) {
18508 char *audioqos, *videoqos, *textqos;
18509 if (p->rtp) {
18510 audioqos = ast_rtp_get_quality(p->rtp, NULL);
18511 if (p->do_history)
18512 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
18513 if (p->owner)
18514 pbx_builtin_setvar_helper(p->owner, "RTPAUDIOQOS", audioqos);
18515 }
18516 if (p->vrtp) {
18517 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
18518 if (p->do_history)
18519 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
18520 if (p->owner)
18521 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", videoqos);
18522 }
18523 if (p->trtp) {
18524 textqos = ast_rtp_get_quality(p->trtp, NULL);
18525 if (p->do_history)
18526 append_history(p, "RTCPtext", "Quality:%s", textqos);
18527 if (p->owner)
18528 pbx_builtin_setvar_helper(p->owner, "RTPTEXTQOS", textqos);
18529 }
18530 }
18531
18532 stop_media_flows(p);
18533 stop_session_timer(p);
18534
18535 if (!ast_strlen_zero(get_header(req, "Also"))) {
18536 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
18537 ast_inet_ntoa(p->recv.sin_addr));
18538 if (ast_strlen_zero(p->context))
18539 ast_string_field_set(p, context, default_context);
18540 res = get_also_info(p, req);
18541 if (!res) {
18542 c = p->owner;
18543 if (c) {
18544 bridged_to = ast_bridged_channel(c);
18545 if (bridged_to) {
18546
18547 ast_queue_control(c, AST_CONTROL_UNHOLD);
18548 ast_async_goto(bridged_to, p->context, p->refer->refer_to, 1);
18549 } else
18550 ast_queue_hangup(p->owner);
18551 }
18552 } else {
18553 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(p->recv.sin_addr));
18554 if (p->owner)
18555 ast_queue_hangup(p->owner);
18556 }
18557 } else if (p->owner) {
18558 ast_queue_hangup(p->owner);
18559 ast_debug(3, "Received bye, issuing owner hangup\n");
18560 } else {
18561 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18562 ast_debug(3, "Received bye, no owner, selfdestruct soon.\n");
18563 }
18564 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
18565 transmit_response(p, "200 OK", req);
18566
18567 return 1;
18568 }
18569
18570
18571 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
18572 {
18573 if (!req->ignore) {
18574 if (req->debug)
18575 ast_verbose("Receiving message!\n");
18576 receive_message(p, req);
18577 } else
18578 transmit_response(p, "202 Accepted", req);
18579 return 1;
18580 }
18581
18582 static void add_peer_mwi_subs(struct sip_peer *peer)
18583 {
18584 struct sip_mailbox *mailbox;
18585
18586 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
18587 mailbox->event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, peer,
18588 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
18589 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
18590 AST_EVENT_IE_END);
18591 }
18592 }
18593
18594
18595 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
18596 {
18597 int gotdest = 0;
18598 int res = 0;
18599 int firststate = AST_EXTENSION_REMOVED;
18600 struct sip_peer *authpeer = NULL;
18601 const char *eventheader = get_header(req, "Event");
18602 const char *accept = get_header(req, "Accept");
18603 int resubscribe = (p->subscribed != NONE);
18604 char *temp, *event;
18605
18606 if (p->initreq.headers) {
18607
18608 if (p->initreq.method != SIP_SUBSCRIBE) {
18609
18610
18611 transmit_response(p, "403 Forbidden (within dialog)", req);
18612
18613 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);
18614 return 0;
18615 } else if (req->debug) {
18616 if (resubscribe)
18617 ast_debug(1, "Got a re-subscribe on existing subscription %s\n", p->callid);
18618 else
18619 ast_debug(1, "Got a new subscription %s (possibly with auth)\n", p->callid);
18620 }
18621 }
18622
18623
18624
18625
18626 if (!global_allowsubscribe) {
18627 transmit_response(p, "403 Forbidden (policy)", req);
18628 p->needdestroy = 1;
18629 return 0;
18630 }
18631
18632 if (!req->ignore && !resubscribe) {
18633 const char *to = get_header(req, "To");
18634 char totag[128];
18635
18636
18637 if (!ast_strlen_zero(to) && gettag(req, "To", totag, sizeof(totag))) {
18638 if (req->debug)
18639 ast_verbose("Received resubscription for a dialog we no longer know about. Telling remote side to subscribe again.\n");
18640 transmit_response(p, "481 Subscription does not exist", req);
18641 p->needdestroy = 1;
18642 return 0;
18643 }
18644
18645
18646 if (req->debug)
18647 ast_verbose("Creating new subscription\n");
18648
18649 copy_request(&p->initreq, req);
18650 if (sipdebug)
18651 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
18652 check_via(p, req);
18653 } else if (req->debug && req->ignore)
18654 ast_verbose("Ignoring this SUBSCRIBE request\n");
18655
18656
18657 if (ast_strlen_zero(eventheader)) {
18658 transmit_response(p, "489 Bad Event", req);
18659 ast_debug(2, "Received SIP subscribe for unknown event package: <none>\n");
18660 p->needdestroy = 1;
18661 return 0;
18662 }
18663
18664 if ( (strchr(eventheader, ';'))) {
18665 event = ast_strdupa(eventheader);
18666 temp = strchr(event, ';');
18667 *temp = '\0';
18668
18669 } else
18670 event = (char *) eventheader;
18671
18672
18673 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, &authpeer);
18674
18675 if (res == AUTH_CHALLENGE_SENT)
18676 return 0;
18677 if (res < 0) {
18678 if (res == AUTH_FAKE_AUTH) {
18679 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
18680 transmit_fake_auth_response(p, SIP_SUBSCRIBE, req, XMIT_UNRELIABLE);
18681 } else {
18682 ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
18683 transmit_response_reliable(p, "403 Forbidden", req);
18684 }
18685 p->needdestroy = 1;
18686 return 0;
18687 }
18688
18689
18690
18691
18692
18693
18694
18695 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
18696 transmit_response(p, "403 Forbidden (policy)", req);
18697 p->needdestroy = 1;
18698 if (authpeer)
18699 unref_peer(authpeer);
18700 return 0;
18701 }
18702
18703 if (strcmp(event, "message-summary")) {
18704
18705 gotdest = get_destination(p, NULL);
18706 }
18707
18708
18709 parse_ok_contact(p, req);
18710
18711 build_contact(p);
18712 if (gotdest) {
18713 transmit_response(p, "404 Not Found", req);
18714 p->needdestroy = 1;
18715 if (authpeer)
18716 unref_peer(authpeer);
18717 return 0;
18718 }
18719
18720
18721 if (ast_strlen_zero(p->tag))
18722 make_our_tag(p->tag, sizeof(p->tag));
18723
18724 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
18725 unsigned int pidf_xml;
18726
18727 if (authpeer)
18728 unref_peer(authpeer);
18729
18730
18731
18732 pidf_xml = strstr(accept, "application/pidf+xml") ? 1 : 0;
18733
18734
18735
18736 if (pidf_xml && strstr(p->useragent, "Polycom")) {
18737 p->subscribed = XPIDF_XML;
18738 } else if (pidf_xml) {
18739 p->subscribed = PIDF_XML;
18740 } else if (strstr(accept, "application/dialog-info+xml")) {
18741 p->subscribed = DIALOG_INFO_XML;
18742
18743 } else if (strstr(accept, "application/cpim-pidf+xml")) {
18744 p->subscribed = CPIM_PIDF_XML;
18745 } else if (strstr(accept, "application/xpidf+xml")) {
18746 p->subscribed = XPIDF_XML;
18747 } else if (ast_strlen_zero(accept)) {
18748 if (p->subscribed == NONE) {
18749 transmit_response(p, "489 Bad Event", req);
18750
18751 ast_log(LOG_WARNING, "SUBSCRIBE failure: no Accept header: pvt: stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
18752 p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
18753 p->needdestroy = 1;
18754 return 0;
18755 }
18756
18757
18758 } else {
18759
18760 char mybuf[200];
18761 snprintf(mybuf, sizeof(mybuf), "489 Bad Event (format %s)", accept);
18762 transmit_response(p, mybuf, req);
18763
18764 ast_log(LOG_WARNING, "SUBSCRIBE failure: unrecognized format: '%s' pvt: subscribed: %d, stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
18765 accept, (int)p->subscribed, p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
18766 p->needdestroy = 1;
18767 return 0;
18768 }
18769 } else if (!strcmp(event, "message-summary")) {
18770 if (!ast_strlen_zero(accept) && strcmp(accept, "application/simple-message-summary")) {
18771
18772 transmit_response(p, "406 Not Acceptable", req);
18773 ast_debug(2, "Received SIP mailbox subscription for unknown format: %s\n", accept);
18774 p->needdestroy = 1;
18775 if (authpeer)
18776 unref_peer(authpeer);
18777 return 0;
18778 }
18779
18780
18781
18782
18783
18784 if (!authpeer || AST_LIST_EMPTY(&authpeer->mailboxes)) {
18785 transmit_response(p, "404 Not found (no mailbox)", req);
18786 p->needdestroy = 1;
18787 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
18788 if (authpeer)
18789 unref_peer(authpeer);
18790 return 0;
18791 }
18792
18793 p->subscribed = MWI_NOTIFICATION;
18794 if (ast_test_flag(&authpeer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY)) {
18795 add_peer_mwi_subs(authpeer);
18796 }
18797 if (authpeer->mwipvt && authpeer->mwipvt != p)
18798
18799 sip_destroy(authpeer->mwipvt);
18800 authpeer->mwipvt = p;
18801 p->relatedpeer = authpeer;
18802
18803 } else {
18804 transmit_response(p, "489 Bad Event", req);
18805 ast_debug(2, "Received SIP subscribe for unknown event package: %s\n", event);
18806 p->needdestroy = 1;
18807 if (authpeer)
18808 unref_peer(authpeer);
18809 return 0;
18810 }
18811
18812
18813 if (p->subscribed != MWI_NOTIFICATION && !resubscribe) {
18814 if (p->stateid > -1)
18815 ast_extension_state_del(p->stateid, cb_extensionstate);
18816 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
18817 }
18818
18819 if (!req->ignore && p)
18820 p->lastinvite = seqno;
18821 if (p && !p->needdestroy) {
18822 p->expiry = atoi(get_header(req, "Expires"));
18823
18824
18825 if (p->expiry > max_expiry)
18826 p->expiry = max_expiry;
18827 if (p->expiry < min_expiry && p->expiry > 0)
18828 p->expiry = min_expiry;
18829
18830 if (sipdebug) {
18831 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
18832 ast_debug(2, "Adding subscription for mailbox notification - peer %s\n", p->relatedpeer->name);
18833 else
18834 ast_debug(2, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
18835 }
18836 if (p->autokillid > -1 && sip_cancel_destroy(p))
18837 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
18838 if (p->expiry > 0)
18839 sip_scheddestroy(p, (p->expiry + 10) * 1000);
18840
18841 if (p->subscribed == MWI_NOTIFICATION) {
18842 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
18843 transmit_response(p, "200 OK", req);
18844 if (p->relatedpeer) {
18845 ASTOBJ_WRLOCK(p->relatedpeer);
18846 sip_send_mwi_to_peer(p->relatedpeer, NULL, 0);
18847 ASTOBJ_UNLOCK(p->relatedpeer);
18848 }
18849 } else {
18850 struct sip_pvt *p_old;
18851
18852 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
18853
18854 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));
18855 transmit_response(p, "404 Not found", req);
18856 p->needdestroy = 1;
18857 return 0;
18858 }
18859 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
18860 transmit_response(p, "200 OK", req);
18861 transmit_state_notify(p, firststate, 1, FALSE);
18862 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
18863
18864 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
18865
18866
18867
18868
18869
18870
18871 dialoglist_lock();
18872 for (p_old = dialoglist; p_old; p_old = p_old->next) {
18873 if (p_old == p)
18874 continue;
18875 if (p_old->initreq.method != SIP_SUBSCRIBE)
18876 continue;
18877 if (p_old->subscribed == NONE)
18878 continue;
18879 sip_pvt_lock(p_old);
18880 if (!strcmp(p_old->username, p->username)) {
18881 if (!strcmp(p_old->exten, p->exten) &&
18882 !strcmp(p_old->context, p->context)) {
18883 p_old->needdestroy = 1;
18884 sip_pvt_unlock(p_old);
18885 break;
18886 }
18887 }
18888 sip_pvt_unlock(p_old);
18889 }
18890 dialoglist_unlock();
18891 }
18892 if (!p->expiry)
18893 p->needdestroy = 1;
18894 }
18895 return 1;
18896 }
18897
18898
18899 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e)
18900 {
18901 enum check_auth_result res;
18902
18903
18904 copy_request(&p->initreq, req);
18905 if (sipdebug)
18906 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
18907 check_via(p, req);
18908 if ((res = register_verify(p, sin, req, e)) < 0) {
18909 const char *reason;
18910
18911 switch (res) {
18912 case AUTH_SECRET_FAILED:
18913 reason = "Wrong password";
18914 break;
18915 case AUTH_USERNAME_MISMATCH:
18916 reason = "Username/auth name mismatch";
18917 break;
18918 case AUTH_NOT_FOUND:
18919 reason = "No matching peer found";
18920 break;
18921 case AUTH_UNKNOWN_DOMAIN:
18922 reason = "Not a local domain";
18923 break;
18924 case AUTH_PEER_NOT_DYNAMIC:
18925 reason = "Peer is not supposed to register";
18926 break;
18927 case AUTH_ACL_FAILED:
18928 reason = "Device does not match ACL";
18929 break;
18930 case AUTH_BAD_TRANSPORT:
18931 reason = "Device not configured to use this transport type";
18932 break;
18933 default:
18934 reason = "Unknown failure";
18935 break;
18936 }
18937 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
18938 get_header(req, "To"), ast_inet_ntoa(sin->sin_addr),
18939 reason);
18940 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
18941 } else
18942 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
18943
18944 if (res < 1) {
18945
18946
18947 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18948 }
18949 return res;
18950 }
18951
18952
18953
18954
18955 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
18956 {
18957
18958
18959 const char *cmd;
18960 const char *cseq;
18961 const char *useragent;
18962 int seqno;
18963 int len;
18964 int respid;
18965 int res = 0;
18966 int debug = sip_debug_test_pvt(p);
18967 char *e;
18968 int error = 0;
18969
18970
18971 cseq = get_header(req, "Cseq");
18972 cmd = req->header[0];
18973
18974
18975 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq)) {
18976 ast_log(LOG_ERROR, "Missing Cseq. Dropping this SIP message, it's incomplete.\n");
18977 error = 1;
18978 }
18979 if (!error && sscanf(cseq, "%d%n", &seqno, &len) != 1) {
18980 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
18981 error = 1;
18982 }
18983 if (error) {
18984 if (!p->initreq.headers)
18985 p->needdestroy = 1;
18986 return -1;
18987 }
18988
18989
18990 cmd = req->rlPart1;
18991 e = req->rlPart2;
18992
18993
18994 useragent = get_header(req, "User-Agent");
18995 if (!ast_strlen_zero(useragent))
18996 ast_string_field_set(p, useragent, useragent);
18997
18998
18999 if (req->method == SIP_RESPONSE) {
19000
19001
19002
19003
19004
19005
19006 int ret = 0;
19007
19008 if (p->ocseq < seqno && seqno != p->lastnoninvite) {
19009 ast_debug(1, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
19010 ret = -1;
19011 } else if (p->ocseq != seqno && seqno != p->lastnoninvite) {
19012
19013
19014
19015
19016
19017
19018 req->ignore = 1;
19019 append_history(p, "Ignore", "Ignoring this retransmit\n");
19020 } else if (e) {
19021 e = ast_skip_blanks(e);
19022 if (sscanf(e, "%d %n", &respid, &len) != 1) {
19023 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
19024
19025 } else if (respid <= 0) {
19026 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
19027
19028 } else {
19029
19030 if ((respid == 200) || ((respid >= 300) && (respid <= 399)))
19031 extract_uri(p, req);
19032 handle_response(p, respid, e + len, req, seqno);
19033 }
19034 }
19035 return 0;
19036 }
19037
19038
19039
19040
19041
19042 p->method = req->method;
19043 ast_debug(4, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
19044
19045 if (p->icseq && (p->icseq > seqno) ) {
19046 if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
19047 ast_debug(2, "Got CANCEL or ACK on INVITE with transactions in between.\n");
19048 } else {
19049 ast_debug(1, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
19050 if (req->method != SIP_ACK)
19051 transmit_response(p, "503 Server error", req);
19052 return -1;
19053 }
19054 } else if (p->icseq &&
19055 p->icseq == seqno &&
19056 req->method != SIP_ACK &&
19057 (p->method != SIP_CANCEL || p->alreadygone)) {
19058
19059
19060
19061 req->ignore = 1;
19062 ast_debug(3, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
19063 }
19064
19065 if (seqno >= p->icseq)
19066
19067
19068
19069 p->icseq = seqno;
19070
19071
19072 if (ast_strlen_zero(p->theirtag)) {
19073 char tag[128];
19074
19075 gettag(req, "From", tag, sizeof(tag));
19076 ast_string_field_set(p, theirtag, tag);
19077 }
19078 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
19079
19080 if (pedanticsipchecking) {
19081
19082
19083
19084
19085 if (!p->initreq.headers && req->has_to_tag) {
19086
19087 if (!req->ignore && req->method == SIP_INVITE) {
19088 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
19089
19090 } else if (req->method != SIP_ACK) {
19091 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
19092 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19093 } else {
19094 ast_debug(1, "Got ACK for unknown dialog... strange.\n");
19095 }
19096 return res;
19097 }
19098 }
19099
19100 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY)) {
19101 transmit_response(p, "400 Bad request", req);
19102 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19103 return -1;
19104 }
19105
19106
19107 switch (p->method) {
19108 case SIP_OPTIONS:
19109 res = handle_request_options(p, req);
19110 break;
19111 case SIP_INVITE:
19112 res = handle_request_invite(p, req, debug, seqno, sin, recount, e, nounlock);
19113 break;
19114 case SIP_REFER:
19115 res = handle_request_refer(p, req, debug, seqno, nounlock);
19116 break;
19117 case SIP_CANCEL:
19118 res = handle_request_cancel(p, req);
19119 break;
19120 case SIP_BYE:
19121 res = handle_request_bye(p, req);
19122 break;
19123 case SIP_MESSAGE:
19124 res = handle_request_message(p, req);
19125 break;
19126 case SIP_SUBSCRIBE:
19127 res = handle_request_subscribe(p, req, sin, seqno, e);
19128 break;
19129 case SIP_REGISTER:
19130 res = handle_request_register(p, req, sin, e);
19131 break;
19132 case SIP_INFO:
19133 if (req->debug)
19134 ast_verbose("Receiving INFO!\n");
19135 if (!req->ignore)
19136 handle_request_info(p, req);
19137 else
19138 transmit_response(p, "200 OK", req);
19139 break;
19140 case SIP_NOTIFY:
19141 res = handle_request_notify(p, req, sin, seqno, e);
19142 break;
19143 case SIP_ACK:
19144
19145 if (seqno == p->pendinginvite) {
19146 p->invitestate = INV_TERMINATED;
19147 p->pendinginvite = 0;
19148 __sip_ack(p, seqno, 1 , 0);
19149 if (find_sdp(req)) {
19150 if (process_sdp(p, req, SDP_T38_NONE))
19151 return -1;
19152 }
19153 check_pendings(p);
19154 } else if (p->glareinvite == seqno) {
19155
19156 p->glareinvite = 0;
19157 __sip_ack(p, seqno, 1, 0);
19158 }
19159
19160 if (!p->lastinvite && ast_strlen_zero(p->randdata))
19161 p->needdestroy = 1;
19162 break;
19163 default:
19164 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
19165 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
19166 cmd, ast_inet_ntoa(p->sa.sin_addr));
19167
19168 if (!p->initreq.headers)
19169 p->needdestroy = 1;
19170 break;
19171 }
19172 return res;
19173 }
19174
19175 static void process_request_queue(struct sip_pvt *p, int *recount, int *nounlock)
19176 {
19177 struct sip_request *req;
19178
19179 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
19180 if (handle_incoming(p, req, &p->recv, recount, nounlock) == -1) {
19181
19182 if (option_debug) {
19183 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
19184 }
19185 }
19186 ast_free(req);
19187 }
19188 }
19189
19190 static int scheduler_process_request_queue(const void *data)
19191 {
19192 struct sip_pvt *p = (struct sip_pvt *) data;
19193 int recount = 0;
19194 int nounlock = 0;
19195 int lockretry;
19196
19197 for (lockretry = 10; lockretry > 0; lockretry--) {
19198 sip_pvt_lock(p);
19199
19200
19201
19202 if (!p->owner || !ast_channel_trylock(p->owner)) {
19203 break;
19204 }
19205
19206 if (lockretry != 1) {
19207 sip_pvt_unlock(p);
19208
19209 usleep(1);
19210 }
19211 }
19212
19213 if (!lockretry) {
19214 int retry = !AST_LIST_EMPTY(&p->request_queue);
19215
19216
19217
19218
19219
19220
19221 sip_pvt_unlock(p);
19222 return retry;
19223 };
19224
19225 process_request_queue(p, &recount, &nounlock);
19226 p->request_queue_sched_id = -1;
19227
19228 if (p->owner && !nounlock) {
19229 ast_channel_unlock(p->owner);
19230 }
19231 sip_pvt_unlock(p);
19232
19233 if (recount) {
19234 ast_update_use_count();
19235 }
19236
19237 return 0;
19238 }
19239
19240 static int queue_request(struct sip_pvt *p, const struct sip_request *req)
19241 {
19242 struct sip_request *newreq;
19243
19244 if (!(newreq = ast_calloc(1, sizeof(*newreq)))) {
19245 return -1;
19246 }
19247
19248 copy_request(newreq, req);
19249 AST_LIST_INSERT_TAIL(&p->request_queue, newreq, next);
19250 if (p->request_queue_sched_id == -1) {
19251 p->request_queue_sched_id = ast_sched_add(sched, 10, scheduler_process_request_queue, p);
19252 }
19253
19254 return 0;
19255 }
19256
19257
19258
19259
19260
19261
19262 static int sipsock_read(int *id, int fd, short events, void *ignore)
19263 {
19264 struct sip_request req;
19265 struct sockaddr_in sin = { 0, };
19266 int res;
19267 socklen_t len = sizeof(sin);
19268
19269 memset(&req, 0, sizeof(req));
19270 res = recvfrom(fd, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
19271 if (res < 0) {
19272 #if !defined(__FreeBSD__)
19273 if (errno == EAGAIN)
19274 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
19275 else
19276 #endif
19277 if (errno != ECONNREFUSED)
19278 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
19279 return 1;
19280 }
19281 if (res == sizeof(req.data) - 1)
19282 ast_debug(1, "Received packet exceeds buffer. Data is possibly lost\n");
19283
19284 req.data[res] = '\0';
19285 req.len = res;
19286
19287 req.socket.fd = sipsock;
19288 set_socket_transport(&req.socket, SIP_TRANSPORT_UDP);
19289 req.socket.tcptls_session = NULL;
19290 req.socket.port = bindaddr.sin_port;
19291
19292 handle_request_do(&req, &sin);
19293
19294 return 1;
19295 }
19296
19297 static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin)
19298 {
19299 struct sip_pvt *p;
19300 int recount = 0;
19301 int nounlock = 0;
19302 int lockretry;
19303
19304 if (sip_debug_test_addr(sin))
19305 req->debug = 1;
19306 if (pedanticsipchecking)
19307 req->len = lws2sws(req->data, req->len);
19308 if (req->debug) {
19309 ast_verbose("\n<--- SIP read from %s://%s:%d --->\n%s\n<------------->\n",
19310 get_transport(req->socket.type), ast_inet_ntoa(sin->sin_addr),
19311 ntohs(sin->sin_port), req->data);
19312 }
19313
19314 if (parse_request(req) == -1) {
19315 req->data[0] = '\0';
19316 return 1;
19317 }
19318
19319 req->method = find_sip_method(req->rlPart1);
19320
19321 if (req->debug)
19322 ast_verbose("--- (%d headers %d lines)%s ---\n", req->headers, req->lines, (req->headers + req->lines == 0) ? " Nat keepalive" : "");
19323
19324 if (req->headers < 2)
19325 return 1;
19326
19327
19328 for (lockretry = 10; lockretry > 0; lockretry--) {
19329 ast_mutex_lock(&netlock);
19330
19331
19332 p = find_call(req, sin, req->method);
19333 if (p == NULL) {
19334 ast_debug(1, "Invalid SIP message - rejected , no callid, len %d\n", req->len);
19335 ast_mutex_unlock(&netlock);
19336 return 1;
19337 }
19338
19339 copy_socket_data(&p->socket, &req->socket);
19340
19341
19342
19343 if (!p->owner || !ast_channel_trylock(p->owner))
19344 break;
19345 if (lockretry != 1) {
19346 sip_pvt_unlock(p);
19347 ast_mutex_unlock(&netlock);
19348
19349 usleep(1);
19350 }
19351 }
19352 p->recv = *sin;
19353
19354 if (p->do_history)
19355 append_history(p, "Rx", "%s / %s / %s", req->data, get_header(req, "CSeq"), req->rlPart2);
19356
19357 if (!lockretry) {
19358 if (!queue_request(p, req)) {
19359
19360 sip_pvt_unlock(p);
19361 ast_mutex_unlock(&netlock);
19362 return 1;
19363 }
19364
19365
19366 if (p->owner)
19367 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 ??? - "));
19368 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
19369 if (req->method != SIP_ACK)
19370 transmit_response(p, "503 Server error", req);
19371
19372 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
19373 sip_pvt_unlock(p);
19374 ast_mutex_unlock(&netlock);
19375 return 1;
19376 }
19377
19378
19379
19380
19381 if (!AST_LIST_EMPTY(&p->request_queue)) {
19382 AST_SCHED_DEL(sched, p->request_queue_sched_id);
19383 process_request_queue(p, &recount, &nounlock);
19384 }
19385
19386 if (handle_incoming(p, req, sin, &recount, &nounlock) == -1) {
19387
19388 ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
19389 }
19390
19391 if (recount)
19392 ast_update_use_count();
19393
19394 if (p->owner && !nounlock)
19395 ast_channel_unlock(p->owner);
19396 sip_pvt_unlock(p);
19397 ast_mutex_unlock(&netlock);
19398
19399 return 1;
19400 }
19401
19402
19403
19404
19405
19406
19407
19408 static int sip_standard_port(enum sip_transport type, int port)
19409 {
19410 if (type & SIP_TRANSPORT_TLS)
19411 return port == STANDARD_TLS_PORT;
19412 else
19413 return port == STANDARD_SIP_PORT;
19414 }
19415
19416
19417
19418
19419
19420
19421 static struct ast_tcptls_session_instance *sip_tcp_locate(struct sockaddr_in *s)
19422 {
19423 struct sip_threadinfo *th;
19424 struct ast_tcptls_session_instance *tcptls_instance = NULL;
19425
19426 AST_LIST_LOCK(&threadl);
19427 AST_LIST_TRAVERSE(&threadl, th, list) {
19428 if ((s->sin_family == th->tcptls_session->requestor.sin_family) &&
19429 (s->sin_addr.s_addr == th->tcptls_session->requestor.sin_addr.s_addr) &&
19430 (s->sin_port == th->tcptls_session->requestor.sin_port)) {
19431 tcptls_instance = (ao2_ref(th->tcptls_session, +1), th->tcptls_session);
19432 break;
19433 }
19434 }
19435 AST_LIST_UNLOCK(&threadl);
19436
19437 return tcptls_instance;
19438 }
19439
19440
19441 static int sip_prepare_socket(struct sip_pvt *p)
19442 {
19443 struct sip_socket *s = &p->socket;
19444 static const char name[] = "SIP socket";
19445 struct ast_tcptls_session_instance *tcptls_session;
19446 struct server_args ca = {
19447 .name = name,
19448 .accept_fd = -1,
19449 };
19450
19451 if (s->fd != -1)
19452 return s->fd;
19453
19454 if (p->outboundproxy && p->outboundproxy->transport) {
19455 s->type = p->outboundproxy->transport;
19456 }
19457
19458 if (s->type & SIP_TRANSPORT_UDP) {
19459 s->fd = sipsock;
19460 return s->fd;
19461 }
19462
19463 ca.sin = *(sip_real_dst(p));
19464
19465 if ((tcptls_session = sip_tcp_locate(&ca.sin))) {
19466 s->fd = tcptls_session->fd;
19467 if (s->tcptls_session) {
19468 ao2_ref(s->tcptls_session, -1);
19469 s->tcptls_session = NULL;
19470 }
19471 s->tcptls_session = tcptls_session;
19472 return s->fd;
19473 }
19474
19475 if (s->tcptls_session && s->tcptls_session->parent->tls_cfg) {
19476 ca.tls_cfg = s->tcptls_session->parent->tls_cfg;
19477 } else {
19478 if (s->type & SIP_TRANSPORT_TLS) {
19479 ca.tls_cfg = ast_calloc(1, sizeof(*ca.tls_cfg));
19480 if (!ca.tls_cfg)
19481 return -1;
19482 memcpy(ca.tls_cfg, &default_tls_cfg, sizeof(*ca.tls_cfg));
19483 if (!ast_strlen_zero(p->tohost))
19484 ast_copy_string(ca.hostname, p->tohost, sizeof(ca.hostname));
19485 }
19486 }
19487
19488 if (s->tcptls_session) {
19489
19490 } else {
19491 s->tcptls_session = ast_tcptls_client_start(&ca);
19492 }
19493
19494 if (!s->tcptls_session) {
19495 if (ca.tls_cfg)
19496 ast_free(ca.tls_cfg);
19497 return -1;
19498 }
19499
19500 s->fd = ca.accept_fd;
19501
19502
19503 ao2_ref(s->tcptls_session, +1);
19504
19505 if (ast_pthread_create_background(&ca.master, NULL, sip_tcp_worker_fn, s->tcptls_session)) {
19506 ast_debug(1, "Unable to launch '%s'.", ca.name);
19507 ao2_ref(s->tcptls_session, -1);
19508 close(ca.accept_fd);
19509 s->fd = ca.accept_fd = -1;
19510 }
19511
19512 return s->fd;
19513 }
19514
19515
19516
19517
19518
19519 static int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport)
19520 {
19521 char *port;
19522
19523 if ((*hostname = strstr(line, "://"))) {
19524 *hostname += 3;
19525
19526 if (!strncasecmp(line, "tcp", 3))
19527 *transport = SIP_TRANSPORT_TCP;
19528 else if (!strncasecmp(line, "tls", 3))
19529 *transport = SIP_TRANSPORT_TLS;
19530 else if (!strncasecmp(line, "udp", 3))
19531 *transport = SIP_TRANSPORT_UDP;
19532 else
19533 ast_log(LOG_NOTICE, "'%.3s' is not a valid transport type on line %d of sip.conf. defaulting to udp.\n", line, lineno);
19534 } else {
19535 *hostname = line;
19536 *transport = SIP_TRANSPORT_UDP;
19537 }
19538
19539 if ((line = strrchr(*hostname, '@')))
19540 line++;
19541 else
19542 line = *hostname;
19543
19544 if ((port = strrchr(line, ':'))) {
19545 *port++ = '\0';
19546
19547 if (!sscanf(port, "%u", portnum)) {
19548 ast_log(LOG_NOTICE, "'%s' is not a valid port number on line %d of sip.conf. using default.\n", port, lineno);
19549 port = NULL;
19550 }
19551 }
19552
19553 if (!port) {
19554 if (*transport & SIP_TRANSPORT_TLS) {
19555 *portnum = STANDARD_TLS_PORT;
19556 } else {
19557 *portnum = STANDARD_SIP_PORT;
19558 }
19559 }
19560
19561 return 0;
19562 }
19563
19564
19565
19566
19567
19568
19569 static int get_cached_mwi(struct sip_peer *peer, int *new, int *old)
19570 {
19571 struct sip_mailbox *mailbox;
19572
19573 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
19574 struct ast_event *event;
19575 event = ast_event_get_cached(AST_EVENT_MWI,
19576 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
19577 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
19578 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
19579 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
19580 AST_EVENT_IE_END);
19581 if (!event)
19582 continue;
19583 *new += ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
19584 *old += ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
19585 ast_event_destroy(event);
19586 }
19587
19588 return (*new || *old) ? 0 : 1;
19589 }
19590
19591
19592 static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only)
19593 {
19594
19595 struct sip_pvt *p;
19596 int newmsgs = 0, oldmsgs = 0;
19597
19598 if (ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY) && !peer->mwipvt)
19599 return 0;
19600
19601
19602 if (!peer->addr.sin_addr.s_addr && !peer->defaddr.sin_addr.s_addr)
19603 return 0;
19604
19605 if (event) {
19606 newmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
19607 oldmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
19608 } else if (!get_cached_mwi(peer, &newmsgs, &oldmsgs)) {
19609
19610 } else if (cache_only) {
19611 return 0;
19612 } else {
19613 struct ast_str *mailbox_str = ast_str_alloca(512);
19614 peer_mailboxes_to_str(&mailbox_str, peer);
19615 ast_app_inboxcount(mailbox_str->str, &newmsgs, &oldmsgs);
19616 }
19617
19618 if (peer->mwipvt) {
19619
19620 p = dialog_ref(peer->mwipvt);
19621 } else {
19622
19623 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL)))
19624 return -1;
19625 if (create_addr_from_peer(p, peer)) {
19626
19627 sip_destroy(p);
19628 return 0;
19629 }
19630
19631 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
19632 build_via(p);
19633 build_callid_pvt(p);
19634
19635 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19636 }
19637
19638
19639 ast_set_flag(&p->flags[0], SIP_OUTGOING);
19640 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
19641
19642 return 0;
19643 }
19644
19645
19646 static void check_rtp_timeout(struct sip_pvt *dialog, time_t t)
19647 {
19648
19649 if (!dialog->rtp || !dialog->owner)
19650 return;
19651
19652 if (dialog->owner->_state != AST_STATE_UP || dialog->redirip.sin_addr.s_addr)
19653 return;
19654
19655
19656 if (dialog->t38.state == T38_ENABLED)
19657 return;
19658
19659
19660 if ((ast_rtp_get_rtpkeepalive(dialog->rtp) == 0) && (ast_rtp_get_rtptimeout(dialog->rtp) == 0) && (ast_rtp_get_rtpholdtimeout(dialog->rtp) == 0))
19661 return;
19662
19663
19664 if (dialog->lastrtptx && ast_rtp_get_rtpkeepalive(dialog->rtp) &&
19665 (t > dialog->lastrtptx + ast_rtp_get_rtpkeepalive(dialog->rtp))) {
19666
19667 dialog->lastrtptx = time(NULL);
19668 ast_rtp_sendcng(dialog->rtp, 0);
19669 }
19670
19671
19672
19673
19674
19675
19676
19677
19678 if (dialog->lastrtprx && (ast_rtp_get_rtptimeout(dialog->rtp) || ast_rtp_get_rtpholdtimeout(dialog->rtp)) &&
19679 (t > dialog->lastrtprx + ast_rtp_get_rtptimeout(dialog->rtp))) {
19680
19681
19682 struct sockaddr_in sin;
19683 ast_rtp_get_peer(dialog->rtp, &sin);
19684 if (!ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD) || (ast_rtp_get_rtpholdtimeout(dialog->rtp) &&
19685 (t > dialog->lastrtprx + ast_rtp_get_rtpholdtimeout(dialog->rtp)))) {
19686
19687 if (ast_rtp_get_rtptimeout(dialog->rtp)) {
19688 while (dialog->owner && ast_channel_trylock(dialog->owner)) {
19689 sip_pvt_unlock(dialog);
19690 usleep(1);
19691 sip_pvt_lock(dialog);
19692 }
19693 ast_log(LOG_NOTICE, "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
19694 dialog->owner->name, (long) (t - dialog->lastrtprx));
19695
19696 ast_softhangup_nolock(dialog->owner, AST_SOFTHANGUP_DEV);
19697 ast_channel_unlock(dialog->owner);
19698
19699
19700
19701
19702 ast_rtp_set_rtptimeout(dialog->rtp, 0);
19703 ast_rtp_set_rtpholdtimeout(dialog->rtp, 0);
19704 if (dialog->vrtp) {
19705 ast_rtp_set_rtptimeout(dialog->vrtp, 0);
19706 ast_rtp_set_rtpholdtimeout(dialog->vrtp, 0);
19707 }
19708 }
19709 }
19710 }
19711 }
19712
19713
19714
19715
19716
19717 static void *do_monitor(void *data)
19718 {
19719 int res;
19720 struct sip_pvt *dialog;
19721 time_t t;
19722 int reloading;
19723
19724
19725 if (sipsock > -1)
19726 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
19727
19728
19729 for(;;) {
19730
19731 ast_mutex_lock(&sip_reload_lock);
19732 reloading = sip_reloading;
19733 sip_reloading = FALSE;
19734 ast_mutex_unlock(&sip_reload_lock);
19735 if (reloading) {
19736 ast_verb(1, "Reloading SIP\n");
19737 sip_do_reload(sip_reloadreason);
19738
19739
19740 if (sipsock > -1) {
19741 if (sipsock_read_id)
19742 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
19743 else
19744 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
19745 } else if (sipsock_read_id) {
19746 ast_io_remove(io, sipsock_read_id);
19747 sipsock_read_id = NULL;
19748 }
19749 }
19750
19751 restartsearch:
19752
19753 dialoglist_lock();
19754 t = time(NULL);
19755
19756
19757
19758
19759 for (dialog = dialoglist; dialog; dialog = dialog->next) {
19760 if (sip_pvt_trylock(dialog)) {
19761 dialoglist_unlock();
19762 usleep(1);
19763 goto restartsearch;
19764 }
19765
19766
19767 check_rtp_timeout(dialog, t);
19768
19769
19770
19771 if (dialog->needdestroy && !dialog->packets && !dialog->owner) {
19772 sip_pvt_unlock(dialog);
19773 __sip_destroy(dialog, TRUE, FALSE);
19774 dialoglist_unlock();
19775 usleep(1);
19776 goto restartsearch;
19777 }
19778 sip_pvt_unlock(dialog);
19779 }
19780 dialoglist_unlock();
19781
19782 pthread_testcancel();
19783
19784 res = ast_sched_wait(sched);
19785 if ((res < 0) || (res > 1000))
19786 res = 1000;
19787 res = ast_io_wait(io, res);
19788 if (res > 20)
19789 ast_debug(1, "chan_sip: ast_io_wait ran %d all at once\n", res);
19790 ast_mutex_lock(&monlock);
19791 if (res >= 0) {
19792 res = ast_sched_runq(sched);
19793 if (res >= 20)
19794 ast_debug(1, "chan_sip: ast_sched_runq ran %d all at once\n", res);
19795 }
19796 ast_mutex_unlock(&monlock);
19797 }
19798
19799
19800 return NULL;
19801 }
19802
19803
19804 static int restart_monitor(void)
19805 {
19806
19807 if (monitor_thread == AST_PTHREADT_STOP)
19808 return 0;
19809 ast_mutex_lock(&monlock);
19810 if (monitor_thread == pthread_self()) {
19811 ast_mutex_unlock(&monlock);
19812 ast_log(LOG_WARNING, "Cannot kill myself\n");
19813 return -1;
19814 }
19815 if (monitor_thread != AST_PTHREADT_NULL) {
19816
19817 pthread_kill(monitor_thread, SIGURG);
19818 } else {
19819
19820 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
19821 ast_mutex_unlock(&monlock);
19822 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
19823 return -1;
19824 }
19825 }
19826 ast_mutex_unlock(&monlock);
19827 return 0;
19828 }
19829
19830
19831
19832 static void restart_session_timer(struct sip_pvt *p)
19833 {
19834 if (!p->stimer) {
19835 ast_log(LOG_WARNING, "Null stimer in restart_session_timer - %s\n", p->callid);
19836 return;
19837 }
19838
19839 if (p->stimer->st_active == TRUE) {
19840 if (ast_sched_del(sched, p->stimer->st_schedid) != 0) {
19841 ast_log(LOG_WARNING, "ast_sched_del failed: %d - %s\n", p->stimer->st_schedid, p->callid);
19842 }
19843
19844 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
19845 start_session_timer(p);
19846 }
19847 }
19848
19849
19850
19851 static void stop_session_timer(struct sip_pvt *p)
19852 {
19853 if (!p->stimer) {
19854 ast_log(LOG_WARNING, "Null stimer in stop_session_timer - %s\n", p->callid);
19855 return;
19856 }
19857
19858 if (p->stimer->st_active == TRUE) {
19859 p->stimer->st_active = FALSE;
19860 ast_sched_del(sched, p->stimer->st_schedid);
19861 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
19862 }
19863 }
19864
19865
19866
19867 static void start_session_timer(struct sip_pvt *p)
19868 {
19869 if (!p->stimer) {
19870 ast_log(LOG_WARNING, "Null stimer in start_session_timer - %s\n", p->callid);
19871 return;
19872 }
19873
19874 p->stimer->st_schedid = ast_sched_add(sched, p->stimer->st_interval * 1000 / 2, proc_session_timer, p);
19875 if (p->stimer->st_schedid < 0) {
19876 ast_log(LOG_ERROR, "ast_sched_add failed.\n");
19877 }
19878 ast_debug(2, "Session timer started: %d - %s\n", p->stimer->st_schedid, p->callid);
19879 }
19880
19881
19882
19883 static int proc_session_timer(const void *vp)
19884 {
19885 struct sip_pvt *p = (struct sip_pvt *) vp;
19886 int sendreinv = FALSE;
19887
19888 if (!p->stimer) {
19889 ast_log(LOG_WARNING, "Null stimer in proc_session_timer - %s\n", p->callid);
19890 return 0;
19891 }
19892
19893 ast_debug(2, "Session timer expired: %d - %s\n", p->stimer->st_schedid, p->callid);
19894
19895 if (!p->owner) {
19896 if (p->stimer->st_active == TRUE) {
19897 stop_session_timer(p);
19898 }
19899 return 0;
19900 }
19901
19902 if ((p->stimer->st_active != TRUE) || (p->owner->_state != AST_STATE_UP)) {
19903 return 0;
19904 }
19905
19906 switch (p->stimer->st_ref) {
19907 case SESSION_TIMER_REFRESHER_UAC:
19908 if (p->outgoing_call == TRUE) {
19909 sendreinv = TRUE;
19910 }
19911 break;
19912 case SESSION_TIMER_REFRESHER_UAS:
19913 if (p->outgoing_call != TRUE) {
19914 sendreinv = TRUE;
19915 }
19916 break;
19917 default:
19918 ast_log(LOG_ERROR, "Unknown session refresher %d\n", p->stimer->st_ref);
19919 return -1;
19920 }
19921
19922 if (sendreinv == TRUE) {
19923 transmit_reinvite_with_sdp(p, FALSE, TRUE);
19924 } else {
19925 p->stimer->st_expirys++;
19926 if (p->stimer->st_expirys >= 2) {
19927 ast_log(LOG_WARNING, "Session-Timer expired - %s\n", p->callid);
19928 stop_session_timer(p);
19929
19930 while (p->owner && ast_channel_trylock(p->owner)) {
19931 sip_pvt_unlock(p);
19932 usleep(1);
19933 sip_pvt_lock(p);
19934 }
19935
19936 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
19937 ast_channel_unlock(p->owner);
19938 }
19939 }
19940 return 1;
19941 }
19942
19943
19944
19945 int parse_minse (const char *p_hdrval, int *const p_interval)
19946 {
19947 if (ast_strlen_zero(p_hdrval)) {
19948 ast_log(LOG_WARNING, "Null Min-SE header\n");
19949 return -1;
19950 }
19951
19952 *p_interval = 0;
19953 p_hdrval = ast_skip_blanks(p_hdrval);
19954 if (!sscanf(p_hdrval, "%d", p_interval)) {
19955 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
19956 return -1;
19957 }
19958
19959 ast_debug(2, "Received Min-SE: %d\n", *p_interval);
19960 return 0;
19961 }
19962
19963
19964
19965 int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher *const p_ref)
19966 {
19967 char *p_token;
19968 int ref_idx;
19969 char *p_se_hdr;
19970
19971 if (ast_strlen_zero(p_hdrval)) {
19972 ast_log(LOG_WARNING, "Null Session-Expires header\n");
19973 return -1;
19974 }
19975
19976 *p_ref = SESSION_TIMER_REFRESHER_AUTO;
19977 *p_interval = 0;
19978
19979 p_se_hdr = ast_strdupa(p_hdrval);
19980 p_se_hdr = ast_skip_blanks(p_se_hdr);
19981
19982 while ((p_token = strsep(&p_se_hdr, ";"))) {
19983 p_token = ast_skip_blanks(p_token);
19984 if (!sscanf(p_token, "%d", p_interval)) {
19985 ast_log(LOG_WARNING, "Parsing of Session-Expires failed\n");
19986 return -1;
19987 }
19988
19989 ast_debug(2, "Session-Expires: %d\n", *p_interval);
19990
19991 if (!p_se_hdr)
19992 continue;
19993
19994 ref_idx = strlen("refresher=");
19995 if (!strncasecmp(p_se_hdr, "refresher=", ref_idx)) {
19996 p_se_hdr += ref_idx;
19997 p_se_hdr = ast_skip_blanks(p_se_hdr);
19998
19999 if (!strncasecmp(p_se_hdr, "uac", strlen("uac"))) {
20000 *p_ref = SESSION_TIMER_REFRESHER_UAC;
20001 ast_debug(2, "Refresher: UAC\n");
20002 } else if (!strncasecmp(p_se_hdr, "uas", strlen("uas"))) {
20003 *p_ref = SESSION_TIMER_REFRESHER_UAS;
20004 ast_debug(2, "Refresher: UAS\n");
20005 } else {
20006 ast_log(LOG_WARNING, "Invalid refresher value %s\n", p_se_hdr);
20007 return -1;
20008 }
20009 break;
20010 }
20011 }
20012 return 0;
20013 }
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp)
20024 {
20025 int rtn;
20026 const char *p_hdrval;
20027 int minse;
20028
20029 p_hdrval = get_header(rsp, "Min-SE");
20030 if (ast_strlen_zero(p_hdrval)) {
20031 ast_log(LOG_WARNING, "422 response without a Min-SE header %s\n", p_hdrval);
20032 return;
20033 }
20034 rtn = parse_minse(p_hdrval, &minse);
20035 if (rtn != 0) {
20036 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
20037 return;
20038 }
20039 p->stimer->st_interval = minse;
20040 transmit_invite(p, SIP_INVITE, 1, 2);
20041 }
20042
20043
20044
20045
20046
20047
20048 int st_get_se(struct sip_pvt *p, int max)
20049 {
20050 if (max == TRUE) {
20051 if (p->stimer->st_cached_max_se) {
20052 return p->stimer->st_cached_max_se;
20053 } else {
20054 if (p->username) {
20055 struct sip_user *up = find_user(p->username, 1);
20056 if (up) {
20057 p->stimer->st_cached_max_se = up->stimer.st_max_se;
20058 unref_user(up);
20059 return (p->stimer->st_cached_max_se);
20060 }
20061 }
20062 if (p->peername) {
20063 struct sip_peer *pp = find_peer(p->peername, NULL, 1, 0);
20064 if (pp) {
20065 p->stimer->st_cached_max_se = pp->stimer.st_max_se;
20066 unref_peer(pp);
20067 return (p->stimer->st_cached_max_se);
20068 }
20069 }
20070 }
20071 p->stimer->st_cached_max_se = global_max_se;
20072 return (p->stimer->st_cached_max_se);
20073 } else {
20074 if (p->stimer->st_cached_min_se) {
20075 return p->stimer->st_cached_min_se;
20076 } else {
20077 if (p->username) {
20078 struct sip_user *up = find_user(p->username, 1);
20079 if (up) {
20080 p->stimer->st_cached_min_se = up->stimer.st_min_se;
20081 unref_user(up);
20082 return (p->stimer->st_cached_min_se);
20083 }
20084 }
20085 if (p->peername) {
20086 struct sip_peer *pp = find_peer(p->peername, NULL, 1, 0);
20087 if (pp) {
20088 p->stimer->st_cached_min_se = pp->stimer.st_min_se;
20089 unref_peer(pp);
20090 return (p->stimer->st_cached_min_se);
20091 }
20092 }
20093 }
20094 p->stimer->st_cached_min_se = global_min_se;
20095 return (p->stimer->st_cached_min_se);
20096 }
20097 }
20098
20099
20100
20101
20102
20103 enum st_refresher st_get_refresher(struct sip_pvt *p)
20104 {
20105 if (p->stimer->st_cached_ref != SESSION_TIMER_REFRESHER_AUTO)
20106 return p->stimer->st_cached_ref;
20107
20108 if (p->username) {
20109 struct sip_user *up = find_user(p->username, 1);
20110 if (up) {
20111 p->stimer->st_cached_ref = up->stimer.st_ref;
20112 return up->stimer.st_ref;
20113 }
20114 }
20115
20116 if (p->peername) {
20117 struct sip_peer *pp = find_peer(p->peername, NULL, 1, 0);
20118 if (pp) {
20119 p->stimer->st_cached_ref = pp->stimer.st_ref;
20120 return pp->stimer.st_ref;
20121 }
20122 }
20123
20124 p->stimer->st_cached_ref = global_st_refresher;
20125 return global_st_refresher;
20126 }
20127
20128
20129
20130
20131
20132 enum st_mode st_get_mode(struct sip_pvt *p)
20133 {
20134 if (!p->stimer)
20135 sip_st_alloc(p);
20136
20137 if (p->stimer->st_cached_mode != SESSION_TIMER_MODE_INVALID)
20138 return p->stimer->st_cached_mode;
20139
20140 if (p->username) {
20141 struct sip_user *up = find_user(p->username, 1);
20142 if (up) {
20143 p->stimer->st_cached_mode = up->stimer.st_mode_oper;
20144 unref_user(up);
20145 return p->stimer->st_cached_mode;
20146 }
20147 }
20148 if (p->peername) {
20149 struct sip_peer *pp = find_peer(p->peername, NULL, 1, 0);
20150 if (pp) {
20151 p->stimer->st_cached_mode = pp->stimer.st_mode_oper;
20152 unref_peer(pp);
20153 return p->stimer->st_cached_mode;
20154 }
20155 }
20156
20157 p->stimer->st_cached_mode = global_st_mode;
20158 return global_st_mode;
20159 }
20160
20161
20162
20163 static int sip_poke_noanswer(const void *data)
20164 {
20165 struct sip_peer *peer = (struct sip_peer *)data;
20166
20167 peer->pokeexpire = -1;
20168 if (peer->lastms > -1) {
20169 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
20170 if (sip_cfg.peer_rtupdate) {
20171 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", "-1", NULL);
20172 }
20173 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
20174 if (global_regextenonqualify) {
20175 register_peer_exten(peer, FALSE);
20176 }
20177 }
20178 if (peer->call)
20179 peer->call = sip_destroy(peer->call);
20180 peer->lastms = -1;
20181 ast_device_state_changed("SIP/%s", peer->name);
20182
20183 AST_SCHED_REPLACE(peer->pokeexpire, sched,
20184 DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
20185 return 0;
20186 }
20187
20188
20189
20190
20191 static int sip_poke_peer(struct sip_peer *peer)
20192 {
20193 struct sip_pvt *p;
20194 int xmitres = 0;
20195
20196 if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
20197
20198
20199 AST_SCHED_DEL(sched, peer->pokeexpire);
20200 peer->lastms = 0;
20201 peer->call = NULL;
20202 return 0;
20203 }
20204 if (peer->call) {
20205 if (sipdebug)
20206 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
20207 peer->call = sip_destroy(peer->call);
20208 }
20209 if (!(p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS, NULL)))
20210 return -1;
20211
20212 p->sa = peer->addr;
20213 p->recv = peer->addr;
20214 copy_socket_data(&p->socket, &peer->socket);
20215 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
20216 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
20217
20218
20219 if (!ast_strlen_zero(peer->fullcontact))
20220 ast_string_field_set(p, fullcontact, peer->fullcontact);
20221
20222 if (!ast_strlen_zero(peer->tohost))
20223 ast_string_field_set(p, tohost, peer->tohost);
20224 else
20225 ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr));
20226
20227
20228 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
20229 build_via(p);
20230 build_callid_pvt(p);
20231
20232 AST_SCHED_DEL(sched, peer->pokeexpire);
20233 p->relatedpeer = peer;
20234 ast_set_flag(&p->flags[0], SIP_OUTGOING);
20235 #ifdef VOCAL_DATA_HACK
20236 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
20237 xmitres = transmit_invite(p, SIP_INVITE, 0, 2);
20238 #else
20239 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2);
20240 #endif
20241 peer->ps = ast_tvnow();
20242 if (xmitres == XMIT_ERROR)
20243 sip_poke_noanswer(peer);
20244 else {
20245 AST_SCHED_REPLACE(peer->pokeexpire, sched,
20246 peer->maxms * 2, sip_poke_noanswer, peer);
20247 }
20248
20249 return 0;
20250 }
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285 static int sip_devicestate(void *data)
20286 {
20287 char *host;
20288 char *tmp;
20289 struct sip_peer *p;
20290
20291 int res = AST_DEVICE_INVALID;
20292
20293
20294 host = ast_strdupa(data ? data : "");
20295 if ((tmp = strchr(host, '@')))
20296 host = tmp + 1;
20297
20298 ast_debug(3, "Checking device state for peer %s\n", host);
20299
20300
20301
20302
20303
20304
20305
20306
20307 if ((p = find_peer(host, NULL, 0, 1))) {
20308 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320 if (p->onHold)
20321
20322 res = AST_DEVICE_ONHOLD;
20323 else if (p->inRinging) {
20324 if (p->inRinging == p->inUse)
20325 res = AST_DEVICE_RINGING;
20326 else
20327 res = AST_DEVICE_RINGINUSE;
20328 } else if (p->call_limit && (p->inUse == p->call_limit))
20329
20330 res = AST_DEVICE_BUSY;
20331 else if (p->call_limit && p->busy_level && p->inUse >= p->busy_level)
20332
20333 res = AST_DEVICE_BUSY;
20334 else if (p->call_limit && p->inUse)
20335
20336 res = AST_DEVICE_INUSE;
20337 else if (p->maxms && ((p->lastms > p->maxms) || (p->lastms < 0)))
20338
20339 res = AST_DEVICE_UNAVAILABLE;
20340 else
20341 res = AST_DEVICE_NOT_INUSE;
20342 } else {
20343
20344 res = AST_DEVICE_UNAVAILABLE;
20345 }
20346 unref_peer(p);
20347 } else {
20348 res = AST_DEVICE_UNKNOWN;
20349 }
20350
20351 return res;
20352 }
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
20365 {
20366 struct sip_pvt *p;
20367 struct ast_channel *tmpc = NULL;
20368 char *ext = NULL, *host;
20369 char tmp[256];
20370 char *dest = data;
20371 char *dnid;
20372 char *secret = NULL;
20373 char *md5secret = NULL;
20374 char *authname = NULL;
20375 char *trans = NULL;
20376 enum sip_transport transport = 0;
20377 int oldformat = format;
20378
20379
20380
20381
20382
20383
20384
20385
20386 format &= AST_FORMAT_AUDIO_MASK;
20387 if (!format) {
20388 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));
20389 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
20390 return NULL;
20391 }
20392 ast_debug(1, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
20393
20394 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE, NULL))) {
20395 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", dest);
20396 *cause = AST_CAUSE_SWITCH_CONGESTION;
20397 return NULL;
20398 }
20399
20400 p->outgoing_call = TRUE;
20401
20402 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
20403 sip_destroy(p);
20404 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
20405 *cause = AST_CAUSE_SWITCH_CONGESTION;
20406 return NULL;
20407 }
20408
20409
20410 ast_copy_string(tmp, dest, sizeof(tmp));
20411
20412
20413
20414 dnid = strchr(tmp, '!');
20415 if (dnid != NULL) {
20416 *dnid++ = '\0';
20417 ast_string_field_set(p, todnid, dnid);
20418 }
20419
20420
20421 host = strchr(tmp, '@');
20422 if (host) {
20423 *host++ = '\0';
20424 ext = tmp;
20425 secret = strchr(ext, ':');
20426 }
20427 if (secret) {
20428 *secret++ = '\0';
20429 md5secret = strchr(secret, ':');
20430 }
20431 if (md5secret) {
20432 *md5secret++ = '\0';
20433 authname = strchr(md5secret, ':');
20434 }
20435 if (authname) {
20436 *authname++ = '\0';
20437 trans = strchr(authname, ':');
20438 }
20439 if (trans) {
20440 *trans++ = '\0';
20441 if (!strcasecmp(trans, "tcp"))
20442 transport = SIP_TRANSPORT_TCP;
20443 else if (!strcasecmp(trans, "tls"))
20444 transport = SIP_TRANSPORT_TLS;
20445 else {
20446 if (strcasecmp(trans, "udp"))
20447 ast_log(LOG_WARNING, "'%s' is not a valid transport option to Dial() for SIP calls, using udp by default.\n", trans);
20448 transport = SIP_TRANSPORT_UDP;
20449 }
20450 } else {
20451 transport = SIP_TRANSPORT_UDP;
20452 }
20453
20454 if (!host) {
20455 ext = strchr(tmp, '/');
20456 if (ext)
20457 *ext++ = '\0';
20458 host = tmp;
20459 }
20460
20461 set_socket_transport(&p->socket, transport);
20462
20463
20464
20465
20466
20467
20468 if (create_addr(p, host, 1)) {
20469 *cause = AST_CAUSE_UNREGISTERED;
20470 ast_debug(3, "Cant create SIP call - target device not registered\n");
20471 sip_destroy(p);
20472 return NULL;
20473 }
20474 if (ast_strlen_zero(p->peername) && ext)
20475 ast_string_field_set(p, peername, ext);
20476
20477 ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p);
20478 build_via(p);
20479 build_callid_pvt(p);
20480
20481
20482
20483
20484
20485
20486 if (ext) {
20487 ast_string_field_set(p, username, ext);
20488 ast_string_field_set(p, fullcontact, NULL);
20489 }
20490 if (secret && !ast_strlen_zero(secret))
20491 ast_string_field_set(p, peersecret, secret);
20492
20493 if (md5secret && !ast_strlen_zero(md5secret))
20494 ast_string_field_set(p, peermd5secret, md5secret);
20495
20496 if (authname && !ast_strlen_zero(authname))
20497 ast_string_field_set(p, authname, authname);
20498 #if 0
20499 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
20500 #endif
20501 p->prefcodec = oldformat;
20502 p->jointcapability = oldformat;
20503 sip_pvt_lock(p);
20504 tmpc = sip_new(p, AST_STATE_DOWN, host);
20505 if (global_callevents)
20506 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
20507 "Channel: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
20508 p->owner? p->owner->name : "", "SIP", p->callid, p->fullcontact, p->peername);
20509 sip_pvt_unlock(p);
20510 if (!tmpc)
20511 sip_destroy(p);
20512 ast_update_use_count();
20513 restart_monitor();
20514 return tmpc;
20515 }
20516
20517
20518 static void set_insecure_flags (struct ast_flags *flags, const char *value, int lineno)
20519 {
20520 if (ast_strlen_zero(value))
20521 return;
20522
20523 if (!ast_false(value)) {
20524 char buf[64];
20525 char *word, *next;
20526
20527 ast_copy_string(buf, value, sizeof(buf));
20528 next = buf;
20529 while ((word = strsep(&next, ","))) {
20530 if (!strcasecmp(word, "port"))
20531 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
20532 else if (!strcasecmp(word, "invite"))
20533 ast_set_flag(&flags[0], SIP_INSECURE_INVITE);
20534 else
20535 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
20536 }
20537 }
20538 }
20539
20540
20541
20542
20543
20544
20545
20546
20547 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
20548 {
20549 int res = 1;
20550
20551 if (!strcasecmp(v->name, "trustrpid")) {
20552 ast_set_flag(&mask[0], SIP_TRUSTRPID);
20553 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
20554 } else if (!strcasecmp(v->name, "sendrpid")) {
20555 ast_set_flag(&mask[0], SIP_SENDRPID);
20556 ast_set2_flag(&flags[0], ast_true(v->value), SIP_SENDRPID);
20557 } else if (!strcasecmp(v->name, "g726nonstandard")) {
20558 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
20559 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
20560 } else if (!strcasecmp(v->name, "useclientcode")) {
20561 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
20562 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
20563 } else if (!strcasecmp(v->name, "dtmfmode")) {
20564 ast_set_flag(&mask[0], SIP_DTMF);
20565 ast_clear_flag(&flags[0], SIP_DTMF);
20566 if (!strcasecmp(v->value, "inband"))
20567 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
20568 else if (!strcasecmp(v->value, "rfc2833"))
20569 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
20570 else if (!strcasecmp(v->value, "info"))
20571 ast_set_flag(&flags[0], SIP_DTMF_INFO);
20572 else if (!strcasecmp(v->value, "shortinfo"))
20573 ast_set_flag(&flags[0], SIP_DTMF_SHORTINFO);
20574 else if (!strcasecmp(v->value, "auto"))
20575 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
20576 else {
20577 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
20578 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
20579 }
20580 } else if (!strcasecmp(v->name, "nat")) {
20581 ast_set_flag(&mask[0], SIP_NAT);
20582 ast_clear_flag(&flags[0], SIP_NAT);
20583 if (!strcasecmp(v->value, "never"))
20584 ast_set_flag(&flags[0], SIP_NAT_NEVER);
20585 else if (!strcasecmp(v->value, "route"))
20586 ast_set_flag(&flags[0], SIP_NAT_ROUTE);
20587 else if (ast_true(v->value))
20588 ast_set_flag(&flags[0], SIP_NAT_ALWAYS);
20589 else
20590 ast_set_flag(&flags[0], SIP_NAT_RFC3581);
20591 } else if (!strcasecmp(v->name, "canreinvite")) {
20592 ast_set_flag(&mask[0], SIP_REINVITE);
20593 ast_clear_flag(&flags[0], SIP_REINVITE);
20594 if (ast_true(v->value)) {
20595 ast_set_flag(&flags[0], SIP_CAN_REINVITE | SIP_CAN_REINVITE_NAT);
20596 } else if (!ast_false(v->value)) {
20597 char buf[64];
20598 char *word, *next = buf;
20599
20600 ast_copy_string(buf, v->value, sizeof(buf));
20601 while ((word = strsep(&next, ","))) {
20602 if (!strcasecmp(word, "update")) {
20603 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_CAN_REINVITE);
20604 } else if (!strcasecmp(word, "nonat")) {
20605 ast_set_flag(&flags[0], SIP_CAN_REINVITE);
20606 ast_clear_flag(&flags[0], SIP_CAN_REINVITE_NAT);
20607 } else {
20608 ast_log(LOG_WARNING, "Unknown canreinvite mode '%s' on line %d\n", v->value, v->lineno);
20609 }
20610 }
20611 }
20612 } else if (!strcasecmp(v->name, "insecure")) {
20613 ast_set_flag(&mask[0], SIP_INSECURE);
20614 ast_clear_flag(&flags[0], SIP_INSECURE);
20615 set_insecure_flags(&flags[0], v->value, v->lineno);
20616 } else if (!strcasecmp(v->name, "progressinband")) {
20617 ast_set_flag(&mask[0], SIP_PROG_INBAND);
20618 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
20619 if (ast_true(v->value))
20620 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
20621 else if (strcasecmp(v->value, "never"))
20622 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
20623 } else if (!strcasecmp(v->name, "promiscredir")) {
20624 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
20625 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
20626 } else if (!strcasecmp(v->name, "videosupport")) {
20627 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
20628 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
20629 } else if (!strcasecmp(v->name, "textsupport")) {
20630 ast_set_flag(&mask[1], SIP_PAGE2_TEXTSUPPORT);
20631 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_TEXTSUPPORT);
20632 res = 1;
20633 } else if (!strcasecmp(v->name, "allowoverlap")) {
20634 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
20635 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
20636 } else if (!strcasecmp(v->name, "allowsubscribe")) {
20637 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
20638 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
20639 } else if (!strcasecmp(v->name, "ignoresdpversion")) {
20640 ast_set_flag(&mask[1], SIP_PAGE2_IGNORESDPVERSION);
20641 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_IGNORESDPVERSION);
20642 } else if (!strcasecmp(v->name, "t38pt_udptl")) {
20643 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_UDPTL);
20644 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);
20645 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
20646 } else if (!strcasecmp(v->name, "t38pt_rtp")) {
20647 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_RTP);
20648 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);
20649 } else if (!strcasecmp(v->name, "t38pt_tcp")) {
20650 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_TCP);
20651 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);
20652 #endif
20653 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
20654 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
20655 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
20656 } else if (!strcasecmp(v->name, "buggymwi")) {
20657 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
20658 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
20659 } else if (!strcasecmp(v->name, "t38pt_usertpsource")) {
20660 ast_set_flag(&mask[1], SIP_PAGE2_UDPTL_DESTINATION);
20661 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_UDPTL_DESTINATION);
20662 } else
20663 res = 0;
20664
20665 return res;
20666 }
20667
20668
20669 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
20670 {
20671 struct domain *d;
20672
20673 if (ast_strlen_zero(domain)) {
20674 ast_log(LOG_WARNING, "Zero length domain.\n");
20675 return 1;
20676 }
20677
20678 if (!(d = ast_calloc(1, sizeof(*d))))
20679 return 0;
20680
20681 ast_copy_string(d->domain, domain, sizeof(d->domain));
20682
20683 if (!ast_strlen_zero(context))
20684 ast_copy_string(d->context, context, sizeof(d->context));
20685
20686 d->mode = mode;
20687
20688 AST_LIST_LOCK(&domain_list);
20689 AST_LIST_INSERT_TAIL(&domain_list, d, list);
20690 AST_LIST_UNLOCK(&domain_list);
20691
20692 if (sipdebug)
20693 ast_debug(1, "Added local SIP domain '%s'\n", domain);
20694
20695 return 1;
20696 }
20697
20698
20699 static int check_sip_domain(const char *domain, char *context, size_t len)
20700 {
20701 struct domain *d;
20702 int result = 0;
20703
20704 AST_LIST_LOCK(&domain_list);
20705 AST_LIST_TRAVERSE(&domain_list, d, list) {
20706 if (strcasecmp(d->domain, domain))
20707 continue;
20708
20709 if (len && !ast_strlen_zero(d->context))
20710 ast_copy_string(context, d->context, len);
20711
20712 result = 1;
20713 break;
20714 }
20715 AST_LIST_UNLOCK(&domain_list);
20716
20717 return result;
20718 }
20719
20720
20721 static void clear_sip_domains(void)
20722 {
20723 struct domain *d;
20724
20725 AST_LIST_LOCK(&domain_list);
20726 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
20727 ast_free(d);
20728 AST_LIST_UNLOCK(&domain_list);
20729 }
20730
20731
20732
20733 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, const char *configuration, int lineno)
20734 {
20735 char authcopy[256];
20736 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
20737 char *stringp;
20738 struct sip_auth *a, *b, *auth;
20739
20740 if (ast_strlen_zero(configuration))
20741 return authlist;
20742
20743 ast_debug(1, "Auth config :: %s\n", configuration);
20744
20745 ast_copy_string(authcopy, configuration, sizeof(authcopy));
20746 stringp = authcopy;
20747
20748 username = stringp;
20749 realm = strrchr(stringp, '@');
20750 if (realm)
20751 *realm++ = '\0';
20752 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
20753 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
20754 return authlist;
20755 }
20756 stringp = username;
20757 username = strsep(&stringp, ":");
20758 if (username) {
20759 secret = strsep(&stringp, ":");
20760 if (!secret) {
20761 stringp = username;
20762 md5secret = strsep(&stringp, "#");
20763 }
20764 }
20765 if (!(auth = ast_calloc(1, sizeof(*auth))))
20766 return authlist;
20767
20768 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
20769 ast_copy_string(auth->username, username, sizeof(auth->username));
20770 if (secret)
20771 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
20772 if (md5secret)
20773 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
20774
20775
20776 for (b = NULL, a = authlist; a ; b = a, a = a->next)
20777 ;
20778 if (b)
20779 b->next = auth;
20780 else
20781 authlist = auth;
20782
20783 ast_verb(3, "Added authentication for realm %s\n", realm);
20784
20785 return authlist;
20786
20787 }
20788
20789
20790 static int clear_realm_authentication(struct sip_auth *authlist)
20791 {
20792 struct sip_auth *a = authlist;
20793 struct sip_auth *b;
20794
20795 while (a) {
20796 b = a;
20797 a = a->next;
20798 ast_free(b);
20799 }
20800
20801 return 1;
20802 }
20803
20804
20805 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
20806 {
20807 struct sip_auth *a;
20808
20809 for (a = authlist; a; a = a->next) {
20810 if (!strcasecmp(a->realm, realm))
20811 break;
20812 }
20813
20814 return a;
20815 }
20816
20817
20818
20819
20820 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
20821 {
20822 struct ast_variable *tmpvar = NULL;
20823 char *varname = ast_strdupa(buf), *varval = NULL;
20824
20825 if ((varval = strchr(varname, '='))) {
20826 *varval++ = '\0';
20827 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
20828 tmpvar->next = list;
20829 list = tmpvar;
20830 }
20831 }
20832 return list;
20833 }
20834
20835
20836 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
20837 {
20838 struct sip_user *user;
20839 int format;
20840 struct ast_ha *oldha = NULL;
20841 struct ast_flags userflags[2] = {{(0)}};
20842 struct ast_flags mask[2] = {{(0)}};
20843
20844
20845 if (!(user = ast_calloc(1, sizeof(*user))))
20846 return NULL;
20847
20848 suserobjs++;
20849 ASTOBJ_INIT(user);
20850 ast_copy_string(user->name, name, sizeof(user->name));
20851 oldha = user->ha;
20852 user->ha = NULL;
20853 ast_copy_flags(&user->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
20854 ast_copy_flags(&user->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
20855 user->capability = global_capability;
20856 user->allowtransfer = global_allowtransfer;
20857 user->maxcallbitrate = default_maxcallbitrate;
20858 user->autoframing = global_autoframing;
20859 if (global_callcounter)
20860 user->call_limit=999;
20861 user->prefs = default_prefs;
20862 user->stimer.st_mode_oper = global_st_mode;
20863 user->stimer.st_ref = global_st_refresher;
20864 user->stimer.st_min_se = global_min_se;
20865 user->stimer.st_max_se = global_max_se;
20866
20867
20868 strcpy(user->context, default_context);
20869 strcpy(user->language, default_language);
20870 strcpy(user->mohinterpret, default_mohinterpret);
20871 strcpy(user->mohsuggest, default_mohsuggest);
20872
20873 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
20874 if (handle_common_options(&userflags[0], &mask[0], v))
20875 continue;
20876 if (!strcasecmp(v->name, "context")) {
20877 ast_copy_string(user->context, v->value, sizeof(user->context));
20878 } else if (!strcasecmp(v->name, "subscribecontext")) {
20879 ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
20880 } else if (!strcasecmp(v->name, "setvar")) {
20881 user->chanvars = add_var(v->value, user->chanvars);
20882 } else if (!strcasecmp(v->name, "permit") ||
20883 !strcasecmp(v->name, "deny")) {
20884 int ha_error = 0;
20885
20886 user->ha = ast_append_ha(v->name, v->value, user->ha, &ha_error);
20887 if (ha_error)
20888 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
20889 } else if (!strcasecmp(v->name, "allowtransfer")) {
20890 user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
20891 } else if (!strcasecmp(v->name, "secret")) {
20892 ast_copy_string(user->secret, v->value, sizeof(user->secret));
20893 } else if (!strcasecmp(v->name, "md5secret")) {
20894 ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
20895 } else if (!strcasecmp(v->name, "callerid")) {
20896 ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
20897 } else if (!strcasecmp(v->name, "fullname")) {
20898 ast_copy_string(user->cid_name, v->value, sizeof(user->cid_name));
20899 } else if (!strcasecmp(v->name, "cid_number")) {
20900 ast_copy_string(user->cid_num, v->value, sizeof(user->cid_num));
20901 } else if (!strcasecmp(v->name, "callgroup")) {
20902 user->callgroup = ast_get_group(v->value);
20903 } else if (!strcasecmp(v->name, "pickupgroup")) {
20904 user->pickupgroup = ast_get_group(v->value);
20905 } else if (!strcasecmp(v->name, "language")) {
20906 ast_copy_string(user->language, v->value, sizeof(user->language));
20907 } else if (!strcasecmp(v->name, "mohinterpret")) {
20908 ast_copy_string(user->mohinterpret, v->value, sizeof(user->mohinterpret));
20909 } else if (!strcasecmp(v->name, "mohsuggest")) {
20910 ast_copy_string(user->mohsuggest, v->value, sizeof(user->mohsuggest));
20911 } else if (!strcasecmp(v->name, "accountcode")) {
20912 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
20913 } else if (!strcasecmp(v->name, "callcounter")) {
20914 user->call_limit = ast_true(v->value) ? 999 : 0;
20915 } else if (!strcasecmp(v->name, "call-limit")) {
20916 user->call_limit = atoi(v->value);
20917 if (user->call_limit < 0)
20918 user->call_limit = 0;
20919 } else if (!strcasecmp(v->name, "amaflags")) {
20920 format = ast_cdr_amaflags2int(v->value);
20921 if (format < 0) {
20922 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
20923 } else {
20924 user->amaflags = format;
20925 }
20926 } else if (!strcasecmp(v->name, "allow")) {
20927 int error = ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, TRUE);
20928 if (error)
20929 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
20930 } else if (!strcasecmp(v->name, "disallow")) {
20931 int error = ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, FALSE);
20932 if (error)
20933 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
20934 } else if (!strcasecmp(v->name, "autoframing")) {
20935 user->autoframing = ast_true(v->value);
20936 } else if (!strcasecmp(v->name, "callingpres")) {
20937 user->callingpres = ast_parse_caller_presentation(v->value);
20938 if (user->callingpres == -1)
20939 user->callingpres = atoi(v->value);
20940 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
20941 user->maxcallbitrate = atoi(v->value);
20942 if (user->maxcallbitrate < 0)
20943 user->maxcallbitrate = default_maxcallbitrate;
20944 } else if (!strcasecmp(v->name, "session-timers")) {
20945 int i = (int) str2stmode(v->value);
20946 if (i < 0) {
20947 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
20948 user->stimer.st_mode_oper = global_st_mode;
20949 } else {
20950 user->stimer.st_mode_oper = i;
20951 }
20952 } else if (!strcasecmp(v->name, "session-expires")) {
20953 if (sscanf(v->value, "%d", &user->stimer.st_max_se) != 1) {
20954 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
20955 user->stimer.st_max_se = global_max_se;
20956 }
20957 } else if (!strcasecmp(v->name, "session-minse")) {
20958 if (sscanf(v->value, "%d", &user->stimer.st_min_se) != 1) {
20959 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
20960 user->stimer.st_min_se = global_min_se;
20961 }
20962 if (user->stimer.st_min_se < 90) {
20963 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);
20964 user->stimer.st_min_se = global_min_se;
20965 }
20966 } else if (!strcasecmp(v->name, "session-refresher")) {
20967 int i = (int) str2strefresher(v->value);
20968 if (i < 0) {
20969 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
20970 user->stimer.st_ref = global_st_refresher;
20971 } else {
20972 user->stimer.st_ref = i;
20973 }
20974 }
20975
20976
20977
20978
20979 }
20980 ast_copy_flags(&user->flags[0], &userflags[0], mask[0].flags);
20981 ast_copy_flags(&user->flags[1], &userflags[1], mask[1].flags);
20982 if (ast_test_flag(&user->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
20983 global_allowsubscribe = TRUE;
20984 ast_free_ha(oldha);
20985 return user;
20986 }
20987
20988
20989 static void set_peer_defaults(struct sip_peer *peer)
20990 {
20991 if (peer->expire == 0) {
20992
20993
20994
20995 peer->expire = -1;
20996 peer->pokeexpire = -1;
20997 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
20998 set_socket_transport(&peer->socket, SIP_TRANSPORT_UDP);
20999 }
21000 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
21001 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
21002 strcpy(peer->context, default_context);
21003 strcpy(peer->subscribecontext, default_subscribecontext);
21004 strcpy(peer->language, default_language);
21005 strcpy(peer->mohinterpret, default_mohinterpret);
21006 strcpy(peer->mohsuggest, default_mohsuggest);
21007 peer->addr.sin_family = AF_INET;
21008 peer->defaddr.sin_family = AF_INET;
21009 peer->capability = global_capability;
21010 peer->maxcallbitrate = default_maxcallbitrate;
21011 peer->rtptimeout = global_rtptimeout;
21012 peer->rtpholdtimeout = global_rtpholdtimeout;
21013 peer->rtpkeepalive = global_rtpkeepalive;
21014 peer->allowtransfer = global_allowtransfer;
21015 peer->autoframing = global_autoframing;
21016 peer->qualifyfreq = global_qualifyfreq;
21017 if (global_callcounter)
21018 peer->call_limit=999;
21019 strcpy(peer->vmexten, default_vmexten);
21020 peer->secret[0] = '\0';
21021 peer->md5secret[0] = '\0';
21022 peer->cid_num[0] = '\0';
21023 peer->cid_name[0] = '\0';
21024 peer->fromdomain[0] = '\0';
21025 peer->fromuser[0] = '\0';
21026 peer->regexten[0] = '\0';
21027 peer->callgroup = 0;
21028 peer->pickupgroup = 0;
21029 peer->maxms = default_qualify;
21030 peer->prefs = default_prefs;
21031 peer->stimer.st_mode_oper = global_st_mode;
21032 peer->stimer.st_ref = global_st_refresher;
21033 peer->stimer.st_min_se = global_min_se;
21034 peer->stimer.st_max_se = global_max_se;
21035 peer->timer_t1 = global_t1;
21036 peer->timer_b = global_timer_b;
21037 clear_peer_mailboxes(peer);
21038 }
21039
21040
21041 static struct sip_peer *temp_peer(const char *name)
21042 {
21043 struct sip_peer *peer;
21044
21045 if (!(peer = ast_calloc(1, sizeof(*peer))))
21046 return NULL;
21047
21048 apeerobjs++;
21049 ASTOBJ_INIT(peer);
21050 set_peer_defaults(peer);
21051
21052 ast_copy_string(peer->name, name, sizeof(peer->name));
21053
21054 peer->selfdestruct = TRUE;
21055 peer->host_dynamic = TRUE;
21056 peer->prefs = default_prefs;
21057 reg_source_db(peer);
21058
21059 return peer;
21060 }
21061
21062
21063 static void add_peer_mailboxes(struct sip_peer *peer, const char *value)
21064 {
21065 char *next, *mbox, *context;
21066
21067 next = ast_strdupa(value);
21068
21069 while ((mbox = context = strsep(&next, ","))) {
21070 struct sip_mailbox *mailbox;
21071
21072 if (!(mailbox = ast_calloc(1, sizeof(*mailbox))))
21073 continue;
21074
21075 strsep(&context, "@");
21076 if (ast_strlen_zero(mbox)) {
21077 ast_free(mailbox);
21078 continue;
21079 }
21080 mailbox->mailbox = ast_strdup(mbox);
21081 mailbox->context = ast_strdup(context);
21082
21083 AST_LIST_INSERT_TAIL(&peer->mailboxes, mailbox, entry);
21084 }
21085 }
21086
21087
21088 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
21089 {
21090 struct sip_peer *peer = NULL;
21091 struct ast_ha *oldha = NULL;
21092 int found = 0;
21093 int firstpass = 1;
21094 int format = 0;
21095 time_t regseconds = 0;
21096 struct ast_flags peerflags[2] = {{(0)}};
21097 struct ast_flags mask[2] = {{(0)}};
21098 char callback[256] = "";
21099 const char *srvlookup = NULL;
21100 static int deprecation_warning = 1;
21101 int alt_fullcontact = alt ? 1 : 0;
21102 struct ast_str *fullcontact = ast_str_alloca(512);
21103
21104 if (!realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
21105
21106
21107
21108
21109
21110 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
21111 }
21112
21113 if (peer) {
21114
21115 found++;
21116 if (!(peer->objflags & ASTOBJ_FLAG_MARKED))
21117 firstpass = 0;
21118 } else {
21119 if (!(peer = ast_calloc(1, sizeof(*peer))))
21120 return NULL;
21121
21122 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
21123 rpeerobjs++;
21124 ast_debug(3, "-REALTIME- peer built. Name: %s. Peer objects: %d\n", name, rpeerobjs);
21125 } else
21126 speerobjs++;
21127 ASTOBJ_INIT(peer);
21128 }
21129
21130 if (firstpass) {
21131 peer->lastmsgssent = -1;
21132 oldha = peer->ha;
21133 peer->ha = NULL;
21134 set_peer_defaults(peer);
21135 }
21136 if (!found && name)
21137 ast_copy_string(peer->name, name, sizeof(peer->name));
21138
21139
21140 if (peer->chanvars) {
21141 ast_variables_destroy(peer->chanvars);
21142 peer->chanvars = NULL;
21143
21144 }
21145
21146
21147 clear_realm_authentication(peer->auth);
21148 peer->auth = NULL;
21149 peer->default_outbound_transport = 0;
21150 peer->transports = 0;
21151
21152 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
21153 if (handle_common_options(&peerflags[0], &mask[0], v))
21154 continue;
21155 if (!strcasecmp(v->name, "transport") && !ast_strlen_zero(v->value)) {
21156 char *val = ast_strdupa(v->value);
21157 char *trans;
21158
21159 while ((trans = strsep(&val, ","))) {
21160 trans = ast_skip_blanks(trans);
21161
21162 if (!strncasecmp(trans, "udp", 3))
21163 peer->transports |= SIP_TRANSPORT_UDP;
21164 else if (!strncasecmp(trans, "tcp", 3))
21165 peer->transports |= SIP_TRANSPORT_TCP;
21166 else if (!strncasecmp(trans, "tls", 3))
21167 peer->transports |= SIP_TRANSPORT_TLS;
21168 else
21169 ast_log(LOG_NOTICE, "'%s' is not a valid transport type. if no other is specified, udp will be used.\n", trans);
21170
21171 if (!peer->default_outbound_transport) {
21172 peer->default_outbound_transport = peer->transports;
21173 }
21174 }
21175 } else if (realtime && !strcasecmp(v->name, "regseconds")) {
21176 ast_get_time_t(v->value, ®seconds, 0, NULL);
21177 } else if (realtime && !strcasecmp(v->name, "lastms")) {
21178 sscanf(v->value, "%d", &peer->lastms);
21179 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
21180 inet_aton(v->value, &(peer->addr.sin_addr));
21181 } else if (realtime && !strcasecmp(v->name, "name"))
21182 ast_copy_string(peer->name, v->value, sizeof(peer->name));
21183 else if (realtime && !strcasecmp(v->name, "fullcontact")) {
21184 if (alt_fullcontact && !alt) {
21185
21186
21187
21188
21189
21190 alt_fullcontact = 0;
21191 ast_str_reset(fullcontact);
21192 }
21193
21194 if (fullcontact->used > 0) {
21195 ast_str_append(&fullcontact, 0, ";%s", v->value);
21196 } else {
21197 ast_str_set(&fullcontact, 0, "%s", v->value);
21198 }
21199 } else if (!strcasecmp(v->name, "secret"))
21200 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
21201 else if (!strcasecmp(v->name, "md5secret"))
21202 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
21203 else if (!strcasecmp(v->name, "auth"))
21204 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
21205 else if (!strcasecmp(v->name, "callerid")) {
21206 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
21207 } else if (!strcasecmp(v->name, "fullname")) {
21208 ast_copy_string(peer->cid_name, v->value, sizeof(peer->cid_name));
21209 } else if (!strcasecmp(v->name, "cid_number")) {
21210 ast_copy_string(peer->cid_num, v->value, sizeof(peer->cid_num));
21211 } else if (!strcasecmp(v->name, "context")) {
21212 ast_copy_string(peer->context, v->value, sizeof(peer->context));
21213 } else if (!strcasecmp(v->name, "subscribecontext")) {
21214 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
21215 } else if (!strcasecmp(v->name, "fromdomain")) {
21216 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
21217 } else if (!strcasecmp(v->name, "usereqphone")) {
21218 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
21219 } else if (!strcasecmp(v->name, "fromuser")) {
21220 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
21221 } else if (!strcasecmp(v->name, "outboundproxy")) {
21222 char *port, *next, *force, *proxyname;
21223 int forceopt = FALSE;
21224
21225 next = proxyname = ast_strdupa(v->value);
21226 if ((port = strchr(proxyname, ':'))) {
21227 *port++ = '\0';
21228 next = port;
21229 }
21230 if ((force = strchr(next, ','))) {
21231 *force++ = '\0';
21232 forceopt = strcmp(force, "force");
21233 }
21234
21235 peer->outboundproxy = proxy_allocate(proxyname, port, forceopt);
21236 } else if (!strcasecmp(v->name, "host")) {
21237 if (!strcasecmp(v->value, "dynamic")) {
21238
21239 if (!found || !peer->host_dynamic) {
21240
21241
21242 memset(&peer->addr.sin_addr, 0, 4);
21243 if (peer->addr.sin_port) {
21244
21245 peer->defaddr.sin_port = peer->addr.sin_port;
21246 peer->addr.sin_port = 0;
21247 }
21248 }
21249 peer->host_dynamic = TRUE;
21250 } else {
21251
21252 AST_SCHED_DEL(sched, peer->expire);
21253 peer->host_dynamic = FALSE;
21254 srvlookup = v->value;
21255 if (global_dynamic_exclude_static) {
21256 int err = 0;
21257 global_contact_ha = ast_append_ha("deny", (char *)ast_inet_ntoa(peer->addr.sin_addr), global_contact_ha, &err);
21258 if (err) {
21259 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
21260 }
21261 }
21262 }
21263 } else if (!strcasecmp(v->name, "defaultip")) {
21264 if (ast_get_ip(&peer->defaddr, v->value)) {
21265 unref_peer(peer);
21266 return NULL;
21267 }
21268 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
21269 int ha_error = 0;
21270
21271 peer->ha = ast_append_ha(v->name, v->value, peer->ha, &ha_error);
21272 if (ha_error)
21273 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
21274 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
21275 int ha_error = 0;
21276 peer->contactha = ast_append_ha(v->name + 7, v->value, peer->contactha, &ha_error);
21277 if (ha_error) {
21278 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
21279 }
21280 } else if (!strcasecmp(v->name, "port")) {
21281 if (!realtime && peer->host_dynamic)
21282 peer->defaddr.sin_port = htons(atoi(v->value));
21283 else
21284 peer->addr.sin_port = htons(atoi(v->value));
21285 } else if (!strcasecmp(v->name, "callingpres")) {
21286 peer->callingpres = ast_parse_caller_presentation(v->value);
21287 if (peer->callingpres == -1)
21288 peer->callingpres = atoi(v->value);
21289 } else if (!strcasecmp(v->name, "username") || !strcmp(v->name, "defaultuser")) {
21290 ast_copy_string(peer->username, v->value, sizeof(peer->username));
21291 if (!strcasecmp(v->name, "username")) {
21292 if (deprecation_warning) {
21293 ast_log(LOG_NOTICE, "The 'username' field for sip peers has been deprecated in favor of the term 'defaultuser'\n");
21294 deprecation_warning = 0;
21295 }
21296 peer->deprecated_username = 1;
21297 }
21298 } else if (!strcasecmp(v->name, "language")) {
21299 ast_copy_string(peer->language, v->value, sizeof(peer->language));
21300 } else if (!strcasecmp(v->name, "regexten")) {
21301 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
21302 } else if (!strcasecmp(v->name, "callbackextension")) {
21303 ast_copy_string(callback, v->value, sizeof(callback));
21304 } else if (!strcasecmp(v->name, "callcounter")) {
21305 peer->call_limit = ast_true(v->value) ? 999 : 0;
21306 } else if (!strcasecmp(v->name, "call-limit")) {
21307 peer->call_limit = atoi(v->value);
21308 if (peer->call_limit < 0)
21309 peer->call_limit = 0;
21310 } else if (!strcasecmp(v->name, "busylevel")) {
21311 peer->busy_level = atoi(v->value);
21312 if (peer->busy_level < 0)
21313 peer->busy_level = 0;
21314 } else if (!strcasecmp(v->name, "amaflags")) {
21315 format = ast_cdr_amaflags2int(v->value);
21316 if (format < 0) {
21317 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
21318 } else {
21319 peer->amaflags = format;
21320 }
21321 } else if (!strcasecmp(v->name, "accountcode")) {
21322 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
21323 } else if (!strcasecmp(v->name, "mohinterpret")) {
21324 ast_copy_string(peer->mohinterpret, v->value, sizeof(peer->mohinterpret));
21325 } else if (!strcasecmp(v->name, "mohsuggest")) {
21326 ast_copy_string(peer->mohsuggest, v->value, sizeof(peer->mohsuggest));
21327 } else if (!strcasecmp(v->name, "mailbox")) {
21328 add_peer_mailboxes(peer, v->value);
21329 } else if (!strcasecmp(v->name, "hasvoicemail")) {
21330
21331
21332 if (ast_true(v->value) && AST_LIST_EMPTY(&peer->mailboxes)) {
21333 add_peer_mailboxes(peer, name);
21334 }
21335 } else if (!strcasecmp(v->name, "subscribemwi")) {
21336 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
21337 } else if (!strcasecmp(v->name, "vmexten")) {
21338 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
21339 } else if (!strcasecmp(v->name, "callgroup")) {
21340 peer->callgroup = ast_get_group(v->value);
21341 } else if (!strcasecmp(v->name, "allowtransfer")) {
21342 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
21343 } else if (!strcasecmp(v->name, "pickupgroup")) {
21344 peer->pickupgroup = ast_get_group(v->value);
21345 } else if (!strcasecmp(v->name, "allow")) {
21346 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, TRUE);
21347 if (error)
21348 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
21349 } else if (!strcasecmp(v->name, "disallow")) {
21350 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, FALSE);
21351 if (error)
21352 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
21353 } else if (!strcasecmp(v->name, "registertrying")) {
21354 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_REGISTERTRYING);
21355 } else if (!strcasecmp(v->name, "autoframing")) {
21356 peer->autoframing = ast_true(v->value);
21357 } else if (!strcasecmp(v->name, "rtptimeout")) {
21358 if ((sscanf(v->value, "%d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
21359 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
21360 peer->rtptimeout = global_rtptimeout;
21361 }
21362 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
21363 if ((sscanf(v->value, "%d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
21364 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
21365 peer->rtpholdtimeout = global_rtpholdtimeout;
21366 }
21367 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
21368 if ((sscanf(v->value, "%d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
21369 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
21370 peer->rtpkeepalive = global_rtpkeepalive;
21371 }
21372 } else if (!strcasecmp(v->name, "timert1")) {
21373 if ((sscanf(v->value, "%d", &peer->timer_t1) != 1) || (peer->timer_t1 < 0)) {
21374 ast_log(LOG_WARNING, "'%s' is not a valid T1 time at line %d. Using default.\n", v->value, v->lineno);
21375 peer->timer_t1 = global_t1;
21376 }
21377
21378
21379 if (peer->timer_b < peer->timer_t1 * 64) {
21380 peer->timer_b = peer->timer_t1 * 64;
21381 }
21382 } else if (!strcasecmp(v->name, "timerb")) {
21383 if ((sscanf(v->value, "%d", &peer->timer_b) != 1) || (peer->timer_b < 0)) {
21384 ast_log(LOG_WARNING, "'%s' is not a valid Timer B time at line %d. Using default.\n", v->value, v->lineno);
21385 peer->timer_b = global_timer_b;
21386 }
21387 if (peer->timer_b < peer->timer_t1 * 64) {
21388 static int warning = 0;
21389 if (warning++ % 20 == 0) {
21390 ast_log(LOG_WARNING, "Timer B has been set lower than recommended. (RFC 3261, 17.1.1.2)\n");
21391 }
21392 }
21393 } else if (!strcasecmp(v->name, "setvar")) {
21394 peer->chanvars = add_var(v->value, peer->chanvars);
21395 } else if (!strcasecmp(v->name, "qualify")) {
21396 if (!strcasecmp(v->value, "no")) {
21397 peer->maxms = 0;
21398 } else if (!strcasecmp(v->value, "yes")) {
21399 peer->maxms = default_qualify ? default_qualify : DEFAULT_MAXMS;
21400 } else if (sscanf(v->value, "%d", &peer->maxms) != 1) {
21401 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);
21402 peer->maxms = 0;
21403 }
21404 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && peer->maxms > 0) {
21405
21406
21407
21408
21409 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);
21410 peer->maxms = 0;
21411 }
21412 } else if (!strcasecmp(v->name, "qualifyfreq")) {
21413 int i;
21414 if (sscanf(v->value, "%d", &i) == 1)
21415 peer->qualifyfreq = i * 1000;
21416 else {
21417 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
21418 peer->qualifyfreq = global_qualifyfreq;
21419 }
21420 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
21421 peer->maxcallbitrate = atoi(v->value);
21422 if (peer->maxcallbitrate < 0)
21423 peer->maxcallbitrate = default_maxcallbitrate;
21424 } else if (!strcasecmp(v->name, "session-timers")) {
21425 int i = (int) str2stmode(v->value);
21426 if (i < 0) {
21427 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
21428 peer->stimer.st_mode_oper = global_st_mode;
21429 } else {
21430 peer->stimer.st_mode_oper = i;
21431 }
21432 } else if (!strcasecmp(v->name, "session-expires")) {
21433 if (sscanf(v->value, "%d", &peer->stimer.st_max_se) != 1) {
21434 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
21435 peer->stimer.st_max_se = global_max_se;
21436 }
21437 } else if (!strcasecmp(v->name, "session-minse")) {
21438 if (sscanf(v->value, "%d", &peer->stimer.st_min_se) != 1) {
21439 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
21440 peer->stimer.st_min_se = global_min_se;
21441 }
21442 if (peer->stimer.st_min_se < 90) {
21443 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);
21444 peer->stimer.st_min_se = global_min_se;
21445 }
21446 } else if (!strcasecmp(v->name, "session-refresher")) {
21447 int i = (int) str2strefresher(v->value);
21448 if (i < 0) {
21449 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
21450 peer->stimer.st_ref = global_st_refresher;
21451 } else {
21452 peer->stimer.st_ref = i;
21453 }
21454 }
21455 }
21456
21457 if (!peer->default_outbound_transport) {
21458 peer->transports = SIP_TRANSPORT_UDP;
21459 peer->default_outbound_transport = SIP_TRANSPORT_UDP;
21460 }
21461
21462
21463
21464
21465
21466 if (((peer->socket.type != peer->default_outbound_transport) && (peer->expire == -1)) ||
21467 !(peer->socket.type & peer->transports) || !(peer->socket.type)) {
21468
21469 set_socket_transport(&peer->socket, peer->default_outbound_transport);
21470 }
21471
21472 if (fullcontact->used > 0) {
21473 ast_copy_string(peer->fullcontact, fullcontact->str, sizeof(peer->fullcontact));
21474 peer->rt_fromcontact = TRUE;
21475
21476
21477
21478
21479
21480
21481
21482
21483 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE) || !peer->addr.sin_addr.s_addr) {
21484 __set_address_from_contact(fullcontact->str, &peer->addr, 0);
21485 }
21486 }
21487
21488 if (srvlookup) {
21489 if (ast_get_ip_or_srv(&peer->addr, srvlookup,
21490 global_srvlookup ?
21491 ((peer->socket.type & SIP_TRANSPORT_UDP) ? "_sip._udp" :
21492 (peer->socket.type & SIP_TRANSPORT_TCP) ? "_sip._tcp" :
21493 "_sip._tls")
21494 : NULL)) {
21495 unref_peer(peer);
21496 return NULL;
21497 }
21498
21499 ast_copy_string(peer->tohost, srvlookup, sizeof(peer->tohost));
21500 }
21501
21502 if (!peer->addr.sin_port)
21503 peer->addr.sin_port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
21504
21505 if (!peer->socket.port)
21506 peer->socket.port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
21507
21508 if (!sip_cfg.ignore_regexpire && peer->host_dynamic && realtime) {
21509 time_t nowtime = time(NULL);
21510
21511 if ((nowtime - regseconds) > 0) {
21512 destroy_association(peer);
21513 memset(&peer->addr, 0, sizeof(peer->addr));
21514 peer->lastms = -1;
21515 ast_debug(1, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
21516 }
21517 }
21518
21519
21520 if (realtime && peer->lastms > 0) {
21521 ASTOBJ_REF(peer);
21522 sip_poke_peer(peer);
21523 }
21524
21525 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
21526 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
21527 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
21528 global_allowsubscribe = TRUE;
21529 if (!found && peer->host_dynamic && !peer->is_realtime)
21530 reg_source_db(peer);
21531
21532
21533
21534 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
21535 !AST_LIST_EMPTY(&peer->mailboxes)) {
21536 add_peer_mwi_subs(peer);
21537
21538
21539
21540 sip_send_mwi_to_peer(peer, NULL, 1);
21541 }
21542
21543 ASTOBJ_UNMARK(peer);
21544
21545 ast_free_ha(oldha);
21546 if (!ast_strlen_zero(callback)) {
21547 char *reg_string;
21548
21549 if (asprintf(®_string, "%s:%s@%s/%s", peer->username, peer->secret, peer->tohost, callback) < 0) {
21550 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
21551 } else if (reg_string) {
21552 sip_register(reg_string, 0);
21553 ast_free(reg_string);
21554 }
21555 }
21556 return peer;
21557 }
21558
21559
21560
21561
21562
21563
21564
21565 static int reload_config(enum channelreloadreason reason)
21566 {
21567 struct ast_config *cfg, *ucfg;
21568 struct ast_variable *v;
21569 struct sip_peer *peer;
21570 struct sip_user *user;
21571 char *cat, *stringp, *context, *oldregcontext;
21572 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
21573 struct ast_flags dummy[2];
21574 struct ast_flags config_flags = { reason == CHANNEL_MODULE_LOAD ? 0 : CONFIG_FLAG_FILEUNCHANGED };
21575 int auto_sip_domains = FALSE;
21576 struct sockaddr_in old_bindaddr = bindaddr;
21577 int registry_count = 0, peer_count = 0, user_count = 0;
21578
21579 cfg = ast_config_load(config, config_flags);
21580
21581
21582 if (!cfg) {
21583 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
21584 return -1;
21585 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
21586 ucfg = ast_config_load("users.conf", config_flags);
21587 if (ucfg == CONFIG_STATUS_FILEUNCHANGED)
21588 return 1;
21589
21590 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
21591 cfg = ast_config_load(config, config_flags);
21592 } else {
21593 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
21594 ucfg = ast_config_load("users.conf", config_flags);
21595 }
21596
21597
21598 memset(&sip_tcp_desc.sin, 0, sizeof(sip_tcp_desc.sin));
21599 memset(&sip_tls_desc.sin, 0, sizeof(sip_tls_desc.sin));
21600
21601 ast_free_ha(global_contact_ha);
21602 global_contact_ha = NULL;
21603
21604 default_tls_cfg.enabled = FALSE;
21605
21606 sip_tcp_desc.sin.sin_port = htons(STANDARD_SIP_PORT);
21607 sip_tls_desc.sin.sin_port = htons(STANDARD_TLS_PORT);
21608
21609 if (reason != CHANNEL_MODULE_LOAD) {
21610 ast_debug(4, "--------------- SIP reload started\n");
21611
21612 clear_realm_authentication(authl);
21613 clear_sip_domains();
21614 authl = NULL;
21615
21616
21617
21618 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
21619 ASTOBJ_RDLOCK(iterator);
21620 if (iterator->call) {
21621 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
21622
21623 iterator->call = sip_destroy(iterator->call);
21624 }
21625 ASTOBJ_UNLOCK(iterator);
21626
21627 } while(0));
21628
21629
21630 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
21631 ast_debug(4, "--------------- Done destroying user list\n");
21632 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
21633 ast_debug(4, "--------------- Done destroying registry list\n");
21634 ASTOBJ_CONTAINER_MARKALL(&peerl);
21635 }
21636
21637
21638 default_tls_cfg.certfile = ast_strdup(AST_CERTFILE);
21639 default_tls_cfg.cipher = ast_strdup("");
21640 default_tls_cfg.cafile = ast_strdup("");
21641 default_tls_cfg.capath = ast_strdup("");
21642
21643
21644 ast_copy_string(oldcontexts, global_regcontext, sizeof(oldcontexts));
21645 oldregcontext = oldcontexts;
21646
21647
21648
21649 sipdebug &= sip_debug_console;
21650 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
21651 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
21652
21653
21654 memset(&bindaddr, 0, sizeof(bindaddr));
21655 memset(&stunaddr, 0, sizeof(stunaddr));
21656 memset(&internip, 0, sizeof(internip));
21657
21658
21659 ast_free_ha(localaddr);
21660 memset(&localaddr, 0, sizeof(localaddr));
21661 memset(&externip, 0, sizeof(externip));
21662 memset(&default_prefs, 0 , sizeof(default_prefs));
21663 memset(&global_outboundproxy, 0, sizeof(struct sip_proxy));
21664 global_outboundproxy.ip.sin_port = htons(STANDARD_SIP_PORT);
21665 global_outboundproxy.ip.sin_family = AF_INET;
21666 global_outboundproxy.force = FALSE;
21667 ourport_tcp = STANDARD_SIP_PORT;
21668 ourport_tls = STANDARD_TLS_PORT;
21669 bindaddr.sin_port = htons(STANDARD_SIP_PORT);
21670 global_srvlookup = DEFAULT_SRVLOOKUP;
21671 global_tos_sip = DEFAULT_TOS_SIP;
21672 global_tos_audio = DEFAULT_TOS_AUDIO;
21673 global_tos_video = DEFAULT_TOS_VIDEO;
21674 global_tos_text = DEFAULT_TOS_TEXT;
21675 global_cos_sip = DEFAULT_COS_SIP;
21676 global_cos_audio = DEFAULT_COS_AUDIO;
21677 global_cos_video = DEFAULT_COS_VIDEO;
21678 global_cos_text = DEFAULT_COS_TEXT;
21679
21680 externhost[0] = '\0';
21681 externexpire = 0;
21682 externrefresh = 10;
21683
21684
21685 allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
21686 global_regcontext[0] = '\0';
21687 global_regextenonqualify = DEFAULT_REGEXTENONQUALIFY;
21688 global_notifyringing = DEFAULT_NOTIFYRINGING;
21689 global_limitonpeers = FALSE;
21690 global_notifyhold = FALSE;
21691 global_directrtpsetup = FALSE;
21692 global_alwaysauthreject = 0;
21693 global_allowsubscribe = FALSE;
21694 snprintf(global_useragent, sizeof(global_useragent), "%s %s", DEFAULT_USERAGENT, ast_get_version());
21695 snprintf(global_sdpsession, sizeof(global_sdpsession), "%s %s", DEFAULT_SDPSESSION, ast_get_version());
21696 snprintf(global_sdpowner, sizeof(global_sdpowner), "%s", DEFAULT_SDPOWNER);
21697 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
21698 ast_copy_string(global_realm, S_OR(ast_config_AST_SYSTEM_NAME, DEFAULT_REALM), sizeof(global_realm));
21699 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
21700 compactheaders = DEFAULT_COMPACTHEADERS;
21701 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
21702 global_regattempts_max = 0;
21703 pedanticsipchecking = DEFAULT_PEDANTIC;
21704 autocreatepeer = DEFAULT_AUTOCREATEPEER;
21705 global_autoframing = 0;
21706 global_allowguest = DEFAULT_ALLOWGUEST;
21707 global_callcounter = DEFAULT_CALLCOUNTER;
21708 global_match_auth_username = FALSE;
21709 global_rtptimeout = 0;
21710 global_rtpholdtimeout = 0;
21711 global_rtpkeepalive = 0;
21712 global_allowtransfer = TRANSFER_OPENFORALL;
21713 global_rtautoclear = 120;
21714 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
21715 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
21716 sip_cfg.peer_rtupdate = TRUE;
21717
21718
21719 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
21720 global_st_refresher = SESSION_TIMER_REFRESHER_UAS;
21721 global_min_se = DEFAULT_MIN_SE;
21722 global_max_se = DEFAULT_MAX_SE;
21723
21724
21725 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
21726 default_subscribecontext[0] = '\0';
21727 default_language[0] = '\0';
21728 default_fromdomain[0] = '\0';
21729 default_qualify = DEFAULT_QUALIFY;
21730 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
21731 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
21732 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
21733 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
21734 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
21735 ast_set_flag(&global_flags[0], SIP_NAT_RFC3581);
21736 ast_set_flag(&global_flags[0], SIP_CAN_REINVITE);
21737
21738
21739 dumphistory = FALSE;
21740 recordhistory = FALSE;
21741 sipdebug &= ~sip_debug_config;
21742
21743
21744 global_relaxdtmf = FALSE;
21745 global_callevents = FALSE;
21746 global_t1 = SIP_TIMER_T1;
21747 global_timer_b = 64 * SIP_TIMER_T1;
21748 global_t1min = DEFAULT_T1MIN;
21749 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
21750
21751 global_matchexterniplocally = FALSE;
21752
21753
21754 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
21755
21756 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT);
21757 ast_clear_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT);
21758 ast_clear_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION);
21759
21760
21761 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
21762 if (handle_common_options(&global_flags[0], &dummy[0], v))
21763 continue;
21764
21765 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
21766 continue;
21767
21768 if (!strcasecmp(v->name, "context")) {
21769 ast_copy_string(default_context, v->value, sizeof(default_context));
21770 } else if (!strcasecmp(v->name, "subscribecontext")) {
21771 ast_copy_string(default_subscribecontext, v->value, sizeof(default_subscribecontext));
21772 } else if (!strcasecmp(v->name, "callcounter")) {
21773 global_callcounter = ast_true(v->value) ? 1 : 0;
21774 } else if (!strcasecmp(v->name, "allowguest")) {
21775 global_allowguest = ast_true(v->value) ? 1 : 0;
21776 } else if (!strcasecmp(v->name, "realm")) {
21777 ast_copy_string(global_realm, v->value, sizeof(global_realm));
21778 } else if (!strcasecmp(v->name, "useragent")) {
21779 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
21780 ast_debug(1, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
21781 } else if (!strcasecmp(v->name, "sdpsession")) {
21782 ast_copy_string(global_sdpsession, v->value, sizeof(global_sdpsession));
21783 } else if (!strcasecmp(v->name, "sdpowner")) {
21784
21785 if (!strstr(v->value, " "))
21786 ast_copy_string(global_sdpowner, v->value, sizeof(global_sdpowner));
21787 else
21788 ast_log(LOG_WARNING, "'%s' must not contain spaces at line %d. Using default.\n", v->value, v->lineno);
21789 } else if (!strcasecmp(v->name, "allowtransfer")) {
21790 global_allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
21791 } else if (!strcasecmp(v->name, "rtcachefriends")) {
21792 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
21793 } else if (!strcasecmp(v->name, "rtsavesysname")) {
21794 sip_cfg.rtsave_sysname = ast_true(v->value);
21795 } else if (!strcasecmp(v->name, "rtupdate")) {
21796 sip_cfg.peer_rtupdate = ast_true(v->value);
21797 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
21798 sip_cfg.ignore_regexpire = ast_true(v->value);
21799 } else if (!strcasecmp(v->name, "timert1")) {
21800
21801
21802
21803 global_t1 = atoi(v->value);
21804
21805 global_timer_b = global_t1 * 64;
21806 } else if (!strcasecmp(v->name, "t1min")) {
21807 global_t1min = atoi(v->value);
21808 } else if (!strcasecmp(v->name, "tcpenable")) {
21809 sip_tcp_desc.sin.sin_family = ast_false(v->value) ? 0 : AF_INET;
21810 ast_debug(2, "Enabling TCP socket for listening\n");
21811 } else if (!strcasecmp(v->name, "tcpbindaddr")) {
21812 int family = sip_tcp_desc.sin.sin_family;
21813 if (ast_parse_arg(v->value, PARSE_INADDR, &sip_tcp_desc.sin))
21814 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n", v->name, v->value, v->lineno, config);
21815 sip_tcp_desc.sin.sin_family = family;
21816 ast_debug(2, "Setting TCP socket address to %s\n", v->value);
21817 } else if (!strcasecmp(v->name, "tlsenable")) {
21818 default_tls_cfg.enabled = ast_true(v->value) ? TRUE : FALSE;
21819 sip_tls_desc.sin.sin_family = AF_INET;
21820 } else if (!strcasecmp(v->name, "tlscertfile")) {
21821 ast_free(default_tls_cfg.certfile);
21822 default_tls_cfg.certfile = ast_strdup(v->value);
21823 } else if (!strcasecmp(v->name, "tlscipher")) {
21824 ast_free(default_tls_cfg.cipher);
21825 default_tls_cfg.cipher = ast_strdup(v->value);
21826 } else if (!strcasecmp(v->name, "tlscafile")) {
21827 ast_free(default_tls_cfg.cafile);
21828 default_tls_cfg.cafile = ast_strdup(v->value);
21829 } else if (!strcasecmp(v->name, "tlscapath")) {
21830 ast_free(default_tls_cfg.capath);
21831 default_tls_cfg.capath = ast_strdup(v->value);
21832 } else if (!strcasecmp(v->name, "tlsverifyclient")) {
21833 ast_set2_flag(&default_tls_cfg.flags, ast_true(v->value), AST_SSL_VERIFY_CLIENT);
21834 } else if (!strcasecmp(v->name, "tlsdontverifyserver")) {
21835 ast_set2_flag(&default_tls_cfg.flags, ast_true(v->value), AST_SSL_DONT_VERIFY_SERVER);
21836 } else if (!strcasecmp(v->name, "tlsbindaddr")) {
21837 if (ast_parse_arg(v->value, PARSE_INADDR, &sip_tls_desc.sin))
21838 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n", v->name, v->value, v->lineno, config);
21839 } else if (!strcasecmp(v->name, "dynamic_exclude_static") || !strcasecmp(v->name, "dynamic_excludes_static")) {
21840 global_dynamic_exclude_static = ast_true(v->value);
21841 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
21842 int ha_error = 0;
21843 global_contact_ha = ast_append_ha(v->name + 7, v->value, global_contact_ha, &ha_error);
21844 if (ha_error) {
21845 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
21846 }
21847 } else if (!strcasecmp(v->name, "rtautoclear")) {
21848 int i = atoi(v->value);
21849 if (i > 0)
21850 global_rtautoclear = i;
21851 else
21852 i = 0;
21853 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
21854 } else if (!strcasecmp(v->name, "usereqphone")) {
21855 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
21856 } else if (!strcasecmp(v->name, "relaxdtmf")) {
21857 global_relaxdtmf = ast_true(v->value);
21858 } else if (!strcasecmp(v->name, "vmexten")) {
21859 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
21860 } else if (!strcasecmp(v->name, "rtptimeout")) {
21861 if ((sscanf(v->value, "%d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
21862 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
21863 global_rtptimeout = 0;
21864 }
21865 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
21866 if ((sscanf(v->value, "%d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
21867 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
21868 global_rtpholdtimeout = 0;
21869 }
21870 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
21871 if ((sscanf(v->value, "%d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
21872 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
21873 global_rtpkeepalive = 0;
21874 }
21875 } else if (!strcasecmp(v->name, "compactheaders")) {
21876 compactheaders = ast_true(v->value);
21877 } else if (!strcasecmp(v->name, "notifymimetype")) {
21878 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
21879 } else if (!strncasecmp(v->name, "limitonpeer", 11) || !strcasecmp(v->name, "counteronpeer")) {
21880 global_limitonpeers = ast_true(v->value);
21881 } else if (!strcasecmp(v->name, "directrtpsetup")) {
21882 global_directrtpsetup = ast_true(v->value);
21883 } else if (!strcasecmp(v->name, "notifyringing")) {
21884 global_notifyringing = ast_true(v->value);
21885 } else if (!strcasecmp(v->name, "notifyhold")) {
21886 global_notifyhold = ast_true(v->value);
21887 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
21888 global_alwaysauthreject = ast_true(v->value);
21889 } else if (!strcasecmp(v->name, "mohinterpret")) {
21890 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
21891 } else if (!strcasecmp(v->name, "mohsuggest")) {
21892 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
21893 } else if (!strcasecmp(v->name, "language")) {
21894 ast_copy_string(default_language, v->value, sizeof(default_language));
21895 } else if (!strcasecmp(v->name, "regcontext")) {
21896 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
21897 stringp = newcontexts;
21898
21899 cleanup_stale_contexts(stringp, oldregcontext);
21900
21901 while ((context = strsep(&stringp, "&"))) {
21902 ast_copy_string(used_context, context, sizeof(used_context));
21903 ast_context_find_or_create(NULL, NULL, context, "SIP");
21904 }
21905 ast_copy_string(global_regcontext, v->value, sizeof(global_regcontext));
21906 } else if (!strcasecmp(v->name, "regextenonqualify")) {
21907 global_regextenonqualify = ast_true(v->value);
21908 } else if (!strcasecmp(v->name, "callerid")) {
21909 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
21910 } else if (!strcasecmp(v->name, "fromdomain")) {
21911 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
21912 } else if (!strcasecmp(v->name, "outboundproxy")) {
21913 int portnum;
21914 char *tok, *proxyname;
21915
21916 if (ast_strlen_zero(v->value)) {
21917 ast_log(LOG_WARNING, "no value given for outbound proxy on line %d of sip.conf.", v->lineno);
21918 continue;
21919 }
21920
21921 tok = ast_skip_blanks(strtok(ast_strdupa(v->value), ","));
21922
21923 sip_parse_host(tok, v->lineno, &proxyname, &portnum, &global_outboundproxy.transport);
21924
21925 global_outboundproxy.ip.sin_port = htons(portnum);
21926
21927 if ((tok = strtok(NULL, ","))) {
21928 global_outboundproxy.force = !strncasecmp(ast_skip_blanks(tok), "force", 5);
21929 } else {
21930 global_outboundproxy.force = FALSE;
21931 }
21932
21933 if (ast_strlen_zero(proxyname)) {
21934 ast_log(LOG_WARNING, "you must specify a name for the outboundproxy on line %d of sip.conf.", v->lineno);
21935 global_outboundproxy.name[0] = '\0';
21936 continue;
21937 }
21938
21939 ast_copy_string(global_outboundproxy.name, proxyname, sizeof(global_outboundproxy.name));
21940
21941 proxy_update(&global_outboundproxy);
21942 } else if (!strcasecmp(v->name, "autocreatepeer")) {
21943 autocreatepeer = ast_true(v->value);
21944 } else if (!strcasecmp(v->name, "match_auth_username")) {
21945 global_match_auth_username = ast_true(v->value);
21946 } else if (!strcasecmp(v->name, "srvlookup")) {
21947 global_srvlookup = ast_true(v->value);
21948 } else if (!strcasecmp(v->name, "pedantic")) {
21949 pedanticsipchecking = ast_true(v->value);
21950 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
21951 max_expiry = atoi(v->value);
21952 if (max_expiry < 1)
21953 max_expiry = DEFAULT_MAX_EXPIRY;
21954 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
21955 min_expiry = atoi(v->value);
21956 if (min_expiry < 1)
21957 min_expiry = DEFAULT_MIN_EXPIRY;
21958 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
21959 default_expiry = atoi(v->value);
21960 if (default_expiry < 1)
21961 default_expiry = DEFAULT_DEFAULT_EXPIRY;
21962 } else if (!strcasecmp(v->name, "sipdebug")) {
21963 if (ast_true(v->value))
21964 sipdebug |= sip_debug_config;
21965 } else if (!strcasecmp(v->name, "dumphistory")) {
21966 dumphistory = ast_true(v->value);
21967 } else if (!strcasecmp(v->name, "recordhistory")) {
21968 recordhistory = ast_true(v->value);
21969 } else if (!strcasecmp(v->name, "registertimeout")) {
21970 global_reg_timeout = atoi(v->value);
21971 if (global_reg_timeout < 1)
21972 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
21973 } else if (!strcasecmp(v->name, "registerattempts")) {
21974 global_regattempts_max = atoi(v->value);
21975 } else if (!strcasecmp(v->name, "stunaddr")) {
21976 stunaddr.sin_port = htons(3478);
21977 if (ast_parse_arg(v->value, PARSE_INADDR, &stunaddr))
21978 ast_log(LOG_WARNING, "Invalid STUN server address: %s\n", v->value);
21979 externexpire = time(NULL);
21980 } else if (!strcasecmp(v->name, "bindaddr")) {
21981 if (ast_parse_arg(v->value, PARSE_INADDR, &bindaddr))
21982 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
21983 } else if (!strcasecmp(v->name, "localnet")) {
21984 struct ast_ha *na;
21985 int ha_error = 0;
21986
21987 if (!(na = ast_append_ha("d", v->value, localaddr, &ha_error)))
21988 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
21989 else
21990 localaddr = na;
21991 if (ha_error)
21992 ast_log(LOG_ERROR, "Bad localnet configuration value line %d : %s\n", v->lineno, v->value);
21993 } else if (!strcasecmp(v->name, "externip")) {
21994 if (ast_parse_arg(v->value, PARSE_INADDR, &externip))
21995 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
21996 externexpire = 0;
21997
21998 if (!externip.sin_port)
21999 externip.sin_port = bindaddr.sin_port;
22000 } else if (!strcasecmp(v->name, "externhost")) {
22001 ast_copy_string(externhost, v->value, sizeof(externhost));
22002 if (ast_parse_arg(externhost, PARSE_INADDR, &externip))
22003 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
22004 externexpire = time(NULL);
22005
22006 if (!externip.sin_port)
22007 externip.sin_port = bindaddr.sin_port;
22008 } else if (!strcasecmp(v->name, "externrefresh")) {
22009 if (sscanf(v->value, "%d", &externrefresh) != 1) {
22010 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
22011 externrefresh = 10;
22012 }
22013 } else if (!strcasecmp(v->name, "allow")) {
22014 int error = ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, TRUE);
22015 if (error)
22016 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
22017 } else if (!strcasecmp(v->name, "disallow")) {
22018 int error = ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, FALSE);
22019 if (error)
22020 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
22021 } else if (!strcasecmp(v->name, "autoframing")) {
22022 global_autoframing = ast_true(v->value);
22023 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
22024 allow_external_domains = ast_true(v->value);
22025 } else if (!strcasecmp(v->name, "autodomain")) {
22026 auto_sip_domains = ast_true(v->value);
22027 } else if (!strcasecmp(v->name, "domain")) {
22028 char *domain = ast_strdupa(v->value);
22029 char *context = strchr(domain, ',');
22030
22031 if (context)
22032 *context++ = '\0';
22033
22034 if (ast_strlen_zero(context))
22035 ast_debug(1, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
22036 if (ast_strlen_zero(domain))
22037 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
22038 else
22039 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, context ? ast_strip(context) : "");
22040 } else if (!strcasecmp(v->name, "register")) {
22041 if (sip_register(v->value, v->lineno) == 0)
22042 registry_count++;
22043 } else if (!strcasecmp(v->name, "tos_sip")) {
22044 if (ast_str2tos(v->value, &global_tos_sip))
22045 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, refer to QoS documentation\n", v->lineno);
22046 } else if (!strcasecmp(v->name, "tos_audio")) {
22047 if (ast_str2tos(v->value, &global_tos_audio))
22048 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
22049 } else if (!strcasecmp(v->name, "tos_video")) {
22050 if (ast_str2tos(v->value, &global_tos_video))
22051 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, refer to QoS documentation\n", v->lineno);
22052 } else if (!strcasecmp(v->name, "tos_text")) {
22053 if (ast_str2tos(v->value, &global_tos_text))
22054 ast_log(LOG_WARNING, "Invalid tos_text value at line %d, refer to QoS documentation\n", v->lineno);
22055 } else if (!strcasecmp(v->name, "cos_sip")) {
22056 if (ast_str2cos(v->value, &global_cos_sip))
22057 ast_log(LOG_WARNING, "Invalid cos_sip value at line %d, refer to QoS documentation\n", v->lineno);
22058 } else if (!strcasecmp(v->name, "cos_audio")) {
22059 if (ast_str2cos(v->value, &global_cos_audio))
22060 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
22061 } else if (!strcasecmp(v->name, "cos_video")) {
22062 if (ast_str2cos(v->value, &global_cos_video))
22063 ast_log(LOG_WARNING, "Invalid cos_video value at line %d, refer to QoS documentation\n", v->lineno);
22064 } else if (!strcasecmp(v->name, "cos_text")) {
22065 if (ast_str2cos(v->value, &global_cos_text))
22066 ast_log(LOG_WARNING, "Invalid cos_text value at line %d, refer to QoS documentation\n", v->lineno);
22067 } else if (!strcasecmp(v->name, "bindport")) {
22068 int i;
22069 if (sscanf(v->value, "%d", &i) == 1) {
22070 bindaddr.sin_port = htons(i);
22071 } else {
22072 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
22073 }
22074 } else if (!strcasecmp(v->name, "qualify")) {
22075 if (!strcasecmp(v->value, "no")) {
22076 default_qualify = 0;
22077 } else if (!strcasecmp(v->value, "yes")) {
22078 default_qualify = DEFAULT_MAXMS;
22079 } else if (sscanf(v->value, "%d", &default_qualify) != 1) {
22080 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
22081 default_qualify = 0;
22082 }
22083 } else if (!strcasecmp(v->name, "qualifyfreq")) {
22084 int i;
22085 if (sscanf(v->value, "%d", &i) == 1)
22086 global_qualifyfreq = i * 1000;
22087 else {
22088 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
22089 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
22090 }
22091 } else if (!strcasecmp(v->name, "callevents")) {
22092 global_callevents = ast_true(v->value);
22093 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
22094 default_maxcallbitrate = atoi(v->value);
22095 if (default_maxcallbitrate < 0)
22096 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
22097 } else if (!strcasecmp(v->name, "matchexterniplocally")) {
22098 global_matchexterniplocally = ast_true(v->value);
22099 } else if (!strcasecmp(v->name, "session-timers")) {
22100 int i = (int) str2stmode(v->value);
22101 if (i < 0) {
22102 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
22103 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
22104 } else {
22105 global_st_mode = i;
22106 }
22107 } else if (!strcasecmp(v->name, "session-expires")) {
22108 if (sscanf(v->value, "%d", &global_max_se) != 1) {
22109 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
22110 global_max_se = DEFAULT_MAX_SE;
22111 }
22112 } else if (!strcasecmp(v->name, "session-minse")) {
22113 if (sscanf(v->value, "%d", &global_min_se) != 1) {
22114 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
22115 global_min_se = DEFAULT_MIN_SE;
22116 }
22117 if (global_min_se < 90) {
22118 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);
22119 global_min_se = DEFAULT_MIN_SE;
22120 }
22121 } else if (!strcasecmp(v->name, "session-refresher")) {
22122 int i = (int) str2strefresher(v->value);
22123 if (i < 0) {
22124 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
22125 global_st_refresher = SESSION_TIMER_REFRESHER_UAS;
22126 } else {
22127 global_st_refresher = i;
22128 }
22129 }
22130 }
22131
22132 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
22133 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
22134 allow_external_domains = 1;
22135 }
22136
22137
22138 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
22139
22140 if (!strcasecmp(v->name, "auth"))
22141 authl = add_realm_authentication(authl, v->value, v->lineno);
22142 }
22143
22144 if (ucfg) {
22145 struct ast_variable *gen;
22146 int genhassip, genregistersip;
22147 const char *hassip, *registersip;
22148
22149 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
22150 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
22151 gen = ast_variable_browse(ucfg, "general");
22152 cat = ast_category_browse(ucfg, NULL);
22153 while (cat) {
22154 if (strcasecmp(cat, "general")) {
22155 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
22156 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
22157 if (ast_true(hassip) || (!hassip && genhassip)) {
22158 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
22159 if (user) {
22160 ASTOBJ_CONTAINER_LINK(&userl,user);
22161 ASTOBJ_UNREF(user, sip_destroy_user);
22162 user_count++;
22163 }
22164 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
22165 if (peer) {
22166 ASTOBJ_CONTAINER_LINK(&peerl, peer);
22167 unref_peer(peer);
22168 peer_count++;
22169 }
22170 }
22171 if (ast_true(registersip) || (!registersip && genregistersip)) {
22172 char tmp[256];
22173 const char *host = ast_variable_retrieve(ucfg, cat, "host");
22174 const char *username = ast_variable_retrieve(ucfg, cat, "username");
22175 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
22176 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
22177 if (!host)
22178 host = ast_variable_retrieve(ucfg, "general", "host");
22179 if (!username)
22180 username = ast_variable_retrieve(ucfg, "general", "username");
22181 if (!secret)
22182 secret = ast_variable_retrieve(ucfg, "general", "secret");
22183 if (!contact)
22184 contact = "s";
22185 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
22186 if (!ast_strlen_zero(secret))
22187 snprintf(tmp, sizeof(tmp), "%s:%s@%s/%s", username, secret, host, contact);
22188 else
22189 snprintf(tmp, sizeof(tmp), "%s@%s/%s", username, host, contact);
22190 if (sip_register(tmp, 0) == 0)
22191 registry_count++;
22192 }
22193 }
22194 }
22195 cat = ast_category_browse(ucfg, cat);
22196 }
22197 ast_config_destroy(ucfg);
22198 }
22199
22200
22201
22202 cat = NULL;
22203 while ( (cat = ast_category_browse(cfg, cat)) ) {
22204 const char *utype;
22205 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
22206 continue;
22207 utype = ast_variable_retrieve(cfg, cat, "type");
22208 if (!utype) {
22209 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
22210 continue;
22211 } else {
22212 int is_user = 0, is_peer = 0;
22213 if (!strcasecmp(utype, "user"))
22214 is_user = 1;
22215 else if (!strcasecmp(utype, "friend"))
22216 is_user = is_peer = 1;
22217 else if (!strcasecmp(utype, "peer"))
22218 is_peer = 1;
22219 else {
22220 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
22221 continue;
22222 }
22223 if (is_user) {
22224 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
22225 if (user) {
22226 ASTOBJ_CONTAINER_LINK(&userl, user);
22227 unref_user(user);
22228 user_count++;
22229 }
22230 }
22231 if (is_peer) {
22232 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
22233 if (peer) {
22234 ASTOBJ_CONTAINER_LINK(&peerl, peer);
22235 unref_peer(peer);
22236 peer_count++;
22237 }
22238 }
22239 }
22240 }
22241
22242 bindaddr.sin_family = AF_INET;
22243 internip = bindaddr;
22244 if (ast_find_ourip(&internip.sin_addr, bindaddr)) {
22245 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
22246 ast_config_destroy(cfg);
22247 return 0;
22248 }
22249 ast_mutex_lock(&netlock);
22250 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
22251 close(sipsock);
22252 sipsock = -1;
22253 }
22254 if (sipsock < 0) {
22255 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
22256 if (sipsock < 0) {
22257 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
22258 ast_config_destroy(cfg);
22259 return -1;
22260 } else {
22261
22262 const int reuseFlag = 1;
22263
22264 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
22265 (const char*)&reuseFlag,
22266 sizeof reuseFlag);
22267
22268 ast_enable_packet_fragmentation(sipsock);
22269
22270 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
22271 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
22272 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
22273 strerror(errno));
22274 close(sipsock);
22275 sipsock = -1;
22276 } else {
22277 ast_verb(2, "SIP Listening on %s:%d\n",
22278 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
22279 ast_netsock_set_qos(sipsock, global_tos_sip, global_cos_sip, "SIP");
22280 }
22281 }
22282 }
22283 if (stunaddr.sin_addr.s_addr != 0) {
22284 ast_debug(1, "stun to %s:%d\n",
22285 ast_inet_ntoa(stunaddr.sin_addr) , ntohs(stunaddr.sin_port));
22286 ast_stun_request(sipsock, &stunaddr,
22287 NULL, &externip);
22288 ast_debug(1, "STUN sees us at %s:%d\n",
22289 ast_inet_ntoa(externip.sin_addr) , ntohs(externip.sin_port));
22290 }
22291 ast_mutex_unlock(&netlock);
22292
22293
22294 ast_tcptls_server_start(&sip_tcp_desc);
22295
22296
22297 memcpy(sip_tls_desc.tls_cfg, &default_tls_cfg, sizeof(default_tls_cfg));
22298
22299 if (ast_ssl_setup(sip_tls_desc.tls_cfg))
22300 ast_tcptls_server_start(&sip_tls_desc);
22301 else if (sip_tls_desc.tls_cfg->enabled) {
22302 sip_tls_desc.tls_cfg = NULL;
22303 ast_log(LOG_WARNING, "SIP TLS server did not load because of errors.\n");
22304 }
22305
22306
22307
22308
22309
22310
22311
22312 if (auto_sip_domains) {
22313 char temp[MAXHOSTNAMELEN];
22314
22315
22316 if (bindaddr.sin_addr.s_addr)
22317 add_sip_domain(ast_inet_ntoa(bindaddr.sin_addr), SIP_DOMAIN_AUTO, NULL);
22318 else
22319 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
22320
22321
22322 if (sip_tcp_desc.sin.sin_addr.s_addr && !inaddrcmp(&bindaddr, &sip_tcp_desc.sin))
22323 add_sip_domain(ast_inet_ntoa(sip_tcp_desc.sin.sin_addr), SIP_DOMAIN_AUTO, NULL);
22324
22325
22326 if (sip_tls_desc.sin.sin_addr.s_addr && !inaddrcmp(&bindaddr, &sip_tls_desc.sin) && inaddrcmp(&sip_tcp_desc.sin, &sip_tls_desc.sin))
22327 add_sip_domain(ast_inet_ntoa(sip_tls_desc.sin.sin_addr), SIP_DOMAIN_AUTO, NULL);
22328
22329
22330 if (externip.sin_addr.s_addr)
22331 add_sip_domain(ast_inet_ntoa(externip.sin_addr), SIP_DOMAIN_AUTO, NULL);
22332
22333
22334 if (!ast_strlen_zero(externhost))
22335 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
22336
22337
22338 if (!gethostname(temp, sizeof(temp)))
22339 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
22340 }
22341
22342
22343 ast_config_destroy(cfg);
22344
22345
22346 if (notify_types)
22347 ast_config_destroy(notify_types);
22348 notify_types = ast_config_load(notify_config, config_flags);
22349
22350
22351 manager_event(EVENT_FLAG_SYSTEM, "ChannelReload", "ChannelType: SIP\r\nReloadReason: %s\r\nRegistry_Count: %d\r\nPeer_Count: %d\r\nUser_Count: %d\r\n", channelreloadreason2txt(reason), registry_count, peer_count, user_count);
22352
22353 return 0;
22354 }
22355
22356 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
22357 {
22358 struct sip_pvt *p;
22359 struct ast_udptl *udptl = NULL;
22360
22361 p = chan->tech_pvt;
22362 if (!p)
22363 return NULL;
22364
22365 sip_pvt_lock(p);
22366 if (p->udptl && ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
22367 udptl = p->udptl;
22368 sip_pvt_unlock(p);
22369 return udptl;
22370 }
22371
22372 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
22373 {
22374 struct sip_pvt *p;
22375
22376 p = chan->tech_pvt;
22377 if (!p)
22378 return -1;
22379 sip_pvt_lock(p);
22380 if (udptl)
22381 ast_udptl_get_peer(udptl, &p->udptlredirip);
22382 else
22383 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
22384 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
22385 if (!p->pendinginvite) {
22386 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);
22387 transmit_reinvite_with_sdp(p, TRUE, FALSE);
22388 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
22389 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);
22390 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
22391 }
22392 }
22393
22394 p->lastrtprx = p->lastrtptx = time(NULL);
22395 sip_pvt_unlock(p);
22396 return 0;
22397 }
22398
22399
22400 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
22401 {
22402 struct sip_pvt *p = NULL;
22403 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
22404
22405 if (!(p = chan->tech_pvt))
22406 return AST_RTP_GET_FAILED;
22407
22408 sip_pvt_lock(p);
22409 if (!(p->rtp)) {
22410 sip_pvt_unlock(p);
22411 return AST_RTP_GET_FAILED;
22412 }
22413
22414 *rtp = p->rtp;
22415
22416 if (ast_rtp_getnat(*rtp) && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT))
22417 res = AST_RTP_TRY_PARTIAL;
22418 else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
22419 res = AST_RTP_TRY_NATIVE;
22420 else if (ast_test_flag(&global_jbconf, AST_JB_FORCED))
22421 res = AST_RTP_GET_FAILED;
22422
22423 sip_pvt_unlock(p);
22424
22425 return res;
22426 }
22427
22428
22429 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
22430 {
22431 struct sip_pvt *p = NULL;
22432 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
22433
22434 if (!(p = chan->tech_pvt))
22435 return AST_RTP_GET_FAILED;
22436
22437 sip_pvt_lock(p);
22438 if (!(p->vrtp)) {
22439 sip_pvt_unlock(p);
22440 return AST_RTP_GET_FAILED;
22441 }
22442
22443 *rtp = p->vrtp;
22444
22445 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
22446 res = AST_RTP_TRY_NATIVE;
22447
22448 sip_pvt_unlock(p);
22449
22450 return res;
22451 }
22452
22453
22454 static enum ast_rtp_get_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
22455 {
22456 struct sip_pvt *p = NULL;
22457 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
22458
22459 if (!(p = chan->tech_pvt))
22460 return AST_RTP_GET_FAILED;
22461
22462 sip_pvt_lock(p);
22463 if (!(p->trtp)) {
22464 sip_pvt_unlock(p);
22465 return AST_RTP_GET_FAILED;
22466 }
22467
22468 *rtp = p->trtp;
22469
22470 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
22471 res = AST_RTP_TRY_NATIVE;
22472
22473 sip_pvt_unlock(p);
22474
22475 return res;
22476 }
22477
22478
22479 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)
22480 {
22481 struct sip_pvt *p;
22482 int changed = 0;
22483
22484 p = chan->tech_pvt;
22485 if (!p)
22486 return -1;
22487
22488
22489 if (!ast_bridged_channel(chan) && !global_directrtpsetup)
22490 return 0;
22491
22492 sip_pvt_lock(p);
22493 if (p->alreadygone) {
22494
22495 sip_pvt_unlock(p);
22496 return 0;
22497 }
22498
22499
22500
22501
22502 if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
22503 sip_pvt_unlock(p);
22504 return 0;
22505 }
22506
22507 if (rtp) {
22508 changed |= ast_rtp_get_peer(rtp, &p->redirip);
22509 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
22510 memset(&p->redirip, 0, sizeof(p->redirip));
22511 changed = 1;
22512 }
22513 if (vrtp) {
22514 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
22515 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
22516 memset(&p->vredirip, 0, sizeof(p->vredirip));
22517 changed = 1;
22518 }
22519 if (trtp) {
22520 changed |= ast_rtp_get_peer(trtp, &p->tredirip);
22521 } else if (p->tredirip.sin_addr.s_addr || ntohs(p->tredirip.sin_port) != 0) {
22522 memset(&p->tredirip, 0, sizeof(p->tredirip));
22523 changed = 1;
22524 }
22525 if (codecs && (p->redircodecs != codecs)) {
22526 p->redircodecs = codecs;
22527 changed = 1;
22528 }
22529 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
22530 if (chan->_state != AST_STATE_UP) {
22531 if (p->do_history)
22532 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
22533 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));
22534 } else if (!p->pendinginvite) {
22535 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));
22536 transmit_reinvite_with_sdp(p, FALSE, FALSE);
22537 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
22538 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));
22539
22540 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
22541 }
22542 }
22543
22544 p->lastrtprx = p->lastrtptx = time(NULL);
22545 sip_pvt_unlock(p);
22546 return 0;
22547 }
22548
22549 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
22550 static char *descrip_dtmfmode = " SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
22551 static char *app_dtmfmode = "SIPDtmfMode";
22552
22553 static char *app_sipaddheader = "SIPAddHeader";
22554 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
22555
22556 static char *descrip_sipaddheader = ""
22557 " SIPAddHeader(Header: Content):\n"
22558 "Adds a header to a SIP call placed with DIAL.\n"
22559 "Remember to user the X-header if you are adding non-standard SIP\n"
22560 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
22561 "Adding the wrong headers may jeopardize the SIP dialog.\n"
22562 "Always returns 0\n";
22563
22564
22565
22566 static int sip_dtmfmode(struct ast_channel *chan, void *data)
22567 {
22568 struct sip_pvt *p;
22569 char *mode = data;
22570
22571 if (!data) {
22572 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
22573 return 0;
22574 }
22575 ast_channel_lock(chan);
22576 if (!IS_SIP_TECH(chan->tech)) {
22577 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
22578 ast_channel_unlock(chan);
22579 return 0;
22580 }
22581 p = chan->tech_pvt;
22582 if (!p) {
22583 ast_channel_unlock(chan);
22584 return 0;
22585 }
22586 sip_pvt_lock(p);
22587 if (!strcasecmp(mode, "info")) {
22588 ast_clear_flag(&p->flags[0], SIP_DTMF);
22589 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
22590 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
22591 } else if (!strcasecmp(mode, "shortinfo")) {
22592 ast_clear_flag(&p->flags[0], SIP_DTMF);
22593 ast_set_flag(&p->flags[0], SIP_DTMF_SHORTINFO);
22594 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
22595 } else if (!strcasecmp(mode, "rfc2833")) {
22596 ast_clear_flag(&p->flags[0], SIP_DTMF);
22597 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
22598 p->jointnoncodeccapability |= AST_RTP_DTMF;
22599 } else if (!strcasecmp(mode, "inband")) {
22600 ast_clear_flag(&p->flags[0], SIP_DTMF);
22601 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
22602 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
22603 } else
22604 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n", mode);
22605 if (p->rtp)
22606 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
22607 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
22608 if (!p->vad) {
22609 p->vad = ast_dsp_new();
22610 ast_dsp_set_features(p->vad, DSP_FEATURE_DTMF_DETECT);
22611 }
22612 } else {
22613 if (p->vad) {
22614 ast_dsp_free(p->vad);
22615 p->vad = NULL;
22616 }
22617 }
22618 sip_pvt_unlock(p);
22619 ast_channel_unlock(chan);
22620 return 0;
22621 }
22622
22623
22624 static int sip_addheader(struct ast_channel *chan, void *data)
22625 {
22626 int no = 0;
22627 int ok = FALSE;
22628 char varbuf[30];
22629 char *inbuf = data, *subbuf;
22630
22631 if (ast_strlen_zero(inbuf)) {
22632 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
22633 return 0;
22634 }
22635 ast_channel_lock(chan);
22636
22637
22638 while (!ok && no <= 50) {
22639 no++;
22640 snprintf(varbuf, sizeof(varbuf), "__SIPADDHEADER%.2d", no);
22641
22642
22643 if ((pbx_builtin_getvar_helper(chan, (const char *) varbuf + 2) == (const char *) NULL)) {
22644 ok = TRUE;
22645 }
22646 }
22647 if (ok) {
22648 size_t len = strlen(inbuf);
22649 subbuf = alloca(len + 1);
22650 ast_get_encoded_str(inbuf, subbuf, len + 1);
22651 pbx_builtin_setvar_helper(chan, varbuf, subbuf);
22652 if (sipdebug) {
22653 ast_debug(1, "SIP Header added \"%s\" as %s\n", inbuf, varbuf);
22654 }
22655 } else {
22656 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
22657 }
22658 ast_channel_unlock(chan);
22659 return 0;
22660 }
22661
22662
22663
22664
22665
22666
22667
22668 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
22669 {
22670 char *cdest;
22671 char *extension, *host, *port;
22672 char tmp[80];
22673
22674 cdest = ast_strdupa(dest);
22675
22676 extension = strsep(&cdest, "@");
22677 host = strsep(&cdest, ":");
22678 port = strsep(&cdest, ":");
22679 if (ast_strlen_zero(extension)) {
22680 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
22681 return 0;
22682 }
22683
22684
22685 if (!host) {
22686 char *localtmp;
22687
22688 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
22689 if (ast_strlen_zero(tmp)) {
22690 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
22691 return 0;
22692 }
22693 if ( ( (localtmp = strcasestr(tmp, "sip:")) || (localtmp = strcasestr(tmp, "sips:")) )
22694 && (localtmp = strchr(localtmp, '@'))) {
22695 char lhost[80], lport[80];
22696
22697 memset(lhost, 0, sizeof(lhost));
22698 memset(lport, 0, sizeof(lport));
22699 localtmp++;
22700
22701 sscanf(localtmp, "%[^<>:; ]:%[^<>:; ]", lhost, lport);
22702 if (ast_strlen_zero(lhost)) {
22703 ast_log(LOG_ERROR, "Can't find the host address\n");
22704 return 0;
22705 }
22706 host = ast_strdupa(lhost);
22707 if (!ast_strlen_zero(lport)) {
22708 port = ast_strdupa(lport);
22709 }
22710 }
22711 }
22712
22713 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
22714 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
22715
22716 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
22717 sip_alreadygone(p);
22718
22719 return 0;
22720 }
22721
22722
22723 static int sip_get_codec(struct ast_channel *chan)
22724 {
22725 struct sip_pvt *p = chan->tech_pvt;
22726 return p->peercapability ? p->peercapability : p->capability;
22727 }
22728
22729
22730
22731
22732
22733 static void sip_poke_all_peers(void)
22734 {
22735 int ms = 0;
22736
22737 if (!speerobjs)
22738 return;
22739
22740 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
22741 ASTOBJ_WRLOCK(iterator);
22742 ms += 100;
22743 AST_SCHED_REPLACE(iterator->pokeexpire, sched, ms, sip_poke_peer_s, iterator);
22744 ASTOBJ_UNLOCK(iterator);
22745 } while (0)
22746 );
22747 }
22748
22749
22750 static void sip_send_all_registers(void)
22751 {
22752 int ms;
22753 int regspacing;
22754 if (!regobjs)
22755 return;
22756 regspacing = default_expiry * 1000/regobjs;
22757 if (regspacing > 100)
22758 regspacing = 100;
22759 ms = regspacing;
22760 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
22761 ASTOBJ_WRLOCK(iterator);
22762 ms += regspacing;
22763 AST_SCHED_REPLACE(iterator->expire,
22764 sched, ms, sip_reregister, iterator);
22765 ASTOBJ_UNLOCK(iterator);
22766 } while (0)
22767 );
22768 }
22769
22770
22771 static int sip_do_reload(enum channelreloadreason reason)
22772 {
22773 reload_config(reason);
22774
22775
22776 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
22777 ast_debug(4, "--------------- Done destroying pruned peers\n");
22778
22779
22780 sip_poke_all_peers();
22781
22782
22783 sip_send_all_registers();
22784
22785 ast_debug(4, "--------------- SIP reload done\n");
22786
22787 return 0;
22788 }
22789
22790
22791 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
22792 {
22793
22794 switch (cmd) {
22795 case CLI_INIT:
22796 e->command = "sip reload";
22797 e->usage =
22798 "Usage: sip reload\n"
22799 " Reloads SIP configuration from sip.conf\n";
22800 return NULL;
22801 case CLI_GENERATE:
22802 return NULL;
22803 }
22804
22805 ast_mutex_lock(&sip_reload_lock);
22806 if (sip_reloading)
22807 ast_verbose("Previous SIP reload not yet done\n");
22808 else {
22809 sip_reloading = TRUE;
22810 sip_reloadreason = (a && a->fd) ? CHANNEL_CLI_RELOAD : CHANNEL_MODULE_RELOAD;
22811 }
22812 ast_mutex_unlock(&sip_reload_lock);
22813 restart_monitor();
22814
22815 return CLI_SUCCESS;
22816 }
22817
22818
22819 static int reload(void)
22820 {
22821 if (sip_reload(0, 0, NULL))
22822 return 0;
22823 return 1;
22824 }
22825
22826 static struct ast_cli_entry cli_sip_do_history_deprecated = AST_CLI_DEFINE(sip_do_history_deprecated, "Enable/Disable SIP history");
22827
22828 static struct ast_cli_entry cli_sip[] = {
22829 AST_CLI_DEFINE(sip_show_channels, "List active SIP channels/subscriptions"),
22830 AST_CLI_DEFINE(sip_show_domains, "List our local SIP domains"),
22831 AST_CLI_DEFINE(sip_show_inuse, "List all inuse/limits"),
22832 AST_CLI_DEFINE(sip_show_objects, "List all SIP object allocations"),
22833 AST_CLI_DEFINE(sip_show_peers, "List defined SIP peers"),
22834 AST_CLI_DEFINE(sip_show_registry, "List SIP registration status"),
22835 AST_CLI_DEFINE(sip_unregister, "Unregister (force expiration) a SIP peer from the registry"),
22836 AST_CLI_DEFINE(sip_show_settings, "Show SIP global settings"),
22837 AST_CLI_DEFINE(sip_notify, "Send a notify packet to a SIP peer"),
22838 AST_CLI_DEFINE(sip_show_channel, "Show detailed SIP channel info"),
22839 AST_CLI_DEFINE(sip_show_history, "Show SIP dialog history"),
22840 AST_CLI_DEFINE(sip_show_peer, "Show details on specific SIP peer"),
22841 AST_CLI_DEFINE(sip_show_users, "List defined SIP users"),
22842 AST_CLI_DEFINE(sip_show_user, "Show details on specific SIP user"),
22843 AST_CLI_DEFINE(sip_prune_realtime, "Prune cached Realtime users/peers"),
22844 AST_CLI_DEFINE(sip_do_debug, "Enable/Disable SIP debugging"),
22845 AST_CLI_DEFINE(sip_set_history, "Enable/Disable SIP history", .deprecate_cmd = &cli_sip_do_history_deprecated),
22846 AST_CLI_DEFINE(sip_reload, "Reload SIP configuration"),
22847 AST_CLI_DEFINE(sip_show_tcp, "List TCP Connections")
22848 };
22849
22850
22851 static int load_module(void)
22852 {
22853 ast_verbose("SIP channel loading...\n");
22854 ASTOBJ_CONTAINER_INIT(&userl);
22855 ASTOBJ_CONTAINER_INIT(&peerl);
22856 ASTOBJ_CONTAINER_INIT(®l);
22857
22858 if (!(sched = sched_context_create())) {
22859 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
22860 return AST_MODULE_LOAD_FAILURE;
22861 }
22862
22863 if (!(io = io_context_create())) {
22864 ast_log(LOG_ERROR, "Unable to create I/O context\n");
22865 sched_context_destroy(sched);
22866 return AST_MODULE_LOAD_FAILURE;
22867 }
22868
22869 sip_reloadreason = CHANNEL_MODULE_LOAD;
22870
22871 if(reload_config(sip_reloadreason))
22872 return AST_MODULE_LOAD_DECLINE;
22873
22874
22875
22876
22877
22878 memcpy(&sip_tech_info, &sip_tech, sizeof(sip_tech));
22879 memset((void *) &sip_tech_info.send_digit_begin, 0, sizeof(sip_tech_info.send_digit_begin));
22880
22881
22882 if (ast_channel_register(&sip_tech)) {
22883 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
22884 io_context_destroy(io);
22885 sched_context_destroy(sched);
22886 return AST_MODULE_LOAD_FAILURE;
22887 }
22888
22889
22890 ast_cli_register_multiple(cli_sip, sizeof(cli_sip)/ sizeof(struct ast_cli_entry));
22891
22892
22893 ast_rtp_proto_register(&sip_rtp);
22894
22895
22896 ast_udptl_proto_register(&sip_udptl);
22897
22898
22899 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
22900 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
22901
22902
22903 ast_custom_function_register(&sip_header_function);
22904 ast_custom_function_register(&sippeer_function);
22905 ast_custom_function_register(&sipchaninfo_function);
22906 ast_custom_function_register(&checksipdomain_function);
22907
22908
22909 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peers,
22910 "List SIP peers (text format)", mandescr_show_peers);
22911 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peer,
22912 "Show SIP peer (text format)", mandescr_show_peer);
22913 ast_manager_register2("SIPshowregistry", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_show_registry,
22914 "Show SIP registrations (text format)", mandescr_show_registry);
22915 sip_poke_all_peers();
22916 sip_send_all_registers();
22917
22918
22919 restart_monitor();
22920
22921 return AST_MODULE_LOAD_SUCCESS;
22922 }
22923
22924
22925 static int unload_module(void)
22926 {
22927 struct sip_pvt *p, *pl;
22928 struct sip_threadinfo *th;
22929 struct ast_context *con;
22930
22931
22932 ast_channel_unregister(&sip_tech);
22933
22934
22935 ast_custom_function_unregister(&sipchaninfo_function);
22936 ast_custom_function_unregister(&sippeer_function);
22937 ast_custom_function_unregister(&sip_header_function);
22938 ast_custom_function_unregister(&checksipdomain_function);
22939
22940
22941 ast_unregister_application(app_dtmfmode);
22942 ast_unregister_application(app_sipaddheader);
22943
22944
22945 ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
22946
22947
22948 ast_rtp_proto_unregister(&sip_rtp);
22949
22950
22951 ast_udptl_proto_unregister(&sip_udptl);
22952
22953
22954 ast_manager_unregister("SIPpeers");
22955 ast_manager_unregister("SIPshowpeer");
22956 ast_manager_unregister("SIPshowregistry");
22957
22958
22959 if (sip_tcp_desc.master)
22960 ast_tcptls_server_stop(&sip_tcp_desc);
22961 if (sip_tls_desc.master)
22962 ast_tcptls_server_stop(&sip_tls_desc);
22963
22964
22965 AST_LIST_LOCK(&threadl);
22966 AST_LIST_TRAVERSE_SAFE_BEGIN(&threadl, th, list) {
22967 pthread_t thread = th->threadid;
22968 th->stop = 1;
22969 AST_LIST_UNLOCK(&threadl);
22970 pthread_kill(thread, SIGURG);
22971 pthread_join(thread, NULL);
22972 AST_LIST_LOCK(&threadl);
22973 }
22974 AST_LIST_TRAVERSE_SAFE_END;
22975 AST_LIST_UNLOCK(&threadl);
22976
22977 dialoglist_lock();
22978
22979 for (p = dialoglist; p ; p = p->next) {
22980 if (p->owner)
22981 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
22982 }
22983 dialoglist_unlock();
22984
22985 ast_mutex_lock(&monlock);
22986 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
22987 pthread_cancel(monitor_thread);
22988 pthread_kill(monitor_thread, SIGURG);
22989 pthread_join(monitor_thread, NULL);
22990 }
22991 monitor_thread = AST_PTHREADT_STOP;
22992 ast_mutex_unlock(&monlock);
22993
22994 restartdestroy:
22995 dialoglist_lock();
22996
22997 p = dialoglist;
22998 while (p) {
22999 pl = p;
23000 p = p->next;
23001 if (__sip_destroy(pl, TRUE, TRUE) < 0) {
23002
23003 dialoglist = p;
23004 dialoglist_unlock();
23005 usleep(1);
23006 goto restartdestroy;
23007 }
23008 }
23009 dialoglist = NULL;
23010 dialoglist_unlock();
23011
23012
23013 ast_free_ha(localaddr);
23014
23015 if (default_tls_cfg.certfile)
23016 ast_free(default_tls_cfg.certfile);
23017 if (default_tls_cfg.cipher)
23018 ast_free(default_tls_cfg.cipher);
23019 if (default_tls_cfg.cafile)
23020 ast_free(default_tls_cfg.cafile);
23021 if (default_tls_cfg.capath)
23022 ast_free(default_tls_cfg.capath);
23023
23024 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
23025 ASTOBJ_CONTAINER_DESTROY(&userl);
23026 ASTOBJ_CONTAINER_DESTROYALL(&peerl, sip_destroy_peer);
23027 ASTOBJ_CONTAINER_DESTROY(&peerl);
23028 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
23029 ASTOBJ_CONTAINER_DESTROY(®l);
23030
23031 clear_realm_authentication(authl);
23032 clear_sip_domains();
23033 close(sipsock);
23034 sched_context_destroy(sched);
23035 con = ast_context_find(used_context);
23036 if (con)
23037 ast_context_destroy(con, "SIP");
23038
23039 return 0;
23040 }
23041
23042 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)",
23043 .load = load_module,
23044 .unload = unload_module,
23045 .reload = reload,
23046 );