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 #include "asterisk.h"
00096
00097 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 231527 $")
00098
00099 #include <stdio.h>
00100 #include <ctype.h>
00101 #include <string.h>
00102 #include <unistd.h>
00103 #include <sys/socket.h>
00104 #include <sys/ioctl.h>
00105 #include <net/if.h>
00106 #include <errno.h>
00107 #include <stdlib.h>
00108 #include <fcntl.h>
00109 #include <netdb.h>
00110 #include <signal.h>
00111 #include <sys/signal.h>
00112 #include <netinet/in.h>
00113 #include <netinet/in_systm.h>
00114 #include <arpa/inet.h>
00115 #include <netinet/ip.h>
00116 #include <regex.h>
00117
00118 #include "asterisk/lock.h"
00119 #include "asterisk/channel.h"
00120 #include "asterisk/config.h"
00121 #include "asterisk/logger.h"
00122 #include "asterisk/module.h"
00123 #include "asterisk/pbx.h"
00124 #include "asterisk/options.h"
00125 #include "asterisk/sched.h"
00126 #include "asterisk/io.h"
00127 #include "asterisk/rtp.h"
00128 #include "asterisk/udptl.h"
00129 #include "asterisk/acl.h"
00130 #include "asterisk/manager.h"
00131 #include "asterisk/callerid.h"
00132 #include "asterisk/cli.h"
00133 #include "asterisk/app.h"
00134 #include "asterisk/musiconhold.h"
00135 #include "asterisk/dsp.h"
00136 #include "asterisk/features.h"
00137 #include "asterisk/srv.h"
00138 #include "asterisk/astdb.h"
00139 #include "asterisk/causes.h"
00140 #include "asterisk/utils.h"
00141 #include "asterisk/file.h"
00142 #include "asterisk/astobj.h"
00143 #include "asterisk/devicestate.h"
00144 #include "asterisk/linkedlists.h"
00145 #include "asterisk/stringfields.h"
00146 #include "asterisk/monitor.h"
00147 #include "asterisk/localtime.h"
00148 #include "asterisk/abstract_jb.h"
00149 #include "asterisk/compiler.h"
00150 #include "asterisk/threadstorage.h"
00151 #include "asterisk/translate.h"
00152
00153 #ifndef FALSE
00154 #define FALSE 0
00155 #endif
00156
00157 #ifndef TRUE
00158 #define TRUE 1
00159 #endif
00160
00161 #define SIPBUFSIZE 512
00162
00163 #define XMIT_ERROR -2
00164
00165 #define VIDEO_CODEC_MASK 0x1fc0000
00166 #ifndef IPTOS_MINCOST
00167 #define IPTOS_MINCOST 0x02
00168 #endif
00169
00170
00171
00172 #define DEFAULT_DEFAULT_EXPIRY 120
00173 #define DEFAULT_MIN_EXPIRY 60
00174 #define DEFAULT_MAX_EXPIRY 3600
00175 #define DEFAULT_REGISTRATION_TIMEOUT 20
00176 #define DEFAULT_MAX_FORWARDS "70"
00177
00178
00179
00180 #define EXPIRY_GUARD_SECS 15
00181 #define EXPIRY_GUARD_LIMIT 30
00182
00183 #define EXPIRY_GUARD_MIN 500
00184
00185
00186
00187 #define EXPIRY_GUARD_PCT 0.20
00188
00189 #define DEFAULT_EXPIRY 900
00190
00191 static int min_expiry = DEFAULT_MIN_EXPIRY;
00192 static int max_expiry = DEFAULT_MAX_EXPIRY;
00193 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00194 static int expiry = DEFAULT_EXPIRY;
00195 static char seen_lastms = 0;
00196
00197 #ifndef MAX
00198 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00199 #endif
00200
00201 #define CALLERID_UNKNOWN "Anonymous"
00202 #define FROMDOMAIN_INVALID "anonymous.invalid"
00203
00204 #define DEFAULT_MAXMS 2000
00205 #define DEFAULT_FREQ_OK 60 * 1000
00206 #define DEFAULT_FREQ_NOTOK 10 * 1000
00207
00208 #define DEFAULT_RETRANS 1000
00209 #define MAX_RETRANS 6
00210 #define SIP_TRANS_TIMEOUT 32000
00211
00212
00213 #define DEFAULT_TRANS_TIMEOUT -1
00214 #define PROVIS_KEEPALIVE_TIMEOUT 60000
00215 #define MAX_AUTHTRIES 3
00216
00217 #define SIP_MAX_HEADERS 64
00218 #define SIP_MAX_LINES 64
00219 #define SIP_MAX_PACKET 4096
00220
00221 #define SDP_MAX_RTPMAP_CODECS 32
00222
00223 #define INITIAL_CSEQ 101
00224
00225
00226 static struct ast_jb_conf default_jbconf =
00227 {
00228 .flags = 0,
00229 .max_size = -1,
00230 .resync_threshold = -1,
00231 .impl = ""
00232 };
00233 static struct ast_jb_conf global_jbconf;
00234
00235 static const char config[] = "sip.conf";
00236 static const char notify_config[] = "sip_notify.conf";
00237
00238 #define RTP 1
00239 #define NO_RTP 0
00240
00241
00242
00243
00244 enum transfermodes {
00245 TRANSFER_OPENFORALL,
00246 TRANSFER_CLOSED,
00247 };
00248
00249
00250 enum sip_result {
00251 AST_SUCCESS = 0,
00252 AST_FAILURE = -1,
00253 };
00254
00255
00256
00257
00258 enum invitestates {
00259 INV_NONE = 0,
00260 INV_CALLING = 1,
00261 INV_PROCEEDING = 2,
00262 INV_EARLY_MEDIA = 3,
00263 INV_COMPLETED = 4,
00264 INV_CONFIRMED = 5,
00265 INV_TERMINATED = 6,
00266
00267 INV_CANCELLED = 7,
00268 };
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 enum xmittype {
00279 XMIT_CRITICAL = 2,
00280
00281 XMIT_RELIABLE = 1,
00282 XMIT_UNRELIABLE = 0,
00283 };
00284
00285 enum parse_register_result {
00286 PARSE_REGISTER_DENIED,
00287 PARSE_REGISTER_FAILED,
00288 PARSE_REGISTER_UPDATE,
00289 PARSE_REGISTER_QUERY,
00290 };
00291
00292 enum subscriptiontype {
00293 NONE = 0,
00294 XPIDF_XML,
00295 DIALOG_INFO_XML,
00296 CPIM_PIDF_XML,
00297 PIDF_XML,
00298 MWI_NOTIFICATION
00299 };
00300
00301 static const struct cfsubscription_types {
00302 enum subscriptiontype type;
00303 const char * const event;
00304 const char * const mediatype;
00305 const char * const text;
00306 } subscription_types[] = {
00307 { NONE, "-", "unknown", "unknown" },
00308
00309 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00310 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00311 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00312 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00313 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00314 };
00315
00316
00317 enum sipmethod {
00318 SIP_UNKNOWN,
00319 SIP_RESPONSE,
00320 SIP_REGISTER,
00321 SIP_OPTIONS,
00322 SIP_NOTIFY,
00323 SIP_INVITE,
00324 SIP_ACK,
00325 SIP_PRACK,
00326 SIP_BYE,
00327 SIP_REFER,
00328 SIP_SUBSCRIBE,
00329 SIP_MESSAGE,
00330 SIP_UPDATE,
00331 SIP_INFO,
00332 SIP_CANCEL,
00333 SIP_PUBLISH,
00334 SIP_PING,
00335 };
00336
00337
00338
00339
00340
00341
00342 enum sip_auth_type {
00343 PROXY_AUTH,
00344 WWW_AUTH,
00345 };
00346
00347
00348 enum check_auth_result {
00349 AUTH_SUCCESSFUL = 0,
00350 AUTH_CHALLENGE_SENT = 1,
00351 AUTH_SECRET_FAILED = -1,
00352 AUTH_USERNAME_MISMATCH = -2,
00353 AUTH_NOT_FOUND = -3,
00354 AUTH_FAKE_AUTH = -4,
00355 AUTH_UNKNOWN_DOMAIN = -5,
00356 AUTH_PEER_NOT_DYNAMIC = -6,
00357 AUTH_ACL_FAILED = -7,
00358 };
00359
00360
00361 enum sipregistrystate {
00362 REG_STATE_UNREGISTERED = 0,
00363 REG_STATE_REGSENT,
00364 REG_STATE_AUTHSENT,
00365 REG_STATE_REGISTERED,
00366 REG_STATE_REJECTED,
00367 REG_STATE_TIMEOUT,
00368 REG_STATE_NOAUTH,
00369 REG_STATE_FAILED,
00370 };
00371
00372 #define CAN_NOT_CREATE_DIALOG 0
00373 #define CAN_CREATE_DIALOG 1
00374 #define CAN_CREATE_DIALOG_UNSUPPORTED_METHOD 2
00375
00376
00377 static const struct cfsip_methods {
00378 enum sipmethod id;
00379 int need_rtp;
00380 char * const text;
00381 int can_create;
00382 } sip_methods[] = {
00383 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00384 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00385 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00386 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00387 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00388 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00389 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00390 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00391 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00392 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00393 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00394 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00395 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00396 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00397 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00398 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00399 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00400 };
00401
00402 static unsigned int chan_idx;
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 #define SUPPORTED 1
00415 #define NOT_SUPPORTED 0
00416
00417 #define SIP_OPT_REPLACES (1 << 0)
00418 #define SIP_OPT_100REL (1 << 1)
00419 #define SIP_OPT_TIMER (1 << 2)
00420 #define SIP_OPT_EARLY_SESSION (1 << 3)
00421 #define SIP_OPT_JOIN (1 << 4)
00422 #define SIP_OPT_PATH (1 << 5)
00423 #define SIP_OPT_PREF (1 << 6)
00424 #define SIP_OPT_PRECONDITION (1 << 7)
00425 #define SIP_OPT_PRIVACY (1 << 8)
00426 #define SIP_OPT_SDP_ANAT (1 << 9)
00427 #define SIP_OPT_SEC_AGREE (1 << 10)
00428 #define SIP_OPT_EVENTLIST (1 << 11)
00429 #define SIP_OPT_GRUU (1 << 12)
00430 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00431 #define SIP_OPT_NOREFERSUB (1 << 14)
00432 #define SIP_OPT_HISTINFO (1 << 15)
00433 #define SIP_OPT_RESPRIORITY (1 << 16)
00434
00435
00436
00437 static const struct cfsip_options {
00438 int id;
00439 int supported;
00440 char * const text;
00441 } sip_options[] = {
00442
00443 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00444
00445 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00446
00447 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00448
00449 { SIP_OPT_TIMER, NOT_SUPPORTED, "timer" },
00450
00451 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00452
00453 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00454
00455 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00456
00457 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00458
00459 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00460
00461 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00462
00463 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00464
00465 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00466
00467 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00468
00469 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00470
00471 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00472
00473 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00474
00475 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00476
00477 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00478 };
00479
00480
00481
00482 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO"
00483
00484
00485 #define SUPPORTED_EXTENSIONS "replaces"
00486
00487
00488 #define STANDARD_SIP_PORT 5060
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 #define DEFAULT_CONTEXT "default"
00502 #define DEFAULT_MOHINTERPRET "default"
00503 #define DEFAULT_MOHSUGGEST ""
00504 #define DEFAULT_VMEXTEN "asterisk"
00505 #define DEFAULT_CALLERID "asterisk"
00506 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00507 #define DEFAULT_MWITIME 10
00508 #define DEFAULT_ALLOWGUEST TRUE
00509 #define DEFAULT_SRVLOOKUP TRUE
00510 #define DEFAULT_COMPACTHEADERS FALSE
00511 #define DEFAULT_TOS_SIP 0
00512 #define DEFAULT_TOS_AUDIO 0
00513 #define DEFAULT_TOS_VIDEO 0
00514 #define DEFAULT_ALLOW_EXT_DOM TRUE
00515 #define DEFAULT_REALM "asterisk"
00516 #define DEFAULT_NOTIFYRINGING TRUE
00517 #define DEFAULT_PEDANTIC FALSE
00518 #define DEFAULT_AUTOCREATEPEER FALSE
00519 #define DEFAULT_QUALIFY FALSE
00520 #define DEFAULT_T1MIN 100
00521 #define DEFAULT_MAX_CALL_BITRATE (384)
00522 #ifndef DEFAULT_USERAGENT
00523 #define DEFAULT_USERAGENT "Asterisk PBX"
00524 #endif
00525
00526
00527
00528
00529 static char default_context[AST_MAX_CONTEXT];
00530 static char default_subscribecontext[AST_MAX_CONTEXT];
00531 static char default_language[MAX_LANGUAGE];
00532 static char default_callerid[AST_MAX_EXTENSION];
00533 static char default_fromdomain[AST_MAX_EXTENSION];
00534 static char default_notifymime[AST_MAX_EXTENSION];
00535 static int default_qualify;
00536 static char default_vmexten[AST_MAX_EXTENSION];
00537 static char default_mohinterpret[MAX_MUSICCLASS];
00538 static char default_mohsuggest[MAX_MUSICCLASS];
00539
00540 static int default_maxcallbitrate;
00541 static struct ast_codec_pref default_prefs;
00542
00543
00544 static int global_directrtpsetup;
00545 static int global_prematuremediafilter;
00546 static int global_limitonpeers;
00547 static int global_rtautoclear;
00548 static int global_notifyringing;
00549 static int global_notifyhold;
00550 static int global_alwaysauthreject;
00551 static int srvlookup;
00552 static int pedanticsipchecking;
00553 static int autocreatepeer;
00554 static int global_relaxdtmf;
00555 static int global_rtptimeout;
00556 static int global_rtpholdtimeout;
00557 static int global_rtpkeepalive;
00558 static int global_reg_timeout;
00559 static int global_regattempts_max;
00560 static int global_shrinkcallerid;
00561 static int global_allowguest;
00562 static int global_allowsubscribe;
00563
00564 static int global_mwitime;
00565 static unsigned int global_tos_sip;
00566 static unsigned int global_tos_audio;
00567 static unsigned int global_tos_video;
00568 static int compactheaders;
00569 static int recordhistory;
00570 static int dumphistory;
00571 static char global_realm[MAXHOSTNAMELEN];
00572 static char global_regcontext[AST_MAX_CONTEXT];
00573 static char global_useragent[AST_MAX_EXTENSION];
00574 static int allow_external_domains;
00575 static int global_callevents;
00576 static int global_t1min;
00577 static int global_autoframing;
00578 static enum transfermodes global_allowtransfer;
00579
00580 static int global_matchexterniplocally;
00581
00582
00583 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00584
00585
00586 static struct ast_ha *global_contact_ha = NULL;
00587 static int global_dynamic_exclude_static = 0;
00588
00589
00590 static int suserobjs = 0;
00591 static int ruserobjs = 0;
00592 static int speerobjs = 0;
00593 static int rpeerobjs = 0;
00594 static int apeerobjs = 0;
00595 static int regobjs = 0;
00596
00597 static struct ast_flags global_flags[2] = {{0}};
00598
00599
00600 AST_MUTEX_DEFINE_STATIC(iflock);
00601
00602
00603
00604 AST_MUTEX_DEFINE_STATIC(netlock);
00605
00606 AST_MUTEX_DEFINE_STATIC(monlock);
00607
00608 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00609
00610
00611
00612 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00613
00614 static int sip_reloading = FALSE;
00615 static enum channelreloadreason sip_reloadreason;
00616
00617 static struct sched_context *sched;
00618 static struct io_context *io;
00619 static int *sipsock_read_id;
00620
00621 #define DEC_CALL_LIMIT 0
00622 #define INC_CALL_LIMIT 1
00623 #define DEC_CALL_RINGING 2
00624 #define INC_CALL_RINGING 3
00625
00626
00627 struct sip_request {
00628 char *rlPart1;
00629 char *rlPart2;
00630 int len;
00631 int headers;
00632 int method;
00633 int lines;
00634 unsigned int flags;
00635 char *header[SIP_MAX_HEADERS];
00636 char *line[SIP_MAX_LINES];
00637 char data[SIP_MAX_PACKET];
00638 unsigned int sdp_start;
00639 unsigned int sdp_count;
00640 AST_LIST_ENTRY(sip_request) next;
00641 };
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663 struct sip_dual {
00664 struct ast_channel *chan1;
00665 struct ast_channel *chan2;
00666 struct sip_request req;
00667 int seqno;
00668 };
00669
00670 struct sip_pkt;
00671
00672
00673 struct sip_invite_param {
00674 const char *distinctive_ring;
00675 int addsipheaders;
00676 const char *uri_options;
00677 const char *vxml_url;
00678 char *auth;
00679 char *authheader;
00680 enum sip_auth_type auth_type;
00681 const char *replaces;
00682 int transfer;
00683 };
00684
00685
00686 struct sip_route {
00687 struct sip_route *next;
00688 char hop[0];
00689 };
00690
00691
00692 enum domain_mode {
00693 SIP_DOMAIN_AUTO,
00694 SIP_DOMAIN_CONFIG,
00695 };
00696
00697
00698
00699
00700
00701 struct domain {
00702 char domain[MAXHOSTNAMELEN];
00703 char context[AST_MAX_EXTENSION];
00704 enum domain_mode mode;
00705 AST_LIST_ENTRY(domain) list;
00706 };
00707
00708 static AST_LIST_HEAD_STATIC(domain_list, domain);
00709
00710
00711
00712 struct sip_history {
00713 AST_LIST_ENTRY(sip_history) list;
00714 char event[0];
00715 };
00716
00717 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00718
00719
00720 struct sip_auth {
00721 char realm[AST_MAX_EXTENSION];
00722 char username[256];
00723 char secret[256];
00724 char md5secret[256];
00725 struct sip_auth *next;
00726 };
00727
00728
00729 #define SIP_ALREADYGONE (1 << 0)
00730 #define SIP_NEEDDESTROY (1 << 1)
00731 #define SIP_NOVIDEO (1 << 2)
00732 #define SIP_RINGING (1 << 3)
00733 #define SIP_PROGRESS_SENT (1 << 4)
00734 #define SIP_NEEDREINVITE (1 << 5)
00735 #define SIP_PENDINGBYE (1 << 6)
00736 #define SIP_GOTREFER (1 << 7)
00737 #define SIP_PROMISCREDIR (1 << 8)
00738 #define SIP_TRUSTRPID (1 << 9)
00739 #define SIP_USEREQPHONE (1 << 10)
00740 #define SIP_REALTIME (1 << 11)
00741 #define SIP_USECLIENTCODE (1 << 12)
00742 #define SIP_OUTGOING (1 << 13)
00743 #define SIP_FREE_BIT (1 << 14)
00744 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 15)
00745 #define SIP_DTMF (3 << 16)
00746 #define SIP_DTMF_RFC2833 (0 << 16)
00747 #define SIP_DTMF_INBAND (1 << 16)
00748 #define SIP_DTMF_INFO (2 << 16)
00749 #define SIP_DTMF_AUTO (3 << 16)
00750
00751 #define SIP_NAT (3 << 18)
00752 #define SIP_NAT_NEVER (0 << 18)
00753 #define SIP_NAT_RFC3581 (1 << 18)
00754 #define SIP_NAT_ROUTE (2 << 18)
00755 #define SIP_NAT_ALWAYS (3 << 18)
00756
00757 #define SIP_REINVITE (7 << 20)
00758 #define SIP_CAN_REINVITE (1 << 20)
00759 #define SIP_CAN_REINVITE_NAT (2 << 20)
00760 #define SIP_REINVITE_UPDATE (4 << 20)
00761
00762 #define SIP_INSECURE_PORT (1 << 23)
00763 #define SIP_INSECURE_INVITE (1 << 24)
00764
00765 #define SIP_PROG_INBAND (3 << 25)
00766 #define SIP_PROG_INBAND_NEVER (0 << 25)
00767 #define SIP_PROG_INBAND_NO (1 << 25)
00768 #define SIP_PROG_INBAND_YES (2 << 25)
00769 #define SIP_NO_HISTORY (1 << 27)
00770 #define SIP_CALL_LIMIT (1 << 28)
00771 #define SIP_SENDRPID (1 << 29)
00772 #define SIP_INC_COUNT (1 << 30)
00773 #define SIP_G726_NONSTANDARD (1 << 31)
00774
00775 #define SIP_FLAGS_TO_COPY \
00776 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
00777 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
00778 SIP_USEREQPHONE | SIP_INSECURE_PORT | SIP_INSECURE_INVITE)
00779
00780
00781
00782 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
00783 #define SIP_PAGE2_RTUPDATE (1 << 1)
00784 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
00785 #define SIP_PAGE2_RT_FROMCONTACT (1 << 4)
00786 #define SIP_PAGE2_RTSAVE_SYSNAME (1 << 5)
00787
00788 #define SIP_PAGE2_STATECHANGEQUEUE (1 << 9)
00789 #define SIP_PAGE2_IGNOREREGEXPIRE (1 << 10)
00790 #define SIP_PAGE2_DEBUG (3 << 11)
00791 #define SIP_PAGE2_DEBUG_CONFIG (1 << 11)
00792 #define SIP_PAGE2_DEBUG_CONSOLE (1 << 12)
00793 #define SIP_PAGE2_DYNAMIC (1 << 13)
00794 #define SIP_PAGE2_SELFDESTRUCT (1 << 14)
00795 #define SIP_PAGE2_VIDEOSUPPORT (1 << 15)
00796 #define SIP_PAGE2_ALLOWSUBSCRIBE (1 << 16)
00797 #define SIP_PAGE2_ALLOWOVERLAP (1 << 17)
00798 #define SIP_PAGE2_SUBSCRIBEMWIONLY (1 << 18)
00799 #define SIP_PAGE2_INC_RINGING (1 << 19)
00800 #define SIP_PAGE2_T38SUPPORT (7 << 20)
00801 #define SIP_PAGE2_T38SUPPORT_UDPTL (1 << 20)
00802 #define SIP_PAGE2_T38SUPPORT_RTP (2 << 20)
00803 #define SIP_PAGE2_T38SUPPORT_TCP (4 << 20)
00804 #define SIP_PAGE2_CALL_ONHOLD (3 << 23)
00805 #define SIP_PAGE2_CALL_ONHOLD_ACTIVE (1 << 23)
00806 #define SIP_PAGE2_CALL_ONHOLD_ONEDIR (2 << 23)
00807 #define SIP_PAGE2_CALL_ONHOLD_INACTIVE (3 << 23)
00808 #define SIP_PAGE2_RFC2833_COMPENSATE (1 << 25)
00809 #define SIP_PAGE2_BUGGY_MWI (1 << 26)
00810 #define SIP_PAGE2_OUTGOING_CALL (1 << 27)
00811 #define SIP_PAGE2_UDPTL_DESTINATION (1 << 28)
00812 #define SIP_PAGE2_DIALOG_ESTABLISHED (1 << 29)
00813 #define SIP_PAGE2_RPORT_PRESENT (1 << 30)
00814 #define SIP_PAGE2_CONSTANT_SSRC (1 << 31)
00815
00816 #define SIP_PAGE2_FLAGS_TO_COPY \
00817 (SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_VIDEOSUPPORT | \
00818 SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | SIP_PAGE2_BUGGY_MWI | \
00819 SIP_PAGE2_UDPTL_DESTINATION | SIP_PAGE2_CONSTANT_SSRC)
00820
00821
00822 #define SIP_PKT_DEBUG (1 << 0)
00823 #define SIP_PKT_WITH_TOTAG (1 << 1)
00824 #define SIP_PKT_IGNORE (1 << 2)
00825 #define SIP_PKT_IGNORE_REQ (1 << 4)
00826
00827
00828 #define T38FAX_FILL_BIT_REMOVAL (1 << 0)
00829 #define T38FAX_TRANSCODING_MMR (1 << 1)
00830 #define T38FAX_TRANSCODING_JBIG (1 << 2)
00831
00832 #define T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF (0 << 3)
00833 #define T38FAX_RATE_MANAGEMENT_LOCAL_TCF (1 << 3)
00834
00835 #define T38FAX_UDP_EC_NONE (0 << 4)
00836 #define T38FAX_UDP_EC_FEC (1 << 4)
00837 #define T38FAX_UDP_EC_REDUNDANCY (2 << 4)
00838
00839 #define T38FAX_VERSION (3 << 6)
00840 #define T38FAX_VERSION_0 (0 << 6)
00841 #define T38FAX_VERSION_1 (1 << 6)
00842
00843 #define T38FAX_RATE_2400 (1 << 8)
00844 #define T38FAX_RATE_4800 (1 << 9)
00845 #define T38FAX_RATE_7200 (1 << 10)
00846 #define T38FAX_RATE_9600 (1 << 11)
00847 #define T38FAX_RATE_12000 (1 << 12)
00848 #define T38FAX_RATE_14400 (1 << 13)
00849
00850
00851 static int global_t38_capability = T38FAX_VERSION_0 | T38FAX_RATE_2400 | T38FAX_RATE_4800 | T38FAX_RATE_7200 | T38FAX_RATE_9600;
00852
00853 #define sipdebug ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG)
00854 #define sipdebug_config ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG)
00855 #define sipdebug_console ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE)
00856
00857
00858 enum t38state {
00859 T38_DISABLED = 0,
00860 T38_LOCAL_REINVITE,
00861 T38_PEER_DIRECT,
00862 T38_PEER_REINVITE,
00863 T38_ENABLED
00864 };
00865
00866
00867 struct t38properties {
00868 struct ast_flags t38support;
00869 int capability;
00870 int peercapability;
00871 int jointcapability;
00872 enum t38state state;
00873 unsigned int direct:1;
00874 };
00875
00876
00877 enum referstatus {
00878 REFER_IDLE,
00879 REFER_SENT,
00880 REFER_RECEIVED,
00881 REFER_CONFIRMED,
00882 REFER_ACCEPTED,
00883 REFER_RINGING,
00884 REFER_200OK,
00885 REFER_FAILED,
00886 REFER_NOAUTH
00887 };
00888
00889 static const struct c_referstatusstring {
00890 enum referstatus status;
00891 char *text;
00892 } referstatusstrings[] = {
00893 { REFER_IDLE, "<none>" },
00894 { REFER_SENT, "Request sent" },
00895 { REFER_RECEIVED, "Request received" },
00896 { REFER_ACCEPTED, "Accepted" },
00897 { REFER_RINGING, "Target ringing" },
00898 { REFER_200OK, "Done" },
00899 { REFER_FAILED, "Failed" },
00900 { REFER_NOAUTH, "Failed - auth failure" }
00901 } ;
00902
00903
00904
00905 struct sip_refer {
00906 char refer_to[AST_MAX_EXTENSION];
00907 char refer_to_domain[AST_MAX_EXTENSION];
00908 char refer_to_urioption[AST_MAX_EXTENSION];
00909 char refer_to_context[AST_MAX_EXTENSION];
00910 char referred_by[AST_MAX_EXTENSION];
00911 char referred_by_name[AST_MAX_EXTENSION];
00912 char refer_contact[AST_MAX_EXTENSION];
00913 char replaces_callid[SIPBUFSIZE];
00914 char replaces_callid_totag[SIPBUFSIZE/2];
00915 char replaces_callid_fromtag[SIPBUFSIZE/2];
00916 struct sip_pvt *refer_call;
00917 int attendedtransfer;
00918 int localtransfer;
00919 enum referstatus status;
00920 };
00921
00922 struct offered_media {
00923 int offered;
00924 char text[128];
00925 };
00926
00927 static struct sip_pvt {
00928 ast_mutex_t lock;
00929 int method;
00930 enum invitestates invitestate;
00931 AST_DECLARE_STRING_FIELDS(
00932 AST_STRING_FIELD(callid);
00933 AST_STRING_FIELD(randdata);
00934 AST_STRING_FIELD(accountcode);
00935 AST_STRING_FIELD(realm);
00936 AST_STRING_FIELD(nonce);
00937 AST_STRING_FIELD(opaque);
00938 AST_STRING_FIELD(qop);
00939 AST_STRING_FIELD(domain);
00940 AST_STRING_FIELD(from);
00941 AST_STRING_FIELD(useragent);
00942 AST_STRING_FIELD(exten);
00943 AST_STRING_FIELD(context);
00944 AST_STRING_FIELD(subscribecontext);
00945 AST_STRING_FIELD(subscribeuri);
00946 AST_STRING_FIELD(fromdomain);
00947 AST_STRING_FIELD(fromuser);
00948 AST_STRING_FIELD(fromname);
00949 AST_STRING_FIELD(tohost);
00950 AST_STRING_FIELD(language);
00951 AST_STRING_FIELD(mohinterpret);
00952 AST_STRING_FIELD(mohsuggest);
00953 AST_STRING_FIELD(rdnis);
00954 AST_STRING_FIELD(theirtag);
00955 AST_STRING_FIELD(username);
00956 AST_STRING_FIELD(peername);
00957 AST_STRING_FIELD(authname);
00958 AST_STRING_FIELD(uri);
00959 AST_STRING_FIELD(okcontacturi);
00960 AST_STRING_FIELD(peersecret);
00961 AST_STRING_FIELD(peermd5secret);
00962 AST_STRING_FIELD(cid_num);
00963 AST_STRING_FIELD(cid_name);
00964 AST_STRING_FIELD(fullcontact);
00965 AST_STRING_FIELD(our_contact);
00966 AST_STRING_FIELD(rpid);
00967 AST_STRING_FIELD(rpid_from);
00968 );
00969 char via[128];
00970 unsigned int ocseq;
00971 unsigned int icseq;
00972 ast_group_t callgroup;
00973 ast_group_t pickupgroup;
00974 int lastinvite;
00975 struct ast_flags flags[2];
00976 int timer_t1;
00977 unsigned int sipoptions;
00978 struct ast_codec_pref prefs;
00979 int capability;
00980 int jointcapability;
00981 int peercapability;
00982 int prefcodec;
00983 int noncodeccapability;
00984 int jointnoncodeccapability;
00985 int redircodecs;
00986 int maxcallbitrate;
00987 struct t38properties t38;
00988 struct sockaddr_in udptlredirip;
00989 struct ast_udptl *udptl;
00990 int callingpres;
00991 int authtries;
00992 int expiry;
00993 long branch;
00994 long invite_branch;
00995 char tag[11];
00996 int sessionid;
00997 int sessionversion;
00998 unsigned int portinuri:1;
00999 struct sockaddr_in sa;
01000 struct sockaddr_in redirip;
01001 struct sockaddr_in vredirip;
01002 time_t lastrtprx;
01003 time_t lastrtptx;
01004 int rtptimeout;
01005 struct sockaddr_in recv;
01006 struct in_addr ourip;
01007 struct ast_channel *owner;
01008 struct sip_route *route;
01009 int route_persistant;
01010 struct sip_auth *peerauth;
01011 int noncecount;
01012 unsigned int stalenonce:1;
01013 char lastmsg[256];
01014 int amaflags;
01015 int pendinginvite;
01016 int glareinvite;
01017
01018
01019 struct sip_request initreq;
01020
01021
01022 int maxtime;
01023 int initid;
01024 int waitid;
01025 int autokillid;
01026 enum transfermodes allowtransfer;
01027 struct sip_refer *refer;
01028 enum subscriptiontype subscribed;
01029 int stateid;
01030 int laststate;
01031 int dialogver;
01032
01033 struct ast_dsp *vad;
01034
01035 struct sip_peer *relatedpeer;
01036
01037 struct sip_registry *registry;
01038 struct ast_rtp *rtp;
01039 struct ast_rtp *vrtp;
01040 struct sip_pkt *packets;
01041 struct sip_history_head *history;
01042 size_t history_entries;
01043 struct ast_variable *chanvars;
01044 AST_LIST_HEAD_NOLOCK(request_queue, sip_request) request_queue;
01045 int request_queue_sched_id;
01046 int provisional_keepalive_sched_id;
01047 const char *last_provisional;
01048 struct sip_pvt *next;
01049 struct sip_invite_param *options;
01050 int autoframing;
01051 int hangupcause;
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066 struct offered_media offered_media[3];
01067 } *iflist = NULL;
01068
01069
01070 #define MAX_HISTORY_ENTRIES 50
01071
01072 #define FLAG_RESPONSE (1 << 0)
01073 #define FLAG_FATAL (1 << 1)
01074
01075
01076 struct sip_pkt {
01077 struct sip_pkt *next;
01078 int retrans;
01079 int method;
01080 int seqno;
01081 int response_code;
01082 unsigned int flags;
01083 struct sip_pvt *owner;
01084 int retransid;
01085 int timer_a;
01086 int timer_t1;
01087 int packetlen;
01088 char data[0];
01089 };
01090
01091
01092 struct sip_user {
01093
01094 ASTOBJ_COMPONENTS(struct sip_user);
01095 char secret[80];
01096 char md5secret[80];
01097 char context[AST_MAX_CONTEXT];
01098 char subscribecontext[AST_MAX_CONTEXT];
01099 char cid_num[80];
01100 char cid_name[80];
01101 char accountcode[AST_MAX_ACCOUNT_CODE];
01102 char language[MAX_LANGUAGE];
01103 char mohinterpret[MAX_MUSICCLASS];
01104 char mohsuggest[MAX_MUSICCLASS];
01105 char useragent[256];
01106 struct ast_codec_pref prefs;
01107 ast_group_t callgroup;
01108 ast_group_t pickupgroup;
01109 unsigned int sipoptions;
01110 struct ast_flags flags[2];
01111 int amaflags;
01112 int callingpres;
01113 int capability;
01114 int inUse;
01115 int call_limit;
01116 enum transfermodes allowtransfer;
01117 struct ast_ha *ha;
01118 struct ast_variable *chanvars;
01119 int maxcallbitrate;
01120 int autoframing;
01121 };
01122
01123
01124
01125 struct sip_peer {
01126 ASTOBJ_COMPONENTS(struct sip_peer);
01127
01128 char secret[80];
01129 char md5secret[80];
01130 struct sip_auth *auth;
01131 char context[AST_MAX_CONTEXT];
01132 char subscribecontext[AST_MAX_CONTEXT];
01133 char username[80];
01134 char accountcode[AST_MAX_ACCOUNT_CODE];
01135 int amaflags;
01136 char tohost[MAXHOSTNAMELEN];
01137 char regexten[AST_MAX_EXTENSION];
01138 char fromuser[80];
01139 char fromdomain[MAXHOSTNAMELEN];
01140 char fullcontact[256];
01141 char cid_num[80];
01142 char cid_name[80];
01143 int callingpres;
01144 int inUse;
01145 int inRinging;
01146 int onHold;
01147 int call_limit;
01148 enum transfermodes allowtransfer;
01149 char vmexten[AST_MAX_EXTENSION];
01150 char mailbox[AST_MAX_EXTENSION];
01151 char language[MAX_LANGUAGE];
01152 char mohinterpret[MAX_MUSICCLASS];
01153 char mohsuggest[MAX_MUSICCLASS];
01154 char useragent[256];
01155 struct ast_codec_pref prefs;
01156 int lastmsgssent;
01157 time_t lastmsgcheck;
01158 unsigned int sipoptions;
01159 struct ast_flags flags[2];
01160 int expire;
01161 int capability;
01162 int rtptimeout;
01163 int rtpholdtimeout;
01164 int rtpkeepalive;
01165 ast_group_t callgroup;
01166 ast_group_t pickupgroup;
01167 struct sockaddr_in addr;
01168 int maxcallbitrate;
01169 unsigned int portinuri:1;
01170
01171
01172 struct sip_pvt *call;
01173 int pokeexpire;
01174 int lastms;
01175 int maxms;
01176 struct timeval ps;
01177
01178 struct sockaddr_in defaddr;
01179 struct ast_ha *ha;
01180 struct ast_ha *contactha;
01181 struct ast_variable *chanvars;
01182 struct sip_pvt *mwipvt;
01183 int lastmsg;
01184 int autoframing;
01185 };
01186
01187
01188
01189
01190 struct sip_registry {
01191 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
01192 AST_DECLARE_STRING_FIELDS(
01193 AST_STRING_FIELD(callid);
01194 AST_STRING_FIELD(realm);
01195 AST_STRING_FIELD(nonce);
01196 AST_STRING_FIELD(opaque);
01197 AST_STRING_FIELD(qop);
01198 AST_STRING_FIELD(domain);
01199 AST_STRING_FIELD(username);
01200 AST_STRING_FIELD(authuser);
01201 AST_STRING_FIELD(hostname);
01202 AST_STRING_FIELD(secret);
01203 AST_STRING_FIELD(md5secret);
01204 AST_STRING_FIELD(contact);
01205 AST_STRING_FIELD(random);
01206 );
01207 int portno;
01208 int expire;
01209 int regattempts;
01210 int timeout;
01211 int refresh;
01212 struct sip_pvt *call;
01213 enum sipregistrystate regstate;
01214 unsigned int needdns:1;
01215 time_t regtime;
01216 int callid_valid;
01217 unsigned int ocseq;
01218 struct sockaddr_in us;
01219 int noncecount;
01220 char lastmsg[256];
01221 };
01222
01223
01224
01225
01226 static struct ast_user_list {
01227 ASTOBJ_CONTAINER_COMPONENTS(struct sip_user);
01228 } userl;
01229
01230
01231 static struct ast_peer_list {
01232 ASTOBJ_CONTAINER_COMPONENTS(struct sip_peer);
01233 } peerl;
01234
01235
01236 static struct ast_register_list {
01237 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01238 int recheck;
01239 } regl;
01240
01241 static void temp_pvt_cleanup(void *);
01242
01243
01244 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01245
01246 #ifdef LOW_MEMORY
01247 static void ts_ast_rtp_destroy(void *);
01248
01249 AST_THREADSTORAGE_CUSTOM(ts_audio_rtp, ts_audio_rtp_init, ts_ast_rtp_destroy);
01250 AST_THREADSTORAGE_CUSTOM(ts_video_rtp, ts_video_rtp_init, ts_ast_rtp_destroy);
01251 #endif
01252
01253
01254 static struct sip_auth *authl = NULL;
01255
01256
01257
01258 static int sipsock = -1;
01259 static struct sockaddr_in bindaddr = { 0, };
01260 static struct sockaddr_in externip;
01261 static char externhost[MAXHOSTNAMELEN];
01262 static time_t externexpire = 0;
01263 static int externrefresh = 10;
01264 static struct ast_ha *localaddr;
01265 static struct in_addr __ourip;
01266 static struct sockaddr_in outboundproxyip;
01267 static int ourport;
01268 static struct sockaddr_in debugaddr;
01269
01270 static struct ast_config *notify_types;
01271
01272
01273
01274
01275
01276
01277 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
01278 static int sip_devicestate(void *data);
01279 static int sip_sendtext(struct ast_channel *ast, const char *text);
01280 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01281 static int sip_hangup(struct ast_channel *ast);
01282 static int sip_answer(struct ast_channel *ast);
01283 static struct ast_frame *sip_read(struct ast_channel *ast);
01284 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01285 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01286 static int sip_transfer(struct ast_channel *ast, const char *dest);
01287 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01288 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01289 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01290
01291
01292 static int sipsock_read(int *id, int fd, short events, void *ignore);
01293 static int __sip_xmit(struct sip_pvt *p, char *data, int len);
01294 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod);
01295 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01296 static int retrans_pkt(const void *data);
01297 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req);
01298 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);
01299 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01300 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01301 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01302 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01303 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01304 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);
01305 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp);
01306 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01307 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable);
01308 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01309 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01310 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
01311 static int transmit_reinvite_with_sdp(struct sip_pvt *p);
01312 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01313 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01314 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01315 static int transmit_refer(struct sip_pvt *p, const char *dest);
01316 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten);
01317 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01318 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01319 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01320 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01321 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01322 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01323 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
01324 static int sip_send_mwi_to_peer(struct sip_peer *peer, int force);
01325 static int does_peer_need_mwi(struct sip_peer *peer);
01326
01327
01328 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
01329 int useglobal_nat, const int intended_method);
01330 static int __sip_autodestruct(const void *data);
01331 static void sip_scheddestroy(struct sip_pvt *p, int ms);
01332 static int sip_cancel_destroy(struct sip_pvt *p);
01333 static void sip_destroy(struct sip_pvt *p);
01334 static int __sip_destroy(struct sip_pvt *p, int lockowner);
01335 static int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01336 static void __sip_pretend_ack(struct sip_pvt *p);
01337 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01338 static int auto_congest(const void *nothing);
01339 static int update_call_counter(struct sip_pvt *fup, int event);
01340 static int hangup_sip2cause(int cause);
01341 static const char *hangup_cause2sip(int cause);
01342 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method);
01343 static void free_old_route(struct sip_route *route);
01344 static void list_route(struct sip_route *route);
01345 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01346 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
01347 struct sip_request *req, char *uri);
01348 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01349 static void check_pendings(struct sip_pvt *p);
01350 static void *sip_park_thread(void *stuff);
01351 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno);
01352 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01353
01354
01355 static void try_suggested_sip_codec(struct sip_pvt *p);
01356 static const char *get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01357 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value);
01358 static int find_sdp(struct sip_request *req);
01359 static int process_sdp(struct sip_pvt *p, struct sip_request *req);
01360 static int process_sdp_c(const char *c, struct ast_hostent *hp);
01361 static int process_sdp_a_sendonly(const char *a, int *sendonly);
01362 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp *newaudiortp, int *last_rtpmap_codec);
01363 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp *newvideortp, int *last_rtpmap_codec);
01364 static int process_sdp_a_image(const char *a, struct sip_pvt *p);
01365 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
01366 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01367 int debug, int *min_packet_size);
01368 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
01369 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01370 int debug);
01371 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int add_audio, int add_t38);
01372 static void stop_media_flows(struct sip_pvt *p);
01373
01374
01375 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01376 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01377 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01378 const char *secret, const char *md5secret, int sipmethod,
01379 char *uri, enum xmittype reliable, int ignore);
01380 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01381 int sipmethod, char *uri, enum xmittype reliable,
01382 struct sockaddr_in *sin, struct sip_peer **authpeer);
01383 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin);
01384
01385
01386 static int check_sip_domain(const char *domain, char *context, size_t len);
01387 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01388 static void clear_sip_domains(void);
01389
01390
01391 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno);
01392 static int clear_realm_authentication(struct sip_auth *authlist);
01393 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
01394
01395
01396 static int sip_do_reload(enum channelreloadreason reason);
01397 static int reload_config(enum channelreloadreason reason);
01398 static int expire_register(const void *data);
01399 static void *do_monitor(void *data);
01400 static int restart_monitor(void);
01401 static int sip_addrcmp(char *name, struct sockaddr_in *sin);
01402 static int sip_refer_allocate(struct sip_pvt *p);
01403 static void ast_quiet_chan(struct ast_channel *chan);
01404 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01405
01406
01407 static int cb_extensionstate(char *context, char* exten, int state, void *data);
01408 static int sip_devicestate(void *data);
01409 static int sip_poke_noanswer(const void *data);
01410 static int sip_poke_peer(struct sip_peer *peer);
01411 static void sip_poke_all_peers(void);
01412 static void sip_peer_hold(struct sip_pvt *p, int hold);
01413
01414
01415 static const char *sip_nat_mode(const struct sip_pvt *p);
01416 static int sip_show_inuse(int fd, int argc, char *argv[]);
01417 static char *transfermode2str(enum transfermodes mode) attribute_const;
01418 static char *nat2str(int nat) attribute_const;
01419 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01420 static int sip_show_users(int fd, int argc, char *argv[]);
01421 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01422 static int sip_show_peers(int fd, int argc, char *argv[]);
01423 static int sip_show_objects(int fd, int argc, char *argv[]);
01424 static void print_group(int fd, ast_group_t group, int crlf);
01425 static const char *dtmfmode2str(int mode) attribute_const;
01426 static const char *insecure2str(int port, int invite) attribute_const;
01427 static void cleanup_stale_contexts(char *new, char *old);
01428 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01429 static const char *domain_mode_to_text(const enum domain_mode mode);
01430 static int sip_show_domains(int fd, int argc, char *argv[]);
01431 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01432 static int sip_show_peer(int fd, int argc, char *argv[]);
01433 static int sip_show_user(int fd, int argc, char *argv[]);
01434 static int sip_show_registry(int fd, int argc, char *argv[]);
01435 static int sip_show_settings(int fd, int argc, char *argv[]);
01436 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01437 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01438 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01439 static int sip_show_channels(int fd, int argc, char *argv[]);
01440 static int sip_show_subscriptions(int fd, int argc, char *argv[]);
01441 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01442 static char *complete_sipch(const char *line, const char *word, int pos, int state);
01443 static char *complete_sip_peer(const char *word, int state, int flags2);
01444 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01445 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state);
01446 static char *complete_sip_user(const char *word, int state, int flags2);
01447 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
01448 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01449 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state);
01450 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state);
01451 static int sip_show_channel(int fd, int argc, char *argv[]);
01452 static int sip_show_history(int fd, int argc, char *argv[]);
01453 static int sip_do_debug_ip(int fd, int argc, char *argv[]);
01454 static int sip_do_debug_peer(int fd, int argc, char *argv[]);
01455 static int sip_do_debug(int fd, int argc, char *argv[]);
01456 static int sip_no_debug(int fd, int argc, char *argv[]);
01457 static int sip_notify(int fd, int argc, char *argv[]);
01458 static int sip_do_history(int fd, int argc, char *argv[]);
01459 static int sip_no_history(int fd, int argc, char *argv[]);
01460 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
01461 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01462 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01463 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01464 static int sip_dtmfmode(struct ast_channel *chan, void *data);
01465 static int sip_addheader(struct ast_channel *chan, void *data);
01466 static int sip_do_reload(enum channelreloadreason reason);
01467 static int sip_reload(int fd, int argc, char *argv[]);
01468 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen);
01469
01470
01471
01472
01473
01474 static void sip_dump_history(struct sip_pvt *dialog);
01475 static inline int sip_debug_test_addr(const struct sockaddr_in *addr);
01476 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01477 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01478 static void sip_dump_history(struct sip_pvt *dialog);
01479
01480
01481 static struct sip_peer *temp_peer(const char *name);
01482 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only);
01483 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01484 static int update_call_counter(struct sip_pvt *fup, int event);
01485 static void sip_destroy_peer(struct sip_peer *peer);
01486 static void sip_destroy_user(struct sip_user *user);
01487 static int sip_poke_peer(struct sip_peer *peer);
01488 static int sip_poke_peer_s(const void *data);
01489 static void set_peer_defaults(struct sip_peer *peer);
01490 static struct sip_peer *temp_peer(const char *name);
01491 static void register_peer_exten(struct sip_peer *peer, int onoff);
01492 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int devstate_only);
01493 static struct sip_user *find_user(const char *name, int realtime);
01494 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01495 static int expire_register(const void *data);
01496 static void reg_source_db(struct sip_peer *peer);
01497 static void destroy_association(struct sip_peer *peer);
01498 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01499
01500
01501 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey, int lastms);
01502 static struct sip_user *realtime_user(const char *username);
01503 static void update_peer(struct sip_peer *p, int expiry);
01504 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin, int devstate_only);
01505 static int sip_prune_realtime(int fd, int argc, char *argv[]);
01506
01507
01508 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us);
01509 static void sip_registry_destroy(struct sip_registry *reg);
01510 static int sip_register(char *value, int lineno);
01511 static char *regstate2str(enum sipregistrystate regstate) attribute_const;
01512 static int sip_reregister(const void *data);
01513 static int __sip_do_register(struct sip_registry *r);
01514 static int sip_reg_timeout(const void *data);
01515 static void sip_send_all_registers(void);
01516
01517
01518 static void append_date(struct sip_request *req);
01519 static int determine_firstline_parts(struct sip_request *req);
01520 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01521 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01522 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
01523 static int find_sip_method(const char *msg);
01524 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
01525 static int parse_request(struct sip_request *req);
01526 static const char *get_header(const struct sip_request *req, const char *name);
01527 static char *referstatus2str(enum referstatus rstatus) attribute_pure;
01528 static int method_match(enum sipmethod id, const char *name);
01529 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
01530 static char *get_in_brackets(char *tmp);
01531 static const char *find_alias(const char *name, const char *_default);
01532 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
01533 static int lws2sws(char *msgbuf, int len);
01534 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
01535 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
01536 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
01537 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
01538 static int set_address_from_contact(struct sip_pvt *pvt);
01539 static void check_via(struct sip_pvt *p, const struct sip_request *req);
01540 static char *get_calleridname(const char *input, char *output, size_t outputsize);
01541 static int get_rpid_num(const char *input, char *output, int maxlen);
01542 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq);
01543 static int get_destination(struct sip_pvt *p, struct sip_request *oreq);
01544 static int get_msg_text(char *buf, int len, struct sip_request *req);
01545 static void free_old_route(struct sip_route *route);
01546 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
01547
01548
01549 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
01550 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
01551 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
01552 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod);
01553 static int init_resp(struct sip_request *resp, const char *msg);
01554 static inline int resp_needs_contact(const char *msg, enum sipmethod method);
01555 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
01556 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p);
01557 static void build_via(struct sip_pvt *p);
01558 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
01559 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct sockaddr_in *sin);
01560 static char *generate_random_string(char *buf, size_t size);
01561 static void build_callid_pvt(struct sip_pvt *pvt);
01562 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain);
01563 static void make_our_tag(char *tagbuf, size_t len);
01564 static int add_header(struct sip_request *req, const char *var, const char *value);
01565 static int add_header_contentLength(struct sip_request *req, int len);
01566 static int add_line(struct sip_request *req, const char *line);
01567 static int add_text(struct sip_request *req, const char *text);
01568 static int add_digit(struct sip_request *req, char digit, unsigned int duration);
01569 static int add_vidupdate(struct sip_request *req);
01570 static void add_route(struct sip_request *req, struct sip_route *route);
01571 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01572 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01573 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
01574 static void set_destination(struct sip_pvt *p, char *uri);
01575 static void append_date(struct sip_request *req);
01576 static void build_contact(struct sip_pvt *p);
01577 static void build_rpid(struct sip_pvt *p);
01578
01579
01580 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock);
01581 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);
01582 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock);
01583 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
01584 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e);
01585 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
01586 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
01587 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01588 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
01589 static int handle_request_options(struct sip_pvt *p, struct sip_request *req);
01590 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin, int *nounlock);
01591 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01592 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno);
01593
01594
01595 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01596 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01597 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01598 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01599
01600
01601 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active);
01602 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01603 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01604 static int sip_get_codec(struct ast_channel *chan);
01605 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect);
01606
01607
01608 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite);
01609 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
01610 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p);
01611 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
01612 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
01613
01614
01615 static const struct ast_channel_tech sip_tech = {
01616 .type = "SIP",
01617 .description = "Session Initiation Protocol (SIP)",
01618 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01619 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01620 .requester = sip_request_call,
01621 .devicestate = sip_devicestate,
01622 .call = sip_call,
01623 .hangup = sip_hangup,
01624 .answer = sip_answer,
01625 .read = sip_read,
01626 .write = sip_write,
01627 .write_video = sip_write,
01628 .indicate = sip_indicate,
01629 .transfer = sip_transfer,
01630 .fixup = sip_fixup,
01631 .send_digit_begin = sip_senddigit_begin,
01632 .send_digit_end = sip_senddigit_end,
01633 .bridge = ast_rtp_bridge,
01634 .send_text = sip_sendtext,
01635 .func_channel_read = acf_channel_read,
01636 };
01637
01638
01639
01640
01641 static const struct ast_channel_tech sip_tech_info = {
01642 .type = "SIP",
01643 .description = "Session Initiation Protocol (SIP)",
01644 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01645 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01646 .requester = sip_request_call,
01647 .devicestate = sip_devicestate,
01648 .call = sip_call,
01649 .hangup = sip_hangup,
01650 .answer = sip_answer,
01651 .read = sip_read,
01652 .write = sip_write,
01653 .write_video = sip_write,
01654 .indicate = sip_indicate,
01655 .transfer = sip_transfer,
01656 .fixup = sip_fixup,
01657 .send_digit_end = sip_senddigit_end,
01658 .bridge = ast_rtp_bridge,
01659 .send_text = sip_sendtext,
01660 .func_channel_read = acf_channel_read,
01661 };
01662
01663
01664
01665 #define UNLINK(element, head, prev) do { \
01666 if (prev) \
01667 (prev)->next = (element)->next; \
01668 else \
01669 (head) = (element)->next; \
01670 } while (0)
01671
01672
01673 static struct ast_rtp_protocol sip_rtp = {
01674 type: "SIP",
01675 get_rtp_info: sip_get_rtp_peer,
01676 get_vrtp_info: sip_get_vrtp_peer,
01677 set_rtp_peer: sip_set_rtp_peer,
01678 get_codec: sip_get_codec,
01679 };
01680
01681
01682 static struct ast_udptl_protocol sip_udptl = {
01683 type: "SIP",
01684 get_udptl_info: sip_get_udptl_peer,
01685 set_udptl_peer: sip_set_udptl_peer,
01686 };
01687
01688
01689 static char *referstatus2str(enum referstatus rstatus)
01690 {
01691 int i = (sizeof(referstatusstrings) / sizeof(referstatusstrings[0]));
01692 int x;
01693
01694 for (x = 0; x < i; x++) {
01695 if (referstatusstrings[x].status == rstatus)
01696 return (char *) referstatusstrings[x].text;
01697 }
01698 return "";
01699 }
01700
01701
01702
01703
01704 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
01705 {
01706 if (p->initreq.headers && option_debug) {
01707 ast_log(LOG_DEBUG, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
01708 }
01709
01710 copy_request(&p->initreq, req);
01711 parse_request(&p->initreq);
01712 if (ast_test_flag(req, SIP_PKT_DEBUG))
01713 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
01714 }
01715
01716 static void sip_alreadygone(struct sip_pvt *dialog)
01717 {
01718 if (option_debug > 2)
01719 ast_log(LOG_DEBUG, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
01720 ast_set_flag(&dialog->flags[0], SIP_ALREADYGONE);
01721 }
01722
01723
01724
01725
01726
01727
01728
01729
01730 static int method_match(enum sipmethod id, const char *name)
01731 {
01732 int len = strlen(sip_methods[id].text);
01733 int l_name = name ? strlen(name) : 0;
01734
01735 return (l_name >= len && name[len] < 33 &&
01736 !strncasecmp(sip_methods[id].text, name, len));
01737 }
01738
01739
01740 static int find_sip_method(const char *msg)
01741 {
01742 int i, res = 0;
01743
01744 if (ast_strlen_zero(msg))
01745 return 0;
01746 for (i = 1; i < (sizeof(sip_methods) / sizeof(sip_methods[0])) && !res; i++) {
01747 if (method_match(i, msg))
01748 res = sip_methods[i].id;
01749 }
01750 return res;
01751 }
01752
01753
01754 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
01755 {
01756 char *next, *sep;
01757 char *temp;
01758 unsigned int profile = 0;
01759 int i, found;
01760
01761 if (ast_strlen_zero(supported) )
01762 return 0;
01763 temp = ast_strdupa(supported);
01764
01765 if (option_debug > 2 && sipdebug)
01766 ast_log(LOG_DEBUG, "Begin: parsing SIP \"Supported: %s\"\n", supported);
01767
01768 for (next = temp; next; next = sep) {
01769 found = FALSE;
01770 if ( (sep = strchr(next, ',')) != NULL)
01771 *sep++ = '\0';
01772 next = ast_skip_blanks(next);
01773 if (option_debug > 2 && sipdebug)
01774 ast_log(LOG_DEBUG, "Found SIP option: -%s-\n", next);
01775 for (i=0; i < (sizeof(sip_options) / sizeof(sip_options[0])); i++) {
01776 if (!strcasecmp(next, sip_options[i].text)) {
01777 profile |= sip_options[i].id;
01778 found = TRUE;
01779 if (option_debug > 2 && sipdebug)
01780 ast_log(LOG_DEBUG, "Matched SIP option: %s\n", next);
01781 break;
01782 }
01783 }
01784 if (!found && option_debug > 2 && sipdebug) {
01785 if (!strncasecmp(next, "x-", 2))
01786 ast_log(LOG_DEBUG, "Found private SIP option, not supported: %s\n", next);
01787 else
01788 ast_log(LOG_DEBUG, "Found no match for SIP option: %s (Please file bug report!)\n", next);
01789 }
01790 }
01791
01792 if (pvt)
01793 pvt->sipoptions = profile;
01794 return profile;
01795 }
01796
01797
01798 static inline int sip_debug_test_addr(const struct sockaddr_in *addr)
01799 {
01800 if (!sipdebug)
01801 return 0;
01802 if (debugaddr.sin_addr.s_addr) {
01803 if (((ntohs(debugaddr.sin_port) != 0)
01804 && (debugaddr.sin_port != addr->sin_port))
01805 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
01806 return 0;
01807 }
01808 return 1;
01809 }
01810
01811
01812 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
01813 {
01814 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
01815 }
01816
01817
01818 static const char *sip_nat_mode(const struct sip_pvt *p)
01819 {
01820 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
01821 }
01822
01823
01824 static inline int sip_debug_test_pvt(struct sip_pvt *p)
01825 {
01826 if (!sipdebug)
01827 return 0;
01828 return sip_debug_test_addr(sip_real_dst(p));
01829 }
01830
01831
01832 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
01833 {
01834 int res;
01835 const struct sockaddr_in *dst = sip_real_dst(p);
01836 res = sendto(sipsock, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
01837
01838 if (res == -1) {
01839 switch (errno) {
01840 case EBADF:
01841 case EHOSTUNREACH:
01842 case ENETDOWN:
01843 case ENETUNREACH:
01844 case ECONNREFUSED:
01845 res = XMIT_ERROR;
01846 }
01847
01848 }
01849
01850 if (res != len)
01851 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));
01852 return res;
01853 }
01854
01855
01856
01857 static void build_via(struct sip_pvt *p)
01858 {
01859
01860 const char *rport = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_RFC3581 ? ";rport" : "";
01861
01862
01863 snprintf(p->via, sizeof(p->via), "SIP/2.0/UDP %s:%d;branch=z9hG4bK%08x%s",
01864 ast_inet_ntoa(p->ourip), ourport, (int) p->branch, rport);
01865 }
01866
01867
01868
01869
01870
01871
01872
01873 static enum sip_result ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us)
01874 {
01875 struct sockaddr_in theirs, ours;
01876
01877
01878 ast_ouraddrfor(them, us);
01879 theirs.sin_addr = *them;
01880 ours.sin_addr = *us;
01881
01882 if (localaddr && externip.sin_addr.s_addr &&
01883 (ast_apply_ha(localaddr, &theirs)) &&
01884 (!global_matchexterniplocally || !ast_apply_ha(localaddr, &ours))) {
01885 if (externexpire && time(NULL) >= externexpire) {
01886 struct ast_hostent ahp;
01887 struct hostent *hp;
01888
01889 externexpire = time(NULL) + externrefresh;
01890 if ((hp = ast_gethostbyname(externhost, &ahp))) {
01891 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
01892 } else
01893 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
01894 }
01895 *us = externip.sin_addr;
01896 if (option_debug) {
01897 ast_log(LOG_DEBUG, "Target address %s is not local, substituting externip\n",
01898 ast_inet_ntoa(*(struct in_addr *)&them->s_addr));
01899 }
01900 } else if (bindaddr.sin_addr.s_addr)
01901 *us = bindaddr.sin_addr;
01902 return AST_SUCCESS;
01903 }
01904
01905
01906
01907 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
01908
01909 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01910 __attribute__((format(printf, 2, 3)));
01911
01912
01913 static void __attribute__((format(printf, 2, 0))) append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
01914 {
01915 char buf[80], *c = buf;
01916 struct sip_history *hist;
01917 int l;
01918
01919 vsnprintf(buf, sizeof(buf), fmt, ap);
01920 strsep(&c, "\r\n");
01921 l = strlen(buf) + 1;
01922 if (!(hist = ast_calloc(1, sizeof(*hist) + l)))
01923 return;
01924 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
01925 free(hist);
01926 return;
01927 }
01928 memcpy(hist->event, buf, l);
01929 if (p->history_entries == MAX_HISTORY_ENTRIES) {
01930 struct sip_history *oldest;
01931 oldest = AST_LIST_REMOVE_HEAD(p->history, list);
01932 p->history_entries--;
01933 free(oldest);
01934 }
01935 AST_LIST_INSERT_TAIL(p->history, hist, list);
01936 p->history_entries++;
01937 }
01938
01939
01940 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01941 {
01942 va_list ap;
01943
01944 if (!p)
01945 return;
01946
01947 if (ast_test_flag(&p->flags[0], SIP_NO_HISTORY)
01948 && !recordhistory && !dumphistory) {
01949 return;
01950 }
01951
01952 va_start(ap, fmt);
01953 append_history_va(p, fmt, ap);
01954 va_end(ap);
01955
01956 return;
01957 }
01958
01959
01960 static int retrans_pkt(const void *data)
01961 {
01962 struct sip_pkt *pkt = (struct sip_pkt *)data, *prev, *cur = NULL;
01963 int reschedule = DEFAULT_RETRANS;
01964 int xmitres = 0;
01965
01966
01967 ast_mutex_lock(&pkt->owner->lock);
01968
01969 if (pkt->retrans < MAX_RETRANS) {
01970 pkt->retrans++;
01971 if (!pkt->timer_t1) {
01972 if (sipdebug && option_debug > 3)
01973 ast_log(LOG_DEBUG, "SIP TIMER: Not rescheduling id #%d:%s (Method %d) (No timer T1)\n", pkt->retransid, sip_methods[pkt->method].text, pkt->method);
01974 } else {
01975 int siptimer_a;
01976
01977 if (sipdebug && option_debug > 3)
01978 ast_log(LOG_DEBUG, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
01979 if (!pkt->timer_a)
01980 pkt->timer_a = 2 ;
01981 else
01982 pkt->timer_a = 2 * pkt->timer_a;
01983
01984
01985 siptimer_a = pkt->timer_t1 * pkt->timer_a;
01986 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
01987 siptimer_a = 4000;
01988
01989
01990 reschedule = siptimer_a;
01991 if (option_debug > 3)
01992 ast_log(LOG_DEBUG, "** 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);
01993 }
01994
01995 if (sip_debug_test_pvt(pkt->owner)) {
01996 const struct sockaddr_in *dst = sip_real_dst(pkt->owner);
01997 ast_verbose("Retransmitting #%d (%s) to %s:%d:\n%s\n---\n",
01998 pkt->retrans, sip_nat_mode(pkt->owner),
01999 ast_inet_ntoa(dst->sin_addr),
02000 ntohs(dst->sin_port), pkt->data);
02001 }
02002
02003 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data);
02004 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02005 ast_mutex_unlock(&pkt->owner->lock);
02006 if (xmitres == XMIT_ERROR)
02007 ast_log(LOG_WARNING, "Network error on retransmit in dialog %s\n", pkt->owner->callid);
02008 else
02009 return reschedule;
02010 }
02011
02012 if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
02013 if (ast_test_flag(pkt, FLAG_FATAL) || sipdebug)
02014 ast_log(LOG_WARNING, "Maximum retries exceeded on transmission %s from %s for packet seqno %d (%s %s %s)\n", pkt->owner->callid, pkt->owner->from, pkt->seqno, (ast_test_flag(pkt, FLAG_FATAL)) ? "Critical" : "Non-critical", (ast_test_flag(pkt, FLAG_RESPONSE)) ? "Response: " : "Request: ", sip_methods[pkt->method].text);
02015 } else if ((pkt->method == SIP_OPTIONS) && sipdebug) {
02016 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) -- See doc/sip-retransmit.txt.\n", pkt->owner->callid);
02017 }
02018 if (xmitres == XMIT_ERROR) {
02019 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission of transaction in call id %s \n", pkt->owner->callid);
02020 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02021 } else
02022 append_history(pkt->owner, "MaxRetries", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02023
02024 pkt->retransid = -1;
02025
02026 if (ast_test_flag(pkt, FLAG_FATAL)) {
02027 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
02028 DEADLOCK_AVOIDANCE(&pkt->owner->lock);
02029 }
02030
02031 if (pkt->owner->owner && !pkt->owner->owner->hangupcause)
02032 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
02033
02034 if (pkt->owner->owner) {
02035 sip_alreadygone(pkt->owner);
02036 ast_log(LOG_WARNING, "Hanging up call %s from channel %s . No reply to our critical packet after %d retries (see doc/sip-retransmit.txt).\n", pkt->owner->callid, pkt->owner->owner->name, pkt->retrans);
02037 ast_queue_hangup(pkt->owner->owner);
02038 ast_channel_unlock(pkt->owner->owner);
02039 } else {
02040
02041
02042
02043 if (pkt->method != SIP_OPTIONS) {
02044 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
02045 sip_alreadygone(pkt->owner);
02046 if (option_debug)
02047 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
02048 }
02049 }
02050 }
02051
02052 if (pkt->method == SIP_BYE) {
02053
02054 if (pkt->owner->owner)
02055 ast_channel_unlock(pkt->owner->owner);
02056 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
02057 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
02058 }
02059
02060
02061 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
02062 if (cur == pkt)
02063 break;
02064 }
02065 if (cur) {
02066 if (prev)
02067 prev->next = cur->next;
02068 else
02069 pkt->owner->packets = cur->next;
02070 ast_mutex_unlock(&pkt->owner->lock);
02071 free(cur);
02072 pkt = NULL;
02073 } else
02074 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
02075 if (pkt)
02076 ast_mutex_unlock(&pkt->owner->lock);
02077 return 0;
02078 }
02079
02080
02081
02082
02083 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod)
02084 {
02085 struct sip_pkt *pkt;
02086 int siptimer_a = DEFAULT_RETRANS;
02087 int xmitres = 0;
02088 int respid;
02089
02090 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1)))
02091 return AST_FAILURE;
02092 memcpy(pkt->data, data, len);
02093 pkt->method = sipmethod;
02094 pkt->packetlen = len;
02095 pkt->next = p->packets;
02096 pkt->owner = p;
02097 pkt->seqno = seqno;
02098 pkt->data[len] = '\0';
02099 if (resp) {
02100 ast_set_flag(pkt, FLAG_RESPONSE);
02101
02102 if (sscanf(pkt->data, "SIP/2.0 %30d", &respid) == 1) {
02103 pkt->response_code = respid;
02104 }
02105 }
02106 pkt->timer_t1 = p->timer_t1;
02107 pkt->retransid = -1;
02108 if (fatal)
02109 ast_set_flag(pkt, FLAG_FATAL);
02110 if (pkt->timer_t1)
02111 siptimer_a = pkt->timer_t1 * 2;
02112
02113 if (option_debug > 3 && sipdebug)
02114 ast_log(LOG_DEBUG, "*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->retransid);
02115 pkt->retransid = -1;
02116 pkt->next = p->packets;
02117 p->packets = pkt;
02118 if (sipmethod == SIP_INVITE) {
02119
02120 p->pendinginvite = seqno;
02121 }
02122
02123 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02124
02125 if (xmitres == XMIT_ERROR) {
02126 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02127 return AST_FAILURE;
02128 } else {
02129
02130 pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
02131 return AST_SUCCESS;
02132 }
02133 }
02134
02135
02136 static int __sip_autodestruct(const void *data)
02137 {
02138 struct sip_pvt *p = (struct sip_pvt *)data;
02139
02140
02141 if (p->subscribed) {
02142 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
02143 p->subscribed = NONE;
02144 append_history(p, "Subscribestatus", "timeout");
02145 if (option_debug > 2)
02146 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP subsription %s\n", p->callid ? p->callid : "<unknown>");
02147 return 10000;
02148 }
02149
02150
02151
02152
02153
02154
02155 if (p->packets && !ast_test_flag(&p->flags[0], SIP_NEEDDESTROY)) {
02156 char method_str[31];
02157 if (option_debug > 2)
02158 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP call %s\n", p->callid ? p->callid : "<unknown>");
02159 append_history(p, "ReliableXmit", "timeout");
02160 if (sscanf(p->lastmsg, "Tx: %30s", method_str) == 1 || sscanf(p->lastmsg, "Rx: %30s", method_str) == 1) {
02161 if (method_match(SIP_CANCEL, method_str) || method_match(SIP_BYE, method_str)) {
02162 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
02163 }
02164 }
02165 return 10000;
02166 }
02167
02168
02169 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
02170 ASTOBJ_UNREF(p->relatedpeer,sip_destroy_peer);
02171
02172
02173 p->autokillid = -1;
02174
02175 if (option_debug)
02176 ast_log(LOG_DEBUG, "Auto destroying SIP dialog '%s'\n", p->callid);
02177 append_history(p, "AutoDestroy", "%s", p->callid);
02178 if (p->owner) {
02179 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
02180 ast_queue_hangup(p->owner);
02181 } else if (p->refer && !ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
02182 if (option_debug > 2)
02183 ast_log(LOG_DEBUG, "Finally hanging up channel after transfer: %s\n", p->callid);
02184 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
02185 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
02186 } else
02187 sip_destroy(p);
02188 return 0;
02189 }
02190
02191
02192 static void sip_scheddestroy(struct sip_pvt *p, int ms)
02193 {
02194 if (ms < 0) {
02195 if (p->timer_t1 == 0)
02196 p->timer_t1 = 500;
02197 ms = p->timer_t1 * 64;
02198 }
02199 if (sip_debug_test_pvt(p))
02200 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
02201 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
02202 append_history(p, "SchedDestroy", "%d ms", ms);
02203
02204 AST_SCHED_DEL(sched, p->autokillid);
02205 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, p);
02206 }
02207
02208
02209 static int sip_cancel_destroy(struct sip_pvt *p)
02210 {
02211 int res = 0;
02212 if (p->autokillid > -1) {
02213 if (!(res = ast_sched_del(sched, p->autokillid))) {
02214 append_history(p, "CancelDestroy", "");
02215 p->autokillid = -1;
02216 }
02217 }
02218 return res;
02219 }
02220
02221
02222
02223 static int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02224 {
02225 struct sip_pkt *cur, *prev = NULL;
02226
02227
02228 char *msg;
02229 int res = FALSE;
02230
02231 msg = sip_methods[sipmethod].text;
02232
02233 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
02234 if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
02235 ((ast_test_flag(cur, FLAG_RESPONSE)) ||
02236 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
02237 if (!resp && (seqno == p->pendinginvite)) {
02238 if (option_debug)
02239 ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
02240 p->pendinginvite = 0;
02241 }
02242
02243 res = TRUE;
02244 UNLINK(cur, p->packets, prev);
02245 if (cur->retransid > -1) {
02246 if (sipdebug && option_debug > 3)
02247 ast_log(LOG_DEBUG, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
02248 }
02249
02250
02251
02252
02253
02254
02255
02256
02257
02258
02259
02260
02261
02262
02263
02264
02265 AST_SCHED_DEL_SPINLOCK(sched, cur->retransid, &p->lock);
02266 free(cur);
02267 break;
02268 }
02269 }
02270 if (option_debug)
02271 ast_log(LOG_DEBUG, "Stopping retransmission on '%s' of %s %d: Match %s\n", p->callid, resp ? "Response" : "Request", seqno, res == FALSE ? "Not Found" : "Found");
02272 return res;
02273 }
02274
02275
02276
02277 static void __sip_pretend_ack(struct sip_pvt *p)
02278 {
02279 struct sip_pkt *cur = NULL;
02280
02281 while (p->packets) {
02282 int method;
02283 if (cur == p->packets) {
02284 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
02285 return;
02286 }
02287 cur = p->packets;
02288 method = (cur->method) ? cur->method : find_sip_method(cur->data);
02289 __sip_ack(p, cur->seqno, ast_test_flag(cur, FLAG_RESPONSE), method);
02290 }
02291 }
02292
02293
02294 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02295 {
02296 struct sip_pkt *cur;
02297 int res = FALSE;
02298
02299 for (cur = p->packets; cur; cur = cur->next) {
02300 if (cur->seqno == seqno && ast_test_flag(cur, FLAG_RESPONSE) == resp &&
02301 (ast_test_flag(cur, FLAG_RESPONSE) || method_match(sipmethod, cur->data))) {
02302
02303 if (cur->retransid > -1) {
02304 if (option_debug > 3 && sipdebug)
02305 ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
02306 }
02307 AST_SCHED_DEL(sched, cur->retransid);
02308 res = TRUE;
02309 break;
02310 }
02311 }
02312 if (option_debug)
02313 ast_log(LOG_DEBUG, "(Provisional) Stopping retransmission (but retaining packet) on '%s' %s %d: %s\n", p->callid, resp ? "Response" : "Request", seqno, res == -1 ? "Not Found" : "Found");
02314 return res;
02315 }
02316
02317
02318
02319 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
02320 {
02321 memset(dst, 0, sizeof(*dst));
02322 memcpy(dst->data, src->data, sizeof(dst->data));
02323 dst->len = src->len;
02324 parse_request(dst);
02325 }
02326
02327
02328 static void add_blank(struct sip_request *req)
02329 {
02330 if (!req->lines) {
02331
02332 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
02333 req->len += strlen(req->data + req->len);
02334 }
02335 }
02336
02337 static int send_provisional_keepalive_full(struct sip_pvt *pvt, int with_sdp)
02338 {
02339 const char *msg = NULL;
02340
02341 if (!pvt->last_provisional || !strncasecmp(pvt->last_provisional, "100", 3)) {
02342 msg = "183 Session Progress";
02343 }
02344
02345 if (pvt->invitestate < INV_COMPLETED) {
02346 if (with_sdp) {
02347 transmit_response_with_sdp(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq, XMIT_UNRELIABLE);
02348 } else {
02349 transmit_response(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq);
02350 }
02351 return PROVIS_KEEPALIVE_TIMEOUT;
02352 }
02353
02354 return 0;
02355 }
02356
02357 static int send_provisional_keepalive(const void *data) {
02358 struct sip_pvt *pvt = (struct sip_pvt *) data;
02359
02360 return send_provisional_keepalive_full(pvt, 0);
02361 }
02362
02363 static int send_provisional_keepalive_with_sdp(const void *data) {
02364 struct sip_pvt *pvt = (void *)data;
02365
02366 return send_provisional_keepalive_full(pvt, 1);
02367 }
02368
02369 static void update_provisional_keepalive(struct sip_pvt *pvt, int with_sdp)
02370 {
02371 AST_SCHED_DEL(sched, pvt->provisional_keepalive_sched_id);
02372
02373 pvt->provisional_keepalive_sched_id = ast_sched_add(sched, PROVIS_KEEPALIVE_TIMEOUT,
02374 with_sdp ? send_provisional_keepalive_with_sdp : send_provisional_keepalive, pvt);
02375 }
02376
02377
02378 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02379 {
02380 int res;
02381
02382 add_blank(req);
02383 if (sip_debug_test_pvt(p)) {
02384 const struct sockaddr_in *dst = sip_real_dst(p);
02385
02386 ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
02387 reliable ? "Reliably " : "", sip_nat_mode(p),
02388 ast_inet_ntoa(dst->sin_addr),
02389 ntohs(dst->sin_port), req->data);
02390 }
02391 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02392 struct sip_request tmp;
02393 parse_copy(&tmp, req);
02394 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"),
02395 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? tmp.rlPart2 : sip_methods[tmp.method].text);
02396 }
02397
02398
02399 if (p->initreq.method == SIP_INVITE && reliable == XMIT_CRITICAL) {
02400 AST_SCHED_DEL(sched, p->provisional_keepalive_sched_id);
02401 }
02402
02403 res = (reliable) ?
02404 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02405 __sip_xmit(p, req->data, req->len);
02406 if (res > 0)
02407 return 0;
02408 return res;
02409 }
02410
02411
02412 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02413 {
02414 int res;
02415
02416 add_blank(req);
02417 if (sip_debug_test_pvt(p)) {
02418 if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
02419 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);
02420 else
02421 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);
02422 }
02423 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02424 struct sip_request tmp;
02425 parse_copy(&tmp, req);
02426 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
02427 }
02428 res = (reliable) ?
02429 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02430 __sip_xmit(p, req->data, req->len);
02431 return res;
02432 }
02433
02434
02435
02436
02437
02438 static const char *find_closing_quote(const char *start, const char *lim)
02439 {
02440 char last_char = '\0';
02441 const char *s;
02442 for (s = start; *s && s != lim; last_char = *s++) {
02443 if (*s == '"' && last_char != '\\')
02444 break;
02445 }
02446 return s;
02447 }
02448
02449
02450
02451
02452
02453
02454
02455
02456
02457
02458
02459
02460 static char *get_in_brackets(char *tmp)
02461 {
02462 const char *parse = tmp;
02463 char *first_bracket;
02464
02465
02466
02467
02468
02469 while ( (first_bracket = strchr(parse, '<')) ) {
02470 char *first_quote = strchr(parse, '"');
02471
02472 if (!first_quote || first_quote > first_bracket)
02473 break;
02474
02475 parse = find_closing_quote(first_quote + 1, NULL);
02476 if (!*parse) {
02477
02478 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
02479 break;
02480 }
02481 parse++;
02482 }
02483 if (first_bracket) {
02484 char *second_bracket = strchr(first_bracket + 1, '>');
02485 if (second_bracket) {
02486 *second_bracket = '\0';
02487 tmp = first_bracket + 1;
02488 } else {
02489 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
02490 }
02491 }
02492 return tmp;
02493 }
02494
02495
02496
02497 static int sip_sendtext(struct ast_channel *ast, const char *text)
02498 {
02499 struct sip_pvt *p = ast->tech_pvt;
02500 int debug = sip_debug_test_pvt(p);
02501
02502 if (debug)
02503 ast_verbose("Sending text %s on %s\n", text, ast->name);
02504 if (!p)
02505 return -1;
02506
02507
02508 if (!text)
02509 return 0;
02510 if (debug)
02511 ast_verbose("Really sending text %s on %s\n", text, ast->name);
02512 transmit_message_with_text(p, text);
02513 return 0;
02514 }
02515
02516
02517
02518
02519
02520
02521 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey, int lastms)
02522 {
02523 char port[10];
02524 char ipaddr[INET_ADDRSTRLEN];
02525 char regseconds[20];
02526 char str_lastms[20];
02527
02528 char *sysname = ast_config_AST_SYSTEM_NAME;
02529 char *syslabel = NULL;
02530
02531 time_t nowtime = time(NULL) + expirey;
02532 const char *fc = fullcontact ? "fullcontact" : NULL;
02533
02534 snprintf(str_lastms, sizeof(str_lastms), "%d", lastms);
02535 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
02536 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
02537 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
02538
02539 if (ast_strlen_zero(sysname))
02540 sysname = NULL;
02541 else if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME))
02542 syslabel = "regserver";
02543
02544 if (fc)
02545 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02546 "port", port, "regseconds", regseconds,
02547 "username", username, fc, fullcontact, syslabel, sysname, NULL);
02548 else
02549 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02550 "port", port, "regseconds", regseconds,
02551 "username", username, syslabel, sysname, NULL);
02552 if (seen_lastms) {
02553
02554
02555 ast_update_realtime("sippeers", "name", peername, "lastms", str_lastms, NULL);
02556 }
02557 }
02558
02559
02560 static void register_peer_exten(struct sip_peer *peer, int onoff)
02561 {
02562 char multi[256];
02563 char *stringp, *ext, *context;
02564
02565
02566
02567
02568
02569 if (ast_strlen_zero(global_regcontext))
02570 return;
02571
02572 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
02573 stringp = multi;
02574 while ((ext = strsep(&stringp, "&"))) {
02575 if ((context = strchr(ext, '@'))) {
02576 *context++ = '\0';
02577 if (!ast_context_find(context)) {
02578 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
02579 continue;
02580 }
02581 } else {
02582 context = global_regcontext;
02583 }
02584 if (onoff) {
02585 if (!ast_exists_extension(NULL, context, ext, 1, NULL)) {
02586 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
02587 ast_strdup(peer->name), ast_free_ptr, "SIP");
02588 }
02589 } else {
02590 ast_context_remove_extension(context, ext, 1, NULL);
02591 }
02592 }
02593 }
02594
02595
02596 static void sip_destroy_peer(struct sip_peer *peer)
02597 {
02598 if (option_debug > 2)
02599 ast_log(LOG_DEBUG, "Destroying SIP peer %s\n", peer->name);
02600
02601
02602 if (peer->call)
02603 sip_destroy(peer->call);
02604
02605 if (peer->mwipvt)
02606 sip_destroy(peer->mwipvt);
02607
02608 if (peer->chanvars) {
02609 ast_variables_destroy(peer->chanvars);
02610 peer->chanvars = NULL;
02611 }
02612
02613 register_peer_exten(peer, FALSE);
02614 ast_free_ha(peer->ha);
02615 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT))
02616 apeerobjs--;
02617 else if (ast_test_flag(&peer->flags[0], SIP_REALTIME))
02618 rpeerobjs--;
02619 else
02620 speerobjs--;
02621 clear_realm_authentication(peer->auth);
02622 peer->auth = NULL;
02623 free(peer);
02624 }
02625
02626
02627 static void update_peer(struct sip_peer *p, int expiry)
02628 {
02629 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02630 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) &&
02631 (ast_test_flag(&p->flags[0], SIP_REALTIME) || rtcachefriends)) {
02632 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, expiry, p->lastms);
02633 }
02634 }
02635
02636
02637
02638
02639
02640
02641
02642 static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_in *sin, int devstate_only)
02643 {
02644 struct sip_peer *peer=NULL;
02645 struct ast_variable *var = NULL;
02646 struct ast_config *peerlist = NULL;
02647 struct ast_variable *tmp;
02648 struct ast_flags flags = {0};
02649 const char *iabuf = NULL;
02650 char portstring[6];
02651 const char *insecure;
02652 char *cat = NULL;
02653 unsigned short portnum;
02654
02655
02656 if (newpeername) {
02657 var = ast_load_realtime("sippeers", "name", newpeername, "host", "dynamic", NULL);
02658 if (!var && sin)
02659 var = ast_load_realtime("sippeers", "name", newpeername, "host", ast_inet_ntoa(sin->sin_addr), NULL);
02660 if (!var) {
02661 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
02662
02663
02664
02665
02666
02667
02668 if (var && sin) {
02669 for (tmp = var; tmp; tmp = tmp->next) {
02670 if (!strcasecmp(tmp->name, "host")) {
02671 struct hostent *hp;
02672 struct ast_hostent ahp;
02673 if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
02674
02675 ast_variables_destroy(var);
02676 var = NULL;
02677 }
02678 break;
02679 }
02680 }
02681 }
02682 }
02683 }
02684
02685 if (!var && sin) {
02686 iabuf = ast_inet_ntoa(sin->sin_addr);
02687 portnum = ntohs(sin->sin_port);
02688 sprintf(portstring, "%d", portnum);
02689 var = ast_load_realtime("sippeers", "host", iabuf, "port", portstring, NULL);
02690 if (!var)
02691 var = ast_load_realtime("sippeers", "ipaddr", iabuf, "port", portstring, NULL);
02692 if (!var) {
02693 peerlist = ast_load_realtime_multientry("sippeers", "host", iabuf, NULL);
02694 if(peerlist){
02695 while((cat = ast_category_browse(peerlist, cat)))
02696 {
02697 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02698 set_insecure_flags(&flags, insecure, -1);
02699 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02700 var = ast_category_root(peerlist, cat);
02701 break;
02702 }
02703 }
02704 }
02705 if(!var) {
02706 ast_config_destroy(peerlist);
02707 peerlist = NULL;
02708 cat = NULL;
02709 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", iabuf, NULL);
02710 if(peerlist) {
02711 while((cat = ast_category_browse(peerlist, cat)))
02712 {
02713 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02714 set_insecure_flags(&flags, insecure, -1);
02715 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02716 var = ast_category_root(peerlist, cat);
02717 break;
02718 }
02719 }
02720 }
02721 }
02722 }
02723 }
02724
02725 if (!var) {
02726 if(peerlist)
02727 ast_config_destroy(peerlist);
02728 return NULL;
02729 }
02730
02731 for (tmp = var; tmp; tmp = tmp->next) {
02732
02733 if (!strcasecmp(tmp->name, "type") &&
02734 !strcasecmp(tmp->value, "user")) {
02735 ast_variables_destroy(var);
02736 return NULL;
02737 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
02738 newpeername = tmp->value;
02739 } else if (!strcasecmp(tmp->name, "lastms")) {
02740 seen_lastms = 1;
02741 }
02742 }
02743
02744 if (!newpeername) {
02745 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", iabuf);
02746 if(peerlist)
02747 ast_config_destroy(peerlist);
02748 else
02749 ast_variables_destroy(var);
02750 return NULL;
02751 }
02752
02753
02754 peer = build_peer(newpeername, var, NULL, 1, devstate_only);
02755 if (!peer) {
02756 if(peerlist)
02757 ast_config_destroy(peerlist);
02758 else
02759 ast_variables_destroy(var);
02760 return NULL;
02761 }
02762
02763 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && !devstate_only) {
02764
02765 ast_copy_flags(&peer->flags[1],&global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
02766 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
02767 if (!AST_SCHED_DEL(sched, peer->expire)) {
02768 struct sip_peer *peer_ptr = peer;
02769 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
02770 }
02771 peer->expire = ast_sched_add(sched, (global_rtautoclear) * 1000, expire_register, ASTOBJ_REF(peer));
02772 if (peer->expire == -1) {
02773 struct sip_peer *peer_ptr = peer;
02774 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
02775 }
02776 }
02777 ASTOBJ_CONTAINER_LINK(&peerl,peer);
02778 }
02779 ast_set_flag(&peer->flags[0], SIP_REALTIME);
02780 if(peerlist)
02781 ast_config_destroy(peerlist);
02782 else
02783 ast_variables_destroy(var);
02784 return peer;
02785 }
02786
02787
02788 static int sip_addrcmp(char *name, struct sockaddr_in *sin)
02789 {
02790
02791 struct sip_peer *p = (struct sip_peer *) name;
02792 return !(!inaddrcmp(&p->addr, sin) ||
02793 (ast_test_flag(&p->flags[0], SIP_INSECURE_PORT) &&
02794 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
02795 }
02796
02797
02798
02799
02800 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int devstate_only)
02801 {
02802 struct sip_peer *p = NULL;
02803
02804 if (peer)
02805 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
02806 else
02807 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, name, sip_addr_hashfunc, 1, sip_addrcmp);
02808
02809 if (!p && (realtime || devstate_only))
02810 p = realtime_peer(peer, sin, devstate_only);
02811
02812 return p;
02813 }
02814
02815
02816 static void sip_destroy_user(struct sip_user *user)
02817 {
02818 if (option_debug > 2)
02819 ast_log(LOG_DEBUG, "Destroying user object from memory: %s\n", user->name);
02820 ast_free_ha(user->ha);
02821 if (user->chanvars) {
02822 ast_variables_destroy(user->chanvars);
02823 user->chanvars = NULL;
02824 }
02825 if (ast_test_flag(&user->flags[0], SIP_REALTIME))
02826 ruserobjs--;
02827 else
02828 suserobjs--;
02829 free(user);
02830 }
02831
02832
02833
02834
02835 static struct sip_user *realtime_user(const char *username)
02836 {
02837 struct ast_variable *var;
02838 struct ast_variable *tmp;
02839 struct sip_user *user = NULL;
02840
02841 var = ast_load_realtime("sipusers", "name", username, NULL);
02842
02843 if (!var)
02844 return NULL;
02845
02846 for (tmp = var; tmp; tmp = tmp->next) {
02847 if (!strcasecmp(tmp->name, "type") &&
02848 !strcasecmp(tmp->value, "peer")) {
02849 ast_variables_destroy(var);
02850 return NULL;
02851 }
02852 }
02853
02854 user = build_user(username, var, NULL, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
02855
02856 if (!user) {
02857 ast_variables_destroy(var);
02858 return NULL;
02859 }
02860
02861 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
02862 ast_set_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02863 suserobjs++;
02864 ASTOBJ_CONTAINER_LINK(&userl,user);
02865 } else {
02866
02867 suserobjs--;
02868 ruserobjs++;
02869 }
02870 ast_set_flag(&user->flags[0], SIP_REALTIME);
02871 ast_variables_destroy(var);
02872 return user;
02873 }
02874
02875
02876
02877
02878
02879 static struct sip_user *find_user(const char *name, int realtime)
02880 {
02881 struct sip_user *u = ASTOBJ_CONTAINER_FIND(&userl, name);
02882 if (!u && realtime)
02883 u = realtime_user(name);
02884 return u;
02885 }
02886
02887
02888 static void do_setnat(struct sip_pvt *p, int natflags)
02889 {
02890 const char *mode = natflags ? "On" : "Off";
02891
02892 if (p->rtp) {
02893 if (option_debug)
02894 ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", mode);
02895 ast_rtp_setnat(p->rtp, natflags);
02896 }
02897 if (p->vrtp) {
02898 if (option_debug)
02899 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", mode);
02900 ast_rtp_setnat(p->vrtp, natflags);
02901 }
02902 if (p->udptl) {
02903 if (option_debug)
02904 ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", mode);
02905 ast_udptl_setnat(p->udptl, natflags);
02906 }
02907 }
02908
02909
02910
02911
02912 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
02913 {
02914 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
02915 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
02916 dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
02917 dialog->recv = dialog->sa;
02918 } else
02919 return -1;
02920
02921 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
02922 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
02923 dialog->capability = peer->capability;
02924 if ((!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(dialog->capability & AST_FORMAT_VIDEO_MASK)) && dialog->vrtp) {
02925 ast_rtp_destroy(dialog->vrtp);
02926 dialog->vrtp = NULL;
02927 }
02928 dialog->prefs = peer->prefs;
02929 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
02930 dialog->t38.capability = global_t38_capability;
02931 if (dialog->udptl) {
02932 if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_FEC )
02933 dialog->t38.capability |= T38FAX_UDP_EC_FEC;
02934 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY )
02935 dialog->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
02936 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_NONE )
02937 dialog->t38.capability |= T38FAX_UDP_EC_NONE;
02938 dialog->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
02939 if (option_debug > 1)
02940 ast_log(LOG_DEBUG,"Our T38 capability (%d)\n", dialog->t38.capability);
02941 }
02942 dialog->t38.jointcapability = dialog->t38.capability;
02943 } else if (dialog->udptl) {
02944 ast_udptl_destroy(dialog->udptl);
02945 dialog->udptl = NULL;
02946 }
02947 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE );
02948
02949 if (dialog->rtp) {
02950 ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
02951 ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
02952 ast_rtp_set_rtptimeout(dialog->rtp, peer->rtptimeout);
02953 ast_rtp_set_rtpholdtimeout(dialog->rtp, peer->rtpholdtimeout);
02954 ast_rtp_set_rtpkeepalive(dialog->rtp, peer->rtpkeepalive);
02955 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_CONSTANT_SSRC)) {
02956 ast_rtp_set_constantssrc(dialog->rtp);
02957 }
02958
02959 ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
02960 dialog->autoframing = peer->autoframing;
02961 }
02962 if (dialog->vrtp) {
02963 ast_rtp_setdtmf(dialog->vrtp, 0);
02964 ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
02965 ast_rtp_set_rtptimeout(dialog->vrtp, peer->rtptimeout);
02966 ast_rtp_set_rtpholdtimeout(dialog->vrtp, peer->rtpholdtimeout);
02967 ast_rtp_set_rtpkeepalive(dialog->vrtp, peer->rtpkeepalive);
02968 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_CONSTANT_SSRC)) {
02969 ast_rtp_set_constantssrc(dialog->vrtp);
02970 }
02971 }
02972
02973 ast_string_field_set(dialog, peername, peer->name);
02974 ast_string_field_set(dialog, authname, peer->username);
02975 ast_string_field_set(dialog, username, peer->username);
02976 ast_string_field_set(dialog, peersecret, peer->secret);
02977 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
02978 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
02979 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
02980 ast_string_field_set(dialog, tohost, peer->tohost);
02981 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
02982 if (!dialog->initreq.headers && !ast_strlen_zero(peer->fromdomain)) {
02983 char *tmpcall;
02984 char *c;
02985 tmpcall = ast_strdupa(dialog->callid);
02986 c = strchr(tmpcall, '@');
02987 if (c) {
02988 *c = '\0';
02989 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
02990 }
02991 }
02992 if (ast_strlen_zero(dialog->tohost))
02993 ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
02994 if (!ast_strlen_zero(peer->fromdomain))
02995 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
02996 if (!ast_strlen_zero(peer->fromuser))
02997 ast_string_field_set(dialog, fromuser, peer->fromuser);
02998 if (!ast_strlen_zero(peer->language))
02999 ast_string_field_set(dialog, language, peer->language);
03000 dialog->maxtime = peer->maxms;
03001 dialog->callgroup = peer->callgroup;
03002 dialog->pickupgroup = peer->pickupgroup;
03003 dialog->peerauth = peer->auth;
03004 dialog->allowtransfer = peer->allowtransfer;
03005
03006
03007 if (peer->maxms && peer->lastms)
03008 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
03009 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
03010 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
03011 dialog->noncodeccapability |= AST_RTP_DTMF;
03012 else
03013 dialog->noncodeccapability &= ~AST_RTP_DTMF;
03014 dialog->jointnoncodeccapability = dialog->noncodeccapability;
03015 ast_string_field_set(dialog, context, peer->context);
03016 dialog->rtptimeout = peer->rtptimeout;
03017 if (peer->call_limit)
03018 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
03019 dialog->maxcallbitrate = peer->maxcallbitrate;
03020 if (!dialog->portinuri)
03021 dialog->portinuri = peer->portinuri;
03022
03023 return 0;
03024 }
03025
03026
03027
03028
03029 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct sockaddr_in *sin)
03030 {
03031 struct hostent *hp;
03032 struct ast_hostent ahp;
03033 struct sip_peer *p;
03034 char *port;
03035 int portno = 0;
03036 char host[MAXHOSTNAMELEN], *hostn;
03037 char peer[256];
03038
03039 ast_copy_string(peer, opeer, sizeof(peer));
03040 port = strchr(peer, ':');
03041 if (port) {
03042 *port++ = '\0';
03043 dialog->portinuri = 1;
03044 }
03045 dialog->sa.sin_family = AF_INET;
03046 dialog->timer_t1 = 500;
03047 p = find_peer(peer, NULL, 1, 0);
03048
03049 if (p) {
03050 int res = create_addr_from_peer(dialog, p);
03051 if (port) {
03052 portno = atoi(port);
03053 dialog->sa.sin_port = dialog->recv.sin_port = htons(portno);
03054 }
03055 ASTOBJ_UNREF(p, sip_destroy_peer);
03056 return res;
03057 }
03058
03059 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
03060
03061 ast_string_field_set(dialog, tohost, peer);
03062
03063 if (sin) {
03064 memcpy(&dialog->sa.sin_addr, &sin->sin_addr, sizeof(dialog->sa.sin_addr));
03065 if (!sin->sin_port) {
03066 if (ast_strlen_zero(port) || sscanf(port, "%30u", &portno) != 1) {
03067 portno = STANDARD_SIP_PORT;
03068 }
03069 } else {
03070 portno = ntohs(sin->sin_port);
03071 }
03072 } else {
03073 hostn = peer;
03074
03075
03076
03077 if (!port && srvlookup) {
03078 char service[MAXHOSTNAMELEN];
03079 int tportno;
03080 int ret;
03081
03082 snprintf(service, sizeof(service), "_sip._udp.%s", peer);
03083 ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
03084 if (ret > 0) {
03085 hostn = host;
03086 portno = tportno;
03087 }
03088 }
03089 if (!portno)
03090 portno = port ? atoi(port) : STANDARD_SIP_PORT;
03091
03092 hp = ast_gethostbyname(hostn, &ahp);
03093 if (!hp) {
03094 ast_log(LOG_WARNING, "No such host: %s\n", peer);
03095 return -1;
03096 }
03097 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
03098 }
03099 dialog->sa.sin_port = htons(portno);
03100 dialog->recv = dialog->sa;
03101 return 0;
03102 }
03103
03104
03105 static int auto_congest(const void *nothing)
03106 {
03107 struct sip_pvt *p = (struct sip_pvt *)nothing;
03108
03109 ast_mutex_lock(&p->lock);
03110 p->initid = -1;
03111 if (p->owner) {
03112
03113 if (!ast_channel_trylock(p->owner)) {
03114 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
03115 append_history(p, "Cong", "Auto-congesting (timer)");
03116 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
03117 ast_channel_unlock(p->owner);
03118 }
03119 }
03120 ast_mutex_unlock(&p->lock);
03121 return 0;
03122 }
03123
03124
03125
03126
03127 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
03128 {
03129 int res, xmitres = 0;
03130 struct sip_pvt *p;
03131 struct varshead *headp;
03132 struct ast_var_t *current;
03133 const char *referer = NULL;
03134
03135 p = ast->tech_pvt;
03136 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
03137 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
03138 return -1;
03139 }
03140
03141
03142 headp=&ast->varshead;
03143 AST_LIST_TRAVERSE(headp,current,entries) {
03144
03145 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
03146 p->options->vxml_url = ast_var_value(current);
03147 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
03148 p->options->uri_options = ast_var_value(current);
03149 } else if (!p->options->distinctive_ring && !strcasecmp(ast_var_name(current), "ALERT_INFO")) {
03150
03151 p->options->distinctive_ring = ast_var_value(current);
03152 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
03153
03154 p->options->addsipheaders = 1;
03155 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
03156
03157 p->options->transfer = 1;
03158 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
03159
03160 referer = ast_var_value(current);
03161 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
03162
03163 p->options->replaces = ast_var_value(current);
03164 }
03165 }
03166
03167 res = 0;
03168 ast_set_flag(&p->flags[0], SIP_OUTGOING);
03169
03170 if (p->options->transfer) {
03171 char buf[SIPBUFSIZE/2];
03172
03173 if (referer) {
03174 if (sipdebug && option_debug > 2)
03175 ast_log(LOG_DEBUG, "Call for %s transfered by %s\n", p->username, referer);
03176 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
03177 } else
03178 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
03179 ast_string_field_set(p, cid_name, buf);
03180 }
03181 if (option_debug)
03182 ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
03183
03184 res = update_call_counter(p, INC_CALL_RINGING);
03185 if ( res != -1 ) {
03186 p->callingpres = ast->cid.cid_pres;
03187 p->jointcapability = ast_translate_available_formats(p->capability, p->prefcodec);
03188 p->jointnoncodeccapability = p->noncodeccapability;
03189
03190
03191 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
03192 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
03193 res = -1;
03194 } else {
03195 p->t38.jointcapability = p->t38.capability;
03196 if (option_debug > 1)
03197 ast_log(LOG_DEBUG,"Our T38 capability (%d), joint T38 capability (%d)\n", p->t38.capability, p->t38.jointcapability);
03198 xmitres = transmit_invite(p, SIP_INVITE, 1, 2);
03199 if (xmitres == XMIT_ERROR)
03200 return -1;
03201
03202 p->invitestate = INV_CALLING;
03203
03204
03205 AST_SCHED_DEL(sched, p->initid);
03206 p->initid = ast_sched_add(sched, p->maxtime ? (p->maxtime * 4) : SIP_TRANS_TIMEOUT, auto_congest, p);
03207 }
03208 } else {
03209 ast->hangupcause = AST_CAUSE_USER_BUSY;
03210 }
03211 return res;
03212 }
03213
03214
03215
03216 static void sip_registry_destroy(struct sip_registry *reg)
03217 {
03218
03219 if (option_debug > 2)
03220 ast_log(LOG_DEBUG, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
03221
03222 if (reg->call) {
03223
03224
03225 reg->call->registry = NULL;
03226 if (option_debug > 2)
03227 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
03228 sip_destroy(reg->call);
03229 }
03230 AST_SCHED_DEL(sched, reg->expire);
03231 AST_SCHED_DEL(sched, reg->timeout);
03232 ast_string_field_free_memory(reg);
03233 regobjs--;
03234 free(reg);
03235
03236 }
03237
03238
03239 static int __sip_destroy(struct sip_pvt *p, int lockowner)
03240 {
03241 struct sip_pvt *cur, *prev = NULL;
03242 struct sip_pkt *cp;
03243 struct sip_request *req;
03244
03245
03246 if (p->rtp && ast_rtp_get_bridged(p->rtp)) {
03247 ast_verbose("Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03248 return -1;
03249 }
03250
03251 if (p->vrtp && ast_rtp_get_bridged(p->vrtp)) {
03252 ast_verbose("Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03253 return -1;
03254 }
03255
03256 if (sip_debug_test_pvt(p) || option_debug > 2)
03257 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03258
03259 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03260 update_call_counter(p, DEC_CALL_LIMIT);
03261 if (option_debug > 1)
03262 ast_log(LOG_DEBUG, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
03263 }
03264
03265
03266 if (p->owner) {
03267 if (lockowner)
03268 ast_channel_lock(p->owner);
03269 if (option_debug)
03270 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
03271 p->owner->tech_pvt = NULL;
03272
03273 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03274 if (lockowner)
03275 ast_channel_unlock(p->owner);
03276
03277 usleep(1);
03278 }
03279
03280
03281 if (p->relatedpeer) {
03282 if (p->relatedpeer->mwipvt == p) {
03283 p->relatedpeer->mwipvt = NULL;
03284 }
03285 ASTOBJ_UNREF(p->relatedpeer, sip_destroy_peer);
03286 }
03287
03288 if (dumphistory)
03289 sip_dump_history(p);
03290
03291 if (p->options)
03292 free(p->options);
03293
03294 if (p->stateid > -1)
03295 ast_extension_state_del(p->stateid, NULL);
03296 AST_SCHED_DEL(sched, p->initid);
03297 AST_SCHED_DEL(sched, p->waitid);
03298 AST_SCHED_DEL(sched, p->autokillid);
03299 AST_SCHED_DEL(sched, p->request_queue_sched_id);
03300 AST_SCHED_DEL(sched, p->provisional_keepalive_sched_id);
03301
03302 if (p->rtp) {
03303 ast_rtp_destroy(p->rtp);
03304 }
03305 if (p->vrtp) {
03306 ast_rtp_destroy(p->vrtp);
03307 }
03308 if (p->udptl)
03309 ast_udptl_destroy(p->udptl);
03310 if (p->refer)
03311 free(p->refer);
03312 if (p->route) {
03313 free_old_route(p->route);
03314 p->route = NULL;
03315 }
03316 if (p->registry) {
03317 if (p->registry->call == p)
03318 p->registry->call = NULL;
03319 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
03320 }
03321
03322
03323 if (p->history) {
03324 struct sip_history *hist;
03325 while ( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) ) {
03326 free(hist);
03327 p->history_entries--;
03328 }
03329 free(p->history);
03330 p->history = NULL;
03331 }
03332
03333 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
03334 ast_free(req);
03335 }
03336
03337 for (prev = NULL, cur = iflist; cur; prev = cur, cur = cur->next) {
03338 if (cur == p) {
03339 UNLINK(cur, iflist, prev);
03340 break;
03341 }
03342 }
03343 if (!cur) {
03344 ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
03345 return 0;
03346 }
03347
03348
03349 while((cp = p->packets)) {
03350 p->packets = p->packets->next;
03351 AST_SCHED_DEL(sched, cp->retransid);
03352 free(cp);
03353 }
03354 if (p->chanvars) {
03355 ast_variables_destroy(p->chanvars);
03356 p->chanvars = NULL;
03357 }
03358 ast_mutex_destroy(&p->lock);
03359
03360 ast_string_field_free_memory(p);
03361
03362 free(p);
03363 return 0;
03364 }
03365
03366
03367
03368
03369
03370
03371
03372
03373
03374
03375
03376
03377
03378
03379
03380 static int update_call_counter(struct sip_pvt *fup, int event)
03381 {
03382 char name[256];
03383 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
03384 int outgoing = ast_test_flag(&fup->flags[1], SIP_PAGE2_OUTGOING_CALL);
03385 struct sip_user *u = NULL;
03386 struct sip_peer *p = NULL;
03387
03388 if (option_debug > 2)
03389 ast_log(LOG_DEBUG, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
03390
03391
03392
03393 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
03394 return 0;
03395
03396 ast_copy_string(name, fup->username, sizeof(name));
03397
03398
03399 if (global_limitonpeers == FALSE && !outgoing && (u = find_user(name, 1))) {
03400 inuse = &u->inUse;
03401 call_limit = &u->call_limit;
03402 inringing = NULL;
03403 } else if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, 1, 0) ) ) {
03404 inuse = &p->inUse;
03405 call_limit = &p->call_limit;
03406 inringing = &p->inRinging;
03407 ast_copy_string(name, fup->peername, sizeof(name));
03408 }
03409 if (!p && !u) {
03410 if (option_debug > 1)
03411 ast_log(LOG_DEBUG, "%s is not a local device, no call limit\n", name);
03412 return 0;
03413 }
03414
03415 switch(event) {
03416
03417 case DEC_CALL_LIMIT:
03418 if ( *inuse > 0 ) {
03419 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
03420 (*inuse)--;
03421 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
03422 }
03423 } else {
03424 *inuse = 0;
03425 }
03426 if (inringing) {
03427 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03428 if (*inringing > 0)
03429 (*inringing)--;
03430 else if (!ast_test_flag(&p->flags[0], SIP_REALTIME) || ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03431 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", fup->peername);
03432 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03433 }
03434 }
03435 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold) {
03436 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
03437 sip_peer_hold(fup, 0);
03438 }
03439 if (option_debug > 1 || sipdebug) {
03440 ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
03441 }
03442 break;
03443
03444 case INC_CALL_RINGING:
03445 case INC_CALL_LIMIT:
03446 if (*call_limit > 0 ) {
03447
03448 if (outgoing && (*inuse >= *call_limit)) {
03449 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);
03450 if (u)
03451 ASTOBJ_UNREF(u, sip_destroy_user);
03452 else
03453 ASTOBJ_UNREF(p, sip_destroy_peer);
03454 return -1;
03455 }
03456 }
03457 if (inringing && (event == INC_CALL_RINGING)) {
03458 if (!ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03459 (*inringing)++;
03460 ast_set_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03461 }
03462 }
03463
03464 (*inuse)++;
03465 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
03466 if (option_debug > 1 || sipdebug) {
03467 ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
03468 }
03469 break;
03470
03471 case DEC_CALL_RINGING:
03472 if (inringing) {
03473 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03474 if (*inringing > 0)
03475 (*inringing)--;
03476 else if (!ast_test_flag(&p->flags[0], SIP_REALTIME) || ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03477 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", p->name);
03478 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03479 }
03480 }
03481 break;
03482
03483 default:
03484 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
03485 }
03486 if (p) {
03487 ast_device_state_changed("SIP/%s", p->name);
03488 ASTOBJ_UNREF(p, sip_destroy_peer);
03489 } else
03490 ASTOBJ_UNREF(u, sip_destroy_user);
03491 return 0;
03492 }
03493
03494
03495 static void sip_destroy(struct sip_pvt *p)
03496 {
03497 ast_mutex_lock(&iflock);
03498 if (option_debug > 2)
03499 ast_log(LOG_DEBUG, "Destroying SIP dialog %s\n", p->callid);
03500 __sip_destroy(p, 1);
03501 ast_mutex_unlock(&iflock);
03502 }
03503
03504
03505 static int hangup_sip2cause(int cause)
03506 {
03507
03508
03509 switch(cause) {
03510 case 401:
03511 return AST_CAUSE_CALL_REJECTED;
03512 case 403:
03513 return AST_CAUSE_CALL_REJECTED;
03514 case 404:
03515 return AST_CAUSE_UNALLOCATED;
03516 case 405:
03517 return AST_CAUSE_INTERWORKING;
03518 case 407:
03519 return AST_CAUSE_CALL_REJECTED;
03520 case 408:
03521 return AST_CAUSE_NO_USER_RESPONSE;
03522 case 409:
03523 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
03524 case 410:
03525 return AST_CAUSE_NUMBER_CHANGED;
03526 case 411:
03527 return AST_CAUSE_INTERWORKING;
03528 case 413:
03529 return AST_CAUSE_INTERWORKING;
03530 case 414:
03531 return AST_CAUSE_INTERWORKING;
03532 case 415:
03533 return AST_CAUSE_INTERWORKING;
03534 case 420:
03535 return AST_CAUSE_NO_ROUTE_DESTINATION;
03536 case 480:
03537 return AST_CAUSE_NO_ANSWER;
03538 case 481:
03539 return AST_CAUSE_INTERWORKING;
03540 case 482:
03541 return AST_CAUSE_INTERWORKING;
03542 case 483:
03543 return AST_CAUSE_NO_ANSWER;
03544 case 484:
03545 return AST_CAUSE_INVALID_NUMBER_FORMAT;
03546 case 485:
03547 return AST_CAUSE_UNALLOCATED;
03548 case 486:
03549 return AST_CAUSE_BUSY;
03550 case 487:
03551 return AST_CAUSE_INTERWORKING;
03552 case 488:
03553 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03554 case 491:
03555 return AST_CAUSE_INTERWORKING;
03556 case 493:
03557 return AST_CAUSE_INTERWORKING;
03558 case 500:
03559 return AST_CAUSE_FAILURE;
03560 case 501:
03561 return AST_CAUSE_FACILITY_REJECTED;
03562 case 502:
03563 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
03564 case 503:
03565 return AST_CAUSE_CONGESTION;
03566 case 504:
03567 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
03568 case 505:
03569 return AST_CAUSE_INTERWORKING;
03570 case 600:
03571 return AST_CAUSE_USER_BUSY;
03572 case 603:
03573 return AST_CAUSE_CALL_REJECTED;
03574 case 604:
03575 return AST_CAUSE_UNALLOCATED;
03576 case 606:
03577 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03578 default:
03579 return AST_CAUSE_NORMAL;
03580 }
03581
03582 return 0;
03583 }
03584
03585
03586
03587
03588
03589
03590
03591
03592
03593
03594
03595
03596
03597
03598
03599
03600
03601
03602
03603
03604
03605
03606
03607
03608
03609
03610
03611
03612
03613
03614
03615
03616
03617 static const char *hangup_cause2sip(int cause)
03618 {
03619 switch (cause) {
03620 case AST_CAUSE_UNALLOCATED:
03621 case AST_CAUSE_NO_ROUTE_DESTINATION:
03622 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
03623 return "404 Not Found";
03624 case AST_CAUSE_CONGESTION:
03625 case AST_CAUSE_SWITCH_CONGESTION:
03626 return "503 Service Unavailable";
03627 case AST_CAUSE_NO_USER_RESPONSE:
03628 return "408 Request Timeout";
03629 case AST_CAUSE_NO_ANSWER:
03630 case AST_CAUSE_UNREGISTERED:
03631 return "480 Temporarily unavailable";
03632 case AST_CAUSE_CALL_REJECTED:
03633 return "403 Forbidden";
03634 case AST_CAUSE_NUMBER_CHANGED:
03635 return "410 Gone";
03636 case AST_CAUSE_NORMAL_UNSPECIFIED:
03637 return "480 Temporarily unavailable";
03638 case AST_CAUSE_INVALID_NUMBER_FORMAT:
03639 return "484 Address incomplete";
03640 case AST_CAUSE_USER_BUSY:
03641 return "486 Busy here";
03642 case AST_CAUSE_FAILURE:
03643 return "500 Server internal failure";
03644 case AST_CAUSE_FACILITY_REJECTED:
03645 return "501 Not Implemented";
03646 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
03647 return "503 Service Unavailable";
03648
03649 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
03650 return "502 Bad Gateway";
03651 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
03652 return "488 Not Acceptable Here";
03653
03654 case AST_CAUSE_NOTDEFINED:
03655 default:
03656 if (option_debug)
03657 ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
03658 return NULL;
03659 }
03660
03661
03662 return 0;
03663 }
03664
03665
03666
03667
03668 static int sip_hangup(struct ast_channel *ast)
03669 {
03670 struct sip_pvt *p = ast->tech_pvt;
03671 int needcancel = FALSE;
03672 int needdestroy = 0;
03673 struct ast_channel *oldowner = ast;
03674
03675 if (!p) {
03676 if (option_debug)
03677 ast_log(LOG_DEBUG, "Asked to hangup channel that was not connected\n");
03678 return 0;
03679 }
03680
03681
03682 if (p->owner)
03683 p->hangupcause = p->owner->hangupcause;
03684
03685 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
03686 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03687 if (option_debug && sipdebug)
03688 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03689 update_call_counter(p, DEC_CALL_LIMIT);
03690 }
03691 if (option_debug >3)
03692 ast_log(LOG_DEBUG, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
03693 if (p->autokillid > -1 && sip_cancel_destroy(p))
03694 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03695 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03696 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
03697 ast_clear_flag(&p->flags[0], SIP_NEEDDESTROY);
03698 p->owner->tech_pvt = NULL;
03699 p->owner = NULL;
03700 return 0;
03701 }
03702 if (option_debug) {
03703 if (ast_test_flag(ast, AST_FLAG_ZOMBIE) && p->refer && option_debug)
03704 ast_log(LOG_DEBUG, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
03705 else {
03706 if (option_debug)
03707 ast_log(LOG_DEBUG, "Hangup call %s, SIP callid %s)\n", ast->name, p->callid);
03708 }
03709 }
03710 if (option_debug && ast_test_flag(ast, AST_FLAG_ZOMBIE))
03711 ast_log(LOG_DEBUG, "Hanging up zombie call. Be scared.\n");
03712
03713 ast_mutex_lock(&p->lock);
03714 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03715 if (option_debug && sipdebug)
03716 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03717 update_call_counter(p, DEC_CALL_LIMIT);
03718 }
03719
03720
03721 if (p->owner != ast) {
03722 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
03723 ast_mutex_unlock(&p->lock);
03724 return 0;
03725 }
03726
03727 if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING || (p->invitestate < INV_COMPLETED && ast->_state != AST_STATE_UP)) {
03728 needcancel = TRUE;
03729 if (option_debug > 3)
03730 ast_log(LOG_DEBUG, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
03731 }
03732
03733 stop_media_flows(p);
03734
03735 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", p->owner ? ast_cause2str(p->hangupcause) : "Unknown");
03736
03737
03738 if (p->vad)
03739 ast_dsp_free(p->vad);
03740
03741 p->owner = NULL;
03742 ast->tech_pvt = NULL;
03743
03744 ast_module_unref(ast_module_info->self);
03745
03746
03747
03748
03749
03750
03751
03752 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE))
03753 needdestroy = 1;
03754 else if (p->invitestate != INV_CALLING)
03755 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03756
03757
03758 if (!ast_test_flag(&p->flags[0], SIP_ALREADYGONE) && !ast_strlen_zero(p->initreq.data)) {
03759 if (needcancel) {
03760 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03761
03762 struct sip_pkt *cur;
03763 for (cur = p->packets; cur; cur = cur->next) {
03764 __sip_semi_ack(p, cur->seqno, ast_test_flag(cur, FLAG_RESPONSE), cur->method ? cur->method : find_sip_method(cur->data));
03765 }
03766
03767
03768 if (p->invitestate == INV_CALLING) {
03769
03770 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03771
03772 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03773 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
03774 } else {
03775 p->invitestate = INV_CANCELLED;
03776
03777 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
03778
03779
03780 needdestroy = 0;
03781 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03782 }
03783 } else {
03784 const char *res;
03785 if (p->hangupcause && (res = hangup_cause2sip(p->hangupcause)))
03786 transmit_response_reliable(p, res, &p->initreq);
03787 else
03788 transmit_response_reliable(p, "603 Declined", &p->initreq);
03789 p->invitestate = INV_TERMINATED;
03790 }
03791 } else {
03792 if (!p->pendinginvite) {
03793 char *audioqos = "";
03794 char *videoqos = "";
03795 if (p->rtp)
03796 audioqos = ast_rtp_get_quality(p->rtp, NULL);
03797 if (p->vrtp)
03798 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
03799
03800 if (oldowner->_state == AST_STATE_UP) {
03801 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03802 }
03803
03804
03805 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
03806 if (p->rtp)
03807 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
03808 if (p->vrtp)
03809 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
03810 }
03811 if (p->rtp && oldowner)
03812 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", audioqos);
03813 if (p->vrtp && oldowner)
03814 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", videoqos);
03815 } else {
03816
03817
03818 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03819 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
03820 AST_SCHED_DEL(sched, p->waitid);
03821 if (sip_cancel_destroy(p))
03822 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03823 }
03824 }
03825 }
03826 if (needdestroy)
03827 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
03828 ast_mutex_unlock(&p->lock);
03829 return 0;
03830 }
03831
03832
03833 static void try_suggested_sip_codec(struct sip_pvt *p)
03834 {
03835 int fmt;
03836 const char *codec;
03837
03838 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
03839 if (!codec)
03840 return;
03841
03842 fmt = ast_getformatbyname(codec);
03843 if (fmt) {
03844 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
03845 if (p->jointcapability & fmt) {
03846 p->jointcapability &= fmt;
03847 p->capability &= fmt;
03848 } else
03849 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
03850 } else
03851 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
03852 return;
03853 }
03854
03855
03856
03857 static int sip_answer(struct ast_channel *ast)
03858 {
03859 int res = 0;
03860 struct sip_pvt *p = ast->tech_pvt;
03861
03862 ast_mutex_lock(&p->lock);
03863 if (ast->_state != AST_STATE_UP) {
03864 try_suggested_sip_codec(p);
03865
03866 ast_setstate(ast, AST_STATE_UP);
03867 if (option_debug)
03868 ast_log(LOG_DEBUG, "SIP answering channel: %s\n", ast->name);
03869
03870 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
03871 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
03872 }
03873 ast_mutex_unlock(&p->lock);
03874 return res;
03875 }
03876
03877
03878 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
03879 {
03880 struct sip_pvt *p = ast->tech_pvt;
03881 int res = 0;
03882
03883 switch (frame->frametype) {
03884 case AST_FRAME_VOICE:
03885 if (!(frame->subclass & ast->nativeformats)) {
03886 char s1[512], s2[512], s3[512];
03887 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %s(%d) read/write = %s(%d)/%s(%d)\n",
03888 frame->subclass,
03889 ast_getformatname_multiple(s1, sizeof(s1) - 1, ast->nativeformats & AST_FORMAT_AUDIO_MASK),
03890 ast->nativeformats & AST_FORMAT_AUDIO_MASK,
03891 ast_getformatname_multiple(s2, sizeof(s2) - 1, ast->readformat),
03892 ast->readformat,
03893 ast_getformatname_multiple(s3, sizeof(s3) - 1, ast->writeformat),
03894 ast->writeformat);
03895 return 0;
03896 }
03897 if (p) {
03898 ast_mutex_lock(&p->lock);
03899 if (p->rtp) {
03900
03901 if ((ast->_state != AST_STATE_UP) &&
03902 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03903 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03904 ast_rtp_new_source(p->rtp);
03905 if (!global_prematuremediafilter) {
03906 p->invitestate = INV_EARLY_MEDIA;
03907 transmit_provisional_response(p, "183 Session Progress", &p->initreq, 1);
03908 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03909 }
03910 } else if (p->t38.state == T38_ENABLED && !p->t38.direct) {
03911
03912 } else {
03913 p->lastrtptx = time(NULL);
03914 res = ast_rtp_write(p->rtp, frame);
03915 }
03916 }
03917 ast_mutex_unlock(&p->lock);
03918 }
03919 break;
03920 case AST_FRAME_VIDEO:
03921 if (p) {
03922 ast_mutex_lock(&p->lock);
03923 if (p->vrtp) {
03924
03925 if ((ast->_state != AST_STATE_UP) &&
03926 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03927 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03928 p->invitestate = INV_EARLY_MEDIA;
03929 transmit_provisional_response(p, "183 Session Progress", &p->initreq, 1);
03930 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03931 }
03932 p->lastrtptx = time(NULL);
03933 res = ast_rtp_write(p->vrtp, frame);
03934 }
03935 ast_mutex_unlock(&p->lock);
03936 }
03937 break;
03938 case AST_FRAME_IMAGE:
03939 return 0;
03940 break;
03941 case AST_FRAME_MODEM:
03942 if (p) {
03943 ast_mutex_lock(&p->lock);
03944
03945
03946
03947
03948 if (ast->_state == AST_STATE_UP) {
03949 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && p->t38.state == T38_DISABLED) {
03950 if (!p->pendinginvite) {
03951 p->t38.state = T38_LOCAL_REINVITE;
03952 transmit_reinvite_with_t38_sdp(p);
03953 }
03954 } else if (p->t38.state == T38_ENABLED) {
03955 res = ast_udptl_write(p->udptl, frame);
03956 }
03957 }
03958 ast_mutex_unlock(&p->lock);
03959 }
03960 break;
03961 default:
03962 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
03963 return 0;
03964 }
03965
03966 return res;
03967 }
03968
03969
03970
03971 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03972 {
03973 int ret = -1;
03974 struct sip_pvt *p;
03975
03976 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE) && option_debug)
03977 ast_log(LOG_DEBUG, "New channel is zombie\n");
03978 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE) && option_debug)
03979 ast_log(LOG_DEBUG, "Old channel is zombie\n");
03980
03981 if (!newchan || !newchan->tech_pvt) {
03982 if (!newchan)
03983 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
03984 else
03985 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
03986 return -1;
03987 }
03988 p = newchan->tech_pvt;
03989
03990 if (!p) {
03991 ast_log(LOG_WARNING, "No pvt after masquerade. Strange things may happen\n");
03992 return -1;
03993 }
03994
03995 ast_mutex_lock(&p->lock);
03996 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
03997 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
03998 if (p->owner != oldchan)
03999 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
04000 else {
04001 p->owner = newchan;
04002
04003
04004
04005
04006
04007
04008 sip_set_rtp_peer(newchan, NULL, NULL, 0, 0);
04009 ret = 0;
04010 }
04011 if (option_debug > 2)
04012 ast_log(LOG_DEBUG, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
04013
04014 ast_mutex_unlock(&p->lock);
04015 return ret;
04016 }
04017
04018 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
04019 {
04020 struct sip_pvt *p = ast->tech_pvt;
04021 int res = 0;
04022
04023 ast_mutex_lock(&p->lock);
04024 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
04025 case SIP_DTMF_INBAND:
04026 res = -1;
04027 break;
04028 case SIP_DTMF_RFC2833:
04029 if (p->rtp)
04030 ast_rtp_senddigit_begin(p->rtp, digit);
04031 break;
04032 default:
04033 break;
04034 }
04035 ast_mutex_unlock(&p->lock);
04036
04037 return res;
04038 }
04039
04040
04041
04042 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
04043 {
04044 struct sip_pvt *p = ast->tech_pvt;
04045 int res = 0;
04046
04047 ast_mutex_lock(&p->lock);
04048 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
04049 case SIP_DTMF_INFO:
04050 transmit_info_with_digit(p, digit, duration);
04051 break;
04052 case SIP_DTMF_RFC2833:
04053 if (p->rtp)
04054 ast_rtp_senddigit_end(p->rtp, digit);
04055 break;
04056 case SIP_DTMF_INBAND:
04057 res = -1;
04058 break;
04059 }
04060 ast_mutex_unlock(&p->lock);
04061
04062 return res;
04063 }
04064
04065
04066 static int sip_transfer(struct ast_channel *ast, const char *dest)
04067 {
04068 struct sip_pvt *p = ast->tech_pvt;
04069 int res;
04070
04071 if (dest == NULL)
04072 dest = "";
04073 ast_mutex_lock(&p->lock);
04074 if (ast->_state == AST_STATE_RING)
04075 res = sip_sipredirect(p, dest);
04076 else
04077 res = transmit_refer(p, dest);
04078 ast_mutex_unlock(&p->lock);
04079 return res;
04080 }
04081
04082
04083
04084
04085
04086
04087 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
04088 {
04089 struct sip_pvt *p = ast->tech_pvt;
04090 int res = 0;
04091
04092 ast_mutex_lock(&p->lock);
04093 switch(condition) {
04094 case AST_CONTROL_RINGING:
04095 if (ast->_state == AST_STATE_RING) {
04096 p->invitestate = INV_EARLY_MEDIA;
04097 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
04098 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
04099
04100 transmit_provisional_response(p, "180 Ringing", &p->initreq, 0);
04101 ast_set_flag(&p->flags[0], SIP_RINGING);
04102 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
04103 break;
04104 } else {
04105
04106 }
04107 }
04108 res = -1;
04109 break;
04110 case AST_CONTROL_BUSY:
04111 if (ast->_state != AST_STATE_UP) {
04112 transmit_response_reliable(p, "486 Busy Here", &p->initreq);
04113 p->invitestate = INV_COMPLETED;
04114 sip_alreadygone(p);
04115 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
04116 break;
04117 }
04118 res = -1;
04119 break;
04120 case AST_CONTROL_CONGESTION:
04121 if (ast->_state != AST_STATE_UP) {
04122 transmit_response_reliable(p, "503 Service Unavailable", &p->initreq);
04123 p->invitestate = INV_COMPLETED;
04124 sip_alreadygone(p);
04125 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
04126 break;
04127 }
04128 res = -1;
04129 break;
04130 case AST_CONTROL_PROCEEDING:
04131 if ((ast->_state != AST_STATE_UP) &&
04132 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
04133 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
04134 transmit_response(p, "100 Trying", &p->initreq);
04135 p->invitestate = INV_PROCEEDING;
04136 break;
04137 }
04138 res = -1;
04139 break;
04140 case AST_CONTROL_PROGRESS:
04141 if ((ast->_state != AST_STATE_UP) &&
04142 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
04143 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
04144 p->invitestate = INV_EARLY_MEDIA;
04145 transmit_provisional_response(p, "183 Session Progress", &p->initreq, 1);
04146 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
04147 break;
04148 }
04149 res = -1;
04150 break;
04151 case AST_CONTROL_HOLD:
04152 ast_rtp_new_source(p->rtp);
04153 ast_moh_start(ast, data, p->mohinterpret);
04154 break;
04155 case AST_CONTROL_UNHOLD:
04156 ast_rtp_new_source(p->rtp);
04157 ast_moh_stop(ast);
04158 break;
04159 case AST_CONTROL_VIDUPDATE:
04160 if (p->vrtp && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
04161 transmit_info_with_vidupdate(p);
04162
04163 } else
04164 res = -1;
04165 break;
04166 case AST_CONTROL_SRCUPDATE:
04167 ast_rtp_new_source(p->rtp);
04168 break;
04169 case -1:
04170 res = -1;
04171 break;
04172 default:
04173 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
04174 res = -1;
04175 break;
04176 }
04177 ast_mutex_unlock(&p->lock);
04178 return res;
04179 }
04180
04181
04182
04183
04184
04185
04186
04187 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
04188 {
04189 struct ast_channel *tmp;
04190 struct ast_variable *v = NULL;
04191 int fmt;
04192 int what;
04193 int needvideo = 0, video = 0;
04194 char *decoded_exten;
04195
04196 if (option_debug != 0) {
04197 ast_verbose(VERBOSE_PREFIX_3 "NEW SIP CHANNEL, title: <%s>\n", title?title:"Null");
04198 ast_verbose(VERBOSE_PREFIX_3 "from: %s\n", i->from);
04199 ast_verbose(VERBOSE_PREFIX_3 "username: <%s>\n", i->username);
04200 ast_verbose(VERBOSE_PREFIX_3 "peername: <%s>\n", i->peername);
04201 ast_verbose(VERBOSE_PREFIX_3 "fromdomain: %s\n", i->fromdomain);
04202 ast_verbose(VERBOSE_PREFIX_3 "fromuser: %s\n", i->fromuser);
04203 ast_verbose(VERBOSE_PREFIX_3 "fromname: %s\n", i->fromname);
04204 ast_verbose(VERBOSE_PREFIX_3 "fullcontact: %s\n", i->fullcontact);
04205 }
04206
04207 {
04208 char my_name[128];
04209 const char *f, *fromdomain = NULL;
04210
04211 if (!ast_strlen_zero(i->fromdomain) && strchr(i->fromdomain,':'))
04212 fromdomain = strchr(i->fromdomain,':') + 1;
04213 else
04214 fromdomain = i->fromdomain;
04215
04216 if (!ast_strlen_zero(i->username)) {
04217 if (!ast_strlen_zero(title) && strcmp(i->username, title)) {
04218
04219 snprintf(my_name, sizeof(my_name), "%s@%s", i->username, title);
04220 } else {
04221
04222 snprintf(my_name, sizeof(my_name), "%s", i->username);
04223 }
04224 } else {
04225 if (!ast_strlen_zero(i->peername)) {
04226
04227 snprintf(my_name, sizeof(my_name), "%s", i->peername);
04228 } else {
04229 if (!ast_strlen_zero(title)) {
04230 snprintf(my_name, sizeof(my_name), "%s", title);
04231 } else if (!ast_strlen_zero(i->from)) {
04232 f = i->from;
04233 if (!strncmp(f, "sip:", 4))
04234 f += 4;
04235 snprintf(my_name, sizeof(my_name), "%s", f);
04236 } else {
04237 snprintf(my_name, sizeof(my_name), "%s", fromdomain);
04238 }
04239 }
04240 }
04241 ast_mutex_unlock(&i->lock);
04242
04243 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, "SIP/%s-%08x", my_name, ast_atomic_fetchadd_int((int *)&chan_idx, +1));
04244
04245 }
04246 if (!tmp) {
04247 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
04248 ast_mutex_lock(&i->lock);
04249 return NULL;
04250 }
04251 ast_mutex_lock(&i->lock);
04252
04253 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO)
04254 tmp->tech = &sip_tech_info;
04255 else
04256 tmp->tech = &sip_tech;
04257
04258
04259
04260 if (i->jointcapability) {
04261 what = i->jointcapability;
04262 video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
04263 } else if (i->capability) {
04264 what = i->capability;
04265 video = i->capability & AST_FORMAT_VIDEO_MASK;
04266 } else {
04267 what = global_capability;
04268 video = global_capability & AST_FORMAT_VIDEO_MASK;
04269 }
04270
04271
04272 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video;
04273 if (option_debug > 2) {
04274 char buf[SIPBUFSIZE];
04275 ast_log(LOG_DEBUG, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
04276 ast_log(LOG_DEBUG, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
04277 ast_log(LOG_DEBUG, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
04278 ast_log(LOG_DEBUG, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
04279 if (i->prefcodec)
04280 ast_log(LOG_DEBUG, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
04281 }
04282
04283
04284 fmt = ast_best_codec(tmp->nativeformats);
04285
04286
04287
04288
04289
04290 if (i->vrtp) {
04291 if (i->prefcodec)
04292 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
04293 else
04294 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
04295 }
04296
04297 if (option_debug > 2) {
04298 if (needvideo)
04299 ast_log(LOG_DEBUG, "This channel can handle video! HOLLYWOOD next!\n");
04300 else
04301 ast_log(LOG_DEBUG, "This channel will not be able to handle video.\n");
04302 }
04303
04304
04305
04306 if ((ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) || (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
04307 i->vad = ast_dsp_new();
04308 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
04309 if (global_relaxdtmf)
04310 ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
04311 }
04312 if (i->rtp) {
04313 tmp->fds[0] = ast_rtp_fd(i->rtp);
04314 tmp->fds[1] = ast_rtcp_fd(i->rtp);
04315 }
04316 if (needvideo && i->vrtp) {
04317 tmp->fds[2] = ast_rtp_fd(i->vrtp);
04318 tmp->fds[3] = ast_rtcp_fd(i->vrtp);
04319 }
04320 if (i->udptl) {
04321 tmp->fds[5] = ast_udptl_fd(i->udptl);
04322 }
04323 if (state == AST_STATE_RING)
04324 tmp->rings = 1;
04325 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
04326 tmp->writeformat = fmt;
04327 tmp->rawwriteformat = fmt;
04328 tmp->readformat = fmt;
04329 tmp->rawreadformat = fmt;
04330 tmp->tech_pvt = i;
04331
04332 tmp->callgroup = i->callgroup;
04333 tmp->pickupgroup = i->pickupgroup;
04334 tmp->cid.cid_pres = i->callingpres;
04335 if (!ast_strlen_zero(i->accountcode))
04336 ast_string_field_set(tmp, accountcode, i->accountcode);
04337 if (i->amaflags)
04338 tmp->amaflags = i->amaflags;
04339 if (!ast_strlen_zero(i->language))
04340 ast_string_field_set(tmp, language, i->language);
04341 i->owner = tmp;
04342 ast_module_ref(ast_module_info->self);
04343 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
04344
04345
04346
04347
04348 decoded_exten = ast_strdupa(i->exten);
04349 ast_uri_decode(decoded_exten);
04350 ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
04351
04352
04353
04354 tmp->cid.cid_ani = ast_strdup(i->cid_num);
04355 if (!ast_strlen_zero(i->rdnis))
04356 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
04357
04358 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
04359 tmp->cid.cid_dnid = ast_strdup(i->exten);
04360
04361 tmp->priority = 1;
04362 if (!ast_strlen_zero(i->uri))
04363 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
04364 if (!ast_strlen_zero(i->domain))
04365 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
04366 if (!ast_strlen_zero(i->useragent))
04367 pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
04368 if (!ast_strlen_zero(i->callid))
04369 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
04370 if (i->rtp)
04371 ast_jb_configure(tmp, &global_jbconf);
04372
04373
04374 for (v = i->chanvars ; v ; v = v->next)
04375 pbx_builtin_setvar_helper(tmp, v->name, v->value);
04376
04377 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
04378 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
04379 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
04380 ast_hangup(tmp);
04381 tmp = NULL;
04382 }
04383
04384 if (!ast_test_flag(&i->flags[0], SIP_NO_HISTORY))
04385 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
04386
04387 return tmp;
04388 }
04389
04390
04391 static char *get_body_by_line(const char *line, const char *name, int nameLen)
04392 {
04393 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=')
04394 return ast_skip_blanks(line + nameLen + 1);
04395
04396 return "";
04397 }
04398
04399
04400
04401
04402
04403 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
04404 {
04405 int len = strlen(name);
04406
04407 while (*start < (req->sdp_start + req->sdp_count)) {
04408 const char *r = get_body_by_line(req->line[(*start)++], name, len);
04409 if (r[0] != '\0')
04410 return r;
04411 }
04412
04413
04414 (*start)++;
04415
04416 return "";
04417 }
04418
04419
04420
04421
04422
04423
04424 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value)
04425 {
04426 char type = '\0';
04427 const char *line = NULL;
04428
04429 if (stop > (req->sdp_start + req->sdp_count)) {
04430 stop = req->sdp_start + req->sdp_count;
04431 }
04432
04433 while (*start < stop) {
04434 line = req->line[(*start)++];
04435 if (line[1] == '=') {
04436 type = line[0];
04437 *value = ast_skip_blanks(line + 2);
04438 break;
04439 }
04440 }
04441
04442 return type;
04443 }
04444
04445
04446 static char *get_body(struct sip_request *req, char *name)
04447 {
04448 int x;
04449 int len = strlen(name);
04450 char *r;
04451
04452 for (x = 0; x < req->lines; x++) {
04453 r = get_body_by_line(req->line[x], name, len);
04454 if (r[0] != '\0')
04455 return r;
04456 }
04457
04458 return "";
04459 }
04460
04461
04462 static const char *find_alias(const char *name, const char *_default)
04463 {
04464
04465 static const struct cfalias {
04466 char * const fullname;
04467 char * const shortname;
04468 } aliases[] = {
04469 { "Content-Type", "c" },
04470 { "Content-Encoding", "e" },
04471 { "From", "f" },
04472 { "Call-ID", "i" },
04473 { "Contact", "m" },
04474 { "Content-Length", "l" },
04475 { "Subject", "s" },
04476 { "To", "t" },
04477 { "Supported", "k" },
04478 { "Refer-To", "r" },
04479 { "Referred-By", "b" },
04480 { "Allow-Events", "u" },
04481 { "Event", "o" },
04482 { "Via", "v" },
04483 { "Accept-Contact", "a" },
04484 { "Reject-Contact", "j" },
04485 { "Request-Disposition", "d" },
04486 { "Session-Expires", "x" },
04487 { "Identity", "y" },
04488 { "Identity-Info", "n" },
04489 };
04490 int x;
04491
04492 for (x=0; x<sizeof(aliases) / sizeof(aliases[0]); x++)
04493 if (!strcasecmp(aliases[x].fullname, name))
04494 return aliases[x].shortname;
04495
04496 return _default;
04497 }
04498
04499 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
04500 {
04501 int pass;
04502
04503
04504
04505
04506
04507
04508
04509
04510
04511
04512 for (pass = 0; name && pass < 2;pass++) {
04513 int x, len = strlen(name);
04514 for (x=*start; x<req->headers; x++) {
04515 if (!strncasecmp(req->header[x], name, len)) {
04516 char *r = req->header[x] + len;
04517 if (pedanticsipchecking)
04518 r = ast_skip_blanks(r);
04519
04520 if (*r == ':') {
04521 *start = x+1;
04522 return ast_skip_blanks(r+1);
04523 }
04524 }
04525 }
04526 if (pass == 0)
04527 name = find_alias(name, NULL);
04528 }
04529
04530
04531 return "";
04532 }
04533
04534
04535 static const char *get_header(const struct sip_request *req, const char *name)
04536 {
04537 int start = 0;
04538 return __get_header(req, name, &start);
04539 }
04540
04541
04542 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
04543 {
04544
04545 struct ast_frame *f;
04546
04547 if (!p->rtp) {
04548
04549 return &ast_null_frame;
04550 }
04551
04552 switch(ast->fdno) {
04553 case 0:
04554 f = ast_rtp_read(p->rtp);
04555 break;
04556 case 1:
04557 f = ast_rtcp_read(p->rtp);
04558 break;
04559 case 2:
04560 f = ast_rtp_read(p->vrtp);
04561 break;
04562 case 3:
04563 f = ast_rtcp_read(p->vrtp);
04564 break;
04565 case 5:
04566 f = ast_udptl_read(p->udptl);
04567 break;
04568 default:
04569 f = &ast_null_frame;
04570 }
04571
04572 if (f && (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) &&
04573 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833)) {
04574 ast_log(LOG_DEBUG,"Ignoring DTMF (%c) RTP frame because dtmfmode is not RFC2833\n", f->subclass);
04575 return &ast_null_frame;
04576 }
04577
04578
04579 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
04580 return f;
04581
04582 if (f && f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
04583 if (!(f->subclass & p->jointcapability)) {
04584 if (option_debug) {
04585 ast_log(LOG_DEBUG, "Bogus frame of format '%s' received from '%s'!\n",
04586 ast_getformatname(f->subclass), p->owner->name);
04587 }
04588 return &ast_null_frame;
04589 }
04590 if (option_debug)
04591 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
04592 p->owner->nativeformats = (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
04593 ast_set_read_format(p->owner, p->owner->readformat);
04594 ast_set_write_format(p->owner, p->owner->writeformat);
04595 }
04596
04597 if (f && (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
04598 f = ast_dsp_process(p->owner, p->vad, f);
04599 if (f && f->frametype == AST_FRAME_DTMF) {
04600 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && f->subclass == 'f') {
04601 if (option_debug)
04602 ast_log(LOG_DEBUG, "Fax CNG detected on %s\n", ast->name);
04603 *faxdetect = 1;
04604 } else if (option_debug) {
04605 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
04606 }
04607 }
04608 }
04609
04610 return f;
04611 }
04612
04613
04614 static struct ast_frame *sip_read(struct ast_channel *ast)
04615 {
04616 struct ast_frame *fr;
04617 struct sip_pvt *p = ast->tech_pvt;
04618 int faxdetected = FALSE;
04619
04620 ast_mutex_lock(&p->lock);
04621 fr = sip_rtp_read(ast, p, &faxdetected);
04622 p->lastrtprx = time(NULL);
04623
04624
04625
04626 if (faxdetected && ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && (p->t38.state == T38_DISABLED) && !(ast_bridged_channel(ast))) {
04627 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
04628 if (!p->pendinginvite) {
04629 if (option_debug > 2)
04630 ast_log(LOG_DEBUG, "Sending reinvite on SIP (%s) for T.38 negotiation.\n",ast->name);
04631 p->t38.state = T38_LOCAL_REINVITE;
04632 transmit_reinvite_with_t38_sdp(p);
04633 if (option_debug > 1)
04634 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, ast->name);
04635 }
04636 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04637 if (option_debug > 2)
04638 ast_log(LOG_DEBUG, "Deferring reinvite on SIP (%s) - it will be re-negotiated for T.38\n", ast->name);
04639 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04640 }
04641 }
04642
04643
04644 if (fr && fr->frametype == AST_FRAME_VOICE && p->invitestate != INV_EARLY_MEDIA && ast->_state != AST_STATE_UP) {
04645 fr = &ast_null_frame;
04646 }
04647
04648 ast_mutex_unlock(&p->lock);
04649 return fr;
04650 }
04651
04652
04653
04654 static char *generate_random_string(char *buf, size_t size)
04655 {
04656 long val[4];
04657 int x;
04658
04659 for (x=0; x<4; x++)
04660 val[x] = ast_random();
04661 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
04662
04663 return buf;
04664 }
04665
04666
04667 static void build_callid_pvt(struct sip_pvt *pvt)
04668 {
04669 char buf[33];
04670
04671 const char *host = S_OR(pvt->fromdomain, ast_inet_ntoa(pvt->ourip));
04672
04673 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04674
04675 }
04676
04677
04678 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain)
04679 {
04680 char buf[33];
04681
04682 const char *host = S_OR(fromdomain, ast_inet_ntoa(ourip));
04683
04684 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04685 }
04686
04687
04688 static void make_our_tag(char *tagbuf, size_t len)
04689 {
04690 snprintf(tagbuf, len, "as%08lx", ast_random());
04691 }
04692
04693
04694 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
04695 int useglobal_nat, const int intended_method)
04696 {
04697 struct sip_pvt *p;
04698
04699 if (!(p = ast_calloc(1, sizeof(*p))))
04700 return NULL;
04701
04702 if (ast_string_field_init(p, 512)) {
04703 free(p);
04704 return NULL;
04705 }
04706
04707 ast_mutex_init(&p->lock);
04708
04709 p->method = intended_method;
04710 p->initid = -1;
04711 p->waitid = -1;
04712 p->autokillid = -1;
04713 p->request_queue_sched_id = -1;
04714 p->provisional_keepalive_sched_id = -1;
04715 p->subscribed = NONE;
04716 p->stateid = -1;
04717 p->prefs = default_prefs;
04718
04719 if (intended_method != SIP_OPTIONS)
04720 p->timer_t1 = 500;
04721
04722 if (sin) {
04723 p->sa = *sin;
04724 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
04725 p->ourip = __ourip;
04726 } else
04727 p->ourip = __ourip;
04728
04729
04730 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
04731 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
04732
04733 ast_set2_flag(&p->flags[0], !recordhistory, SIP_NO_HISTORY);
04734
04735 p->branch = ast_random();
04736 make_our_tag(p->tag, sizeof(p->tag));
04737 p->ocseq = INITIAL_CSEQ;
04738
04739 if (sip_methods[intended_method].need_rtp) {
04740 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04741
04742 if (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT))
04743 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04744 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT))
04745 p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
04746 if (!p->rtp || (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) && !p->vrtp)) {
04747 ast_log(LOG_WARNING, "Unable to create RTP audio %s session: %s\n",
04748 ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "and video" : "", strerror(errno));
04749
04750 if (p->rtp) {
04751 ast_rtp_destroy(p->rtp);
04752 }
04753 if (p->udptl) {
04754 ast_udptl_destroy(p->udptl);
04755 }
04756 ast_mutex_destroy(&p->lock);
04757 if (p->chanvars) {
04758 ast_variables_destroy(p->chanvars);
04759 p->chanvars = NULL;
04760 }
04761 ast_string_field_free_memory(p);
04762 free(p);
04763 return NULL;
04764 }
04765 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04766 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04767 ast_rtp_settos(p->rtp, global_tos_audio);
04768 ast_rtp_set_rtptimeout(p->rtp, global_rtptimeout);
04769 ast_rtp_set_rtpholdtimeout(p->rtp, global_rtpholdtimeout);
04770 ast_rtp_set_rtpkeepalive(p->rtp, global_rtpkeepalive);
04771 if (p->vrtp) {
04772 ast_rtp_settos(p->vrtp, global_tos_video);
04773 ast_rtp_setdtmf(p->vrtp, 0);
04774 ast_rtp_setdtmfcompensate(p->vrtp, 0);
04775 ast_rtp_set_rtptimeout(p->vrtp, global_rtptimeout);
04776 ast_rtp_set_rtpholdtimeout(p->vrtp, global_rtpholdtimeout);
04777 ast_rtp_set_rtpkeepalive(p->vrtp, global_rtpkeepalive);
04778 }
04779 if (p->udptl)
04780 ast_udptl_settos(p->udptl, global_tos_audio);
04781 p->maxcallbitrate = default_maxcallbitrate;
04782 p->autoframing = global_autoframing;
04783 ast_rtp_codec_setpref(p->rtp, &p->prefs);
04784 }
04785
04786 if (useglobal_nat && sin) {
04787
04788 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
04789 p->recv = *sin;
04790 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04791 }
04792
04793 if (p->method != SIP_REGISTER)
04794 ast_string_field_set(p, fromdomain, default_fromdomain);
04795 build_via(p);
04796 if (!callid)
04797 build_callid_pvt(p);
04798 else
04799 ast_string_field_set(p, callid, callid);
04800
04801 ast_string_field_set(p, mohinterpret, default_mohinterpret);
04802 ast_string_field_set(p, mohsuggest, default_mohsuggest);
04803 p->capability = global_capability;
04804 p->allowtransfer = global_allowtransfer;
04805 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
04806 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
04807 p->noncodeccapability |= AST_RTP_DTMF;
04808 if (p->udptl) {
04809 p->t38.capability = global_t38_capability;
04810 if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY)
04811 p->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
04812 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_FEC)
04813 p->t38.capability |= T38FAX_UDP_EC_FEC;
04814 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_NONE)
04815 p->t38.capability |= T38FAX_UDP_EC_NONE;
04816 p->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
04817 p->t38.jointcapability = p->t38.capability;
04818 }
04819 ast_string_field_set(p, context, default_context);
04820
04821 AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
04822
04823
04824 ast_mutex_lock(&iflock);
04825 p->next = iflist;
04826 iflist = p;
04827 ast_mutex_unlock(&iflock);
04828 if (option_debug)
04829 ast_log(LOG_DEBUG, "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");
04830 return p;
04831 }
04832
04833
04834
04835 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
04836 {
04837 struct sip_pvt *p = NULL;
04838 char *tag = "";
04839 char totag[128];
04840 char fromtag[128];
04841 const char *callid = get_header(req, "Call-ID");
04842 const char *from = get_header(req, "From");
04843 const char *to = get_header(req, "To");
04844 const char *cseq = get_header(req, "Cseq");
04845
04846
04847
04848 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
04849 ast_strlen_zero(from) || ast_strlen_zero(cseq))
04850 return NULL;
04851
04852 if (pedanticsipchecking) {
04853
04854
04855
04856
04857
04858
04859 if (gettag(req, "To", totag, sizeof(totag)))
04860 ast_set_flag(req, SIP_PKT_WITH_TOTAG);
04861 gettag(req, "From", fromtag, sizeof(fromtag));
04862
04863 tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
04864
04865 if (option_debug > 4 )
04866 ast_log(LOG_DEBUG, "= Looking for Call ID: %s (Checking %s) --From tag %s --To-tag %s \n", callid, req->method==SIP_RESPONSE ? "To" : "From", fromtag, totag);
04867 }
04868
04869 ast_mutex_lock(&iflock);
04870 for (p = iflist; p; p = p->next) {
04871
04872 int found = FALSE;
04873 if (ast_strlen_zero(p->callid))
04874 continue;
04875 if (req->method == SIP_REGISTER)
04876 found = (!strcmp(p->callid, callid));
04877 else {
04878 found = !strcmp(p->callid, callid);
04879 if (pedanticsipchecking && found) {
04880 found = ast_strlen_zero(tag) || ast_strlen_zero(p->theirtag) || !ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED) || !strcmp(p->theirtag, tag);
04881 }
04882 }
04883
04884 if (option_debug > 4)
04885 ast_log(LOG_DEBUG, "= %s Their Call ID: %s Their Tag %s Our tag: %s\n", found ? "Found" : "No match", p->callid, p->theirtag, p->tag);
04886
04887
04888 if (pedanticsipchecking && found && req->method != SIP_RESPONSE) {
04889 if (p->tag[0] == '\0' && totag[0]) {
04890
04891 found = FALSE;
04892 } else if (totag[0]) {
04893 if (strcmp(totag, p->tag)) {
04894 found = FALSE;
04895 }
04896 }
04897 if (!found && option_debug > 4)
04898 ast_log(LOG_DEBUG, "= 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);
04899 }
04900 if (found) {
04901
04902 ast_mutex_lock(&p->lock);
04903 ast_mutex_unlock(&iflock);
04904 return p;
04905 }
04906 }
04907 ast_mutex_unlock(&iflock);
04908
04909
04910 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
04911 if (intended_method == SIP_REFER) {
04912
04913 transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
04914 } else if (intended_method == SIP_NOTIFY) {
04915
04916
04917 transmit_response_using_temp(callid, sin, 1, intended_method, req, "489 Bad event");
04918 } else {
04919
04920 if ((p = sip_alloc(callid, sin, 1, intended_method))) {
04921
04922 ast_mutex_lock(&p->lock);
04923 } else {
04924
04925
04926
04927
04928
04929
04930
04931
04932 transmit_response_using_temp(callid, sin, 1, intended_method, req, "500 Server internal error");
04933 if (option_debug > 3)
04934 ast_log(LOG_DEBUG, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
04935 }
04936 }
04937 return p;
04938 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
04939
04940 transmit_response_using_temp(callid, sin, 1, intended_method, req, "501 Method Not Implemented");
04941 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
04942
04943
04944
04945 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 Call leg/transaction does not exist");
04946 }
04947
04948
04949
04950 return p;
04951 }
04952
04953
04954 static int sip_register(char *value, int lineno)
04955 {
04956 struct sip_registry *reg;
04957 int portnum = 0;
04958 char username[256] = "";
04959 char *hostname=NULL, *secret=NULL, *authuser=NULL;
04960 char *porta=NULL;
04961 char *contact=NULL;
04962
04963 if (!value)
04964 return -1;
04965 ast_copy_string(username, value, sizeof(username));
04966
04967 hostname = strrchr(username, '@');
04968 if (hostname)
04969 *hostname++ = '\0';
04970 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
04971 ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
04972 return -1;
04973 }
04974
04975 secret = strchr(username, ':');
04976 if (secret) {
04977 *secret++ = '\0';
04978 authuser = strchr(secret, ':');
04979 if (authuser)
04980 *authuser++ = '\0';
04981 }
04982
04983 contact = strchr(hostname, '/');
04984 if (contact)
04985 *contact++ = '\0';
04986 if (ast_strlen_zero(contact))
04987 contact = "s";
04988 porta = strchr(hostname, ':');
04989 if (porta) {
04990 *porta++ = '\0';
04991 portnum = atoi(porta);
04992 if (portnum == 0) {
04993 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
04994 return -1;
04995 }
04996 }
04997 if (!(reg = ast_calloc(1, sizeof(*reg)))) {
04998 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
04999 return -1;
05000 }
05001
05002 if (ast_string_field_init(reg, 256)) {
05003 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry strings\n");
05004 free(reg);
05005 return -1;
05006 }
05007
05008 regobjs++;
05009 ASTOBJ_INIT(reg);
05010 ast_string_field_set(reg, contact, contact);
05011 if (!ast_strlen_zero(username))
05012 ast_string_field_set(reg, username, username);
05013 if (hostname)
05014 ast_string_field_set(reg, hostname, hostname);
05015 if (authuser)
05016 ast_string_field_set(reg, authuser, authuser);
05017 if (secret)
05018 ast_string_field_set(reg, secret, secret);
05019 reg->expire = -1;
05020 reg->timeout = -1;
05021 reg->refresh = default_expiry;
05022 reg->portno = portnum;
05023 reg->callid_valid = FALSE;
05024 reg->ocseq = INITIAL_CSEQ;
05025 reg->needdns = TRUE;
05026 ASTOBJ_CONTAINER_LINK(®l, reg);
05027 ASTOBJ_UNREF(reg,sip_registry_destroy);
05028 return 0;
05029 }
05030
05031
05032
05033 static int lws2sws(char *msgbuf, int len)
05034 {
05035 int h = 0, t = 0;
05036 int lws = 0;
05037
05038 for (; h < len;) {
05039
05040 if (msgbuf[h] == '\r') {
05041 h++;
05042 continue;
05043 }
05044
05045 if (msgbuf[h] == '\n') {
05046
05047 if (h + 1 == len)
05048 break;
05049
05050 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
05051
05052 h++;
05053 continue;
05054 }
05055
05056 msgbuf[t++] = msgbuf[h++];
05057 lws = 0;
05058 continue;
05059 }
05060 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
05061 if (lws) {
05062 h++;
05063 continue;
05064 }
05065 msgbuf[t++] = msgbuf[h++];
05066 lws = 1;
05067 continue;
05068 }
05069 msgbuf[t++] = msgbuf[h++];
05070 if (lws)
05071 lws = 0;
05072 }
05073 msgbuf[t] = '\0';
05074 return t;
05075 }
05076
05077
05078
05079
05080 static int parse_request(struct sip_request *req)
05081 {
05082
05083 char *c;
05084 int f = 0;
05085
05086 c = req->data;
05087
05088
05089 req->header[f] = c;
05090 while(*c) {
05091 if (*c == '\n') {
05092
05093 *c = 0;
05094
05095 if (sipdebug && option_debug > 3)
05096 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
05097 if (ast_strlen_zero(req->header[f])) {
05098
05099 c++;
05100 break;
05101 }
05102 if (f >= SIP_MAX_HEADERS - 1) {
05103 ast_log(LOG_WARNING, "Too many SIP headers. Ignoring.\n");
05104 } else {
05105 f++;
05106 req->header[f] = c + 1;
05107 }
05108 } else if (*c == '\r') {
05109
05110 *c = 0;
05111 }
05112 c++;
05113 }
05114
05115 req->headers = f;
05116
05117
05118 if (!ast_strlen_zero(req->header[f])) {
05119 if (sipdebug && option_debug > 3)
05120 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
05121 req->headers++;
05122 }
05123
05124
05125 f = 0;
05126 req->line[f] = c;
05127 while (*c) {
05128 if (*c == '\n') {
05129
05130 *c = 0;
05131 if (sipdebug && option_debug > 3)
05132 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
05133 if (f == SIP_MAX_LINES - 1) {
05134 ast_log(LOG_WARNING, "Too many SDP lines. Ignoring.\n");
05135 break;
05136 } else {
05137 f++;
05138 req->line[f] = c + 1;
05139 }
05140 } else if (*c == '\r') {
05141
05142 *c = 0;
05143 }
05144 c++;
05145 }
05146
05147 req->lines = f;
05148
05149
05150 if (!ast_strlen_zero(req->line[f])) {
05151 if (sipdebug && option_debug > 3)
05152 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
05153 req->lines++;
05154 }
05155
05156 if (*c)
05157 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
05158
05159
05160 return determine_firstline_parts(req);
05161 }
05162
05163
05164
05165
05166
05167
05168
05169
05170
05171 static int find_sdp(struct sip_request *req)
05172 {
05173 const char *content_type;
05174 const char *content_length;
05175 const char *search;
05176 char *boundary;
05177 unsigned int x;
05178 int boundaryisquoted = FALSE;
05179 int found_application_sdp = FALSE;
05180 int found_end_of_headers = FALSE;
05181
05182 content_length = get_header(req, "Content-Length");
05183
05184 if (!ast_strlen_zero(content_length)) {
05185 if (sscanf(content_length, "%30u", &x) != 1) {
05186 ast_log(LOG_WARNING, "Invalid Content-Length: %s\n", content_length);
05187 return 0;
05188 }
05189
05190
05191
05192 if (x == 0)
05193 return 0;
05194 }
05195
05196 content_type = get_header(req, "Content-Type");
05197
05198
05199 if (!strncasecmp(content_type, "application/sdp", 15)) {
05200 req->sdp_start = 0;
05201 req->sdp_count = req->lines;
05202 return req->lines ? 1 : 0;
05203 }
05204
05205
05206 if (strncasecmp(content_type, "multipart/mixed", 15))
05207 return 0;
05208
05209
05210 if ((search = strcasestr(content_type, ";boundary=")))
05211 search += 10;
05212 else if ((search = strcasestr(content_type, "; boundary=")))
05213 search += 11;
05214 else
05215 return 0;
05216
05217 if (ast_strlen_zero(search))
05218 return 0;
05219
05220
05221 if (*search == '\"') {
05222 search++;
05223 boundaryisquoted = TRUE;
05224 }
05225
05226
05227
05228 boundary = ast_strdupa(search - 2);
05229 boundary[0] = boundary[1] = '-';
05230
05231 if (boundaryisquoted)
05232 boundary[strlen(boundary) - 1] = '\0';
05233
05234
05235
05236
05237 for (x = 0; x < (req->lines ); x++) {
05238 if(!strncasecmp(req->line[x], boundary, strlen(boundary))){
05239 if(found_application_sdp && found_end_of_headers){
05240 req->sdp_count = (x - 1) - req->sdp_start;
05241 return 1;
05242 }
05243 found_application_sdp = FALSE;
05244 }
05245 if(!strcasecmp(req->line[x], "Content-Type: application/sdp"))
05246 found_application_sdp = TRUE;
05247
05248 if(strlen(req->line[x]) == 0 ){
05249 if(found_application_sdp && !found_end_of_headers){
05250 req->sdp_start = x;
05251 found_end_of_headers = TRUE;
05252 }
05253 }
05254 }
05255 if(found_application_sdp && found_end_of_headers) {
05256 req->sdp_count = x - req->sdp_start;
05257 return TRUE;
05258 }
05259 return FALSE;
05260 }
05261
05262
05263 static void change_hold_state(struct sip_pvt *dialog, struct sip_request *req, int holdstate, int sendonly)
05264 {
05265 if (global_notifyhold && (!holdstate || !ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD)))
05266 sip_peer_hold(dialog, holdstate);
05267 if (global_callevents)
05268 manager_event(EVENT_FLAG_CALL, holdstate ? "Hold" : "Unhold",
05269 "Channel: %s\r\n"
05270 "Uniqueid: %s\r\n",
05271 dialog->owner->name,
05272 dialog->owner->uniqueid);
05273 append_history(dialog, holdstate ? "Hold" : "Unhold", "%s", req->data);
05274 if (!holdstate) {
05275 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD);
05276 return;
05277 }
05278
05279
05280 if (sendonly == 1)
05281 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
05282 else if (sendonly == 2)
05283 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
05284 else
05285 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
05286 return;
05287 }
05288
05289 enum media_type {
05290 SDP_AUDIO,
05291 SDP_VIDEO,
05292 SDP_IMAGE,
05293 };
05294
05295 static int get_ip_and_port_from_sdp(struct sip_request *req, const enum media_type media, struct sockaddr_in *sin)
05296 {
05297 const char *m;
05298 const char *c;
05299 int miterator = req->sdp_start;
05300 int citerator = req->sdp_start;
05301 int x = 0;
05302 int numberofports;
05303 int len;
05304 char host[258] = "";
05305 struct ast_hostent audiohp;
05306 struct hostent *hp;
05307
05308 c = get_sdp_iterate(&citerator, req, "c");
05309 if (sscanf(c, "IN IP4 %256s", host) != 1) {
05310 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
05311
05312 }
05313
05314 for (m = get_sdp_iterate(&miterator, req, "m"); !ast_strlen_zero(m); m = get_sdp_iterate(&miterator, req, "m")) {
05315 if ((media == SDP_AUDIO && ((sscanf(m, "audio %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05316 (sscanf(m, "audio %30d RTP/AVP %n", &x, &len) == 1 && len > 0))) ||
05317 (media == SDP_VIDEO && ((sscanf(m, "video %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05318 (sscanf(m, "video %30d RTP/AVP %n", &x, &len) == 1 && len > 0)))) {
05319
05320
05321
05322
05323 c = get_sdp_iterate(&citerator, req, "c");
05324 if (!ast_strlen_zero(c)) {
05325 sscanf(c, "IN IP4 %256s", host);
05326 }
05327 break;
05328 }
05329 }
05330
05331 if (ast_strlen_zero(host) || x == 0) {
05332 ast_log(LOG_WARNING, "Failed to read an alternate host or port in SDP. Expect %s problems\n", media == SDP_AUDIO ? "audio" : "video");
05333 return -1;
05334 }
05335
05336 hp = ast_gethostbyname(host, &audiohp);
05337 if (!hp) {
05338 ast_log(LOG_WARNING, "Could not look up IP address of alternate hostname. Expect %s problems\n", media == SDP_AUDIO? "audio" : "video");
05339 return -1;
05340 }
05341
05342 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
05343 sin->sin_port = htons(x);
05344 return 0;
05345 }
05346
05347
05348
05349
05350
05351
05352 static int process_sdp(struct sip_pvt *p, struct sip_request *req)
05353 {
05354
05355 int start = req->sdp_start;
05356 int next = start;
05357 int iterator = start;
05358
05359
05360 char type = '\0';
05361 const char *value = NULL;
05362 const char *m = NULL;
05363 const char *nextm = NULL;
05364 int len = -1;
05365
05366
05367 struct ast_hostent audiohp;
05368 struct ast_hostent videohp;
05369 struct ast_hostent sessionhp;
05370 struct hostent *hp = NULL;
05371 struct hostent *vhp = NULL;
05372 int portno = -1;
05373 int vportno = -1;
05374 int udptlportno = -1;
05375 struct sockaddr_in sin;
05376 struct sockaddr_in vsin;
05377 struct sockaddr_in isin;
05378
05379
05380 int peercapability = 0, peernoncodeccapability = 0;
05381 int vpeercapability = 0, vpeernoncodeccapability = 0;
05382 struct ast_rtp *newaudiortp, *newvideortp;
05383 int newjointcapability;
05384 int newpeercapability;
05385 int newnoncodeccapability;
05386 const char *codecs;
05387 int codec;
05388
05389
05390 int sendonly = -1;
05391 int vsendonly = -1;
05392 int numberofports;
05393 int numberofmediastreams = 0;
05394 int last_rtpmap_codec = 0;
05395 int debug = sip_debug_test_pvt(p);
05396
05397
05398 if (!p->rtp) {
05399 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
05400 return -1;
05401 }
05402
05403
05404 #ifdef LOW_MEMORY
05405 newaudiortp = ast_threadstorage_get(&ts_audio_rtp, ast_rtp_alloc_size());
05406 #else
05407 newaudiortp = alloca(ast_rtp_alloc_size());
05408 #endif
05409 memset(newaudiortp, 0, ast_rtp_alloc_size());
05410 ast_rtp_new_init(newaudiortp);
05411 ast_rtp_pt_clear(newaudiortp);
05412
05413 #ifdef LOW_MEMORY
05414 newvideortp = ast_threadstorage_get(&ts_video_rtp, ast_rtp_alloc_size());
05415 #else
05416 newvideortp = alloca(ast_rtp_alloc_size());
05417 #endif
05418 memset(newvideortp, 0, ast_rtp_alloc_size());
05419 ast_rtp_new_init(newvideortp);
05420 ast_rtp_pt_clear(newvideortp);
05421
05422
05423
05424 p->lastrtprx = p->lastrtptx = time(NULL);
05425
05426 ast_set_flag(&p->flags[0], SIP_NOVIDEO);
05427
05428 memset(p->offered_media, 0, sizeof(p->offered_media));
05429
05430
05431
05432 nextm = get_sdp_iterate(&next, req, "m");
05433 if (ast_strlen_zero(nextm)) {
05434 ast_log(LOG_WARNING, "Insufficient information for SDP (m= not found)\n");
05435 return -1;
05436 }
05437
05438
05439 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
05440 int processed = FALSE;
05441 switch (type) {
05442 case 'c':
05443 if (process_sdp_c(value, &sessionhp)) {
05444 processed = TRUE;
05445 hp = &sessionhp.hp;
05446 vhp = hp;
05447 }
05448 break;
05449 case 'a':
05450 if (process_sdp_a_sendonly(value, &sendonly)) {
05451 processed = TRUE;
05452 vsendonly = sendonly;
05453 }
05454 else if (process_sdp_a_audio(value, p, newaudiortp, &last_rtpmap_codec))
05455 processed = TRUE;
05456 else if (process_sdp_a_video(value, p, newvideortp, &last_rtpmap_codec))
05457 processed = TRUE;
05458 else if (process_sdp_a_image(value, p))
05459 processed = TRUE;
05460 break;
05461 }
05462
05463 if (option_debug > 2)
05464 ast_log(LOG_DEBUG, "Processing session-level SDP %c=%s... %s\n", type, value, (processed == TRUE)? "OK." : "UNSUPPORTED.");
05465 }
05466
05467
05468
05469 while (!ast_strlen_zero(nextm)) {
05470 int audio = FALSE;
05471 int video = FALSE;
05472 int image = FALSE;
05473 int x;
05474
05475 numberofports = 1;
05476 len = -1;
05477 start = next;
05478 m = nextm;
05479 iterator = next;
05480 nextm = get_sdp_iterate(&next, req, "m");
05481
05482
05483 if ((sscanf(m, "audio %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05484 (sscanf(m, "audio %30d RTP/AVP %n", &x, &len) == 1 && len > 0)) {
05485
05486 audio = TRUE;
05487 p->offered_media[SDP_AUDIO].offered = TRUE;
05488 numberofmediastreams++;
05489 portno = x;
05490
05491
05492 codecs = m + len;
05493 ast_copy_string(p->offered_media[SDP_AUDIO].text, codecs, sizeof(p->offered_media[SDP_AUDIO].text));
05494 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
05495 if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
05496 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
05497 return -1;
05498 }
05499 if (debug)
05500 ast_verbose("Found RTP audio format %d\n", codec);
05501 ast_rtp_set_m_type(newaudiortp, codec);
05502 }
05503
05504 } else if ((sscanf(m, "video %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05505 (sscanf(m, "video %30d RTP/AVP %n", &x, &len) == 1 && len >= 0)) {
05506
05507 video = TRUE;
05508 ast_clear_flag(&p->flags[0], SIP_NOVIDEO);
05509 p->offered_media[SDP_VIDEO].offered = TRUE;
05510 numberofmediastreams++;
05511 vportno = x;
05512
05513
05514 codecs = m + len;
05515 ast_copy_string(p->offered_media[SDP_VIDEO].text, codecs, sizeof(p->offered_media[SDP_VIDEO].text));
05516 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
05517 if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
05518 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
05519 return -1;
05520 }
05521 if (debug)
05522 ast_verbose("Found RTP video format %d\n", codec);
05523 ast_rtp_set_m_type(newvideortp, codec);
05524 }
05525
05526 } else if (p->udptl && ((sscanf(m, "image %30d udptl t38%n", &x, &len) == 1 && len > 0) ||
05527 (sscanf(m, "image %30d UDPTL t38%n", &x, &len) == 1 && len >= 0))) {
05528
05529 image = TRUE;
05530 if (debug)
05531 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
05532 p->offered_media[SDP_IMAGE].offered = TRUE;
05533 udptlportno = x;
05534 numberofmediastreams++;
05535
05536 if (p->owner && p->lastinvite) {
05537 if (p->t38.state != T38_LOCAL_REINVITE) {
05538 p->t38.state = T38_PEER_REINVITE;
05539 if (option_debug > 1)
05540 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>" );
05541 }
05542 } else {
05543 p->t38.state = T38_PEER_DIRECT;
05544 p->t38.direct = 1;
05545 if (option_debug > 1)
05546 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05547 }
05548 } else {
05549 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
05550 continue;
05551 }
05552
05553
05554 if (numberofports > 1)
05555 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
05556
05557
05558
05559
05560 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
05561 int processed = FALSE;
05562
05563 switch (type) {
05564 case 'c':
05565 if (audio) {
05566 if (process_sdp_c(value, &audiohp)) {
05567 processed = TRUE;
05568 hp = &audiohp.hp;
05569 }
05570 }
05571 else if (video) {
05572 if (process_sdp_c(value, &videohp)) {
05573 processed = TRUE;
05574 vhp = &videohp.hp;
05575 }
05576 }
05577 break;
05578 case 'a':
05579
05580 if (audio) {
05581 if (process_sdp_a_sendonly(value, &sendonly))
05582 processed = TRUE;
05583 else if (process_sdp_a_audio(value, p, newaudiortp, &last_rtpmap_codec))
05584 processed = TRUE;
05585 }
05586
05587 else if (video) {
05588 if (process_sdp_a_sendonly(value, &vsendonly))
05589 processed = TRUE;
05590 else if (process_sdp_a_video(value, p, newvideortp, &last_rtpmap_codec))
05591 processed = TRUE;
05592 }
05593
05594 else if (image) {
05595 if (process_sdp_a_image(value, p))
05596 processed = TRUE;
05597 }
05598 break;
05599 }
05600
05601 if (option_debug > 2)
05602 ast_log(LOG_DEBUG, "Processing media-level (%s) SDP %c=%s... %s\n",
05603 (audio == TRUE)? "audio" : (video == TRUE)? "video" : "image",
05604 type, value,
05605 (processed == TRUE)? "OK." : "UNSUPPORTED.");
05606 }
05607 }
05608
05609
05610 if (!hp) {
05611 ast_log(LOG_WARNING, "Insufficient information in SDP (c=)...\n");
05612 return -1;
05613 }
05614
05615 if (portno == -1 && vportno == -1 && udptlportno == -1)
05616
05617
05618 return -2;
05619
05620 if (numberofmediastreams > 2)
05621
05622 return -3;
05623
05624 if (udptlportno == -1) {
05625 p->t38.state = T38_DISABLED;
05626 if (option_debug > 2)
05627 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05628 }
05629
05630
05631
05632 ast_rtp_get_current_formats(newaudiortp, &peercapability, &peernoncodeccapability);
05633 ast_rtp_get_current_formats(newvideortp, &vpeercapability, &vpeernoncodeccapability);
05634
05635 newjointcapability = p->capability & (peercapability | vpeercapability);
05636 newpeercapability = (peercapability | vpeercapability);
05637 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
05638
05639
05640 if (debug) {
05641
05642 char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE];
05643
05644 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s, combined - %s\n",
05645 ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
05646 ast_getformatname_multiple(s2, SIPBUFSIZE, newpeercapability),
05647 ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
05648 ast_getformatname_multiple(s4, SIPBUFSIZE, newjointcapability));
05649
05650 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
05651 ast_rtp_lookup_mime_multiple(s1, SIPBUFSIZE, p->noncodeccapability, 0, 0),
05652 ast_rtp_lookup_mime_multiple(s2, SIPBUFSIZE, peernoncodeccapability, 0, 0),
05653 ast_rtp_lookup_mime_multiple(s3, SIPBUFSIZE, newnoncodeccapability, 0, 0));
05654 }
05655 if (!newjointcapability) {
05656
05657 if (!p->t38.jointcapability || !udptlportno) {
05658 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
05659
05660 return -1;
05661 } else {
05662 if (option_debug > 2)
05663 ast_log(LOG_DEBUG, "Have T.38 but no audio codecs, accepting offer anyway\n");
05664 return 0;
05665 }
05666 }
05667
05668
05669
05670 p->jointcapability = newjointcapability;
05671 p->peercapability = newpeercapability;
05672 p->jointnoncodeccapability = newnoncodeccapability;
05673
05674 ast_rtp_pt_copy(p->rtp, newaudiortp);
05675 if (p->vrtp)
05676 ast_rtp_pt_copy(p->vrtp, newvideortp);
05677
05678 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
05679 ast_clear_flag(&p->flags[0], SIP_DTMF);
05680 if (newnoncodeccapability & AST_RTP_DTMF) {
05681
05682 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
05683
05684 ast_rtp_setdtmf(p->rtp, 1);
05685 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
05686 } else {
05687 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
05688 }
05689 }
05690
05691
05692 if (p->rtp) {
05693 if (portno > 0) {
05694 sin.sin_family = AF_INET;
05695 sin.sin_port = htons(portno);
05696 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
05697 ast_rtp_set_peer(p->rtp, &sin);
05698 if (debug)
05699 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05700 } else if (udptlportno > 0) {
05701 if (debug)
05702 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session.\n");
05703 } else {
05704 ast_rtp_stop(p->rtp);
05705 if (debug)
05706 ast_verbose("Peer doesn't provide audio\n");
05707 }
05708 }
05709
05710
05711 if (p->vrtp) {
05712 if (vportno > 0) {
05713 vsin.sin_family = AF_INET;
05714 vsin.sin_port = htons(vportno);
05715 memcpy(&vsin.sin_addr, vhp->h_addr, sizeof(vsin.sin_addr));
05716 ast_rtp_set_peer(p->vrtp, &vsin);
05717 if (debug)
05718 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
05719 } else {
05720 ast_rtp_stop(p->vrtp);
05721 if (debug)
05722 ast_verbose("Peer doesn't provide video\n");
05723 }
05724 }
05725
05726
05727 if (p->udptl) {
05728 if (udptlportno > 0) {
05729 isin.sin_family = AF_INET;
05730 isin.sin_port = htons(udptlportno);
05731 if (ast_test_flag(&p->flags[0], SIP_NAT) && ast_test_flag(&p->flags[1], SIP_PAGE2_UDPTL_DESTINATION)) {
05732 struct sockaddr_in peer;
05733 ast_rtp_get_peer(p->rtp, &peer);
05734 if (peer.sin_addr.s_addr) {
05735 memcpy(&isin.sin_addr, &peer.sin_addr, sizeof(isin.sin_addr));
05736 if (debug)
05737 ast_log(LOG_DEBUG, "Peer T.38 UDPTL is set behind NAT and with destination, destination address now %s\n", ast_inet_ntoa(isin.sin_addr));
05738 }
05739 } else
05740 memcpy(&isin.sin_addr, hp->h_addr, sizeof(isin.sin_addr));
05741 ast_udptl_set_peer(p->udptl, &isin);
05742 if (debug)
05743 ast_log(LOG_DEBUG,"Peer T.38 UDPTL is at port %s:%d\n",ast_inet_ntoa(isin.sin_addr), ntohs(isin.sin_port));
05744 } else {
05745 ast_udptl_stop(p->udptl);
05746 if (debug)
05747 ast_log(LOG_DEBUG, "Peer doesn't provide T.38 UDPTL\n");
05748 }
05749 }
05750
05751
05752
05753 if (option_debug > 1) {
05754 char buf[SIPBUFSIZE];
05755 ast_log(LOG_DEBUG, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
05756 }
05757
05758 if (!p->owner)
05759 return 0;
05760
05761 if (option_debug > 3)
05762 ast_log(LOG_DEBUG, "We have an owner, now see if we need to change this call\n");
05763
05764 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
05765 if (debug) {
05766 char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
05767 ast_log(LOG_DEBUG, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
05768 ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
05769 ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
05770 }
05771 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability);
05772 ast_set_read_format(p->owner, p->owner->readformat);
05773 ast_set_write_format(p->owner, p->owner->writeformat);
05774 }
05775
05776
05777 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1)) {
05778 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
05779
05780 ast_queue_frame(p->owner, &ast_null_frame);
05781 } else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1)) {
05782 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
05783 S_OR(p->mohsuggest, NULL),
05784 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
05785 if (sendonly)
05786 ast_rtp_stop(p->rtp);
05787
05788
05789 ast_queue_frame(p->owner, &ast_null_frame);
05790 }
05791
05792
05793 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1))
05794 change_hold_state(p, req, FALSE, sendonly);
05795 else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1))
05796 change_hold_state(p, req, TRUE, sendonly);
05797
05798 return 0;
05799 }
05800
05801
05802 static int process_sdp_c(const char *c, struct ast_hostent *ast_hp)
05803 {
05804 char host[258];
05805 struct hostent *hp;
05806
05807
05808 if (sscanf(c, "IN IP4 %255s", host) != 1) {
05809 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
05810 return FALSE;
05811 } else {
05812 if (!(hp = ast_gethostbyname(host, ast_hp))) {
05813 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in c= line, '%s'\n", c);
05814 return FALSE;
05815 }
05816 return TRUE;
05817 }
05818 return FALSE;
05819 }
05820
05821 static int process_sdp_a_sendonly(const char *a, int *sendonly)
05822 {
05823 int found = FALSE;
05824
05825 if (!strcasecmp(a, "sendonly")) {
05826 if (*sendonly == -1)
05827 *sendonly = 1;
05828 found = TRUE;
05829 } else if (!strcasecmp(a, "inactive")) {
05830 if (*sendonly == -1)
05831 *sendonly = 2;
05832 found = TRUE;
05833 } else if (!strcasecmp(a, "sendrecv")) {
05834 if (*sendonly == -1)
05835 *sendonly = 0;
05836 found = TRUE;
05837 }
05838 return found;
05839 }
05840
05841 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp *newaudiortp, int *last_rtpmap_codec)
05842 {
05843 int found = FALSE;
05844 int codec;
05845 char* mimeSubtype = ast_strdupa(a);
05846 int debug = sip_debug_test_pvt(p);
05847
05848 if (strlen(a) > 5 && !strncasecmp(a, "ptime", 5)) {
05849 char *tmp = strrchr(a, ':');
05850 long int framing = 0;
05851 if (tmp) {
05852 tmp++;
05853 framing = strtol(tmp, NULL, 10);
05854 if (framing == LONG_MIN || framing == LONG_MAX) {
05855 framing = 0;
05856 if (option_debug)
05857 ast_log(LOG_DEBUG, "Can't read framing from SDP: %s\n", a);
05858 }
05859 }
05860 if (framing && p->autoframing) {
05861 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
05862 int codec_n;
05863 int format = 0;
05864 for (codec_n = 0; codec_n < MAX_RTP_PT; codec_n++) {
05865 format = ast_rtp_codec_getformat(codec_n);
05866 if (!format)
05867 continue;
05868 if (option_debug)
05869 ast_log(LOG_DEBUG, "Setting framing for %d to %ld\n", format, framing);
05870 ast_codec_pref_setsize(pref, format, framing);
05871 }
05872 ast_rtp_codec_setpref(p->rtp, pref);
05873 }
05874 found = TRUE;
05875 } else if (sscanf(a, "rtpmap: %30u %[^/]/", &codec, mimeSubtype) == 2) {
05876
05877 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
05878
05879 if (ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
05880 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0) != -1) {
05881 if (debug)
05882 ast_verbose("Found audio description format %s for ID %d\n", mimeSubtype, codec);
05883 (*last_rtpmap_codec)++;
05884 found = TRUE;
05885 }
05886 } else {
05887 if (debug)
05888 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
05889 }
05890
05891 if (!found) {
05892
05893 ast_rtp_unset_m_type(newaudiortp, codec);
05894 if (debug)
05895 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
05896 }
05897 }
05898
05899 return found;
05900 }
05901
05902 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp *newvideortp, int *last_rtpmap_codec)
05903 {
05904 int found = FALSE;
05905 int codec;
05906 char* mimeSubtype = ast_strdupa(a);
05907 int debug = sip_debug_test_pvt(p);
05908
05909 if (sscanf(a, "rtpmap: %30u %[^/]/", &codec, mimeSubtype) == 2) {
05910
05911 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
05912
05913 if (p->vrtp && ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0) != -1) {
05914 if (debug)
05915 ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec);
05916 (*last_rtpmap_codec)++;
05917 found = TRUE;
05918 }
05919 } else {
05920 if (debug)
05921 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
05922 }
05923
05924 if (!found) {
05925
05926 ast_rtp_unset_m_type(newvideortp, codec);
05927 if (debug)
05928 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
05929 }
05930 }
05931
05932 return found;
05933 }
05934
05935 static int process_sdp_a_image(const char *a, struct sip_pvt *p)
05936 {
05937 int found = FALSE;
05938 int peert38capability = 0;
05939 char s[256];
05940 int x;
05941 int debug = sip_debug_test_pvt(p);
05942
05943
05944 if ((sscanf(a, "T38FaxMaxBuffer:%30d", &x) == 1)) {
05945 found = TRUE;
05946 if (option_debug > 2)
05947 ast_log(LOG_DEBUG, "MaxBufferSize:%d\n",x);
05948 } else if ((sscanf(a, "T38MaxBitRate:%30d", &x) == 1) || (sscanf(a, "T38FaxMaxRate:%30d", &x) == 1)) {
05949 found = TRUE;
05950 if (option_debug > 2)
05951 ast_log(LOG_DEBUG,"T38MaxBitRate: %d\n",x);
05952 switch (x) {
05953 case 14400:
05954 peert38capability |= T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05955 break;
05956 case 12000:
05957 peert38capability |= T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05958 break;
05959 case 9600:
05960 peert38capability |= T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05961 break;
05962 case 7200:
05963 peert38capability |= T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05964 break;
05965 case 4800:
05966 peert38capability |= T38FAX_RATE_4800 | T38FAX_RATE_2400;
05967 break;
05968 case 2400:
05969 peert38capability |= T38FAX_RATE_2400;
05970 break;
05971 }
05972 } else if ((sscanf(a, "T38FaxVersion:%30d", &x) == 1)) {
05973 found = TRUE;
05974 if (option_debug > 2)
05975 ast_log(LOG_DEBUG, "FaxVersion: %d\n",x);
05976 if (x == 0)
05977 peert38capability |= T38FAX_VERSION_0;
05978 else if (x == 1)
05979 peert38capability |= T38FAX_VERSION_1;
05980 } else if ((sscanf(a, "T38FaxMaxDatagram:%30d", &x) == 1) || (sscanf(a, "T38MaxDatagram:%30d", &x) == 1)) {
05981 found = TRUE;
05982 if (option_debug > 2)
05983 ast_log(LOG_DEBUG, "FaxMaxDatagram: %d\n",x);
05984 ast_udptl_set_far_max_datagram(p->udptl, x);
05985 ast_udptl_set_local_max_datagram(p->udptl, x);
05986 } else if ((strncmp(a, "T38FaxFillBitRemoval", 20) == 0)) {
05987 found = TRUE;
05988 if ((sscanf(a, "T38FaxFillBitRemoval:%30d", &x) == 1)) {
05989 if (option_debug > 2)
05990 ast_log(LOG_DEBUG, "FillBitRemoval: %d\n",x);
05991 if (x == 1)
05992 peert38capability |= T38FAX_FILL_BIT_REMOVAL;
05993 } else {
05994 if (option_debug > 2)
05995 ast_log(LOG_DEBUG, "FillBitRemoval\n");
05996 peert38capability |= T38FAX_FILL_BIT_REMOVAL;
05997 }
05998 } else if ((strncmp(a, "T38FaxTranscodingMMR", 20) == 0)) {
05999 found = TRUE;
06000 if ((sscanf(a, "T38FaxTranscodingMMR:%30d", &x) == 1)) {
06001 if (option_debug > 2)
06002 ast_log(LOG_DEBUG, "Transcoding MMR: %d\n",x);
06003 if (x == 1)
06004 peert38capability |= T38FAX_TRANSCODING_MMR;
06005 } else {
06006 if (option_debug > 2)
06007 ast_log(LOG_DEBUG, "Transcoding MMR\n");
06008 peert38capability |= T38FAX_TRANSCODING_MMR;
06009 }
06010 } else if ((strncmp(a, "T38FaxTranscodingJBIG", 21) == 0)) {
06011 found = TRUE;
06012 if ((sscanf(a, "T38FaxTranscodingJBIG:%30d", &x) == 1)) {
06013 if (option_debug > 2)
06014 ast_log(LOG_DEBUG, "Transcoding JBIG: %d\n",x);
06015 if (x == 1)
06016 peert38capability |= T38FAX_TRANSCODING_JBIG;
06017 } else {
06018 if (option_debug > 2)
06019 ast_log(LOG_DEBUG, "Transcoding JBIG\n");
06020 peert38capability |= T38FAX_TRANSCODING_JBIG;
06021 }
06022 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
06023 found = TRUE;
06024 if (option_debug > 2)
06025 ast_log(LOG_DEBUG, "RateManagement: %s\n", s);
06026 if (!strcasecmp(s, "localTCF"))
06027 peert38capability |= T38FAX_RATE_MANAGEMENT_LOCAL_TCF;
06028 else if (!strcasecmp(s, "transferredTCF"))
06029 peert38capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
06030 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
06031 found = TRUE;
06032 if (option_debug > 2)
06033 ast_log(LOG_DEBUG, "UDP EC: %s\n", s);
06034 if (!strcasecmp(s, "t38UDPRedundancy")) {
06035 peert38capability |= T38FAX_UDP_EC_REDUNDANCY;
06036 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
06037 } else if (!strcasecmp(s, "t38UDPFEC")) {
06038 peert38capability |= T38FAX_UDP_EC_FEC;
06039 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
06040 } else {
06041 peert38capability |= T38FAX_UDP_EC_NONE;
06042 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
06043 }
06044 }
06045
06046 if (found) {
06047 p->t38.peercapability = peert38capability;
06048 p->t38.jointcapability = (peert38capability & 255);
06049 peert38capability &= (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400);
06050 p->t38.jointcapability |= (peert38capability & p->t38.capability);
06051 }
06052 if (debug)
06053 ast_log(LOG_DEBUG, "Our T38 capability = (%d), peer T38 capability (%d), joint T38 capability (%d)\n",
06054 p->t38.capability,
06055 p->t38.peercapability,
06056 p->t38.jointcapability);
06057
06058 return found;
06059 }
06060
06061
06062
06063 #ifdef LOW_MEMORY
06064 static void ts_ast_rtp_destroy(void *data)
06065 {
06066 struct ast_rtp *tmp = data;
06067 ast_rtp_destroy(tmp);
06068 }
06069 #endif
06070
06071
06072 static int add_header(struct sip_request *req, const char *var, const char *value)
06073 {
06074 int maxlen = sizeof(req->data) - 4 - req->len;
06075
06076 if (req->headers == SIP_MAX_HEADERS) {
06077 ast_log(LOG_WARNING, "Out of SIP header space\n");
06078 return -1;
06079 }
06080
06081 if (req->lines) {
06082 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
06083 return -1;
06084 }
06085
06086 if (maxlen <= 0) {
06087 ast_log(LOG_WARNING, "Out of space, can't add anymore (%s:%s)\n", var, value);
06088 return -1;
06089 }
06090
06091 req->header[req->headers] = req->data + req->len;
06092
06093 if (compactheaders)
06094 var = find_alias(var, var);
06095
06096 snprintf(req->header[req->headers], maxlen, "%s: %s\r\n", var, value);
06097 req->len += strlen(req->header[req->headers]);
06098 req->headers++;
06099
06100 return 0;
06101 }
06102
06103
06104 static int add_header_contentLength(struct sip_request *req, int len)
06105 {
06106 char clen[10];
06107
06108 snprintf(clen, sizeof(clen), "%d", len);
06109 return add_header(req, "Content-Length", clen);
06110 }
06111
06112
06113 static int add_line(struct sip_request *req, const char *line)
06114 {
06115 if (req->lines == SIP_MAX_LINES) {
06116 ast_log(LOG_WARNING, "Out of SIP line space\n");
06117 return -1;
06118 }
06119 if (!req->lines) {
06120
06121 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
06122 req->len += strlen(req->data + req->len);
06123 }
06124 if (req->len >= sizeof(req->data) - 4) {
06125 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
06126 return -1;
06127 }
06128 req->line[req->lines] = req->data + req->len;
06129 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
06130 req->len += strlen(req->line[req->lines]);
06131 req->lines++;
06132 return 0;
06133 }
06134
06135
06136 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
06137 {
06138 const char *tmp = get_header(orig, field);
06139
06140 if (!ast_strlen_zero(tmp))
06141 return add_header(req, field, tmp);
06142 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
06143 return -1;
06144 }
06145
06146
06147 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
06148 {
06149 int start = 0;
06150 int copied = 0;
06151 for (;;) {
06152 const char *tmp = __get_header(orig, field, &start);
06153
06154 if (ast_strlen_zero(tmp))
06155 break;
06156
06157 add_header(req, field, tmp);
06158 copied++;
06159 }
06160 return copied ? 0 : -1;
06161 }
06162
06163
06164
06165
06166
06167
06168
06169 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
06170 {
06171 int copied = 0;
06172 int start = 0;
06173
06174 for (;;) {
06175 char new[512];
06176 const char *oh = __get_header(orig, field, &start);
06177
06178 if (ast_strlen_zero(oh))
06179 break;
06180
06181 if (!copied) {
06182 char leftmost[512], *others, *rport;
06183
06184
06185 ast_copy_string(leftmost, oh, sizeof(leftmost));
06186 others = strchr(leftmost, ',');
06187 if (others)
06188 *others++ = '\0';
06189
06190
06191 rport = strstr(leftmost, ";rport");
06192 if (rport && *(rport+6) == '=')
06193 rport = NULL;
06194
06195
06196 if (rport && ((ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_ALWAYS) || (ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_RFC3581))) {
06197
06198 char *end;
06199
06200 rport = strstr(leftmost, ";rport");
06201
06202 if (rport) {
06203 end = strchr(rport + 1, ';');
06204 if (end)
06205 memmove(rport, end, strlen(end) + 1);
06206 else
06207 *rport = '\0';
06208 }
06209
06210
06211 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
06212 leftmost, ast_inet_ntoa(p->recv.sin_addr),
06213 ntohs(p->recv.sin_port),
06214 others ? "," : "", others ? others : "");
06215 } else {
06216
06217 snprintf(new, sizeof(new), "%s;received=%s%s%s",
06218 leftmost, ast_inet_ntoa(p->recv.sin_addr),
06219 others ? "," : "", others ? others : "");
06220 }
06221 oh = new;
06222 }
06223 add_header(req, field, oh);
06224 copied++;
06225 }
06226 if (!copied) {
06227 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
06228 return -1;
06229 }
06230 return 0;
06231 }
06232
06233
06234 static void add_route(struct sip_request *req, struct sip_route *route)
06235 {
06236 char r[SIPBUFSIZE*2], *p;
06237 int n, rem = sizeof(r);
06238
06239 if (!route)
06240 return;
06241
06242 p = r;
06243 for (;route ; route = route->next) {
06244 n = strlen(route->hop);
06245 if (rem < n+3)
06246 break;
06247 if (p != r) {
06248 *p++ = ',';
06249 --rem;
06250 }
06251 *p++ = '<';
06252 ast_copy_string(p, route->hop, rem);
06253 p += n;
06254 *p++ = '>';
06255 rem -= (n+2);
06256 }
06257 *p = '\0';
06258 add_header(req, "Route", r);
06259 }
06260
06261
06262 static void set_destination(struct sip_pvt *p, char *uri)
06263 {
06264 char *h, *maddr, hostname[256];
06265 int port, hn;
06266 struct hostent *hp;
06267 struct ast_hostent ahp;
06268 int debug=sip_debug_test_pvt(p);
06269
06270
06271
06272
06273 if (debug)
06274 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
06275
06276
06277 h = strchr(uri, '@');
06278 if (h)
06279 ++h;
06280 else {
06281 h = uri;
06282 if (strncasecmp(h, "sip:", 4) == 0)
06283 h += 4;
06284 else if (strncasecmp(h, "sips:", 5) == 0)
06285 h += 5;
06286 }
06287 hn = strcspn(h, ":;>") + 1;
06288 if (hn > sizeof(hostname))
06289 hn = sizeof(hostname);
06290 ast_copy_string(hostname, h, hn);
06291
06292 h += hn - 1;
06293
06294
06295 if (*h == ':') {
06296
06297 ++h;
06298 port = strtol(h, &h, 10);
06299 }
06300 else
06301 port = STANDARD_SIP_PORT;
06302
06303
06304 maddr = strstr(h, "maddr=");
06305 if (maddr) {
06306 maddr += 6;
06307 hn = strspn(maddr, "0123456789.") + 1;
06308 if (hn > sizeof(hostname))
06309 hn = sizeof(hostname);
06310 ast_copy_string(hostname, maddr, hn);
06311 }
06312
06313 hp = ast_gethostbyname(hostname, &ahp);
06314 if (hp == NULL) {
06315 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
06316 return;
06317 }
06318 p->sa.sin_family = AF_INET;
06319 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
06320 p->sa.sin_port = htons(port);
06321 if (debug)
06322 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(p->sa.sin_addr), port);
06323 }
06324
06325
06326 static int init_resp(struct sip_request *resp, const char *msg)
06327 {
06328
06329 memset(resp, 0, sizeof(*resp));
06330 resp->method = SIP_RESPONSE;
06331 resp->header[0] = resp->data;
06332 snprintf(resp->header[0], sizeof(resp->data), "SIP/2.0 %s\r\n", msg);
06333 resp->len = strlen(resp->header[0]);
06334 resp->headers++;
06335 return 0;
06336 }
06337
06338
06339 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
06340 {
06341
06342 memset(req, 0, sizeof(*req));
06343 req->method = sipmethod;
06344 req->header[0] = req->data;
06345 snprintf(req->header[0], sizeof(req->data), "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
06346 req->len = strlen(req->header[0]);
06347 req->headers++;
06348 return 0;
06349 }
06350
06351
06352 static inline int resp_needs_contact(const char *msg, enum sipmethod method) {
06353
06354
06355
06356
06357
06358
06359
06360
06361
06362
06363
06364
06365
06366
06367
06368 switch (method) {
06369
06370 case SIP_INVITE:
06371 case SIP_UPDATE:
06372 case SIP_SUBSCRIBE:
06373 case SIP_NOTIFY:
06374 if ((msg[0] >= '1' && msg[0] <= '3') || !strncmp(msg, "485", 3))
06375 return 1;
06376 break;
06377
06378
06379 case SIP_REGISTER:
06380 case SIP_OPTIONS:
06381 if (msg[0] == '2' || msg[0] == '3' || !strncmp(msg, "485", 3))
06382 return 1;
06383 break;
06384
06385
06386 case SIP_BYE:
06387 case SIP_PRACK:
06388 case SIP_MESSAGE:
06389 case SIP_PUBLISH:
06390 if (msg[0] == '3' || !strncmp(msg, "485", 3))
06391 return 1;
06392 break;
06393
06394
06395 case SIP_REFER:
06396 if (msg[0] >= '2' && msg[0] <= '6')
06397 return 1;
06398 break;
06399
06400
06401 case SIP_ACK:
06402 case SIP_CANCEL:
06403 case SIP_INFO:
06404 case SIP_PING:
06405 default:
06406 return 0;
06407 }
06408 return 0;
06409 }
06410
06411
06412
06413 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
06414 {
06415 char newto[256];
06416 const char *ot;
06417
06418 init_resp(resp, msg);
06419 copy_via_headers(p, resp, req, "Via");
06420 if (msg[0] == '1' || msg[0] == '2')
06421 copy_all_header(resp, req, "Record-Route");
06422 copy_header(resp, req, "From");
06423 ot = get_header(req, "To");
06424 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
06425
06426
06427 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
06428 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
06429 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
06430 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
06431 else
06432 ast_copy_string(newto, ot, sizeof(newto));
06433 ot = newto;
06434 }
06435 add_header(resp, "To", ot);
06436 copy_header(resp, req, "Call-ID");
06437 copy_header(resp, req, "CSeq");
06438 if (!ast_strlen_zero(global_useragent))
06439 add_header(resp, "User-Agent", global_useragent);
06440 add_header(resp, "Allow", ALLOWED_METHODS);
06441 add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
06442 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
06443
06444
06445 char tmp[256];
06446
06447 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
06448 add_header(resp, "Expires", tmp);
06449 if (p->expiry) {
06450 char contact[SIPBUFSIZE];
06451 snprintf(contact, sizeof(contact), "%s;expires=%d", (p->method == SIP_SUBSCRIBE ? p->our_contact : p->fullcontact), p->expiry);
06452 add_header(resp, "Contact", contact);
06453 }
06454 } else if (!ast_strlen_zero(p->our_contact) && resp_needs_contact(msg, p->method)) {
06455 add_header(resp, "Contact", p->our_contact);
06456 }
06457 return 0;
06458 }
06459
06460
06461 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
06462 {
06463 struct sip_request *orig = &p->initreq;
06464 char stripped[80];
06465 char tmp[80];
06466 char newto[256];
06467 const char *c;
06468 const char *ot, *of;
06469 int is_strict = FALSE;
06470
06471 memset(req, 0, sizeof(struct sip_request));
06472
06473 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
06474
06475 if (!seqno) {
06476 p->ocseq++;
06477 seqno = p->ocseq;
06478 }
06479
06480
06481 if (sipmethod == SIP_CANCEL) {
06482 p->branch = p->invite_branch;
06483 build_via(p);
06484 } else if (newbranch && (sipmethod == SIP_INVITE)) {
06485 p->branch ^= ast_random();
06486 p->invite_branch = p->branch;
06487 build_via(p);
06488 } else if (newbranch) {
06489 p->branch ^= ast_random();
06490 build_via(p);
06491 }
06492
06493
06494 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop,";lr") == NULL) {
06495 is_strict = TRUE;
06496 if (sipdebug)
06497 ast_log(LOG_DEBUG, "Strict routing enforced for session %s\n", p->callid);
06498 }
06499
06500 if (sipmethod == SIP_CANCEL)
06501 c = p->initreq.rlPart2;
06502 else if (sipmethod == SIP_ACK) {
06503
06504
06505 if (!ast_strlen_zero(p->okcontacturi))
06506 c = is_strict ? p->route->hop : p->okcontacturi;
06507 else
06508 c = p->initreq.rlPart2;
06509 } else if (!ast_strlen_zero(p->okcontacturi))
06510 c = is_strict ? p->route->hop : p->okcontacturi;
06511 else if (!ast_strlen_zero(p->uri))
06512 c = p->uri;
06513 else {
06514 char *n;
06515
06516 ast_copy_string(stripped, get_header(orig, (ast_test_flag(&p->flags[0], SIP_OUTGOING)) ? "To" : "From"),
06517 sizeof(stripped));
06518 n = get_in_brackets(stripped);
06519 c = strsep(&n, ";");
06520 }
06521 init_req(req, sipmethod, c);
06522
06523 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
06524
06525 add_header(req, "Via", p->via);
06526 if (p->route) {
06527 set_destination(p, p->route->hop);
06528 add_route(req, is_strict ? p->route->next : p->route);
06529 }
06530
06531 ot = get_header(orig, "To");
06532 of = get_header(orig, "From");
06533
06534
06535
06536 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
06537
06538
06539 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_strlen_zero(p->theirtag))
06540 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
06541 else if (!ast_test_flag(&p->flags[0], SIP_OUTGOING))
06542 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
06543 else
06544 snprintf(newto, sizeof(newto), "%s", ot);
06545 ot = newto;
06546 }
06547
06548 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06549 add_header(req, "From", of);
06550 add_header(req, "To", ot);
06551 } else {
06552 add_header(req, "From", ot);
06553 add_header(req, "To", of);
06554 }
06555
06556 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
06557 add_header(req, "Contact", p->our_contact);
06558
06559 copy_header(req, orig, "Call-ID");
06560 add_header(req, "CSeq", tmp);
06561
06562 if (!ast_strlen_zero(global_useragent))
06563 add_header(req, "User-Agent", global_useragent);
06564 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
06565
06566 if (!ast_strlen_zero(p->rpid))
06567 add_header(req, "Remote-Party-ID", p->rpid);
06568
06569 return 0;
06570 }
06571
06572
06573 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06574 {
06575 struct sip_request resp;
06576 int seqno = 0;
06577
06578 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
06579 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
06580 return -1;
06581 }
06582 respprep(&resp, p, msg, req);
06583 add_header_contentLength(&resp, 0);
06584
06585
06586 if (p->method == SIP_INVITE && msg[0] != '1' && p->owner && p->owner->hangupcause) {
06587 char buf[10];
06588
06589 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
06590 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
06591 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
06592 }
06593 return send_response(p, &resp, reliable, seqno);
06594 }
06595
06596 static void temp_pvt_cleanup(void *data)
06597 {
06598 struct sip_pvt *p = data;
06599
06600 ast_string_field_free_memory(p);
06601
06602 free(data);
06603 }
06604
06605
06606 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)
06607 {
06608 struct sip_pvt *p = NULL;
06609
06610 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
06611 ast_log(LOG_NOTICE, "Failed to get temporary pvt\n");
06612 return -1;
06613 }
06614
06615
06616 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
06617 ast_set_flag(&p->flags[0], SIP_NO_HISTORY);
06618 if (ast_string_field_init(p, 512))
06619 return -1;
06620 }
06621
06622
06623 p->method = intended_method;
06624
06625 if (sin) {
06626 p->sa = *sin;
06627 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
06628 p->ourip = __ourip;
06629 } else
06630 p->ourip = __ourip;
06631
06632 p->branch = ast_random();
06633 make_our_tag(p->tag, sizeof(p->tag));
06634 p->ocseq = INITIAL_CSEQ;
06635
06636 if (useglobal_nat && sin) {
06637 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
06638 p->recv = *sin;
06639 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
06640 }
06641 check_via(p, req);
06642
06643 ast_string_field_set(p, fromdomain, default_fromdomain);
06644 build_via(p);
06645 ast_string_field_set(p, callid, callid);
06646
06647
06648 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
06649
06650
06651 ast_string_field_reset_all(p);
06652
06653 return 0;
06654 }
06655
06656
06657 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
06658 {
06659 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
06660 }
06661
06662
06663 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
06664 {
06665 struct sip_request resp;
06666 respprep(&resp, p, msg, req);
06667 append_date(&resp);
06668 add_header(&resp, "Unsupported", unsupported);
06669 add_header_contentLength(&resp, 0);
06670 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
06671 }
06672
06673
06674
06675
06676 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
06677 {
06678 return __transmit_response(p, msg, req, XMIT_CRITICAL);
06679 }
06680
06681
06682 static void append_date(struct sip_request *req)
06683 {
06684 char tmpdat[256];
06685 struct tm tm;
06686 time_t t = time(NULL);
06687
06688 gmtime_r(&t, &tm);
06689 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
06690 add_header(req, "Date", tmpdat);
06691 }
06692
06693
06694 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
06695 {
06696 struct sip_request resp;
06697 respprep(&resp, p, msg, req);
06698 append_date(&resp);
06699 add_header_contentLength(&resp, 0);
06700 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
06701 }
06702
06703
06704 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06705 {
06706 struct sip_request resp;
06707 respprep(&resp, p, msg, req);
06708 add_header(&resp, "Accept", "application/sdp");
06709 add_header_contentLength(&resp, 0);
06710 return send_response(p, &resp, reliable, 0);
06711 }
06712
06713
06714 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)
06715 {
06716 struct sip_request resp;
06717 char tmp[512];
06718 int seqno = 0;
06719
06720 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
06721 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
06722 return -1;
06723 }
06724
06725
06726 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
06727 respprep(&resp, p, msg, req);
06728 add_header(&resp, header, tmp);
06729 add_header_contentLength(&resp, 0);
06730 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
06731 return send_response(p, &resp, reliable, seqno);
06732 }
06733
06734
06735 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp)
06736 {
06737 int res;
06738
06739 if (!(res = with_sdp ? transmit_response_with_sdp(p, msg, req, XMIT_UNRELIABLE) : transmit_response(p, msg, req))) {
06740 p->last_provisional = msg;
06741 update_provisional_keepalive(p, with_sdp);
06742 }
06743
06744 return res;
06745 }
06746
06747
06748 static int add_text(struct sip_request *req, const char *text)
06749 {
06750
06751 add_header(req, "Content-Type", "text/plain");
06752 add_header_contentLength(req, strlen(text));
06753 add_line(req, text);
06754 return 0;
06755 }
06756
06757
06758
06759 static int add_digit(struct sip_request *req, char digit, unsigned int duration)
06760 {
06761 char tmp[256];
06762
06763 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
06764 add_header(req, "Content-Type", "application/dtmf-relay");
06765 add_header_contentLength(req, strlen(tmp));
06766 add_line(req, tmp);
06767 return 0;
06768 }
06769
06770
06771
06772 static int add_vidupdate(struct sip_request *req)
06773 {
06774 const char *xml_is_a_huge_waste_of_space =
06775 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
06776 " <media_control>\r\n"
06777 " <vc_primitive>\r\n"
06778 " <to_encoder>\r\n"
06779 " <picture_fast_update>\r\n"
06780 " </picture_fast_update>\r\n"
06781 " </to_encoder>\r\n"
06782 " </vc_primitive>\r\n"
06783 " </media_control>\r\n";
06784 add_header(req, "Content-Type", "application/media_control+xml");
06785 add_header_contentLength(req, strlen(xml_is_a_huge_waste_of_space));
06786 add_line(req, xml_is_a_huge_waste_of_space);
06787 return 0;
06788 }
06789
06790
06791 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
06792 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
06793 int debug, int *min_packet_size)
06794 {
06795 int rtp_code;
06796 struct ast_format_list fmt;
06797
06798
06799 if (debug)
06800 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
06801 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
06802 return;
06803
06804 if (p->rtp) {
06805 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
06806 fmt = ast_codec_pref_getsize(pref, codec);
06807 } else
06808 return;
06809 ast_build_string(m_buf, m_size, " %d", rtp_code);
06810 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
06811 ast_rtp_lookup_mime_subtype(1, codec,
06812 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
06813 sample_rate);
06814 if (codec == AST_FORMAT_G729A) {
06815
06816 ast_build_string(a_buf, a_size, "a=fmtp:%d annexb=no\r\n", rtp_code);
06817 } else if (codec == AST_FORMAT_G723_1) {
06818
06819 ast_build_string(a_buf, a_size, "a=fmtp:%d annexa=no\r\n", rtp_code);
06820 } else if (codec == AST_FORMAT_ILBC) {
06821
06822 ast_build_string(a_buf, a_size, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
06823 }
06824
06825 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
06826 *min_packet_size = fmt.cur_ms;
06827
06828
06829 if ((*min_packet_size) == 0 && fmt.cur_ms)
06830 *min_packet_size = fmt.cur_ms;
06831 }
06832
06833
06834 static int t38_get_rate(int t38cap)
06835 {
06836 int maxrate = (t38cap & (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400));
06837
06838 if (maxrate & T38FAX_RATE_14400) {
06839 if (option_debug > 1)
06840 ast_log(LOG_DEBUG, "T38MaxBitRate 14400 found\n");
06841 return 14400;
06842 } else if (maxrate & T38FAX_RATE_12000) {
06843 if (option_debug > 1)
06844 ast_log(LOG_DEBUG, "T38MaxBitRate 12000 found\n");
06845 return 12000;
06846 } else if (maxrate & T38FAX_RATE_9600) {
06847 if (option_debug > 1)
06848 ast_log(LOG_DEBUG, "T38MaxBitRate 9600 found\n");
06849 return 9600;
06850 } else if (maxrate & T38FAX_RATE_7200) {
06851 if (option_debug > 1)
06852 ast_log(LOG_DEBUG, "T38MaxBitRate 7200 found\n");
06853 return 7200;
06854 } else if (maxrate & T38FAX_RATE_4800) {
06855 if (option_debug > 1)
06856 ast_log(LOG_DEBUG, "T38MaxBitRate 4800 found\n");
06857 return 4800;
06858 } else if (maxrate & T38FAX_RATE_2400) {
06859 if (option_debug > 1)
06860 ast_log(LOG_DEBUG, "T38MaxBitRate 2400 found\n");
06861 return 2400;
06862 } else {
06863 if (option_debug > 1)
06864 ast_log(LOG_DEBUG, "Strange, T38MaxBitRate NOT found in peers T38 SDP.\n");
06865 return 0;
06866 }
06867 }
06868
06869
06870 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
06871 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
06872 int debug)
06873 {
06874 int rtp_code;
06875
06876 if (debug)
06877 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format, 0));
06878 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
06879 return;
06880
06881 ast_build_string(m_buf, m_size, " %d", rtp_code);
06882 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
06883 ast_rtp_lookup_mime_subtype(0, format, 0),
06884 sample_rate);
06885 if (format == AST_RTP_DTMF)
06886
06887 ast_build_string(a_buf, a_size, "a=fmtp:%d 0-16\r\n", rtp_code);
06888 }
06889
06890
06891
06892
06893
06894
06895 #define SDP_SAMPLE_RATE(x) 8000
06896
06897
06898 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int add_audio, int add_t38)
06899 {
06900 int len = 0;
06901 int alreadysent = 0;
06902
06903 struct sockaddr_in sin;
06904 struct sockaddr_in vsin;
06905 struct sockaddr_in dest;
06906 struct sockaddr_in vdest = { 0, };
06907
06908
06909 char *version = "v=0\r\n";
06910 char *subject = "s=session\r\n";
06911 char owner[256];
06912 char connection[256];
06913 char *stime = "t=0 0\r\n";
06914 char bandwidth[256] = "";
06915 char *hold;
06916 char m_audio[256];
06917 char m_video[256];
06918 char m_modem[256];
06919 char a_audio[1024];
06920 char a_video[1024];
06921 char a_modem[1024];
06922 char *m_audio_next = m_audio;
06923 char *m_video_next = m_video;
06924 char *m_modem_next = m_modem;
06925 size_t m_audio_left = sizeof(m_audio);
06926 size_t m_video_left = sizeof(m_video);
06927 size_t m_modem_left = sizeof(m_modem);
06928 char *a_audio_next = a_audio;
06929 char *a_video_next = a_video;
06930 char *a_modem_next = a_modem;
06931 size_t a_audio_left = sizeof(a_audio);
06932 size_t a_video_left = sizeof(a_video);
06933 size_t a_modem_left = sizeof(a_modem);
06934 char dummy_answer[256];
06935
06936 int x;
06937 int capability = 0;
06938 int needvideo = FALSE;
06939 int debug = sip_debug_test_pvt(p);
06940 int min_audio_packet_size = 0;
06941 int min_video_packet_size = 0;
06942
06943 m_video[0] = '\0';
06944 m_modem[0] = '\0';
06945
06946 if (!p->rtp) {
06947 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
06948 return AST_FAILURE;
06949 }
06950
06951
06952 if (!p->sessionid) {
06953 p->sessionid = getpid();
06954 p->sessionversion = p->sessionid;
06955 } else
06956 p->sessionversion++;
06957
06958
06959 ast_rtp_get_us(p->rtp, &sin);
06960 if (p->vrtp)
06961 ast_rtp_get_us(p->vrtp, &vsin);
06962
06963
06964 if (p->redirip.sin_addr.s_addr) {
06965 dest.sin_port = p->redirip.sin_port;
06966 dest.sin_addr = p->redirip.sin_addr;
06967 } else {
06968 dest.sin_addr = p->ourip;
06969 dest.sin_port = sin.sin_port;
06970 }
06971
06972 snprintf(owner, sizeof(owner), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(dest.sin_addr));
06973 snprintf(connection, sizeof(connection), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
06974
06975 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR)
06976 hold = "a=recvonly\r\n";
06977 else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE)
06978 hold = "a=inactive\r\n";
06979 else
06980 hold = "a=sendrecv\r\n";
06981
06982 if (add_audio) {
06983 capability = p->jointcapability;
06984
06985
06986 if (option_debug > 1) {
06987 char codecbuf[SIPBUFSIZE];
06988 ast_log(LOG_DEBUG, "** Our capability: %s Video flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability), ast_test_flag(&p->flags[0], SIP_NOVIDEO) ? "True" : "False");
06989 ast_log(LOG_DEBUG, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
06990 }
06991
06992 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
06993 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_RTP)) {
06994 ast_build_string(&m_audio_next, &m_audio_left, " %d", 191);
06995 ast_build_string(&a_audio_next, &a_audio_left, "a=rtpmap:%d %s/%d\r\n", 191, "t38", 8000);
06996 }
06997 #endif
06998
06999
07000 if ((capability & AST_FORMAT_VIDEO_MASK) && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
07001 if (p->vrtp) {
07002 needvideo = TRUE;
07003 if (option_debug > 1)
07004 ast_log(LOG_DEBUG, "This call needs video offers!\n");
07005 } else if (option_debug > 1)
07006 ast_log(LOG_DEBUG, "This call needs video offers, but there's no video support enabled!\n");
07007 }
07008
07009
07010
07011
07012 if (needvideo) {
07013
07014 if (p->vredirip.sin_addr.s_addr) {
07015 vdest.sin_addr = p->vredirip.sin_addr;
07016 vdest.sin_port = p->vredirip.sin_port;
07017 } else {
07018 vdest.sin_addr = p->ourip;
07019 vdest.sin_port = vsin.sin_port;
07020 }
07021 ast_build_string(&m_video_next, &m_video_left, "m=video %d RTP/AVP", ntohs(vsin.sin_port));
07022
07023
07024 if (p->maxcallbitrate)
07025 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
07026 if (debug)
07027 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(vsin.sin_port));
07028 }
07029
07030 if (debug)
07031 ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(sin.sin_port));
07032
07033 ast_build_string(&m_audio_next, &m_audio_left, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
07034
07035
07036
07037
07038
07039
07040
07041
07042
07043
07044 if (capability & p->prefcodec) {
07045 int codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
07046
07047 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
07048 &m_audio_next, &m_audio_left,
07049 &a_audio_next, &a_audio_left,
07050 debug, &min_audio_packet_size);
07051 alreadysent |= codec;
07052 }
07053
07054
07055 for (x = 0; x < 32; x++) {
07056 int codec;
07057
07058 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
07059 break;
07060
07061 if (!(capability & codec))
07062 continue;
07063
07064 if (alreadysent & codec)
07065 continue;
07066
07067 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
07068 &m_audio_next, &m_audio_left,
07069 &a_audio_next, &a_audio_left,
07070 debug, &min_audio_packet_size);
07071 alreadysent |= codec;
07072 }
07073
07074
07075 for (x = 1; x <= (needvideo ? AST_FORMAT_MAX_VIDEO : AST_FORMAT_MAX_AUDIO); x <<= 1) {
07076 if (!(capability & x))
07077 continue;
07078
07079 if (alreadysent & x)
07080 continue;
07081
07082 if (x <= AST_FORMAT_MAX_AUDIO)
07083 add_codec_to_sdp(p, x, SDP_SAMPLE_RATE(x),
07084 &m_audio_next, &m_audio_left,
07085 &a_audio_next, &a_audio_left,
07086 debug, &min_audio_packet_size);
07087 else
07088 add_codec_to_sdp(p, x, 90000,
07089 &m_video_next, &m_video_left,
07090 &a_video_next, &a_video_left,
07091 debug, &min_video_packet_size);
07092 }
07093
07094
07095 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
07096 if (!(p->jointnoncodeccapability & x))
07097 continue;
07098
07099 add_noncodec_to_sdp(p, x, 8000,
07100 &m_audio_next, &m_audio_left,
07101 &a_audio_next, &a_audio_left,
07102 debug);
07103 }
07104
07105 if (option_debug > 2)
07106 ast_log(LOG_DEBUG, "-- Done with adding codecs to SDP\n");
07107
07108 if (!p->owner || !ast_internal_timing_enabled(p->owner))
07109 ast_build_string(&a_audio_next, &a_audio_left, "a=silenceSupp:off - - - -\r\n");
07110
07111 if (min_audio_packet_size)
07112 ast_build_string(&a_audio_next, &a_audio_left, "a=ptime:%d\r\n", min_audio_packet_size);
07113
07114 if (min_video_packet_size)
07115 ast_build_string(&a_video_next, &a_video_left, "a=ptime:%d\r\n", min_video_packet_size);
07116
07117 if ((m_audio_left < 2) || (m_video_left < 2) || (a_audio_left == 0) || (a_video_left == 0))
07118 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
07119
07120 ast_build_string(&m_audio_next, &m_audio_left, "\r\n");
07121 if (needvideo)
07122 ast_build_string(&m_video_next, &m_video_left, "\r\n");
07123 }
07124
07125 if (add_t38 && p->udptl) {
07126 struct sockaddr_in udptlsin;
07127 struct sockaddr_in udptldest = { 0, };
07128
07129 ast_udptl_get_us(p->udptl, &udptlsin);
07130
07131 if (p->udptlredirip.sin_addr.s_addr) {
07132 udptldest.sin_port = p->udptlredirip.sin_port;
07133 udptldest.sin_addr = p->udptlredirip.sin_addr;
07134 } else {
07135 udptldest.sin_addr = p->ourip;
07136 udptldest.sin_port = udptlsin.sin_port;
07137 }
07138
07139 if (debug) {
07140 ast_log(LOG_DEBUG, "T.38 UDPTL is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(udptlsin.sin_port));
07141 ast_log(LOG_DEBUG, "Our T38 capability (%d), peer T38 capability (%d), joint capability (%d)\n",
07142 p->t38.capability,
07143 p->t38.peercapability,
07144 p->t38.jointcapability);
07145 }
07146
07147 ast_build_string(&m_modem_next, &m_modem_left, "m=image %d udptl t38\r\n", ntohs(udptldest.sin_port));
07148
07149 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_0)
07150 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:0\r\n");
07151 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_1)
07152 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:1\r\n");
07153 if ((x = t38_get_rate(p->t38.jointcapability)))
07154 ast_build_string(&a_modem_next, &a_modem_left, "a=T38MaxBitRate:%d\r\n",x);
07155 if ((p->t38.jointcapability & T38FAX_FILL_BIT_REMOVAL) == T38FAX_FILL_BIT_REMOVAL)
07156 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxFillBitRemoval\r\n");
07157 if ((p->t38.jointcapability & T38FAX_TRANSCODING_MMR) == T38FAX_TRANSCODING_MMR)
07158 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingMMR\r\n");
07159 if ((p->t38.jointcapability & T38FAX_TRANSCODING_JBIG) == T38FAX_TRANSCODING_JBIG)
07160 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingJBIG\r\n");
07161 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxRateManagement:%s\r\n", (p->t38.jointcapability & T38FAX_RATE_MANAGEMENT_LOCAL_TCF) ? "localTCF" : "transferredTCF");
07162 x = ast_udptl_get_local_max_datagram(p->udptl);
07163 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxBuffer:%d\r\n",x);
07164 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxDatagram:%d\r\n",x);
07165 if (p->t38.jointcapability != T38FAX_UDP_EC_NONE)
07166 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxUdpEC:%s\r\n", (p->t38.jointcapability & T38FAX_UDP_EC_REDUNDANCY) ? "t38UDPRedundancy" : "t38UDPFEC");
07167 }
07168
07169 len = strlen(version) + strlen(subject) + strlen(owner) + strlen(connection) + strlen(stime);
07170 if (add_audio)
07171 len += strlen(m_audio) + strlen(a_audio) + strlen(hold);
07172 if (needvideo)
07173 len += strlen(m_video) + strlen(a_video) + strlen(bandwidth) + strlen(hold);
07174 if (add_t38) {
07175 len += strlen(m_modem) + strlen(a_modem);
07176 }
07177
07178 add_header(resp, "Content-Type", "application/sdp");
07179 add_header_contentLength(resp, len);
07180 add_line(resp, version);
07181 add_line(resp, owner);
07182 add_line(resp, subject);
07183 add_line(resp, connection);
07184 if (needvideo)
07185 add_line(resp, bandwidth);
07186 add_line(resp, stime);
07187 if (add_audio) {
07188 add_line(resp, m_audio);
07189 add_line(resp, a_audio);
07190 add_line(resp, hold);
07191 } else if (p->offered_media[SDP_AUDIO].offered) {
07192 snprintf(dummy_answer, sizeof(dummy_answer), "m=audio 0 RTP/AVP %s\r\n", p->offered_media[SDP_AUDIO].text);
07193 add_line(resp, dummy_answer);
07194 }
07195 if (needvideo) {
07196 add_line(resp, m_video);
07197 add_line(resp, a_video);
07198 add_line(resp, hold);
07199 } else if (p->offered_media[SDP_VIDEO].offered) {
07200 snprintf(dummy_answer, sizeof(dummy_answer), "m=video 0 RTP/AVP %s\r\n", p->offered_media[SDP_VIDEO].text);
07201 add_line(resp, dummy_answer);
07202 }
07203 if (add_t38) {
07204 add_line(resp, m_modem);
07205 add_line(resp, a_modem);
07206 } else if (p->offered_media[SDP_IMAGE].offered) {
07207 add_line(resp, "m=image 0 udptl t38\r\n");
07208 }
07209
07210
07211 p->lastrtprx = p->lastrtptx = time(NULL);
07212
07213 if (option_debug > 2) {
07214 char buf[SIPBUFSIZE];
07215 ast_log(LOG_DEBUG, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
07216 }
07217
07218 return AST_SUCCESS;
07219 }
07220
07221
07222 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
07223 {
07224 struct sip_request resp;
07225 int seqno;
07226
07227 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
07228 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
07229 return -1;
07230 }
07231 respprep(&resp, p, msg, req);
07232 if (p->udptl) {
07233 add_sdp(&resp, p, 0, 1);
07234 } else
07235 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
07236 if (retrans && !p->pendinginvite)
07237 p->pendinginvite = seqno;
07238 return send_response(p, &resp, retrans, seqno);
07239 }
07240
07241
07242 static void copy_request(struct sip_request *dst, const struct sip_request *src)
07243 {
07244 long offset;
07245 int x;
07246 offset = ((void *)dst) - ((void *)src);
07247
07248 memcpy(dst, src, sizeof(*dst));
07249
07250 for (x=0; x < src->headers; x++)
07251 dst->header[x] += offset;
07252 for (x=0; x < src->lines; x++)
07253 dst->line[x] += offset;
07254 dst->rlPart1 += offset;
07255 dst->rlPart2 += offset;
07256 }
07257
07258
07259
07260
07261 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
07262 {
07263 struct sip_request resp;
07264 int seqno;
07265 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
07266 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
07267 return -1;
07268 }
07269 respprep(&resp, p, msg, req);
07270 if (p->rtp) {
07271 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
07272 if (option_debug)
07273 ast_log(LOG_DEBUG, "Setting framing from config on incoming call\n");
07274 ast_rtp_codec_setpref(p->rtp, &p->prefs);
07275 }
07276 try_suggested_sip_codec(p);
07277 if (p->t38.state == T38_PEER_DIRECT || p->t38.state == T38_ENABLED) {
07278 p->t38.state = T38_ENABLED;
07279 add_sdp(&resp, p, 1, 1);
07280 } else {
07281 add_sdp(&resp, p, 1, 0);
07282 }
07283 } else
07284 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
07285 if (reliable && !p->pendinginvite)
07286 p->pendinginvite = seqno;
07287 return send_response(p, &resp, reliable, seqno);
07288 }
07289
07290
07291 static int determine_firstline_parts(struct sip_request *req)
07292 {
07293 char *e = ast_skip_blanks(req->header[0]);
07294
07295 if (!*e)
07296 return -1;
07297 req->rlPart1 = e;
07298 e = ast_skip_nonblanks(e);
07299 if (*e)
07300 *e++ = '\0';
07301
07302 e = ast_skip_blanks(e);
07303 if ( !*e )
07304 return -1;
07305 ast_trim_blanks(e);
07306
07307 if (!strcasecmp(req->rlPart1, "SIP/2.0") ) {
07308 if (strlen(e) < 3)
07309 return -1;
07310 req->rlPart2 = e;
07311 } else {
07312 if ( *e == '<' ) {
07313 ast_log(LOG_WARNING, "bogus uri in <> %s\n", e);
07314 e++;
07315 if (!*e)
07316 return -1;
07317 }
07318 req->rlPart2 = e;
07319 e = ast_skip_nonblanks(e);
07320 if (*e)
07321 *e++ = '\0';
07322 e = ast_skip_blanks(e);
07323 if (strcasecmp(e, "SIP/2.0") ) {
07324 ast_log(LOG_WARNING, "Bad request protocol %s\n", e);
07325 return -1;
07326 }
07327 }
07328 return 1;
07329 }
07330
07331
07332
07333
07334
07335
07336
07337 static int transmit_reinvite_with_sdp(struct sip_pvt *p)
07338 {
07339 struct sip_request req;
07340
07341 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
07342
07343 add_header(&req, "Allow", ALLOWED_METHODS);
07344 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07345 if (sipdebug)
07346 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
07347 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
07348 append_history(p, "ReInv", "Re-invite sent");
07349 memset(p->offered_media, 0, sizeof(p->offered_media));
07350 add_sdp(&req, p, 1, 0);
07351
07352 initialize_initreq(p, &req);
07353 p->lastinvite = p->ocseq;
07354 ast_set_flag(&p->flags[0], SIP_OUTGOING);
07355 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
07356 }
07357
07358
07359
07360
07361
07362 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p)
07363 {
07364 struct sip_request req;
07365
07366 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
07367
07368 add_header(&req, "Allow", ALLOWED_METHODS);
07369 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07370 if (sipdebug)
07371 add_header(&req, "X-asterisk-info", "SIP re-invite (T38 switchover)");
07372 memset(p->offered_media, 0, sizeof(p->offered_media));
07373 add_sdp(&req, p, 0, 1);
07374
07375
07376 initialize_initreq(p, &req);
07377 ast_set_flag(&p->flags[0], SIP_OUTGOING);
07378 p->lastinvite = p->ocseq;
07379 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
07380 }
07381
07382
07383 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
07384 {
07385 char stripped[SIPBUFSIZE];
07386 char *c;
07387
07388 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
07389 c = get_in_brackets(stripped);
07390 c = strsep(&c, ";");
07391 if (!ast_strlen_zero(c))
07392 ast_string_field_set(p, uri, c);
07393 }
07394
07395
07396 static void build_contact(struct sip_pvt *p)
07397 {
07398
07399 if (ourport != STANDARD_SIP_PORT)
07400 ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip), ourport);
07401 else
07402 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip));
07403 }
07404
07405
07406 static void build_rpid(struct sip_pvt *p)
07407 {
07408 int send_pres_tags = TRUE;
07409 const char *privacy=NULL;
07410 const char *screen=NULL;
07411 char buf[256];
07412 const char *clid = default_callerid;
07413 const char *clin = NULL;
07414 const char *fromdomain;
07415
07416 if (!ast_strlen_zero(p->rpid) || !ast_strlen_zero(p->rpid_from))
07417 return;
07418
07419 if (p->owner && !ast_strlen_zero(p->owner->cid.cid_num))
07420 clid = p->owner->cid.cid_num;
07421 if (p->owner && p->owner->cid.cid_name)
07422 clin = p->owner->cid.cid_name;
07423 if (ast_strlen_zero(clin))
07424 clin = clid;
07425
07426 switch (p->callingpres) {
07427 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
07428 privacy = "off";
07429 screen = "no";
07430 break;
07431 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
07432 privacy = "off";
07433 screen = "yes";
07434 break;
07435 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
07436 privacy = "off";
07437 screen = "no";
07438 break;
07439 case AST_PRES_ALLOWED_NETWORK_NUMBER:
07440 privacy = "off";
07441 screen = "yes";
07442 break;
07443 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
07444 privacy = "full";
07445 screen = "no";
07446 break;
07447 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
07448 privacy = "full";
07449 screen = "yes";
07450 break;
07451 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
07452 privacy = "full";
07453 screen = "no";
07454 break;
07455 case AST_PRES_PROHIB_NETWORK_NUMBER:
07456 privacy = "full";
07457 screen = "yes";
07458 break;
07459 case AST_PRES_NUMBER_NOT_AVAILABLE:
07460 send_pres_tags = FALSE;
07461 break;
07462 default:
07463 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
07464 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
07465 privacy = "full";
07466 else
07467 privacy = "off";
07468 screen = "no";
07469 break;
07470 }
07471
07472 fromdomain = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip));
07473
07474 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
07475 if (send_pres_tags)
07476 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
07477 ast_string_field_set(p, rpid, buf);
07478
07479 ast_string_field_build(p, rpid_from, "\"%s\" <sip:%s@%s>;tag=%s", clin,
07480 S_OR(p->fromuser, clid),
07481 fromdomain, p->tag);
07482 }
07483
07484
07485 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
07486 {
07487 char invite_buf[256] = "";
07488 char *invite = invite_buf;
07489 size_t invite_max = sizeof(invite_buf);
07490 char from[256];
07491 char to[256];
07492 char tmp[SIPBUFSIZE/2];
07493 char tmp2[SIPBUFSIZE/2];
07494 const char *l = NULL, *n = NULL, *d = NULL;
07495 const char *urioptions = "";
07496
07497 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
07498 const char *s = p->username;
07499
07500
07501
07502
07503
07504
07505 if (*s == '+')
07506 s++;
07507 for (; *s; s++) {
07508 if (!strchr(AST_DIGIT_ANYNUM, *s) )
07509 break;
07510 }
07511
07512 if (!*s)
07513 urioptions = ";user=phone";
07514 }
07515
07516
07517 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
07518
07519 d = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip));
07520 if (p->owner) {
07521 l = p->owner->cid.cid_num;
07522 n = p->owner->cid.cid_name;
07523 }
07524
07525 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID) &&
07526 ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
07527 l = CALLERID_UNKNOWN;
07528 n = l;
07529 d = FROMDOMAIN_INVALID;
07530 }
07531 if (ast_strlen_zero(l))
07532 l = default_callerid;
07533 if (ast_strlen_zero(n))
07534 n = l;
07535
07536 if (!ast_strlen_zero(p->fromuser))
07537 l = p->fromuser;
07538 else
07539 ast_string_field_set(p, fromuser, l);
07540
07541
07542 if (!ast_strlen_zero(p->fromname))
07543 n = p->fromname;
07544 else
07545 ast_string_field_set(p, fromname, n);
07546
07547 if (pedanticsipchecking) {
07548 ast_uri_encode(n, tmp, sizeof(tmp), 0);
07549 n = tmp;
07550 ast_uri_encode(l, tmp2, sizeof(tmp2), 0);
07551 l = tmp2;
07552 }
07553
07554 if (ourport != STANDARD_SIP_PORT && ast_strlen_zero(p->fromdomain))
07555 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, d, ourport, p->tag);
07556 else
07557 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, d, p->tag);
07558
07559
07560 if (!ast_strlen_zero(p->fullcontact)) {
07561
07562 ast_build_string(&invite, &invite_max, "%s", p->fullcontact);
07563 } else {
07564
07565 ast_build_string(&invite, &invite_max, "sip:");
07566 if (!ast_strlen_zero(p->username)) {
07567 n = p->username;
07568 if (pedanticsipchecking) {
07569 ast_uri_encode(n, tmp, sizeof(tmp), 0);
07570 n = tmp;
07571 }
07572 ast_build_string(&invite, &invite_max, "%s@", n);
07573 }
07574 ast_build_string(&invite, &invite_max, "%s", p->tohost);
07575 if (p->portinuri)
07576 ast_build_string(&invite, &invite_max, ":%d", ntohs(p->sa.sin_port));
07577 ast_build_string(&invite, &invite_max, "%s", urioptions);
07578 }
07579
07580
07581 if (p->options && !ast_strlen_zero(p->options->uri_options))
07582 ast_build_string(&invite, &invite_max, ";%s", p->options->uri_options);
07583
07584 ast_string_field_set(p, uri, invite_buf);
07585
07586 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
07587
07588 snprintf(to, sizeof(to), "<%s%s>;tag=%s", (!strncasecmp(p->uri, "sip:", 4) ? "" : "sip:"), p->uri, p->theirtag);
07589 } else if (p->options && p->options->vxml_url) {
07590
07591 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
07592 } else
07593 snprintf(to, sizeof(to), "<%s>", p->uri);
07594
07595 init_req(req, sipmethod, p->uri);
07596 snprintf(tmp, sizeof(tmp), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
07597
07598 add_header(req, "Via", p->via);
07599
07600
07601
07602
07603 add_route(req, p->route);
07604
07605 if (ast_test_flag(&p->flags[0], SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
07606 build_rpid(p);
07607 add_header(req, "From", p->rpid_from);
07608 } else
07609 add_header(req, "From", from);
07610 add_header(req, "To", to);
07611 ast_string_field_set(p, exten, l);
07612 build_contact(p);
07613 add_header(req, "Contact", p->our_contact);
07614 add_header(req, "Call-ID", p->callid);
07615 add_header(req, "CSeq", tmp);
07616 if (!ast_strlen_zero(global_useragent))
07617 add_header(req, "User-Agent", global_useragent);
07618 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07619 if (!ast_strlen_zero(p->rpid))
07620 add_header(req, "Remote-Party-ID", p->rpid);
07621 }
07622
07623
07624 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
07625 {
07626 struct sip_request req;
07627
07628 req.method = sipmethod;
07629 if (init) {
07630
07631 p->branch ^= ast_random();
07632 p->invite_branch = p->branch;
07633 build_via(p);
07634 if (init > 1)
07635 initreqprep(&req, p, sipmethod);
07636 else
07637 reqprep(&req, p, sipmethod, 0, 0);
07638 } else
07639 reqprep(&req, p, sipmethod, 0, 1);
07640
07641 if (p->options && p->options->auth)
07642 add_header(&req, p->options->authheader, p->options->auth);
07643 append_date(&req);
07644 if (sipmethod == SIP_REFER) {
07645 if (p->refer) {
07646 char buf[SIPBUFSIZE];
07647 if (!ast_strlen_zero(p->refer->refer_to))
07648 add_header(&req, "Refer-To", p->refer->refer_to);
07649 if (!ast_strlen_zero(p->refer->referred_by)) {
07650 snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
07651 add_header(&req, "Referred-By", buf);
07652 }
07653 }
07654 }
07655
07656
07657 if (p->options && p->options->replaces && !ast_strlen_zero(p->options->replaces)) {
07658 add_header(&req, "Replaces", p->options->replaces);
07659 add_header(&req, "Require", "replaces");
07660 }
07661
07662 add_header(&req, "Allow", ALLOWED_METHODS);
07663 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07664 if (p->options && p->options->addsipheaders && p->owner) {
07665 struct ast_channel *chan = p->owner;
07666 struct varshead *headp;
07667
07668 ast_channel_lock(chan);
07669
07670 headp = &chan->varshead;
07671
07672 if (!headp)
07673 ast_log(LOG_WARNING,"No Headp for the channel...ooops!\n");
07674 else {
07675 const struct ast_var_t *current;
07676 AST_LIST_TRAVERSE(headp, current, entries) {
07677
07678 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
07679 char *content, *end;
07680 const char *header = ast_var_value(current);
07681 char *headdup = ast_strdupa(header);
07682
07683
07684 if (*headdup == '"')
07685 headdup++;
07686 if ((content = strchr(headdup, ':'))) {
07687 *content++ = '\0';
07688 content = ast_skip_blanks(content);
07689
07690 end = content + strlen(content) -1;
07691 if (*end == '"')
07692 *end = '\0';
07693
07694 add_header(&req, headdup, content);
07695 if (sipdebug)
07696 ast_log(LOG_DEBUG, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
07697 }
07698 }
07699 }
07700 }
07701
07702 ast_channel_unlock(chan);
07703 }
07704 if (sdp) {
07705 memset(p->offered_media, 0, sizeof(p->offered_media));
07706 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
07707 ast_udptl_offered_from_local(p->udptl, 1);
07708 if (option_debug)
07709 ast_log(LOG_DEBUG, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
07710 add_sdp(&req, p, 0, 1);
07711 } else if (p->rtp)
07712 add_sdp(&req, p, 1, 0);
07713 } else {
07714 add_header_contentLength(&req, 0);
07715 }
07716
07717 if (!p->initreq.headers || init > 2)
07718 initialize_initreq(p, &req);
07719 p->lastinvite = p->ocseq;
07720 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
07721 }
07722
07723
07724 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
07725 {
07726 char tmp[4000], from[256], to[256];
07727 char *t = tmp, *c, *mfrom, *mto;
07728 size_t maxbytes = sizeof(tmp);
07729 struct sip_request req;
07730 char hint[AST_MAX_EXTENSION];
07731 char *statestring = "terminated";
07732 const struct cfsubscription_types *subscriptiontype;
07733 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
07734 char *pidfstate = "--";
07735 char *pidfnote= "Ready";
07736
07737 memset(from, 0, sizeof(from));
07738 memset(to, 0, sizeof(to));
07739 memset(tmp, 0, sizeof(tmp));
07740
07741 switch (state) {
07742 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
07743 statestring = (global_notifyringing) ? "early" : "confirmed";
07744 local_state = NOTIFY_INUSE;
07745 pidfstate = "busy";
07746 pidfnote = "Ringing";
07747 break;
07748 case AST_EXTENSION_RINGING:
07749 statestring = "early";
07750 local_state = NOTIFY_INUSE;
07751 pidfstate = "busy";
07752 pidfnote = "Ringing";
07753 break;
07754 case AST_EXTENSION_INUSE:
07755 statestring = "confirmed";
07756 local_state = NOTIFY_INUSE;
07757 pidfstate = "busy";
07758 pidfnote = "On the phone";
07759 break;
07760 case AST_EXTENSION_BUSY:
07761 statestring = "confirmed";
07762 local_state = NOTIFY_CLOSED;
07763 pidfstate = "busy";
07764 pidfnote = "On the phone";
07765 break;
07766 case AST_EXTENSION_UNAVAILABLE:
07767 statestring = "terminated";
07768 local_state = NOTIFY_CLOSED;
07769 pidfstate = "away";
07770 pidfnote = "Unavailable";
07771 break;
07772 case AST_EXTENSION_ONHOLD:
07773 statestring = "confirmed";
07774 local_state = NOTIFY_CLOSED;
07775 pidfstate = "busy";
07776 pidfnote = "On Hold";
07777 break;
07778 case AST_EXTENSION_NOT_INUSE:
07779 default:
07780
07781 break;
07782 }
07783
07784 subscriptiontype = find_subscription_type(p->subscribed);
07785
07786
07787 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
07788 char *hint2 = hint, *individual_hint = NULL;
07789 int hint_count = 0, unavailable_count = 0;
07790
07791 while ((individual_hint = strsep(&hint2, "&"))) {
07792 hint_count++;
07793
07794 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE)
07795 unavailable_count++;
07796 }
07797
07798
07799
07800
07801 if (hint_count > 0 && hint_count == unavailable_count) {
07802 local_state = NOTIFY_CLOSED;
07803 pidfstate = "away";
07804 pidfnote = "Not online";
07805 }
07806 }
07807
07808 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
07809 c = get_in_brackets(from);
07810 if (strncasecmp(c, "sip:", 4)) {
07811 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
07812 return -1;
07813 }
07814 mfrom = strsep(&c, ";");
07815
07816 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
07817 c = get_in_brackets(to);
07818 if (strncasecmp(c, "sip:", 4)) {
07819 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
07820 return -1;
07821 }
07822 mto = strsep(&c, ";");
07823
07824 reqprep(&req, p, SIP_NOTIFY, 0, 1);
07825
07826
07827 add_header(&req, "Event", subscriptiontype->event);
07828 add_header(&req, "Content-Type", subscriptiontype->mediatype);
07829 switch(state) {
07830 case AST_EXTENSION_DEACTIVATED:
07831 if (timeout)
07832 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07833 else {
07834 add_header(&req, "Subscription-State", "terminated;reason=probation");
07835 add_header(&req, "Retry-After", "60");
07836 }
07837 break;
07838 case AST_EXTENSION_REMOVED:
07839 add_header(&req, "Subscription-State", "terminated;reason=noresource");
07840 break;
07841 default:
07842 if (p->expiry)
07843 add_header(&req, "Subscription-State", "active");
07844 else
07845 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07846 }
07847 switch (p->subscribed) {
07848 case XPIDF_XML:
07849 case CPIM_PIDF_XML:
07850 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
07851 ast_build_string(&t, &maxbytes, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n");
07852 ast_build_string(&t, &maxbytes, "<presence>\n");
07853 ast_build_string(&t, &maxbytes, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
07854 ast_build_string(&t, &maxbytes, "<atom id=\"%s\">\n", p->exten);
07855 ast_build_string(&t, &maxbytes, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
07856 ast_build_string(&t, &maxbytes, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
07857 ast_build_string(&t, &maxbytes, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
07858 ast_build_string(&t, &maxbytes, "</address>\n</atom>\n</presence>\n");
07859 break;
07860 case PIDF_XML:
07861 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
07862 ast_build_string(&t, &maxbytes, "<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);
07863 ast_build_string(&t, &maxbytes, "<pp:person><status>\n");
07864 if (pidfstate[0] != '-')
07865 ast_build_string(&t, &maxbytes, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
07866 ast_build_string(&t, &maxbytes, "</status></pp:person>\n");
07867 ast_build_string(&t, &maxbytes, "<note>%s</note>\n", pidfnote);
07868 ast_build_string(&t, &maxbytes, "<tuple id=\"%s\">\n", p->exten);
07869 ast_build_string(&t, &maxbytes, "<contact priority=\"1\">%s</contact>\n", mto);
07870 if (pidfstate[0] == 'b')
07871 ast_build_string(&t, &maxbytes, "<status><basic>open</basic></status>\n");
07872 else
07873 ast_build_string(&t, &maxbytes, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
07874 ast_build_string(&t, &maxbytes, "</tuple>\n</presence>\n");
07875 break;
07876 case DIALOG_INFO_XML:
07877 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
07878 ast_build_string(&t, &maxbytes, "<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" version=\"%d\" state=\"%s\" entity=\"%s\">\n", p->dialogver++, full ? "full":"partial", mto);
07879 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
07880 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
07881 else
07882 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\">\n", p->exten);
07883 ast_build_string(&t, &maxbytes, "<state>%s</state>\n", statestring);
07884 if (state == AST_EXTENSION_ONHOLD) {
07885 ast_build_string(&t, &maxbytes, "<local>\n<target uri=\"%s\">\n"
07886 "<param pname=\"+sip.rendering\" pvalue=\"no\"/>\n"
07887 "</target>\n</local>\n", mto);
07888 }
07889 ast_build_string(&t, &maxbytes, "</dialog>\n</dialog-info>\n");
07890 break;
07891 case NONE:
07892 default:
07893 break;
07894 }
07895
07896 if (t > tmp + sizeof(tmp))
07897 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07898
07899 add_header_contentLength(&req, strlen(tmp));
07900 add_line(&req, tmp);
07901 p->pendinginvite = p->ocseq;
07902
07903 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07904 }
07905
07906
07907
07908
07909
07910
07911
07912 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
07913 {
07914 struct sip_request req;
07915 char tmp[500];
07916 char *t = tmp;
07917 size_t maxbytes = sizeof(tmp);
07918
07919 initreqprep(&req, p, SIP_NOTIFY);
07920 add_header(&req, "Event", "message-summary");
07921 add_header(&req, "Content-Type", default_notifymime);
07922
07923 ast_build_string(&t, &maxbytes, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
07924
07925
07926
07927 if ((ourport != STANDARD_SIP_PORT) && ast_strlen_zero(p->fromdomain)) {
07928 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s:%d\r\n",
07929 S_OR(vmexten, default_vmexten), ast_inet_ntoa(p->ourip), ourport);
07930 } else {
07931 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n",
07932 S_OR(vmexten, default_vmexten), S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)));
07933 }
07934
07935
07936
07937 ast_build_string(&t, &maxbytes, "Voice-Message: %d/%d%s\r\n", newmsgs, oldmsgs, (ast_test_flag(&p->flags[1], SIP_PAGE2_BUGGY_MWI) ? "" : " (0/0)"));
07938
07939 if (p->subscribed) {
07940 if (p->expiry)
07941 add_header(&req, "Subscription-State", "active");
07942 else
07943 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07944 }
07945
07946 if (t > tmp + sizeof(tmp))
07947 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07948
07949 add_header_contentLength(&req, strlen(tmp));
07950 add_line(&req, tmp);
07951
07952 if (!p->initreq.headers)
07953 initialize_initreq(p, &req);
07954 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07955 }
07956
07957
07958 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req)
07959 {
07960 if (!p->initreq.headers)
07961 initialize_initreq(p, req);
07962 return send_request(p, req, XMIT_UNRELIABLE, p->ocseq);
07963 }
07964
07965
07966 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
07967 {
07968 struct sip_request req;
07969 char tmp[SIPBUFSIZE/2];
07970
07971 reqprep(&req, p, SIP_NOTIFY, 0, 1);
07972 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
07973 add_header(&req, "Event", tmp);
07974 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
07975 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
07976 add_header(&req, "Allow", ALLOWED_METHODS);
07977 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07978
07979 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
07980 add_header_contentLength(&req, strlen(tmp));
07981 add_line(&req, tmp);
07982
07983 if (!p->initreq.headers)
07984 initialize_initreq(p, &req);
07985
07986 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07987 }
07988
07989
07990 static char *regstate2str(enum sipregistrystate regstate)
07991 {
07992 switch(regstate) {
07993 case REG_STATE_FAILED:
07994 return "Failed";
07995 case REG_STATE_UNREGISTERED:
07996 return "Unregistered";
07997 case REG_STATE_REGSENT:
07998 return "Request Sent";
07999 case REG_STATE_AUTHSENT:
08000 return "Auth. Sent";
08001 case REG_STATE_REGISTERED:
08002 return "Registered";
08003 case REG_STATE_REJECTED:
08004 return "Rejected";
08005 case REG_STATE_TIMEOUT:
08006 return "Timeout";
08007 case REG_STATE_NOAUTH:
08008 return "No Authentication";
08009 default:
08010 return "Unknown";
08011 }
08012 }
08013
08014
08015 static int sip_reregister(const void *data)
08016 {
08017
08018 struct sip_registry *r= ASTOBJ_REF((struct sip_registry *) data);
08019
08020
08021 if (!r)
08022 return 0;
08023
08024 if (r->call && !ast_test_flag(&r->call->flags[0], SIP_NO_HISTORY))
08025 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
08026
08027
08028 if (sipdebug)
08029 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
08030
08031 r->expire = -1;
08032 __sip_do_register(r);
08033 ASTOBJ_UNREF(r, sip_registry_destroy);
08034 return 0;
08035 }
08036
08037
08038 static int __sip_do_register(struct sip_registry *r)
08039 {
08040 int res;
08041
08042 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
08043 return res;
08044 }
08045
08046
08047 static int sip_reg_timeout(const void *data)
08048 {
08049
08050
08051 struct sip_registry *r = ASTOBJ_REF((struct sip_registry *) data);
08052 struct sip_pvt *p;
08053 int res;
08054
08055
08056 if (!r)
08057 return 0;
08058
08059 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
08060 if (r->call) {
08061
08062
08063 p = r->call;
08064 ast_mutex_lock(&p->lock);
08065 if (p->registry)
08066 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
08067 r->call = NULL;
08068 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
08069
08070 __sip_pretend_ack(p);
08071 ast_mutex_unlock(&p->lock);
08072 }
08073
08074 if (global_regattempts_max && (r->regattempts > global_regattempts_max)) {
08075
08076
08077
08078 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
08079 r->regstate = REG_STATE_FAILED;
08080 } else {
08081 r->regstate = REG_STATE_UNREGISTERED;
08082 r->timeout = -1;
08083 r->needdns = TRUE;
08084 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
08085 }
08086 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: SIP\r\nUsername: %s\r\nDomain: %s\r\nStatus: %s\r\n", r->username, r->hostname, regstate2str(r->regstate));
08087 ASTOBJ_UNREF(r, sip_registry_destroy);
08088 return 0;
08089 }
08090
08091
08092 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
08093 {
08094 struct sip_request req;
08095 char from[256];
08096 char to[256];
08097 char tmp[80];
08098 char addr[80];
08099 struct sip_pvt *p;
08100 char *fromdomain;
08101
08102
08103 if ( r == NULL || ((auth==NULL) && (r->regstate==REG_STATE_REGSENT || r->regstate==REG_STATE_AUTHSENT))) {
08104 if (r) {
08105 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
08106 }
08107 return 0;
08108 }
08109
08110 if (r->call) {
08111 if (!auth) {
08112 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
08113 return 0;
08114 } else {
08115 p = r->call;
08116 make_our_tag(p->tag, sizeof(p->tag));
08117 ast_string_field_free(p, theirtag);
08118 }
08119 } else {
08120
08121 if (!r->callid_valid) {
08122 build_callid_registry(r, __ourip, default_fromdomain);
08123 r->callid_valid = TRUE;
08124 }
08125
08126 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER))) {
08127 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
08128 return 0;
08129 }
08130 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
08131 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
08132
08133
08134 if (create_addr(p, r->hostname, r->needdns ? NULL : &r->us)) {
08135
08136
08137 sip_destroy(p);
08138
08139 if (r->timeout > -1)
08140 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
08141 else
08142 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);
08143
08144 AST_SCHED_DEL(sched, r->timeout);
08145 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
08146 r->regattempts++;
08147 return 0;
08148 }
08149 if (r->needdns) {
08150 memcpy(&r->us, &p->sa, sizeof(r->us));
08151 }
08152 r->needdns = FALSE;
08153
08154 ast_string_field_set(r, callid, p->callid);
08155 if (r->portno) {
08156 p->sa.sin_port = htons(r->portno);
08157 p->recv.sin_port = htons(r->portno);
08158 } else
08159 r->portno = ntohs(p->sa.sin_port);
08160 ast_set_flag(&p->flags[0], SIP_OUTGOING);
08161 r->call=p;
08162 p->registry = ASTOBJ_REF(r);
08163 if (!ast_strlen_zero(r->secret))
08164 ast_string_field_set(p, peersecret, r->secret);
08165 if (!ast_strlen_zero(r->md5secret))
08166 ast_string_field_set(p, peermd5secret, r->md5secret);
08167
08168
08169 if (!ast_strlen_zero(r->authuser)) {
08170 ast_string_field_set(p, peername, r->authuser);
08171 ast_string_field_set(p, authname, r->authuser);
08172 } else if (!ast_strlen_zero(r->username)) {
08173 ast_string_field_set(p, peername, r->username);
08174 ast_string_field_set(p, authname, r->username);
08175 ast_string_field_set(p, fromuser, r->username);
08176 }
08177 if (!ast_strlen_zero(r->username))
08178 ast_string_field_set(p, username, r->username);
08179
08180 ast_string_field_set(p, exten, r->contact);
08181
08182
08183
08184
08185
08186
08187 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
08188 p->ourip = bindaddr.sin_addr;
08189 build_contact(p);
08190 }
08191
08192
08193 if (auth == NULL) {
08194 if (r->timeout > -1)
08195 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
08196 AST_SCHED_DEL(sched, r->timeout);
08197 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
08198 if (option_debug)
08199 ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
08200 }
08201
08202 if ((fromdomain = strchr(r->username, '@'))) {
08203
08204 fromdomain++ ;
08205
08206 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
08207 if (!ast_strlen_zero(p->theirtag))
08208 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
08209 else
08210 snprintf(to, sizeof(to), "<sip:%s>", r->username);
08211
08212
08213
08214 if (ast_strlen_zero(p->fromdomain)) {
08215 ast_string_field_set(p, fromdomain, fromdomain);
08216 }
08217 } else {
08218 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
08219 if (!ast_strlen_zero(p->theirtag))
08220 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
08221 else
08222 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
08223 }
08224
08225
08226
08227 if (!ast_strlen_zero(p->fromdomain)) {
08228 if (r->portno && r->portno != STANDARD_SIP_PORT)
08229 snprintf(addr, sizeof(addr), "sip:%s:%d", p->fromdomain, r->portno);
08230 else
08231 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
08232 } else {
08233 if (r->portno && r->portno != STANDARD_SIP_PORT)
08234 snprintf(addr, sizeof(addr), "sip:%s:%d", r->hostname, r->portno);
08235 else
08236 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
08237 }
08238 ast_string_field_set(p, uri, addr);
08239
08240 p->branch ^= ast_random();
08241
08242 init_req(&req, sipmethod, addr);
08243
08244
08245 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
08246 p->ocseq = r->ocseq;
08247
08248 build_via(p);
08249 add_header(&req, "Via", p->via);
08250 add_header(&req, "From", from);
08251 add_header(&req, "To", to);
08252 add_header(&req, "Call-ID", p->callid);
08253 add_header(&req, "CSeq", tmp);
08254 if (!ast_strlen_zero(global_useragent))
08255 add_header(&req, "User-Agent", global_useragent);
08256 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
08257
08258
08259 if (auth)
08260 add_header(&req, authheader, auth);
08261 else if (!ast_strlen_zero(r->nonce)) {
08262 char digest[1024];
08263
08264
08265 if (sipdebug)
08266 ast_log(LOG_DEBUG, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
08267 ast_string_field_set(p, realm, r->realm);
08268 ast_string_field_set(p, nonce, r->nonce);
08269 ast_string_field_set(p, domain, r->domain);
08270 ast_string_field_set(p, opaque, r->opaque);
08271 ast_string_field_set(p, qop, r->qop);
08272 r->noncecount++;
08273 p->noncecount = r->noncecount;
08274
08275 memset(digest,0,sizeof(digest));
08276 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
08277 add_header(&req, "Authorization", digest);
08278 else
08279 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
08280
08281 }
08282
08283 snprintf(tmp, sizeof(tmp), "%d", default_expiry);
08284 add_header(&req, "Expires", tmp);
08285 add_header(&req, "Contact", p->our_contact);
08286 add_header(&req, "Event", "registration");
08287 add_header_contentLength(&req, 0);
08288
08289 initialize_initreq(p, &req);
08290 if (sip_debug_test_pvt(p))
08291 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
08292 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
08293 r->regattempts++;
08294 if (option_debug > 3)
08295 ast_verbose("REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
08296 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
08297 }
08298
08299
08300 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
08301 {
08302 struct sip_request req;
08303
08304 reqprep(&req, p, SIP_MESSAGE, 0, 1);
08305 add_text(&req, text);
08306 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08307 }
08308
08309
08310 static int sip_refer_allocate(struct sip_pvt *p)
08311 {
08312 p->refer = ast_calloc(1, sizeof(struct sip_refer));
08313 return p->refer ? 1 : 0;
08314 }
08315
08316
08317
08318
08319
08320
08321 static int transmit_refer(struct sip_pvt *p, const char *dest)
08322 {
08323 struct sip_request req = {
08324 .headers = 0,
08325 };
08326 char from[256];
08327 const char *of;
08328 char *c;
08329 char referto[256];
08330 char *ttag, *ftag;
08331 char *theirtag = ast_strdupa(p->theirtag);
08332
08333 if (option_debug || sipdebug)
08334 ast_log(LOG_DEBUG, "SIP transfer of %s to %s\n", p->callid, dest);
08335
08336
08337 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
08338 of = get_header(&p->initreq, "To");
08339 ttag = theirtag;
08340 ftag = p->tag;
08341 } else {
08342 of = get_header(&p->initreq, "From");
08343 ftag = theirtag;
08344 ttag = p->tag;
08345 }
08346
08347 ast_copy_string(from, of, sizeof(from));
08348 of = get_in_brackets(from);
08349 ast_string_field_set(p, from, of);
08350 if (strncasecmp(of, "sip:", 4))
08351 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
08352 else
08353 of += 4;
08354
08355 if ((c = strchr(dest, '@')))
08356 c = NULL;
08357 else if ((c = strchr(of, '@')))
08358 *c++ = '\0';
08359 if (c)
08360 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
08361 else
08362 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
08363
08364
08365 sip_refer_allocate(p);
08366 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
08367 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
08368 p->refer->status = REFER_SENT;
08369
08370 reqprep(&req, p, SIP_REFER, 0, 1);
08371
08372 add_header(&req, "Refer-To", referto);
08373 add_header(&req, "Allow", ALLOWED_METHODS);
08374 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
08375 if (!ast_strlen_zero(p->our_contact))
08376 add_header(&req, "Referred-By", p->our_contact);
08377
08378 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08379
08380
08381
08382
08383
08384
08385
08386
08387 }
08388
08389
08390
08391 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
08392 {
08393 struct sip_request req;
08394
08395 reqprep(&req, p, SIP_INFO, 0, 1);
08396 add_digit(&req, digit, duration);
08397 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08398 }
08399
08400
08401 static int transmit_info_with_vidupdate(struct sip_pvt *p)
08402 {
08403 struct sip_request req;
08404
08405 reqprep(&req, p, SIP_INFO, 0, 1);
08406 add_vidupdate(&req);
08407 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08408 }
08409
08410
08411
08412
08413 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
08414 {
08415 struct sip_request resp;
08416
08417 if (sipmethod == SIP_ACK)
08418 p->invitestate = INV_CONFIRMED;
08419
08420 reqprep(&resp, p, sipmethod, seqno, newbranch);
08421 add_header_contentLength(&resp, 0);
08422 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
08423 }
08424
08425
08426 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
08427 {
08428 struct sip_request resp;
08429
08430 reqprep(&resp, p, sipmethod, seqno, newbranch);
08431 if (!ast_strlen_zero(p->realm)) {
08432 char digest[1024];
08433
08434 memset(digest, 0, sizeof(digest));
08435 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
08436 if (p->options && p->options->auth_type == PROXY_AUTH)
08437 add_header(&resp, "Proxy-Authorization", digest);
08438 else if (p->options && p->options->auth_type == WWW_AUTH)
08439 add_header(&resp, "Authorization", digest);
08440 else
08441 add_header(&resp, "Proxy-Authorization", digest);
08442 } else
08443 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
08444 }
08445
08446
08447 if (sipmethod == SIP_BYE) {
08448 char buf[10];
08449
08450 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->hangupcause));
08451 snprintf(buf, sizeof(buf), "%d", p->hangupcause);
08452 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
08453 }
08454
08455 add_header_contentLength(&resp, 0);
08456 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
08457 }
08458
08459
08460 static void destroy_association(struct sip_peer *peer)
08461 {
08462 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE)) {
08463 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT) && ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE)) {
08464 ast_update_realtime("sippeers", "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "username", "", "regserver", "", NULL);
08465 ast_update_realtime("sippeers", "name", peer->name, "lastms", "", NULL);
08466 } else
08467 ast_db_del("SIP/Registry", peer->name);
08468 }
08469 }
08470
08471
08472 static int expire_register(const void *data)
08473 {
08474 struct sip_peer *peer = (struct sip_peer *)data;
08475
08476 if (!peer)
08477 return 0;
08478
08479 memset(&peer->addr, 0, sizeof(peer->addr));
08480
08481 destroy_association(peer);
08482
08483 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
08484 register_peer_exten(peer, FALSE);
08485 peer->expire = -1;
08486 peer->portinuri = 0;
08487 ast_device_state_changed("SIP/%s", peer->name);
08488
08489
08490
08491
08492 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT) ||
08493 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
08494 struct sip_peer *peer_ptr = peer_ptr;
08495 peer_ptr = ASTOBJ_CONTAINER_UNLINK(&peerl, peer);
08496 if (peer_ptr) {
08497 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08498 }
08499 }
08500
08501 ASTOBJ_UNREF(peer, sip_destroy_peer);
08502
08503 return 0;
08504 }
08505
08506
08507 static int sip_poke_peer_s(const void *data)
08508 {
08509 struct sip_peer *peer = (struct sip_peer *) data;
08510
08511 peer->pokeexpire = -1;
08512
08513 sip_poke_peer(peer);
08514
08515 ASTOBJ_UNREF(peer, sip_destroy_peer);
08516
08517 return 0;
08518 }
08519
08520
08521 static void reg_source_db(struct sip_peer *peer)
08522 {
08523 char data[256];
08524 struct in_addr in;
08525 int expiry;
08526 int port;
08527 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
08528
08529 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
08530 return;
08531 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
08532 return;
08533
08534 scan = data;
08535 addr = strsep(&scan, ":");
08536 port_str = strsep(&scan, ":");
08537 expiry_str = strsep(&scan, ":");
08538 username = strsep(&scan, ":");
08539 contact = scan;
08540
08541 if (!inet_aton(addr, &in))
08542 return;
08543
08544 if (port_str)
08545 port = atoi(port_str);
08546 else
08547 return;
08548
08549 if (expiry_str)
08550 expiry = atoi(expiry_str);
08551 else
08552 return;
08553
08554 if (username)
08555 ast_copy_string(peer->username, username, sizeof(peer->username));
08556 if (contact)
08557 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
08558
08559 if (option_debug > 1)
08560 ast_log(LOG_DEBUG, "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
08561 peer->name, peer->username, ast_inet_ntoa(in), port, expiry);
08562
08563 memset(&peer->addr, 0, sizeof(peer->addr));
08564 peer->addr.sin_family = AF_INET;
08565 peer->addr.sin_addr = in;
08566 peer->addr.sin_port = htons(port);
08567 if (sipsock < 0) {
08568
08569 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
08570 struct sip_peer *peer_ptr = peer;
08571 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08572 }
08573 peer->pokeexpire = ast_sched_add(sched, ast_random() % 5000 + 1, sip_poke_peer_s, ASTOBJ_REF(peer));
08574 if (peer->pokeexpire == -1) {
08575 struct sip_peer *peer_ptr = peer;
08576 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08577 }
08578 } else
08579 sip_poke_peer(peer);
08580 if (!AST_SCHED_DEL(sched, peer->expire)) {
08581 struct sip_peer *peer_ptr = peer;
08582 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08583 }
08584 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, ASTOBJ_REF(peer));
08585 if (peer->expire == -1) {
08586 struct sip_peer *peer_ptr = peer;
08587 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08588 }
08589 register_peer_exten(peer, TRUE);
08590 }
08591
08592
08593 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
08594 {
08595 char contact[SIPBUFSIZE];
08596 char *c;
08597
08598
08599 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
08600 c = get_in_brackets(contact);
08601
08602
08603 ast_string_field_set(pvt, fullcontact, c);
08604
08605
08606 ast_string_field_set(pvt, okcontacturi, c);
08607
08608
08609
08610 return TRUE;
08611 }
08612
08613 static int __set_address_from_contact(const char *fullcontact, struct sockaddr_in *sin)
08614 {
08615 struct hostent *hp;
08616 struct ast_hostent ahp;
08617 int port;
08618 char *c, *host, *pt;
08619 char contact_buf[256];
08620 char *contact;
08621
08622
08623 ast_copy_string(contact_buf, fullcontact, sizeof(contact_buf));
08624 contact = contact_buf;
08625
08626
08627 if (strncasecmp(contact, "sip:", 4)) {
08628 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
08629 } else
08630 contact += 4;
08631
08632
08633
08634
08635
08636 host = strchr(contact, '@');
08637 if (!host) {
08638 host = contact;
08639 c = NULL;
08640 } else {
08641 *host++ = '\0';
08642 }
08643 pt = strchr(host, ':');
08644 if (pt) {
08645 *pt++ = '\0';
08646 port = atoi(pt);
08647 } else
08648 port = STANDARD_SIP_PORT;
08649
08650 contact = strsep(&contact, ";");
08651 host = strsep(&host, ";");
08652
08653
08654
08655 hp = ast_gethostbyname(host, &ahp);
08656 if (!hp) {
08657 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't resolve in DNS) : '%s'\n", host);
08658 return -1;
08659 }
08660 sin->sin_family = AF_INET;
08661 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
08662 sin->sin_port = htons(port);
08663
08664 return 0;
08665 }
08666
08667
08668 static int set_address_from_contact(struct sip_pvt *pvt)
08669 {
08670 if (ast_test_flag(&pvt->flags[0], SIP_NAT_ROUTE)) {
08671
08672
08673 pvt->sa = pvt->recv;
08674 return 0;
08675 }
08676
08677 return __set_address_from_contact(pvt->fullcontact, &pvt->sa);
08678 }
08679
08680
08681
08682 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
08683 {
08684 char contact[SIPBUFSIZE];
08685 char data[SIPBUFSIZE];
08686 const char *expires = get_header(req, "Expires");
08687 int expiry = atoi(expires);
08688 char *curi, *n, *pt;
08689 int port;
08690 const char *useragent;
08691 struct hostent *hp;
08692 struct ast_hostent ahp;
08693 struct sockaddr_in oldsin, testsin;
08694
08695 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
08696
08697 if (ast_strlen_zero(expires)) {
08698 expires = strcasestr(contact, ";expires=");
08699 if (expires) {
08700
08701 expires = strsep((char **) &expires, ";");
08702 if (sscanf(expires + 9, "%30d", &expiry) != 1)
08703 expiry = default_expiry;
08704 } else {
08705
08706 expiry = default_expiry;
08707 }
08708 }
08709
08710
08711 curi = contact;
08712 if (strchr(contact, '<') == NULL)
08713 strsep(&curi, ";");
08714 curi = get_in_brackets(contact);
08715
08716
08717
08718
08719
08720 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
08721
08722 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact))
08723 pvt->expiry = ast_sched_when(sched, peer->expire);
08724 return PARSE_REGISTER_QUERY;
08725 } else if (!strcasecmp(curi, "*") || !expiry) {
08726
08727 memset(&peer->addr, 0, sizeof(peer->addr));
08728 if (!AST_SCHED_DEL(sched, peer->expire)) {
08729 struct sip_peer *peer_ptr = peer;
08730 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08731 }
08732
08733 destroy_association(peer);
08734
08735 register_peer_exten(peer, 0);
08736 peer->fullcontact[0] = '\0';
08737 peer->useragent[0] = '\0';
08738 peer->sipoptions = 0;
08739 peer->lastms = 0;
08740 peer->portinuri = 0;
08741 pvt->expiry = 0;
08742
08743 if (option_verbose > 2)
08744 ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", peer->name);
08745
08746 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\n", peer->name);
08747 return PARSE_REGISTER_UPDATE;
08748 }
08749
08750
08751 ast_copy_string(peer->fullcontact, curi, sizeof(peer->fullcontact));
08752
08753
08754 ast_string_field_build(pvt, our_contact, "<%s>", curi);
08755
08756
08757 if (strncasecmp(curi, "sip:", 4)) {
08758 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", curi);
08759 } else
08760 curi += 4;
08761
08762 curi = strsep(&curi, ";");
08763
08764 n = strchr(curi, '@');
08765 if (!n) {
08766 n = curi;
08767 curi = NULL;
08768 } else
08769 *n++ = '\0';
08770 pt = strchr(n, ':');
08771 if (pt) {
08772 *pt++ = '\0';
08773 port = atoi(pt);
08774 peer->portinuri = 1;
08775 } else {
08776 port = STANDARD_SIP_PORT;
08777 peer->portinuri = 0;
08778 }
08779 oldsin = peer->addr;
08780
08781
08782
08783 hp = ast_gethostbyname(n, &ahp);
08784 if (!hp) {
08785 ast_log(LOG_WARNING, "Invalid host '%s'\n", n);
08786 *peer->fullcontact = '\0';
08787 ast_string_field_set(pvt, our_contact, "");
08788 return PARSE_REGISTER_FAILED;
08789 }
08790 memcpy(&testsin.sin_addr, hp->h_addr, sizeof(testsin.sin_addr));
08791 if ( ast_apply_ha(global_contact_ha, &testsin) != AST_SENSE_ALLOW ||
08792 ast_apply_ha(peer->contactha, &testsin) != AST_SENSE_ALLOW) {
08793 ast_log(LOG_WARNING, "Host '%s' disallowed by contact ACL (violating IP %s)\n", n, ast_inet_ntoa(testsin.sin_addr));
08794 *peer->fullcontact = '\0';
08795 ast_string_field_set(pvt, our_contact, "");
08796 return PARSE_REGISTER_DENIED;
08797 }
08798
08799 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
08800 peer->addr.sin_family = AF_INET;
08801 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
08802 peer->addr.sin_port = htons(port);
08803 } else {
08804
08805
08806 peer->addr = pvt->recv;
08807 }
08808
08809
08810 peer->sipoptions = pvt->sipoptions;
08811
08812 if (curi && ast_strlen_zero(peer->username))
08813 ast_copy_string(peer->username, curi, sizeof(peer->username));
08814
08815 if (!AST_SCHED_DEL(sched, peer->expire)) {
08816 struct sip_peer *peer_ptr = peer;
08817 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08818 }
08819 if (expiry > max_expiry)
08820 expiry = max_expiry;
08821 if (expiry < min_expiry)
08822 expiry = min_expiry;
08823 if (ast_test_flag(&peer->flags[0], SIP_REALTIME) && !ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
08824 peer->expire = -1;
08825 } else {
08826 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, ASTOBJ_REF(peer));
08827 if (peer->expire == -1) {
08828 struct sip_peer *peer_ptr = peer;
08829 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08830 }
08831 }
08832 pvt->expiry = expiry;
08833 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);
08834 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
08835 ast_db_put("SIP/Registry", peer->name, data);
08836 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
08837
08838
08839 if (option_verbose > 2 && inaddrcmp(&peer->addr, &oldsin)) {
08840 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));
08841 }
08842 sip_poke_peer(peer);
08843 register_peer_exten(peer, 1);
08844
08845
08846 useragent = get_header(req, "User-Agent");
08847 if (strcasecmp(useragent, peer->useragent)) {
08848 ast_copy_string(peer->useragent, useragent, sizeof(peer->useragent));
08849 if (option_verbose > 3)
08850 ast_verbose(VERBOSE_PREFIX_3 "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
08851 }
08852 return PARSE_REGISTER_UPDATE;
08853 }
08854
08855
08856 static void free_old_route(struct sip_route *route)
08857 {
08858 struct sip_route *next;
08859
08860 while (route) {
08861 next = route->next;
08862 free(route);
08863 route = next;
08864 }
08865 }
08866
08867
08868 static void list_route(struct sip_route *route)
08869 {
08870 if (!route)
08871 ast_verbose("list_route: no route\n");
08872 else {
08873 for (;route; route = route->next)
08874 ast_verbose("list_route: hop: <%s>\n", route->hop);
08875 }
08876 }
08877
08878
08879 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
08880 {
08881 struct sip_route *thishop, *head, *tail;
08882 int start = 0;
08883 int len;
08884 const char *rr, *contact, *c;
08885
08886
08887 if (p->route && p->route_persistant) {
08888 if (option_debug)
08889 ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
08890 return;
08891 }
08892
08893 if (p->route) {
08894 free_old_route(p->route);
08895 p->route = NULL;
08896 }
08897
08898
08899 p->route_persistant = 1;
08900
08901
08902
08903
08904
08905
08906 head = NULL;
08907 tail = head;
08908
08909 for (;;) {
08910
08911 rr = __get_header(req, "Record-Route", &start);
08912 if (*rr == '\0')
08913 break;
08914 for (; (rr = strchr(rr, '<')) ; rr += len) {
08915 ++rr;
08916 len = strcspn(rr, ">") + 1;
08917
08918 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
08919
08920 ast_copy_string(thishop->hop, rr, len);
08921 if (option_debug > 1)
08922 ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
08923
08924 if (backwards) {
08925
08926 thishop->next = head;
08927 head = thishop;
08928
08929 if (!tail)
08930 tail = thishop;
08931 } else {
08932 thishop->next = NULL;
08933
08934 if (tail)
08935 tail->next = thishop;
08936 else
08937 head = thishop;
08938 tail = thishop;
08939 }
08940 }
08941 }
08942 }
08943
08944
08945 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop,";lr") == NULL) ) {
08946
08947
08948 contact = get_header(req, "Contact");
08949 if (!ast_strlen_zero(contact)) {
08950 if (option_debug > 1)
08951 ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
08952
08953 c = strchr(contact, '<');
08954 if (c) {
08955
08956 ++c;
08957 len = strcspn(c, ">") + 1;
08958 } else {
08959
08960 c = contact;
08961 len = strlen(contact) + 1;
08962 }
08963 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
08964
08965 ast_copy_string(thishop->hop, c, len);
08966 thishop->next = NULL;
08967
08968 if (tail)
08969 tail->next = thishop;
08970 else
08971 head = thishop;
08972 }
08973 }
08974 }
08975
08976
08977 p->route = head;
08978
08979
08980 if (sip_debug_test_pvt(p))
08981 list_route(p->route);
08982 }
08983
08984
08985
08986
08987
08988
08989
08990 static void set_nonce_randdata(struct sip_pvt *p, int forceupdate)
08991 {
08992 if (p->stalenonce || forceupdate || ast_strlen_zero(p->randdata)) {
08993 ast_string_field_build(p, randdata, "%08lx", ast_random());
08994 p->stalenonce = 0;
08995 }
08996 }
08997
08998 AST_THREADSTORAGE(check_auth_buf, check_auth_buf_init);
08999 #define CHECK_AUTH_BUF_INITLEN 256
09000
09001
09002
09003
09004
09005
09006 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
09007 const char *secret, const char *md5secret, int sipmethod,
09008 char *uri, enum xmittype reliable, int ignore)
09009 {
09010 const char *response = "407 Proxy Authentication Required";
09011 const char *reqheader = "Proxy-Authorization";
09012 const char *respheader = "Proxy-Authenticate";
09013 const char *authtoken;
09014 char a1_hash[256];
09015 char resp_hash[256]="";
09016 char *c;
09017 int wrongnonce = FALSE;
09018 int good_response;
09019 const char *usednonce = p->randdata;
09020 struct ast_dynamic_str *buf;
09021 int res;
09022
09023
09024 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
09025 struct x {
09026 const char *key;
09027 const char *s;
09028 } *i, keys[] = {
09029 [K_RESP] = { "response=", "" },
09030 [K_URI] = { "uri=", "" },
09031 [K_USER] = { "username=", "" },
09032 [K_NONCE] = { "nonce=", "" },
09033 [K_LAST] = { NULL, NULL}
09034 };
09035
09036
09037 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
09038 return AUTH_SUCCESSFUL;
09039 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
09040
09041
09042
09043 response = "401 Unauthorized";
09044 reqheader = "Authorization";
09045 respheader = "WWW-Authenticate";
09046 }
09047 authtoken = get_header(req, reqheader);
09048 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
09049
09050
09051 if (!reliable) {
09052
09053
09054 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
09055
09056 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09057 }
09058 return AUTH_CHALLENGE_SENT;
09059 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
09060
09061 set_nonce_randdata(p, 1);
09062 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
09063
09064 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09065 return AUTH_CHALLENGE_SENT;
09066 }
09067
09068
09069
09070
09071
09072
09073 if (!(buf = ast_dynamic_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN)))
09074 return AUTH_SECRET_FAILED;
09075
09076
09077 res = ast_dynamic_str_thread_set(&buf, 0, &check_auth_buf, "%s", authtoken);
09078
09079 if (res == AST_DYNSTR_BUILD_FAILED)
09080 return AUTH_SECRET_FAILED;
09081
09082 c = buf->str;
09083
09084 while(c && *(c = ast_skip_blanks(c)) ) {
09085 for (i = keys; i->key != NULL; i++) {
09086 const char *separator = ",";
09087
09088 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
09089 continue;
09090
09091 c += strlen(i->key);
09092 if (*c == '"') {
09093 c++;
09094 separator = "\"";
09095 }
09096 i->s = c;
09097 strsep(&c, separator);
09098 break;
09099 }
09100 if (i->key == NULL)
09101 strsep(&c, " ,");
09102 }
09103
09104
09105 if (strcmp(username, keys[K_USER].s)) {
09106 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
09107 username, keys[K_USER].s);
09108
09109 return AUTH_USERNAME_MISMATCH;
09110 }
09111
09112
09113
09114 if (strcasecmp(p->randdata, keys[K_NONCE].s) || p->stalenonce) {
09115 wrongnonce = TRUE;
09116 usednonce = keys[K_NONCE].s;
09117 } else {
09118 p->stalenonce = 1;
09119 }
09120
09121 if (!ast_strlen_zero(md5secret))
09122 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
09123 else {
09124 char a1[256];
09125 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
09126 ast_md5_hash(a1_hash, a1);
09127 }
09128
09129
09130 {
09131 char a2[256];
09132 char a2_hash[256];
09133 char resp[256];
09134
09135 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
09136 S_OR(keys[K_URI].s, uri));
09137 ast_md5_hash(a2_hash, a2);
09138 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
09139 ast_md5_hash(resp_hash, resp);
09140 }
09141
09142 good_response = keys[K_RESP].s &&
09143 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
09144 if (wrongnonce) {
09145 if (good_response) {
09146 if (sipdebug)
09147 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "To"));
09148
09149 set_nonce_randdata(p, 0);
09150 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
09151 } else {
09152
09153 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
09154 if (sipdebug)
09155 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
09156 set_nonce_randdata(p, 1);
09157 } else {
09158 if (sipdebug)
09159 ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", get_header(req, "To"));
09160 }
09161 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
09162 }
09163
09164
09165 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09166 return AUTH_CHALLENGE_SENT;
09167 }
09168 if (good_response) {
09169 append_history(p, "AuthOK", "Auth challenge succesful for %s", username);
09170 return AUTH_SUCCESSFUL;
09171 }
09172
09173
09174
09175
09176
09177
09178 return AUTH_SECRET_FAILED;
09179 }
09180
09181
09182 static void sip_peer_hold(struct sip_pvt *p, int hold)
09183 {
09184 struct sip_peer *peer = find_peer(p->peername, NULL, 1, 0);
09185
09186 if (!peer)
09187 return;
09188
09189
09190 if (hold)
09191 peer->onHold++;
09192 else
09193 peer->onHold--;
09194
09195
09196 ast_device_state_changed("SIP/%s", peer->name);
09197
09198 return;
09199 }
09200
09201
09202
09203
09204 static int cb_extensionstate(char *context, char* exten, int state, void *data)
09205 {
09206 struct sip_pvt *p = data;
09207
09208 ast_mutex_lock(&p->lock);
09209
09210 switch(state) {
09211 case AST_EXTENSION_DEACTIVATED:
09212 case AST_EXTENSION_REMOVED:
09213 if (p->autokillid > -1 && sip_cancel_destroy(p))
09214 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
09215 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09216 ast_verbose(VERBOSE_PREFIX_2 "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
09217 p->stateid = -1;
09218 p->subscribed = NONE;
09219 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
09220 break;
09221 default:
09222 p->laststate = state;
09223 break;
09224 }
09225 if (p->subscribed != NONE) {
09226 if (!p->pendinginvite) {
09227 transmit_state_notify(p, state, 1, FALSE);
09228 } else {
09229
09230
09231 ast_set_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
09232 }
09233 }
09234 if (option_verbose > 1)
09235 ast_verbose(VERBOSE_PREFIX_1 "Extension Changed %s[%s] new state %s for Notify User %s %s\n", exten, context, ast_extension_state2str(state), p->username,
09236 ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE) ? "(queued)" : "");
09237
09238
09239 ast_mutex_unlock(&p->lock);
09240
09241 return 0;
09242 }
09243
09244
09245
09246
09247 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable)
09248 {
09249
09250
09251 const char *response = "407 Proxy Authentication Required";
09252 const char *reqheader = "Proxy-Authorization";
09253 const char *respheader = "Proxy-Authenticate";
09254 const char *authtoken;
09255 struct ast_dynamic_str *buf;
09256 char *c;
09257
09258
09259 enum keys { K_NONCE, K_LAST };
09260 struct x {
09261 const char *key;
09262 const char *s;
09263 } *i, keys[] = {
09264 [K_NONCE] = { "nonce=", "" },
09265 [K_LAST] = { NULL, NULL}
09266 };
09267
09268 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
09269 response = "401 Unauthorized";
09270 reqheader = "Authorization";
09271 respheader = "WWW-Authenticate";
09272 }
09273 authtoken = get_header(req, reqheader);
09274 if (ast_test_flag(req, SIP_PKT_IGNORE) && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
09275
09276
09277 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
09278
09279 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09280 return;
09281 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
09282
09283 set_nonce_randdata(p, 1);
09284 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
09285
09286 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09287 return;
09288 }
09289
09290 if (!(buf = ast_dynamic_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
09291 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
09292 return;
09293 }
09294
09295
09296 if (ast_dynamic_str_thread_set(&buf, 0, &check_auth_buf, "%s", authtoken) == AST_DYNSTR_BUILD_FAILED) {
09297 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
09298 return;
09299 }
09300
09301 c = buf->str;
09302
09303 while (c && *(c = ast_skip_blanks(c))) {
09304 for (i = keys; i->key != NULL; i++) {
09305 const char *separator = ",";
09306
09307 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
09308 continue;
09309 }
09310
09311 c += strlen(i->key);
09312 if (*c == '"') {
09313 c++;
09314 separator = "\"";
09315 }
09316 i->s = c;
09317 strsep(&c, separator);
09318 break;
09319 }
09320 if (i->key == NULL) {
09321 strsep(&c, " ,");
09322 }
09323 }
09324
09325
09326 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
09327 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
09328 set_nonce_randdata(p, 1);
09329 }
09330 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
09331
09332
09333 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09334 } else {
09335 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
09336 }
09337 }
09338
09339
09340
09341
09342
09343
09344 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
09345 struct sip_request *req, char *uri)
09346 {
09347 enum check_auth_result res = AUTH_NOT_FOUND;
09348 struct sip_peer *peer;
09349 char tmp[256];
09350 char *name, *c;
09351 char *t;
09352 char *domain;
09353
09354
09355 t = uri;
09356 while(*t && (*t > 32) && (*t != ';'))
09357 t++;
09358 *t = '\0';
09359
09360 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
09361 if (pedanticsipchecking)
09362 ast_uri_decode(tmp);
09363
09364 c = get_in_brackets(tmp);
09365 c = strsep(&c, ";");
09366
09367 if (!strncasecmp(c, "sip:", 4)) {
09368 name = c + 4;
09369 } else {
09370 name = c;
09371 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(sin->sin_addr));
09372 }
09373
09374
09375 if ((c = strchr(name, '@'))) {
09376 *c++ = '\0';
09377 domain = c;
09378 if ((c = strchr(domain, ':')))
09379 *c = '\0';
09380 if (!AST_LIST_EMPTY(&domain_list)) {
09381 if (!check_sip_domain(domain, NULL, 0)) {
09382 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
09383 return AUTH_UNKNOWN_DOMAIN;
09384 }
09385 }
09386 }
09387
09388 ast_string_field_set(p, exten, name);
09389 build_contact(p);
09390 peer = find_peer(name, NULL, 1, 0);
09391 if (!(peer && ast_apply_ha(peer->ha, sin))) {
09392
09393 if (peer) {
09394 ASTOBJ_UNREF(peer, sip_destroy_peer);
09395 res = AUTH_ACL_FAILED;
09396 } else
09397 res = AUTH_NOT_FOUND;
09398 }
09399 if (peer) {
09400
09401 if (p->rtp) {
09402 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
09403 p->autoframing = peer->autoframing;
09404 }
09405 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
09406 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
09407 res = AUTH_PEER_NOT_DYNAMIC;
09408 } else {
09409 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
09410 transmit_response(p, "100 Trying", req);
09411 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09412 if (sip_cancel_destroy(p))
09413 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
09414
09415
09416
09417 switch (parse_register_contact(p, peer, req)) {
09418 case PARSE_REGISTER_DENIED:
09419 transmit_response_with_date(p, "603 Denied", req);
09420 peer->lastmsgssent = -1;
09421 res = 0;
09422 break;
09423 case PARSE_REGISTER_FAILED:
09424 ast_log(LOG_WARNING, "Failed to parse contact info\n");
09425 transmit_response_with_date(p, "400 Bad Request", req);
09426 peer->lastmsgssent = -1;
09427 res = 0;
09428 break;
09429 case PARSE_REGISTER_QUERY:
09430 ast_string_field_set(p, fullcontact, peer->fullcontact);
09431 transmit_response_with_date(p, "200 OK", req);
09432 peer->lastmsgssent = -1;
09433 res = 0;
09434 break;
09435 case PARSE_REGISTER_UPDATE:
09436 ast_string_field_set(p, fullcontact, peer->fullcontact);
09437 update_peer(peer, p->expiry);
09438
09439 transmit_response_with_date(p, "200 OK", req);
09440 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
09441 peer->lastmsgssent = -1;
09442 res = 0;
09443 break;
09444 }
09445 }
09446 }
09447 }
09448 if (!peer && autocreatepeer) {
09449
09450 peer = temp_peer(name);
09451 if (peer) {
09452 ASTOBJ_CONTAINER_LINK(&peerl, peer);
09453 if (sip_cancel_destroy(p))
09454 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
09455 switch (parse_register_contact(p, peer, req)) {
09456 case PARSE_REGISTER_DENIED:
09457 ast_log(LOG_WARNING, "Registration denied because of contact ACL\n");
09458 transmit_response_with_date(p, "403 Forbidden (ACL)", req);
09459 peer->lastmsgssent = -1;
09460 res = 0;
09461 break;
09462 case PARSE_REGISTER_FAILED:
09463 ast_log(LOG_WARNING, "Failed to parse contact info\n");
09464 transmit_response_with_date(p, "400 Bad Request", req);
09465 peer->lastmsgssent = -1;
09466 res = 0;
09467 break;
09468 case PARSE_REGISTER_QUERY:
09469 ast_string_field_set(p, fullcontact, peer->fullcontact);
09470 transmit_response_with_date(p, "200 OK", req);
09471 peer->lastmsgssent = -1;
09472 res = 0;
09473 break;
09474 case PARSE_REGISTER_UPDATE:
09475 ast_string_field_set(p, fullcontact, peer->fullcontact);
09476
09477 transmit_response_with_date(p, "200 OK", req);
09478 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
09479 peer->lastmsgssent = -1;
09480 res = 0;
09481 break;
09482 }
09483 }
09484 }
09485 if (!peer && global_alwaysauthreject) {
09486
09487
09488
09489 transmit_response(p, "100 Trying", req);
09490
09491 sched_yield();
09492 }
09493 if (!res) {
09494 ast_device_state_changed("SIP/%s", peer->name);
09495 }
09496 if (res < 0) {
09497 switch (res) {
09498 case AUTH_SECRET_FAILED:
09499
09500 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
09501 break;
09502 case AUTH_USERNAME_MISMATCH:
09503
09504
09505
09506
09507 case AUTH_NOT_FOUND:
09508 case AUTH_PEER_NOT_DYNAMIC:
09509 case AUTH_ACL_FAILED:
09510 if (global_alwaysauthreject) {
09511 transmit_fake_auth_response(p, SIP_REGISTER, &p->initreq, XMIT_UNRELIABLE);
09512 } else {
09513
09514 if (res == AUTH_PEER_NOT_DYNAMIC)
09515 transmit_response(p, "403 Forbidden", &p->initreq);
09516 else
09517 transmit_response(p, "404 Not found", &p->initreq);
09518 }
09519 break;
09520 default:
09521 break;
09522 }
09523 }
09524 if (peer)
09525 ASTOBJ_UNREF(peer, sip_destroy_peer);
09526
09527 return res;
09528 }
09529
09530
09531 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
09532 {
09533 char tmp[256], *c, *a;
09534 struct sip_request *req;
09535
09536 req = oreq;
09537 if (!req)
09538 req = &p->initreq;
09539 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
09540 if (ast_strlen_zero(tmp))
09541 return 0;
09542 c = get_in_brackets(tmp);
09543 if (strncasecmp(c, "sip:", 4)) {
09544 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", c);
09545 return -1;
09546 }
09547 c += 4;
09548 a = c;
09549 strsep(&a, "@;");
09550 if (sip_debug_test_pvt(p))
09551 ast_verbose("RDNIS is %s\n", c);
09552 ast_string_field_set(p, rdnis, c);
09553
09554 return 0;
09555 }
09556
09557
09558
09559
09560 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
09561 {
09562 char tmp[256] = "", *uri, *a;
09563 char tmpf[256] = "", *from;
09564 struct sip_request *req;
09565 char *colon;
09566 char *decoded_uri;
09567
09568 req = oreq;
09569 if (!req)
09570 req = &p->initreq;
09571
09572
09573 if (req->rlPart2)
09574 ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
09575
09576 if (pedanticsipchecking)
09577 ast_uri_decode(tmp);
09578
09579 uri = get_in_brackets(tmp);
09580
09581 if (strncasecmp(uri, "sip:", 4)) {
09582 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
09583 return -1;
09584 }
09585 uri += 4;
09586
09587
09588 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
09589 if (!ast_strlen_zero(tmpf)) {
09590 if (pedanticsipchecking)
09591 ast_uri_decode(tmpf);
09592 from = get_in_brackets(tmpf);
09593 } else {
09594 from = NULL;
09595 }
09596
09597 if (!ast_strlen_zero(from)) {
09598 if (strncasecmp(from, "sip:", 4)) {
09599 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
09600 return -1;
09601 }
09602 from += 4;
09603 if ((a = strchr(from, '@')))
09604 *a++ = '\0';
09605 else
09606 a = from;
09607 from = strsep(&from, ";");
09608 a = strsep(&a, ";");
09609 ast_string_field_set(p, fromdomain, a);
09610 }
09611
09612
09613
09614
09615 if ((a = strchr(uri, '@'))) {
09616 *a++ = '\0';
09617 } else {
09618 a = uri;
09619 uri = "s";
09620 }
09621 colon = strchr(a, ':');
09622 if (colon)
09623 *colon = '\0';
09624
09625 uri = strsep(&uri, ";");
09626 a = strsep(&a, ";");
09627
09628 ast_string_field_set(p, domain, a);
09629
09630 if (!AST_LIST_EMPTY(&domain_list)) {
09631 char domain_context[AST_MAX_EXTENSION];
09632
09633 domain_context[0] = '\0';
09634 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
09635 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
09636 if (option_debug)
09637 ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
09638 return -2;
09639 }
09640 }
09641
09642 if (!ast_strlen_zero(domain_context))
09643 ast_string_field_set(p, context, domain_context);
09644 }
09645
09646
09647 if (req->method == SIP_SUBSCRIBE && !ast_strlen_zero(p->subscribecontext))
09648 ast_string_field_set(p, context, p->subscribecontext);
09649
09650 if (sip_debug_test_pvt(p))
09651 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
09652
09653
09654 if (req->method == SIP_SUBSCRIBE) {
09655 char hint[AST_MAX_EXTENSION];
09656 return (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten) ? 0 : -1);
09657 } else {
09658 decoded_uri = ast_strdupa(uri);
09659 ast_uri_decode(decoded_uri);
09660
09661
09662
09663
09664
09665 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)) ||
09666 !strcmp(decoded_uri, ast_pickup_ext())) {
09667 if (!oreq)
09668 ast_string_field_set(p, exten, decoded_uri);
09669 return 0;
09670 }
09671 }
09672
09673
09674 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
09675 ast_canmatch_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from))) ||
09676 !strncmp(decoded_uri, ast_pickup_ext(), strlen(decoded_uri))) {
09677 return 1;
09678 }
09679
09680 return -1;
09681 }
09682
09683
09684
09685 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
09686 {
09687 struct sip_pvt *sip_pvt_ptr;
09688
09689 ast_mutex_lock(&iflock);
09690
09691 if (option_debug > 3 && totag) {
09692 ast_log(LOG_DEBUG, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
09693 }
09694
09695
09696 for (sip_pvt_ptr = iflist; sip_pvt_ptr; sip_pvt_ptr = sip_pvt_ptr->next) {
09697 if (!strcmp(sip_pvt_ptr->callid, callid)) {
09698 int match = 1;
09699
09700 if (option_debug > 3)
09701 ast_log(LOG_DEBUG, "Found call with callid %s (ourtag=%s, theirtag=%s)\n", callid, sip_pvt_ptr->tag, sip_pvt_ptr->theirtag);
09702
09703
09704 ast_mutex_lock(&sip_pvt_ptr->lock);
09705
09706
09707
09708
09709
09710 if (pedanticsipchecking) {
09711
09712
09713
09714
09715
09716
09717
09718
09719
09720
09721
09722
09723
09724
09725 if (ast_strlen_zero(fromtag) || strcmp(fromtag, sip_pvt_ptr->theirtag) || (!ast_strlen_zero(totag) && strcmp(totag, sip_pvt_ptr->tag)))
09726 match = 0;
09727 }
09728
09729 if (!match) {
09730 ast_mutex_unlock(&sip_pvt_ptr->lock);
09731 continue;
09732 }
09733
09734 if (option_debug > 3 && totag)
09735 ast_log(LOG_DEBUG, "Matched %s call - their tag is %s Our tag is %s\n",
09736 ast_test_flag(&sip_pvt_ptr->flags[1], SIP_PAGE2_OUTGOING_CALL) ? "OUTGOING": "INCOMING",
09737 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
09738
09739
09740 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
09741 DEADLOCK_AVOIDANCE(&sip_pvt_ptr->lock);
09742 }
09743 break;
09744 }
09745 }
09746 ast_mutex_unlock(&iflock);
09747 if (option_debug > 3 && !sip_pvt_ptr)
09748 ast_log(LOG_DEBUG, "Found no match for callid %s to-tag %s from-tag %s\n", callid, totag, fromtag);
09749 return sip_pvt_ptr;
09750 }
09751
09752
09753
09754 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
09755 {
09756
09757 const char *p_referred_by = NULL;
09758 char *h_refer_to = NULL;
09759 char *h_referred_by = NULL;
09760 char *refer_to;
09761 const char *p_refer_to;
09762 char *referred_by_uri = NULL;
09763 char *ptr;
09764 struct sip_request *req = NULL;
09765 const char *transfer_context = NULL;
09766 struct sip_refer *referdata;
09767
09768
09769 req = outgoing_req;
09770 referdata = transferer->refer;
09771
09772 if (!req)
09773 req = &transferer->initreq;
09774
09775 p_refer_to = get_header(req, "Refer-To");
09776 if (ast_strlen_zero(p_refer_to)) {
09777 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
09778 return -2;
09779 }
09780 h_refer_to = ast_strdupa(p_refer_to);
09781 refer_to = get_in_brackets(h_refer_to);
09782 if (pedanticsipchecking)
09783 ast_uri_decode(refer_to);
09784
09785 if (strncasecmp(refer_to, "sip:", 4)) {
09786 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
09787 return -3;
09788 }
09789 refer_to += 4;
09790
09791
09792 p_referred_by = get_header(req, "Referred-By");
09793 if (!ast_strlen_zero(p_referred_by)) {
09794 char *lessthan;
09795 h_referred_by = ast_strdupa(p_referred_by);
09796 if (pedanticsipchecking)
09797 ast_uri_decode(h_referred_by);
09798
09799
09800 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
09801 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
09802 *(lessthan - 1) = '\0';
09803 }
09804
09805 referred_by_uri = get_in_brackets(h_referred_by);
09806 if(strncasecmp(referred_by_uri, "sip:", 4)) {
09807 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
09808 referred_by_uri = (char *) NULL;
09809 } else {
09810 referred_by_uri += 4;
09811 }
09812 }
09813
09814
09815 if ((ptr = strcasestr(refer_to, "replaces="))) {
09816 char *to = NULL, *from = NULL;
09817
09818
09819 referdata->attendedtransfer = 1;
09820 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
09821 ast_uri_decode(referdata->replaces_callid);
09822 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
09823 *ptr++ = '\0';
09824 }
09825
09826 if (ptr) {
09827
09828 to = strcasestr(ptr, "to-tag=");
09829 from = strcasestr(ptr, "from-tag=");
09830 }
09831
09832
09833 if (to) {
09834 ptr = to + 7;
09835 if ((to = strchr(ptr, '&')))
09836 *to = '\0';
09837 if ((to = strchr(ptr, ';')))
09838 *to = '\0';
09839 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
09840 }
09841
09842 if (from) {
09843 ptr = from + 9;
09844 if ((to = strchr(ptr, '&')))
09845 *to = '\0';
09846 if ((to = strchr(ptr, ';')))
09847 *to = '\0';
09848 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
09849 }
09850
09851 if (option_debug > 1) {
09852 if (!pedanticsipchecking)
09853 ast_log(LOG_DEBUG,"Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
09854 else
09855 ast_log(LOG_DEBUG,"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>" );
09856 }
09857 }
09858
09859 if ((ptr = strchr(refer_to, '@'))) {
09860 char *urioption = NULL, *domain;
09861 *ptr++ = '\0';
09862
09863 if ((urioption = strchr(ptr, ';')))
09864 *urioption++ = '\0';
09865
09866 domain = ptr;
09867 if ((ptr = strchr(domain, ':')))
09868 *ptr = '\0';
09869
09870
09871 ast_copy_string(referdata->refer_to_domain, domain, sizeof(referdata->refer_to_domain));
09872 if (urioption)
09873 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
09874 }
09875
09876 if ((ptr = strchr(refer_to, ';')))
09877 *ptr = '\0';
09878 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
09879
09880 if (referred_by_uri) {
09881 if ((ptr = strchr(referred_by_uri, ';')))
09882 *ptr = '\0';
09883 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
09884 } else {
09885 referdata->referred_by[0] = '\0';
09886 }
09887
09888
09889 if (transferer->owner)
09890 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
09891
09892
09893 if (ast_strlen_zero(transfer_context)) {
09894 transfer_context = S_OR(transferer->owner->macrocontext,
09895 S_OR(transferer->context, default_context));
09896 }
09897
09898 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
09899
09900
09901 if (referdata->attendedtransfer || ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
09902 if (sip_debug_test_pvt(transferer)) {
09903 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
09904 }
09905
09906 return 0;
09907 }
09908 if (sip_debug_test_pvt(transferer))
09909 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
09910
09911
09912 return -1;
09913 }
09914
09915
09916
09917 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
09918 {
09919 char tmp[256] = "", *c, *a;
09920 struct sip_request *req = oreq ? oreq : &p->initreq;
09921 struct sip_refer *referdata = NULL;
09922 const char *transfer_context = NULL;
09923
09924 if (!p->refer && !sip_refer_allocate(p))
09925 return -1;
09926
09927 referdata = p->refer;
09928
09929 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
09930 c = get_in_brackets(tmp);
09931
09932 if (pedanticsipchecking)
09933 ast_uri_decode(c);
09934
09935 if (strncasecmp(c, "sip:", 4)) {
09936 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
09937 return -1;
09938 }
09939 c += 4;
09940 if ((a = strchr(c, ';')))
09941 *a = '\0';
09942
09943 if ((a = strchr(c, '@'))) {
09944 *a++ = '\0';
09945 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
09946 }
09947
09948 if (sip_debug_test_pvt(p))
09949 ast_verbose("Looking for %s in %s\n", c, p->context);
09950
09951 if (p->owner)
09952 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
09953
09954
09955 if (ast_strlen_zero(transfer_context)) {
09956 transfer_context = S_OR(p->owner->macrocontext,
09957 S_OR(p->context, default_context));
09958 }
09959 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
09960
09961 if (option_debug)
09962 ast_log(LOG_DEBUG,"SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
09963 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
09964 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
09965 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
09966 referdata->refer_call = NULL;
09967
09968 ast_string_field_set(p, context, transfer_context);
09969 return 0;
09970 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
09971 return 1;
09972 }
09973
09974 return -1;
09975 }
09976
09977 static void check_via(struct sip_pvt *p, const struct sip_request *req)
09978 {
09979 char via[512];
09980 char *c, *pt;
09981 struct hostent *hp;
09982 struct ast_hostent ahp;
09983
09984 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
09985
09986
09987 c = strchr(via, ',');
09988 if (c)
09989 *c = '\0';
09990
09991
09992 c = strstr(via, ";rport");
09993 if (c && (c[6] != '='))
09994 ast_set_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT);
09995
09996 c = strchr(via, ';');
09997 if (c)
09998 *c = '\0';
09999
10000 c = strchr(via, ' ');
10001 if (c) {
10002 *c = '\0';
10003 c = ast_skip_blanks(c+1);
10004 if (strcasecmp(via, "SIP/2.0/UDP")) {
10005 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
10006 return;
10007 }
10008 pt = strchr(c, ':');
10009 if (pt)
10010 *pt++ = '\0';
10011 hp = ast_gethostbyname(c, &ahp);
10012 if (!hp) {
10013 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
10014 return;
10015 }
10016 memset(&p->sa, 0, sizeof(p->sa));
10017 p->sa.sin_family = AF_INET;
10018 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
10019 p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
10020
10021 if (sip_debug_test_pvt(p)) {
10022 const struct sockaddr_in *dst = sip_real_dst(p);
10023 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
10024 }
10025 }
10026 }
10027
10028
10029 static char *get_calleridname(const char *input, char *output, size_t outputsize)
10030 {
10031 const char *end = strchr(input,'<');
10032 const char *tmp = strchr(input,'"');
10033 int bytes = 0;
10034 int maxbytes = outputsize - 1;
10035
10036 if (!end || end == input)
10037 return NULL;
10038
10039 end--;
10040
10041 if (tmp && tmp <= end) {
10042
10043
10044
10045 end = strchr(tmp+1, '"');
10046 if (!end)
10047 return NULL;
10048 bytes = (int) (end - tmp);
10049
10050 if (bytes > maxbytes)
10051 bytes = maxbytes;
10052 ast_copy_string(output, tmp + 1, bytes);
10053 } else {
10054
10055
10056 input = ast_skip_blanks(input);
10057
10058 while(*end && *end < 33 && end > input)
10059 end--;
10060 if (end >= input) {
10061 bytes = (int) (end - input) + 2;
10062
10063 if (bytes > maxbytes)
10064 bytes = maxbytes;
10065 ast_copy_string(output, input, bytes);
10066 } else
10067 return NULL;
10068 }
10069 return output;
10070 }
10071
10072
10073
10074
10075
10076 static int get_rpid_num(const char *input, char *output, int maxlen)
10077 {
10078 char *start;
10079 char *end;
10080
10081 start = strchr(input,':');
10082 if (!start) {
10083 output[0] = '\0';
10084 return 0;
10085 }
10086 start++;
10087
10088
10089 ast_copy_string(output,start,maxlen);
10090 output[maxlen-1] = '\0';
10091
10092 end = strchr(output,'@');
10093 if (end)
10094 *end = '\0';
10095 else
10096 output[0] = '\0';
10097 if (strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
10098 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
10099
10100 return 0;
10101 }
10102
10103
10104
10105
10106
10107
10108
10109 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
10110 int sipmethod, char *uri, enum xmittype reliable,
10111 struct sockaddr_in *sin, struct sip_peer **authpeer)
10112 {
10113 struct sip_user *user = NULL;
10114 struct sip_peer *peer;
10115 char from[256], *c;
10116 char *of;
10117 char rpid_num[50];
10118 const char *rpid;
10119 enum check_auth_result res = AUTH_SUCCESSFUL;
10120 char *t;
10121 char calleridname[50];
10122 int debug=sip_debug_test_addr(sin);
10123 struct ast_variable *tmpvar = NULL, *v = NULL;
10124 char *uri2 = ast_strdupa(uri);
10125
10126
10127 t = uri2;
10128 while (*t && *t > 32 && *t != ';')
10129 t++;
10130 *t = '\0';
10131 ast_copy_string(from, get_header(req, "From"), sizeof(from));
10132 if (pedanticsipchecking)
10133 ast_uri_decode(from);
10134
10135 memset(calleridname, 0, sizeof(calleridname));
10136 get_calleridname(from, calleridname, sizeof(calleridname));
10137 if (calleridname[0])
10138 ast_string_field_set(p, cid_name, calleridname);
10139
10140 rpid = get_header(req, "Remote-Party-ID");
10141 memset(rpid_num, 0, sizeof(rpid_num));
10142 if (!ast_strlen_zero(rpid))
10143 p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
10144
10145 of = get_in_brackets(from);
10146 if (ast_strlen_zero(p->exten)) {
10147 t = uri2;
10148 if (!strncasecmp(t, "sip:", 4))
10149 t+= 4;
10150 ast_string_field_set(p, exten, t);
10151 t = strchr(p->exten, '@');
10152 if (t)
10153 *t = '\0';
10154 if (ast_strlen_zero(p->our_contact))
10155 build_contact(p);
10156 }
10157
10158 ast_string_field_set(p, from, of);
10159 if (strncasecmp(of, "sip:", 4)) {
10160 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
10161 } else
10162 of += 4;
10163
10164 if ((c = strchr(of, '@'))) {
10165 char *tmp;
10166 *c = '\0';
10167 if ((c = strchr(of, ':')))
10168 *c = '\0';
10169 tmp = ast_strdupa(of);
10170
10171
10172
10173 tmp = strsep(&tmp, ";");
10174 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10175 ast_shrink_phone_number(tmp);
10176 ast_string_field_set(p, cid_num, tmp);
10177 }
10178
10179 if (!authpeer)
10180 user = find_user(of, 1);
10181
10182
10183 if (user && ast_apply_ha(user->ha, sin)) {
10184 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
10185 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10186 if (sipmethod == SIP_INVITE) {
10187
10188 for (v = user->chanvars ; v ; v = v->next) {
10189 if ((tmpvar = ast_variable_new(v->name, v->value))) {
10190 tmpvar->next = p->chanvars;
10191 p->chanvars = tmpvar;
10192 }
10193 }
10194 }
10195 p->prefs = user->prefs;
10196
10197 if (p->rtp) {
10198 ast_rtp_codec_setpref(p->rtp, &p->prefs);
10199 p->autoframing = user->autoframing;
10200 }
10201
10202 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
10203 char *tmp;
10204 if (*calleridname)
10205 ast_string_field_set(p, cid_name, calleridname);
10206 tmp = ast_strdupa(rpid_num);
10207 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10208 ast_shrink_phone_number(tmp);
10209 ast_string_field_set(p, cid_num, tmp);
10210 }
10211
10212 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
10213
10214 if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
10215 if (sip_cancel_destroy(p))
10216 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
10217 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
10218 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10219
10220 if (p->sipoptions)
10221 user->sipoptions = p->sipoptions;
10222
10223
10224 if (user->call_limit)
10225 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
10226 if (!ast_strlen_zero(user->context))
10227 ast_string_field_set(p, context, user->context);
10228 if (!ast_strlen_zero(user->cid_num)) {
10229 char *tmp = ast_strdupa(user->cid_num);
10230 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10231 ast_shrink_phone_number(tmp);
10232 ast_string_field_set(p, cid_num, tmp);
10233 }
10234 if (!ast_strlen_zero(user->cid_name))
10235 ast_string_field_set(p, cid_name, user->cid_name);
10236 ast_string_field_set(p, username, user->name);
10237 ast_string_field_set(p, peername, user->name);
10238 ast_string_field_set(p, peersecret, user->secret);
10239 ast_string_field_set(p, peermd5secret, user->md5secret);
10240 ast_string_field_set(p, subscribecontext, user->subscribecontext);
10241 ast_string_field_set(p, accountcode, user->accountcode);
10242 ast_string_field_set(p, language, user->language);
10243 ast_string_field_set(p, mohsuggest, user->mohsuggest);
10244 ast_string_field_set(p, mohinterpret, user->mohinterpret);
10245 p->allowtransfer = user->allowtransfer;
10246 p->amaflags = user->amaflags;
10247 p->callgroup = user->callgroup;
10248 p->pickupgroup = user->pickupgroup;
10249 if (user->callingpres)
10250 p->callingpres = user->callingpres;
10251
10252
10253 p->capability = user->capability;
10254 p->jointcapability = user->capability;
10255 if (p->peercapability)
10256 p->jointcapability &= p->peercapability;
10257 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
10258 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
10259 p->noncodeccapability |= AST_RTP_DTMF;
10260 else
10261 p->noncodeccapability &= ~AST_RTP_DTMF;
10262 p->jointnoncodeccapability = p->noncodeccapability;
10263 if (p->t38.peercapability)
10264 p->t38.jointcapability &= p->t38.peercapability;
10265 p->maxcallbitrate = user->maxcallbitrate;
10266
10267 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
10268 ast_rtp_destroy(p->vrtp);
10269 p->vrtp = NULL;
10270 }
10271 }
10272 if (user && debug)
10273 ast_verbose("Found user '%s'\n", user->name);
10274 } else {
10275 if (user) {
10276 if (!authpeer && debug)
10277 ast_verbose("Found user '%s', but fails host access\n", user->name);
10278 ASTOBJ_UNREF(user,sip_destroy_user);
10279 }
10280 user = NULL;
10281 }
10282
10283 if (!user) {
10284
10285 if (sipmethod == SIP_SUBSCRIBE)
10286
10287 peer = find_peer(of, NULL, 1, 0);
10288 else
10289
10290
10291
10292
10293 peer = find_peer(NULL, &p->recv, 1, 0);
10294
10295 if (peer) {
10296
10297 if (p->rtp) {
10298 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
10299 p->autoframing = peer->autoframing;
10300 }
10301 if (debug)
10302 ast_verbose("Found peer '%s'\n", peer->name);
10303
10304
10305 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
10306 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10307
10308
10309 if (p->sipoptions)
10310 peer->sipoptions = p->sipoptions;
10311
10312
10313 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
10314 char *tmp = ast_strdupa(rpid_num);
10315 if (*calleridname)
10316 ast_string_field_set(p, cid_name, calleridname);
10317 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10318 ast_shrink_phone_number(tmp);
10319 ast_string_field_set(p, cid_num, tmp);
10320 }
10321 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
10322
10323 ast_string_field_set(p, peersecret, peer->secret);
10324 ast_string_field_set(p, peermd5secret, peer->md5secret);
10325 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
10326 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
10327 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
10328 if (peer->callingpres)
10329 p->callingpres = peer->callingpres;
10330 if (peer->maxms && peer->lastms)
10331 p->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
10332 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
10333
10334 ast_string_field_free(p, peersecret);
10335 ast_string_field_free(p, peermd5secret);
10336 }
10337 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
10338 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
10339 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10340
10341 if (peer->call_limit)
10342 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
10343 ast_string_field_set(p, peername, peer->name);
10344 ast_string_field_set(p, authname, peer->name);
10345
10346 if (sipmethod == SIP_INVITE) {
10347
10348 for (v = peer->chanvars ; v ; v = v->next) {
10349 if ((tmpvar = ast_variable_new(v->name, v->value))) {
10350 tmpvar->next = p->chanvars;
10351 p->chanvars = tmpvar;
10352 }
10353 }
10354 }
10355 if (authpeer) {
10356 (*authpeer) = ASTOBJ_REF(peer);
10357 }
10358
10359 if (!ast_strlen_zero(peer->username)) {
10360 ast_string_field_set(p, username, peer->username);
10361
10362
10363 ast_string_field_set(p, authname, peer->username);
10364 }
10365 if (!ast_strlen_zero(peer->cid_num)) {
10366 char *tmp = ast_strdupa(peer->cid_num);
10367 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10368 ast_shrink_phone_number(tmp);
10369 ast_string_field_set(p, cid_num, tmp);
10370 }
10371 if (!ast_strlen_zero(peer->cid_name))
10372 ast_string_field_set(p, cid_name, peer->cid_name);
10373 ast_string_field_set(p, fullcontact, peer->fullcontact);
10374 if (!ast_strlen_zero(peer->context))
10375 ast_string_field_set(p, context, peer->context);
10376 ast_string_field_set(p, peersecret, peer->secret);
10377 ast_string_field_set(p, peermd5secret, peer->md5secret);
10378 ast_string_field_set(p, language, peer->language);
10379 ast_string_field_set(p, accountcode, peer->accountcode);
10380 p->amaflags = peer->amaflags;
10381 p->callgroup = peer->callgroup;
10382 p->pickupgroup = peer->pickupgroup;
10383 p->capability = peer->capability;
10384 p->prefs = peer->prefs;
10385 p->jointcapability = peer->capability;
10386 if (p->peercapability)
10387 p->jointcapability &= p->peercapability;
10388 p->maxcallbitrate = peer->maxcallbitrate;
10389 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
10390 ast_rtp_destroy(p->vrtp);
10391 p->vrtp = NULL;
10392 }
10393 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
10394 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
10395 p->noncodeccapability |= AST_RTP_DTMF;
10396 else
10397 p->noncodeccapability &= ~AST_RTP_DTMF;
10398 p->jointnoncodeccapability = p->noncodeccapability;
10399 if (p->t38.peercapability)
10400 p->t38.jointcapability &= p->t38.peercapability;
10401 }
10402 ASTOBJ_UNREF(peer, sip_destroy_peer);
10403 } else {
10404 if (debug)
10405 ast_verbose("Found no matching peer or user for '%s:%d'\n", ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
10406
10407
10408 if (!global_allowguest) {
10409 if (global_alwaysauthreject)
10410 res = AUTH_FAKE_AUTH;
10411 else
10412 res = AUTH_SECRET_FAILED;
10413 } else if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
10414 char *tmp = ast_strdupa(rpid_num);
10415 if (*calleridname)
10416 ast_string_field_set(p, cid_name, calleridname);
10417 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10418 ast_shrink_phone_number(tmp);
10419 ast_string_field_set(p, cid_num, tmp);
10420 }
10421 }
10422
10423 }
10424
10425 if (user)
10426 ASTOBJ_UNREF(user, sip_destroy_user);
10427
10428 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT)) {
10429 ast_set_flag(&p->flags[0], SIP_NAT_ROUTE);
10430 }
10431
10432 return res;
10433 }
10434
10435
10436
10437
10438 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin)
10439 {
10440 return check_user_full(p, req, sipmethod, uri, reliable, sin, NULL);
10441 }
10442
10443
10444 static int get_msg_text(char *buf, int len, struct sip_request *req)
10445 {
10446 int x;
10447 int y;
10448
10449 buf[0] = '\0';
10450 y = len - strlen(buf) - 5;
10451 if (y < 0)
10452 y = 0;
10453 for (x=0;x<req->lines;x++) {
10454 strncat(buf, req->line[x], y);
10455 y -= strlen(req->line[x]) + 1;
10456 if (y < 0)
10457 y = 0;
10458 if (y != 0)
10459 strcat(buf, "\n");
10460 }
10461 return 0;
10462 }
10463
10464
10465
10466
10467
10468 static void receive_message(struct sip_pvt *p, struct sip_request *req)
10469 {
10470 char buf[1024];
10471 struct ast_frame f;
10472 const char *content_type = get_header(req, "Content-Type");
10473
10474 if (strncmp(content_type, "text/plain", strlen("text/plain"))) {
10475 transmit_response(p, "415 Unsupported Media Type", req);
10476 if (!p->owner)
10477 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10478 return;
10479 }
10480
10481 if (get_msg_text(buf, sizeof(buf), req)) {
10482 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
10483 transmit_response(p, "202 Accepted", req);
10484 if (!p->owner)
10485 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10486 return;
10487 }
10488
10489 if (p->owner) {
10490 if (sip_debug_test_pvt(p))
10491 ast_verbose("Message received: '%s'\n", buf);
10492 memset(&f, 0, sizeof(f));
10493 f.frametype = AST_FRAME_TEXT;
10494 f.subclass = 0;
10495 f.offset = 0;
10496 f.data = buf;
10497 f.datalen = strlen(buf);
10498 ast_queue_frame(p->owner, &f);
10499 transmit_response(p, "202 Accepted", req);
10500 } else {
10501 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);
10502 transmit_response(p, "405 Method Not Allowed", req);
10503 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10504 }
10505 return;
10506 }
10507
10508
10509 static int sip_show_inuse(int fd, int argc, char *argv[])
10510 {
10511 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
10512 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
10513 char ilimits[40];
10514 char iused[40];
10515 int showall = FALSE;
10516
10517 if (argc < 3)
10518 return RESULT_SHOWUSAGE;
10519
10520 if (argc == 4 && !strcmp(argv[3],"all"))
10521 showall = TRUE;
10522
10523 ast_cli(fd, FORMAT, "* User name", "In use", "Limit");
10524 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
10525 ASTOBJ_RDLOCK(iterator);
10526 if (iterator->call_limit)
10527 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
10528 else
10529 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
10530 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
10531 if (showall || iterator->call_limit)
10532 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
10533 ASTOBJ_UNLOCK(iterator);
10534 } while (0) );
10535
10536 ast_cli(fd, FORMAT, "* Peer name", "In use", "Limit");
10537
10538 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
10539 ASTOBJ_RDLOCK(iterator);
10540 if (iterator->call_limit)
10541 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
10542 else
10543 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
10544 snprintf(iused, sizeof(iused), "%d/%d", iterator->inUse, iterator->inRinging);
10545 if (showall || iterator->call_limit)
10546 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
10547 ASTOBJ_UNLOCK(iterator);
10548 } while (0) );
10549
10550 return RESULT_SUCCESS;
10551 #undef FORMAT
10552 #undef FORMAT2
10553 }
10554
10555
10556 static char *transfermode2str(enum transfermodes mode)
10557 {
10558 if (mode == TRANSFER_OPENFORALL)
10559 return "open";
10560 else if (mode == TRANSFER_CLOSED)
10561 return "closed";
10562 return "strict";
10563 }
10564
10565
10566 static char *nat2str(int nat)
10567 {
10568 switch(nat) {
10569 case SIP_NAT_NEVER:
10570 return "No";
10571 case SIP_NAT_ROUTE:
10572 return "Route";
10573 case SIP_NAT_ALWAYS:
10574 return "Always";
10575 case SIP_NAT_RFC3581:
10576 return "RFC3581";
10577 default:
10578 return "Unknown";
10579 }
10580 }
10581
10582
10583
10584
10585 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
10586 {
10587 int res = 0;
10588 if (peer->maxms) {
10589 if (peer->lastms < 0) {
10590 ast_copy_string(status, "UNREACHABLE", statuslen);
10591 } else if (peer->lastms > peer->maxms) {
10592 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
10593 res = 1;
10594 } else if (peer->lastms) {
10595 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
10596 res = 1;
10597 } else {
10598 ast_copy_string(status, "UNKNOWN", statuslen);
10599 }
10600 } else {
10601 ast_copy_string(status, "Unmonitored", statuslen);
10602
10603 res = -1;
10604 }
10605 return res;
10606 }
10607
10608
10609 static int sip_show_users(int fd, int argc, char *argv[])
10610 {
10611 regex_t regexbuf;
10612 int havepattern = FALSE;
10613
10614 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
10615
10616 switch (argc) {
10617 case 5:
10618 if (!strcasecmp(argv[3], "like")) {
10619 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
10620 return RESULT_SHOWUSAGE;
10621 havepattern = TRUE;
10622 } else
10623 return RESULT_SHOWUSAGE;
10624 case 3:
10625 break;
10626 default:
10627 return RESULT_SHOWUSAGE;
10628 }
10629
10630 ast_cli(fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
10631 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
10632 ASTOBJ_RDLOCK(iterator);
10633
10634 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
10635 ASTOBJ_UNLOCK(iterator);
10636 continue;
10637 }
10638
10639 ast_cli(fd, FORMAT, iterator->name,
10640 iterator->secret,
10641 iterator->accountcode,
10642 iterator->context,
10643 iterator->ha ? "Yes" : "No",
10644 nat2str(ast_test_flag(&iterator->flags[0], SIP_NAT)));
10645 ASTOBJ_UNLOCK(iterator);
10646 } while (0)
10647 );
10648
10649 if (havepattern)
10650 regfree(®exbuf);
10651
10652 return RESULT_SUCCESS;
10653 #undef FORMAT
10654 }
10655
10656 static char mandescr_show_peers[] =
10657 "Description: Lists SIP peers in text format with details on current status.\n"
10658 "Variables: \n"
10659 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
10660
10661
10662
10663 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
10664 {
10665 const char *id = astman_get_header(m,"ActionID");
10666 const char *a[] = {"sip", "show", "peers"};
10667 char idtext[256] = "";
10668 int total = 0;
10669
10670 if (!ast_strlen_zero(id))
10671 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
10672
10673 astman_send_ack(s, m, "Peer status list will follow");
10674
10675 _sip_show_peers(-1, &total, s, m, 3, a);
10676
10677 astman_append(s,
10678 "Event: PeerlistComplete\r\n"
10679 "ListItems: %d\r\n"
10680 "%s"
10681 "\r\n", total, idtext);
10682 return 0;
10683 }
10684
10685
10686 static int sip_show_peers(int fd, int argc, char *argv[])
10687 {
10688 return _sip_show_peers(fd, NULL, NULL, NULL, argc, (const char **) argv);
10689 }
10690
10691
10692 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
10693 {
10694 regex_t regexbuf;
10695 int havepattern = FALSE;
10696
10697 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s %-10s\n"
10698 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s %-10s\n"
10699
10700 char name[256];
10701 int total_peers = 0;
10702 int peers_mon_online = 0;
10703 int peers_mon_offline = 0;
10704 int peers_unmon_offline = 0;
10705 int peers_unmon_online = 0;
10706 const char *id;
10707 char idtext[256] = "";
10708 int realtimepeers;
10709
10710 realtimepeers = ast_check_realtime("sippeers");
10711
10712 if (s) {
10713 id = astman_get_header(m,"ActionID");
10714 if (!ast_strlen_zero(id))
10715 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
10716 }
10717
10718 switch (argc) {
10719 case 5:
10720 if (!strcasecmp(argv[3], "like")) {
10721 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
10722 return RESULT_SHOWUSAGE;
10723 havepattern = TRUE;
10724 } else
10725 return RESULT_SHOWUSAGE;
10726 case 3:
10727 break;
10728 default:
10729 return RESULT_SHOWUSAGE;
10730 }
10731
10732 if (!s)
10733 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
10734
10735 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
10736 char status[20] = "";
10737 char srch[2000];
10738 char pstatus;
10739
10740 ASTOBJ_RDLOCK(iterator);
10741
10742 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
10743 ASTOBJ_UNLOCK(iterator);
10744 continue;
10745 }
10746
10747 if (!ast_strlen_zero(iterator->username) && !s)
10748 snprintf(name, sizeof(name), "%s/%s", iterator->name, iterator->username);
10749 else
10750 ast_copy_string(name, iterator->name, sizeof(name));
10751
10752 pstatus = peer_status(iterator, status, sizeof(status));
10753 if (pstatus == 1)
10754 peers_mon_online++;
10755 else if (pstatus == 0)
10756 peers_mon_offline++;
10757 else {
10758 if (iterator->addr.sin_port == 0)
10759 peers_unmon_offline++;
10760 else
10761 peers_unmon_online++;
10762 }
10763
10764 snprintf(srch, sizeof(srch), FORMAT, name,
10765 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
10766 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
10767 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
10768 iterator->ha ? " A " : " ",
10769 ntohs(iterator->addr.sin_port), status,
10770 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
10771
10772 if (!s) {
10773 ast_cli(fd, FORMAT, name,
10774 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
10775 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
10776 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
10777 iterator->ha ? " A " : " ",
10778
10779 ntohs(iterator->addr.sin_port), status,
10780 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
10781 } else {
10782
10783 astman_append(s,
10784 "Event: PeerEntry\r\n%s"
10785 "Channeltype: SIP\r\n"
10786 "ObjectName: %s\r\n"
10787 "ChanObjectType: peer\r\n"
10788 "IPaddress: %s\r\n"
10789 "IPport: %d\r\n"
10790 "Dynamic: %s\r\n"
10791 "Natsupport: %s\r\n"
10792 "VideoSupport: %s\r\n"
10793 "ACL: %s\r\n"
10794 "Status: %s\r\n"
10795 "RealtimeDevice: %s\r\n\r\n",
10796 idtext,
10797 iterator->name,
10798 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "-none-",
10799 ntohs(iterator->addr.sin_port),
10800 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no",
10801 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? "yes" : "no",
10802 ast_test_flag(&iterator->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
10803 iterator->ha ? "yes" : "no",
10804 status,
10805 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "yes":"no") : "no");
10806 }
10807
10808 ASTOBJ_UNLOCK(iterator);
10809
10810 total_peers++;
10811 } while(0) );
10812
10813 if (!s)
10814 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
10815 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
10816
10817 if (havepattern)
10818 regfree(®exbuf);
10819
10820 if (total)
10821 *total = total_peers;
10822
10823
10824 return RESULT_SUCCESS;
10825 #undef FORMAT
10826 #undef FORMAT2
10827 }
10828
10829
10830 static int sip_show_objects(int fd, int argc, char *argv[])
10831 {
10832 char tmp[256];
10833 if (argc != 3)
10834 return RESULT_SHOWUSAGE;
10835 ast_cli(fd, "-= User objects: %d static, %d realtime =-\n\n", suserobjs, ruserobjs);
10836 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &userl);
10837 ast_cli(fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
10838 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &peerl);
10839 ast_cli(fd, "-= Registry objects: %d =-\n\n", regobjs);
10840 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), ®l);
10841 return RESULT_SUCCESS;
10842 }
10843
10844 static void print_group(int fd, ast_group_t group, int crlf)
10845 {
10846 char buf[256];
10847 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
10848 }
10849
10850
10851 static const char *dtmfmode2str(int mode)
10852 {
10853 switch (mode) {
10854 case SIP_DTMF_RFC2833:
10855 return "rfc2833";
10856 case SIP_DTMF_INFO:
10857 return "info";
10858 case SIP_DTMF_INBAND:
10859 return "inband";
10860 case SIP_DTMF_AUTO:
10861 return "auto";
10862 }
10863 return "<error>";
10864 }
10865
10866
10867 static const char *insecure2str(int port, int invite)
10868 {
10869 if (port && invite)
10870 return "port,invite";
10871 else if (port)
10872 return "port";
10873 else if (invite)
10874 return "invite";
10875 else
10876 return "no";
10877 }
10878
10879
10880
10881
10882 static void cleanup_stale_contexts(char *new, char *old)
10883 {
10884 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
10885
10886 while ((oldcontext = strsep(&old, "&"))) {
10887 stalecontext = '\0';
10888 ast_copy_string(newlist, new, sizeof(newlist));
10889 stringp = newlist;
10890 while ((newcontext = strsep(&stringp, "&"))) {
10891 if (strcmp(newcontext, oldcontext) == 0) {
10892
10893 stalecontext = '\0';
10894 break;
10895 } else if (strcmp(newcontext, oldcontext)) {
10896 stalecontext = oldcontext;
10897 }
10898
10899 }
10900 if (stalecontext)
10901 ast_context_destroy(ast_context_find(stalecontext), "SIP");
10902 }
10903 }
10904
10905
10906 static int sip_prune_realtime(int fd, int argc, char *argv[])
10907 {
10908 struct sip_peer *peer;
10909 struct sip_user *user;
10910 int pruneuser = FALSE;
10911 int prunepeer = FALSE;
10912 int multi = FALSE;
10913 char *name = NULL;
10914 regex_t regexbuf;
10915
10916 switch (argc) {
10917 case 4:
10918 if (!strcasecmp(argv[3], "user"))
10919 return RESULT_SHOWUSAGE;
10920 if (!strcasecmp(argv[3], "peer"))
10921 return RESULT_SHOWUSAGE;
10922 if (!strcasecmp(argv[3], "like"))
10923 return RESULT_SHOWUSAGE;
10924 if (!strcasecmp(argv[3], "all")) {
10925 multi = TRUE;
10926 pruneuser = prunepeer = TRUE;
10927 } else {
10928 pruneuser = prunepeer = TRUE;
10929 name = argv[3];
10930 }
10931 break;
10932 case 5:
10933 if (!strcasecmp(argv[4], "like"))
10934 return RESULT_SHOWUSAGE;
10935 if (!strcasecmp(argv[3], "all"))
10936 return RESULT_SHOWUSAGE;
10937 if (!strcasecmp(argv[3], "like")) {
10938 multi = TRUE;
10939 name = argv[4];
10940 pruneuser = prunepeer = TRUE;
10941 } else if (!strcasecmp(argv[3], "user")) {
10942 pruneuser = TRUE;
10943 if (!strcasecmp(argv[4], "all"))
10944 multi = TRUE;
10945 else
10946 name = argv[4];
10947 } else if (!strcasecmp(argv[3], "peer")) {
10948 prunepeer = TRUE;
10949 if (!strcasecmp(argv[4], "all"))
10950 multi = TRUE;
10951 else
10952 name = argv[4];
10953 } else
10954 return RESULT_SHOWUSAGE;
10955 break;
10956 case 6:
10957 if (strcasecmp(argv[4], "like"))
10958 return RESULT_SHOWUSAGE;
10959 if (!strcasecmp(argv[3], "user")) {
10960 pruneuser = TRUE;
10961 name = argv[5];
10962 } else if (!strcasecmp(argv[3], "peer")) {
10963 prunepeer = TRUE;
10964 name = argv[5];
10965 } else
10966 return RESULT_SHOWUSAGE;
10967 break;
10968 default:
10969 return RESULT_SHOWUSAGE;
10970 }
10971
10972 if (multi && name) {
10973 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
10974 return RESULT_SHOWUSAGE;
10975 }
10976
10977 if (multi) {
10978 if (prunepeer) {
10979 int pruned = 0;
10980
10981 ASTOBJ_CONTAINER_WRLOCK(&peerl);
10982 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
10983 ASTOBJ_RDLOCK(iterator);
10984 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
10985 ASTOBJ_UNLOCK(iterator);
10986 continue;
10987 };
10988 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
10989 ASTOBJ_MARK(iterator);
10990 pruned++;
10991 }
10992 ASTOBJ_UNLOCK(iterator);
10993 } while (0) );
10994 if (pruned) {
10995 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
10996 ast_cli(fd, "%d peers pruned.\n", pruned);
10997 } else
10998 ast_cli(fd, "No peers found to prune.\n");
10999 ASTOBJ_CONTAINER_UNLOCK(&peerl);
11000 }
11001 if (pruneuser) {
11002 int pruned = 0;
11003
11004 ASTOBJ_CONTAINER_WRLOCK(&userl);
11005 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
11006 ASTOBJ_RDLOCK(iterator);
11007 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
11008 ASTOBJ_UNLOCK(iterator);
11009 continue;
11010 };
11011 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11012 ASTOBJ_MARK(iterator);
11013 pruned++;
11014 }
11015 ASTOBJ_UNLOCK(iterator);
11016 } while (0) );
11017 if (pruned) {
11018 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
11019 ast_cli(fd, "%d users pruned.\n", pruned);
11020 } else
11021 ast_cli(fd, "No users found to prune.\n");
11022 ASTOBJ_CONTAINER_UNLOCK(&userl);
11023 }
11024 } else {
11025 if (prunepeer) {
11026 if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
11027 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11028 ast_cli(fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
11029 ASTOBJ_CONTAINER_LINK(&peerl, peer);
11030 } else
11031 ast_cli(fd, "Peer '%s' pruned.\n", name);
11032 ASTOBJ_UNREF(peer, sip_destroy_peer);
11033 } else
11034 ast_cli(fd, "Peer '%s' not found.\n", name);
11035 }
11036 if (pruneuser) {
11037 if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
11038 if (!ast_test_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11039 ast_cli(fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
11040 ASTOBJ_CONTAINER_LINK(&userl, user);
11041 } else
11042 ast_cli(fd, "User '%s' pruned.\n", name);
11043 ASTOBJ_UNREF(user, sip_destroy_user);
11044 } else
11045 ast_cli(fd, "User '%s' not found.\n", name);
11046 }
11047 }
11048
11049 return RESULT_SUCCESS;
11050 }
11051
11052
11053 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
11054 {
11055 int x, codec;
11056
11057 for(x = 0; x < 32 ; x++) {
11058 codec = ast_codec_pref_index(pref, x);
11059 if (!codec)
11060 break;
11061 ast_cli(fd, "%s", ast_getformatname(codec));
11062 ast_cli(fd, ":%d", pref->framing[x]);
11063 if (x < 31 && ast_codec_pref_index(pref, x + 1))
11064 ast_cli(fd, ",");
11065 }
11066 if (!x)
11067 ast_cli(fd, "none");
11068 }
11069
11070
11071 static const char *domain_mode_to_text(const enum domain_mode mode)
11072 {
11073 switch (mode) {
11074 case SIP_DOMAIN_AUTO:
11075 return "[Automatic]";
11076 case SIP_DOMAIN_CONFIG:
11077 return "[Configured]";
11078 }
11079
11080 return "";
11081 }
11082
11083
11084 static int sip_show_domains(int fd, int argc, char *argv[])
11085 {
11086 struct domain *d;
11087 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
11088
11089 if (AST_LIST_EMPTY(&domain_list)) {
11090 ast_cli(fd, "SIP Domain support not enabled.\n\n");
11091 return RESULT_SUCCESS;
11092 } else {
11093 ast_cli(fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
11094 AST_LIST_LOCK(&domain_list);
11095 AST_LIST_TRAVERSE(&domain_list, d, list)
11096 ast_cli(fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
11097 domain_mode_to_text(d->mode));
11098 AST_LIST_UNLOCK(&domain_list);
11099 ast_cli(fd, "\n");
11100 return RESULT_SUCCESS;
11101 }
11102 }
11103 #undef FORMAT
11104
11105 static char mandescr_show_peer[] =
11106 "Description: Show one SIP peer with details on current status.\n"
11107 "Variables: \n"
11108 " Peer: <name> The peer name you want to check.\n"
11109 " ActionID: <id> Optional action ID for this AMI transaction.\n";
11110
11111
11112 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
11113 {
11114 const char *a[4];
11115 const char *peer;
11116 int ret;
11117
11118 peer = astman_get_header(m,"Peer");
11119 if (ast_strlen_zero(peer)) {
11120 astman_send_error(s, m, "Peer: <name> missing.");
11121 return 0;
11122 }
11123 a[0] = "sip";
11124 a[1] = "show";
11125 a[2] = "peer";
11126 a[3] = peer;
11127
11128 ret = _sip_show_peer(1, -1, s, m, 4, a);
11129 astman_append(s, "\r\n\r\n" );
11130 return ret;
11131 }
11132
11133
11134
11135
11136 static int sip_show_peer(int fd, int argc, char *argv[])
11137 {
11138 return _sip_show_peer(0, fd, NULL, NULL, argc, (const char **) argv);
11139 }
11140
11141
11142 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
11143 {
11144 char status[30] = "";
11145 char cbuf[256];
11146 struct sip_peer *peer;
11147 char codec_buf[512];
11148 struct ast_codec_pref *pref;
11149 struct ast_variable *v;
11150 struct sip_auth *auth;
11151 int x = 0, codec = 0, load_realtime;
11152 int realtimepeers;
11153
11154 realtimepeers = ast_check_realtime("sippeers");
11155
11156 if (argc < 4)
11157 return RESULT_SHOWUSAGE;
11158
11159 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
11160 peer = find_peer(argv[3], NULL, load_realtime, 0);
11161 if (s) {
11162 if (peer) {
11163 const char *id = astman_get_header(m,"ActionID");
11164
11165 astman_append(s, "Response: Success\r\n");
11166 if (!ast_strlen_zero(id))
11167 astman_append(s, "ActionID: %s\r\n",id);
11168 } else {
11169 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.", argv[3]);
11170 astman_send_error(s, m, cbuf);
11171 return 0;
11172 }
11173 }
11174 if (peer && type==0 ) {
11175 ast_cli(fd,"\n\n");
11176 ast_cli(fd, " * Name : %s\n", peer->name);
11177 if (realtimepeers) {
11178 ast_cli(fd, " Realtime peer: %s\n", ast_test_flag(&peer->flags[0], SIP_REALTIME) ? "Yes, cached" : "No");
11179 }
11180 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
11181 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
11182 for (auth = peer->auth; auth; auth = auth->next) {
11183 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
11184 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
11185 }
11186 ast_cli(fd, " Context : %s\n", peer->context);
11187 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
11188 ast_cli(fd, " Language : %s\n", peer->language);
11189 if (!ast_strlen_zero(peer->accountcode))
11190 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
11191 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
11192 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
11193 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
11194 if (!ast_strlen_zero(peer->fromuser))
11195 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
11196 if (!ast_strlen_zero(peer->fromdomain))
11197 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
11198 ast_cli(fd, " Callgroup : ");
11199 print_group(fd, peer->callgroup, 0);
11200 ast_cli(fd, " Pickupgroup : ");
11201 print_group(fd, peer->pickupgroup, 0);
11202 ast_cli(fd, " Mailbox : %s\n", peer->mailbox);
11203 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
11204 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
11205 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
11206 ast_cli(fd, " Dynamic : %s\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Yes":"No"));
11207 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
11208 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
11209 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
11210 ast_cli(fd, " Insecure : %s\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE_PORT), ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)));
11211 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
11212 ast_cli(fd, " ACL : %s\n", (peer->ha?"Yes":"No"));
11213 ast_cli(fd, " T38 pt UDPTL : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)?"Yes":"No");
11214 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
11215 ast_cli(fd, " T38 pt RTP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_RTP)?"Yes":"No");
11216 ast_cli(fd, " T38 pt TCP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_TCP)?"Yes":"No");
11217 #endif
11218 ast_cli(fd, " CanReinvite : %s\n", ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Yes":"No");
11219 ast_cli(fd, " PromiscRedir : %s\n", ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Yes":"No");
11220 ast_cli(fd, " User=Phone : %s\n", ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Yes":"No");
11221 ast_cli(fd, " Video Support: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Yes":"No");
11222 ast_cli(fd, " Trust RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_TRUSTRPID) ? "Yes" : "No");
11223 ast_cli(fd, " Send RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_SENDRPID) ? "Yes" : "No");
11224 ast_cli(fd, " Subscriptions: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
11225 ast_cli(fd, " Overlap dial : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
11226
11227
11228 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
11229 ast_cli(fd, " LastMsg : %d\n", peer->lastmsg);
11230 ast_cli(fd, " ToHost : %s\n", peer->tohost);
11231 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));
11232 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
11233 if (!ast_strlen_zero(global_regcontext))
11234 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
11235 ast_cli(fd, " Def. Username: %s\n", peer->username);
11236 ast_cli(fd, " SIP Options : ");
11237 if (peer->sipoptions) {
11238 int lastoption = -1;
11239 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
11240 if (sip_options[x].id != lastoption) {
11241 if (peer->sipoptions & sip_options[x].id)
11242 ast_cli(fd, "%s ", sip_options[x].text);
11243 lastoption = x;
11244 }
11245 }
11246 } else
11247 ast_cli(fd, "(none)");
11248
11249 ast_cli(fd, "\n");
11250 ast_cli(fd, " Codecs : ");
11251 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
11252 ast_cli(fd, "%s\n", codec_buf);
11253 ast_cli(fd, " Codec Order : (");
11254 print_codec_to_cli(fd, &peer->prefs);
11255 ast_cli(fd, ")\n");
11256
11257 ast_cli(fd, " Auto-Framing: %s \n", peer->autoframing ? "Yes" : "No");
11258 ast_cli(fd, " Status : ");
11259 peer_status(peer, status, sizeof(status));
11260 ast_cli(fd, "%s\n",status);
11261 ast_cli(fd, " Useragent : %s\n", peer->useragent);
11262 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
11263 if (peer->chanvars) {
11264 ast_cli(fd, " Variables :\n");
11265 for (v = peer->chanvars ; v ; v = v->next)
11266 ast_cli(fd, " %s = %s\n", v->name, v->value);
11267 }
11268 ast_cli(fd,"\n");
11269 ASTOBJ_UNREF(peer,sip_destroy_peer);
11270 } else if (peer && type == 1) {
11271 char buf[256];
11272 astman_append(s, "Channeltype: SIP\r\n");
11273 astman_append(s, "ObjectName: %s\r\n", peer->name);
11274 astman_append(s, "ChanObjectType: peer\r\n");
11275 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
11276 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
11277 astman_append(s, "Context: %s\r\n", peer->context);
11278 astman_append(s, "Language: %s\r\n", peer->language);
11279 if (!ast_strlen_zero(peer->accountcode))
11280 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
11281 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
11282 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
11283 if (!ast_strlen_zero(peer->fromuser))
11284 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
11285 if (!ast_strlen_zero(peer->fromdomain))
11286 astman_append(s, "SIP-FromDomain: %s\r\n", peer->fromdomain);
11287 astman_append(s, "Callgroup: ");
11288 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->callgroup));
11289 astman_append(s, "Pickupgroup: ");
11290 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->pickupgroup));
11291 astman_append(s, "VoiceMailbox: %s\r\n", peer->mailbox);
11292 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
11293 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
11294 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
11295 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
11296 astman_append(s, "Dynamic: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Y":"N"));
11297 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
11298 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched,peer->expire));
11299 astman_append(s, "SIP-AuthInsecure: %s\r\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE_PORT), ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)));
11300 astman_append(s, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
11301 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
11302 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Y":"N"));
11303 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
11304 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
11305 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
11306
11307
11308 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
11309 astman_append(s, "SIPLastMsg: %d\r\n", peer->lastmsg);
11310 astman_append(s, "ToHost: %s\r\n", peer->tohost);
11311 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));
11312 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));
11313 astman_append(s, "Default-Username: %s\r\n", peer->username);
11314 if (!ast_strlen_zero(global_regcontext))
11315 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
11316 astman_append(s, "Codecs: ");
11317 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
11318 astman_append(s, "%s\r\n", codec_buf);
11319 astman_append(s, "CodecOrder: ");
11320 pref = &peer->prefs;
11321 for(x = 0; x < 32 ; x++) {
11322 codec = ast_codec_pref_index(pref,x);
11323 if (!codec)
11324 break;
11325 astman_append(s, "%s", ast_getformatname(codec));
11326 if (x < 31 && ast_codec_pref_index(pref,x+1))
11327 astman_append(s, ",");
11328 }
11329
11330 astman_append(s, "\r\n");
11331 astman_append(s, "Status: ");
11332 peer_status(peer, status, sizeof(status));
11333 astman_append(s, "%s\r\n", status);
11334 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
11335 astman_append(s, "Reg-Contact : %s\r\n", peer->fullcontact);
11336 if (peer->chanvars) {
11337 for (v = peer->chanvars ; v ; v = v->next) {
11338 astman_append(s, "ChanVariable:\n");
11339 astman_append(s, " %s,%s\r\n", v->name, v->value);
11340 }
11341 }
11342
11343 ASTOBJ_UNREF(peer,sip_destroy_peer);
11344
11345 } else {
11346 ast_cli(fd,"Peer %s not found.\n", argv[3]);
11347 ast_cli(fd,"\n");
11348 }
11349
11350 return RESULT_SUCCESS;
11351 }
11352
11353
11354 static int sip_show_user(int fd, int argc, char *argv[])
11355 {
11356 char cbuf[256];
11357 struct sip_user *user;
11358 struct ast_variable *v;
11359 int load_realtime;
11360
11361 if (argc < 4)
11362 return RESULT_SHOWUSAGE;
11363
11364
11365 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
11366
11367 user = find_user(argv[3], load_realtime);
11368 if (user) {
11369 ast_cli(fd,"\n\n");
11370 ast_cli(fd, " * Name : %s\n", user->name);
11371 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
11372 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
11373 ast_cli(fd, " Context : %s\n", user->context);
11374 ast_cli(fd, " Language : %s\n", user->language);
11375 if (!ast_strlen_zero(user->accountcode))
11376 ast_cli(fd, " Accountcode : %s\n", user->accountcode);
11377 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
11378 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
11379 ast_cli(fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
11380 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
11381 ast_cli(fd, " Call limit : %d\n", user->call_limit);
11382 ast_cli(fd, " Callgroup : ");
11383 print_group(fd, user->callgroup, 0);
11384 ast_cli(fd, " Pickupgroup : ");
11385 print_group(fd, user->pickupgroup, 0);
11386 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
11387 ast_cli(fd, " ACL : %s\n", (user->ha?"Yes":"No"));
11388 ast_cli(fd, " Codec Order : (");
11389 print_codec_to_cli(fd, &user->prefs);
11390 ast_cli(fd, ")\n");
11391
11392 ast_cli(fd, " Auto-Framing: %s \n", user->autoframing ? "Yes" : "No");
11393 if (user->chanvars) {
11394 ast_cli(fd, " Variables :\n");
11395 for (v = user->chanvars ; v ; v = v->next)
11396 ast_cli(fd, " %s = %s\n", v->name, v->value);
11397 }
11398 ast_cli(fd,"\n");
11399 ASTOBJ_UNREF(user,sip_destroy_user);
11400 } else {
11401 ast_cli(fd,"User %s not found.\n", argv[3]);
11402 ast_cli(fd,"\n");
11403 }
11404
11405 return RESULT_SUCCESS;
11406 }
11407
11408
11409 static int sip_show_registry(int fd, int argc, char *argv[])
11410 {
11411 #define FORMAT2 "%-30.30s %-12.12s %8.8s %-20.20s %-25.25s\n"
11412 #define FORMAT "%-30.30s %-12.12s %8d %-20.20s %-25.25s\n"
11413 char host[80];
11414 char tmpdat[256];
11415 struct tm tm;
11416
11417
11418 if (argc != 3)
11419 return RESULT_SHOWUSAGE;
11420 ast_cli(fd, FORMAT2, "Host", "Username", "Refresh", "State", "Reg.Time");
11421 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
11422 ASTOBJ_RDLOCK(iterator);
11423 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
11424 if (iterator->regtime) {
11425 ast_localtime(&iterator->regtime, &tm, NULL);
11426 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
11427 } else {
11428 tmpdat[0] = 0;
11429 }
11430 ast_cli(fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
11431 ASTOBJ_UNLOCK(iterator);
11432 } while(0));
11433 return RESULT_SUCCESS;
11434 #undef FORMAT
11435 #undef FORMAT2
11436 }
11437
11438
11439 static int sip_show_settings(int fd, int argc, char *argv[])
11440 {
11441 int realtimepeers;
11442 int realtimeusers;
11443 char codec_buf[SIPBUFSIZE];
11444
11445 realtimepeers = ast_check_realtime("sippeers");
11446 realtimeusers = ast_check_realtime("sipusers");
11447
11448 if (argc != 3)
11449 return RESULT_SHOWUSAGE;
11450 ast_cli(fd, "\n\nGlobal Settings:\n");
11451 ast_cli(fd, "----------------\n");
11452 ast_cli(fd, " SIP Port: %d\n", ntohs(bindaddr.sin_port));
11453 ast_cli(fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
11454 ast_cli(fd, " Videosupport: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "Yes" : "No");
11455 ast_cli(fd, " AutoCreatePeer: %s\n", autocreatepeer ? "Yes" : "No");
11456 ast_cli(fd, " Allow unknown access: %s\n", global_allowguest ? "Yes" : "No");
11457 ast_cli(fd, " Allow subscriptions: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
11458 ast_cli(fd, " Allow overlap dialing: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
11459 ast_cli(fd, " Promsic. redir: %s\n", ast_test_flag(&global_flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
11460 ast_cli(fd, " SIP domain support: %s\n", AST_LIST_EMPTY(&domain_list) ? "No" : "Yes");
11461 ast_cli(fd, " Call to non-local dom.: %s\n", allow_external_domains ? "Yes" : "No");
11462 ast_cli(fd, " URI user is phone no: %s\n", ast_test_flag(&global_flags[0], SIP_USEREQPHONE) ? "Yes" : "No");
11463 ast_cli(fd, " Our auth realm %s\n", global_realm);
11464 ast_cli(fd, " Realm. auth: %s\n", authl ? "Yes": "No");
11465 ast_cli(fd, " Always auth rejects: %s\n", global_alwaysauthreject ? "Yes" : "No");
11466 ast_cli(fd, " Call limit peers only: %s\n", global_limitonpeers ? "Yes" : "No");
11467 ast_cli(fd, " Direct RTP setup: %s\n", global_directrtpsetup ? "Yes" : "No");
11468 ast_cli(fd, " User Agent: %s\n", global_useragent);
11469 ast_cli(fd, " MWI checking interval: %d secs\n", global_mwitime);
11470 ast_cli(fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
11471 ast_cli(fd, " Caller ID: %s\n", default_callerid);
11472 ast_cli(fd, " From: Domain: %s\n", default_fromdomain);
11473 ast_cli(fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
11474 ast_cli(fd, " Call Events: %s\n", global_callevents ? "On" : "Off");
11475 ast_cli(fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
11476 ast_cli(fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
11477 ast_cli(fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
11478 ast_cli(fd, " T38 fax pt UDPTL: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL) ? "Yes" : "No");
11479 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
11480 ast_cli(fd, " T38 fax pt RTP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_RTP) ? "Yes" : "No");
11481 ast_cli(fd, " T38 fax pt TCP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_TCP) ? "Yes" : "No");
11482 #endif
11483 ast_cli(fd, " RFC2833 Compensation: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE) ? "Yes" : "No");
11484 if (!realtimepeers && !realtimeusers)
11485 ast_cli(fd, " SIP realtime: Disabled\n" );
11486 else
11487 ast_cli(fd, " SIP realtime: Enabled\n" );
11488
11489 ast_cli(fd, "\nGlobal Signalling Settings:\n");
11490 ast_cli(fd, "---------------------------\n");
11491 ast_cli(fd, " Codecs: ");
11492 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, global_capability);
11493 ast_cli(fd, "%s\n", codec_buf);
11494 ast_cli(fd, " Codec Order: ");
11495 print_codec_to_cli(fd, &default_prefs);
11496 ast_cli(fd, "\n");
11497 ast_cli(fd, " T1 minimum: %d\n", global_t1min);
11498 ast_cli(fd, " No premature media: %s\n", global_prematuremediafilter ? "Yes" : "No");
11499 ast_cli(fd, " Relax DTMF: %s\n", global_relaxdtmf ? "Yes" : "No");
11500 ast_cli(fd, " Compact SIP headers: %s\n", compactheaders ? "Yes" : "No");
11501 ast_cli(fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
11502 ast_cli(fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
11503 ast_cli(fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
11504 ast_cli(fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
11505 ast_cli(fd, " DNS SRV lookup: %s\n", srvlookup ? "Yes" : "No");
11506 ast_cli(fd, " Pedantic SIP support: %s\n", pedanticsipchecking ? "Yes" : "No");
11507 ast_cli(fd, " Reg. min duration %d secs\n", min_expiry);
11508 ast_cli(fd, " Reg. max duration: %d secs\n", max_expiry);
11509 ast_cli(fd, " Reg. default duration: %d secs\n", default_expiry);
11510 ast_cli(fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
11511 ast_cli(fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
11512 ast_cli(fd, " Notify ringing state: %s\n", global_notifyringing ? "Yes" : "No");
11513 ast_cli(fd, " Notify hold state: %s\n", global_notifyhold ? "Yes" : "No");
11514 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(global_allowtransfer));
11515 ast_cli(fd, " Max Call Bitrate: %d kbps\r\n", default_maxcallbitrate);
11516 ast_cli(fd, " Auto-Framing: %s \r\n", global_autoframing ? "Yes" : "No");
11517 ast_cli(fd, "\nDefault Settings:\n");
11518 ast_cli(fd, "-----------------\n");
11519 ast_cli(fd, " Context: %s\n", default_context);
11520 ast_cli(fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags[0], SIP_NAT)));
11521 ast_cli(fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
11522 ast_cli(fd, " Qualify: %d\n", default_qualify);
11523 ast_cli(fd, " Use ClientCode: %s\n", ast_test_flag(&global_flags[0], SIP_USECLIENTCODE) ? "Yes" : "No");
11524 ast_cli(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" );
11525 ast_cli(fd, " Language: %s\n", S_OR(default_language, "(Defaults to English)"));
11526 ast_cli(fd, " MOH Interpret: %s\n", default_mohinterpret);
11527 ast_cli(fd, " MOH Suggest: %s\n", default_mohsuggest);
11528 ast_cli(fd, " Voice Mail Extension: %s\n", default_vmexten);
11529
11530
11531 if (realtimepeers || realtimeusers) {
11532 ast_cli(fd, "\nRealtime SIP Settings:\n");
11533 ast_cli(fd, "----------------------\n");
11534 ast_cli(fd, " Realtime Peers: %s\n", realtimepeers ? "Yes" : "No");
11535 ast_cli(fd, " Realtime Users: %s\n", realtimeusers ? "Yes" : "No");
11536 ast_cli(fd, " Cache Friends: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? "Yes" : "No");
11537 ast_cli(fd, " Update: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) ? "Yes" : "No");
11538 ast_cli(fd, " Ignore Reg. Expire: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) ? "Yes" : "No");
11539 ast_cli(fd, " Save sys. name: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME) ? "Yes" : "No");
11540 ast_cli(fd, " Auto Clear: %d\n", global_rtautoclear);
11541 }
11542 ast_cli(fd, "\n----\n");
11543 return RESULT_SUCCESS;
11544 }
11545
11546
11547 static const char *subscription_type2str(enum subscriptiontype subtype)
11548 {
11549 int i;
11550
11551 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
11552 if (subscription_types[i].type == subtype) {
11553 return subscription_types[i].text;
11554 }
11555 }
11556 return subscription_types[0].text;
11557 }
11558
11559
11560 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
11561 {
11562 int i;
11563
11564 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
11565 if (subscription_types[i].type == subtype) {
11566 return &subscription_types[i];
11567 }
11568 }
11569 return &subscription_types[0];
11570 }
11571
11572
11573 static int sip_show_channels(int fd, int argc, char *argv[])
11574 {
11575 return __sip_show_channels(fd, argc, argv, 0);
11576 }
11577
11578
11579 static int sip_show_subscriptions(int fd, int argc, char *argv[])
11580 {
11581 return __sip_show_channels(fd, argc, argv, 1);
11582 }
11583
11584
11585 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions)
11586 {
11587 #define FORMAT3L "%-15.15s %-10.10s %-11.11s %-15.15s %-13.13s %-15.15s %-10.10s %6d\n"
11588 #define FORMAT3H "%-15.15s %-10.10s %-11.11s %-15.15s %-13.13s %-15.15s %-10.10s %-6s\n"
11589 #define FORMAT2 "%-15.15s %-10.10s %-11.11s %-11.11s %-15.15s %-7.7s %-15.15s\n"
11590 #define FORMAT "%-15.15s %-10.10s %-11.11s %5.5d/%5.5d %-15.15s %-3.3s %-3.3s %-15.15s %-10.10s\n"
11591 struct sip_pvt *cur;
11592 int numchans = 0;
11593 int usedchans = 0;
11594 char *referstatus = NULL;
11595
11596 if (argc != 3)
11597 return RESULT_SHOWUSAGE;
11598 ast_mutex_lock(&iflock);
11599 cur = iflist;
11600 if (!subscriptions)
11601 ast_cli(fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Seq (Tx/Rx)", "Format", "Hold", "Last Message");
11602 else
11603 ast_cli(fd, FORMAT3H, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox", "Expiry");
11604 for (; cur; cur = cur->next) {
11605 referstatus = "";
11606 if (cur->refer) {
11607 referstatus = referstatus2str(cur->refer->status);
11608 }
11609 if (cur->subscribed == NONE && !subscriptions) {
11610 char formatbuf[SIPBUFSIZE/2];
11611 ast_cli(fd, FORMAT, ast_inet_ntoa(cur->sa.sin_addr),
11612 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
11613 cur->callid,
11614 cur->ocseq, cur->icseq,
11615 ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
11616 ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD) ? "Yes" : "No",
11617 ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY) ? "(d)" : "",
11618 cur->lastmsg ,
11619 referstatus
11620 );
11621 numchans++;
11622 }
11623 if (cur->subscribed != NONE && subscriptions) {
11624 ast_cli(fd, FORMAT3L, ast_inet_ntoa(cur->sa.sin_addr),
11625 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
11626 cur->callid,
11627
11628 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
11629 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
11630 subscription_type2str(cur->subscribed),
11631 cur->subscribed == MWI_NOTIFICATION ? (cur->relatedpeer ? cur->relatedpeer->mailbox : "<none>") : "<none>",
11632 cur->expiry
11633 );
11634 numchans++;
11635 }
11636 if (cur->owner) {
11637 usedchans++;
11638 }
11639 }
11640 ast_mutex_unlock(&iflock);
11641 if (!subscriptions)
11642 ast_cli(fd, "%d active SIP dialog%s\n", numchans, (numchans != 1) ? "s" : "");
11643 else
11644 ast_cli(fd, "%d active SIP subscription%s\n", numchans, (numchans != 1) ? "s" : "");
11645 ast_cli(fd, "%d used SIP channel%s\n", usedchans, (usedchans != 1) ? "s" : "");
11646 return RESULT_SUCCESS;
11647 #undef FORMAT
11648 #undef FORMAT2
11649 #undef FORMAT3
11650 }
11651
11652
11653 static char *complete_sipch(const char *line, const char *word, int pos, int state)
11654 {
11655 int which=0;
11656 struct sip_pvt *cur;
11657 char *c = NULL;
11658 int wordlen = strlen(word);
11659
11660 if (pos != 3) {
11661 return NULL;
11662 }
11663
11664 ast_mutex_lock(&iflock);
11665 for (cur = iflist; cur; cur = cur->next) {
11666 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
11667 c = ast_strdup(cur->callid);
11668 break;
11669 }
11670 }
11671 ast_mutex_unlock(&iflock);
11672 return c;
11673 }
11674
11675
11676 static char *complete_sip_peer(const char *word, int state, int flags2)
11677 {
11678 char *result = NULL;
11679 int wordlen = strlen(word);
11680 int which = 0;
11681
11682 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
11683
11684 if (!strncasecmp(word, iterator->name, wordlen) &&
11685 (!flags2 || ast_test_flag(&iterator->flags[1], flags2)) &&
11686 ++which > state)
11687 result = ast_strdup(iterator->name);
11688 } while(0) );
11689 return result;
11690 }
11691
11692
11693 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
11694 {
11695 if (pos == 3)
11696 return complete_sip_peer(word, state, 0);
11697
11698 return NULL;
11699 }
11700
11701
11702 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state)
11703 {
11704 if (pos == 3)
11705 return complete_sip_peer(word, state, 0);
11706
11707 return NULL;
11708 }
11709
11710
11711 static char *complete_sip_user(const char *word, int state, int flags2)
11712 {
11713 char *result = NULL;
11714 int wordlen = strlen(word);
11715 int which = 0;
11716
11717 ASTOBJ_CONTAINER_TRAVERSE(&userl, !result, do {
11718
11719 if (!strncasecmp(word, iterator->name, wordlen)) {
11720 if (flags2 && !ast_test_flag(&iterator->flags[1], flags2))
11721 continue;
11722 if (++which > state) {
11723 result = ast_strdup(iterator->name);
11724 }
11725 }
11726 } while(0) );
11727 return result;
11728 }
11729
11730
11731 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
11732 {
11733 if (pos == 3)
11734 return complete_sip_user(word, state, 0);
11735
11736 return NULL;
11737 }
11738
11739
11740 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
11741 {
11742 char *c = NULL;
11743
11744 if (pos == 2) {
11745 int which = 0;
11746 char *cat = NULL;
11747 int wordlen = strlen(word);
11748
11749
11750
11751 if (!notify_types)
11752 return NULL;
11753
11754 while ( (cat = ast_category_browse(notify_types, cat)) ) {
11755 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
11756 c = ast_strdup(cat);
11757 break;
11758 }
11759 }
11760 return c;
11761 }
11762
11763 if (pos > 2)
11764 return complete_sip_peer(word, state, 0);
11765
11766 return NULL;
11767 }
11768
11769
11770 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state)
11771 {
11772 if (pos == 4)
11773 return complete_sip_peer(word, state, SIP_PAGE2_RTCACHEFRIENDS);
11774 return NULL;
11775 }
11776
11777
11778 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state)
11779 {
11780 if (pos == 4)
11781 return complete_sip_user(word, state, SIP_PAGE2_RTCACHEFRIENDS);
11782
11783 return NULL;
11784 }
11785
11786
11787 static int sip_show_channel(int fd, int argc, char *argv[])
11788 {
11789 struct sip_pvt *cur;
11790 size_t len;
11791 int found = 0;
11792
11793 if (argc != 4)
11794 return RESULT_SHOWUSAGE;
11795 len = strlen(argv[3]);
11796 ast_mutex_lock(&iflock);
11797 for (cur = iflist; cur; cur = cur->next) {
11798 if (!strncasecmp(cur->callid, argv[3], len)) {
11799 char formatbuf[SIPBUFSIZE/2];
11800 ast_cli(fd,"\n");
11801 if (cur->subscribed != NONE)
11802 ast_cli(fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
11803 else
11804 ast_cli(fd, " * SIP Call\n");
11805 ast_cli(fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
11806 ast_cli(fd, " Call-ID: %s\n", cur->callid);
11807 ast_cli(fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
11808 ast_cli(fd, " Our Codec Capability: %d\n", cur->capability);
11809 ast_cli(fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
11810 ast_cli(fd, " Their Codec Capability: %d\n", cur->peercapability);
11811 ast_cli(fd, " Joint Codec Capability: %d\n", cur->jointcapability);
11812 ast_cli(fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
11813 ast_cli(fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
11814 ast_cli(fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(cur->sa.sin_addr), ntohs(cur->sa.sin_port));
11815 ast_cli(fd, " Received Address: %s:%d\n", ast_inet_ntoa(cur->recv.sin_addr), ntohs(cur->recv.sin_port));
11816 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
11817 ast_cli(fd, " NAT Support: %s\n", nat2str(ast_test_flag(&cur->flags[0], SIP_NAT)));
11818 ast_cli(fd, " Audio IP: %s %s\n", ast_inet_ntoa(cur->redirip.sin_addr.s_addr ? cur->redirip.sin_addr : cur->ourip), cur->redirip.sin_addr.s_addr ? "(Outside bridge)" : "(local)" );
11819 ast_cli(fd, " Our Tag: %s\n", cur->tag);
11820 ast_cli(fd, " Their Tag: %s\n", cur->theirtag);
11821 ast_cli(fd, " SIP User agent: %s\n", cur->useragent);
11822 if (!ast_strlen_zero(cur->username))
11823 ast_cli(fd, " Username: %s\n", cur->username);
11824 if (!ast_strlen_zero(cur->peername))
11825 ast_cli(fd, " Peername: %s\n", cur->peername);
11826 if (!ast_strlen_zero(cur->uri))
11827 ast_cli(fd, " Original uri: %s\n", cur->uri);
11828 if (!ast_strlen_zero(cur->cid_num))
11829 ast_cli(fd, " Caller-ID: %s\n", cur->cid_num);
11830 ast_cli(fd, " Need Destroy: %d\n", ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY));
11831 ast_cli(fd, " Last Message: %s\n", cur->lastmsg);
11832 ast_cli(fd, " Promiscuous Redir: %s\n", ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
11833 ast_cli(fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
11834 ast_cli(fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
11835 ast_cli(fd, " SIP Options: ");
11836 if (cur->sipoptions) {
11837 int x;
11838 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
11839 if (cur->sipoptions & sip_options[x].id)
11840 ast_cli(fd, "%s ", sip_options[x].text);
11841 }
11842 } else
11843 ast_cli(fd, "(none)\n");
11844 ast_cli(fd, "\n\n");
11845 found++;
11846 }
11847 }
11848 ast_mutex_unlock(&iflock);
11849 if (!found)
11850 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
11851 return RESULT_SUCCESS;
11852 }
11853
11854
11855 static int sip_show_history(int fd, int argc, char *argv[])
11856 {
11857 struct sip_pvt *cur;
11858 size_t len;
11859 int found = 0;
11860
11861 if (argc != 4)
11862 return RESULT_SHOWUSAGE;
11863 if (!recordhistory)
11864 ast_cli(fd, "\n***Note: History recording is currently DISABLED. Use 'sip history' to ENABLE.\n");
11865 len = strlen(argv[3]);
11866 ast_mutex_lock(&iflock);
11867 for (cur = iflist; cur; cur = cur->next) {
11868 if (!strncasecmp(cur->callid, argv[3], len)) {
11869 struct sip_history *hist;
11870 int x = 0;
11871
11872 ast_cli(fd,"\n");
11873 if (cur->subscribed != NONE)
11874 ast_cli(fd, " * Subscription\n");
11875 else
11876 ast_cli(fd, " * SIP Call\n");
11877 if (cur->history)
11878 AST_LIST_TRAVERSE(cur->history, hist, list)
11879 ast_cli(fd, "%d. %s\n", ++x, hist->event);
11880 if (x == 0)
11881 ast_cli(fd, "Call '%s' has no history\n", cur->callid);
11882 found++;
11883 }
11884 }
11885 ast_mutex_unlock(&iflock);
11886 if (!found)
11887 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
11888 return RESULT_SUCCESS;
11889 }
11890
11891
11892 static void sip_dump_history(struct sip_pvt *dialog)
11893 {
11894 int x = 0;
11895 struct sip_history *hist;
11896 static int errmsg = 0;
11897
11898 if (!dialog)
11899 return;
11900
11901 if (!option_debug && !sipdebug) {
11902 if (!errmsg) {
11903 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
11904 errmsg = 1;
11905 }
11906 return;
11907 }
11908
11909 ast_log(LOG_DEBUG, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
11910 if (dialog->subscribed)
11911 ast_log(LOG_DEBUG, " * Subscription\n");
11912 else
11913 ast_log(LOG_DEBUG, " * SIP Call\n");
11914 if (dialog->history)
11915 AST_LIST_TRAVERSE(dialog->history, hist, list)
11916 ast_log(LOG_DEBUG, " %-3.3d. %s\n", ++x, hist->event);
11917 if (!x)
11918 ast_log(LOG_DEBUG, "Call '%s' has no history\n", dialog->callid);
11919 ast_log(LOG_DEBUG, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
11920 }
11921
11922
11923
11924
11925 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
11926 {
11927 char buf[1024];
11928 unsigned int event;
11929 const char *c = get_header(req, "Content-Type");
11930
11931
11932 if (!strcasecmp(c, "application/dtmf-relay") ||
11933 !strcasecmp(c, "application/DTMF") ||
11934 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
11935 unsigned int duration = 0;
11936
11937
11938 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
11939 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
11940 transmit_response(p, "200 OK", req);
11941 return;
11942 } else {
11943 ast_copy_string(buf, c, sizeof(buf));
11944 }
11945
11946 if (!ast_strlen_zero((c = get_body(req, "Duration"))))
11947 duration = atoi(c);
11948 if (!duration)
11949 duration = 100;
11950
11951 if (!p->owner) {
11952 transmit_response(p, "481 Call leg/transaction does not exist", req);
11953 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11954 return;
11955 }
11956
11957 if (ast_strlen_zero(buf)) {
11958 transmit_response(p, "200 OK", req);
11959 return;
11960 }
11961
11962 if (buf[0] == '*')
11963 event = 10;
11964 else if (buf[0] == '#')
11965 event = 11;
11966 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
11967 event = 12 + buf[0] - 'A';
11968 else
11969 event = atoi(buf);
11970 if (event == 16) {
11971
11972 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
11973 ast_queue_frame(p->owner, &f);
11974 if (sipdebug)
11975 ast_verbose("* DTMF-relay event received: FLASH\n");
11976 } else {
11977
11978 struct ast_frame f = { AST_FRAME_DTMF, };
11979 if (event < 10) {
11980 f.subclass = '0' + event;
11981 } else if (event < 11) {
11982 f.subclass = '*';
11983 } else if (event < 12) {
11984 f.subclass = '#';
11985 } else if (event < 16) {
11986 f.subclass = 'A' + (event - 12);
11987 }
11988 f.len = duration;
11989 ast_queue_frame(p->owner, &f);
11990 if (sipdebug)
11991 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
11992 }
11993 transmit_response(p, "200 OK", req);
11994 return;
11995 } else if (!strcasecmp(c, "application/media_control+xml")) {
11996
11997 if (p->owner)
11998 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
11999 transmit_response(p, "200 OK", req);
12000 return;
12001 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
12002
12003 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
12004 if (p->owner && p->owner->cdr)
12005 ast_cdr_setuserfield(p->owner, c);
12006 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
12007 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
12008 transmit_response(p, "200 OK", req);
12009 } else {
12010 transmit_response(p, "403 Unauthorized", req);
12011 }
12012 return;
12013 } else if (ast_strlen_zero(c = get_header(req, "Content-Length")) || !strcasecmp(c, "0")) {
12014
12015 transmit_response(p, "200 OK", req);
12016 return;
12017 }
12018
12019
12020
12021
12022
12023 if (!strcasecmp(get_header(req, "Content-Length"), "0")) {
12024 transmit_response(p, "200 OK", req);
12025 return;
12026 }
12027
12028 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
12029 transmit_response(p, "415 Unsupported media type", req);
12030 return;
12031 }
12032
12033
12034 static int sip_do_debug_ip(int fd, int argc, char *argv[])
12035 {
12036 struct hostent *hp;
12037 struct ast_hostent ahp;
12038 int port = 0;
12039 char *p, *arg;
12040
12041
12042 if (argc != 5)
12043 return RESULT_SHOWUSAGE;
12044 p = arg = argv[4];
12045 strsep(&p, ":");
12046 if (p)
12047 port = atoi(p);
12048 hp = ast_gethostbyname(arg, &ahp);
12049 if (hp == NULL)
12050 return RESULT_SHOWUSAGE;
12051
12052 debugaddr.sin_family = AF_INET;
12053 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
12054 debugaddr.sin_port = htons(port);
12055 if (port == 0)
12056 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(debugaddr.sin_addr));
12057 else
12058 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), port);
12059
12060 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12061
12062 return RESULT_SUCCESS;
12063 }
12064
12065
12066 static int sip_do_debug_peer(int fd, int argc, char *argv[])
12067 {
12068 struct sip_peer *peer;
12069 if (argc != 5)
12070 return RESULT_SHOWUSAGE;
12071 peer = find_peer(argv[4], NULL, 1, 0);
12072 if (peer) {
12073 if (peer->addr.sin_addr.s_addr) {
12074 debugaddr.sin_family = AF_INET;
12075 debugaddr.sin_addr = peer->addr.sin_addr;
12076 debugaddr.sin_port = peer->addr.sin_port;
12077 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
12078 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12079 } else
12080 ast_cli(fd, "Unable to get IP address of peer '%s'\n", argv[4]);
12081 ASTOBJ_UNREF(peer,sip_destroy_peer);
12082 } else
12083 ast_cli(fd, "No such peer '%s'\n", argv[4]);
12084 return RESULT_SUCCESS;
12085 }
12086
12087
12088 static int sip_do_debug(int fd, int argc, char *argv[])
12089 {
12090 int oldsipdebug = sipdebug_console;
12091 if (argc != 3) {
12092 if (argc != 5)
12093 return RESULT_SHOWUSAGE;
12094 else if (strcmp(argv[3], "ip") == 0)
12095 return sip_do_debug_ip(fd, argc, argv);
12096 else if (strcmp(argv[3], "peer") == 0)
12097 return sip_do_debug_peer(fd, argc, argv);
12098 else
12099 return RESULT_SHOWUSAGE;
12100 }
12101 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12102 memset(&debugaddr, 0, sizeof(debugaddr));
12103 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
12104 return RESULT_SUCCESS;
12105 }
12106
12107 static int sip_do_debug_deprecated(int fd, int argc, char *argv[])
12108 {
12109 int oldsipdebug = sipdebug_console;
12110 char *newargv[6] = { "sip", "set", "debug", NULL };
12111 if (argc != 2) {
12112 if (argc != 4)
12113 return RESULT_SHOWUSAGE;
12114 else if (strcmp(argv[2], "ip") == 0) {
12115 newargv[3] = argv[2];
12116 newargv[4] = argv[3];
12117 return sip_do_debug_ip(fd, argc + 1, newargv);
12118 } else if (strcmp(argv[2], "peer") == 0) {
12119 newargv[3] = argv[2];
12120 newargv[4] = argv[3];
12121 return sip_do_debug_peer(fd, argc + 1, newargv);
12122 } else
12123 return RESULT_SHOWUSAGE;
12124 }
12125 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12126 memset(&debugaddr, 0, sizeof(debugaddr));
12127 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
12128 return RESULT_SUCCESS;
12129 }
12130
12131
12132 static int sip_notify(int fd, int argc, char *argv[])
12133 {
12134 struct ast_variable *varlist;
12135 int i;
12136
12137 if (argc < 4)
12138 return RESULT_SHOWUSAGE;
12139
12140 if (!notify_types) {
12141 ast_cli(fd, "No %s file found, or no types listed there\n", notify_config);
12142 return RESULT_FAILURE;
12143 }
12144
12145 varlist = ast_variable_browse(notify_types, argv[2]);
12146
12147 if (!varlist) {
12148 ast_cli(fd, "Unable to find notify type '%s'\n", argv[2]);
12149 return RESULT_FAILURE;
12150 }
12151
12152 for (i = 3; i < argc; i++) {
12153 struct sip_pvt *p;
12154 struct sip_request req;
12155 struct ast_variable *var;
12156
12157 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY))) {
12158 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
12159 return RESULT_FAILURE;
12160 }
12161
12162 if (create_addr(p, argv[i], NULL)) {
12163
12164 sip_destroy(p);
12165 ast_cli(fd, "Could not create address for '%s'\n", argv[i]);
12166 continue;
12167 }
12168
12169 initreqprep(&req, p, SIP_NOTIFY);
12170
12171 for (var = varlist; var; var = var->next)
12172 add_header(&req, var->name, ast_unescape_semicolon(var->value));
12173
12174
12175 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
12176 p->ourip = __ourip;
12177 build_via(p);
12178 build_callid_pvt(p);
12179 ast_cli(fd, "Sending NOTIFY of type '%s' to '%s'\n", argv[2], argv[i]);
12180 transmit_sip_request(p, &req);
12181 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12182 }
12183
12184 return RESULT_SUCCESS;
12185 }
12186
12187
12188 static int sip_no_debug(int fd, int argc, char *argv[])
12189 {
12190 if (argc != 4)
12191 return RESULT_SHOWUSAGE;
12192 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12193 ast_cli(fd, "SIP Debugging Disabled\n");
12194 return RESULT_SUCCESS;
12195 }
12196
12197 static int sip_no_debug_deprecated(int fd, int argc, char *argv[])
12198 {
12199 if (argc != 3)
12200 return RESULT_SHOWUSAGE;
12201 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12202 ast_cli(fd, "SIP Debugging Disabled\n");
12203 return RESULT_SUCCESS;
12204 }
12205
12206
12207 static int sip_do_history(int fd, int argc, char *argv[])
12208 {
12209 if (argc != 2) {
12210 return RESULT_SHOWUSAGE;
12211 }
12212 recordhistory = TRUE;
12213 ast_cli(fd, "SIP History Recording Enabled (use 'sip show history')\n");
12214 return RESULT_SUCCESS;
12215 }
12216
12217
12218 static int sip_no_history(int fd, int argc, char *argv[])
12219 {
12220 if (argc != 3) {
12221 return RESULT_SHOWUSAGE;
12222 }
12223 recordhistory = FALSE;
12224 ast_cli(fd, "SIP History Recording Disabled\n");
12225 return RESULT_SUCCESS;
12226 }
12227
12228
12229 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader)
12230 {
12231 char digest[1024];
12232 p->authtries++;
12233 memset(digest,0,sizeof(digest));
12234 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
12235
12236
12237 if (sip_debug_test_pvt(p) && p->registry)
12238 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
12239
12240 return -1;
12241 }
12242 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
12243 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
12244 if (sip_debug_test_pvt(p) && p->registry)
12245 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
12246 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
12247 }
12248
12249
12250 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader, int sipmethod, int init)
12251 {
12252 char digest[1024];
12253
12254 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
12255 return -2;
12256
12257 p->authtries++;
12258 if (option_debug > 1)
12259 ast_log(LOG_DEBUG, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
12260 memset(digest, 0, sizeof(digest));
12261 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
12262
12263 return -1;
12264 }
12265
12266 p->options->auth = digest;
12267 p->options->authheader = respheader;
12268 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
12269 }
12270
12271
12272
12273
12274
12275 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
12276 {
12277 char tmp[512];
12278 char *c;
12279 char oldnonce[256];
12280
12281
12282 const struct x {
12283 const char *key;
12284 int field_index;
12285 } *i, keys[] = {
12286 { "realm=", ast_string_field_index(p, realm) },
12287 { "nonce=", ast_string_field_index(p, nonce) },
12288 { "opaque=", ast_string_field_index(p, opaque) },
12289 { "qop=", ast_string_field_index(p, qop) },
12290 { "domain=", ast_string_field_index(p, domain) },
12291 { NULL, 0 },
12292 };
12293
12294 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
12295 if (ast_strlen_zero(tmp))
12296 return -1;
12297 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
12298 ast_log(LOG_WARNING, "missing Digest.\n");
12299 return -1;
12300 }
12301 c = tmp + strlen("Digest ");
12302 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
12303 while (c && *(c = ast_skip_blanks(c))) {
12304 for (i = keys; i->key != NULL; i++) {
12305 char *src, *separator;
12306 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
12307 continue;
12308
12309 c += strlen(i->key);
12310 if (*c == '"') {
12311 src = ++c;
12312 separator = "\"";
12313 } else {
12314 src = c;
12315 separator = ",";
12316 }
12317 strsep(&c, separator);
12318 ast_string_field_index_set(p, i->field_index, src);
12319 break;
12320 }
12321 if (i->key == NULL)
12322 strsep(&c, ",");
12323 }
12324
12325 if (strcmp(p->nonce, oldnonce))
12326 p->noncecount = 0;
12327
12328
12329 if (p->registry) {
12330 struct sip_registry *r = p->registry;
12331
12332 if (strcmp(r->nonce, p->nonce)) {
12333 ast_string_field_set(r, realm, p->realm);
12334 ast_string_field_set(r, nonce, p->nonce);
12335 ast_string_field_set(r, domain, p->domain);
12336 ast_string_field_set(r, opaque, p->opaque);
12337 ast_string_field_set(r, qop, p->qop);
12338 r->noncecount = 0;
12339 }
12340 }
12341 return build_reply_digest(p, sipmethod, digest, digest_len);
12342 }
12343
12344
12345
12346
12347
12348
12349 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
12350 {
12351 char a1[256];
12352 char a2[256];
12353 char a1_hash[256];
12354 char a2_hash[256];
12355 char resp[256];
12356 char resp_hash[256];
12357 char uri[256];
12358 char opaque[256] = "";
12359 char cnonce[80];
12360 const char *username;
12361 const char *secret;
12362 const char *md5secret;
12363 struct sip_auth *auth = NULL;
12364
12365 if (!ast_strlen_zero(p->domain))
12366 ast_copy_string(uri, p->domain, sizeof(uri));
12367 else if (!ast_strlen_zero(p->uri))
12368 ast_copy_string(uri, p->uri, sizeof(uri));
12369 else
12370 snprintf(uri, sizeof(uri), "sip:%s@%s",p->username, ast_inet_ntoa(p->sa.sin_addr));
12371
12372 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
12373
12374
12375 if(!(auth = find_realm_authentication(p->peerauth, p->realm)))
12376 auth = find_realm_authentication(authl, p->realm);
12377
12378 if (auth) {
12379 if (sipdebug && option_debug > 1)
12380 ast_log(LOG_DEBUG, "use realm [%s] from peer [%s][%s]\n", auth->username, p->peername, p->username);
12381 username = auth->username;
12382 secret = auth->secret;
12383 md5secret = auth->md5secret;
12384 if (sipdebug)
12385 ast_log(LOG_DEBUG,"Using realm %s authentication for call %s\n", p->realm, p->callid);
12386 } else {
12387
12388 username = p->authname;
12389 secret = p->peersecret;
12390 md5secret = p->peermd5secret;
12391 }
12392 if (ast_strlen_zero(username))
12393 return -1;
12394
12395
12396 snprintf(a1,sizeof(a1),"%s:%s:%s", username, p->realm, secret);
12397 snprintf(a2,sizeof(a2),"%s:%s", sip_methods[method].text, uri);
12398 if (!ast_strlen_zero(md5secret))
12399 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
12400 else
12401 ast_md5_hash(a1_hash,a1);
12402 ast_md5_hash(a2_hash,a2);
12403
12404 p->noncecount++;
12405 if (!ast_strlen_zero(p->qop))
12406 snprintf(resp,sizeof(resp),"%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
12407 else
12408 snprintf(resp,sizeof(resp),"%s:%s:%s", a1_hash, p->nonce, a2_hash);
12409 ast_md5_hash(resp_hash, resp);
12410
12411
12412 if (!ast_strlen_zero(p->opaque)) {
12413 snprintf(opaque, sizeof(opaque), ", opaque=\"%s\"", p->opaque);
12414 }
12415
12416
12417 if (!ast_strlen_zero(p->qop))
12418 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);
12419 else
12420 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);
12421
12422 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
12423
12424 return 0;
12425 }
12426
12427 static char show_domains_usage[] =
12428 "Usage: sip show domains\n"
12429 " Lists all configured SIP local domains.\n"
12430 " Asterisk only responds to SIP messages to local domains.\n";
12431
12432 static char notify_usage[] =
12433 "Usage: sip notify <type> <peer> [<peer>...]\n"
12434 " Send a NOTIFY message to a SIP peer or peers\n"
12435 " Message types are defined in sip_notify.conf\n";
12436
12437 static char show_users_usage[] =
12438 "Usage: sip show users [like <pattern>]\n"
12439 " Lists all known SIP users.\n"
12440 " Optional regular expression pattern is used to filter the user list.\n";
12441
12442 static char show_user_usage[] =
12443 "Usage: sip show user <name> [load]\n"
12444 " Shows all details on one SIP user and the current status.\n"
12445 " Option \"load\" forces lookup of peer in realtime storage.\n";
12446
12447 static char show_inuse_usage[] =
12448 "Usage: sip show inuse [all]\n"
12449 " List all SIP users and peers usage counters and limits.\n"
12450 " Add option \"all\" to show all devices, not only those with a limit.\n";
12451
12452 static char show_channels_usage[] =
12453 "Usage: sip show channels\n"
12454 " Lists all currently active SIP channels.\n";
12455
12456 static char show_channel_usage[] =
12457 "Usage: sip show channel <channel>\n"
12458 " Provides detailed status on a given SIP channel.\n";
12459
12460 static char show_history_usage[] =
12461 "Usage: sip show history <channel>\n"
12462 " Provides detailed dialog history on a given SIP channel.\n";
12463
12464 static char show_peers_usage[] =
12465 "Usage: sip show peers [like <pattern>]\n"
12466 " Lists all known SIP peers.\n"
12467 " Optional regular expression pattern is used to filter the peer list.\n";
12468
12469 static char show_peer_usage[] =
12470 "Usage: sip show peer <name> [load]\n"
12471 " Shows all details on one SIP peer and the current status.\n"
12472 " Option \"load\" forces lookup of peer in realtime storage.\n";
12473
12474 static char prune_realtime_usage[] =
12475 "Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
12476 " Prunes object(s) from the cache.\n"
12477 " Optional regular expression pattern is used to filter the objects.\n";
12478
12479 static char show_reg_usage[] =
12480 "Usage: sip show registry\n"
12481 " Lists all registration requests and status.\n";
12482
12483 static char debug_usage[] =
12484 "Usage: sip set debug\n"
12485 " Enables dumping of SIP packets for debugging purposes\n\n"
12486 " sip set debug ip <host[:PORT]>\n"
12487 " Enables dumping of SIP packets to and from host.\n\n"
12488 " sip set debug peer <peername>\n"
12489 " Enables dumping of SIP packets to and from host.\n"
12490 " Require peer to be registered.\n";
12491
12492 static char no_debug_usage[] =
12493 "Usage: sip set debug off\n"
12494 " Disables dumping of SIP packets for debugging purposes\n";
12495
12496 static char no_history_usage[] =
12497 "Usage: sip history off\n"
12498 " Disables recording of SIP dialog history for debugging purposes\n";
12499
12500 static char history_usage[] =
12501 "Usage: sip history\n"
12502 " Enables recording of SIP dialog history for debugging purposes.\n"
12503 "Use 'sip show history' to view the history of a call number.\n";
12504
12505 static char sip_reload_usage[] =
12506 "Usage: sip reload\n"
12507 " Reloads SIP configuration from sip.conf\n";
12508
12509 static char show_subscriptions_usage[] =
12510 "Usage: sip show subscriptions\n"
12511 " Lists active SIP subscriptions for extension states\n";
12512
12513 static char show_objects_usage[] =
12514 "Usage: sip show objects\n"
12515 " Lists status of known SIP objects\n";
12516
12517 static char show_settings_usage[] =
12518 "Usage: sip show settings\n"
12519 " Provides detailed list of the configuration of the SIP channel.\n";
12520
12521
12522 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
12523 {
12524 struct sip_pvt *p;
12525 const char *content = NULL;
12526 AST_DECLARE_APP_ARGS(args,
12527 AST_APP_ARG(header);
12528 AST_APP_ARG(number);
12529 );
12530 int i, number, start = 0;
12531
12532 if (ast_strlen_zero(data)) {
12533 ast_log(LOG_WARNING, "This function requires a header name.\n");
12534 return -1;
12535 }
12536
12537 ast_channel_lock(chan);
12538 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
12539 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
12540 ast_channel_unlock(chan);
12541 return -1;
12542 }
12543
12544 AST_STANDARD_APP_ARGS(args, data);
12545 if (!args.number) {
12546 number = 1;
12547 } else {
12548 sscanf(args.number, "%30d", &number);
12549 if (number < 1)
12550 number = 1;
12551 }
12552
12553 p = chan->tech_pvt;
12554
12555
12556 if (!p) {
12557 ast_channel_unlock(chan);
12558 return -1;
12559 }
12560
12561 for (i = 0; i < number; i++)
12562 content = __get_header(&p->initreq, args.header, &start);
12563
12564 if (ast_strlen_zero(content)) {
12565 ast_channel_unlock(chan);
12566 return -1;
12567 }
12568
12569 ast_copy_string(buf, content, len);
12570 ast_channel_unlock(chan);
12571
12572 return 0;
12573 }
12574
12575 static struct ast_custom_function sip_header_function = {
12576 .name = "SIP_HEADER",
12577 .synopsis = "Gets the specified SIP header",
12578 .syntax = "SIP_HEADER(<name>[,<number>])",
12579 .desc = "Since there are several headers (such as Via) which can occur multiple\n"
12580 "times, SIP_HEADER takes an optional second argument to specify which header with\n"
12581 "that name to retrieve. Headers start at offset 1.\n",
12582 .read = func_header_read,
12583 };
12584
12585
12586 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
12587 {
12588 if (ast_strlen_zero(data)) {
12589 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
12590 return -1;
12591 }
12592 if (check_sip_domain(data, NULL, 0))
12593 ast_copy_string(buf, data, len);
12594 else
12595 buf[0] = '\0';
12596 return 0;
12597 }
12598
12599 static struct ast_custom_function checksipdomain_function = {
12600 .name = "CHECKSIPDOMAIN",
12601 .synopsis = "Checks if domain is a local domain",
12602 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
12603 .read = func_check_sipdomain,
12604 .desc = "This function checks if the domain in the argument is configured\n"
12605 "as a local SIP domain that this Asterisk server is configured to handle.\n"
12606 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
12607 "Check the domain= configuration in sip.conf\n",
12608 };
12609
12610
12611 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
12612 {
12613 struct sip_peer *peer;
12614 char *colname;
12615
12616 if ((colname = strchr(data, ':')))
12617 *colname++ = '\0';
12618 else if ((colname = strchr(data, '|')))
12619 *colname++ = '\0';
12620 else
12621 colname = "ip";
12622
12623 if (!(peer = find_peer(data, NULL, 1, 0)))
12624 return -1;
12625
12626 if (!strcasecmp(colname, "ip")) {
12627 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
12628 } else if (!strcasecmp(colname, "status")) {
12629 peer_status(peer, buf, len);
12630 } else if (!strcasecmp(colname, "language")) {
12631 ast_copy_string(buf, peer->language, len);
12632 } else if (!strcasecmp(colname, "regexten")) {
12633 ast_copy_string(buf, peer->regexten, len);
12634 } else if (!strcasecmp(colname, "limit")) {
12635 snprintf(buf, len, "%d", peer->call_limit);
12636 } else if (!strcasecmp(colname, "curcalls")) {
12637 snprintf(buf, len, "%d", peer->inUse);
12638 } else if (!strcasecmp(colname, "accountcode")) {
12639 ast_copy_string(buf, peer->accountcode, len);
12640 } else if (!strcasecmp(colname, "useragent")) {
12641 ast_copy_string(buf, peer->useragent, len);
12642 } else if (!strcasecmp(colname, "mailbox")) {
12643 ast_copy_string(buf, peer->mailbox, len);
12644 } else if (!strcasecmp(colname, "context")) {
12645 ast_copy_string(buf, peer->context, len);
12646 } else if (!strcasecmp(colname, "expire")) {
12647 snprintf(buf, len, "%d", peer->expire);
12648 } else if (!strcasecmp(colname, "dynamic")) {
12649 ast_copy_string(buf, (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no"), len);
12650 } else if (!strcasecmp(colname, "callerid_name")) {
12651 ast_copy_string(buf, peer->cid_name, len);
12652 } else if (!strcasecmp(colname, "callerid_num")) {
12653 ast_copy_string(buf, peer->cid_num, len);
12654 } else if (!strcasecmp(colname, "codecs")) {
12655 ast_getformatname_multiple(buf, len -1, peer->capability);
12656 } else if (!strncasecmp(colname, "codec[", 6)) {
12657 char *codecnum;
12658 int index = 0, codec = 0;
12659
12660 codecnum = colname + 6;
12661 codecnum = strsep(&codecnum, "]");
12662 index = atoi(codecnum);
12663 if((codec = ast_codec_pref_index(&peer->prefs, index))) {
12664 ast_copy_string(buf, ast_getformatname(codec), len);
12665 } else {
12666 buf[0] = '\0';
12667 }
12668 } else {
12669 buf[0] = '\0';
12670 }
12671
12672 ASTOBJ_UNREF(peer, sip_destroy_peer);
12673
12674 return 0;
12675 }
12676
12677
12678 struct ast_custom_function sippeer_function = {
12679 .name = "SIPPEER",
12680 .synopsis = "Gets SIP peer information",
12681 .syntax = "SIPPEER(<peername>[|item])",
12682 .read = function_sippeer,
12683 .desc = "Valid items are:\n"
12684 "- ip (default) The IP address.\n"
12685 "- mailbox The configured mailbox.\n"
12686 "- context The configured context.\n"
12687 "- expire The epoch time of the next expire.\n"
12688 "- dynamic Is it dynamic? (yes/no).\n"
12689 "- callerid_name The configured Caller ID name.\n"
12690 "- callerid_num The configured Caller ID number.\n"
12691 "- codecs The configured codecs.\n"
12692 "- status Status (if qualify=yes).\n"
12693 "- regexten Registration extension\n"
12694 "- limit Call limit (call-limit)\n"
12695 "- curcalls Current amount of calls \n"
12696 " Only available if call-limit is set\n"
12697 "- language Default language for peer\n"
12698 "- accountcode Account code for this peer\n"
12699 "- useragent Current user agent id for peer\n"
12700 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
12701 "\n"
12702 };
12703
12704
12705 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
12706 {
12707 struct sip_pvt *p;
12708
12709 *buf = 0;
12710
12711 if (!data) {
12712 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
12713 return -1;
12714 }
12715
12716 ast_channel_lock(chan);
12717 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
12718 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
12719 ast_channel_unlock(chan);
12720 return -1;
12721 }
12722
12723 p = chan->tech_pvt;
12724
12725
12726 if (!p) {
12727 ast_channel_unlock(chan);
12728 return -1;
12729 }
12730
12731 if (!strcasecmp(data, "peerip")) {
12732 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", len);
12733 } else if (!strcasecmp(data, "recvip")) {
12734 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", len);
12735 } else if (!strcasecmp(data, "from")) {
12736 ast_copy_string(buf, p->from, len);
12737 } else if (!strcasecmp(data, "uri")) {
12738 ast_copy_string(buf, p->uri, len);
12739 } else if (!strcasecmp(data, "useragent")) {
12740 ast_copy_string(buf, p->useragent, len);
12741 } else if (!strcasecmp(data, "peername")) {
12742 ast_copy_string(buf, p->peername, len);
12743 } else if (!strcasecmp(data, "t38passthrough")) {
12744 if (p->t38.state == T38_DISABLED)
12745 ast_copy_string(buf, "0", sizeof("0"));
12746 else
12747 ast_copy_string(buf, "1", sizeof("1"));
12748 } else {
12749 ast_channel_unlock(chan);
12750 return -1;
12751 }
12752 ast_channel_unlock(chan);
12753
12754 return 0;
12755 }
12756
12757
12758 static struct ast_custom_function sipchaninfo_function = {
12759 .name = "SIPCHANINFO",
12760 .synopsis = "Gets the specified SIP parameter from the current channel",
12761 .syntax = "SIPCHANINFO(item)",
12762 .read = function_sipchaninfo_read,
12763 .desc = "Valid items are:\n"
12764 "- peerip The IP address of the peer.\n"
12765 "- recvip The source IP address of the peer.\n"
12766 "- from The URI from the From: header.\n"
12767 "- uri The URI from the Contact: header.\n"
12768 "- useragent The useragent.\n"
12769 "- peername The name of the peer.\n"
12770 "- t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
12771 };
12772
12773
12774 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
12775 {
12776 char tmp[SIPBUFSIZE];
12777 char *s, *e, *uri, *t;
12778 char *domain;
12779
12780 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
12781 if ((t = strchr(tmp, ',')))
12782 *t = '\0';
12783 s = get_in_brackets(tmp);
12784 uri = ast_strdupa(s);
12785 if (ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
12786 if (!strncasecmp(s, "sip:", 4))
12787 s += 4;
12788 e = strchr(s, ';');
12789 if (e)
12790 *e = '\0';
12791 if (option_debug)
12792 ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
12793 if (p->owner)
12794 ast_string_field_build(p->owner, call_forward, "SIP/%s", s);
12795 } else {
12796 e = strchr(tmp, '@');
12797 if (e) {
12798 *e++ = '\0';
12799 domain = e;
12800 } else {
12801
12802 domain = tmp;
12803 }
12804 e = strchr(s, ';');
12805 if (e)
12806 *e = '\0';
12807 e = strchr(domain, ';');
12808 if (e)
12809 *e = '\0';
12810
12811 if (!strncasecmp(s, "sip:", 4))
12812 s += 4;
12813 if (option_debug > 1)
12814 ast_log(LOG_DEBUG, "Received 302 Redirect to extension '%s' (domain %s)\n", s, domain);
12815 if (p->owner) {
12816 pbx_builtin_setvar_helper(p->owner, "SIPREDIRECTURI", uri);
12817 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
12818 ast_string_field_set(p->owner, call_forward, s);
12819 }
12820 }
12821 }
12822
12823
12824 static void check_pendings(struct sip_pvt *p)
12825 {
12826 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
12827
12828 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA)
12829 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
12830
12831
12832 else {
12833
12834
12835 if (p->pendinginvite)
12836 return;
12837
12838
12839 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
12840 }
12841 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
12842 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12843 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
12844
12845 if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
12846 if (option_debug)
12847 ast_log(LOG_DEBUG, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
12848 } else {
12849 if (option_debug)
12850 ast_log(LOG_DEBUG, "Sending pending reinvite on '%s'\n", p->callid);
12851
12852 transmit_reinvite_with_sdp(p);
12853 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
12854 }
12855 }
12856 }
12857
12858
12859
12860
12861
12862 static int sip_reinvite_retry(const void *data)
12863 {
12864 struct sip_pvt *p = (struct sip_pvt *) data;
12865
12866 ast_mutex_lock(&p->lock);
12867 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
12868 p->waitid = -1;
12869 check_pendings(p);
12870 ast_mutex_unlock(&p->lock);
12871 return 0;
12872 }
12873
12874
12875
12876 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
12877 {
12878 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
12879 int res = 0;
12880 int xmitres = 0;
12881 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
12882 struct ast_channel *bridgepeer = NULL;
12883
12884 if (option_debug > 3) {
12885 if (reinvite)
12886 ast_log(LOG_DEBUG, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
12887 else
12888 ast_log(LOG_DEBUG, "SIP response %d to standard invite\n", resp);
12889 }
12890
12891 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
12892 if (option_debug)
12893 ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
12894 return;
12895 }
12896
12897
12898
12899 AST_SCHED_DEL(sched, p->initid);
12900
12901
12902
12903
12904 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 182 && resp != 183)
12905 resp = 183;
12906
12907
12908 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
12909 p->invitestate = INV_PROCEEDING;
12910
12911
12912 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
12913 p->invitestate = INV_COMPLETED;
12914
12915
12916 switch (resp) {
12917 case 100:
12918 case 101:
12919 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12920 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12921 check_pendings(p);
12922 break;
12923
12924 case 180:
12925 case 182:
12926 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12927 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12928 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12929 ast_queue_control(p->owner, AST_CONTROL_RINGING);
12930 if (p->owner->_state != AST_STATE_UP) {
12931 ast_setstate(p->owner, AST_STATE_RINGING);
12932 }
12933 }
12934 if (find_sdp(req)) {
12935 if (p->invitestate != INV_CANCELLED)
12936 p->invitestate = INV_EARLY_MEDIA;
12937 res = process_sdp(p, req);
12938 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12939
12940 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
12941 }
12942 }
12943 check_pendings(p);
12944 break;
12945
12946 case 183:
12947 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12948 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12949 if (find_sdp(req)) {
12950 if (p->invitestate != INV_CANCELLED)
12951 p->invitestate = INV_EARLY_MEDIA;
12952 res = process_sdp(p, req);
12953 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12954
12955 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
12956 }
12957 } else {
12958
12959
12960
12961
12962 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12963 ast_queue_control(p->owner, AST_CONTROL_RINGING);
12964 }
12965 }
12966 check_pendings(p);
12967 break;
12968
12969 case 200:
12970 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12971 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12972 p->authtries = 0;
12973 if (find_sdp(req)) {
12974 if ((res = process_sdp(p, req)) && !ast_test_flag(req, SIP_PKT_IGNORE))
12975 if (!reinvite)
12976
12977
12978 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
12979 }
12980
12981
12982
12983
12984 if (outgoing) {
12985 update_call_counter(p, DEC_CALL_RINGING);
12986 parse_ok_contact(p, req);
12987
12988 if (!reinvite)
12989 build_route(p, req, 1);
12990
12991 if(set_address_from_contact(p)) {
12992
12993
12994 if (!p->route && !ast_test_flag(req, SIP_PKT_IGNORE))
12995 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
12996 }
12997
12998 }
12999
13000 if (p->owner && (p->owner->_state == AST_STATE_UP) && (bridgepeer = ast_bridged_channel(p->owner))) {
13001 struct sip_pvt *bridgepvt = NULL;
13002
13003 if (!bridgepeer->tech) {
13004 ast_log(LOG_WARNING, "Ooooh.. no tech! That's REALLY bad\n");
13005 break;
13006 }
13007 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
13008 bridgepvt = (struct sip_pvt*)(bridgepeer->tech_pvt);
13009 if (bridgepvt->udptl) {
13010 if (p->t38.state == T38_PEER_REINVITE) {
13011 sip_handle_t38_reinvite(bridgepeer, p, 0);
13012 ast_rtp_set_rtptimers_onhold(p->rtp);
13013 if (p->vrtp)
13014 ast_rtp_set_rtptimers_onhold(p->vrtp);
13015 }
13016 } else {
13017 if (option_debug > 1)
13018 ast_log(LOG_DEBUG, "Strange... The other side of the bridge does not have a udptl struct\n");
13019 ast_mutex_lock(&bridgepvt->lock);
13020 bridgepvt->t38.state = T38_DISABLED;
13021 ast_mutex_unlock(&bridgepvt->lock);
13022 if (option_debug)
13023 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->tech->type);
13024 p->t38.state = T38_DISABLED;
13025 if (option_debug > 1)
13026 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13027 }
13028 } else {
13029
13030 if (option_debug > 1)
13031 ast_log(LOG_DEBUG, "Strange... The other side of the bridge is not a SIP channel\n");
13032 p->t38.state = T38_DISABLED;
13033 if (option_debug > 1)
13034 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13035 }
13036 }
13037 if (p->t38.state == T38_LOCAL_REINVITE) {
13038
13039 p->t38.state = T38_ENABLED;
13040 if (option_debug)
13041 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13042 }
13043
13044 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
13045 if (!reinvite) {
13046 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
13047 } else {
13048 ast_queue_frame(p->owner, &ast_null_frame);
13049 }
13050 } else {
13051
13052
13053
13054 if (!ast_test_flag(req, SIP_PKT_IGNORE))
13055 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
13056 }
13057
13058 p->invitestate = INV_TERMINATED;
13059 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
13060 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
13061 check_pendings(p);
13062 break;
13063 case 407:
13064 case 401:
13065
13066 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13067 if (p->options)
13068 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
13069
13070
13071 ast_string_field_free(p, theirtag);
13072 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13073 char *authenticate = (resp == 401 ? "WWW-Authenticate" : "Proxy-Authenticate");
13074 char *authorization = (resp == 401 ? "Authorization" : "Proxy-Authorization");
13075 if (p->authtries < MAX_AUTHTRIES)
13076 p->invitestate = INV_CALLING;
13077 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, authenticate, authorization, SIP_INVITE, 1)) {
13078 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
13079 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13080 sip_alreadygone(p);
13081 if (p->owner)
13082 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13083 }
13084 }
13085 break;
13086
13087 case 403:
13088
13089 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13090 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
13091 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner)
13092 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13093 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13094 sip_alreadygone(p);
13095 break;
13096
13097 case 404:
13098 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13099 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
13100 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13101 sip_alreadygone(p);
13102 break;
13103
13104 case 408:
13105 case 481:
13106
13107 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
13108 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13109 if (p->owner)
13110 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13111 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13112 break;
13113 case 487:
13114
13115
13116
13117 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13118 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE)) {
13119 ast_queue_hangup(p->owner);
13120 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
13121 } else if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13122 update_call_counter(p, DEC_CALL_LIMIT);
13123 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
13124 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13125 sip_alreadygone(p);
13126 }
13127 break;
13128 case 488:
13129 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13130 if (reinvite && p->udptl) {
13131
13132
13133
13134
13135
13136 p->t38.state = T38_DISABLED;
13137
13138 ast_rtp_set_rtptimers_onhold(p->rtp);
13139 ast_log(LOG_ERROR, "Got error on T.38 re-invite. Bad configuration. Peer needs to have T.38 disabled.\n");
13140
13141
13142
13143
13144
13145 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
13146 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13147 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13148 } else {
13149
13150 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
13151 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13152 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13153
13154 if (!reinvite)
13155 sip_alreadygone(p);
13156 }
13157 break;
13158 case 491:
13159
13160
13161
13162
13163 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13164 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE)) {
13165 if (p->owner->_state != AST_STATE_UP) {
13166 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13167 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13168 } else {
13169
13170
13171
13172 int wait;
13173
13174
13175 if (ast_test_flag(&p->flags[1], SIP_PAGE2_OUTGOING_CALL)) {
13176 wait = 2100 + ast_random() % 2000;
13177 } else {
13178 wait = ast_random() % 2000;
13179 }
13180 p->waitid = ast_sched_add(sched, wait, sip_reinvite_retry, p);
13181 if (option_debug > 2)
13182 ast_log(LOG_DEBUG, "Reinvite race. Waiting %d secs before retry\n", wait);
13183 }
13184 }
13185 break;
13186
13187 case 501:
13188 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13189 if (p->owner)
13190 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13191 break;
13192 }
13193 if (xmitres == XMIT_ERROR)
13194 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
13195 }
13196
13197
13198
13199
13200 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
13201 {
13202 char *auth = "Proxy-Authenticate";
13203 char *auth2 = "Proxy-Authorization";
13204
13205
13206 if (!p->refer)
13207 return;
13208
13209 switch (resp) {
13210 case 202:
13211
13212
13213 p->refer->status = REFER_ACCEPTED;
13214
13215 if (option_debug > 2)
13216 ast_log(LOG_DEBUG, "Got 202 accepted on transfer\n");
13217
13218 break;
13219
13220 case 401:
13221 case 407:
13222 if (ast_strlen_zero(p->authname)) {
13223 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s:%d but we have no matching peer or realm auth!\n",
13224 ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
13225 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13226 }
13227 if (resp == 401) {
13228 auth = "WWW-Authenticate";
13229 auth2 = "Authorization";
13230 }
13231 if ((p->authtries > 1) || do_proxy_auth(p, req, auth, auth2, SIP_REFER, 0)) {
13232 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
13233 p->refer->status = REFER_NOAUTH;
13234 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13235 }
13236 break;
13237 case 481:
13238
13239
13240
13241
13242 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
13243 if (p->owner)
13244 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13245 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13246 break;
13247
13248 case 500:
13249 case 501:
13250
13251
13252 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
13253 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13254 p->refer->status = REFER_FAILED;
13255 break;
13256 case 603:
13257 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
13258 p->refer->status = REFER_FAILED;
13259 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13260 break;
13261 }
13262 }
13263
13264
13265 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
13266 {
13267 int expires, expires_ms;
13268 struct sip_registry *r;
13269 r=p->registry;
13270
13271 switch (resp) {
13272 case 401:
13273 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "WWW-Authenticate", "Authorization")) {
13274 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
13275 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13276 }
13277 break;
13278 case 403:
13279 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
13280 if (global_regattempts_max)
13281 p->registry->regattempts = global_regattempts_max+1;
13282 AST_SCHED_DEL(sched, r->timeout);
13283 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13284 break;
13285 case 404:
13286 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username,p->registry->hostname);
13287 if (global_regattempts_max)
13288 p->registry->regattempts = global_regattempts_max+1;
13289 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13290 r->call = NULL;
13291 AST_SCHED_DEL(sched, r->timeout);
13292 break;
13293 case 407:
13294 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization")) {
13295 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
13296 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13297 }
13298 break;
13299 case 408:
13300
13301 if (r) {
13302 r->regattempts = 0;
13303 } else {
13304 ast_log(LOG_WARNING, "Got a 408 response to our REGISTER on call %s after we had destroyed the registry object\n", p->callid);
13305 }
13306 break;
13307 case 479:
13308 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username,p->registry->hostname);
13309 if (global_regattempts_max)
13310 p->registry->regattempts = global_regattempts_max+1;
13311 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13312 r->call = NULL;
13313 AST_SCHED_DEL(sched, r->timeout);
13314 break;
13315 case 200:
13316 if (!r) {
13317 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));
13318 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13319 return 0;
13320 }
13321
13322 r->regstate = REG_STATE_REGISTERED;
13323 r->regtime = time(NULL);
13324 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
13325 r->regattempts = 0;
13326 if (option_debug)
13327 ast_log(LOG_DEBUG, "Registration successful\n");
13328 if (r->timeout > -1) {
13329 if (option_debug)
13330 ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
13331 }
13332 AST_SCHED_DEL(sched, r->timeout);
13333 r->call = NULL;
13334 p->registry = NULL;
13335
13336 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13337
13338
13339
13340
13341 AST_SCHED_DEL(sched, r->expire);
13342
13343
13344 expires = 0;
13345
13346
13347 if (!ast_strlen_zero(get_header(req, "Contact"))) {
13348 const char *contact = NULL;
13349 const char *tmptmp = NULL;
13350 int start = 0;
13351 for(;;) {
13352 contact = __get_header(req, "Contact", &start);
13353
13354 if(!ast_strlen_zero(contact)) {
13355 if( (tmptmp=strstr(contact, p->our_contact))) {
13356 contact=tmptmp;
13357 break;
13358 }
13359 } else
13360 break;
13361 }
13362 tmptmp = strcasestr(contact, "expires=");
13363 if (tmptmp) {
13364 if (sscanf(tmptmp + 8, "%30d;", &expires) != 1)
13365 expires = 0;
13366 }
13367
13368 }
13369 if (!expires)
13370 expires=atoi(get_header(req, "expires"));
13371 if (!expires)
13372 expires=default_expiry;
13373
13374 expires_ms = expires * 1000;
13375 if (expires <= EXPIRY_GUARD_LIMIT)
13376 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT),EXPIRY_GUARD_MIN);
13377 else
13378 expires_ms -= EXPIRY_GUARD_SECS * 1000;
13379 if (sipdebug)
13380 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
13381
13382 r->refresh= (int) expires_ms / 1000;
13383
13384
13385 AST_SCHED_DEL(sched, r->expire);
13386 r->expire = ast_sched_add(sched, expires_ms, sip_reregister, r);
13387 ASTOBJ_UNREF(r, sip_registry_destroy);
13388 }
13389 return 1;
13390 }
13391
13392
13393 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
13394 {
13395 struct sip_peer *peer = p->relatedpeer;
13396 int statechanged, is_reachable, was_reachable;
13397 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
13398
13399
13400
13401
13402
13403
13404 if (pingtime < 1)
13405 pingtime = 1;
13406
13407
13408
13409
13410
13411 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
13412 is_reachable = pingtime <= peer->maxms;
13413 statechanged = peer->lastms == 0
13414 || was_reachable != is_reachable;
13415
13416 peer->lastms = pingtime;
13417 peer->call = NULL;
13418 if (statechanged) {
13419 const char *s = is_reachable ? "Reachable" : "Lagged";
13420 char str_lastms[20];
13421 snprintf(str_lastms, sizeof(str_lastms), "%d", pingtime);
13422
13423 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
13424 peer->name, s, pingtime, peer->maxms);
13425 ast_device_state_changed("SIP/%s", peer->name);
13426 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE)) {
13427 ast_update_realtime("sippeers", "name", peer->name, "lastms", str_lastms, NULL);
13428 }
13429 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
13430 "Peer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
13431 peer->name, s, pingtime);
13432 }
13433
13434 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
13435 struct sip_peer *peer_ptr = peer;
13436 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
13437 }
13438
13439 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13440
13441
13442 peer->pokeexpire = ast_sched_add(sched,
13443 is_reachable ? DEFAULT_FREQ_OK : DEFAULT_FREQ_NOTOK,
13444 sip_poke_peer_s, ASTOBJ_REF(peer));
13445
13446 if (peer->pokeexpire == -1) {
13447 ASTOBJ_UNREF(peer, sip_destroy_peer);
13448 }
13449 }
13450
13451
13452 static void stop_media_flows(struct sip_pvt *p)
13453 {
13454
13455 if (p->rtp)
13456 ast_rtp_stop(p->rtp);
13457 if (p->vrtp)
13458 ast_rtp_stop(p->vrtp);
13459 if (p->udptl)
13460 ast_udptl_stop(p->udptl);
13461 }
13462
13463
13464
13465 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
13466 {
13467 struct ast_channel *owner;
13468 int sipmethod;
13469 int res = 1;
13470 int ack_res;
13471 const char *c = get_header(req, "Cseq");
13472
13473 char *c_copy = ast_strdupa(c);
13474
13475 const char *msg = ast_skip_blanks(ast_skip_nonblanks(c_copy));
13476
13477 if (!msg)
13478 msg = "";
13479
13480 sipmethod = find_sip_method(msg);
13481
13482 owner = p->owner;
13483 if (owner)
13484 owner->hangupcause = hangup_sip2cause(resp);
13485
13486
13487 if ((resp >= 100) && (resp <= 199)) {
13488 ack_res = __sip_semi_ack(p, seqno, 0, sipmethod);
13489 } else {
13490 ack_res = __sip_ack(p, seqno, 0, sipmethod);
13491 }
13492
13493 if (ack_res == FALSE) {
13494 append_history(p, "Ignore", "Ignoring this retransmit\n");
13495 return;
13496 }
13497
13498
13499 if (!p->owner && sipmethod == SIP_NOTIFY && p->pendinginvite)
13500 p->pendinginvite = 0;
13501
13502
13503 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
13504 char tag[128];
13505
13506 gettag(req, "To", tag, sizeof(tag));
13507 ast_string_field_set(p, theirtag, tag);
13508 }
13509
13510
13511
13512
13513
13514
13515
13516
13517 if ((resp == 404 || resp == 408 || resp == 481) && sipmethod == SIP_BYE) {
13518 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13519 return;
13520 }
13521
13522 if (p->relatedpeer && p->method == SIP_OPTIONS) {
13523
13524
13525
13526 if (resp != 100)
13527 handle_response_peerpoke(p, resp, req);
13528 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
13529 switch(resp) {
13530 case 100:
13531 case 101:
13532 if (sipmethod == SIP_INVITE)
13533 handle_response_invite(p, resp, rest, req, seqno);
13534 break;
13535 case 183:
13536 if (sipmethod == SIP_INVITE)
13537 handle_response_invite(p, resp, rest, req, seqno);
13538 break;
13539 case 180:
13540 if (sipmethod == SIP_INVITE)
13541 handle_response_invite(p, resp, rest, req, seqno);
13542 break;
13543 case 182:
13544 if (sipmethod == SIP_INVITE)
13545 handle_response_invite(p, resp, rest, req, seqno);
13546 break;
13547 case 200:
13548 p->authtries = 0;
13549 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
13550
13551
13552
13553 } else if (sipmethod == SIP_INVITE) {
13554 handle_response_invite(p, resp, rest, req, seqno);
13555 } else if (sipmethod == SIP_NOTIFY) {
13556
13557 if (p->owner) {
13558 if (!p->refer) {
13559 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
13560 ast_queue_hangup(p->owner);
13561 } else if (option_debug > 3)
13562 ast_log(LOG_DEBUG, "Got OK on REFER Notify message\n");
13563 } else {
13564 if (p->subscribed == NONE)
13565 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13566 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
13567
13568 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
13569 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
13570 }
13571 }
13572 } else if (sipmethod == SIP_REGISTER)
13573 res = handle_response_register(p, resp, rest, req, seqno);
13574 else if (sipmethod == SIP_BYE) {
13575 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13576 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
13577 } else if (sipmethod == SIP_SUBSCRIBE)
13578 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
13579 break;
13580 case 202:
13581 if (sipmethod == SIP_REFER)
13582 handle_response_refer(p, resp, rest, req, seqno);
13583 break;
13584 case 401:
13585 if (sipmethod == SIP_INVITE)
13586 handle_response_invite(p, resp, rest, req, seqno);
13587 else if (sipmethod == SIP_REFER)
13588 handle_response_refer(p, resp, rest, req, seqno);
13589 else if (p->registry && sipmethod == SIP_REGISTER)
13590 res = handle_response_register(p, resp, rest, req, seqno);
13591 else if (sipmethod == SIP_BYE) {
13592 if (ast_strlen_zero(p->authname)) {
13593 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
13594 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
13595 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13596 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "WWW-Authenticate", "Authorization", sipmethod, 0)) {
13597 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
13598 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13599
13600
13601 }
13602 } else {
13603 ast_log(LOG_WARNING, "Got authentication request (401) on unknown %s to '%s'\n", sip_methods[sipmethod].text, get_header(req, "To"));
13604 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13605 }
13606 break;
13607 case 403:
13608 if (sipmethod == SIP_INVITE)
13609 handle_response_invite(p, resp, rest, req, seqno);
13610 else if (p->registry && sipmethod == SIP_REGISTER)
13611 res = handle_response_register(p, resp, rest, req, seqno);
13612 else {
13613 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
13614 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13615 }
13616 break;
13617 case 404:
13618 if (p->registry && sipmethod == SIP_REGISTER)
13619 res = handle_response_register(p, resp, rest, req, seqno);
13620 else if (sipmethod == SIP_INVITE)
13621 handle_response_invite(p, resp, rest, req, seqno);
13622 else if (owner)
13623 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13624 break;
13625 case 407:
13626 if (sipmethod == SIP_INVITE)
13627 handle_response_invite(p, resp, rest, req, seqno);
13628 else if (sipmethod == SIP_REFER)
13629 handle_response_refer(p, resp, rest, req, seqno);
13630 else if (p->registry && sipmethod == SIP_REGISTER)
13631 res = handle_response_register(p, resp, rest, req, seqno);
13632 else if (sipmethod == SIP_BYE) {
13633 if (ast_strlen_zero(p->authname)) {
13634 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
13635 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
13636 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13637 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization", sipmethod, 0)) {
13638 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
13639 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13640 }
13641 } else
13642 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13643
13644 break;
13645 case 408:
13646 if (sipmethod == SIP_INVITE)
13647 handle_response_invite(p, resp, rest, req, seqno);
13648 else if (sipmethod == SIP_REGISTER)
13649 res = handle_response_register(p, resp, rest, req, seqno);
13650 else if (sipmethod == SIP_BYE) {
13651 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13652 if (option_debug)
13653 ast_log(LOG_DEBUG, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
13654 } else {
13655 if (owner)
13656 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13657 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13658 }
13659 break;
13660 case 481:
13661 if (sipmethod == SIP_INVITE) {
13662 handle_response_invite(p, resp, rest, req, seqno);
13663 } else if (sipmethod == SIP_REFER) {
13664 handle_response_refer(p, resp, rest, req, seqno);
13665 } else if (sipmethod == SIP_BYE) {
13666
13667
13668 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
13669 } else if (sipmethod == SIP_CANCEL) {
13670
13671
13672 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
13673 } else {
13674 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
13675
13676 }
13677 break;
13678 case 487:
13679 if (sipmethod == SIP_INVITE)
13680 handle_response_invite(p, resp, rest, req, seqno);
13681 break;
13682 case 488:
13683 if (sipmethod == SIP_INVITE)
13684 handle_response_invite(p, resp, rest, req, seqno);
13685 break;
13686 case 491:
13687 if (sipmethod == SIP_INVITE)
13688 handle_response_invite(p, resp, rest, req, seqno);
13689 else {
13690 if (option_debug)
13691 ast_log(LOG_DEBUG, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
13692 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13693 }
13694 break;
13695 case 501:
13696 if (sipmethod == SIP_INVITE)
13697 handle_response_invite(p, resp, rest, req, seqno);
13698 else if (sipmethod == SIP_REFER)
13699 handle_response_refer(p, resp, rest, req, seqno);
13700 else
13701 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(p->sa.sin_addr), msg);
13702 break;
13703 case 603:
13704 if (sipmethod == SIP_REFER) {
13705 handle_response_refer(p, resp, rest, req, seqno);
13706 break;
13707 }
13708
13709 default:
13710 if ((resp >= 300) && (resp < 700)) {
13711
13712 if ((option_verbose > 2) && (resp != 487))
13713 ast_verbose(VERBOSE_PREFIX_3 "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
13714
13715 if (sipmethod == SIP_INVITE)
13716 stop_media_flows(p);
13717
13718
13719 switch(resp) {
13720 case 300:
13721 case 301:
13722 case 302:
13723 case 305:
13724 parse_moved_contact(p, req);
13725
13726 case 486:
13727 case 600:
13728 case 603:
13729 if (p->owner)
13730 ast_queue_control(p->owner, AST_CONTROL_BUSY);
13731 break;
13732 case 482:
13733
13734
13735
13736
13737 if (option_debug)
13738 ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
13739 if (p->owner)
13740 ast_string_field_build(p->owner, call_forward,
13741 "Local/%s@%s", p->username, p->context);
13742
13743 case 480:
13744 case 404:
13745 case 410:
13746 case 400:
13747 case 500:
13748 if (sipmethod == SIP_REFER) {
13749 handle_response_refer(p, resp, rest, req, seqno);
13750 break;
13751 }
13752
13753 case 502:
13754 case 503:
13755 case 504:
13756 if (owner)
13757 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13758 break;
13759 default:
13760
13761 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO && sipmethod != SIP_BYE)
13762 ast_queue_hangup(p->owner);
13763 break;
13764 }
13765
13766 if (sipmethod == SIP_INVITE)
13767 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13768 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
13769 sip_alreadygone(p);
13770 if (!p->owner)
13771 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13772 } else if ((resp >= 100) && (resp < 200)) {
13773 if (sipmethod == SIP_INVITE) {
13774 if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
13775 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13776 if (find_sdp(req))
13777 process_sdp(p, req);
13778 if (p->owner) {
13779
13780 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
13781 }
13782 }
13783 } else
13784 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));
13785 }
13786 } else {
13787
13788
13789 if (ast_test_flag(req, SIP_PKT_DEBUG))
13790 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
13791
13792 if (sipmethod == SIP_INVITE && resp == 200) {
13793
13794
13795 char tag[128];
13796
13797 gettag(req, "To", tag, sizeof(tag));
13798 ast_string_field_set(p, theirtag, tag);
13799 }
13800
13801 switch(resp) {
13802 case 200:
13803 if (sipmethod == SIP_INVITE) {
13804 handle_response_invite(p, resp, rest, req, seqno);
13805 } else if (sipmethod == SIP_CANCEL) {
13806 if (option_debug)
13807 ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
13808
13809
13810 } else if (sipmethod == SIP_NOTIFY) {
13811
13812 if (p->owner) {
13813 if (p->refer) {
13814 if (option_debug)
13815 ast_log(LOG_DEBUG, "Got 200 OK on NOTIFY for transfer\n");
13816 } else
13817 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
13818
13819 } else {
13820 if (!p->subscribed && !p->refer)
13821 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13822 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
13823
13824 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
13825 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
13826 }
13827 }
13828 } else if (sipmethod == SIP_BYE)
13829 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13830 else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO)
13831
13832
13833 ;
13834 else if (sipmethod == SIP_BYE)
13835
13836 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13837 break;
13838 case 202:
13839 if (sipmethod == SIP_REFER)
13840 handle_response_refer(p, resp, rest, req, seqno);
13841 break;
13842 case 401:
13843 case 407:
13844 if (sipmethod == SIP_REFER)
13845 handle_response_refer(p, resp, rest, req, seqno);
13846 else if (sipmethod == SIP_INVITE)
13847 handle_response_invite(p, resp, rest, req, seqno);
13848 else if (sipmethod == SIP_BYE) {
13849 char *auth, *auth2;
13850
13851 auth = (resp == 407 ? "Proxy-Authenticate" : "WWW-Authenticate");
13852 auth2 = (resp == 407 ? "Proxy-Authorization" : "Authorization");
13853 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, auth, auth2, sipmethod, 0)) {
13854 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
13855 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13856 }
13857 }
13858 break;
13859 case 481:
13860 if (sipmethod == SIP_INVITE) {
13861
13862 handle_response_invite(p, resp, rest, req, seqno);
13863 } else if (sipmethod == SIP_BYE) {
13864 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13865 } else if (sipdebug) {
13866 ast_log (LOG_DEBUG, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
13867 }
13868 break;
13869 case 501:
13870 if (sipmethod == SIP_INVITE)
13871 handle_response_invite(p, resp, rest, req, seqno);
13872 else if (sipmethod == SIP_REFER)
13873 handle_response_refer(p, resp, rest, req, seqno);
13874 break;
13875 case 603:
13876 if (sipmethod == SIP_REFER) {
13877 handle_response_refer(p, resp, rest, req, seqno);
13878 break;
13879 }
13880
13881 default:
13882 if ((resp >= 100) && (resp < 200)) {
13883 if (sipmethod == SIP_INVITE) {
13884 if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
13885 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13886 }
13887 }
13888 if ((resp >= 300) && (resp < 700)) {
13889 if ((option_verbose > 2) && (resp != 487))
13890 ast_verbose(VERBOSE_PREFIX_3 "Incoming call: Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
13891 switch(resp) {
13892 case 488:
13893 case 603:
13894 case 500:
13895 case 502:
13896 case 503:
13897 case 504:
13898
13899
13900 if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
13901 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13902 break;
13903 }
13904 }
13905 break;
13906 }
13907 }
13908 }
13909
13910
13911
13912
13913
13914
13915
13916 static void *sip_park_thread(void *stuff)
13917 {
13918 struct ast_channel *transferee, *transferer;
13919 struct sip_dual *d;
13920 struct sip_request req;
13921 int ext;
13922 int res;
13923
13924 d = stuff;
13925 transferee = d->chan1;
13926 transferer = d->chan2;
13927 copy_request(&req, &d->req);
13928
13929 if (!transferee || !transferer) {
13930 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
13931 return NULL;
13932 }
13933 if (option_debug > 3)
13934 ast_log(LOG_DEBUG, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
13935
13936 ast_channel_lock(transferee);
13937 if (ast_do_masquerade(transferee)) {
13938 ast_log(LOG_WARNING, "Masquerade failed.\n");
13939 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
13940 ast_channel_unlock(transferee);
13941 return NULL;
13942 }
13943 ast_channel_unlock(transferee);
13944
13945 res = ast_park_call(transferee, transferer, 0, &ext);
13946
13947
13948 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
13949 if (!res) {
13950 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
13951 } else {
13952
13953 sprintf(buf, "Call parked on extension '%d'", ext);
13954 transmit_message_with_text(transferer->tech_pvt, buf);
13955 }
13956 #endif
13957
13958
13959
13960 transmit_response(transferer->tech_pvt, "202 Accepted", &req);
13961 if (!res) {
13962
13963 append_history(transferer->tech_pvt, "SIPpark","Parked call on %d", ext);
13964 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
13965 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
13966 ast_hangup(transferer);
13967 if (option_debug)
13968 ast_log(LOG_DEBUG, "SIP Call parked on extension '%d'\n", ext);
13969 } else {
13970 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
13971 append_history(transferer->tech_pvt, "SIPpark","Parking failed\n");
13972 if (option_debug)
13973 ast_log(LOG_DEBUG, "SIP Call parked failed \n");
13974
13975 }
13976 free(d);
13977 return NULL;
13978 }
13979
13980
13981
13982
13983 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno)
13984 {
13985 struct sip_dual *d;
13986 struct ast_channel *transferee, *transferer;
13987
13988 pthread_t th;
13989
13990 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
13991 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "SIPPeer/%s", chan2->name);
13992 if ((!transferer) || (!transferee)) {
13993 if (transferee) {
13994 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13995 ast_hangup(transferee);
13996 }
13997 if (transferer) {
13998 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13999 ast_hangup(transferer);
14000 }
14001 return -1;
14002 }
14003
14004
14005 transferee->readformat = chan1->readformat;
14006 transferee->writeformat = chan1->writeformat;
14007
14008
14009 ast_channel_masquerade(transferee, chan1);
14010
14011
14012 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
14013 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
14014 transferee->priority = chan1->priority;
14015
14016
14017
14018
14019
14020 transferer->readformat = chan2->readformat;
14021 transferer->writeformat = chan2->writeformat;
14022
14023
14024
14025
14026 while (ast_channel_trylock(chan2)) {
14027 struct sip_pvt *pvt = chan2->tech_pvt;
14028 DEADLOCK_AVOIDANCE(&pvt->lock);
14029 }
14030 ast_channel_masquerade(transferer, chan2);
14031 ast_channel_unlock(chan2);
14032
14033
14034 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
14035 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
14036 transferer->priority = chan2->priority;
14037
14038 ast_channel_lock(transferer);
14039 if (ast_do_masquerade(transferer)) {
14040 ast_log(LOG_WARNING, "Masquerade failed :(\n");
14041 ast_channel_unlock(transferer);
14042 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
14043 ast_hangup(transferer);
14044 return -1;
14045 }
14046 ast_channel_unlock(transferer);
14047 if (!transferer || !transferee) {
14048 if (!transferer) {
14049 if (option_debug)
14050 ast_log(LOG_DEBUG, "No transferer channel, giving up parking\n");
14051 }
14052 if (!transferee) {
14053 if (option_debug)
14054 ast_log(LOG_DEBUG, "No transferee channel, giving up parking\n");
14055 }
14056 return -1;
14057 }
14058 if ((d = ast_calloc(1, sizeof(*d)))) {
14059 pthread_attr_t attr;
14060
14061 pthread_attr_init(&attr);
14062 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
14063
14064
14065 copy_request(&d->req, req);
14066 d->chan1 = transferee;
14067 d->chan2 = transferer;
14068 d->seqno = seqno;
14069 if (ast_pthread_create_background(&th, &attr, sip_park_thread, d) < 0) {
14070
14071 free(d);
14072
14073 pthread_attr_destroy(&attr);
14074 return 0;
14075 }
14076 pthread_attr_destroy(&attr);
14077 }
14078 return -1;
14079 }
14080
14081
14082
14083
14084 static void ast_quiet_chan(struct ast_channel *chan)
14085 {
14086 if (chan && chan->_state == AST_STATE_UP) {
14087 if (ast_test_flag(chan, AST_FLAG_MOH))
14088 ast_moh_stop(chan);
14089 else if (chan->generatordata)
14090 ast_deactivate_generator(chan);
14091 }
14092 }
14093
14094
14095
14096 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
14097 {
14098 int res = 0;
14099 struct ast_channel *peera = NULL,
14100 *peerb = NULL,
14101 *peerc = NULL,
14102 *peerd = NULL;
14103
14104
14105
14106
14107 if (option_debug > 3) {
14108 ast_log(LOG_DEBUG, "Sip transfer:--------------------\n");
14109 if (transferer->chan1)
14110 ast_log(LOG_DEBUG, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
14111 else
14112 ast_log(LOG_DEBUG, "-- No transferer first channel - odd??? \n");
14113 if (target->chan1)
14114 ast_log(LOG_DEBUG, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
14115 else
14116 ast_log(LOG_DEBUG, "-- No target first channel ---\n");
14117 if (transferer->chan2)
14118 ast_log(LOG_DEBUG, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
14119 else
14120 ast_log(LOG_DEBUG, "-- No bridged call to transferee\n");
14121 if (target->chan2)
14122 ast_log(LOG_DEBUG, "-- Bridged call to transfer target: %s State %s\n", target->chan2 ? target->chan2->name : "<none>", target->chan2 ? ast_state2str(target->chan2->_state) : "(none)");
14123 else
14124 ast_log(LOG_DEBUG, "-- No target second channel ---\n");
14125 ast_log(LOG_DEBUG, "-- END Sip transfer:--------------------\n");
14126 }
14127 if (transferer->chan2) {
14128 peera = transferer->chan1;
14129 peerb = target->chan1;
14130 peerc = transferer->chan2;
14131 peerd = target->chan2;
14132 if (option_debug > 2)
14133 ast_log(LOG_DEBUG, "SIP transfer: Four channels to handle\n");
14134 } else if (target->chan2) {
14135 peera = target->chan1;
14136 peerb = transferer->chan1;
14137 peerc = target->chan2;
14138 peerd = transferer->chan2;
14139 if (option_debug > 2)
14140 ast_log(LOG_DEBUG, "SIP transfer: Three channels to handle\n");
14141 }
14142
14143 if (peera && peerb && peerc && (peerb != peerc)) {
14144 ast_quiet_chan(peera);
14145 ast_quiet_chan(peerb);
14146 ast_quiet_chan(peerc);
14147 if (peerd)
14148 ast_quiet_chan(peerd);
14149
14150 if (option_debug > 3)
14151 ast_log(LOG_DEBUG, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
14152 if (ast_channel_masquerade(peerb, peerc)) {
14153 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
14154 res = -1;
14155 } else
14156 ast_log(LOG_DEBUG, "SIP transfer: Succeeded to masquerade channels.\n");
14157 return res;
14158 } else {
14159 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
14160 if (transferer->chan1)
14161 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
14162 if (target->chan1)
14163 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
14164 return -2;
14165 }
14166 return 0;
14167 }
14168
14169
14170
14171
14172
14173
14174 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
14175 {
14176 const char *thetag;
14177
14178 if (!tagbuf)
14179 return NULL;
14180 tagbuf[0] = '\0';
14181 thetag = get_header(req, header);
14182 thetag = strcasestr(thetag, ";tag=");
14183 if (thetag) {
14184 thetag += 5;
14185 ast_copy_string(tagbuf, thetag, tagbufsize);
14186 return strsep(&tagbuf, ";");
14187 }
14188 return NULL;
14189 }
14190
14191
14192 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
14193 {
14194
14195
14196 int res = 0;
14197 const char *event = get_header(req, "Event");
14198 char *eventid = NULL;
14199 char *sep;
14200
14201 if( (sep = strchr(event, ';')) ) {
14202 *sep++ = '\0';
14203 eventid = sep;
14204 }
14205
14206 if (option_debug > 1 && sipdebug)
14207 ast_log(LOG_DEBUG, "Got NOTIFY Event: %s\n", event);
14208
14209 if (strcmp(event, "refer")) {
14210
14211
14212 transmit_response(p, "489 Bad event", req);
14213 res = -1;
14214 } else {
14215
14216
14217
14218
14219
14220 char buf[1024];
14221 char *cmd, *code;
14222 int respcode;
14223 int success = TRUE;
14224
14225
14226
14227
14228
14229
14230
14231
14232 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
14233
14234 transmit_response(p, "400 Bad request", req);
14235 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14236 return -1;
14237 }
14238
14239
14240 if (get_msg_text(buf, sizeof(buf), req)) {
14241 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
14242 transmit_response(p, "400 Bad request", req);
14243 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14244 return -1;
14245 }
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267 if (option_debug > 2)
14268 ast_log(LOG_DEBUG, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
14269 cmd = ast_skip_blanks(buf);
14270 code = cmd;
14271
14272 while(*code && (*code > 32)) {
14273 code++;
14274 }
14275 *code++ = '\0';
14276 code = ast_skip_blanks(code);
14277 sep = code;
14278 sep++;
14279 while(*sep && (*sep > 32)) {
14280 sep++;
14281 }
14282 *sep++ = '\0';
14283 respcode = atoi(code);
14284 switch (respcode) {
14285 case 100:
14286 case 101:
14287
14288 break;
14289 case 183:
14290
14291 break;
14292 case 200:
14293
14294 break;
14295 case 301:
14296 case 302:
14297
14298 success = FALSE;
14299 break;
14300 case 503:
14301
14302 success = FALSE;
14303 break;
14304 case 603:
14305
14306 success = FALSE;
14307 break;
14308 }
14309 if (!success) {
14310 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
14311 }
14312
14313
14314 transmit_response(p, "200 OK", req);
14315 };
14316
14317 if (!p->lastinvite)
14318 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14319
14320 return res;
14321 }
14322
14323
14324 static int handle_request_options(struct sip_pvt *p, struct sip_request *req)
14325 {
14326 int res;
14327
14328
14329
14330
14331 if (p->lastinvite) {
14332
14333 transmit_response_with_allow(p, "200 OK", req, 0);
14334 return 0;
14335 }
14336
14337 res = get_destination(p, req);
14338 build_contact(p);
14339
14340 if (ast_strlen_zero(p->context))
14341 ast_string_field_set(p, context, default_context);
14342
14343 if (ast_shutting_down())
14344 transmit_response_with_allow(p, "503 Unavailable", req, 0);
14345 else if (res < 0)
14346 transmit_response_with_allow(p, "404 Not Found", req, 0);
14347 else
14348 transmit_response_with_allow(p, "200 OK", req, 0);
14349
14350
14351
14352 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14353
14354 return res;
14355 }
14356
14357
14358
14359
14360
14361
14362
14363
14364 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin, int *nounlock)
14365 {
14366 int earlyreplace = 0;
14367 int oneleggedreplace = 0;
14368 struct ast_channel *c = p->owner;
14369 struct ast_channel *replacecall = p->refer->refer_call->owner;
14370 struct ast_channel *targetcall;
14371
14372
14373 if (replacecall->_state == AST_STATE_RING)
14374 earlyreplace = 1;
14375
14376
14377 if (!(targetcall = ast_bridged_channel(replacecall))) {
14378
14379 if (!earlyreplace) {
14380 if (option_debug > 1)
14381 ast_log(LOG_DEBUG, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
14382 oneleggedreplace = 1;
14383 }
14384 }
14385 if (option_debug > 3 && targetcall && targetcall->_state == AST_STATE_RINGING)
14386 ast_log(LOG_DEBUG, "SIP transfer: Target channel is in ringing state\n");
14387
14388 if (option_debug > 3) {
14389 if (targetcall)
14390 ast_log(LOG_DEBUG, "SIP transfer: Invite Replace incoming channel should bridge to channel %s while hanging up channel %s\n", targetcall->name, replacecall->name);
14391 else
14392 ast_log(LOG_DEBUG, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
14393 }
14394
14395 if (ignore) {
14396 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
14397
14398
14399
14400 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
14401
14402 if (c) {
14403 *nounlock = 1;
14404 ast_channel_unlock(c);
14405 }
14406 ast_channel_unlock(replacecall);
14407 ast_mutex_unlock(&p->refer->refer_call->lock);
14408 return 1;
14409 }
14410 if (!c) {
14411
14412 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
14413 transmit_response_reliable(p, "503 Service Unavailable", req);
14414 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
14415 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14416 ast_channel_unlock(replacecall);
14417 ast_mutex_unlock(&p->refer->refer_call->lock);
14418 return 1;
14419 }
14420 append_history(p, "Xfer", "INVITE/Replace received");
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432 transmit_response(p, "100 Trying", req);
14433 ast_setstate(c, AST_STATE_RING);
14434
14435
14436
14437
14438
14439 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
14440
14441 ast_setstate(c, AST_STATE_UP);
14442
14443
14444 ast_quiet_chan(replacecall);
14445 ast_quiet_chan(targetcall);
14446 if (option_debug > 3)
14447 ast_log(LOG_DEBUG, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
14448
14449
14450 if (! earlyreplace && ! oneleggedreplace )
14451 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14452
14453
14454 if(ast_channel_masquerade(replacecall, c))
14455 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
14456 else if (option_debug > 3)
14457 ast_log(LOG_DEBUG, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
14458
14459
14460 if (ast_do_masquerade(replacecall)) {
14461 ast_log(LOG_WARNING, "Failed to perform masquerade with INVITE replaces\n");
14462 }
14463
14464 if (earlyreplace || oneleggedreplace ) {
14465 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
14466 }
14467
14468 ast_setstate(c, AST_STATE_DOWN);
14469 if (option_debug > 3) {
14470 struct ast_channel *test;
14471 ast_log(LOG_DEBUG, "After transfer:----------------------------\n");
14472 ast_log(LOG_DEBUG, " -- C: %s State %s\n", c->name, ast_state2str(c->_state));
14473 if (replacecall)
14474 ast_log(LOG_DEBUG, " -- replacecall: %s State %s\n", replacecall->name, ast_state2str(replacecall->_state));
14475 if (p->owner) {
14476 ast_log(LOG_DEBUG, " -- P->owner: %s State %s\n", p->owner->name, ast_state2str(p->owner->_state));
14477 test = ast_bridged_channel(p->owner);
14478 if (test)
14479 ast_log(LOG_DEBUG, " -- Call bridged to P->owner: %s State %s\n", test->name, ast_state2str(test->_state));
14480 else
14481 ast_log(LOG_DEBUG, " -- No call bridged to C->owner \n");
14482 } else
14483 ast_log(LOG_DEBUG, " -- No channel yet \n");
14484 ast_log(LOG_DEBUG, "End After transfer:----------------------------\n");
14485 }
14486
14487
14488 ast_channel_unlock(replacecall);
14489 ast_channel_unlock(c);
14490 ast_mutex_unlock(&p->refer->refer_call->lock);
14491 ast_mutex_unlock(&p->lock);
14492 *nounlock = 1;
14493
14494
14495 c->tech_pvt = NULL;
14496 ast_hangup(c);
14497
14498 ast_mutex_lock(&p->lock);
14499
14500 return 0;
14501 }
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519 static int sip_uri_params_cmp(const char *input1, const char *input2)
14520 {
14521 char *params1 = NULL;
14522 char *params2 = NULL;
14523 char *pos1;
14524 char *pos2;
14525 int zerolength1 = 0;
14526 int zerolength2 = 0;
14527 int maddrmatch = 0;
14528 int ttlmatch = 0;
14529 int usermatch = 0;
14530 int methodmatch = 0;
14531
14532 if (ast_strlen_zero(input1)) {
14533 zerolength1 = 1;
14534 } else {
14535 params1 = ast_strdupa(input1);
14536 }
14537 if (ast_strlen_zero(input2)) {
14538 zerolength2 = 1;
14539 } else {
14540 params2 = ast_strdupa(input2);
14541 }
14542
14543
14544
14545
14546 if (zerolength1 && zerolength2) {
14547 return 0;
14548 }
14549
14550 pos1 = params1;
14551 while (!ast_strlen_zero(pos1)) {
14552 char *name1 = pos1;
14553 char *value1 = strchr(pos1, '=');
14554 char *semicolon1 = strchr(pos1, ';');
14555 int matched = 0;
14556 if (semicolon1) {
14557 *semicolon1++ = '\0';
14558 }
14559 if (!value1) {
14560 goto fail;
14561 }
14562 *value1++ = '\0';
14563
14564
14565
14566
14567
14568 pos2 = ast_strdupa(params2);
14569 while (!ast_strlen_zero(pos2)) {
14570 char *name2 = pos2;
14571 char *value2 = strchr(pos2, '=');
14572 char *semicolon2 = strchr(pos2, ';');
14573 if (semicolon2) {
14574 *semicolon2++ = '\0';
14575 }
14576 if (!value2) {
14577 goto fail;
14578 }
14579 *value2++ = '\0';
14580 if (!strcasecmp(name1, name2)) {
14581 if (strcasecmp(value1, value2)) {
14582 goto fail;
14583 } else {
14584 matched = 1;
14585 break;
14586 }
14587 }
14588 pos2 = semicolon2;
14589 }
14590
14591 if (!strcasecmp(name1, "maddr")) {
14592 if (matched) {
14593 maddrmatch = 1;
14594 } else {
14595 goto fail;
14596 }
14597 } else if (!strcasecmp(name1, "ttl")) {
14598 if (matched) {
14599 ttlmatch = 1;
14600 } else {
14601 goto fail;
14602 }
14603 } else if (!strcasecmp(name1, "user")) {
14604 if (matched) {
14605 usermatch = 1;
14606 } else {
14607 goto fail;
14608 }
14609 } else if (!strcasecmp(name1, "method")) {
14610 if (matched) {
14611 methodmatch = 1;
14612 } else {
14613 goto fail;
14614 }
14615 }
14616 pos1 = semicolon1;
14617 }
14618
14619
14620
14621
14622
14623 pos2 = params2;
14624 while (!ast_strlen_zero(pos2)) {
14625 char *name2 = pos2;
14626 char *value2 = strchr(pos2, '=');
14627 char *semicolon2 = strchr(pos2, ';');
14628 if (semicolon2) {
14629 *semicolon2++ = '\0';
14630 }
14631 if (!value2) {
14632 goto fail;
14633 }
14634 *value2++ = '\0';
14635 if ((!strcasecmp(name2, "maddr") && !maddrmatch) ||
14636 (!strcasecmp(name2, "ttl") && !ttlmatch) ||
14637 (!strcasecmp(name2, "user") && !usermatch) ||
14638 (!strcasecmp(name2, "method") && !methodmatch)) {
14639 goto fail;
14640 }
14641 }
14642 return 0;
14643
14644 fail:
14645 return 1;
14646 }
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659 static int sip_uri_headers_cmp(const char *input1, const char *input2)
14660 {
14661 char *headers1 = NULL;
14662 char *headers2 = NULL;
14663 int zerolength1 = 0;
14664 int zerolength2 = 0;
14665 int different = 0;
14666 char *header1;
14667
14668 if (ast_strlen_zero(input1)) {
14669 zerolength1 = 1;
14670 } else {
14671 headers1 = ast_strdupa(input1);
14672 }
14673
14674 if (ast_strlen_zero(input2)) {
14675 zerolength2 = 1;
14676 } else {
14677 headers2 = ast_strdupa(input2);
14678 }
14679
14680 if ((zerolength1 && !zerolength2) ||
14681 (zerolength2 && !zerolength1))
14682 return 1;
14683
14684 if (zerolength1 && zerolength2)
14685 return 0;
14686
14687
14688
14689
14690
14691 if (strlen(headers1) != strlen(headers2)) {
14692 return 1;
14693 }
14694
14695 for (header1 = strsep(&headers1, "&"); header1; header1 = strsep(&headers1, "&")) {
14696 if (!strcasestr(headers2, header1)) {
14697 different = 1;
14698 break;
14699 }
14700 }
14701
14702 return different;
14703 }
14704
14705 static int sip_uri_cmp(const char *input1, const char *input2)
14706 {
14707 char *uri1 = ast_strdupa(input1);
14708 char *uri2 = ast_strdupa(input2);
14709 char *host1;
14710 char *host2;
14711 char *params1;
14712 char *params2;
14713 char *headers1;
14714 char *headers2;
14715
14716
14717
14718
14719 strsep(&uri1, ":");
14720 strsep(&uri2, ":");
14721
14722 if ((host1 = strchr(uri1, '@'))) {
14723 *host1++ = '\0';
14724 }
14725 if ((host2 = strchr(uri2, '@'))) {
14726 *host2++ = '\0';
14727 }
14728
14729
14730
14731
14732 if ((host1 && !host2) ||
14733 (host2 && !host1) ||
14734 (host1 && host2 && strcmp(uri1, uri2))) {
14735 return 1;
14736 }
14737
14738 if (!host1)
14739 host1 = uri1;
14740 if (!host2)
14741 host2 = uri2;
14742
14743
14744
14745
14746
14747 if ((params1 = strchr(host1, ';'))) {
14748 *params1++ = '\0';
14749 }
14750 if ((params2 = strchr(host2, ';'))) {
14751 *params2++ = '\0';
14752 }
14753
14754
14755
14756
14757 if ((headers1 = strchr(S_OR(params1, host1), '?'))) {
14758 *headers1++ = '\0';
14759 }
14760 if ((headers2 = strchr(S_OR(params2, host2), '?'))) {
14761 *headers2++ = '\0';
14762 }
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774 if (strcasecmp(host1, host2)) {
14775 return 1;
14776 }
14777
14778
14779 if (sip_uri_headers_cmp(headers1, headers2)) {
14780 return 1;
14781 }
14782
14783
14784 return sip_uri_params_cmp(params1, params2);
14785 }
14786
14787
14788
14789
14790
14791
14792
14793
14794 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)
14795 {
14796 int res = 1;
14797 int gotdest;
14798 const char *p_replaces;
14799 char *replace_id = NULL;
14800 const char *required;
14801 unsigned int required_profile = 0;
14802 struct ast_channel *c = NULL;
14803 int reinvite = 0;
14804
14805
14806 if (!p->sipoptions) {
14807 const char *supported = get_header(req, "Supported");
14808 if (!ast_strlen_zero(supported))
14809 parse_sip_options(p, supported);
14810 }
14811
14812
14813 required = get_header(req, "Require");
14814 if (!ast_strlen_zero(required)) {
14815 required_profile = parse_sip_options(NULL, required);
14816 if (required_profile && !(required_profile & SIP_OPT_REPLACES)) {
14817
14818 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
14819 ast_log(LOG_WARNING,"Received SIP INVITE with unsupported required extension: %s\n", required);
14820 p->invitestate = INV_COMPLETED;
14821 if (!p->lastinvite)
14822 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14823 return -1;
14824 }
14825 }
14826
14827
14828 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->invitestate != INV_TERMINATED && p->invitestate != INV_CONFIRMED)) {
14829
14830
14831
14832
14833
14834 int different;
14835 if (pedanticsipchecking)
14836 different = sip_uri_cmp(p->initreq.rlPart2, req->rlPart2);
14837 else
14838 different = strcmp(p->initreq.rlPart2, req->rlPart2);
14839 if (!different) {
14840 transmit_response(p, "482 Loop Detected", req);
14841 p->invitestate = INV_COMPLETED;
14842 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14843 return 0;
14844 } else {
14845
14846
14847
14848
14849 char *uri = ast_strdupa(req->rlPart2);
14850 char *at = strchr(uri, '@');
14851 char *peerorhost;
14852 if (option_debug > 2) {
14853 ast_log(LOG_DEBUG, "Potential spiral detected. Original RURI was %s, new RURI is %s\n", p->initreq.rlPart2, req->rlPart2);
14854 }
14855 transmit_response(p, "100 Trying", req);
14856 if (at) {
14857 *at = '\0';
14858 }
14859
14860 if ((peerorhost = strchr(uri, ':'))) {
14861 *peerorhost++ = '\0';
14862 }
14863 ast_string_field_free(p, theirtag);
14864
14865
14866 ast_string_field_set(p->owner, call_forward, peerorhost);
14867 ast_queue_control(p->owner, AST_CONTROL_BUSY);
14868 return 0;
14869 }
14870 }
14871
14872 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->pendinginvite) {
14873 if (!ast_test_flag(&p->flags[0], SIP_OUTGOING) && (p->invitestate == INV_COMPLETED || p->invitestate == INV_TERMINATED)) {
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884 __sip_ack(p, p->pendinginvite, FLAG_RESPONSE, 0);
14885 } else {
14886
14887 p->glareinvite = seqno;
14888 if (p->rtp && find_sdp(req)) {
14889 struct sockaddr_in sin;
14890 if (get_ip_and_port_from_sdp(req, SDP_AUDIO, &sin)) {
14891 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Audio may not work properly on this call.\n");
14892 } else {
14893 ast_rtp_set_alt_peer(p->rtp, &sin);
14894 }
14895 if (p->vrtp) {
14896 if (get_ip_and_port_from_sdp(req, SDP_VIDEO, &sin)) {
14897 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Video may not work properly on this call.\n");
14898 } else {
14899 ast_rtp_set_alt_peer(p->vrtp, &sin);
14900 }
14901 }
14902 }
14903 transmit_response_reliable(p, "491 Request Pending", req);
14904 if (option_debug)
14905 ast_log(LOG_DEBUG, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
14906
14907 return 0;
14908 }
14909 }
14910
14911 p_replaces = get_header(req, "Replaces");
14912 if (!ast_strlen_zero(p_replaces)) {
14913
14914 char *ptr;
14915 char *fromtag = NULL;
14916 char *totag = NULL;
14917 char *start, *to;
14918 int error = 0;
14919
14920 if (p->owner) {
14921 if (option_debug > 2)
14922 ast_log(LOG_DEBUG, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
14923 transmit_response_reliable(p, "400 Bad request", req);
14924
14925 return -1;
14926 }
14927
14928 if (sipdebug && option_debug > 2)
14929 ast_log(LOG_DEBUG, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
14930
14931 replace_id = ast_strdupa(p_replaces);
14932 ast_uri_decode(replace_id);
14933
14934 if (!p->refer && !sip_refer_allocate(p)) {
14935 transmit_response_reliable(p, "500 Server Internal Error", req);
14936 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
14937 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14938 p->invitestate = INV_COMPLETED;
14939 return -1;
14940 }
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951 replace_id = ast_skip_blanks(replace_id);
14952
14953 start = replace_id;
14954 while ( (ptr = strsep(&start, ";")) ) {
14955 ptr = ast_skip_blanks(ptr);
14956 if ( (to = strcasestr(ptr, "to-tag=") ) )
14957 totag = to + 7;
14958 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
14959 fromtag = to + 9;
14960 fromtag = strsep(&fromtag, "&");
14961 }
14962 }
14963
14964 if (sipdebug && option_debug > 3)
14965 ast_log(LOG_DEBUG,"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>");
14966
14967
14968
14969
14970
14971 if ((p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
14972 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
14973 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
14974 error = 1;
14975 }
14976
14977
14978
14979
14980
14981
14982
14983 if (p->refer->refer_call == p) {
14984 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
14985 p->refer->refer_call = NULL;
14986 transmit_response_reliable(p, "400 Bad request", req);
14987 error = 1;
14988 }
14989
14990 if (!error && !p->refer->refer_call->owner) {
14991
14992 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
14993
14994 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replace)", req);
14995 error = 1;
14996 }
14997
14998 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 ) {
14999 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
15000 transmit_response_reliable(p, "603 Declined (Replaces)", req);
15001 error = 1;
15002 }
15003
15004 if (error) {
15005 append_history(p, "Xfer", "INVITE/Replace Failed.");
15006 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15007 ast_mutex_unlock(&p->lock);
15008 if (p->refer->refer_call) {
15009 ast_mutex_unlock(&p->refer->refer_call->lock);
15010 if (p->refer->refer_call->owner) {
15011 ast_channel_unlock(p->refer->refer_call->owner);
15012 }
15013 }
15014 p->invitestate = INV_COMPLETED;
15015 return -1;
15016 }
15017 }
15018
15019
15020
15021
15022
15023 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
15024 int newcall = (p->initreq.headers ? TRUE : FALSE);
15025
15026 if (sip_cancel_destroy(p))
15027 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15028
15029 p->pendinginvite = seqno;
15030 check_via(p, req);
15031
15032 copy_request(&p->initreq, req);
15033 if (!p->owner) {
15034 if (debug)
15035 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
15036 if (newcall)
15037 append_history(p, "Invite", "New call: %s", p->callid);
15038 parse_ok_contact(p, req);
15039 } else {
15040 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
15041
15042 if (find_sdp(req)) {
15043 if (process_sdp(p, req)) {
15044 transmit_response_reliable(p, "488 Not acceptable here", req);
15045 if (!p->lastinvite)
15046 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15047 return -1;
15048 }
15049 ast_queue_control(p->owner, AST_CONTROL_SRCUPDATE);
15050 } else {
15051 p->jointcapability = p->capability;
15052 if (option_debug > 2)
15053 ast_log(LOG_DEBUG, "Hm.... No sdp for the moment\n");
15054
15055
15056
15057 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
15058 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
15059
15060 ast_queue_frame(p->owner, &ast_null_frame);
15061 change_hold_state(p, req, FALSE, 0);
15062 }
15063 }
15064 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
15065 append_history(p, "ReInv", "Re-invite received");
15066 }
15067 } else if (debug)
15068 ast_verbose("Ignoring this INVITE request\n");
15069
15070
15071 if (!p->lastinvite && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner) {
15072
15073
15074 res = check_user(p, req, SIP_INVITE, e, XMIT_RELIABLE, sin);
15075 if (res == AUTH_CHALLENGE_SENT) {
15076 p->invitestate = INV_COMPLETED;
15077 return 0;
15078 }
15079 if (res < 0) {
15080 if (res == AUTH_FAKE_AUTH) {
15081 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
15082 transmit_fake_auth_response(p, SIP_INVITE, req, XMIT_RELIABLE);
15083 } else {
15084 ast_log(LOG_NOTICE, "Failed to authenticate user %s\n", get_header(req, "From"));
15085 transmit_response_reliable(p, "403 Forbidden", req);
15086 }
15087 p->invitestate = INV_COMPLETED;
15088 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15089 ast_string_field_free(p, theirtag);
15090 return 0;
15091 }
15092
15093
15094 if (find_sdp(req)) {
15095 if (process_sdp(p, req)) {
15096
15097 transmit_response_reliable(p, "488 Not acceptable here", req);
15098 p->invitestate = INV_COMPLETED;
15099 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15100 if (option_debug)
15101 ast_log(LOG_DEBUG, "No compatible codecs for this SIP call.\n");
15102 return -1;
15103 }
15104 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CONSTANT_SSRC)) {
15105 if (p->rtp) {
15106 ast_rtp_set_constantssrc(p->rtp);
15107 }
15108 if (p->vrtp) {
15109 ast_rtp_set_constantssrc(p->vrtp);
15110 }
15111 }
15112 } else {
15113 p->jointcapability = p->capability;
15114 if (option_debug > 1)
15115 ast_log(LOG_DEBUG, "No SDP in Invite, third party call control\n");
15116 }
15117
15118
15119
15120 if (p->owner)
15121 ast_queue_frame(p->owner, &ast_null_frame);
15122
15123
15124
15125 if (ast_strlen_zero(p->context))
15126 ast_string_field_set(p, context, default_context);
15127
15128
15129
15130 if (option_debug)
15131 ast_log(LOG_DEBUG, "Checking SIP call limits for device %s\n", p->username);
15132 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
15133 if (res < 0) {
15134 ast_log(LOG_NOTICE, "Failed to place call for user %s, too many calls\n", p->username);
15135 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
15136 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15137 p->invitestate = INV_COMPLETED;
15138 }
15139 return 0;
15140 }
15141 gotdest = get_destination(p, NULL);
15142 get_rdnis(p, NULL);
15143 extract_uri(p, req);
15144 build_contact(p);
15145
15146 if (p->rtp) {
15147 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
15148 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
15149 }
15150
15151 if (!replace_id && gotdest) {
15152 if (gotdest == 1 && ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP))
15153 transmit_response_reliable(p, "484 Address Incomplete", req);
15154 else {
15155 char *decoded_exten = ast_strdupa(p->exten);
15156
15157 transmit_response_reliable(p, "404 Not Found", req);
15158 ast_uri_decode(decoded_exten);
15159 ast_log(LOG_NOTICE, "Call from '%s' to extension"
15160 " '%s' rejected because extension not found.\n",
15161 S_OR(p->username, p->peername), decoded_exten);
15162 }
15163 p->invitestate = INV_COMPLETED;
15164 update_call_counter(p, DEC_CALL_LIMIT);
15165 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15166 return 0;
15167 } else {
15168
15169
15170 if (ast_strlen_zero(p->exten))
15171 ast_string_field_set(p, exten, "s");
15172
15173
15174 make_our_tag(p->tag, sizeof(p->tag));
15175
15176 c = sip_new(p, AST_STATE_DOWN, S_OR(p->peername, NULL));
15177 *recount = 1;
15178
15179
15180 build_route(p, req, 0);
15181
15182 if (c) {
15183
15184 ast_channel_lock(c);
15185 }
15186 }
15187 } else {
15188 if (option_debug > 1 && sipdebug) {
15189 if (!ast_test_flag(req, SIP_PKT_IGNORE))
15190 ast_log(LOG_DEBUG, "Got a SIP re-invite for call %s\n", p->callid);
15191 else
15192 ast_log(LOG_DEBUG, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
15193 }
15194 if (!ast_test_flag(req, SIP_PKT_IGNORE))
15195 reinvite = 1;
15196 c = p->owner;
15197 }
15198
15199 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
15200 p->lastinvite = seqno;
15201
15202 if (replace_id) {
15203
15204 if (sipdebug && option_debug > 3)
15205 ast_log(LOG_DEBUG, "Sending this call to the invite/replcaes handler %s\n", p->callid);
15206 return handle_invite_replaces(p, req, debug, ast_test_flag(req, SIP_PKT_IGNORE), seqno, sin, nounlock);
15207 }
15208
15209
15210 if (c) {
15211 enum ast_channel_state c_state = c->_state;
15212
15213 if (c_state != AST_STATE_UP && reinvite &&
15214 (p->invitestate == INV_TERMINATED || p->invitestate == INV_CONFIRMED)) {
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224 c_state = AST_STATE_UP;
15225 }
15226
15227 switch(c_state) {
15228 case AST_STATE_DOWN:
15229 if (option_debug > 1)
15230 ast_log(LOG_DEBUG, "%s: New call is still down.... Trying... \n", c->name);
15231 transmit_provisional_response(p, "100 Trying", req, 0);
15232 p->invitestate = INV_PROCEEDING;
15233 ast_setstate(c, AST_STATE_RING);
15234 if (strcmp(p->exten, ast_pickup_ext())) {
15235 enum ast_pbx_result res;
15236
15237 res = ast_pbx_start(c);
15238
15239 switch(res) {
15240 case AST_PBX_FAILED:
15241 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
15242 p->invitestate = INV_COMPLETED;
15243 if (ast_test_flag(req, SIP_PKT_IGNORE))
15244 transmit_response(p, "503 Unavailable", req);
15245 else
15246 transmit_response_reliable(p, "503 Unavailable", req);
15247 break;
15248 case AST_PBX_CALL_LIMIT:
15249 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
15250 p->invitestate = INV_COMPLETED;
15251 if (ast_test_flag(req, SIP_PKT_IGNORE))
15252 transmit_response(p, "480 Temporarily Unavailable", req);
15253 else
15254 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
15255 break;
15256 case AST_PBX_SUCCESS:
15257
15258 break;
15259 }
15260
15261 if (res) {
15262
15263
15264 ast_mutex_unlock(&c->lock);
15265 ast_mutex_unlock(&p->lock);
15266 ast_hangup(c);
15267 ast_mutex_lock(&p->lock);
15268 c = NULL;
15269 }
15270 } else {
15271 ast_channel_unlock(c);
15272 *nounlock = 1;
15273 if (ast_pickup_call(c)) {
15274 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
15275 if (ast_test_flag(req, SIP_PKT_IGNORE))
15276 transmit_response(p, "503 Unavailable", req);
15277 else
15278 transmit_response_reliable(p, "503 Unavailable", req);
15279 sip_alreadygone(p);
15280
15281 ast_mutex_unlock(&p->lock);
15282 c->hangupcause = AST_CAUSE_CALL_REJECTED;
15283 } else {
15284 ast_mutex_unlock(&p->lock);
15285 ast_setstate(c, AST_STATE_DOWN);
15286 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
15287 }
15288 p->invitestate = INV_COMPLETED;
15289 ast_hangup(c);
15290 ast_mutex_lock(&p->lock);
15291 c = NULL;
15292 }
15293 break;
15294 case AST_STATE_RING:
15295 transmit_provisional_response(p, "100 Trying", req, 0);
15296 p->invitestate = INV_PROCEEDING;
15297 break;
15298 case AST_STATE_RINGING:
15299 transmit_provisional_response(p, "180 Ringing", req, 0);
15300 p->invitestate = INV_PROCEEDING;
15301 break;
15302 case AST_STATE_UP:
15303 if (option_debug > 1)
15304 ast_log(LOG_DEBUG, "%s: This call is UP.... \n", c->name);
15305
15306 transmit_response(p, "100 Trying", req);
15307
15308 if (p->t38.state == T38_PEER_REINVITE) {
15309 struct ast_channel *bridgepeer = NULL;
15310 struct sip_pvt *bridgepvt = NULL;
15311
15312 if ((bridgepeer = ast_bridged_channel(p->owner))) {
15313
15314
15315 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
15316 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
15317 if (bridgepvt->t38.state == T38_DISABLED) {
15318 if (bridgepvt->udptl) {
15319
15320 sip_handle_t38_reinvite(bridgepeer, p, 1);
15321 } else {
15322 ast_log(LOG_WARNING, "Strange... The other side of the bridge don't have udptl struct\n");
15323 ast_mutex_lock(&bridgepvt->lock);
15324 bridgepvt->t38.state = T38_DISABLED;
15325 ast_mutex_unlock(&bridgepvt->lock);
15326 if (option_debug > 1)
15327 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->name);
15328 if (ast_test_flag(req, SIP_PKT_IGNORE))
15329 transmit_response(p, "488 Not acceptable here", req);
15330 else
15331 transmit_response_reliable(p, "488 Not acceptable here", req);
15332
15333 }
15334 } else {
15335
15336 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
15337 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
15338 p->t38.state = T38_ENABLED;
15339 if (option_debug)
15340 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
15341 }
15342 } else {
15343
15344 if (ast_test_flag(req, SIP_PKT_IGNORE))
15345 transmit_response(p, "488 Not acceptable here", req);
15346 else
15347 transmit_response_reliable(p, "488 Not acceptable here", req);
15348 p->t38.state = T38_DISABLED;
15349 if (option_debug > 1)
15350 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
15351
15352 if (!p->lastinvite)
15353 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15354 }
15355 } else {
15356
15357 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
15358 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
15359 p->t38.state = T38_ENABLED;
15360 if (option_debug)
15361 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
15362 }
15363 } else if (p->t38.state == T38_DISABLED) {
15364
15365 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
15366 transmit_response_with_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (ast_test_flag(req, SIP_PKT_IGNORE) ? XMIT_UNRELIABLE : XMIT_CRITICAL)));
15367 }
15368 p->invitestate = INV_TERMINATED;
15369 break;
15370 default:
15371 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
15372 transmit_response(p, "100 Trying", req);
15373 break;
15374 }
15375 } else {
15376 if (p && (p->autokillid == -1)) {
15377 const char *msg;
15378
15379 if (!p->jointcapability)
15380 msg = "488 Not Acceptable Here (codec error)";
15381 else {
15382 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
15383 msg = "503 Unavailable";
15384 }
15385 if (ast_test_flag(req, SIP_PKT_IGNORE))
15386 transmit_response(p, msg, req);
15387 else
15388 transmit_response_reliable(p, msg, req);
15389 p->invitestate = INV_COMPLETED;
15390 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15391 }
15392 }
15393 return res;
15394 }
15395
15396
15397
15398 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno)
15399 {
15400 struct sip_dual target;
15401
15402 int res = 0;
15403 struct sip_pvt *targetcall_pvt;
15404
15405
15406 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
15407 transferer->refer->replaces_callid_fromtag))) {
15408 if (transferer->refer->localtransfer) {
15409
15410 transmit_response(transferer, "202 Accepted", req);
15411
15412
15413 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
15414 append_history(transferer, "Xfer", "Refer failed");
15415 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
15416 transferer->refer->status = REFER_FAILED;
15417 return -1;
15418 }
15419
15420 if (option_debug > 2)
15421 ast_log(LOG_DEBUG, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
15422 return 0;
15423 }
15424
15425
15426 transmit_response(transferer, "202 Accepted", req);
15427 append_history(transferer, "Xfer", "Refer accepted");
15428 if (!targetcall_pvt->owner) {
15429 if (option_debug > 3)
15430 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No owner of target call\n");
15431
15432 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
15433 append_history(transferer, "Xfer", "Refer failed");
15434 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
15435 transferer->refer->status = REFER_FAILED;
15436 ast_mutex_unlock(&targetcall_pvt->lock);
15437 return -1;
15438 }
15439
15440
15441 target.chan1 = targetcall_pvt->owner;
15442 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
15443
15444 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
15445
15446 if (option_debug > 3) {
15447 if (target.chan2)
15448 ast_log(LOG_DEBUG, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
15449 else if (target.chan1->_state != AST_STATE_RING)
15450 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No target channel\n");
15451 else
15452 ast_log(LOG_DEBUG, "SIP attended transfer: Attempting transfer in ringing state\n");
15453 }
15454 }
15455
15456
15457 if (option_debug > 3 && sipdebug) {
15458 if (current->chan2)
15459 ast_log(LOG_DEBUG, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
15460 else
15461 ast_log(LOG_DEBUG, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
15462 }
15463
15464 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
15465
15466
15467 res = attempt_transfer(current, &target);
15468 ast_mutex_unlock(&targetcall_pvt->lock);
15469 if (res) {
15470
15471 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
15472 append_history(transferer, "Xfer", "Refer failed");
15473 transferer->refer->status = REFER_FAILED;
15474 if (targetcall_pvt->owner)
15475 ast_channel_unlock(targetcall_pvt->owner);
15476
15477 if (res != -2)
15478 ast_hangup(transferer->owner);
15479 else
15480 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
15481 } else {
15482
15483
15484
15485 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
15486 append_history(transferer, "Xfer", "Refer succeeded");
15487 transferer->refer->status = REFER_200OK;
15488 if (targetcall_pvt->owner) {
15489 if (option_debug)
15490 ast_log(LOG_DEBUG, "SIP attended transfer: Unlocking channel %s\n", targetcall_pvt->owner->name);
15491 ast_channel_unlock(targetcall_pvt->owner);
15492 }
15493 }
15494 return 1;
15495 }
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock)
15562 {
15563 struct sip_dual current;
15564
15565
15566 int res = 0;
15567
15568 if (ast_test_flag(req, SIP_PKT_DEBUG))
15569 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");
15570
15571 if (!p->owner) {
15572
15573
15574 if (option_debug > 2)
15575 ast_log(LOG_DEBUG, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
15576 transmit_response(p, "603 Declined (No dialog)", req);
15577 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
15578 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
15579 sip_alreadygone(p);
15580 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15581 }
15582 return 0;
15583 }
15584
15585
15586
15587 if (p->allowtransfer == TRANSFER_CLOSED ) {
15588
15589 transmit_response(p, "603 Declined (policy)", req);
15590 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
15591
15592 return 0;
15593 }
15594
15595 if(!ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
15596
15597 transmit_response(p, "491 Request pending", req);
15598 append_history(p, "Xfer", "Refer failed. Request pending.");
15599 return 0;
15600 }
15601
15602
15603 if (!p->refer && !sip_refer_allocate(p)) {
15604 transmit_response(p, "500 Internal Server Error", req);
15605 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
15606 return -3;
15607 }
15608
15609 res = get_refer_info(p, req);
15610
15611 p->refer->status = REFER_SENT;
15612
15613 if (res != 0) {
15614 switch (res) {
15615 case -2:
15616 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
15617 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
15618 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
15619 ast_log(LOG_DEBUG, "SIP transfer to black hole can't be handled (no refer-to: )\n");
15620 break;
15621 case -3:
15622 transmit_response(p, "603 Declined (Non sip: uri)", req);
15623 append_history(p, "Xfer", "Refer failed. Non SIP uri");
15624 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
15625 ast_log(LOG_DEBUG, "SIP transfer to non-SIP uri denied\n");
15626 break;
15627 default:
15628
15629 transmit_response(p, "202 Accepted", req);
15630 append_history(p, "Xfer", "Refer failed. Bad extension.");
15631 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
15632 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
15633 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
15634 ast_log(LOG_DEBUG, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
15635 break;
15636 }
15637 return 0;
15638 }
15639 if (ast_strlen_zero(p->context))
15640 ast_string_field_set(p, context, default_context);
15641
15642
15643 if (allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
15644 p->refer->localtransfer = 1;
15645 if (sipdebug && option_debug > 2)
15646 ast_log(LOG_DEBUG, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
15647 } else if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
15648
15649 p->refer->localtransfer = 1;
15650 } else if (sipdebug && option_debug > 2)
15651 ast_log(LOG_DEBUG, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
15652
15653
15654
15655 if (ignore)
15656 return res;
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684 current.chan1 = p->owner;
15685
15686
15687 current.chan2 = ast_bridged_channel(current.chan1);
15688
15689 if (sipdebug && option_debug > 2)
15690 ast_log(LOG_DEBUG, "SIP %s transfer: Transferer channel %s, transferee channel %s\n", p->refer->attendedtransfer ? "attended" : "blind", current.chan1->name, current.chan2 ? current.chan2->name : "<none>");
15691
15692 if (!current.chan2 && !p->refer->attendedtransfer) {
15693
15694
15695
15696 if (sipdebug && option_debug > 2)
15697 ast_log(LOG_DEBUG,"Refused SIP transfer on non-bridged channel.\n");
15698 p->refer->status = REFER_FAILED;
15699 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
15700 transmit_response(p, "603 Declined", req);
15701 return -1;
15702 }
15703
15704 if (current.chan2) {
15705 if (sipdebug && option_debug > 3)
15706 ast_log(LOG_DEBUG, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
15707
15708 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
15709 }
15710
15711 ast_set_flag(&p->flags[0], SIP_GOTREFER);
15712
15713
15714 if (p->refer->attendedtransfer) {
15715 if ((res = local_attended_transfer(p, ¤t, req, seqno)))
15716 return res;
15717
15718 if (sipdebug && option_debug > 3)
15719 ast_log(LOG_DEBUG, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
15720
15721 }
15722
15723
15724
15725 if (p->refer->localtransfer && !strcmp(p->refer->refer_to, ast_parking_ext())) {
15726
15727 *nounlock = 1;
15728 ast_channel_unlock(current.chan1);
15729 copy_request(¤t.req, req);
15730 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
15731 p->refer->status = REFER_200OK;
15732 append_history(p, "Xfer", "REFER to call parking.");
15733 if (sipdebug && option_debug > 3)
15734 ast_log(LOG_DEBUG, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
15735 sip_park(current.chan2, current.chan1, req, seqno);
15736 return res;
15737 }
15738
15739
15740 transmit_response(p, "202 Accepted", req);
15741
15742 if (current.chan1 && current.chan2) {
15743 if (option_debug > 2)
15744 ast_log(LOG_DEBUG, "chan1->name: %s\n", current.chan1->name);
15745 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
15746 }
15747 if (current.chan2) {
15748 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
15749 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
15750 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
15751
15752 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
15753
15754 if (p->refer->referred_by)
15755 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
15756 }
15757
15758 if (p->refer->replaces_callid && !ast_strlen_zero(p->refer->replaces_callid)) {
15759 char tempheader[SIPBUFSIZE];
15760 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
15761 p->refer->replaces_callid_totag ? ";to-tag=" : "",
15762 p->refer->replaces_callid_totag,
15763 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
15764 p->refer->replaces_callid_fromtag);
15765 if (current.chan2)
15766 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
15767 }
15768
15769
15770 *nounlock = 1;
15771 ast_channel_unlock(current.chan1);
15772
15773
15774
15775
15776 if (!p->refer->attendedtransfer)
15777 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
15778
15779
15780
15781
15782
15783 if (!current.chan2) {
15784
15785
15786
15787
15788
15789
15790
15791 p->refer->status = REFER_FAILED;
15792 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
15793 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
15794 append_history(p, "Xfer", "Refer failed (only bridged calls).");
15795 return -1;
15796 }
15797 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
15798
15799
15800
15801 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
15802
15803 if (!res) {
15804
15805 if (option_debug > 2)
15806 ast_log(LOG_DEBUG, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
15807 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
15808 if (p->refer->localtransfer)
15809 p->refer->status = REFER_200OK;
15810 if (p->owner)
15811 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
15812 append_history(p, "Xfer", "Refer succeeded.");
15813 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
15814
15815
15816 res = 0;
15817 } else {
15818 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
15819 if (option_debug > 2)
15820 ast_log(LOG_DEBUG, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
15821 append_history(p, "Xfer", "Refer failed.");
15822
15823 p->refer->status = REFER_FAILED;
15824 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
15825 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
15826 res = -1;
15827 }
15828 return res;
15829 }
15830
15831
15832 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
15833 {
15834
15835 check_via(p, req);
15836 sip_alreadygone(p);
15837
15838
15839
15840
15841
15842
15843 if (p->invitestate == INV_TERMINATED)
15844 __sip_pretend_ack(p);
15845 else
15846 p->invitestate = INV_CANCELLED;
15847
15848 if (p->owner && p->owner->_state == AST_STATE_UP) {
15849
15850 transmit_response(p, "200 OK", req);
15851 if (option_debug)
15852 ast_log(LOG_DEBUG, "Got CANCEL on an answered call. Ignoring... \n");
15853 return 0;
15854 }
15855
15856 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
15857 update_call_counter(p, DEC_CALL_LIMIT);
15858
15859 stop_media_flows(p);
15860 if (p->owner)
15861 ast_queue_hangup(p->owner);
15862 else
15863 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15864 if (p->initreq.len > 0) {
15865 struct sip_pkt *pkt, *prev_pkt;
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877 for (pkt = p->packets, prev_pkt = NULL; pkt; prev_pkt = pkt, pkt = pkt->next) {
15878 if (pkt->seqno == p->lastinvite && pkt->response_code == 487) {
15879 AST_SCHED_DEL(sched, pkt->retransid);
15880 UNLINK(pkt, p->packets, prev_pkt);
15881 ast_free(pkt);
15882 break;
15883 }
15884 }
15885 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
15886 transmit_response(p, "200 OK", req);
15887 return 1;
15888 } else {
15889 transmit_response(p, "481 Call Leg Does Not Exist", req);
15890 return 0;
15891 }
15892 }
15893
15894 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen)
15895 {
15896 struct ast_rtp_quality qos;
15897 struct sip_pvt *p = chan->tech_pvt;
15898 char *all = "", *parse = ast_strdupa(preparse);
15899 AST_DECLARE_APP_ARGS(args,
15900 AST_APP_ARG(param);
15901 AST_APP_ARG(type);
15902 AST_APP_ARG(field);
15903 );
15904 AST_STANDARD_APP_ARGS(args, parse);
15905
15906
15907 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
15908 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
15909 return 0;
15910 }
15911
15912 if (strcasecmp(args.param, "rtpqos"))
15913 return 0;
15914
15915
15916 if (ast_strlen_zero(args.type))
15917 args.type = "audio";
15918 if (ast_strlen_zero(args.field))
15919 args.field = "all";
15920
15921 memset(buf, 0, buflen);
15922 memset(&qos, 0, sizeof(qos));
15923
15924 if (p == NULL) {
15925 return -1;
15926 }
15927
15928 if (strcasecmp(args.type, "AUDIO") == 0) {
15929 all = ast_rtp_get_quality(p->rtp, &qos);
15930 } else if (strcasecmp(args.type, "VIDEO") == 0) {
15931 all = ast_rtp_get_quality(p->vrtp, &qos);
15932 }
15933
15934 if (strcasecmp(args.field, "local_ssrc") == 0)
15935 snprintf(buf, buflen, "%u", qos.local_ssrc);
15936 else if (strcasecmp(args.field, "local_lostpackets") == 0)
15937 snprintf(buf, buflen, "%u", qos.local_lostpackets);
15938 else if (strcasecmp(args.field, "local_jitter") == 0)
15939 snprintf(buf, buflen, "%.0lf", qos.local_jitter * 1000.0);
15940 else if (strcasecmp(args.field, "local_count") == 0)
15941 snprintf(buf, buflen, "%u", qos.local_count);
15942 else if (strcasecmp(args.field, "remote_ssrc") == 0)
15943 snprintf(buf, buflen, "%u", qos.remote_ssrc);
15944 else if (strcasecmp(args.field, "remote_lostpackets") == 0)
15945 snprintf(buf, buflen, "%u", qos.remote_lostpackets);
15946 else if (strcasecmp(args.field, "remote_jitter") == 0)
15947 snprintf(buf, buflen, "%.0lf", qos.remote_jitter * 1000.0);
15948 else if (strcasecmp(args.field, "remote_count") == 0)
15949 snprintf(buf, buflen, "%u", qos.remote_count);
15950 else if (strcasecmp(args.field, "rtt") == 0)
15951 snprintf(buf, buflen, "%.0lf", qos.rtt * 1000.0);
15952 else if (strcasecmp(args.field, "all") == 0)
15953 ast_copy_string(buf, all, buflen);
15954 else {
15955 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
15956 return -1;
15957 }
15958 return 0;
15959 }
15960
15961
15962 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
15963 {
15964 struct ast_channel *c=NULL;
15965 int res;
15966 struct ast_channel *bridged_to;
15967
15968
15969 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_test_flag(req, SIP_PKT_IGNORE))
15970 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
15971
15972 __sip_pretend_ack(p);
15973
15974 p->invitestate = INV_TERMINATED;
15975
15976 copy_request(&p->initreq, req);
15977 check_via(p, req);
15978 sip_alreadygone(p);
15979
15980
15981 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY) || p->owner) {
15982 char *audioqos, *videoqos;
15983 if (p->rtp) {
15984 audioqos = ast_rtp_get_quality(p->rtp, NULL);
15985 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
15986 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
15987 if (p->owner)
15988 pbx_builtin_setvar_helper(p->owner, "RTPAUDIOQOS", audioqos);
15989 }
15990 if (p->vrtp) {
15991 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
15992 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
15993 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
15994 if (p->owner)
15995 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", videoqos);
15996 }
15997 }
15998
15999 stop_media_flows(p);
16000
16001 if (!ast_strlen_zero(get_header(req, "Also"))) {
16002 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
16003 ast_inet_ntoa(p->recv.sin_addr));
16004 if (ast_strlen_zero(p->context))
16005 ast_string_field_set(p, context, default_context);
16006 res = get_also_info(p, req);
16007 if (!res) {
16008 c = p->owner;
16009 if (c) {
16010 bridged_to = ast_bridged_channel(c);
16011 if (bridged_to) {
16012
16013 ast_queue_control(c, AST_CONTROL_UNHOLD);
16014 ast_async_goto(bridged_to, p->context, p->refer->refer_to,1);
16015 } else
16016 ast_queue_hangup(p->owner);
16017 }
16018 } else {
16019 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(p->recv.sin_addr));
16020 if (p->owner)
16021 ast_queue_hangup(p->owner);
16022 }
16023 } else if (p->owner) {
16024 ast_queue_hangup(p->owner);
16025 if (option_debug > 2)
16026 ast_log(LOG_DEBUG, "Received bye, issuing owner hangup\n");
16027 } else {
16028 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16029 if (option_debug > 2)
16030 ast_log(LOG_DEBUG, "Received bye, no owner, selfdestruct soon.\n");
16031 }
16032 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16033 transmit_response(p, "200 OK", req);
16034
16035 return 1;
16036 }
16037
16038
16039 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
16040 {
16041 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
16042 if (ast_test_flag(req, SIP_PKT_DEBUG))
16043 ast_verbose("Receiving message!\n");
16044 receive_message(p, req);
16045 } else
16046 transmit_response(p, "202 Accepted", req);
16047 return 1;
16048 }
16049
16050
16051 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
16052 {
16053 int gotdest = 0;
16054 int res = 0;
16055 int firststate = AST_EXTENSION_REMOVED;
16056 struct sip_peer *authpeer = NULL;
16057 const char *eventheader = get_header(req, "Event");
16058 const char *accept = get_header(req, "Accept");
16059 int resubscribe = (p->subscribed != NONE);
16060 char *temp, *event;
16061
16062 if (p->initreq.headers) {
16063
16064 if (p->initreq.method != SIP_SUBSCRIBE) {
16065
16066
16067 transmit_response(p, "403 Forbidden (within dialog)", req);
16068
16069 if (option_debug)
16070 ast_log(LOG_DEBUG, "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);
16071 return 0;
16072 } else if (ast_test_flag(req, SIP_PKT_DEBUG)) {
16073 if (option_debug) {
16074 if (resubscribe)
16075 ast_log(LOG_DEBUG, "Got a re-subscribe on existing subscription %s\n", p->callid);
16076 else
16077 ast_log(LOG_DEBUG, "Got a new subscription %s (possibly with auth)\n", p->callid);
16078 }
16079 }
16080 }
16081
16082
16083
16084
16085 if (!global_allowsubscribe) {
16086 transmit_response(p, "403 Forbidden (policy)", req);
16087 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16088 return 0;
16089 }
16090
16091 if (!ast_test_flag(req, SIP_PKT_IGNORE) && !resubscribe) {
16092 const char *to = get_header(req, "To");
16093 char totag[128];
16094
16095
16096 if (!ast_strlen_zero(to) && gettag(req, "To", totag, sizeof(totag))) {
16097 if (ast_test_flag(req, SIP_PKT_DEBUG))
16098 ast_verbose("Received resubscription for a dialog we no longer know about. Telling remote side to subscribe again.\n");
16099 transmit_response(p, "481 Subscription does not exist", req);
16100 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16101 return 0;
16102 }
16103
16104
16105 if (ast_test_flag(req, SIP_PKT_DEBUG))
16106 ast_verbose("Creating new subscription\n");
16107
16108 copy_request(&p->initreq, req);
16109 check_via(p, req);
16110 build_route(p, req, 0);
16111 } else if (ast_test_flag(req, SIP_PKT_DEBUG) && ast_test_flag(req, SIP_PKT_IGNORE))
16112 ast_verbose("Ignoring this SUBSCRIBE request\n");
16113
16114
16115 if (ast_strlen_zero(eventheader)) {
16116 transmit_response(p, "489 Bad Event", req);
16117 if (option_debug > 1)
16118 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: <none>\n");
16119 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16120 return 0;
16121 }
16122
16123 if ( (strchr(eventheader, ';'))) {
16124 event = ast_strdupa(eventheader);
16125 temp = strchr(event, ';');
16126 *temp = '\0';
16127
16128 } else
16129 event = (char *) eventheader;
16130
16131
16132 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, &authpeer);
16133
16134 if (res == AUTH_CHALLENGE_SENT) {
16135 if (authpeer)
16136 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16137 return 0;
16138 }
16139 if (res < 0) {
16140 if (res == AUTH_FAKE_AUTH) {
16141 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
16142 transmit_fake_auth_response(p, SIP_SUBSCRIBE, req, XMIT_UNRELIABLE);
16143 } else {
16144 ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
16145 transmit_response_reliable(p, "403 Forbidden", req);
16146 }
16147 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16148 if (authpeer)
16149 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16150 return 0;
16151 }
16152
16153
16154 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
16155 transmit_response(p, "403 Forbidden (policy)", req);
16156 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16157 if (authpeer)
16158 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16159 return 0;
16160 }
16161
16162 if (strcmp(event, "message-summary")) {
16163
16164 gotdest = get_destination(p, NULL);
16165 }
16166
16167
16168 parse_ok_contact(p, req);
16169
16170 build_contact(p);
16171 if (gotdest) {
16172 transmit_response(p, "404 Not Found", req);
16173 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16174 if (authpeer)
16175 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16176 return 0;
16177 }
16178
16179
16180 if (ast_strlen_zero(p->tag))
16181 make_our_tag(p->tag, sizeof(p->tag));
16182
16183 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
16184 unsigned int pidf_xml;
16185
16186 if (authpeer)
16187 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16188
16189
16190
16191 pidf_xml = strstr(accept, "application/pidf+xml") ? 1 : 0;
16192
16193
16194
16195 if (pidf_xml && strstr(p->useragent, "Polycom")) {
16196 p->subscribed = XPIDF_XML;
16197 } else if (pidf_xml) {
16198 p->subscribed = PIDF_XML;
16199 } else if (strstr(accept, "application/dialog-info+xml")) {
16200 p->subscribed = DIALOG_INFO_XML;
16201
16202 } else if (strstr(accept, "application/cpim-pidf+xml")) {
16203 p->subscribed = CPIM_PIDF_XML;
16204 } else if (strstr(accept, "application/xpidf+xml")) {
16205 p->subscribed = XPIDF_XML;
16206 } else if (ast_strlen_zero(accept)) {
16207 if (p->subscribed == NONE) {
16208 transmit_response(p, "489 Bad Event", req);
16209
16210 ast_log(LOG_WARNING,"SUBSCRIBE failure: no Accept header: pvt: stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
16211 p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
16212 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16213 return 0;
16214 }
16215
16216
16217 } else {
16218
16219 char mybuf[200];
16220 snprintf(mybuf,sizeof(mybuf),"489 Bad Event (format %s)", accept);
16221 transmit_response(p, mybuf, req);
16222
16223 ast_log(LOG_WARNING,"SUBSCRIBE failure: unrecognized format: '%s' pvt: subscribed: %d, stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
16224 accept, (int)p->subscribed, p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
16225 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16226 return 0;
16227 }
16228 } else if (!strcmp(event, "message-summary")) {
16229 if (!ast_strlen_zero(accept) && strcmp(accept, "application/simple-message-summary")) {
16230
16231 transmit_response(p, "406 Not Acceptable", req);
16232 if (option_debug > 1)
16233 ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", accept);
16234 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16235 if (authpeer)
16236 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16237 return 0;
16238 }
16239
16240
16241
16242
16243
16244 if (!authpeer || ast_strlen_zero(authpeer->mailbox)) {
16245 transmit_response(p, "404 Not found (no mailbox)", req);
16246 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16247 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
16248 if (authpeer)
16249 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16250 return 0;
16251 }
16252
16253 p->subscribed = MWI_NOTIFICATION;
16254 if (authpeer->mwipvt && authpeer->mwipvt != p)
16255
16256 sip_destroy(authpeer->mwipvt);
16257 authpeer->mwipvt = p;
16258 p->relatedpeer = ASTOBJ_REF(authpeer);
16259 } else {
16260 transmit_response(p, "489 Bad Event", req);
16261 if (option_debug > 1)
16262 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
16263 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16264 if (authpeer)
16265 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16266 return 0;
16267 }
16268
16269 if (p->subscribed != MWI_NOTIFICATION && !resubscribe) {
16270 if (p->stateid > -1)
16271 ast_extension_state_del(p->stateid, cb_extensionstate);
16272 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
16273 }
16274
16275 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
16276 p->lastinvite = seqno;
16277 if (p && !ast_test_flag(&p->flags[0], SIP_NEEDDESTROY)) {
16278 p->expiry = atoi(get_header(req, "Expires"));
16279
16280
16281 if (p->expiry > max_expiry)
16282 p->expiry = max_expiry;
16283 if (p->expiry < min_expiry && p->expiry > 0)
16284 p->expiry = min_expiry;
16285
16286 if (sipdebug || option_debug > 1) {
16287 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
16288 ast_log(LOG_DEBUG, "Adding subscription for mailbox notification - peer %s Mailbox %s\n", p->relatedpeer->name, p->relatedpeer->mailbox);
16289 else
16290 ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
16291 }
16292 if (p->autokillid > -1 && sip_cancel_destroy(p))
16293 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
16294 if (p->expiry > 0)
16295 sip_scheddestroy(p, (p->expiry + 10) * 1000);
16296
16297 if (p->subscribed == MWI_NOTIFICATION) {
16298 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16299 transmit_response(p, "200 OK", req);
16300 if (p->relatedpeer) {
16301 ASTOBJ_WRLOCK(p->relatedpeer);
16302 sip_send_mwi_to_peer(p->relatedpeer, TRUE);
16303 ASTOBJ_UNLOCK(p->relatedpeer);
16304 }
16305 } else {
16306 struct sip_pvt *p_old;
16307
16308 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
16309
16310 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));
16311 transmit_response(p, "404 Not found", req);
16312 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16313 return 0;
16314 }
16315 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16316 transmit_response(p, "200 OK", req);
16317 transmit_state_notify(p, firststate, 1, FALSE);
16318 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
16319
16320 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
16321
16322
16323
16324
16325
16326
16327 ast_mutex_lock(&iflock);
16328 for (p_old = iflist; p_old; p_old = p_old->next) {
16329 if (p_old == p)
16330 continue;
16331 if (p_old->initreq.method != SIP_SUBSCRIBE)
16332 continue;
16333 if (p_old->subscribed == NONE)
16334 continue;
16335 ast_mutex_lock(&p_old->lock);
16336 if (!strcmp(p_old->username, p->username)) {
16337 if (!strcmp(p_old->exten, p->exten) &&
16338 !strcmp(p_old->context, p->context)) {
16339 ast_set_flag(&p_old->flags[0], SIP_NEEDDESTROY);
16340 ast_mutex_unlock(&p_old->lock);
16341 break;
16342 }
16343 }
16344 ast_mutex_unlock(&p_old->lock);
16345 }
16346 ast_mutex_unlock(&iflock);
16347 }
16348 if (!p->expiry)
16349 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16350 }
16351 return 1;
16352 }
16353
16354
16355 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e)
16356 {
16357 enum check_auth_result res;
16358
16359
16360 if (ast_test_flag(req, SIP_PKT_DEBUG))
16361 ast_verbose("Using latest REGISTER request as basis request\n");
16362 copy_request(&p->initreq, req);
16363 check_via(p, req);
16364 if ((res = register_verify(p, sin, req, e)) < 0) {
16365 const char *reason;
16366
16367 switch (res) {
16368 case AUTH_SECRET_FAILED:
16369 reason = "Wrong password";
16370 break;
16371 case AUTH_USERNAME_MISMATCH:
16372 reason = "Username/auth name mismatch";
16373 break;
16374 case AUTH_NOT_FOUND:
16375 reason = "No matching peer found";
16376 break;
16377 case AUTH_UNKNOWN_DOMAIN:
16378 reason = "Not a local domain";
16379 break;
16380 case AUTH_PEER_NOT_DYNAMIC:
16381 reason = "Peer is not supposed to register";
16382 break;
16383 case AUTH_ACL_FAILED:
16384 reason = "Device does not match ACL";
16385 break;
16386 default:
16387 reason = "Unknown failure";
16388 break;
16389 }
16390 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
16391 get_header(req, "To"), ast_inet_ntoa(sin->sin_addr),
16392 reason);
16393 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
16394 } else
16395 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
16396
16397 if (res < 1) {
16398
16399
16400 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16401 }
16402 return res;
16403 }
16404
16405
16406
16407
16408 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
16409 {
16410
16411
16412 const char *cmd;
16413 const char *cseq;
16414 const char *useragent;
16415 int seqno;
16416 int len;
16417 int ignore = FALSE;
16418 int respid;
16419 int res = 0;
16420 int debug = sip_debug_test_pvt(p);
16421 char *e;
16422 int error = 0;
16423
16424
16425 cseq = get_header(req, "Cseq");
16426 cmd = req->header[0];
16427
16428
16429 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq)) {
16430 ast_log(LOG_ERROR, "Missing Cseq. Dropping this SIP message, it's incomplete.\n");
16431 error = 1;
16432 }
16433 if (!error && sscanf(cseq, "%30d%n", &seqno, &len) != 1) {
16434 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
16435 error = 1;
16436 }
16437 if (error) {
16438 if (!p->initreq.headers)
16439 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16440 return -1;
16441 }
16442
16443
16444 cmd = req->rlPart1;
16445 e = ast_skip_blanks(req->rlPart2);
16446
16447
16448 useragent = get_header(req, "User-Agent");
16449 if (!ast_strlen_zero(useragent))
16450 ast_string_field_set(p, useragent, useragent);
16451
16452
16453 if (req->method == SIP_RESPONSE) {
16454
16455 if (ast_strlen_zero(e)) {
16456 return 0;
16457 }
16458 if (sscanf(e, "%30d %n", &respid, &len) != 1) {
16459 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
16460 return 0;
16461 }
16462 if (respid <= 0) {
16463 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
16464 return 0;
16465 }
16466 if (!p->initreq.headers) {
16467 if (option_debug)
16468 ast_log(LOG_DEBUG, "That's odd... Got a response on a call we dont know about. Cseq %d Cmd %s\n", seqno, cmd);
16469 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16470 return 0;
16471 }
16472 if (p->ocseq && (p->ocseq < seqno)) {
16473 if (option_debug)
16474 ast_log(LOG_DEBUG, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
16475 return -1;
16476 } else {
16477 if ((respid == 200) || ((respid >= 300) && (respid <= 399))) {
16478 extract_uri(p, req);
16479 }
16480 handle_response(p, respid, e + len, req, seqno);
16481 }
16482 return 0;
16483 }
16484
16485
16486
16487
16488
16489 p->method = req->method;
16490 if (option_debug > 3)
16491 ast_log(LOG_DEBUG, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
16492
16493 if (p->icseq && (p->icseq > seqno) ) {
16494 if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
16495 if (option_debug > 2)
16496 ast_log(LOG_DEBUG, "Got CANCEL or ACK on INVITE with transactions in between.\n");
16497 } else {
16498 if (option_debug)
16499 ast_log(LOG_DEBUG, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
16500 if (req->method != SIP_ACK)
16501 transmit_response(p, "503 Server error", req);
16502 return -1;
16503 }
16504 } else if (p->icseq &&
16505 p->icseq == seqno &&
16506 req->method != SIP_ACK &&
16507 (p->method != SIP_CANCEL || ast_test_flag(&p->flags[0], SIP_ALREADYGONE))) {
16508
16509
16510
16511 ignore = 2;
16512 ast_set_flag(req, SIP_PKT_IGNORE);
16513 ast_set_flag(req, SIP_PKT_IGNORE_REQ);
16514 if (option_debug > 2)
16515 ast_log(LOG_DEBUG, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
16516 }
16517
16518 if (seqno >= p->icseq)
16519
16520
16521
16522 p->icseq = seqno;
16523
16524
16525 if (ast_strlen_zero(p->theirtag)) {
16526 char tag[128];
16527
16528 gettag(req, "From", tag, sizeof(tag));
16529 ast_string_field_set(p, theirtag, tag);
16530 }
16531 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
16532
16533 if (pedanticsipchecking) {
16534
16535
16536
16537
16538 if (!p->initreq.headers && ast_test_flag(req, SIP_PKT_WITH_TOTAG)) {
16539
16540 if (!ast_test_flag(req, SIP_PKT_IGNORE) && req->method == SIP_INVITE) {
16541 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
16542
16543 } else if (req->method != SIP_ACK) {
16544 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
16545 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16546 }
16547 return res;
16548 }
16549 }
16550
16551 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY)) {
16552 transmit_response(p, "400 Bad request", req);
16553 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16554 return -1;
16555 }
16556
16557
16558 switch (p->method) {
16559 case SIP_OPTIONS:
16560 res = handle_request_options(p, req);
16561 break;
16562 case SIP_INVITE:
16563 res = handle_request_invite(p, req, debug, seqno, sin, recount, e, nounlock);
16564 break;
16565 case SIP_REFER:
16566 res = handle_request_refer(p, req, debug, ignore, seqno, nounlock);
16567 break;
16568 case SIP_CANCEL:
16569 res = handle_request_cancel(p, req);
16570 break;
16571 case SIP_BYE:
16572 res = handle_request_bye(p, req);
16573 break;
16574 case SIP_MESSAGE:
16575 res = handle_request_message(p, req);
16576 break;
16577 case SIP_SUBSCRIBE:
16578 res = handle_request_subscribe(p, req, sin, seqno, e);
16579 break;
16580 case SIP_REGISTER:
16581 res = handle_request_register(p, req, sin, e);
16582 break;
16583 case SIP_INFO:
16584 if (ast_test_flag(req, SIP_PKT_DEBUG))
16585 ast_verbose("Receiving INFO!\n");
16586 if (!ignore)
16587 handle_request_info(p, req);
16588 else
16589 transmit_response(p, "200 OK", req);
16590 break;
16591 case SIP_NOTIFY:
16592 res = handle_request_notify(p, req, sin, seqno, e);
16593 break;
16594 case SIP_ACK:
16595
16596 if (seqno == p->pendinginvite) {
16597 p->invitestate = INV_TERMINATED;
16598 p->pendinginvite = 0;
16599 __sip_ack(p, seqno, FLAG_RESPONSE, 0);
16600 if (find_sdp(req)) {
16601 if (process_sdp(p, req))
16602 return -1;
16603 }
16604 check_pendings(p);
16605 } else if (p->glareinvite == seqno) {
16606
16607 p->glareinvite = 0;
16608 __sip_ack(p, seqno, 1, 0);
16609 }
16610
16611 if (!p->lastinvite && ast_strlen_zero(p->randdata))
16612 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16613 break;
16614 default:
16615 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
16616 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
16617 cmd, ast_inet_ntoa(p->sa.sin_addr));
16618
16619 if (!p->initreq.headers)
16620 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16621 break;
16622 }
16623 return res;
16624 }
16625
16626 static void process_request_queue(struct sip_pvt *p, int *recount, int *nounlock)
16627 {
16628 struct sip_request *req;
16629
16630 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
16631 if (handle_request(p, req, &p->recv, recount, nounlock) == -1) {
16632
16633 if (option_debug) {
16634 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
16635 }
16636 }
16637 ast_free(req);
16638 }
16639 }
16640
16641 static int scheduler_process_request_queue(const void *data)
16642 {
16643 struct sip_pvt *p = (struct sip_pvt *) data;
16644 int recount = 0;
16645 int nounlock = 0;
16646 int lockretry;
16647
16648 for (lockretry = 10; lockretry > 0; lockretry--) {
16649 ast_mutex_lock(&p->lock);
16650
16651
16652
16653 if (!p->owner || !ast_channel_trylock(p->owner)) {
16654 break;
16655 }
16656
16657 if (lockretry != 1) {
16658 ast_mutex_unlock(&p->lock);
16659
16660 usleep(1);
16661 }
16662 }
16663
16664 if (!lockretry) {
16665 int retry = !AST_LIST_EMPTY(&p->request_queue);
16666
16667
16668
16669
16670
16671
16672 ast_mutex_unlock(&p->lock);
16673 return retry;
16674 };
16675
16676 process_request_queue(p, &recount, &nounlock);
16677 p->request_queue_sched_id = -1;
16678
16679 if (p->owner && !nounlock) {
16680 ast_channel_unlock(p->owner);
16681 }
16682 ast_mutex_unlock(&p->lock);
16683
16684 if (recount) {
16685 ast_update_use_count();
16686 }
16687
16688 return 0;
16689 }
16690
16691 static int queue_request(struct sip_pvt *p, const struct sip_request *req)
16692 {
16693 struct sip_request *newreq;
16694
16695 if (!(newreq = ast_calloc(1, sizeof(*newreq)))) {
16696 return -1;
16697 }
16698
16699 copy_request(newreq, req);
16700 AST_LIST_INSERT_TAIL(&p->request_queue, newreq, next);
16701 if (p->request_queue_sched_id == -1) {
16702 p->request_queue_sched_id = ast_sched_add(sched, 10, scheduler_process_request_queue, p);
16703 }
16704
16705 return 0;
16706 }
16707
16708
16709
16710
16711
16712
16713 static int sipsock_read(int *id, int fd, short events, void *ignore)
16714 {
16715 struct sip_request req;
16716 struct sockaddr_in sin = { 0, };
16717 struct sip_pvt *p;
16718 int res;
16719 socklen_t len = sizeof(sin);
16720 int nounlock = 0;
16721 int recount = 0;
16722 int lockretry;
16723
16724 memset(&req, 0, sizeof(req));
16725 res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
16726 if (res < 0) {
16727 #if !defined(__FreeBSD__)
16728 if (errno == EAGAIN)
16729 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
16730 else
16731 #endif
16732 if (errno != ECONNREFUSED)
16733 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
16734 return 1;
16735 }
16736 if (option_debug && res == sizeof(req.data) - 1)
16737 ast_log(LOG_DEBUG, "Received packet exceeds buffer. Data is possibly lost\n");
16738
16739 req.data[res] = '\0';
16740 req.len = res;
16741 if(sip_debug_test_addr(&sin))
16742 ast_set_flag(&req, SIP_PKT_DEBUG);
16743 if (pedanticsipchecking)
16744 req.len = lws2sws(req.data, req.len);
16745 if (ast_test_flag(&req, SIP_PKT_DEBUG))
16746 ast_verbose("\n<--- SIP read from %s:%d --->\n%s\n<------------->\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), req.data);
16747
16748 if(parse_request(&req) == -1)
16749 return 1;
16750
16751 req.method = find_sip_method(req.rlPart1);
16752
16753 if (ast_test_flag(&req, SIP_PKT_DEBUG))
16754 ast_verbose("--- (%d headers %d lines)%s ---\n", req.headers, req.lines, (req.headers + req.lines == 0) ? " Nat keepalive" : "");
16755
16756 if (req.headers < 2)
16757 return 1;
16758
16759
16760 for (lockretry = 10; lockretry > 0; lockretry--) {
16761 ast_mutex_lock(&netlock);
16762
16763
16764 p = find_call(&req, &sin, req.method);
16765 if (p == NULL) {
16766 if (option_debug)
16767 ast_log(LOG_DEBUG, "Invalid SIP message - rejected , no callid, len %d\n", req.len);
16768 ast_mutex_unlock(&netlock);
16769 return 1;
16770 }
16771
16772
16773 if (!p->owner || !ast_channel_trylock(p->owner))
16774 break;
16775 if (lockretry != 1) {
16776 ast_mutex_unlock(&p->lock);
16777 ast_mutex_unlock(&netlock);
16778
16779 usleep(1);
16780 }
16781 }
16782 p->recv = sin;
16783
16784 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
16785 append_history(p, "Rx", "%s / %s / %s", req.data, get_header(&req, "CSeq"), req.rlPart2);
16786
16787 if (!lockretry) {
16788 if (!queue_request(p, &req)) {
16789
16790 ast_mutex_unlock(&p->lock);
16791 ast_mutex_unlock(&netlock);
16792 return 1;
16793 }
16794
16795
16796 if (p->owner)
16797 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 ??? - "));
16798 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
16799 if (req.method != SIP_ACK)
16800 transmit_response(p, "503 Server error", &req);
16801
16802 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
16803 ast_mutex_unlock(&p->lock);
16804 ast_mutex_unlock(&netlock);
16805 return 1;
16806 }
16807
16808
16809
16810
16811 if (!AST_LIST_EMPTY(&p->request_queue)) {
16812 AST_SCHED_DEL(sched, p->request_queue_sched_id);
16813 process_request_queue(p, &recount, &nounlock);
16814 }
16815
16816 if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
16817
16818 if (option_debug)
16819 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
16820 }
16821
16822 if (p->owner && !nounlock)
16823 ast_channel_unlock(p->owner);
16824 ast_mutex_unlock(&p->lock);
16825 ast_mutex_unlock(&netlock);
16826 if (recount)
16827 ast_update_use_count();
16828
16829 return 1;
16830 }
16831
16832
16833 static int sip_send_mwi_to_peer(struct sip_peer *peer, int force)
16834 {
16835
16836 struct sip_pvt *p;
16837 int newmsgs, oldmsgs;
16838
16839
16840 if (!peer->addr.sin_addr.s_addr && !peer->defaddr.sin_addr.s_addr)
16841 return 0;
16842
16843
16844 ast_app_inboxcount(peer->mailbox, &newmsgs, &oldmsgs);
16845
16846 peer->lastmsgcheck = time(NULL);
16847
16848
16849 if (!force && ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)) == peer->lastmsgssent) {
16850 return 0;
16851 }
16852
16853
16854 peer->lastmsgssent = ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs));
16855
16856 if (peer->mwipvt) {
16857
16858 p = peer->mwipvt;
16859 } else {
16860
16861 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY)))
16862 return -1;
16863 if (create_addr_from_peer(p, peer)) {
16864
16865 sip_destroy(p);
16866 return 0;
16867 }
16868
16869 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
16870 p->ourip = __ourip;
16871 build_via(p);
16872 build_callid_pvt(p);
16873
16874 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16875 }
16876
16877 ast_set_flag(&p->flags[0], SIP_OUTGOING);
16878 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
16879 return 0;
16880 }
16881
16882
16883 static int does_peer_need_mwi(struct sip_peer *peer)
16884 {
16885 time_t t = time(NULL);
16886
16887 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
16888 !peer->mwipvt) {
16889 peer->lastmsgcheck = t;
16890 return FALSE;
16891 }
16892
16893 if (!ast_strlen_zero(peer->mailbox) && (t - peer->lastmsgcheck) > global_mwitime)
16894 return TRUE;
16895
16896 return FALSE;
16897 }
16898
16899
16900
16901
16902
16903
16904 static void *do_monitor(void *data)
16905 {
16906 int res;
16907 struct sip_pvt *sip;
16908 struct sip_peer *peer = NULL;
16909 time_t t;
16910 int fastrestart = FALSE;
16911 int lastpeernum = -1;
16912 int curpeernum;
16913 int reloading;
16914
16915
16916 if (sipsock > -1)
16917 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
16918
16919
16920 for(;;) {
16921
16922 ast_mutex_lock(&sip_reload_lock);
16923 reloading = sip_reloading;
16924 sip_reloading = FALSE;
16925 ast_mutex_unlock(&sip_reload_lock);
16926 if (reloading) {
16927 if (option_verbose > 0)
16928 ast_verbose(VERBOSE_PREFIX_1 "Reloading SIP\n");
16929 sip_do_reload(sip_reloadreason);
16930
16931
16932 if (sipsock > -1) {
16933 if (sipsock_read_id)
16934 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
16935 else
16936 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
16937 } else if (sipsock_read_id) {
16938 ast_io_remove(io, sipsock_read_id);
16939 sipsock_read_id = NULL;
16940 }
16941 }
16942 restartsearch:
16943
16944 ast_mutex_lock(&iflock);
16945 t = time(NULL);
16946
16947
16948
16949
16950 for (sip = iflist; !fastrestart && sip; sip = sip->next) {
16951
16952
16953
16954
16955
16956 if (ast_mutex_trylock(&sip->lock))
16957 continue;
16958
16959
16960 if (sip->rtp && sip->owner &&
16961 (sip->owner->_state == AST_STATE_UP) &&
16962 !sip->redirip.sin_addr.s_addr &&
16963 sip->t38.state != T38_ENABLED) {
16964 if (sip->lastrtptx &&
16965 ast_rtp_get_rtpkeepalive(sip->rtp) &&
16966 (t > sip->lastrtptx + ast_rtp_get_rtpkeepalive(sip->rtp))) {
16967
16968 sip->lastrtptx = time(NULL);
16969 ast_rtp_sendcng(sip->rtp, 0);
16970 }
16971 if (sip->lastrtprx &&
16972 (ast_rtp_get_rtptimeout(sip->rtp) || ast_rtp_get_rtpholdtimeout(sip->rtp)) &&
16973 (t > sip->lastrtprx + ast_rtp_get_rtptimeout(sip->rtp))) {
16974
16975 struct sockaddr_in sin;
16976 ast_rtp_get_peer(sip->rtp, &sin);
16977 if (!ast_test_flag(&sip->flags[1], SIP_PAGE2_CALL_ONHOLD) ||
16978 (ast_rtp_get_rtpholdtimeout(sip->rtp) &&
16979 (t > sip->lastrtprx + ast_rtp_get_rtpholdtimeout(sip->rtp)))) {
16980
16981 if (ast_rtp_get_rtptimeout(sip->rtp)) {
16982 while (sip->owner && ast_channel_trylock(sip->owner)) {
16983 DEADLOCK_AVOIDANCE(&sip->lock);
16984 }
16985 if (sip->owner) {
16986 ast_log(LOG_NOTICE,
16987 "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
16988 sip->owner->name,
16989 (long) (t - sip->lastrtprx));
16990
16991 ast_softhangup_nolock(sip->owner, AST_SOFTHANGUP_DEV);
16992 ast_channel_unlock(sip->owner);
16993
16994
16995
16996
16997 ast_rtp_set_rtptimeout(sip->rtp, 0);
16998 ast_rtp_set_rtpholdtimeout(sip->rtp, 0);
16999 if (sip->vrtp) {
17000 ast_rtp_set_rtptimeout(sip->vrtp, 0);
17001 ast_rtp_set_rtpholdtimeout(sip->vrtp, 0);
17002 }
17003 }
17004 }
17005 }
17006 }
17007 }
17008
17009 if (ast_test_flag(&sip->flags[0], SIP_NEEDDESTROY) && !sip->packets &&
17010 !sip->owner) {
17011 ast_mutex_unlock(&sip->lock);
17012 __sip_destroy(sip, 1);
17013 ast_mutex_unlock(&iflock);
17014 usleep(1);
17015 goto restartsearch;
17016 }
17017 ast_mutex_unlock(&sip->lock);
17018 }
17019 ast_mutex_unlock(&iflock);
17020
17021
17022
17023
17024
17025
17026
17027 pthread_testcancel();
17028
17029 res = ast_sched_wait(sched);
17030 if ((res < 0) || (res > 1000))
17031 res = 1000;
17032
17033 if (fastrestart)
17034 res = 1;
17035 res = ast_io_wait(io, res);
17036 if (option_debug && res > 20)
17037 ast_log(LOG_DEBUG, "chan_sip: ast_io_wait ran %d all at once\n", res);
17038 ast_mutex_lock(&monlock);
17039 res = ast_sched_runq(sched);
17040 if (option_debug && res >= 20)
17041 ast_log(LOG_DEBUG, "chan_sip: ast_sched_runq ran %d all at once\n", res);
17042
17043
17044 t = time(NULL);
17045 fastrestart = FALSE;
17046 curpeernum = 0;
17047 peer = NULL;
17048
17049 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !peer, do {
17050 if ((curpeernum > lastpeernum) && does_peer_need_mwi(iterator)) {
17051 fastrestart = TRUE;
17052 lastpeernum = curpeernum;
17053 peer = ASTOBJ_REF(iterator);
17054 };
17055 curpeernum++;
17056 } while (0)
17057 );
17058
17059 if (peer) {
17060 ASTOBJ_WRLOCK(peer);
17061 sip_send_mwi_to_peer(peer, FALSE);
17062 ASTOBJ_UNLOCK(peer);
17063 ASTOBJ_UNREF(peer,sip_destroy_peer);
17064 } else {
17065
17066 lastpeernum = -1;
17067 }
17068 ast_mutex_unlock(&monlock);
17069 }
17070
17071 return NULL;
17072
17073 }
17074
17075
17076 static int restart_monitor(void)
17077 {
17078
17079 if (monitor_thread == AST_PTHREADT_STOP)
17080 return 0;
17081 ast_mutex_lock(&monlock);
17082 if (monitor_thread == pthread_self()) {
17083 ast_mutex_unlock(&monlock);
17084 ast_log(LOG_WARNING, "Cannot kill myself\n");
17085 return -1;
17086 }
17087 if (monitor_thread != AST_PTHREADT_NULL) {
17088
17089 pthread_kill(monitor_thread, SIGURG);
17090 } else {
17091
17092 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
17093 ast_mutex_unlock(&monlock);
17094 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
17095 return -1;
17096 }
17097 }
17098 ast_mutex_unlock(&monlock);
17099 return 0;
17100 }
17101
17102
17103 static int sip_poke_noanswer(const void *data)
17104 {
17105 struct sip_peer *peer = (struct sip_peer *)data;
17106
17107 peer->pokeexpire = -1;
17108 if (peer->lastms > -1) {
17109 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
17110 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE)) {
17111 ast_update_realtime("sippeers", "name", peer->name, "lastms", "-1", NULL);
17112 }
17113 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
17114 }
17115 if (peer->call)
17116 sip_destroy(peer->call);
17117 peer->call = NULL;
17118 peer->lastms = -1;
17119 ast_device_state_changed("SIP/%s", peer->name);
17120
17121
17122 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17123 struct sip_peer *peer_ptr = peer;
17124 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17125 }
17126
17127
17128
17129 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
17130 if (peer->pokeexpire == -1) {
17131 ASTOBJ_UNREF(peer, sip_destroy_peer);
17132 }
17133
17134 return 0;
17135 }
17136
17137
17138
17139
17140 static int sip_poke_peer(struct sip_peer *peer)
17141 {
17142 struct sip_pvt *p;
17143 int xmitres = 0;
17144
17145 if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
17146
17147
17148 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17149 struct sip_peer *peer_ptr = peer;
17150 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17151 }
17152 peer->lastms = 0;
17153 peer->call = NULL;
17154 return 0;
17155 }
17156 if (peer->call) {
17157 if (sipdebug)
17158 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
17159 sip_destroy(peer->call);
17160 }
17161 if (!(p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS)))
17162 return -1;
17163
17164 p->sa = peer->addr;
17165 p->recv = peer->addr;
17166 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
17167 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
17168
17169
17170 if (!ast_strlen_zero(peer->fullcontact))
17171 ast_string_field_set(p, fullcontact, peer->fullcontact);
17172
17173 if (!ast_strlen_zero(peer->tohost))
17174 ast_string_field_set(p, tohost, peer->tohost);
17175 else
17176 ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr));
17177
17178
17179 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
17180 p->ourip = __ourip;
17181 build_via(p);
17182 build_callid_pvt(p);
17183
17184 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17185 struct sip_peer *peer_ptr = peer;
17186 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17187 }
17188
17189 p->relatedpeer = ASTOBJ_REF(peer);
17190 ast_set_flag(&p->flags[0], SIP_OUTGOING);
17191 #ifdef VOCAL_DATA_HACK
17192 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
17193 xmitres = transmit_invite(p, SIP_INVITE, 0, 2);
17194 #else
17195 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2);
17196 #endif
17197 gettimeofday(&peer->ps, NULL);
17198 if (xmitres == XMIT_ERROR) {
17199 sip_poke_noanswer(ASTOBJ_REF(peer));
17200 } else {
17201 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17202 struct sip_peer *peer_ptr = peer;
17203 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17204 }
17205 peer->pokeexpire = ast_sched_add(sched, peer->maxms * 2, sip_poke_noanswer, ASTOBJ_REF(peer));
17206 if (peer->pokeexpire == -1) {
17207 struct sip_peer *peer_ptr = peer;
17208 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17209 }
17210 }
17211
17212 return 0;
17213 }
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248 static int sip_devicestate(void *data)
17249 {
17250 char *host;
17251 char *tmp;
17252
17253 struct hostent *hp;
17254 struct ast_hostent ahp;
17255 struct sip_peer *p;
17256
17257 int res = AST_DEVICE_INVALID;
17258
17259
17260 host = ast_strdupa(data ? data : "");
17261 if ((tmp = strchr(host, '@')))
17262 host = tmp + 1;
17263
17264 if (option_debug > 2)
17265 ast_log(LOG_DEBUG, "Checking device state for peer %s\n", host);
17266
17267
17268
17269
17270
17271
17272
17273
17274 if ((p = find_peer(host, NULL, 0, 1))) {
17275 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287 if (p->onHold)
17288
17289 res = AST_DEVICE_ONHOLD;
17290 else if (p->inRinging) {
17291 if (p->inRinging == p->inUse)
17292 res = AST_DEVICE_RINGING;
17293 else
17294 res = AST_DEVICE_RINGINUSE;
17295 } else if (p->call_limit && (p->inUse == p->call_limit))
17296
17297 res = AST_DEVICE_BUSY;
17298 else if (p->call_limit && p->inUse)
17299
17300 res = AST_DEVICE_INUSE;
17301 else if (p->maxms && ((p->lastms > p->maxms) || (p->lastms < 0)))
17302
17303 res = AST_DEVICE_UNAVAILABLE;
17304 else
17305 res = AST_DEVICE_NOT_INUSE;
17306 } else {
17307
17308 res = AST_DEVICE_UNAVAILABLE;
17309 }
17310 ASTOBJ_UNREF(p,sip_destroy_peer);
17311 } else {
17312 char *port = strchr(host, ':');
17313 if (port)
17314 *port = '\0';
17315 hp = ast_gethostbyname(host, &ahp);
17316 if (hp)
17317 res = AST_DEVICE_UNKNOWN;
17318 }
17319
17320 return res;
17321 }
17322
17323
17324
17325 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
17326 {
17327 int oldformat;
17328 struct sip_pvt *p;
17329 struct ast_channel *tmpc = NULL;
17330 char *ext, *host;
17331 char tmp[256];
17332 char *dest = data;
17333
17334 oldformat = format;
17335 if (!(format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1))) {
17336 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));
17337 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
17338 return NULL;
17339 }
17340 if (option_debug)
17341 ast_log(LOG_DEBUG, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
17342
17343 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE))) {
17344 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", (char *)data);
17345 *cause = AST_CAUSE_SWITCH_CONGESTION;
17346 return NULL;
17347 }
17348
17349 ast_set_flag(&p->flags[1], SIP_PAGE2_OUTGOING_CALL);
17350
17351 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
17352 sip_destroy(p);
17353 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
17354 *cause = AST_CAUSE_SWITCH_CONGESTION;
17355 return NULL;
17356 }
17357
17358 ast_copy_string(tmp, dest, sizeof(tmp));
17359 host = strchr(tmp, '@');
17360 if (host) {
17361 *host++ = '\0';
17362 ext = tmp;
17363 } else {
17364 ext = strchr(tmp, '/');
17365 if (ext)
17366 *ext++ = '\0';
17367 host = tmp;
17368 }
17369
17370 if (create_addr(p, host, NULL)) {
17371 *cause = AST_CAUSE_UNREGISTERED;
17372 if (option_debug > 2)
17373 ast_log(LOG_DEBUG, "Cant create SIP call - target device not registered\n");
17374 sip_destroy(p);
17375 return NULL;
17376 }
17377 if (ast_strlen_zero(p->peername) && ext)
17378 ast_string_field_set(p, peername, ext);
17379
17380 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
17381 p->ourip = __ourip;
17382 build_via(p);
17383 build_callid_pvt(p);
17384
17385
17386
17387
17388
17389 if (ext) {
17390 ast_string_field_set(p, username, ext);
17391 ast_string_field_free(p, fullcontact);
17392 }
17393 #if 0
17394 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
17395 #endif
17396 p->prefcodec = oldformat;
17397 ast_mutex_lock(&p->lock);
17398 tmpc = sip_new(p, AST_STATE_DOWN, host);
17399 ast_mutex_unlock(&p->lock);
17400 if (!tmpc)
17401 sip_destroy(p);
17402 ast_update_use_count();
17403 restart_monitor();
17404 return tmpc;
17405 }
17406
17407
17408
17409
17410
17411
17412
17413 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno)
17414 {
17415 static int dep_insecure_very = 0;
17416 static int dep_insecure_yes = 0;
17417
17418 if (ast_strlen_zero(value))
17419 return;
17420
17421 if (!strcasecmp(value, "very")) {
17422 ast_set_flag(flags, SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
17423 if(!dep_insecure_very) {
17424 if(lineno != -1)
17425 ast_log(LOG_WARNING, "insecure=very at line %d is deprecated; use insecure=port,invite instead\n", lineno);
17426 else
17427 ast_log(LOG_WARNING, "insecure=very is deprecated; use insecure=port,invite instead\n");
17428 dep_insecure_very = 1;
17429 }
17430 }
17431 else if (ast_true(value)) {
17432 ast_set_flag(flags, SIP_INSECURE_PORT);
17433 if(!dep_insecure_yes) {
17434 if(lineno != -1)
17435 ast_log(LOG_WARNING, "insecure=%s at line %d is deprecated; use insecure=port instead\n", value, lineno);
17436 else
17437 ast_log(LOG_WARNING, "insecure=%s is deprecated; use insecure=port instead\n", value);
17438 dep_insecure_yes = 1;
17439 }
17440 }
17441 else if (!ast_false(value)) {
17442 char buf[64];
17443 char *word, *next;
17444 ast_copy_string(buf, value, sizeof(buf));
17445 next = buf;
17446 while ((word = strsep(&next, ","))) {
17447 if (!strcasecmp(word, "port"))
17448 ast_set_flag(flags, SIP_INSECURE_PORT);
17449 else if (!strcasecmp(word, "invite"))
17450 ast_set_flag(flags, SIP_INSECURE_INVITE);
17451 else
17452 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
17453 }
17454 }
17455 }
17456
17457
17458
17459
17460
17461
17462
17463
17464 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
17465 {
17466 int res = 1;
17467
17468 if (!strcasecmp(v->name, "trustrpid")) {
17469 ast_set_flag(&mask[0], SIP_TRUSTRPID);
17470 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
17471 } else if (!strcasecmp(v->name, "sendrpid")) {
17472 ast_set_flag(&mask[0], SIP_SENDRPID);
17473 ast_set2_flag(&flags[0], ast_true(v->value), SIP_SENDRPID);
17474 } else if (!strcasecmp(v->name, "g726nonstandard")) {
17475 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
17476 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
17477 } else if (!strcasecmp(v->name, "useclientcode")) {
17478 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
17479 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
17480 } else if (!strcasecmp(v->name, "dtmfmode")) {
17481 ast_set_flag(&mask[0], SIP_DTMF);
17482 ast_clear_flag(&flags[0], SIP_DTMF);
17483 if (!strcasecmp(v->value, "inband"))
17484 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
17485 else if (!strcasecmp(v->value, "rfc2833"))
17486 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
17487 else if (!strcasecmp(v->value, "info"))
17488 ast_set_flag(&flags[0], SIP_DTMF_INFO);
17489 else if (!strcasecmp(v->value, "auto"))
17490 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
17491 else {
17492 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
17493 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
17494 }
17495 } else if (!strcasecmp(v->name, "nat")) {
17496 ast_set_flag(&mask[0], SIP_NAT);
17497 ast_clear_flag(&flags[0], SIP_NAT);
17498 if (!strcasecmp(v->value, "never"))
17499 ast_set_flag(&flags[0], SIP_NAT_NEVER);
17500 else if (!strcasecmp(v->value, "route"))
17501 ast_set_flag(&flags[0], SIP_NAT_ROUTE);
17502 else if (ast_true(v->value))
17503 ast_set_flag(&flags[0], SIP_NAT_ALWAYS);
17504 else
17505 ast_set_flag(&flags[0], SIP_NAT_RFC3581);
17506 } else if (!strcasecmp(v->name, "canreinvite")) {
17507 ast_set_flag(&mask[0], SIP_REINVITE);
17508 ast_clear_flag(&flags[0], SIP_REINVITE);
17509 if(ast_true(v->value)) {
17510 ast_set_flag(&flags[0], SIP_CAN_REINVITE | SIP_CAN_REINVITE_NAT);
17511 } else if (!ast_false(v->value)) {
17512 char buf[64];
17513 char *word, *next = buf;
17514
17515 ast_copy_string(buf, v->value, sizeof(buf));
17516 while ((word = strsep(&next, ","))) {
17517 if(!strcasecmp(word, "update")) {
17518 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_CAN_REINVITE);
17519 } else if(!strcasecmp(word, "nonat")) {
17520 ast_set_flag(&flags[0], SIP_CAN_REINVITE);
17521 ast_clear_flag(&flags[0], SIP_CAN_REINVITE_NAT);
17522 } else {
17523 ast_log(LOG_WARNING, "Unknown canreinvite mode '%s' on line %d\n", v->value, v->lineno);
17524 }
17525 }
17526 }
17527 } else if (!strcasecmp(v->name, "insecure")) {
17528 ast_set_flag(&mask[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
17529 ast_clear_flag(&flags[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
17530 set_insecure_flags(flags, v->value, v->lineno);
17531 } else if (!strcasecmp(v->name, "progressinband")) {
17532 ast_set_flag(&mask[0], SIP_PROG_INBAND);
17533 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
17534 if (ast_true(v->value))
17535 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
17536 else if (strcasecmp(v->value, "never"))
17537 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
17538 } else if (!strcasecmp(v->name, "promiscredir")) {
17539 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
17540 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
17541 } else if (!strcasecmp(v->name, "videosupport")) {
17542 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
17543 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
17544 } else if (!strcasecmp(v->name, "allowoverlap")) {
17545 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
17546 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
17547 } else if (!strcasecmp(v->name, "allowsubscribe")) {
17548 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
17549 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
17550 } else if (!strcasecmp(v->name, "t38pt_udptl")) {
17551 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_UDPTL);
17552 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);
17553 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
17554 } else if (!strcasecmp(v->name, "t38pt_rtp")) {
17555 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_RTP);
17556 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);
17557 } else if (!strcasecmp(v->name, "t38pt_tcp")) {
17558 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_TCP);
17559 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);
17560 #endif
17561 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
17562 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
17563 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
17564 } else if (!strcasecmp(v->name, "buggymwi")) {
17565 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
17566 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
17567 } else if (!strcasecmp(v->name, "t38pt_usertpsource")) {
17568 ast_set_flag(&mask[1], SIP_PAGE2_UDPTL_DESTINATION);
17569 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_UDPTL_DESTINATION);
17570 } else if (!strcasecmp(v->name, "constantssrc")) {
17571 ast_set_flag(&mask[1], SIP_PAGE2_CONSTANT_SSRC);
17572 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_CONSTANT_SSRC);
17573 } else
17574 res = 0;
17575
17576 return res;
17577 }
17578
17579
17580 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
17581 {
17582 struct domain *d;
17583
17584 if (ast_strlen_zero(domain)) {
17585 ast_log(LOG_WARNING, "Zero length domain.\n");
17586 return 1;
17587 }
17588
17589 if (!(d = ast_calloc(1, sizeof(*d))))
17590 return 0;
17591
17592 ast_copy_string(d->domain, domain, sizeof(d->domain));
17593
17594 if (!ast_strlen_zero(context))
17595 ast_copy_string(d->context, context, sizeof(d->context));
17596
17597 d->mode = mode;
17598
17599 AST_LIST_LOCK(&domain_list);
17600 AST_LIST_INSERT_TAIL(&domain_list, d, list);
17601 AST_LIST_UNLOCK(&domain_list);
17602
17603 if (sipdebug)
17604 ast_log(LOG_DEBUG, "Added local SIP domain '%s'\n", domain);
17605
17606 return 1;
17607 }
17608
17609
17610 static int check_sip_domain(const char *domain, char *context, size_t len)
17611 {
17612 struct domain *d;
17613 int result = 0;
17614
17615 AST_LIST_LOCK(&domain_list);
17616 AST_LIST_TRAVERSE(&domain_list, d, list) {
17617 if (strcasecmp(d->domain, domain))
17618 continue;
17619
17620 if (len && !ast_strlen_zero(d->context))
17621 ast_copy_string(context, d->context, len);
17622
17623 result = 1;
17624 break;
17625 }
17626 AST_LIST_UNLOCK(&domain_list);
17627
17628 return result;
17629 }
17630
17631
17632 static void clear_sip_domains(void)
17633 {
17634 struct domain *d;
17635
17636 AST_LIST_LOCK(&domain_list);
17637 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
17638 free(d);
17639 AST_LIST_UNLOCK(&domain_list);
17640 }
17641
17642
17643
17644 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno)
17645 {
17646 char authcopy[256];
17647 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
17648 struct sip_auth *a, *b, *auth;
17649
17650 if (ast_strlen_zero(configuration))
17651 return authlist;
17652
17653 if (option_debug)
17654 ast_log(LOG_DEBUG, "Auth config :: %s\n", configuration);
17655
17656 ast_copy_string(authcopy, configuration, sizeof(authcopy));
17657
17658 username = authcopy;
17659
17660 realm = strrchr(username, '@');
17661 if (realm)
17662 *realm++ = '\0';
17663 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
17664 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
17665 return authlist;
17666 }
17667
17668
17669 if ((secret = strchr(username, ':'))) {
17670 *secret++ = '\0';
17671 } else if ((md5secret = strchr(username, '#'))) {
17672 *md5secret++ = '\0';
17673 }
17674
17675 if (!(auth = ast_calloc(1, sizeof(*auth))))
17676 return authlist;
17677
17678 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
17679 ast_copy_string(auth->username, username, sizeof(auth->username));
17680 if (secret)
17681 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
17682 if (md5secret)
17683 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
17684
17685
17686 for (b = NULL, a = authlist; a ; b = a, a = a->next)
17687 ;
17688 if (b)
17689 b->next = auth;
17690 else
17691 authlist = auth;
17692
17693 if (option_verbose > 2)
17694 ast_verbose("Added authentication for realm %s\n", realm);
17695
17696 return authlist;
17697
17698 }
17699
17700
17701 static int clear_realm_authentication(struct sip_auth *authlist)
17702 {
17703 struct sip_auth *a = authlist;
17704 struct sip_auth *b;
17705
17706 while (a) {
17707 b = a;
17708 a = a->next;
17709 free(b);
17710 }
17711
17712 return 1;
17713 }
17714
17715
17716 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
17717 {
17718 struct sip_auth *a;
17719
17720 for (a = authlist; a; a = a->next) {
17721 if (!strcasecmp(a->realm, realm))
17722 break;
17723 }
17724
17725 return a;
17726 }
17727
17728
17729 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
17730 {
17731 struct sip_user *user;
17732 int format;
17733 struct ast_ha *oldha = NULL;
17734 char *varname = NULL, *varval = NULL;
17735 struct ast_variable *tmpvar = NULL;
17736 struct ast_flags userflags[2] = {{(0)}};
17737 struct ast_flags mask[2] = {{(0)}};
17738
17739
17740 if (!(user = ast_calloc(1, sizeof(*user))))
17741 return NULL;
17742
17743 suserobjs++;
17744 ASTOBJ_INIT(user);
17745 ast_copy_string(user->name, name, sizeof(user->name));
17746 oldha = user->ha;
17747 user->ha = NULL;
17748 ast_copy_flags(&user->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
17749 ast_copy_flags(&user->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
17750 user->capability = global_capability;
17751 user->allowtransfer = global_allowtransfer;
17752 user->maxcallbitrate = default_maxcallbitrate;
17753 user->autoframing = global_autoframing;
17754 user->prefs = default_prefs;
17755
17756 strcpy(user->context, default_context);
17757 strcpy(user->language, default_language);
17758 strcpy(user->mohinterpret, default_mohinterpret);
17759 strcpy(user->mohsuggest, default_mohsuggest);
17760
17761 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
17762 if (handle_common_options(&userflags[0], &mask[0], v))
17763 continue;
17764
17765 if (!strcasecmp(v->name, "context")) {
17766 ast_copy_string(user->context, v->value, sizeof(user->context));
17767 } else if (!strcasecmp(v->name, "subscribecontext")) {
17768 ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
17769 } else if (!strcasecmp(v->name, "setvar")) {
17770 varname = ast_strdupa(v->value);
17771 if ((varval = strchr(varname,'='))) {
17772 *varval++ = '\0';
17773 if ((tmpvar = ast_variable_new(varname, varval))) {
17774 tmpvar->next = user->chanvars;
17775 user->chanvars = tmpvar;
17776 }
17777 }
17778 } else if (!strcasecmp(v->name, "permit") ||
17779 !strcasecmp(v->name, "deny")) {
17780 user->ha = ast_append_ha(v->name, v->value, user->ha);
17781 } else if (!strcasecmp(v->name, "allowtransfer")) {
17782 user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
17783 } else if (!strcasecmp(v->name, "secret")) {
17784 ast_copy_string(user->secret, v->value, sizeof(user->secret));
17785 } else if (!strcasecmp(v->name, "md5secret")) {
17786 ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
17787 } else if (!strcasecmp(v->name, "callerid")) {
17788 ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
17789 } else if (!strcasecmp(v->name, "fullname")) {
17790 ast_copy_string(user->cid_name, v->value, sizeof(user->cid_name));
17791 } else if (!strcasecmp(v->name, "trunkname")) {
17792
17793 ast_copy_string(user->cid_name, "", sizeof(user->cid_name));
17794 } else if (!strcasecmp(v->name, "cid_number")) {
17795 ast_copy_string(user->cid_num, v->value, sizeof(user->cid_num));
17796 } else if (!strcasecmp(v->name, "callgroup")) {
17797 user->callgroup = ast_get_group(v->value);
17798 } else if (!strcasecmp(v->name, "pickupgroup")) {
17799 user->pickupgroup = ast_get_group(v->value);
17800 } else if (!strcasecmp(v->name, "language")) {
17801 ast_copy_string(user->language, v->value, sizeof(user->language));
17802 } else if (!strcasecmp(v->name, "mohinterpret")
17803 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
17804 ast_copy_string(user->mohinterpret, v->value, sizeof(user->mohinterpret));
17805 } else if (!strcasecmp(v->name, "mohsuggest")) {
17806 ast_copy_string(user->mohsuggest, v->value, sizeof(user->mohsuggest));
17807 } else if (!strcasecmp(v->name, "accountcode")) {
17808 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
17809 } else if (!strcasecmp(v->name, "call-limit")) {
17810 user->call_limit = atoi(v->value);
17811 if (user->call_limit < 0)
17812 user->call_limit = 0;
17813 } else if (!strcasecmp(v->name, "amaflags")) {
17814 format = ast_cdr_amaflags2int(v->value);
17815 if (format < 0) {
17816 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
17817 } else {
17818 user->amaflags = format;
17819 }
17820 } else if (!strcasecmp(v->name, "allow")) {
17821 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
17822 } else if (!strcasecmp(v->name, "disallow")) {
17823 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
17824 } else if (!strcasecmp(v->name, "autoframing")) {
17825 user->autoframing = ast_true(v->value);
17826 } else if (!strcasecmp(v->name, "callingpres")) {
17827 user->callingpres = ast_parse_caller_presentation(v->value);
17828 if (user->callingpres == -1)
17829 user->callingpres = atoi(v->value);
17830 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
17831 user->maxcallbitrate = atoi(v->value);
17832 if (user->maxcallbitrate < 0)
17833 user->maxcallbitrate = default_maxcallbitrate;
17834 }
17835
17836
17837
17838 }
17839 ast_copy_flags(&user->flags[0], &userflags[0], mask[0].flags);
17840 ast_copy_flags(&user->flags[1], &userflags[1], mask[1].flags);
17841 if (ast_test_flag(&user->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
17842 global_allowsubscribe = TRUE;
17843 ast_free_ha(oldha);
17844 return user;
17845 }
17846
17847
17848 static void set_peer_defaults(struct sip_peer *peer)
17849 {
17850 if (peer->expire == 0) {
17851
17852
17853
17854 peer->expire = -1;
17855 peer->pokeexpire = -1;
17856 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
17857 }
17858 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
17859 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
17860 strcpy(peer->context, default_context);
17861 strcpy(peer->subscribecontext, default_subscribecontext);
17862 strcpy(peer->language, default_language);
17863 strcpy(peer->mohinterpret, default_mohinterpret);
17864 strcpy(peer->mohsuggest, default_mohsuggest);
17865 peer->addr.sin_family = AF_INET;
17866 peer->defaddr.sin_family = AF_INET;
17867 peer->capability = global_capability;
17868 peer->maxcallbitrate = default_maxcallbitrate;
17869 peer->rtptimeout = global_rtptimeout;
17870 peer->rtpholdtimeout = global_rtpholdtimeout;
17871 peer->rtpkeepalive = global_rtpkeepalive;
17872 peer->allowtransfer = global_allowtransfer;
17873 peer->autoframing = global_autoframing;
17874 strcpy(peer->vmexten, default_vmexten);
17875 peer->secret[0] = '\0';
17876 peer->md5secret[0] = '\0';
17877 peer->cid_num[0] = '\0';
17878 peer->cid_name[0] = '\0';
17879 peer->fromdomain[0] = '\0';
17880 peer->fromuser[0] = '\0';
17881 peer->regexten[0] = '\0';
17882 peer->mailbox[0] = '\0';
17883 peer->callgroup = 0;
17884 peer->pickupgroup = 0;
17885 peer->maxms = default_qualify;
17886 peer->prefs = default_prefs;
17887 }
17888
17889
17890 static struct sip_peer *temp_peer(const char *name)
17891 {
17892 struct sip_peer *peer;
17893
17894 if (!(peer = ast_calloc(1, sizeof(*peer))))
17895 return NULL;
17896
17897 apeerobjs++;
17898 ASTOBJ_INIT(peer);
17899 set_peer_defaults(peer);
17900
17901 ast_copy_string(peer->name, name, sizeof(peer->name));
17902
17903 ast_set_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT);
17904 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
17905 peer->prefs = default_prefs;
17906 reg_source_db(peer);
17907
17908 return peer;
17909 }
17910
17911
17912 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only)
17913 {
17914 struct sip_peer *peer = NULL;
17915 struct ast_ha *oldha = NULL;
17916 int obproxyfound=0;
17917 int found=0;
17918 int firstpass=1;
17919 int format=0;
17920 time_t regseconds = 0;
17921 char *varname = NULL, *varval = NULL;
17922 struct ast_variable *tmpvar = NULL;
17923 struct ast_flags peerflags[2] = {{(0)}};
17924 struct ast_flags mask[2] = {{(0)}};
17925 int alt_fullcontact = alt ? 1 : 0;
17926 char fullcontact[sizeof(peer->fullcontact)] = "";
17927
17928 if (!realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS))
17929
17930
17931
17932
17933
17934 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
17935
17936 if (peer) {
17937
17938 found = 1;
17939 if (!(peer->objflags & ASTOBJ_FLAG_MARKED))
17940 firstpass = 0;
17941 } else {
17942 if (!(peer = ast_calloc(1, sizeof(*peer))))
17943 return NULL;
17944
17945 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS))
17946 rpeerobjs++;
17947 else
17948 speerobjs++;
17949 ASTOBJ_INIT(peer);
17950 }
17951
17952 if (firstpass) {
17953 peer->lastmsgssent = -1;
17954 oldha = peer->ha;
17955 peer->ha = NULL;
17956 set_peer_defaults(peer);
17957 }
17958 if (!found && name)
17959 ast_copy_string(peer->name, name, sizeof(peer->name));
17960
17961
17962 if (peer->chanvars) {
17963 ast_variables_destroy(peer->chanvars);
17964 peer->chanvars = NULL;
17965
17966 }
17967
17968 if (found)
17969 peer->portinuri = 0;
17970
17971
17972 clear_realm_authentication(peer->auth);
17973 peer->auth = NULL;
17974
17975 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
17976 if (!devstate_only) {
17977 if (handle_common_options(&peerflags[0], &mask[0], v)) {
17978 continue;
17979 }
17980 if (realtime && !strcasecmp(v->name, "regseconds")) {
17981 ast_get_time_t(v->value, ®seconds, 0, NULL);
17982 } else if (realtime && !strcasecmp(v->name, "name"))
17983 ast_copy_string(peer->name, v->value, sizeof(peer->name));
17984 else if (realtime && !strcasecmp(v->name, "fullcontact")) {
17985 if (alt_fullcontact && !alt) {
17986
17987
17988
17989
17990
17991 alt_fullcontact = 0;
17992 fullcontact[0] = '\0';
17993 }
17994
17995 if (!ast_strlen_zero(fullcontact)) {
17996 strncat(fullcontact, ";", sizeof(fullcontact) - strlen(fullcontact) - 1);
17997 strncat(fullcontact, v->value, sizeof(fullcontact) - strlen(fullcontact) - 1);
17998 } else {
17999 ast_copy_string(fullcontact, v->value, sizeof(fullcontact));
18000 ast_set_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT);
18001 }
18002 } else if (!strcasecmp(v->name, "secret"))
18003 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
18004 else if (!strcasecmp(v->name, "md5secret"))
18005 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
18006 else if (!strcasecmp(v->name, "auth"))
18007 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
18008 else if (!strcasecmp(v->name, "callerid")) {
18009 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
18010 } else if (!strcasecmp(v->name, "fullname")) {
18011 ast_copy_string(peer->cid_name, v->value, sizeof(peer->cid_name));
18012 } else if (!strcasecmp(v->name, "trunkname")) {
18013
18014 ast_copy_string(peer->cid_name, "", sizeof(peer->cid_name));
18015 } else if (!strcasecmp(v->name, "cid_number")) {
18016 ast_copy_string(peer->cid_num, v->value, sizeof(peer->cid_num));
18017 } else if (!strcasecmp(v->name, "context")) {
18018 ast_copy_string(peer->context, v->value, sizeof(peer->context));
18019 } else if (!strcasecmp(v->name, "subscribecontext")) {
18020 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
18021 } else if (!strcasecmp(v->name, "fromdomain")) {
18022 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
18023 } else if (!strcasecmp(v->name, "usereqphone")) {
18024 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
18025 } else if (!strcasecmp(v->name, "fromuser")) {
18026 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
18027 } else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "outboundproxy")) {
18028 if (!strcasecmp(v->value, "dynamic")) {
18029 if (!strcasecmp(v->name, "outboundproxy") || obproxyfound) {
18030 ast_log(LOG_WARNING, "You can't have a dynamic outbound proxy, you big silly head at line %d.\n", v->lineno);
18031 } else {
18032
18033 if (!found || !ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
18034
18035
18036 memset(&peer->addr.sin_addr, 0, 4);
18037 if (peer->addr.sin_port) {
18038
18039 peer->defaddr.sin_port = peer->addr.sin_port;
18040 peer->addr.sin_port = 0;
18041 }
18042 }
18043 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
18044 }
18045 } else {
18046
18047 if (!AST_SCHED_DEL(sched, peer->expire)) {
18048 struct sip_peer *peer_ptr = peer;
18049 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
18050 }
18051 ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
18052 if (!strcasecmp(v->name, "outboundproxy")) {
18053 if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
18054 ast_log(LOG_ERROR, "srvlookup failed for outboundproxy: %s, on peer %s, removing peer\n", v->value, peer->name);
18055 ASTOBJ_UNREF(peer, sip_destroy_peer);
18056 return NULL;
18057 }
18058 }
18059 if (!strcasecmp(v->name, "outboundproxy"))
18060 obproxyfound=1;
18061 else {
18062 ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
18063 if (!peer->addr.sin_port)
18064 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
18065 }
18066 if (global_dynamic_exclude_static) {
18067 global_contact_ha = ast_append_ha("deny", (char *)ast_inet_ntoa(peer->addr.sin_addr), global_contact_ha);
18068 }
18069 }
18070 } else if (!strcasecmp(v->name, "defaultip")) {
18071 if (ast_get_ip(&peer->defaddr, v->value)) {
18072 ASTOBJ_UNREF(peer, sip_destroy_peer);
18073 return NULL;
18074 }
18075 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
18076 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
18077 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
18078 peer->contactha = ast_append_ha(v->name + 7, v->value, peer->contactha);
18079 } else if (!strcasecmp(v->name, "port")) {
18080 peer->portinuri = 1;
18081 if (!realtime && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC))
18082 peer->defaddr.sin_port = htons(atoi(v->value));
18083 else
18084 peer->addr.sin_port = htons(atoi(v->value));
18085 } else if (!strcasecmp(v->name, "callingpres")) {
18086 peer->callingpres = ast_parse_caller_presentation(v->value);
18087 if (peer->callingpres == -1)
18088 peer->callingpres = atoi(v->value);
18089 } else if (!strcasecmp(v->name, "username")) {
18090 ast_copy_string(peer->username, v->value, sizeof(peer->username));
18091 } else if (!strcasecmp(v->name, "language")) {
18092 ast_copy_string(peer->language, v->value, sizeof(peer->language));
18093 } else if (!strcasecmp(v->name, "regexten")) {
18094 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
18095 } else if (!strcasecmp(v->name, "amaflags")) {
18096 format = ast_cdr_amaflags2int(v->value);
18097 if (format < 0) {
18098 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
18099 } else {
18100 peer->amaflags = format;
18101 }
18102 } else if (!strcasecmp(v->name, "accountcode")) {
18103 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
18104 } else if (!strcasecmp(v->name, "mohinterpret")
18105 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
18106 ast_copy_string(peer->mohinterpret, v->value, sizeof(peer->mohinterpret));
18107 } else if (!strcasecmp(v->name, "mohsuggest")) {
18108 ast_copy_string(peer->mohsuggest, v->value, sizeof(peer->mohsuggest));
18109 } else if (!strcasecmp(v->name, "mailbox")) {
18110 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
18111 } else if (!strcasecmp(v->name, "hasvoicemail")) {
18112
18113
18114 if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
18115 ast_copy_string(peer->mailbox, name, sizeof(peer->mailbox));
18116 }
18117 } else if (!strcasecmp(v->name, "subscribemwi")) {
18118 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
18119 } else if (!strcasecmp(v->name, "vmexten")) {
18120 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
18121 } else if (!strcasecmp(v->name, "callgroup")) {
18122 peer->callgroup = ast_get_group(v->value);
18123 } else if (!strcasecmp(v->name, "allowtransfer")) {
18124 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
18125 } else if (!strcasecmp(v->name, "pickupgroup")) {
18126 peer->pickupgroup = ast_get_group(v->value);
18127 } else if (!strcasecmp(v->name, "allow")) {
18128 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
18129 } else if (!strcasecmp(v->name, "disallow")) {
18130 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
18131 } else if (!strcasecmp(v->name, "autoframing")) {
18132 peer->autoframing = ast_true(v->value);
18133 } else if (!strcasecmp(v->name, "rtptimeout")) {
18134 if ((sscanf(v->value, "%30d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
18135 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
18136 peer->rtptimeout = global_rtptimeout;
18137 }
18138 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
18139 if ((sscanf(v->value, "%30d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
18140 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
18141 peer->rtpholdtimeout = global_rtpholdtimeout;
18142 }
18143 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
18144 if ((sscanf(v->value, "%30d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
18145 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
18146 peer->rtpkeepalive = global_rtpkeepalive;
18147 }
18148 } else if (!strcasecmp(v->name, "setvar")) {
18149
18150 varname = ast_strdupa(v->value);
18151 if ((varval = strchr(varname, '='))) {
18152 *varval++ = '\0';
18153 if ((tmpvar = ast_variable_new(varname, varval))) {
18154 tmpvar->next = peer->chanvars;
18155 peer->chanvars = tmpvar;
18156 }
18157 }
18158 }
18159 }
18160
18161
18162 if (realtime && !strcasecmp(v->name, "lastms")) {
18163 sscanf(v->value, "%30d", &peer->lastms);
18164 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
18165 inet_aton(v->value, &(peer->addr.sin_addr));
18166 } else if (!strcasecmp(v->name, "qualify")) {
18167 if (!strcasecmp(v->value, "no")) {
18168 peer->maxms = 0;
18169 } else if (!strcasecmp(v->value, "yes")) {
18170 peer->maxms = default_qualify ? default_qualify : DEFAULT_MAXMS;
18171 } else if (sscanf(v->value, "%30d", &peer->maxms) != 1) {
18172 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);
18173 peer->maxms = 0;
18174 }
18175 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && peer->maxms > 0) {
18176
18177
18178
18179
18180 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);
18181 peer->maxms = 0;
18182 }
18183 } else if (!strcasecmp(v->name, "call-limit") || !strcasecmp(v->name, "incominglimit")) {
18184 peer->call_limit = atoi(v->value);
18185 if (peer->call_limit < 0) {
18186 peer->call_limit = 0;
18187 }
18188 }
18189 }
18190
18191 if (!ast_strlen_zero(fullcontact)) {
18192 ast_copy_string(peer->fullcontact, fullcontact, sizeof(peer->fullcontact));
18193
18194
18195
18196
18197
18198
18199 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE) || !peer->addr.sin_addr.s_addr) {
18200 __set_address_from_contact(fullcontact, &peer->addr);
18201 }
18202 }
18203
18204 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !obproxyfound && !ast_strlen_zero(peer->tohost)) {
18205 if (ast_get_ip_or_srv(&peer->addr, peer->tohost, srvlookup && !peer->portinuri ? "_sip._udp" : NULL)) {
18206 ast_log(LOG_ERROR, "host lookup failed for %s, on peer %s, removing peer\n", peer->tohost, peer->name);
18207 ASTOBJ_UNREF(peer, sip_destroy_peer);
18208 return NULL;
18209 }
18210 }
18211
18212 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && realtime) {
18213 time_t nowtime = time(NULL);
18214
18215 if ((nowtime - regseconds) > 0) {
18216 destroy_association(peer);
18217 memset(&peer->addr, 0, sizeof(peer->addr));
18218 peer->lastms = -1;
18219 if (option_debug)
18220 ast_log(LOG_DEBUG, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
18221 }
18222 }
18223
18224
18225 if (realtime && peer->lastms > 0) {
18226 ASTOBJ_REF(peer);
18227 sip_poke_peer(peer);
18228 }
18229
18230 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
18231 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
18232 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
18233 global_allowsubscribe = TRUE;
18234 if (!found && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !ast_test_flag(&peer->flags[0], SIP_REALTIME))
18235 reg_source_db(peer);
18236 ASTOBJ_UNMARK(peer);
18237 ast_free_ha(oldha);
18238 return peer;
18239 }
18240
18241
18242
18243
18244
18245
18246
18247 static int reload_config(enum channelreloadreason reason)
18248 {
18249 struct ast_config *cfg, *ucfg;
18250 struct ast_variable *v;
18251 struct sip_peer *peer;
18252 struct sip_user *user;
18253 struct ast_hostent ahp;
18254 char *cat, *stringp, *context, *oldregcontext;
18255 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
18256 struct hostent *hp;
18257 int format;
18258 struct ast_flags dummy[2];
18259 int auto_sip_domains = FALSE;
18260 struct sockaddr_in old_bindaddr = bindaddr;
18261 int registry_count = 0, peer_count = 0, user_count = 0;
18262 unsigned int temp_tos = 0;
18263 struct ast_flags debugflag = {0};
18264
18265 cfg = ast_config_load(config);
18266
18267
18268 if (!cfg) {
18269 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
18270 return -1;
18271 }
18272
18273 if (option_debug > 3)
18274 ast_log(LOG_DEBUG, "--------------- SIP reload started\n");
18275
18276 clear_realm_authentication(authl);
18277 clear_sip_domains();
18278 authl = NULL;
18279
18280 ast_free_ha(global_contact_ha);
18281 global_contact_ha = NULL;
18282
18283
18284
18285 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
18286 ASTOBJ_RDLOCK(iterator);
18287 if (iterator->call) {
18288 if (option_debug > 2)
18289 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
18290
18291 sip_destroy(iterator->call);
18292 }
18293 ASTOBJ_UNLOCK(iterator);
18294
18295 } while(0));
18296
18297
18298 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
18299 if (option_debug > 3)
18300 ast_log(LOG_DEBUG, "--------------- Done destroying user list\n");
18301 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
18302 if (option_debug > 3)
18303 ast_log(LOG_DEBUG, "--------------- Done destroying registry list\n");
18304 ASTOBJ_CONTAINER_MARKALL(&peerl);
18305
18306
18307 ast_copy_string(oldcontexts, global_regcontext, sizeof(oldcontexts));
18308 oldregcontext = oldcontexts;
18309
18310
18311
18312 ast_copy_flags(&debugflag, &global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
18313 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
18314 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
18315 ast_copy_flags(&global_flags[1], &debugflag, SIP_PAGE2_DEBUG_CONSOLE);
18316
18317
18318 memset(&bindaddr, 0, sizeof(bindaddr));
18319 ast_free_ha(localaddr);
18320 memset(&localaddr, 0, sizeof(localaddr));
18321 memset(&externip, 0, sizeof(externip));
18322 memset(&default_prefs, 0 , sizeof(default_prefs));
18323 outboundproxyip.sin_port = htons(STANDARD_SIP_PORT);
18324 outboundproxyip.sin_family = AF_INET;
18325 ourport = STANDARD_SIP_PORT;
18326 srvlookup = DEFAULT_SRVLOOKUP;
18327 global_tos_sip = DEFAULT_TOS_SIP;
18328 global_tos_audio = DEFAULT_TOS_AUDIO;
18329 global_tos_video = DEFAULT_TOS_VIDEO;
18330 externhost[0] = '\0';
18331 externexpire = 0;
18332 externrefresh = 10;
18333 memset(&outboundproxyip, 0, sizeof(outboundproxyip));
18334
18335
18336 allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
18337 global_regcontext[0] = '\0';
18338 expiry = DEFAULT_EXPIRY;
18339 global_notifyringing = DEFAULT_NOTIFYRINGING;
18340 global_limitonpeers = FALSE;
18341 global_prematuremediafilter = FALSE;
18342 global_directrtpsetup = FALSE;
18343 global_notifyhold = FALSE;
18344 global_alwaysauthreject = 0;
18345 global_allowsubscribe = FALSE;
18346 ast_copy_string(global_useragent, DEFAULT_USERAGENT, sizeof(global_useragent));
18347 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
18348 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME))
18349 ast_copy_string(global_realm, DEFAULT_REALM, sizeof(global_realm));
18350 else
18351 ast_copy_string(global_realm, ast_config_AST_SYSTEM_NAME, sizeof(global_realm));
18352 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
18353 compactheaders = DEFAULT_COMPACTHEADERS;
18354 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
18355 global_regattempts_max = 0;
18356 pedanticsipchecking = DEFAULT_PEDANTIC;
18357 global_mwitime = DEFAULT_MWITIME;
18358 autocreatepeer = DEFAULT_AUTOCREATEPEER;
18359 global_autoframing = 0;
18360 global_allowguest = DEFAULT_ALLOWGUEST;
18361 global_rtptimeout = 0;
18362 global_rtpholdtimeout = 0;
18363 global_rtpkeepalive = 0;
18364 global_allowtransfer = TRANSFER_OPENFORALL;
18365 global_rtautoclear = 120;
18366 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
18367 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
18368 ast_set_flag(&global_flags[1], SIP_PAGE2_RTUPDATE);
18369
18370
18371 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
18372 default_subscribecontext[0] = '\0';
18373 default_language[0] = '\0';
18374 default_fromdomain[0] = '\0';
18375 default_qualify = DEFAULT_QUALIFY;
18376 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
18377 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
18378 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
18379 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
18380 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
18381 ast_set_flag(&global_flags[0], SIP_NAT_RFC3581);
18382 ast_set_flag(&global_flags[0], SIP_CAN_REINVITE);
18383
18384
18385 dumphistory = FALSE;
18386 recordhistory = FALSE;
18387 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
18388
18389
18390 global_relaxdtmf = FALSE;
18391 global_callevents = FALSE;
18392 global_t1min = DEFAULT_T1MIN;
18393 global_shrinkcallerid = 1;
18394
18395 global_matchexterniplocally = FALSE;
18396
18397
18398 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
18399
18400 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT);
18401
18402
18403 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
18404 if (handle_common_options(&global_flags[0], &dummy[0], v))
18405 continue;
18406
18407 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
18408 continue;
18409
18410
18411 if (!strcasecmp(v->name, "context")) {
18412 ast_copy_string(default_context, v->value, sizeof(default_context));
18413 } else if (!strcasecmp(v->name, "subscribecontext")) {
18414 ast_copy_string(default_subscribecontext, v->value, sizeof(default_subscribecontext));
18415 } else if (!strcasecmp(v->name, "allowguest")) {
18416 global_allowguest = ast_true(v->value) ? 1 : 0;
18417 } else if (!strcasecmp(v->name, "realm")) {
18418 ast_copy_string(global_realm, v->value, sizeof(global_realm));
18419 } else if (!strcasecmp(v->name, "useragent")) {
18420 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
18421 if (option_debug)
18422 ast_log(LOG_DEBUG, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
18423 } else if (!strcasecmp(v->name, "allowtransfer")) {
18424 global_allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
18425 } else if (!strcasecmp(v->name, "rtcachefriends")) {
18426 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
18427 } else if (!strcasecmp(v->name, "rtsavesysname")) {
18428 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTSAVE_SYSNAME);
18429 } else if (!strcasecmp(v->name, "rtupdate")) {
18430 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTUPDATE);
18431 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
18432 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_IGNOREREGEXPIRE);
18433 } else if (!strcasecmp(v->name, "t1min")) {
18434 global_t1min = atoi(v->value);
18435 } else if (!strcasecmp(v->name, "dynamic_exclude_static") || !strcasecmp(v->name, "dynamic_excludes_static")) {
18436 global_dynamic_exclude_static = ast_true(v->value);
18437 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
18438 global_contact_ha = ast_append_ha(v->name + 7, v->value, global_contact_ha);
18439 } else if (!strcasecmp(v->name, "rtautoclear")) {
18440 int i = atoi(v->value);
18441 if (i > 0)
18442 global_rtautoclear = i;
18443 else
18444 i = 0;
18445 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
18446 } else if (!strcasecmp(v->name, "usereqphone")) {
18447 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
18448 } else if (!strcasecmp(v->name, "prematuremedia")) {
18449 global_prematuremediafilter = ast_true(v->value);
18450 } else if (!strcasecmp(v->name, "relaxdtmf")) {
18451 global_relaxdtmf = ast_true(v->value);
18452 } else if (!strcasecmp(v->name, "checkmwi")) {
18453 if ((sscanf(v->value, "%30d", &global_mwitime) != 1) || (global_mwitime < 0)) {
18454 ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d. Using default (10).\n", v->value, v->lineno);
18455 global_mwitime = DEFAULT_MWITIME;
18456 }
18457 } else if (!strcasecmp(v->name, "vmexten")) {
18458 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
18459 } else if (!strcasecmp(v->name, "rtptimeout")) {
18460 if ((sscanf(v->value, "%30d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
18461 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
18462 global_rtptimeout = 0;
18463 }
18464 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
18465 if ((sscanf(v->value, "%30d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
18466 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
18467 global_rtpholdtimeout = 0;
18468 }
18469 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
18470 if ((sscanf(v->value, "%30d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
18471 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
18472 global_rtpkeepalive = 0;
18473 }
18474 } else if (!strcasecmp(v->name, "compactheaders")) {
18475 compactheaders = ast_true(v->value);
18476 } else if (!strcasecmp(v->name, "notifymimetype")) {
18477 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
18478 } else if (!strncasecmp(v->name, "limitonpeer", 11)) {
18479 global_limitonpeers = ast_true(v->value);
18480 } else if (!strcasecmp(v->name, "directrtpsetup")) {
18481 global_directrtpsetup = ast_true(v->value);
18482 } else if (!strcasecmp(v->name, "notifyringing")) {
18483 global_notifyringing = ast_true(v->value);
18484 } else if (!strcasecmp(v->name, "notifyhold")) {
18485 global_notifyhold = ast_true(v->value);
18486 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
18487 global_alwaysauthreject = ast_true(v->value);
18488 } else if (!strcasecmp(v->name, "mohinterpret")
18489 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
18490 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
18491 } else if (!strcasecmp(v->name, "mohsuggest")) {
18492 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
18493 } else if (!strcasecmp(v->name, "language")) {
18494 ast_copy_string(default_language, v->value, sizeof(default_language));
18495 } else if (!strcasecmp(v->name, "regcontext")) {
18496 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
18497 stringp = newcontexts;
18498
18499 cleanup_stale_contexts(stringp, oldregcontext);
18500
18501 while ((context = strsep(&stringp, "&"))) {
18502 if (!ast_context_find(context))
18503 ast_context_create(NULL, context,"SIP");
18504 }
18505 ast_copy_string(global_regcontext, v->value, sizeof(global_regcontext));
18506 } else if (!strcasecmp(v->name, "callerid")) {
18507 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
18508 } else if (!strcasecmp(v->name, "fromdomain")) {
18509 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
18510 } else if (!strcasecmp(v->name, "outboundproxy")) {
18511 if (ast_get_ip_or_srv(&outboundproxyip, v->value, srvlookup ? "_sip._udp" : NULL) < 0)
18512 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", v->value);
18513 } else if (!strcasecmp(v->name, "outboundproxyport")) {
18514
18515 sscanf(v->value, "%30d", &format);
18516 outboundproxyip.sin_port = htons(format);
18517 } else if (!strcasecmp(v->name, "autocreatepeer")) {
18518 autocreatepeer = ast_true(v->value);
18519 } else if (!strcasecmp(v->name, "srvlookup")) {
18520 srvlookup = ast_true(v->value);
18521 } else if (!strcasecmp(v->name, "pedantic")) {
18522 pedanticsipchecking = ast_true(v->value);
18523 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
18524 max_expiry = atoi(v->value);
18525 if (max_expiry < 1)
18526 max_expiry = DEFAULT_MAX_EXPIRY;
18527 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
18528 min_expiry = atoi(v->value);
18529 if (min_expiry < 1)
18530 min_expiry = DEFAULT_MIN_EXPIRY;
18531 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
18532 default_expiry = atoi(v->value);
18533 if (default_expiry < 1)
18534 default_expiry = DEFAULT_DEFAULT_EXPIRY;
18535 } else if (!strcasecmp(v->name, "sipdebug")) {
18536 if (ast_true(v->value))
18537 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
18538 } else if (!strcasecmp(v->name, "dumphistory")) {
18539 dumphistory = ast_true(v->value);
18540 } else if (!strcasecmp(v->name, "recordhistory")) {
18541 recordhistory = ast_true(v->value);
18542 } else if (!strcasecmp(v->name, "registertimeout")) {
18543 global_reg_timeout = atoi(v->value);
18544 if (global_reg_timeout < 1)
18545 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
18546 } else if (!strcasecmp(v->name, "registerattempts")) {
18547 global_regattempts_max = atoi(v->value);
18548 } else if (!strcasecmp(v->name, "bindaddr")) {
18549 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
18550 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
18551 } else {
18552 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
18553 }
18554 } else if (!strcasecmp(v->name, "localnet")) {
18555 struct ast_ha *na;
18556 if (!(na = ast_append_ha("d", v->value, localaddr)))
18557 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
18558 else
18559 localaddr = na;
18560 } else if (!strcasecmp(v->name, "localmask")) {
18561 ast_log(LOG_WARNING, "Use of localmask is no long supported -- use localnet with mask syntax\n");
18562 } else if (!strcasecmp(v->name, "externip")) {
18563 if (!(hp = ast_gethostbyname(v->value, &ahp)))
18564 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
18565 else
18566 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
18567 externexpire = 0;
18568 } else if (!strcasecmp(v->name, "externhost")) {
18569 ast_copy_string(externhost, v->value, sizeof(externhost));
18570 if (!(hp = ast_gethostbyname(externhost, &ahp)))
18571 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
18572 else
18573 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
18574 externexpire = time(NULL);
18575 } else if (!strcasecmp(v->name, "externrefresh")) {
18576 if (sscanf(v->value, "%30d", &externrefresh) != 1) {
18577 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
18578 externrefresh = 10;
18579 }
18580 } else if (!strcasecmp(v->name, "allow")) {
18581 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 1);
18582 } else if (!strcasecmp(v->name, "disallow")) {
18583 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 0);
18584 } else if (!strcasecmp(v->name, "autoframing")) {
18585 global_autoframing = ast_true(v->value);
18586 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
18587 allow_external_domains = ast_true(v->value);
18588 } else if (!strcasecmp(v->name, "autodomain")) {
18589 auto_sip_domains = ast_true(v->value);
18590 } else if (!strcasecmp(v->name, "domain")) {
18591 char *domain = ast_strdupa(v->value);
18592 char *context = strchr(domain, ',');
18593
18594 if (context)
18595 *context++ = '\0';
18596
18597 if (option_debug && ast_strlen_zero(context))
18598 ast_log(LOG_DEBUG, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
18599 if (ast_strlen_zero(domain))
18600 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
18601 else
18602 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, context ? ast_strip(context) : "");
18603 } else if (!strcasecmp(v->name, "register")) {
18604 if (sip_register(v->value, v->lineno) == 0)
18605 registry_count++;
18606 } else if (!strcasecmp(v->name, "tos")) {
18607 if (!ast_str2tos(v->value, &temp_tos)) {
18608 global_tos_sip = temp_tos;
18609 global_tos_audio = temp_tos;
18610 global_tos_video = temp_tos;
18611 ast_log(LOG_WARNING, "tos value at line %d is deprecated. See doc/ip-tos.txt for more information.\n", v->lineno);
18612 } else
18613 ast_log(LOG_WARNING, "Invalid tos value at line %d, See doc/ip-tos.txt for more information.\n", v->lineno);
18614 } else if (!strcasecmp(v->name, "tos_sip")) {
18615 if (ast_str2tos(v->value, &global_tos_sip))
18616 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, recommended value is 'cs3'. See doc/ip-tos.txt.\n", v->lineno);
18617 } else if (!strcasecmp(v->name, "tos_audio")) {
18618 if (ast_str2tos(v->value, &global_tos_audio))
18619 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, recommended value is 'ef'. See doc/ip-tos.txt.\n", v->lineno);
18620 } else if (!strcasecmp(v->name, "tos_video")) {
18621 if (ast_str2tos(v->value, &global_tos_video))
18622 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, recommended value is 'af41'. See doc/ip-tos.txt.\n", v->lineno);
18623 } else if (!strcasecmp(v->name, "bindport")) {
18624 if (sscanf(v->value, "%5d", &ourport) == 1) {
18625 bindaddr.sin_port = htons(ourport);
18626 } else {
18627 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
18628 }
18629 } else if (!strcasecmp(v->name, "qualify")) {
18630 if (!strcasecmp(v->value, "no")) {
18631 default_qualify = 0;
18632 } else if (!strcasecmp(v->value, "yes")) {
18633 default_qualify = DEFAULT_MAXMS;
18634 } else if (sscanf(v->value, "%30d", &default_qualify) != 1) {
18635 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
18636 default_qualify = 0;
18637 }
18638 } else if (!strcasecmp(v->name, "callevents")) {
18639 global_callevents = ast_true(v->value);
18640 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
18641 default_maxcallbitrate = atoi(v->value);
18642 if (default_maxcallbitrate < 0)
18643 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
18644 } else if (!strcasecmp(v->name, "matchexterniplocally")) {
18645 global_matchexterniplocally = ast_true(v->value);
18646 } else if (!strcasecmp(v->name, "constantssrc")) {
18647 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_CONSTANT_SSRC);
18648 } else if (!strcasecmp(v->name, "shrinkcallerid")) {
18649 if (ast_true(v->value)) {
18650 global_shrinkcallerid = 1;
18651 } else if (ast_false(v->value)) {
18652 global_shrinkcallerid = 0;
18653 } else {
18654 ast_log(LOG_WARNING, "shrinkcallerid value %s is not valid at line %d.\n", v->value, v->lineno);
18655 }
18656 }
18657 }
18658
18659 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
18660 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
18661 allow_external_domains = 1;
18662 }
18663
18664
18665 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
18666
18667 if (!strcasecmp(v->name, "auth"))
18668 authl = add_realm_authentication(authl, v->value, v->lineno);
18669 }
18670
18671 ucfg = ast_config_load("users.conf");
18672 if (ucfg) {
18673 struct ast_variable *gen;
18674 int genhassip, genregistersip;
18675 const char *hassip, *registersip;
18676
18677 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
18678 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
18679 gen = ast_variable_browse(ucfg, "general");
18680 cat = ast_category_browse(ucfg, NULL);
18681 while (cat) {
18682 if (strcasecmp(cat, "general")) {
18683 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
18684 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
18685 if (ast_true(hassip) || (!hassip && genhassip)) {
18686 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
18687 if (user) {
18688 ASTOBJ_CONTAINER_LINK(&userl,user);
18689 ASTOBJ_UNREF(user, sip_destroy_user);
18690 user_count++;
18691 }
18692 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0, 0);
18693 if (peer) {
18694 ast_device_state_changed("SIP/%s", peer->name);
18695 ASTOBJ_CONTAINER_LINK(&peerl,peer);
18696 ASTOBJ_UNREF(peer, sip_destroy_peer);
18697 peer_count++;
18698 }
18699 }
18700 if (ast_true(registersip) || (!registersip && genregistersip)) {
18701 char tmp[256];
18702 const char *host = ast_variable_retrieve(ucfg, cat, "host");
18703 const char *username = ast_variable_retrieve(ucfg, cat, "username");
18704 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
18705 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
18706 const char *authuser = ast_variable_retrieve(ucfg, cat, "authuser");
18707 if (!host)
18708 host = ast_variable_retrieve(ucfg, "general", "host");
18709 if (!username)
18710 username = ast_variable_retrieve(ucfg, "general", "username");
18711 if (!secret)
18712 secret = ast_variable_retrieve(ucfg, "general", "secret");
18713 if (!contact)
18714 contact = "s";
18715 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
18716 if (!ast_strlen_zero(secret)) {
18717 if (!ast_strlen_zero(authuser)) {
18718 snprintf(tmp, sizeof(tmp), "%s:%s:%s@%s/%s", username, secret, authuser, host, contact);
18719 } else {
18720 snprintf(tmp, sizeof(tmp), "%s:%s@%s/%s", username, secret, host, contact);
18721 }
18722 } else if (!ast_strlen_zero(authuser)) {
18723 snprintf(tmp, sizeof(tmp), "%s::%s@%s/%s", username, authuser, host, contact);
18724 } else {
18725 snprintf(tmp, sizeof(tmp), "%s@%s/%s", username, host, contact);
18726 }
18727 if (sip_register(tmp, 0) == 0)
18728 registry_count++;
18729 }
18730 }
18731 }
18732 cat = ast_category_browse(ucfg, cat);
18733 }
18734 ast_config_destroy(ucfg);
18735 }
18736
18737
18738
18739 cat = NULL;
18740 while ( (cat = ast_category_browse(cfg, cat)) ) {
18741 const char *utype;
18742 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
18743 continue;
18744 utype = ast_variable_retrieve(cfg, cat, "type");
18745 if (!utype) {
18746 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
18747 continue;
18748 } else {
18749 int is_user = 0, is_peer = 0;
18750 if (!strcasecmp(utype, "user"))
18751 is_user = 1;
18752 else if (!strcasecmp(utype, "friend"))
18753 is_user = is_peer = 1;
18754 else if (!strcasecmp(utype, "peer"))
18755 is_peer = 1;
18756 else {
18757 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
18758 continue;
18759 }
18760 if (is_user) {
18761 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
18762 if (user) {
18763 ASTOBJ_CONTAINER_LINK(&userl,user);
18764 ASTOBJ_UNREF(user, sip_destroy_user);
18765 user_count++;
18766 }
18767 }
18768 if (is_peer) {
18769 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0, 0);
18770 if (peer) {
18771 ASTOBJ_CONTAINER_LINK(&peerl,peer);
18772 ASTOBJ_UNREF(peer, sip_destroy_peer);
18773 peer_count++;
18774 }
18775 }
18776 }
18777 }
18778 if (ast_find_ourip(&__ourip, bindaddr)) {
18779 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
18780 ast_config_destroy(cfg);
18781 return 0;
18782 }
18783 if (!ntohs(bindaddr.sin_port))
18784 bindaddr.sin_port = ntohs(STANDARD_SIP_PORT);
18785 bindaddr.sin_family = AF_INET;
18786 ast_mutex_lock(&netlock);
18787 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
18788 close(sipsock);
18789 sipsock = -1;
18790 }
18791 if (sipsock < 0) {
18792 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
18793 if (sipsock < 0) {
18794 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
18795 ast_config_destroy(cfg);
18796 return -1;
18797 } else {
18798
18799 const int reuseFlag = 1;
18800
18801 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
18802 (const char*)&reuseFlag,
18803 sizeof reuseFlag);
18804
18805 ast_enable_packet_fragmentation(sipsock);
18806
18807 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
18808 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
18809 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
18810 strerror(errno));
18811 close(sipsock);
18812 sipsock = -1;
18813 } else {
18814 if (option_verbose > 1) {
18815 ast_verbose(VERBOSE_PREFIX_2 "SIP Listening on %s:%d\n",
18816 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
18817 ast_verbose(VERBOSE_PREFIX_2 "Using SIP TOS: %s\n", ast_tos2str(global_tos_sip));
18818 }
18819 if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &global_tos_sip, sizeof(global_tos_sip)))
18820 ast_log(LOG_WARNING, "Unable to set SIP TOS to %s\n", ast_tos2str(global_tos_sip));
18821 }
18822 }
18823 }
18824 ast_mutex_unlock(&netlock);
18825
18826
18827
18828
18829
18830 if (auto_sip_domains) {
18831 char temp[MAXHOSTNAMELEN];
18832
18833
18834 if (bindaddr.sin_addr.s_addr)
18835 add_sip_domain(ast_inet_ntoa(bindaddr.sin_addr), SIP_DOMAIN_AUTO, NULL);
18836 else
18837 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
18838
18839
18840 if (externip.sin_addr.s_addr)
18841 add_sip_domain(ast_inet_ntoa(externip.sin_addr), SIP_DOMAIN_AUTO, NULL);
18842
18843
18844 if (!ast_strlen_zero(externhost))
18845 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
18846
18847
18848 if (!gethostname(temp, sizeof(temp)))
18849 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
18850 }
18851
18852
18853 ast_config_destroy(cfg);
18854
18855
18856 if (notify_types)
18857 ast_config_destroy(notify_types);
18858 notify_types = ast_config_load(notify_config);
18859
18860
18861 manager_event(EVENT_FLAG_SYSTEM, "ChannelReload", "Channel: 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);
18862
18863 return 0;
18864 }
18865
18866 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
18867 {
18868 struct sip_pvt *p;
18869 struct ast_udptl *udptl = NULL;
18870
18871 p = chan->tech_pvt;
18872 if (!p)
18873 return NULL;
18874
18875 ast_mutex_lock(&p->lock);
18876 if (p->udptl && ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
18877 udptl = p->udptl;
18878 ast_mutex_unlock(&p->lock);
18879 return udptl;
18880 }
18881
18882 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
18883 {
18884 struct sip_pvt *p;
18885
18886 p = chan->tech_pvt;
18887 if (!p)
18888 return -1;
18889 ast_mutex_lock(&p->lock);
18890 if (udptl)
18891 ast_udptl_get_peer(udptl, &p->udptlredirip);
18892 else
18893 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
18894 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
18895 if (!p->pendinginvite) {
18896 if (option_debug > 2) {
18897 ast_log(LOG_DEBUG, "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), udptl ? ntohs(p->udptlredirip.sin_port) : 0);
18898 }
18899 transmit_reinvite_with_t38_sdp(p);
18900 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
18901 if (option_debug > 2) {
18902 ast_log(LOG_DEBUG, "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), udptl ? ntohs(p->udptlredirip.sin_port) : 0);
18903 }
18904 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
18905 }
18906 }
18907
18908 p->lastrtprx = p->lastrtptx = time(NULL);
18909 ast_mutex_unlock(&p->lock);
18910 return 0;
18911 }
18912
18913
18914
18915
18916
18917
18918 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite)
18919 {
18920 struct sip_pvt *p;
18921 int flag = 0;
18922
18923 p = chan->tech_pvt;
18924 if (!p || !pvt->udptl)
18925 return -1;
18926
18927
18928 ast_mutex_lock(&p->lock);
18929
18930
18931
18932 p->t38.jointcapability = p->t38.peercapability = pvt->t38.jointcapability;
18933
18934 ast_udptl_set_far_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
18935 ast_udptl_set_local_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
18936 ast_udptl_set_error_correction_scheme(p->udptl, ast_udptl_get_error_correction_scheme(pvt->udptl));
18937
18938 if (reinvite) {
18939
18940
18941
18942
18943
18944 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
18945 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
18946 flag =1;
18947 } else {
18948 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
18949 }
18950 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
18951 if (!p->pendinginvite) {
18952 if (option_debug > 2) {
18953 if (flag)
18954 ast_log(LOG_DEBUG, "Sending reinvite on SIP '%s' - It's UDPTL soon redirected to IP %s:%d\n", p->callid, ast_inet_ntoa(p->udptlredirip.sin_addr), ntohs(p->udptlredirip.sin_port));
18955 else
18956 ast_log(LOG_DEBUG, "Sending reinvite on SIP '%s' - It's UDPTL soon redirected to us (IP %s)\n", p->callid, ast_inet_ntoa(p->ourip));
18957 }
18958 transmit_reinvite_with_t38_sdp(p);
18959 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
18960 if (option_debug > 2) {
18961 if (flag)
18962 ast_log(LOG_DEBUG, "Deferring reinvite on SIP '%s' - It's UDPTL will be redirected to IP %s:%d\n", p->callid, ast_inet_ntoa(p->udptlredirip.sin_addr), ntohs(p->udptlredirip.sin_port));
18963 else
18964 ast_log(LOG_DEBUG, "Deferring reinvite on SIP '%s' - It's UDPTL will be redirected to us (IP %s)\n", p->callid, ast_inet_ntoa(p->ourip));
18965 }
18966 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
18967 }
18968 }
18969
18970 p->lastrtprx = p->lastrtptx = time(NULL);
18971 ast_mutex_unlock(&p->lock);
18972 return 0;
18973 } else {
18974 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
18975 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
18976 flag = 1;
18977 } else {
18978 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
18979 }
18980 if (option_debug > 2) {
18981 if (flag)
18982 ast_log(LOG_DEBUG, "Responding 200 OK on SIP '%s' - It's UDPTL soon redirected to IP %s:%d\n", p->callid, ast_inet_ntoa(p->udptlredirip.sin_addr), ntohs(p->udptlredirip.sin_port));
18983 else
18984 ast_log(LOG_DEBUG, "Responding 200 OK on SIP '%s' - It's UDPTL soon redirected to us (IP %s)\n", p->callid, ast_inet_ntoa(p->ourip));
18985 }
18986 pvt->t38.state = T38_ENABLED;
18987 p->t38.state = T38_ENABLED;
18988 if (option_debug > 1) {
18989 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", pvt->t38.state, pvt->owner ? pvt->owner->name : "<none>");
18990 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
18991 }
18992 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
18993 p->lastrtprx = p->lastrtptx = time(NULL);
18994 ast_mutex_unlock(&p->lock);
18995 return 0;
18996 }
18997 }
18998
18999
19000
19001 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
19002 {
19003 struct sip_pvt *p = NULL;
19004 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
19005
19006 if (!(p = chan->tech_pvt))
19007 return AST_RTP_GET_FAILED;
19008
19009 ast_mutex_lock(&p->lock);
19010 if (!(p->rtp)) {
19011 ast_mutex_unlock(&p->lock);
19012 return AST_RTP_GET_FAILED;
19013 }
19014
19015 *rtp = p->rtp;
19016
19017 if (ast_rtp_getnat(*rtp) && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT))
19018 res = AST_RTP_TRY_PARTIAL;
19019 else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
19020 res = AST_RTP_TRY_NATIVE;
19021 else if (ast_test_flag(&global_jbconf, AST_JB_FORCED))
19022 res = AST_RTP_GET_FAILED;
19023
19024 ast_mutex_unlock(&p->lock);
19025
19026 return res;
19027 }
19028
19029
19030 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
19031 {
19032 struct sip_pvt *p = NULL;
19033 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
19034
19035 if (!(p = chan->tech_pvt))
19036 return AST_RTP_GET_FAILED;
19037
19038 ast_mutex_lock(&p->lock);
19039 if (!(p->vrtp)) {
19040 ast_mutex_unlock(&p->lock);
19041 return AST_RTP_GET_FAILED;
19042 }
19043
19044 *rtp = p->vrtp;
19045
19046 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
19047 res = AST_RTP_TRY_NATIVE;
19048
19049 ast_mutex_unlock(&p->lock);
19050
19051 return res;
19052 }
19053
19054
19055 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
19056 {
19057 struct sip_pvt *p;
19058 int changed = 0;
19059
19060 p = chan->tech_pvt;
19061 if (!p)
19062 return -1;
19063
19064
19065 if (!ast_bridged_channel(chan) && !global_directrtpsetup)
19066 return 0;
19067
19068 ast_mutex_lock(&p->lock);
19069 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
19070
19071 ast_mutex_unlock(&p->lock);
19072 return 0;
19073 }
19074
19075
19076
19077
19078 if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
19079 ast_mutex_unlock(&p->lock);
19080 return 0;
19081 }
19082
19083 if (rtp) {
19084 changed |= ast_rtp_get_peer(rtp, &p->redirip);
19085 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
19086 memset(&p->redirip, 0, sizeof(p->redirip));
19087 changed = 1;
19088 }
19089 if (vrtp) {
19090 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
19091 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
19092 memset(&p->vredirip, 0, sizeof(p->vredirip));
19093 changed = 1;
19094 }
19095 if (codecs) {
19096 if (p->redircodecs != codecs && (p->jointcapability & codecs) != p->jointcapability) {
19097 p->redircodecs = codecs;
19098 p->jointcapability &= codecs;
19099 p->capability &= codecs;
19100 changed = 1;
19101 }
19102 }
19103 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
19104 if (chan->_state != AST_STATE_UP) {
19105 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
19106 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
19107 if (option_debug)
19108 ast_log(LOG_DEBUG, "Early remote bridge setting SIP '%s' - Sending media to %s\n", p->callid, ast_inet_ntoa(rtp ? p->redirip.sin_addr : p->ourip));
19109 } else if (!p->pendinginvite) {
19110 if (option_debug > 2) {
19111 ast_log(LOG_DEBUG, "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));
19112 }
19113 transmit_reinvite_with_sdp(p);
19114 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
19115 if (option_debug > 2) {
19116 ast_log(LOG_DEBUG, "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));
19117 }
19118
19119 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
19120 }
19121 }
19122
19123 p->lastrtprx = p->lastrtptx = time(NULL);
19124 ast_mutex_unlock(&p->lock);
19125 return 0;
19126 }
19127
19128 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
19129 static char *descrip_dtmfmode = "SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
19130 static char *app_dtmfmode = "SIPDtmfMode";
19131
19132 static char *app_sipaddheader = "SIPAddHeader";
19133 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
19134
19135 static char *descrip_sipaddheader = ""
19136 " SIPAddHeader(Header: Content)\n"
19137 "Adds a header to a SIP call placed with DIAL.\n"
19138 "Remember to user the X-header if you are adding non-standard SIP\n"
19139 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
19140 "Adding the wrong headers may jeopardize the SIP dialog.\n"
19141 "Always returns 0\n";
19142
19143
19144
19145 static int sip_dtmfmode(struct ast_channel *chan, void *data)
19146 {
19147 struct sip_pvt *p;
19148 char *mode;
19149 if (data)
19150 mode = (char *)data;
19151 else {
19152 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
19153 return 0;
19154 }
19155 ast_channel_lock(chan);
19156 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
19157 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
19158 ast_channel_unlock(chan);
19159 return 0;
19160 }
19161 p = chan->tech_pvt;
19162 if (!p) {
19163 ast_channel_unlock(chan);
19164 return 0;
19165 }
19166 ast_mutex_lock(&p->lock);
19167 if (!strcasecmp(mode,"info")) {
19168 ast_clear_flag(&p->flags[0], SIP_DTMF);
19169 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
19170 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
19171 } else if (!strcasecmp(mode,"rfc2833")) {
19172 ast_clear_flag(&p->flags[0], SIP_DTMF);
19173 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
19174 p->jointnoncodeccapability |= AST_RTP_DTMF;
19175 } else if (!strcasecmp(mode,"inband")) {
19176 ast_clear_flag(&p->flags[0], SIP_DTMF);
19177 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
19178 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
19179 } else
19180 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n",mode);
19181 if (p->rtp)
19182 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
19183 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
19184 if (!p->vad) {
19185 p->vad = ast_dsp_new();
19186 ast_dsp_set_features(p->vad, DSP_FEATURE_DTMF_DETECT);
19187 }
19188 } else {
19189 if (p->vad) {
19190 ast_dsp_free(p->vad);
19191 p->vad = NULL;
19192 }
19193 }
19194 ast_mutex_unlock(&p->lock);
19195 ast_channel_unlock(chan);
19196 return 0;
19197 }
19198
19199
19200 static int sip_addheader(struct ast_channel *chan, void *data)
19201 {
19202 int no = 0;
19203 int ok = FALSE;
19204 char varbuf[30];
19205 char *inbuf = (char *) data;
19206
19207 if (ast_strlen_zero(inbuf)) {
19208 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
19209 return 0;
19210 }
19211 ast_channel_lock(chan);
19212
19213
19214 while (!ok && no <= 50) {
19215 no++;
19216 snprintf(varbuf, sizeof(varbuf), "__SIPADDHEADER%.2d", no);
19217
19218
19219 if( (pbx_builtin_getvar_helper(chan, (const char *) varbuf + 2) == (const char *) NULL) )
19220 ok = TRUE;
19221 }
19222 if (ok) {
19223 pbx_builtin_setvar_helper (chan, varbuf, inbuf);
19224 if (sipdebug)
19225 ast_log(LOG_DEBUG,"SIP Header added \"%s\" as %s\n", inbuf, varbuf);
19226 } else {
19227 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
19228 }
19229 ast_channel_unlock(chan);
19230 return 0;
19231 }
19232
19233
19234
19235
19236
19237
19238
19239 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
19240 {
19241 char *cdest;
19242 char *extension, *host, *port;
19243 char tmp[80];
19244
19245 cdest = ast_strdupa(dest);
19246
19247 extension = strsep(&cdest, "@");
19248 host = strsep(&cdest, ":");
19249 port = strsep(&cdest, ":");
19250 if (ast_strlen_zero(extension)) {
19251 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
19252 return 0;
19253 }
19254
19255
19256 if (!host) {
19257 char *localtmp;
19258 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
19259 if (ast_strlen_zero(tmp)) {
19260 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
19261 return 0;
19262 }
19263 if ((localtmp = strcasestr(tmp, "sip:")) && (localtmp = strchr(localtmp, '@'))) {
19264 char lhost[80], lport[80];
19265 memset(lhost, 0, sizeof(lhost));
19266 memset(lport, 0, sizeof(lport));
19267 localtmp++;
19268
19269 sscanf(localtmp, "%80[^<>:; ]:%80[^<>:; ]", lhost, lport);
19270 if (ast_strlen_zero(lhost)) {
19271 ast_log(LOG_ERROR, "Can't find the host address\n");
19272 return 0;
19273 }
19274 host = ast_strdupa(lhost);
19275 if (!ast_strlen_zero(lport)) {
19276 port = ast_strdupa(lport);
19277 }
19278 }
19279 }
19280
19281 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
19282 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
19283
19284 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
19285 sip_alreadygone(p);
19286 return 0;
19287 }
19288
19289
19290 static int sip_get_codec(struct ast_channel *chan)
19291 {
19292 struct sip_pvt *p = chan->tech_pvt;
19293 return p->jointcapability ? p->jointcapability : p->capability;
19294 }
19295
19296
19297
19298
19299
19300 static void sip_poke_all_peers(void)
19301 {
19302 int ms = 0;
19303
19304 if (!speerobjs)
19305 return;
19306
19307 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
19308 ASTOBJ_WRLOCK(iterator);
19309 if (!AST_SCHED_DEL(sched, iterator->pokeexpire)) {
19310 struct sip_peer *peer_ptr = iterator;
19311 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
19312 }
19313 ms += 100;
19314 iterator->pokeexpire = ast_sched_add(sched, ms, sip_poke_peer_s, ASTOBJ_REF(iterator));
19315 if (iterator->pokeexpire == -1) {
19316 struct sip_peer *peer_ptr = iterator;
19317 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
19318 }
19319 ASTOBJ_UNLOCK(iterator);
19320 } while (0)
19321 );
19322 }
19323
19324
19325 static void sip_send_all_registers(void)
19326 {
19327 int ms;
19328 int regspacing;
19329 if (!regobjs)
19330 return;
19331 regspacing = default_expiry * 1000/regobjs;
19332 if (regspacing > 100)
19333 regspacing = 100;
19334 ms = regspacing;
19335 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
19336 ASTOBJ_WRLOCK(iterator);
19337 AST_SCHED_DEL(sched, iterator->expire);
19338 ms += regspacing;
19339 iterator->expire = ast_sched_add(sched, ms, sip_reregister, iterator);
19340 ASTOBJ_UNLOCK(iterator);
19341 } while (0)
19342 );
19343 }
19344
19345
19346 static int sip_do_reload(enum channelreloadreason reason)
19347 {
19348 reload_config(reason);
19349
19350
19351 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
19352 if (option_debug > 3)
19353 ast_log(LOG_DEBUG, "--------------- Done destroying pruned peers\n");
19354
19355
19356 sip_poke_all_peers();
19357
19358
19359 sip_send_all_registers();
19360
19361 if (option_debug > 3)
19362 ast_log(LOG_DEBUG, "--------------- SIP reload done\n");
19363
19364 return 0;
19365 }
19366
19367
19368 static int sip_reload(int fd, int argc, char *argv[])
19369 {
19370 ast_mutex_lock(&sip_reload_lock);
19371 if (sip_reloading)
19372 ast_verbose("Previous SIP reload not yet done\n");
19373 else {
19374 sip_reloading = TRUE;
19375 if (fd)
19376 sip_reloadreason = CHANNEL_CLI_RELOAD;
19377 else
19378 sip_reloadreason = CHANNEL_MODULE_RELOAD;
19379 }
19380 ast_mutex_unlock(&sip_reload_lock);
19381 restart_monitor();
19382
19383 return 0;
19384 }
19385
19386
19387 static int reload(void)
19388 {
19389 return sip_reload(0, 0, NULL);
19390 }
19391
19392 static struct ast_cli_entry cli_sip_debug_deprecated =
19393 { { "sip", "debug", NULL },
19394 sip_do_debug_deprecated, "Enable SIP debugging",
19395 debug_usage };
19396
19397 static struct ast_cli_entry cli_sip_no_debug_deprecated =
19398 { { "sip", "no", "debug", NULL },
19399 sip_no_debug_deprecated, "Disable SIP debugging",
19400 debug_usage };
19401
19402 static struct ast_cli_entry cli_sip[] = {
19403 { { "sip", "show", "channels", NULL },
19404 sip_show_channels, "List active SIP channels",
19405 show_channels_usage },
19406
19407 { { "sip", "show", "domains", NULL },
19408 sip_show_domains, "List our local SIP domains.",
19409 show_domains_usage },
19410
19411 { { "sip", "show", "inuse", NULL },
19412 sip_show_inuse, "List all inuse/limits",
19413 show_inuse_usage },
19414
19415 { { "sip", "show", "objects", NULL },
19416 sip_show_objects, "List all SIP object allocations",
19417 show_objects_usage },
19418
19419 { { "sip", "show", "peers", NULL },
19420 sip_show_peers, "List defined SIP peers",
19421 show_peers_usage },
19422
19423 { { "sip", "show", "registry", NULL },
19424 sip_show_registry, "List SIP registration status",
19425 show_reg_usage },
19426
19427 { { "sip", "show", "settings", NULL },
19428 sip_show_settings, "Show SIP global settings",
19429 show_settings_usage },
19430
19431 { { "sip", "show", "subscriptions", NULL },
19432 sip_show_subscriptions, "List active SIP subscriptions",
19433 show_subscriptions_usage },
19434
19435 { { "sip", "show", "users", NULL },
19436 sip_show_users, "List defined SIP users",
19437 show_users_usage },
19438
19439 { { "sip", "notify", NULL },
19440 sip_notify, "Send a notify packet to a SIP peer",
19441 notify_usage, complete_sipnotify },
19442
19443 { { "sip", "show", "channel", NULL },
19444 sip_show_channel, "Show detailed SIP channel info",
19445 show_channel_usage, complete_sipch },
19446
19447 { { "sip", "show", "history", NULL },
19448 sip_show_history, "Show SIP dialog history",
19449 show_history_usage, complete_sipch },
19450
19451 { { "sip", "show", "peer", NULL },
19452 sip_show_peer, "Show details on specific SIP peer",
19453 show_peer_usage, complete_sip_show_peer },
19454
19455 { { "sip", "show", "user", NULL },
19456 sip_show_user, "Show details on specific SIP user",
19457 show_user_usage, complete_sip_show_user },
19458
19459 { { "sip", "prune", "realtime", NULL },
19460 sip_prune_realtime, "Prune cached Realtime object(s)",
19461 prune_realtime_usage },
19462
19463 { { "sip", "prune", "realtime", "peer", NULL },
19464 sip_prune_realtime, "Prune cached Realtime peer(s)",
19465 prune_realtime_usage, complete_sip_prune_realtime_peer },
19466
19467 { { "sip", "prune", "realtime", "user", NULL },
19468 sip_prune_realtime, "Prune cached Realtime user(s)",
19469 prune_realtime_usage, complete_sip_prune_realtime_user },
19470
19471 { { "sip", "set", "debug", NULL },
19472 sip_do_debug, "Enable SIP debugging",
19473 debug_usage, NULL, &cli_sip_debug_deprecated },
19474
19475 { { "sip", "set", "debug", "ip", NULL },
19476 sip_do_debug, "Enable SIP debugging on IP",
19477 debug_usage },
19478
19479 { { "sip", "set", "debug", "peer", NULL },
19480 sip_do_debug, "Enable SIP debugging on Peername",
19481 debug_usage, complete_sip_debug_peer },
19482
19483 { { "sip", "set", "debug", "off", NULL },
19484 sip_no_debug, "Disable SIP debugging",
19485 no_debug_usage, NULL, &cli_sip_no_debug_deprecated },
19486
19487 { { "sip", "history", NULL },
19488 sip_do_history, "Enable SIP history",
19489 history_usage },
19490
19491 { { "sip", "history", "off", NULL },
19492 sip_no_history, "Disable SIP history",
19493 no_history_usage },
19494
19495 { { "sip", "reload", NULL },
19496 sip_reload, "Reload SIP configuration",
19497 sip_reload_usage },
19498 };
19499
19500
19501 static int load_module(void)
19502 {
19503 ASTOBJ_CONTAINER_INIT(&userl);
19504 ASTOBJ_CONTAINER_INIT(&peerl);
19505 ASTOBJ_CONTAINER_INIT(®l);
19506
19507 if (!(sched = sched_context_create())) {
19508 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
19509 return AST_MODULE_LOAD_FAILURE;
19510 }
19511
19512 if (!(io = io_context_create())) {
19513 ast_log(LOG_ERROR, "Unable to create I/O context\n");
19514 sched_context_destroy(sched);
19515 return AST_MODULE_LOAD_FAILURE;
19516 }
19517
19518 sip_reloadreason = CHANNEL_MODULE_LOAD;
19519
19520 if(reload_config(sip_reloadreason))
19521 return AST_MODULE_LOAD_DECLINE;
19522
19523
19524 if (ast_channel_register(&sip_tech)) {
19525 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
19526 io_context_destroy(io);
19527 sched_context_destroy(sched);
19528 return AST_MODULE_LOAD_FAILURE;
19529 }
19530
19531
19532 ast_cli_register_multiple(cli_sip, sizeof(cli_sip)/ sizeof(struct ast_cli_entry));
19533
19534
19535 ast_rtp_proto_register(&sip_rtp);
19536
19537
19538 ast_udptl_proto_register(&sip_udptl);
19539
19540
19541 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
19542 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
19543
19544
19545 ast_custom_function_register(&sip_header_function);
19546 ast_custom_function_register(&sippeer_function);
19547 ast_custom_function_register(&sipchaninfo_function);
19548 ast_custom_function_register(&checksipdomain_function);
19549
19550
19551 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM, manager_sip_show_peers,
19552 "List SIP peers (text format)", mandescr_show_peers);
19553 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM, manager_sip_show_peer,
19554 "Show SIP peer (text format)", mandescr_show_peer);
19555
19556 sip_poke_all_peers();
19557 sip_send_all_registers();
19558
19559
19560 restart_monitor();
19561
19562 return AST_MODULE_LOAD_SUCCESS;
19563 }
19564
19565
19566 static int unload_module(void)
19567 {
19568 struct sip_pvt *p, *pl;
19569
19570
19571 ast_channel_unregister(&sip_tech);
19572
19573
19574 ast_custom_function_unregister(&sipchaninfo_function);
19575 ast_custom_function_unregister(&sippeer_function);
19576 ast_custom_function_unregister(&sip_header_function);
19577 ast_custom_function_unregister(&checksipdomain_function);
19578
19579
19580 ast_unregister_application(app_dtmfmode);
19581 ast_unregister_application(app_sipaddheader);
19582
19583
19584 ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
19585
19586
19587 ast_rtp_proto_unregister(&sip_rtp);
19588
19589
19590 ast_udptl_proto_unregister(&sip_udptl);
19591
19592
19593 ast_manager_unregister("SIPpeers");
19594 ast_manager_unregister("SIPshowpeer");
19595
19596 ast_mutex_lock(&iflock);
19597
19598 for (p = iflist; p ; p = p->next) {
19599 if (p->owner)
19600 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
19601 }
19602 ast_mutex_unlock(&iflock);
19603
19604 ast_mutex_lock(&monlock);
19605 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
19606 pthread_cancel(monitor_thread);
19607 pthread_kill(monitor_thread, SIGURG);
19608 pthread_join(monitor_thread, NULL);
19609 }
19610 monitor_thread = AST_PTHREADT_STOP;
19611 ast_mutex_unlock(&monlock);
19612
19613 restartdestroy:
19614 ast_mutex_lock(&iflock);
19615
19616 p = iflist;
19617 while (p) {
19618 pl = p;
19619 p = p->next;
19620 if (__sip_destroy(pl, TRUE) < 0) {
19621
19622 iflist = p;
19623 ast_mutex_unlock(&iflock);
19624 usleep(1);
19625 goto restartdestroy;
19626 }
19627 }
19628 iflist = NULL;
19629 ast_mutex_unlock(&iflock);
19630
19631
19632 ast_free_ha(localaddr);
19633
19634 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
19635 ASTOBJ_CONTAINER_DESTROY(&userl);
19636 ASTOBJ_CONTAINER_DESTROYALL(&peerl, sip_destroy_peer);
19637 ASTOBJ_CONTAINER_DESTROY(&peerl);
19638 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
19639 ASTOBJ_CONTAINER_DESTROY(®l);
19640
19641 clear_realm_authentication(authl);
19642 clear_sip_domains();
19643 close(sipsock);
19644 sched_context_destroy(sched);
19645
19646 return 0;
19647 }
19648
19649 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)",
19650 .load = load_module,
19651 .unload = unload_module,
19652 .reload = reload,
19653 );