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: 325275 $")
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 #include "asterisk/astobj2.h"
00153
00154 #ifndef FALSE
00155 #define FALSE 0
00156 #endif
00157
00158 #ifndef TRUE
00159 #define TRUE 1
00160 #endif
00161
00162 #define SIPBUFSIZE 512
00163
00164 #define XMIT_ERROR -2
00165
00166 #define VIDEO_CODEC_MASK 0x1fc0000
00167 #ifndef IPTOS_MINCOST
00168 #define IPTOS_MINCOST 0x02
00169 #endif
00170
00171
00172
00173 #define DEFAULT_DEFAULT_EXPIRY 120
00174 #define DEFAULT_MIN_EXPIRY 60
00175 #define DEFAULT_MAX_EXPIRY 3600
00176 #define DEFAULT_REGISTRATION_TIMEOUT 20
00177 #define DEFAULT_MAX_FORWARDS "70"
00178
00179
00180
00181 #define EXPIRY_GUARD_SECS 15
00182 #define EXPIRY_GUARD_LIMIT 30
00183
00184 #define EXPIRY_GUARD_MIN 500
00185
00186
00187
00188 #define EXPIRY_GUARD_PCT 0.20
00189
00190 #define DEFAULT_EXPIRY 900
00191
00192 static int min_expiry = DEFAULT_MIN_EXPIRY;
00193 static int max_expiry = DEFAULT_MAX_EXPIRY;
00194 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00195 static int expiry = DEFAULT_EXPIRY;
00196 static char seen_lastms = 0;
00197
00198 #ifndef MAX
00199 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00200 #endif
00201
00202 #define CALLERID_UNKNOWN "Anonymous"
00203 #define FROMDOMAIN_INVALID "anonymous.invalid"
00204
00205 #define DEFAULT_MAXMS 2000
00206 #define DEFAULT_FREQ_OK 60 * 1000
00207 #define DEFAULT_FREQ_NOTOK 10 * 1000
00208
00209 #define DEFAULT_RETRANS 1000
00210 #define MAX_RETRANS 6
00211 #define SIP_TRANS_TIMEOUT 32000
00212
00213
00214 #define DEFAULT_TRANS_TIMEOUT -1
00215 #define PROVIS_KEEPALIVE_TIMEOUT 60000
00216 #define MAX_AUTHTRIES 3
00217
00218 #define SIP_MAX_HEADERS 64
00219 #define SIP_MAX_LINES 64
00220 #define SIP_MAX_PACKET 4096
00221
00222 #define SDP_MAX_RTPMAP_CODECS 32
00223
00224 #define INITIAL_CSEQ 101
00225
00226
00227 static struct ast_jb_conf default_jbconf =
00228 {
00229 .flags = 0,
00230 .max_size = -1,
00231 .resync_threshold = -1,
00232 .impl = ""
00233 };
00234 static struct ast_jb_conf global_jbconf;
00235
00236 static const char config[] = "sip.conf";
00237 static const char notify_config[] = "sip_notify.conf";
00238
00239 #define RTP 1
00240 #define NO_RTP 0
00241
00242
00243
00244
00245 enum transfermodes {
00246 TRANSFER_OPENFORALL,
00247 TRANSFER_CLOSED,
00248 };
00249
00250
00251 enum sip_result {
00252 AST_SUCCESS = 0,
00253 AST_FAILURE = -1,
00254 };
00255
00256
00257
00258
00259 enum invitestates {
00260 INV_NONE = 0,
00261 INV_CALLING = 1,
00262 INV_PROCEEDING = 2,
00263 INV_EARLY_MEDIA = 3,
00264 INV_COMPLETED = 4,
00265 INV_CONFIRMED = 5,
00266 INV_TERMINATED = 6,
00267
00268 INV_CANCELLED = 7,
00269 };
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 enum xmittype {
00280 XMIT_CRITICAL = 2,
00281
00282 XMIT_RELIABLE = 1,
00283 XMIT_UNRELIABLE = 0,
00284 };
00285
00286 enum parse_register_result {
00287 PARSE_REGISTER_DENIED,
00288 PARSE_REGISTER_FAILED,
00289 PARSE_REGISTER_UPDATE,
00290 PARSE_REGISTER_QUERY,
00291 };
00292
00293 enum subscriptiontype {
00294 NONE = 0,
00295 XPIDF_XML,
00296 DIALOG_INFO_XML,
00297 CPIM_PIDF_XML,
00298 PIDF_XML,
00299 MWI_NOTIFICATION
00300 };
00301
00302 static const struct cfsubscription_types {
00303 enum subscriptiontype type;
00304 const char * const event;
00305 const char * const mediatype;
00306 const char * const text;
00307 } subscription_types[] = {
00308 { NONE, "-", "unknown", "unknown" },
00309
00310 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00311 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00312 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00313 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00314 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00315 };
00316
00317
00318 enum sipmethod {
00319 SIP_UNKNOWN,
00320 SIP_RESPONSE,
00321 SIP_REGISTER,
00322 SIP_OPTIONS,
00323 SIP_NOTIFY,
00324 SIP_INVITE,
00325 SIP_ACK,
00326 SIP_PRACK,
00327 SIP_BYE,
00328 SIP_REFER,
00329 SIP_SUBSCRIBE,
00330 SIP_MESSAGE,
00331 SIP_UPDATE,
00332 SIP_INFO,
00333 SIP_CANCEL,
00334 SIP_PUBLISH,
00335 SIP_PING,
00336 };
00337
00338
00339
00340
00341
00342
00343 enum sip_auth_type {
00344 PROXY_AUTH,
00345 WWW_AUTH,
00346 };
00347
00348
00349 enum check_auth_result {
00350 AUTH_SUCCESSFUL = 0,
00351 AUTH_CHALLENGE_SENT = 1,
00352 AUTH_SECRET_FAILED = -1,
00353 AUTH_USERNAME_MISMATCH = -2,
00354 AUTH_NOT_FOUND = -3,
00355 AUTH_FAKE_AUTH = -4,
00356 AUTH_UNKNOWN_DOMAIN = -5,
00357 AUTH_PEER_NOT_DYNAMIC = -6,
00358 AUTH_ACL_FAILED = -7,
00359 };
00360
00361
00362 enum sipregistrystate {
00363 REG_STATE_UNREGISTERED = 0,
00364 REG_STATE_REGSENT,
00365 REG_STATE_AUTHSENT,
00366 REG_STATE_REGISTERED,
00367 REG_STATE_REJECTED,
00368 REG_STATE_TIMEOUT,
00369 REG_STATE_NOAUTH,
00370 REG_STATE_FAILED,
00371 };
00372
00373 #define CAN_NOT_CREATE_DIALOG 0
00374 #define CAN_CREATE_DIALOG 1
00375 #define CAN_CREATE_DIALOG_UNSUPPORTED_METHOD 2
00376
00377
00378 static const struct cfsip_methods {
00379 enum sipmethod id;
00380 int need_rtp;
00381 char * const text;
00382 int can_create;
00383 } sip_methods[] = {
00384 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00385 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00386 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00387 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00388 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00389 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00390 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00391 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00392 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00393 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00394 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00395 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00396 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00397 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00398 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00399 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00400 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00401 };
00402
00403 static unsigned int chan_idx;
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 #define SUPPORTED 1
00416 #define NOT_SUPPORTED 0
00417
00418 #define SIP_OPT_REPLACES (1 << 0)
00419 #define SIP_OPT_100REL (1 << 1)
00420 #define SIP_OPT_TIMER (1 << 2)
00421 #define SIP_OPT_EARLY_SESSION (1 << 3)
00422 #define SIP_OPT_JOIN (1 << 4)
00423 #define SIP_OPT_PATH (1 << 5)
00424 #define SIP_OPT_PREF (1 << 6)
00425 #define SIP_OPT_PRECONDITION (1 << 7)
00426 #define SIP_OPT_PRIVACY (1 << 8)
00427 #define SIP_OPT_SDP_ANAT (1 << 9)
00428 #define SIP_OPT_SEC_AGREE (1 << 10)
00429 #define SIP_OPT_EVENTLIST (1 << 11)
00430 #define SIP_OPT_GRUU (1 << 12)
00431 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00432 #define SIP_OPT_NOREFERSUB (1 << 14)
00433 #define SIP_OPT_HISTINFO (1 << 15)
00434 #define SIP_OPT_RESPRIORITY (1 << 16)
00435
00436
00437
00438 static const struct cfsip_options {
00439 int id;
00440 int supported;
00441 char * const text;
00442 } sip_options[] = {
00443
00444 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00445
00446 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00447
00448 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00449
00450 { SIP_OPT_TIMER, NOT_SUPPORTED, "timer" },
00451
00452 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00453
00454 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00455
00456 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00457
00458 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00459
00460 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00461
00462 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00463
00464 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00465
00466 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00467
00468 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00469
00470 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00471
00472 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00473
00474 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00475
00476 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00477
00478 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00479 };
00480
00481
00482
00483 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO"
00484
00485
00486 #define SUPPORTED_EXTENSIONS "replaces"
00487
00488
00489 #define STANDARD_SIP_PORT 5060
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 #define DEFAULT_CONTEXT "default"
00503 #define DEFAULT_MOHINTERPRET "default"
00504 #define DEFAULT_MOHSUGGEST ""
00505 #define DEFAULT_VMEXTEN "asterisk"
00506 #define DEFAULT_CALLERID "asterisk"
00507 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00508 #define DEFAULT_MWITIME 10
00509 #define DEFAULT_ALLOWGUEST TRUE
00510 #define DEFAULT_SRVLOOKUP TRUE
00511 #define DEFAULT_COMPACTHEADERS FALSE
00512 #define DEFAULT_TOS_SIP 0
00513 #define DEFAULT_TOS_AUDIO 0
00514 #define DEFAULT_TOS_VIDEO 0
00515 #define DEFAULT_ALLOW_EXT_DOM TRUE
00516 #define DEFAULT_REALM "asterisk"
00517 #define DEFAULT_NOTIFYRINGING TRUE
00518 #define DEFAULT_PEDANTIC FALSE
00519 #define DEFAULT_AUTOCREATEPEER FALSE
00520 #define DEFAULT_QUALIFY FALSE
00521 #define DEFAULT_T1MIN 100
00522 #define DEFAULT_MAX_CALL_BITRATE (384)
00523 #ifndef DEFAULT_USERAGENT
00524 #define DEFAULT_USERAGENT "Asterisk PBX"
00525 #endif
00526
00527
00528
00529
00530 static char default_context[AST_MAX_CONTEXT];
00531 static char default_subscribecontext[AST_MAX_CONTEXT];
00532 static char default_language[MAX_LANGUAGE];
00533 static char default_callerid[AST_MAX_EXTENSION];
00534 static char default_fromdomain[AST_MAX_EXTENSION];
00535 static char default_notifymime[AST_MAX_EXTENSION];
00536 static int default_qualify;
00537 static char default_vmexten[AST_MAX_EXTENSION];
00538 static char default_mohinterpret[MAX_MUSICCLASS];
00539 static char default_mohsuggest[MAX_MUSICCLASS];
00540
00541 static int default_maxcallbitrate;
00542 static struct ast_codec_pref default_prefs;
00543
00544
00545 static int global_directrtpsetup;
00546 static int global_prematuremediafilter;
00547 static int global_limitonpeers;
00548 static int global_rtautoclear;
00549 static int global_notifyringing;
00550 static int global_notifyhold;
00551 static int global_alwaysauthreject;
00552 static int srvlookup;
00553 static int pedanticsipchecking;
00554 static int autocreatepeer;
00555 static int global_relaxdtmf;
00556 static int global_rtptimeout;
00557 static int global_rtpholdtimeout;
00558 static int global_rtpkeepalive;
00559 static int global_reg_timeout;
00560 static int global_regattempts_max;
00561 static int global_shrinkcallerid;
00562 static int global_allowguest;
00563 static int global_allowsubscribe;
00564
00565 static int global_mwitime;
00566 static unsigned int global_tos_sip;
00567 static unsigned int global_tos_audio;
00568 static unsigned int global_tos_video;
00569 static int compactheaders;
00570 static int recordhistory;
00571 static int dumphistory;
00572 static char global_realm[MAXHOSTNAMELEN];
00573 static char global_regcontext[AST_MAX_CONTEXT];
00574 static char global_useragent[AST_MAX_EXTENSION];
00575 static int allow_external_domains;
00576 static int global_callevents;
00577 static int global_t1min;
00578 static int global_autoframing;
00579 static enum transfermodes global_allowtransfer;
00580
00581 static int global_matchexterniplocally;
00582
00583
00584 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00585
00586
00587 static struct ast_ha *global_contact_ha = NULL;
00588 static int global_dynamic_exclude_static = 0;
00589
00590
00591 static int suserobjs = 0;
00592 static int ruserobjs = 0;
00593 static int speerobjs = 0;
00594 static int rpeerobjs = 0;
00595 static int apeerobjs = 0;
00596 static int regobjs = 0;
00597
00598 static struct ast_flags global_flags[2] = {{0}};
00599
00600
00601 AST_MUTEX_DEFINE_STATIC(iflock);
00602
00603
00604
00605 AST_MUTEX_DEFINE_STATIC(netlock);
00606
00607 AST_MUTEX_DEFINE_STATIC(monlock);
00608
00609 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00610
00611
00612
00613 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00614
00615 static int sip_reloading = FALSE;
00616 static enum channelreloadreason sip_reloadreason;
00617
00618 static struct sched_context *sched;
00619 static struct io_context *io;
00620 static int *sipsock_read_id;
00621
00622 #define DEC_CALL_LIMIT 0
00623 #define INC_CALL_LIMIT 1
00624 #define DEC_CALL_RINGING 2
00625 #define INC_CALL_RINGING 3
00626
00627
00628 struct sip_request {
00629 char *rlPart1;
00630 char *rlPart2;
00631 int len;
00632 int headers;
00633 int method;
00634 int lines;
00635 unsigned int flags;
00636 char *header[SIP_MAX_HEADERS];
00637 char *line[SIP_MAX_LINES];
00638 char data[SIP_MAX_PACKET];
00639 char content[SIP_MAX_PACKET];
00640 unsigned int sdp_start;
00641 unsigned int sdp_count;
00642 AST_LIST_ENTRY(sip_request) next;
00643 };
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665 struct sip_dual {
00666 struct ast_channel *chan1;
00667 struct ast_channel *chan2;
00668 struct sip_request req;
00669 int seqno;
00670 };
00671
00672 struct sip_pkt;
00673
00674
00675 struct sip_invite_param {
00676 const char *distinctive_ring;
00677 int addsipheaders;
00678 const char *uri_options;
00679 const char *vxml_url;
00680 char *auth;
00681 char *authheader;
00682 enum sip_auth_type auth_type;
00683 const char *replaces;
00684 int transfer;
00685 };
00686
00687
00688 struct sip_route {
00689 struct sip_route *next;
00690 char hop[0];
00691 };
00692
00693
00694 struct sip_via {
00695 char *via;
00696 const char *protocol;
00697 const char *sent_by;
00698 const char *branch;
00699 const char *maddr;
00700 unsigned int port;
00701 unsigned char ttl;
00702 };
00703
00704
00705 enum domain_mode {
00706 SIP_DOMAIN_AUTO,
00707 SIP_DOMAIN_CONFIG,
00708 };
00709
00710
00711
00712
00713
00714 struct domain {
00715 char domain[MAXHOSTNAMELEN];
00716 char context[AST_MAX_EXTENSION];
00717 enum domain_mode mode;
00718 AST_LIST_ENTRY(domain) list;
00719 };
00720
00721 static AST_LIST_HEAD_STATIC(domain_list, domain);
00722
00723
00724
00725 struct sip_history {
00726 AST_LIST_ENTRY(sip_history) list;
00727 char event[0];
00728 };
00729
00730 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00731
00732
00733 struct sip_auth {
00734 char realm[AST_MAX_EXTENSION];
00735 char username[256];
00736 char secret[256];
00737 char md5secret[256];
00738 struct sip_auth *next;
00739 };
00740
00741
00742 #define SIP_ALREADYGONE (1 << 0)
00743 #define SIP_NEEDDESTROY (1 << 1)
00744 #define SIP_NOVIDEO (1 << 2)
00745 #define SIP_RINGING (1 << 3)
00746 #define SIP_PROGRESS_SENT (1 << 4)
00747 #define SIP_NEEDREINVITE (1 << 5)
00748 #define SIP_PENDINGBYE (1 << 6)
00749 #define SIP_GOTREFER (1 << 7)
00750 #define SIP_PROMISCREDIR (1 << 8)
00751 #define SIP_TRUSTRPID (1 << 9)
00752 #define SIP_USEREQPHONE (1 << 10)
00753 #define SIP_REALTIME (1 << 11)
00754 #define SIP_USECLIENTCODE (1 << 12)
00755 #define SIP_OUTGOING (1 << 13)
00756 #define SIP_FREE_BIT (1 << 14)
00757 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 15)
00758 #define SIP_DTMF (3 << 16)
00759 #define SIP_DTMF_RFC2833 (0 << 16)
00760 #define SIP_DTMF_INBAND (1 << 16)
00761 #define SIP_DTMF_INFO (2 << 16)
00762 #define SIP_DTMF_AUTO (3 << 16)
00763
00764 #define SIP_NAT (3 << 18)
00765 #define SIP_NAT_NEVER (0 << 18)
00766 #define SIP_NAT_RFC3581 (1 << 18)
00767 #define SIP_NAT_ROUTE (2 << 18)
00768 #define SIP_NAT_ALWAYS (3 << 18)
00769
00770 #define SIP_REINVITE (7 << 20)
00771 #define SIP_CAN_REINVITE (1 << 20)
00772 #define SIP_CAN_REINVITE_NAT (2 << 20)
00773 #define SIP_REINVITE_UPDATE (4 << 20)
00774
00775 #define SIP_INSECURE_PORT (1 << 23)
00776 #define SIP_INSECURE_INVITE (1 << 24)
00777
00778 #define SIP_PROG_INBAND (3 << 25)
00779 #define SIP_PROG_INBAND_NEVER (0 << 25)
00780 #define SIP_PROG_INBAND_NO (1 << 25)
00781 #define SIP_PROG_INBAND_YES (2 << 25)
00782 #define SIP_NO_HISTORY (1 << 27)
00783 #define SIP_CALL_LIMIT (1 << 28)
00784 #define SIP_SENDRPID (1 << 29)
00785 #define SIP_INC_COUNT (1 << 30)
00786 #define SIP_G726_NONSTANDARD (1 << 31)
00787
00788 #define SIP_FLAGS_TO_COPY \
00789 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
00790 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
00791 SIP_USEREQPHONE | SIP_INSECURE_PORT | SIP_INSECURE_INVITE)
00792
00793
00794
00795 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
00796 #define SIP_PAGE2_RTUPDATE (1 << 1)
00797 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
00798 #define SIP_PAGE2_RT_FROMCONTACT (1 << 4)
00799 #define SIP_PAGE2_RTSAVE_SYSNAME (1 << 5)
00800
00801 #define SIP_PAGE2_STATECHANGEQUEUE (1 << 9)
00802 #define SIP_PAGE2_IGNOREREGEXPIRE (1 << 10)
00803 #define SIP_PAGE2_DEBUG (3 << 11)
00804 #define SIP_PAGE2_DEBUG_CONFIG (1 << 11)
00805 #define SIP_PAGE2_DEBUG_CONSOLE (1 << 12)
00806 #define SIP_PAGE2_DYNAMIC (1 << 13)
00807 #define SIP_PAGE2_SELFDESTRUCT (1 << 14)
00808 #define SIP_PAGE2_VIDEOSUPPORT (1 << 15)
00809 #define SIP_PAGE2_ALLOWSUBSCRIBE (1 << 16)
00810 #define SIP_PAGE2_ALLOWOVERLAP (1 << 17)
00811 #define SIP_PAGE2_SUBSCRIBEMWIONLY (1 << 18)
00812 #define SIP_PAGE2_INC_RINGING (1 << 19)
00813 #define SIP_PAGE2_T38SUPPORT (7 << 20)
00814 #define SIP_PAGE2_T38SUPPORT_UDPTL (1 << 20)
00815 #define SIP_PAGE2_T38SUPPORT_RTP (2 << 20)
00816 #define SIP_PAGE2_T38SUPPORT_TCP (4 << 20)
00817 #define SIP_PAGE2_CALL_ONHOLD (3 << 23)
00818 #define SIP_PAGE2_CALL_ONHOLD_ACTIVE (1 << 23)
00819 #define SIP_PAGE2_CALL_ONHOLD_ONEDIR (2 << 23)
00820 #define SIP_PAGE2_CALL_ONHOLD_INACTIVE (3 << 23)
00821 #define SIP_PAGE2_RFC2833_COMPENSATE (1 << 25)
00822 #define SIP_PAGE2_BUGGY_MWI (1 << 26)
00823 #define SIP_PAGE2_OUTGOING_CALL (1 << 27)
00824 #define SIP_PAGE2_UDPTL_DESTINATION (1 << 28)
00825 #define SIP_PAGE2_DIALOG_ESTABLISHED (1 << 29)
00826 #define SIP_PAGE2_RPORT_PRESENT (1 << 30)
00827 #define SIP_PAGE2_FORWARD_LOOP_DETECTED (1 << 31)
00828
00829 #define SIP_PAGE2_FLAGS_TO_COPY \
00830 (SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_VIDEOSUPPORT | \
00831 SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | SIP_PAGE2_BUGGY_MWI | \
00832 SIP_PAGE2_UDPTL_DESTINATION | SIP_PAGE2_FORWARD_LOOP_DETECTED)
00833
00834
00835 #define SIP_PKT_DEBUG (1 << 0)
00836 #define SIP_PKT_WITH_TOTAG (1 << 1)
00837 #define SIP_PKT_IGNORE (1 << 2)
00838 #define SIP_PKT_IGNORE_REQ (1 << 4)
00839
00840
00841 #define T38FAX_FILL_BIT_REMOVAL (1 << 0)
00842 #define T38FAX_TRANSCODING_MMR (1 << 1)
00843 #define T38FAX_TRANSCODING_JBIG (1 << 2)
00844
00845 #define T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF (0 << 3)
00846 #define T38FAX_RATE_MANAGEMENT_LOCAL_TCF (1 << 3)
00847
00848 #define T38FAX_UDP_EC_NONE (0 << 4)
00849 #define T38FAX_UDP_EC_FEC (1 << 4)
00850 #define T38FAX_UDP_EC_REDUNDANCY (2 << 4)
00851
00852 #define T38FAX_VERSION (3 << 6)
00853 #define T38FAX_VERSION_0 (0 << 6)
00854 #define T38FAX_VERSION_1 (1 << 6)
00855
00856 #define T38FAX_RATE_2400 (1 << 8)
00857 #define T38FAX_RATE_4800 (1 << 9)
00858 #define T38FAX_RATE_7200 (1 << 10)
00859 #define T38FAX_RATE_9600 (1 << 11)
00860 #define T38FAX_RATE_12000 (1 << 12)
00861 #define T38FAX_RATE_14400 (1 << 13)
00862
00863
00864 static int global_t38_capability = T38FAX_VERSION_0 | T38FAX_RATE_2400 | T38FAX_RATE_4800 | T38FAX_RATE_7200 | T38FAX_RATE_9600;
00865
00866 #define sipdebug ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG)
00867 #define sipdebug_config ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG)
00868 #define sipdebug_console ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE)
00869
00870
00871 struct provisional_keepalive_data {
00872 struct sip_pvt *pvt;
00873 int sched_id;
00874 };
00875
00876
00877 enum t38state {
00878 T38_DISABLED = 0,
00879 T38_LOCAL_REINVITE,
00880 T38_PEER_DIRECT,
00881 T38_PEER_REINVITE,
00882 T38_ENABLED
00883 };
00884
00885
00886 struct t38properties {
00887 struct ast_flags t38support;
00888 int capability;
00889 int peercapability;
00890 int jointcapability;
00891 enum t38state state;
00892 unsigned int direct:1;
00893 };
00894
00895
00896 enum referstatus {
00897 REFER_IDLE,
00898 REFER_SENT,
00899 REFER_RECEIVED,
00900 REFER_CONFIRMED,
00901 REFER_ACCEPTED,
00902 REFER_RINGING,
00903 REFER_200OK,
00904 REFER_FAILED,
00905 REFER_NOAUTH
00906 };
00907
00908 static const struct c_referstatusstring {
00909 enum referstatus status;
00910 char *text;
00911 } referstatusstrings[] = {
00912 { REFER_IDLE, "<none>" },
00913 { REFER_SENT, "Request sent" },
00914 { REFER_RECEIVED, "Request received" },
00915 { REFER_ACCEPTED, "Accepted" },
00916 { REFER_RINGING, "Target ringing" },
00917 { REFER_200OK, "Done" },
00918 { REFER_FAILED, "Failed" },
00919 { REFER_NOAUTH, "Failed - auth failure" }
00920 } ;
00921
00922
00923
00924 struct sip_refer {
00925 char refer_to[AST_MAX_EXTENSION];
00926 char refer_to_domain[AST_MAX_EXTENSION];
00927 char refer_to_urioption[AST_MAX_EXTENSION];
00928 char refer_to_context[AST_MAX_EXTENSION];
00929 char referred_by[AST_MAX_EXTENSION];
00930 char referred_by_name[AST_MAX_EXTENSION];
00931 char refer_contact[AST_MAX_EXTENSION];
00932 char replaces_callid[SIPBUFSIZE];
00933 char replaces_callid_totag[SIPBUFSIZE/2];
00934 char replaces_callid_fromtag[SIPBUFSIZE/2];
00935 struct sip_pvt *refer_call;
00936 int attendedtransfer;
00937 int localtransfer;
00938 enum referstatus status;
00939 };
00940
00941 struct offered_media {
00942 int offered;
00943 char text[128];
00944 };
00945
00946 static struct sip_pvt {
00947 ast_mutex_t lock;
00948 int method;
00949 enum invitestates invitestate;
00950 AST_DECLARE_STRING_FIELDS(
00951 AST_STRING_FIELD(callid);
00952 AST_STRING_FIELD(randdata);
00953 AST_STRING_FIELD(accountcode);
00954 AST_STRING_FIELD(realm);
00955 AST_STRING_FIELD(nonce);
00956 AST_STRING_FIELD(opaque);
00957 AST_STRING_FIELD(qop);
00958 AST_STRING_FIELD(domain);
00959 AST_STRING_FIELD(from);
00960 AST_STRING_FIELD(useragent);
00961 AST_STRING_FIELD(exten);
00962 AST_STRING_FIELD(context);
00963 AST_STRING_FIELD(subscribecontext);
00964 AST_STRING_FIELD(subscribeuri);
00965 AST_STRING_FIELD(fromdomain);
00966 AST_STRING_FIELD(fromuser);
00967 AST_STRING_FIELD(fromname);
00968 AST_STRING_FIELD(tohost);
00969 AST_STRING_FIELD(language);
00970 AST_STRING_FIELD(mohinterpret);
00971 AST_STRING_FIELD(mohsuggest);
00972 AST_STRING_FIELD(rdnis);
00973 AST_STRING_FIELD(theirtag);
00974 AST_STRING_FIELD(username);
00975 AST_STRING_FIELD(peername);
00976 AST_STRING_FIELD(authname);
00977 AST_STRING_FIELD(uri);
00978 AST_STRING_FIELD(okcontacturi);
00979 AST_STRING_FIELD(peersecret);
00980 AST_STRING_FIELD(peermd5secret);
00981 AST_STRING_FIELD(cid_num);
00982 AST_STRING_FIELD(cid_name);
00983 AST_STRING_FIELD(fullcontact);
00984 AST_STRING_FIELD(our_contact);
00985 AST_STRING_FIELD(rpid);
00986 AST_STRING_FIELD(rpid_from);
00987 );
00988 char via[128];
00989 unsigned int ocseq;
00990 unsigned int icseq;
00991 ast_group_t callgroup;
00992 ast_group_t pickupgroup;
00993 int lastinvite;
00994 struct ast_flags flags[2];
00995 int timer_t1;
00996 unsigned int sipoptions;
00997 struct ast_codec_pref prefs;
00998 int capability;
00999 int jointcapability;
01000 int peercapability;
01001 int prefcodec;
01002 int noncodeccapability;
01003 int jointnoncodeccapability;
01004 int redircodecs;
01005 int maxcallbitrate;
01006 struct t38properties t38;
01007 struct sockaddr_in udptlredirip;
01008 struct ast_udptl *udptl;
01009 int callingpres;
01010 int authtries;
01011 int expiry;
01012 long branch;
01013 long invite_branch;
01014 char tag[11];
01015 int sessionid;
01016 int sessionversion;
01017 unsigned int portinuri:1;
01018 struct sockaddr_in sa;
01019 struct sockaddr_in redirip;
01020 struct sockaddr_in vredirip;
01021 time_t lastrtprx;
01022 time_t lastrtptx;
01023 int rtptimeout;
01024 struct sockaddr_in recv;
01025 struct in_addr ourip;
01026 struct ast_channel *owner;
01027 struct sip_route *route;
01028 int route_persistant;
01029 struct sip_auth *peerauth;
01030 int noncecount;
01031 unsigned int stalenonce:1;
01032 char lastmsg[256];
01033 int amaflags;
01034 int pendinginvite;
01035 int glareinvite;
01036
01037
01038 struct sip_request initreq;
01039
01040
01041 int maxtime;
01042 int initid;
01043 int waitid;
01044 int autokillid;
01045 enum transfermodes allowtransfer;
01046 struct sip_refer *refer;
01047 enum subscriptiontype subscribed;
01048 int stateid;
01049 int laststate;
01050 int dialogver;
01051
01052 struct ast_dsp *vad;
01053
01054 struct sip_peer *relatedpeer;
01055
01056 struct sip_registry *registry;
01057 struct ast_rtp *rtp;
01058 struct ast_rtp *vrtp;
01059 struct sip_pkt *packets;
01060 struct sip_history_head *history;
01061 size_t history_entries;
01062 struct ast_variable *chanvars;
01063 AST_LIST_HEAD_NOLOCK(request_queue, sip_request) request_queue;
01064 int request_queue_sched_id;
01065 struct provisional_keepalive_data *provisional_keepalive_data;
01066 const char *last_provisional;
01067 struct sip_pvt *next;
01068 struct sip_invite_param *options;
01069 int autoframing;
01070 int hangupcause;
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085 struct offered_media offered_media[3];
01086 } *iflist = NULL;
01087
01088
01089 #define MAX_HISTORY_ENTRIES 50
01090
01091 #define FLAG_RESPONSE (1 << 0)
01092 #define FLAG_FATAL (1 << 1)
01093
01094
01095 struct sip_pkt {
01096 struct sip_pkt *next;
01097 int retrans;
01098 int method;
01099 int seqno;
01100 int response_code;
01101 unsigned int flags;
01102 struct sip_pvt *owner;
01103 int retransid;
01104 int timer_a;
01105 int timer_t1;
01106 int packetlen;
01107 char data[0];
01108 };
01109
01110
01111 struct sip_user {
01112
01113 ASTOBJ_COMPONENTS(struct sip_user);
01114 char secret[80];
01115 char md5secret[80];
01116 char context[AST_MAX_CONTEXT];
01117 char subscribecontext[AST_MAX_CONTEXT];
01118 char cid_num[80];
01119 char cid_name[80];
01120 char accountcode[AST_MAX_ACCOUNT_CODE];
01121 char language[MAX_LANGUAGE];
01122 char mohinterpret[MAX_MUSICCLASS];
01123 char mohsuggest[MAX_MUSICCLASS];
01124 char useragent[256];
01125 struct ast_codec_pref prefs;
01126 ast_group_t callgroup;
01127 ast_group_t pickupgroup;
01128 unsigned int sipoptions;
01129 struct ast_flags flags[2];
01130 int amaflags;
01131 int callingpres;
01132 int capability;
01133 int inUse;
01134 int call_limit;
01135 enum transfermodes allowtransfer;
01136 struct ast_ha *ha;
01137 struct ast_variable *chanvars;
01138 int maxcallbitrate;
01139 int autoframing;
01140 };
01141
01142
01143
01144 struct sip_peer {
01145 ASTOBJ_COMPONENTS(struct sip_peer);
01146
01147 char secret[80];
01148 char md5secret[80];
01149 struct sip_auth *auth;
01150 char context[AST_MAX_CONTEXT];
01151 char subscribecontext[AST_MAX_CONTEXT];
01152 char username[80];
01153 char accountcode[AST_MAX_ACCOUNT_CODE];
01154 int amaflags;
01155 char tohost[MAXHOSTNAMELEN];
01156 char regexten[AST_MAX_EXTENSION];
01157 char fromuser[80];
01158 char fromdomain[MAXHOSTNAMELEN];
01159 char fullcontact[256];
01160 char cid_num[80];
01161 char cid_name[80];
01162 int callingpres;
01163 int inUse;
01164 int inRinging;
01165 int onHold;
01166 int call_limit;
01167 enum transfermodes allowtransfer;
01168 char vmexten[AST_MAX_EXTENSION];
01169 char mailbox[AST_MAX_EXTENSION];
01170 char language[MAX_LANGUAGE];
01171 char mohinterpret[MAX_MUSICCLASS];
01172 char mohsuggest[MAX_MUSICCLASS];
01173 char useragent[256];
01174 struct ast_codec_pref prefs;
01175 int lastmsgssent;
01176 time_t lastmsgcheck;
01177 unsigned int sipoptions;
01178 struct ast_flags flags[2];
01179 int expire;
01180 int capability;
01181 int rtptimeout;
01182 int rtpholdtimeout;
01183 int rtpkeepalive;
01184 ast_group_t callgroup;
01185 ast_group_t pickupgroup;
01186 struct sockaddr_in addr;
01187 int maxcallbitrate;
01188 unsigned int portinuri:1;
01189
01190
01191 struct sip_pvt *call;
01192 int pokeexpire;
01193 int lastms;
01194 int maxms;
01195 struct timeval ps;
01196
01197 struct sockaddr_in defaddr;
01198 struct ast_ha *ha;
01199 struct ast_ha *contactha;
01200 struct ast_variable *chanvars;
01201 struct sip_pvt *mwipvt;
01202 int lastmsg;
01203 int autoframing;
01204 };
01205
01206
01207
01208
01209 struct sip_registry {
01210 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
01211 AST_DECLARE_STRING_FIELDS(
01212 AST_STRING_FIELD(callid);
01213 AST_STRING_FIELD(realm);
01214 AST_STRING_FIELD(nonce);
01215 AST_STRING_FIELD(opaque);
01216 AST_STRING_FIELD(qop);
01217 AST_STRING_FIELD(domain);
01218 AST_STRING_FIELD(username);
01219 AST_STRING_FIELD(authuser);
01220 AST_STRING_FIELD(hostname);
01221 AST_STRING_FIELD(secret);
01222 AST_STRING_FIELD(md5secret);
01223 AST_STRING_FIELD(contact);
01224 AST_STRING_FIELD(random);
01225 );
01226 int portno;
01227 int expire;
01228 int regattempts;
01229 int timeout;
01230 int refresh;
01231 struct sip_pvt *call;
01232 enum sipregistrystate regstate;
01233 unsigned int needdns:1;
01234 time_t regtime;
01235 int callid_valid;
01236 unsigned int ocseq;
01237 struct sockaddr_in us;
01238 int noncecount;
01239 char lastmsg[256];
01240 };
01241
01242
01243
01244
01245 static struct ast_user_list {
01246 ASTOBJ_CONTAINER_COMPONENTS(struct sip_user);
01247 } userl;
01248
01249
01250 static struct ast_peer_list {
01251 ASTOBJ_CONTAINER_COMPONENTS(struct sip_peer);
01252 } peerl;
01253
01254
01255 static struct ast_register_list {
01256 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01257 int recheck;
01258 } regl;
01259
01260 static void temp_pvt_cleanup(void *);
01261
01262
01263 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01264
01265 #ifdef LOW_MEMORY
01266 static void ts_ast_rtp_destroy(void *);
01267
01268 AST_THREADSTORAGE_CUSTOM(ts_audio_rtp, ts_audio_rtp_init, ts_ast_rtp_destroy);
01269 AST_THREADSTORAGE_CUSTOM(ts_video_rtp, ts_video_rtp_init, ts_ast_rtp_destroy);
01270 #endif
01271
01272 struct sip_extenstate_update {
01273 struct sip_pvt *pvt;
01274 int state;
01275 int marked;
01276 AST_LIST_ENTRY(sip_extenstate_update) list;
01277 char *context;
01278 char exten[0];
01279 };
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303 static AST_LIST_HEAD_STATIC(sip_extenstate_updates, sip_extenstate_update);
01304
01305
01306 static struct sip_auth *authl = NULL;
01307
01308
01309
01310 static int sipsock = -1;
01311 static struct sockaddr_in bindaddr = { 0, };
01312 static struct sockaddr_in externip;
01313 static char externhost[MAXHOSTNAMELEN];
01314 static time_t externexpire = 0;
01315 static int externrefresh = 10;
01316 static struct ast_ha *localaddr;
01317 static struct in_addr __ourip;
01318 static struct sockaddr_in outboundproxyip;
01319 static int ourport;
01320 static struct sockaddr_in debugaddr;
01321
01322 static struct ast_config *notify_types = NULL;
01323
01324
01325
01326
01327
01328
01329 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
01330 static int sip_devicestate(void *data);
01331 static int sip_sendtext(struct ast_channel *ast, const char *text);
01332 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01333 static int sip_hangup(struct ast_channel *ast);
01334 static int sip_answer(struct ast_channel *ast);
01335 static struct ast_frame *sip_read(struct ast_channel *ast);
01336 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01337 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01338 static int sip_transfer(struct ast_channel *ast, const char *dest);
01339 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01340 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01341 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01342
01343
01344 static int sipsock_read(int *id, int fd, short events, void *ignore);
01345 static int __sip_xmit(struct sip_pvt *p, char *data, int len);
01346 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod);
01347 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01348 static int retrans_pkt(const void *data);
01349 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req);
01350 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);
01351 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01352 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01353 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01354 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01355 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01356 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);
01357 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp);
01358 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01359 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable);
01360 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01361 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01362 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
01363 static int transmit_reinvite_with_sdp(struct sip_pvt *p);
01364 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01365 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01366 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01367 static int transmit_refer(struct sip_pvt *p, const char *dest);
01368 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten);
01369 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01370 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01371 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01372 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01373 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01374 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01375 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
01376 static int sip_send_mwi_to_peer(struct sip_peer *peer, int force);
01377 static int does_peer_need_mwi(struct sip_peer *peer);
01378
01379
01380 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
01381 int useglobal_nat, const int intended_method);
01382 static int __sip_autodestruct(const void *data);
01383 static void sip_scheddestroy(struct sip_pvt *p, int ms);
01384 static int sip_cancel_destroy(struct sip_pvt *p);
01385 static void sip_destroy(struct sip_pvt *p);
01386 static int __sip_destroy(struct sip_pvt *p, int lockowner);
01387 static int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01388 static void __sip_pretend_ack(struct sip_pvt *p);
01389 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01390 static int auto_congest(const void *nothing);
01391 static int update_call_counter(struct sip_pvt *fup, int event);
01392 static int hangup_sip2cause(int cause);
01393 static const char *hangup_cause2sip(int cause);
01394 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method);
01395 static void free_old_route(struct sip_route *route);
01396 static void list_route(struct sip_route *route);
01397 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01398 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
01399 struct sip_request *req, char *uri);
01400 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01401 static void check_pendings(struct sip_pvt *p);
01402 static void *sip_park_thread(void *stuff);
01403 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno);
01404 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01405
01406
01407 static void try_suggested_sip_codec(struct sip_pvt *p);
01408 static const char *get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01409 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value);
01410 static int find_sdp(struct sip_request *req);
01411 static int process_sdp(struct sip_pvt *p, struct sip_request *req);
01412 static int process_sdp_c(const char *c, struct ast_hostent *hp);
01413 static int process_sdp_a_sendonly(const char *a, int *sendonly);
01414 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp *newaudiortp, int *last_rtpmap_codec);
01415 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp *newvideortp, int *last_rtpmap_codec);
01416 static int process_sdp_a_image(const char *a, struct sip_pvt *p);
01417 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
01418 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01419 int debug, int *min_packet_size);
01420 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
01421 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01422 int debug);
01423 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int add_audio, int add_t38);
01424 static void stop_media_flows(struct sip_pvt *p);
01425
01426
01427 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01428 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01429 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01430 const char *secret, const char *md5secret, int sipmethod,
01431 char *uri, enum xmittype reliable, int ignore);
01432 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01433 int sipmethod, char *uri, enum xmittype reliable,
01434 struct sockaddr_in *sin, struct sip_peer **authpeer);
01435 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin);
01436
01437
01438 static int check_sip_domain(const char *domain, char *context, size_t len);
01439 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01440 static void clear_sip_domains(void);
01441
01442
01443 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno);
01444 static int clear_realm_authentication(struct sip_auth *authlist);
01445 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
01446
01447
01448 static int sip_do_reload(enum channelreloadreason reason);
01449 static int reload_config(enum channelreloadreason reason);
01450 static int expire_register(const void *data);
01451 static void *do_monitor(void *data);
01452 static int restart_monitor(void);
01453 static int sip_addrcmp(char *name, struct sockaddr_in *sin);
01454 static int sip_refer_allocate(struct sip_pvt *p);
01455 static void ast_quiet_chan(struct ast_channel *chan);
01456 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01457
01458
01459 static int notify_extenstate_update(char *context, char* exten, int state, void *data);
01460 static void clear_extenstate_updates(struct sip_pvt *pvt);
01461 static int sip_devicestate(void *data);
01462 static int sip_poke_noanswer(const void *data);
01463 static int sip_poke_peer(struct sip_peer *peer);
01464 static void sip_poke_all_peers(void);
01465 static void sip_peer_hold(struct sip_pvt *p, int hold);
01466
01467
01468 static const char *sip_nat_mode(const struct sip_pvt *p);
01469 static int sip_show_inuse(int fd, int argc, char *argv[]);
01470 static char *transfermode2str(enum transfermodes mode) attribute_const;
01471 static char *nat2str(int nat) attribute_const;
01472 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01473 static int sip_show_users(int fd, int argc, char *argv[]);
01474 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01475 static int sip_show_peers(int fd, int argc, char *argv[]);
01476 static int sip_show_objects(int fd, int argc, char *argv[]);
01477 static void print_group(int fd, ast_group_t group, int crlf);
01478 static const char *dtmfmode2str(int mode) attribute_const;
01479 static const char *insecure2str(int port, int invite) attribute_const;
01480 static void cleanup_stale_contexts(char *new, char *old);
01481 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01482 static const char *domain_mode_to_text(const enum domain_mode mode);
01483 static int sip_show_domains(int fd, int argc, char *argv[]);
01484 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01485 static int sip_show_peer(int fd, int argc, char *argv[]);
01486 static int sip_show_user(int fd, int argc, char *argv[]);
01487 static int sip_show_registry(int fd, int argc, char *argv[]);
01488 static int sip_show_settings(int fd, int argc, char *argv[]);
01489 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01490 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01491 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01492 static int sip_show_channels(int fd, int argc, char *argv[]);
01493 static int sip_show_subscriptions(int fd, int argc, char *argv[]);
01494 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01495 static char *complete_sipch(const char *line, const char *word, int pos, int state);
01496 static char *complete_sip_peer(const char *word, int state, int flags2);
01497 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01498 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state);
01499 static char *complete_sip_user(const char *word, int state, int flags2);
01500 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
01501 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01502 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state);
01503 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state);
01504 static int sip_show_channel(int fd, int argc, char *argv[]);
01505 static int sip_show_history(int fd, int argc, char *argv[]);
01506 static int sip_do_debug_ip(int fd, int argc, char *argv[]);
01507 static int sip_do_debug_peer(int fd, int argc, char *argv[]);
01508 static int sip_do_debug(int fd, int argc, char *argv[]);
01509 static int sip_no_debug(int fd, int argc, char *argv[]);
01510 static int sip_notify(int fd, int argc, char *argv[]);
01511 static int sip_do_history(int fd, int argc, char *argv[]);
01512 static int sip_no_history(int fd, int argc, char *argv[]);
01513 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
01514 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01515 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01516 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01517 static int sip_dtmfmode(struct ast_channel *chan, void *data);
01518 static int sip_addheader(struct ast_channel *chan, void *data);
01519 static int sip_do_reload(enum channelreloadreason reason);
01520 static int sip_reload(int fd, int argc, char *argv[]);
01521 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen);
01522
01523
01524
01525
01526
01527 static void sip_dump_history(struct sip_pvt *dialog);
01528 static inline int sip_debug_test_addr(const struct sockaddr_in *addr);
01529 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01530 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01531 static void sip_dump_history(struct sip_pvt *dialog);
01532
01533
01534 static struct sip_peer *temp_peer(const char *name);
01535 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only);
01536 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01537 static int update_call_counter(struct sip_pvt *fup, int event);
01538 static void sip_destroy_peer(struct sip_peer *peer);
01539 static void sip_destroy_user(struct sip_user *user);
01540 static int sip_poke_peer(struct sip_peer *peer);
01541 static int sip_poke_peer_s(const void *data);
01542 static void set_peer_defaults(struct sip_peer *peer);
01543 static struct sip_peer *temp_peer(const char *name);
01544 static void register_peer_exten(struct sip_peer *peer, int onoff);
01545 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int devstate_only);
01546 static struct sip_user *find_user(const char *name, int realtime);
01547 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01548 static int expire_register(const void *data);
01549 static void reg_source_db(struct sip_peer *peer);
01550 static void destroy_association(struct sip_peer *peer);
01551 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01552
01553
01554 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey, int lastms);
01555 static struct sip_user *realtime_user(const char *username);
01556 static void update_peer(struct sip_peer *p, int expiry);
01557 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin, int devstate_only);
01558 static int sip_prune_realtime(int fd, int argc, char *argv[]);
01559
01560
01561 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us);
01562 static void sip_registry_destroy(struct sip_registry *reg);
01563 static int sip_register(char *value, int lineno);
01564 static char *regstate2str(enum sipregistrystate regstate) attribute_const;
01565 static int sip_reregister(const void *data);
01566 static int __sip_do_register(struct sip_registry *r);
01567 static int sip_reg_timeout(const void *data);
01568 static void sip_send_all_registers(void);
01569
01570
01571 static void append_date(struct sip_request *req);
01572 static int determine_firstline_parts(struct sip_request *req);
01573 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01574 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01575 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
01576 static int find_sip_method(const char *msg);
01577 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
01578 static int parse_request(struct sip_request *req);
01579 static const char *get_header(const struct sip_request *req, const char *name);
01580 static char *referstatus2str(enum referstatus rstatus) attribute_pure;
01581 static int method_match(enum sipmethod id, const char *name);
01582 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
01583 static char *get_in_brackets(char *tmp);
01584 static const char *find_alias(const char *name, const char *_default);
01585 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
01586 static int lws2sws(char *msgbuf, int len);
01587 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
01588 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
01589 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
01590 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
01591 static int set_address_from_contact(struct sip_pvt *pvt);
01592 static void check_via(struct sip_pvt *p, const struct sip_request *req);
01593 static char *get_calleridname(const char *input, char *output, size_t outputsize);
01594 static int get_rpid_num(const char *input, char *output, int maxlen);
01595 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq);
01596 static int get_destination(struct sip_pvt *p, struct sip_request *oreq);
01597 static int get_msg_text(char *buf, int len, struct sip_request *req);
01598 static void free_old_route(struct sip_route *route);
01599 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
01600 static struct sip_via *parse_via(const char *header);
01601 static void free_via(struct sip_via *v);
01602
01603
01604 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
01605 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
01606 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
01607 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod);
01608 static int init_resp(struct sip_request *resp, const char *msg);
01609 static inline int resp_needs_contact(const char *msg, enum sipmethod method);
01610 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
01611 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p);
01612 static void build_via(struct sip_pvt *p);
01613 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
01614 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct sockaddr_in *sin);
01615 static char *generate_random_string(char *buf, size_t size);
01616 static void build_callid_pvt(struct sip_pvt *pvt);
01617 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain);
01618 static void make_our_tag(char *tagbuf, size_t len);
01619 static int add_header(struct sip_request *req, const char *var, const char *value);
01620 static int add_content(struct sip_request *req, const char *line);
01621 static int finalize_content(struct sip_request *req);
01622 static int add_text(struct sip_request *req, const char *text);
01623 static int add_digit(struct sip_request *req, char digit, unsigned int duration);
01624 static int add_vidupdate(struct sip_request *req);
01625 static void add_route(struct sip_request *req, struct sip_route *route);
01626 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01627 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01628 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
01629 static void set_destination(struct sip_pvt *p, char *uri);
01630 static void append_date(struct sip_request *req);
01631 static void build_contact(struct sip_pvt *p);
01632 static void build_rpid(struct sip_pvt *p);
01633
01634
01635 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock);
01636 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);
01637 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock);
01638 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
01639 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e);
01640 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
01641 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
01642 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01643 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
01644 static int handle_request_options(struct sip_pvt *p, struct sip_request *req);
01645 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);
01646 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01647 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno);
01648
01649
01650 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01651 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01652 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01653 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01654
01655
01656 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active);
01657 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01658 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01659 static int sip_get_codec(struct ast_channel *chan);
01660 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect);
01661
01662
01663 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite);
01664 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
01665 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p);
01666 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
01667 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
01668
01669
01670 static const struct ast_channel_tech sip_tech = {
01671 .type = "SIP",
01672 .description = "Session Initiation Protocol (SIP)",
01673 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01674 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01675 .requester = sip_request_call,
01676 .devicestate = sip_devicestate,
01677 .call = sip_call,
01678 .hangup = sip_hangup,
01679 .answer = sip_answer,
01680 .read = sip_read,
01681 .write = sip_write,
01682 .write_video = sip_write,
01683 .indicate = sip_indicate,
01684 .transfer = sip_transfer,
01685 .fixup = sip_fixup,
01686 .send_digit_begin = sip_senddigit_begin,
01687 .send_digit_end = sip_senddigit_end,
01688 .bridge = ast_rtp_bridge,
01689 .send_text = sip_sendtext,
01690 .func_channel_read = acf_channel_read,
01691 };
01692
01693
01694
01695
01696 static const struct ast_channel_tech sip_tech_info = {
01697 .type = "SIP",
01698 .description = "Session Initiation Protocol (SIP)",
01699 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01700 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01701 .requester = sip_request_call,
01702 .devicestate = sip_devicestate,
01703 .call = sip_call,
01704 .hangup = sip_hangup,
01705 .answer = sip_answer,
01706 .read = sip_read,
01707 .write = sip_write,
01708 .write_video = sip_write,
01709 .indicate = sip_indicate,
01710 .transfer = sip_transfer,
01711 .fixup = sip_fixup,
01712 .send_digit_end = sip_senddigit_end,
01713 .bridge = ast_rtp_bridge,
01714 .send_text = sip_sendtext,
01715 .func_channel_read = acf_channel_read,
01716 };
01717
01718
01719
01720 #define UNLINK(element, head, prev) do { \
01721 if (prev) \
01722 (prev)->next = (element)->next; \
01723 else \
01724 (head) = (element)->next; \
01725 } while (0)
01726
01727
01728 static struct ast_rtp_protocol sip_rtp = {
01729 type: "SIP",
01730 get_rtp_info: sip_get_rtp_peer,
01731 get_vrtp_info: sip_get_vrtp_peer,
01732 set_rtp_peer: sip_set_rtp_peer,
01733 get_codec: sip_get_codec,
01734 };
01735
01736
01737 static struct ast_udptl_protocol sip_udptl = {
01738 type: "SIP",
01739 get_udptl_info: sip_get_udptl_peer,
01740 set_udptl_peer: sip_set_udptl_peer,
01741 };
01742
01743
01744 static char *referstatus2str(enum referstatus rstatus)
01745 {
01746 int i = (sizeof(referstatusstrings) / sizeof(referstatusstrings[0]));
01747 int x;
01748
01749 for (x = 0; x < i; x++) {
01750 if (referstatusstrings[x].status == rstatus)
01751 return (char *) referstatusstrings[x].text;
01752 }
01753 return "";
01754 }
01755
01756
01757
01758
01759 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
01760 {
01761 if (p->initreq.headers && option_debug) {
01762 ast_log(LOG_DEBUG, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
01763 }
01764
01765 copy_request(&p->initreq, req);
01766 parse_request(&p->initreq);
01767 if (ast_test_flag(req, SIP_PKT_DEBUG))
01768 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
01769 }
01770
01771 static void sip_alreadygone(struct sip_pvt *dialog)
01772 {
01773 if (option_debug > 2)
01774 ast_log(LOG_DEBUG, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
01775 ast_set_flag(&dialog->flags[0], SIP_ALREADYGONE);
01776 }
01777
01778
01779
01780
01781
01782
01783
01784
01785 static int method_match(enum sipmethod id, const char *name)
01786 {
01787 int len = strlen(sip_methods[id].text);
01788 int l_name = name ? strlen(name) : 0;
01789
01790 return (l_name >= len && name[len] < 33 &&
01791 !strncasecmp(sip_methods[id].text, name, len));
01792 }
01793
01794
01795 static int find_sip_method(const char *msg)
01796 {
01797 int i, res = 0;
01798
01799 if (ast_strlen_zero(msg))
01800 return 0;
01801 for (i = 1; i < (sizeof(sip_methods) / sizeof(sip_methods[0])) && !res; i++) {
01802 if (method_match(i, msg))
01803 res = sip_methods[i].id;
01804 }
01805 return res;
01806 }
01807
01808
01809 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
01810 {
01811 char *next, *sep;
01812 char *temp;
01813 unsigned int profile = 0;
01814 int i, found;
01815
01816 if (ast_strlen_zero(supported) )
01817 return 0;
01818 temp = ast_strdupa(supported);
01819
01820 if (option_debug > 2 && sipdebug)
01821 ast_log(LOG_DEBUG, "Begin: parsing SIP \"Supported: %s\"\n", supported);
01822
01823 for (next = temp; next; next = sep) {
01824 found = FALSE;
01825 if ( (sep = strchr(next, ',')) != NULL)
01826 *sep++ = '\0';
01827 next = ast_skip_blanks(next);
01828 if (option_debug > 2 && sipdebug)
01829 ast_log(LOG_DEBUG, "Found SIP option: -%s-\n", next);
01830 for (i=0; i < (sizeof(sip_options) / sizeof(sip_options[0])); i++) {
01831 if (!strcasecmp(next, sip_options[i].text)) {
01832 profile |= sip_options[i].id;
01833 found = TRUE;
01834 if (option_debug > 2 && sipdebug)
01835 ast_log(LOG_DEBUG, "Matched SIP option: %s\n", next);
01836 break;
01837 }
01838 }
01839 if (!found && option_debug > 2 && sipdebug) {
01840 if (!strncasecmp(next, "x-", 2))
01841 ast_log(LOG_DEBUG, "Found private SIP option, not supported: %s\n", next);
01842 else
01843 ast_log(LOG_DEBUG, "Found no match for SIP option: %s (Please file bug report!)\n", next);
01844 }
01845 }
01846
01847 if (pvt)
01848 pvt->sipoptions = profile;
01849 return profile;
01850 }
01851
01852
01853 static inline int sip_debug_test_addr(const struct sockaddr_in *addr)
01854 {
01855 if (!sipdebug)
01856 return 0;
01857 if (debugaddr.sin_addr.s_addr) {
01858 if (((ntohs(debugaddr.sin_port) != 0)
01859 && (debugaddr.sin_port != addr->sin_port))
01860 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
01861 return 0;
01862 }
01863 return 1;
01864 }
01865
01866
01867 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
01868 {
01869 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
01870 }
01871
01872
01873 static const char *sip_nat_mode(const struct sip_pvt *p)
01874 {
01875 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
01876 }
01877
01878
01879 static inline int sip_debug_test_pvt(struct sip_pvt *p)
01880 {
01881 if (!sipdebug)
01882 return 0;
01883 return sip_debug_test_addr(sip_real_dst(p));
01884 }
01885
01886
01887 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
01888 {
01889 int res;
01890 const struct sockaddr_in *dst = sip_real_dst(p);
01891 res = sendto(sipsock, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
01892
01893 if (res == -1) {
01894 switch (errno) {
01895 case EBADF:
01896 case EHOSTUNREACH:
01897 case ENETDOWN:
01898 case ENETUNREACH:
01899 case ECONNREFUSED:
01900 res = XMIT_ERROR;
01901 }
01902
01903 }
01904
01905 if (res != len)
01906 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));
01907 return res;
01908 }
01909
01910
01911
01912 static void build_via(struct sip_pvt *p)
01913 {
01914
01915 const char *rport = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_RFC3581 ? ";rport" : "";
01916
01917
01918 snprintf(p->via, sizeof(p->via), "SIP/2.0/UDP %s:%d;branch=z9hG4bK%08x%s",
01919 ast_inet_ntoa(p->ourip), ourport, (int) p->branch, rport);
01920 }
01921
01922
01923
01924
01925
01926
01927
01928 static enum sip_result ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us)
01929 {
01930 struct sockaddr_in theirs, ours;
01931
01932
01933 ast_ouraddrfor(them, us);
01934 theirs.sin_addr = *them;
01935 ours.sin_addr = *us;
01936
01937 if (localaddr && externip.sin_addr.s_addr &&
01938 (ast_apply_ha(localaddr, &theirs)) &&
01939 (!global_matchexterniplocally || !ast_apply_ha(localaddr, &ours))) {
01940 if (externexpire && time(NULL) >= externexpire) {
01941 struct ast_hostent ahp;
01942 struct hostent *hp;
01943
01944 externexpire = time(NULL) + externrefresh;
01945 if ((hp = ast_gethostbyname(externhost, &ahp))) {
01946 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
01947 } else
01948 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
01949 }
01950 *us = externip.sin_addr;
01951 if (option_debug) {
01952 ast_log(LOG_DEBUG, "Target address %s is not local, substituting externip\n",
01953 ast_inet_ntoa(*(struct in_addr *)&them->s_addr));
01954 }
01955 } else if (bindaddr.sin_addr.s_addr)
01956 *us = bindaddr.sin_addr;
01957 return AST_SUCCESS;
01958 }
01959
01960
01961
01962 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
01963
01964 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01965 __attribute__((format(printf, 2, 3)));
01966
01967
01968 static void __attribute__((format(printf, 2, 0))) append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
01969 {
01970 char buf[80], *c = buf;
01971 struct sip_history *hist;
01972 int l;
01973
01974 vsnprintf(buf, sizeof(buf), fmt, ap);
01975 strsep(&c, "\r\n");
01976 l = strlen(buf) + 1;
01977 if (!(hist = ast_calloc(1, sizeof(*hist) + l)))
01978 return;
01979 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
01980 free(hist);
01981 return;
01982 }
01983 memcpy(hist->event, buf, l);
01984 if (p->history_entries == MAX_HISTORY_ENTRIES) {
01985 struct sip_history *oldest;
01986 oldest = AST_LIST_REMOVE_HEAD(p->history, list);
01987 p->history_entries--;
01988 free(oldest);
01989 }
01990 AST_LIST_INSERT_TAIL(p->history, hist, list);
01991 p->history_entries++;
01992 }
01993
01994
01995 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01996 {
01997 va_list ap;
01998
01999 if (!p)
02000 return;
02001
02002 if (ast_test_flag(&p->flags[0], SIP_NO_HISTORY)
02003 && !recordhistory && !dumphistory) {
02004 return;
02005 }
02006
02007 va_start(ap, fmt);
02008 append_history_va(p, fmt, ap);
02009 va_end(ap);
02010
02011 return;
02012 }
02013
02014
02015 static int retrans_pkt(const void *data)
02016 {
02017 struct sip_pkt *pkt = (struct sip_pkt *)data, *prev, *cur = NULL;
02018 int reschedule = DEFAULT_RETRANS;
02019 int xmitres = 0;
02020
02021
02022 ast_mutex_lock(&pkt->owner->lock);
02023
02024 if (pkt->retrans < MAX_RETRANS) {
02025 pkt->retrans++;
02026 if (!pkt->timer_t1) {
02027 if (sipdebug && option_debug > 3)
02028 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);
02029 } else {
02030 int siptimer_a;
02031
02032 if (sipdebug && option_debug > 3)
02033 ast_log(LOG_DEBUG, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
02034 if (!pkt->timer_a)
02035 pkt->timer_a = 2 ;
02036 else
02037 pkt->timer_a = 2 * pkt->timer_a;
02038
02039
02040 siptimer_a = pkt->timer_t1 * pkt->timer_a;
02041 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
02042 siptimer_a = 4000;
02043
02044
02045 reschedule = siptimer_a;
02046 if (option_debug > 3)
02047 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);
02048 }
02049
02050 if (sip_debug_test_pvt(pkt->owner)) {
02051 const struct sockaddr_in *dst = sip_real_dst(pkt->owner);
02052 ast_verbose("Retransmitting #%d (%s) to %s:%d:\n%s\n---\n",
02053 pkt->retrans, sip_nat_mode(pkt->owner),
02054 ast_inet_ntoa(dst->sin_addr),
02055 ntohs(dst->sin_port), pkt->data);
02056 }
02057
02058 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data);
02059 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02060 if (xmitres == XMIT_ERROR) {
02061 ast_log(LOG_WARNING, "Network error on retransmit in dialog %s\n", pkt->owner->callid);
02062 } else {
02063 ast_mutex_unlock(&pkt->owner->lock);
02064 return reschedule;
02065 }
02066 }
02067
02068 if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
02069 if (ast_test_flag(pkt, FLAG_FATAL) || sipdebug)
02070 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);
02071 } else if ((pkt->method == SIP_OPTIONS) && sipdebug) {
02072 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) -- See doc/sip-retransmit.txt.\n", pkt->owner->callid);
02073 }
02074 if (xmitres == XMIT_ERROR) {
02075 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission of transaction in call id %s \n", pkt->owner->callid);
02076 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02077 } else
02078 append_history(pkt->owner, "MaxRetries", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02079
02080 pkt->retransid = -1;
02081
02082 if (ast_test_flag(pkt, FLAG_FATAL)) {
02083 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
02084 DEADLOCK_AVOIDANCE(&pkt->owner->lock);
02085 }
02086
02087 if (pkt->owner->owner && !pkt->owner->owner->hangupcause)
02088 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
02089
02090 if (pkt->owner->owner) {
02091 sip_alreadygone(pkt->owner);
02092 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);
02093 ast_queue_hangup(pkt->owner->owner);
02094 ast_channel_unlock(pkt->owner->owner);
02095 } else {
02096
02097
02098
02099 if (pkt->method != SIP_OPTIONS) {
02100 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
02101 sip_alreadygone(pkt->owner);
02102 if (option_debug)
02103 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
02104 }
02105 }
02106 }
02107
02108 if (pkt->method == SIP_BYE) {
02109
02110 sip_alreadygone(pkt->owner);
02111 if (pkt->owner->owner)
02112 ast_channel_unlock(pkt->owner->owner);
02113 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
02114 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
02115 }
02116
02117
02118 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
02119 if (cur == pkt)
02120 break;
02121 }
02122 if (cur) {
02123 if (prev)
02124 prev->next = cur->next;
02125 else
02126 pkt->owner->packets = cur->next;
02127 ast_mutex_unlock(&pkt->owner->lock);
02128 free(cur);
02129 pkt = NULL;
02130 } else
02131 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
02132 if (pkt)
02133 ast_mutex_unlock(&pkt->owner->lock);
02134 return 0;
02135 }
02136
02137
02138
02139
02140 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod)
02141 {
02142 struct sip_pkt *pkt;
02143 int siptimer_a = DEFAULT_RETRANS;
02144 int xmitres = 0;
02145 int respid;
02146
02147 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1)))
02148 return AST_FAILURE;
02149 memcpy(pkt->data, data, len);
02150 pkt->method = sipmethod;
02151 pkt->packetlen = len;
02152 pkt->next = p->packets;
02153 pkt->owner = p;
02154 pkt->seqno = seqno;
02155 pkt->data[len] = '\0';
02156 if (resp) {
02157 ast_set_flag(pkt, FLAG_RESPONSE);
02158
02159 if (sscanf(pkt->data, "SIP/2.0 %30d", &respid) == 1) {
02160 pkt->response_code = respid;
02161 }
02162 }
02163 pkt->timer_t1 = p->timer_t1;
02164 pkt->retransid = -1;
02165 if (fatal)
02166 ast_set_flag(pkt, FLAG_FATAL);
02167 if (pkt->timer_t1)
02168 siptimer_a = pkt->timer_t1 * 2;
02169
02170 if (option_debug > 3 && sipdebug)
02171 ast_log(LOG_DEBUG, "*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->retransid);
02172 pkt->retransid = -1;
02173 pkt->next = p->packets;
02174 p->packets = pkt;
02175 if (sipmethod == SIP_INVITE) {
02176
02177 p->pendinginvite = seqno;
02178 }
02179
02180 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02181
02182 if (xmitres == XMIT_ERROR) {
02183 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02184 return AST_FAILURE;
02185 } else {
02186
02187 pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
02188 return AST_SUCCESS;
02189 }
02190 }
02191
02192
02193 static int __sip_autodestruct(const void *data)
02194 {
02195 struct sip_pvt *p = (struct sip_pvt *)data;
02196
02197
02198 if (p->subscribed) {
02199 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
02200 p->subscribed = NONE;
02201 append_history(p, "Subscribestatus", "timeout");
02202 if (option_debug > 2)
02203 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP subsription %s\n", p->callid ? p->callid : "<unknown>");
02204 return 10000;
02205 }
02206
02207
02208
02209
02210
02211
02212 if (p->packets && !ast_test_flag(&p->flags[0], SIP_NEEDDESTROY)) {
02213 char method_str[31];
02214 if (option_debug > 2)
02215 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP call %s\n", p->callid ? p->callid : "<unknown>");
02216 append_history(p, "ReliableXmit", "timeout");
02217 if (sscanf(p->lastmsg, "Tx: %30s", method_str) == 1 || sscanf(p->lastmsg, "Rx: %30s", method_str) == 1) {
02218 if (method_match(SIP_CANCEL, method_str) || method_match(SIP_BYE, method_str)) {
02219 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
02220 }
02221 }
02222 return 10000;
02223 }
02224
02225
02226 p->autokillid = -1;
02227
02228 if (option_debug)
02229 ast_log(LOG_DEBUG, "Auto destroying SIP dialog '%s'\n", p->callid);
02230 append_history(p, "AutoDestroy", "%s", p->callid);
02231
02232
02233
02234
02235 ast_mutex_lock(&p->lock);
02236 while (p->owner && ast_channel_trylock(p->owner)) {
02237 DEADLOCK_AVOIDANCE(&p->lock);
02238 }
02239
02240 if (p->owner) {
02241 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
02242 ast_queue_hangup(p->owner);
02243 ast_channel_unlock(p->owner);
02244 ast_mutex_unlock(&p->lock);
02245 } else if (p->refer && !ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
02246 if (option_debug > 2)
02247 ast_log(LOG_DEBUG, "Finally hanging up channel after transfer: %s\n", p->callid);
02248 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
02249 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
02250 ast_mutex_unlock(&p->lock);
02251 } else {
02252 ast_mutex_unlock(&p->lock);
02253 sip_destroy(p);
02254 }
02255
02256 return 0;
02257 }
02258
02259
02260 static void sip_scheddestroy(struct sip_pvt *p, int ms)
02261 {
02262 if (ms < 0) {
02263 if (p->timer_t1 == 0)
02264 p->timer_t1 = 500;
02265 ms = p->timer_t1 * 64;
02266 }
02267 if (sip_debug_test_pvt(p))
02268 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
02269 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
02270 append_history(p, "SchedDestroy", "%d ms", ms);
02271
02272 AST_SCHED_DEL(sched, p->autokillid);
02273 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, p);
02274 }
02275
02276
02277 static int sip_cancel_destroy(struct sip_pvt *p)
02278 {
02279 int res = 0;
02280 if (p->autokillid > -1) {
02281 if (!(res = ast_sched_del(sched, p->autokillid))) {
02282 append_history(p, "CancelDestroy", "");
02283 p->autokillid = -1;
02284 }
02285 }
02286 return res;
02287 }
02288
02289
02290
02291 static int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02292 {
02293 struct sip_pkt *cur, *prev = NULL;
02294
02295
02296 char *msg;
02297 int res = FALSE;
02298
02299 msg = sip_methods[sipmethod].text;
02300
02301 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
02302 if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
02303 ((ast_test_flag(cur, FLAG_RESPONSE)) ||
02304 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
02305 if (!resp && (seqno == p->pendinginvite)) {
02306 if (option_debug)
02307 ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
02308 p->pendinginvite = 0;
02309 }
02310
02311 res = TRUE;
02312 UNLINK(cur, p->packets, prev);
02313 if (cur->retransid > -1) {
02314 if (sipdebug && option_debug > 3)
02315 ast_log(LOG_DEBUG, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
02316 }
02317
02318
02319
02320
02321
02322
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333 AST_SCHED_DEL_SPINLOCK(sched, cur->retransid, &p->lock);
02334 free(cur);
02335 break;
02336 }
02337 }
02338 if (option_debug)
02339 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");
02340 return res;
02341 }
02342
02343
02344
02345 static void __sip_pretend_ack(struct sip_pvt *p)
02346 {
02347 struct sip_pkt *cur = NULL;
02348
02349 while (p->packets) {
02350 int method;
02351 if (cur == p->packets) {
02352 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
02353 return;
02354 }
02355 cur = p->packets;
02356 method = (cur->method) ? cur->method : find_sip_method(cur->data);
02357 __sip_ack(p, cur->seqno, ast_test_flag(cur, FLAG_RESPONSE), method);
02358 }
02359 }
02360
02361
02362 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02363 {
02364 struct sip_pkt *cur;
02365 int res = FALSE;
02366
02367 for (cur = p->packets; cur; cur = cur->next) {
02368 if (cur->seqno == seqno && ast_test_flag(cur, FLAG_RESPONSE) == resp &&
02369 (ast_test_flag(cur, FLAG_RESPONSE) || method_match(sipmethod, cur->data))) {
02370
02371 if (cur->retransid > -1) {
02372 if (option_debug > 3 && sipdebug)
02373 ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
02374 }
02375 AST_SCHED_DEL(sched, cur->retransid);
02376 res = TRUE;
02377 break;
02378 }
02379 }
02380 if (option_debug)
02381 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");
02382 return res;
02383 }
02384
02385
02386
02387 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
02388 {
02389 memset(dst, 0, sizeof(*dst));
02390 memcpy(dst->data, src->data, sizeof(dst->data));
02391 dst->len = src->len;
02392 parse_request(dst);
02393 }
02394
02395
02396 static void add_blank(struct sip_request *req)
02397 {
02398 if (!req->lines) {
02399
02400 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
02401 req->len += strlen(req->data + req->len);
02402 }
02403 }
02404
02405
02406 static int send_provisional_keepalive_full(struct provisional_keepalive_data *data, int with_sdp)
02407 {
02408 const char *msg = NULL;
02409 int res = 0;
02410 struct sip_pvt *pvt = data->pvt;
02411
02412 if (!pvt) {
02413 ao2_ref(data, -1);
02414 return res;
02415 }
02416
02417 ast_mutex_lock(&pvt->lock);
02418 while (pvt->owner && ast_channel_trylock(pvt->owner)) {
02419 ast_mutex_unlock(&pvt->lock);
02420 sched_yield();
02421 if ((pvt = data->pvt)) {
02422 ast_mutex_lock(&pvt->lock);
02423 } else {
02424 ao2_ref(data, -1);
02425 return res;
02426 }
02427 }
02428
02429 if (data->sched_id == -1 || pvt->invitestate >= INV_COMPLETED) {
02430 goto provisional_keepalive_cleanup;
02431 }
02432
02433 if (!pvt->last_provisional || !strncasecmp(pvt->last_provisional, "100", 3)) {
02434 msg = "183 Session Progress";
02435 }
02436
02437 if (with_sdp) {
02438 transmit_response_with_sdp(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq, XMIT_UNRELIABLE);
02439 } else {
02440 transmit_response(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq);
02441 }
02442
02443 res = PROVIS_KEEPALIVE_TIMEOUT;
02444
02445 provisional_keepalive_cleanup:
02446 if (!res) {
02447 data->sched_id = -1;
02448 ao2_ref(data, -1);
02449 }
02450
02451 if (pvt->owner) {
02452 ast_channel_unlock(pvt->owner);
02453 }
02454 ast_mutex_unlock(&pvt->lock);
02455
02456 return res;
02457 }
02458
02459 static int send_provisional_keepalive(const void *data)
02460 {
02461 struct provisional_keepalive_data *d = (struct provisional_keepalive_data *) data;
02462
02463 return send_provisional_keepalive_full(d, 0);
02464 }
02465
02466 static int send_provisional_keepalive_with_sdp(const void *data)
02467 {
02468 struct provisional_keepalive_data *d = (struct provisional_keepalive_data *) data;
02469
02470 return send_provisional_keepalive_full(d, 1);
02471 }
02472
02473 static void *unref_provisional_keepalive(struct provisional_keepalive_data *data)
02474 {
02475 if (data) {
02476 data->sched_id = -1;
02477 data->pvt = NULL;
02478 ao2_ref(data, -1);
02479 }
02480 return NULL;
02481 }
02482
02483 static void remove_provisional_keepalive_sched(struct sip_pvt *pvt)
02484 {
02485 int res;
02486 if (!pvt->provisional_keepalive_data) {
02487 return;
02488 }
02489 res = AST_SCHED_DEL(sched, pvt->provisional_keepalive_data->sched_id);
02490 if (res == -1) {
02491
02492
02493
02494
02495
02496 pvt->provisional_keepalive_data = unref_provisional_keepalive(pvt->provisional_keepalive_data);
02497 } else {
02498
02499
02500 ao2_ref(pvt->provisional_keepalive_data, -1);
02501 }
02502 }
02503
02504 static void update_provisional_keepalive(struct sip_pvt *pvt, int with_sdp)
02505 {
02506 remove_provisional_keepalive_sched(pvt);
02507
02508 if (!pvt->provisional_keepalive_data) {
02509 if (!(pvt->provisional_keepalive_data = ao2_alloc(sizeof(*pvt->provisional_keepalive_data), NULL))) {
02510 return;
02511 }
02512 pvt->provisional_keepalive_data->sched_id = -1;
02513 pvt->provisional_keepalive_data->pvt = pvt;
02514 }
02515
02516
02517 ao2_ref(pvt->provisional_keepalive_data, +1);
02518
02519
02520 pvt->provisional_keepalive_data->sched_id = ast_sched_add(sched,
02521 PROVIS_KEEPALIVE_TIMEOUT,
02522 with_sdp ? send_provisional_keepalive_with_sdp : send_provisional_keepalive,
02523 pvt->provisional_keepalive_data);
02524
02525
02526 if (pvt->provisional_keepalive_data->sched_id == -1) {
02527 ao2_ref(pvt->provisional_keepalive_data, -1);
02528 }
02529 }
02530
02531
02532 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02533 {
02534 int res;
02535
02536 finalize_content(req);
02537 add_blank(req);
02538 if (sip_debug_test_pvt(p)) {
02539 const struct sockaddr_in *dst = sip_real_dst(p);
02540
02541 ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
02542 reliable ? "Reliably " : "", sip_nat_mode(p),
02543 ast_inet_ntoa(dst->sin_addr),
02544 ntohs(dst->sin_port), req->data);
02545 }
02546 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02547 struct sip_request tmp;
02548 parse_copy(&tmp, req);
02549 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"),
02550 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? tmp.rlPart2 : sip_methods[tmp.method].text);
02551 }
02552
02553
02554 if (p->initreq.method == SIP_INVITE && reliable == XMIT_CRITICAL) {
02555 remove_provisional_keepalive_sched(p);
02556 }
02557
02558 res = (reliable) ?
02559 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02560 __sip_xmit(p, req->data, req->len);
02561 if (res > 0)
02562 return 0;
02563 return res;
02564 }
02565
02566
02567 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02568 {
02569 int res;
02570
02571 finalize_content(req);
02572 add_blank(req);
02573 if (sip_debug_test_pvt(p)) {
02574 if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
02575 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);
02576 else
02577 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);
02578 }
02579 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02580 struct sip_request tmp;
02581 parse_copy(&tmp, req);
02582 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
02583 }
02584 res = (reliable) ?
02585 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02586 __sip_xmit(p, req->data, req->len);
02587 return res;
02588 }
02589
02590
02591
02592
02593
02594 static const char *find_closing_quote(const char *start, const char *lim)
02595 {
02596 char last_char = '\0';
02597 const char *s;
02598 for (s = start; *s && s != lim; last_char = *s++) {
02599 if (*s == '"' && last_char != '\\')
02600 break;
02601 }
02602 return s;
02603 }
02604
02605
02606
02607
02608
02609
02610
02611
02612
02613
02614
02615
02616 static char *get_in_brackets(char *tmp)
02617 {
02618 const char *parse = tmp;
02619 char *first_bracket;
02620
02621
02622
02623
02624
02625 while ( (first_bracket = strchr(parse, '<')) ) {
02626 char *first_quote = strchr(parse, '"');
02627
02628 if (!first_quote || first_quote > first_bracket)
02629 break;
02630
02631 parse = find_closing_quote(first_quote + 1, NULL);
02632 if (!*parse) {
02633
02634 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
02635 break;
02636 }
02637 parse++;
02638 }
02639 if (first_bracket) {
02640 char *second_bracket = strchr(first_bracket + 1, '>');
02641 if (second_bracket) {
02642 *second_bracket = '\0';
02643 tmp = first_bracket + 1;
02644 } else {
02645 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
02646 }
02647 }
02648 return tmp;
02649 }
02650
02651
02652
02653 static int sip_sendtext(struct ast_channel *ast, const char *text)
02654 {
02655 struct sip_pvt *p = ast->tech_pvt;
02656 int debug = sip_debug_test_pvt(p);
02657
02658 if (debug)
02659 ast_verbose("Sending text %s on %s\n", text, ast->name);
02660 if (!p)
02661 return -1;
02662
02663
02664 if (!text)
02665 return 0;
02666 if (debug)
02667 ast_verbose("Really sending text %s on %s\n", text, ast->name);
02668 transmit_message_with_text(p, text);
02669 return 0;
02670 }
02671
02672
02673
02674
02675
02676
02677 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey, int lastms)
02678 {
02679 char port[10];
02680 char ipaddr[INET_ADDRSTRLEN];
02681 char regseconds[20];
02682 char str_lastms[20];
02683
02684 char *sysname = ast_config_AST_SYSTEM_NAME;
02685 char *syslabel = NULL;
02686
02687 time_t nowtime = time(NULL) + expirey;
02688 const char *fc = fullcontact ? "fullcontact" : NULL;
02689
02690 snprintf(str_lastms, sizeof(str_lastms), "%d", lastms);
02691 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
02692 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
02693 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
02694
02695 if (ast_strlen_zero(sysname))
02696 sysname = NULL;
02697 else if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME))
02698 syslabel = "regserver";
02699
02700 if (fc)
02701 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02702 "port", port, "regseconds", regseconds,
02703 "username", username, fc, fullcontact, syslabel, sysname, NULL);
02704 else
02705 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02706 "port", port, "regseconds", regseconds,
02707 "username", username, syslabel, sysname, NULL);
02708 if (seen_lastms) {
02709
02710
02711 ast_update_realtime("sippeers", "name", peername, "lastms", str_lastms, NULL);
02712 }
02713 }
02714
02715
02716 static void register_peer_exten(struct sip_peer *peer, int onoff)
02717 {
02718 char multi[256];
02719 char *stringp, *ext, *context;
02720
02721
02722
02723
02724
02725 if (ast_strlen_zero(global_regcontext))
02726 return;
02727
02728 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
02729 stringp = multi;
02730 while ((ext = strsep(&stringp, "&"))) {
02731 if ((context = strchr(ext, '@'))) {
02732 *context++ = '\0';
02733 if (!ast_context_find(context)) {
02734 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
02735 continue;
02736 }
02737 } else {
02738 context = global_regcontext;
02739 }
02740 if (onoff) {
02741 if (!ast_exists_extension(NULL, context, ext, 1, NULL)) {
02742 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
02743 ast_strdup(peer->name), ast_free_ptr, "SIP");
02744 }
02745 } else {
02746 ast_context_remove_extension(context, ext, 1, NULL);
02747 }
02748 }
02749 }
02750
02751
02752 static void sip_destroy_peer(struct sip_peer *peer)
02753 {
02754 if (option_debug > 2)
02755 ast_log(LOG_DEBUG, "Destroying SIP peer %s\n", peer->name);
02756
02757
02758 if (peer->call)
02759 sip_destroy(peer->call);
02760
02761 if (peer->mwipvt)
02762 sip_destroy(peer->mwipvt);
02763
02764 if (peer->chanvars) {
02765 ast_variables_destroy(peer->chanvars);
02766 peer->chanvars = NULL;
02767 }
02768
02769 register_peer_exten(peer, FALSE);
02770 ast_free_ha(peer->ha);
02771 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT))
02772 apeerobjs--;
02773 else if (!ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && ast_test_flag(&peer->flags[0], SIP_REALTIME))
02774 rpeerobjs--;
02775 else
02776 speerobjs--;
02777 clear_realm_authentication(peer->auth);
02778 peer->auth = NULL;
02779 free(peer);
02780 }
02781
02782
02783 static void update_peer(struct sip_peer *p, int expiry)
02784 {
02785 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02786 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) &&
02787 (ast_test_flag(&p->flags[0], SIP_REALTIME) || rtcachefriends)) {
02788 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, expiry, p->lastms);
02789 }
02790 }
02791
02792
02793
02794
02795
02796
02797
02798 static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_in *sin, int devstate_only)
02799 {
02800 struct sip_peer *peer=NULL;
02801 struct ast_variable *var = NULL;
02802 struct ast_config *peerlist = NULL;
02803 struct ast_variable *tmp;
02804 struct ast_flags flags = {0};
02805 const char *iabuf = NULL;
02806 char portstring[6];
02807 const char *insecure;
02808 char *cat = NULL;
02809 unsigned short portnum;
02810
02811
02812 if (newpeername) {
02813 var = ast_load_realtime("sippeers", "name", newpeername, "host", "dynamic", NULL);
02814 if (!var && sin)
02815 var = ast_load_realtime("sippeers", "name", newpeername, "host", ast_inet_ntoa(sin->sin_addr), NULL);
02816 if (!var) {
02817 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
02818
02819
02820
02821
02822
02823
02824 if (var && sin) {
02825 for (tmp = var; tmp; tmp = tmp->next) {
02826 if (!strcasecmp(tmp->name, "host")) {
02827 struct hostent *hp;
02828 struct ast_hostent ahp;
02829 if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
02830
02831 ast_variables_destroy(var);
02832 var = NULL;
02833 }
02834 break;
02835 }
02836 }
02837 }
02838 }
02839 }
02840
02841 if (!var && sin) {
02842 iabuf = ast_inet_ntoa(sin->sin_addr);
02843 portnum = ntohs(sin->sin_port);
02844 sprintf(portstring, "%d", portnum);
02845 var = ast_load_realtime("sippeers", "host", iabuf, "port", portstring, NULL);
02846 if (!var)
02847 var = ast_load_realtime("sippeers", "ipaddr", iabuf, "port", portstring, NULL);
02848 if (!var) {
02849 peerlist = ast_load_realtime_multientry("sippeers", "host", iabuf, NULL);
02850 if(peerlist){
02851 while((cat = ast_category_browse(peerlist, cat)))
02852 {
02853 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02854 set_insecure_flags(&flags, insecure, -1);
02855 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02856 var = ast_category_root(peerlist, cat);
02857 break;
02858 }
02859 }
02860 }
02861 if(!var) {
02862 ast_config_destroy(peerlist);
02863 peerlist = NULL;
02864 cat = NULL;
02865 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", iabuf, NULL);
02866 if(peerlist) {
02867 while((cat = ast_category_browse(peerlist, cat)))
02868 {
02869 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02870 set_insecure_flags(&flags, insecure, -1);
02871 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02872 var = ast_category_root(peerlist, cat);
02873 break;
02874 }
02875 }
02876 }
02877 }
02878 }
02879 }
02880
02881 if (!var) {
02882 if(peerlist)
02883 ast_config_destroy(peerlist);
02884 return NULL;
02885 }
02886
02887 for (tmp = var; tmp; tmp = tmp->next) {
02888
02889 if (!strcasecmp(tmp->name, "type") &&
02890 !strcasecmp(tmp->value, "user")) {
02891 ast_variables_destroy(var);
02892 return NULL;
02893 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
02894 newpeername = tmp->value;
02895 } else if (!strcasecmp(tmp->name, "lastms")) {
02896 seen_lastms = 1;
02897 }
02898 }
02899
02900 if (!newpeername) {
02901 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", iabuf);
02902 if(peerlist)
02903 ast_config_destroy(peerlist);
02904 else
02905 ast_variables_destroy(var);
02906 return NULL;
02907 }
02908
02909
02910 peer = build_peer(newpeername, var, NULL, 1, devstate_only);
02911 if (!peer) {
02912 if(peerlist)
02913 ast_config_destroy(peerlist);
02914 else
02915 ast_variables_destroy(var);
02916 return NULL;
02917 }
02918
02919 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && !devstate_only) {
02920
02921 ast_copy_flags(&peer->flags[1],&global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
02922 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
02923 if (!AST_SCHED_DEL(sched, peer->expire)) {
02924 struct sip_peer *peer_ptr = peer;
02925 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
02926 }
02927 peer->expire = ast_sched_add(sched, (global_rtautoclear) * 1000, expire_register, ASTOBJ_REF(peer));
02928 if (peer->expire == -1) {
02929 struct sip_peer *peer_ptr = peer;
02930 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
02931 }
02932 }
02933 ASTOBJ_CONTAINER_LINK(&peerl,peer);
02934 }
02935 ast_set_flag(&peer->flags[0], SIP_REALTIME);
02936 if(peerlist)
02937 ast_config_destroy(peerlist);
02938 else
02939 ast_variables_destroy(var);
02940 return peer;
02941 }
02942
02943
02944 static int sip_addrcmp(char *name, struct sockaddr_in *sin)
02945 {
02946
02947 struct sip_peer *p = (struct sip_peer *) name;
02948 return !(!inaddrcmp(&p->addr, sin) ||
02949 (ast_test_flag(&p->flags[0], SIP_INSECURE_PORT) &&
02950 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
02951 }
02952
02953
02954
02955
02956 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int devstate_only)
02957 {
02958 struct sip_peer *p = NULL;
02959
02960 if (peer)
02961 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
02962 else
02963 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, name, sip_addr_hashfunc, 1, sip_addrcmp);
02964
02965 if (!p && (realtime || devstate_only))
02966 p = realtime_peer(peer, sin, devstate_only);
02967
02968 return p;
02969 }
02970
02971
02972 static void sip_destroy_user(struct sip_user *user)
02973 {
02974 if (option_debug > 2)
02975 ast_log(LOG_DEBUG, "Destroying user object from memory: %s\n", user->name);
02976 ast_free_ha(user->ha);
02977 if (user->chanvars) {
02978 ast_variables_destroy(user->chanvars);
02979 user->chanvars = NULL;
02980 }
02981 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && ast_test_flag(&user->flags[0], SIP_REALTIME))
02982 ruserobjs--;
02983 else
02984 suserobjs--;
02985 free(user);
02986 }
02987
02988
02989
02990
02991 static struct sip_user *realtime_user(const char *username)
02992 {
02993 struct ast_variable *var;
02994 struct ast_variable *tmp;
02995 struct sip_user *user = NULL;
02996
02997 var = ast_load_realtime("sipusers", "name", username, NULL);
02998
02999 if (!var)
03000 return NULL;
03001
03002 for (tmp = var; tmp; tmp = tmp->next) {
03003 if (!strcasecmp(tmp->name, "type") &&
03004 !strcasecmp(tmp->value, "peer")) {
03005 ast_variables_destroy(var);
03006 return NULL;
03007 }
03008 }
03009
03010 user = build_user(username, var, NULL, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
03011
03012 if (!user) {
03013 ast_variables_destroy(var);
03014 return NULL;
03015 }
03016
03017 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
03018 ast_set_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
03019 suserobjs++;
03020 ASTOBJ_CONTAINER_LINK(&userl,user);
03021 } else {
03022
03023 suserobjs--;
03024 ruserobjs++;
03025 }
03026 ast_set_flag(&user->flags[0], SIP_REALTIME);
03027 ast_variables_destroy(var);
03028 return user;
03029 }
03030
03031
03032
03033
03034
03035 static struct sip_user *find_user(const char *name, int realtime)
03036 {
03037 struct sip_user *u = ASTOBJ_CONTAINER_FIND(&userl, name);
03038 if (!u && realtime)
03039 u = realtime_user(name);
03040 return u;
03041 }
03042
03043
03044 static void do_setnat(struct sip_pvt *p, int natflags)
03045 {
03046 const char *mode = natflags ? "On" : "Off";
03047
03048 if (p->rtp) {
03049 if (option_debug)
03050 ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", mode);
03051 ast_rtp_setnat(p->rtp, natflags);
03052 }
03053 if (p->vrtp) {
03054 if (option_debug)
03055 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", mode);
03056 ast_rtp_setnat(p->vrtp, natflags);
03057 }
03058 if (p->udptl) {
03059 if (option_debug)
03060 ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", mode);
03061 ast_udptl_setnat(p->udptl, natflags);
03062 }
03063 }
03064
03065
03066
03067
03068 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
03069 {
03070 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
03071 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
03072 dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
03073 dialog->recv = dialog->sa;
03074 } else
03075 return -1;
03076
03077 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
03078 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
03079 dialog->capability = peer->capability;
03080 if ((!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(dialog->capability & AST_FORMAT_VIDEO_MASK)) && dialog->vrtp) {
03081 ast_rtp_destroy(dialog->vrtp);
03082 dialog->vrtp = NULL;
03083 }
03084 dialog->prefs = peer->prefs;
03085 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
03086 dialog->t38.capability = global_t38_capability;
03087 if (dialog->udptl) {
03088 if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_FEC )
03089 dialog->t38.capability |= T38FAX_UDP_EC_FEC;
03090 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY )
03091 dialog->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
03092 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_NONE )
03093 dialog->t38.capability |= T38FAX_UDP_EC_NONE;
03094 dialog->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
03095 if (option_debug > 1)
03096 ast_log(LOG_DEBUG,"Our T38 capability (%d)\n", dialog->t38.capability);
03097 }
03098 dialog->t38.jointcapability = dialog->t38.capability;
03099 } else if (dialog->udptl) {
03100 ast_udptl_destroy(dialog->udptl);
03101 dialog->udptl = NULL;
03102 }
03103 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE );
03104
03105 if (dialog->rtp) {
03106 ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
03107 ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
03108 ast_rtp_set_rtptimeout(dialog->rtp, peer->rtptimeout);
03109 ast_rtp_set_rtpholdtimeout(dialog->rtp, peer->rtpholdtimeout);
03110 ast_rtp_set_rtpkeepalive(dialog->rtp, peer->rtpkeepalive);
03111
03112 ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
03113 dialog->autoframing = peer->autoframing;
03114 }
03115 if (dialog->vrtp) {
03116 ast_rtp_setdtmf(dialog->vrtp, 0);
03117 ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
03118 ast_rtp_set_rtptimeout(dialog->vrtp, peer->rtptimeout);
03119 ast_rtp_set_rtpholdtimeout(dialog->vrtp, peer->rtpholdtimeout);
03120 ast_rtp_set_rtpkeepalive(dialog->vrtp, peer->rtpkeepalive);
03121 }
03122
03123 ast_string_field_set(dialog, peername, peer->name);
03124 ast_string_field_set(dialog, authname, peer->username);
03125 ast_string_field_set(dialog, username, peer->username);
03126 ast_string_field_set(dialog, peersecret, peer->secret);
03127 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
03128 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
03129 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
03130 ast_string_field_set(dialog, tohost, peer->tohost);
03131 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
03132 if (!dialog->initreq.headers && !ast_strlen_zero(peer->fromdomain)) {
03133 char *tmpcall;
03134 char *c;
03135 tmpcall = ast_strdupa(dialog->callid);
03136 c = strchr(tmpcall, '@');
03137 if (c) {
03138 *c = '\0';
03139 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
03140 }
03141 }
03142 if (ast_strlen_zero(dialog->tohost))
03143 ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
03144 if (!ast_strlen_zero(peer->fromdomain))
03145 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
03146 if (!ast_strlen_zero(peer->fromuser))
03147 ast_string_field_set(dialog, fromuser, peer->fromuser);
03148 if (!ast_strlen_zero(peer->language))
03149 ast_string_field_set(dialog, language, peer->language);
03150 dialog->maxtime = peer->maxms;
03151 dialog->callgroup = peer->callgroup;
03152 dialog->pickupgroup = peer->pickupgroup;
03153 dialog->peerauth = peer->auth;
03154 dialog->allowtransfer = peer->allowtransfer;
03155
03156
03157 if (peer->maxms && peer->lastms)
03158 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
03159 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
03160 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
03161 dialog->noncodeccapability |= AST_RTP_DTMF;
03162 else
03163 dialog->noncodeccapability &= ~AST_RTP_DTMF;
03164 dialog->jointnoncodeccapability = dialog->noncodeccapability;
03165 ast_string_field_set(dialog, context, peer->context);
03166 dialog->rtptimeout = peer->rtptimeout;
03167 if (peer->call_limit)
03168 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
03169 dialog->maxcallbitrate = peer->maxcallbitrate;
03170 if (!dialog->portinuri)
03171 dialog->portinuri = peer->portinuri;
03172
03173 return 0;
03174 }
03175
03176
03177
03178
03179 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct sockaddr_in *sin)
03180 {
03181 struct hostent *hp;
03182 struct ast_hostent ahp;
03183 struct sip_peer *p;
03184 char *port;
03185 int portno = 0;
03186 char host[MAXHOSTNAMELEN], *hostn;
03187 char peer[256];
03188
03189 ast_copy_string(peer, opeer, sizeof(peer));
03190 port = strchr(peer, ':');
03191 if (port) {
03192 *port++ = '\0';
03193 dialog->portinuri = 1;
03194 }
03195 dialog->sa.sin_family = AF_INET;
03196 dialog->timer_t1 = 500;
03197 p = find_peer(peer, NULL, 1, 0);
03198
03199 if (p) {
03200 int res = create_addr_from_peer(dialog, p);
03201 if (port) {
03202 portno = atoi(port);
03203 dialog->sa.sin_port = dialog->recv.sin_port = htons(portno);
03204 }
03205 ASTOBJ_UNREF(p, sip_destroy_peer);
03206 return res;
03207 }
03208
03209 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
03210
03211 ast_string_field_set(dialog, tohost, peer);
03212
03213 if (sin) {
03214 memcpy(&dialog->sa.sin_addr, &sin->sin_addr, sizeof(dialog->sa.sin_addr));
03215 if (!sin->sin_port) {
03216 if (ast_strlen_zero(port) || sscanf(port, "%30u", &portno) != 1) {
03217 portno = STANDARD_SIP_PORT;
03218 }
03219 } else {
03220 portno = ntohs(sin->sin_port);
03221 }
03222 } else {
03223 hostn = peer;
03224
03225
03226
03227 if (!port && srvlookup) {
03228 char service[MAXHOSTNAMELEN];
03229 int tportno;
03230 int ret;
03231
03232 snprintf(service, sizeof(service), "_sip._udp.%s", peer);
03233 ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
03234 if (ret > 0) {
03235 hostn = host;
03236 portno = tportno;
03237 }
03238 }
03239 if (!portno)
03240 portno = port ? atoi(port) : STANDARD_SIP_PORT;
03241
03242 hp = ast_gethostbyname(hostn, &ahp);
03243 if (!hp) {
03244 ast_log(LOG_WARNING, "No such host: %s\n", peer);
03245 return -1;
03246 }
03247 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
03248 }
03249 dialog->sa.sin_port = htons(portno);
03250 dialog->recv = dialog->sa;
03251 return 0;
03252 }
03253
03254
03255 static int auto_congest(const void *nothing)
03256 {
03257 struct sip_pvt *p = (struct sip_pvt *)nothing;
03258
03259 ast_mutex_lock(&p->lock);
03260 p->initid = -1;
03261 if (p->owner) {
03262
03263 if (!ast_channel_trylock(p->owner)) {
03264 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
03265 append_history(p, "Cong", "Auto-congesting (timer)");
03266 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
03267 ast_channel_unlock(p->owner);
03268 }
03269
03270
03271 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03272 }
03273 ast_mutex_unlock(&p->lock);
03274 return 0;
03275 }
03276
03277
03278
03279
03280 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
03281 {
03282 int res, xmitres = 0;
03283 struct sip_pvt *p;
03284 struct varshead *headp;
03285 struct ast_var_t *current;
03286 const char *referer = NULL;
03287
03288 p = ast->tech_pvt;
03289 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
03290 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
03291 return -1;
03292 }
03293
03294
03295 headp=&ast->varshead;
03296 AST_LIST_TRAVERSE(headp,current,entries) {
03297
03298 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
03299 p->options->vxml_url = ast_var_value(current);
03300 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
03301 p->options->uri_options = ast_var_value(current);
03302 } else if (!p->options->distinctive_ring && !strcasecmp(ast_var_name(current), "ALERT_INFO")) {
03303
03304 p->options->distinctive_ring = ast_var_value(current);
03305 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
03306
03307 p->options->addsipheaders = 1;
03308 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
03309
03310 p->options->transfer = 1;
03311 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
03312
03313 referer = ast_var_value(current);
03314 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
03315
03316 p->options->replaces = ast_var_value(current);
03317 }
03318 }
03319
03320 res = 0;
03321 ast_set_flag(&p->flags[0], SIP_OUTGOING);
03322
03323 if (p->options->transfer) {
03324 char buf[SIPBUFSIZE/2];
03325
03326 if (referer) {
03327 if (sipdebug && option_debug > 2)
03328 ast_log(LOG_DEBUG, "Call for %s transfered by %s\n", p->username, referer);
03329 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
03330 } else
03331 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
03332 ast_string_field_set(p, cid_name, buf);
03333 }
03334 if (option_debug)
03335 ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
03336
03337 res = update_call_counter(p, INC_CALL_RINGING);
03338 if ( res != -1 ) {
03339 p->callingpres = ast->cid.cid_pres;
03340 p->jointcapability = ast_translate_available_formats(p->capability, p->prefcodec);
03341 p->jointnoncodeccapability = p->noncodeccapability;
03342
03343
03344 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
03345 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
03346 res = -1;
03347 } else {
03348 p->t38.jointcapability = p->t38.capability;
03349 if (option_debug > 1)
03350 ast_log(LOG_DEBUG,"Our T38 capability (%d), joint T38 capability (%d)\n", p->t38.capability, p->t38.jointcapability);
03351 xmitres = transmit_invite(p, SIP_INVITE, 1, 2);
03352 if (xmitres == XMIT_ERROR)
03353 return -1;
03354
03355 p->invitestate = INV_CALLING;
03356
03357
03358 AST_SCHED_DEL(sched, p->initid);
03359 p->initid = ast_sched_add(sched, p->maxtime ? (p->maxtime * 4) : SIP_TRANS_TIMEOUT, auto_congest, p);
03360 }
03361 } else {
03362 ast->hangupcause = AST_CAUSE_USER_BUSY;
03363 }
03364 return res;
03365 }
03366
03367
03368
03369 static void sip_registry_destroy(struct sip_registry *reg)
03370 {
03371
03372 if (option_debug > 2)
03373 ast_log(LOG_DEBUG, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
03374
03375 if (reg->call) {
03376
03377
03378 reg->call->registry = NULL;
03379 if (option_debug > 2)
03380 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
03381 sip_destroy(reg->call);
03382 }
03383 AST_SCHED_DEL(sched, reg->expire);
03384 AST_SCHED_DEL(sched, reg->timeout);
03385 ast_string_field_free_memory(reg);
03386 regobjs--;
03387 free(reg);
03388
03389 }
03390
03391
03392 static int __sip_destroy(struct sip_pvt *p, int lockowner)
03393 {
03394 struct sip_pvt *cur, *prev = NULL;
03395 struct sip_pkt *cp;
03396 struct sip_request *req;
03397
03398
03399 if (p->rtp && ast_rtp_get_bridged(p->rtp)) {
03400 ast_verbose("Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03401 return -1;
03402 }
03403
03404 if (p->vrtp && ast_rtp_get_bridged(p->vrtp)) {
03405 ast_verbose("Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03406 return -1;
03407 }
03408
03409 if (sip_debug_test_pvt(p) || option_debug > 2)
03410 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03411
03412 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03413 update_call_counter(p, DEC_CALL_LIMIT);
03414 if (option_debug > 1)
03415 ast_log(LOG_DEBUG, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
03416 }
03417
03418
03419 if (p->owner) {
03420 if (lockowner)
03421 ast_channel_lock(p->owner);
03422 if (option_debug)
03423 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
03424 p->owner->tech_pvt = NULL;
03425
03426 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03427 if (lockowner)
03428 ast_channel_unlock(p->owner);
03429
03430 usleep(1);
03431 }
03432
03433
03434 if (p->relatedpeer) {
03435 if (p->relatedpeer->mwipvt == p) {
03436 p->relatedpeer->mwipvt = NULL;
03437 }
03438 ASTOBJ_UNREF(p->relatedpeer, sip_destroy_peer);
03439 }
03440
03441 if (dumphistory)
03442 sip_dump_history(p);
03443
03444 if (p->options)
03445 free(p->options);
03446
03447 if (p->stateid > -1)
03448 ast_extension_state_del(p->stateid, NULL);
03449
03450
03451
03452 clear_extenstate_updates(p);
03453
03454 AST_SCHED_DEL(sched, p->initid);
03455 AST_SCHED_DEL(sched, p->waitid);
03456 AST_SCHED_DEL(sched, p->autokillid);
03457 AST_SCHED_DEL(sched, p->request_queue_sched_id);
03458
03459 remove_provisional_keepalive_sched(p);
03460 if (p->provisional_keepalive_data) {
03461 ast_mutex_lock(&p->lock);
03462 p->provisional_keepalive_data = unref_provisional_keepalive(p->provisional_keepalive_data);
03463 ast_mutex_unlock(&p->lock);
03464 }
03465
03466 if (p->rtp) {
03467 ast_rtp_destroy(p->rtp);
03468 }
03469 if (p->vrtp) {
03470 ast_rtp_destroy(p->vrtp);
03471 }
03472 if (p->udptl)
03473 ast_udptl_destroy(p->udptl);
03474 if (p->refer)
03475 free(p->refer);
03476 if (p->route) {
03477 free_old_route(p->route);
03478 p->route = NULL;
03479 }
03480 if (p->registry) {
03481 if (p->registry->call == p)
03482 p->registry->call = NULL;
03483 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
03484 }
03485
03486
03487 if (p->history) {
03488 struct sip_history *hist;
03489 while ( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) ) {
03490 free(hist);
03491 p->history_entries--;
03492 }
03493 free(p->history);
03494 p->history = NULL;
03495 }
03496
03497 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
03498 ast_free(req);
03499 }
03500
03501 for (prev = NULL, cur = iflist; cur; prev = cur, cur = cur->next) {
03502 if (cur == p) {
03503 UNLINK(cur, iflist, prev);
03504 break;
03505 }
03506 }
03507 if (!cur) {
03508 ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
03509 return 0;
03510 }
03511
03512
03513 while((cp = p->packets)) {
03514 p->packets = p->packets->next;
03515 AST_SCHED_DEL(sched, cp->retransid);
03516 free(cp);
03517 }
03518 if (p->chanvars) {
03519 ast_variables_destroy(p->chanvars);
03520 p->chanvars = NULL;
03521 }
03522 ast_mutex_destroy(&p->lock);
03523
03524 ast_string_field_free_memory(p);
03525
03526 free(p);
03527 return 0;
03528 }
03529
03530
03531
03532
03533
03534
03535
03536
03537
03538
03539
03540
03541
03542
03543
03544 static int update_call_counter(struct sip_pvt *fup, int event)
03545 {
03546 char name[256];
03547 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
03548 int outgoing = ast_test_flag(&fup->flags[1], SIP_PAGE2_OUTGOING_CALL);
03549 struct sip_user *u = NULL;
03550 struct sip_peer *p = NULL;
03551
03552 if (option_debug > 2)
03553 ast_log(LOG_DEBUG, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
03554
03555
03556
03557 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
03558 return 0;
03559
03560 ast_copy_string(name, fup->username, sizeof(name));
03561
03562
03563 if (global_limitonpeers == FALSE && !outgoing && (u = find_user(name, 1))) {
03564 inuse = &u->inUse;
03565 call_limit = &u->call_limit;
03566 inringing = NULL;
03567 } else if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, 1, 0) ) ) {
03568 inuse = &p->inUse;
03569 call_limit = &p->call_limit;
03570 inringing = &p->inRinging;
03571 ast_copy_string(name, fup->peername, sizeof(name));
03572 }
03573 if (!p && !u) {
03574 if (option_debug > 1)
03575 ast_log(LOG_DEBUG, "%s is not a local device, no call limit\n", name);
03576 return 0;
03577 }
03578
03579 switch(event) {
03580
03581 case DEC_CALL_LIMIT:
03582 if ( *inuse > 0 ) {
03583 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
03584 (*inuse)--;
03585 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
03586 }
03587 } else {
03588 *inuse = 0;
03589 }
03590 if (inringing) {
03591 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03592 if (*inringing > 0)
03593 (*inringing)--;
03594 else if (!ast_test_flag(&p->flags[0], SIP_REALTIME) || ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03595 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", fup->peername);
03596 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03597 }
03598 }
03599 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold) {
03600 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
03601 sip_peer_hold(fup, 0);
03602 }
03603 if (option_debug > 1 || sipdebug) {
03604 ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
03605 }
03606 break;
03607
03608 case INC_CALL_RINGING:
03609 case INC_CALL_LIMIT:
03610 if (*call_limit > 0 ) {
03611
03612 if (outgoing && (*inuse >= *call_limit)) {
03613 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);
03614 if (u)
03615 ASTOBJ_UNREF(u, sip_destroy_user);
03616 else
03617 ASTOBJ_UNREF(p, sip_destroy_peer);
03618 return -1;
03619 }
03620 }
03621 if (inringing && (event == INC_CALL_RINGING)) {
03622 if (!ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03623 (*inringing)++;
03624 ast_set_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03625 }
03626 }
03627
03628 (*inuse)++;
03629 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
03630 if (option_debug > 1 || sipdebug) {
03631 ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
03632 }
03633 break;
03634
03635 case DEC_CALL_RINGING:
03636 if (inringing) {
03637 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03638 if (*inringing > 0)
03639 (*inringing)--;
03640 else if (!ast_test_flag(&p->flags[0], SIP_REALTIME) || ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03641 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", p->name);
03642 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03643 }
03644 }
03645 break;
03646
03647 default:
03648 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
03649 }
03650 if (p) {
03651 ast_device_state_changed("SIP/%s", p->name);
03652 ASTOBJ_UNREF(p, sip_destroy_peer);
03653 } else
03654 ASTOBJ_UNREF(u, sip_destroy_user);
03655 return 0;
03656 }
03657
03658
03659 static void sip_destroy(struct sip_pvt *p)
03660 {
03661 ast_mutex_lock(&iflock);
03662 if (option_debug > 2)
03663 ast_log(LOG_DEBUG, "Destroying SIP dialog %s\n", p->callid);
03664 __sip_destroy(p, 1);
03665 ast_mutex_unlock(&iflock);
03666 }
03667
03668
03669 static int hangup_sip2cause(int cause)
03670 {
03671
03672
03673 switch(cause) {
03674 case 401:
03675 return AST_CAUSE_CALL_REJECTED;
03676 case 403:
03677 return AST_CAUSE_CALL_REJECTED;
03678 case 404:
03679 return AST_CAUSE_UNALLOCATED;
03680 case 405:
03681 return AST_CAUSE_INTERWORKING;
03682 case 407:
03683 return AST_CAUSE_CALL_REJECTED;
03684 case 408:
03685 return AST_CAUSE_NO_USER_RESPONSE;
03686 case 409:
03687 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
03688 case 410:
03689 return AST_CAUSE_NUMBER_CHANGED;
03690 case 411:
03691 return AST_CAUSE_INTERWORKING;
03692 case 413:
03693 return AST_CAUSE_INTERWORKING;
03694 case 414:
03695 return AST_CAUSE_INTERWORKING;
03696 case 415:
03697 return AST_CAUSE_INTERWORKING;
03698 case 420:
03699 return AST_CAUSE_NO_ROUTE_DESTINATION;
03700 case 480:
03701 return AST_CAUSE_NO_ANSWER;
03702 case 481:
03703 return AST_CAUSE_INTERWORKING;
03704 case 482:
03705 return AST_CAUSE_INTERWORKING;
03706 case 483:
03707 return AST_CAUSE_NO_ANSWER;
03708 case 484:
03709 return AST_CAUSE_INVALID_NUMBER_FORMAT;
03710 case 485:
03711 return AST_CAUSE_UNALLOCATED;
03712 case 486:
03713 return AST_CAUSE_BUSY;
03714 case 487:
03715 return AST_CAUSE_INTERWORKING;
03716 case 488:
03717 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03718 case 491:
03719 return AST_CAUSE_INTERWORKING;
03720 case 493:
03721 return AST_CAUSE_INTERWORKING;
03722 case 500:
03723 return AST_CAUSE_FAILURE;
03724 case 501:
03725 return AST_CAUSE_FACILITY_REJECTED;
03726 case 502:
03727 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
03728 case 503:
03729 return AST_CAUSE_CONGESTION;
03730 case 504:
03731 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
03732 case 505:
03733 return AST_CAUSE_INTERWORKING;
03734 case 600:
03735 return AST_CAUSE_USER_BUSY;
03736 case 603:
03737 return AST_CAUSE_CALL_REJECTED;
03738 case 604:
03739 return AST_CAUSE_UNALLOCATED;
03740 case 606:
03741 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03742 default:
03743 return AST_CAUSE_NORMAL;
03744 }
03745
03746 return 0;
03747 }
03748
03749
03750
03751
03752
03753
03754
03755
03756
03757
03758
03759
03760
03761
03762
03763
03764
03765
03766
03767
03768
03769
03770
03771
03772
03773
03774
03775
03776
03777
03778
03779
03780
03781 static const char *hangup_cause2sip(int cause)
03782 {
03783 switch (cause) {
03784 case AST_CAUSE_UNALLOCATED:
03785 case AST_CAUSE_NO_ROUTE_DESTINATION:
03786 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
03787 return "404 Not Found";
03788 case AST_CAUSE_CONGESTION:
03789 case AST_CAUSE_SWITCH_CONGESTION:
03790 return "503 Service Unavailable";
03791 case AST_CAUSE_NO_USER_RESPONSE:
03792 return "408 Request Timeout";
03793 case AST_CAUSE_NO_ANSWER:
03794 case AST_CAUSE_UNREGISTERED:
03795 return "480 Temporarily unavailable";
03796 case AST_CAUSE_CALL_REJECTED:
03797 return "403 Forbidden";
03798 case AST_CAUSE_NUMBER_CHANGED:
03799 return "410 Gone";
03800 case AST_CAUSE_NORMAL_UNSPECIFIED:
03801 return "480 Temporarily unavailable";
03802 case AST_CAUSE_INVALID_NUMBER_FORMAT:
03803 return "484 Address incomplete";
03804 case AST_CAUSE_USER_BUSY:
03805 return "486 Busy here";
03806 case AST_CAUSE_FAILURE:
03807 return "500 Server internal failure";
03808 case AST_CAUSE_FACILITY_REJECTED:
03809 return "501 Not Implemented";
03810 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
03811 return "503 Service Unavailable";
03812
03813 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
03814 return "502 Bad Gateway";
03815 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
03816 return "488 Not Acceptable Here";
03817
03818 case AST_CAUSE_NOTDEFINED:
03819 default:
03820 if (option_debug)
03821 ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
03822 return NULL;
03823 }
03824
03825
03826 return 0;
03827 }
03828
03829
03830
03831
03832 static int sip_hangup(struct ast_channel *ast)
03833 {
03834 struct sip_pvt *p = ast->tech_pvt;
03835 int needcancel = FALSE;
03836 int needdestroy = 0;
03837 struct ast_channel *oldowner = ast;
03838
03839 if (!p) {
03840 if (option_debug)
03841 ast_log(LOG_DEBUG, "Asked to hangup channel that was not connected\n");
03842 return 0;
03843 }
03844
03845
03846 if (p->owner)
03847 p->hangupcause = p->owner->hangupcause;
03848
03849 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
03850 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03851 if (option_debug && sipdebug)
03852 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03853 update_call_counter(p, DEC_CALL_LIMIT);
03854 }
03855 if (option_debug >3)
03856 ast_log(LOG_DEBUG, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
03857 if (p->autokillid > -1 && sip_cancel_destroy(p))
03858 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03859 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03860 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
03861 ast_clear_flag(&p->flags[0], SIP_NEEDDESTROY);
03862 p->owner->tech_pvt = NULL;
03863 p->owner = NULL;
03864 ast_module_unref(ast_module_info->self);
03865 return 0;
03866 }
03867 if (option_debug) {
03868 if (ast_test_flag(ast, AST_FLAG_ZOMBIE) && p->refer && option_debug)
03869 ast_log(LOG_DEBUG, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
03870 else {
03871 if (option_debug)
03872 ast_log(LOG_DEBUG, "Hangup call %s, SIP callid %s)\n", ast->name, p->callid);
03873 }
03874 }
03875 if (option_debug && ast_test_flag(ast, AST_FLAG_ZOMBIE))
03876 ast_log(LOG_DEBUG, "Hanging up zombie call. Be scared.\n");
03877
03878 ast_mutex_lock(&p->lock);
03879 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03880 if (option_debug && sipdebug)
03881 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03882 update_call_counter(p, DEC_CALL_LIMIT);
03883 }
03884
03885
03886 if (p->owner != ast) {
03887 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
03888 ast_mutex_unlock(&p->lock);
03889 return 0;
03890 }
03891
03892 if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING || (p->invitestate < INV_COMPLETED && ast->_state != AST_STATE_UP)) {
03893 needcancel = TRUE;
03894 if (option_debug > 3)
03895 ast_log(LOG_DEBUG, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
03896 }
03897
03898 stop_media_flows(p);
03899
03900 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", p->owner ? ast_cause2str(p->hangupcause) : "Unknown");
03901
03902
03903 if (p->vad)
03904 ast_dsp_free(p->vad);
03905
03906 p->owner = NULL;
03907 ast->tech_pvt = NULL;
03908
03909 ast_module_unref(ast_module_info->self);
03910
03911
03912
03913
03914
03915
03916
03917 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE))
03918 needdestroy = 1;
03919 else if (p->invitestate != INV_CALLING)
03920 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03921
03922
03923 if (!ast_test_flag(&p->flags[0], SIP_ALREADYGONE) && !ast_strlen_zero(p->initreq.data)) {
03924 if (needcancel) {
03925 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03926
03927 struct sip_pkt *cur;
03928
03929
03930 if (p->invitestate == INV_CALLING) {
03931
03932 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03933
03934 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03935 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
03936 } else {
03937 for (cur = p->packets; cur; cur = cur->next) {
03938 __sip_semi_ack(p, cur->seqno, ast_test_flag(cur, FLAG_RESPONSE), cur->method ? cur->method : find_sip_method(cur->data));
03939 }
03940 p->invitestate = INV_CANCELLED;
03941
03942 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
03943
03944
03945 needdestroy = 0;
03946 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03947 }
03948 } else {
03949 const char *res;
03950 remove_provisional_keepalive_sched(p);
03951 if (p->hangupcause && (res = hangup_cause2sip(p->hangupcause)))
03952 transmit_response_reliable(p, res, &p->initreq);
03953 else
03954 transmit_response_reliable(p, "603 Declined", &p->initreq);
03955 p->invitestate = INV_TERMINATED;
03956 }
03957 } else {
03958 if (!p->pendinginvite) {
03959 char *audioqos = "";
03960 char *videoqos = "";
03961 if (p->rtp)
03962 audioqos = ast_rtp_get_quality(p->rtp, NULL);
03963 if (p->vrtp)
03964 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
03965
03966 if (oldowner->_state == AST_STATE_UP) {
03967 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03968 }
03969
03970
03971 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
03972 if (p->rtp)
03973 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
03974 if (p->vrtp)
03975 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
03976 }
03977 if (p->rtp && oldowner)
03978 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", audioqos);
03979 if (p->vrtp && oldowner)
03980 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", videoqos);
03981 } else {
03982
03983
03984 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03985 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
03986 AST_SCHED_DEL(sched, p->waitid);
03987 if (sip_cancel_destroy(p))
03988 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03989 }
03990 }
03991 }
03992 if (needdestroy)
03993 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
03994 ast_mutex_unlock(&p->lock);
03995 return 0;
03996 }
03997
03998
03999 static void try_suggested_sip_codec(struct sip_pvt *p)
04000 {
04001 int fmt;
04002 const char *codec;
04003
04004 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
04005 if (!codec)
04006 return;
04007
04008 fmt = ast_getformatbyname(codec);
04009 if (fmt) {
04010 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
04011 if (p->jointcapability & fmt) {
04012 p->jointcapability &= fmt;
04013 p->capability &= fmt;
04014 } else
04015 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
04016 } else
04017 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
04018 return;
04019 }
04020
04021
04022
04023 static int sip_answer(struct ast_channel *ast)
04024 {
04025 int res = 0;
04026 struct sip_pvt *p = ast->tech_pvt;
04027
04028 ast_mutex_lock(&p->lock);
04029 if (ast->_state != AST_STATE_UP) {
04030 try_suggested_sip_codec(p);
04031
04032 ast_setstate(ast, AST_STATE_UP);
04033 if (option_debug)
04034 ast_log(LOG_DEBUG, "SIP answering channel: %s\n", ast->name);
04035
04036 ast_rtp_new_source(p->rtp);
04037 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
04038 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
04039 }
04040 ast_mutex_unlock(&p->lock);
04041 return res;
04042 }
04043
04044
04045 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
04046 {
04047 struct sip_pvt *p = ast->tech_pvt;
04048 int res = 0;
04049
04050 switch (frame->frametype) {
04051 case AST_FRAME_VOICE:
04052 if (!(frame->subclass & ast->nativeformats)) {
04053 char s1[512], s2[512], s3[512];
04054 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %s(%d) read/write = %s(%d)/%s(%d)\n",
04055 frame->subclass,
04056 ast_getformatname_multiple(s1, sizeof(s1) - 1, ast->nativeformats & AST_FORMAT_AUDIO_MASK),
04057 ast->nativeformats & AST_FORMAT_AUDIO_MASK,
04058 ast_getformatname_multiple(s2, sizeof(s2) - 1, ast->readformat),
04059 ast->readformat,
04060 ast_getformatname_multiple(s3, sizeof(s3) - 1, ast->writeformat),
04061 ast->writeformat);
04062 return 0;
04063 }
04064 if (p) {
04065 ast_mutex_lock(&p->lock);
04066 if (p->t38.state == T38_ENABLED && !p->t38.direct) {
04067
04068 ast_mutex_unlock(&p->lock);
04069 break;
04070 } else if (p->rtp) {
04071
04072 if ((ast->_state != AST_STATE_UP) &&
04073 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
04074 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
04075 ast_rtp_new_source(p->rtp);
04076 if (!global_prematuremediafilter) {
04077 p->invitestate = INV_EARLY_MEDIA;
04078 transmit_provisional_response(p, "183 Session Progress", &p->initreq, 1);
04079 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
04080 }
04081 }
04082 p->lastrtptx = time(NULL);
04083 res = ast_rtp_write(p->rtp, frame);
04084 }
04085 ast_mutex_unlock(&p->lock);
04086 }
04087 break;
04088 case AST_FRAME_VIDEO:
04089 if (p) {
04090 ast_mutex_lock(&p->lock);
04091 if (p->vrtp) {
04092
04093 if ((ast->_state != AST_STATE_UP) &&
04094 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
04095 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
04096 p->invitestate = INV_EARLY_MEDIA;
04097 transmit_provisional_response(p, "183 Session Progress", &p->initreq, 1);
04098 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
04099 }
04100 p->lastrtptx = time(NULL);
04101 res = ast_rtp_write(p->vrtp, frame);
04102 }
04103 ast_mutex_unlock(&p->lock);
04104 }
04105 break;
04106 case AST_FRAME_IMAGE:
04107 return 0;
04108 break;
04109 case AST_FRAME_MODEM:
04110 if (p) {
04111 ast_mutex_lock(&p->lock);
04112
04113
04114
04115
04116 if (ast->_state == AST_STATE_UP) {
04117 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && p->t38.state == T38_DISABLED) {
04118 if (!p->pendinginvite) {
04119 p->t38.state = T38_LOCAL_REINVITE;
04120 transmit_reinvite_with_t38_sdp(p);
04121 }
04122 } else if (p->t38.state == T38_ENABLED) {
04123 res = ast_udptl_write(p->udptl, frame);
04124 }
04125 }
04126 ast_mutex_unlock(&p->lock);
04127 }
04128 break;
04129 default:
04130 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
04131 return 0;
04132 }
04133
04134 return res;
04135 }
04136
04137
04138
04139 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
04140 {
04141 int ret = -1;
04142 struct sip_pvt *p;
04143
04144 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE) && option_debug)
04145 ast_log(LOG_DEBUG, "New channel is zombie\n");
04146 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE) && option_debug)
04147 ast_log(LOG_DEBUG, "Old channel is zombie\n");
04148
04149 if (!newchan || !newchan->tech_pvt) {
04150 if (!newchan)
04151 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
04152 else
04153 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
04154 return -1;
04155 }
04156 p = newchan->tech_pvt;
04157
04158 if (!p) {
04159 ast_log(LOG_WARNING, "No pvt after masquerade. Strange things may happen\n");
04160 return -1;
04161 }
04162
04163 ast_mutex_lock(&p->lock);
04164 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
04165 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
04166 if (p->owner != oldchan)
04167 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
04168 else {
04169 p->owner = newchan;
04170
04171
04172
04173
04174
04175
04176 sip_set_rtp_peer(newchan, NULL, NULL, 0, 0);
04177 ret = 0;
04178 }
04179 if (option_debug > 2)
04180 ast_log(LOG_DEBUG, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
04181
04182 ast_mutex_unlock(&p->lock);
04183 return ret;
04184 }
04185
04186 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
04187 {
04188 struct sip_pvt *p = ast->tech_pvt;
04189 int res = 0;
04190
04191 ast_mutex_lock(&p->lock);
04192 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
04193 case SIP_DTMF_INBAND:
04194 res = -1;
04195 break;
04196 case SIP_DTMF_RFC2833:
04197 if (p->rtp)
04198 ast_rtp_senddigit_begin(p->rtp, digit);
04199 break;
04200 default:
04201 break;
04202 }
04203 ast_mutex_unlock(&p->lock);
04204
04205 return res;
04206 }
04207
04208
04209
04210 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
04211 {
04212 struct sip_pvt *p = ast->tech_pvt;
04213 int res = 0;
04214
04215 ast_mutex_lock(&p->lock);
04216 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
04217 case SIP_DTMF_INFO:
04218 transmit_info_with_digit(p, digit, duration);
04219 break;
04220 case SIP_DTMF_RFC2833:
04221 if (p->rtp)
04222 ast_rtp_senddigit_end_with_duration(p->rtp, digit, duration);
04223 break;
04224 case SIP_DTMF_INBAND:
04225 res = -1;
04226 break;
04227 }
04228 ast_mutex_unlock(&p->lock);
04229
04230 return res;
04231 }
04232
04233
04234 static int sip_transfer(struct ast_channel *ast, const char *dest)
04235 {
04236 struct sip_pvt *p = ast->tech_pvt;
04237 int res;
04238
04239 if (dest == NULL)
04240 dest = "";
04241 ast_mutex_lock(&p->lock);
04242 if (ast->_state == AST_STATE_RING)
04243 res = sip_sipredirect(p, dest);
04244 else
04245 res = transmit_refer(p, dest);
04246 ast_mutex_unlock(&p->lock);
04247 return res;
04248 }
04249
04250
04251
04252
04253
04254
04255 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
04256 {
04257 struct sip_pvt *p = ast->tech_pvt;
04258 int res = 0;
04259
04260 ast_mutex_lock(&p->lock);
04261 switch(condition) {
04262 case AST_CONTROL_RINGING:
04263 if (ast->_state == AST_STATE_RING) {
04264 p->invitestate = INV_EARLY_MEDIA;
04265 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
04266 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
04267
04268 transmit_provisional_response(p, "180 Ringing", &p->initreq, 0);
04269 ast_set_flag(&p->flags[0], SIP_RINGING);
04270 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
04271 break;
04272 } else {
04273
04274 }
04275 }
04276 res = -1;
04277 break;
04278 case AST_CONTROL_BUSY:
04279 if (ast->_state != AST_STATE_UP) {
04280 transmit_response_reliable(p, "486 Busy Here", &p->initreq);
04281 p->invitestate = INV_COMPLETED;
04282 sip_alreadygone(p);
04283 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
04284 break;
04285 }
04286 res = -1;
04287 break;
04288 case AST_CONTROL_CONGESTION:
04289 if (ast->_state != AST_STATE_UP) {
04290 transmit_response_reliable(p, "503 Service Unavailable", &p->initreq);
04291 p->invitestate = INV_COMPLETED;
04292 sip_alreadygone(p);
04293 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
04294 break;
04295 }
04296 res = -1;
04297 break;
04298 case AST_CONTROL_PROCEEDING:
04299 if ((ast->_state != AST_STATE_UP) &&
04300 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
04301 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
04302 transmit_response(p, "100 Trying", &p->initreq);
04303 p->invitestate = INV_PROCEEDING;
04304 break;
04305 }
04306 res = -1;
04307 break;
04308 case AST_CONTROL_PROGRESS:
04309 if ((ast->_state != AST_STATE_UP) &&
04310 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
04311 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
04312 p->invitestate = INV_EARLY_MEDIA;
04313 transmit_provisional_response(p, "183 Session Progress", &p->initreq, 1);
04314 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
04315 break;
04316 }
04317 res = -1;
04318 break;
04319 case AST_CONTROL_HOLD:
04320 ast_rtp_new_source(p->rtp);
04321 ast_moh_start(ast, data, p->mohinterpret);
04322 break;
04323 case AST_CONTROL_UNHOLD:
04324 ast_rtp_new_source(p->rtp);
04325 ast_moh_stop(ast);
04326 break;
04327 case AST_CONTROL_VIDUPDATE:
04328 if (p->vrtp && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
04329 transmit_info_with_vidupdate(p);
04330
04331 } else
04332 res = -1;
04333 break;
04334 case AST_CONTROL_SRCUPDATE:
04335 ast_rtp_new_source(p->rtp);
04336 break;
04337 case AST_CONTROL_SRCCHANGE:
04338 ast_rtp_change_source(p->rtp);
04339 break;
04340 case -1:
04341 res = -1;
04342 break;
04343 default:
04344 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
04345 res = -1;
04346 break;
04347 }
04348 ast_mutex_unlock(&p->lock);
04349 return res;
04350 }
04351
04352
04353
04354
04355
04356
04357
04358 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
04359 {
04360 struct ast_channel *tmp;
04361 struct ast_variable *v = NULL;
04362 int fmt;
04363 int what;
04364 int needvideo = 0, video = 0;
04365 char *decoded_exten;
04366
04367 if (option_debug != 0) {
04368 ast_verbose(VERBOSE_PREFIX_3 "NEW SIP CHANNEL, title: <%s>\n", title?title:"Null");
04369 ast_verbose(VERBOSE_PREFIX_3 "from: %s\n", i->from);
04370 ast_verbose(VERBOSE_PREFIX_3 "username: <%s>\n", i->username);
04371 ast_verbose(VERBOSE_PREFIX_3 "peername: <%s>\n", i->peername);
04372 ast_verbose(VERBOSE_PREFIX_3 "fromdomain: %s\n", i->fromdomain);
04373 ast_verbose(VERBOSE_PREFIX_3 "fromuser: %s\n", i->fromuser);
04374 ast_verbose(VERBOSE_PREFIX_3 "fromname: %s\n", i->fromname);
04375 ast_verbose(VERBOSE_PREFIX_3 "fullcontact: %s\n", i->fullcontact);
04376 }
04377
04378 {
04379 char my_name[128];
04380 const char *f, *fromdomain = NULL;
04381
04382 if (!ast_strlen_zero(i->fromdomain) && strchr(i->fromdomain,':'))
04383 fromdomain = strchr(i->fromdomain,':') + 1;
04384 else
04385 fromdomain = i->fromdomain;
04386
04387 if (!ast_strlen_zero(i->username)) {
04388 if (!ast_strlen_zero(title) && strcmp(i->username, title)) {
04389
04390 snprintf(my_name, sizeof(my_name), "%s@%s", i->username, title);
04391 } else {
04392
04393 snprintf(my_name, sizeof(my_name), "%s", i->username);
04394 }
04395 } else {
04396 if (!ast_strlen_zero(i->peername)) {
04397
04398 snprintf(my_name, sizeof(my_name), "%s", i->peername);
04399 } else {
04400 if (!ast_strlen_zero(title)) {
04401 snprintf(my_name, sizeof(my_name), "%s", title);
04402 } else if (!ast_strlen_zero(i->from)) {
04403 f = i->from;
04404 if (!strncmp(f, "sip:", 4))
04405 f += 4;
04406 snprintf(my_name, sizeof(my_name), "%s", f);
04407 } else {
04408 snprintf(my_name, sizeof(my_name), "%s", fromdomain);
04409 }
04410 }
04411 }
04412 ast_mutex_unlock(&i->lock);
04413
04414 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));
04415
04416 }
04417 if (!tmp) {
04418 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
04419 ast_mutex_lock(&i->lock);
04420 return NULL;
04421 }
04422 ast_mutex_lock(&i->lock);
04423
04424 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO)
04425 tmp->tech = &sip_tech_info;
04426 else
04427 tmp->tech = &sip_tech;
04428
04429
04430
04431 if (i->jointcapability) {
04432 what = i->jointcapability;
04433 video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
04434 } else if (i->capability) {
04435 what = i->capability;
04436 video = i->capability & AST_FORMAT_VIDEO_MASK;
04437 } else {
04438 what = global_capability;
04439 video = global_capability & AST_FORMAT_VIDEO_MASK;
04440 }
04441
04442
04443 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video;
04444 if (option_debug > 2) {
04445 char buf[SIPBUFSIZE];
04446 ast_log(LOG_DEBUG, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
04447 ast_log(LOG_DEBUG, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
04448 ast_log(LOG_DEBUG, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
04449 ast_log(LOG_DEBUG, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
04450 if (i->prefcodec)
04451 ast_log(LOG_DEBUG, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
04452 }
04453
04454
04455 fmt = ast_best_codec(tmp->nativeformats);
04456
04457
04458
04459
04460
04461 if (i->vrtp) {
04462 if (i->prefcodec)
04463 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
04464 else
04465 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
04466 }
04467
04468 if (option_debug > 2) {
04469 if (needvideo)
04470 ast_log(LOG_DEBUG, "This channel can handle video! HOLLYWOOD next!\n");
04471 else
04472 ast_log(LOG_DEBUG, "This channel will not be able to handle video.\n");
04473 }
04474
04475
04476
04477 if ((ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) || (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
04478 i->vad = ast_dsp_new();
04479 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
04480 if (global_relaxdtmf)
04481 ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
04482 }
04483 if (i->rtp) {
04484 tmp->fds[0] = ast_rtp_fd(i->rtp);
04485 tmp->fds[1] = ast_rtcp_fd(i->rtp);
04486 }
04487 if (needvideo && i->vrtp) {
04488 tmp->fds[2] = ast_rtp_fd(i->vrtp);
04489 tmp->fds[3] = ast_rtcp_fd(i->vrtp);
04490 }
04491 if (i->udptl) {
04492 tmp->fds[5] = ast_udptl_fd(i->udptl);
04493 }
04494 if (state == AST_STATE_RING)
04495 tmp->rings = 1;
04496 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
04497 tmp->writeformat = fmt;
04498 tmp->rawwriteformat = fmt;
04499 tmp->readformat = fmt;
04500 tmp->rawreadformat = fmt;
04501 tmp->tech_pvt = i;
04502
04503 tmp->callgroup = i->callgroup;
04504 tmp->pickupgroup = i->pickupgroup;
04505 tmp->cid.cid_pres = i->callingpres;
04506 if (!ast_strlen_zero(i->accountcode))
04507 ast_string_field_set(tmp, accountcode, i->accountcode);
04508 if (i->amaflags)
04509 tmp->amaflags = i->amaflags;
04510 if (!ast_strlen_zero(i->language))
04511 ast_string_field_set(tmp, language, i->language);
04512 i->owner = tmp;
04513 ast_module_ref(ast_module_info->self);
04514 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
04515
04516
04517
04518
04519 if (ast_exists_extension(NULL, i->context, i->exten, 1, i->cid_num)) {
04520
04521 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
04522 } else {
04523 decoded_exten = ast_strdupa(i->exten);
04524 ast_uri_decode(decoded_exten);
04525 ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
04526 }
04527
04528
04529
04530 tmp->cid.cid_ani = ast_strdup(i->cid_num);
04531 if (!ast_strlen_zero(i->rdnis))
04532 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
04533
04534 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
04535 tmp->cid.cid_dnid = ast_strdup(i->exten);
04536
04537 tmp->priority = 1;
04538 if (!ast_strlen_zero(i->uri))
04539 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
04540 if (!ast_strlen_zero(i->domain))
04541 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
04542 if (!ast_strlen_zero(i->useragent))
04543 pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
04544 if (!ast_strlen_zero(i->callid))
04545 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
04546 if (i->rtp)
04547 ast_jb_configure(tmp, &global_jbconf);
04548
04549
04550 for (v = i->chanvars ; v ; v = v->next)
04551 pbx_builtin_setvar_helper(tmp, v->name, v->value);
04552
04553 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
04554 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
04555 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
04556 ast_hangup(tmp);
04557 tmp = NULL;
04558 }
04559
04560 if (!ast_test_flag(&i->flags[0], SIP_NO_HISTORY))
04561 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
04562
04563 return tmp;
04564 }
04565
04566
04567 static char *get_body_by_line(const char *line, const char *name, int nameLen)
04568 {
04569 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=')
04570 return ast_skip_blanks(line + nameLen + 1);
04571
04572 return "";
04573 }
04574
04575
04576
04577
04578
04579 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
04580 {
04581 int len = strlen(name);
04582
04583 while (*start < (req->sdp_start + req->sdp_count)) {
04584 const char *r = get_body_by_line(req->line[(*start)++], name, len);
04585 if (r[0] != '\0')
04586 return r;
04587 }
04588
04589
04590 (*start)++;
04591
04592 return "";
04593 }
04594
04595
04596
04597
04598
04599
04600 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value)
04601 {
04602 char type = '\0';
04603 const char *line = NULL;
04604
04605 if (stop > (req->sdp_start + req->sdp_count)) {
04606 stop = req->sdp_start + req->sdp_count;
04607 }
04608
04609 while (*start < stop) {
04610 line = req->line[(*start)++];
04611 if (line[1] == '=') {
04612 type = line[0];
04613 *value = ast_skip_blanks(line + 2);
04614 break;
04615 }
04616 }
04617
04618 return type;
04619 }
04620
04621
04622 static char *get_body(struct sip_request *req, char *name)
04623 {
04624 int x;
04625 int len = strlen(name);
04626 char *r;
04627
04628 for (x = 0; x < req->lines; x++) {
04629 r = get_body_by_line(req->line[x], name, len);
04630 if (r[0] != '\0')
04631 return r;
04632 }
04633
04634 return "";
04635 }
04636
04637
04638 static const char *find_alias(const char *name, const char *_default)
04639 {
04640
04641 static const struct cfalias {
04642 char * const fullname;
04643 char * const shortname;
04644 } aliases[] = {
04645 { "Content-Type", "c" },
04646 { "Content-Encoding", "e" },
04647 { "From", "f" },
04648 { "Call-ID", "i" },
04649 { "Contact", "m" },
04650 { "Content-Length", "l" },
04651 { "Subject", "s" },
04652 { "To", "t" },
04653 { "Supported", "k" },
04654 { "Refer-To", "r" },
04655 { "Referred-By", "b" },
04656 { "Allow-Events", "u" },
04657 { "Event", "o" },
04658 { "Via", "v" },
04659 { "Accept-Contact", "a" },
04660 { "Reject-Contact", "j" },
04661 { "Request-Disposition", "d" },
04662 { "Session-Expires", "x" },
04663 { "Identity", "y" },
04664 { "Identity-Info", "n" },
04665 };
04666 int x;
04667
04668 for (x=0; x<sizeof(aliases) / sizeof(aliases[0]); x++)
04669 if (!strcasecmp(aliases[x].fullname, name))
04670 return aliases[x].shortname;
04671
04672 return _default;
04673 }
04674
04675 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
04676 {
04677 int pass;
04678
04679
04680
04681
04682
04683
04684
04685
04686
04687
04688 for (pass = 0; name && pass < 2;pass++) {
04689 int x, len = strlen(name);
04690 for (x=*start; x<req->headers; x++) {
04691 if (!strncasecmp(req->header[x], name, len)) {
04692 char *r = req->header[x] + len;
04693 if (pedanticsipchecking)
04694 r = ast_skip_blanks(r);
04695
04696 if (*r == ':') {
04697 *start = x+1;
04698 return ast_skip_blanks(r+1);
04699 }
04700 }
04701 }
04702 if (pass == 0)
04703 name = find_alias(name, NULL);
04704 }
04705
04706
04707 return "";
04708 }
04709
04710
04711 static const char *get_header(const struct sip_request *req, const char *name)
04712 {
04713 int start = 0;
04714 return __get_header(req, name, &start);
04715 }
04716
04717
04718 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
04719 {
04720
04721 struct ast_frame *f;
04722
04723 if (!p->rtp) {
04724
04725 return &ast_null_frame;
04726 }
04727
04728 switch(ast->fdno) {
04729 case 0:
04730 f = ast_rtp_read(p->rtp);
04731 break;
04732 case 1:
04733 f = ast_rtcp_read(p->rtp);
04734 break;
04735 case 2:
04736 f = ast_rtp_read(p->vrtp);
04737 break;
04738 case 3:
04739 f = ast_rtcp_read(p->vrtp);
04740 break;
04741 case 5:
04742 f = ast_udptl_read(p->udptl);
04743 break;
04744 default:
04745 f = &ast_null_frame;
04746 }
04747
04748 if (f && (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) &&
04749 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833)) {
04750 ast_log(LOG_DEBUG,"Ignoring DTMF (%c) RTP frame because dtmfmode is not RFC2833\n", f->subclass);
04751 return &ast_null_frame;
04752 }
04753
04754
04755 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
04756 return f;
04757
04758 if (f && f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
04759 if (!(f->subclass & p->jointcapability)) {
04760 if (option_debug) {
04761 ast_log(LOG_DEBUG, "Bogus frame of format '%s' received from '%s'!\n",
04762 ast_getformatname(f->subclass), p->owner->name);
04763 }
04764 return &ast_null_frame;
04765 }
04766 if (option_debug)
04767 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
04768 p->owner->nativeformats = (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
04769 ast_set_read_format(p->owner, p->owner->readformat);
04770 ast_set_write_format(p->owner, p->owner->writeformat);
04771 }
04772
04773 if (f && (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
04774 f = ast_dsp_process(p->owner, p->vad, f);
04775 if (f && f->frametype == AST_FRAME_DTMF) {
04776 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && f->subclass == 'f') {
04777 if (option_debug)
04778 ast_log(LOG_DEBUG, "Fax CNG detected on %s\n", ast->name);
04779 *faxdetect = 1;
04780 } else if (option_debug) {
04781 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
04782 }
04783 }
04784 }
04785
04786 return f;
04787 }
04788
04789
04790 static struct ast_frame *sip_read(struct ast_channel *ast)
04791 {
04792 struct ast_frame *fr;
04793 struct sip_pvt *p = ast->tech_pvt;
04794 int faxdetected = FALSE;
04795
04796 ast_mutex_lock(&p->lock);
04797 fr = sip_rtp_read(ast, p, &faxdetected);
04798 p->lastrtprx = time(NULL);
04799
04800
04801
04802 if (faxdetected && ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && (p->t38.state == T38_DISABLED) && !(ast_bridged_channel(ast))) {
04803 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
04804 if (!p->pendinginvite) {
04805 if (option_debug > 2)
04806 ast_log(LOG_DEBUG, "Sending reinvite on SIP (%s) for T.38 negotiation.\n",ast->name);
04807 p->t38.state = T38_LOCAL_REINVITE;
04808 transmit_reinvite_with_t38_sdp(p);
04809 if (option_debug > 1)
04810 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, ast->name);
04811 }
04812 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04813 if (option_debug > 2)
04814 ast_log(LOG_DEBUG, "Deferring reinvite on SIP (%s) - it will be re-negotiated for T.38\n", ast->name);
04815 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04816 }
04817 }
04818
04819
04820 if (fr && fr->frametype == AST_FRAME_VOICE && p->invitestate != INV_EARLY_MEDIA && ast->_state != AST_STATE_UP) {
04821 fr = &ast_null_frame;
04822 }
04823
04824 ast_mutex_unlock(&p->lock);
04825 return fr;
04826 }
04827
04828
04829
04830 static char *generate_random_string(char *buf, size_t size)
04831 {
04832 long val[4];
04833 int x;
04834
04835 for (x=0; x<4; x++)
04836 val[x] = ast_random();
04837 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
04838
04839 return buf;
04840 }
04841
04842
04843 static void build_callid_pvt(struct sip_pvt *pvt)
04844 {
04845 char buf[33];
04846
04847 const char *host = S_OR(pvt->fromdomain, ast_inet_ntoa(pvt->ourip));
04848
04849 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04850
04851 }
04852
04853
04854 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain)
04855 {
04856 char buf[33];
04857
04858 const char *host = S_OR(fromdomain, ast_inet_ntoa(ourip));
04859
04860 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04861 }
04862
04863
04864 static void make_our_tag(char *tagbuf, size_t len)
04865 {
04866 snprintf(tagbuf, len, "as%08lx", ast_random());
04867 }
04868
04869
04870 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
04871 int useglobal_nat, const int intended_method)
04872 {
04873 struct sip_pvt *p;
04874
04875 if (!(p = ast_calloc(1, sizeof(*p))))
04876 return NULL;
04877
04878 if (ast_string_field_init(p, 512)) {
04879 free(p);
04880 return NULL;
04881 }
04882
04883 ast_mutex_init(&p->lock);
04884
04885 p->method = intended_method;
04886 p->initid = -1;
04887 p->waitid = -1;
04888 p->autokillid = -1;
04889 p->request_queue_sched_id = -1;
04890 p->subscribed = NONE;
04891 p->stateid = -1;
04892 p->prefs = default_prefs;
04893
04894 if (intended_method != SIP_OPTIONS)
04895 p->timer_t1 = 500;
04896
04897 if (sin) {
04898 p->sa = *sin;
04899 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
04900 p->ourip = __ourip;
04901 } else
04902 p->ourip = __ourip;
04903
04904
04905 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
04906 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
04907
04908 ast_set2_flag(&p->flags[0], !recordhistory, SIP_NO_HISTORY);
04909
04910 p->branch = ast_random();
04911 make_our_tag(p->tag, sizeof(p->tag));
04912 p->ocseq = INITIAL_CSEQ;
04913
04914 if (sip_methods[intended_method].need_rtp) {
04915 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04916
04917 if (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT))
04918 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04919 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT))
04920 p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
04921 if (!p->rtp || (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) && !p->vrtp)) {
04922 ast_log(LOG_WARNING, "Unable to create RTP audio %s session: %s\n",
04923 ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "and video" : "", strerror(errno));
04924
04925 if (p->rtp) {
04926 ast_rtp_destroy(p->rtp);
04927 }
04928 if (p->udptl) {
04929 ast_udptl_destroy(p->udptl);
04930 }
04931 ast_mutex_destroy(&p->lock);
04932 if (p->chanvars) {
04933 ast_variables_destroy(p->chanvars);
04934 p->chanvars = NULL;
04935 }
04936 ast_string_field_free_memory(p);
04937 free(p);
04938 return NULL;
04939 }
04940 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04941 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04942 ast_rtp_settos(p->rtp, global_tos_audio);
04943 ast_rtp_set_rtptimeout(p->rtp, global_rtptimeout);
04944 ast_rtp_set_rtpholdtimeout(p->rtp, global_rtpholdtimeout);
04945 ast_rtp_set_rtpkeepalive(p->rtp, global_rtpkeepalive);
04946 if (p->vrtp) {
04947 ast_rtp_settos(p->vrtp, global_tos_video);
04948 ast_rtp_setdtmf(p->vrtp, 0);
04949 ast_rtp_setdtmfcompensate(p->vrtp, 0);
04950 ast_rtp_set_rtptimeout(p->vrtp, global_rtptimeout);
04951 ast_rtp_set_rtpholdtimeout(p->vrtp, global_rtpholdtimeout);
04952 ast_rtp_set_rtpkeepalive(p->vrtp, global_rtpkeepalive);
04953 }
04954 if (p->udptl)
04955 ast_udptl_settos(p->udptl, global_tos_audio);
04956 p->maxcallbitrate = default_maxcallbitrate;
04957 p->autoframing = global_autoframing;
04958 ast_rtp_codec_setpref(p->rtp, &p->prefs);
04959 }
04960
04961 if (useglobal_nat && sin) {
04962
04963 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
04964 p->recv = *sin;
04965 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04966 }
04967
04968 if (p->method != SIP_REGISTER)
04969 ast_string_field_set(p, fromdomain, default_fromdomain);
04970 build_via(p);
04971 if (!callid)
04972 build_callid_pvt(p);
04973 else
04974 ast_string_field_set(p, callid, callid);
04975
04976 ast_string_field_set(p, mohinterpret, default_mohinterpret);
04977 ast_string_field_set(p, mohsuggest, default_mohsuggest);
04978 p->capability = global_capability;
04979 p->allowtransfer = global_allowtransfer;
04980 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
04981 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
04982 p->noncodeccapability |= AST_RTP_DTMF;
04983 if (p->udptl) {
04984 p->t38.capability = global_t38_capability;
04985 if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY)
04986 p->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
04987 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_FEC)
04988 p->t38.capability |= T38FAX_UDP_EC_FEC;
04989 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_NONE)
04990 p->t38.capability |= T38FAX_UDP_EC_NONE;
04991 p->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
04992 p->t38.jointcapability = p->t38.capability;
04993 }
04994 ast_string_field_set(p, context, default_context);
04995
04996 AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
04997
04998
04999 ast_mutex_lock(&iflock);
05000 p->next = iflist;
05001 iflist = p;
05002 ast_mutex_unlock(&iflock);
05003 if (option_debug)
05004 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");
05005 return p;
05006 }
05007
05008 static void free_via(struct sip_via *v)
05009 {
05010 if (!v) {
05011 return;
05012 }
05013
05014 ast_free(v->via);
05015 ast_free(v);
05016 }
05017
05018
05019
05020
05021
05022
05023
05024
05025
05026
05027
05028
05029
05030
05031
05032
05033
05034
05035
05036
05037
05038
05039
05040
05041
05042
05043
05044
05045
05046
05047
05048
05049
05050
05051 static struct sip_via *parse_via(const char *header)
05052 {
05053 struct sip_via *v = ast_calloc(1, sizeof(*v));
05054 char *via, *parm;
05055
05056 if (!v) {
05057 return NULL;
05058 }
05059
05060 v->via = ast_strdup(header);
05061 v->ttl = 1;
05062
05063 via = v->via;
05064
05065 if (ast_strlen_zero(via)) {
05066 ast_log(LOG_ERROR, "received request without a Via header\n");
05067 free_via(v);
05068 return NULL;
05069 }
05070
05071
05072 via = strsep(&via, ",");
05073
05074
05075 v->protocol = strsep(&via, " \t\r\n");
05076 if (ast_strlen_zero(v->protocol)) {
05077 ast_log(LOG_ERROR, "missing sent-protocol in Via header\n");
05078 free_via(v);
05079 return NULL;
05080 }
05081 v->protocol = ast_skip_blanks(v->protocol);
05082
05083 if (via) {
05084 via = ast_skip_blanks(via);
05085 }
05086
05087
05088 v->sent_by = strsep(&via, "; \t\r\n");
05089 if (ast_strlen_zero(v->sent_by)) {
05090 ast_log(LOG_ERROR, "missing sent-by in Via header\n");
05091 free_via(v);
05092 return NULL;
05093 }
05094 v->sent_by = ast_skip_blanks(v->sent_by);
05095
05096
05097 if ((parm = strchr(v->sent_by, ':'))) {
05098 char *endptr;
05099
05100 v->port = strtol(++parm, &endptr, 10);
05101 }
05102
05103
05104 while ((parm = strsep(&via, "; \t\r\n"))) {
05105 char *c;
05106 if ((c = strstr(parm, "maddr="))) {
05107 v->maddr = ast_skip_blanks(c + sizeof("maddr=") - 1);
05108 } else if ((c = strstr(parm, "branch="))) {
05109 v->branch = ast_skip_blanks(c + sizeof("branch=") - 1);
05110 } else if ((c = strstr(parm, "ttl="))) {
05111 char *endptr;
05112 c = ast_skip_blanks(c + sizeof("ttl=") - 1);
05113 v->ttl = strtol(c, &endptr, 10);
05114
05115
05116 if (c == endptr) {
05117 v->ttl = 1;
05118 }
05119 }
05120 }
05121
05122 return v;
05123 }
05124
05125
05126
05127
05128
05129
05130
05131
05132 static int addr_is_multicast(struct in_addr *addr)
05133 {
05134 return ((ntohl(addr->s_addr) & 0xf0000000) == 0xe0000000);
05135 }
05136
05137
05138
05139
05140
05141
05142
05143
05144
05145
05146
05147
05148
05149
05150
05151
05152 static int process_via(struct sip_pvt *p, const struct sip_request *req)
05153 {
05154 struct sip_via *via = parse_via(get_header(req, "Via"));
05155
05156 if (!via) {
05157 ast_log(LOG_ERROR, "error processing via header\n");
05158 return -1;
05159 }
05160
05161 if (via->maddr) {
05162 struct hostent *hp;
05163 struct ast_hostent ahp;
05164
05165 hp = ast_gethostbyname(via->maddr, &ahp);
05166 if (hp == NULL) {
05167 ast_log(LOG_WARNING, "Can't find address for maddr '%s'\n", via->maddr);
05168 ast_log(LOG_ERROR, "error processing via header\n");
05169 free_via(via);
05170 return -1;
05171 }
05172
05173 p->sa.sin_family = AF_INET;
05174 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
05175
05176 if (addr_is_multicast(&p->sa.sin_addr)) {
05177 setsockopt(sipsock, IPPROTO_IP, IP_MULTICAST_TTL, &via->ttl, sizeof(via->ttl));
05178 }
05179 }
05180
05181 p->sa.sin_port = htons(via->port ? via->port : STANDARD_SIP_PORT);
05182
05183 free_via(via);
05184 return 0;
05185 }
05186
05187
05188
05189 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
05190 {
05191 struct sip_pvt *p = NULL;
05192 char *tag = "";
05193 char totag[128];
05194 char fromtag[128];
05195 const char *callid = get_header(req, "Call-ID");
05196 const char *from = get_header(req, "From");
05197 const char *to = get_header(req, "To");
05198 const char *cseq = get_header(req, "Cseq");
05199
05200
05201
05202 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
05203 ast_strlen_zero(from) || ast_strlen_zero(cseq))
05204 return NULL;
05205
05206 if (pedanticsipchecking) {
05207
05208
05209
05210
05211
05212
05213 if (gettag(req, "To", totag, sizeof(totag)))
05214 ast_set_flag(req, SIP_PKT_WITH_TOTAG);
05215 gettag(req, "From", fromtag, sizeof(fromtag));
05216
05217 tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
05218
05219 if (option_debug > 4 )
05220 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);
05221 }
05222
05223 ast_mutex_lock(&iflock);
05224 for (p = iflist; p; p = p->next) {
05225
05226 int found = FALSE;
05227 if (ast_strlen_zero(p->callid))
05228 continue;
05229 if (req->method == SIP_REGISTER)
05230 found = (!strcmp(p->callid, callid));
05231 else {
05232 found = !strcmp(p->callid, callid);
05233 if (pedanticsipchecking && found) {
05234 found = ast_strlen_zero(tag) || ast_strlen_zero(p->theirtag) || !ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED) || !strcmp(p->theirtag, tag);
05235 }
05236 }
05237
05238 if (option_debug > 4)
05239 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);
05240
05241
05242 if (pedanticsipchecking && found && req->method != SIP_RESPONSE) {
05243 if (p->tag[0] == '\0' && totag[0]) {
05244
05245 found = FALSE;
05246 } else if (totag[0]) {
05247 if (strcmp(totag, p->tag)) {
05248 found = FALSE;
05249 }
05250 }
05251 if (!found && option_debug > 4)
05252 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);
05253 }
05254 if (found) {
05255
05256 ast_mutex_lock(&p->lock);
05257 ast_mutex_unlock(&iflock);
05258 return p;
05259 }
05260 }
05261 ast_mutex_unlock(&iflock);
05262
05263
05264 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
05265 if (intended_method == SIP_REFER) {
05266
05267 transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
05268 } else if (intended_method == SIP_NOTIFY) {
05269
05270
05271 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 No subscription");
05272 } else {
05273
05274 if ((p = sip_alloc(callid, sin, 1, intended_method))) {
05275
05276 ast_mutex_lock(&p->lock);
05277 } else {
05278
05279
05280
05281
05282
05283
05284
05285
05286 transmit_response_using_temp(callid, sin, 1, intended_method, req, "500 Server internal error");
05287 if (option_debug > 3)
05288 ast_log(LOG_DEBUG, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
05289 }
05290 }
05291 return p;
05292 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
05293
05294 transmit_response_using_temp(callid, sin, 1, intended_method, req, "501 Method Not Implemented");
05295 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
05296
05297
05298
05299 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 Call leg/transaction does not exist");
05300 }
05301
05302
05303
05304 return p;
05305 }
05306
05307
05308 static int sip_register(char *value, int lineno)
05309 {
05310 struct sip_registry *reg;
05311 int portnum = 0;
05312 char username[256] = "";
05313 char *hostname=NULL, *secret=NULL, *authuser=NULL;
05314 char *porta=NULL;
05315 char *contact=NULL;
05316
05317 if (!value)
05318 return -1;
05319 ast_copy_string(username, value, sizeof(username));
05320
05321 hostname = strrchr(username, '@');
05322 if (hostname)
05323 *hostname++ = '\0';
05324 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
05325 ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
05326 return -1;
05327 }
05328
05329 secret = strchr(username, ':');
05330 if (secret) {
05331 *secret++ = '\0';
05332 authuser = strchr(secret, ':');
05333 if (authuser)
05334 *authuser++ = '\0';
05335 }
05336
05337 contact = strchr(hostname, '/');
05338 if (contact)
05339 *contact++ = '\0';
05340 if (ast_strlen_zero(contact))
05341 contact = "s";
05342 porta = strchr(hostname, ':');
05343 if (porta) {
05344 *porta++ = '\0';
05345 portnum = atoi(porta);
05346 if (portnum == 0) {
05347 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
05348 return -1;
05349 }
05350 }
05351 if (!(reg = ast_calloc(1, sizeof(*reg)))) {
05352 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
05353 return -1;
05354 }
05355
05356 if (ast_string_field_init(reg, 256)) {
05357 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry strings\n");
05358 free(reg);
05359 return -1;
05360 }
05361
05362 regobjs++;
05363 ASTOBJ_INIT(reg);
05364 ast_string_field_set(reg, contact, contact);
05365 if (!ast_strlen_zero(username))
05366 ast_string_field_set(reg, username, username);
05367 if (hostname)
05368 ast_string_field_set(reg, hostname, hostname);
05369 if (authuser)
05370 ast_string_field_set(reg, authuser, authuser);
05371 if (secret)
05372 ast_string_field_set(reg, secret, secret);
05373 reg->expire = -1;
05374 reg->timeout = -1;
05375 reg->refresh = default_expiry;
05376 reg->portno = portnum;
05377 reg->callid_valid = FALSE;
05378 reg->ocseq = INITIAL_CSEQ;
05379 reg->needdns = TRUE;
05380 ASTOBJ_CONTAINER_LINK(®l, reg);
05381 ASTOBJ_UNREF(reg,sip_registry_destroy);
05382 return 0;
05383 }
05384
05385
05386
05387 static int lws2sws(char *msgbuf, int len)
05388 {
05389 int h = 0, t = 0;
05390 int lws = 0;
05391
05392 for (; h < len;) {
05393
05394 if (msgbuf[h] == '\r') {
05395 h++;
05396 continue;
05397 }
05398
05399 if (msgbuf[h] == '\n') {
05400
05401 if (h + 1 == len)
05402 break;
05403
05404 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
05405
05406 h++;
05407 continue;
05408 }
05409
05410 msgbuf[t++] = msgbuf[h++];
05411 lws = 0;
05412 continue;
05413 }
05414 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
05415 if (lws) {
05416 h++;
05417 continue;
05418 }
05419 msgbuf[t++] = msgbuf[h++];
05420 lws = 1;
05421 continue;
05422 }
05423 msgbuf[t++] = msgbuf[h++];
05424 if (lws)
05425 lws = 0;
05426 }
05427 msgbuf[t] = '\0';
05428 return t;
05429 }
05430
05431
05432
05433
05434 static int parse_request(struct sip_request *req)
05435 {
05436
05437 char *c;
05438 int f = 0;
05439
05440 c = req->data;
05441
05442
05443 req->header[f] = c;
05444 while(*c) {
05445 if (*c == '\n') {
05446
05447 *c = 0;
05448
05449 if (sipdebug && option_debug > 3)
05450 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
05451 if (ast_strlen_zero(req->header[f])) {
05452
05453 c++;
05454 break;
05455 }
05456 if (f >= SIP_MAX_HEADERS - 1) {
05457 ast_log(LOG_WARNING, "Too many SIP headers. Ignoring.\n");
05458 } else {
05459 f++;
05460 req->header[f] = c + 1;
05461 }
05462 } else if (*c == '\r') {
05463
05464 *c = 0;
05465 }
05466 c++;
05467 }
05468
05469 req->headers = f;
05470
05471
05472 if (!ast_strlen_zero(req->header[f])) {
05473 if (sipdebug && option_debug > 3)
05474 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
05475 req->headers++;
05476 }
05477
05478
05479 f = 0;
05480 req->line[f] = c;
05481 while (*c) {
05482 if (*c == '\n') {
05483
05484 *c = 0;
05485 if (sipdebug && option_debug > 3)
05486 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
05487 if (f == SIP_MAX_LINES - 1) {
05488 ast_log(LOG_WARNING, "Too many SDP lines. Ignoring.\n");
05489 break;
05490 } else {
05491 f++;
05492 req->line[f] = c + 1;
05493 }
05494 } else if (*c == '\r') {
05495
05496 *c = 0;
05497 }
05498 c++;
05499 }
05500
05501 req->lines = f;
05502
05503
05504 if (!ast_strlen_zero(req->line[f])) {
05505 if (sipdebug && option_debug > 3)
05506 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
05507 req->lines++;
05508 }
05509
05510 if (*c)
05511 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
05512
05513
05514 return determine_firstline_parts(req);
05515 }
05516
05517
05518
05519
05520
05521
05522
05523
05524
05525 static int find_sdp(struct sip_request *req)
05526 {
05527 const char *content_type;
05528 const char *content_length;
05529 const char *search;
05530 char *boundary;
05531 unsigned int x;
05532 int boundaryisquoted = FALSE;
05533 int found_application_sdp = FALSE;
05534 int found_end_of_headers = FALSE;
05535
05536 content_length = get_header(req, "Content-Length");
05537
05538 if (!ast_strlen_zero(content_length)) {
05539 if (sscanf(content_length, "%30u", &x) != 1) {
05540 ast_log(LOG_WARNING, "Invalid Content-Length: %s\n", content_length);
05541 return 0;
05542 }
05543
05544
05545
05546 if (x == 0)
05547 return 0;
05548 }
05549
05550 content_type = get_header(req, "Content-Type");
05551
05552
05553 if (!strncasecmp(content_type, "application/sdp", 15)) {
05554 req->sdp_start = 0;
05555 req->sdp_count = req->lines;
05556 return req->lines ? 1 : 0;
05557 }
05558
05559
05560 if (strncasecmp(content_type, "multipart/mixed", 15))
05561 return 0;
05562
05563
05564 if ((search = strcasestr(content_type, ";boundary=")))
05565 search += 10;
05566 else if ((search = strcasestr(content_type, "; boundary=")))
05567 search += 11;
05568 else
05569 return 0;
05570
05571 if (ast_strlen_zero(search))
05572 return 0;
05573
05574
05575 if (*search == '\"') {
05576 search++;
05577 boundaryisquoted = TRUE;
05578 }
05579
05580
05581
05582 boundary = ast_strdupa(search - 2);
05583 boundary[0] = boundary[1] = '-';
05584
05585 if (boundaryisquoted)
05586 boundary[strlen(boundary) - 1] = '\0';
05587
05588
05589
05590
05591 for (x = 0; x < (req->lines ); x++) {
05592 if(!strncasecmp(req->line[x], boundary, strlen(boundary))){
05593 if(found_application_sdp && found_end_of_headers){
05594 req->sdp_count = (x - 1) - req->sdp_start;
05595 return 1;
05596 }
05597 found_application_sdp = FALSE;
05598 }
05599 if(!strcasecmp(req->line[x], "Content-Type: application/sdp"))
05600 found_application_sdp = TRUE;
05601
05602 if(strlen(req->line[x]) == 0 ){
05603 if(found_application_sdp && !found_end_of_headers){
05604 req->sdp_start = x;
05605 found_end_of_headers = TRUE;
05606 }
05607 }
05608 }
05609 if(found_application_sdp && found_end_of_headers) {
05610 req->sdp_count = x - req->sdp_start;
05611 return TRUE;
05612 }
05613 return FALSE;
05614 }
05615
05616
05617 static void change_hold_state(struct sip_pvt *dialog, struct sip_request *req, int holdstate, int sendonly)
05618 {
05619 if (global_notifyhold && (!holdstate || !ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD)))
05620 sip_peer_hold(dialog, holdstate);
05621 if (global_callevents)
05622 manager_event(EVENT_FLAG_CALL, holdstate ? "Hold" : "Unhold",
05623 "Channel: %s\r\n"
05624 "Uniqueid: %s\r\n",
05625 dialog->owner->name,
05626 dialog->owner->uniqueid);
05627 append_history(dialog, holdstate ? "Hold" : "Unhold", "%s", req->data);
05628 if (!holdstate) {
05629 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD);
05630 return;
05631 }
05632
05633
05634 if (sendonly == 1)
05635 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
05636 else if (sendonly == 2)
05637 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
05638 else
05639 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
05640 return;
05641 }
05642
05643 enum media_type {
05644 SDP_AUDIO,
05645 SDP_VIDEO,
05646 SDP_IMAGE,
05647 };
05648
05649 static int get_ip_and_port_from_sdp(struct sip_request *req, const enum media_type media, struct sockaddr_in *sin)
05650 {
05651 const char *m;
05652 const char *c;
05653 int miterator = req->sdp_start;
05654 int citerator = req->sdp_start;
05655 int x = 0;
05656 int numberofports;
05657 int len;
05658 char host[258] = "";
05659 struct ast_hostent audiohp;
05660 struct hostent *hp;
05661
05662 c = get_sdp_iterate(&citerator, req, "c");
05663 if (sscanf(c, "IN IP4 %256s", host) != 1) {
05664 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
05665
05666 }
05667
05668 for (m = get_sdp_iterate(&miterator, req, "m"); !ast_strlen_zero(m); m = get_sdp_iterate(&miterator, req, "m")) {
05669 if ((media == SDP_AUDIO && ((sscanf(m, "audio %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05670 (sscanf(m, "audio %30d RTP/AVP %n", &x, &len) == 1 && len > 0))) ||
05671 (media == SDP_VIDEO && ((sscanf(m, "video %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05672 (sscanf(m, "video %30d RTP/AVP %n", &x, &len) == 1 && len > 0)))) {
05673
05674
05675
05676
05677 c = get_sdp_iterate(&citerator, req, "c");
05678 if (!ast_strlen_zero(c)) {
05679 sscanf(c, "IN IP4 %256s", host);
05680 }
05681 break;
05682 }
05683 }
05684
05685 if (ast_strlen_zero(host) || x == 0) {
05686 ast_log(LOG_WARNING, "Failed to read an alternate host or port in SDP. Expect %s problems\n", media == SDP_AUDIO ? "audio" : "video");
05687 return -1;
05688 }
05689
05690 hp = ast_gethostbyname(host, &audiohp);
05691 if (!hp) {
05692 ast_log(LOG_WARNING, "Could not look up IP address of alternate hostname. Expect %s problems\n", media == SDP_AUDIO? "audio" : "video");
05693 return -1;
05694 }
05695
05696 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
05697 sin->sin_port = htons(x);
05698 return 0;
05699 }
05700
05701
05702
05703
05704
05705
05706 static int process_sdp(struct sip_pvt *p, struct sip_request *req)
05707 {
05708
05709 int start = req->sdp_start;
05710 int next = start;
05711 int iterator = start;
05712
05713
05714 char type = '\0';
05715 const char *value = NULL;
05716 const char *m = NULL;
05717 const char *nextm = NULL;
05718 int len = -1;
05719
05720
05721 struct ast_hostent audiohp;
05722 struct ast_hostent videohp;
05723 struct ast_hostent imagehp;
05724 struct ast_hostent sessionhp;
05725 struct hostent *hp = NULL;
05726 struct hostent *vhp = NULL;
05727 struct hostent *ihp = NULL;
05728 int portno = -1;
05729 int vportno = -1;
05730 int udptlportno = -1;
05731 struct sockaddr_in sin = { 0, };
05732 struct sockaddr_in vsin = { 0, };
05733 struct sockaddr_in isin = { 0, };
05734
05735
05736 int peercapability = 0, peernoncodeccapability = 0;
05737 int vpeercapability = 0, vpeernoncodeccapability = 0;
05738 struct ast_rtp *newaudiortp, *newvideortp;
05739 int newjointcapability;
05740 int newpeercapability;
05741 int newnoncodeccapability;
05742 const char *codecs;
05743 int codec;
05744
05745
05746 int sendonly = -1;
05747 int vsendonly = -1;
05748 int numberofports;
05749 int numberofmediastreams = 0;
05750 int last_rtpmap_codec = 0;
05751 int debug = sip_debug_test_pvt(p);
05752
05753
05754 if (!p->rtp) {
05755 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
05756 return -1;
05757 }
05758
05759
05760 #ifdef LOW_MEMORY
05761 newaudiortp = ast_threadstorage_get(&ts_audio_rtp, ast_rtp_alloc_size());
05762 #else
05763 newaudiortp = alloca(ast_rtp_alloc_size());
05764 #endif
05765 memset(newaudiortp, 0, ast_rtp_alloc_size());
05766 ast_rtp_new_init(newaudiortp);
05767 ast_rtp_pt_clear(newaudiortp);
05768
05769 #ifdef LOW_MEMORY
05770 newvideortp = ast_threadstorage_get(&ts_video_rtp, ast_rtp_alloc_size());
05771 #else
05772 newvideortp = alloca(ast_rtp_alloc_size());
05773 #endif
05774 memset(newvideortp, 0, ast_rtp_alloc_size());
05775 ast_rtp_new_init(newvideortp);
05776 ast_rtp_pt_clear(newvideortp);
05777
05778
05779
05780 p->lastrtprx = p->lastrtptx = time(NULL);
05781
05782 ast_set_flag(&p->flags[0], SIP_NOVIDEO);
05783
05784 memset(p->offered_media, 0, sizeof(p->offered_media));
05785
05786
05787
05788 nextm = get_sdp_iterate(&next, req, "m");
05789 if (ast_strlen_zero(nextm)) {
05790 ast_log(LOG_WARNING, "Insufficient information for SDP (m= not found)\n");
05791 return -1;
05792 }
05793
05794
05795 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
05796 int processed = FALSE;
05797 switch (type) {
05798 case 'c':
05799 if (process_sdp_c(value, &sessionhp)) {
05800 processed = TRUE;
05801 hp = &sessionhp.hp;
05802 vhp = hp;
05803 ihp = hp;
05804 }
05805 break;
05806 case 'a':
05807 if (process_sdp_a_sendonly(value, &sendonly)) {
05808 processed = TRUE;
05809 vsendonly = sendonly;
05810 }
05811 else if (process_sdp_a_audio(value, p, newaudiortp, &last_rtpmap_codec))
05812 processed = TRUE;
05813 else if (process_sdp_a_video(value, p, newvideortp, &last_rtpmap_codec))
05814 processed = TRUE;
05815 else if (process_sdp_a_image(value, p))
05816 processed = TRUE;
05817 break;
05818 }
05819
05820 if (option_debug > 2)
05821 ast_log(LOG_DEBUG, "Processing session-level SDP %c=%s... %s\n", type, value, (processed == TRUE)? "OK." : "UNSUPPORTED.");
05822 }
05823
05824
05825
05826 while (!ast_strlen_zero(nextm)) {
05827 int audio = FALSE;
05828 int video = FALSE;
05829 int image = FALSE;
05830 int x;
05831
05832 numberofports = 1;
05833 len = -1;
05834 start = next;
05835 m = nextm;
05836 iterator = next;
05837 nextm = get_sdp_iterate(&next, req, "m");
05838
05839
05840 if ((sscanf(m, "audio %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05841 (sscanf(m, "audio %30d RTP/AVP %n", &x, &len) == 1 && len > 0)) {
05842
05843 audio = TRUE;
05844 p->offered_media[SDP_AUDIO].offered = TRUE;
05845 numberofmediastreams++;
05846 portno = x;
05847
05848
05849 codecs = m + len;
05850 ast_copy_string(p->offered_media[SDP_AUDIO].text, codecs, sizeof(p->offered_media[SDP_AUDIO].text));
05851 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
05852 if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
05853 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
05854 return -1;
05855 }
05856 if (debug)
05857 ast_verbose("Found RTP audio format %d\n", codec);
05858 ast_rtp_set_m_type(newaudiortp, codec);
05859 }
05860
05861 } else if ((sscanf(m, "video %30d/%30d RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
05862 (sscanf(m, "video %30d RTP/AVP %n", &x, &len) == 1 && len >= 0)) {
05863
05864 video = TRUE;
05865 ast_clear_flag(&p->flags[0], SIP_NOVIDEO);
05866 p->offered_media[SDP_VIDEO].offered = TRUE;
05867 numberofmediastreams++;
05868 vportno = x;
05869
05870
05871 codecs = m + len;
05872 ast_copy_string(p->offered_media[SDP_VIDEO].text, codecs, sizeof(p->offered_media[SDP_VIDEO].text));
05873 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
05874 if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
05875 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
05876 return -1;
05877 }
05878 if (debug)
05879 ast_verbose("Found RTP video format %d\n", codec);
05880 ast_rtp_set_m_type(newvideortp, codec);
05881 }
05882
05883 } else if (p->udptl && ((sscanf(m, "image %30d udptl t38%n", &x, &len) == 1 && len > 0) ||
05884 (sscanf(m, "image %30d UDPTL t38%n", &x, &len) == 1 && len >= 0))) {
05885
05886 image = TRUE;
05887 if (debug)
05888 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
05889 p->offered_media[SDP_IMAGE].offered = TRUE;
05890 udptlportno = x;
05891 numberofmediastreams++;
05892
05893 if (p->owner && p->lastinvite) {
05894 if (p->t38.state != T38_LOCAL_REINVITE) {
05895 p->t38.state = T38_PEER_REINVITE;
05896 if (option_debug > 1)
05897 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>" );
05898 }
05899 } else {
05900 p->t38.state = T38_PEER_DIRECT;
05901 p->t38.direct = 1;
05902 if (option_debug > 1)
05903 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05904 }
05905
05906
05907
05908 p->t38.peercapability &= ~T38FAX_UDP_EC_NONE;
05909 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
05910 } else {
05911 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
05912 continue;
05913 }
05914
05915
05916 if (numberofports > 1)
05917 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
05918
05919
05920
05921
05922 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
05923 int processed = FALSE;
05924
05925 switch (type) {
05926 case 'c':
05927 if (audio) {
05928 if (process_sdp_c(value, &audiohp)) {
05929 processed = TRUE;
05930 hp = &audiohp.hp;
05931 }
05932 }
05933 else if (video) {
05934 if (process_sdp_c(value, &videohp)) {
05935 processed = TRUE;
05936 vhp = &videohp.hp;
05937 }
05938 } else if (image) {
05939 if (process_sdp_c(value, &imagehp)) {
05940 processed = TRUE;
05941 ihp = &imagehp.hp;
05942 }
05943 }
05944 break;
05945 case 'a':
05946
05947 if (audio) {
05948 if (process_sdp_a_sendonly(value, &sendonly))
05949 processed = TRUE;
05950 else if (process_sdp_a_audio(value, p, newaudiortp, &last_rtpmap_codec))
05951 processed = TRUE;
05952 }
05953
05954 else if (video) {
05955 if (process_sdp_a_sendonly(value, &vsendonly))
05956 processed = TRUE;
05957 else if (process_sdp_a_video(value, p, newvideortp, &last_rtpmap_codec))
05958 processed = TRUE;
05959 }
05960
05961 else if (image) {
05962 if (process_sdp_a_image(value, p))
05963 processed = TRUE;
05964 }
05965 break;
05966 }
05967
05968 if (option_debug > 2)
05969 ast_log(LOG_DEBUG, "Processing media-level (%s) SDP %c=%s... %s\n",
05970 (audio == TRUE)? "audio" : (video == TRUE)? "video" : "image",
05971 type, value,
05972 (processed == TRUE)? "OK." : "UNSUPPORTED.");
05973 }
05974 }
05975
05976
05977 if (!hp && !vhp && !ihp) {
05978 ast_log(LOG_WARNING, "Insufficient information in SDP (c=)...\n");
05979 return -1;
05980 }
05981
05982 if (portno == -1 && vportno == -1 && udptlportno == -1)
05983
05984
05985 return -2;
05986
05987 if (numberofmediastreams > 2)
05988
05989 return -3;
05990
05991 if (udptlportno == -1) {
05992 p->t38.state = T38_DISABLED;
05993 if (option_debug > 2)
05994 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05995 }
05996
05997
05998
05999 ast_rtp_get_current_formats(newaudiortp, &peercapability, &peernoncodeccapability);
06000 ast_rtp_get_current_formats(newvideortp, &vpeercapability, &vpeernoncodeccapability);
06001
06002 newjointcapability = p->capability & (peercapability | vpeercapability);
06003 newpeercapability = (peercapability | vpeercapability);
06004 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
06005
06006
06007 if (debug) {
06008
06009 char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE];
06010
06011 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s, combined - %s\n",
06012 ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
06013 ast_getformatname_multiple(s2, SIPBUFSIZE, newpeercapability),
06014 ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
06015 ast_getformatname_multiple(s4, SIPBUFSIZE, newjointcapability));
06016
06017 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
06018 ast_rtp_lookup_mime_multiple(s1, SIPBUFSIZE, p->noncodeccapability, 0, 0),
06019 ast_rtp_lookup_mime_multiple(s2, SIPBUFSIZE, peernoncodeccapability, 0, 0),
06020 ast_rtp_lookup_mime_multiple(s3, SIPBUFSIZE, newnoncodeccapability, 0, 0));
06021
06022 ast_log(LOG_DEBUG, "Our T38 capability = (%d), peer T38 capability (%d), joint T38 capability (%d)\n",
06023 p->t38.capability,
06024 p->t38.peercapability,
06025 p->t38.jointcapability);
06026
06027 }
06028 if (!newjointcapability) {
06029
06030 if (!p->t38.jointcapability || !udptlportno) {
06031 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
06032
06033 return -1;
06034 } else {
06035 if (option_debug > 2)
06036 ast_log(LOG_DEBUG, "Have T.38 but no audio codecs, accepting offer anyway\n");
06037 }
06038 }
06039
06040
06041
06042 p->jointcapability = newjointcapability;
06043 p->peercapability = newpeercapability;
06044 p->jointnoncodeccapability = newnoncodeccapability;
06045
06046 ast_rtp_pt_copy(p->rtp, newaudiortp);
06047 if (p->vrtp)
06048 ast_rtp_pt_copy(p->vrtp, newvideortp);
06049
06050 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
06051 ast_clear_flag(&p->flags[0], SIP_DTMF);
06052 if (newnoncodeccapability & AST_RTP_DTMF) {
06053
06054 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
06055
06056 ast_rtp_setdtmf(p->rtp, 1);
06057 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
06058 } else {
06059 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
06060 }
06061 }
06062
06063
06064 if (p->rtp) {
06065 if (portno > 0) {
06066 sin.sin_family = AF_INET;
06067 sin.sin_port = htons(portno);
06068 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
06069 ast_rtp_set_peer(p->rtp, &sin);
06070 if (debug)
06071 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
06072 } else if (udptlportno > 0) {
06073 if (debug)
06074 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session.\n");
06075 } else {
06076 ast_rtp_stop(p->rtp);
06077 if (debug)
06078 ast_verbose("Peer doesn't provide audio\n");
06079 }
06080 }
06081
06082
06083 if (p->vrtp) {
06084 if (vportno > 0) {
06085 vsin.sin_family = AF_INET;
06086 vsin.sin_port = htons(vportno);
06087 memcpy(&vsin.sin_addr, vhp->h_addr, sizeof(vsin.sin_addr));
06088 ast_rtp_set_peer(p->vrtp, &vsin);
06089 if (debug)
06090 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
06091 } else {
06092 ast_rtp_stop(p->vrtp);
06093 if (debug)
06094 ast_verbose("Peer doesn't provide video\n");
06095 }
06096 }
06097
06098
06099 if (p->udptl) {
06100 if (udptlportno > 0) {
06101 isin.sin_family = AF_INET;
06102 isin.sin_port = htons(udptlportno);
06103 if (ast_test_flag(&p->flags[0], SIP_NAT) && ast_test_flag(&p->flags[1], SIP_PAGE2_UDPTL_DESTINATION)) {
06104 struct sockaddr_in peer = { 0, };
06105 ast_rtp_get_peer(p->rtp, &peer);
06106 if (peer.sin_addr.s_addr) {
06107 memcpy(&isin.sin_addr, &peer.sin_addr, sizeof(isin.sin_addr));
06108 if (debug)
06109 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));
06110 }
06111 } else
06112 memcpy(&isin.sin_addr, ihp->h_addr, sizeof(isin.sin_addr));
06113 ast_udptl_set_peer(p->udptl, &isin);
06114 if (debug)
06115 ast_log(LOG_DEBUG,"Peer T.38 UDPTL is at port %s:%d\n",ast_inet_ntoa(isin.sin_addr), ntohs(isin.sin_port));
06116 } else {
06117 ast_udptl_stop(p->udptl);
06118 if (debug)
06119 ast_log(LOG_DEBUG, "Peer doesn't provide T.38 UDPTL\n");
06120 }
06121 }
06122
06123
06124
06125 if (option_debug > 1) {
06126 char buf[SIPBUFSIZE];
06127 ast_log(LOG_DEBUG, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
06128 }
06129
06130 if (!p->owner)
06131 return 0;
06132
06133 if (option_debug > 3)
06134 ast_log(LOG_DEBUG, "We have an owner, now see if we need to change this call\n");
06135
06136 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
06137 if (debug) {
06138 char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
06139 ast_log(LOG_DEBUG, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
06140 ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
06141 ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
06142 }
06143 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability);
06144 ast_set_read_format(p->owner, p->owner->readformat);
06145 ast_set_write_format(p->owner, p->owner->writeformat);
06146 }
06147
06148
06149 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && (sin.sin_addr.s_addr || vsin.sin_addr.s_addr || isin.sin_addr.s_addr) && (!sendonly || sendonly == -1)) {
06150 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
06151
06152 ast_queue_frame(p->owner, &ast_null_frame);
06153 } else if (!(sin.sin_addr.s_addr || vsin.sin_addr.s_addr || isin.sin_addr.s_addr) || (sendonly && sendonly != -1)) {
06154 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
06155 S_OR(p->mohsuggest, NULL),
06156 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
06157 if (sendonly)
06158 ast_rtp_stop(p->rtp);
06159
06160
06161 ast_queue_frame(p->owner, &ast_null_frame);
06162 }
06163
06164
06165 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && (sin.sin_addr.s_addr || vsin.sin_addr.s_addr || isin.sin_addr.s_addr) && (!sendonly || sendonly == -1))
06166 change_hold_state(p, req, FALSE, sendonly);
06167 else if (!(sin.sin_addr.s_addr || vsin.sin_addr.s_addr || isin.sin_addr.s_addr) || (sendonly && sendonly != -1))
06168 change_hold_state(p, req, TRUE, sendonly);
06169
06170 return 0;
06171 }
06172
06173
06174 static int process_sdp_c(const char *c, struct ast_hostent *ast_hp)
06175 {
06176 char host[258];
06177 struct hostent *hp;
06178
06179
06180 if (sscanf(c, "IN IP4 %255s", host) != 1) {
06181 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
06182 return FALSE;
06183 } else {
06184 if (!(hp = ast_gethostbyname(host, ast_hp))) {
06185 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in c= line, '%s'\n", c);
06186 return FALSE;
06187 }
06188 return TRUE;
06189 }
06190 return FALSE;
06191 }
06192
06193 static int process_sdp_a_sendonly(const char *a, int *sendonly)
06194 {
06195 int found = FALSE;
06196
06197 if (!strcasecmp(a, "sendonly")) {
06198 if (*sendonly == -1)
06199 *sendonly = 1;
06200 found = TRUE;
06201 } else if (!strcasecmp(a, "inactive")) {
06202 if (*sendonly == -1)
06203 *sendonly = 2;
06204 found = TRUE;
06205 } else if (!strcasecmp(a, "sendrecv")) {
06206 if (*sendonly == -1)
06207 *sendonly = 0;
06208 found = TRUE;
06209 }
06210 return found;
06211 }
06212
06213 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp *newaudiortp, int *last_rtpmap_codec)
06214 {
06215 int found = FALSE;
06216 int codec;
06217 char* mimeSubtype = ast_strdupa(a);
06218 int debug = sip_debug_test_pvt(p);
06219
06220 if (strlen(a) > 5 && !strncasecmp(a, "ptime", 5)) {
06221 char *tmp = strrchr(a, ':');
06222 long int framing = 0;
06223 if (tmp) {
06224 tmp++;
06225 framing = strtol(tmp, NULL, 10);
06226 if (framing == LONG_MIN || framing == LONG_MAX) {
06227 framing = 0;
06228 if (option_debug)
06229 ast_log(LOG_DEBUG, "Can't read framing from SDP: %s\n", a);
06230 }
06231 }
06232 if (framing && p->autoframing) {
06233 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
06234 int codec_n;
06235 int format = 0;
06236 for (codec_n = 0; codec_n < MAX_RTP_PT; codec_n++) {
06237 format = ast_rtp_codec_getformat(codec_n);
06238 if (!format)
06239 continue;
06240 if (option_debug)
06241 ast_log(LOG_DEBUG, "Setting framing for %d to %ld\n", format, framing);
06242 ast_codec_pref_setsize(pref, format, framing);
06243 }
06244 ast_rtp_codec_setpref(p->rtp, pref);
06245 }
06246 found = TRUE;
06247 } else if (sscanf(a, "rtpmap: %30u %[^/]/", &codec, mimeSubtype) == 2) {
06248
06249 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
06250
06251 if (ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
06252 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0) != -1) {
06253 if (debug)
06254 ast_verbose("Found audio description format %s for ID %d\n", mimeSubtype, codec);
06255 (*last_rtpmap_codec)++;
06256 found = TRUE;
06257 }
06258 } else {
06259 if (debug)
06260 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
06261 }
06262
06263 if (!found) {
06264
06265 ast_rtp_unset_m_type(newaudiortp, codec);
06266 if (debug)
06267 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
06268 }
06269 }
06270
06271 return found;
06272 }
06273
06274 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp *newvideortp, int *last_rtpmap_codec)
06275 {
06276 int found = FALSE;
06277 int codec;
06278 char* mimeSubtype = ast_strdupa(a);
06279 int debug = sip_debug_test_pvt(p);
06280
06281 if (sscanf(a, "rtpmap: %30u %[^/]/", &codec, mimeSubtype) == 2) {
06282
06283 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
06284
06285 if (p->vrtp && ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0) != -1) {
06286 if (debug)
06287 ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec);
06288 (*last_rtpmap_codec)++;
06289 found = TRUE;
06290 }
06291 } else {
06292 if (debug)
06293 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
06294 }
06295
06296 if (!found) {
06297
06298 ast_rtp_unset_m_type(newvideortp, codec);
06299 if (debug)
06300 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
06301 }
06302 }
06303
06304 return found;
06305 }
06306
06307 static int process_sdp_a_image(const char *a, struct sip_pvt *p)
06308 {
06309 int found = FALSE;
06310 char s[256];
06311 int x;
06312
06313
06314 if ((sscanf(a, "T38FaxMaxBuffer:%30d", &x) == 1)) {
06315 found = TRUE;
06316 if (option_debug > 2)
06317 ast_log(LOG_DEBUG, "MaxBufferSize:%d\n",x);
06318 } else if ((sscanf(a, "T38MaxBitRate:%30d", &x) == 1) || (sscanf(a, "T38FaxMaxRate:%30d", &x) == 1)) {
06319 found = TRUE;
06320 if (option_debug > 2)
06321 ast_log(LOG_DEBUG,"T38MaxBitRate: %d\n",x);
06322 switch (x) {
06323 case 14400:
06324 p->t38.peercapability |= T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
06325 break;
06326 case 12000:
06327 p->t38.peercapability |= T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
06328 break;
06329 case 9600:
06330 p->t38.peercapability |= T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
06331 break;
06332 case 7200:
06333 p->t38.peercapability |= T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
06334 break;
06335 case 4800:
06336 p->t38.peercapability |= T38FAX_RATE_4800 | T38FAX_RATE_2400;
06337 break;
06338 case 2400:
06339 p->t38.peercapability |= T38FAX_RATE_2400;
06340 break;
06341 }
06342 } else if ((sscanf(a, "T38FaxVersion:%30d", &x) == 1)) {
06343 found = TRUE;
06344 if (option_debug > 2)
06345 ast_log(LOG_DEBUG, "FaxVersion: %d\n",x);
06346 if (x == 0)
06347 p->t38.peercapability |= T38FAX_VERSION_0;
06348 else if (x == 1)
06349 p->t38.peercapability |= T38FAX_VERSION_1;
06350 } else if ((sscanf(a, "T38FaxMaxDatagram:%30d", &x) == 1) || (sscanf(a, "T38MaxDatagram:%30d", &x) == 1)) {
06351 found = TRUE;
06352 if (option_debug > 2)
06353 ast_log(LOG_DEBUG, "FaxMaxDatagram: %d\n",x);
06354 ast_udptl_set_far_max_datagram(p->udptl, x);
06355 ast_udptl_set_local_max_datagram(p->udptl, x);
06356 } else if ((strncmp(a, "T38FaxFillBitRemoval", 20) == 0)) {
06357 found = TRUE;
06358 if ((sscanf(a, "T38FaxFillBitRemoval:%30d", &x) == 1)) {
06359 if (option_debug > 2)
06360 ast_log(LOG_DEBUG, "FillBitRemoval: %d\n",x);
06361 if (x == 1)
06362 p->t38.peercapability |= T38FAX_FILL_BIT_REMOVAL;
06363 } else {
06364 if (option_debug > 2)
06365 ast_log(LOG_DEBUG, "FillBitRemoval\n");
06366 p->t38.peercapability |= T38FAX_FILL_BIT_REMOVAL;
06367 }
06368 } else if ((strncmp(a, "T38FaxTranscodingMMR", 20) == 0)) {
06369 found = TRUE;
06370 if ((sscanf(a, "T38FaxTranscodingMMR:%30d", &x) == 1)) {
06371 if (option_debug > 2)
06372 ast_log(LOG_DEBUG, "Transcoding MMR: %d\n",x);
06373 if (x == 1)
06374 p->t38.peercapability |= T38FAX_TRANSCODING_MMR;
06375 } else {
06376 if (option_debug > 2)
06377 ast_log(LOG_DEBUG, "Transcoding MMR\n");
06378 p->t38.peercapability |= T38FAX_TRANSCODING_MMR;
06379 }
06380 } else if ((strncmp(a, "T38FaxTranscodingJBIG", 21) == 0)) {
06381 found = TRUE;
06382 if ((sscanf(a, "T38FaxTranscodingJBIG:%30d", &x) == 1)) {
06383 if (option_debug > 2)
06384 ast_log(LOG_DEBUG, "Transcoding JBIG: %d\n",x);
06385 if (x == 1)
06386 p->t38.peercapability |= T38FAX_TRANSCODING_JBIG;
06387 } else {
06388 if (option_debug > 2)
06389 ast_log(LOG_DEBUG, "Transcoding JBIG\n");
06390 p->t38.peercapability |= T38FAX_TRANSCODING_JBIG;
06391 }
06392 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
06393 found = TRUE;
06394 if (option_debug > 2)
06395 ast_log(LOG_DEBUG, "RateManagement: %s\n", s);
06396 if (!strcasecmp(s, "localTCF"))
06397 p->t38.peercapability |= T38FAX_RATE_MANAGEMENT_LOCAL_TCF;
06398 else if (!strcasecmp(s, "transferredTCF"))
06399 p->t38.peercapability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
06400 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
06401 found = TRUE;
06402 if (option_debug > 2)
06403 ast_log(LOG_DEBUG, "UDP EC: %s\n", s);
06404 if (!strcasecmp(s, "t38UDPRedundancy")) {
06405 p->t38.peercapability |= T38FAX_UDP_EC_REDUNDANCY;
06406 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
06407 } else if (!strcasecmp(s, "t38UDPFEC")) {
06408 p->t38.peercapability |= T38FAX_UDP_EC_FEC;
06409 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
06410 } else {
06411 p->t38.peercapability |= T38FAX_UDP_EC_NONE;
06412 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
06413 }
06414 }
06415
06416 if (found) {
06417 int t38speed = p->t38.peercapability & (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400);
06418
06419 p->t38.jointcapability = (p->t38.peercapability & 255);
06420 p->t38.jointcapability |= (t38speed & p->t38.capability);
06421 }
06422
06423 return found;
06424 }
06425
06426
06427
06428 #ifdef LOW_MEMORY
06429 static void ts_ast_rtp_destroy(void *data)
06430 {
06431 struct ast_rtp *tmp = data;
06432 ast_rtp_destroy(tmp);
06433 }
06434 #endif
06435
06436
06437 static int add_header(struct sip_request *req, const char *var, const char *value)
06438 {
06439 int maxlen = sizeof(req->data) - 4 - req->len - strlen(req->content);
06440
06441 if (req->headers == SIP_MAX_HEADERS) {
06442 ast_log(LOG_WARNING, "Out of SIP header space\n");
06443 return -1;
06444 }
06445
06446 if (req->lines) {
06447 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
06448 return -1;
06449 }
06450
06451 if (maxlen <= 0) {
06452 ast_log(LOG_WARNING, "Out of space, can't add anymore (%s:%s)\n", var, value);
06453 return -1;
06454 }
06455
06456 req->header[req->headers] = req->data + req->len;
06457
06458 if (compactheaders)
06459 var = find_alias(var, var);
06460
06461 snprintf(req->header[req->headers], maxlen, "%s: %s\r\n", var, value);
06462 req->len += strlen(req->header[req->headers]);
06463 req->headers++;
06464
06465 return 0;
06466 }
06467
06468
06469 static int finalize_content(struct sip_request *req)
06470 {
06471 char clen[10];
06472
06473 if (req->lines) {
06474 ast_log(LOG_WARNING, "finalize_content() called on a message that has already been finalized\n");
06475 return -1;
06476 }
06477
06478 snprintf(clen, sizeof(clen), "%zd", strlen(req->content));
06479 add_header(req, "Content-Length", clen);
06480
06481 if (!ast_strlen_zero(req->content)) {
06482 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n%s", req->content);
06483 req->len += strlen(req->data + req->len);
06484 }
06485
06486 req->lines = !ast_strlen_zero(req->content);
06487 return 0;
06488 }
06489
06490
06491 static int add_content(struct sip_request *req, const char *line)
06492 {
06493 if (req->lines) {
06494 ast_log(LOG_WARNING, "Can't add more content when the content has been finalized\n");
06495 return -1;
06496 }
06497
06498 if (req->len + strlen(req->content) + strlen(line) >= sizeof(req->data) - 4) {
06499 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
06500 return -1;
06501 }
06502
06503 snprintf(req->content + strlen(req->content), sizeof(req->content) - strlen(req->content), "%s", line);
06504 return 0;
06505 }
06506
06507
06508 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
06509 {
06510 const char *tmp = get_header(orig, field);
06511
06512 if (!ast_strlen_zero(tmp))
06513 return add_header(req, field, tmp);
06514 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
06515 return -1;
06516 }
06517
06518
06519 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
06520 {
06521 int start = 0;
06522 int copied = 0;
06523 for (;;) {
06524 const char *tmp = __get_header(orig, field, &start);
06525
06526 if (ast_strlen_zero(tmp))
06527 break;
06528
06529 add_header(req, field, tmp);
06530 copied++;
06531 }
06532 return copied ? 0 : -1;
06533 }
06534
06535
06536
06537
06538
06539
06540
06541 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
06542 {
06543 int copied = 0;
06544 int start = 0;
06545
06546 for (;;) {
06547 char new[512];
06548 const char *oh = __get_header(orig, field, &start);
06549
06550 if (ast_strlen_zero(oh))
06551 break;
06552
06553 if (!copied) {
06554 char leftmost[512], *others, *rport;
06555
06556
06557 ast_copy_string(leftmost, oh, sizeof(leftmost));
06558 others = strchr(leftmost, ',');
06559 if (others)
06560 *others++ = '\0';
06561
06562
06563 rport = strstr(leftmost, ";rport");
06564 if (rport && *(rport+6) == '=')
06565 rport = NULL;
06566
06567
06568 if (rport && ((ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_ALWAYS) || (ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_RFC3581))) {
06569
06570 char *end;
06571
06572 rport = strstr(leftmost, ";rport");
06573
06574 if (rport) {
06575 end = strchr(rport + 1, ';');
06576 if (end)
06577 memmove(rport, end, strlen(end) + 1);
06578 else
06579 *rport = '\0';
06580 }
06581
06582
06583 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
06584 leftmost, ast_inet_ntoa(p->recv.sin_addr),
06585 ntohs(p->recv.sin_port),
06586 others ? "," : "", others ? others : "");
06587 } else {
06588
06589 snprintf(new, sizeof(new), "%s;received=%s%s%s",
06590 leftmost, ast_inet_ntoa(p->recv.sin_addr),
06591 others ? "," : "", others ? others : "");
06592 }
06593 oh = new;
06594 }
06595 add_header(req, field, oh);
06596 copied++;
06597 }
06598 if (!copied) {
06599 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
06600 return -1;
06601 }
06602 return 0;
06603 }
06604
06605
06606 static void add_route(struct sip_request *req, struct sip_route *route)
06607 {
06608 char r[SIPBUFSIZE*2], *p;
06609 int n, rem = sizeof(r);
06610
06611 if (!route)
06612 return;
06613
06614 p = r;
06615 for (;route ; route = route->next) {
06616 n = strlen(route->hop);
06617 if (rem < n+3)
06618 break;
06619 if (p != r) {
06620 *p++ = ',';
06621 --rem;
06622 }
06623 *p++ = '<';
06624 ast_copy_string(p, route->hop, rem);
06625 p += n;
06626 *p++ = '>';
06627 rem -= (n+2);
06628 }
06629 *p = '\0';
06630 add_header(req, "Route", r);
06631 }
06632
06633
06634 static void set_destination(struct sip_pvt *p, char *uri)
06635 {
06636 char *h, *maddr, hostname[256];
06637 int port, hn;
06638 struct hostent *hp;
06639 struct ast_hostent ahp;
06640 int debug=sip_debug_test_pvt(p);
06641
06642
06643
06644
06645 if (debug)
06646 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
06647
06648
06649 h = strchr(uri, '@');
06650 if (h)
06651 ++h;
06652 else {
06653 h = uri;
06654 if (strncasecmp(h, "sip:", 4) == 0)
06655 h += 4;
06656 else if (strncasecmp(h, "sips:", 5) == 0)
06657 h += 5;
06658 }
06659 hn = strcspn(h, ":;>") + 1;
06660 if (hn > sizeof(hostname))
06661 hn = sizeof(hostname);
06662 ast_copy_string(hostname, h, hn);
06663
06664 h += hn - 1;
06665
06666
06667 if (*h == ':') {
06668
06669 ++h;
06670 port = strtol(h, &h, 10);
06671 }
06672 else
06673 port = STANDARD_SIP_PORT;
06674
06675
06676 maddr = strstr(h, "maddr=");
06677 if (maddr) {
06678 maddr += 6;
06679 hn = strspn(maddr, "0123456789.") + 1;
06680 if (hn > sizeof(hostname))
06681 hn = sizeof(hostname);
06682 ast_copy_string(hostname, maddr, hn);
06683 }
06684
06685 hp = ast_gethostbyname(hostname, &ahp);
06686 if (hp == NULL) {
06687 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
06688 return;
06689 }
06690 p->sa.sin_family = AF_INET;
06691 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
06692 p->sa.sin_port = htons(port);
06693 if (debug)
06694 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(p->sa.sin_addr), port);
06695 }
06696
06697
06698 static int init_resp(struct sip_request *resp, const char *msg)
06699 {
06700
06701 memset(resp, 0, sizeof(*resp));
06702 resp->method = SIP_RESPONSE;
06703 resp->header[0] = resp->data;
06704 snprintf(resp->header[0], sizeof(resp->data), "SIP/2.0 %s\r\n", msg);
06705 resp->len = strlen(resp->header[0]);
06706 resp->headers++;
06707 return 0;
06708 }
06709
06710
06711 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
06712 {
06713
06714 memset(req, 0, sizeof(*req));
06715 req->method = sipmethod;
06716 req->header[0] = req->data;
06717 snprintf(req->header[0], sizeof(req->data), "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
06718 req->len = strlen(req->header[0]);
06719 req->headers++;
06720 return 0;
06721 }
06722
06723
06724 static inline int resp_needs_contact(const char *msg, enum sipmethod method) {
06725
06726
06727
06728
06729
06730
06731
06732
06733
06734
06735
06736
06737
06738
06739
06740 switch (method) {
06741
06742 case SIP_INVITE:
06743 case SIP_UPDATE:
06744 case SIP_SUBSCRIBE:
06745 case SIP_NOTIFY:
06746 if ((msg[0] >= '1' && msg[0] <= '3') || !strncmp(msg, "485", 3))
06747 return 1;
06748 break;
06749
06750
06751 case SIP_REGISTER:
06752 case SIP_OPTIONS:
06753 if (msg[0] == '2' || msg[0] == '3' || !strncmp(msg, "485", 3))
06754 return 1;
06755 break;
06756
06757
06758 case SIP_BYE:
06759 case SIP_PRACK:
06760 case SIP_MESSAGE:
06761 case SIP_PUBLISH:
06762 if (msg[0] == '3' || !strncmp(msg, "485", 3))
06763 return 1;
06764 break;
06765
06766
06767 case SIP_REFER:
06768 if (msg[0] >= '2' && msg[0] <= '6')
06769 return 1;
06770 break;
06771
06772
06773 case SIP_ACK:
06774 case SIP_CANCEL:
06775 case SIP_INFO:
06776 case SIP_PING:
06777 default:
06778 return 0;
06779 }
06780 return 0;
06781 }
06782
06783
06784
06785 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
06786 {
06787 char newto[256];
06788 const char *ot;
06789
06790 init_resp(resp, msg);
06791 copy_via_headers(p, resp, req, "Via");
06792 if (msg[0] == '1' || msg[0] == '2')
06793 copy_all_header(resp, req, "Record-Route");
06794 copy_header(resp, req, "From");
06795 ot = get_header(req, "To");
06796 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
06797
06798
06799 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
06800 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
06801 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
06802 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
06803 else
06804 ast_copy_string(newto, ot, sizeof(newto));
06805 ot = newto;
06806 }
06807 add_header(resp, "To", ot);
06808 copy_header(resp, req, "Call-ID");
06809 copy_header(resp, req, "CSeq");
06810 if (!ast_strlen_zero(global_useragent))
06811 add_header(resp, "User-Agent", global_useragent);
06812 add_header(resp, "Allow", ALLOWED_METHODS);
06813 add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
06814 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
06815
06816
06817 char tmp[256];
06818
06819 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
06820 add_header(resp, "Expires", tmp);
06821 if (p->expiry) {
06822 char contact[SIPBUFSIZE];
06823 const char *contact_uri = p->method == SIP_SUBSCRIBE ? p->our_contact : p->fullcontact;
06824 char *brackets = strchr(contact_uri, '<');
06825 snprintf(contact, sizeof(contact), "%s%s%s;expires=%d", brackets ? "" : "<", contact_uri, brackets ? "" : ">", p->expiry);
06826 add_header(resp, "Contact", contact);
06827 }
06828 } else if (!ast_strlen_zero(p->our_contact) && resp_needs_contact(msg, p->method)) {
06829 add_header(resp, "Contact", p->our_contact);
06830 }
06831
06832
06833
06834
06835
06836
06837
06838 p->sa = p->recv;
06839
06840 if (process_via(p, req)) {
06841 ast_log(LOG_WARNING, "error processing via header, will send response to originating address\n");
06842 }
06843
06844 return 0;
06845 }
06846
06847
06848 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
06849 {
06850 struct sip_request *orig = &p->initreq;
06851 char stripped[80];
06852 char tmp[80];
06853 char newto[256];
06854 const char *c;
06855 const char *ot, *of;
06856 int is_strict = FALSE;
06857
06858 memset(req, 0, sizeof(struct sip_request));
06859
06860 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
06861
06862 if (!seqno) {
06863 p->ocseq++;
06864 seqno = p->ocseq;
06865 }
06866
06867
06868 if (sipmethod == SIP_CANCEL) {
06869 p->branch = p->invite_branch;
06870 build_via(p);
06871 } else if (newbranch && (sipmethod == SIP_INVITE)) {
06872 p->branch ^= ast_random();
06873 p->invite_branch = p->branch;
06874 build_via(p);
06875 } else if (newbranch) {
06876 p->branch ^= ast_random();
06877 build_via(p);
06878 }
06879
06880
06881 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop,";lr") == NULL) {
06882 is_strict = TRUE;
06883 if (sipdebug)
06884 ast_log(LOG_DEBUG, "Strict routing enforced for session %s\n", p->callid);
06885 }
06886
06887 if (sipmethod == SIP_CANCEL)
06888 c = p->initreq.rlPart2;
06889 else if (sipmethod == SIP_ACK) {
06890
06891
06892 if (!ast_strlen_zero(p->okcontacturi))
06893 c = is_strict ? p->route->hop : p->okcontacturi;
06894 else
06895 c = p->initreq.rlPart2;
06896 } else if (!ast_strlen_zero(p->okcontacturi))
06897 c = is_strict ? p->route->hop : p->okcontacturi;
06898 else if (!ast_strlen_zero(p->uri))
06899 c = p->uri;
06900 else {
06901 char *n;
06902
06903 ast_copy_string(stripped, get_header(orig, (ast_test_flag(&p->flags[0], SIP_OUTGOING)) ? "To" : "From"),
06904 sizeof(stripped));
06905 n = get_in_brackets(stripped);
06906 c = strsep(&n, ";");
06907 }
06908 init_req(req, sipmethod, c);
06909
06910 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
06911
06912 add_header(req, "Via", p->via);
06913 if (p->route) {
06914 set_destination(p, p->route->hop);
06915 add_route(req, is_strict ? p->route->next : p->route);
06916 }
06917
06918 ot = get_header(orig, "To");
06919 of = get_header(orig, "From");
06920
06921
06922
06923 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
06924
06925
06926 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_strlen_zero(p->theirtag))
06927 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
06928 else if (!ast_test_flag(&p->flags[0], SIP_OUTGOING))
06929 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
06930 else
06931 snprintf(newto, sizeof(newto), "%s", ot);
06932 ot = newto;
06933 }
06934
06935 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06936 add_header(req, "From", of);
06937 add_header(req, "To", ot);
06938 } else {
06939 add_header(req, "From", ot);
06940 add_header(req, "To", of);
06941 }
06942
06943 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
06944 add_header(req, "Contact", p->our_contact);
06945
06946 copy_header(req, orig, "Call-ID");
06947 add_header(req, "CSeq", tmp);
06948
06949 if (!ast_strlen_zero(global_useragent))
06950 add_header(req, "User-Agent", global_useragent);
06951 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
06952
06953 if (!ast_strlen_zero(p->rpid))
06954 add_header(req, "Remote-Party-ID", p->rpid);
06955
06956 return 0;
06957 }
06958
06959
06960 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06961 {
06962 struct sip_request resp;
06963 int seqno = 0;
06964
06965 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
06966 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
06967 return -1;
06968 }
06969 respprep(&resp, p, msg, req);
06970
06971
06972 if (p->method == SIP_INVITE && msg[0] != '1' && p->owner && p->owner->hangupcause) {
06973 char buf[10];
06974
06975 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
06976 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
06977 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
06978 }
06979 return send_response(p, &resp, reliable, seqno);
06980 }
06981
06982 static void temp_pvt_cleanup(void *data)
06983 {
06984 struct sip_pvt *p = data;
06985
06986 ast_string_field_free_memory(p);
06987
06988 free(data);
06989 }
06990
06991
06992 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)
06993 {
06994 struct sip_pvt *p = NULL;
06995
06996 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
06997 ast_log(LOG_NOTICE, "Failed to get temporary pvt\n");
06998 return -1;
06999 }
07000
07001
07002 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
07003 ast_set_flag(&p->flags[0], SIP_NO_HISTORY);
07004 if (ast_string_field_init(p, 512))
07005 return -1;
07006 }
07007
07008
07009 p->method = intended_method;
07010
07011 if (sin) {
07012 p->sa = *sin;
07013 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
07014 p->ourip = __ourip;
07015 } else
07016 p->ourip = __ourip;
07017
07018 p->branch = ast_random();
07019 make_our_tag(p->tag, sizeof(p->tag));
07020 p->ocseq = INITIAL_CSEQ;
07021
07022 if (useglobal_nat && sin) {
07023 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
07024 p->recv = *sin;
07025 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
07026 }
07027 check_via(p, req);
07028
07029 ast_string_field_set(p, fromdomain, default_fromdomain);
07030 build_via(p);
07031 ast_string_field_set(p, callid, callid);
07032
07033
07034 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
07035
07036
07037 ast_string_field_reset_all(p);
07038
07039 return 0;
07040 }
07041
07042
07043 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
07044 {
07045 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
07046 }
07047
07048
07049 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
07050 {
07051 struct sip_request resp;
07052 respprep(&resp, p, msg, req);
07053 append_date(&resp);
07054 add_header(&resp, "Unsupported", unsupported);
07055 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
07056 }
07057
07058
07059
07060
07061 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
07062 {
07063 return __transmit_response(p, msg, req, XMIT_CRITICAL);
07064 }
07065
07066
07067 static void append_date(struct sip_request *req)
07068 {
07069 char tmpdat[256];
07070 struct tm tm;
07071 time_t t = time(NULL);
07072
07073 gmtime_r(&t, &tm);
07074 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
07075 add_header(req, "Date", tmpdat);
07076 }
07077
07078
07079 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
07080 {
07081 struct sip_request resp;
07082 respprep(&resp, p, msg, req);
07083 append_date(&resp);
07084 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
07085 }
07086
07087
07088 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
07089 {
07090 struct sip_request resp;
07091 respprep(&resp, p, msg, req);
07092 add_header(&resp, "Accept", "application/sdp");
07093 return send_response(p, &resp, reliable, 0);
07094 }
07095
07096
07097 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)
07098 {
07099 struct sip_request resp;
07100 char tmp[512];
07101 int seqno = 0;
07102
07103 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
07104 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
07105 return -1;
07106 }
07107
07108
07109 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
07110 respprep(&resp, p, msg, req);
07111 add_header(&resp, header, tmp);
07112 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
07113 return send_response(p, &resp, reliable, seqno);
07114 }
07115
07116
07117 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp)
07118 {
07119 int res;
07120
07121 if (!(res = with_sdp ? transmit_response_with_sdp(p, msg, req, XMIT_UNRELIABLE) : transmit_response(p, msg, req))) {
07122 p->last_provisional = msg;
07123 update_provisional_keepalive(p, with_sdp);
07124 }
07125
07126 return res;
07127 }
07128
07129
07130 static int add_text(struct sip_request *req, const char *text)
07131 {
07132
07133 add_header(req, "Content-Type", "text/plain");
07134 add_content(req, text);
07135 return 0;
07136 }
07137
07138
07139
07140 static int add_digit(struct sip_request *req, char digit, unsigned int duration)
07141 {
07142 char tmp[256];
07143
07144 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
07145 add_header(req, "Content-Type", "application/dtmf-relay");
07146 add_content(req, tmp);
07147 return 0;
07148 }
07149
07150
07151
07152 static int add_vidupdate(struct sip_request *req)
07153 {
07154 const char *xml_is_a_huge_waste_of_space =
07155 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
07156 " <media_control>\r\n"
07157 " <vc_primitive>\r\n"
07158 " <to_encoder>\r\n"
07159 " <picture_fast_update>\r\n"
07160 " </picture_fast_update>\r\n"
07161 " </to_encoder>\r\n"
07162 " </vc_primitive>\r\n"
07163 " </media_control>\r\n";
07164 add_header(req, "Content-Type", "application/media_control+xml");
07165 add_content(req, xml_is_a_huge_waste_of_space);
07166 return 0;
07167 }
07168
07169
07170 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
07171 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
07172 int debug, int *min_packet_size)
07173 {
07174 int rtp_code;
07175 struct ast_format_list fmt;
07176
07177
07178 if (debug)
07179 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
07180 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
07181 return;
07182
07183 if (p->rtp) {
07184 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
07185 fmt = ast_codec_pref_getsize(pref, codec);
07186 } else
07187 return;
07188 ast_build_string(m_buf, m_size, " %d", rtp_code);
07189 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
07190 ast_rtp_lookup_mime_subtype(1, codec,
07191 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
07192 sample_rate);
07193 if (codec == AST_FORMAT_G729A) {
07194
07195 ast_build_string(a_buf, a_size, "a=fmtp:%d annexb=no\r\n", rtp_code);
07196 } else if (codec == AST_FORMAT_G723_1) {
07197
07198 ast_build_string(a_buf, a_size, "a=fmtp:%d annexa=no\r\n", rtp_code);
07199 } else if (codec == AST_FORMAT_ILBC) {
07200
07201 ast_build_string(a_buf, a_size, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
07202 }
07203
07204 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
07205 *min_packet_size = fmt.cur_ms;
07206
07207
07208 if ((*min_packet_size) == 0 && fmt.cur_ms)
07209 *min_packet_size = fmt.cur_ms;
07210 }
07211
07212
07213 static int t38_get_rate(int t38cap)
07214 {
07215 int maxrate = (t38cap & (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400));
07216
07217 if (maxrate & T38FAX_RATE_14400) {
07218 if (option_debug > 1)
07219 ast_log(LOG_DEBUG, "T38MaxBitRate 14400 found\n");
07220 return 14400;
07221 } else if (maxrate & T38FAX_RATE_12000) {
07222 if (option_debug > 1)
07223 ast_log(LOG_DEBUG, "T38MaxBitRate 12000 found\n");
07224 return 12000;
07225 } else if (maxrate & T38FAX_RATE_9600) {
07226 if (option_debug > 1)
07227 ast_log(LOG_DEBUG, "T38MaxBitRate 9600 found\n");
07228 return 9600;
07229 } else if (maxrate & T38FAX_RATE_7200) {
07230 if (option_debug > 1)
07231 ast_log(LOG_DEBUG, "T38MaxBitRate 7200 found\n");
07232 return 7200;
07233 } else if (maxrate & T38FAX_RATE_4800) {
07234 if (option_debug > 1)
07235 ast_log(LOG_DEBUG, "T38MaxBitRate 4800 found\n");
07236 return 4800;
07237 } else if (maxrate & T38FAX_RATE_2400) {
07238 if (option_debug > 1)
07239 ast_log(LOG_DEBUG, "T38MaxBitRate 2400 found\n");
07240 return 2400;
07241 } else {
07242 if (option_debug > 1)
07243 ast_log(LOG_DEBUG, "Strange, T38MaxBitRate NOT found in peers T38 SDP.\n");
07244 return 0;
07245 }
07246 }
07247
07248
07249 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
07250 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
07251 int debug)
07252 {
07253 int rtp_code;
07254
07255 if (debug)
07256 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format, 0));
07257 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
07258 return;
07259
07260 ast_build_string(m_buf, m_size, " %d", rtp_code);
07261 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
07262 ast_rtp_lookup_mime_subtype(0, format, 0),
07263 sample_rate);
07264 if (format == AST_RTP_DTMF)
07265
07266 ast_build_string(a_buf, a_size, "a=fmtp:%d 0-16\r\n", rtp_code);
07267 }
07268
07269
07270
07271
07272
07273
07274 #define SDP_SAMPLE_RATE(x) 8000
07275
07276
07277 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int add_audio, int add_t38)
07278 {
07279 int alreadysent = 0;
07280 int doing_directmedia = FALSE;
07281
07282 struct sockaddr_in sin;
07283 struct sockaddr_in vsin;
07284 struct sockaddr_in dest;
07285 struct sockaddr_in vdest = { 0, };
07286
07287
07288 char *version = "v=0\r\n";
07289 char *subject = "s=session\r\n";
07290 char owner[256];
07291 char connection[256];
07292 char *stime = "t=0 0\r\n";
07293 char bandwidth[256] = "";
07294 char *hold;
07295 char m_audio[256];
07296 char m_video[256];
07297 char m_modem[256];
07298 char a_audio[1024];
07299 char a_video[1024];
07300 char a_modem[1024];
07301 char *m_audio_next = m_audio;
07302 char *m_video_next = m_video;
07303 char *m_modem_next = m_modem;
07304 size_t m_audio_left = sizeof(m_audio);
07305 size_t m_video_left = sizeof(m_video);
07306 size_t m_modem_left = sizeof(m_modem);
07307 char *a_audio_next = a_audio;
07308 char *a_video_next = a_video;
07309 char *a_modem_next = a_modem;
07310 size_t a_audio_left = sizeof(a_audio);
07311 size_t a_video_left = sizeof(a_video);
07312 size_t a_modem_left = sizeof(a_modem);
07313 char dummy_answer[256];
07314
07315 int x;
07316 int capability = 0;
07317 int needvideo = FALSE;
07318 int debug = sip_debug_test_pvt(p);
07319 int min_audio_packet_size = 0;
07320 int min_video_packet_size = 0;
07321
07322 m_video[0] = '\0';
07323 m_modem[0] = '\0';
07324
07325 if (!p->rtp) {
07326 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
07327 return AST_FAILURE;
07328 }
07329
07330
07331 if (!p->sessionid) {
07332 p->sessionid = getpid();
07333 p->sessionversion = p->sessionid;
07334 } else
07335 p->sessionversion++;
07336
07337
07338 ast_rtp_get_us(p->rtp, &sin);
07339 if (p->vrtp)
07340 ast_rtp_get_us(p->vrtp, &vsin);
07341
07342
07343 if (p->redirip.sin_addr.s_addr) {
07344 dest.sin_port = p->redirip.sin_port;
07345 dest.sin_addr = p->redirip.sin_addr;
07346 doing_directmedia = p->redircodecs ? TRUE : FALSE;
07347 } else {
07348 dest.sin_addr = p->ourip;
07349 dest.sin_port = sin.sin_port;
07350 }
07351
07352 snprintf(owner, sizeof(owner), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(dest.sin_addr));
07353 snprintf(connection, sizeof(connection), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
07354
07355 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR)
07356 hold = "a=recvonly\r\n";
07357 else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE)
07358 hold = "a=inactive\r\n";
07359 else
07360 hold = "a=sendrecv\r\n";
07361
07362 if (add_audio) {
07363 char codecbuf[SIPBUFSIZE];
07364 capability = p->jointcapability;
07365
07366 if (option_debug > 1) {
07367 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");
07368 ast_log(LOG_DEBUG, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
07369 }
07370
07371 if (doing_directmedia) {
07372 capability &= p->redircodecs;
07373 if (option_debug > 1) {
07374 ast_log(LOG_NOTICE, "** Our native-bridge filtered capablity: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability));
07375 }
07376 }
07377
07378 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
07379 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_RTP)) {
07380 ast_build_string(&m_audio_next, &m_audio_left, " %d", 191);
07381 ast_build_string(&a_audio_next, &a_audio_left, "a=rtpmap:%d %s/%d\r\n", 191, "t38", 8000);
07382 }
07383 #endif
07384
07385
07386 if ((capability & AST_FORMAT_VIDEO_MASK) && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
07387 if (p->vrtp) {
07388 needvideo = TRUE;
07389 if (option_debug > 1)
07390 ast_log(LOG_DEBUG, "This call needs video offers!\n");
07391 } else if (option_debug > 1)
07392 ast_log(LOG_DEBUG, "This call needs video offers, but there's no video support enabled!\n");
07393 }
07394
07395
07396
07397
07398 if (needvideo) {
07399
07400 if (p->vredirip.sin_addr.s_addr) {
07401 vdest.sin_addr = p->vredirip.sin_addr;
07402 vdest.sin_port = p->vredirip.sin_port;
07403 } else {
07404 vdest.sin_addr = p->ourip;
07405 vdest.sin_port = vsin.sin_port;
07406 }
07407 ast_build_string(&m_video_next, &m_video_left, "m=video %d RTP/AVP", ntohs(vsin.sin_port));
07408
07409
07410 if (p->maxcallbitrate)
07411 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
07412 if (debug)
07413 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(vsin.sin_port));
07414 }
07415
07416 if (debug)
07417 ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(sin.sin_port));
07418
07419 ast_build_string(&m_audio_next, &m_audio_left, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
07420
07421
07422
07423
07424
07425
07426
07427
07428
07429
07430 if (capability & p->prefcodec) {
07431 int codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
07432
07433 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
07434 &m_audio_next, &m_audio_left,
07435 &a_audio_next, &a_audio_left,
07436 debug, &min_audio_packet_size);
07437 alreadysent |= codec;
07438 }
07439
07440
07441 for (x = 0; x < 32; x++) {
07442 int codec;
07443
07444 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
07445 break;
07446
07447 if (!(capability & codec))
07448 continue;
07449
07450 if (alreadysent & codec)
07451 continue;
07452
07453 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
07454 &m_audio_next, &m_audio_left,
07455 &a_audio_next, &a_audio_left,
07456 debug, &min_audio_packet_size);
07457 alreadysent |= codec;
07458 }
07459
07460
07461 for (x = 1; x <= (needvideo ? AST_FORMAT_MAX_VIDEO : AST_FORMAT_MAX_AUDIO); x <<= 1) {
07462 if (!(capability & x))
07463 continue;
07464
07465 if (alreadysent & x)
07466 continue;
07467
07468 if (x <= AST_FORMAT_MAX_AUDIO)
07469 add_codec_to_sdp(p, x, SDP_SAMPLE_RATE(x),
07470 &m_audio_next, &m_audio_left,
07471 &a_audio_next, &a_audio_left,
07472 debug, &min_audio_packet_size);
07473 else
07474 add_codec_to_sdp(p, x, 90000,
07475 &m_video_next, &m_video_left,
07476 &a_video_next, &a_video_left,
07477 debug, &min_video_packet_size);
07478 }
07479
07480
07481 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
07482 if (!(p->jointnoncodeccapability & x))
07483 continue;
07484
07485 add_noncodec_to_sdp(p, x, 8000,
07486 &m_audio_next, &m_audio_left,
07487 &a_audio_next, &a_audio_left,
07488 debug);
07489 }
07490
07491 if (option_debug > 2)
07492 ast_log(LOG_DEBUG, "-- Done with adding codecs to SDP\n");
07493
07494 if (!p->owner || !ast_internal_timing_enabled(p->owner))
07495 ast_build_string(&a_audio_next, &a_audio_left, "a=silenceSupp:off - - - -\r\n");
07496
07497 if (min_audio_packet_size)
07498 ast_build_string(&a_audio_next, &a_audio_left, "a=ptime:%d\r\n", min_audio_packet_size);
07499
07500 if (min_video_packet_size)
07501 ast_build_string(&a_video_next, &a_video_left, "a=ptime:%d\r\n", min_video_packet_size);
07502
07503 if ((m_audio_left < 2) || (m_video_left < 2) || (a_audio_left == 0) || (a_video_left == 0))
07504 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
07505
07506 ast_build_string(&m_audio_next, &m_audio_left, "\r\n");
07507 if (needvideo)
07508 ast_build_string(&m_video_next, &m_video_left, "\r\n");
07509 }
07510
07511 if (add_t38 && p->udptl) {
07512 struct sockaddr_in udptlsin;
07513 struct sockaddr_in udptldest = { 0, };
07514
07515 ast_udptl_get_us(p->udptl, &udptlsin);
07516
07517 if (p->udptlredirip.sin_addr.s_addr) {
07518 udptldest.sin_port = p->udptlredirip.sin_port;
07519 udptldest.sin_addr = p->udptlredirip.sin_addr;
07520 } else {
07521 udptldest.sin_addr = p->ourip;
07522 udptldest.sin_port = udptlsin.sin_port;
07523 }
07524
07525 if (debug) {
07526 ast_log(LOG_DEBUG, "T.38 UDPTL is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(udptlsin.sin_port));
07527 ast_log(LOG_DEBUG, "Our T38 capability (%d), peer T38 capability (%d), joint capability (%d)\n",
07528 p->t38.capability,
07529 p->t38.peercapability,
07530 p->t38.jointcapability);
07531 }
07532
07533 ast_build_string(&m_modem_next, &m_modem_left, "m=image %d udptl t38\r\n", ntohs(udptldest.sin_port));
07534
07535 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_0)
07536 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:0\r\n");
07537 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_1)
07538 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:1\r\n");
07539 if ((x = t38_get_rate(p->t38.jointcapability)))
07540 ast_build_string(&a_modem_next, &a_modem_left, "a=T38MaxBitRate:%d\r\n",x);
07541 if ((p->t38.jointcapability & T38FAX_FILL_BIT_REMOVAL) == T38FAX_FILL_BIT_REMOVAL)
07542 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxFillBitRemoval\r\n");
07543 if ((p->t38.jointcapability & T38FAX_TRANSCODING_MMR) == T38FAX_TRANSCODING_MMR)
07544 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingMMR\r\n");
07545 if ((p->t38.jointcapability & T38FAX_TRANSCODING_JBIG) == T38FAX_TRANSCODING_JBIG)
07546 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingJBIG\r\n");
07547 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxRateManagement:%s\r\n", (p->t38.jointcapability & T38FAX_RATE_MANAGEMENT_LOCAL_TCF) ? "localTCF" : "transferredTCF");
07548 x = ast_udptl_get_local_max_datagram(p->udptl);
07549 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxBuffer:%d\r\n",x);
07550 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxDatagram:%d\r\n",x);
07551 if (p->t38.jointcapability != T38FAX_UDP_EC_NONE)
07552 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxUdpEC:%s\r\n", (p->t38.jointcapability & T38FAX_UDP_EC_REDUNDANCY) ? "t38UDPRedundancy" : "t38UDPFEC");
07553 }
07554
07555 add_header(resp, "Content-Type", "application/sdp");
07556 add_content(resp, version);
07557 add_content(resp, owner);
07558 add_content(resp, subject);
07559 add_content(resp, connection);
07560 if (needvideo)
07561 add_content(resp, bandwidth);
07562 add_content(resp, stime);
07563 if (add_audio) {
07564 add_content(resp, m_audio);
07565 add_content(resp, a_audio);
07566 add_content(resp, hold);
07567 } else if (p->offered_media[SDP_AUDIO].offered) {
07568 snprintf(dummy_answer, sizeof(dummy_answer), "m=audio 0 RTP/AVP %s\r\n", p->offered_media[SDP_AUDIO].text);
07569 add_content(resp, dummy_answer);
07570 }
07571 if (needvideo) {
07572 add_content(resp, m_video);
07573 add_content(resp, a_video);
07574 add_content(resp, hold);
07575 } else if (p->offered_media[SDP_VIDEO].offered) {
07576 snprintf(dummy_answer, sizeof(dummy_answer), "m=video 0 RTP/AVP %s\r\n", p->offered_media[SDP_VIDEO].text);
07577 add_content(resp, dummy_answer);
07578 }
07579 if (add_t38) {
07580 add_content(resp, m_modem);
07581 add_content(resp, a_modem);
07582 } else if (p->offered_media[SDP_IMAGE].offered) {
07583 add_content(resp, "m=image 0 udptl t38\r\n");
07584 }
07585
07586
07587 p->lastrtprx = p->lastrtptx = time(NULL);
07588
07589 if (option_debug > 2) {
07590 char buf[SIPBUFSIZE];
07591 ast_log(LOG_DEBUG, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
07592 }
07593
07594 return AST_SUCCESS;
07595 }
07596
07597
07598 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
07599 {
07600 struct sip_request resp;
07601 int seqno;
07602
07603 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
07604 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
07605 return -1;
07606 }
07607 respprep(&resp, p, msg, req);
07608 if (p->udptl) {
07609 add_sdp(&resp, p, 0, 1);
07610 } else
07611 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
07612 if (retrans && !p->pendinginvite)
07613 p->pendinginvite = seqno;
07614 return send_response(p, &resp, retrans, seqno);
07615 }
07616
07617
07618 static void copy_request(struct sip_request *dst, const struct sip_request *src)
07619 {
07620 long offset;
07621 int x;
07622 offset = ((void *)dst) - ((void *)src);
07623
07624 memcpy(dst, src, sizeof(*dst));
07625
07626 for (x=0; x < src->headers; x++)
07627 dst->header[x] += offset;
07628 for (x=0; x < src->lines; x++)
07629 dst->line[x] += offset;
07630 dst->rlPart1 += offset;
07631 dst->rlPart2 += offset;
07632 }
07633
07634
07635
07636
07637 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
07638 {
07639 struct sip_request resp;
07640 int seqno;
07641 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
07642 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
07643 return -1;
07644 }
07645 respprep(&resp, p, msg, req);
07646 if (p->rtp) {
07647 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
07648 if (option_debug)
07649 ast_log(LOG_DEBUG, "Setting framing from config on incoming call\n");
07650 ast_rtp_codec_setpref(p->rtp, &p->prefs);
07651 }
07652 try_suggested_sip_codec(p);
07653 if (p->t38.state == T38_PEER_DIRECT || p->t38.state == T38_ENABLED) {
07654 p->t38.state = T38_ENABLED;
07655 add_sdp(&resp, p, 1, 1);
07656 } else {
07657 add_sdp(&resp, p, 1, 0);
07658 }
07659 } else
07660 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
07661 if (reliable && !p->pendinginvite)
07662 p->pendinginvite = seqno;
07663 return send_response(p, &resp, reliable, seqno);
07664 }
07665
07666
07667 static int determine_firstline_parts(struct sip_request *req)
07668 {
07669 char *e = ast_skip_blanks(req->header[0]);
07670
07671 if (!*e)
07672 return -1;
07673 req->rlPart1 = e;
07674 e = ast_skip_nonblanks(e);
07675 if (*e)
07676 *e++ = '\0';
07677
07678 e = ast_skip_blanks(e);
07679 if ( !*e )
07680 return -1;
07681 ast_trim_blanks(e);
07682
07683 if (!strcasecmp(req->rlPart1, "SIP/2.0") ) {
07684 if (strlen(e) < 3)
07685 return -1;
07686 req->rlPart2 = e;
07687 } else {
07688 if ( *e == '<' ) {
07689 ast_log(LOG_WARNING, "bogus uri in <> %s\n", e);
07690 e++;
07691 if (!*e)
07692 return -1;
07693 }
07694 req->rlPart2 = e;
07695 e = ast_skip_nonblanks(e);
07696 if (*e)
07697 *e++ = '\0';
07698 e = ast_skip_blanks(e);
07699 if (strcasecmp(e, "SIP/2.0") ) {
07700 ast_log(LOG_WARNING, "Bad request protocol %s\n", e);
07701 return -1;
07702 }
07703 }
07704 return 1;
07705 }
07706
07707
07708
07709
07710
07711
07712
07713 static int transmit_reinvite_with_sdp(struct sip_pvt *p)
07714 {
07715 struct sip_request req;
07716
07717 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
07718
07719 add_header(&req, "Allow", ALLOWED_METHODS);
07720 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07721 if (sipdebug)
07722 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
07723 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
07724 append_history(p, "ReInv", "Re-invite sent");
07725 memset(p->offered_media, 0, sizeof(p->offered_media));
07726 add_sdp(&req, p, 1, 0);
07727
07728 initialize_initreq(p, &req);
07729 p->lastinvite = p->ocseq;
07730 ast_set_flag(&p->flags[0], SIP_OUTGOING);
07731 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
07732 }
07733
07734
07735
07736
07737
07738 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p)
07739 {
07740 struct sip_request req;
07741
07742 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
07743
07744 add_header(&req, "Allow", ALLOWED_METHODS);
07745 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07746 if (sipdebug)
07747 add_header(&req, "X-asterisk-info", "SIP re-invite (T38 switchover)");
07748 memset(p->offered_media, 0, sizeof(p->offered_media));
07749 add_sdp(&req, p, 0, 1);
07750
07751
07752 initialize_initreq(p, &req);
07753 ast_set_flag(&p->flags[0], SIP_OUTGOING);
07754 p->lastinvite = p->ocseq;
07755 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
07756 }
07757
07758
07759 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
07760 {
07761 char stripped[SIPBUFSIZE];
07762 char *c;
07763
07764 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
07765 c = get_in_brackets(stripped);
07766 c = strsep(&c, ";");
07767 if (!ast_strlen_zero(c))
07768 ast_string_field_set(p, uri, c);
07769 }
07770
07771
07772 static void build_contact(struct sip_pvt *p)
07773 {
07774 char tmp[SIPBUFSIZE];
07775 char *user;
07776
07777 user = ast_uri_encode(p->exten, tmp, sizeof(tmp), 1);
07778
07779
07780 if (ourport != STANDARD_SIP_PORT)
07781 ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d>", user, ast_strlen_zero(user) ? "" : "@", ast_inet_ntoa(p->ourip), ourport);
07782 else
07783 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", user, ast_strlen_zero(user) ? "" : "@", ast_inet_ntoa(p->ourip));
07784 }
07785
07786
07787 static void build_rpid(struct sip_pvt *p)
07788 {
07789 int send_pres_tags = TRUE;
07790 const char *privacy=NULL;
07791 const char *screen=NULL;
07792 char buf[256];
07793 const char *clid = default_callerid;
07794 const char *clin = NULL;
07795 const char *fromdomain;
07796
07797 if (!ast_strlen_zero(p->rpid) || !ast_strlen_zero(p->rpid_from))
07798 return;
07799
07800 if (p->owner && !ast_strlen_zero(p->owner->cid.cid_num))
07801 clid = p->owner->cid.cid_num;
07802 if (p->owner && p->owner->cid.cid_name)
07803 clin = p->owner->cid.cid_name;
07804 if (ast_strlen_zero(clin))
07805 clin = clid;
07806
07807 switch (p->callingpres) {
07808 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
07809 privacy = "off";
07810 screen = "no";
07811 break;
07812 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
07813 privacy = "off";
07814 screen = "yes";
07815 break;
07816 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
07817 privacy = "off";
07818 screen = "no";
07819 break;
07820 case AST_PRES_ALLOWED_NETWORK_NUMBER:
07821 privacy = "off";
07822 screen = "yes";
07823 break;
07824 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
07825 privacy = "full";
07826 screen = "no";
07827 break;
07828 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
07829 privacy = "full";
07830 screen = "yes";
07831 break;
07832 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
07833 privacy = "full";
07834 screen = "no";
07835 break;
07836 case AST_PRES_PROHIB_NETWORK_NUMBER:
07837 privacy = "full";
07838 screen = "yes";
07839 break;
07840 case AST_PRES_NUMBER_NOT_AVAILABLE:
07841 send_pres_tags = FALSE;
07842 break;
07843 default:
07844 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
07845 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
07846 privacy = "full";
07847 else
07848 privacy = "off";
07849 screen = "no";
07850 break;
07851 }
07852
07853 fromdomain = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip));
07854
07855 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
07856 if (send_pres_tags)
07857 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
07858 ast_string_field_set(p, rpid, buf);
07859
07860 ast_string_field_build(p, rpid_from, "\"%s\" <sip:%s@%s>;tag=%s", clin,
07861 S_OR(p->fromuser, clid),
07862 fromdomain, p->tag);
07863 }
07864
07865
07866 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
07867 {
07868 char invite_buf[256] = "";
07869 char *invite = invite_buf;
07870 size_t invite_max = sizeof(invite_buf);
07871 char from[256];
07872 char to[256];
07873 char tmp[SIPBUFSIZE/2];
07874 char tmp2[SIPBUFSIZE/2];
07875 const char *l = NULL, *n = NULL, *d = NULL;
07876 const char *urioptions = "";
07877
07878 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
07879 const char *s = p->username;
07880
07881
07882
07883
07884
07885
07886 if (*s == '+')
07887 s++;
07888 for (; *s; s++) {
07889 if (!strchr(AST_DIGIT_ANYNUM, *s) )
07890 break;
07891 }
07892
07893 if (!*s)
07894 urioptions = ";user=phone";
07895 }
07896
07897
07898 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
07899
07900 d = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip));
07901 if (p->owner) {
07902 l = p->owner->cid.cid_num;
07903 n = p->owner->cid.cid_name;
07904 }
07905
07906 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID) &&
07907 ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
07908 l = CALLERID_UNKNOWN;
07909 n = l;
07910 d = FROMDOMAIN_INVALID;
07911 }
07912 if (ast_strlen_zero(l))
07913 l = default_callerid;
07914 if (ast_strlen_zero(n))
07915 n = l;
07916
07917 if (!ast_strlen_zero(p->fromuser))
07918 l = p->fromuser;
07919 else
07920 ast_string_field_set(p, fromuser, l);
07921
07922
07923 if (!ast_strlen_zero(p->fromname))
07924 n = p->fromname;
07925 else
07926 ast_string_field_set(p, fromname, n);
07927
07928 if (pedanticsipchecking) {
07929 ast_uri_encode(n, tmp, sizeof(tmp), 0);
07930 n = tmp;
07931 ast_uri_encode(l, tmp2, sizeof(tmp2), 0);
07932 l = tmp2;
07933 }
07934
07935 if (ourport != STANDARD_SIP_PORT && ast_strlen_zero(p->fromdomain))
07936 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, d, ourport, p->tag);
07937 else
07938 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, d, p->tag);
07939
07940
07941 if (!ast_strlen_zero(p->fullcontact)) {
07942
07943 ast_build_string(&invite, &invite_max, "%s", p->fullcontact);
07944 } else {
07945
07946 ast_build_string(&invite, &invite_max, "sip:");
07947 if (!ast_strlen_zero(p->username)) {
07948 n = p->username;
07949 if (pedanticsipchecking) {
07950 ast_uri_encode(n, tmp, sizeof(tmp), 0);
07951 n = tmp;
07952 }
07953 ast_build_string(&invite, &invite_max, "%s@", n);
07954 }
07955 ast_build_string(&invite, &invite_max, "%s", p->tohost);
07956 if (p->portinuri)
07957 ast_build_string(&invite, &invite_max, ":%d", ntohs(p->sa.sin_port));
07958 ast_build_string(&invite, &invite_max, "%s", urioptions);
07959 }
07960
07961
07962 if (p->options && !ast_strlen_zero(p->options->uri_options))
07963 ast_build_string(&invite, &invite_max, ";%s", p->options->uri_options);
07964
07965 ast_string_field_set(p, uri, invite_buf);
07966
07967 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
07968
07969 snprintf(to, sizeof(to), "<%s%s>;tag=%s", (!strncasecmp(p->uri, "sip:", 4) ? "" : "sip:"), p->uri, p->theirtag);
07970 } else if (p->options && p->options->vxml_url) {
07971
07972 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
07973 } else
07974 snprintf(to, sizeof(to), "<%s>", p->uri);
07975
07976 init_req(req, sipmethod, p->uri);
07977 snprintf(tmp, sizeof(tmp), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
07978
07979 add_header(req, "Via", p->via);
07980
07981
07982
07983
07984 add_route(req, p->route);
07985
07986 if (ast_test_flag(&p->flags[0], SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
07987 build_rpid(p);
07988 add_header(req, "From", p->rpid_from);
07989 } else
07990 add_header(req, "From", from);
07991 add_header(req, "To", to);
07992 ast_string_field_set(p, exten, l);
07993 build_contact(p);
07994 add_header(req, "Contact", p->our_contact);
07995 add_header(req, "Call-ID", p->callid);
07996 add_header(req, "CSeq", tmp);
07997 if (!ast_strlen_zero(global_useragent))
07998 add_header(req, "User-Agent", global_useragent);
07999 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
08000 if (!ast_strlen_zero(p->rpid))
08001 add_header(req, "Remote-Party-ID", p->rpid);
08002 }
08003
08004
08005 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
08006 {
08007 struct sip_request req;
08008
08009 req.method = sipmethod;
08010 if (init) {
08011
08012 p->branch ^= ast_random();
08013 p->invite_branch = p->branch;
08014 build_via(p);
08015 if (init > 1)
08016 initreqprep(&req, p, sipmethod);
08017 else
08018 reqprep(&req, p, sipmethod, 0, 0);
08019 } else
08020 reqprep(&req, p, sipmethod, 0, 1);
08021
08022 if (p->options && p->options->auth)
08023 add_header(&req, p->options->authheader, p->options->auth);
08024 append_date(&req);
08025 if (sipmethod == SIP_REFER) {
08026 if (p->refer) {
08027 char buf[SIPBUFSIZE];
08028 if (!ast_strlen_zero(p->refer->refer_to))
08029 add_header(&req, "Refer-To", p->refer->refer_to);
08030 if (!ast_strlen_zero(p->refer->referred_by)) {
08031 snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
08032 add_header(&req, "Referred-By", buf);
08033 }
08034 }
08035 }
08036
08037
08038 if (p->options && p->options->replaces && !ast_strlen_zero(p->options->replaces)) {
08039 add_header(&req, "Replaces", p->options->replaces);
08040 add_header(&req, "Require", "replaces");
08041 }
08042
08043 add_header(&req, "Allow", ALLOWED_METHODS);
08044 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
08045 if (p->options && p->options->addsipheaders && p->owner) {
08046 struct ast_channel *chan = p->owner;
08047 struct varshead *headp;
08048
08049 ast_channel_lock(chan);
08050
08051 headp = &chan->varshead;
08052
08053 if (!headp)
08054 ast_log(LOG_WARNING,"No Headp for the channel...ooops!\n");
08055 else {
08056 const struct ast_var_t *current;
08057 AST_LIST_TRAVERSE(headp, current, entries) {
08058
08059 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
08060 char *content, *end;
08061 const char *header = ast_var_value(current);
08062 char *headdup = ast_strdupa(header);
08063
08064
08065 if (*headdup == '"')
08066 headdup++;
08067 if ((content = strchr(headdup, ':'))) {
08068 *content++ = '\0';
08069 content = ast_skip_blanks(content);
08070
08071 end = content + strlen(content) -1;
08072 if (*end == '"')
08073 *end = '\0';
08074
08075 add_header(&req, headdup, content);
08076 if (sipdebug)
08077 ast_log(LOG_DEBUG, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
08078 }
08079 }
08080 }
08081 }
08082
08083 ast_channel_unlock(chan);
08084 }
08085 if (sdp) {
08086 memset(p->offered_media, 0, sizeof(p->offered_media));
08087 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
08088 ast_udptl_offered_from_local(p->udptl, 1);
08089 if (option_debug)
08090 ast_log(LOG_DEBUG, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
08091 add_sdp(&req, p, 0, 1);
08092 } else if (p->rtp)
08093 add_sdp(&req, p, 1, 0);
08094 }
08095
08096 if (!p->initreq.headers || init > 2)
08097 initialize_initreq(p, &req);
08098 p->lastinvite = p->ocseq;
08099 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
08100 }
08101
08102
08103 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
08104 {
08105 char tmp[4000], from[256], to[256];
08106 char *t = tmp, *c, *mfrom, *mto;
08107 size_t maxbytes = sizeof(tmp);
08108 struct sip_request req;
08109 char hint[AST_MAX_EXTENSION];
08110 char *statestring = "terminated";
08111 const struct cfsubscription_types *subscriptiontype;
08112 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
08113 char *pidfstate = "--";
08114 char *pidfnote= "Ready";
08115
08116 memset(from, 0, sizeof(from));
08117 memset(to, 0, sizeof(to));
08118 memset(tmp, 0, sizeof(tmp));
08119
08120 switch (state) {
08121 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
08122 statestring = (global_notifyringing) ? "early" : "confirmed";
08123 local_state = NOTIFY_INUSE;
08124 pidfstate = "busy";
08125 pidfnote = "Ringing";
08126 break;
08127 case AST_EXTENSION_RINGING:
08128 statestring = "early";
08129 local_state = NOTIFY_INUSE;
08130 pidfstate = "busy";
08131 pidfnote = "Ringing";
08132 break;
08133 case AST_EXTENSION_INUSE:
08134 statestring = "confirmed";
08135 local_state = NOTIFY_INUSE;
08136 pidfstate = "busy";
08137 pidfnote = "On the phone";
08138 break;
08139 case AST_EXTENSION_BUSY:
08140 statestring = "confirmed";
08141 local_state = NOTIFY_CLOSED;
08142 pidfstate = "busy";
08143 pidfnote = "On the phone";
08144 break;
08145 case AST_EXTENSION_UNAVAILABLE:
08146 statestring = "terminated";
08147 local_state = NOTIFY_CLOSED;
08148 pidfstate = "away";
08149 pidfnote = "Unavailable";
08150 break;
08151 case AST_EXTENSION_ONHOLD:
08152 statestring = "confirmed";
08153 local_state = NOTIFY_CLOSED;
08154 pidfstate = "busy";
08155 pidfnote = "On Hold";
08156 break;
08157 case AST_EXTENSION_NOT_INUSE:
08158 default:
08159
08160 break;
08161 }
08162
08163 subscriptiontype = find_subscription_type(p->subscribed);
08164
08165
08166 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
08167 char *hint2 = hint, *individual_hint = NULL;
08168 int hint_count = 0, unavailable_count = 0;
08169
08170 while ((individual_hint = strsep(&hint2, "&"))) {
08171 hint_count++;
08172
08173 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE)
08174 unavailable_count++;
08175 }
08176
08177
08178
08179
08180 if (hint_count > 0 && hint_count == unavailable_count) {
08181 local_state = NOTIFY_CLOSED;
08182 pidfstate = "away";
08183 pidfnote = "Not online";
08184 }
08185 }
08186
08187 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
08188 c = get_in_brackets(from);
08189 if (strncasecmp(c, "sip:", 4)) {
08190 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
08191 return -1;
08192 }
08193 mfrom = strsep(&c, ";");
08194
08195 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
08196 c = get_in_brackets(to);
08197 if (strncasecmp(c, "sip:", 4)) {
08198 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
08199 return -1;
08200 }
08201 mto = strsep(&c, ";");
08202
08203 reqprep(&req, p, SIP_NOTIFY, 0, 1);
08204
08205
08206 add_header(&req, "Event", subscriptiontype->event);
08207 add_header(&req, "Content-Type", subscriptiontype->mediatype);
08208 switch(state) {
08209 case AST_EXTENSION_DEACTIVATED:
08210 if (timeout)
08211 add_header(&req, "Subscription-State", "terminated;reason=timeout");
08212 else {
08213 add_header(&req, "Subscription-State", "terminated;reason=probation");
08214 add_header(&req, "Retry-After", "60");
08215 }
08216 break;
08217 case AST_EXTENSION_REMOVED:
08218 add_header(&req, "Subscription-State", "terminated;reason=noresource");
08219 break;
08220 default:
08221 if (p->expiry)
08222 add_header(&req, "Subscription-State", "active");
08223 else
08224 add_header(&req, "Subscription-State", "terminated;reason=timeout");
08225 }
08226 switch (p->subscribed) {
08227 case XPIDF_XML:
08228 case CPIM_PIDF_XML:
08229 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
08230 ast_build_string(&t, &maxbytes, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n");
08231 ast_build_string(&t, &maxbytes, "<presence>\n");
08232 ast_build_string(&t, &maxbytes, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
08233 ast_build_string(&t, &maxbytes, "<atom id=\"%s\">\n", p->exten);
08234 ast_build_string(&t, &maxbytes, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
08235 ast_build_string(&t, &maxbytes, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
08236 ast_build_string(&t, &maxbytes, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
08237 ast_build_string(&t, &maxbytes, "</address>\n</atom>\n</presence>\n");
08238 break;
08239 case PIDF_XML:
08240 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
08241 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);
08242 ast_build_string(&t, &maxbytes, "<pp:person><status>\n");
08243 if (pidfstate[0] != '-')
08244 ast_build_string(&t, &maxbytes, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
08245 ast_build_string(&t, &maxbytes, "</status></pp:person>\n");
08246 ast_build_string(&t, &maxbytes, "<note>%s</note>\n", pidfnote);
08247 ast_build_string(&t, &maxbytes, "<tuple id=\"%s\">\n", p->exten);
08248 ast_build_string(&t, &maxbytes, "<contact priority=\"1\">%s</contact>\n", mto);
08249 if (pidfstate[0] == 'b')
08250 ast_build_string(&t, &maxbytes, "<status><basic>open</basic></status>\n");
08251 else
08252 ast_build_string(&t, &maxbytes, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
08253 ast_build_string(&t, &maxbytes, "</tuple>\n</presence>\n");
08254 break;
08255 case DIALOG_INFO_XML:
08256 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
08257 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);
08258 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
08259 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
08260 else
08261 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\">\n", p->exten);
08262 ast_build_string(&t, &maxbytes, "<state>%s</state>\n", statestring);
08263 if (state == AST_EXTENSION_ONHOLD) {
08264 ast_build_string(&t, &maxbytes, "<local>\n<target uri=\"%s\">\n"
08265 "<param pname=\"+sip.rendering\" pvalue=\"no\"/>\n"
08266 "</target>\n</local>\n", mto);
08267 }
08268 ast_build_string(&t, &maxbytes, "</dialog>\n</dialog-info>\n");
08269 break;
08270 case NONE:
08271 default:
08272 break;
08273 }
08274
08275 if (t > tmp + sizeof(tmp))
08276 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
08277
08278 add_content(&req, tmp);
08279 p->pendinginvite = p->ocseq;
08280
08281 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08282 }
08283
08284
08285
08286
08287
08288
08289
08290 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
08291 {
08292 struct sip_request req;
08293 char tmp[500];
08294 char *t = tmp;
08295 size_t maxbytes = sizeof(tmp);
08296
08297 initreqprep(&req, p, SIP_NOTIFY);
08298 add_header(&req, "Event", "message-summary");
08299 add_header(&req, "Content-Type", default_notifymime);
08300
08301 ast_build_string(&t, &maxbytes, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
08302
08303
08304
08305 if ((ourport != STANDARD_SIP_PORT) && ast_strlen_zero(p->fromdomain)) {
08306 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s:%d\r\n",
08307 S_OR(vmexten, default_vmexten), ast_inet_ntoa(p->ourip), ourport);
08308 } else {
08309 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n",
08310 S_OR(vmexten, default_vmexten), S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)));
08311 }
08312
08313
08314
08315 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)"));
08316
08317 if (p->subscribed) {
08318 if (p->expiry)
08319 add_header(&req, "Subscription-State", "active");
08320 else
08321 add_header(&req, "Subscription-State", "terminated;reason=timeout");
08322 }
08323
08324 if (t > tmp + sizeof(tmp))
08325 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
08326
08327 add_content(&req, tmp);
08328
08329 if (!p->initreq.headers)
08330 initialize_initreq(p, &req);
08331 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08332 }
08333
08334
08335 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req)
08336 {
08337 if (!p->initreq.headers)
08338 initialize_initreq(p, req);
08339 return send_request(p, req, XMIT_UNRELIABLE, p->ocseq);
08340 }
08341
08342
08343 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
08344 {
08345 struct sip_request req;
08346 char tmp[SIPBUFSIZE/2];
08347
08348 reqprep(&req, p, SIP_NOTIFY, 0, 1);
08349 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
08350 add_header(&req, "Event", tmp);
08351 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
08352 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
08353 add_header(&req, "Allow", ALLOWED_METHODS);
08354 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
08355
08356 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
08357 add_content(&req, tmp);
08358
08359 if (!p->initreq.headers)
08360 initialize_initreq(p, &req);
08361
08362 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08363 }
08364
08365
08366 static char *regstate2str(enum sipregistrystate regstate)
08367 {
08368 switch(regstate) {
08369 case REG_STATE_FAILED:
08370 return "Failed";
08371 case REG_STATE_UNREGISTERED:
08372 return "Unregistered";
08373 case REG_STATE_REGSENT:
08374 return "Request Sent";
08375 case REG_STATE_AUTHSENT:
08376 return "Auth. Sent";
08377 case REG_STATE_REGISTERED:
08378 return "Registered";
08379 case REG_STATE_REJECTED:
08380 return "Rejected";
08381 case REG_STATE_TIMEOUT:
08382 return "Timeout";
08383 case REG_STATE_NOAUTH:
08384 return "No Authentication";
08385 default:
08386 return "Unknown";
08387 }
08388 }
08389
08390
08391 static int sip_reregister(const void *data)
08392 {
08393
08394 struct sip_registry *r= ASTOBJ_REF((struct sip_registry *) data);
08395
08396
08397 if (!r)
08398 return 0;
08399
08400 if (r->call && !ast_test_flag(&r->call->flags[0], SIP_NO_HISTORY))
08401 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
08402
08403
08404 if (sipdebug)
08405 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
08406
08407 r->expire = -1;
08408 __sip_do_register(r);
08409 ASTOBJ_UNREF(r, sip_registry_destroy);
08410 return 0;
08411 }
08412
08413
08414 static int __sip_do_register(struct sip_registry *r)
08415 {
08416 int res;
08417
08418 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
08419 return res;
08420 }
08421
08422
08423 static int sip_reg_timeout(const void *data)
08424 {
08425
08426
08427 struct sip_registry *r = ASTOBJ_REF((struct sip_registry *) data);
08428 struct sip_pvt *p;
08429 int res;
08430
08431
08432 if (!r)
08433 return 0;
08434
08435 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
08436 if (r->call) {
08437
08438
08439 p = r->call;
08440 ast_mutex_lock(&p->lock);
08441 if (p->registry)
08442 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
08443 r->call = NULL;
08444 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
08445
08446 __sip_pretend_ack(p);
08447 ast_mutex_unlock(&p->lock);
08448 }
08449
08450 if (global_regattempts_max && (r->regattempts > global_regattempts_max)) {
08451
08452
08453
08454 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
08455 r->regstate = REG_STATE_FAILED;
08456 } else {
08457 r->regstate = REG_STATE_UNREGISTERED;
08458 r->timeout = -1;
08459 r->needdns = TRUE;
08460 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
08461 }
08462 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));
08463 ASTOBJ_UNREF(r, sip_registry_destroy);
08464 return 0;
08465 }
08466
08467
08468 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
08469 {
08470 struct sip_request req;
08471 char from[256];
08472 char to[256];
08473 char tmp[80];
08474 char addr[80];
08475 struct sip_pvt *p;
08476 char *fromdomain;
08477
08478
08479 if ( r == NULL || ((auth==NULL) && (r->regstate==REG_STATE_REGSENT || r->regstate==REG_STATE_AUTHSENT))) {
08480 if (r) {
08481 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
08482 }
08483 return 0;
08484 }
08485
08486 if (r->call) {
08487 if (!auth) {
08488 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
08489 return 0;
08490 } else {
08491 p = r->call;
08492 make_our_tag(p->tag, sizeof(p->tag));
08493 ast_string_field_free(p, theirtag);
08494 }
08495 } else {
08496
08497 if (!r->callid_valid) {
08498 build_callid_registry(r, __ourip, default_fromdomain);
08499 r->callid_valid = TRUE;
08500 }
08501
08502 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER))) {
08503 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
08504 return 0;
08505 }
08506 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
08507 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
08508
08509
08510 if (create_addr(p, r->hostname, r->needdns ? NULL : &r->us)) {
08511
08512
08513 sip_destroy(p);
08514
08515 if (r->timeout > -1)
08516 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
08517 else
08518 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);
08519
08520 AST_SCHED_DEL(sched, r->timeout);
08521 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
08522 r->regattempts++;
08523 return 0;
08524 }
08525 if (r->needdns) {
08526 memcpy(&r->us, &p->sa, sizeof(r->us));
08527 }
08528 r->needdns = FALSE;
08529
08530 ast_string_field_set(r, callid, p->callid);
08531 if (r->portno) {
08532 p->sa.sin_port = htons(r->portno);
08533 p->recv.sin_port = htons(r->portno);
08534 } else
08535 r->portno = ntohs(p->sa.sin_port);
08536 ast_set_flag(&p->flags[0], SIP_OUTGOING);
08537 r->call=p;
08538 p->registry = ASTOBJ_REF(r);
08539 if (!ast_strlen_zero(r->secret))
08540 ast_string_field_set(p, peersecret, r->secret);
08541 if (!ast_strlen_zero(r->md5secret))
08542 ast_string_field_set(p, peermd5secret, r->md5secret);
08543
08544
08545 if (!ast_strlen_zero(r->authuser)) {
08546 ast_string_field_set(p, peername, r->authuser);
08547 ast_string_field_set(p, authname, r->authuser);
08548 } else if (!ast_strlen_zero(r->username)) {
08549 ast_string_field_set(p, peername, r->username);
08550 ast_string_field_set(p, authname, r->username);
08551 ast_string_field_set(p, fromuser, r->username);
08552 }
08553 if (!ast_strlen_zero(r->username))
08554 ast_string_field_set(p, username, r->username);
08555
08556 ast_string_field_set(p, exten, r->contact);
08557
08558
08559
08560
08561
08562
08563 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
08564 p->ourip = bindaddr.sin_addr;
08565 build_contact(p);
08566 }
08567
08568
08569 if (auth == NULL) {
08570 if (r->timeout > -1)
08571 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
08572 AST_SCHED_DEL(sched, r->timeout);
08573 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
08574 if (option_debug)
08575 ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
08576 }
08577
08578 if ((fromdomain = strchr(r->username, '@'))) {
08579
08580 fromdomain++ ;
08581
08582 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
08583 if (!ast_strlen_zero(p->theirtag))
08584 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
08585 else
08586 snprintf(to, sizeof(to), "<sip:%s>", r->username);
08587
08588
08589
08590 if (ast_strlen_zero(p->fromdomain)) {
08591 ast_string_field_set(p, fromdomain, fromdomain);
08592 }
08593 } else {
08594 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
08595 if (!ast_strlen_zero(p->theirtag))
08596 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
08597 else
08598 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
08599 }
08600
08601
08602
08603 if (!ast_strlen_zero(p->fromdomain)) {
08604 if (r->portno && r->portno != STANDARD_SIP_PORT)
08605 snprintf(addr, sizeof(addr), "sip:%s:%d", p->fromdomain, r->portno);
08606 else
08607 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
08608 } else {
08609 if (r->portno && r->portno != STANDARD_SIP_PORT)
08610 snprintf(addr, sizeof(addr), "sip:%s:%d", r->hostname, r->portno);
08611 else
08612 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
08613 }
08614 ast_string_field_set(p, uri, addr);
08615
08616 p->branch ^= ast_random();
08617
08618 init_req(&req, sipmethod, addr);
08619
08620
08621 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
08622 p->ocseq = r->ocseq;
08623
08624 build_via(p);
08625 add_header(&req, "Via", p->via);
08626 add_header(&req, "From", from);
08627 add_header(&req, "To", to);
08628 add_header(&req, "Call-ID", p->callid);
08629 add_header(&req, "CSeq", tmp);
08630 if (!ast_strlen_zero(global_useragent))
08631 add_header(&req, "User-Agent", global_useragent);
08632 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
08633
08634
08635 if (auth)
08636 add_header(&req, authheader, auth);
08637 else if (!ast_strlen_zero(r->nonce)) {
08638 char digest[1024];
08639
08640
08641 if (sipdebug)
08642 ast_log(LOG_DEBUG, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
08643 ast_string_field_set(p, realm, r->realm);
08644 ast_string_field_set(p, nonce, r->nonce);
08645 ast_string_field_set(p, domain, r->domain);
08646 ast_string_field_set(p, opaque, r->opaque);
08647 ast_string_field_set(p, qop, r->qop);
08648 r->noncecount++;
08649 p->noncecount = r->noncecount;
08650
08651 memset(digest,0,sizeof(digest));
08652 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
08653 add_header(&req, "Authorization", digest);
08654 else
08655 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
08656
08657 }
08658
08659 snprintf(tmp, sizeof(tmp), "%d", default_expiry);
08660 add_header(&req, "Expires", tmp);
08661 add_header(&req, "Contact", p->our_contact);
08662 add_header(&req, "Event", "registration");
08663
08664 initialize_initreq(p, &req);
08665 if (sip_debug_test_pvt(p))
08666 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
08667 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
08668 r->regattempts++;
08669 if (option_debug > 3)
08670 ast_verbose("REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
08671 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
08672 }
08673
08674
08675 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
08676 {
08677 struct sip_request req;
08678
08679 reqprep(&req, p, SIP_MESSAGE, 0, 1);
08680 add_text(&req, text);
08681 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08682 }
08683
08684
08685 static int sip_refer_allocate(struct sip_pvt *p)
08686 {
08687 p->refer = ast_calloc(1, sizeof(struct sip_refer));
08688 return p->refer ? 1 : 0;
08689 }
08690
08691
08692
08693
08694
08695
08696 static int transmit_refer(struct sip_pvt *p, const char *dest)
08697 {
08698 struct sip_request req = {
08699 .headers = 0,
08700 };
08701 char from[256];
08702 const char *of;
08703 char *c;
08704 char referto[256];
08705 char *ttag, *ftag;
08706 char *theirtag = ast_strdupa(p->theirtag);
08707
08708 if (option_debug || sipdebug)
08709 ast_log(LOG_DEBUG, "SIP transfer of %s to %s\n", p->callid, dest);
08710
08711
08712 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
08713 of = get_header(&p->initreq, "To");
08714 ttag = theirtag;
08715 ftag = p->tag;
08716 } else {
08717 of = get_header(&p->initreq, "From");
08718 ftag = theirtag;
08719 ttag = p->tag;
08720 }
08721
08722 ast_copy_string(from, of, sizeof(from));
08723 of = get_in_brackets(from);
08724 ast_string_field_set(p, from, of);
08725 if (strncasecmp(of, "sip:", 4))
08726 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
08727 else
08728 of += 4;
08729
08730 if ((c = strchr(dest, '@')))
08731 c = NULL;
08732 else if ((c = strchr(of, '@')))
08733 *c++ = '\0';
08734 if (c)
08735 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
08736 else
08737 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
08738
08739
08740 sip_refer_allocate(p);
08741 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
08742 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
08743 p->refer->status = REFER_SENT;
08744
08745 reqprep(&req, p, SIP_REFER, 0, 1);
08746
08747 add_header(&req, "Refer-To", referto);
08748 add_header(&req, "Allow", ALLOWED_METHODS);
08749 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
08750 if (!ast_strlen_zero(p->our_contact))
08751 add_header(&req, "Referred-By", p->our_contact);
08752
08753 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08754
08755
08756
08757
08758
08759
08760
08761
08762 }
08763
08764
08765
08766 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
08767 {
08768 struct sip_request req;
08769
08770 reqprep(&req, p, SIP_INFO, 0, 1);
08771 add_digit(&req, digit, duration);
08772 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08773 }
08774
08775
08776 static int transmit_info_with_vidupdate(struct sip_pvt *p)
08777 {
08778 struct sip_request req;
08779
08780 reqprep(&req, p, SIP_INFO, 0, 1);
08781 add_vidupdate(&req);
08782 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
08783 }
08784
08785
08786
08787
08788 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
08789 {
08790 struct sip_request resp;
08791
08792 if (sipmethod == SIP_ACK)
08793 p->invitestate = INV_CONFIRMED;
08794
08795 reqprep(&resp, p, sipmethod, seqno, newbranch);
08796 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
08797 }
08798
08799
08800 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
08801 {
08802 struct sip_request resp;
08803
08804 reqprep(&resp, p, sipmethod, seqno, newbranch);
08805 if (!ast_strlen_zero(p->realm)) {
08806 char digest[1024];
08807
08808 memset(digest, 0, sizeof(digest));
08809 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
08810 if (p->options && p->options->auth_type == PROXY_AUTH)
08811 add_header(&resp, "Proxy-Authorization", digest);
08812 else if (p->options && p->options->auth_type == WWW_AUTH)
08813 add_header(&resp, "Authorization", digest);
08814 else
08815 add_header(&resp, "Proxy-Authorization", digest);
08816 } else
08817 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
08818 }
08819
08820
08821 if (sipmethod == SIP_BYE) {
08822 char buf[10];
08823
08824 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->hangupcause));
08825 snprintf(buf, sizeof(buf), "%d", p->hangupcause);
08826 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
08827 }
08828
08829 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
08830 }
08831
08832
08833 static void destroy_association(struct sip_peer *peer)
08834 {
08835 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE)) {
08836 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT) && ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE)) {
08837 ast_update_realtime("sippeers", "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "regserver", "", NULL);
08838 ast_update_realtime("sippeers", "name", peer->name, "lastms", "", NULL);
08839 } else
08840 ast_db_del("SIP/Registry", peer->name);
08841 }
08842 }
08843
08844
08845 static int expire_register(const void *data)
08846 {
08847 struct sip_peer *peer = (struct sip_peer *)data;
08848
08849 if (!peer)
08850 return 0;
08851
08852 memset(&peer->addr, 0, sizeof(peer->addr));
08853
08854 destroy_association(peer);
08855
08856 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
08857 register_peer_exten(peer, FALSE);
08858 peer->expire = -1;
08859 peer->portinuri = 0;
08860 ast_device_state_changed("SIP/%s", peer->name);
08861
08862
08863
08864
08865 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT) ||
08866 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
08867 struct sip_peer *peer_ptr = peer_ptr;
08868 peer_ptr = ASTOBJ_CONTAINER_UNLINK(&peerl, peer);
08869 if (peer_ptr) {
08870 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08871 }
08872 }
08873
08874 ASTOBJ_UNREF(peer, sip_destroy_peer);
08875
08876 return 0;
08877 }
08878
08879
08880 static int sip_poke_peer_s(const void *data)
08881 {
08882 struct sip_peer *peer = (struct sip_peer *) data;
08883 struct sip_peer *foundpeer;
08884
08885 peer->pokeexpire = -1;
08886
08887 foundpeer = ASTOBJ_CONTAINER_FIND(&peerl, peer->name);
08888 if (!foundpeer) {
08889 ASTOBJ_UNREF(peer, sip_destroy_peer);
08890 return 0;
08891 } else if (foundpeer->name != peer->name) {
08892 ASTOBJ_UNREF(foundpeer, sip_destroy_peer);
08893 ASTOBJ_UNREF(peer, sip_destroy_peer);
08894 return 0;
08895 }
08896
08897 ASTOBJ_UNREF(foundpeer, sip_destroy_peer);
08898 sip_poke_peer(peer);
08899 ASTOBJ_UNREF(peer, sip_destroy_peer);
08900
08901 return 0;
08902 }
08903
08904
08905 static void reg_source_db(struct sip_peer *peer)
08906 {
08907 char data[256];
08908 struct in_addr in;
08909 int expiry;
08910 int port;
08911 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
08912
08913 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
08914 return;
08915 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
08916 return;
08917
08918 scan = data;
08919 addr = strsep(&scan, ":");
08920 port_str = strsep(&scan, ":");
08921 expiry_str = strsep(&scan, ":");
08922 username = strsep(&scan, ":");
08923 contact = scan;
08924
08925 if (!inet_aton(addr, &in))
08926 return;
08927
08928 if (port_str)
08929 port = atoi(port_str);
08930 else
08931 return;
08932
08933 if (expiry_str)
08934 expiry = atoi(expiry_str);
08935 else
08936 return;
08937
08938 if (username)
08939 ast_copy_string(peer->username, username, sizeof(peer->username));
08940 if (contact)
08941 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
08942
08943 if (option_debug > 1)
08944 ast_log(LOG_DEBUG, "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
08945 peer->name, peer->username, ast_inet_ntoa(in), port, expiry);
08946
08947 memset(&peer->addr, 0, sizeof(peer->addr));
08948 peer->addr.sin_family = AF_INET;
08949 peer->addr.sin_addr = in;
08950 peer->addr.sin_port = htons(port);
08951 if (sipsock < 0) {
08952
08953 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
08954 struct sip_peer *peer_ptr = peer;
08955 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08956 }
08957 peer->pokeexpire = ast_sched_add(sched, ast_random() % 5000 + 1, sip_poke_peer_s, ASTOBJ_REF(peer));
08958 if (peer->pokeexpire == -1) {
08959 struct sip_peer *peer_ptr = peer;
08960 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08961 }
08962 } else
08963 sip_poke_peer(peer);
08964 if (!AST_SCHED_DEL(sched, peer->expire)) {
08965 struct sip_peer *peer_ptr = peer;
08966 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08967 }
08968 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, ASTOBJ_REF(peer));
08969 if (peer->expire == -1) {
08970 struct sip_peer *peer_ptr = peer;
08971 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08972 }
08973 register_peer_exten(peer, TRUE);
08974 }
08975
08976
08977 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
08978 {
08979 char contact[SIPBUFSIZE];
08980 char *c;
08981
08982
08983 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
08984 c = get_in_brackets(contact);
08985
08986
08987 ast_string_field_set(pvt, fullcontact, c);
08988
08989
08990 ast_string_field_set(pvt, okcontacturi, c);
08991
08992
08993
08994 return TRUE;
08995 }
08996
08997 static int __set_address_from_contact(const char *fullcontact, struct sockaddr_in *sin)
08998 {
08999 struct hostent *hp;
09000 struct ast_hostent ahp;
09001 int port;
09002 char *c, *host, *pt;
09003 char contact_buf[256];
09004 char *contact;
09005
09006
09007 ast_copy_string(contact_buf, fullcontact, sizeof(contact_buf));
09008 contact = contact_buf;
09009
09010
09011 if (strncasecmp(contact, "sip:", 4)) {
09012 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
09013 } else
09014 contact += 4;
09015
09016
09017
09018
09019
09020 host = strchr(contact, '@');
09021 if (!host) {
09022 host = contact;
09023 c = NULL;
09024 } else {
09025 *host++ = '\0';
09026 }
09027 pt = strchr(host, ':');
09028 if (pt) {
09029 *pt++ = '\0';
09030 port = atoi(pt);
09031 } else
09032 port = STANDARD_SIP_PORT;
09033
09034 contact = strsep(&contact, ";");
09035 host = strsep(&host, ";");
09036
09037
09038
09039 hp = ast_gethostbyname(host, &ahp);
09040 if (!hp) {
09041 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't resolve in DNS) : '%s'\n", host);
09042 return -1;
09043 }
09044 sin->sin_family = AF_INET;
09045 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
09046 sin->sin_port = htons(port);
09047
09048 return 0;
09049 }
09050
09051
09052 static int set_address_from_contact(struct sip_pvt *pvt)
09053 {
09054 if (ast_test_flag(&pvt->flags[0], SIP_NAT_ROUTE)) {
09055
09056
09057 pvt->sa = pvt->recv;
09058 return 0;
09059 }
09060
09061 return __set_address_from_contact(pvt->fullcontact, &pvt->sa);
09062 }
09063
09064
09065
09066 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
09067 {
09068 char contact[SIPBUFSIZE];
09069 char data[SIPBUFSIZE];
09070 const char *expires = get_header(req, "Expires");
09071 int expiry = atoi(expires);
09072 char *curi, *n, *pt;
09073 int port;
09074 const char *useragent;
09075 struct hostent *hp;
09076 struct ast_hostent ahp;
09077 struct sockaddr_in oldsin, testsin;
09078 char *firstcuri = NULL;
09079 int start = 0;
09080 int wildcard_found = 0;
09081 int single_binding_found = 0;
09082
09083 ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
09084
09085 if (ast_strlen_zero(expires)) {
09086 expires = strcasestr(contact, ";expires=");
09087 if (expires) {
09088
09089 expires = strsep((char **) &expires, ";");
09090 if (sscanf(expires + 9, "%30d", &expiry) != 1)
09091 expiry = default_expiry;
09092 } else {
09093
09094 expiry = default_expiry;
09095 }
09096 }
09097
09098 do {
09099
09100 curi = contact;
09101 if (strchr(contact, '<') == NULL)
09102 strsep(&curi, ";");
09103 curi = get_in_brackets(contact);
09104 if (!firstcuri) {
09105 firstcuri = ast_strdupa(curi);
09106 }
09107
09108 if (!strcasecmp(curi, "*")) {
09109 wildcard_found = 1;
09110 } else {
09111 single_binding_found = 1;
09112 }
09113
09114 if (wildcard_found && (ast_strlen_zero(expires) || expiry != 0 || single_binding_found)) {
09115
09116
09117 return PARSE_REGISTER_FAILED;
09118 }
09119
09120 ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
09121 } while (!ast_strlen_zero(contact));
09122 curi = firstcuri;
09123
09124
09125
09126
09127
09128 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
09129
09130 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact))
09131 pvt->expiry = ast_sched_when(sched, peer->expire);
09132 return PARSE_REGISTER_QUERY;
09133 } else if (!strcasecmp(curi, "*") || !expiry) {
09134
09135 if (!AST_SCHED_DEL(sched, peer->expire)) {
09136 struct sip_peer *peer_ptr = peer;
09137 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
09138 }
09139
09140 expire_register(ASTOBJ_REF(peer));
09141
09142 if (option_verbose > 2)
09143 ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", peer->name);
09144
09145 return PARSE_REGISTER_UPDATE;
09146 }
09147
09148
09149 ast_copy_string(peer->fullcontact, curi, sizeof(peer->fullcontact));
09150
09151
09152 ast_string_field_build(pvt, our_contact, "<%s>", curi);
09153
09154
09155 if (strncasecmp(curi, "sip:", 4)) {
09156 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", curi);
09157 } else
09158 curi += 4;
09159
09160 curi = strsep(&curi, ";");
09161
09162 n = strchr(curi, '@');
09163 if (!n) {
09164 n = curi;
09165 curi = NULL;
09166 } else
09167 *n++ = '\0';
09168 pt = strchr(n, ':');
09169 if (pt) {
09170 *pt++ = '\0';
09171 port = atoi(pt);
09172 peer->portinuri = 1;
09173 } else {
09174 port = STANDARD_SIP_PORT;
09175 peer->portinuri = 0;
09176 }
09177 oldsin = peer->addr;
09178
09179 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
09180
09181
09182 hp = ast_gethostbyname(n, &ahp);
09183 if (!hp) {
09184 ast_log(LOG_WARNING, "Invalid host '%s'\n", n);
09185 *peer->fullcontact = '\0';
09186 ast_string_field_set(pvt, our_contact, "");
09187 return PARSE_REGISTER_FAILED;
09188 }
09189
09190 peer->addr.sin_family = AF_INET;
09191 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
09192 peer->addr.sin_port = htons(port);
09193 } else {
09194
09195
09196 peer->addr = pvt->recv;
09197 }
09198
09199
09200 memcpy(&testsin.sin_addr, &peer->addr.sin_addr, sizeof(testsin.sin_addr));
09201 if (ast_apply_ha(global_contact_ha, &testsin) != AST_SENSE_ALLOW ||
09202 ast_apply_ha(peer->contactha, &testsin) != AST_SENSE_ALLOW) {
09203 ast_log(LOG_WARNING, "Host '%s' disallowed by contact ACL (violating IP %s)\n", n, ast_inet_ntoa(testsin.sin_addr));
09204 *peer->fullcontact = '\0';
09205 ast_string_field_set(pvt, our_contact, "");
09206 return PARSE_REGISTER_DENIED;
09207 }
09208
09209
09210 peer->sipoptions = pvt->sipoptions;
09211
09212 if (curi && ast_strlen_zero(peer->username))
09213 ast_copy_string(peer->username, curi, sizeof(peer->username));
09214
09215 if (!AST_SCHED_DEL(sched, peer->expire)) {
09216 struct sip_peer *peer_ptr = peer;
09217 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
09218 }
09219 if (expiry > max_expiry)
09220 expiry = max_expiry;
09221 if (expiry < min_expiry)
09222 expiry = min_expiry;
09223 if (ast_test_flag(&peer->flags[0], SIP_REALTIME) && !ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
09224 peer->expire = -1;
09225 } else {
09226 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, ASTOBJ_REF(peer));
09227 if (peer->expire == -1) {
09228 struct sip_peer *peer_ptr = peer;
09229 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
09230 }
09231 }
09232 pvt->expiry = expiry;
09233 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);
09234 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
09235 ast_db_put("SIP/Registry", peer->name, data);
09236 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
09237
09238
09239 if (option_verbose > 2 && inaddrcmp(&peer->addr, &oldsin)) {
09240 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));
09241 }
09242 sip_poke_peer(peer);
09243 register_peer_exten(peer, 1);
09244
09245
09246 useragent = get_header(req, "User-Agent");
09247 if (strcasecmp(useragent, peer->useragent)) {
09248 ast_copy_string(peer->useragent, useragent, sizeof(peer->useragent));
09249 if (option_verbose > 3)
09250 ast_verbose(VERBOSE_PREFIX_3 "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
09251 }
09252 return PARSE_REGISTER_UPDATE;
09253 }
09254
09255
09256 static void free_old_route(struct sip_route *route)
09257 {
09258 struct sip_route *next;
09259
09260 while (route) {
09261 next = route->next;
09262 free(route);
09263 route = next;
09264 }
09265 }
09266
09267
09268 static void list_route(struct sip_route *route)
09269 {
09270 if (!route)
09271 ast_verbose("list_route: no route\n");
09272 else {
09273 for (;route; route = route->next)
09274 ast_verbose("list_route: hop: <%s>\n", route->hop);
09275 }
09276 }
09277
09278
09279 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
09280 {
09281 struct sip_route *thishop, *head, *tail;
09282 int start = 0;
09283 int len;
09284 const char *rr, *contact, *c;
09285
09286
09287 if (p->route && p->route_persistant) {
09288 if (option_debug)
09289 ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
09290 return;
09291 }
09292
09293 if (p->route) {
09294 free_old_route(p->route);
09295 p->route = NULL;
09296 }
09297
09298
09299 p->route_persistant = 1;
09300
09301
09302
09303
09304
09305
09306 head = NULL;
09307 tail = head;
09308
09309 for (;;) {
09310
09311 rr = __get_header(req, "Record-Route", &start);
09312 if (*rr == '\0')
09313 break;
09314 for (; (rr = strchr(rr, '<')) ; rr += len) {
09315 ++rr;
09316 len = strcspn(rr, ">") + 1;
09317
09318 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
09319
09320 ast_copy_string(thishop->hop, rr, len);
09321 if (option_debug > 1)
09322 ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
09323
09324 if (backwards) {
09325
09326 thishop->next = head;
09327 head = thishop;
09328
09329 if (!tail)
09330 tail = thishop;
09331 } else {
09332 thishop->next = NULL;
09333
09334 if (tail)
09335 tail->next = thishop;
09336 else
09337 head = thishop;
09338 tail = thishop;
09339 }
09340 }
09341 }
09342 }
09343
09344
09345 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop,";lr") == NULL) ) {
09346
09347
09348 contact = get_header(req, "Contact");
09349 if (!ast_strlen_zero(contact)) {
09350 if (option_debug > 1)
09351 ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
09352
09353 c = strchr(contact, '<');
09354 if (c) {
09355
09356 ++c;
09357 len = strcspn(c, ">") + 1;
09358 } else {
09359
09360 c = contact;
09361 len = strlen(contact) + 1;
09362 }
09363 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
09364
09365 ast_copy_string(thishop->hop, c, len);
09366 thishop->next = NULL;
09367
09368 if (tail)
09369 tail->next = thishop;
09370 else
09371 head = thishop;
09372 }
09373 }
09374 }
09375
09376
09377 p->route = head;
09378
09379
09380 if (sip_debug_test_pvt(p))
09381 list_route(p->route);
09382 }
09383
09384
09385
09386
09387
09388
09389
09390 static void set_nonce_randdata(struct sip_pvt *p, int forceupdate)
09391 {
09392 if (p->stalenonce || forceupdate || ast_strlen_zero(p->randdata)) {
09393 ast_string_field_build(p, randdata, "%08lx", ast_random());
09394 p->stalenonce = 0;
09395 }
09396 }
09397
09398 AST_THREADSTORAGE(check_auth_buf, check_auth_buf_init);
09399 #define CHECK_AUTH_BUF_INITLEN 256
09400
09401
09402
09403
09404
09405
09406 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
09407 const char *secret, const char *md5secret, int sipmethod,
09408 char *uri, enum xmittype reliable, int ignore)
09409 {
09410 const char *response = "407 Proxy Authentication Required";
09411 const char *reqheader = "Proxy-Authorization";
09412 const char *respheader = "Proxy-Authenticate";
09413 const char *authtoken;
09414 char a1_hash[256];
09415 char resp_hash[256]="";
09416 char *c;
09417 int wrongnonce = FALSE;
09418 int good_response;
09419 const char *usednonce = p->randdata;
09420 struct ast_dynamic_str *buf;
09421 int res;
09422
09423
09424 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
09425 struct x {
09426 const char *key;
09427 const char *s;
09428 } *i, keys[] = {
09429 [K_RESP] = { "response=", "" },
09430 [K_URI] = { "uri=", "" },
09431 [K_USER] = { "username=", "" },
09432 [K_NONCE] = { "nonce=", "" },
09433 [K_LAST] = { NULL, NULL}
09434 };
09435
09436
09437 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
09438 return AUTH_SUCCESSFUL;
09439 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
09440
09441
09442
09443 response = "401 Unauthorized";
09444 reqheader = "Authorization";
09445 respheader = "WWW-Authenticate";
09446 }
09447 authtoken = get_header(req, reqheader);
09448 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
09449
09450
09451 if (!reliable) {
09452
09453
09454 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
09455
09456 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09457 }
09458 return AUTH_CHALLENGE_SENT;
09459 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
09460
09461 set_nonce_randdata(p, 1);
09462 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
09463
09464 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09465 return AUTH_CHALLENGE_SENT;
09466 }
09467
09468
09469
09470
09471
09472
09473 if (!(buf = ast_dynamic_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN)))
09474 return AUTH_SECRET_FAILED;
09475
09476
09477 res = ast_dynamic_str_thread_set(&buf, 0, &check_auth_buf, "%s", authtoken);
09478
09479 if (res == AST_DYNSTR_BUILD_FAILED)
09480 return AUTH_SECRET_FAILED;
09481
09482 c = buf->str;
09483
09484 while(c && *(c = ast_skip_blanks(c)) ) {
09485 for (i = keys; i->key != NULL; i++) {
09486 const char *separator = ",";
09487
09488 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
09489 continue;
09490
09491 c += strlen(i->key);
09492 if (*c == '"') {
09493 c++;
09494 separator = "\"";
09495 }
09496 i->s = c;
09497 strsep(&c, separator);
09498 break;
09499 }
09500 if (i->key == NULL)
09501 strsep(&c, " ,");
09502 }
09503
09504
09505 if (strcmp(username, keys[K_USER].s)) {
09506 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
09507 username, keys[K_USER].s);
09508
09509 return AUTH_USERNAME_MISMATCH;
09510 }
09511
09512
09513
09514 if (strcasecmp(p->randdata, keys[K_NONCE].s) || p->stalenonce) {
09515 wrongnonce = TRUE;
09516 usednonce = keys[K_NONCE].s;
09517 } else {
09518 p->stalenonce = 1;
09519 }
09520
09521 if (!ast_strlen_zero(md5secret))
09522 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
09523 else {
09524 char a1[256];
09525 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
09526 ast_md5_hash(a1_hash, a1);
09527 }
09528
09529
09530 {
09531 char a2[256];
09532 char a2_hash[256];
09533 char resp[256];
09534
09535 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
09536 S_OR(keys[K_URI].s, uri));
09537 ast_md5_hash(a2_hash, a2);
09538 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
09539 ast_md5_hash(resp_hash, resp);
09540 }
09541
09542 good_response = keys[K_RESP].s &&
09543 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
09544 if (wrongnonce) {
09545 if (good_response) {
09546 if (sipdebug)
09547 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "From"));
09548
09549 set_nonce_randdata(p, 0);
09550 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
09551 } else {
09552
09553 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
09554 if (sipdebug)
09555 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
09556 set_nonce_randdata(p, 1);
09557 } else {
09558 if (sipdebug)
09559 ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", get_header(req, "To"));
09560 }
09561 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
09562 }
09563
09564
09565 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09566 return AUTH_CHALLENGE_SENT;
09567 }
09568 if (good_response) {
09569 append_history(p, "AuthOK", "Auth challenge successful for %s", username);
09570 return AUTH_SUCCESSFUL;
09571 }
09572
09573
09574
09575
09576
09577
09578 return AUTH_SECRET_FAILED;
09579 }
09580
09581
09582 static void sip_peer_hold(struct sip_pvt *p, int hold)
09583 {
09584 struct sip_peer *peer = find_peer(p->peername, NULL, 1, 0);
09585
09586 if (!peer)
09587 return;
09588
09589
09590 if (hold)
09591 peer->onHold++;
09592 else
09593 peer->onHold--;
09594
09595
09596 ast_device_state_changed("SIP/%s", peer->name);
09597
09598 return;
09599 }
09600
09601 static int add_extensionstate_update(char *context, char *exten, int state, void *data)
09602 {
09603 struct sip_extenstate_update *update;
09604 size_t exten_len = strlen(exten);
09605 size_t context_len = strlen(context);
09606
09607 if (!(update = ast_calloc(1, sizeof(*update) + exten_len + context_len + 2))) {
09608 return -1;
09609 }
09610
09611 strcpy(update->exten, exten);
09612
09613 update->context = (char *) (update->exten + exten_len + 1);
09614 strcpy(update->context, context);
09615
09616 update->state = state;
09617 update->pvt = data;
09618
09619 AST_LIST_LOCK(&sip_extenstate_updates);
09620 AST_LIST_INSERT_TAIL(&sip_extenstate_updates, update, list);
09621 AST_LIST_UNLOCK(&sip_extenstate_updates);
09622
09623
09624 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
09625 pthread_kill(monitor_thread, SIGURG);
09626 }
09627
09628 return 0;
09629 }
09630
09631
09632
09633
09634
09635 static void check_extenstate_updates(struct sip_pvt *pvt)
09636 {
09637 struct sip_extenstate_update *update = NULL;
09638
09639 if (AST_LIST_EMPTY(&sip_extenstate_updates)) {
09640
09641 return;
09642 }
09643
09644 do {
09645 if (update) {
09646
09647 notify_extenstate_update(update->context, update->exten, update->state, pvt);
09648 ast_free(update);
09649 }
09650
09651 AST_LIST_LOCK(&sip_extenstate_updates);
09652 AST_LIST_TRAVERSE_SAFE_BEGIN(&sip_extenstate_updates, update, list) {
09653 if (update->pvt == pvt) {
09654 AST_LIST_REMOVE_CURRENT(&sip_extenstate_updates, list);
09655 break;
09656 }
09657 }
09658 AST_LIST_TRAVERSE_SAFE_END
09659 AST_LIST_UNLOCK(&sip_extenstate_updates);
09660 } while (update);
09661 }
09662
09663
09664
09665
09666
09667 static void clearmarked_extenstate_updates(void)
09668 {
09669 struct sip_extenstate_update *update = NULL;
09670
09671 if (AST_LIST_EMPTY(&sip_extenstate_updates)) {
09672
09673 return;
09674 }
09675
09676 AST_LIST_LOCK(&sip_extenstate_updates);
09677 AST_LIST_TRAVERSE_SAFE_BEGIN(&sip_extenstate_updates, update, list) {
09678 if (update->marked) {
09679 AST_LIST_REMOVE_CURRENT(&sip_extenstate_updates, list);
09680 ast_free(update);
09681 }
09682 }
09683 AST_LIST_TRAVERSE_SAFE_END
09684 AST_LIST_UNLOCK(&sip_extenstate_updates);
09685
09686 }
09687
09688
09689
09690
09691
09692
09693
09694
09695 static void unmark_extenstate_update(struct sip_pvt *pvt)
09696 {
09697 struct sip_extenstate_update *update = NULL;
09698
09699 if (AST_LIST_EMPTY(&sip_extenstate_updates)) {
09700
09701 return;
09702 }
09703
09704 AST_LIST_LOCK(&sip_extenstate_updates);
09705 AST_LIST_TRAVERSE(&sip_extenstate_updates, update, list) {
09706 if (update->pvt == pvt) {
09707 update->marked = 0;
09708 }
09709 }
09710 AST_LIST_UNLOCK(&sip_extenstate_updates);
09711 }
09712
09713
09714
09715
09716
09717 static void markall_extenstate_updates(void)
09718 {
09719 struct sip_extenstate_update *update = NULL;
09720
09721 if (AST_LIST_EMPTY(&sip_extenstate_updates)) {
09722
09723 return;
09724 }
09725
09726 AST_LIST_LOCK(&sip_extenstate_updates);
09727 AST_LIST_TRAVERSE(&sip_extenstate_updates, update, list) {
09728 update->marked = 1;
09729 }
09730 AST_LIST_UNLOCK(&sip_extenstate_updates);
09731 }
09732
09733
09734
09735
09736
09737 static void clear_extenstate_updates(struct sip_pvt *pvt)
09738 {
09739 struct sip_extenstate_update *update = NULL;
09740
09741 if (AST_LIST_EMPTY(&sip_extenstate_updates)) {
09742
09743 return;
09744 }
09745
09746 AST_LIST_LOCK(&sip_extenstate_updates);
09747 AST_LIST_TRAVERSE_SAFE_BEGIN(&sip_extenstate_updates, update, list) {
09748 if (update->pvt == pvt) {
09749 AST_LIST_REMOVE_CURRENT(&sip_extenstate_updates, list);
09750 ast_free(update);
09751 }
09752 }
09753 AST_LIST_TRAVERSE_SAFE_END
09754 AST_LIST_UNLOCK(&sip_extenstate_updates);
09755 }
09756
09757
09758
09759
09760 static int notify_extenstate_update(char *context, char* exten, int state, void *data)
09761 {
09762 struct sip_pvt *p = data;
09763
09764 ast_mutex_lock(&p->lock);
09765
09766 switch(state) {
09767 case AST_EXTENSION_DEACTIVATED:
09768 case AST_EXTENSION_REMOVED:
09769 if (p->autokillid > -1 && sip_cancel_destroy(p))
09770 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
09771 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09772 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);
09773 p->stateid = -1;
09774 p->subscribed = NONE;
09775 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
09776 break;
09777 default:
09778 p->laststate = state;
09779 break;
09780 }
09781 if (p->subscribed != NONE) {
09782 if (!p->pendinginvite) {
09783 transmit_state_notify(p, state, 1, FALSE);
09784 } else {
09785
09786
09787 ast_set_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
09788 }
09789 }
09790 if (option_verbose > 1)
09791 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,
09792 ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE) ? "(queued)" : "");
09793
09794
09795 ast_mutex_unlock(&p->lock);
09796
09797 return 0;
09798 }
09799
09800
09801
09802
09803 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable)
09804 {
09805
09806
09807 const char *response = "407 Proxy Authentication Required";
09808 const char *reqheader = "Proxy-Authorization";
09809 const char *respheader = "Proxy-Authenticate";
09810 const char *authtoken;
09811 struct ast_dynamic_str *buf;
09812 char *c;
09813
09814
09815 enum keys { K_NONCE, K_LAST };
09816 struct x {
09817 const char *key;
09818 const char *s;
09819 } *i, keys[] = {
09820 [K_NONCE] = { "nonce=", "" },
09821 [K_LAST] = { NULL, NULL}
09822 };
09823
09824 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
09825 response = "401 Unauthorized";
09826 reqheader = "Authorization";
09827 respheader = "WWW-Authenticate";
09828 }
09829 authtoken = get_header(req, reqheader);
09830 if (ast_test_flag(req, SIP_PKT_IGNORE) && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
09831
09832
09833 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
09834
09835 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09836 return;
09837 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
09838
09839 set_nonce_randdata(p, 1);
09840 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
09841
09842 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09843 return;
09844 }
09845
09846 if (!(buf = ast_dynamic_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
09847 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
09848 return;
09849 }
09850
09851
09852 if (ast_dynamic_str_thread_set(&buf, 0, &check_auth_buf, "%s", authtoken) == AST_DYNSTR_BUILD_FAILED) {
09853 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
09854 return;
09855 }
09856
09857 c = buf->str;
09858
09859 while (c && *(c = ast_skip_blanks(c))) {
09860 for (i = keys; i->key != NULL; i++) {
09861 const char *separator = ",";
09862
09863 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
09864 continue;
09865 }
09866
09867 c += strlen(i->key);
09868 if (*c == '"') {
09869 c++;
09870 separator = "\"";
09871 }
09872 i->s = c;
09873 strsep(&c, separator);
09874 break;
09875 }
09876 if (i->key == NULL) {
09877 strsep(&c, " ,");
09878 }
09879 }
09880
09881
09882 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
09883 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
09884 set_nonce_randdata(p, 1);
09885 }
09886 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
09887
09888
09889 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09890 } else {
09891 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
09892 }
09893 }
09894
09895
09896
09897
09898
09899
09900 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
09901 struct sip_request *req, char *uri)
09902 {
09903 enum check_auth_result res = AUTH_NOT_FOUND;
09904 struct sip_peer *peer;
09905 char tmp[256];
09906 char *name, *c;
09907 char *t;
09908 char *domain;
09909
09910
09911 t = uri;
09912 while(*t && (*t > 32) && (*t != ';'))
09913 t++;
09914 *t = '\0';
09915
09916 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
09917 if (pedanticsipchecking)
09918 ast_uri_decode(tmp);
09919
09920 c = get_in_brackets(tmp);
09921 c = strsep(&c, ";");
09922
09923 if (!strncasecmp(c, "sip:", 4)) {
09924 name = c + 4;
09925 } else {
09926 name = c;
09927 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(sin->sin_addr));
09928 }
09929
09930
09931 if ((c = strchr(name, '@'))) {
09932 *c++ = '\0';
09933 domain = c;
09934 if ((c = strchr(domain, ':')))
09935 *c = '\0';
09936 if (!AST_LIST_EMPTY(&domain_list)) {
09937 if (!check_sip_domain(domain, NULL, 0)) {
09938 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
09939 return AUTH_UNKNOWN_DOMAIN;
09940 }
09941 }
09942 }
09943
09944 ast_string_field_set(p, exten, name);
09945 build_contact(p);
09946 if (ast_test_flag(req, SIP_PKT_IGNORE)) {
09947
09948 const char *expires = get_header(req, "Expires");
09949 int expire = atoi(expires);
09950
09951 if (ast_strlen_zero(expires)) {
09952 if ((expires = strcasestr(get_header(req, "Contact"), ";expires="))) {
09953 expire = atoi(expires + 9);
09954 }
09955 }
09956 if (!ast_strlen_zero(expires) && expire == 0) {
09957 transmit_response_with_date(p, "200 OK", req);
09958 return 0;
09959 }
09960 }
09961 peer = find_peer(name, NULL, 1, 0);
09962 if (!(peer && ast_apply_ha(peer->ha, sin))) {
09963
09964 if (peer) {
09965 ASTOBJ_UNREF(peer, sip_destroy_peer);
09966 res = AUTH_ACL_FAILED;
09967 } else
09968 res = AUTH_NOT_FOUND;
09969 }
09970 if (peer) {
09971
09972 if (p->rtp) {
09973 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
09974 p->autoframing = peer->autoframing;
09975 }
09976 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
09977 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
09978 res = AUTH_PEER_NOT_DYNAMIC;
09979 } else {
09980 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
09981 transmit_response(p, "100 Trying", req);
09982 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09983 if (sip_cancel_destroy(p))
09984 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
09985
09986
09987
09988 switch (parse_register_contact(p, peer, req)) {
09989 case PARSE_REGISTER_DENIED:
09990 transmit_response_with_date(p, "603 Denied", req);
09991 peer->lastmsgssent = -1;
09992 res = 0;
09993 break;
09994 case PARSE_REGISTER_FAILED:
09995 ast_log(LOG_WARNING, "Failed to parse contact info\n");
09996 transmit_response_with_date(p, "400 Bad Request", req);
09997 peer->lastmsgssent = -1;
09998 res = 0;
09999 break;
10000 case PARSE_REGISTER_QUERY:
10001 ast_string_field_set(p, fullcontact, peer->fullcontact);
10002 transmit_response_with_date(p, "200 OK", req);
10003 peer->lastmsgssent = -1;
10004 res = 0;
10005 break;
10006 case PARSE_REGISTER_UPDATE:
10007 ast_string_field_set(p, fullcontact, peer->fullcontact);
10008 update_peer(peer, p->expiry);
10009
10010 transmit_response_with_date(p, "200 OK", req);
10011 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
10012 peer->lastmsgssent = -1;
10013 res = 0;
10014 break;
10015 }
10016 }
10017 }
10018 }
10019 if (!peer && autocreatepeer) {
10020
10021 peer = temp_peer(name);
10022 if (peer) {
10023 ASTOBJ_CONTAINER_LINK(&peerl, peer);
10024 if (sip_cancel_destroy(p))
10025 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
10026 switch (parse_register_contact(p, peer, req)) {
10027 case PARSE_REGISTER_DENIED:
10028 ast_log(LOG_WARNING, "Registration denied because of contact ACL\n");
10029 transmit_response_with_date(p, "403 Forbidden (ACL)", req);
10030 peer->lastmsgssent = -1;
10031 res = 0;
10032 break;
10033 case PARSE_REGISTER_FAILED:
10034 ast_log(LOG_WARNING, "Failed to parse contact info\n");
10035 transmit_response_with_date(p, "400 Bad Request", req);
10036 peer->lastmsgssent = -1;
10037 res = 0;
10038 break;
10039 case PARSE_REGISTER_QUERY:
10040 ast_string_field_set(p, fullcontact, peer->fullcontact);
10041 transmit_response_with_date(p, "200 OK", req);
10042 peer->lastmsgssent = -1;
10043 res = 0;
10044 break;
10045 case PARSE_REGISTER_UPDATE:
10046 ast_string_field_set(p, fullcontact, peer->fullcontact);
10047
10048 transmit_response_with_date(p, "200 OK", req);
10049 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
10050 peer->lastmsgssent = -1;
10051 res = 0;
10052 break;
10053 }
10054 }
10055 }
10056 if (!peer && global_alwaysauthreject) {
10057
10058
10059
10060 transmit_response(p, "100 Trying", req);
10061
10062 sched_yield();
10063 }
10064 if (!res) {
10065 ast_device_state_changed("SIP/%s", peer->name);
10066 }
10067 if (res < 0) {
10068 switch (res) {
10069 case AUTH_SECRET_FAILED:
10070
10071 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
10072 break;
10073 case AUTH_USERNAME_MISMATCH:
10074
10075
10076
10077
10078 case AUTH_NOT_FOUND:
10079 case AUTH_PEER_NOT_DYNAMIC:
10080 case AUTH_ACL_FAILED:
10081 if (global_alwaysauthreject) {
10082 transmit_fake_auth_response(p, SIP_REGISTER, &p->initreq, XMIT_UNRELIABLE);
10083 } else {
10084
10085 if (res == AUTH_PEER_NOT_DYNAMIC)
10086 transmit_response(p, "403 Forbidden", &p->initreq);
10087 else
10088 transmit_response(p, "404 Not found", &p->initreq);
10089 }
10090 break;
10091 default:
10092 break;
10093 }
10094 }
10095 if (peer)
10096 ASTOBJ_UNREF(peer, sip_destroy_peer);
10097
10098 return res;
10099 }
10100
10101
10102 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
10103 {
10104 char tmp[256], *c, *a;
10105 struct sip_request *req;
10106
10107 req = oreq;
10108 if (!req)
10109 req = &p->initreq;
10110 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
10111 if (ast_strlen_zero(tmp))
10112 return 0;
10113 c = get_in_brackets(tmp);
10114 if (strncasecmp(c, "sip:", 4)) {
10115 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", c);
10116 return -1;
10117 }
10118 c += 4;
10119 a = c;
10120 strsep(&a, "@;");
10121 if (sip_debug_test_pvt(p))
10122 ast_verbose("RDNIS is %s\n", c);
10123 ast_string_field_set(p, rdnis, c);
10124
10125 return 0;
10126 }
10127
10128
10129
10130
10131 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
10132 {
10133 char tmp[256] = "", *uri, *a;
10134 char tmpf[256] = "", *from;
10135 struct sip_request *req;
10136 char *colon;
10137 char *decoded_uri;
10138
10139 req = oreq;
10140 if (!req)
10141 req = &p->initreq;
10142
10143
10144 if (req->rlPart2)
10145 ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
10146
10147 if (pedanticsipchecking)
10148 ast_uri_decode(tmp);
10149
10150 uri = get_in_brackets(tmp);
10151
10152 if (strncasecmp(uri, "sip:", 4)) {
10153 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
10154 return -1;
10155 }
10156 uri += 4;
10157
10158
10159 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
10160 if (!ast_strlen_zero(tmpf)) {
10161 if (pedanticsipchecking)
10162 ast_uri_decode(tmpf);
10163 from = get_in_brackets(tmpf);
10164 } else {
10165 from = NULL;
10166 }
10167
10168 if (!ast_strlen_zero(from)) {
10169 if (strncasecmp(from, "sip:", 4)) {
10170 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
10171 return -1;
10172 }
10173 from += 4;
10174 if ((a = strchr(from, '@')))
10175 *a++ = '\0';
10176 else
10177 a = from;
10178 from = strsep(&from, ";");
10179 a = strsep(&a, ";");
10180 ast_string_field_set(p, fromdomain, a);
10181 }
10182
10183
10184
10185
10186 if ((a = strchr(uri, '@'))) {
10187 *a++ = '\0';
10188 } else {
10189 a = uri;
10190 uri = "s";
10191 }
10192 colon = strchr(a, ':');
10193 if (colon)
10194 *colon = '\0';
10195
10196 uri = strsep(&uri, ";");
10197 a = strsep(&a, ";");
10198
10199 ast_string_field_set(p, domain, a);
10200
10201 if (!AST_LIST_EMPTY(&domain_list)) {
10202 char domain_context[AST_MAX_EXTENSION];
10203
10204 domain_context[0] = '\0';
10205 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
10206 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
10207 if (option_debug)
10208 ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
10209 return -2;
10210 }
10211 }
10212
10213 if (!ast_strlen_zero(domain_context))
10214 ast_string_field_set(p, context, domain_context);
10215 }
10216
10217
10218 if (req->method == SIP_SUBSCRIBE && !ast_strlen_zero(p->subscribecontext))
10219 ast_string_field_set(p, context, p->subscribecontext);
10220
10221 if (sip_debug_test_pvt(p))
10222 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
10223
10224
10225
10226 decoded_uri = ast_strdupa(uri);
10227 ast_uri_decode(decoded_uri);
10228
10229
10230 if (req->method == SIP_SUBSCRIBE) {
10231 char hint[AST_MAX_EXTENSION];
10232 int which = 0;
10233 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, uri) ||
10234 (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, decoded_uri) && (which = 1))) {
10235 if (!oreq) {
10236 ast_string_field_set(p, exten, which ? decoded_uri : uri);
10237 }
10238 return 0;
10239 } else {
10240 return -1;
10241 }
10242 } else {
10243 int which = 0;
10244
10245
10246
10247 if (ast_exists_extension(NULL, p->context, uri, 1, S_OR(p->cid_num, from)) ||
10248 (ast_exists_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from)) && (which = 1)) ||
10249 !strcmp(decoded_uri, ast_pickup_ext())) {
10250 if (!oreq) {
10251 ast_string_field_set(p, exten, which ? decoded_uri : uri);
10252 }
10253 return 0;
10254 }
10255 }
10256
10257
10258 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
10259 ast_canmatch_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from))) ||
10260 !strncmp(decoded_uri, ast_pickup_ext(), strlen(decoded_uri))) {
10261 return 1;
10262 }
10263
10264 return -1;
10265 }
10266
10267
10268
10269 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
10270 {
10271 struct sip_pvt *sip_pvt_ptr;
10272
10273 ast_mutex_lock(&iflock);
10274
10275 if (option_debug > 3 && totag) {
10276 ast_log(LOG_DEBUG, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
10277 }
10278
10279
10280 for (sip_pvt_ptr = iflist; sip_pvt_ptr; sip_pvt_ptr = sip_pvt_ptr->next) {
10281 if (!strcmp(sip_pvt_ptr->callid, callid)) {
10282 int match = 1;
10283
10284 if (option_debug > 3)
10285 ast_log(LOG_DEBUG, "Found call with callid %s (ourtag=%s, theirtag=%s)\n", callid, sip_pvt_ptr->tag, sip_pvt_ptr->theirtag);
10286
10287
10288 ast_mutex_lock(&sip_pvt_ptr->lock);
10289
10290
10291
10292
10293
10294 if (pedanticsipchecking) {
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309 if (ast_strlen_zero(fromtag) || strcmp(fromtag, sip_pvt_ptr->theirtag) || (!ast_strlen_zero(totag) && strcmp(totag, sip_pvt_ptr->tag)))
10310 match = 0;
10311 }
10312
10313 if (!match) {
10314 ast_mutex_unlock(&sip_pvt_ptr->lock);
10315 continue;
10316 }
10317
10318 if (option_debug > 3 && totag)
10319 ast_log(LOG_DEBUG, "Matched %s call - their tag is %s Our tag is %s\n",
10320 ast_test_flag(&sip_pvt_ptr->flags[1], SIP_PAGE2_OUTGOING_CALL) ? "OUTGOING": "INCOMING",
10321 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
10322
10323
10324 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
10325 DEADLOCK_AVOIDANCE(&sip_pvt_ptr->lock);
10326 }
10327 break;
10328 }
10329 }
10330 ast_mutex_unlock(&iflock);
10331 if (option_debug > 3 && !sip_pvt_ptr)
10332 ast_log(LOG_DEBUG, "Found no match for callid %s to-tag %s from-tag %s\n", callid, totag, fromtag);
10333 return sip_pvt_ptr;
10334 }
10335
10336
10337
10338 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
10339 {
10340
10341 const char *p_referred_by = NULL;
10342 char *h_refer_to = NULL;
10343 char *h_referred_by = NULL;
10344 char *refer_to;
10345 const char *p_refer_to;
10346 char *referred_by_uri = NULL;
10347 char *ptr;
10348 struct sip_request *req = NULL;
10349 const char *transfer_context = NULL;
10350 struct sip_refer *referdata;
10351
10352
10353 req = outgoing_req;
10354 referdata = transferer->refer;
10355
10356 if (!req)
10357 req = &transferer->initreq;
10358
10359 p_refer_to = get_header(req, "Refer-To");
10360 if (ast_strlen_zero(p_refer_to)) {
10361 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
10362 return -2;
10363 }
10364 h_refer_to = ast_strdupa(p_refer_to);
10365 refer_to = get_in_brackets(h_refer_to);
10366 if (pedanticsipchecking)
10367 ast_uri_decode(refer_to);
10368
10369 if (strncasecmp(refer_to, "sip:", 4)) {
10370 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
10371 return -3;
10372 }
10373 refer_to += 4;
10374
10375
10376 p_referred_by = get_header(req, "Referred-By");
10377 if (!ast_strlen_zero(p_referred_by)) {
10378 char *lessthan;
10379 h_referred_by = ast_strdupa(p_referred_by);
10380 if (pedanticsipchecking)
10381 ast_uri_decode(h_referred_by);
10382
10383
10384 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
10385 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
10386 *(lessthan - 1) = '\0';
10387 }
10388
10389 referred_by_uri = get_in_brackets(h_referred_by);
10390 if(strncasecmp(referred_by_uri, "sip:", 4)) {
10391 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
10392 referred_by_uri = (char *) NULL;
10393 } else {
10394 referred_by_uri += 4;
10395 }
10396 }
10397
10398
10399 if ((ptr = strcasestr(refer_to, "replaces="))) {
10400 char *to = NULL, *from = NULL;
10401
10402
10403 referdata->attendedtransfer = 1;
10404 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
10405 ast_uri_decode(referdata->replaces_callid);
10406 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
10407 *ptr++ = '\0';
10408 }
10409
10410 if (ptr) {
10411
10412 to = strcasestr(ptr, "to-tag=");
10413 from = strcasestr(ptr, "from-tag=");
10414 }
10415
10416
10417 if (to) {
10418 ptr = to + 7;
10419 if ((to = strchr(ptr, '&')))
10420 *to = '\0';
10421 if ((to = strchr(ptr, ';')))
10422 *to = '\0';
10423 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
10424 }
10425
10426 if (from) {
10427 ptr = from + 9;
10428 if ((to = strchr(ptr, '&')))
10429 *to = '\0';
10430 if ((to = strchr(ptr, ';')))
10431 *to = '\0';
10432 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
10433 }
10434
10435 if (!strcmp(referdata->replaces_callid, transferer->callid) &&
10436 (!pedanticsipchecking ||
10437 (!strcmp(referdata->replaces_callid_fromtag, transferer->theirtag) &&
10438 !strcmp(referdata->replaces_callid_totag, transferer->tag)))) {
10439 ast_log(LOG_WARNING, "Got an attempt to replace own Call-ID on %s\n", transferer->callid);
10440 return -4;
10441 }
10442
10443 if (option_debug > 1) {
10444 if (!pedanticsipchecking)
10445 ast_log(LOG_DEBUG,"Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
10446 else
10447 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>" );
10448 }
10449 }
10450
10451 if ((ptr = strchr(refer_to, '@'))) {
10452 char *urioption = NULL, *domain;
10453 *ptr++ = '\0';
10454
10455 if ((urioption = strchr(ptr, ';')))
10456 *urioption++ = '\0';
10457
10458 domain = ptr;
10459 if ((ptr = strchr(domain, ':')))
10460 *ptr = '\0';
10461
10462
10463 ast_copy_string(referdata->refer_to_domain, domain, sizeof(referdata->refer_to_domain));
10464 if (urioption)
10465 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
10466 }
10467
10468 if ((ptr = strchr(refer_to, ';')))
10469 *ptr = '\0';
10470 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
10471
10472 if (referred_by_uri) {
10473 if ((ptr = strchr(referred_by_uri, ';')))
10474 *ptr = '\0';
10475 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
10476 } else {
10477 referdata->referred_by[0] = '\0';
10478 }
10479
10480
10481 if (transferer->owner)
10482 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
10483
10484
10485 if (ast_strlen_zero(transfer_context)) {
10486 transfer_context = S_OR(transferer->owner->macrocontext,
10487 S_OR(transferer->context, default_context));
10488 }
10489
10490 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
10491
10492
10493 if (referdata->attendedtransfer || ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
10494 if (sip_debug_test_pvt(transferer)) {
10495 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
10496 }
10497
10498 return 0;
10499 }
10500 if (sip_debug_test_pvt(transferer))
10501 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
10502
10503
10504 return -1;
10505 }
10506
10507
10508
10509 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
10510 {
10511 char tmp[256] = "", *c, *a;
10512 struct sip_request *req = oreq ? oreq : &p->initreq;
10513 struct sip_refer *referdata = NULL;
10514 const char *transfer_context = NULL;
10515
10516 if (!p->refer && !sip_refer_allocate(p))
10517 return -1;
10518
10519 referdata = p->refer;
10520
10521 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
10522 c = get_in_brackets(tmp);
10523
10524 if (pedanticsipchecking)
10525 ast_uri_decode(c);
10526
10527 if (strncasecmp(c, "sip:", 4)) {
10528 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
10529 return -1;
10530 }
10531 c += 4;
10532 if ((a = strchr(c, ';')))
10533 *a = '\0';
10534
10535 if ((a = strchr(c, '@'))) {
10536 *a++ = '\0';
10537 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
10538 }
10539
10540 if (sip_debug_test_pvt(p))
10541 ast_verbose("Looking for %s in %s\n", c, p->context);
10542
10543 if (p->owner)
10544 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
10545
10546
10547 if (ast_strlen_zero(transfer_context)) {
10548 transfer_context = S_OR(p->owner->macrocontext,
10549 S_OR(p->context, default_context));
10550 }
10551 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
10552
10553 if (option_debug)
10554 ast_log(LOG_DEBUG,"SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
10555 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
10556 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
10557 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
10558 referdata->refer_call = NULL;
10559
10560 ast_string_field_set(p, context, transfer_context);
10561 return 0;
10562 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
10563 return 1;
10564 }
10565
10566 return -1;
10567 }
10568
10569 static void check_via(struct sip_pvt *p, const struct sip_request *req)
10570 {
10571 char via[512];
10572 char *c, *pt, *maddr;
10573 struct hostent *hp;
10574 struct ast_hostent ahp;
10575
10576 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
10577
10578
10579 c = strchr(via, ',');
10580 if (c)
10581 *c = '\0';
10582
10583
10584 c = strstr(via, ";rport");
10585 if (c && (c[6] != '='))
10586 ast_set_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT);
10587
10588
10589 maddr = strstr(via, "maddr=");
10590 if (maddr) {
10591 maddr += 6;
10592 c = maddr + strspn(maddr, "0123456789.");
10593 *c = '\0';
10594 }
10595
10596 c = strchr(via, ';');
10597 if (c)
10598 *c = '\0';
10599
10600 c = strchr(via, ' ');
10601 if (c) {
10602 *c = '\0';
10603 c = ast_skip_blanks(c+1);
10604 if (strcasecmp(via, "SIP/2.0/UDP")) {
10605 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
10606 return;
10607 }
10608 pt = strchr(c, ':');
10609 if (pt)
10610 *pt++ = '\0';
10611
10612 if (maddr)
10613 c = maddr;
10614 hp = ast_gethostbyname(c, &ahp);
10615 if (!hp) {
10616 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
10617 return;
10618 }
10619 memset(&p->sa, 0, sizeof(p->sa));
10620 p->sa.sin_family = AF_INET;
10621 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
10622 p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
10623
10624 if (sip_debug_test_pvt(p)) {
10625 const struct sockaddr_in *dst = sip_real_dst(p);
10626 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
10627 }
10628 }
10629 }
10630
10631
10632 static char *get_calleridname(const char *input, char *output, size_t outputsize)
10633 {
10634 const char *end = strchr(input,'<');
10635 const char *tmp = strchr(input,'"');
10636 int bytes = 0;
10637 int maxbytes = outputsize - 1;
10638
10639 if (!end || end == input)
10640 return NULL;
10641
10642 end--;
10643
10644 if (tmp && tmp <= end) {
10645
10646
10647
10648 end = strchr(tmp+1, '"');
10649 if (!end)
10650 return NULL;
10651 bytes = (int) (end - tmp);
10652
10653 if (bytes > maxbytes)
10654 bytes = maxbytes;
10655 ast_copy_string(output, tmp + 1, bytes);
10656 } else {
10657
10658
10659 input = ast_skip_blanks(input);
10660
10661 while(*end && *end < 33 && end > input)
10662 end--;
10663 if (end >= input) {
10664 bytes = (int) (end - input) + 2;
10665
10666 if (bytes > maxbytes)
10667 bytes = maxbytes;
10668 ast_copy_string(output, input, bytes);
10669 } else
10670 return NULL;
10671 }
10672 return output;
10673 }
10674
10675
10676
10677
10678
10679 static int get_rpid_num(const char *input, char *output, int maxlen)
10680 {
10681 char *start;
10682 char *end;
10683
10684 start = strchr(input,':');
10685 if (!start) {
10686 output[0] = '\0';
10687 return 0;
10688 }
10689 start++;
10690
10691
10692 ast_copy_string(output,start,maxlen);
10693 output[maxlen-1] = '\0';
10694
10695 end = strchr(output,'@');
10696 if (end)
10697 *end = '\0';
10698 else
10699 output[0] = '\0';
10700 if (strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
10701 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
10702
10703 return 0;
10704 }
10705
10706
10707
10708
10709
10710
10711
10712 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
10713 int sipmethod, char *uri, enum xmittype reliable,
10714 struct sockaddr_in *sin, struct sip_peer **authpeer)
10715 {
10716 struct sip_user *user = NULL;
10717 struct sip_peer *peer;
10718 char from[256], *c;
10719 char *of;
10720 char rpid_num[50];
10721 const char *rpid;
10722 enum check_auth_result res = AUTH_SUCCESSFUL;
10723 char *t;
10724 char calleridname[50];
10725 int debug=sip_debug_test_addr(sin);
10726 struct ast_variable *tmpvar = NULL, *v = NULL;
10727 char *uri2 = ast_strdupa(uri);
10728
10729
10730 t = uri2;
10731 while (*t && *t > 32 && *t != ';')
10732 t++;
10733 *t = '\0';
10734 ast_copy_string(from, get_header(req, "From"), sizeof(from));
10735 if (pedanticsipchecking)
10736 ast_uri_decode(from);
10737
10738 memset(calleridname, 0, sizeof(calleridname));
10739 get_calleridname(from, calleridname, sizeof(calleridname));
10740 if (calleridname[0])
10741 ast_string_field_set(p, cid_name, calleridname);
10742
10743 rpid = get_header(req, "Remote-Party-ID");
10744 memset(rpid_num, 0, sizeof(rpid_num));
10745 if (!ast_strlen_zero(rpid))
10746 p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
10747
10748 of = get_in_brackets(from);
10749 if (ast_strlen_zero(p->exten)) {
10750 t = uri2;
10751 if (!strncasecmp(t, "sip:", 4))
10752 t+= 4;
10753 ast_string_field_set(p, exten, t);
10754 t = strchr(p->exten, '@');
10755 if (t)
10756 *t = '\0';
10757 if (ast_strlen_zero(p->our_contact))
10758 build_contact(p);
10759 }
10760
10761 ast_string_field_set(p, from, of);
10762 if (strncasecmp(of, "sip:", 4)) {
10763 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
10764 } else
10765 of += 4;
10766
10767 if ((c = strchr(of, '@'))) {
10768 char *tmp;
10769 *c = '\0';
10770 if ((c = strchr(of, ':')))
10771 *c = '\0';
10772 tmp = ast_strdupa(of);
10773
10774
10775
10776 tmp = strsep(&tmp, ";");
10777 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10778 ast_shrink_phone_number(tmp);
10779 ast_string_field_set(p, cid_num, tmp);
10780 }
10781
10782 if (!authpeer)
10783 user = find_user(of, 1);
10784
10785
10786 if (user && ast_apply_ha(user->ha, sin)) {
10787 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
10788 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10789 if (sipmethod == SIP_INVITE) {
10790
10791 ast_variables_destroy(p->chanvars);
10792 p->chanvars = NULL;
10793 for (v = user->chanvars ; v ; v = v->next) {
10794 if ((tmpvar = ast_variable_new(v->name, v->value))) {
10795 tmpvar->next = p->chanvars;
10796 p->chanvars = tmpvar;
10797 }
10798 }
10799 }
10800 p->prefs = user->prefs;
10801
10802 if (p->rtp) {
10803 ast_rtp_codec_setpref(p->rtp, &p->prefs);
10804 p->autoframing = user->autoframing;
10805 }
10806
10807 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
10808 char *tmp;
10809 if (*calleridname)
10810 ast_string_field_set(p, cid_name, calleridname);
10811 tmp = ast_strdupa(rpid_num);
10812 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10813 ast_shrink_phone_number(tmp);
10814 ast_string_field_set(p, cid_num, tmp);
10815 }
10816
10817 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
10818
10819 if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
10820 if (sip_cancel_destroy(p))
10821 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
10822 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
10823 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10824
10825 if (p->sipoptions)
10826 user->sipoptions = p->sipoptions;
10827
10828
10829 if (user->call_limit)
10830 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
10831 if (!ast_strlen_zero(user->context))
10832 ast_string_field_set(p, context, user->context);
10833 if (!ast_strlen_zero(user->cid_num)) {
10834 char *tmp = ast_strdupa(user->cid_num);
10835 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10836 ast_shrink_phone_number(tmp);
10837 ast_string_field_set(p, cid_num, tmp);
10838 }
10839 if (!ast_strlen_zero(user->cid_name))
10840 ast_string_field_set(p, cid_name, user->cid_name);
10841 ast_string_field_set(p, username, user->name);
10842 ast_string_field_set(p, peername, user->name);
10843 ast_string_field_set(p, peersecret, user->secret);
10844 ast_string_field_set(p, peermd5secret, user->md5secret);
10845 ast_string_field_set(p, subscribecontext, user->subscribecontext);
10846 ast_string_field_set(p, accountcode, user->accountcode);
10847 ast_string_field_set(p, language, user->language);
10848 ast_string_field_set(p, mohsuggest, user->mohsuggest);
10849 ast_string_field_set(p, mohinterpret, user->mohinterpret);
10850 p->allowtransfer = user->allowtransfer;
10851 p->amaflags = user->amaflags;
10852 p->callgroup = user->callgroup;
10853 p->pickupgroup = user->pickupgroup;
10854 if (user->callingpres)
10855 p->callingpres = user->callingpres;
10856
10857
10858 p->capability = user->capability;
10859 p->jointcapability = user->capability;
10860 if (p->peercapability)
10861 p->jointcapability &= p->peercapability;
10862 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
10863 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
10864 p->noncodeccapability |= AST_RTP_DTMF;
10865 else
10866 p->noncodeccapability &= ~AST_RTP_DTMF;
10867 p->jointnoncodeccapability = p->noncodeccapability;
10868 if (p->t38.peercapability)
10869 p->t38.jointcapability &= p->t38.peercapability;
10870 p->maxcallbitrate = user->maxcallbitrate;
10871
10872 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
10873 ast_rtp_destroy(p->vrtp);
10874 p->vrtp = NULL;
10875 }
10876 }
10877 if (user && debug)
10878 ast_verbose("Found user '%s'\n", user->name);
10879 } else {
10880 if (user) {
10881 if (!authpeer && debug)
10882 ast_verbose("Found user '%s', but fails host access\n", user->name);
10883 ASTOBJ_UNREF(user,sip_destroy_user);
10884 }
10885 user = NULL;
10886 }
10887
10888 if (!user) {
10889
10890 if (sipmethod == SIP_SUBSCRIBE)
10891
10892 peer = find_peer(of, NULL, 1, 0);
10893 else
10894
10895
10896
10897
10898 peer = find_peer(NULL, &p->recv, 1, 0);
10899
10900 if (peer) {
10901
10902 if (p->rtp) {
10903 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
10904 p->autoframing = peer->autoframing;
10905 }
10906 if (debug)
10907 ast_verbose("Found peer '%s'\n", peer->name);
10908
10909
10910 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
10911 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10912
10913
10914 if (p->sipoptions)
10915 peer->sipoptions = p->sipoptions;
10916
10917
10918 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
10919 char *tmp = ast_strdupa(rpid_num);
10920 if (*calleridname)
10921 ast_string_field_set(p, cid_name, calleridname);
10922 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10923 ast_shrink_phone_number(tmp);
10924 ast_string_field_set(p, cid_num, tmp);
10925 }
10926 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
10927
10928 ast_string_field_set(p, peersecret, peer->secret);
10929 ast_string_field_set(p, peermd5secret, peer->md5secret);
10930 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
10931 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
10932 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
10933 if (peer->callingpres)
10934 p->callingpres = peer->callingpres;
10935 if (peer->maxms && peer->lastms)
10936 p->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
10937 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
10938
10939 ast_string_field_free(p, peersecret);
10940 ast_string_field_free(p, peermd5secret);
10941 }
10942 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
10943 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
10944 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
10945
10946 if (peer->call_limit)
10947 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
10948 ast_string_field_set(p, peername, peer->name);
10949 ast_string_field_set(p, authname, peer->name);
10950
10951 if (sipmethod == SIP_INVITE) {
10952
10953 ast_variables_destroy(p->chanvars);
10954 p->chanvars = NULL;
10955 for (v = peer->chanvars ; v ; v = v->next) {
10956 if ((tmpvar = ast_variable_new(v->name, v->value))) {
10957 tmpvar->next = p->chanvars;
10958 p->chanvars = tmpvar;
10959 }
10960 }
10961 }
10962 if (authpeer) {
10963 (*authpeer) = ASTOBJ_REF(peer);
10964 }
10965
10966 if (!ast_strlen_zero(peer->username)) {
10967 ast_string_field_set(p, username, peer->username);
10968
10969
10970 ast_string_field_set(p, authname, peer->username);
10971 }
10972 if (!ast_strlen_zero(peer->cid_num)) {
10973 char *tmp = ast_strdupa(peer->cid_num);
10974 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
10975 ast_shrink_phone_number(tmp);
10976 ast_string_field_set(p, cid_num, tmp);
10977 }
10978 if (!ast_strlen_zero(peer->cid_name))
10979 ast_string_field_set(p, cid_name, peer->cid_name);
10980 ast_string_field_set(p, fullcontact, peer->fullcontact);
10981 if (!ast_strlen_zero(peer->context))
10982 ast_string_field_set(p, context, peer->context);
10983 ast_string_field_set(p, peersecret, peer->secret);
10984 ast_string_field_set(p, peermd5secret, peer->md5secret);
10985 ast_string_field_set(p, language, peer->language);
10986 ast_string_field_set(p, accountcode, peer->accountcode);
10987 p->amaflags = peer->amaflags;
10988 p->callgroup = peer->callgroup;
10989 p->pickupgroup = peer->pickupgroup;
10990 p->capability = peer->capability;
10991 p->prefs = peer->prefs;
10992 p->jointcapability = peer->capability;
10993 if (p->peercapability)
10994 p->jointcapability &= p->peercapability;
10995 p->maxcallbitrate = peer->maxcallbitrate;
10996 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
10997 ast_rtp_destroy(p->vrtp);
10998 p->vrtp = NULL;
10999 }
11000 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
11001 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
11002 p->noncodeccapability |= AST_RTP_DTMF;
11003 else
11004 p->noncodeccapability &= ~AST_RTP_DTMF;
11005 p->jointnoncodeccapability = p->noncodeccapability;
11006 if (p->t38.peercapability)
11007 p->t38.jointcapability &= p->t38.peercapability;
11008 }
11009 ASTOBJ_UNREF(peer, sip_destroy_peer);
11010 } else {
11011 if (debug)
11012 ast_verbose("Found no matching peer or user for '%s:%d'\n", ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
11013
11014
11015 if (!global_allowguest) {
11016 if (global_alwaysauthreject)
11017 res = AUTH_FAKE_AUTH;
11018 else
11019 res = AUTH_SECRET_FAILED;
11020 } else if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
11021 char *tmp = ast_strdupa(rpid_num);
11022 if (*calleridname)
11023 ast_string_field_set(p, cid_name, calleridname);
11024 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
11025 ast_shrink_phone_number(tmp);
11026 ast_string_field_set(p, cid_num, tmp);
11027 }
11028 }
11029
11030 }
11031
11032 if (user)
11033 ASTOBJ_UNREF(user, sip_destroy_user);
11034
11035 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT)) {
11036 ast_set_flag(&p->flags[0], SIP_NAT_ROUTE);
11037 }
11038
11039 return res;
11040 }
11041
11042
11043
11044
11045 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin)
11046 {
11047 return check_user_full(p, req, sipmethod, uri, reliable, sin, NULL);
11048 }
11049
11050
11051 static int get_msg_text(char *buf, int len, struct sip_request *req)
11052 {
11053 int x;
11054 int y;
11055
11056 buf[0] = '\0';
11057 y = len - strlen(buf) - 5;
11058 if (y < 0)
11059 y = 0;
11060 for (x=0;x<req->lines;x++) {
11061 strncat(buf, req->line[x], y);
11062 y -= strlen(req->line[x]) + 1;
11063 if (y < 0)
11064 y = 0;
11065 if (y != 0)
11066 strcat(buf, "\n");
11067 }
11068 return 0;
11069 }
11070
11071
11072
11073
11074
11075 static void receive_message(struct sip_pvt *p, struct sip_request *req)
11076 {
11077 char buf[1024];
11078 struct ast_frame f;
11079 const char *content_type = get_header(req, "Content-Type");
11080
11081 if (strncmp(content_type, "text/plain", strlen("text/plain"))) {
11082 transmit_response(p, "415 Unsupported Media Type", req);
11083 if (!p->owner)
11084 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11085 return;
11086 }
11087
11088 if (get_msg_text(buf, sizeof(buf), req)) {
11089 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
11090 transmit_response(p, "202 Accepted", req);
11091 if (!p->owner)
11092 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11093 return;
11094 }
11095
11096 if (p->owner) {
11097 if (sip_debug_test_pvt(p))
11098 ast_verbose("Message received: '%s'\n", buf);
11099 memset(&f, 0, sizeof(f));
11100 f.frametype = AST_FRAME_TEXT;
11101 f.subclass = 0;
11102 f.offset = 0;
11103 f.data = buf;
11104 f.datalen = strlen(buf) + 1;
11105 ast_queue_frame(p->owner, &f);
11106 transmit_response(p, "202 Accepted", req);
11107 } else {
11108 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);
11109 transmit_response(p, "405 Method Not Allowed", req);
11110 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11111 }
11112 return;
11113 }
11114
11115
11116 static int sip_show_inuse(int fd, int argc, char *argv[])
11117 {
11118 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
11119 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
11120 char ilimits[40];
11121 char iused[40];
11122 int showall = FALSE;
11123
11124 if (argc < 3)
11125 return RESULT_SHOWUSAGE;
11126
11127 if (argc == 4 && !strcmp(argv[3],"all"))
11128 showall = TRUE;
11129
11130 ast_cli(fd, FORMAT, "* User name", "In use", "Limit");
11131 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
11132 ASTOBJ_RDLOCK(iterator);
11133 if (iterator->call_limit)
11134 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
11135 else
11136 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
11137 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
11138 if (showall || iterator->call_limit)
11139 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
11140 ASTOBJ_UNLOCK(iterator);
11141 } while (0) );
11142
11143 ast_cli(fd, FORMAT, "* Peer name", "In use", "Limit");
11144
11145 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
11146 ASTOBJ_RDLOCK(iterator);
11147 if (iterator->call_limit)
11148 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
11149 else
11150 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
11151 snprintf(iused, sizeof(iused), "%d/%d", iterator->inUse, iterator->inRinging);
11152 if (showall || iterator->call_limit)
11153 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
11154 ASTOBJ_UNLOCK(iterator);
11155 } while (0) );
11156
11157 return RESULT_SUCCESS;
11158 #undef FORMAT
11159 #undef FORMAT2
11160 }
11161
11162
11163 static char *transfermode2str(enum transfermodes mode)
11164 {
11165 if (mode == TRANSFER_OPENFORALL)
11166 return "open";
11167 else if (mode == TRANSFER_CLOSED)
11168 return "closed";
11169 return "strict";
11170 }
11171
11172
11173 static char *nat2str(int nat)
11174 {
11175 switch(nat) {
11176 case SIP_NAT_NEVER:
11177 return "No";
11178 case SIP_NAT_ROUTE:
11179 return "Route";
11180 case SIP_NAT_ALWAYS:
11181 return "Always";
11182 case SIP_NAT_RFC3581:
11183 return "RFC3581";
11184 default:
11185 return "Unknown";
11186 }
11187 }
11188
11189
11190
11191
11192 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
11193 {
11194 int res = 0;
11195 if (peer->maxms) {
11196 if (peer->lastms < 0) {
11197 ast_copy_string(status, "UNREACHABLE", statuslen);
11198 } else if (peer->lastms > peer->maxms) {
11199 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
11200 res = 1;
11201 } else if (peer->lastms) {
11202 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
11203 res = 1;
11204 } else {
11205 ast_copy_string(status, "UNKNOWN", statuslen);
11206 }
11207 } else {
11208 ast_copy_string(status, "Unmonitored", statuslen);
11209
11210 res = -1;
11211 }
11212 return res;
11213 }
11214
11215
11216 static int sip_show_users(int fd, int argc, char *argv[])
11217 {
11218 regex_t regexbuf;
11219 int havepattern = FALSE;
11220
11221 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
11222
11223 switch (argc) {
11224 case 5:
11225 if (!strcasecmp(argv[3], "like")) {
11226 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
11227 return RESULT_SHOWUSAGE;
11228 havepattern = TRUE;
11229 } else
11230 return RESULT_SHOWUSAGE;
11231 case 3:
11232 break;
11233 default:
11234 return RESULT_SHOWUSAGE;
11235 }
11236
11237 ast_cli(fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
11238 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
11239 ASTOBJ_RDLOCK(iterator);
11240
11241 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
11242 ASTOBJ_UNLOCK(iterator);
11243 continue;
11244 }
11245
11246 ast_cli(fd, FORMAT, iterator->name,
11247 iterator->secret,
11248 iterator->accountcode,
11249 iterator->context,
11250 iterator->ha ? "Yes" : "No",
11251 nat2str(ast_test_flag(&iterator->flags[0], SIP_NAT)));
11252 ASTOBJ_UNLOCK(iterator);
11253 } while (0)
11254 );
11255
11256 if (havepattern)
11257 regfree(®exbuf);
11258
11259 return RESULT_SUCCESS;
11260 #undef FORMAT
11261 }
11262
11263 static char mandescr_show_peers[] =
11264 "Description: Lists SIP peers in text format with details on current status.\n"
11265 "Variables: \n"
11266 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
11267
11268
11269
11270 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
11271 {
11272 const char *id = astman_get_header(m,"ActionID");
11273 const char *a[] = {"sip", "show", "peers"};
11274 char idtext[256] = "";
11275 int total = 0;
11276
11277 if (!ast_strlen_zero(id))
11278 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
11279
11280 astman_send_ack(s, m, "Peer status list will follow");
11281
11282 _sip_show_peers(-1, &total, s, m, 3, a);
11283
11284 astman_append(s,
11285 "Event: PeerlistComplete\r\n"
11286 "ListItems: %d\r\n"
11287 "%s"
11288 "\r\n", total, idtext);
11289 return 0;
11290 }
11291
11292
11293 static int sip_show_peers(int fd, int argc, char *argv[])
11294 {
11295 return _sip_show_peers(fd, NULL, NULL, NULL, argc, (const char **) argv);
11296 }
11297
11298
11299 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
11300 {
11301 regex_t regexbuf;
11302 int havepattern = FALSE;
11303
11304 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s %-10s\n"
11305 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s %-10s\n"
11306
11307 char name[256];
11308 int total_peers = 0;
11309 int peers_mon_online = 0;
11310 int peers_mon_offline = 0;
11311 int peers_unmon_offline = 0;
11312 int peers_unmon_online = 0;
11313 const char *id;
11314 char idtext[256] = "";
11315 int realtimepeers;
11316
11317 realtimepeers = ast_check_realtime("sippeers");
11318
11319 if (s) {
11320 id = astman_get_header(m,"ActionID");
11321 if (!ast_strlen_zero(id))
11322 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
11323 }
11324
11325 switch (argc) {
11326 case 5:
11327 if (!strcasecmp(argv[3], "like")) {
11328 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
11329 return RESULT_SHOWUSAGE;
11330 havepattern = TRUE;
11331 } else
11332 return RESULT_SHOWUSAGE;
11333 case 3:
11334 break;
11335 default:
11336 return RESULT_SHOWUSAGE;
11337 }
11338
11339 if (!s)
11340 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
11341
11342 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
11343 char status[20] = "";
11344 char srch[2000];
11345 char pstatus;
11346
11347 ASTOBJ_RDLOCK(iterator);
11348
11349 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
11350 ASTOBJ_UNLOCK(iterator);
11351 continue;
11352 }
11353
11354 if (!ast_strlen_zero(iterator->username) && !s)
11355 snprintf(name, sizeof(name), "%s/%s", iterator->name, iterator->username);
11356 else
11357 ast_copy_string(name, iterator->name, sizeof(name));
11358
11359 pstatus = peer_status(iterator, status, sizeof(status));
11360 if (pstatus == 1)
11361 peers_mon_online++;
11362 else if (pstatus == 0)
11363 peers_mon_offline++;
11364 else {
11365 if (iterator->addr.sin_port == 0)
11366 peers_unmon_offline++;
11367 else
11368 peers_unmon_online++;
11369 }
11370
11371 snprintf(srch, sizeof(srch), FORMAT, name,
11372 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
11373 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
11374 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
11375 iterator->ha ? " A " : " ",
11376 ntohs(iterator->addr.sin_port), status,
11377 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
11378
11379 if (!s) {
11380 ast_cli(fd, FORMAT, name,
11381 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
11382 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
11383 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
11384 iterator->ha ? " A " : " ",
11385
11386 ntohs(iterator->addr.sin_port), status,
11387 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
11388 } else {
11389
11390 astman_append(s,
11391 "Event: PeerEntry\r\n%s"
11392 "Channeltype: SIP\r\n"
11393 "ObjectName: %s\r\n"
11394 "ChanObjectType: peer\r\n"
11395 "IPaddress: %s\r\n"
11396 "IPport: %d\r\n"
11397 "Dynamic: %s\r\n"
11398 "Natsupport: %s\r\n"
11399 "VideoSupport: %s\r\n"
11400 "ACL: %s\r\n"
11401 "Status: %s\r\n"
11402 "RealtimeDevice: %s\r\n\r\n",
11403 idtext,
11404 iterator->name,
11405 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "-none-",
11406 ntohs(iterator->addr.sin_port),
11407 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no",
11408 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? "yes" : "no",
11409 ast_test_flag(&iterator->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
11410 iterator->ha ? "yes" : "no",
11411 status,
11412 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "yes":"no") : "no");
11413 }
11414
11415 ASTOBJ_UNLOCK(iterator);
11416
11417 total_peers++;
11418 } while(0) );
11419
11420 if (!s)
11421 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
11422 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
11423
11424 if (havepattern)
11425 regfree(®exbuf);
11426
11427 if (total)
11428 *total = total_peers;
11429
11430
11431 return RESULT_SUCCESS;
11432 #undef FORMAT
11433 #undef FORMAT2
11434 }
11435
11436
11437 static int sip_show_objects(int fd, int argc, char *argv[])
11438 {
11439 char tmp[256];
11440 if (argc != 3)
11441 return RESULT_SHOWUSAGE;
11442 ast_cli(fd, "-= User objects: %d static, %d realtime =-\n\n", suserobjs, ruserobjs);
11443 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &userl);
11444 ast_cli(fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
11445 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &peerl);
11446 ast_cli(fd, "-= Registry objects: %d =-\n\n", regobjs);
11447 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), ®l);
11448 return RESULT_SUCCESS;
11449 }
11450
11451 static void print_group(int fd, ast_group_t group, int crlf)
11452 {
11453 char buf[256];
11454 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
11455 }
11456
11457
11458 static const char *dtmfmode2str(int mode)
11459 {
11460 switch (mode) {
11461 case SIP_DTMF_RFC2833:
11462 return "rfc2833";
11463 case SIP_DTMF_INFO:
11464 return "info";
11465 case SIP_DTMF_INBAND:
11466 return "inband";
11467 case SIP_DTMF_AUTO:
11468 return "auto";
11469 }
11470 return "<error>";
11471 }
11472
11473
11474 static const char *insecure2str(int port, int invite)
11475 {
11476 if (port && invite)
11477 return "port,invite";
11478 else if (port)
11479 return "port";
11480 else if (invite)
11481 return "invite";
11482 else
11483 return "no";
11484 }
11485
11486
11487
11488
11489 static void cleanup_stale_contexts(char *new, char *old)
11490 {
11491 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
11492
11493 while ((oldcontext = strsep(&old, "&"))) {
11494 stalecontext = '\0';
11495 ast_copy_string(newlist, new, sizeof(newlist));
11496 stringp = newlist;
11497 while ((newcontext = strsep(&stringp, "&"))) {
11498 if (strcmp(newcontext, oldcontext) == 0) {
11499
11500 stalecontext = '\0';
11501 break;
11502 } else if (strcmp(newcontext, oldcontext)) {
11503 stalecontext = oldcontext;
11504 }
11505
11506 }
11507 if (stalecontext)
11508 ast_context_destroy(ast_context_find(stalecontext), "SIP");
11509 }
11510 }
11511
11512
11513 static int sip_prune_realtime(int fd, int argc, char *argv[])
11514 {
11515 struct sip_peer *peer;
11516 struct sip_user *user;
11517 int pruneuser = FALSE;
11518 int prunepeer = FALSE;
11519 int multi = FALSE;
11520 char *name = NULL;
11521 regex_t regexbuf;
11522 int havepattern = 0;
11523
11524 switch (argc) {
11525 case 4:
11526 if (!strcasecmp(argv[3], "user"))
11527 return RESULT_SHOWUSAGE;
11528 if (!strcasecmp(argv[3], "peer"))
11529 return RESULT_SHOWUSAGE;
11530 if (!strcasecmp(argv[3], "like"))
11531 return RESULT_SHOWUSAGE;
11532 if (!strcasecmp(argv[3], "all")) {
11533 multi = TRUE;
11534 pruneuser = prunepeer = TRUE;
11535 } else {
11536 pruneuser = prunepeer = TRUE;
11537 name = argv[3];
11538 }
11539 break;
11540 case 5:
11541 if (!strcasecmp(argv[4], "like"))
11542 return RESULT_SHOWUSAGE;
11543 if (!strcasecmp(argv[3], "all"))
11544 return RESULT_SHOWUSAGE;
11545 if (!strcasecmp(argv[3], "like")) {
11546 multi = TRUE;
11547 name = argv[4];
11548 pruneuser = prunepeer = TRUE;
11549 } else if (!strcasecmp(argv[3], "user")) {
11550 pruneuser = TRUE;
11551 if (!strcasecmp(argv[4], "all"))
11552 multi = TRUE;
11553 else
11554 name = argv[4];
11555 } else if (!strcasecmp(argv[3], "peer")) {
11556 prunepeer = TRUE;
11557 if (!strcasecmp(argv[4], "all"))
11558 multi = TRUE;
11559 else
11560 name = argv[4];
11561 } else
11562 return RESULT_SHOWUSAGE;
11563 break;
11564 case 6:
11565 if (strcasecmp(argv[4], "like"))
11566 return RESULT_SHOWUSAGE;
11567 if (!strcasecmp(argv[3], "user")) {
11568 pruneuser = TRUE;
11569 name = argv[5];
11570 } else if (!strcasecmp(argv[3], "peer")) {
11571 prunepeer = TRUE;
11572 name = argv[5];
11573 } else
11574 return RESULT_SHOWUSAGE;
11575 break;
11576 default:
11577 return RESULT_SHOWUSAGE;
11578 }
11579
11580 if (multi && name) {
11581 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB)) {
11582 return RESULT_SHOWUSAGE;
11583 }
11584 havepattern = 1;
11585 }
11586
11587 if (multi) {
11588 if (prunepeer) {
11589 int pruned = 0;
11590
11591 ASTOBJ_CONTAINER_WRLOCK(&peerl);
11592 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
11593 ASTOBJ_RDLOCK(iterator);
11594 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
11595 ASTOBJ_UNLOCK(iterator);
11596 continue;
11597 };
11598 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11599 ASTOBJ_MARK(iterator);
11600 pruned++;
11601 }
11602 ASTOBJ_UNLOCK(iterator);
11603 } while (0) );
11604 if (pruned) {
11605 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
11606 ast_cli(fd, "%d peers pruned.\n", pruned);
11607 } else
11608 ast_cli(fd, "No peers found to prune.\n");
11609 ASTOBJ_CONTAINER_UNLOCK(&peerl);
11610 }
11611 if (pruneuser) {
11612 int pruned = 0;
11613
11614 ASTOBJ_CONTAINER_WRLOCK(&userl);
11615 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
11616 ASTOBJ_RDLOCK(iterator);
11617 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
11618 ASTOBJ_UNLOCK(iterator);
11619 continue;
11620 };
11621 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11622 ASTOBJ_MARK(iterator);
11623 pruned++;
11624 }
11625 ASTOBJ_UNLOCK(iterator);
11626 } while (0) );
11627 if (pruned) {
11628 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
11629 ast_cli(fd, "%d users pruned.\n", pruned);
11630 } else
11631 ast_cli(fd, "No users found to prune.\n");
11632 ASTOBJ_CONTAINER_UNLOCK(&userl);
11633 }
11634 } else {
11635 if (prunepeer) {
11636 if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
11637 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11638 ast_cli(fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
11639 ASTOBJ_CONTAINER_LINK(&peerl, peer);
11640 } else
11641 ast_cli(fd, "Peer '%s' pruned.\n", name);
11642 ASTOBJ_UNREF(peer, sip_destroy_peer);
11643 } else
11644 ast_cli(fd, "Peer '%s' not found.\n", name);
11645 }
11646 if (pruneuser) {
11647 if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
11648 if (!ast_test_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
11649 ast_cli(fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
11650 ASTOBJ_CONTAINER_LINK(&userl, user);
11651 } else
11652 ast_cli(fd, "User '%s' pruned.\n", name);
11653 ASTOBJ_UNREF(user, sip_destroy_user);
11654 } else
11655 ast_cli(fd, "User '%s' not found.\n", name);
11656 }
11657 }
11658
11659 if (havepattern) {
11660 regfree(®exbuf);
11661 }
11662
11663 return RESULT_SUCCESS;
11664 }
11665
11666
11667 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
11668 {
11669 int x, codec;
11670
11671 for(x = 0; x < 32 ; x++) {
11672 codec = ast_codec_pref_index(pref, x);
11673 if (!codec)
11674 break;
11675 ast_cli(fd, "%s", ast_getformatname(codec));
11676 ast_cli(fd, ":%d", pref->framing[x]);
11677 if (x < 31 && ast_codec_pref_index(pref, x + 1))
11678 ast_cli(fd, ",");
11679 }
11680 if (!x)
11681 ast_cli(fd, "none");
11682 }
11683
11684
11685 static const char *domain_mode_to_text(const enum domain_mode mode)
11686 {
11687 switch (mode) {
11688 case SIP_DOMAIN_AUTO:
11689 return "[Automatic]";
11690 case SIP_DOMAIN_CONFIG:
11691 return "[Configured]";
11692 }
11693
11694 return "";
11695 }
11696
11697
11698 static int sip_show_domains(int fd, int argc, char *argv[])
11699 {
11700 struct domain *d;
11701 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
11702
11703 if (AST_LIST_EMPTY(&domain_list)) {
11704 ast_cli(fd, "SIP Domain support not enabled.\n\n");
11705 return RESULT_SUCCESS;
11706 } else {
11707 ast_cli(fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
11708 AST_LIST_LOCK(&domain_list);
11709 AST_LIST_TRAVERSE(&domain_list, d, list)
11710 ast_cli(fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
11711 domain_mode_to_text(d->mode));
11712 AST_LIST_UNLOCK(&domain_list);
11713 ast_cli(fd, "\n");
11714 return RESULT_SUCCESS;
11715 }
11716 }
11717 #undef FORMAT
11718
11719 static char mandescr_show_peer[] =
11720 "Description: Show one SIP peer with details on current status.\n"
11721 "Variables: \n"
11722 " Peer: <name> The peer name you want to check.\n"
11723 " ActionID: <id> Optional action ID for this AMI transaction.\n";
11724
11725
11726 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
11727 {
11728 const char *a[4];
11729 const char *peer;
11730 int ret;
11731
11732 peer = astman_get_header(m,"Peer");
11733 if (ast_strlen_zero(peer)) {
11734 astman_send_error(s, m, "Peer: <name> missing.");
11735 return 0;
11736 }
11737 a[0] = "sip";
11738 a[1] = "show";
11739 a[2] = "peer";
11740 a[3] = peer;
11741
11742 ret = _sip_show_peer(1, -1, s, m, 4, a);
11743 astman_append(s, "\r\n\r\n" );
11744 return ret;
11745 }
11746
11747
11748
11749
11750 static int sip_show_peer(int fd, int argc, char *argv[])
11751 {
11752 return _sip_show_peer(0, fd, NULL, NULL, argc, (const char **) argv);
11753 }
11754
11755
11756 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
11757 {
11758 char status[30] = "";
11759 char cbuf[256];
11760 struct sip_peer *peer;
11761 char codec_buf[512];
11762 struct ast_codec_pref *pref;
11763 struct ast_variable *v;
11764 struct sip_auth *auth;
11765 int x = 0, codec = 0, load_realtime;
11766 int realtimepeers;
11767
11768 realtimepeers = ast_check_realtime("sippeers");
11769
11770 if (argc < 4)
11771 return RESULT_SHOWUSAGE;
11772
11773 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
11774 peer = find_peer(argv[3], NULL, load_realtime, 0);
11775 if (s) {
11776 if (peer) {
11777 const char *id = astman_get_header(m,"ActionID");
11778
11779 astman_append(s, "Response: Success\r\n");
11780 if (!ast_strlen_zero(id))
11781 astman_append(s, "ActionID: %s\r\n",id);
11782 } else {
11783 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.", argv[3]);
11784 astman_send_error(s, m, cbuf);
11785 return 0;
11786 }
11787 }
11788 if (peer && type==0 ) {
11789 ast_cli(fd,"\n\n");
11790 ast_cli(fd, " * Name : %s\n", peer->name);
11791 if (realtimepeers) {
11792 ast_cli(fd, " Realtime peer: %s\n", ast_test_flag(&peer->flags[0], SIP_REALTIME) ? "Yes, cached" : "No");
11793 }
11794 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
11795 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
11796 for (auth = peer->auth; auth; auth = auth->next) {
11797 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
11798 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
11799 }
11800 ast_cli(fd, " Context : %s\n", peer->context);
11801 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
11802 ast_cli(fd, " Language : %s\n", peer->language);
11803 if (!ast_strlen_zero(peer->accountcode))
11804 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
11805 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
11806 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
11807 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
11808 if (!ast_strlen_zero(peer->fromuser))
11809 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
11810 if (!ast_strlen_zero(peer->fromdomain))
11811 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
11812 ast_cli(fd, " Callgroup : ");
11813 print_group(fd, peer->callgroup, 0);
11814 ast_cli(fd, " Pickupgroup : ");
11815 print_group(fd, peer->pickupgroup, 0);
11816 ast_cli(fd, " Mailbox : %s\n", peer->mailbox);
11817 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
11818 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
11819 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
11820 ast_cli(fd, " Dynamic : %s\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Yes":"No"));
11821 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
11822 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
11823 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
11824 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)));
11825 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
11826 ast_cli(fd, " ACL : %s\n", (peer->ha?"Yes":"No"));
11827 ast_cli(fd, " T38 pt UDPTL : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)?"Yes":"No");
11828 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
11829 ast_cli(fd, " T38 pt RTP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_RTP)?"Yes":"No");
11830 ast_cli(fd, " T38 pt TCP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_TCP)?"Yes":"No");
11831 #endif
11832 ast_cli(fd, " CanReinvite : %s\n", ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Yes":"No");
11833 ast_cli(fd, " PromiscRedir : %s\n", ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Yes":"No");
11834 ast_cli(fd, " User=Phone : %s\n", ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Yes":"No");
11835 ast_cli(fd, " Video Support: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Yes":"No");
11836 ast_cli(fd, " Trust RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_TRUSTRPID) ? "Yes" : "No");
11837 ast_cli(fd, " Send RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_SENDRPID) ? "Yes" : "No");
11838 ast_cli(fd, " Subscriptions: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
11839 ast_cli(fd, " Overlap dial : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
11840 ast_cli(fd, " Forward Loop : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_FORWARD_LOOP_DETECTED) ? "Yes" : "No");
11841
11842
11843 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
11844 ast_cli(fd, " LastMsg : %d\n", peer->lastmsg);
11845 ast_cli(fd, " ToHost : %s\n", peer->tohost);
11846 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));
11847 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
11848 if (!ast_strlen_zero(global_regcontext))
11849 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
11850 ast_cli(fd, " Def. Username: %s\n", peer->username);
11851 ast_cli(fd, " SIP Options : ");
11852 if (peer->sipoptions) {
11853 int lastoption = -1;
11854 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
11855 if (sip_options[x].id != lastoption) {
11856 if (peer->sipoptions & sip_options[x].id)
11857 ast_cli(fd, "%s ", sip_options[x].text);
11858 lastoption = x;
11859 }
11860 }
11861 } else
11862 ast_cli(fd, "(none)");
11863
11864 ast_cli(fd, "\n");
11865 ast_cli(fd, " Codecs : ");
11866 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
11867 ast_cli(fd, "%s\n", codec_buf);
11868 ast_cli(fd, " Codec Order : (");
11869 print_codec_to_cli(fd, &peer->prefs);
11870 ast_cli(fd, ")\n");
11871
11872 ast_cli(fd, " Auto-Framing: %s \n", peer->autoframing ? "Yes" : "No");
11873 ast_cli(fd, " Status : ");
11874 peer_status(peer, status, sizeof(status));
11875 ast_cli(fd, "%s\n",status);
11876 ast_cli(fd, " Useragent : %s\n", peer->useragent);
11877 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
11878 if (peer->chanvars) {
11879 ast_cli(fd, " Variables :\n");
11880 for (v = peer->chanvars ; v ; v = v->next)
11881 ast_cli(fd, " %s = %s\n", v->name, v->value);
11882 }
11883 ast_cli(fd,"\n");
11884 ASTOBJ_UNREF(peer,sip_destroy_peer);
11885 } else if (peer && type == 1) {
11886 char buf[256];
11887 astman_append(s, "Channeltype: SIP\r\n");
11888 astman_append(s, "ObjectName: %s\r\n", peer->name);
11889 astman_append(s, "ChanObjectType: peer\r\n");
11890 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
11891 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
11892 astman_append(s, "Context: %s\r\n", peer->context);
11893 astman_append(s, "Language: %s\r\n", peer->language);
11894 if (!ast_strlen_zero(peer->accountcode))
11895 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
11896 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
11897 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
11898 if (!ast_strlen_zero(peer->fromuser))
11899 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
11900 if (!ast_strlen_zero(peer->fromdomain))
11901 astman_append(s, "SIP-FromDomain: %s\r\n", peer->fromdomain);
11902 astman_append(s, "Callgroup: ");
11903 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->callgroup));
11904 astman_append(s, "Pickupgroup: ");
11905 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->pickupgroup));
11906 astman_append(s, "VoiceMailbox: %s\r\n", peer->mailbox);
11907 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
11908 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
11909 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
11910 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
11911 astman_append(s, "Dynamic: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Y":"N"));
11912 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
11913 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched,peer->expire));
11914 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)));
11915 astman_append(s, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
11916 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
11917 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Y":"N"));
11918 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
11919 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
11920 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
11921
11922
11923 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
11924 astman_append(s, "SIPLastMsg: %d\r\n", peer->lastmsg);
11925 astman_append(s, "ToHost: %s\r\n", peer->tohost);
11926 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));
11927 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));
11928 astman_append(s, "Default-Username: %s\r\n", peer->username);
11929 if (!ast_strlen_zero(global_regcontext))
11930 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
11931 astman_append(s, "Codecs: ");
11932 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
11933 astman_append(s, "%s\r\n", codec_buf);
11934 astman_append(s, "CodecOrder: ");
11935 pref = &peer->prefs;
11936 for(x = 0; x < 32 ; x++) {
11937 codec = ast_codec_pref_index(pref,x);
11938 if (!codec)
11939 break;
11940 astman_append(s, "%s", ast_getformatname(codec));
11941 if (x < 31 && ast_codec_pref_index(pref,x+1))
11942 astman_append(s, ",");
11943 }
11944
11945 astman_append(s, "\r\n");
11946 astman_append(s, "Status: ");
11947 peer_status(peer, status, sizeof(status));
11948 astman_append(s, "%s\r\n", status);
11949 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
11950 astman_append(s, "Reg-Contact : %s\r\n", peer->fullcontact);
11951 if (peer->chanvars) {
11952 for (v = peer->chanvars ; v ; v = v->next) {
11953 astman_append(s, "ChanVariable:\n");
11954 astman_append(s, " %s,%s\r\n", v->name, v->value);
11955 }
11956 }
11957
11958 ASTOBJ_UNREF(peer,sip_destroy_peer);
11959
11960 } else {
11961 ast_cli(fd,"Peer %s not found.\n", argv[3]);
11962 ast_cli(fd,"\n");
11963 }
11964
11965 return RESULT_SUCCESS;
11966 }
11967
11968
11969 static int sip_show_user(int fd, int argc, char *argv[])
11970 {
11971 char cbuf[256];
11972 struct sip_user *user;
11973 struct ast_variable *v;
11974 int load_realtime;
11975
11976 if (argc < 4)
11977 return RESULT_SHOWUSAGE;
11978
11979
11980 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
11981
11982 user = find_user(argv[3], load_realtime);
11983 if (user) {
11984 ast_cli(fd,"\n\n");
11985 ast_cli(fd, " * Name : %s\n", user->name);
11986 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
11987 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
11988 ast_cli(fd, " Context : %s\n", user->context);
11989 ast_cli(fd, " Language : %s\n", user->language);
11990 if (!ast_strlen_zero(user->accountcode))
11991 ast_cli(fd, " Accountcode : %s\n", user->accountcode);
11992 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
11993 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
11994 ast_cli(fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
11995 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
11996 ast_cli(fd, " Call limit : %d\n", user->call_limit);
11997 ast_cli(fd, " Callgroup : ");
11998 print_group(fd, user->callgroup, 0);
11999 ast_cli(fd, " Pickupgroup : ");
12000 print_group(fd, user->pickupgroup, 0);
12001 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
12002 ast_cli(fd, " ACL : %s\n", (user->ha?"Yes":"No"));
12003 ast_cli(fd, " Codec Order : (");
12004 print_codec_to_cli(fd, &user->prefs);
12005 ast_cli(fd, ")\n");
12006
12007 ast_cli(fd, " Auto-Framing: %s \n", user->autoframing ? "Yes" : "No");
12008 if (user->chanvars) {
12009 ast_cli(fd, " Variables :\n");
12010 for (v = user->chanvars ; v ; v = v->next)
12011 ast_cli(fd, " %s = %s\n", v->name, v->value);
12012 }
12013 ast_cli(fd,"\n");
12014 ASTOBJ_UNREF(user,sip_destroy_user);
12015 } else {
12016 ast_cli(fd,"User %s not found.\n", argv[3]);
12017 ast_cli(fd,"\n");
12018 }
12019
12020 return RESULT_SUCCESS;
12021 }
12022
12023
12024 static int sip_show_registry(int fd, int argc, char *argv[])
12025 {
12026 #define FORMAT2 "%-30.30s %-12.12s %8.8s %-20.20s %-25.25s\n"
12027 #define FORMAT "%-30.30s %-12.12s %8d %-20.20s %-25.25s\n"
12028 char host[80];
12029 char tmpdat[256];
12030 struct tm tm;
12031
12032
12033 if (argc != 3)
12034 return RESULT_SHOWUSAGE;
12035 ast_cli(fd, FORMAT2, "Host", "Username", "Refresh", "State", "Reg.Time");
12036 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
12037 ASTOBJ_RDLOCK(iterator);
12038 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
12039 if (iterator->regtime) {
12040 ast_localtime(&iterator->regtime, &tm, NULL);
12041 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
12042 } else {
12043 tmpdat[0] = 0;
12044 }
12045 ast_cli(fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
12046 ASTOBJ_UNLOCK(iterator);
12047 } while(0));
12048 return RESULT_SUCCESS;
12049 #undef FORMAT
12050 #undef FORMAT2
12051 }
12052
12053
12054 static int sip_show_settings(int fd, int argc, char *argv[])
12055 {
12056 int realtimepeers;
12057 int realtimeusers;
12058 char codec_buf[SIPBUFSIZE];
12059
12060 realtimepeers = ast_check_realtime("sippeers");
12061 realtimeusers = ast_check_realtime("sipusers");
12062
12063 if (argc != 3)
12064 return RESULT_SHOWUSAGE;
12065 ast_cli(fd, "\n\nGlobal Settings:\n");
12066 ast_cli(fd, "----------------\n");
12067 ast_cli(fd, " SIP Port: %d\n", ntohs(bindaddr.sin_port));
12068 ast_cli(fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
12069 ast_cli(fd, " Videosupport: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "Yes" : "No");
12070 ast_cli(fd, " AutoCreatePeer: %s\n", autocreatepeer ? "Yes" : "No");
12071 ast_cli(fd, " Allow unknown access: %s\n", global_allowguest ? "Yes" : "No");
12072 ast_cli(fd, " Allow subscriptions: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
12073 ast_cli(fd, " Allow overlap dialing: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
12074 ast_cli(fd, " Promsic. redir: %s\n", ast_test_flag(&global_flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
12075 ast_cli(fd, " SIP domain support: %s\n", AST_LIST_EMPTY(&domain_list) ? "No" : "Yes");
12076 ast_cli(fd, " Call to non-local dom.: %s\n", allow_external_domains ? "Yes" : "No");
12077 ast_cli(fd, " URI user is phone no: %s\n", ast_test_flag(&global_flags[0], SIP_USEREQPHONE) ? "Yes" : "No");
12078 ast_cli(fd, " Our auth realm %s\n", global_realm);
12079 ast_cli(fd, " Realm. auth: %s\n", authl ? "Yes": "No");
12080 ast_cli(fd, " Always auth rejects: %s\n", global_alwaysauthreject ? "Yes" : "No");
12081 ast_cli(fd, " Call limit peers only: %s\n", global_limitonpeers ? "Yes" : "No");
12082 ast_cli(fd, " Direct RTP setup: %s\n", global_directrtpsetup ? "Yes" : "No");
12083 ast_cli(fd, " User Agent: %s\n", global_useragent);
12084 ast_cli(fd, " MWI checking interval: %d secs\n", global_mwitime);
12085 ast_cli(fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
12086 ast_cli(fd, " Caller ID: %s\n", default_callerid);
12087 ast_cli(fd, " From: Domain: %s\n", default_fromdomain);
12088 ast_cli(fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
12089 ast_cli(fd, " Call Events: %s\n", global_callevents ? "On" : "Off");
12090 ast_cli(fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
12091 ast_cli(fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
12092 ast_cli(fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
12093 ast_cli(fd, " T38 fax pt UDPTL: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL) ? "Yes" : "No");
12094 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
12095 ast_cli(fd, " T38 fax pt RTP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_RTP) ? "Yes" : "No");
12096 ast_cli(fd, " T38 fax pt TCP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_TCP) ? "Yes" : "No");
12097 #endif
12098 ast_cli(fd, " RFC2833 Compensation: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE) ? "Yes" : "No");
12099 if (!realtimepeers && !realtimeusers)
12100 ast_cli(fd, " SIP realtime: Disabled\n" );
12101 else
12102 ast_cli(fd, " SIP realtime: Enabled\n" );
12103
12104 ast_cli(fd, "\nGlobal Signalling Settings:\n");
12105 ast_cli(fd, "---------------------------\n");
12106 ast_cli(fd, " Codecs: ");
12107 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, global_capability);
12108 ast_cli(fd, "%s\n", codec_buf);
12109 ast_cli(fd, " Codec Order: ");
12110 print_codec_to_cli(fd, &default_prefs);
12111 ast_cli(fd, "\n");
12112 ast_cli(fd, " T1 minimum: %d\n", global_t1min);
12113 ast_cli(fd, " No premature media: %s\n", global_prematuremediafilter ? "Yes" : "No");
12114 ast_cli(fd, " Relax DTMF: %s\n", global_relaxdtmf ? "Yes" : "No");
12115 ast_cli(fd, " Compact SIP headers: %s\n", compactheaders ? "Yes" : "No");
12116 ast_cli(fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
12117 ast_cli(fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
12118 ast_cli(fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
12119 ast_cli(fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
12120 ast_cli(fd, " DNS SRV lookup: %s\n", srvlookup ? "Yes" : "No");
12121 ast_cli(fd, " Pedantic SIP support: %s\n", pedanticsipchecking ? "Yes" : "No");
12122 ast_cli(fd, " Reg. min duration %d secs\n", min_expiry);
12123 ast_cli(fd, " Reg. max duration: %d secs\n", max_expiry);
12124 ast_cli(fd, " Reg. default duration: %d secs\n", default_expiry);
12125 ast_cli(fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
12126 ast_cli(fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
12127 ast_cli(fd, " Notify ringing state: %s\n", global_notifyringing ? "Yes" : "No");
12128 ast_cli(fd, " Notify hold state: %s\n", global_notifyhold ? "Yes" : "No");
12129 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(global_allowtransfer));
12130 ast_cli(fd, " Max Call Bitrate: %d kbps\r\n", default_maxcallbitrate);
12131 ast_cli(fd, " Auto-Framing: %s \r\n", global_autoframing ? "Yes" : "No");
12132 ast_cli(fd, "\nDefault Settings:\n");
12133 ast_cli(fd, "-----------------\n");
12134 ast_cli(fd, " Context: %s\n", default_context);
12135 ast_cli(fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags[0], SIP_NAT)));
12136 ast_cli(fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
12137 ast_cli(fd, " Qualify: %d\n", default_qualify);
12138 ast_cli(fd, " Use ClientCode: %s\n", ast_test_flag(&global_flags[0], SIP_USECLIENTCODE) ? "Yes" : "No");
12139 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" );
12140 ast_cli(fd, " Language: %s\n", S_OR(default_language, "(Defaults to English)"));
12141 ast_cli(fd, " MOH Interpret: %s\n", default_mohinterpret);
12142 ast_cli(fd, " MOH Suggest: %s\n", default_mohsuggest);
12143 ast_cli(fd, " Voice Mail Extension: %s\n", default_vmexten);
12144 ast_cli(fd, " Forward Detected Loops: %s\n", (ast_test_flag(&global_flags[1], SIP_PAGE2_FORWARD_LOOP_DETECTED) ? "Yes" : "No"));
12145
12146 if (realtimepeers || realtimeusers) {
12147 ast_cli(fd, "\nRealtime SIP Settings:\n");
12148 ast_cli(fd, "----------------------\n");
12149 ast_cli(fd, " Realtime Peers: %s\n", realtimepeers ? "Yes" : "No");
12150 ast_cli(fd, " Realtime Users: %s\n", realtimeusers ? "Yes" : "No");
12151 ast_cli(fd, " Cache Friends: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? "Yes" : "No");
12152 ast_cli(fd, " Update: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) ? "Yes" : "No");
12153 ast_cli(fd, " Ignore Reg. Expire: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) ? "Yes" : "No");
12154 ast_cli(fd, " Save sys. name: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME) ? "Yes" : "No");
12155 ast_cli(fd, " Auto Clear: %d (%s)", global_rtautoclear, ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR) ? "Enabled" : "Disabled");
12156 }
12157 ast_cli(fd, "\n----\n");
12158 return RESULT_SUCCESS;
12159 }
12160
12161
12162 static const char *subscription_type2str(enum subscriptiontype subtype)
12163 {
12164 int i;
12165
12166 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
12167 if (subscription_types[i].type == subtype) {
12168 return subscription_types[i].text;
12169 }
12170 }
12171 return subscription_types[0].text;
12172 }
12173
12174
12175 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
12176 {
12177 int i;
12178
12179 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
12180 if (subscription_types[i].type == subtype) {
12181 return &subscription_types[i];
12182 }
12183 }
12184 return &subscription_types[0];
12185 }
12186
12187
12188 static int sip_show_channels(int fd, int argc, char *argv[])
12189 {
12190 return __sip_show_channels(fd, argc, argv, 0);
12191 }
12192
12193
12194 static int sip_show_subscriptions(int fd, int argc, char *argv[])
12195 {
12196 return __sip_show_channels(fd, argc, argv, 1);
12197 }
12198
12199
12200 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions)
12201 {
12202 #define FORMAT3L "%-15.15s %-15.15s %-11.11s %-15.15s %-13.13s %-15.15s %-10.10s %6d\n"
12203 #define FORMAT3H "%-15.15s %-15.15s %-11.11s %-15.15s %-13.13s %-15.15s %-10.10s %-6s\n"
12204 #define FORMAT2 "%-15.15s %-15.15s %-11.11s %-11.11s %-15.15s %-7.7s %-15.15s\n"
12205 #define FORMAT "%-15.15s %-15.15s %-11.11s %5.5d/%5.5d %-15.15s %-3.3s %-3.3s %-15.15s %-10.10s\n"
12206 struct sip_pvt *cur;
12207 int numchans = 0;
12208 int usedchans = 0;
12209 char *referstatus = NULL;
12210
12211 if (argc != 3)
12212 return RESULT_SHOWUSAGE;
12213 ast_mutex_lock(&iflock);
12214 cur = iflist;
12215 if (!subscriptions)
12216 ast_cli(fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Seq (Tx/Rx)", "Format", "Hold", "Last Message");
12217 else
12218 ast_cli(fd, FORMAT3H, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox", "Expiry");
12219 for (; cur; cur = cur->next) {
12220 referstatus = "";
12221 if (cur->refer) {
12222 referstatus = referstatus2str(cur->refer->status);
12223 }
12224 if (cur->subscribed == NONE && !subscriptions) {
12225 char formatbuf[SIPBUFSIZE/2];
12226 ast_cli(fd, FORMAT, ast_inet_ntoa(cur->sa.sin_addr),
12227 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
12228 cur->callid,
12229 cur->ocseq, cur->icseq,
12230 ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
12231 ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD) ? "Yes" : "No",
12232 ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY) ? "(d)" : "",
12233 cur->lastmsg ,
12234 referstatus
12235 );
12236 numchans++;
12237 }
12238 if (cur->subscribed != NONE && subscriptions) {
12239 ast_cli(fd, FORMAT3L, ast_inet_ntoa(cur->sa.sin_addr),
12240 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
12241 cur->callid,
12242
12243 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
12244 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
12245 subscription_type2str(cur->subscribed),
12246 cur->subscribed == MWI_NOTIFICATION ? (cur->relatedpeer ? cur->relatedpeer->mailbox : "<none>") : "<none>",
12247 cur->expiry
12248 );
12249 numchans++;
12250 }
12251 if (cur->owner) {
12252 usedchans++;
12253 }
12254 }
12255 ast_mutex_unlock(&iflock);
12256 if (!subscriptions)
12257 ast_cli(fd, "%d active SIP dialog%s\n", numchans, (numchans != 1) ? "s" : "");
12258 else
12259 ast_cli(fd, "%d active SIP subscription%s\n", numchans, (numchans != 1) ? "s" : "");
12260 ast_cli(fd, "%d used SIP channel%s\n", usedchans, (usedchans != 1) ? "s" : "");
12261 return RESULT_SUCCESS;
12262 #undef FORMAT
12263 #undef FORMAT2
12264 #undef FORMAT3
12265 }
12266
12267
12268 static char *complete_sipch(const char *line, const char *word, int pos, int state)
12269 {
12270 int which=0;
12271 struct sip_pvt *cur;
12272 char *c = NULL;
12273 int wordlen = strlen(word);
12274
12275 if (pos != 3) {
12276 return NULL;
12277 }
12278
12279 ast_mutex_lock(&iflock);
12280 for (cur = iflist; cur; cur = cur->next) {
12281 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
12282 c = ast_strdup(cur->callid);
12283 break;
12284 }
12285 }
12286 ast_mutex_unlock(&iflock);
12287 return c;
12288 }
12289
12290
12291 static char *complete_sip_peer(const char *word, int state, int flags2)
12292 {
12293 char *result = NULL;
12294 int wordlen = strlen(word);
12295 int which = 0;
12296
12297 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
12298
12299 if (!strncasecmp(word, iterator->name, wordlen) &&
12300 (!flags2 || ast_test_flag(&iterator->flags[1], flags2)) &&
12301 ++which > state)
12302 result = ast_strdup(iterator->name);
12303 } while(0) );
12304 return result;
12305 }
12306
12307
12308 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
12309 {
12310 if (pos == 3)
12311 return complete_sip_peer(word, state, 0);
12312
12313 return NULL;
12314 }
12315
12316
12317 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state)
12318 {
12319 if (pos == 3)
12320 return complete_sip_peer(word, state, 0);
12321
12322 return NULL;
12323 }
12324
12325
12326 static char *complete_sip_user(const char *word, int state, int flags2)
12327 {
12328 char *result = NULL;
12329 int wordlen = strlen(word);
12330 int which = 0;
12331
12332 ASTOBJ_CONTAINER_TRAVERSE(&userl, !result, do {
12333
12334 if (!strncasecmp(word, iterator->name, wordlen)) {
12335 if (flags2 && !ast_test_flag(&iterator->flags[1], flags2))
12336 continue;
12337 if (++which > state) {
12338 result = ast_strdup(iterator->name);
12339 }
12340 }
12341 } while(0) );
12342 return result;
12343 }
12344
12345
12346 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
12347 {
12348 if (pos == 3)
12349 return complete_sip_user(word, state, 0);
12350
12351 return NULL;
12352 }
12353
12354
12355 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
12356 {
12357 char *c = NULL;
12358
12359 if (pos == 2) {
12360 int which = 0;
12361 char *cat = NULL;
12362 int wordlen = strlen(word);
12363
12364
12365
12366 if (!notify_types)
12367 return NULL;
12368
12369 while ( (cat = ast_category_browse(notify_types, cat)) ) {
12370 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
12371 c = ast_strdup(cat);
12372 break;
12373 }
12374 }
12375 return c;
12376 }
12377
12378 if (pos > 2)
12379 return complete_sip_peer(word, state, 0);
12380
12381 return NULL;
12382 }
12383
12384
12385 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state)
12386 {
12387 if (pos == 4)
12388 return complete_sip_peer(word, state, SIP_PAGE2_RTCACHEFRIENDS);
12389 return NULL;
12390 }
12391
12392
12393 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state)
12394 {
12395 if (pos == 4)
12396 return complete_sip_user(word, state, SIP_PAGE2_RTCACHEFRIENDS);
12397
12398 return NULL;
12399 }
12400
12401
12402 static int sip_show_channel(int fd, int argc, char *argv[])
12403 {
12404 struct sip_pvt *cur;
12405 size_t len;
12406 int found = 0;
12407
12408 if (argc != 4)
12409 return RESULT_SHOWUSAGE;
12410 len = strlen(argv[3]);
12411 ast_mutex_lock(&iflock);
12412 for (cur = iflist; cur; cur = cur->next) {
12413 if (!strncasecmp(cur->callid, argv[3], len)) {
12414 char formatbuf[SIPBUFSIZE/2];
12415 ast_cli(fd,"\n");
12416 if (cur->subscribed != NONE)
12417 ast_cli(fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
12418 else
12419 ast_cli(fd, " * SIP Call\n");
12420 ast_cli(fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
12421 ast_cli(fd, " Call-ID: %s\n", cur->callid);
12422 ast_cli(fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
12423 ast_cli(fd, " Our Codec Capability: %d\n", cur->capability);
12424 ast_cli(fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
12425 ast_cli(fd, " Their Codec Capability: %d\n", cur->peercapability);
12426 ast_cli(fd, " Joint Codec Capability: %d\n", cur->jointcapability);
12427 ast_cli(fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
12428 ast_cli(fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
12429 ast_cli(fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(cur->sa.sin_addr), ntohs(cur->sa.sin_port));
12430 ast_cli(fd, " Received Address: %s:%d\n", ast_inet_ntoa(cur->recv.sin_addr), ntohs(cur->recv.sin_port));
12431 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
12432 ast_cli(fd, " NAT Support: %s\n", nat2str(ast_test_flag(&cur->flags[0], SIP_NAT)));
12433 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)" );
12434 ast_cli(fd, " Our Tag: %s\n", cur->tag);
12435 ast_cli(fd, " Their Tag: %s\n", cur->theirtag);
12436 ast_cli(fd, " SIP User agent: %s\n", cur->useragent);
12437 if (!ast_strlen_zero(cur->username))
12438 ast_cli(fd, " Username: %s\n", cur->username);
12439 if (!ast_strlen_zero(cur->peername))
12440 ast_cli(fd, " Peername: %s\n", cur->peername);
12441 if (!ast_strlen_zero(cur->uri))
12442 ast_cli(fd, " Original uri: %s\n", cur->uri);
12443 if (!ast_strlen_zero(cur->cid_num))
12444 ast_cli(fd, " Caller-ID: %s\n", cur->cid_num);
12445 ast_cli(fd, " Need Destroy: %d\n", ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY));
12446 ast_cli(fd, " Last Message: %s\n", cur->lastmsg);
12447 ast_cli(fd, " Promiscuous Redir: %s\n", ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
12448 ast_cli(fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
12449 ast_cli(fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
12450 ast_cli(fd, " SIP Options: ");
12451 if (cur->sipoptions) {
12452 int x;
12453 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
12454 if (cur->sipoptions & sip_options[x].id)
12455 ast_cli(fd, "%s ", sip_options[x].text);
12456 }
12457 } else
12458 ast_cli(fd, "(none)\n");
12459 ast_cli(fd, "\n\n");
12460 found++;
12461 }
12462 }
12463 ast_mutex_unlock(&iflock);
12464 if (!found)
12465 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
12466 return RESULT_SUCCESS;
12467 }
12468
12469
12470 static int sip_show_history(int fd, int argc, char *argv[])
12471 {
12472 struct sip_pvt *cur;
12473 size_t len;
12474 int found = 0;
12475
12476 if (argc != 4)
12477 return RESULT_SHOWUSAGE;
12478 if (!recordhistory)
12479 ast_cli(fd, "\n***Note: History recording is currently DISABLED. Use 'sip history' to ENABLE.\n");
12480 len = strlen(argv[3]);
12481 ast_mutex_lock(&iflock);
12482 for (cur = iflist; cur; cur = cur->next) {
12483 if (!strncasecmp(cur->callid, argv[3], len)) {
12484 struct sip_history *hist;
12485 int x = 0;
12486
12487 ast_cli(fd,"\n");
12488 if (cur->subscribed != NONE)
12489 ast_cli(fd, " * Subscription\n");
12490 else
12491 ast_cli(fd, " * SIP Call\n");
12492 if (cur->history)
12493 AST_LIST_TRAVERSE(cur->history, hist, list)
12494 ast_cli(fd, "%d. %s\n", ++x, hist->event);
12495 if (x == 0)
12496 ast_cli(fd, "Call '%s' has no history\n", cur->callid);
12497 found++;
12498 }
12499 }
12500 ast_mutex_unlock(&iflock);
12501 if (!found)
12502 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
12503 return RESULT_SUCCESS;
12504 }
12505
12506
12507 static void sip_dump_history(struct sip_pvt *dialog)
12508 {
12509 int x = 0;
12510 struct sip_history *hist;
12511 static int errmsg = 0;
12512
12513 if (!dialog)
12514 return;
12515
12516 if (!option_debug && !sipdebug) {
12517 if (!errmsg) {
12518 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
12519 errmsg = 1;
12520 }
12521 return;
12522 }
12523
12524 ast_log(LOG_DEBUG, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
12525 if (dialog->subscribed)
12526 ast_log(LOG_DEBUG, " * Subscription\n");
12527 else
12528 ast_log(LOG_DEBUG, " * SIP Call\n");
12529 if (dialog->history)
12530 AST_LIST_TRAVERSE(dialog->history, hist, list)
12531 ast_log(LOG_DEBUG, " %-3.3d. %s\n", ++x, hist->event);
12532 if (!x)
12533 ast_log(LOG_DEBUG, "Call '%s' has no history\n", dialog->callid);
12534 ast_log(LOG_DEBUG, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
12535 }
12536
12537
12538
12539
12540 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
12541 {
12542 char buf[1024];
12543 unsigned int event;
12544 const char *c = get_header(req, "Content-Type");
12545
12546
12547 if (!strcasecmp(c, "application/dtmf-relay") ||
12548 !strcasecmp(c, "application/DTMF") ||
12549 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
12550 unsigned int duration = 0;
12551
12552
12553 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
12554 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
12555 transmit_response(p, "200 OK", req);
12556 return;
12557 } else {
12558 ast_copy_string(buf, c, sizeof(buf));
12559 }
12560
12561 if (!ast_strlen_zero((c = get_body(req, "Duration"))))
12562 duration = atoi(c);
12563 if (!duration)
12564 duration = 100;
12565
12566 if (!p->owner) {
12567 transmit_response(p, "481 Call leg/transaction does not exist", req);
12568 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12569 return;
12570 }
12571
12572 if (ast_strlen_zero(buf)) {
12573 transmit_response(p, "200 OK", req);
12574 return;
12575 }
12576
12577 if (buf[0] == '*')
12578 event = 10;
12579 else if (buf[0] == '#')
12580 event = 11;
12581 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
12582 event = 12 + buf[0] - 'A';
12583 else
12584 event = atoi(buf);
12585 if (event == 16) {
12586
12587 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
12588 ast_queue_frame(p->owner, &f);
12589 if (sipdebug)
12590 ast_verbose("* DTMF-relay event received: FLASH\n");
12591 } else {
12592
12593 struct ast_frame f = { AST_FRAME_DTMF, };
12594 if (event < 10) {
12595 f.subclass = '0' + event;
12596 } else if (event < 11) {
12597 f.subclass = '*';
12598 } else if (event < 12) {
12599 f.subclass = '#';
12600 } else if (event < 16) {
12601 f.subclass = 'A' + (event - 12);
12602 }
12603 f.len = duration;
12604 ast_queue_frame(p->owner, &f);
12605 if (sipdebug)
12606 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
12607 }
12608 transmit_response(p, "200 OK", req);
12609 return;
12610 } else if (!strcasecmp(c, "application/media_control+xml")) {
12611
12612 if (p->owner)
12613 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
12614 transmit_response(p, "200 OK", req);
12615 return;
12616 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
12617
12618 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
12619 if (p->owner && p->owner->cdr)
12620 ast_cdr_setuserfield(p->owner, c);
12621 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
12622 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
12623 transmit_response(p, "200 OK", req);
12624 } else {
12625 transmit_response(p, "403 Unauthorized", req);
12626 }
12627 return;
12628 } else if (ast_strlen_zero(c = get_header(req, "Content-Length")) || !strcasecmp(c, "0")) {
12629
12630 transmit_response(p, "200 OK", req);
12631 return;
12632 }
12633
12634
12635
12636
12637
12638 if (!strcasecmp(get_header(req, "Content-Length"), "0")) {
12639 transmit_response(p, "200 OK", req);
12640 return;
12641 }
12642
12643 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
12644 transmit_response(p, "415 Unsupported media type", req);
12645 return;
12646 }
12647
12648
12649 static int sip_do_debug_ip(int fd, int argc, char *argv[])
12650 {
12651 struct hostent *hp;
12652 struct ast_hostent ahp;
12653 int port = 0;
12654 char *p, *arg;
12655
12656
12657 if (argc != 5)
12658 return RESULT_SHOWUSAGE;
12659 p = arg = argv[4];
12660 strsep(&p, ":");
12661 if (p)
12662 port = atoi(p);
12663 hp = ast_gethostbyname(arg, &ahp);
12664 if (hp == NULL)
12665 return RESULT_SHOWUSAGE;
12666
12667 debugaddr.sin_family = AF_INET;
12668 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
12669 debugaddr.sin_port = htons(port);
12670 if (port == 0)
12671 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(debugaddr.sin_addr));
12672 else
12673 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), port);
12674
12675 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12676
12677 return RESULT_SUCCESS;
12678 }
12679
12680
12681 static int sip_do_debug_peer(int fd, int argc, char *argv[])
12682 {
12683 struct sip_peer *peer;
12684 if (argc != 5)
12685 return RESULT_SHOWUSAGE;
12686 peer = find_peer(argv[4], NULL, 1, 0);
12687 if (peer) {
12688 if (peer->addr.sin_addr.s_addr) {
12689 debugaddr.sin_family = AF_INET;
12690 debugaddr.sin_addr = peer->addr.sin_addr;
12691 debugaddr.sin_port = peer->addr.sin_port;
12692 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
12693 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12694 } else
12695 ast_cli(fd, "Unable to get IP address of peer '%s'\n", argv[4]);
12696 ASTOBJ_UNREF(peer,sip_destroy_peer);
12697 } else
12698 ast_cli(fd, "No such peer '%s'\n", argv[4]);
12699 return RESULT_SUCCESS;
12700 }
12701
12702
12703 static int sip_do_debug(int fd, int argc, char *argv[])
12704 {
12705 int oldsipdebug = sipdebug_console;
12706 if (argc != 3) {
12707 if (argc != 5)
12708 return RESULT_SHOWUSAGE;
12709 else if (strcmp(argv[3], "ip") == 0)
12710 return sip_do_debug_ip(fd, argc, argv);
12711 else if (strcmp(argv[3], "peer") == 0)
12712 return sip_do_debug_peer(fd, argc, argv);
12713 else
12714 return RESULT_SHOWUSAGE;
12715 }
12716 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12717 memset(&debugaddr, 0, sizeof(debugaddr));
12718 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
12719 return RESULT_SUCCESS;
12720 }
12721
12722 static int sip_do_debug_deprecated(int fd, int argc, char *argv[])
12723 {
12724 int oldsipdebug = sipdebug_console;
12725 char *newargv[6] = { "sip", "set", "debug", NULL };
12726 if (argc != 2) {
12727 if (argc != 4)
12728 return RESULT_SHOWUSAGE;
12729 else if (strcmp(argv[2], "ip") == 0) {
12730 newargv[3] = argv[2];
12731 newargv[4] = argv[3];
12732 return sip_do_debug_ip(fd, argc + 1, newargv);
12733 } else if (strcmp(argv[2], "peer") == 0) {
12734 newargv[3] = argv[2];
12735 newargv[4] = argv[3];
12736 return sip_do_debug_peer(fd, argc + 1, newargv);
12737 } else
12738 return RESULT_SHOWUSAGE;
12739 }
12740 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12741 memset(&debugaddr, 0, sizeof(debugaddr));
12742 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
12743 return RESULT_SUCCESS;
12744 }
12745
12746
12747 static int sip_notify(int fd, int argc, char *argv[])
12748 {
12749 struct ast_variable *varlist;
12750 int i;
12751
12752 if (argc < 4)
12753 return RESULT_SHOWUSAGE;
12754
12755 if (!notify_types) {
12756 ast_cli(fd, "No %s file found, or no types listed there\n", notify_config);
12757 return RESULT_FAILURE;
12758 }
12759
12760 varlist = ast_variable_browse(notify_types, argv[2]);
12761
12762 if (!varlist) {
12763 ast_cli(fd, "Unable to find notify type '%s'\n", argv[2]);
12764 return RESULT_FAILURE;
12765 }
12766
12767 for (i = 3; i < argc; i++) {
12768 struct sip_pvt *p;
12769 struct sip_request req;
12770 struct ast_variable *var;
12771
12772 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY))) {
12773 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
12774 return RESULT_FAILURE;
12775 }
12776
12777 if (create_addr(p, argv[i], NULL)) {
12778
12779 sip_destroy(p);
12780 ast_cli(fd, "Could not create address for '%s'\n", argv[i]);
12781 continue;
12782 }
12783
12784 initreqprep(&req, p, SIP_NOTIFY);
12785
12786 for (var = varlist; var; var = var->next) {
12787 if (!strcasecmp(var->name, "Content-Length")) {
12788 if (option_debug >= 2) {
12789 ast_log(LOG_DEBUG, "Ignoring pair %s=%s\n", var->name, var->value);
12790 }
12791 continue;
12792 }
12793 add_header(&req, var->name, ast_unescape_semicolon(var->value));
12794 }
12795
12796
12797 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
12798 p->ourip = __ourip;
12799 build_via(p);
12800 build_callid_pvt(p);
12801 ast_cli(fd, "Sending NOTIFY of type '%s' to '%s'\n", argv[2], argv[i]);
12802 transmit_sip_request(p, &req);
12803 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12804 }
12805
12806 return RESULT_SUCCESS;
12807 }
12808
12809
12810 static int sip_no_debug(int fd, int argc, char *argv[])
12811 {
12812 if (argc != 4)
12813 return RESULT_SHOWUSAGE;
12814 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12815 ast_cli(fd, "SIP Debugging Disabled\n");
12816 return RESULT_SUCCESS;
12817 }
12818
12819 static int sip_no_debug_deprecated(int fd, int argc, char *argv[])
12820 {
12821 if (argc != 3)
12822 return RESULT_SHOWUSAGE;
12823 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
12824 ast_cli(fd, "SIP Debugging Disabled\n");
12825 return RESULT_SUCCESS;
12826 }
12827
12828
12829 static int sip_do_history(int fd, int argc, char *argv[])
12830 {
12831 if (argc != 2) {
12832 return RESULT_SHOWUSAGE;
12833 }
12834 recordhistory = TRUE;
12835 ast_cli(fd, "SIP History Recording Enabled (use 'sip show history')\n");
12836 return RESULT_SUCCESS;
12837 }
12838
12839
12840 static int sip_no_history(int fd, int argc, char *argv[])
12841 {
12842 if (argc != 3) {
12843 return RESULT_SHOWUSAGE;
12844 }
12845 recordhistory = FALSE;
12846 ast_cli(fd, "SIP History Recording Disabled\n");
12847 return RESULT_SUCCESS;
12848 }
12849
12850
12851 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader)
12852 {
12853 char digest[1024];
12854 p->authtries++;
12855 memset(digest,0,sizeof(digest));
12856 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
12857
12858
12859 if (sip_debug_test_pvt(p) && p->registry)
12860 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
12861
12862 return -1;
12863 }
12864 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
12865 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
12866 if (sip_debug_test_pvt(p) && p->registry)
12867 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
12868 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
12869 }
12870
12871
12872 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader, int sipmethod, int init)
12873 {
12874 char digest[1024];
12875
12876 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
12877 return -2;
12878
12879 p->authtries++;
12880 if (option_debug > 1)
12881 ast_log(LOG_DEBUG, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
12882 memset(digest, 0, sizeof(digest));
12883 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
12884
12885 return -1;
12886 }
12887
12888 p->options->auth = digest;
12889 p->options->authheader = respheader;
12890 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
12891 }
12892
12893
12894
12895
12896
12897 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
12898 {
12899 char tmp[512];
12900 char *c;
12901 char oldnonce[256];
12902
12903
12904 const struct x {
12905 const char *key;
12906 int field_index;
12907 } *i, keys[] = {
12908 { "realm=", ast_string_field_index(p, realm) },
12909 { "nonce=", ast_string_field_index(p, nonce) },
12910 { "opaque=", ast_string_field_index(p, opaque) },
12911 { "qop=", ast_string_field_index(p, qop) },
12912 { "domain=", ast_string_field_index(p, domain) },
12913 { NULL, 0 },
12914 };
12915
12916 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
12917 if (ast_strlen_zero(tmp))
12918 return -1;
12919 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
12920 ast_log(LOG_WARNING, "missing Digest.\n");
12921 return -1;
12922 }
12923 c = tmp + strlen("Digest ");
12924 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
12925 while (c && *(c = ast_skip_blanks(c))) {
12926 for (i = keys; i->key != NULL; i++) {
12927 char *src, *separator;
12928 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
12929 continue;
12930
12931 c += strlen(i->key);
12932 if (*c == '"') {
12933 src = ++c;
12934 separator = "\"";
12935 } else {
12936 src = c;
12937 separator = ",";
12938 }
12939 strsep(&c, separator);
12940 ast_string_field_index_set(p, i->field_index, src);
12941 break;
12942 }
12943 if (i->key == NULL)
12944 strsep(&c, ",");
12945 }
12946
12947 if (strcmp(p->nonce, oldnonce))
12948 p->noncecount = 0;
12949
12950
12951 if (p->registry) {
12952 struct sip_registry *r = p->registry;
12953
12954 if (strcmp(r->nonce, p->nonce)) {
12955 ast_string_field_set(r, realm, p->realm);
12956 ast_string_field_set(r, nonce, p->nonce);
12957 ast_string_field_set(r, domain, p->domain);
12958 ast_string_field_set(r, opaque, p->opaque);
12959 ast_string_field_set(r, qop, p->qop);
12960 r->noncecount = 0;
12961 }
12962 }
12963 return build_reply_digest(p, sipmethod, digest, digest_len);
12964 }
12965
12966
12967
12968
12969
12970
12971 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
12972 {
12973 char a1[256];
12974 char a2[256];
12975 char a1_hash[256];
12976 char a2_hash[256];
12977 char resp[256];
12978 char resp_hash[256];
12979 char uri[256];
12980 char opaque[256] = "";
12981 char cnonce[80];
12982 const char *username;
12983 const char *secret;
12984 const char *md5secret;
12985 struct sip_auth *auth = NULL;
12986
12987 if (!ast_strlen_zero(p->domain))
12988 ast_copy_string(uri, p->domain, sizeof(uri));
12989 else if (!ast_strlen_zero(p->uri))
12990 ast_copy_string(uri, p->uri, sizeof(uri));
12991 else
12992 snprintf(uri, sizeof(uri), "sip:%s@%s",p->username, ast_inet_ntoa(p->sa.sin_addr));
12993
12994 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
12995
12996
12997 if(!(auth = find_realm_authentication(p->peerauth, p->realm)))
12998 auth = find_realm_authentication(authl, p->realm);
12999
13000 if (auth) {
13001 if (sipdebug && option_debug > 1)
13002 ast_log(LOG_DEBUG, "use realm [%s] from peer [%s][%s]\n", auth->username, p->peername, p->username);
13003 username = auth->username;
13004 secret = auth->secret;
13005 md5secret = auth->md5secret;
13006 if (sipdebug)
13007 ast_log(LOG_DEBUG,"Using realm %s authentication for call %s\n", p->realm, p->callid);
13008 } else {
13009
13010 username = p->authname;
13011 secret = p->peersecret;
13012 md5secret = p->peermd5secret;
13013 }
13014 if (ast_strlen_zero(username))
13015 return -1;
13016
13017
13018 snprintf(a1,sizeof(a1),"%s:%s:%s", username, p->realm, secret);
13019 snprintf(a2,sizeof(a2),"%s:%s", sip_methods[method].text, uri);
13020 if (!ast_strlen_zero(md5secret))
13021 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
13022 else
13023 ast_md5_hash(a1_hash,a1);
13024 ast_md5_hash(a2_hash,a2);
13025
13026 p->noncecount++;
13027 if (!ast_strlen_zero(p->qop))
13028 snprintf(resp,sizeof(resp),"%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
13029 else
13030 snprintf(resp,sizeof(resp),"%s:%s:%s", a1_hash, p->nonce, a2_hash);
13031 ast_md5_hash(resp_hash, resp);
13032
13033
13034 if (!ast_strlen_zero(p->opaque)) {
13035 snprintf(opaque, sizeof(opaque), ", opaque=\"%s\"", p->opaque);
13036 }
13037
13038
13039 if (!ast_strlen_zero(p->qop))
13040 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);
13041 else
13042 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);
13043
13044 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
13045
13046 return 0;
13047 }
13048
13049 static char show_domains_usage[] =
13050 "Usage: sip show domains\n"
13051 " Lists all configured SIP local domains.\n"
13052 " Asterisk only responds to SIP messages to local domains.\n";
13053
13054 static char notify_usage[] =
13055 "Usage: sip notify <type> <peer> [<peer>...]\n"
13056 " Send a NOTIFY message to a SIP peer or peers\n"
13057 " Message types are defined in sip_notify.conf\n";
13058
13059 static char show_users_usage[] =
13060 "Usage: sip show users [like <pattern>]\n"
13061 " Lists all known SIP users.\n"
13062 " Optional regular expression pattern is used to filter the user list.\n";
13063
13064 static char show_user_usage[] =
13065 "Usage: sip show user <name> [load]\n"
13066 " Shows all details on one SIP user and the current status.\n"
13067 " Option \"load\" forces lookup of peer in realtime storage.\n";
13068
13069 static char show_inuse_usage[] =
13070 "Usage: sip show inuse [all]\n"
13071 " List all SIP users and peers usage counters and limits.\n"
13072 " Add option \"all\" to show all devices, not only those with a limit.\n";
13073
13074 static char show_channels_usage[] =
13075 "Usage: sip show channels\n"
13076 " Lists all currently active SIP channels.\n";
13077
13078 static char show_channel_usage[] =
13079 "Usage: sip show channel <channel>\n"
13080 " Provides detailed status on a given SIP channel.\n";
13081
13082 static char show_history_usage[] =
13083 "Usage: sip show history <channel>\n"
13084 " Provides detailed dialog history on a given SIP channel.\n";
13085
13086 static char show_peers_usage[] =
13087 "Usage: sip show peers [like <pattern>]\n"
13088 " Lists all known SIP peers.\n"
13089 " Optional regular expression pattern is used to filter the peer list.\n";
13090
13091 static char show_peer_usage[] =
13092 "Usage: sip show peer <name> [load]\n"
13093 " Shows all details on one SIP peer and the current status.\n"
13094 " Option \"load\" forces lookup of peer in realtime storage.\n";
13095
13096 static char prune_realtime_usage[] =
13097 "Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
13098 " Prunes object(s) from the cache.\n"
13099 " Optional regular expression pattern is used to filter the objects.\n";
13100
13101 static char show_reg_usage[] =
13102 "Usage: sip show registry\n"
13103 " Lists all registration requests and status.\n";
13104
13105 static char debug_usage[] =
13106 "Usage: sip set debug\n"
13107 " Enables dumping of SIP packets for debugging purposes\n\n"
13108 " sip set debug ip <host[:PORT]>\n"
13109 " Enables dumping of SIP packets to and from host.\n\n"
13110 " sip set debug peer <peername>\n"
13111 " Enables dumping of SIP packets to and from host.\n"
13112 " Require peer to be registered.\n";
13113
13114 static char no_debug_usage[] =
13115 "Usage: sip set debug off\n"
13116 " Disables dumping of SIP packets for debugging purposes\n";
13117
13118 static char no_history_usage[] =
13119 "Usage: sip history off\n"
13120 " Disables recording of SIP dialog history for debugging purposes\n";
13121
13122 static char history_usage[] =
13123 "Usage: sip history\n"
13124 " Enables recording of SIP dialog history for debugging purposes.\n"
13125 "Use 'sip show history' to view the history of a call number.\n";
13126
13127 static char sip_reload_usage[] =
13128 "Usage: sip reload\n"
13129 " Reloads SIP configuration from sip.conf\n";
13130
13131 static char show_subscriptions_usage[] =
13132 "Usage: sip show subscriptions\n"
13133 " Lists active SIP subscriptions for extension states\n";
13134
13135 static char show_objects_usage[] =
13136 "Usage: sip show objects\n"
13137 " Lists status of known SIP objects\n";
13138
13139 static char show_settings_usage[] =
13140 "Usage: sip show settings\n"
13141 " Provides detailed list of the configuration of the SIP channel.\n";
13142
13143
13144 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
13145 {
13146 struct sip_pvt *p;
13147 const char *content = NULL;
13148 AST_DECLARE_APP_ARGS(args,
13149 AST_APP_ARG(header);
13150 AST_APP_ARG(number);
13151 );
13152 int i, number, start = 0;
13153
13154 if (ast_strlen_zero(data)) {
13155 ast_log(LOG_WARNING, "This function requires a header name.\n");
13156 return -1;
13157 }
13158
13159 ast_channel_lock(chan);
13160 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
13161 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
13162 ast_channel_unlock(chan);
13163 return -1;
13164 }
13165
13166 AST_STANDARD_APP_ARGS(args, data);
13167 if (!args.number) {
13168 number = 1;
13169 } else {
13170 sscanf(args.number, "%30d", &number);
13171 if (number < 1)
13172 number = 1;
13173 }
13174
13175 p = chan->tech_pvt;
13176
13177
13178 if (!p) {
13179 ast_channel_unlock(chan);
13180 return -1;
13181 }
13182
13183 for (i = 0; i < number; i++)
13184 content = __get_header(&p->initreq, args.header, &start);
13185
13186 if (ast_strlen_zero(content)) {
13187 ast_channel_unlock(chan);
13188 return -1;
13189 }
13190
13191 ast_copy_string(buf, content, len);
13192 ast_channel_unlock(chan);
13193
13194 return 0;
13195 }
13196
13197 static struct ast_custom_function sip_header_function = {
13198 .name = "SIP_HEADER",
13199 .synopsis = "Gets the specified SIP header",
13200 .syntax = "SIP_HEADER(<name>[,<number>])",
13201 .desc = "Since there are several headers (such as Via) which can occur multiple\n"
13202 "times, SIP_HEADER takes an optional second argument to specify which header with\n"
13203 "that name to retrieve. Headers start at offset 1.\n",
13204 .read = func_header_read,
13205 };
13206
13207
13208 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
13209 {
13210 if (ast_strlen_zero(data)) {
13211 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
13212 return -1;
13213 }
13214 if (check_sip_domain(data, NULL, 0))
13215 ast_copy_string(buf, data, len);
13216 else
13217 buf[0] = '\0';
13218 return 0;
13219 }
13220
13221 static struct ast_custom_function checksipdomain_function = {
13222 .name = "CHECKSIPDOMAIN",
13223 .synopsis = "Checks if domain is a local domain",
13224 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
13225 .read = func_check_sipdomain,
13226 .desc = "This function checks if the domain in the argument is configured\n"
13227 "as a local SIP domain that this Asterisk server is configured to handle.\n"
13228 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
13229 "Check the domain= configuration in sip.conf\n",
13230 };
13231
13232
13233 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
13234 {
13235 struct sip_peer *peer;
13236 char *colname;
13237
13238 if ((colname = strchr(data, ':')))
13239 *colname++ = '\0';
13240 else if ((colname = strchr(data, '|')))
13241 *colname++ = '\0';
13242 else
13243 colname = "ip";
13244
13245 if (!(peer = find_peer(data, NULL, 1, 0)))
13246 return -1;
13247
13248 if (!strcasecmp(colname, "ip")) {
13249 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
13250 } else if (!strcasecmp(colname, "status")) {
13251 peer_status(peer, buf, len);
13252 } else if (!strcasecmp(colname, "language")) {
13253 ast_copy_string(buf, peer->language, len);
13254 } else if (!strcasecmp(colname, "regexten")) {
13255 ast_copy_string(buf, peer->regexten, len);
13256 } else if (!strcasecmp(colname, "limit")) {
13257 snprintf(buf, len, "%d", peer->call_limit);
13258 } else if (!strcasecmp(colname, "curcalls")) {
13259 snprintf(buf, len, "%d", peer->inUse);
13260 } else if (!strcasecmp(colname, "accountcode")) {
13261 ast_copy_string(buf, peer->accountcode, len);
13262 } else if (!strcasecmp(colname, "useragent")) {
13263 ast_copy_string(buf, peer->useragent, len);
13264 } else if (!strcasecmp(colname, "mailbox")) {
13265 ast_copy_string(buf, peer->mailbox, len);
13266 } else if (!strcasecmp(colname, "context")) {
13267 ast_copy_string(buf, peer->context, len);
13268 } else if (!strcasecmp(colname, "expire")) {
13269 snprintf(buf, len, "%d", peer->expire);
13270 } else if (!strcasecmp(colname, "dynamic")) {
13271 ast_copy_string(buf, (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no"), len);
13272 } else if (!strcasecmp(colname, "callerid_name")) {
13273 ast_copy_string(buf, peer->cid_name, len);
13274 } else if (!strcasecmp(colname, "callerid_num")) {
13275 ast_copy_string(buf, peer->cid_num, len);
13276 } else if (!strcasecmp(colname, "codecs")) {
13277 ast_getformatname_multiple(buf, len -1, peer->capability);
13278 } else if (!strncasecmp(colname, "codec[", 6)) {
13279 char *codecnum;
13280 int index = 0, codec = 0;
13281
13282 codecnum = colname + 6;
13283 codecnum = strsep(&codecnum, "]");
13284 index = atoi(codecnum);
13285 if((codec = ast_codec_pref_index(&peer->prefs, index))) {
13286 ast_copy_string(buf, ast_getformatname(codec), len);
13287 } else {
13288 buf[0] = '\0';
13289 }
13290 } else {
13291 buf[0] = '\0';
13292 }
13293
13294 ASTOBJ_UNREF(peer, sip_destroy_peer);
13295
13296 return 0;
13297 }
13298
13299
13300 struct ast_custom_function sippeer_function = {
13301 .name = "SIPPEER",
13302 .synopsis = "Gets SIP peer information",
13303 .syntax = "SIPPEER(<peername>[|item])",
13304 .read = function_sippeer,
13305 .desc = "Valid items are:\n"
13306 "- ip (default) The IP address.\n"
13307 "- mailbox The configured mailbox.\n"
13308 "- context The configured context.\n"
13309 "- expire The epoch time of the next expire.\n"
13310 "- dynamic Is it dynamic? (yes/no).\n"
13311 "- callerid_name The configured Caller ID name.\n"
13312 "- callerid_num The configured Caller ID number.\n"
13313 "- codecs The configured codecs.\n"
13314 "- status Status (if qualify=yes).\n"
13315 "- regexten Registration extension\n"
13316 "- limit Call limit (call-limit)\n"
13317 "- curcalls Current amount of calls \n"
13318 " Only available if call-limit is set\n"
13319 "- language Default language for peer\n"
13320 "- accountcode Account code for this peer\n"
13321 "- useragent Current user agent id for peer\n"
13322 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
13323 "\n"
13324 };
13325
13326
13327 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
13328 {
13329 struct sip_pvt *p;
13330
13331 *buf = 0;
13332
13333 if (!data) {
13334 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
13335 return -1;
13336 }
13337
13338 ast_channel_lock(chan);
13339 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
13340 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
13341 ast_channel_unlock(chan);
13342 return -1;
13343 }
13344
13345 p = chan->tech_pvt;
13346
13347
13348 if (!p) {
13349 ast_channel_unlock(chan);
13350 return -1;
13351 }
13352
13353 if (!strcasecmp(data, "peerip")) {
13354 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", len);
13355 } else if (!strcasecmp(data, "recvip")) {
13356 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", len);
13357 } else if (!strcasecmp(data, "from")) {
13358 ast_copy_string(buf, p->from, len);
13359 } else if (!strcasecmp(data, "uri")) {
13360 ast_copy_string(buf, p->uri, len);
13361 } else if (!strcasecmp(data, "useragent")) {
13362 ast_copy_string(buf, p->useragent, len);
13363 } else if (!strcasecmp(data, "peername")) {
13364 ast_copy_string(buf, p->peername, len);
13365 } else if (!strcasecmp(data, "t38passthrough")) {
13366 if (p->t38.state == T38_DISABLED) {
13367 ast_copy_string(buf, "0", len);
13368 } else {
13369 ast_copy_string(buf, "1", len);
13370 }
13371 } else {
13372 ast_channel_unlock(chan);
13373 return -1;
13374 }
13375 ast_channel_unlock(chan);
13376
13377 return 0;
13378 }
13379
13380
13381 static struct ast_custom_function sipchaninfo_function = {
13382 .name = "SIPCHANINFO",
13383 .synopsis = "Gets the specified SIP parameter from the current channel",
13384 .syntax = "SIPCHANINFO(item)",
13385 .read = function_sipchaninfo_read,
13386 .desc = "Valid items are:\n"
13387 "- peerip The IP address of the peer.\n"
13388 "- recvip The source IP address of the peer.\n"
13389 "- from The URI from the From: header.\n"
13390 "- uri The URI from the Contact: header.\n"
13391 "- useragent The useragent.\n"
13392 "- peername The name of the peer.\n"
13393 "- t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
13394 };
13395
13396
13397 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
13398 {
13399 char tmp[SIPBUFSIZE];
13400 char *s, *e, *uri, *t;
13401 char *domain;
13402
13403 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
13404 if ((t = strchr(tmp, ',')))
13405 *t = '\0';
13406 s = get_in_brackets(tmp);
13407 uri = ast_strdupa(s);
13408 if (ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
13409 if (!strncasecmp(s, "sip:", 4))
13410 s += 4;
13411 e = strchr(s, ';');
13412 if (e)
13413 *e = '\0';
13414 if (option_debug)
13415 ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
13416 if (p->owner)
13417 ast_string_field_build(p->owner, call_forward, "SIP/%s", s);
13418 } else {
13419 e = strchr(tmp, '@');
13420 if (e) {
13421 *e++ = '\0';
13422 domain = e;
13423 } else {
13424
13425 domain = tmp;
13426 }
13427 e = strchr(s, ';');
13428 if (e)
13429 *e = '\0';
13430 e = strchr(domain, ';');
13431 if (e)
13432 *e = '\0';
13433
13434 if (!strncasecmp(s, "sip:", 4))
13435 s += 4;
13436 ast_uri_decode(s);
13437 if (option_debug > 1)
13438 ast_log(LOG_DEBUG, "Received 302 Redirect to extension '%s' (domain %s)\n", s, domain);
13439 if (p->owner) {
13440 pbx_builtin_setvar_helper(p->owner, "SIPREDIRECTURI", uri);
13441 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
13442 ast_string_field_set(p->owner, call_forward, s);
13443 }
13444 }
13445 }
13446
13447
13448
13449
13450
13451
13452 static void check_pendings(struct sip_pvt *p)
13453 {
13454 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
13455
13456 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA) {
13457 p->invitestate = INV_CANCELLED;
13458 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
13459
13460
13461 } else {
13462
13463
13464 if (p->pendinginvite)
13465 return;
13466
13467 if (p->owner) {
13468 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
13469 }
13470
13471 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
13472 }
13473 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
13474 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13475 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
13476
13477 if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
13478 if (option_debug)
13479 ast_log(LOG_DEBUG, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
13480 } else {
13481 if (option_debug)
13482 ast_log(LOG_DEBUG, "Sending pending reinvite on '%s'\n", p->callid);
13483
13484 transmit_reinvite_with_sdp(p);
13485 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
13486 }
13487 }
13488 }
13489
13490
13491
13492
13493
13494 static int sip_reinvite_retry(const void *data)
13495 {
13496 struct sip_pvt *p = (struct sip_pvt *) data;
13497 struct ast_channel *owner;
13498
13499 ast_mutex_lock(&p->lock);
13500 while ((owner = p->owner) && ast_channel_trylock(owner)) {
13501 ast_mutex_unlock(&p->lock);
13502 usleep(1);
13503 ast_mutex_lock(&p->lock);
13504 }
13505 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
13506 p->waitid = -1;
13507 check_pendings(p);
13508 ast_mutex_unlock(&p->lock);
13509 if (owner) {
13510 ast_channel_unlock(owner);
13511 }
13512
13513 return 0;
13514 }
13515
13516
13517
13518 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
13519 {
13520 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
13521 int res = 0;
13522 int xmitres = 0;
13523 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
13524 struct ast_channel *bridgepeer = NULL;
13525
13526 if (option_debug > 3) {
13527 if (reinvite)
13528 ast_log(LOG_DEBUG, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
13529 else
13530 ast_log(LOG_DEBUG, "SIP response %d to standard invite\n", resp);
13531 }
13532
13533 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
13534 if (option_debug)
13535 ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
13536 return;
13537 }
13538
13539
13540
13541 AST_SCHED_DEL(sched, p->initid);
13542
13543
13544
13545
13546 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 182 && resp != 183)
13547 resp = 183;
13548
13549
13550 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
13551 p->invitestate = INV_PROCEEDING;
13552
13553
13554 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
13555 p->invitestate = INV_COMPLETED;
13556
13557
13558 switch (resp) {
13559 case 100:
13560 case 101:
13561 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
13562 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13563 check_pendings(p);
13564 break;
13565
13566 case 180:
13567 case 182:
13568 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
13569 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13570 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
13571 ast_queue_control(p->owner, AST_CONTROL_RINGING);
13572 if (p->owner->_state != AST_STATE_UP) {
13573 ast_setstate(p->owner, AST_STATE_RINGING);
13574 }
13575 }
13576 if (find_sdp(req)) {
13577 if (p->invitestate != INV_CANCELLED)
13578 p->invitestate = INV_EARLY_MEDIA;
13579 res = process_sdp(p, req);
13580 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
13581
13582 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
13583 }
13584 }
13585 check_pendings(p);
13586 break;
13587
13588 case 183:
13589 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
13590 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13591 if (find_sdp(req)) {
13592 if (p->invitestate != INV_CANCELLED)
13593 p->invitestate = INV_EARLY_MEDIA;
13594 res = process_sdp(p, req);
13595 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
13596
13597 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
13598 }
13599 } else {
13600
13601
13602
13603
13604 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
13605 ast_queue_control(p->owner, AST_CONTROL_RINGING);
13606 }
13607 }
13608 check_pendings(p);
13609 break;
13610
13611 case 200:
13612 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
13613 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13614 p->authtries = 0;
13615 if (find_sdp(req)) {
13616 if ((res = process_sdp(p, req)) && !ast_test_flag(req, SIP_PKT_IGNORE))
13617 if (!reinvite)
13618
13619
13620 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
13621 }
13622
13623
13624
13625
13626 if (outgoing) {
13627 update_call_counter(p, DEC_CALL_RINGING);
13628 parse_ok_contact(p, req);
13629
13630 if (!reinvite)
13631 build_route(p, req, 1);
13632
13633 if(set_address_from_contact(p)) {
13634
13635
13636 if (!p->route && !ast_test_flag(req, SIP_PKT_IGNORE))
13637 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
13638 }
13639
13640 }
13641
13642 if (p->owner && (p->owner->_state == AST_STATE_UP) && (bridgepeer = ast_bridged_channel(p->owner))) {
13643 struct sip_pvt *bridgepvt = NULL;
13644
13645 if (!bridgepeer->tech) {
13646 ast_log(LOG_WARNING, "Ooooh.. no tech! That's REALLY bad\n");
13647 break;
13648 }
13649 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
13650 bridgepvt = (struct sip_pvt*)(bridgepeer->tech_pvt);
13651 if (bridgepvt->udptl) {
13652 if (p->t38.state == T38_PEER_REINVITE) {
13653 sip_handle_t38_reinvite(bridgepeer, p, 0);
13654 ast_rtp_set_rtptimers_onhold(p->rtp);
13655 if (p->vrtp)
13656 ast_rtp_set_rtptimers_onhold(p->vrtp);
13657 }
13658 } else {
13659 if (option_debug > 1)
13660 ast_log(LOG_DEBUG, "Strange... The other side of the bridge does not have a udptl struct\n");
13661 ast_mutex_lock(&bridgepvt->lock);
13662 bridgepvt->t38.state = T38_DISABLED;
13663 ast_mutex_unlock(&bridgepvt->lock);
13664 if (option_debug)
13665 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->tech->type);
13666 p->t38.state = T38_DISABLED;
13667 if (option_debug > 1)
13668 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13669 }
13670 } else {
13671
13672 if (option_debug > 1)
13673 ast_log(LOG_DEBUG, "Strange... The other side of the bridge is not a SIP channel\n");
13674 p->t38.state = T38_DISABLED;
13675 if (option_debug > 1)
13676 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13677 }
13678 }
13679 if (p->t38.state == T38_LOCAL_REINVITE) {
13680
13681 p->t38.state = T38_ENABLED;
13682 if (option_debug)
13683 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13684 }
13685
13686 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
13687 if (!reinvite) {
13688 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
13689 } else {
13690 ast_queue_frame(p->owner, &ast_null_frame);
13691 }
13692 } else {
13693
13694
13695
13696 if (!ast_test_flag(req, SIP_PKT_IGNORE))
13697 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
13698 }
13699
13700 p->invitestate = INV_TERMINATED;
13701 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
13702 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
13703 check_pendings(p);
13704 break;
13705 case 407:
13706 case 401:
13707
13708 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13709 if (p->options)
13710 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
13711
13712
13713 ast_string_field_free(p, theirtag);
13714 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13715 char *authenticate = (resp == 401 ? "WWW-Authenticate" : "Proxy-Authenticate");
13716 char *authorization = (resp == 401 ? "Authorization" : "Proxy-Authorization");
13717 if (p->authtries < MAX_AUTHTRIES)
13718 p->invitestate = INV_CALLING;
13719 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, authenticate, authorization, SIP_INVITE, 1)) {
13720 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
13721 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13722 sip_alreadygone(p);
13723 if (p->owner)
13724 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13725 }
13726 }
13727 break;
13728
13729 case 403:
13730
13731 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13732 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
13733 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner)
13734 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13735 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13736 sip_alreadygone(p);
13737 break;
13738
13739 case 404:
13740 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13741 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
13742 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13743 sip_alreadygone(p);
13744 break;
13745
13746 case 408:
13747 case 481:
13748
13749 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
13750 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13751 if (p->owner)
13752 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13753 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13754 break;
13755 case 487:
13756
13757
13758
13759 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13760 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE)) {
13761 ast_queue_hangup(p->owner);
13762 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
13763 } else if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13764 update_call_counter(p, DEC_CALL_LIMIT);
13765 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
13766 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13767 sip_alreadygone(p);
13768 }
13769 break;
13770 case 488:
13771 case 606:
13772 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13773 if (reinvite && p->udptl) {
13774 p->t38.state = T38_DISABLED;
13775
13776 ast_rtp_set_rtptimers_onhold(p->rtp);
13777
13778 transmit_reinvite_with_sdp(p);
13779
13780 if (p->owner && (p->owner->_state == AST_STATE_UP) && (bridgepeer = ast_bridged_channel(p->owner))) {
13781 struct sip_pvt *bridgepvt = NULL;
13782 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
13783 bridgepvt = (struct sip_pvt*)(bridgepeer->tech_pvt);
13784 if (bridgepvt->udptl) {
13785 sip_handle_t38_reinvite(bridgepeer, p, 0);
13786 }
13787 }
13788 }
13789 } else {
13790
13791 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
13792 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13793 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13794
13795 if (!reinvite)
13796 sip_alreadygone(p);
13797 }
13798 break;
13799 case 491:
13800
13801
13802
13803
13804 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13805 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE)) {
13806 if (p->owner->_state != AST_STATE_UP) {
13807 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13808 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13809 } else {
13810
13811
13812
13813 int wait;
13814
13815
13816 if (ast_test_flag(&p->flags[1], SIP_PAGE2_OUTGOING_CALL)) {
13817 wait = 2100 + ast_random() % 2000;
13818 } else {
13819 wait = ast_random() % 2000;
13820 }
13821
13822 if (p->waitid != -1) {
13823 if (option_debug > 2)
13824 ast_log(LOG_DEBUG, "Reinvite race during existing reinvite race. Abandoning previous reinvite retry.\n");
13825 AST_SCHED_DEL(sched, p->waitid);
13826 p->waitid = -1;
13827 }
13828
13829 p->waitid = ast_sched_add(sched, wait, sip_reinvite_retry, p);
13830 if (option_debug > 2)
13831 ast_log(LOG_DEBUG, "Reinvite race. Waiting %d secs before retry\n", wait);
13832 }
13833 }
13834 break;
13835
13836 case 501:
13837 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
13838 if (p->owner)
13839 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13840 break;
13841 }
13842 if (xmitres == XMIT_ERROR)
13843 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
13844 }
13845
13846
13847
13848
13849 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
13850 {
13851 char *auth = "Proxy-Authenticate";
13852 char *auth2 = "Proxy-Authorization";
13853
13854
13855 if (!p->refer)
13856 return;
13857
13858 switch (resp) {
13859 case 202:
13860
13861
13862 p->refer->status = REFER_ACCEPTED;
13863
13864 if (option_debug > 2)
13865 ast_log(LOG_DEBUG, "Got 202 accepted on transfer\n");
13866
13867 break;
13868
13869 case 401:
13870 case 407:
13871 if (ast_strlen_zero(p->authname)) {
13872 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s:%d but we have no matching peer or realm auth!\n",
13873 ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
13874 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13875 }
13876 if (resp == 401) {
13877 auth = "WWW-Authenticate";
13878 auth2 = "Authorization";
13879 }
13880 if ((p->authtries > 1) || do_proxy_auth(p, req, auth, auth2, SIP_REFER, 0)) {
13881 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
13882 p->refer->status = REFER_NOAUTH;
13883 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13884 }
13885 break;
13886 case 481:
13887
13888
13889
13890
13891 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
13892 if (p->owner)
13893 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
13894 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13895 break;
13896
13897 case 500:
13898 case 501:
13899
13900
13901 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
13902 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13903 p->refer->status = REFER_FAILED;
13904 break;
13905 case 603:
13906 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
13907 p->refer->status = REFER_FAILED;
13908 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13909 break;
13910 }
13911 }
13912
13913
13914 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
13915 {
13916 int expires, expires_ms;
13917 struct sip_registry *r;
13918 r=p->registry;
13919
13920 switch (resp) {
13921 case 401:
13922 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "WWW-Authenticate", "Authorization")) {
13923 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
13924 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13925 }
13926 break;
13927 case 403:
13928 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
13929 if (global_regattempts_max)
13930 p->registry->regattempts = global_regattempts_max+1;
13931 AST_SCHED_DEL(sched, r->timeout);
13932 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13933 break;
13934 case 404:
13935 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username,p->registry->hostname);
13936 if (global_regattempts_max)
13937 p->registry->regattempts = global_regattempts_max+1;
13938 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13939 r->call = NULL;
13940 AST_SCHED_DEL(sched, r->timeout);
13941 break;
13942 case 407:
13943 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization")) {
13944 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
13945 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13946 }
13947 break;
13948 case 408:
13949
13950 if (r) {
13951 r->regattempts = 0;
13952 } else {
13953 ast_log(LOG_WARNING, "Got a 408 response to our REGISTER on call %s after we had destroyed the registry object\n", p->callid);
13954 }
13955 break;
13956 case 479:
13957 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username,p->registry->hostname);
13958 if (global_regattempts_max)
13959 p->registry->regattempts = global_regattempts_max+1;
13960 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13961 r->call = NULL;
13962 AST_SCHED_DEL(sched, r->timeout);
13963 break;
13964 case 200:
13965 if (!r) {
13966 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));
13967 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13968 return 0;
13969 }
13970
13971 r->regstate = REG_STATE_REGISTERED;
13972 r->regtime = time(NULL);
13973 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
13974 r->regattempts = 0;
13975 if (option_debug)
13976 ast_log(LOG_DEBUG, "Registration successful\n");
13977 if (r->timeout > -1) {
13978 if (option_debug)
13979 ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
13980 }
13981 AST_SCHED_DEL(sched, r->timeout);
13982 r->call = NULL;
13983 p->registry = NULL;
13984
13985 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13986
13987
13988
13989
13990 AST_SCHED_DEL(sched, r->expire);
13991
13992
13993 expires = 0;
13994
13995
13996 if (!ast_strlen_zero(get_header(req, "Contact"))) {
13997 const char *contact = NULL;
13998 const char *tmptmp = NULL;
13999 int start = 0;
14000 for(;;) {
14001 contact = __get_header(req, "Contact", &start);
14002
14003 if(!ast_strlen_zero(contact)) {
14004 if( (tmptmp=strstr(contact, p->our_contact))) {
14005 contact=tmptmp;
14006 break;
14007 }
14008 } else
14009 break;
14010 }
14011 tmptmp = strcasestr(contact, "expires=");
14012 if (tmptmp) {
14013 if (sscanf(tmptmp + 8, "%30d;", &expires) != 1)
14014 expires = 0;
14015 }
14016
14017 }
14018 if (!expires)
14019 expires=atoi(get_header(req, "expires"));
14020 if (!expires)
14021 expires=default_expiry;
14022
14023 expires_ms = expires * 1000;
14024 if (expires <= EXPIRY_GUARD_LIMIT)
14025 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT),EXPIRY_GUARD_MIN);
14026 else
14027 expires_ms -= EXPIRY_GUARD_SECS * 1000;
14028 if (sipdebug)
14029 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
14030
14031 r->refresh= (int) expires_ms / 1000;
14032
14033
14034 AST_SCHED_DEL(sched, r->expire);
14035 r->expire = ast_sched_add(sched, expires_ms, sip_reregister, r);
14036 ASTOBJ_UNREF(r, sip_registry_destroy);
14037 }
14038 return 1;
14039 }
14040
14041
14042 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
14043 {
14044 struct sip_peer *peer = p->relatedpeer;
14045 int statechanged, is_reachable, was_reachable;
14046 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
14047
14048
14049
14050
14051
14052
14053 if (pingtime < 1)
14054 pingtime = 1;
14055
14056
14057
14058
14059
14060 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
14061 is_reachable = pingtime <= peer->maxms;
14062 statechanged = peer->lastms == 0
14063 || was_reachable != is_reachable;
14064
14065 peer->lastms = pingtime;
14066 peer->call = NULL;
14067 if (statechanged) {
14068 const char *s = is_reachable ? "Reachable" : "Lagged";
14069 char str_lastms[20];
14070 snprintf(str_lastms, sizeof(str_lastms), "%d", pingtime);
14071
14072 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
14073 peer->name, s, pingtime, peer->maxms);
14074 ast_device_state_changed("SIP/%s", peer->name);
14075 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE)) {
14076 ast_update_realtime("sippeers", "name", peer->name, "lastms", str_lastms, NULL);
14077 }
14078 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
14079 "Peer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
14080 peer->name, s, pingtime);
14081 }
14082
14083 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
14084 struct sip_peer *peer_ptr = peer;
14085 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
14086 }
14087
14088 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14089
14090
14091 peer->pokeexpire = ast_sched_add(sched,
14092 is_reachable ? DEFAULT_FREQ_OK : DEFAULT_FREQ_NOTOK,
14093 sip_poke_peer_s, ASTOBJ_REF(peer));
14094
14095 if (peer->pokeexpire == -1) {
14096 ASTOBJ_UNREF(peer, sip_destroy_peer);
14097 }
14098 }
14099
14100
14101 static void stop_media_flows(struct sip_pvt *p)
14102 {
14103
14104 if (p->rtp)
14105 ast_rtp_stop(p->rtp);
14106 if (p->vrtp)
14107 ast_rtp_stop(p->vrtp);
14108 if (p->udptl)
14109 ast_udptl_stop(p->udptl);
14110 }
14111
14112
14113
14114 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
14115 {
14116 struct ast_channel *owner;
14117 int sipmethod;
14118 int res = 1;
14119 int ack_res;
14120 const char *c = get_header(req, "Cseq");
14121
14122 char *c_copy = ast_strdupa(c);
14123
14124 const char *msg = ast_skip_blanks(ast_skip_nonblanks(c_copy));
14125
14126 if (!msg)
14127 msg = "";
14128
14129 sipmethod = find_sip_method(msg);
14130
14131 owner = p->owner;
14132 if (owner)
14133 owner->hangupcause = hangup_sip2cause(resp);
14134
14135
14136 if ((resp >= 100) && (resp <= 199)) {
14137 ack_res = __sip_semi_ack(p, seqno, 0, sipmethod);
14138 } else {
14139 ack_res = __sip_ack(p, seqno, 0, sipmethod);
14140 }
14141
14142 if (ack_res == FALSE) {
14143 append_history(p, "Ignore", "Ignoring this retransmit\n");
14144 return;
14145 }
14146
14147
14148 if (!p->owner && sipmethod == SIP_NOTIFY && p->pendinginvite)
14149 p->pendinginvite = 0;
14150
14151
14152 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
14153 char tag[128];
14154
14155 gettag(req, "To", tag, sizeof(tag));
14156 ast_string_field_set(p, theirtag, tag);
14157 }
14158
14159
14160
14161
14162
14163
14164
14165
14166 if ((resp == 404 || resp == 408 || resp == 481) && sipmethod == SIP_BYE) {
14167 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14168 return;
14169 }
14170
14171 if (p->relatedpeer && p->method == SIP_OPTIONS) {
14172
14173
14174
14175 if (resp != 100)
14176 handle_response_peerpoke(p, resp, req);
14177 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
14178 switch(resp) {
14179 case 100:
14180 case 101:
14181 if (sipmethod == SIP_INVITE)
14182 handle_response_invite(p, resp, rest, req, seqno);
14183 break;
14184 case 183:
14185 if (sipmethod == SIP_INVITE)
14186 handle_response_invite(p, resp, rest, req, seqno);
14187 break;
14188 case 180:
14189 if (sipmethod == SIP_INVITE)
14190 handle_response_invite(p, resp, rest, req, seqno);
14191 break;
14192 case 182:
14193 if (sipmethod == SIP_INVITE)
14194 handle_response_invite(p, resp, rest, req, seqno);
14195 break;
14196 case 200:
14197 p->authtries = 0;
14198 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
14199
14200
14201
14202 } else if (sipmethod == SIP_INVITE) {
14203 handle_response_invite(p, resp, rest, req, seqno);
14204 } else if (sipmethod == SIP_NOTIFY) {
14205
14206 if (p->owner) {
14207 if (!p->refer) {
14208 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
14209 ast_queue_hangup(p->owner);
14210 } else if (option_debug > 3)
14211 ast_log(LOG_DEBUG, "Got OK on REFER Notify message\n");
14212 } else {
14213 if (p->subscribed == NONE)
14214 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14215 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
14216
14217 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
14218 notify_extenstate_update((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
14219 }
14220 }
14221 } else if (sipmethod == SIP_REGISTER)
14222 res = handle_response_register(p, resp, rest, req, seqno);
14223 else if (sipmethod == SIP_BYE) {
14224 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14225 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
14226 } else if (sipmethod == SIP_SUBSCRIBE)
14227 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
14228 break;
14229 case 202:
14230 if (sipmethod == SIP_REFER)
14231 handle_response_refer(p, resp, rest, req, seqno);
14232 break;
14233 case 401:
14234 if (sipmethod == SIP_INVITE)
14235 handle_response_invite(p, resp, rest, req, seqno);
14236 else if (sipmethod == SIP_REFER)
14237 handle_response_refer(p, resp, rest, req, seqno);
14238 else if (p->registry && sipmethod == SIP_REGISTER)
14239 res = handle_response_register(p, resp, rest, req, seqno);
14240 else if (sipmethod == SIP_BYE) {
14241 if (ast_strlen_zero(p->authname)) {
14242 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
14243 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
14244 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14245 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "WWW-Authenticate", "Authorization", sipmethod, 0)) {
14246 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
14247 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14248
14249
14250 }
14251 } else {
14252 ast_log(LOG_WARNING, "Got authentication request (401) on unknown %s to '%s'\n", sip_methods[sipmethod].text, get_header(req, "To"));
14253 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14254 }
14255 break;
14256 case 403:
14257 if (sipmethod == SIP_INVITE)
14258 handle_response_invite(p, resp, rest, req, seqno);
14259 else if (p->registry && sipmethod == SIP_REGISTER)
14260 res = handle_response_register(p, resp, rest, req, seqno);
14261 else {
14262 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
14263 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14264 }
14265 break;
14266 case 404:
14267 if (p->registry && sipmethod == SIP_REGISTER)
14268 res = handle_response_register(p, resp, rest, req, seqno);
14269 else if (sipmethod == SIP_INVITE)
14270 handle_response_invite(p, resp, rest, req, seqno);
14271 else if (owner)
14272 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
14273 break;
14274 case 407:
14275 if (sipmethod == SIP_INVITE)
14276 handle_response_invite(p, resp, rest, req, seqno);
14277 else if (sipmethod == SIP_REFER)
14278 handle_response_refer(p, resp, rest, req, seqno);
14279 else if (p->registry && sipmethod == SIP_REGISTER)
14280 res = handle_response_register(p, resp, rest, req, seqno);
14281 else if (sipmethod == SIP_BYE) {
14282 if (ast_strlen_zero(p->authname)) {
14283 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
14284 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
14285 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14286 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization", sipmethod, 0)) {
14287 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
14288 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14289 }
14290 } else
14291 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14292
14293 break;
14294 case 408:
14295 if (sipmethod == SIP_INVITE)
14296 handle_response_invite(p, resp, rest, req, seqno);
14297 else if (sipmethod == SIP_REGISTER)
14298 res = handle_response_register(p, resp, rest, req, seqno);
14299 else if (sipmethod == SIP_BYE) {
14300 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14301 if (option_debug)
14302 ast_log(LOG_DEBUG, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
14303 } else {
14304 if (owner)
14305 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
14306 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14307 }
14308 break;
14309 case 481:
14310 if (sipmethod == SIP_INVITE) {
14311 handle_response_invite(p, resp, rest, req, seqno);
14312 } else if (sipmethod == SIP_REFER) {
14313 handle_response_refer(p, resp, rest, req, seqno);
14314 } else if (sipmethod == SIP_BYE) {
14315
14316
14317 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
14318 } else if (sipmethod == SIP_CANCEL) {
14319
14320
14321 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
14322 } else if (sipmethod == SIP_NOTIFY) {
14323
14324
14325 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14326 } else {
14327 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
14328
14329 }
14330 break;
14331 case 487:
14332 if (sipmethod == SIP_INVITE)
14333 handle_response_invite(p, resp, rest, req, seqno);
14334 break;
14335 case 488:
14336 case 606:
14337 if (sipmethod == SIP_INVITE)
14338 handle_response_invite(p, resp, rest, req, seqno);
14339 break;
14340 case 491:
14341 if (sipmethod == SIP_INVITE)
14342 handle_response_invite(p, resp, rest, req, seqno);
14343 else {
14344 if (option_debug)
14345 ast_log(LOG_DEBUG, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
14346 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14347 }
14348 break;
14349 case 501:
14350 if (sipmethod == SIP_INVITE)
14351 handle_response_invite(p, resp, rest, req, seqno);
14352 else if (sipmethod == SIP_REFER)
14353 handle_response_refer(p, resp, rest, req, seqno);
14354 else
14355 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(p->sa.sin_addr), msg);
14356 break;
14357 case 603:
14358 if (sipmethod == SIP_REFER) {
14359 handle_response_refer(p, resp, rest, req, seqno);
14360 break;
14361 }
14362
14363 default:
14364 if ((resp >= 300) && (resp < 700)) {
14365
14366 if ((option_verbose > 2) && (resp != 487))
14367 ast_verbose(VERBOSE_PREFIX_3 "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
14368
14369 if (sipmethod == SIP_INVITE)
14370 stop_media_flows(p);
14371
14372
14373 switch(resp) {
14374 case 300:
14375 case 301:
14376 case 302:
14377 case 305:
14378 parse_moved_contact(p, req);
14379
14380 case 486:
14381 case 600:
14382 case 603:
14383 if (p->owner)
14384 ast_queue_control(p->owner, AST_CONTROL_BUSY);
14385 break;
14386 case 482:
14387
14388
14389
14390
14391
14392 if (p->owner && ast_test_flag(&p->flags[1], SIP_PAGE2_FORWARD_LOOP_DETECTED)) {
14393 if (option_debug) {
14394 ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
14395 }
14396 ast_string_field_build(p->owner, call_forward,
14397 "Local/%s@%s", p->username, p->context);
14398 }
14399
14400 case 480:
14401 case 404:
14402 case 410:
14403 case 400:
14404 case 500:
14405 if (sipmethod == SIP_REFER) {
14406 handle_response_refer(p, resp, rest, req, seqno);
14407 break;
14408 }
14409
14410 case 502:
14411 case 503:
14412 case 504:
14413 if (owner)
14414 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
14415 break;
14416 default:
14417
14418 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO && sipmethod != SIP_BYE)
14419 ast_queue_hangup(p->owner);
14420 break;
14421 }
14422
14423 if (sipmethod == SIP_INVITE)
14424 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
14425 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
14426 sip_alreadygone(p);
14427 if (!p->owner)
14428 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14429 } else if ((resp >= 100) && (resp < 200)) {
14430 if (sipmethod == SIP_INVITE) {
14431 if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
14432 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
14433 if (find_sdp(req))
14434 process_sdp(p, req);
14435 if (p->owner) {
14436
14437 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
14438 }
14439 }
14440 } else
14441 ast_log(LOG_NOTICE, "Don't know how to handle a %d %s response from %s\n", resp, rest, p->owner ? p->owner->name : ast_inet_ntoa(p->sa.sin_addr));
14442 }
14443 } else {
14444
14445
14446 if (ast_test_flag(req, SIP_PKT_DEBUG))
14447 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
14448
14449 if (sipmethod == SIP_INVITE && resp == 200) {
14450
14451
14452 char tag[128];
14453
14454 gettag(req, "To", tag, sizeof(tag));
14455 ast_string_field_set(p, theirtag, tag);
14456 }
14457
14458 switch(resp) {
14459 case 200:
14460 if (sipmethod == SIP_INVITE) {
14461 handle_response_invite(p, resp, rest, req, seqno);
14462 } else if (sipmethod == SIP_CANCEL) {
14463 if (option_debug)
14464 ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
14465
14466
14467 } else if (sipmethod == SIP_NOTIFY) {
14468
14469 if (p->owner) {
14470 if (p->refer) {
14471 if (option_debug)
14472 ast_log(LOG_DEBUG, "Got 200 OK on NOTIFY for transfer\n");
14473 } else
14474 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
14475
14476 } else {
14477 if (!p->subscribed && !p->refer)
14478 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14479 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
14480
14481 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
14482 notify_extenstate_update((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
14483 }
14484 }
14485 } else if (sipmethod == SIP_BYE)
14486 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14487 else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO)
14488
14489
14490 ;
14491 else if (sipmethod == SIP_BYE)
14492
14493 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14494 break;
14495 case 202:
14496 if (sipmethod == SIP_REFER)
14497 handle_response_refer(p, resp, rest, req, seqno);
14498 break;
14499 case 401:
14500 case 407:
14501 if (sipmethod == SIP_REFER)
14502 handle_response_refer(p, resp, rest, req, seqno);
14503 else if (sipmethod == SIP_INVITE)
14504 handle_response_invite(p, resp, rest, req, seqno);
14505 else if (sipmethod == SIP_BYE) {
14506 char *auth, *auth2;
14507
14508 auth = (resp == 407 ? "Proxy-Authenticate" : "WWW-Authenticate");
14509 auth2 = (resp == 407 ? "Proxy-Authorization" : "Authorization");
14510 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, auth, auth2, sipmethod, 0)) {
14511 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
14512 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14513 }
14514 }
14515 break;
14516 case 481:
14517 if (sipmethod == SIP_INVITE) {
14518
14519 handle_response_invite(p, resp, rest, req, seqno);
14520 } else if (sipmethod == SIP_BYE) {
14521 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14522 } else if (sipmethod == SIP_NOTIFY) {
14523 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14524 } else if (sipdebug) {
14525 ast_log (LOG_DEBUG, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
14526 }
14527 break;
14528 case 501:
14529 if (sipmethod == SIP_INVITE)
14530 handle_response_invite(p, resp, rest, req, seqno);
14531 else if (sipmethod == SIP_REFER)
14532 handle_response_refer(p, resp, rest, req, seqno);
14533 break;
14534 case 603:
14535 if (sipmethod == SIP_REFER) {
14536 handle_response_refer(p, resp, rest, req, seqno);
14537 break;
14538 }
14539
14540 default:
14541 if ((resp >= 100) && (resp < 200)) {
14542 if (sipmethod == SIP_INVITE) {
14543 if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
14544 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
14545 }
14546 }
14547 if ((resp >= 300) && (resp < 700)) {
14548 if ((option_verbose > 2) && (resp != 487))
14549 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));
14550 switch(resp) {
14551 case 488:
14552 case 603:
14553 case 500:
14554 case 502:
14555 case 503:
14556 case 504:
14557
14558
14559 if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
14560 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
14561 break;
14562 }
14563 }
14564 break;
14565 }
14566 }
14567 }
14568
14569
14570
14571
14572
14573
14574
14575 static void *sip_park_thread(void *stuff)
14576 {
14577 struct ast_channel *transferee, *transferer;
14578 struct sip_dual *d;
14579 struct sip_request req;
14580 int ext;
14581 int res;
14582
14583 d = stuff;
14584 transferee = d->chan1;
14585 transferer = d->chan2;
14586 copy_request(&req, &d->req);
14587
14588 if (!transferee || !transferer) {
14589 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
14590 ast_free(d);
14591 return NULL;
14592 }
14593 if (option_debug > 3)
14594 ast_log(LOG_DEBUG, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
14595
14596 ast_channel_lock(transferee);
14597 if (ast_do_masquerade(transferee)) {
14598 ast_log(LOG_WARNING, "Masquerade failed.\n");
14599 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
14600 ast_channel_unlock(transferee);
14601 ast_free(d);
14602 return NULL;
14603 }
14604 ast_channel_unlock(transferee);
14605
14606 res = ast_park_call(transferee, transferer, 0, &ext);
14607
14608
14609 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
14610 if (!res) {
14611 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
14612 } else {
14613
14614 sprintf(buf, "Call parked on extension '%d'", ext);
14615 transmit_message_with_text(transferer->tech_pvt, buf);
14616 }
14617 #endif
14618
14619
14620
14621 if (!res) {
14622
14623 append_history(transferer->tech_pvt, "SIPpark","Parked call on %d", ext);
14624 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
14625 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
14626 ast_hangup(transferer);
14627 if (option_debug)
14628 ast_log(LOG_DEBUG, "SIP Call parked on extension '%d'\n", ext);
14629 } else {
14630 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
14631 append_history(transferer->tech_pvt, "SIPpark","Parking failed\n");
14632 if (option_debug)
14633 ast_log(LOG_DEBUG, "SIP Call parked failed \n");
14634
14635 }
14636 ast_free(d);
14637 return NULL;
14638 }
14639
14640
14641
14642
14643 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno)
14644 {
14645 struct sip_dual *d;
14646 struct ast_channel *transferee, *transferer;
14647
14648 pthread_t th;
14649 pthread_attr_t attr;
14650
14651 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
14652 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "SIPPeer/%s", chan2->name);
14653 if ((!transferer) || (!transferee)) {
14654 if (transferee) {
14655 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
14656 ast_hangup(transferee);
14657 }
14658 if (transferer) {
14659 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
14660 ast_hangup(transferer);
14661 }
14662 return -1;
14663 }
14664
14665
14666 transferee->readformat = chan1->readformat;
14667 transferee->writeformat = chan1->writeformat;
14668
14669
14670 ast_channel_masquerade(transferee, chan1);
14671
14672
14673 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
14674 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
14675 transferee->priority = chan1->priority;
14676
14677
14678
14679
14680
14681 transferer->readformat = chan2->readformat;
14682 transferer->writeformat = chan2->writeformat;
14683
14684
14685
14686
14687 while (ast_channel_trylock(chan2)) {
14688 struct sip_pvt *pvt = chan2->tech_pvt;
14689 DEADLOCK_AVOIDANCE(&pvt->lock);
14690 }
14691 ast_channel_masquerade(transferer, chan2);
14692 ast_channel_unlock(chan2);
14693
14694
14695 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
14696 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
14697 transferer->priority = chan2->priority;
14698
14699 ast_channel_lock(transferer);
14700 if (ast_do_masquerade(transferer)) {
14701 ast_log(LOG_WARNING, "Masquerade failed :(\n");
14702 ast_channel_unlock(transferer);
14703 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
14704 ast_hangup(transferer);
14705 return -1;
14706 }
14707 ast_channel_unlock(transferer);
14708 if (!transferer || !transferee) {
14709 if (!transferer) {
14710 if (option_debug)
14711 ast_log(LOG_DEBUG, "No transferer channel, giving up parking\n");
14712 }
14713 if (!transferee) {
14714 if (option_debug)
14715 ast_log(LOG_DEBUG, "No transferee channel, giving up parking\n");
14716 }
14717 return -1;
14718 }
14719 if (!(d = ast_calloc(1, sizeof(*d)))) {
14720 return -1;
14721 }
14722
14723 pthread_attr_init(&attr);
14724 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
14725
14726
14727 copy_request(&d->req, req);
14728 d->chan1 = transferee;
14729 d->chan2 = transferer;
14730 d->seqno = seqno;
14731 if (ast_pthread_create_background(&th, &attr, sip_park_thread, d) < 0) {
14732
14733 ast_free(d);
14734
14735 pthread_attr_destroy(&attr);
14736 return -1;
14737 }
14738 pthread_attr_destroy(&attr);
14739
14740 return 0;
14741 }
14742
14743
14744
14745
14746 static void ast_quiet_chan(struct ast_channel *chan)
14747 {
14748 if (chan && chan->_state == AST_STATE_UP) {
14749 if (ast_test_flag(chan, AST_FLAG_MOH))
14750 ast_moh_stop(chan);
14751 else if (chan->generatordata)
14752 ast_deactivate_generator(chan);
14753 }
14754 }
14755
14756
14757
14758 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
14759 {
14760 int res = 0;
14761 struct ast_channel *peera = NULL,
14762 *peerb = NULL,
14763 *peerc = NULL,
14764 *peerd = NULL;
14765
14766
14767
14768
14769 if (option_debug > 3) {
14770 ast_log(LOG_DEBUG, "Sip transfer:--------------------\n");
14771 if (transferer->chan1)
14772 ast_log(LOG_DEBUG, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
14773 else
14774 ast_log(LOG_DEBUG, "-- No transferer first channel - odd??? \n");
14775 if (target->chan1)
14776 ast_log(LOG_DEBUG, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
14777 else
14778 ast_log(LOG_DEBUG, "-- No target first channel ---\n");
14779 if (transferer->chan2)
14780 ast_log(LOG_DEBUG, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
14781 else
14782 ast_log(LOG_DEBUG, "-- No bridged call to transferee\n");
14783 if (target->chan2)
14784 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)");
14785 else
14786 ast_log(LOG_DEBUG, "-- No target second channel ---\n");
14787 ast_log(LOG_DEBUG, "-- END Sip transfer:--------------------\n");
14788 }
14789 if (transferer->chan2) {
14790 peera = transferer->chan1;
14791 peerb = target->chan1;
14792 peerc = transferer->chan2;
14793 peerd = target->chan2;
14794 if (option_debug > 2)
14795 ast_log(LOG_DEBUG, "SIP transfer: Four channels to handle\n");
14796 } else if (target->chan2) {
14797 peera = target->chan1;
14798 peerb = transferer->chan1;
14799 peerc = target->chan2;
14800 peerd = transferer->chan2;
14801 if (option_debug > 2)
14802 ast_log(LOG_DEBUG, "SIP transfer: Three channels to handle\n");
14803 }
14804
14805 if (peera && peerb && peerc && (peerb != peerc)) {
14806 ast_quiet_chan(peera);
14807 ast_quiet_chan(peerb);
14808 ast_quiet_chan(peerc);
14809 if (peerd)
14810 ast_quiet_chan(peerd);
14811
14812 if (option_debug > 3)
14813 ast_log(LOG_DEBUG, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
14814 if (ast_channel_masquerade(peerb, peerc)) {
14815 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
14816 res = -1;
14817 } else
14818 ast_log(LOG_DEBUG, "SIP transfer: Succeeded to masquerade channels.\n");
14819 return res;
14820 } else {
14821 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
14822 if (transferer->chan1)
14823 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
14824 if (target->chan1)
14825 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
14826 return -2;
14827 }
14828 return 0;
14829 }
14830
14831
14832
14833
14834
14835
14836 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
14837 {
14838 const char *thetag;
14839
14840 if (!tagbuf)
14841 return NULL;
14842 tagbuf[0] = '\0';
14843 thetag = get_header(req, header);
14844 thetag = strcasestr(thetag, ";tag=");
14845 if (thetag) {
14846 thetag += 5;
14847 ast_copy_string(tagbuf, thetag, tagbufsize);
14848 return strsep(&tagbuf, ";");
14849 }
14850 return NULL;
14851 }
14852
14853
14854 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
14855 {
14856
14857
14858 int res = 0;
14859 const char *event = get_header(req, "Event");
14860 char *eventid = NULL;
14861 char *sep;
14862
14863 if( (sep = strchr(event, ';')) ) {
14864 *sep++ = '\0';
14865 eventid = sep;
14866 }
14867
14868 if (option_debug > 1 && sipdebug)
14869 ast_log(LOG_DEBUG, "Got NOTIFY Event: %s\n", event);
14870
14871 if (strcmp(event, "refer")) {
14872
14873
14874 transmit_response(p, "489 Bad event", req);
14875 res = -1;
14876 } else {
14877
14878
14879
14880
14881
14882 char buf[1024];
14883 char *cmd, *code;
14884 int respcode;
14885 int success = TRUE;
14886
14887
14888
14889
14890
14891
14892
14893
14894 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
14895
14896 transmit_response(p, "400 Bad request", req);
14897 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14898 return -1;
14899 }
14900
14901
14902 if (get_msg_text(buf, sizeof(buf), req)) {
14903 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
14904 transmit_response(p, "400 Bad request", req);
14905 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14906 return -1;
14907 }
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929 if (option_debug > 2)
14930 ast_log(LOG_DEBUG, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
14931 cmd = ast_skip_blanks(buf);
14932 code = cmd;
14933
14934 while(*code && (*code > 32)) {
14935 code++;
14936 }
14937 *code++ = '\0';
14938 code = ast_skip_blanks(code);
14939 sep = code;
14940 sep++;
14941 while(*sep && (*sep > 32)) {
14942 sep++;
14943 }
14944 *sep++ = '\0';
14945 respcode = atoi(code);
14946 switch (respcode) {
14947 case 100:
14948 case 101:
14949
14950 break;
14951 case 183:
14952
14953 break;
14954 case 200:
14955
14956 break;
14957 case 301:
14958 case 302:
14959
14960 success = FALSE;
14961 break;
14962 case 503:
14963
14964 success = FALSE;
14965 break;
14966 case 603:
14967
14968 success = FALSE;
14969 break;
14970 }
14971 if (!success) {
14972 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
14973 }
14974
14975
14976 transmit_response(p, "200 OK", req);
14977 };
14978
14979 if (!p->lastinvite)
14980 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14981
14982 return res;
14983 }
14984
14985
14986 static int handle_request_options(struct sip_pvt *p, struct sip_request *req)
14987 {
14988 int res;
14989
14990
14991
14992
14993 if (p->lastinvite) {
14994
14995 transmit_response_with_allow(p, "200 OK", req, 0);
14996 return 0;
14997 }
14998
14999 res = get_destination(p, req);
15000 build_contact(p);
15001
15002 if (ast_strlen_zero(p->context))
15003 ast_string_field_set(p, context, default_context);
15004
15005 if (ast_shutting_down())
15006 transmit_response_with_allow(p, "503 Unavailable", req, 0);
15007 else if (res < 0)
15008 transmit_response_with_allow(p, "404 Not Found", req, 0);
15009 else
15010 transmit_response_with_allow(p, "200 OK", req, 0);
15011
15012
15013
15014 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15015
15016 return res;
15017 }
15018
15019
15020
15021
15022
15023
15024
15025
15026 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)
15027 {
15028 int earlyreplace = 0;
15029 int oneleggedreplace = 0;
15030 struct ast_channel *c = p->owner;
15031 struct ast_channel *replacecall = p->refer->refer_call->owner;
15032 struct ast_channel *targetcall;
15033
15034
15035 if (replacecall->_state == AST_STATE_RING)
15036 earlyreplace = 1;
15037
15038
15039 if (!(targetcall = ast_bridged_channel(replacecall))) {
15040
15041 if (!earlyreplace) {
15042 if (option_debug > 1)
15043 ast_log(LOG_DEBUG, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
15044 oneleggedreplace = 1;
15045 }
15046 }
15047 if (option_debug > 3 && targetcall && targetcall->_state == AST_STATE_RINGING)
15048 ast_log(LOG_DEBUG, "SIP transfer: Target channel is in ringing state\n");
15049
15050 if (option_debug > 3) {
15051 if (targetcall)
15052 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);
15053 else
15054 ast_log(LOG_DEBUG, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
15055 }
15056
15057 if (ignore) {
15058 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
15059
15060
15061
15062 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
15063
15064 if (c) {
15065 *nounlock = 1;
15066 ast_channel_unlock(c);
15067 }
15068 ast_channel_unlock(replacecall);
15069 ast_mutex_unlock(&p->refer->refer_call->lock);
15070 return 1;
15071 }
15072 if (!c) {
15073
15074 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
15075 transmit_response_reliable(p, "503 Service Unavailable", req);
15076 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
15077 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15078 ast_channel_unlock(replacecall);
15079 ast_mutex_unlock(&p->refer->refer_call->lock);
15080 return 1;
15081 }
15082 append_history(p, "Xfer", "INVITE/Replace received");
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094 transmit_response(p, "100 Trying", req);
15095 ast_setstate(c, AST_STATE_RING);
15096
15097
15098
15099
15100
15101 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
15102
15103 ast_setstate(c, AST_STATE_UP);
15104
15105
15106 ast_quiet_chan(replacecall);
15107 ast_quiet_chan(targetcall);
15108 if (option_debug > 3)
15109 ast_log(LOG_DEBUG, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
15110
15111
15112 if (! earlyreplace && ! oneleggedreplace )
15113 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
15114
15115
15116 if(ast_channel_masquerade(replacecall, c))
15117 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
15118 else if (option_debug > 3)
15119 ast_log(LOG_DEBUG, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
15120
15121
15122 if (ast_do_masquerade(replacecall)) {
15123 ast_log(LOG_WARNING, "Failed to perform masquerade with INVITE replaces\n");
15124 }
15125
15126 if (earlyreplace || oneleggedreplace ) {
15127 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
15128 }
15129
15130 ast_setstate(c, AST_STATE_DOWN);
15131 if (option_debug > 3) {
15132 struct ast_channel *test;
15133 ast_log(LOG_DEBUG, "After transfer:----------------------------\n");
15134 ast_log(LOG_DEBUG, " -- C: %s State %s\n", c->name, ast_state2str(c->_state));
15135 if (replacecall)
15136 ast_log(LOG_DEBUG, " -- replacecall: %s State %s\n", replacecall->name, ast_state2str(replacecall->_state));
15137 if (p->owner) {
15138 ast_log(LOG_DEBUG, " -- P->owner: %s State %s\n", p->owner->name, ast_state2str(p->owner->_state));
15139 test = ast_bridged_channel(p->owner);
15140 if (test)
15141 ast_log(LOG_DEBUG, " -- Call bridged to P->owner: %s State %s\n", test->name, ast_state2str(test->_state));
15142 else
15143 ast_log(LOG_DEBUG, " -- No call bridged to C->owner \n");
15144 } else
15145 ast_log(LOG_DEBUG, " -- No channel yet \n");
15146 ast_log(LOG_DEBUG, "End After transfer:----------------------------\n");
15147 }
15148
15149
15150 ast_channel_unlock(replacecall);
15151 ast_channel_unlock(c);
15152 ast_mutex_unlock(&p->refer->refer_call->lock);
15153 ast_mutex_unlock(&p->lock);
15154 *nounlock = 1;
15155
15156
15157 c->tech_pvt = NULL;
15158 ast_hangup(c);
15159
15160 ast_mutex_lock(&p->lock);
15161
15162 return 0;
15163 }
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182 static int sip_uri_params_cmp(const char *input1, const char *input2)
15183 {
15184 char *params1 = NULL;
15185 char *params2 = NULL;
15186 char *pos1;
15187 char *pos2;
15188 int zerolength1 = 0;
15189 int zerolength2 = 0;
15190 int maddrmatch = 0;
15191 int ttlmatch = 0;
15192 int usermatch = 0;
15193 int methodmatch = 0;
15194
15195 if (ast_strlen_zero(input1)) {
15196 zerolength1 = 1;
15197 } else {
15198 params1 = ast_strdupa(input1);
15199 }
15200 if (ast_strlen_zero(input2)) {
15201 zerolength2 = 1;
15202 } else {
15203 params2 = ast_strdupa(input2);
15204 }
15205
15206
15207
15208
15209 if (zerolength1 && zerolength2) {
15210 return 0;
15211 }
15212
15213 for (pos1 = strsep(¶ms1, ";"); pos1; pos1 = strsep(¶ms1, ";")) {
15214 char *value1 = pos1;
15215 char *name1 = strsep(&value1, "=");
15216 char *params2dup = NULL;
15217 int matched = 0;
15218 if (!value1) {
15219 value1 = "";
15220 }
15221
15222
15223
15224
15225 if (!zerolength2) {
15226 params2dup = ast_strdupa(params2);
15227 }
15228 for (pos2 = strsep(¶ms2dup, ";"); pos2; pos2 = strsep(¶ms2dup, ";")) {
15229 char *name2 = pos2;
15230 char *value2 = strchr(pos2, '=');
15231 if (!value2) {
15232 value2 = "";
15233 } else {
15234 *value2++ = '\0';
15235 }
15236 if (!strcasecmp(name1, name2)) {
15237 if (strcasecmp(value1, value2)) {
15238 goto fail;
15239 } else {
15240 matched = 1;
15241 break;
15242 }
15243 }
15244 }
15245
15246 if (!strcasecmp(name1, "maddr")) {
15247 if (matched) {
15248 maddrmatch = 1;
15249 } else {
15250 goto fail;
15251 }
15252 } else if (!strcasecmp(name1, "ttl")) {
15253 if (matched) {
15254 ttlmatch = 1;
15255 } else {
15256 goto fail;
15257 }
15258 } else if (!strcasecmp(name1, "user")) {
15259 if (matched) {
15260 usermatch = 1;
15261 } else {
15262 goto fail;
15263 }
15264 } else if (!strcasecmp(name1, "method")) {
15265 if (matched) {
15266 methodmatch = 1;
15267 } else {
15268 goto fail;
15269 }
15270 }
15271 }
15272
15273
15274
15275
15276
15277 for (pos2 = strsep(¶ms2, ";"); pos2; pos2 = strsep(¶ms2, ";")) {
15278 char *value2 = pos2;
15279 char *name2 = strsep(&value2, "=");
15280 if (!value2) {
15281 value2 = "";
15282 }
15283 if ((!strcasecmp(name2, "maddr") && !maddrmatch) ||
15284 (!strcasecmp(name2, "ttl") && !ttlmatch) ||
15285 (!strcasecmp(name2, "user") && !usermatch) ||
15286 (!strcasecmp(name2, "method") && !methodmatch)) {
15287 goto fail;
15288 }
15289 }
15290 return 0;
15291
15292 fail:
15293 return 1;
15294 }
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307 static int sip_uri_headers_cmp(const char *input1, const char *input2)
15308 {
15309 char *headers1 = NULL;
15310 char *headers2 = NULL;
15311 int zerolength1 = 0;
15312 int zerolength2 = 0;
15313 int different = 0;
15314 char *header1;
15315
15316 if (ast_strlen_zero(input1)) {
15317 zerolength1 = 1;
15318 } else {
15319 headers1 = ast_strdupa(input1);
15320 }
15321
15322 if (ast_strlen_zero(input2)) {
15323 zerolength2 = 1;
15324 } else {
15325 headers2 = ast_strdupa(input2);
15326 }
15327
15328 if ((zerolength1 && !zerolength2) ||
15329 (zerolength2 && !zerolength1))
15330 return 1;
15331
15332 if (zerolength1 && zerolength2)
15333 return 0;
15334
15335
15336
15337
15338
15339 if (strlen(headers1) != strlen(headers2)) {
15340 return 1;
15341 }
15342
15343 for (header1 = strsep(&headers1, "&"); header1; header1 = strsep(&headers1, "&")) {
15344 if (!strcasestr(headers2, header1)) {
15345 different = 1;
15346 break;
15347 }
15348 }
15349
15350 return different;
15351 }
15352
15353 static int sip_uri_cmp(const char *input1, const char *input2)
15354 {
15355 char *uri1 = ast_strdupa(input1);
15356 char *uri2 = ast_strdupa(input2);
15357 char *host1;
15358 char *host2;
15359 char *params1;
15360 char *params2;
15361 char *headers1;
15362 char *headers2;
15363
15364
15365
15366
15367 strsep(&uri1, ":");
15368 strsep(&uri2, ":");
15369
15370 if ((host1 = strchr(uri1, '@'))) {
15371 *host1++ = '\0';
15372 }
15373 if ((host2 = strchr(uri2, '@'))) {
15374 *host2++ = '\0';
15375 }
15376
15377
15378
15379
15380 if ((host1 && !host2) ||
15381 (host2 && !host1) ||
15382 (host1 && host2 && strcmp(uri1, uri2))) {
15383 return 1;
15384 }
15385
15386 if (!host1)
15387 host1 = uri1;
15388 if (!host2)
15389 host2 = uri2;
15390
15391
15392
15393
15394
15395 if ((params1 = strchr(host1, ';'))) {
15396 *params1++ = '\0';
15397 }
15398 if ((params2 = strchr(host2, ';'))) {
15399 *params2++ = '\0';
15400 }
15401
15402
15403
15404
15405 if ((headers1 = strchr(S_OR(params1, host1), '?'))) {
15406 *headers1++ = '\0';
15407 }
15408 if ((headers2 = strchr(S_OR(params2, host2), '?'))) {
15409 *headers2++ = '\0';
15410 }
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422 if (strcasecmp(host1, host2)) {
15423 return 1;
15424 }
15425
15426
15427 if (sip_uri_headers_cmp(headers1, headers2)) {
15428 return 1;
15429 }
15430
15431
15432 return sip_uri_params_cmp(params1, params2);
15433 }
15434
15435
15436
15437
15438
15439
15440
15441
15442 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)
15443 {
15444 int res = 1;
15445 int gotdest;
15446 const char *p_replaces;
15447 char *replace_id = NULL;
15448 int refer_locked = 0;
15449 const char *required;
15450 unsigned int required_profile = 0;
15451 struct ast_channel *c = NULL;
15452 int reinvite = 0;
15453
15454
15455 if (!p->sipoptions) {
15456 const char *supported = get_header(req, "Supported");
15457 if (!ast_strlen_zero(supported))
15458 parse_sip_options(p, supported);
15459 }
15460
15461
15462 required = get_header(req, "Require");
15463 if (!ast_strlen_zero(required)) {
15464 required_profile = parse_sip_options(NULL, required);
15465 if (required_profile && !(required_profile & SIP_OPT_REPLACES)) {
15466
15467 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
15468 ast_log(LOG_WARNING,"Received SIP INVITE with unsupported required extension: %s\n", required);
15469 p->invitestate = INV_COMPLETED;
15470 if (!p->lastinvite)
15471 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15472 res = -1;
15473 goto request_invite_cleanup;
15474 }
15475 }
15476
15477
15478 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->invitestate != INV_TERMINATED && p->invitestate != INV_CONFIRMED)) {
15479
15480
15481
15482
15483
15484 int different;
15485 if (pedanticsipchecking)
15486 different = sip_uri_cmp(p->initreq.rlPart2, req->rlPart2);
15487 else
15488 different = strcmp(p->initreq.rlPart2, req->rlPart2);
15489 if (!different) {
15490 transmit_response(p, "482 Loop Detected", req);
15491 p->invitestate = INV_COMPLETED;
15492 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15493 res = 0;
15494 goto request_invite_cleanup;
15495 } else {
15496
15497
15498
15499
15500 char *uri = ast_strdupa(req->rlPart2);
15501 char *at = strchr(uri, '@');
15502 char *peerorhost;
15503 if (option_debug > 2) {
15504 ast_log(LOG_DEBUG, "Potential spiral detected. Original RURI was %s, new RURI is %s\n", p->initreq.rlPart2, req->rlPart2);
15505 }
15506 transmit_response(p, "100 Trying", req);
15507 if (at) {
15508 *at = '\0';
15509 }
15510
15511 if ((peerorhost = strchr(uri, ':'))) {
15512 *peerorhost++ = '\0';
15513 }
15514 ast_string_field_free(p, theirtag);
15515
15516
15517 ast_string_field_set(p->owner, call_forward, peerorhost);
15518 ast_queue_control(p->owner, AST_CONTROL_BUSY);
15519 res = 0;
15520 goto request_invite_cleanup;
15521 }
15522 }
15523
15524 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->pendinginvite) {
15525 if (!ast_test_flag(&p->flags[0], SIP_OUTGOING) && (p->invitestate == INV_COMPLETED || p->invitestate == INV_TERMINATED)) {
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536 __sip_ack(p, p->pendinginvite, FLAG_RESPONSE, 0);
15537 } else {
15538
15539 p->glareinvite = seqno;
15540 if (p->rtp && find_sdp(req)) {
15541 struct sockaddr_in sin;
15542 if (get_ip_and_port_from_sdp(req, SDP_AUDIO, &sin)) {
15543 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Audio may not work properly on this call.\n");
15544 } else {
15545 ast_rtp_set_alt_peer(p->rtp, &sin);
15546 }
15547 if (p->vrtp) {
15548 if (get_ip_and_port_from_sdp(req, SDP_VIDEO, &sin)) {
15549 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Video may not work properly on this call.\n");
15550 } else {
15551 ast_rtp_set_alt_peer(p->vrtp, &sin);
15552 }
15553 }
15554 }
15555 transmit_response_reliable(p, "491 Request Pending", req);
15556 if (option_debug)
15557 ast_log(LOG_DEBUG, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
15558
15559 res = 0;
15560 goto request_invite_cleanup;
15561 }
15562 }
15563
15564 p_replaces = get_header(req, "Replaces");
15565 if (!ast_strlen_zero(p_replaces)) {
15566
15567 char *ptr;
15568 char *fromtag = NULL;
15569 char *totag = NULL;
15570 char *start, *to;
15571 int error = 0;
15572
15573 if (p->owner) {
15574 if (option_debug > 2)
15575 ast_log(LOG_DEBUG, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
15576 transmit_response_reliable(p, "400 Bad request", req);
15577
15578 res = -1;
15579 goto request_invite_cleanup;
15580 }
15581
15582 if (sipdebug && option_debug > 2)
15583 ast_log(LOG_DEBUG, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
15584
15585 replace_id = ast_strdupa(p_replaces);
15586 ast_uri_decode(replace_id);
15587
15588 if (!p->refer && !sip_refer_allocate(p)) {
15589 transmit_response_reliable(p, "500 Server Internal Error", req);
15590 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
15591 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15592 p->invitestate = INV_COMPLETED;
15593 res = -1;
15594 goto request_invite_cleanup;
15595 }
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606 replace_id = ast_skip_blanks(replace_id);
15607
15608 start = replace_id;
15609 while ( (ptr = strsep(&start, ";")) ) {
15610 ptr = ast_skip_blanks(ptr);
15611 if ( (to = strcasestr(ptr, "to-tag=") ) )
15612 totag = to + 7;
15613 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
15614 fromtag = to + 9;
15615 fromtag = strsep(&fromtag, "&");
15616 }
15617 }
15618
15619 if (sipdebug && option_debug > 3)
15620 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>");
15621
15622
15623
15624
15625
15626 if ((p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
15627 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
15628 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
15629 error = 1;
15630 } else {
15631 refer_locked = 1;
15632 }
15633
15634
15635
15636
15637
15638
15639
15640 if (p->refer->refer_call == p) {
15641 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
15642 p->refer->refer_call = NULL;
15643 transmit_response_reliable(p, "400 Bad request", req);
15644 error = 1;
15645 }
15646
15647 if (!error && !p->refer->refer_call->owner) {
15648
15649 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
15650
15651 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replace)", req);
15652 error = 1;
15653 }
15654
15655 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 ) {
15656 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
15657 transmit_response_reliable(p, "603 Declined (Replaces)", req);
15658 error = 1;
15659 }
15660
15661 if (error) {
15662 append_history(p, "Xfer", "INVITE/Replace Failed.");
15663 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15664 ast_mutex_unlock(&p->lock);
15665 if (p->refer->refer_call) {
15666 ast_mutex_unlock(&p->refer->refer_call->lock);
15667 if (p->refer->refer_call->owner) {
15668 ast_channel_unlock(p->refer->refer_call->owner);
15669 }
15670 }
15671 refer_locked = 0;
15672 p->invitestate = INV_COMPLETED;
15673 res = -1;
15674 goto request_invite_cleanup;
15675 }
15676 }
15677
15678
15679
15680
15681
15682 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
15683 int newcall = (p->initreq.headers ? TRUE : FALSE);
15684
15685 if (sip_cancel_destroy(p))
15686 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15687
15688 p->pendinginvite = seqno;
15689 check_via(p, req);
15690
15691 copy_request(&p->initreq, req);
15692 if (!p->owner) {
15693 if (debug)
15694 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
15695 if (newcall)
15696 append_history(p, "Invite", "New call: %s", p->callid);
15697 parse_ok_contact(p, req);
15698 } else {
15699 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
15700
15701 if (find_sdp(req)) {
15702 if (process_sdp(p, req)) {
15703 transmit_response_reliable(p, "488 Not acceptable here", req);
15704 if (!p->lastinvite)
15705 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15706 res = -1;
15707 goto request_invite_cleanup;
15708 }
15709 ast_queue_control(p->owner, AST_CONTROL_SRCUPDATE);
15710 } else {
15711 p->jointcapability = p->capability;
15712 if (option_debug > 2)
15713 ast_log(LOG_DEBUG, "Hm.... No sdp for the moment\n");
15714
15715
15716
15717 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
15718 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
15719
15720 ast_queue_frame(p->owner, &ast_null_frame);
15721 change_hold_state(p, req, FALSE, 0);
15722 }
15723 }
15724 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
15725 append_history(p, "ReInv", "Re-invite received");
15726 }
15727 } else if (debug)
15728 ast_verbose("Ignoring this INVITE request\n");
15729
15730
15731 if (!p->lastinvite && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner) {
15732
15733
15734 res = check_user(p, req, SIP_INVITE, e, XMIT_RELIABLE, sin);
15735 if (res == AUTH_CHALLENGE_SENT) {
15736 p->invitestate = INV_COMPLETED;
15737 res = 0;
15738 goto request_invite_cleanup;
15739 }
15740 if (res < 0) {
15741 if (res == AUTH_FAKE_AUTH) {
15742 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
15743 transmit_fake_auth_response(p, SIP_INVITE, req, XMIT_RELIABLE);
15744 } else {
15745 ast_log(LOG_NOTICE, "Failed to authenticate user %s\n", get_header(req, "From"));
15746 transmit_response_reliable(p, "403 Forbidden", req);
15747 }
15748 p->invitestate = INV_COMPLETED;
15749 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15750 ast_string_field_free(p, theirtag);
15751 res = 0;
15752 goto request_invite_cleanup;
15753
15754 }
15755
15756
15757 if (find_sdp(req)) {
15758 if (process_sdp(p, req)) {
15759
15760 transmit_response_reliable(p, "488 Not acceptable here", req);
15761 p->invitestate = INV_COMPLETED;
15762 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15763 if (option_debug)
15764 ast_log(LOG_DEBUG, "No compatible codecs for this SIP call.\n");
15765 res = -1;
15766 goto request_invite_cleanup;
15767 }
15768 } else {
15769 p->jointcapability = p->capability;
15770 if (option_debug > 1)
15771 ast_log(LOG_DEBUG, "No SDP in Invite, third party call control\n");
15772 }
15773
15774
15775
15776 if (p->owner)
15777 ast_queue_frame(p->owner, &ast_null_frame);
15778
15779
15780
15781 if (ast_strlen_zero(p->context))
15782 ast_string_field_set(p, context, default_context);
15783
15784
15785
15786 if (option_debug)
15787 ast_log(LOG_DEBUG, "Checking SIP call limits for device %s\n", p->username);
15788 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
15789 if (res < 0) {
15790 ast_log(LOG_NOTICE, "Failed to place call for user %s, too many calls\n", p->username);
15791 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
15792 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15793 p->invitestate = INV_COMPLETED;
15794 }
15795 res = 0;
15796 goto request_invite_cleanup;
15797 }
15798 gotdest = get_destination(p, NULL);
15799 get_rdnis(p, NULL);
15800 extract_uri(p, req);
15801 build_contact(p);
15802
15803 if (p->rtp) {
15804 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
15805 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
15806 }
15807
15808 if (!replace_id && gotdest) {
15809 if (gotdest == 1 && ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP))
15810 transmit_response_reliable(p, "484 Address Incomplete", req);
15811 else {
15812 char *decoded_exten = ast_strdupa(p->exten);
15813
15814 transmit_response_reliable(p, "404 Not Found", req);
15815 ast_uri_decode(decoded_exten);
15816 ast_log(LOG_NOTICE, "Call from '%s' to extension"
15817 " '%s' rejected because extension not found.\n",
15818 S_OR(p->username, p->peername), decoded_exten);
15819 }
15820 p->invitestate = INV_COMPLETED;
15821 update_call_counter(p, DEC_CALL_LIMIT);
15822 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15823 res = 0;
15824 goto request_invite_cleanup;
15825 } else {
15826
15827
15828 if (ast_strlen_zero(p->exten))
15829 ast_string_field_set(p, exten, "s");
15830
15831
15832 make_our_tag(p->tag, sizeof(p->tag));
15833
15834 c = sip_new(p, AST_STATE_DOWN, S_OR(p->peername, NULL));
15835 *recount = 1;
15836
15837
15838 build_route(p, req, 0);
15839
15840 if (c) {
15841
15842 ast_channel_lock(c);
15843 }
15844 }
15845 } else {
15846 if (option_debug > 1 && sipdebug) {
15847 if (!ast_test_flag(req, SIP_PKT_IGNORE))
15848 ast_log(LOG_DEBUG, "Got a SIP re-invite for call %s\n", p->callid);
15849 else
15850 ast_log(LOG_DEBUG, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
15851 }
15852 if (!ast_test_flag(req, SIP_PKT_IGNORE))
15853 reinvite = 1;
15854 c = p->owner;
15855 }
15856
15857 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
15858 p->lastinvite = seqno;
15859
15860 if (replace_id) {
15861
15862 if (sipdebug && option_debug > 3)
15863 ast_log(LOG_DEBUG, "Sending this call to the invite/replcaes handler %s\n", p->callid);
15864
15865 res = handle_invite_replaces(p, req, debug, ast_test_flag(req, SIP_PKT_IGNORE), seqno, sin, nounlock);
15866 refer_locked = 0;
15867 goto request_invite_cleanup;
15868 }
15869
15870
15871 if (c) {
15872 enum ast_channel_state c_state = c->_state;
15873
15874 if (c_state != AST_STATE_UP && reinvite &&
15875 (p->invitestate == INV_TERMINATED || p->invitestate == INV_CONFIRMED)) {
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885 c_state = AST_STATE_UP;
15886 }
15887
15888 switch(c_state) {
15889 case AST_STATE_DOWN:
15890 if (option_debug > 1)
15891 ast_log(LOG_DEBUG, "%s: New call is still down.... Trying... \n", c->name);
15892 transmit_provisional_response(p, "100 Trying", req, 0);
15893 p->invitestate = INV_PROCEEDING;
15894 ast_setstate(c, AST_STATE_RING);
15895 if (strcmp(p->exten, ast_pickup_ext())) {
15896 enum ast_pbx_result res;
15897
15898 res = ast_pbx_start(c);
15899
15900 switch(res) {
15901 case AST_PBX_FAILED:
15902 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
15903 p->invitestate = INV_COMPLETED;
15904 if (ast_test_flag(req, SIP_PKT_IGNORE))
15905 transmit_response(p, "503 Unavailable", req);
15906 else
15907 transmit_response_reliable(p, "503 Unavailable", req);
15908 break;
15909 case AST_PBX_CALL_LIMIT:
15910 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
15911 p->invitestate = INV_COMPLETED;
15912 if (ast_test_flag(req, SIP_PKT_IGNORE))
15913 transmit_response(p, "480 Temporarily Unavailable", req);
15914 else
15915 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
15916 break;
15917 case AST_PBX_SUCCESS:
15918
15919 break;
15920 }
15921
15922 if (res) {
15923
15924
15925 ast_mutex_unlock(&c->lock);
15926 *nounlock = 1;
15927 ast_mutex_unlock(&p->lock);
15928 ast_hangup(c);
15929 ast_mutex_lock(&p->lock);
15930 c = NULL;
15931 }
15932 } else {
15933 ast_channel_unlock(c);
15934 *nounlock = 1;
15935 ast_mutex_unlock(&p->lock);
15936 if (ast_pickup_call(c)) {
15937 ast_mutex_lock(&p->lock);
15938 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
15939 if (ast_test_flag(req, SIP_PKT_IGNORE))
15940 transmit_response(p, "503 Unavailable", req);
15941 else
15942 transmit_response_reliable(p, "503 Unavailable", req);
15943 sip_alreadygone(p);
15944
15945 ast_mutex_unlock(&p->lock);
15946 c->hangupcause = AST_CAUSE_CALL_REJECTED;
15947 } else {
15948 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
15949 }
15950 ast_hangup(c);
15951 ast_mutex_lock(&p->lock);
15952 p->invitestate = INV_COMPLETED;
15953 c = NULL;
15954 }
15955 break;
15956 case AST_STATE_RING:
15957 transmit_provisional_response(p, "100 Trying", req, 0);
15958 p->invitestate = INV_PROCEEDING;
15959 break;
15960 case AST_STATE_RINGING:
15961 transmit_provisional_response(p, "180 Ringing", req, 0);
15962 p->invitestate = INV_PROCEEDING;
15963 break;
15964 case AST_STATE_UP:
15965 if (option_debug > 1)
15966 ast_log(LOG_DEBUG, "%s: This call is UP.... \n", c->name);
15967
15968 transmit_response(p, "100 Trying", req);
15969
15970 if (p->t38.state == T38_PEER_REINVITE) {
15971 struct ast_channel *bridgepeer = NULL;
15972 struct sip_pvt *bridgepvt = NULL;
15973
15974 if ((bridgepeer = ast_bridged_channel(p->owner))) {
15975
15976
15977 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
15978 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
15979 if (bridgepvt->t38.state == T38_DISABLED) {
15980 if (bridgepvt->udptl) {
15981
15982 sip_handle_t38_reinvite(bridgepeer, p, 1);
15983 } else {
15984 ast_log(LOG_WARNING, "Strange... The other side of the bridge don't have udptl struct\n");
15985 ast_mutex_lock(&bridgepvt->lock);
15986 bridgepvt->t38.state = T38_DISABLED;
15987 ast_mutex_unlock(&bridgepvt->lock);
15988 if (option_debug > 1)
15989 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->name);
15990 if (ast_test_flag(req, SIP_PKT_IGNORE))
15991 transmit_response(p, "488 Not acceptable here", req);
15992 else
15993 transmit_response_reliable(p, "488 Not acceptable here", req);
15994
15995 }
15996 } else {
15997
15998 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
15999 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
16000 p->t38.state = T38_ENABLED;
16001 if (option_debug)
16002 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
16003 }
16004 } else {
16005
16006 if (ast_test_flag(req, SIP_PKT_IGNORE))
16007 transmit_response(p, "488 Not acceptable here", req);
16008 else
16009 transmit_response_reliable(p, "488 Not acceptable here", req);
16010 p->t38.state = T38_DISABLED;
16011 if (option_debug > 1)
16012 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
16013
16014 if (!p->lastinvite)
16015 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16016 }
16017 } else {
16018
16019 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16020 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
16021 p->t38.state = T38_ENABLED;
16022 if (option_debug)
16023 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
16024 }
16025 } else if (p->t38.state == T38_DISABLED) {
16026
16027 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16028 transmit_response_with_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (ast_test_flag(req, SIP_PKT_IGNORE) ? XMIT_UNRELIABLE : XMIT_CRITICAL)));
16029 }
16030 p->invitestate = INV_TERMINATED;
16031 break;
16032 default:
16033 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
16034 transmit_response(p, "100 Trying", req);
16035 break;
16036 }
16037 } else {
16038 if (p && (p->autokillid == -1)) {
16039 const char *msg;
16040
16041 if (!p->jointcapability)
16042 msg = "488 Not Acceptable Here (codec error)";
16043 else {
16044 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
16045 msg = "503 Unavailable";
16046 }
16047 if (ast_test_flag(req, SIP_PKT_IGNORE))
16048 transmit_response(p, msg, req);
16049 else
16050 transmit_response_reliable(p, msg, req);
16051 p->invitestate = INV_COMPLETED;
16052 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16053 }
16054 }
16055 return res;
16056
16057 request_invite_cleanup:
16058
16059 if (refer_locked && p->refer && p->refer->refer_call) {
16060 ast_mutex_unlock(&p->refer->refer_call->lock);
16061 if (p->refer->refer_call->owner) {
16062 ast_channel_unlock(p->refer->refer_call->owner);
16063 }
16064 }
16065
16066 return res;
16067 }
16068
16069
16070
16071 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno)
16072 {
16073 struct sip_dual target;
16074
16075 int res = 0;
16076 struct sip_pvt *targetcall_pvt;
16077
16078
16079 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
16080 transferer->refer->replaces_callid_fromtag))) {
16081 if (transferer->refer->localtransfer) {
16082
16083
16084
16085 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
16086 append_history(transferer, "Xfer", "Refer failed");
16087 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
16088 transferer->refer->status = REFER_FAILED;
16089 return -1;
16090 }
16091
16092 if (option_debug > 2)
16093 ast_log(LOG_DEBUG, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
16094 return 0;
16095 }
16096
16097
16098 append_history(transferer, "Xfer", "Refer accepted");
16099 if (!targetcall_pvt->owner) {
16100 if (option_debug > 3)
16101 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No owner of target call\n");
16102
16103 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
16104 append_history(transferer, "Xfer", "Refer failed");
16105 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
16106 transferer->refer->status = REFER_FAILED;
16107 ast_mutex_unlock(&targetcall_pvt->lock);
16108 return -1;
16109 }
16110
16111
16112 target.chan1 = targetcall_pvt->owner;
16113 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
16114
16115 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
16116
16117 if (option_debug > 3) {
16118 if (target.chan2)
16119 ast_log(LOG_DEBUG, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
16120 else if (target.chan1->_state != AST_STATE_RING)
16121 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No target channel\n");
16122 else
16123 ast_log(LOG_DEBUG, "SIP attended transfer: Attempting transfer in ringing state\n");
16124 }
16125 }
16126
16127
16128 if (option_debug > 3 && sipdebug) {
16129 if (current->chan2)
16130 ast_log(LOG_DEBUG, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
16131 else
16132 ast_log(LOG_DEBUG, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
16133 }
16134
16135 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
16136
16137
16138 res = attempt_transfer(current, &target);
16139 ast_mutex_unlock(&targetcall_pvt->lock);
16140 if (res) {
16141
16142 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
16143 append_history(transferer, "Xfer", "Refer failed");
16144 transferer->refer->status = REFER_FAILED;
16145 if (targetcall_pvt->owner)
16146 ast_channel_unlock(targetcall_pvt->owner);
16147
16148 if (res != -2)
16149 ast_hangup(transferer->owner);
16150 else
16151 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
16152 } else {
16153
16154
16155
16156 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
16157 append_history(transferer, "Xfer", "Refer succeeded");
16158 transferer->refer->status = REFER_200OK;
16159 if (targetcall_pvt->owner) {
16160 if (option_debug)
16161 ast_log(LOG_DEBUG, "SIP attended transfer: Unlocking channel %s\n", targetcall_pvt->owner->name);
16162 ast_channel_unlock(targetcall_pvt->owner);
16163 }
16164 ast_indicate(target.chan1, AST_CONTROL_UNHOLD);
16165 if (target.chan2) {
16166 ast_indicate(target.chan2, AST_CONTROL_UNHOLD);
16167 }
16168 }
16169 return 1;
16170 }
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock)
16237 {
16238 struct sip_dual current;
16239
16240
16241 int res = 0;
16242
16243 if (ast_test_flag(req, SIP_PKT_DEBUG))
16244 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");
16245
16246 if (!p->owner) {
16247
16248
16249 if (option_debug > 2)
16250 ast_log(LOG_DEBUG, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
16251 transmit_response(p, "603 Declined (No dialog)", req);
16252 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
16253 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
16254 sip_alreadygone(p);
16255 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16256 }
16257 return 0;
16258 }
16259
16260
16261
16262 if (p->allowtransfer == TRANSFER_CLOSED ) {
16263
16264 transmit_response(p, "603 Declined (policy)", req);
16265 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
16266
16267 return 0;
16268 }
16269
16270 if(!ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
16271
16272 transmit_response(p, "491 Request pending", req);
16273 append_history(p, "Xfer", "Refer failed. Request pending.");
16274 return 0;
16275 }
16276
16277
16278 if (!p->refer && !sip_refer_allocate(p)) {
16279 transmit_response(p, "500 Internal Server Error", req);
16280 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
16281 return -3;
16282 }
16283
16284 res = get_refer_info(p, req);
16285
16286 p->refer->status = REFER_SENT;
16287
16288 if (res != 0) {
16289 switch (res) {
16290 case -2:
16291 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
16292 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
16293 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
16294 ast_log(LOG_DEBUG, "SIP transfer to black hole can't be handled (no refer-to: )\n");
16295 break;
16296 case -3:
16297 transmit_response(p, "603 Declined (Non sip: uri)", req);
16298 append_history(p, "Xfer", "Refer failed. Non SIP uri");
16299 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
16300 ast_log(LOG_DEBUG, "SIP transfer to non-SIP uri denied\n");
16301 break;
16302 default:
16303
16304 transmit_response(p, "202 Accepted", req);
16305 append_history(p, "Xfer", "Refer failed. Bad extension.");
16306 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
16307 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
16308 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
16309 ast_log(LOG_DEBUG, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
16310 break;
16311 }
16312 return 0;
16313 }
16314 if (ast_strlen_zero(p->context))
16315 ast_string_field_set(p, context, default_context);
16316
16317
16318 if (allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
16319 p->refer->localtransfer = 1;
16320 if (sipdebug && option_debug > 2)
16321 ast_log(LOG_DEBUG, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
16322 } else if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
16323
16324 p->refer->localtransfer = 1;
16325 } else if (sipdebug && option_debug > 2)
16326 ast_log(LOG_DEBUG, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
16327
16328
16329
16330 if (ignore)
16331 return res;
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359 current.chan1 = p->owner;
16360
16361
16362 current.chan2 = ast_bridged_channel(current.chan1);
16363
16364 if (sipdebug && option_debug > 2)
16365 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>");
16366
16367 if (!current.chan2 && !p->refer->attendedtransfer) {
16368
16369
16370
16371 if (sipdebug && option_debug > 2)
16372 ast_log(LOG_DEBUG,"Refused SIP transfer on non-bridged channel.\n");
16373 p->refer->status = REFER_FAILED;
16374 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
16375 transmit_response(p, "603 Declined", req);
16376 return -1;
16377 }
16378
16379 if (current.chan2) {
16380 if (sipdebug && option_debug > 3)
16381 ast_log(LOG_DEBUG, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
16382
16383 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
16384 }
16385
16386 ast_set_flag(&p->flags[0], SIP_GOTREFER);
16387
16388
16389 transmit_response(p, "202 Accepted", req);
16390
16391
16392 if (p->refer->attendedtransfer) {
16393 if ((res = local_attended_transfer(p, ¤t, req, seqno)))
16394 return res;
16395
16396 if (sipdebug && option_debug > 3)
16397 ast_log(LOG_DEBUG, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
16398
16399 }
16400
16401
16402
16403 if (p->refer->localtransfer && !strcmp(p->refer->refer_to, ast_parking_ext())) {
16404
16405 *nounlock = 1;
16406 ast_channel_unlock(current.chan1);
16407 copy_request(¤t.req, req);
16408 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
16409 p->refer->status = REFER_200OK;
16410 append_history(p, "Xfer", "REFER to call parking.");
16411 if (sipdebug && option_debug > 3)
16412 ast_log(LOG_DEBUG, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
16413 if ((res = sip_park(current.chan2, current.chan1, req, seqno))) {
16414 transmit_notify_with_sipfrag(p, seqno, "500 Internal Server Error", TRUE);
16415 }
16416 return res;
16417 }
16418
16419
16420 if (current.chan1 && current.chan2) {
16421 if (option_debug > 2)
16422 ast_log(LOG_DEBUG, "chan1->name: %s\n", current.chan1->name);
16423 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
16424 }
16425 if (current.chan2) {
16426 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
16427 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
16428 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
16429
16430 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
16431
16432 if (p->refer->referred_by)
16433 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
16434 }
16435
16436 if (p->refer->replaces_callid && !ast_strlen_zero(p->refer->replaces_callid)) {
16437 char tempheader[SIPBUFSIZE];
16438 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
16439 p->refer->replaces_callid_totag ? ";to-tag=" : "",
16440 p->refer->replaces_callid_totag,
16441 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
16442 p->refer->replaces_callid_fromtag);
16443 if (current.chan2)
16444 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
16445 }
16446
16447
16448 *nounlock = 1;
16449 ast_channel_unlock(current.chan1);
16450
16451
16452
16453
16454 if (!p->refer->attendedtransfer)
16455 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
16456
16457
16458
16459
16460
16461 if (!current.chan2) {
16462
16463
16464
16465
16466
16467
16468
16469 p->refer->status = REFER_FAILED;
16470 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
16471 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
16472 append_history(p, "Xfer", "Refer failed (only bridged calls).");
16473 return -1;
16474 }
16475 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
16476
16477
16478
16479
16480
16481 ast_indicate(current.chan2, AST_CONTROL_UNHOLD);
16482 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
16483
16484 if (!res) {
16485
16486 if (option_debug > 2)
16487 ast_log(LOG_DEBUG, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
16488 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
16489 if (p->refer->localtransfer)
16490 p->refer->status = REFER_200OK;
16491 if (p->owner)
16492 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
16493 append_history(p, "Xfer", "Refer succeeded.");
16494 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
16495
16496
16497 res = 0;
16498 } else {
16499 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
16500 if (option_debug > 2)
16501 ast_log(LOG_DEBUG, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
16502 append_history(p, "Xfer", "Refer failed.");
16503
16504 p->refer->status = REFER_FAILED;
16505 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
16506 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
16507 res = -1;
16508 }
16509 return res;
16510 }
16511
16512
16513 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
16514 {
16515
16516 check_via(p, req);
16517 sip_alreadygone(p);
16518
16519
16520
16521
16522
16523
16524 if (p->invitestate == INV_TERMINATED)
16525 __sip_pretend_ack(p);
16526 else
16527 p->invitestate = INV_CANCELLED;
16528
16529 if (p->owner && p->owner->_state == AST_STATE_UP) {
16530
16531 transmit_response(p, "200 OK", req);
16532 if (option_debug)
16533 ast_log(LOG_DEBUG, "Got CANCEL on an answered call. Ignoring... \n");
16534 return 0;
16535 }
16536
16537 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
16538 update_call_counter(p, DEC_CALL_LIMIT);
16539
16540 stop_media_flows(p);
16541 if (p->owner)
16542 ast_queue_hangup(p->owner);
16543 else
16544 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16545 if (p->initreq.len > 0) {
16546 struct sip_pkt *pkt, *prev_pkt;
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558 for (pkt = p->packets, prev_pkt = NULL; pkt; prev_pkt = pkt, pkt = pkt->next) {
16559 if (pkt->seqno == p->lastinvite && pkt->response_code == 487) {
16560 AST_SCHED_DEL(sched, pkt->retransid);
16561 UNLINK(pkt, p->packets, prev_pkt);
16562 ast_free(pkt);
16563 break;
16564 }
16565 }
16566 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
16567 transmit_response(p, "200 OK", req);
16568 return 1;
16569 } else {
16570 transmit_response(p, "481 Call Leg Does Not Exist", req);
16571 return 0;
16572 }
16573 }
16574
16575 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen)
16576 {
16577 struct ast_rtp_quality qos;
16578 struct sip_pvt *p = chan->tech_pvt;
16579 char *all = "", *parse = ast_strdupa(preparse);
16580 AST_DECLARE_APP_ARGS(args,
16581 AST_APP_ARG(param);
16582 AST_APP_ARG(type);
16583 AST_APP_ARG(field);
16584 );
16585 AST_STANDARD_APP_ARGS(args, parse);
16586
16587
16588 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
16589 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
16590 return 0;
16591 }
16592
16593 if (strcasecmp(args.param, "rtpqos"))
16594 return 0;
16595
16596
16597 if (ast_strlen_zero(args.type))
16598 args.type = "audio";
16599 if (ast_strlen_zero(args.field))
16600 args.field = "all";
16601
16602 memset(buf, 0, buflen);
16603 memset(&qos, 0, sizeof(qos));
16604
16605 if (p == NULL) {
16606 return -1;
16607 }
16608
16609 if (strcasecmp(args.type, "AUDIO") == 0) {
16610 all = ast_rtp_get_quality(p->rtp, &qos);
16611 } else if (strcasecmp(args.type, "VIDEO") == 0) {
16612 all = ast_rtp_get_quality(p->vrtp, &qos);
16613 }
16614
16615 if (strcasecmp(args.field, "local_ssrc") == 0)
16616 snprintf(buf, buflen, "%u", qos.local_ssrc);
16617 else if (strcasecmp(args.field, "local_lostpackets") == 0)
16618 snprintf(buf, buflen, "%u", qos.local_lostpackets);
16619 else if (strcasecmp(args.field, "local_jitter") == 0)
16620 snprintf(buf, buflen, "%.0lf", qos.local_jitter * 1000.0);
16621 else if (strcasecmp(args.field, "local_count") == 0)
16622 snprintf(buf, buflen, "%u", qos.local_count);
16623 else if (strcasecmp(args.field, "remote_ssrc") == 0)
16624 snprintf(buf, buflen, "%u", qos.remote_ssrc);
16625 else if (strcasecmp(args.field, "remote_lostpackets") == 0)
16626 snprintf(buf, buflen, "%u", qos.remote_lostpackets);
16627 else if (strcasecmp(args.field, "remote_jitter") == 0)
16628 snprintf(buf, buflen, "%.0lf", qos.remote_jitter * 1000.0);
16629 else if (strcasecmp(args.field, "remote_count") == 0)
16630 snprintf(buf, buflen, "%u", qos.remote_count);
16631 else if (strcasecmp(args.field, "rtt") == 0)
16632 snprintf(buf, buflen, "%.0lf", qos.rtt * 1000.0);
16633 else if (strcasecmp(args.field, "all") == 0)
16634 ast_copy_string(buf, all, buflen);
16635 else {
16636 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
16637 return -1;
16638 }
16639 return 0;
16640 }
16641
16642
16643 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
16644 {
16645 struct ast_channel *c=NULL;
16646 int res;
16647 struct ast_channel *bridged_to;
16648
16649
16650 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_test_flag(req, SIP_PKT_IGNORE))
16651 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
16652
16653 __sip_pretend_ack(p);
16654
16655 p->invitestate = INV_TERMINATED;
16656
16657 copy_request(&p->initreq, req);
16658 check_via(p, req);
16659 sip_alreadygone(p);
16660
16661
16662 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY) || p->owner) {
16663 char *audioqos, *videoqos;
16664 if (p->rtp) {
16665 audioqos = ast_rtp_get_quality(p->rtp, NULL);
16666 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
16667 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
16668 if (p->owner)
16669 pbx_builtin_setvar_helper(p->owner, "RTPAUDIOQOS", audioqos);
16670 }
16671 if (p->vrtp) {
16672 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
16673 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
16674 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
16675 if (p->owner)
16676 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", videoqos);
16677 }
16678 }
16679
16680 stop_media_flows(p);
16681
16682 if (!ast_strlen_zero(get_header(req, "Also"))) {
16683 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
16684 ast_inet_ntoa(p->recv.sin_addr));
16685 if (ast_strlen_zero(p->context))
16686 ast_string_field_set(p, context, default_context);
16687 res = get_also_info(p, req);
16688 if (!res) {
16689 c = p->owner;
16690 if (c) {
16691 bridged_to = ast_bridged_channel(c);
16692 if (bridged_to) {
16693
16694 ast_queue_control(c, AST_CONTROL_UNHOLD);
16695 ast_async_goto(bridged_to, p->context, p->refer->refer_to,1);
16696 } else
16697 ast_queue_hangup(p->owner);
16698 }
16699 } else {
16700 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(p->recv.sin_addr));
16701 if (p->owner)
16702 ast_queue_hangup(p->owner);
16703 }
16704 } else if (p->owner) {
16705 ast_queue_hangup(p->owner);
16706 if (option_debug > 2)
16707 ast_log(LOG_DEBUG, "Received bye, issuing owner hangup\n");
16708 } else {
16709 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16710 if (option_debug > 2)
16711 ast_log(LOG_DEBUG, "Received bye, no owner, selfdestruct soon.\n");
16712 }
16713 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
16714 transmit_response(p, "200 OK", req);
16715
16716 return 1;
16717 }
16718
16719
16720 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
16721 {
16722 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
16723 if (ast_test_flag(req, SIP_PKT_DEBUG))
16724 ast_verbose("Receiving message!\n");
16725 receive_message(p, req);
16726 } else
16727 transmit_response(p, "202 Accepted", req);
16728 return 1;
16729 }
16730
16731
16732 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
16733 {
16734 int gotdest = 0;
16735 int res = 0;
16736 int firststate = AST_EXTENSION_REMOVED;
16737 struct sip_peer *authpeer = NULL;
16738 const char *eventheader = get_header(req, "Event");
16739 int resubscribe = (p->subscribed != NONE) && !ast_test_flag(req, SIP_PKT_IGNORE);
16740 char *temp, *event;
16741
16742 if (p->initreq.headers) {
16743
16744 if (p->initreq.method != SIP_SUBSCRIBE) {
16745
16746
16747 transmit_response(p, "403 Forbidden (within dialog)", req);
16748
16749 if (option_debug)
16750 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);
16751 return 0;
16752 } else if (ast_test_flag(req, SIP_PKT_DEBUG)) {
16753 if (option_debug) {
16754 if (resubscribe)
16755 ast_log(LOG_DEBUG, "Got a re-subscribe on existing subscription %s\n", p->callid);
16756 else
16757 ast_log(LOG_DEBUG, "Got a new subscription %s (possibly with auth) or retransmission\n", p->callid);
16758 }
16759 }
16760 }
16761
16762
16763
16764
16765 if (!global_allowsubscribe) {
16766 transmit_response(p, "403 Forbidden (policy)", req);
16767 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16768 return 0;
16769 }
16770
16771 if (!ast_test_flag(req, SIP_PKT_IGNORE) && !resubscribe) {
16772 const char *to = get_header(req, "To");
16773 char totag[128];
16774
16775
16776 if (!ast_strlen_zero(to) && gettag(req, "To", totag, sizeof(totag))) {
16777 if (ast_test_flag(req, SIP_PKT_DEBUG))
16778 ast_verbose("Received resubscription for a dialog we no longer know about. Telling remote side to subscribe again.\n");
16779 transmit_response(p, "481 Subscription does not exist", req);
16780 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16781 return 0;
16782 }
16783
16784
16785 if (ast_test_flag(req, SIP_PKT_DEBUG))
16786 ast_verbose("Creating new subscription\n");
16787
16788 copy_request(&p->initreq, req);
16789 check_via(p, req);
16790 build_route(p, req, 0);
16791 } else if (ast_test_flag(req, SIP_PKT_DEBUG) && ast_test_flag(req, SIP_PKT_IGNORE))
16792 ast_verbose("Ignoring this SUBSCRIBE request\n");
16793
16794
16795 if (ast_strlen_zero(eventheader)) {
16796 transmit_response(p, "489 Bad Event", req);
16797 if (option_debug > 1)
16798 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: <none>\n");
16799 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16800 return 0;
16801 }
16802
16803 if ( (strchr(eventheader, ';'))) {
16804 event = ast_strdupa(eventheader);
16805 temp = strchr(event, ';');
16806 *temp = '\0';
16807
16808 } else
16809 event = (char *) eventheader;
16810
16811
16812
16813
16814 if (p->subscribed == NONE || resubscribe) {
16815 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, &authpeer);
16816
16817
16818 if (res == AUTH_CHALLENGE_SENT) {
16819 if (authpeer)
16820 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16821 return 0;
16822 }
16823 if (res < 0) {
16824 if (res == AUTH_FAKE_AUTH) {
16825 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
16826 transmit_fake_auth_response(p, SIP_SUBSCRIBE, req, XMIT_UNRELIABLE);
16827 } else {
16828 ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
16829 transmit_response_reliable(p, "403 Forbidden", req);
16830 }
16831 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16832 if (authpeer)
16833 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16834 return 0;
16835 }
16836 }
16837
16838
16839 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
16840 transmit_response(p, "403 Forbidden (policy)", req);
16841 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16842 if (authpeer)
16843 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16844 return 0;
16845 }
16846
16847 if (strcmp(event, "message-summary")) {
16848
16849 gotdest = get_destination(p, NULL);
16850 }
16851
16852
16853 parse_ok_contact(p, req);
16854
16855 build_contact(p);
16856 if (gotdest) {
16857 transmit_response(p, "404 Not Found", req);
16858 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16859 if (authpeer)
16860 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16861 return 0;
16862 }
16863
16864
16865 if (ast_strlen_zero(p->tag))
16866 make_our_tag(p->tag, sizeof(p->tag));
16867
16868 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
16869 unsigned int pidf_xml;
16870 const char *accept;
16871 int start = 0;
16872 enum subscriptiontype subscribed = NONE;
16873 const char *unknown_acceptheader = NULL;
16874
16875 if (authpeer)
16876 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16877
16878
16879 accept = __get_header(req, "Accept", &start);
16880 while ((subscribed == NONE) && !ast_strlen_zero(accept)) {
16881 pidf_xml = strstr(accept, "application/pidf+xml") ? 1 : 0;
16882
16883
16884
16885 if (pidf_xml && strstr(p->useragent, "Polycom")) {
16886 subscribed = XPIDF_XML;
16887 } else if (pidf_xml) {
16888 subscribed = PIDF_XML;
16889 } else if (strstr(accept, "application/dialog-info+xml")) {
16890 subscribed = DIALOG_INFO_XML;
16891
16892 } else if (strstr(accept, "application/cpim-pidf+xml")) {
16893 subscribed = CPIM_PIDF_XML;
16894 } else if (strstr(accept, "application/xpidf+xml")) {
16895 subscribed = XPIDF_XML;
16896 } else {
16897 unknown_acceptheader = accept;
16898 }
16899
16900 accept = __get_header(req, "Accept", &start);
16901 }
16902
16903 if (!start) {
16904 if (p->subscribed == NONE) {
16905 transmit_response(p, "489 Bad Event", req);
16906 ast_log(LOG_WARNING,"SUBSCRIBE failure: no Accept header: pvt: "
16907 "stateid: %d, laststate: %d, dialogver: %d, subscribecont: "
16908 "'%s', subscribeuri: '%s'\n",
16909 p->stateid,
16910 p->laststate,
16911 p->dialogver,
16912 p->subscribecontext,
16913 p->subscribeuri);
16914 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16915 return 0;
16916 }
16917
16918
16919 } else if (subscribed == NONE) {
16920
16921 char mybuf[200];
16922 if (!ast_strlen_zero(unknown_acceptheader)) {
16923 snprintf(mybuf, sizeof(mybuf), "489 Bad Event (format %s)", unknown_acceptheader);
16924 } else {
16925 snprintf(mybuf, sizeof(mybuf), "489 Bad Event");
16926 }
16927 transmit_response(p, mybuf, req);
16928 ast_log(LOG_WARNING,"SUBSCRIBE failure: unrecognized format:"
16929 "'%s' pvt: subscribed: %d, stateid: %d, laststate: %d,"
16930 "dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
16931 unknown_acceptheader,
16932 (int)p->subscribed,
16933 p->stateid,
16934 p->laststate,
16935 p->dialogver,
16936 p->subscribecontext,
16937 p->subscribeuri);
16938 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16939 return 0;
16940 } else {
16941 p->subscribed = subscribed;
16942 }
16943 } else if (!strcmp(event, "message-summary")) {
16944 int start = 0;
16945 int found_supported = 0;
16946 const char *acceptheader;
16947
16948 acceptheader = __get_header(req, "Accept", &start);
16949 while (!found_supported && !ast_strlen_zero(acceptheader)) {
16950 found_supported = strcmp(acceptheader, "application/simple-message-summary") ? 0 : 1;
16951 if (!found_supported && (option_debug > 2)) {
16952 ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
16953 }
16954 acceptheader = __get_header(req, "Accept", &start);
16955 }
16956 if (start && !found_supported) {
16957
16958 transmit_response(p, "406 Not Acceptable", req);
16959 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16960 if (authpeer)
16961 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16962 return 0;
16963 }
16964
16965
16966
16967
16968
16969 if (!authpeer || ast_strlen_zero(authpeer->mailbox)) {
16970 transmit_response(p, "404 Not found (no mailbox)", req);
16971 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16972 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
16973 if (authpeer)
16974 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16975 return 0;
16976 }
16977
16978 p->subscribed = MWI_NOTIFICATION;
16979 if (authpeer->mwipvt && authpeer->mwipvt != p)
16980
16981 sip_destroy(authpeer->mwipvt);
16982 authpeer->mwipvt = p;
16983 p->relatedpeer = ASTOBJ_REF(authpeer);
16984 } else {
16985 transmit_response(p, "489 Bad Event", req);
16986 if (option_debug > 1)
16987 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
16988 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
16989 if (authpeer)
16990 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
16991 return 0;
16992 }
16993
16994 if (p->subscribed != MWI_NOTIFICATION && !resubscribe) {
16995 if (p->stateid > -1)
16996 ast_extension_state_del(p->stateid, add_extensionstate_update);
16997 p->stateid = ast_extension_state_add(p->context, p->exten, add_extensionstate_update, p);
16998 }
16999
17000 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
17001 p->lastinvite = seqno;
17002 if (p && !ast_test_flag(&p->flags[0], SIP_NEEDDESTROY)) {
17003 p->expiry = atoi(get_header(req, "Expires"));
17004
17005
17006 if (p->expiry > max_expiry)
17007 p->expiry = max_expiry;
17008 if (p->expiry < min_expiry && p->expiry > 0)
17009 p->expiry = min_expiry;
17010
17011 if (sipdebug || option_debug > 1) {
17012 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
17013 ast_log(LOG_DEBUG, "Adding subscription for mailbox notification - peer %s Mailbox %s\n", p->relatedpeer->name, p->relatedpeer->mailbox);
17014 else
17015 ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
17016 }
17017 if (p->autokillid > -1 && sip_cancel_destroy(p))
17018 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
17019 if (p->expiry > 0)
17020 sip_scheddestroy(p, (p->expiry + 10) * 1000);
17021
17022 if (p->subscribed == MWI_NOTIFICATION) {
17023 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
17024 transmit_response(p, "200 OK", req);
17025 if (p->relatedpeer) {
17026 ASTOBJ_WRLOCK(p->relatedpeer);
17027 sip_send_mwi_to_peer(p->relatedpeer, TRUE);
17028 ASTOBJ_UNLOCK(p->relatedpeer);
17029 }
17030 } else {
17031
17032 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
17033
17034 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));
17035 transmit_response(p, "404 Not found", req);
17036 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
17037 return 0;
17038 }
17039 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
17040 transmit_response(p, "200 OK", req);
17041 transmit_state_notify(p, firststate, 1, FALSE);
17042 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
17043
17044 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
17045
17046 }
17047 if (!p->expiry)
17048 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
17049 }
17050 return 1;
17051 }
17052
17053
17054 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e)
17055 {
17056 enum check_auth_result res;
17057
17058
17059
17060 if (p->initreq.headers && p->initreq.method != SIP_REGISTER) {
17061 ast_log(LOG_WARNING, "Ignoring spurious REGISTER with Call-ID: %s\n", p->callid);
17062 return -1;
17063 }
17064
17065
17066 if (ast_test_flag(req, SIP_PKT_DEBUG))
17067 ast_verbose("Using latest REGISTER request as basis request\n");
17068 copy_request(&p->initreq, req);
17069 check_via(p, req);
17070 if ((res = register_verify(p, sin, req, e)) < 0) {
17071 const char *reason;
17072
17073 switch (res) {
17074 case AUTH_SECRET_FAILED:
17075 reason = "Wrong password";
17076 break;
17077 case AUTH_USERNAME_MISMATCH:
17078 reason = "Username/auth name mismatch";
17079 break;
17080 case AUTH_NOT_FOUND:
17081 reason = "No matching peer found";
17082 break;
17083 case AUTH_UNKNOWN_DOMAIN:
17084 reason = "Not a local domain";
17085 break;
17086 case AUTH_PEER_NOT_DYNAMIC:
17087 reason = "Peer is not supposed to register";
17088 break;
17089 case AUTH_ACL_FAILED:
17090 reason = "Device does not match ACL";
17091 break;
17092 default:
17093 reason = "Unknown failure";
17094 break;
17095 }
17096 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
17097 get_header(req, "To"), ast_inet_ntoa(sin->sin_addr),
17098 reason);
17099 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
17100 } else
17101 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
17102
17103 if (res < 1) {
17104
17105
17106 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17107 }
17108 return res;
17109 }
17110
17111
17112
17113
17114
17115
17116
17117
17118 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
17119 {
17120
17121
17122 const char *cmd;
17123 const char *cseq;
17124 const char *useragent;
17125 const char *via;
17126 const char *callid;
17127 int via_pos = 0;
17128 int seqno;
17129 int len;
17130 int ignore = FALSE;
17131 int respid;
17132 int res = 0;
17133 int debug = sip_debug_test_pvt(p);
17134 char *e;
17135 int error = 0;
17136 int oldmethod = p->method;
17137 int acked = 0;
17138
17139
17140
17141
17142 cseq = get_header(req, "Cseq");
17143 cmd = req->header[0];
17144
17145 via = __get_header(req, "Via", &via_pos);
17146
17147 callid = get_header(req, "Call-ID");
17148
17149
17150 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq) || ast_strlen_zero(via)) {
17151 ast_log(LOG_ERROR, "Dropping this SIP message with Call-ID '%s', it's incomplete.\n", callid);
17152 error = 1;
17153 }
17154 if (!error && sscanf(cseq, "%30d%n", &seqno, &len) != 1) {
17155 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
17156 error = 1;
17157 }
17158 if (error) {
17159 if (!p->initreq.headers)
17160 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
17161 return -1;
17162 }
17163
17164
17165 cmd = req->rlPart1;
17166 e = ast_skip_blanks(req->rlPart2);
17167
17168
17169 useragent = get_header(req, "User-Agent");
17170 if (!ast_strlen_zero(useragent))
17171 ast_string_field_set(p, useragent, useragent);
17172
17173
17174 if (req->method == SIP_RESPONSE) {
17175
17176 if (ast_strlen_zero(e)) {
17177 return 0;
17178 }
17179 if (sscanf(e, "%30d %n", &respid, &len) != 1) {
17180 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
17181 return 0;
17182 }
17183 if (respid <= 0) {
17184 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
17185 return 0;
17186 }
17187
17188
17189
17190 if (!ast_strlen_zero(__get_header(req, "via", &via_pos))) {
17191 ast_log(LOG_WARNING, "Misrouted SIP response '%s' with Call-ID '%s', too many vias\n", e, callid);
17192 return 0;
17193 }
17194 if (!p->initreq.headers) {
17195 if (option_debug)
17196 ast_log(LOG_DEBUG, "That's odd... Got a response on a call we don't know about. Cseq %d Cmd %s\n", seqno, cmd);
17197 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
17198 return 0;
17199 }
17200 if (p->ocseq && (p->ocseq < seqno)) {
17201 if (option_debug)
17202 ast_log(LOG_DEBUG, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
17203 return -1;
17204 } else {
17205 if ((respid == 200) || ((respid >= 300) && (respid <= 399))) {
17206 extract_uri(p, req);
17207 }
17208 handle_response(p, respid, e + len, req, seqno);
17209 }
17210 return 0;
17211 }
17212
17213
17214
17215
17216
17217 p->method = req->method;
17218 if (option_debug > 3)
17219 ast_log(LOG_DEBUG, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
17220
17221 if (p->icseq && (p->icseq > seqno) ) {
17222 if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
17223 if (option_debug > 2)
17224 ast_log(LOG_DEBUG, "Got CANCEL or ACK on INVITE with transactions in between.\n");
17225 } else {
17226 if (option_debug)
17227 ast_log(LOG_DEBUG, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
17228 if (req->method != SIP_ACK)
17229 transmit_response(p, "500 Server error", req);
17230 return -1;
17231 }
17232 } else if (p->icseq &&
17233 p->icseq == seqno &&
17234 req->method != SIP_ACK &&
17235 (p->method != SIP_CANCEL || ast_test_flag(&p->flags[0], SIP_ALREADYGONE))) {
17236
17237
17238
17239 ignore = 2;
17240 ast_set_flag(req, SIP_PKT_IGNORE);
17241 ast_set_flag(req, SIP_PKT_IGNORE_REQ);
17242 if (option_debug > 2)
17243 ast_log(LOG_DEBUG, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
17244 }
17245
17246 if (seqno >= p->icseq)
17247
17248
17249
17250 p->icseq = seqno;
17251
17252
17253 if (ast_strlen_zero(p->theirtag)) {
17254 char tag[128];
17255
17256 gettag(req, "From", tag, sizeof(tag));
17257 ast_string_field_set(p, theirtag, tag);
17258 }
17259 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
17260
17261 if (pedanticsipchecking) {
17262
17263
17264
17265
17266 if (!p->initreq.headers && ast_test_flag(req, SIP_PKT_WITH_TOTAG)) {
17267
17268 if (!ast_test_flag(req, SIP_PKT_IGNORE) && req->method == SIP_INVITE) {
17269 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
17270
17271 } else if (req->method != SIP_ACK) {
17272 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
17273 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17274 }
17275 return res;
17276 }
17277 }
17278
17279 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY)) {
17280 transmit_response(p, "400 Bad request", req);
17281 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17282 return -1;
17283 }
17284
17285
17286 switch (p->method) {
17287 case SIP_OPTIONS:
17288 res = handle_request_options(p, req);
17289 break;
17290 case SIP_INVITE:
17291 res = handle_request_invite(p, req, debug, seqno, sin, recount, e, nounlock);
17292 break;
17293 case SIP_REFER:
17294 res = handle_request_refer(p, req, debug, ignore, seqno, nounlock);
17295 break;
17296 case SIP_CANCEL:
17297 res = handle_request_cancel(p, req);
17298 break;
17299 case SIP_BYE:
17300 res = handle_request_bye(p, req);
17301 break;
17302 case SIP_MESSAGE:
17303 res = handle_request_message(p, req);
17304 break;
17305 case SIP_SUBSCRIBE:
17306 res = handle_request_subscribe(p, req, sin, seqno, e);
17307 break;
17308 case SIP_REGISTER:
17309 res = handle_request_register(p, req, sin, e);
17310 break;
17311 case SIP_INFO:
17312 if (ast_test_flag(req, SIP_PKT_DEBUG))
17313 ast_verbose("Receiving INFO!\n");
17314 if (!ignore)
17315 handle_request_info(p, req);
17316 else
17317 transmit_response(p, "200 OK", req);
17318 break;
17319 case SIP_NOTIFY:
17320 res = handle_request_notify(p, req, sin, seqno, e);
17321 break;
17322 case SIP_ACK:
17323
17324 if (seqno == p->pendinginvite) {
17325 p->invitestate = INV_TERMINATED;
17326 p->pendinginvite = 0;
17327 acked = __sip_ack(p, seqno, FLAG_RESPONSE, 0);
17328 if (find_sdp(req)) {
17329 if (process_sdp(p, req))
17330 return -1;
17331 }
17332 check_pendings(p);
17333 } else if (p->glareinvite == seqno) {
17334
17335 p->glareinvite = 0;
17336 acked = __sip_ack(p, seqno, 1, 0);
17337 }
17338 if (!acked) {
17339
17340
17341 p->method = oldmethod;
17342 }
17343
17344 if (!p->lastinvite && ast_strlen_zero(p->randdata))
17345 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
17346 break;
17347 default:
17348 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
17349 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
17350 cmd, ast_inet_ntoa(p->sa.sin_addr));
17351
17352 if (!p->initreq.headers)
17353 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
17354 break;
17355 }
17356 return res;
17357 }
17358
17359 static void process_request_queue(struct sip_pvt *p, int *recount, int *nounlock)
17360 {
17361 struct sip_request *req;
17362
17363 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
17364 if (handle_request(p, req, &p->recv, recount, nounlock) == -1) {
17365
17366 if (option_debug) {
17367 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
17368 }
17369 }
17370 ast_free(req);
17371 }
17372 }
17373
17374 static int scheduler_process_request_queue(const void *data)
17375 {
17376 struct sip_pvt *p = (struct sip_pvt *) data;
17377 int recount = 0;
17378 int nounlock = 0;
17379 int lockretry;
17380
17381 for (lockretry = 10; lockretry > 0; lockretry--) {
17382 ast_mutex_lock(&p->lock);
17383
17384
17385
17386 if (!p->owner || !ast_channel_trylock(p->owner)) {
17387 break;
17388 }
17389
17390 if (lockretry != 1) {
17391 ast_mutex_unlock(&p->lock);
17392
17393 usleep(1);
17394 }
17395 }
17396
17397 if (!lockretry) {
17398 int retry = !AST_LIST_EMPTY(&p->request_queue);
17399
17400
17401
17402
17403
17404
17405 ast_mutex_unlock(&p->lock);
17406 return retry;
17407 };
17408
17409 process_request_queue(p, &recount, &nounlock);
17410 p->request_queue_sched_id = -1;
17411
17412 if (p->owner && !nounlock) {
17413 ast_channel_unlock(p->owner);
17414 }
17415 ast_mutex_unlock(&p->lock);
17416
17417 if (recount) {
17418 ast_update_use_count();
17419 }
17420
17421 return 0;
17422 }
17423
17424 static int queue_request(struct sip_pvt *p, const struct sip_request *req)
17425 {
17426 struct sip_request *newreq;
17427
17428 if (!(newreq = ast_calloc(1, sizeof(*newreq)))) {
17429 return -1;
17430 }
17431
17432 copy_request(newreq, req);
17433 AST_LIST_INSERT_TAIL(&p->request_queue, newreq, next);
17434 if (p->request_queue_sched_id == -1) {
17435 p->request_queue_sched_id = ast_sched_add(sched, 10, scheduler_process_request_queue, p);
17436 }
17437
17438 return 0;
17439 }
17440
17441
17442
17443
17444
17445
17446 static int sipsock_read(int *id, int fd, short events, void *ignore)
17447 {
17448 struct sip_request req;
17449 struct sockaddr_in sin = { 0, };
17450 struct sip_pvt *p;
17451 int res;
17452 socklen_t len = sizeof(sin);
17453 int nounlock = 0;
17454 int recount = 0;
17455 int lockretry;
17456
17457 memset(&req, 0, sizeof(req));
17458 res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
17459 if (res < 0) {
17460 #if !defined(__FreeBSD__)
17461 if (errno == EAGAIN)
17462 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
17463 else
17464 #endif
17465 if (errno != ECONNREFUSED)
17466 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
17467 return 1;
17468 }
17469 if (option_debug && res == sizeof(req.data) - 1)
17470 ast_log(LOG_DEBUG, "Received packet exceeds buffer. Data is possibly lost\n");
17471
17472 req.data[res] = '\0';
17473 req.len = res;
17474 if(sip_debug_test_addr(&sin))
17475 ast_set_flag(&req, SIP_PKT_DEBUG);
17476 if (pedanticsipchecking)
17477 req.len = lws2sws(req.data, req.len);
17478 if (ast_test_flag(&req, SIP_PKT_DEBUG))
17479 ast_verbose("\n<--- SIP read from %s:%d --->\n%s\n<------------->\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), req.data);
17480
17481 if(parse_request(&req) == -1)
17482 return 1;
17483
17484 req.method = find_sip_method(req.rlPart1);
17485
17486 if (ast_test_flag(&req, SIP_PKT_DEBUG))
17487 ast_verbose("--- (%d headers %d lines)%s ---\n", req.headers, req.lines, (req.headers + req.lines == 0) ? " Nat keepalive" : "");
17488
17489 if (req.headers < 2)
17490 return 1;
17491
17492
17493 for (lockretry = 10; lockretry > 0; lockretry--) {
17494 ast_mutex_lock(&netlock);
17495
17496
17497 p = find_call(&req, &sin, req.method);
17498 if (p == NULL) {
17499 if (option_debug)
17500 ast_log(LOG_DEBUG, "Invalid SIP message - rejected , no callid, len %d\n", req.len);
17501 ast_mutex_unlock(&netlock);
17502 return 1;
17503 }
17504
17505
17506 if (!p->owner || !ast_channel_trylock(p->owner))
17507 break;
17508 if (lockretry != 1) {
17509 ast_mutex_unlock(&p->lock);
17510 ast_mutex_unlock(&netlock);
17511
17512 usleep(1);
17513 }
17514 }
17515 p->recv = sin;
17516
17517 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
17518 append_history(p, "Rx", "%s / %s / %s", req.data, get_header(&req, "CSeq"), req.rlPart2);
17519
17520 if (!lockretry) {
17521 if (!queue_request(p, &req)) {
17522
17523 ast_mutex_unlock(&p->lock);
17524 ast_mutex_unlock(&netlock);
17525 return 1;
17526 }
17527
17528
17529 if (p->owner)
17530 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 ??? - "));
17531 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
17532 if (req.method != SIP_ACK)
17533 transmit_response(p, "503 Server error", &req);
17534
17535 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
17536 ast_mutex_unlock(&p->lock);
17537 ast_mutex_unlock(&netlock);
17538 return 1;
17539 }
17540
17541
17542
17543
17544 if (!AST_LIST_EMPTY(&p->request_queue)) {
17545 AST_SCHED_DEL(sched, p->request_queue_sched_id);
17546 process_request_queue(p, &recount, &nounlock);
17547 }
17548
17549 if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
17550
17551 if (option_debug)
17552 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
17553 }
17554
17555 if (p->owner && !nounlock)
17556 ast_channel_unlock(p->owner);
17557 ast_mutex_unlock(&p->lock);
17558 ast_mutex_unlock(&netlock);
17559 if (recount)
17560 ast_update_use_count();
17561
17562 return 1;
17563 }
17564
17565
17566 static int sip_send_mwi_to_peer(struct sip_peer *peer, int force)
17567 {
17568
17569 struct sip_pvt *p;
17570 int newmsgs, oldmsgs;
17571
17572
17573 if (!peer->addr.sin_addr.s_addr && !peer->defaddr.sin_addr.s_addr)
17574 return 0;
17575
17576
17577 ast_app_inboxcount(peer->mailbox, &newmsgs, &oldmsgs);
17578
17579 peer->lastmsgcheck = time(NULL);
17580
17581
17582 if (!force && ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)) == peer->lastmsgssent) {
17583 return 0;
17584 }
17585
17586
17587 peer->lastmsgssent = ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs));
17588
17589 if (peer->mwipvt) {
17590
17591 p = peer->mwipvt;
17592 } else {
17593
17594 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY)))
17595 return -1;
17596 if (create_addr_from_peer(p, peer)) {
17597
17598 sip_destroy(p);
17599 return 0;
17600 }
17601
17602 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
17603 p->ourip = __ourip;
17604 build_via(p);
17605 build_callid_pvt(p);
17606
17607 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17608 }
17609
17610 ast_set_flag(&p->flags[0], SIP_OUTGOING);
17611 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
17612 return 0;
17613 }
17614
17615
17616 static int does_peer_need_mwi(struct sip_peer *peer)
17617 {
17618 time_t t = time(NULL);
17619
17620 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
17621 !peer->mwipvt) {
17622 peer->lastmsgcheck = t;
17623 return FALSE;
17624 }
17625
17626 if (!ast_strlen_zero(peer->mailbox) && (t - peer->lastmsgcheck) > global_mwitime)
17627 return TRUE;
17628
17629 return FALSE;
17630 }
17631
17632
17633
17634
17635
17636
17637 static void *do_monitor(void *data)
17638 {
17639 int res;
17640 struct sip_pvt *sip;
17641 struct sip_peer *peer = NULL;
17642 time_t t;
17643 int fastrestart = FALSE;
17644 int lastpeernum = -1;
17645 int curpeernum;
17646 int reloading;
17647
17648
17649 if (sipsock > -1)
17650 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
17651
17652
17653 for(;;) {
17654
17655 ast_mutex_lock(&sip_reload_lock);
17656 reloading = sip_reloading;
17657 sip_reloading = FALSE;
17658 ast_mutex_unlock(&sip_reload_lock);
17659 if (reloading) {
17660 if (option_verbose > 0)
17661 ast_verbose(VERBOSE_PREFIX_1 "Reloading SIP\n");
17662 sip_do_reload(sip_reloadreason);
17663
17664
17665 if (sipsock > -1) {
17666 if (sipsock_read_id)
17667 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
17668 else
17669 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
17670 } else if (sipsock_read_id) {
17671 ast_io_remove(io, sipsock_read_id);
17672 sipsock_read_id = NULL;
17673 }
17674 }
17675
17676
17677
17678 if (!fastrestart) {
17679 markall_extenstate_updates();
17680 }
17681
17682 restartsearch:
17683
17684 ast_mutex_lock(&iflock);
17685 t = time(NULL);
17686
17687
17688
17689
17690 for (sip = iflist; !fastrestart && sip; sip = sip->next) {
17691
17692
17693
17694
17695
17696 if (ast_mutex_trylock(&sip->lock)) {
17697
17698
17699 unmark_extenstate_update(sip);
17700 continue;
17701 }
17702
17703
17704
17705 check_extenstate_updates(sip);
17706
17707
17708 if (sip->rtp && sip->owner &&
17709 (sip->owner->_state == AST_STATE_UP) &&
17710 !sip->redirip.sin_addr.s_addr &&
17711 sip->t38.state != T38_ENABLED) {
17712 if (sip->lastrtptx &&
17713 ast_rtp_get_rtpkeepalive(sip->rtp) &&
17714 (t > sip->lastrtptx + ast_rtp_get_rtpkeepalive(sip->rtp))) {
17715
17716 sip->lastrtptx = time(NULL);
17717 ast_rtp_sendcng(sip->rtp, 0);
17718 }
17719 if (sip->lastrtprx &&
17720 (ast_rtp_get_rtptimeout(sip->rtp) || ast_rtp_get_rtpholdtimeout(sip->rtp)) &&
17721 (t > sip->lastrtprx + ast_rtp_get_rtptimeout(sip->rtp))) {
17722
17723 struct sockaddr_in sin = { 0, };
17724 ast_rtp_get_peer(sip->rtp, &sin);
17725 if (!ast_test_flag(&sip->flags[1], SIP_PAGE2_CALL_ONHOLD) ||
17726 (ast_rtp_get_rtpholdtimeout(sip->rtp) &&
17727 (t > sip->lastrtprx + ast_rtp_get_rtpholdtimeout(sip->rtp)))) {
17728
17729 if (ast_rtp_get_rtptimeout(sip->rtp)) {
17730 while (sip->owner && ast_channel_trylock(sip->owner)) {
17731 DEADLOCK_AVOIDANCE(&sip->lock);
17732 }
17733 if (sip->owner) {
17734 ast_log(LOG_NOTICE,
17735 "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
17736 sip->owner->name,
17737 (long) (t - sip->lastrtprx));
17738
17739 ast_softhangup_nolock(sip->owner, AST_SOFTHANGUP_DEV);
17740 ast_channel_unlock(sip->owner);
17741
17742
17743
17744
17745 ast_rtp_set_rtptimeout(sip->rtp, 0);
17746 ast_rtp_set_rtpholdtimeout(sip->rtp, 0);
17747 if (sip->vrtp) {
17748 ast_rtp_set_rtptimeout(sip->vrtp, 0);
17749 ast_rtp_set_rtpholdtimeout(sip->vrtp, 0);
17750 }
17751 }
17752 }
17753 }
17754 }
17755 }
17756
17757 if (ast_test_flag(&sip->flags[0], SIP_NEEDDESTROY) && !sip->packets &&
17758 !sip->owner) {
17759 ast_mutex_unlock(&sip->lock);
17760 __sip_destroy(sip, 1);
17761 ast_mutex_unlock(&iflock);
17762 usleep(1);
17763 goto restartsearch;
17764 }
17765 ast_mutex_unlock(&sip->lock);
17766 }
17767 ast_mutex_unlock(&iflock);
17768
17769
17770 if (!fastrestart) {
17771 clearmarked_extenstate_updates();
17772 }
17773
17774
17775
17776
17777
17778
17779
17780 pthread_testcancel();
17781
17782 res = ast_sched_wait(sched);
17783 if ((res < 0) || (res > 1000))
17784 res = 1000;
17785
17786 if (fastrestart)
17787 res = 1;
17788 res = ast_io_wait(io, res);
17789 if (option_debug && res > 20)
17790 ast_log(LOG_DEBUG, "chan_sip: ast_io_wait ran %d all at once\n", res);
17791 ast_mutex_lock(&monlock);
17792 res = ast_sched_runq(sched);
17793 if (option_debug && res >= 20)
17794 ast_log(LOG_DEBUG, "chan_sip: ast_sched_runq ran %d all at once\n", res);
17795
17796
17797 t = time(NULL);
17798 fastrestart = FALSE;
17799 curpeernum = 0;
17800 peer = NULL;
17801
17802 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !peer, do {
17803 if ((curpeernum > lastpeernum) && does_peer_need_mwi(iterator)) {
17804 fastrestart = TRUE;
17805 lastpeernum = curpeernum;
17806 peer = ASTOBJ_REF(iterator);
17807 };
17808 curpeernum++;
17809 } while (0)
17810 );
17811
17812 if (peer) {
17813 ASTOBJ_WRLOCK(peer);
17814 sip_send_mwi_to_peer(peer, FALSE);
17815 ASTOBJ_UNLOCK(peer);
17816 ASTOBJ_UNREF(peer,sip_destroy_peer);
17817 } else {
17818
17819 lastpeernum = -1;
17820 }
17821 ast_mutex_unlock(&monlock);
17822 }
17823
17824 return NULL;
17825
17826 }
17827
17828
17829 static int restart_monitor(void)
17830 {
17831
17832 if (monitor_thread == AST_PTHREADT_STOP)
17833 return 0;
17834 ast_mutex_lock(&monlock);
17835 if (monitor_thread == pthread_self()) {
17836 ast_mutex_unlock(&monlock);
17837 ast_log(LOG_WARNING, "Cannot kill myself\n");
17838 return -1;
17839 }
17840 if (monitor_thread != AST_PTHREADT_NULL) {
17841
17842 pthread_kill(monitor_thread, SIGURG);
17843 } else {
17844
17845 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
17846 ast_mutex_unlock(&monlock);
17847 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
17848 return -1;
17849 }
17850 }
17851 ast_mutex_unlock(&monlock);
17852 return 0;
17853 }
17854
17855
17856 static int sip_poke_noanswer(const void *data)
17857 {
17858 struct sip_peer *peer = (struct sip_peer *)data;
17859
17860 peer->pokeexpire = -1;
17861 if (peer->lastms > -1) {
17862 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
17863 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE)) {
17864 ast_update_realtime("sippeers", "name", peer->name, "lastms", "-1", NULL);
17865 }
17866 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
17867 }
17868 if (peer->call)
17869 sip_destroy(peer->call);
17870 peer->call = NULL;
17871
17872
17873 if (peer->lastms > -1) {
17874 peer->lastms = -1;
17875 ast_device_state_changed("SIP/%s", peer->name);
17876 }
17877
17878
17879 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17880 struct sip_peer *peer_ptr = peer;
17881 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17882 }
17883
17884
17885
17886 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
17887 if (peer->pokeexpire == -1) {
17888 ASTOBJ_UNREF(peer, sip_destroy_peer);
17889 }
17890
17891 return 0;
17892 }
17893
17894
17895
17896
17897 static int sip_poke_peer(struct sip_peer *peer)
17898 {
17899 struct sip_pvt *p;
17900 int xmitres = 0;
17901
17902 if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
17903
17904
17905 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17906 struct sip_peer *peer_ptr = peer;
17907 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17908 }
17909 peer->lastms = 0;
17910 peer->call = NULL;
17911 return 0;
17912 }
17913 if (peer->call) {
17914 if (sipdebug)
17915 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
17916 sip_destroy(peer->call);
17917 }
17918 if (!(p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS)))
17919 return -1;
17920
17921 p->sa = peer->addr;
17922 p->recv = peer->addr;
17923 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
17924 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
17925
17926
17927 if (!ast_strlen_zero(peer->fullcontact))
17928 ast_string_field_set(p, fullcontact, peer->fullcontact);
17929
17930 if (!ast_strlen_zero(peer->tohost))
17931 ast_string_field_set(p, tohost, peer->tohost);
17932 else
17933 ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr));
17934
17935
17936 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
17937 p->ourip = __ourip;
17938 build_via(p);
17939 build_callid_pvt(p);
17940
17941 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17942 struct sip_peer *peer_ptr = peer;
17943 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17944 }
17945
17946 p->relatedpeer = ASTOBJ_REF(peer);
17947 ast_set_flag(&p->flags[0], SIP_OUTGOING);
17948 #ifdef VOCAL_DATA_HACK
17949 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
17950 xmitres = transmit_invite(p, SIP_INVITE, 0, 2);
17951 #else
17952 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2);
17953 #endif
17954 gettimeofday(&peer->ps, NULL);
17955 if (xmitres == XMIT_ERROR) {
17956 sip_poke_noanswer(ASTOBJ_REF(peer));
17957 } else {
17958 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
17959 struct sip_peer *peer_ptr = peer;
17960 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17961 }
17962 peer->pokeexpire = ast_sched_add(sched, peer->maxms * 2, sip_poke_noanswer, ASTOBJ_REF(peer));
17963 if (peer->pokeexpire == -1) {
17964 struct sip_peer *peer_ptr = peer;
17965 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17966 }
17967 }
17968
17969 return 0;
17970 }
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005 static int sip_devicestate(void *data)
18006 {
18007 char *host;
18008 char *tmp;
18009
18010 struct hostent *hp;
18011 struct ast_hostent ahp;
18012 struct sip_peer *p;
18013
18014 int res = AST_DEVICE_INVALID;
18015
18016
18017 host = ast_strdupa(data ? data : "");
18018 if ((tmp = strchr(host, '@')))
18019 host = tmp + 1;
18020
18021 if (option_debug > 2)
18022 ast_log(LOG_DEBUG, "Checking device state for peer %s\n", host);
18023
18024
18025
18026
18027
18028
18029
18030
18031 if ((p = find_peer(host, NULL, 0, 1))) {
18032 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044 if (p->onHold)
18045
18046 res = AST_DEVICE_ONHOLD;
18047 else if (p->inRinging) {
18048 if (p->inRinging == p->inUse)
18049 res = AST_DEVICE_RINGING;
18050 else
18051 res = AST_DEVICE_RINGINUSE;
18052 } else if (p->call_limit && (p->inUse == p->call_limit))
18053
18054 res = AST_DEVICE_BUSY;
18055 else if (p->call_limit && p->inUse)
18056
18057 res = AST_DEVICE_INUSE;
18058 else if (p->maxms && ((p->lastms > p->maxms) || (p->lastms < 0)))
18059
18060 res = AST_DEVICE_UNAVAILABLE;
18061 else
18062 res = AST_DEVICE_NOT_INUSE;
18063 } else {
18064
18065 res = AST_DEVICE_UNAVAILABLE;
18066 }
18067 ASTOBJ_UNREF(p,sip_destroy_peer);
18068 } else {
18069 char *port = strchr(host, ':');
18070 if (port)
18071 *port = '\0';
18072 hp = ast_gethostbyname(host, &ahp);
18073 if (hp)
18074 res = AST_DEVICE_UNKNOWN;
18075 }
18076
18077 return res;
18078 }
18079
18080
18081
18082 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
18083 {
18084 int oldformat;
18085 struct sip_pvt *p;
18086 struct ast_channel *tmpc = NULL;
18087 char *ext, *host;
18088 char tmp[256];
18089 char *dest = data;
18090
18091 oldformat = format;
18092 if (!(format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1))) {
18093 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));
18094 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
18095 return NULL;
18096 }
18097 if (option_debug)
18098 ast_log(LOG_DEBUG, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
18099
18100 if (ast_strlen_zero(dest)) {
18101 ast_log(LOG_ERROR, "Unable to create channel with empty destination.\n");
18102 *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
18103 return NULL;
18104 }
18105
18106 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE))) {
18107 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", (char *)data);
18108 *cause = AST_CAUSE_SWITCH_CONGESTION;
18109 return NULL;
18110 }
18111
18112 ast_set_flag(&p->flags[1], SIP_PAGE2_OUTGOING_CALL);
18113
18114 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
18115 sip_destroy(p);
18116 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
18117 *cause = AST_CAUSE_SWITCH_CONGESTION;
18118 return NULL;
18119 }
18120
18121 ast_copy_string(tmp, dest, sizeof(tmp));
18122 host = strchr(tmp, '@');
18123 if (host) {
18124 *host++ = '\0';
18125 ext = tmp;
18126 } else {
18127 ext = strchr(tmp, '/');
18128 if (ext)
18129 *ext++ = '\0';
18130 host = tmp;
18131 }
18132
18133 if (create_addr(p, host, NULL)) {
18134 *cause = AST_CAUSE_UNREGISTERED;
18135 if (option_debug > 2)
18136 ast_log(LOG_DEBUG, "Cant create SIP call - target device not registered\n");
18137 sip_destroy(p);
18138 return NULL;
18139 }
18140 if (ast_strlen_zero(p->peername) && ext)
18141 ast_string_field_set(p, peername, ext);
18142
18143 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
18144 p->ourip = __ourip;
18145 build_via(p);
18146 build_callid_pvt(p);
18147
18148
18149
18150
18151
18152 if (ext) {
18153 ast_string_field_set(p, username, ext);
18154 ast_string_field_free(p, fullcontact);
18155 }
18156 #if 0
18157 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
18158 #endif
18159 p->prefcodec = oldformat;
18160 ast_mutex_lock(&p->lock);
18161 tmpc = sip_new(p, AST_STATE_DOWN, host);
18162 ast_mutex_unlock(&p->lock);
18163 if (!tmpc)
18164 sip_destroy(p);
18165 ast_update_use_count();
18166 restart_monitor();
18167 return tmpc;
18168 }
18169
18170
18171
18172
18173
18174
18175
18176 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno)
18177 {
18178 static int dep_insecure_very = 0;
18179 static int dep_insecure_yes = 0;
18180
18181 if (ast_strlen_zero(value))
18182 return;
18183
18184 if (!strcasecmp(value, "very")) {
18185 ast_set_flag(flags, SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
18186 if(!dep_insecure_very) {
18187 if(lineno != -1)
18188 ast_log(LOG_WARNING, "insecure=very at line %d is deprecated; use insecure=port,invite instead\n", lineno);
18189 else
18190 ast_log(LOG_WARNING, "insecure=very is deprecated; use insecure=port,invite instead\n");
18191 dep_insecure_very = 1;
18192 }
18193 }
18194 else if (ast_true(value)) {
18195 ast_set_flag(flags, SIP_INSECURE_PORT);
18196 if(!dep_insecure_yes) {
18197 if(lineno != -1)
18198 ast_log(LOG_WARNING, "insecure=%s at line %d is deprecated; use insecure=port instead\n", value, lineno);
18199 else
18200 ast_log(LOG_WARNING, "insecure=%s is deprecated; use insecure=port instead\n", value);
18201 dep_insecure_yes = 1;
18202 }
18203 }
18204 else if (!ast_false(value)) {
18205 char buf[64];
18206 char *word, *next;
18207 ast_copy_string(buf, value, sizeof(buf));
18208 next = buf;
18209 while ((word = strsep(&next, ","))) {
18210 if (!strcasecmp(word, "port"))
18211 ast_set_flag(flags, SIP_INSECURE_PORT);
18212 else if (!strcasecmp(word, "invite"))
18213 ast_set_flag(flags, SIP_INSECURE_INVITE);
18214 else
18215 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
18216 }
18217 }
18218 }
18219
18220
18221
18222
18223
18224
18225
18226
18227 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
18228 {
18229 int res = 1;
18230
18231 if (!strcasecmp(v->name, "trustrpid")) {
18232 ast_set_flag(&mask[0], SIP_TRUSTRPID);
18233 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
18234 } else if (!strcasecmp(v->name, "sendrpid")) {
18235 ast_set_flag(&mask[0], SIP_SENDRPID);
18236 ast_set2_flag(&flags[0], ast_true(v->value), SIP_SENDRPID);
18237 } else if (!strcasecmp(v->name, "g726nonstandard")) {
18238 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
18239 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
18240 } else if (!strcasecmp(v->name, "useclientcode")) {
18241 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
18242 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
18243 } else if (!strcasecmp(v->name, "dtmfmode")) {
18244 ast_set_flag(&mask[0], SIP_DTMF);
18245 ast_clear_flag(&flags[0], SIP_DTMF);
18246 if (!strcasecmp(v->value, "inband"))
18247 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
18248 else if (!strcasecmp(v->value, "rfc2833"))
18249 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
18250 else if (!strcasecmp(v->value, "info"))
18251 ast_set_flag(&flags[0], SIP_DTMF_INFO);
18252 else if (!strcasecmp(v->value, "auto"))
18253 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
18254 else {
18255 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
18256 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
18257 }
18258 } else if (!strcasecmp(v->name, "nat")) {
18259 ast_set_flag(&mask[0], SIP_NAT);
18260 ast_clear_flag(&flags[0], SIP_NAT);
18261 if (!strcasecmp(v->value, "never"))
18262 ast_set_flag(&flags[0], SIP_NAT_NEVER);
18263 else if (!strcasecmp(v->value, "route"))
18264 ast_set_flag(&flags[0], SIP_NAT_ROUTE);
18265 else if (ast_true(v->value))
18266 ast_set_flag(&flags[0], SIP_NAT_ALWAYS);
18267 else
18268 ast_set_flag(&flags[0], SIP_NAT_RFC3581);
18269 } else if (!strcasecmp(v->name, "canreinvite")) {
18270 ast_set_flag(&mask[0], SIP_REINVITE);
18271 ast_clear_flag(&flags[0], SIP_REINVITE);
18272 if(ast_true(v->value)) {
18273 ast_set_flag(&flags[0], SIP_CAN_REINVITE | SIP_CAN_REINVITE_NAT);
18274 } else if (!ast_false(v->value)) {
18275 char buf[64];
18276 char *word, *next = buf;
18277
18278 ast_copy_string(buf, v->value, sizeof(buf));
18279 while ((word = strsep(&next, ","))) {
18280 if(!strcasecmp(word, "update")) {
18281 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_CAN_REINVITE);
18282 } else if(!strcasecmp(word, "nonat")) {
18283 ast_set_flag(&flags[0], SIP_CAN_REINVITE);
18284 ast_clear_flag(&flags[0], SIP_CAN_REINVITE_NAT);
18285 } else {
18286 ast_log(LOG_WARNING, "Unknown canreinvite mode '%s' on line %d\n", v->value, v->lineno);
18287 }
18288 }
18289 }
18290 } else if (!strcasecmp(v->name, "insecure")) {
18291 ast_set_flag(&mask[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
18292 ast_clear_flag(&flags[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
18293 set_insecure_flags(flags, v->value, v->lineno);
18294 } else if (!strcasecmp(v->name, "progressinband")) {
18295 ast_set_flag(&mask[0], SIP_PROG_INBAND);
18296 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
18297 if (ast_true(v->value))
18298 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
18299 else if (strcasecmp(v->value, "never"))
18300 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
18301 } else if (!strcasecmp(v->name, "promiscredir")) {
18302 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
18303 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
18304 } else if (!strcasecmp(v->name, "videosupport")) {
18305 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
18306 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
18307 } else if (!strcasecmp(v->name, "allowoverlap")) {
18308 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
18309 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
18310 } else if (!strcasecmp(v->name, "allowsubscribe")) {
18311 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
18312 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
18313 } else if (!strcasecmp(v->name, "t38pt_udptl")) {
18314 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_UDPTL);
18315 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);
18316 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
18317 } else if (!strcasecmp(v->name, "t38pt_rtp")) {
18318 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_RTP);
18319 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);
18320 } else if (!strcasecmp(v->name, "t38pt_tcp")) {
18321 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_TCP);
18322 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);
18323 #endif
18324 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
18325 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
18326 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
18327 } else if (!strcasecmp(v->name, "buggymwi")) {
18328 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
18329 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
18330 } else if (!strcasecmp(v->name, "t38pt_usertpsource")) {
18331 ast_set_flag(&mask[1], SIP_PAGE2_UDPTL_DESTINATION);
18332 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_UDPTL_DESTINATION);
18333 } else if (!strcasecmp(v->name, "forwardloopdetected")) {
18334 ast_set_flag(&mask[1], SIP_PAGE2_FORWARD_LOOP_DETECTED);
18335 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_FORWARD_LOOP_DETECTED);
18336 } else
18337 res = 0;
18338
18339 return res;
18340 }
18341
18342
18343 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
18344 {
18345 struct domain *d;
18346
18347 if (ast_strlen_zero(domain)) {
18348 ast_log(LOG_WARNING, "Zero length domain.\n");
18349 return 1;
18350 }
18351
18352 if (!(d = ast_calloc(1, sizeof(*d))))
18353 return 0;
18354
18355 ast_copy_string(d->domain, domain, sizeof(d->domain));
18356
18357 if (!ast_strlen_zero(context))
18358 ast_copy_string(d->context, context, sizeof(d->context));
18359
18360 d->mode = mode;
18361
18362 AST_LIST_LOCK(&domain_list);
18363 AST_LIST_INSERT_TAIL(&domain_list, d, list);
18364 AST_LIST_UNLOCK(&domain_list);
18365
18366 if (sipdebug)
18367 ast_log(LOG_DEBUG, "Added local SIP domain '%s'\n", domain);
18368
18369 return 1;
18370 }
18371
18372
18373 static int check_sip_domain(const char *domain, char *context, size_t len)
18374 {
18375 struct domain *d;
18376 int result = 0;
18377
18378 AST_LIST_LOCK(&domain_list);
18379 AST_LIST_TRAVERSE(&domain_list, d, list) {
18380 if (strcasecmp(d->domain, domain))
18381 continue;
18382
18383 if (len && !ast_strlen_zero(d->context))
18384 ast_copy_string(context, d->context, len);
18385
18386 result = 1;
18387 break;
18388 }
18389 AST_LIST_UNLOCK(&domain_list);
18390
18391 return result;
18392 }
18393
18394
18395 static void clear_sip_domains(void)
18396 {
18397 struct domain *d;
18398
18399 AST_LIST_LOCK(&domain_list);
18400 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
18401 free(d);
18402 AST_LIST_UNLOCK(&domain_list);
18403 }
18404
18405
18406
18407 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno)
18408 {
18409 char authcopy[256];
18410 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
18411 struct sip_auth *a, *b, *auth;
18412
18413 if (ast_strlen_zero(configuration))
18414 return authlist;
18415
18416 if (option_debug)
18417 ast_log(LOG_DEBUG, "Auth config :: %s\n", configuration);
18418
18419 ast_copy_string(authcopy, configuration, sizeof(authcopy));
18420
18421 username = authcopy;
18422
18423 realm = strrchr(username, '@');
18424 if (realm)
18425 *realm++ = '\0';
18426 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
18427 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
18428 return authlist;
18429 }
18430
18431
18432 if ((secret = strchr(username, ':'))) {
18433 *secret++ = '\0';
18434 } else if ((md5secret = strchr(username, '#'))) {
18435 *md5secret++ = '\0';
18436 }
18437
18438 if (!(auth = ast_calloc(1, sizeof(*auth))))
18439 return authlist;
18440
18441 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
18442 ast_copy_string(auth->username, username, sizeof(auth->username));
18443 if (secret)
18444 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
18445 if (md5secret)
18446 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
18447
18448
18449 for (b = NULL, a = authlist; a ; b = a, a = a->next)
18450 ;
18451 if (b)
18452 b->next = auth;
18453 else
18454 authlist = auth;
18455
18456 if (option_verbose > 2)
18457 ast_verbose("Added authentication for realm %s\n", realm);
18458
18459 return authlist;
18460
18461 }
18462
18463
18464 static int clear_realm_authentication(struct sip_auth *authlist)
18465 {
18466 struct sip_auth *a = authlist;
18467 struct sip_auth *b;
18468
18469 while (a) {
18470 b = a;
18471 a = a->next;
18472 free(b);
18473 }
18474
18475 return 1;
18476 }
18477
18478
18479 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
18480 {
18481 struct sip_auth *a;
18482
18483 for (a = authlist; a; a = a->next) {
18484 if (!strcasecmp(a->realm, realm))
18485 break;
18486 }
18487
18488 return a;
18489 }
18490
18491
18492 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
18493 {
18494 struct sip_user *user;
18495 int format;
18496 struct ast_ha *oldha = NULL;
18497 char *varname = NULL, *varval = NULL;
18498 struct ast_variable *tmpvar = NULL;
18499 struct ast_flags userflags[2] = {{(0)}};
18500 struct ast_flags mask[2] = {{(0)}};
18501
18502
18503 if (!(user = ast_calloc(1, sizeof(*user))))
18504 return NULL;
18505
18506 suserobjs++;
18507 ASTOBJ_INIT(user);
18508 ast_copy_string(user->name, name, sizeof(user->name));
18509 oldha = user->ha;
18510 user->ha = NULL;
18511 ast_copy_flags(&user->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
18512 ast_copy_flags(&user->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
18513 user->capability = global_capability;
18514 user->allowtransfer = global_allowtransfer;
18515 user->maxcallbitrate = default_maxcallbitrate;
18516 user->autoframing = global_autoframing;
18517 user->prefs = default_prefs;
18518
18519 strcpy(user->context, default_context);
18520 strcpy(user->language, default_language);
18521 strcpy(user->mohinterpret, default_mohinterpret);
18522 strcpy(user->mohsuggest, default_mohsuggest);
18523
18524 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
18525 if (handle_common_options(&userflags[0], &mask[0], v))
18526 continue;
18527
18528 if (!strcasecmp(v->name, "context")) {
18529 ast_copy_string(user->context, v->value, sizeof(user->context));
18530 } else if (!strcasecmp(v->name, "subscribecontext")) {
18531 ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
18532 } else if (!strcasecmp(v->name, "setvar")) {
18533 varname = ast_strdupa(v->value);
18534 if ((varval = strchr(varname,'='))) {
18535 *varval++ = '\0';
18536 if ((tmpvar = ast_variable_new(varname, varval))) {
18537 tmpvar->next = user->chanvars;
18538 user->chanvars = tmpvar;
18539 }
18540 }
18541 } else if (!strcasecmp(v->name, "permit") ||
18542 !strcasecmp(v->name, "deny")) {
18543 user->ha = ast_append_ha(v->name, v->value, user->ha);
18544 } else if (!strcasecmp(v->name, "allowtransfer")) {
18545 user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
18546 } else if (!strcasecmp(v->name, "secret")) {
18547 ast_copy_string(user->secret, v->value, sizeof(user->secret));
18548 } else if (!strcasecmp(v->name, "md5secret")) {
18549 ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
18550 } else if (!strcasecmp(v->name, "callerid")) {
18551 ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
18552 } else if (!strcasecmp(v->name, "fullname")) {
18553 ast_copy_string(user->cid_name, v->value, sizeof(user->cid_name));
18554 } else if (!strcasecmp(v->name, "trunkname")) {
18555
18556 ast_copy_string(user->cid_name, "", sizeof(user->cid_name));
18557 } else if (!strcasecmp(v->name, "cid_number")) {
18558 ast_copy_string(user->cid_num, v->value, sizeof(user->cid_num));
18559 } else if (!strcasecmp(v->name, "callgroup")) {
18560 user->callgroup = ast_get_group(v->value);
18561 } else if (!strcasecmp(v->name, "pickupgroup")) {
18562 user->pickupgroup = ast_get_group(v->value);
18563 } else if (!strcasecmp(v->name, "language")) {
18564 ast_copy_string(user->language, v->value, sizeof(user->language));
18565 } else if (!strcasecmp(v->name, "mohinterpret")
18566 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
18567 ast_copy_string(user->mohinterpret, v->value, sizeof(user->mohinterpret));
18568 } else if (!strcasecmp(v->name, "mohsuggest")) {
18569 ast_copy_string(user->mohsuggest, v->value, sizeof(user->mohsuggest));
18570 } else if (!strcasecmp(v->name, "accountcode")) {
18571 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
18572 } else if (!strcasecmp(v->name, "call-limit")) {
18573 user->call_limit = atoi(v->value);
18574 if (user->call_limit < 0)
18575 user->call_limit = 0;
18576 } else if (!strcasecmp(v->name, "amaflags")) {
18577 format = ast_cdr_amaflags2int(v->value);
18578 if (format < 0) {
18579 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
18580 } else {
18581 user->amaflags = format;
18582 }
18583 } else if (!strcasecmp(v->name, "allow")) {
18584 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
18585 } else if (!strcasecmp(v->name, "disallow")) {
18586 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
18587 } else if (!strcasecmp(v->name, "autoframing")) {
18588 user->autoframing = ast_true(v->value);
18589 } else if (!strcasecmp(v->name, "callingpres")) {
18590 user->callingpres = ast_parse_caller_presentation(v->value);
18591 if (user->callingpres == -1)
18592 user->callingpres = atoi(v->value);
18593 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
18594 user->maxcallbitrate = atoi(v->value);
18595 if (user->maxcallbitrate < 0)
18596 user->maxcallbitrate = default_maxcallbitrate;
18597 }
18598
18599
18600
18601 }
18602 ast_copy_flags(&user->flags[0], &userflags[0], mask[0].flags);
18603 ast_copy_flags(&user->flags[1], &userflags[1], mask[1].flags);
18604 if (ast_test_flag(&user->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
18605 global_allowsubscribe = TRUE;
18606 ast_free_ha(oldha);
18607 return user;
18608 }
18609
18610
18611 static void set_peer_defaults(struct sip_peer *peer)
18612 {
18613 if (peer->expire == 0) {
18614
18615
18616
18617 peer->expire = -1;
18618 peer->pokeexpire = -1;
18619 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
18620 }
18621 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
18622 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
18623 strcpy(peer->context, default_context);
18624 strcpy(peer->subscribecontext, default_subscribecontext);
18625 strcpy(peer->language, default_language);
18626 strcpy(peer->mohinterpret, default_mohinterpret);
18627 strcpy(peer->mohsuggest, default_mohsuggest);
18628 peer->addr.sin_family = AF_INET;
18629 peer->defaddr.sin_family = AF_INET;
18630 peer->capability = global_capability;
18631 peer->maxcallbitrate = default_maxcallbitrate;
18632 peer->rtptimeout = global_rtptimeout;
18633 peer->rtpholdtimeout = global_rtpholdtimeout;
18634 peer->rtpkeepalive = global_rtpkeepalive;
18635 peer->allowtransfer = global_allowtransfer;
18636 peer->autoframing = global_autoframing;
18637 strcpy(peer->vmexten, default_vmexten);
18638 peer->secret[0] = '\0';
18639 peer->md5secret[0] = '\0';
18640 peer->cid_num[0] = '\0';
18641 peer->cid_name[0] = '\0';
18642 peer->fromdomain[0] = '\0';
18643 peer->fromuser[0] = '\0';
18644 peer->regexten[0] = '\0';
18645 peer->mailbox[0] = '\0';
18646 peer->callgroup = 0;
18647 peer->pickupgroup = 0;
18648 peer->maxms = default_qualify;
18649 peer->prefs = default_prefs;
18650 }
18651
18652
18653 static struct sip_peer *temp_peer(const char *name)
18654 {
18655 struct sip_peer *peer;
18656
18657 if (!(peer = ast_calloc(1, sizeof(*peer))))
18658 return NULL;
18659
18660 apeerobjs++;
18661 ASTOBJ_INIT(peer);
18662 set_peer_defaults(peer);
18663
18664 ast_copy_string(peer->name, name, sizeof(peer->name));
18665
18666 ast_set_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT);
18667 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
18668 peer->prefs = default_prefs;
18669 reg_source_db(peer);
18670
18671 return peer;
18672 }
18673
18674
18675 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only)
18676 {
18677 struct sip_peer *peer = NULL;
18678 struct ast_ha *oldha = NULL;
18679 int obproxyfound=0;
18680 int found=0;
18681 int firstpass=1;
18682 int format=0;
18683 time_t regseconds = 0;
18684 char *varname = NULL, *varval = NULL;
18685 struct ast_variable *tmpvar = NULL;
18686 struct ast_flags peerflags[2] = {{(0)}};
18687 struct ast_flags mask[2] = {{(0)}};
18688 int alt_fullcontact = alt ? 1 : 0;
18689 char fullcontact[sizeof(peer->fullcontact)] = "";
18690
18691 if (!realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS))
18692
18693
18694
18695
18696
18697 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
18698
18699 if (peer) {
18700
18701 found = 1;
18702 if (!(peer->objflags & ASTOBJ_FLAG_MARKED))
18703 firstpass = 0;
18704 } else {
18705 if (!(peer = ast_calloc(1, sizeof(*peer))))
18706 return NULL;
18707
18708 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS))
18709 rpeerobjs++;
18710 else
18711 speerobjs++;
18712 ASTOBJ_INIT(peer);
18713 }
18714
18715 if (firstpass) {
18716 peer->lastmsgssent = -1;
18717 oldha = peer->ha;
18718 peer->ha = NULL;
18719 set_peer_defaults(peer);
18720 }
18721 if (!found && name)
18722 ast_copy_string(peer->name, name, sizeof(peer->name));
18723
18724
18725 if (peer->chanvars) {
18726 ast_variables_destroy(peer->chanvars);
18727 peer->chanvars = NULL;
18728
18729 }
18730
18731 if (found)
18732 peer->portinuri = 0;
18733
18734
18735 clear_realm_authentication(peer->auth);
18736 peer->auth = NULL;
18737
18738 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
18739 if (!devstate_only) {
18740 if (handle_common_options(&peerflags[0], &mask[0], v)) {
18741 continue;
18742 }
18743 if (realtime && !strcasecmp(v->name, "regseconds")) {
18744 ast_get_time_t(v->value, ®seconds, 0, NULL);
18745 } else if (realtime && !strcasecmp(v->name, "name")) {
18746 ast_copy_string(peer->name, v->value, sizeof(peer->name));
18747 } else if (realtime && !strcasecmp(v->name, "useragent")) {
18748 ast_copy_string(peer->useragent, v->value, sizeof(peer->useragent));
18749 } else if (realtime && !strcasecmp(v->name, "fullcontact")) {
18750 if (alt_fullcontact && !alt) {
18751
18752
18753
18754
18755
18756 alt_fullcontact = 0;
18757 fullcontact[0] = '\0';
18758 }
18759
18760 if (!ast_strlen_zero(fullcontact)) {
18761 strncat(fullcontact, ";", sizeof(fullcontact) - strlen(fullcontact) - 1);
18762 strncat(fullcontact, v->value, sizeof(fullcontact) - strlen(fullcontact) - 1);
18763 } else {
18764 ast_copy_string(fullcontact, v->value, sizeof(fullcontact));
18765 ast_set_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT);
18766 }
18767 } else if (!strcasecmp(v->name, "secret")) {
18768 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
18769 } else if (!strcasecmp(v->name, "md5secret")) {
18770 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
18771 } else if (!strcasecmp(v->name, "auth")) {
18772 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
18773 } else if (!strcasecmp(v->name, "callerid")) {
18774 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
18775 } else if (!strcasecmp(v->name, "fullname")) {
18776 ast_copy_string(peer->cid_name, v->value, sizeof(peer->cid_name));
18777 } else if (!strcasecmp(v->name, "trunkname")) {
18778
18779 ast_copy_string(peer->cid_name, "", sizeof(peer->cid_name));
18780 } else if (!strcasecmp(v->name, "cid_number")) {
18781 ast_copy_string(peer->cid_num, v->value, sizeof(peer->cid_num));
18782 } else if (!strcasecmp(v->name, "context")) {
18783 ast_copy_string(peer->context, v->value, sizeof(peer->context));
18784 } else if (!strcasecmp(v->name, "subscribecontext")) {
18785 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
18786 } else if (!strcasecmp(v->name, "fromdomain")) {
18787 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
18788 } else if (!strcasecmp(v->name, "usereqphone")) {
18789 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
18790 } else if (!strcasecmp(v->name, "fromuser")) {
18791 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
18792 } else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "outboundproxy")) {
18793 if (!strcasecmp(v->value, "dynamic")) {
18794 if (!strcasecmp(v->name, "outboundproxy") || obproxyfound) {
18795 ast_log(LOG_WARNING, "You can't have a dynamic outbound proxy, you big silly head at line %d.\n", v->lineno);
18796 } else {
18797
18798 if (!found || !ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
18799
18800
18801 memset(&peer->addr.sin_addr, 0, 4);
18802 if (peer->addr.sin_port) {
18803
18804 peer->defaddr.sin_port = peer->addr.sin_port;
18805 peer->addr.sin_port = 0;
18806 }
18807 }
18808 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
18809 }
18810 } else {
18811
18812 if (!AST_SCHED_DEL(sched, peer->expire)) {
18813 struct sip_peer *peer_ptr = peer;
18814 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
18815 }
18816 ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
18817 if (!strcasecmp(v->name, "outboundproxy")) {
18818 if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
18819 ast_log(LOG_ERROR, "srvlookup failed for outboundproxy: %s, on peer %s, removing peer\n", v->value, peer->name);
18820 ASTOBJ_UNREF(peer, sip_destroy_peer);
18821 return NULL;
18822 }
18823 }
18824 if (!strcasecmp(v->name, "outboundproxy"))
18825 obproxyfound=1;
18826 else {
18827 ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
18828 if (!peer->addr.sin_port)
18829 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
18830 }
18831 if (global_dynamic_exclude_static) {
18832 global_contact_ha = ast_append_ha("deny", (char *)ast_inet_ntoa(peer->addr.sin_addr), global_contact_ha);
18833 }
18834 }
18835 } else if (!strcasecmp(v->name, "defaultip")) {
18836 if (!ast_strlen_zero(v->value) && ast_get_ip(&peer->defaddr, v->value)) {
18837 ASTOBJ_UNREF(peer, sip_destroy_peer);
18838 return NULL;
18839 }
18840 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
18841 if (!ast_strlen_zero(v->value)) {
18842 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
18843 }
18844 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
18845 if (!ast_strlen_zero(v->value)) {
18846 peer->contactha = ast_append_ha(v->name + 7, v->value, peer->contactha);
18847 }
18848 } else if (!strcasecmp(v->name, "port")) {
18849 peer->portinuri = 1;
18850 if (!realtime && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC))
18851 peer->defaddr.sin_port = htons(atoi(v->value));
18852 else
18853 peer->addr.sin_port = htons(atoi(v->value));
18854 } else if (!strcasecmp(v->name, "callingpres")) {
18855 peer->callingpres = ast_parse_caller_presentation(v->value);
18856 if (peer->callingpres == -1)
18857 peer->callingpres = atoi(v->value);
18858 } else if (!strcasecmp(v->name, "username")) {
18859 ast_copy_string(peer->username, v->value, sizeof(peer->username));
18860 } else if (!strcasecmp(v->name, "language")) {
18861 ast_copy_string(peer->language, v->value, sizeof(peer->language));
18862 } else if (!strcasecmp(v->name, "regexten")) {
18863 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
18864 } else if (!strcasecmp(v->name, "amaflags")) {
18865 format = ast_cdr_amaflags2int(v->value);
18866 if (format < 0) {
18867 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
18868 } else {
18869 peer->amaflags = format;
18870 }
18871 } else if (!strcasecmp(v->name, "accountcode")) {
18872 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
18873 } else if (!strcasecmp(v->name, "mohinterpret")
18874 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
18875 ast_copy_string(peer->mohinterpret, v->value, sizeof(peer->mohinterpret));
18876 } else if (!strcasecmp(v->name, "mohsuggest")) {
18877 ast_copy_string(peer->mohsuggest, v->value, sizeof(peer->mohsuggest));
18878 } else if (!strcasecmp(v->name, "mailbox")) {
18879 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
18880 } else if (!strcasecmp(v->name, "hasvoicemail")) {
18881
18882
18883 if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
18884 ast_copy_string(peer->mailbox, name, sizeof(peer->mailbox));
18885 }
18886 } else if (!strcasecmp(v->name, "subscribemwi")) {
18887 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
18888 } else if (!strcasecmp(v->name, "vmexten")) {
18889 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
18890 } else if (!strcasecmp(v->name, "callgroup")) {
18891 peer->callgroup = ast_get_group(v->value);
18892 } else if (!strcasecmp(v->name, "allowtransfer")) {
18893 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
18894 } else if (!strcasecmp(v->name, "pickupgroup")) {
18895 peer->pickupgroup = ast_get_group(v->value);
18896 } else if (!strcasecmp(v->name, "allow")) {
18897 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
18898 } else if (!strcasecmp(v->name, "disallow")) {
18899 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
18900 } else if (!strcasecmp(v->name, "autoframing")) {
18901 peer->autoframing = ast_true(v->value);
18902 } else if (!strcasecmp(v->name, "rtptimeout")) {
18903 if ((sscanf(v->value, "%30d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
18904 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
18905 peer->rtptimeout = global_rtptimeout;
18906 }
18907 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
18908 if ((sscanf(v->value, "%30d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
18909 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
18910 peer->rtpholdtimeout = global_rtpholdtimeout;
18911 }
18912 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
18913 if ((sscanf(v->value, "%30d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
18914 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
18915 peer->rtpkeepalive = global_rtpkeepalive;
18916 }
18917 } else if (!strcasecmp(v->name, "setvar")) {
18918
18919 varname = ast_strdupa(v->value);
18920 if ((varval = strchr(varname, '='))) {
18921 *varval++ = '\0';
18922 if ((tmpvar = ast_variable_new(varname, varval))) {
18923 tmpvar->next = peer->chanvars;
18924 peer->chanvars = tmpvar;
18925 }
18926 }
18927 }
18928 }
18929
18930
18931 if (realtime && !strcasecmp(v->name, "lastms")) {
18932 sscanf(v->value, "%30d", &peer->lastms);
18933 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
18934 inet_aton(v->value, &(peer->addr.sin_addr));
18935 } else if (!strcasecmp(v->name, "qualify")) {
18936 if (!strcasecmp(v->value, "no")) {
18937 peer->maxms = 0;
18938 } else if (!strcasecmp(v->value, "yes")) {
18939 peer->maxms = default_qualify ? default_qualify : DEFAULT_MAXMS;
18940 } else if (sscanf(v->value, "%30d", &peer->maxms) != 1) {
18941 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);
18942 peer->maxms = 0;
18943 }
18944 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && peer->maxms > 0) {
18945
18946
18947
18948
18949 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);
18950 peer->maxms = 0;
18951 }
18952 } else if (!strcasecmp(v->name, "call-limit") || !strcasecmp(v->name, "incominglimit")) {
18953 peer->call_limit = atoi(v->value);
18954 if (peer->call_limit < 0) {
18955 peer->call_limit = 0;
18956 }
18957 }
18958 }
18959
18960 if (!ast_strlen_zero(fullcontact)) {
18961 ast_copy_string(peer->fullcontact, fullcontact, sizeof(peer->fullcontact));
18962
18963
18964
18965
18966
18967
18968 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE) || !peer->addr.sin_addr.s_addr) {
18969 __set_address_from_contact(fullcontact, &peer->addr);
18970 }
18971 }
18972
18973 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !obproxyfound && !ast_strlen_zero(peer->tohost)) {
18974 if (ast_get_ip_or_srv(&peer->addr, peer->tohost, srvlookup && !peer->portinuri ? "_sip._udp" : NULL)) {
18975 ast_log(LOG_ERROR, "host lookup failed for %s, on peer %s, removing peer\n", peer->tohost, peer->name);
18976 ASTOBJ_UNREF(peer, sip_destroy_peer);
18977 return NULL;
18978 }
18979 }
18980
18981 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && realtime) {
18982 time_t nowtime = time(NULL);
18983
18984 if ((nowtime - regseconds) > 0) {
18985 destroy_association(peer);
18986 memset(&peer->addr, 0, sizeof(peer->addr));
18987 peer->lastms = -1;
18988 if (option_debug)
18989 ast_log(LOG_DEBUG, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
18990 }
18991 }
18992
18993
18994 if (realtime && peer->lastms > 0) {
18995 ASTOBJ_REF(peer);
18996 sip_poke_peer(peer);
18997 }
18998
18999 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
19000 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
19001 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
19002 global_allowsubscribe = TRUE;
19003 if (!found && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !ast_test_flag(&peer->flags[0], SIP_REALTIME))
19004 reg_source_db(peer);
19005 ASTOBJ_UNMARK(peer);
19006 ast_free_ha(oldha);
19007 return peer;
19008 }
19009
19010
19011
19012
19013
19014
19015
19016 static int reload_config(enum channelreloadreason reason)
19017 {
19018 struct ast_config *cfg, *ucfg;
19019 struct ast_variable *v;
19020 struct sip_peer *peer;
19021 struct sip_user *user;
19022 struct ast_hostent ahp;
19023 char *cat, *stringp, *context, *oldregcontext;
19024 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
19025 struct hostent *hp;
19026 int format;
19027 struct ast_flags dummy[2];
19028 int auto_sip_domains = FALSE;
19029 struct sockaddr_in old_bindaddr = bindaddr;
19030 int registry_count = 0, peer_count = 0, user_count = 0;
19031 unsigned int temp_tos = 0;
19032 struct ast_flags debugflag = {0};
19033
19034 cfg = ast_config_load(config);
19035
19036
19037 if (!cfg) {
19038 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
19039 return -1;
19040 }
19041
19042 if (option_debug > 3)
19043 ast_log(LOG_DEBUG, "--------------- SIP reload started\n");
19044
19045 clear_realm_authentication(authl);
19046 clear_sip_domains();
19047 authl = NULL;
19048
19049 ast_free_ha(global_contact_ha);
19050 global_contact_ha = NULL;
19051
19052
19053
19054 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
19055 ASTOBJ_RDLOCK(iterator);
19056 if (iterator->call) {
19057 if (option_debug > 2)
19058 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
19059
19060 sip_destroy(iterator->call);
19061 }
19062 ASTOBJ_UNLOCK(iterator);
19063
19064 } while(0));
19065
19066
19067 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
19068 if (option_debug > 3)
19069 ast_log(LOG_DEBUG, "--------------- Done destroying user list\n");
19070 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
19071 if (option_debug > 3)
19072 ast_log(LOG_DEBUG, "--------------- Done destroying registry list\n");
19073 ASTOBJ_CONTAINER_MARKALL(&peerl);
19074
19075
19076 ast_copy_string(oldcontexts, global_regcontext, sizeof(oldcontexts));
19077 oldregcontext = oldcontexts;
19078
19079
19080
19081 ast_copy_flags(&debugflag, &global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
19082 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
19083 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
19084 ast_copy_flags(&global_flags[1], &debugflag, SIP_PAGE2_DEBUG_CONSOLE);
19085
19086
19087 memset(&bindaddr, 0, sizeof(bindaddr));
19088 ast_free_ha(localaddr);
19089 memset(&localaddr, 0, sizeof(localaddr));
19090 memset(&externip, 0, sizeof(externip));
19091 memset(&default_prefs, 0 , sizeof(default_prefs));
19092 outboundproxyip.sin_port = htons(STANDARD_SIP_PORT);
19093 outboundproxyip.sin_family = AF_INET;
19094 ourport = STANDARD_SIP_PORT;
19095 srvlookup = DEFAULT_SRVLOOKUP;
19096 global_tos_sip = DEFAULT_TOS_SIP;
19097 global_tos_audio = DEFAULT_TOS_AUDIO;
19098 global_tos_video = DEFAULT_TOS_VIDEO;
19099 externhost[0] = '\0';
19100 externexpire = 0;
19101 externrefresh = 10;
19102 memset(&outboundproxyip, 0, sizeof(outboundproxyip));
19103
19104
19105 allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
19106 global_regcontext[0] = '\0';
19107 expiry = DEFAULT_EXPIRY;
19108 global_notifyringing = DEFAULT_NOTIFYRINGING;
19109 global_limitonpeers = FALSE;
19110 global_prematuremediafilter = FALSE;
19111 global_directrtpsetup = FALSE;
19112 global_notifyhold = FALSE;
19113 global_alwaysauthreject = 0;
19114 global_allowsubscribe = FALSE;
19115 ast_copy_string(global_useragent, DEFAULT_USERAGENT, sizeof(global_useragent));
19116 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
19117 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME))
19118 ast_copy_string(global_realm, DEFAULT_REALM, sizeof(global_realm));
19119 else
19120 ast_copy_string(global_realm, ast_config_AST_SYSTEM_NAME, sizeof(global_realm));
19121 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
19122 compactheaders = DEFAULT_COMPACTHEADERS;
19123 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
19124 global_regattempts_max = 0;
19125 pedanticsipchecking = DEFAULT_PEDANTIC;
19126 global_mwitime = DEFAULT_MWITIME;
19127 autocreatepeer = DEFAULT_AUTOCREATEPEER;
19128 global_autoframing = 0;
19129 global_allowguest = DEFAULT_ALLOWGUEST;
19130 global_rtptimeout = 0;
19131 global_rtpholdtimeout = 0;
19132 global_rtpkeepalive = 0;
19133 global_allowtransfer = TRANSFER_OPENFORALL;
19134 global_rtautoclear = 120;
19135 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
19136 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
19137 ast_set_flag(&global_flags[1], SIP_PAGE2_RTUPDATE);
19138
19139
19140 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
19141 default_subscribecontext[0] = '\0';
19142 default_language[0] = '\0';
19143 default_fromdomain[0] = '\0';
19144 default_qualify = DEFAULT_QUALIFY;
19145 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
19146 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
19147 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
19148 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
19149 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
19150 ast_set_flag(&global_flags[0], SIP_NAT_RFC3581);
19151 ast_set_flag(&global_flags[0], SIP_CAN_REINVITE);
19152 ast_set_flag(&global_flags[1], SIP_PAGE2_FORWARD_LOOP_DETECTED);
19153
19154
19155 dumphistory = FALSE;
19156 recordhistory = FALSE;
19157 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
19158
19159
19160 global_relaxdtmf = FALSE;
19161 global_callevents = FALSE;
19162 global_t1min = DEFAULT_T1MIN;
19163 global_shrinkcallerid = 1;
19164
19165 global_matchexterniplocally = FALSE;
19166
19167
19168 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
19169
19170 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT);
19171
19172
19173 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
19174 if (handle_common_options(&global_flags[0], &dummy[0], v))
19175 continue;
19176
19177 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
19178 continue;
19179
19180
19181 if (!strcasecmp(v->name, "context")) {
19182 ast_copy_string(default_context, v->value, sizeof(default_context));
19183 } else if (!strcasecmp(v->name, "subscribecontext")) {
19184 ast_copy_string(default_subscribecontext, v->value, sizeof(default_subscribecontext));
19185 } else if (!strcasecmp(v->name, "allowguest")) {
19186 global_allowguest = ast_true(v->value) ? 1 : 0;
19187 } else if (!strcasecmp(v->name, "realm")) {
19188 ast_copy_string(global_realm, v->value, sizeof(global_realm));
19189 } else if (!strcasecmp(v->name, "useragent")) {
19190 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
19191 if (option_debug)
19192 ast_log(LOG_DEBUG, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
19193 } else if (!strcasecmp(v->name, "allowtransfer")) {
19194 global_allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
19195 } else if (!strcasecmp(v->name, "rtcachefriends")) {
19196 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
19197 } else if (!strcasecmp(v->name, "rtsavesysname")) {
19198 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTSAVE_SYSNAME);
19199 } else if (!strcasecmp(v->name, "rtupdate")) {
19200 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTUPDATE);
19201 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
19202 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_IGNOREREGEXPIRE);
19203 } else if (!strcasecmp(v->name, "t1min")) {
19204 global_t1min = atoi(v->value);
19205 } else if (!strcasecmp(v->name, "dynamic_exclude_static") || !strcasecmp(v->name, "dynamic_excludes_static")) {
19206 global_dynamic_exclude_static = ast_true(v->value);
19207 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
19208 global_contact_ha = ast_append_ha(v->name + 7, v->value, global_contact_ha);
19209 } else if (!strcasecmp(v->name, "rtautoclear")) {
19210 int i = atoi(v->value);
19211 if (i > 0)
19212 global_rtautoclear = i;
19213 else
19214 i = 0;
19215 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
19216 } else if (!strcasecmp(v->name, "usereqphone")) {
19217 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
19218 } else if (!strcasecmp(v->name, "prematuremedia")) {
19219 global_prematuremediafilter = ast_true(v->value);
19220 } else if (!strcasecmp(v->name, "relaxdtmf")) {
19221 global_relaxdtmf = ast_true(v->value);
19222 } else if (!strcasecmp(v->name, "checkmwi")) {
19223 if ((sscanf(v->value, "%30d", &global_mwitime) != 1) || (global_mwitime < 0)) {
19224 ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d. Using default (10).\n", v->value, v->lineno);
19225 global_mwitime = DEFAULT_MWITIME;
19226 }
19227 } else if (!strcasecmp(v->name, "vmexten")) {
19228 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
19229 } else if (!strcasecmp(v->name, "rtptimeout")) {
19230 if ((sscanf(v->value, "%30d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
19231 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
19232 global_rtptimeout = 0;
19233 }
19234 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
19235 if ((sscanf(v->value, "%30d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
19236 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
19237 global_rtpholdtimeout = 0;
19238 }
19239 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
19240 if ((sscanf(v->value, "%30d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
19241 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
19242 global_rtpkeepalive = 0;
19243 }
19244 } else if (!strcasecmp(v->name, "compactheaders")) {
19245 compactheaders = ast_true(v->value);
19246 } else if (!strcasecmp(v->name, "notifymimetype")) {
19247 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
19248 } else if (!strncasecmp(v->name, "limitonpeer", 11)) {
19249 global_limitonpeers = ast_true(v->value);
19250 } else if (!strcasecmp(v->name, "directrtpsetup")) {
19251 global_directrtpsetup = ast_true(v->value);
19252 } else if (!strcasecmp(v->name, "notifyringing")) {
19253 global_notifyringing = ast_true(v->value);
19254 } else if (!strcasecmp(v->name, "notifyhold")) {
19255 global_notifyhold = ast_true(v->value);
19256 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
19257 global_alwaysauthreject = ast_true(v->value);
19258 } else if (!strcasecmp(v->name, "mohinterpret")
19259 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
19260 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
19261 } else if (!strcasecmp(v->name, "mohsuggest")) {
19262 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
19263 } else if (!strcasecmp(v->name, "language")) {
19264 ast_copy_string(default_language, v->value, sizeof(default_language));
19265 } else if (!strcasecmp(v->name, "regcontext")) {
19266 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
19267 stringp = newcontexts;
19268
19269 cleanup_stale_contexts(stringp, oldregcontext);
19270
19271 while ((context = strsep(&stringp, "&"))) {
19272 if (!ast_context_find(context))
19273 ast_context_create(NULL, context,"SIP");
19274 }
19275 ast_copy_string(global_regcontext, v->value, sizeof(global_regcontext));
19276 } else if (!strcasecmp(v->name, "callerid")) {
19277 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
19278 } else if (!strcasecmp(v->name, "fromdomain")) {
19279 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
19280 } else if (!strcasecmp(v->name, "outboundproxy")) {
19281 if (ast_get_ip_or_srv(&outboundproxyip, v->value, srvlookup ? "_sip._udp" : NULL) < 0)
19282 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", v->value);
19283 } else if (!strcasecmp(v->name, "outboundproxyport")) {
19284
19285 sscanf(v->value, "%30d", &format);
19286 outboundproxyip.sin_port = htons(format);
19287 } else if (!strcasecmp(v->name, "autocreatepeer")) {
19288 autocreatepeer = ast_true(v->value);
19289 } else if (!strcasecmp(v->name, "srvlookup")) {
19290 srvlookup = ast_true(v->value);
19291 } else if (!strcasecmp(v->name, "pedantic")) {
19292 pedanticsipchecking = ast_true(v->value);
19293 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
19294 max_expiry = atoi(v->value);
19295 if (max_expiry < 1)
19296 max_expiry = DEFAULT_MAX_EXPIRY;
19297 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
19298 min_expiry = atoi(v->value);
19299 if (min_expiry < 1)
19300 min_expiry = DEFAULT_MIN_EXPIRY;
19301 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
19302 default_expiry = atoi(v->value);
19303 if (default_expiry < 1)
19304 default_expiry = DEFAULT_DEFAULT_EXPIRY;
19305 } else if (!strcasecmp(v->name, "sipdebug")) {
19306 if (ast_true(v->value))
19307 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
19308 } else if (!strcasecmp(v->name, "dumphistory")) {
19309 dumphistory = ast_true(v->value);
19310 } else if (!strcasecmp(v->name, "recordhistory")) {
19311 recordhistory = ast_true(v->value);
19312 } else if (!strcasecmp(v->name, "registertimeout")) {
19313 global_reg_timeout = atoi(v->value);
19314 if (global_reg_timeout < 1)
19315 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
19316 } else if (!strcasecmp(v->name, "registerattempts")) {
19317 global_regattempts_max = atoi(v->value);
19318 } else if (!strcasecmp(v->name, "bindaddr")) {
19319 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
19320 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
19321 } else {
19322 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
19323 }
19324 } else if (!strcasecmp(v->name, "localnet")) {
19325 struct ast_ha *na;
19326 if (!(na = ast_append_ha("d", v->value, localaddr)))
19327 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
19328 else
19329 localaddr = na;
19330 } else if (!strcasecmp(v->name, "localmask")) {
19331 ast_log(LOG_WARNING, "Use of localmask is no long supported -- use localnet with mask syntax\n");
19332 } else if (!strcasecmp(v->name, "externip")) {
19333 if (!(hp = ast_gethostbyname(v->value, &ahp)))
19334 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
19335 else
19336 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
19337 externexpire = 0;
19338 } else if (!strcasecmp(v->name, "externhost")) {
19339 ast_copy_string(externhost, v->value, sizeof(externhost));
19340 if (!(hp = ast_gethostbyname(externhost, &ahp)))
19341 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
19342 else
19343 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
19344 externexpire = time(NULL);
19345 } else if (!strcasecmp(v->name, "externrefresh")) {
19346 if (sscanf(v->value, "%30d", &externrefresh) != 1) {
19347 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
19348 externrefresh = 10;
19349 }
19350 } else if (!strcasecmp(v->name, "allow")) {
19351 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 1);
19352 } else if (!strcasecmp(v->name, "disallow")) {
19353 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 0);
19354 } else if (!strcasecmp(v->name, "autoframing")) {
19355 global_autoframing = ast_true(v->value);
19356 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
19357 allow_external_domains = ast_true(v->value);
19358 } else if (!strcasecmp(v->name, "autodomain")) {
19359 auto_sip_domains = ast_true(v->value);
19360 } else if (!strcasecmp(v->name, "domain")) {
19361 char *domain = ast_strdupa(v->value);
19362 char *context = strchr(domain, ',');
19363
19364 if (context)
19365 *context++ = '\0';
19366
19367 if (option_debug && ast_strlen_zero(context))
19368 ast_log(LOG_DEBUG, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
19369 if (ast_strlen_zero(domain))
19370 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
19371 else
19372 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, context ? ast_strip(context) : "");
19373 } else if (!strcasecmp(v->name, "register")) {
19374 if (sip_register(v->value, v->lineno) == 0)
19375 registry_count++;
19376 } else if (!strcasecmp(v->name, "tos")) {
19377 if (!ast_str2tos(v->value, &temp_tos)) {
19378 global_tos_sip = temp_tos;
19379 global_tos_audio = temp_tos;
19380 global_tos_video = temp_tos;
19381 ast_log(LOG_WARNING, "tos value at line %d is deprecated. See doc/ip-tos.txt for more information.\n", v->lineno);
19382 } else
19383 ast_log(LOG_WARNING, "Invalid tos value at line %d, See doc/ip-tos.txt for more information.\n", v->lineno);
19384 } else if (!strcasecmp(v->name, "tos_sip")) {
19385 if (ast_str2tos(v->value, &global_tos_sip))
19386 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, recommended value is 'cs3'. See doc/ip-tos.txt.\n", v->lineno);
19387 } else if (!strcasecmp(v->name, "tos_audio")) {
19388 if (ast_str2tos(v->value, &global_tos_audio))
19389 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, recommended value is 'ef'. See doc/ip-tos.txt.\n", v->lineno);
19390 } else if (!strcasecmp(v->name, "tos_video")) {
19391 if (ast_str2tos(v->value, &global_tos_video))
19392 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, recommended value is 'af41'. See doc/ip-tos.txt.\n", v->lineno);
19393 } else if (!strcasecmp(v->name, "bindport")) {
19394 if (sscanf(v->value, "%5d", &ourport) == 1) {
19395 bindaddr.sin_port = htons(ourport);
19396 } else {
19397 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
19398 }
19399 } else if (!strcasecmp(v->name, "qualify")) {
19400 if (!strcasecmp(v->value, "no")) {
19401 default_qualify = 0;
19402 } else if (!strcasecmp(v->value, "yes")) {
19403 default_qualify = DEFAULT_MAXMS;
19404 } else if (sscanf(v->value, "%30d", &default_qualify) != 1) {
19405 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
19406 default_qualify = 0;
19407 }
19408 } else if (!strcasecmp(v->name, "callevents")) {
19409 global_callevents = ast_true(v->value);
19410 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
19411 default_maxcallbitrate = atoi(v->value);
19412 if (default_maxcallbitrate < 0)
19413 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
19414 } else if (!strcasecmp(v->name, "matchexterniplocally")) {
19415 global_matchexterniplocally = ast_true(v->value);
19416 } else if (!strcasecmp(v->name, "shrinkcallerid")) {
19417 if (ast_true(v->value)) {
19418 global_shrinkcallerid = 1;
19419 } else if (ast_false(v->value)) {
19420 global_shrinkcallerid = 0;
19421 } else {
19422 ast_log(LOG_WARNING, "shrinkcallerid value %s is not valid at line %d.\n", v->value, v->lineno);
19423 }
19424 }
19425 }
19426
19427 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
19428 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
19429 allow_external_domains = 1;
19430 }
19431
19432
19433 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
19434
19435 if (!strcasecmp(v->name, "auth"))
19436 authl = add_realm_authentication(authl, v->value, v->lineno);
19437 }
19438
19439 ucfg = ast_config_load("users.conf");
19440 if (ucfg) {
19441 struct ast_variable *gen;
19442 int genhassip, genregistersip;
19443 const char *hassip, *registersip;
19444
19445 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
19446 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
19447 gen = ast_variable_browse(ucfg, "general");
19448 cat = ast_category_browse(ucfg, NULL);
19449 while (cat) {
19450 if (strcasecmp(cat, "general")) {
19451 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
19452 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
19453 if (ast_true(hassip) || (!hassip && genhassip)) {
19454 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
19455 if (user) {
19456 ASTOBJ_CONTAINER_LINK(&userl,user);
19457 ASTOBJ_UNREF(user, sip_destroy_user);
19458 user_count++;
19459 }
19460 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0, 0);
19461 if (peer) {
19462 ast_device_state_changed("SIP/%s", peer->name);
19463 ASTOBJ_CONTAINER_LINK(&peerl,peer);
19464 ASTOBJ_UNREF(peer, sip_destroy_peer);
19465 peer_count++;
19466 }
19467 }
19468 if (ast_true(registersip) || (!registersip && genregistersip)) {
19469 char tmp[256];
19470 const char *host = ast_variable_retrieve(ucfg, cat, "host");
19471 const char *username = ast_variable_retrieve(ucfg, cat, "username");
19472 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
19473 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
19474 const char *authuser = ast_variable_retrieve(ucfg, cat, "authuser");
19475 if (!host)
19476 host = ast_variable_retrieve(ucfg, "general", "host");
19477 if (!username)
19478 username = ast_variable_retrieve(ucfg, "general", "username");
19479 if (!secret)
19480 secret = ast_variable_retrieve(ucfg, "general", "secret");
19481 if (!contact)
19482 contact = "s";
19483 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
19484 if (!ast_strlen_zero(secret)) {
19485 if (!ast_strlen_zero(authuser)) {
19486 snprintf(tmp, sizeof(tmp), "%s:%s:%s@%s/%s", username, secret, authuser, host, contact);
19487 } else {
19488 snprintf(tmp, sizeof(tmp), "%s:%s@%s/%s", username, secret, host, contact);
19489 }
19490 } else if (!ast_strlen_zero(authuser)) {
19491 snprintf(tmp, sizeof(tmp), "%s::%s@%s/%s", username, authuser, host, contact);
19492 } else {
19493 snprintf(tmp, sizeof(tmp), "%s@%s/%s", username, host, contact);
19494 }
19495 if (sip_register(tmp, 0) == 0)
19496 registry_count++;
19497 }
19498 }
19499 }
19500 cat = ast_category_browse(ucfg, cat);
19501 }
19502 ast_config_destroy(ucfg);
19503 }
19504
19505
19506
19507 cat = NULL;
19508 while ( (cat = ast_category_browse(cfg, cat)) ) {
19509 const char *utype;
19510 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
19511 continue;
19512 utype = ast_variable_retrieve(cfg, cat, "type");
19513 if (!utype) {
19514 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
19515 continue;
19516 } else {
19517 int is_user = 0, is_peer = 0;
19518 if (!strcasecmp(utype, "user"))
19519 is_user = 1;
19520 else if (!strcasecmp(utype, "friend"))
19521 is_user = is_peer = 1;
19522 else if (!strcasecmp(utype, "peer"))
19523 is_peer = 1;
19524 else {
19525 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
19526 continue;
19527 }
19528 if (is_user) {
19529 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
19530 if (user) {
19531 ASTOBJ_CONTAINER_LINK(&userl,user);
19532 ASTOBJ_UNREF(user, sip_destroy_user);
19533 user_count++;
19534 }
19535 }
19536 if (is_peer) {
19537 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0, 0);
19538 if (peer) {
19539 ASTOBJ_CONTAINER_LINK(&peerl,peer);
19540 ASTOBJ_UNREF(peer, sip_destroy_peer);
19541 peer_count++;
19542 }
19543 }
19544 }
19545 }
19546 if (ast_find_ourip(&__ourip, bindaddr)) {
19547 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
19548 ast_config_destroy(cfg);
19549 return 0;
19550 }
19551 if (!ntohs(bindaddr.sin_port))
19552 bindaddr.sin_port = ntohs(STANDARD_SIP_PORT);
19553 bindaddr.sin_family = AF_INET;
19554 ast_mutex_lock(&netlock);
19555 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
19556 close(sipsock);
19557 sipsock = -1;
19558 }
19559 if (sipsock < 0) {
19560 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
19561 if (sipsock < 0) {
19562 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
19563 ast_config_destroy(cfg);
19564 ast_mutex_unlock(&netlock);
19565 return -1;
19566 } else {
19567
19568 const int reuseFlag = 1;
19569
19570 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
19571 (const char*)&reuseFlag,
19572 sizeof reuseFlag);
19573
19574 ast_enable_packet_fragmentation(sipsock);
19575
19576 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
19577 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
19578 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
19579 strerror(errno));
19580 close(sipsock);
19581 sipsock = -1;
19582 } else {
19583 if (option_verbose > 1) {
19584 ast_verbose(VERBOSE_PREFIX_2 "SIP Listening on %s:%d\n",
19585 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
19586 ast_verbose(VERBOSE_PREFIX_2 "Using SIP TOS: %s\n", ast_tos2str(global_tos_sip));
19587 }
19588 if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &global_tos_sip, sizeof(global_tos_sip)))
19589 ast_log(LOG_WARNING, "Unable to set SIP TOS to %s\n", ast_tos2str(global_tos_sip));
19590 }
19591 }
19592 } else if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &global_tos_sip, sizeof(global_tos_sip))) {
19593 ast_log(LOG_WARNING, "Unable to set SIP TOS to %s\n", ast_tos2str(global_tos_sip));
19594 }
19595 ast_mutex_unlock(&netlock);
19596
19597
19598
19599
19600
19601 if (auto_sip_domains) {
19602 char temp[MAXHOSTNAMELEN];
19603
19604
19605 if (bindaddr.sin_addr.s_addr)
19606 add_sip_domain(ast_inet_ntoa(bindaddr.sin_addr), SIP_DOMAIN_AUTO, NULL);
19607 else
19608 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
19609
19610
19611 if (externip.sin_addr.s_addr)
19612 add_sip_domain(ast_inet_ntoa(externip.sin_addr), SIP_DOMAIN_AUTO, NULL);
19613
19614
19615 if (!ast_strlen_zero(externhost))
19616 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
19617
19618
19619 if (!gethostname(temp, sizeof(temp)))
19620 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
19621 }
19622
19623
19624 ast_config_destroy(cfg);
19625
19626
19627 if (notify_types)
19628 ast_config_destroy(notify_types);
19629 notify_types = ast_config_load(notify_config);
19630
19631
19632 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);
19633
19634 return 0;
19635 }
19636
19637 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
19638 {
19639 struct sip_pvt *p;
19640 struct ast_udptl *udptl = NULL;
19641
19642 p = chan->tech_pvt;
19643 if (!p)
19644 return NULL;
19645
19646 ast_mutex_lock(&p->lock);
19647 if (p->udptl && ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
19648 udptl = p->udptl;
19649 ast_mutex_unlock(&p->lock);
19650 return udptl;
19651 }
19652
19653 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
19654 {
19655 struct sip_pvt *p;
19656
19657 p = chan->tech_pvt;
19658 if (!p)
19659 return -1;
19660 ast_mutex_lock(&p->lock);
19661 if (udptl)
19662 ast_udptl_get_peer(udptl, &p->udptlredirip);
19663 else
19664 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
19665 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
19666 if (!p->pendinginvite) {
19667 if (option_debug > 2) {
19668 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);
19669 }
19670 transmit_reinvite_with_t38_sdp(p);
19671 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
19672 if (option_debug > 2) {
19673 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);
19674 }
19675 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
19676 }
19677 }
19678
19679 p->lastrtprx = p->lastrtptx = time(NULL);
19680 ast_mutex_unlock(&p->lock);
19681 return 0;
19682 }
19683
19684
19685
19686
19687
19688
19689 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite)
19690 {
19691 struct sip_pvt *p;
19692 int flag = 0;
19693
19694 p = chan->tech_pvt;
19695 if (!p || !pvt->udptl)
19696 return -1;
19697
19698
19699 ast_mutex_lock(&p->lock);
19700
19701
19702
19703 p->t38.jointcapability = p->t38.peercapability = pvt->t38.jointcapability;
19704
19705 ast_udptl_set_far_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
19706 ast_udptl_set_local_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
19707 ast_udptl_set_error_correction_scheme(p->udptl, ast_udptl_get_error_correction_scheme(pvt->udptl));
19708
19709 if (reinvite) {
19710
19711
19712
19713
19714
19715 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
19716 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
19717 flag =1;
19718 } else {
19719 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
19720 }
19721 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
19722 if (!p->pendinginvite) {
19723 if (option_debug > 2) {
19724 if (flag)
19725 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));
19726 else
19727 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));
19728 }
19729 transmit_reinvite_with_t38_sdp(p);
19730 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
19731 if (option_debug > 2) {
19732 if (flag)
19733 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));
19734 else
19735 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));
19736 }
19737 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
19738 }
19739 }
19740
19741 p->lastrtprx = p->lastrtptx = time(NULL);
19742 ast_mutex_unlock(&p->lock);
19743 return 0;
19744 } else if (pvt->t38.state != T38_DISABLED) {
19745 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
19746 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
19747 flag = 1;
19748 } else {
19749 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
19750 }
19751 if (option_debug > 2) {
19752 if (flag)
19753 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));
19754 else
19755 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));
19756 }
19757 pvt->t38.state = T38_ENABLED;
19758 p->t38.state = T38_ENABLED;
19759 if (option_debug > 1) {
19760 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", pvt->t38.state, pvt->owner ? pvt->owner->name : "<none>");
19761 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
19762 }
19763 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
19764 p->lastrtprx = p->lastrtptx = time(NULL);
19765 ast_mutex_unlock(&p->lock);
19766 return 0;
19767 } else if (pvt->t38.state == T38_DISABLED) {
19768 p->t38.state = T38_DISABLED;
19769 if (option_debug > 1) {
19770 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", pvt->t38.state, pvt->owner ? pvt->owner->name : "<none>");
19771 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
19772 }
19773 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
19774 p->lastrtprx = p->lastrtptx = time(NULL);
19775 ast_mutex_unlock(&p->lock);
19776 return 0;
19777 } else {
19778 ast_log(LOG_ERROR, "Something went wrong with T.38. State is:%d on channel %s and %d on channel %s\n", pvt->t38.state, pvt->owner ? pvt->owner->name : "<none>", p->t38.state, chan ? chan->name : "<none>");
19779 ast_mutex_unlock(&p->lock);
19780 return 0;
19781 }
19782 }
19783
19784
19785 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
19786 {
19787 struct sip_pvt *p = NULL;
19788 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
19789
19790 if (!(p = chan->tech_pvt))
19791 return AST_RTP_GET_FAILED;
19792
19793 ast_mutex_lock(&p->lock);
19794 if (!(p->rtp)) {
19795 ast_mutex_unlock(&p->lock);
19796 return AST_RTP_GET_FAILED;
19797 }
19798
19799 *rtp = p->rtp;
19800
19801 if (ast_rtp_getnat(*rtp) && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT))
19802 res = AST_RTP_TRY_PARTIAL;
19803 else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
19804 res = AST_RTP_TRY_NATIVE;
19805 else if (ast_test_flag(&global_jbconf, AST_JB_FORCED))
19806 res = AST_RTP_GET_FAILED;
19807
19808 ast_mutex_unlock(&p->lock);
19809
19810 return res;
19811 }
19812
19813
19814 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
19815 {
19816 struct sip_pvt *p = NULL;
19817 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
19818
19819 if (!(p = chan->tech_pvt))
19820 return AST_RTP_GET_FAILED;
19821
19822 ast_mutex_lock(&p->lock);
19823 if (!(p->vrtp)) {
19824 ast_mutex_unlock(&p->lock);
19825 return AST_RTP_GET_FAILED;
19826 }
19827
19828 *rtp = p->vrtp;
19829
19830 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
19831 res = AST_RTP_TRY_NATIVE;
19832
19833 ast_mutex_unlock(&p->lock);
19834
19835 return res;
19836 }
19837
19838
19839 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
19840 {
19841 struct sip_pvt *p;
19842 int changed = 0;
19843
19844 p = chan->tech_pvt;
19845 if (!p)
19846 return -1;
19847
19848
19849 if (!ast_bridged_channel(chan) && !global_directrtpsetup)
19850 return 0;
19851
19852 ast_mutex_lock(&p->lock);
19853 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
19854
19855 ast_mutex_unlock(&p->lock);
19856 return 0;
19857 }
19858
19859
19860
19861
19862 if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
19863 ast_mutex_unlock(&p->lock);
19864 return 0;
19865 }
19866
19867 if (rtp) {
19868 changed |= ast_rtp_get_peer(rtp, &p->redirip);
19869 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
19870 memset(&p->redirip, 0, sizeof(p->redirip));
19871 changed = 1;
19872 }
19873 if (vrtp) {
19874 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
19875 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
19876 memset(&p->vredirip, 0, sizeof(p->vredirip));
19877 changed = 1;
19878 }
19879 if (codecs) {
19880 if (p->redircodecs != codecs && (p->jointcapability & codecs) != p->jointcapability) {
19881 p->redircodecs = codecs;
19882 p->jointcapability &= codecs;
19883 p->capability &= codecs;
19884 changed = 1;
19885 }
19886 }
19887 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
19888 if (chan->_state != AST_STATE_UP) {
19889 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
19890 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
19891 if (option_debug)
19892 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));
19893 } else if (!p->pendinginvite) {
19894 if (option_debug > 2) {
19895 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));
19896 }
19897 transmit_reinvite_with_sdp(p);
19898 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
19899 if (option_debug > 2) {
19900 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));
19901 }
19902
19903 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
19904 }
19905 }
19906
19907 p->lastrtprx = p->lastrtptx = time(NULL);
19908 ast_mutex_unlock(&p->lock);
19909 return 0;
19910 }
19911
19912 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
19913 static char *descrip_dtmfmode = "SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
19914 static char *app_dtmfmode = "SIPDtmfMode";
19915
19916 static char *app_sipaddheader = "SIPAddHeader";
19917 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
19918
19919 static char *descrip_sipaddheader = ""
19920 " SIPAddHeader(Header: Content)\n"
19921 "Adds a header to a SIP call placed with DIAL.\n"
19922 "Remember to user the X-header if you are adding non-standard SIP\n"
19923 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
19924 "Adding the wrong headers may jeopardize the SIP dialog.\n"
19925 "Always returns 0\n";
19926
19927
19928
19929 static int sip_dtmfmode(struct ast_channel *chan, void *data)
19930 {
19931 struct sip_pvt *p;
19932 char *mode;
19933 if (data)
19934 mode = (char *)data;
19935 else {
19936 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
19937 return 0;
19938 }
19939 ast_channel_lock(chan);
19940 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
19941 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
19942 ast_channel_unlock(chan);
19943 return 0;
19944 }
19945 p = chan->tech_pvt;
19946 if (!p) {
19947 ast_channel_unlock(chan);
19948 return 0;
19949 }
19950 ast_mutex_lock(&p->lock);
19951 if (!strcasecmp(mode,"info")) {
19952 ast_clear_flag(&p->flags[0], SIP_DTMF);
19953 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
19954 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
19955 } else if (!strcasecmp(mode,"rfc2833")) {
19956 ast_clear_flag(&p->flags[0], SIP_DTMF);
19957 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
19958 p->jointnoncodeccapability |= AST_RTP_DTMF;
19959 } else if (!strcasecmp(mode,"inband")) {
19960 ast_clear_flag(&p->flags[0], SIP_DTMF);
19961 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
19962 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
19963 } else
19964 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n",mode);
19965 if (p->rtp)
19966 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
19967 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
19968 if (!p->vad) {
19969 p->vad = ast_dsp_new();
19970 ast_dsp_set_features(p->vad, DSP_FEATURE_DTMF_DETECT);
19971 }
19972 } else {
19973 if (p->vad) {
19974 ast_dsp_free(p->vad);
19975 p->vad = NULL;
19976 }
19977 }
19978 ast_mutex_unlock(&p->lock);
19979 ast_channel_unlock(chan);
19980 return 0;
19981 }
19982
19983
19984 static int sip_addheader(struct ast_channel *chan, void *data)
19985 {
19986 int no = 0;
19987 int ok = FALSE;
19988 char varbuf[30];
19989 char *inbuf = (char *) data;
19990
19991 if (ast_strlen_zero(inbuf)) {
19992 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
19993 return 0;
19994 }
19995 ast_channel_lock(chan);
19996
19997
19998 while (!ok && no <= 50) {
19999 no++;
20000 snprintf(varbuf, sizeof(varbuf), "__SIPADDHEADER%.2d", no);
20001
20002
20003 if( (pbx_builtin_getvar_helper(chan, (const char *) varbuf + 2) == (const char *) NULL) )
20004 ok = TRUE;
20005 }
20006 if (ok) {
20007 pbx_builtin_setvar_helper (chan, varbuf, inbuf);
20008 if (sipdebug)
20009 ast_log(LOG_DEBUG,"SIP Header added \"%s\" as %s\n", inbuf, varbuf);
20010 } else {
20011 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
20012 }
20013 ast_channel_unlock(chan);
20014 return 0;
20015 }
20016
20017
20018
20019
20020
20021
20022
20023 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
20024 {
20025 char *cdest;
20026 char *extension, *host, *port;
20027 char tmp[80];
20028
20029 cdest = ast_strdupa(dest);
20030
20031 extension = strsep(&cdest, "@");
20032 host = strsep(&cdest, ":");
20033 port = strsep(&cdest, ":");
20034 if (ast_strlen_zero(extension)) {
20035 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
20036 return 0;
20037 }
20038
20039
20040 if (!host) {
20041 char *localtmp;
20042 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
20043 if (ast_strlen_zero(tmp)) {
20044 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
20045 return 0;
20046 }
20047 if ((localtmp = strcasestr(tmp, "sip:")) && (localtmp = strchr(localtmp, '@'))) {
20048 char lhost[80], lport[80];
20049 memset(lhost, 0, sizeof(lhost));
20050 memset(lport, 0, sizeof(lport));
20051 localtmp++;
20052
20053 sscanf(localtmp, "%80[^<>:; ]:%80[^<>:; ]", lhost, lport);
20054 if (ast_strlen_zero(lhost)) {
20055 ast_log(LOG_ERROR, "Can't find the host address\n");
20056 return 0;
20057 }
20058 host = ast_strdupa(lhost);
20059 if (!ast_strlen_zero(lport)) {
20060 port = ast_strdupa(lport);
20061 }
20062 }
20063 }
20064
20065 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
20066 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
20067
20068 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
20069 sip_alreadygone(p);
20070 return 0;
20071 }
20072
20073
20074 static int sip_get_codec(struct ast_channel *chan)
20075 {
20076 struct sip_pvt *p = chan->tech_pvt;
20077 return p->jointcapability ? p->jointcapability : p->capability;
20078 }
20079
20080
20081
20082
20083
20084 static void sip_poke_all_peers(void)
20085 {
20086 int ms = 0;
20087
20088 if (!speerobjs)
20089 return;
20090
20091 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
20092 ASTOBJ_WRLOCK(iterator);
20093 if (!AST_SCHED_DEL(sched, iterator->pokeexpire)) {
20094 struct sip_peer *peer_ptr = iterator;
20095 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
20096 }
20097 ms += 100;
20098 iterator->pokeexpire = ast_sched_add(sched, ms, sip_poke_peer_s, ASTOBJ_REF(iterator));
20099 if (iterator->pokeexpire == -1) {
20100 struct sip_peer *peer_ptr = iterator;
20101 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
20102 }
20103 ASTOBJ_UNLOCK(iterator);
20104 } while (0)
20105 );
20106 }
20107
20108
20109 static void sip_send_all_registers(void)
20110 {
20111 int ms;
20112 int regspacing;
20113 if (!regobjs)
20114 return;
20115 regspacing = default_expiry * 1000/regobjs;
20116 if (regspacing > 100)
20117 regspacing = 100;
20118 ms = regspacing;
20119 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
20120 ASTOBJ_WRLOCK(iterator);
20121 AST_SCHED_DEL(sched, iterator->expire);
20122 ms += regspacing;
20123 iterator->expire = ast_sched_add(sched, ms, sip_reregister, iterator);
20124 ASTOBJ_UNLOCK(iterator);
20125 } while (0)
20126 );
20127 }
20128
20129
20130 static int sip_do_reload(enum channelreloadreason reason)
20131 {
20132 reload_config(reason);
20133
20134
20135 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
20136 ASTOBJ_RDLOCK(iterator);
20137 if (ast_test_flag(&iterator->flags[0], SIP_REALTIME)) {
20138 if (!AST_SCHED_DEL(sched, iterator->pokeexpire)) {
20139 struct sip_peer *peer_ptr = iterator;
20140 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
20141 }
20142 }
20143 ASTOBJ_UNLOCK(iterator);
20144 } while (0) );
20145
20146
20147 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
20148 if (option_debug > 3)
20149 ast_log(LOG_DEBUG, "--------------- Done destroying pruned peers\n");
20150
20151
20152 sip_poke_all_peers();
20153
20154
20155 sip_send_all_registers();
20156
20157 if (option_debug > 3)
20158 ast_log(LOG_DEBUG, "--------------- SIP reload done\n");
20159
20160 return 0;
20161 }
20162
20163
20164 static int sip_reload(int fd, int argc, char *argv[])
20165 {
20166 ast_mutex_lock(&sip_reload_lock);
20167 if (sip_reloading)
20168 ast_verbose("Previous SIP reload not yet done\n");
20169 else {
20170 sip_reloading = TRUE;
20171 if (fd)
20172 sip_reloadreason = CHANNEL_CLI_RELOAD;
20173 else
20174 sip_reloadreason = CHANNEL_MODULE_RELOAD;
20175 }
20176 ast_mutex_unlock(&sip_reload_lock);
20177 restart_monitor();
20178
20179 return 0;
20180 }
20181
20182
20183 static int reload(void)
20184 {
20185 return sip_reload(0, 0, NULL);
20186 }
20187
20188 static struct ast_cli_entry cli_sip_debug_deprecated =
20189 { { "sip", "debug", NULL },
20190 sip_do_debug_deprecated, "Enable SIP debugging",
20191 debug_usage };
20192
20193 static struct ast_cli_entry cli_sip_no_debug_deprecated =
20194 { { "sip", "no", "debug", NULL },
20195 sip_no_debug_deprecated, "Disable SIP debugging",
20196 debug_usage };
20197
20198 static struct ast_cli_entry cli_sip[] = {
20199 { { "sip", "show", "channels", NULL },
20200 sip_show_channels, "List active SIP channels",
20201 show_channels_usage },
20202
20203 { { "sip", "show", "domains", NULL },
20204 sip_show_domains, "List our local SIP domains.",
20205 show_domains_usage },
20206
20207 { { "sip", "show", "inuse", NULL },
20208 sip_show_inuse, "List all inuse/limits",
20209 show_inuse_usage },
20210
20211 { { "sip", "show", "objects", NULL },
20212 sip_show_objects, "List all SIP object allocations",
20213 show_objects_usage },
20214
20215 { { "sip", "show", "peers", NULL },
20216 sip_show_peers, "List defined SIP peers",
20217 show_peers_usage },
20218
20219 { { "sip", "show", "registry", NULL },
20220 sip_show_registry, "List SIP registration status",
20221 show_reg_usage },
20222
20223 { { "sip", "show", "settings", NULL },
20224 sip_show_settings, "Show SIP global settings",
20225 show_settings_usage },
20226
20227 { { "sip", "show", "subscriptions", NULL },
20228 sip_show_subscriptions, "List active SIP subscriptions",
20229 show_subscriptions_usage },
20230
20231 { { "sip", "show", "users", NULL },
20232 sip_show_users, "List defined SIP users",
20233 show_users_usage },
20234
20235 { { "sip", "notify", NULL },
20236 sip_notify, "Send a notify packet to a SIP peer",
20237 notify_usage, complete_sipnotify },
20238
20239 { { "sip", "show", "channel", NULL },
20240 sip_show_channel, "Show detailed SIP channel info",
20241 show_channel_usage, complete_sipch },
20242
20243 { { "sip", "show", "history", NULL },
20244 sip_show_history, "Show SIP dialog history",
20245 show_history_usage, complete_sipch },
20246
20247 { { "sip", "show", "peer", NULL },
20248 sip_show_peer, "Show details on specific SIP peer",
20249 show_peer_usage, complete_sip_show_peer },
20250
20251 { { "sip", "show", "user", NULL },
20252 sip_show_user, "Show details on specific SIP user",
20253 show_user_usage, complete_sip_show_user },
20254
20255 { { "sip", "prune", "realtime", NULL },
20256 sip_prune_realtime, "Prune cached Realtime object(s)",
20257 prune_realtime_usage },
20258
20259 { { "sip", "prune", "realtime", "peer", NULL },
20260 sip_prune_realtime, "Prune cached Realtime peer(s)",
20261 prune_realtime_usage, complete_sip_prune_realtime_peer },
20262
20263 { { "sip", "prune", "realtime", "user", NULL },
20264 sip_prune_realtime, "Prune cached Realtime user(s)",
20265 prune_realtime_usage, complete_sip_prune_realtime_user },
20266
20267 { { "sip", "set", "debug", NULL },
20268 sip_do_debug, "Enable SIP debugging",
20269 debug_usage, NULL, &cli_sip_debug_deprecated },
20270
20271 { { "sip", "set", "debug", "ip", NULL },
20272 sip_do_debug, "Enable SIP debugging on IP",
20273 debug_usage },
20274
20275 { { "sip", "set", "debug", "peer", NULL },
20276 sip_do_debug, "Enable SIP debugging on Peername",
20277 debug_usage, complete_sip_debug_peer },
20278
20279 { { "sip", "set", "debug", "off", NULL },
20280 sip_no_debug, "Disable SIP debugging",
20281 no_debug_usage, NULL, &cli_sip_no_debug_deprecated },
20282
20283 { { "sip", "history", NULL },
20284 sip_do_history, "Enable SIP history",
20285 history_usage },
20286
20287 { { "sip", "history", "off", NULL },
20288 sip_no_history, "Disable SIP history",
20289 no_history_usage },
20290
20291 { { "sip", "reload", NULL },
20292 sip_reload, "Reload SIP configuration",
20293 sip_reload_usage },
20294 };
20295
20296
20297 static int load_module(void)
20298 {
20299 ASTOBJ_CONTAINER_INIT(&userl);
20300 ASTOBJ_CONTAINER_INIT(&peerl);
20301 ASTOBJ_CONTAINER_INIT(®l);
20302
20303 if (!(sched = sched_context_create())) {
20304 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
20305 return AST_MODULE_LOAD_FAILURE;
20306 }
20307
20308 if (!(io = io_context_create())) {
20309 ast_log(LOG_ERROR, "Unable to create I/O context\n");
20310 sched_context_destroy(sched);
20311 return AST_MODULE_LOAD_FAILURE;
20312 }
20313
20314 sip_reloadreason = CHANNEL_MODULE_LOAD;
20315
20316 if(reload_config(sip_reloadreason))
20317 return AST_MODULE_LOAD_DECLINE;
20318
20319
20320 if (ast_channel_register(&sip_tech)) {
20321 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
20322 io_context_destroy(io);
20323 sched_context_destroy(sched);
20324 return AST_MODULE_LOAD_FAILURE;
20325 }
20326
20327
20328 ast_cli_register_multiple(cli_sip, sizeof(cli_sip)/ sizeof(struct ast_cli_entry));
20329
20330
20331 ast_rtp_proto_register(&sip_rtp);
20332
20333
20334 ast_udptl_proto_register(&sip_udptl);
20335
20336
20337 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
20338 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
20339
20340
20341 ast_custom_function_register(&sip_header_function);
20342 ast_custom_function_register(&sippeer_function);
20343 ast_custom_function_register(&sipchaninfo_function);
20344 ast_custom_function_register(&checksipdomain_function);
20345
20346
20347 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM, manager_sip_show_peers,
20348 "List SIP peers (text format)", mandescr_show_peers);
20349 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM, manager_sip_show_peer,
20350 "Show SIP peer (text format)", mandescr_show_peer);
20351
20352 sip_poke_all_peers();
20353 sip_send_all_registers();
20354
20355
20356 restart_monitor();
20357
20358 return AST_MODULE_LOAD_SUCCESS;
20359 }
20360
20361
20362 static int unload_module(void)
20363 {
20364 struct sip_pvt *p, *pl;
20365 struct sip_extenstate_update *update;
20366
20367
20368 ast_channel_unregister(&sip_tech);
20369
20370
20371 ast_custom_function_unregister(&sipchaninfo_function);
20372 ast_custom_function_unregister(&sippeer_function);
20373 ast_custom_function_unregister(&sip_header_function);
20374 ast_custom_function_unregister(&checksipdomain_function);
20375
20376
20377 ast_unregister_application(app_dtmfmode);
20378 ast_unregister_application(app_sipaddheader);
20379
20380
20381 ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
20382
20383
20384 ast_rtp_proto_unregister(&sip_rtp);
20385
20386
20387 ast_udptl_proto_unregister(&sip_udptl);
20388
20389
20390 ast_manager_unregister("SIPpeers");
20391 ast_manager_unregister("SIPshowpeer");
20392
20393 ast_mutex_lock(&iflock);
20394
20395 for (p = iflist; p ; p = p->next) {
20396 if (p->owner)
20397 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
20398 }
20399 ast_mutex_unlock(&iflock);
20400
20401 ast_mutex_lock(&monlock);
20402 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
20403 pthread_cancel(monitor_thread);
20404 pthread_kill(monitor_thread, SIGURG);
20405 pthread_join(monitor_thread, NULL);
20406 }
20407 monitor_thread = AST_PTHREADT_STOP;
20408 ast_mutex_unlock(&monlock);
20409
20410 restartdestroy:
20411 ast_mutex_lock(&iflock);
20412
20413 p = iflist;
20414 while (p) {
20415 pl = p;
20416 p = p->next;
20417 if (__sip_destroy(pl, TRUE) < 0) {
20418
20419 iflist = p;
20420 ast_mutex_unlock(&iflock);
20421 usleep(1);
20422 goto restartdestroy;
20423 }
20424 }
20425 iflist = NULL;
20426 ast_mutex_unlock(&iflock);
20427
20428
20429 ast_free_ha(localaddr);
20430
20431 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
20432 ASTOBJ_CONTAINER_DESTROY(&userl);
20433 ASTOBJ_CONTAINER_DESTROYALL(&peerl, sip_destroy_peer);
20434 ASTOBJ_CONTAINER_DESTROY(&peerl);
20435 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
20436 ASTOBJ_CONTAINER_DESTROY(®l);
20437
20438 AST_LIST_LOCK(&sip_extenstate_updates);
20439 AST_LIST_TRAVERSE_SAFE_BEGIN(&sip_extenstate_updates, update, list) {
20440 AST_LIST_REMOVE_CURRENT(&sip_extenstate_updates, list);
20441 ast_free(update);
20442 }
20443 AST_LIST_TRAVERSE_SAFE_END
20444 AST_LIST_UNLOCK(&sip_extenstate_updates);
20445
20446 clear_realm_authentication(authl);
20447 clear_sip_domains();
20448 ast_free_ha(global_contact_ha);
20449 close(sipsock);
20450 sched_context_destroy(sched);
20451
20452 return 0;
20453 }
20454
20455 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)",
20456 .load = load_module,
20457 .unload = unload_module,
20458 .reload = reload,
20459 );