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: 119926 $")
00098
00099 #include <stdio.h>
00100 #include <ctype.h>
00101 #include <string.h>
00102 #include <unistd.h>
00103 #include <sys/socket.h>
00104 #include <sys/ioctl.h>
00105 #include <net/if.h>
00106 #include <errno.h>
00107 #include <stdlib.h>
00108 #include <fcntl.h>
00109 #include <netdb.h>
00110 #include <signal.h>
00111 #include <sys/signal.h>
00112 #include <netinet/in.h>
00113 #include <netinet/in_systm.h>
00114 #include <arpa/inet.h>
00115 #include <netinet/ip.h>
00116 #include <regex.h>
00117
00118 #include "asterisk/lock.h"
00119 #include "asterisk/channel.h"
00120 #include "asterisk/config.h"
00121 #include "asterisk/logger.h"
00122 #include "asterisk/module.h"
00123 #include "asterisk/pbx.h"
00124 #include "asterisk/options.h"
00125 #include "asterisk/sched.h"
00126 #include "asterisk/io.h"
00127 #include "asterisk/rtp.h"
00128 #include "asterisk/udptl.h"
00129 #include "asterisk/acl.h"
00130 #include "asterisk/manager.h"
00131 #include "asterisk/callerid.h"
00132 #include "asterisk/cli.h"
00133 #include "asterisk/app.h"
00134 #include "asterisk/musiconhold.h"
00135 #include "asterisk/dsp.h"
00136 #include "asterisk/features.h"
00137 #include "asterisk/srv.h"
00138 #include "asterisk/astdb.h"
00139 #include "asterisk/causes.h"
00140 #include "asterisk/utils.h"
00141 #include "asterisk/file.h"
00142 #include "asterisk/astobj.h"
00143 #include "asterisk/devicestate.h"
00144 #include "asterisk/linkedlists.h"
00145 #include "asterisk/stringfields.h"
00146 #include "asterisk/monitor.h"
00147 #include "asterisk/localtime.h"
00148 #include "asterisk/abstract_jb.h"
00149 #include "asterisk/compiler.h"
00150 #include "asterisk/threadstorage.h"
00151 #include "asterisk/translate.h"
00152
00153 #ifndef FALSE
00154 #define FALSE 0
00155 #endif
00156
00157 #ifndef TRUE
00158 #define TRUE 1
00159 #endif
00160
00161 #define SIPBUFSIZE 512
00162
00163 #define XMIT_ERROR -2
00164
00165 #define VIDEO_CODEC_MASK 0x1fc0000
00166 #ifndef IPTOS_MINCOST
00167 #define IPTOS_MINCOST 0x02
00168 #endif
00169
00170
00171
00172 #define DEFAULT_DEFAULT_EXPIRY 120
00173 #define DEFAULT_MIN_EXPIRY 60
00174 #define DEFAULT_MAX_EXPIRY 3600
00175 #define DEFAULT_REGISTRATION_TIMEOUT 20
00176 #define DEFAULT_MAX_FORWARDS "70"
00177
00178
00179
00180 #define EXPIRY_GUARD_SECS 15
00181 #define EXPIRY_GUARD_LIMIT 30
00182
00183 #define EXPIRY_GUARD_MIN 500
00184
00185
00186
00187 #define EXPIRY_GUARD_PCT 0.20
00188
00189 #define DEFAULT_EXPIRY 900
00190
00191 static int min_expiry = DEFAULT_MIN_EXPIRY;
00192 static int max_expiry = DEFAULT_MAX_EXPIRY;
00193 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00194 static int expiry = DEFAULT_EXPIRY;
00195
00196 #ifndef MAX
00197 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00198 #endif
00199
00200 #define CALLERID_UNKNOWN "Unknown"
00201
00202 #define DEFAULT_MAXMS 2000
00203 #define DEFAULT_FREQ_OK 60 * 1000
00204 #define DEFAULT_FREQ_NOTOK 10 * 1000
00205
00206 #define DEFAULT_RETRANS 1000
00207 #define MAX_RETRANS 6
00208 #define SIP_TRANS_TIMEOUT 32000
00209
00210
00211 #define DEFAULT_TRANS_TIMEOUT -1
00212 #define MAX_AUTHTRIES 3
00213
00214 #define SIP_MAX_HEADERS 64
00215 #define SIP_MAX_LINES 64
00216 #define SIP_MAX_PACKET 4096
00217
00218 #define SDP_MAX_RTPMAP_CODECS 32
00219
00220 #define INITIAL_CSEQ 101
00221
00222
00223 static struct ast_jb_conf default_jbconf =
00224 {
00225 .flags = 0,
00226 .max_size = -1,
00227 .resync_threshold = -1,
00228 .impl = ""
00229 };
00230 static struct ast_jb_conf global_jbconf;
00231
00232 static const char config[] = "sip.conf";
00233 static const char notify_config[] = "sip_notify.conf";
00234
00235 #define RTP 1
00236 #define NO_RTP 0
00237
00238
00239
00240
00241 enum transfermodes {
00242 TRANSFER_OPENFORALL,
00243 TRANSFER_CLOSED,
00244 };
00245
00246
00247 enum sip_result {
00248 AST_SUCCESS = 0,
00249 AST_FAILURE = -1,
00250 };
00251
00252
00253
00254
00255 enum invitestates {
00256 INV_NONE = 0,
00257 INV_CALLING = 1,
00258 INV_PROCEEDING = 2,
00259 INV_EARLY_MEDIA = 3,
00260 INV_COMPLETED = 4,
00261 INV_CONFIRMED = 5,
00262 INV_TERMINATED = 6,
00263
00264 INV_CANCELLED = 7,
00265 };
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 enum xmittype {
00276 XMIT_CRITICAL = 2,
00277
00278 XMIT_RELIABLE = 1,
00279 XMIT_UNRELIABLE = 0,
00280 };
00281
00282 enum parse_register_result {
00283 PARSE_REGISTER_FAILED,
00284 PARSE_REGISTER_UPDATE,
00285 PARSE_REGISTER_QUERY,
00286 };
00287
00288 enum subscriptiontype {
00289 NONE = 0,
00290 XPIDF_XML,
00291 DIALOG_INFO_XML,
00292 CPIM_PIDF_XML,
00293 PIDF_XML,
00294 MWI_NOTIFICATION
00295 };
00296
00297 static const struct cfsubscription_types {
00298 enum subscriptiontype type;
00299 const char * const event;
00300 const char * const mediatype;
00301 const char * const text;
00302 } subscription_types[] = {
00303 { NONE, "-", "unknown", "unknown" },
00304
00305 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00306 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00307 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00308 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00309 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00310 };
00311
00312
00313 enum sipmethod {
00314 SIP_UNKNOWN,
00315 SIP_RESPONSE,
00316 SIP_REGISTER,
00317 SIP_OPTIONS,
00318 SIP_NOTIFY,
00319 SIP_INVITE,
00320 SIP_ACK,
00321 SIP_PRACK,
00322 SIP_BYE,
00323 SIP_REFER,
00324 SIP_SUBSCRIBE,
00325 SIP_MESSAGE,
00326 SIP_UPDATE,
00327 SIP_INFO,
00328 SIP_CANCEL,
00329 SIP_PUBLISH,
00330 SIP_PING,
00331 };
00332
00333
00334
00335
00336
00337
00338 enum sip_auth_type {
00339 PROXY_AUTH,
00340 WWW_AUTH,
00341 };
00342
00343
00344 enum check_auth_result {
00345 AUTH_SUCCESSFUL = 0,
00346 AUTH_CHALLENGE_SENT = 1,
00347 AUTH_SECRET_FAILED = -1,
00348 AUTH_USERNAME_MISMATCH = -2,
00349 AUTH_NOT_FOUND = -3,
00350 AUTH_FAKE_AUTH = -4,
00351 AUTH_UNKNOWN_DOMAIN = -5,
00352 AUTH_PEER_NOT_DYNAMIC = -6,
00353 AUTH_ACL_FAILED = -7,
00354 };
00355
00356
00357 enum sipregistrystate {
00358 REG_STATE_UNREGISTERED = 0,
00359 REG_STATE_REGSENT,
00360 REG_STATE_AUTHSENT,
00361 REG_STATE_REGISTERED,
00362 REG_STATE_REJECTED,
00363 REG_STATE_TIMEOUT,
00364 REG_STATE_NOAUTH,
00365 REG_STATE_FAILED,
00366 };
00367
00368 #define CAN_NOT_CREATE_DIALOG 0
00369 #define CAN_CREATE_DIALOG 1
00370 #define CAN_CREATE_DIALOG_UNSUPPORTED_METHOD 2
00371
00372
00373 static const struct cfsip_methods {
00374 enum sipmethod id;
00375 int need_rtp;
00376 char * const text;
00377 int can_create;
00378 } sip_methods[] = {
00379 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00380 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00381 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00382 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00383 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00384 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00385 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00386 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00387 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00388 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00389 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00390 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00391 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00392 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00393 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00394 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00395 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00396 };
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 #define SUPPORTED 1
00409 #define NOT_SUPPORTED 0
00410
00411 #define SIP_OPT_REPLACES (1 << 0)
00412 #define SIP_OPT_100REL (1 << 1)
00413 #define SIP_OPT_TIMER (1 << 2)
00414 #define SIP_OPT_EARLY_SESSION (1 << 3)
00415 #define SIP_OPT_JOIN (1 << 4)
00416 #define SIP_OPT_PATH (1 << 5)
00417 #define SIP_OPT_PREF (1 << 6)
00418 #define SIP_OPT_PRECONDITION (1 << 7)
00419 #define SIP_OPT_PRIVACY (1 << 8)
00420 #define SIP_OPT_SDP_ANAT (1 << 9)
00421 #define SIP_OPT_SEC_AGREE (1 << 10)
00422 #define SIP_OPT_EVENTLIST (1 << 11)
00423 #define SIP_OPT_GRUU (1 << 12)
00424 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00425 #define SIP_OPT_NOREFERSUB (1 << 14)
00426 #define SIP_OPT_HISTINFO (1 << 15)
00427 #define SIP_OPT_RESPRIORITY (1 << 16)
00428
00429
00430
00431 static const struct cfsip_options {
00432 int id;
00433 int supported;
00434 char * const text;
00435 } sip_options[] = {
00436
00437 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00438
00439 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00440
00441 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00442
00443 { SIP_OPT_TIMER, NOT_SUPPORTED, "timer" },
00444
00445 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00446
00447 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00448
00449 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00450
00451 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00452
00453 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00454
00455 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00456
00457 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00458
00459 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00460
00461 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00462
00463 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00464
00465 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00466
00467 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00468
00469 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00470
00471 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00472 };
00473
00474
00475
00476 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
00477
00478
00479 #define SUPPORTED_EXTENSIONS "replaces"
00480
00481
00482 #define STANDARD_SIP_PORT 5060
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 #define DEFAULT_CONTEXT "default"
00496 #define DEFAULT_MOHINTERPRET "default"
00497 #define DEFAULT_MOHSUGGEST ""
00498 #define DEFAULT_VMEXTEN "asterisk"
00499 #define DEFAULT_CALLERID "asterisk"
00500 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00501 #define DEFAULT_MWITIME 10
00502 #define DEFAULT_ALLOWGUEST TRUE
00503 #define DEFAULT_SRVLOOKUP TRUE
00504 #define DEFAULT_COMPACTHEADERS FALSE
00505 #define DEFAULT_TOS_SIP 0
00506 #define DEFAULT_TOS_AUDIO 0
00507 #define DEFAULT_TOS_VIDEO 0
00508 #define DEFAULT_ALLOW_EXT_DOM TRUE
00509 #define DEFAULT_REALM "asterisk"
00510 #define DEFAULT_NOTIFYRINGING TRUE
00511 #define DEFAULT_PEDANTIC FALSE
00512 #define DEFAULT_AUTOCREATEPEER FALSE
00513 #define DEFAULT_QUALIFY FALSE
00514 #define DEFAULT_T1MIN 100
00515 #define DEFAULT_MAX_CALL_BITRATE (384)
00516 #ifndef DEFAULT_USERAGENT
00517 #define DEFAULT_USERAGENT "Asterisk PBX"
00518 #endif
00519
00520
00521
00522
00523 static char default_context[AST_MAX_CONTEXT];
00524 static char default_subscribecontext[AST_MAX_CONTEXT];
00525 static char default_language[MAX_LANGUAGE];
00526 static char default_callerid[AST_MAX_EXTENSION];
00527 static char default_fromdomain[AST_MAX_EXTENSION];
00528 static char default_notifymime[AST_MAX_EXTENSION];
00529 static int default_qualify;
00530 static char default_vmexten[AST_MAX_EXTENSION];
00531 static char default_mohinterpret[MAX_MUSICCLASS];
00532 static char default_mohsuggest[MAX_MUSICCLASS];
00533
00534 static int default_maxcallbitrate;
00535 static struct ast_codec_pref default_prefs;
00536
00537
00538 static int global_directrtpsetup;
00539 static int global_limitonpeers;
00540 static int global_rtautoclear;
00541 static int global_notifyringing;
00542 static int global_notifyhold;
00543 static int global_alwaysauthreject;
00544 static int srvlookup;
00545 static int pedanticsipchecking;
00546 static int autocreatepeer;
00547 static int global_relaxdtmf;
00548 static int global_rtptimeout;
00549 static int global_rtpholdtimeout;
00550 static int global_rtpkeepalive;
00551 static int global_reg_timeout;
00552 static int global_regattempts_max;
00553 static int global_allowguest;
00554 static int global_allowsubscribe;
00555
00556 static int global_mwitime;
00557 static unsigned int global_tos_sip;
00558 static unsigned int global_tos_audio;
00559 static unsigned int global_tos_video;
00560 static int compactheaders;
00561 static int recordhistory;
00562 static int dumphistory;
00563 static char global_realm[MAXHOSTNAMELEN];
00564 static char global_regcontext[AST_MAX_CONTEXT];
00565 static char global_useragent[AST_MAX_EXTENSION];
00566 static int allow_external_domains;
00567 static int global_callevents;
00568 static int global_t1min;
00569 static int global_autoframing;
00570 static enum transfermodes global_allowtransfer;
00571
00572 static int global_matchexterniplocally;
00573
00574
00575 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00576
00577
00578 static int suserobjs = 0;
00579 static int ruserobjs = 0;
00580 static int speerobjs = 0;
00581 static int rpeerobjs = 0;
00582 static int apeerobjs = 0;
00583 static int regobjs = 0;
00584
00585 static struct ast_flags global_flags[2] = {{0}};
00586
00587
00588 AST_MUTEX_DEFINE_STATIC(iflock);
00589
00590
00591
00592 AST_MUTEX_DEFINE_STATIC(netlock);
00593
00594 AST_MUTEX_DEFINE_STATIC(monlock);
00595
00596 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00597
00598
00599
00600 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00601
00602 static int sip_reloading = FALSE;
00603 static enum channelreloadreason sip_reloadreason;
00604
00605 static struct sched_context *sched;
00606 static struct io_context *io;
00607 static int *sipsock_read_id;
00608
00609 #define DEC_CALL_LIMIT 0
00610 #define INC_CALL_LIMIT 1
00611 #define DEC_CALL_RINGING 2
00612 #define INC_CALL_RINGING 3
00613
00614
00615 struct sip_request {
00616 char *rlPart1;
00617 char *rlPart2;
00618 int len;
00619 int headers;
00620 int method;
00621 int lines;
00622 unsigned int flags;
00623 char *header[SIP_MAX_HEADERS];
00624 char *line[SIP_MAX_LINES];
00625 char data[SIP_MAX_PACKET];
00626 unsigned int sdp_start;
00627 unsigned int sdp_end;
00628 };
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650 struct sip_dual {
00651 struct ast_channel *chan1;
00652 struct ast_channel *chan2;
00653 struct sip_request req;
00654 int seqno;
00655 };
00656
00657 struct sip_pkt;
00658
00659
00660 struct sip_invite_param {
00661 const char *distinctive_ring;
00662 int addsipheaders;
00663 const char *uri_options;
00664 const char *vxml_url;
00665 char *auth;
00666 char *authheader;
00667 enum sip_auth_type auth_type;
00668 const char *replaces;
00669 int transfer;
00670 };
00671
00672
00673 struct sip_route {
00674 struct sip_route *next;
00675 char hop[0];
00676 };
00677
00678
00679 enum domain_mode {
00680 SIP_DOMAIN_AUTO,
00681 SIP_DOMAIN_CONFIG,
00682 };
00683
00684
00685
00686
00687
00688 struct domain {
00689 char domain[MAXHOSTNAMELEN];
00690 char context[AST_MAX_EXTENSION];
00691 enum domain_mode mode;
00692 AST_LIST_ENTRY(domain) list;
00693 };
00694
00695 static AST_LIST_HEAD_STATIC(domain_list, domain);
00696
00697
00698
00699 struct sip_history {
00700 AST_LIST_ENTRY(sip_history) list;
00701 char event[0];
00702 };
00703
00704 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00705
00706
00707 struct sip_auth {
00708 char realm[AST_MAX_EXTENSION];
00709 char username[256];
00710 char secret[256];
00711 char md5secret[256];
00712 struct sip_auth *next;
00713 };
00714
00715
00716 #define SIP_ALREADYGONE (1 << 0)
00717 #define SIP_NEEDDESTROY (1 << 1)
00718 #define SIP_NOVIDEO (1 << 2)
00719 #define SIP_RINGING (1 << 3)
00720 #define SIP_PROGRESS_SENT (1 << 4)
00721 #define SIP_NEEDREINVITE (1 << 5)
00722 #define SIP_PENDINGBYE (1 << 6)
00723 #define SIP_GOTREFER (1 << 7)
00724 #define SIP_PROMISCREDIR (1 << 8)
00725 #define SIP_TRUSTRPID (1 << 9)
00726 #define SIP_USEREQPHONE (1 << 10)
00727 #define SIP_REALTIME (1 << 11)
00728 #define SIP_USECLIENTCODE (1 << 12)
00729 #define SIP_OUTGOING (1 << 13)
00730 #define SIP_FREE_BIT (1 << 14)
00731 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 15)
00732 #define SIP_DTMF (3 << 16)
00733 #define SIP_DTMF_RFC2833 (0 << 16)
00734 #define SIP_DTMF_INBAND (1 << 16)
00735 #define SIP_DTMF_INFO (2 << 16)
00736 #define SIP_DTMF_AUTO (3 << 16)
00737
00738 #define SIP_NAT (3 << 18)
00739 #define SIP_NAT_NEVER (0 << 18)
00740 #define SIP_NAT_RFC3581 (1 << 18)
00741 #define SIP_NAT_ROUTE (2 << 18)
00742 #define SIP_NAT_ALWAYS (3 << 18)
00743
00744 #define SIP_REINVITE (7 << 20)
00745 #define SIP_CAN_REINVITE (1 << 20)
00746 #define SIP_CAN_REINVITE_NAT (2 << 20)
00747 #define SIP_REINVITE_UPDATE (4 << 20)
00748
00749 #define SIP_INSECURE_PORT (1 << 23)
00750 #define SIP_INSECURE_INVITE (1 << 24)
00751
00752 #define SIP_PROG_INBAND (3 << 25)
00753 #define SIP_PROG_INBAND_NEVER (0 << 25)
00754 #define SIP_PROG_INBAND_NO (1 << 25)
00755 #define SIP_PROG_INBAND_YES (2 << 25)
00756 #define SIP_NO_HISTORY (1 << 27)
00757 #define SIP_CALL_LIMIT (1 << 28)
00758 #define SIP_SENDRPID (1 << 29)
00759 #define SIP_INC_COUNT (1 << 30)
00760 #define SIP_G726_NONSTANDARD (1 << 31)
00761
00762 #define SIP_FLAGS_TO_COPY \
00763 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
00764 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
00765 SIP_USEREQPHONE | SIP_INSECURE_PORT | SIP_INSECURE_INVITE)
00766
00767
00768
00769 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
00770 #define SIP_PAGE2_RTUPDATE (1 << 1)
00771 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
00772 #define SIP_PAGE2_RT_FROMCONTACT (1 << 4)
00773 #define SIP_PAGE2_RTSAVE_SYSNAME (1 << 5)
00774
00775 #define SIP_PAGE2_STATECHANGEQUEUE (1 << 9)
00776 #define SIP_PAGE2_IGNOREREGEXPIRE (1 << 10)
00777 #define SIP_PAGE2_DEBUG (3 << 11)
00778 #define SIP_PAGE2_DEBUG_CONFIG (1 << 11)
00779 #define SIP_PAGE2_DEBUG_CONSOLE (1 << 12)
00780 #define SIP_PAGE2_DYNAMIC (1 << 13)
00781 #define SIP_PAGE2_SELFDESTRUCT (1 << 14)
00782 #define SIP_PAGE2_VIDEOSUPPORT (1 << 15)
00783 #define SIP_PAGE2_ALLOWSUBSCRIBE (1 << 16)
00784 #define SIP_PAGE2_ALLOWOVERLAP (1 << 17)
00785 #define SIP_PAGE2_SUBSCRIBEMWIONLY (1 << 18)
00786 #define SIP_PAGE2_INC_RINGING (1 << 19)
00787 #define SIP_PAGE2_T38SUPPORT (7 << 20)
00788 #define SIP_PAGE2_T38SUPPORT_UDPTL (1 << 20)
00789 #define SIP_PAGE2_T38SUPPORT_RTP (2 << 20)
00790 #define SIP_PAGE2_T38SUPPORT_TCP (4 << 20)
00791 #define SIP_PAGE2_CALL_ONHOLD (3 << 23)
00792 #define SIP_PAGE2_CALL_ONHOLD_ACTIVE (1 << 23)
00793 #define SIP_PAGE2_CALL_ONHOLD_ONEDIR (2 << 23)
00794 #define SIP_PAGE2_CALL_ONHOLD_INACTIVE (3 << 23)
00795 #define SIP_PAGE2_RFC2833_COMPENSATE (1 << 25)
00796 #define SIP_PAGE2_BUGGY_MWI (1 << 26)
00797 #define SIP_PAGE2_OUTGOING_CALL (1 << 27)
00798 #define SIP_PAGE2_UDPTL_DESTINATION (1 << 28)
00799
00800 #define SIP_PAGE2_FLAGS_TO_COPY \
00801 (SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_VIDEOSUPPORT | \
00802 SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | SIP_PAGE2_BUGGY_MWI | SIP_PAGE2_UDPTL_DESTINATION)
00803
00804
00805 #define SIP_PKT_DEBUG (1 << 0)
00806 #define SIP_PKT_WITH_TOTAG (1 << 1)
00807 #define SIP_PKT_IGNORE (1 << 2)
00808 #define SIP_PKT_IGNORE_RESP (1 << 3)
00809 #define SIP_PKT_IGNORE_REQ (1 << 4)
00810
00811
00812 #define T38FAX_FILL_BIT_REMOVAL (1 << 0)
00813 #define T38FAX_TRANSCODING_MMR (1 << 1)
00814 #define T38FAX_TRANSCODING_JBIG (1 << 2)
00815
00816 #define T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF (0 << 3)
00817 #define T38FAX_RATE_MANAGEMENT_LOCAL_TCF (1 << 3)
00818
00819 #define T38FAX_UDP_EC_NONE (0 << 4)
00820 #define T38FAX_UDP_EC_FEC (1 << 4)
00821 #define T38FAX_UDP_EC_REDUNDANCY (2 << 4)
00822
00823 #define T38FAX_VERSION (3 << 6)
00824 #define T38FAX_VERSION_0 (0 << 6)
00825 #define T38FAX_VERSION_1 (1 << 6)
00826
00827 #define T38FAX_RATE_2400 (1 << 8)
00828 #define T38FAX_RATE_4800 (1 << 9)
00829 #define T38FAX_RATE_7200 (1 << 10)
00830 #define T38FAX_RATE_9600 (1 << 11)
00831 #define T38FAX_RATE_12000 (1 << 12)
00832 #define T38FAX_RATE_14400 (1 << 13)
00833
00834
00835 static int global_t38_capability = T38FAX_VERSION_0 | T38FAX_RATE_2400 | T38FAX_RATE_4800 | T38FAX_RATE_7200 | T38FAX_RATE_9600;
00836
00837 #define sipdebug ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG)
00838 #define sipdebug_config ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG)
00839 #define sipdebug_console ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE)
00840
00841
00842 enum t38state {
00843 T38_DISABLED = 0,
00844 T38_LOCAL_DIRECT,
00845 T38_LOCAL_REINVITE,
00846 T38_PEER_DIRECT,
00847 T38_PEER_REINVITE,
00848 T38_ENABLED
00849 };
00850
00851
00852 struct t38properties {
00853 struct ast_flags t38support;
00854 int capability;
00855 int peercapability;
00856 int jointcapability;
00857 enum t38state state;
00858 };
00859
00860
00861 enum referstatus {
00862 REFER_IDLE,
00863 REFER_SENT,
00864 REFER_RECEIVED,
00865 REFER_CONFIRMED,
00866 REFER_ACCEPTED,
00867 REFER_RINGING,
00868 REFER_200OK,
00869 REFER_FAILED,
00870 REFER_NOAUTH
00871 };
00872
00873 static const struct c_referstatusstring {
00874 enum referstatus status;
00875 char *text;
00876 } referstatusstrings[] = {
00877 { REFER_IDLE, "<none>" },
00878 { REFER_SENT, "Request sent" },
00879 { REFER_RECEIVED, "Request received" },
00880 { REFER_ACCEPTED, "Accepted" },
00881 { REFER_RINGING, "Target ringing" },
00882 { REFER_200OK, "Done" },
00883 { REFER_FAILED, "Failed" },
00884 { REFER_NOAUTH, "Failed - auth failure" }
00885 } ;
00886
00887
00888
00889 struct sip_refer {
00890 char refer_to[AST_MAX_EXTENSION];
00891 char refer_to_domain[AST_MAX_EXTENSION];
00892 char refer_to_urioption[AST_MAX_EXTENSION];
00893 char refer_to_context[AST_MAX_EXTENSION];
00894 char referred_by[AST_MAX_EXTENSION];
00895 char referred_by_name[AST_MAX_EXTENSION];
00896 char refer_contact[AST_MAX_EXTENSION];
00897 char replaces_callid[SIPBUFSIZE];
00898 char replaces_callid_totag[SIPBUFSIZE/2];
00899 char replaces_callid_fromtag[SIPBUFSIZE/2];
00900 struct sip_pvt *refer_call;
00901 int attendedtransfer;
00902 int localtransfer;
00903 enum referstatus status;
00904 };
00905
00906
00907 static struct sip_pvt {
00908 ast_mutex_t lock;
00909 int method;
00910 enum invitestates invitestate;
00911 AST_DECLARE_STRING_FIELDS(
00912 AST_STRING_FIELD(callid);
00913 AST_STRING_FIELD(randdata);
00914 AST_STRING_FIELD(accountcode);
00915 AST_STRING_FIELD(realm);
00916 AST_STRING_FIELD(nonce);
00917 AST_STRING_FIELD(opaque);
00918 AST_STRING_FIELD(qop);
00919 AST_STRING_FIELD(domain);
00920 AST_STRING_FIELD(from);
00921 AST_STRING_FIELD(useragent);
00922 AST_STRING_FIELD(exten);
00923 AST_STRING_FIELD(context);
00924 AST_STRING_FIELD(subscribecontext);
00925 AST_STRING_FIELD(subscribeuri);
00926 AST_STRING_FIELD(fromdomain);
00927 AST_STRING_FIELD(fromuser);
00928 AST_STRING_FIELD(fromname);
00929 AST_STRING_FIELD(tohost);
00930 AST_STRING_FIELD(language);
00931 AST_STRING_FIELD(mohinterpret);
00932 AST_STRING_FIELD(mohsuggest);
00933 AST_STRING_FIELD(rdnis);
00934 AST_STRING_FIELD(theirtag);
00935 AST_STRING_FIELD(username);
00936 AST_STRING_FIELD(peername);
00937 AST_STRING_FIELD(authname);
00938 AST_STRING_FIELD(uri);
00939 AST_STRING_FIELD(okcontacturi);
00940 AST_STRING_FIELD(peersecret);
00941 AST_STRING_FIELD(peermd5secret);
00942 AST_STRING_FIELD(cid_num);
00943 AST_STRING_FIELD(cid_name);
00944 AST_STRING_FIELD(via);
00945 AST_STRING_FIELD(fullcontact);
00946 AST_STRING_FIELD(our_contact);
00947 AST_STRING_FIELD(rpid);
00948 AST_STRING_FIELD(rpid_from);
00949 );
00950 unsigned int ocseq;
00951 unsigned int icseq;
00952 ast_group_t callgroup;
00953 ast_group_t pickupgroup;
00954 int lastinvite;
00955 int lastnoninvite;
00956 struct ast_flags flags[2];
00957 int timer_t1;
00958 unsigned int sipoptions;
00959 struct ast_codec_pref prefs;
00960 int capability;
00961 int jointcapability;
00962 int peercapability;
00963 int prefcodec;
00964 int noncodeccapability;
00965 int jointnoncodeccapability;
00966 int redircodecs;
00967 int maxcallbitrate;
00968 struct t38properties t38;
00969 struct sockaddr_in udptlredirip;
00970 struct ast_udptl *udptl;
00971 int callingpres;
00972 int authtries;
00973 int expiry;
00974 long branch;
00975 char tag[11];
00976 int sessionid;
00977 int sessionversion;
00978 struct sockaddr_in sa;
00979 struct sockaddr_in redirip;
00980 struct sockaddr_in vredirip;
00981 time_t lastrtprx;
00982 time_t lastrtptx;
00983 int rtptimeout;
00984 struct sockaddr_in recv;
00985 struct in_addr ourip;
00986 struct ast_channel *owner;
00987 struct sip_route *route;
00988 int route_persistant;
00989 struct sip_auth *peerauth;
00990 int noncecount;
00991 char lastmsg[256];
00992 int amaflags;
00993 int pendinginvite;
00994 struct sip_request initreq;
00995
00996
00997 int maxtime;
00998 int initid;
00999 int waitid;
01000 int autokillid;
01001 enum transfermodes allowtransfer;
01002 struct sip_refer *refer;
01003 enum subscriptiontype subscribed;
01004 int stateid;
01005 int laststate;
01006 int dialogver;
01007
01008 struct ast_dsp *vad;
01009
01010 struct sip_peer *relatedpeer;
01011
01012 struct sip_registry *registry;
01013 struct ast_rtp *rtp;
01014 struct ast_rtp *vrtp;
01015 struct sip_pkt *packets;
01016 struct sip_history_head *history;
01017 size_t history_entries;
01018 struct ast_variable *chanvars;
01019 struct sip_pvt *next;
01020 struct sip_invite_param *options;
01021 int autoframing;
01022 } *iflist = NULL;
01023
01024
01025 #define MAX_HISTORY_ENTRIES 50
01026
01027 #define FLAG_RESPONSE (1 << 0)
01028 #define FLAG_FATAL (1 << 1)
01029
01030
01031 struct sip_pkt {
01032 struct sip_pkt *next;
01033 int retrans;
01034 int method;
01035 int seqno;
01036 unsigned int flags;
01037 struct sip_pvt *owner;
01038 int retransid;
01039 int timer_a;
01040 int timer_t1;
01041 int packetlen;
01042 char data[0];
01043 };
01044
01045
01046 struct sip_user {
01047
01048 ASTOBJ_COMPONENTS(struct sip_user);
01049 char secret[80];
01050 char md5secret[80];
01051 char context[AST_MAX_CONTEXT];
01052 char subscribecontext[AST_MAX_CONTEXT];
01053 char cid_num[80];
01054 char cid_name[80];
01055 char accountcode[AST_MAX_ACCOUNT_CODE];
01056 char language[MAX_LANGUAGE];
01057 char mohinterpret[MAX_MUSICCLASS];
01058 char mohsuggest[MAX_MUSICCLASS];
01059 char useragent[256];
01060 struct ast_codec_pref prefs;
01061 ast_group_t callgroup;
01062 ast_group_t pickupgroup;
01063 unsigned int sipoptions;
01064 struct ast_flags flags[2];
01065 int amaflags;
01066 int callingpres;
01067 int capability;
01068 int inUse;
01069 int call_limit;
01070 enum transfermodes allowtransfer;
01071 struct ast_ha *ha;
01072 struct ast_variable *chanvars;
01073 int maxcallbitrate;
01074 int autoframing;
01075 };
01076
01077
01078
01079 struct sip_peer {
01080 ASTOBJ_COMPONENTS(struct sip_peer);
01081
01082 char secret[80];
01083 char md5secret[80];
01084 struct sip_auth *auth;
01085 char context[AST_MAX_CONTEXT];
01086 char subscribecontext[AST_MAX_CONTEXT];
01087 char username[80];
01088 char accountcode[AST_MAX_ACCOUNT_CODE];
01089 int amaflags;
01090 char tohost[MAXHOSTNAMELEN];
01091 char regexten[AST_MAX_EXTENSION];
01092 char fromuser[80];
01093 char fromdomain[MAXHOSTNAMELEN];
01094 char fullcontact[256];
01095 char cid_num[80];
01096 char cid_name[80];
01097 int callingpres;
01098 int inUse;
01099 int inRinging;
01100 int onHold;
01101 int call_limit;
01102 enum transfermodes allowtransfer;
01103 char vmexten[AST_MAX_EXTENSION];
01104 char mailbox[AST_MAX_EXTENSION];
01105 char language[MAX_LANGUAGE];
01106 char mohinterpret[MAX_MUSICCLASS];
01107 char mohsuggest[MAX_MUSICCLASS];
01108 char useragent[256];
01109 struct ast_codec_pref prefs;
01110 int lastmsgssent;
01111 time_t lastmsgcheck;
01112 unsigned int sipoptions;
01113 struct ast_flags flags[2];
01114 int expire;
01115 int capability;
01116 int rtptimeout;
01117 int rtpholdtimeout;
01118 int rtpkeepalive;
01119 ast_group_t callgroup;
01120 ast_group_t pickupgroup;
01121 struct sockaddr_in addr;
01122 int maxcallbitrate;
01123
01124
01125 struct sip_pvt *call;
01126 int pokeexpire;
01127 int lastms;
01128 int maxms;
01129 struct timeval ps;
01130
01131 struct sockaddr_in defaddr;
01132 struct ast_ha *ha;
01133 struct ast_variable *chanvars;
01134 struct sip_pvt *mwipvt;
01135 int lastmsg;
01136 int autoframing;
01137 };
01138
01139
01140
01141
01142 struct sip_registry {
01143 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
01144 AST_DECLARE_STRING_FIELDS(
01145 AST_STRING_FIELD(callid);
01146 AST_STRING_FIELD(realm);
01147 AST_STRING_FIELD(nonce);
01148 AST_STRING_FIELD(opaque);
01149 AST_STRING_FIELD(qop);
01150 AST_STRING_FIELD(domain);
01151 AST_STRING_FIELD(username);
01152 AST_STRING_FIELD(authuser);
01153 AST_STRING_FIELD(hostname);
01154 AST_STRING_FIELD(secret);
01155 AST_STRING_FIELD(md5secret);
01156 AST_STRING_FIELD(contact);
01157 AST_STRING_FIELD(random);
01158 );
01159 int portno;
01160 int expire;
01161 int regattempts;
01162 int timeout;
01163 int refresh;
01164 struct sip_pvt *call;
01165 enum sipregistrystate regstate;
01166 time_t regtime;
01167 int callid_valid;
01168 unsigned int ocseq;
01169 struct sockaddr_in us;
01170 int noncecount;
01171 char lastmsg[256];
01172 };
01173
01174
01175
01176
01177 static struct ast_user_list {
01178 ASTOBJ_CONTAINER_COMPONENTS(struct sip_user);
01179 } userl;
01180
01181
01182 static struct ast_peer_list {
01183 ASTOBJ_CONTAINER_COMPONENTS(struct sip_peer);
01184 } peerl;
01185
01186
01187 static struct ast_register_list {
01188 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01189 int recheck;
01190 } regl;
01191
01192 static void temp_pvt_cleanup(void *);
01193
01194
01195 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01196
01197
01198 static struct sip_auth *authl = NULL;
01199
01200
01201
01202 static int sipsock = -1;
01203 static struct sockaddr_in bindaddr = { 0, };
01204 static struct sockaddr_in externip;
01205 static char externhost[MAXHOSTNAMELEN];
01206 static time_t externexpire = 0;
01207 static int externrefresh = 10;
01208 static struct ast_ha *localaddr;
01209 static struct in_addr __ourip;
01210 static struct sockaddr_in outboundproxyip;
01211 static int ourport;
01212 static struct sockaddr_in debugaddr;
01213
01214 static struct ast_config *notify_types;
01215
01216
01217
01218
01219
01220
01221 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
01222 static int sip_devicestate(void *data);
01223 static int sip_sendtext(struct ast_channel *ast, const char *text);
01224 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01225 static int sip_hangup(struct ast_channel *ast);
01226 static int sip_answer(struct ast_channel *ast);
01227 static struct ast_frame *sip_read(struct ast_channel *ast);
01228 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01229 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01230 static int sip_transfer(struct ast_channel *ast, const char *dest);
01231 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01232 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01233 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01234
01235
01236 static int sipsock_read(int *id, int fd, short events, void *ignore);
01237 static int __sip_xmit(struct sip_pvt *p, char *data, int len);
01238 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod);
01239 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01240 static int retrans_pkt(const void *data);
01241 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req);
01242 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);
01243 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01244 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01245 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01246 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01247 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01248 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);
01249 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01250 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, int reliable);
01251 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01252 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01253 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
01254 static int transmit_reinvite_with_sdp(struct sip_pvt *p);
01255 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01256 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01257 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01258 static int transmit_refer(struct sip_pvt *p, const char *dest);
01259 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten);
01260 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01261 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01262 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01263 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01264 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01265 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01266 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
01267 static int sip_send_mwi_to_peer(struct sip_peer *peer);
01268 static int does_peer_need_mwi(struct sip_peer *peer);
01269
01270
01271 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
01272 int useglobal_nat, const int intended_method);
01273 static int __sip_autodestruct(const void *data);
01274 static void sip_scheddestroy(struct sip_pvt *p, int ms);
01275 static int sip_cancel_destroy(struct sip_pvt *p);
01276 static void sip_destroy(struct sip_pvt *p);
01277 static int __sip_destroy(struct sip_pvt *p, int lockowner);
01278 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01279 static void __sip_pretend_ack(struct sip_pvt *p);
01280 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01281 static int auto_congest(const void *nothing);
01282 static int update_call_counter(struct sip_pvt *fup, int event);
01283 static int hangup_sip2cause(int cause);
01284 static const char *hangup_cause2sip(int cause);
01285 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method);
01286 static void free_old_route(struct sip_route *route);
01287 static void list_route(struct sip_route *route);
01288 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01289 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
01290 struct sip_request *req, char *uri);
01291 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01292 static void check_pendings(struct sip_pvt *p);
01293 static void *sip_park_thread(void *stuff);
01294 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno);
01295 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01296
01297
01298 static void try_suggested_sip_codec(struct sip_pvt *p);
01299 static const char* get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01300 static const char *get_sdp(struct sip_request *req, const char *name);
01301 static int find_sdp(struct sip_request *req);
01302 static int process_sdp(struct sip_pvt *p, struct sip_request *req);
01303 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
01304 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01305 int debug, int *min_packet_size);
01306 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
01307 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01308 int debug);
01309 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p);
01310 static void stop_media_flows(struct sip_pvt *p);
01311
01312
01313 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01314 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01315 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01316 const char *secret, const char *md5secret, int sipmethod,
01317 char *uri, enum xmittype reliable, int ignore);
01318 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01319 int sipmethod, char *uri, enum xmittype reliable,
01320 struct sockaddr_in *sin, struct sip_peer **authpeer);
01321 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin);
01322
01323
01324 static int check_sip_domain(const char *domain, char *context, size_t len);
01325 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01326 static void clear_sip_domains(void);
01327
01328
01329 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno);
01330 static int clear_realm_authentication(struct sip_auth *authlist);
01331 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
01332
01333
01334 static int sip_do_reload(enum channelreloadreason reason);
01335 static int reload_config(enum channelreloadreason reason);
01336 static int expire_register(const void *data);
01337 static void *do_monitor(void *data);
01338 static int restart_monitor(void);
01339 static int sip_send_mwi_to_peer(struct sip_peer *peer);
01340 static int sip_addrcmp(char *name, struct sockaddr_in *sin);
01341 static int sip_refer_allocate(struct sip_pvt *p);
01342 static void ast_quiet_chan(struct ast_channel *chan);
01343 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01344
01345
01346 static int cb_extensionstate(char *context, char* exten, int state, void *data, char *cid_num, char *cid_name);
01347 static int sip_devicestate(void *data);
01348 static int sip_poke_noanswer(const void *data);
01349 static int sip_poke_peer(struct sip_peer *peer);
01350 static void sip_poke_all_peers(void);
01351 static void sip_peer_hold(struct sip_pvt *p, int hold);
01352
01353
01354 static const char *sip_nat_mode(const struct sip_pvt *p);
01355 static int sip_show_inuse(int fd, int argc, char *argv[]);
01356 static char *transfermode2str(enum transfermodes mode) attribute_const;
01357 static char *nat2str(int nat) attribute_const;
01358 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01359 static int sip_show_users(int fd, int argc, char *argv[]);
01360 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01361 static int sip_show_peers(int fd, int argc, char *argv[]);
01362 static int sip_show_objects(int fd, int argc, char *argv[]);
01363 static void print_group(int fd, ast_group_t group, int crlf);
01364 static const char *dtmfmode2str(int mode) attribute_const;
01365 static const char *insecure2str(int port, int invite) attribute_const;
01366 static void cleanup_stale_contexts(char *new, char *old);
01367 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01368 static const char *domain_mode_to_text(const enum domain_mode mode);
01369 static int sip_show_domains(int fd, int argc, char *argv[]);
01370 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01371 static int sip_show_peer(int fd, int argc, char *argv[]);
01372 static int sip_show_user(int fd, int argc, char *argv[]);
01373 static int sip_show_registry(int fd, int argc, char *argv[]);
01374 static int sip_show_settings(int fd, int argc, char *argv[]);
01375 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01376 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01377 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01378 static int sip_show_channels(int fd, int argc, char *argv[]);
01379 static int sip_show_subscriptions(int fd, int argc, char *argv[]);
01380 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01381 static char *complete_sipch(const char *line, const char *word, int pos, int state);
01382 static char *complete_sip_peer(const char *word, int state, int flags2);
01383 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01384 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state);
01385 static char *complete_sip_user(const char *word, int state, int flags2);
01386 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
01387 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01388 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state);
01389 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state);
01390 static int sip_show_channel(int fd, int argc, char *argv[]);
01391 static int sip_show_history(int fd, int argc, char *argv[]);
01392 static int sip_do_debug_ip(int fd, int argc, char *argv[]);
01393 static int sip_do_debug_peer(int fd, int argc, char *argv[]);
01394 static int sip_do_debug(int fd, int argc, char *argv[]);
01395 static int sip_no_debug(int fd, int argc, char *argv[]);
01396 static int sip_notify(int fd, int argc, char *argv[]);
01397 static int sip_do_history(int fd, int argc, char *argv[]);
01398 static int sip_no_history(int fd, int argc, char *argv[]);
01399 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
01400 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01401 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01402 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01403 static int sip_dtmfmode(struct ast_channel *chan, void *data);
01404 static int sip_addheader(struct ast_channel *chan, void *data);
01405 static int sip_do_reload(enum channelreloadreason reason);
01406 static int sip_reload(int fd, int argc, char *argv[]);
01407 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen);
01408
01409
01410
01411
01412
01413 static void sip_dump_history(struct sip_pvt *dialog);
01414 static inline int sip_debug_test_addr(const struct sockaddr_in *addr);
01415 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01416 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01417 static void sip_dump_history(struct sip_pvt *dialog);
01418
01419
01420 static struct sip_peer *temp_peer(const char *name);
01421 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01422 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01423 static int update_call_counter(struct sip_pvt *fup, int event);
01424 static void sip_destroy_peer(struct sip_peer *peer);
01425 static void sip_destroy_user(struct sip_user *user);
01426 static int sip_poke_peer(struct sip_peer *peer);
01427 static int sip_poke_peer_s(const void *data);
01428 static void set_peer_defaults(struct sip_peer *peer);
01429 static struct sip_peer *temp_peer(const char *name);
01430 static void register_peer_exten(struct sip_peer *peer, int onoff);
01431 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime);
01432 static struct sip_user *find_user(const char *name, int realtime);
01433 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01434 static int expire_register(const void *data);
01435 static void reg_source_db(struct sip_peer *peer);
01436 static void destroy_association(struct sip_peer *peer);
01437 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01438
01439
01440 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey);
01441 static struct sip_user *realtime_user(const char *username);
01442 static void update_peer(struct sip_peer *p, int expiry);
01443 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin);
01444 static int sip_prune_realtime(int fd, int argc, char *argv[]);
01445
01446
01447 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us);
01448 static void sip_registry_destroy(struct sip_registry *reg);
01449 static int sip_register(char *value, int lineno);
01450 static char *regstate2str(enum sipregistrystate regstate) attribute_const;
01451 static int sip_reregister(const void *data);
01452 static int __sip_do_register(struct sip_registry *r);
01453 static int sip_reg_timeout(const void *data);
01454 static void sip_send_all_registers(void);
01455
01456
01457 static void append_date(struct sip_request *req);
01458 static int determine_firstline_parts(struct sip_request *req);
01459 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01460 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01461 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
01462 static int find_sip_method(const char *msg);
01463 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
01464 static int parse_request(struct sip_request *req);
01465 static const char *get_header(const struct sip_request *req, const char *name);
01466 static char *referstatus2str(enum referstatus rstatus) attribute_pure;
01467 static int method_match(enum sipmethod id, const char *name);
01468 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
01469 static char *get_in_brackets(char *tmp);
01470 static const char *find_alias(const char *name, const char *_default);
01471 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
01472 static int lws2sws(char *msgbuf, int len);
01473 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
01474 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
01475 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
01476 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
01477 static int set_address_from_contact(struct sip_pvt *pvt);
01478 static void check_via(struct sip_pvt *p, const struct sip_request *req);
01479 static char *get_calleridname(const char *input, char *output, size_t outputsize);
01480 static int get_rpid_num(const char *input, char *output, int maxlen);
01481 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq);
01482 static int get_destination(struct sip_pvt *p, struct sip_request *oreq);
01483 static int get_msg_text(char *buf, int len, struct sip_request *req);
01484 static void free_old_route(struct sip_route *route);
01485 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
01486
01487
01488 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
01489 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
01490 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
01491 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod);
01492 static int init_resp(struct sip_request *resp, const char *msg);
01493 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
01494 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p);
01495 static void build_via(struct sip_pvt *p);
01496 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
01497 static int create_addr(struct sip_pvt *dialog, const char *opeer);
01498 static char *generate_random_string(char *buf, size_t size);
01499 static void build_callid_pvt(struct sip_pvt *pvt);
01500 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain);
01501 static void make_our_tag(char *tagbuf, size_t len);
01502 static int add_header(struct sip_request *req, const char *var, const char *value);
01503 static int add_header_contentLength(struct sip_request *req, int len);
01504 static int add_line(struct sip_request *req, const char *line);
01505 static int add_text(struct sip_request *req, const char *text);
01506 static int add_digit(struct sip_request *req, char digit, unsigned int duration);
01507 static int add_vidupdate(struct sip_request *req);
01508 static void add_route(struct sip_request *req, struct sip_route *route);
01509 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01510 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01511 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
01512 static void set_destination(struct sip_pvt *p, char *uri);
01513 static void append_date(struct sip_request *req);
01514 static void build_contact(struct sip_pvt *p);
01515 static void build_rpid(struct sip_pvt *p);
01516
01517
01518 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock);
01519 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);
01520 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock);
01521 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
01522 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e);
01523 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
01524 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
01525 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01526 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
01527 static int handle_request_options(struct sip_pvt *p, struct sip_request *req);
01528 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin);
01529 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01530 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno);
01531
01532
01533 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01534 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01535 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno);
01536 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno);
01537
01538
01539 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active);
01540 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01541 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01542 static int sip_get_codec(struct ast_channel *chan);
01543 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect);
01544
01545
01546 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite);
01547 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
01548 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p);
01549 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
01550 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
01551
01552
01553 static const struct ast_channel_tech sip_tech = {
01554 .type = "SIP",
01555 .description = "Session Initiation Protocol (SIP)",
01556 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01557 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01558 .requester = sip_request_call,
01559 .devicestate = sip_devicestate,
01560 .call = sip_call,
01561 .hangup = sip_hangup,
01562 .answer = sip_answer,
01563 .read = sip_read,
01564 .write = sip_write,
01565 .write_video = sip_write,
01566 .indicate = sip_indicate,
01567 .transfer = sip_transfer,
01568 .fixup = sip_fixup,
01569 .send_digit_begin = sip_senddigit_begin,
01570 .send_digit_end = sip_senddigit_end,
01571 .bridge = ast_rtp_bridge,
01572 .send_text = sip_sendtext,
01573 .func_channel_read = acf_channel_read,
01574 };
01575
01576
01577
01578
01579 static const struct ast_channel_tech sip_tech_info = {
01580 .type = "SIP",
01581 .description = "Session Initiation Protocol (SIP)",
01582 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01583 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01584 .requester = sip_request_call,
01585 .devicestate = sip_devicestate,
01586 .call = sip_call,
01587 .hangup = sip_hangup,
01588 .answer = sip_answer,
01589 .read = sip_read,
01590 .write = sip_write,
01591 .write_video = sip_write,
01592 .indicate = sip_indicate,
01593 .transfer = sip_transfer,
01594 .fixup = sip_fixup,
01595 .send_digit_end = sip_senddigit_end,
01596 .bridge = ast_rtp_bridge,
01597 .send_text = sip_sendtext,
01598 .func_channel_read = acf_channel_read,
01599 };
01600
01601
01602
01603 #define UNLINK(element, head, prev) do { \
01604 if (prev) \
01605 (prev)->next = (element)->next; \
01606 else \
01607 (head) = (element)->next; \
01608 } while (0)
01609
01610
01611 static struct ast_rtp_protocol sip_rtp = {
01612 type: "SIP",
01613 get_rtp_info: sip_get_rtp_peer,
01614 get_vrtp_info: sip_get_vrtp_peer,
01615 set_rtp_peer: sip_set_rtp_peer,
01616 get_codec: sip_get_codec,
01617 };
01618
01619
01620 static struct ast_udptl_protocol sip_udptl = {
01621 type: "SIP",
01622 get_udptl_info: sip_get_udptl_peer,
01623 set_udptl_peer: sip_set_udptl_peer,
01624 };
01625
01626
01627 static char *referstatus2str(enum referstatus rstatus)
01628 {
01629 int i = (sizeof(referstatusstrings) / sizeof(referstatusstrings[0]));
01630 int x;
01631
01632 for (x = 0; x < i; x++) {
01633 if (referstatusstrings[x].status == rstatus)
01634 return (char *) referstatusstrings[x].text;
01635 }
01636 return "";
01637 }
01638
01639
01640
01641
01642 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
01643 {
01644 if (p->initreq.headers && option_debug) {
01645 ast_log(LOG_DEBUG, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
01646 }
01647
01648 copy_request(&p->initreq, req);
01649 parse_request(&p->initreq);
01650 if (ast_test_flag(req, SIP_PKT_DEBUG))
01651 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
01652 }
01653
01654 static void sip_alreadygone(struct sip_pvt *dialog)
01655 {
01656 if (option_debug > 2)
01657 ast_log(LOG_DEBUG, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
01658 ast_set_flag(&dialog->flags[0], SIP_ALREADYGONE);
01659 }
01660
01661
01662
01663
01664
01665
01666
01667
01668 static int method_match(enum sipmethod id, const char *name)
01669 {
01670 int len = strlen(sip_methods[id].text);
01671 int l_name = name ? strlen(name) : 0;
01672
01673 return (l_name >= len && name[len] < 33 &&
01674 !strncasecmp(sip_methods[id].text, name, len));
01675 }
01676
01677
01678 static int find_sip_method(const char *msg)
01679 {
01680 int i, res = 0;
01681
01682 if (ast_strlen_zero(msg))
01683 return 0;
01684 for (i = 1; i < (sizeof(sip_methods) / sizeof(sip_methods[0])) && !res; i++) {
01685 if (method_match(i, msg))
01686 res = sip_methods[i].id;
01687 }
01688 return res;
01689 }
01690
01691
01692 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
01693 {
01694 char *next, *sep;
01695 char *temp;
01696 unsigned int profile = 0;
01697 int i, found;
01698
01699 if (ast_strlen_zero(supported) )
01700 return 0;
01701 temp = ast_strdupa(supported);
01702
01703 if (option_debug > 2 && sipdebug)
01704 ast_log(LOG_DEBUG, "Begin: parsing SIP \"Supported: %s\"\n", supported);
01705
01706 for (next = temp; next; next = sep) {
01707 found = FALSE;
01708 if ( (sep = strchr(next, ',')) != NULL)
01709 *sep++ = '\0';
01710 next = ast_skip_blanks(next);
01711 if (option_debug > 2 && sipdebug)
01712 ast_log(LOG_DEBUG, "Found SIP option: -%s-\n", next);
01713 for (i=0; i < (sizeof(sip_options) / sizeof(sip_options[0])); i++) {
01714 if (!strcasecmp(next, sip_options[i].text)) {
01715 profile |= sip_options[i].id;
01716 found = TRUE;
01717 if (option_debug > 2 && sipdebug)
01718 ast_log(LOG_DEBUG, "Matched SIP option: %s\n", next);
01719 break;
01720 }
01721 }
01722 if (!found && option_debug > 2 && sipdebug) {
01723 if (!strncasecmp(next, "x-", 2))
01724 ast_log(LOG_DEBUG, "Found private SIP option, not supported: %s\n", next);
01725 else
01726 ast_log(LOG_DEBUG, "Found no match for SIP option: %s (Please file bug report!)\n", next);
01727 }
01728 }
01729
01730 if (pvt)
01731 pvt->sipoptions = profile;
01732 return profile;
01733 }
01734
01735
01736 static inline int sip_debug_test_addr(const struct sockaddr_in *addr)
01737 {
01738 if (!sipdebug)
01739 return 0;
01740 if (debugaddr.sin_addr.s_addr) {
01741 if (((ntohs(debugaddr.sin_port) != 0)
01742 && (debugaddr.sin_port != addr->sin_port))
01743 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
01744 return 0;
01745 }
01746 return 1;
01747 }
01748
01749
01750 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
01751 {
01752 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
01753 }
01754
01755
01756 static const char *sip_nat_mode(const struct sip_pvt *p)
01757 {
01758 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
01759 }
01760
01761
01762 static inline int sip_debug_test_pvt(struct sip_pvt *p)
01763 {
01764 if (!sipdebug)
01765 return 0;
01766 return sip_debug_test_addr(sip_real_dst(p));
01767 }
01768
01769
01770 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
01771 {
01772 int res;
01773 const struct sockaddr_in *dst = sip_real_dst(p);
01774 res = sendto(sipsock, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
01775
01776 if (res == -1) {
01777 switch (errno) {
01778 case EBADF:
01779 case EHOSTUNREACH:
01780 case ENETDOWN:
01781 case ENETUNREACH:
01782 case ECONNREFUSED:
01783 res = XMIT_ERROR;
01784 }
01785 }
01786 if (res != len)
01787 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));
01788 return res;
01789 }
01790
01791
01792
01793 static void build_via(struct sip_pvt *p)
01794 {
01795
01796 const char *rport = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_RFC3581 ? ";rport" : "";
01797
01798
01799 ast_string_field_build(p, via, "SIP/2.0/UDP %s:%d;branch=z9hG4bK%08x%s",
01800 ast_inet_ntoa(p->ourip), ourport, p->branch, rport);
01801 }
01802
01803
01804
01805
01806
01807
01808
01809 static enum sip_result ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us)
01810 {
01811 struct sockaddr_in theirs, ours;
01812
01813
01814 ast_ouraddrfor(them, us);
01815 theirs.sin_addr = *them;
01816 ours.sin_addr = *us;
01817
01818 if (localaddr && externip.sin_addr.s_addr &&
01819 (ast_apply_ha(localaddr, &theirs)) &&
01820 (!global_matchexterniplocally || !ast_apply_ha(localaddr, &ours))) {
01821 if (externexpire && time(NULL) >= externexpire) {
01822 struct ast_hostent ahp;
01823 struct hostent *hp;
01824
01825 externexpire = time(NULL) + externrefresh;
01826 if ((hp = ast_gethostbyname(externhost, &ahp))) {
01827 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
01828 } else
01829 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
01830 }
01831 *us = externip.sin_addr;
01832 if (option_debug) {
01833 ast_log(LOG_DEBUG, "Target address %s is not local, substituting externip\n",
01834 ast_inet_ntoa(*(struct in_addr *)&them->s_addr));
01835 }
01836 } else if (bindaddr.sin_addr.s_addr)
01837 *us = bindaddr.sin_addr;
01838 return AST_SUCCESS;
01839 }
01840
01841
01842
01843 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
01844
01845 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01846 __attribute__ ((format (printf, 2, 3)));
01847
01848
01849 static void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
01850 {
01851 char buf[80], *c = buf;
01852 struct sip_history *hist;
01853 int l;
01854
01855 vsnprintf(buf, sizeof(buf), fmt, ap);
01856 strsep(&c, "\r\n");
01857 l = strlen(buf) + 1;
01858 if (!(hist = ast_calloc(1, sizeof(*hist) + l)))
01859 return;
01860 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
01861 free(hist);
01862 return;
01863 }
01864 memcpy(hist->event, buf, l);
01865 if (p->history_entries == MAX_HISTORY_ENTRIES) {
01866 struct sip_history *oldest;
01867 oldest = AST_LIST_REMOVE_HEAD(p->history, list);
01868 p->history_entries--;
01869 free(oldest);
01870 }
01871 AST_LIST_INSERT_TAIL(p->history, hist, list);
01872 p->history_entries++;
01873 }
01874
01875
01876 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01877 {
01878 va_list ap;
01879
01880 if (!p)
01881 return;
01882
01883 if (ast_test_flag(&p->flags[0], SIP_NO_HISTORY)
01884 && !recordhistory && !dumphistory) {
01885 return;
01886 }
01887
01888 va_start(ap, fmt);
01889 append_history_va(p, fmt, ap);
01890 va_end(ap);
01891
01892 return;
01893 }
01894
01895
01896 static int retrans_pkt(const void *data)
01897 {
01898 struct sip_pkt *pkt = (struct sip_pkt *)data, *prev, *cur = NULL;
01899 int reschedule = DEFAULT_RETRANS;
01900 int xmitres = 0;
01901
01902
01903 ast_mutex_lock(&pkt->owner->lock);
01904
01905 if (pkt->retrans < MAX_RETRANS) {
01906 pkt->retrans++;
01907 if (!pkt->timer_t1) {
01908 if (sipdebug && option_debug > 3)
01909 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);
01910 } else {
01911 int siptimer_a;
01912
01913 if (sipdebug && option_debug > 3)
01914 ast_log(LOG_DEBUG, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
01915 if (!pkt->timer_a)
01916 pkt->timer_a = 2 ;
01917 else
01918 pkt->timer_a = 2 * pkt->timer_a;
01919
01920
01921 siptimer_a = pkt->timer_t1 * pkt->timer_a;
01922 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
01923 siptimer_a = 4000;
01924
01925
01926 reschedule = siptimer_a;
01927 if (option_debug > 3)
01928 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);
01929 }
01930
01931 if (sip_debug_test_pvt(pkt->owner)) {
01932 const struct sockaddr_in *dst = sip_real_dst(pkt->owner);
01933 ast_verbose("Retransmitting #%d (%s) to %s:%d:\n%s\n---\n",
01934 pkt->retrans, sip_nat_mode(pkt->owner),
01935 ast_inet_ntoa(dst->sin_addr),
01936 ntohs(dst->sin_port), pkt->data);
01937 }
01938
01939 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data);
01940 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
01941 ast_mutex_unlock(&pkt->owner->lock);
01942 if (xmitres == XMIT_ERROR)
01943 ast_log(LOG_WARNING, "Network error on retransmit in dialog %s\n", pkt->owner->callid);
01944 else
01945 return reschedule;
01946 }
01947
01948 if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
01949 if (ast_test_flag(pkt, FLAG_FATAL) || sipdebug)
01950 ast_log(LOG_WARNING, "Maximum retries exceeded on transmission %s for seqno %d (%s %s)\n", pkt->owner->callid, pkt->seqno, (ast_test_flag(pkt, FLAG_FATAL)) ? "Critical" : "Non-critical", (ast_test_flag(pkt, FLAG_RESPONSE)) ? "Response" : "Request");
01951 } else if ((pkt->method == SIP_OPTIONS) && sipdebug) {
01952 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) \n", pkt->owner->callid);
01953 }
01954 if (xmitres == XMIT_ERROR) {
01955 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission of transaction in call id %s \n", pkt->owner->callid);
01956 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
01957 } else
01958 append_history(pkt->owner, "MaxRetries", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
01959
01960 pkt->retransid = -1;
01961
01962 if (ast_test_flag(pkt, FLAG_FATAL)) {
01963 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
01964 DEADLOCK_AVOIDANCE(&pkt->owner->lock);
01965 }
01966
01967 if (pkt->owner->owner && !pkt->owner->owner->hangupcause)
01968 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
01969
01970 if (pkt->owner->owner) {
01971 sip_alreadygone(pkt->owner);
01972 ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet.\n", pkt->owner->callid);
01973 ast_queue_hangup(pkt->owner->owner);
01974 ast_channel_unlock(pkt->owner->owner);
01975 } else {
01976
01977
01978
01979 if (pkt->method != SIP_OPTIONS) {
01980 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
01981 sip_alreadygone(pkt->owner);
01982 if (option_debug)
01983 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
01984 }
01985 }
01986 }
01987
01988 if (pkt->method == SIP_BYE) {
01989
01990 if (pkt->owner->owner)
01991 ast_channel_unlock(pkt->owner->owner);
01992 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
01993 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
01994 }
01995
01996
01997 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
01998 if (cur == pkt)
01999 break;
02000 }
02001 if (cur) {
02002 if (prev)
02003 prev->next = cur->next;
02004 else
02005 pkt->owner->packets = cur->next;
02006 ast_mutex_unlock(&pkt->owner->lock);
02007 free(cur);
02008 pkt = NULL;
02009 } else
02010 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
02011 if (pkt)
02012 ast_mutex_unlock(&pkt->owner->lock);
02013 return 0;
02014 }
02015
02016
02017
02018
02019 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod)
02020 {
02021 struct sip_pkt *pkt;
02022 int siptimer_a = DEFAULT_RETRANS;
02023 int xmitres = 0;
02024
02025 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1)))
02026 return AST_FAILURE;
02027 memcpy(pkt->data, data, len);
02028 pkt->method = sipmethod;
02029 pkt->packetlen = len;
02030 pkt->next = p->packets;
02031 pkt->owner = p;
02032 pkt->seqno = seqno;
02033 if (resp)
02034 ast_set_flag(pkt, FLAG_RESPONSE);
02035 pkt->data[len] = '\0';
02036 pkt->timer_t1 = p->timer_t1;
02037 pkt->retransid = -1;
02038 if (fatal)
02039 ast_set_flag(pkt, FLAG_FATAL);
02040 if (pkt->timer_t1)
02041 siptimer_a = pkt->timer_t1 * 2;
02042
02043 if (option_debug > 3 && sipdebug)
02044 ast_log(LOG_DEBUG, "*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->retransid);
02045 pkt->retransid = -1;
02046 pkt->next = p->packets;
02047 p->packets = pkt;
02048 if (sipmethod == SIP_INVITE) {
02049
02050 p->pendinginvite = seqno;
02051 }
02052
02053 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02054
02055 if (xmitres == XMIT_ERROR) {
02056 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02057 return AST_FAILURE;
02058 } else {
02059
02060 pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
02061 return AST_SUCCESS;
02062 }
02063 }
02064
02065
02066 static int __sip_autodestruct(const void *data)
02067 {
02068 struct sip_pvt *p = (struct sip_pvt *)data;
02069
02070
02071 if (p->subscribed) {
02072 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
02073 p->subscribed = NONE;
02074 append_history(p, "Subscribestatus", "timeout");
02075 if (option_debug > 2)
02076 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP subsription %s\n", p->callid ? p->callid : "<unknown>");
02077 return 10000;
02078 }
02079
02080
02081 if (p->packets) {
02082 if (option_debug > 2)
02083 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP call %s\n", p->callid ? p->callid : "<unknown>");
02084 append_history(p, "ReliableXmit", "timeout");
02085 return 10000;
02086 }
02087
02088
02089 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
02090 ASTOBJ_UNREF(p->relatedpeer,sip_destroy_peer);
02091
02092
02093 p->autokillid = -1;
02094
02095 if (option_debug)
02096 ast_log(LOG_DEBUG, "Auto destroying SIP dialog '%s'\n", p->callid);
02097 append_history(p, "AutoDestroy", "%s", p->callid);
02098 if (p->owner) {
02099 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
02100 ast_queue_hangup(p->owner);
02101 } else if (p->refer) {
02102 if (option_debug > 2)
02103 ast_log(LOG_DEBUG, "Finally hanging up channel after transfer: %s\n", p->callid);
02104 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
02105 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
02106 } else
02107 sip_destroy(p);
02108 return 0;
02109 }
02110
02111
02112 static void sip_scheddestroy(struct sip_pvt *p, int ms)
02113 {
02114 if (ms < 0) {
02115 if (p->timer_t1 == 0)
02116 p->timer_t1 = 500;
02117 ms = p->timer_t1 * 64;
02118 }
02119 if (sip_debug_test_pvt(p))
02120 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
02121 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
02122 append_history(p, "SchedDestroy", "%d ms", ms);
02123
02124 AST_SCHED_DEL(sched, p->autokillid);
02125 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, p);
02126 }
02127
02128
02129 static int sip_cancel_destroy(struct sip_pvt *p)
02130 {
02131 int res = 0;
02132 if (p->autokillid > -1) {
02133 if (!(res = ast_sched_del(sched, p->autokillid))) {
02134 append_history(p, "CancelDestroy", "");
02135 p->autokillid = -1;
02136 }
02137 }
02138 return res;
02139 }
02140
02141
02142
02143 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02144 {
02145 struct sip_pkt *cur, *prev = NULL;
02146
02147
02148 char *msg;
02149 int res = FALSE;
02150
02151 msg = sip_methods[sipmethod].text;
02152
02153 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
02154 if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
02155 ((ast_test_flag(cur, FLAG_RESPONSE)) ||
02156 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
02157 if (!resp && (seqno == p->pendinginvite)) {
02158 if (option_debug)
02159 ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
02160 p->pendinginvite = 0;
02161 }
02162
02163 res = TRUE;
02164 UNLINK(cur, p->packets, prev);
02165 if (cur->retransid > -1) {
02166 if (sipdebug && option_debug > 3)
02167 ast_log(LOG_DEBUG, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
02168 }
02169
02170
02171
02172
02173
02174
02175
02176
02177
02178
02179
02180
02181
02182
02183
02184
02185 while (cur->retransid > -1 && ast_sched_del(sched, cur->retransid)) {
02186 DEADLOCK_AVOIDANCE(&p->lock);
02187 }
02188 free(cur);
02189 break;
02190 }
02191 }
02192 if (option_debug)
02193 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");
02194 }
02195
02196
02197
02198 static void __sip_pretend_ack(struct sip_pvt *p)
02199 {
02200 struct sip_pkt *cur = NULL;
02201
02202 while (p->packets) {
02203 int method;
02204 if (cur == p->packets) {
02205 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
02206 return;
02207 }
02208 cur = p->packets;
02209 method = (cur->method) ? cur->method : find_sip_method(cur->data);
02210 __sip_ack(p, cur->seqno, ast_test_flag(cur, FLAG_RESPONSE), method);
02211 }
02212 }
02213
02214
02215 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02216 {
02217 struct sip_pkt *cur;
02218 int res = -1;
02219
02220 for (cur = p->packets; cur; cur = cur->next) {
02221 if (cur->seqno == seqno && ast_test_flag(cur, FLAG_RESPONSE) == resp &&
02222 (ast_test_flag(cur, FLAG_RESPONSE) || method_match(sipmethod, cur->data))) {
02223
02224 if (cur->retransid > -1) {
02225 if (option_debug > 3 && sipdebug)
02226 ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
02227 }
02228 AST_SCHED_DEL(sched, cur->retransid);
02229 res = 0;
02230 break;
02231 }
02232 }
02233 if (option_debug)
02234 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");
02235 return res;
02236 }
02237
02238
02239
02240 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
02241 {
02242 memset(dst, 0, sizeof(*dst));
02243 memcpy(dst->data, src->data, sizeof(dst->data));
02244 dst->len = src->len;
02245 parse_request(dst);
02246 }
02247
02248
02249 static void add_blank(struct sip_request *req)
02250 {
02251 if (!req->lines) {
02252
02253 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
02254 req->len += strlen(req->data + req->len);
02255 }
02256 }
02257
02258
02259 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02260 {
02261 int res;
02262
02263 add_blank(req);
02264 if (sip_debug_test_pvt(p)) {
02265 const struct sockaddr_in *dst = sip_real_dst(p);
02266
02267 ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
02268 reliable ? "Reliably " : "", sip_nat_mode(p),
02269 ast_inet_ntoa(dst->sin_addr),
02270 ntohs(dst->sin_port), req->data);
02271 }
02272 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02273 struct sip_request tmp;
02274 parse_copy(&tmp, req);
02275 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"),
02276 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? tmp.rlPart2 : sip_methods[tmp.method].text);
02277 }
02278 res = (reliable) ?
02279 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02280 __sip_xmit(p, req->data, req->len);
02281 if (res > 0)
02282 return 0;
02283 return res;
02284 }
02285
02286
02287 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02288 {
02289 int res;
02290
02291 add_blank(req);
02292 if (sip_debug_test_pvt(p)) {
02293 if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
02294 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);
02295 else
02296 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);
02297 }
02298 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02299 struct sip_request tmp;
02300 parse_copy(&tmp, req);
02301 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
02302 }
02303 res = (reliable) ?
02304 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02305 __sip_xmit(p, req->data, req->len);
02306 return res;
02307 }
02308
02309
02310
02311
02312
02313 static const char *find_closing_quote(const char *start, const char *lim)
02314 {
02315 char last_char = '\0';
02316 const char *s;
02317 for (s = start; *s && s != lim; last_char = *s++) {
02318 if (*s == '"' && last_char != '\\')
02319 break;
02320 }
02321 return s;
02322 }
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333
02334
02335 static char *get_in_brackets(char *tmp)
02336 {
02337 const char *parse = tmp;
02338 char *first_bracket;
02339
02340
02341
02342
02343
02344 while ( (first_bracket = strchr(parse, '<')) ) {
02345 char *first_quote = strchr(parse, '"');
02346
02347 if (!first_quote || first_quote > first_bracket)
02348 break;
02349
02350 parse = find_closing_quote(first_quote + 1, NULL);
02351 if (!*parse) {
02352
02353 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
02354 break;
02355 }
02356 parse++;
02357 }
02358 if (first_bracket) {
02359 char *second_bracket = strchr(first_bracket + 1, '>');
02360 if (second_bracket) {
02361 *second_bracket = '\0';
02362 tmp = first_bracket + 1;
02363 } else {
02364 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
02365 }
02366 }
02367 return tmp;
02368 }
02369
02370
02371
02372 static int sip_sendtext(struct ast_channel *ast, const char *text)
02373 {
02374 struct sip_pvt *p = ast->tech_pvt;
02375 int debug = sip_debug_test_pvt(p);
02376
02377 if (debug)
02378 ast_verbose("Sending text %s on %s\n", text, ast->name);
02379 if (!p)
02380 return -1;
02381 if (ast_strlen_zero(text))
02382 return 0;
02383 if (debug)
02384 ast_verbose("Really sending text %s on %s\n", text, ast->name);
02385 transmit_message_with_text(p, text);
02386 return 0;
02387 }
02388
02389
02390
02391
02392
02393
02394 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey)
02395 {
02396 char port[10];
02397 char ipaddr[INET_ADDRSTRLEN];
02398 char regseconds[20];
02399
02400 char *sysname = ast_config_AST_SYSTEM_NAME;
02401 char *syslabel = NULL;
02402
02403 time_t nowtime = time(NULL) + expirey;
02404 const char *fc = fullcontact ? "fullcontact" : NULL;
02405
02406 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
02407 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
02408 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
02409
02410 if (ast_strlen_zero(sysname))
02411 sysname = NULL;
02412 else if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME))
02413 syslabel = "regserver";
02414
02415 if (fc)
02416 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02417 "port", port, "regseconds", regseconds,
02418 "username", username, fc, fullcontact, syslabel, sysname, NULL);
02419 else
02420 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02421 "port", port, "regseconds", regseconds,
02422 "username", username, syslabel, sysname, NULL);
02423 }
02424
02425
02426 static void register_peer_exten(struct sip_peer *peer, int onoff)
02427 {
02428 char multi[256];
02429 char *stringp, *ext, *context;
02430
02431
02432
02433
02434
02435 if (ast_strlen_zero(global_regcontext))
02436 return;
02437
02438 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
02439 stringp = multi;
02440 while ((ext = strsep(&stringp, "&"))) {
02441 if ((context = strchr(ext, '@'))) {
02442 *context++ = '\0';
02443 if (!ast_context_find(context)) {
02444 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
02445 continue;
02446 }
02447 } else {
02448 context = global_regcontext;
02449 }
02450 if (onoff)
02451 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
02452 ast_strdup(peer->name), ast_free, "SIP");
02453 else
02454 ast_context_remove_extension(context, ext, 1, NULL);
02455 }
02456 }
02457
02458
02459 static void sip_destroy_peer(struct sip_peer *peer)
02460 {
02461 if (option_debug > 2)
02462 ast_log(LOG_DEBUG, "Destroying SIP peer %s\n", peer->name);
02463
02464
02465 if (peer->call)
02466 sip_destroy(peer->call);
02467
02468 if (peer->mwipvt)
02469 sip_destroy(peer->mwipvt);
02470
02471 if (peer->chanvars) {
02472 ast_variables_destroy(peer->chanvars);
02473 peer->chanvars = NULL;
02474 }
02475
02476 register_peer_exten(peer, FALSE);
02477 ast_free_ha(peer->ha);
02478 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT))
02479 apeerobjs--;
02480 else if (ast_test_flag(&peer->flags[0], SIP_REALTIME))
02481 rpeerobjs--;
02482 else
02483 speerobjs--;
02484 clear_realm_authentication(peer->auth);
02485 peer->auth = NULL;
02486 free(peer);
02487 }
02488
02489
02490 static void update_peer(struct sip_peer *p, int expiry)
02491 {
02492 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02493 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) &&
02494 (ast_test_flag(&p->flags[0], SIP_REALTIME) || rtcachefriends)) {
02495 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, expiry);
02496 }
02497 }
02498
02499
02500
02501
02502
02503
02504
02505 static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_in *sin)
02506 {
02507 struct sip_peer *peer=NULL;
02508 struct ast_variable *var = NULL;
02509 struct ast_config *peerlist = NULL;
02510 struct ast_variable *tmp;
02511 struct ast_flags flags = {0};
02512 const char *iabuf = NULL;
02513 char portstring[6];
02514 const char *insecure;
02515 char *cat = NULL;
02516 unsigned short portnum;
02517
02518
02519 if (newpeername) {
02520 var = ast_load_realtime("sippeers", "name", newpeername, "host", "dynamic", NULL);
02521 if (!var && sin)
02522 var = ast_load_realtime("sippeers", "name", newpeername, "host", ast_inet_ntoa(sin->sin_addr), NULL);
02523 if (!var) {
02524 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
02525
02526
02527
02528
02529
02530
02531 if (var && sin) {
02532 for (tmp = var; tmp; tmp = tmp->next) {
02533 if (!strcasecmp(tmp->name, "host")) {
02534 struct hostent *hp;
02535 struct ast_hostent ahp;
02536 if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(&hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
02537
02538 ast_variables_destroy(var);
02539 var = NULL;
02540 }
02541 break;
02542 }
02543 }
02544 }
02545 }
02546 }
02547
02548 if (!var && sin) {
02549 iabuf = ast_inet_ntoa(sin->sin_addr);
02550 portnum = ntohs(sin->sin_port);
02551 sprintf(portstring, "%d", portnum);
02552 var = ast_load_realtime("sippeers", "host", iabuf, "port", portstring, NULL);
02553 if (!var)
02554 var = ast_load_realtime("sippeers", "ipaddr", iabuf, "port", portstring, NULL);
02555 if (!var) {
02556 peerlist = ast_load_realtime_multientry("sippeers", "host", iabuf, NULL);
02557 if(peerlist){
02558 while((cat = ast_category_browse(peerlist, cat)))
02559 {
02560 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02561 set_insecure_flags(&flags, insecure, -1);
02562 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02563 var = ast_category_root(peerlist, cat);
02564 break;
02565 }
02566 }
02567 }
02568 if(!var) {
02569 ast_config_destroy(peerlist);
02570 peerlist = NULL;
02571 cat = NULL;
02572 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", iabuf, NULL);
02573 if(peerlist) {
02574 while((cat = ast_category_browse(peerlist, cat)))
02575 {
02576 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02577 set_insecure_flags(&flags, insecure, -1);
02578 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02579 var = ast_category_root(peerlist, cat);
02580 break;
02581 }
02582 }
02583 }
02584 }
02585 }
02586 }
02587
02588 if (!var) {
02589 if(peerlist)
02590 ast_config_destroy(peerlist);
02591 return NULL;
02592 }
02593
02594 for (tmp = var; tmp; tmp = tmp->next) {
02595
02596 if (!strcasecmp(tmp->name, "type") &&
02597 !strcasecmp(tmp->value, "user")) {
02598 ast_variables_destroy(var);
02599 return NULL;
02600 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
02601 newpeername = tmp->value;
02602 }
02603 }
02604
02605 if (!newpeername) {
02606 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", iabuf);
02607 if(peerlist)
02608 ast_config_destroy(peerlist);
02609 else
02610 ast_variables_destroy(var);
02611 return NULL;
02612 }
02613
02614
02615 peer = build_peer(newpeername, var, NULL, 1);
02616 if (!peer) {
02617 if(peerlist)
02618 ast_config_destroy(peerlist);
02619 else
02620 ast_variables_destroy(var);
02621 return NULL;
02622 }
02623
02624 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
02625
02626 ast_copy_flags(&peer->flags[1],&global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
02627 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
02628 if (!AST_SCHED_DEL(sched, peer->expire)) {
02629 struct sip_peer *peer_ptr = peer;
02630 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
02631 }
02632 peer->expire = ast_sched_add(sched, (global_rtautoclear) * 1000, expire_register, ASTOBJ_REF(peer));
02633 if (peer->expire == -1) {
02634 struct sip_peer *peer_ptr = peer;
02635 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
02636 }
02637 }
02638 ASTOBJ_CONTAINER_LINK(&peerl,peer);
02639 } else {
02640 ast_set_flag(&peer->flags[0], SIP_REALTIME);
02641 }
02642 if(peerlist)
02643 ast_config_destroy(peerlist);
02644 else
02645 ast_variables_destroy(var);
02646 return peer;
02647 }
02648
02649
02650 static int sip_addrcmp(char *name, struct sockaddr_in *sin)
02651 {
02652
02653 struct sip_peer *p = (struct sip_peer *) name;
02654 return !(!inaddrcmp(&p->addr, sin) ||
02655 (ast_test_flag(&p->flags[0], SIP_INSECURE_PORT) &&
02656 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
02657 }
02658
02659
02660
02661
02662 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
02663 {
02664 struct sip_peer *p = NULL;
02665
02666 if (peer)
02667 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
02668 else
02669 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, name, sip_addr_hashfunc, 1, sip_addrcmp);
02670
02671 if (!p && realtime)
02672 p = realtime_peer(peer, sin);
02673
02674 return p;
02675 }
02676
02677
02678 static void sip_destroy_user(struct sip_user *user)
02679 {
02680 if (option_debug > 2)
02681 ast_log(LOG_DEBUG, "Destroying user object from memory: %s\n", user->name);
02682 ast_free_ha(user->ha);
02683 if (user->chanvars) {
02684 ast_variables_destroy(user->chanvars);
02685 user->chanvars = NULL;
02686 }
02687 if (ast_test_flag(&user->flags[0], SIP_REALTIME))
02688 ruserobjs--;
02689 else
02690 suserobjs--;
02691 free(user);
02692 }
02693
02694
02695
02696
02697 static struct sip_user *realtime_user(const char *username)
02698 {
02699 struct ast_variable *var;
02700 struct ast_variable *tmp;
02701 struct sip_user *user = NULL;
02702
02703 var = ast_load_realtime("sipusers", "name", username, NULL);
02704
02705 if (!var)
02706 return NULL;
02707
02708 for (tmp = var; tmp; tmp = tmp->next) {
02709 if (!strcasecmp(tmp->name, "type") &&
02710 !strcasecmp(tmp->value, "peer")) {
02711 ast_variables_destroy(var);
02712 return NULL;
02713 }
02714 }
02715
02716 user = build_user(username, var, NULL, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
02717
02718 if (!user) {
02719 ast_variables_destroy(var);
02720 return NULL;
02721 }
02722
02723 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
02724 ast_set_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02725 suserobjs++;
02726 ASTOBJ_CONTAINER_LINK(&userl,user);
02727 } else {
02728
02729 suserobjs--;
02730 ruserobjs++;
02731 ast_set_flag(&user->flags[0], SIP_REALTIME);
02732 }
02733 ast_variables_destroy(var);
02734 return user;
02735 }
02736
02737
02738
02739
02740
02741 static struct sip_user *find_user(const char *name, int realtime)
02742 {
02743 struct sip_user *u = ASTOBJ_CONTAINER_FIND(&userl, name);
02744 if (!u && realtime)
02745 u = realtime_user(name);
02746 return u;
02747 }
02748
02749
02750 static void do_setnat(struct sip_pvt *p, int natflags)
02751 {
02752 const char *mode = natflags ? "On" : "Off";
02753
02754 if (p->rtp) {
02755 if (option_debug)
02756 ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", mode);
02757 ast_rtp_setnat(p->rtp, natflags);
02758 }
02759 if (p->vrtp) {
02760 if (option_debug)
02761 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", mode);
02762 ast_rtp_setnat(p->vrtp, natflags);
02763 }
02764 if (p->udptl) {
02765 if (option_debug)
02766 ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", mode);
02767 ast_udptl_setnat(p->udptl, natflags);
02768 }
02769 }
02770
02771
02772
02773
02774 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
02775 {
02776 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
02777 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
02778 dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
02779 dialog->recv = dialog->sa;
02780 } else
02781 return -1;
02782
02783 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
02784 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
02785 dialog->capability = peer->capability;
02786 if ((!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(dialog->capability & AST_FORMAT_VIDEO_MASK)) && dialog->vrtp) {
02787 ast_rtp_destroy(dialog->vrtp);
02788 dialog->vrtp = NULL;
02789 }
02790 dialog->prefs = peer->prefs;
02791 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
02792 dialog->t38.capability = global_t38_capability;
02793 if (dialog->udptl) {
02794 if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_FEC )
02795 dialog->t38.capability |= T38FAX_UDP_EC_FEC;
02796 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY )
02797 dialog->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
02798 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_NONE )
02799 dialog->t38.capability |= T38FAX_UDP_EC_NONE;
02800 dialog->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
02801 if (option_debug > 1)
02802 ast_log(LOG_DEBUG,"Our T38 capability (%d)\n", dialog->t38.capability);
02803 }
02804 dialog->t38.jointcapability = dialog->t38.capability;
02805 } else if (dialog->udptl) {
02806 ast_udptl_destroy(dialog->udptl);
02807 dialog->udptl = NULL;
02808 }
02809 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE );
02810
02811 if (dialog->rtp) {
02812 ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
02813 ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
02814 ast_rtp_set_rtptimeout(dialog->rtp, peer->rtptimeout);
02815 ast_rtp_set_rtpholdtimeout(dialog->rtp, peer->rtpholdtimeout);
02816 ast_rtp_set_rtpkeepalive(dialog->rtp, peer->rtpkeepalive);
02817
02818 ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
02819 dialog->autoframing = peer->autoframing;
02820 }
02821 if (dialog->vrtp) {
02822 ast_rtp_setdtmf(dialog->vrtp, 0);
02823 ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
02824 ast_rtp_set_rtptimeout(dialog->vrtp, peer->rtptimeout);
02825 ast_rtp_set_rtpholdtimeout(dialog->vrtp, peer->rtpholdtimeout);
02826 ast_rtp_set_rtpkeepalive(dialog->vrtp, peer->rtpkeepalive);
02827 }
02828
02829 ast_string_field_set(dialog, peername, peer->name);
02830 ast_string_field_set(dialog, authname, peer->username);
02831 ast_string_field_set(dialog, username, peer->username);
02832 ast_string_field_set(dialog, peersecret, peer->secret);
02833 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
02834 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
02835 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
02836 ast_string_field_set(dialog, tohost, peer->tohost);
02837 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
02838 if (!dialog->initreq.headers && !ast_strlen_zero(peer->fromdomain)) {
02839 char *tmpcall;
02840 char *c;
02841 tmpcall = ast_strdupa(dialog->callid);
02842 c = strchr(tmpcall, '@');
02843 if (c) {
02844 *c = '\0';
02845 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
02846 }
02847 }
02848 if (ast_strlen_zero(dialog->tohost))
02849 ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
02850 if (!ast_strlen_zero(peer->fromdomain))
02851 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
02852 if (!ast_strlen_zero(peer->fromuser))
02853 ast_string_field_set(dialog, fromuser, peer->fromuser);
02854 if (!ast_strlen_zero(peer->language))
02855 ast_string_field_set(dialog, language, peer->language);
02856 dialog->maxtime = peer->maxms;
02857 dialog->callgroup = peer->callgroup;
02858 dialog->pickupgroup = peer->pickupgroup;
02859 dialog->allowtransfer = peer->allowtransfer;
02860
02861
02862 if (peer->maxms && peer->lastms)
02863 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
02864 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
02865 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
02866 dialog->noncodeccapability |= AST_RTP_DTMF;
02867 else
02868 dialog->noncodeccapability &= ~AST_RTP_DTMF;
02869 dialog->jointnoncodeccapability = dialog->noncodeccapability;
02870 ast_string_field_set(dialog, context, peer->context);
02871 dialog->rtptimeout = peer->rtptimeout;
02872 if (peer->call_limit)
02873 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
02874 dialog->maxcallbitrate = peer->maxcallbitrate;
02875
02876 return 0;
02877 }
02878
02879
02880
02881
02882 static int create_addr(struct sip_pvt *dialog, const char *opeer)
02883 {
02884 struct hostent *hp;
02885 struct ast_hostent ahp;
02886 struct sip_peer *p;
02887 char *port;
02888 int portno;
02889 char host[MAXHOSTNAMELEN], *hostn;
02890 char peer[256];
02891
02892 ast_copy_string(peer, opeer, sizeof(peer));
02893 port = strchr(peer, ':');
02894 if (port)
02895 *port++ = '\0';
02896 dialog->sa.sin_family = AF_INET;
02897 dialog->timer_t1 = 500;
02898 p = find_peer(peer, NULL, 1);
02899
02900 if (p) {
02901 int res = create_addr_from_peer(dialog, p);
02902 ASTOBJ_UNREF(p, sip_destroy_peer);
02903 return res;
02904 }
02905 hostn = peer;
02906 portno = port ? atoi(port) : STANDARD_SIP_PORT;
02907 if (srvlookup) {
02908 char service[MAXHOSTNAMELEN];
02909 int tportno;
02910 int ret;
02911
02912 snprintf(service, sizeof(service), "_sip._udp.%s", peer);
02913 ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
02914 if (ret > 0) {
02915 hostn = host;
02916 portno = tportno;
02917 }
02918 }
02919 hp = ast_gethostbyname(hostn, &ahp);
02920 if (!hp) {
02921 ast_log(LOG_WARNING, "No such host: %s\n", peer);
02922 return -1;
02923 }
02924 ast_string_field_set(dialog, tohost, peer);
02925 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
02926 dialog->sa.sin_port = htons(portno);
02927 dialog->recv = dialog->sa;
02928 return 0;
02929 }
02930
02931
02932 static int auto_congest(const void *nothing)
02933 {
02934 struct sip_pvt *p = (struct sip_pvt *)nothing;
02935
02936 ast_mutex_lock(&p->lock);
02937 p->initid = -1;
02938 if (p->owner) {
02939
02940 if (!ast_channel_trylock(p->owner)) {
02941 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
02942 append_history(p, "Cong", "Auto-congesting (timer)");
02943 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
02944 ast_channel_unlock(p->owner);
02945 }
02946 }
02947 ast_mutex_unlock(&p->lock);
02948 return 0;
02949 }
02950
02951
02952
02953
02954 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
02955 {
02956 int res, xmitres = 0;
02957 struct sip_pvt *p;
02958 struct varshead *headp;
02959 struct ast_var_t *current;
02960 const char *referer = NULL;
02961
02962 p = ast->tech_pvt;
02963 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
02964 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
02965 return -1;
02966 }
02967
02968
02969 headp=&ast->varshead;
02970 AST_LIST_TRAVERSE(headp,current,entries) {
02971
02972 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
02973 p->options->vxml_url = ast_var_value(current);
02974 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
02975 p->options->uri_options = ast_var_value(current);
02976 } else if (!p->options->distinctive_ring && !strcasecmp(ast_var_name(current), "ALERT_INFO")) {
02977
02978 p->options->distinctive_ring = ast_var_value(current);
02979 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
02980
02981 p->options->addsipheaders = 1;
02982 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
02983
02984 p->options->transfer = 1;
02985 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
02986
02987 referer = ast_var_value(current);
02988 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
02989
02990 p->options->replaces = ast_var_value(current);
02991 } else if (!strcasecmp(ast_var_name(current), "T38CALL")) {
02992 p->t38.state = T38_LOCAL_DIRECT;
02993 if (option_debug)
02994 ast_log(LOG_DEBUG,"T38State change to %d on channel %s\n", p->t38.state, ast->name);
02995 }
02996
02997 }
02998
02999 res = 0;
03000 ast_set_flag(&p->flags[0], SIP_OUTGOING);
03001
03002 if (p->options->transfer) {
03003 char buf[SIPBUFSIZE/2];
03004
03005 if (referer) {
03006 if (sipdebug && option_debug > 2)
03007 ast_log(LOG_DEBUG, "Call for %s transfered by %s\n", p->username, referer);
03008 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
03009 } else
03010 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
03011 ast_string_field_set(p, cid_name, buf);
03012 }
03013 if (option_debug)
03014 ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
03015
03016 res = update_call_counter(p, INC_CALL_RINGING);
03017 if ( res != -1 ) {
03018 p->callingpres = ast->cid.cid_pres;
03019 p->jointcapability = ast_translate_available_formats(p->capability, p->prefcodec);
03020 p->jointnoncodeccapability = p->noncodeccapability;
03021
03022
03023 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
03024 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
03025 res = -1;
03026 } else {
03027 p->t38.jointcapability = p->t38.capability;
03028 if (option_debug > 1)
03029 ast_log(LOG_DEBUG,"Our T38 capability (%d), joint T38 capability (%d)\n", p->t38.capability, p->t38.jointcapability);
03030 xmitres = transmit_invite(p, SIP_INVITE, 1, 2);
03031 if (xmitres == XMIT_ERROR)
03032 return -1;
03033
03034 p->invitestate = INV_CALLING;
03035
03036
03037 AST_SCHED_DEL(sched, p->initid);
03038 p->initid = ast_sched_add(sched, p->maxtime ? (p->maxtime * 4) : SIP_TRANS_TIMEOUT, auto_congest, p);
03039 }
03040 }
03041 return res;
03042 }
03043
03044
03045
03046 static void sip_registry_destroy(struct sip_registry *reg)
03047 {
03048
03049 if (option_debug > 2)
03050 ast_log(LOG_DEBUG, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
03051
03052 if (reg->call) {
03053
03054
03055 reg->call->registry = NULL;
03056 if (option_debug > 2)
03057 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
03058 sip_destroy(reg->call);
03059 }
03060 AST_SCHED_DEL(sched, reg->expire);
03061 AST_SCHED_DEL(sched, reg->timeout);
03062 ast_string_field_free_memory(reg);
03063 regobjs--;
03064 free(reg);
03065
03066 }
03067
03068
03069 static int __sip_destroy(struct sip_pvt *p, int lockowner)
03070 {
03071 struct sip_pvt *cur, *prev = NULL;
03072 struct sip_pkt *cp;
03073
03074
03075 if (p->rtp && ast_rtp_get_bridged(p->rtp)) {
03076 ast_verbose("Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03077 return -1;
03078 }
03079
03080 if (p->vrtp && ast_rtp_get_bridged(p->vrtp)) {
03081 ast_verbose("Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03082 return -1;
03083 }
03084
03085 if (sip_debug_test_pvt(p) || option_debug > 2)
03086 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
03087
03088 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03089 update_call_counter(p, DEC_CALL_LIMIT);
03090 if (option_debug > 1)
03091 ast_log(LOG_DEBUG, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
03092 }
03093
03094
03095 if (p->owner) {
03096 if (lockowner)
03097 ast_channel_lock(p->owner);
03098 if (option_debug)
03099 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
03100 p->owner->tech_pvt = NULL;
03101
03102 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03103 if (lockowner)
03104 ast_channel_unlock(p->owner);
03105
03106 usleep(1);
03107 }
03108
03109
03110 if (p->relatedpeer) {
03111 p->relatedpeer->mwipvt = NULL;
03112 ASTOBJ_UNREF(p->relatedpeer, sip_destroy_peer);
03113 }
03114
03115 if (dumphistory)
03116 sip_dump_history(p);
03117
03118 if (p->options)
03119 free(p->options);
03120
03121 if (p->stateid > -1)
03122 ast_extension_state_del(p->stateid, NULL);
03123 AST_SCHED_DEL(sched, p->initid);
03124 AST_SCHED_DEL(sched, p->waitid);
03125 AST_SCHED_DEL(sched, p->autokillid);
03126
03127 if (p->rtp) {
03128 ast_rtp_destroy(p->rtp);
03129 }
03130 if (p->vrtp) {
03131 ast_rtp_destroy(p->vrtp);
03132 }
03133 if (p->udptl)
03134 ast_udptl_destroy(p->udptl);
03135 if (p->refer)
03136 free(p->refer);
03137 if (p->route) {
03138 free_old_route(p->route);
03139 p->route = NULL;
03140 }
03141 if (p->registry) {
03142 if (p->registry->call == p)
03143 p->registry->call = NULL;
03144 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
03145 }
03146
03147
03148 if (p->history) {
03149 struct sip_history *hist;
03150 while ( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) ) {
03151 free(hist);
03152 p->history_entries--;
03153 }
03154 free(p->history);
03155 p->history = NULL;
03156 }
03157
03158 for (prev = NULL, cur = iflist; cur; prev = cur, cur = cur->next) {
03159 if (cur == p) {
03160 UNLINK(cur, iflist, prev);
03161 break;
03162 }
03163 }
03164 if (!cur) {
03165 ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
03166 return 0;
03167 }
03168
03169
03170 while((cp = p->packets)) {
03171 p->packets = p->packets->next;
03172 AST_SCHED_DEL(sched, cp->retransid);
03173 free(cp);
03174 }
03175 if (p->chanvars) {
03176 ast_variables_destroy(p->chanvars);
03177 p->chanvars = NULL;
03178 }
03179 ast_mutex_destroy(&p->lock);
03180
03181 ast_string_field_free_memory(p);
03182
03183 free(p);
03184 return 0;
03185 }
03186
03187
03188
03189
03190
03191
03192
03193
03194
03195
03196
03197
03198
03199
03200
03201 static int update_call_counter(struct sip_pvt *fup, int event)
03202 {
03203 char name[256];
03204 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
03205 int outgoing = ast_test_flag(&fup->flags[1], SIP_PAGE2_OUTGOING_CALL);
03206 struct sip_user *u = NULL;
03207 struct sip_peer *p = NULL;
03208
03209 if (option_debug > 2)
03210 ast_log(LOG_DEBUG, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
03211
03212
03213
03214 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
03215 return 0;
03216
03217 ast_copy_string(name, fup->username, sizeof(name));
03218
03219
03220 if (global_limitonpeers == FALSE && !outgoing && (u = find_user(name, 1))) {
03221 inuse = &u->inUse;
03222 call_limit = &u->call_limit;
03223 inringing = NULL;
03224 } else if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, 1) ) ) {
03225 inuse = &p->inUse;
03226 call_limit = &p->call_limit;
03227 inringing = &p->inRinging;
03228 ast_copy_string(name, fup->peername, sizeof(name));
03229 }
03230 if (!p && !u) {
03231 if (option_debug > 1)
03232 ast_log(LOG_DEBUG, "%s is not a local device, no call limit\n", name);
03233 return 0;
03234 }
03235
03236 switch(event) {
03237
03238 case DEC_CALL_LIMIT:
03239 if ( *inuse > 0 ) {
03240 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
03241 (*inuse)--;
03242 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
03243 }
03244 } else {
03245 *inuse = 0;
03246 }
03247 if (inringing) {
03248 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03249 if (*inringing > 0)
03250 (*inringing)--;
03251 else if (!ast_test_flag(&p->flags[0], SIP_REALTIME) || ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03252 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", fup->peername);
03253 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03254 }
03255 }
03256 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold) {
03257 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
03258 sip_peer_hold(fup, 0);
03259 }
03260 if (option_debug > 1 || sipdebug) {
03261 ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
03262 }
03263 break;
03264
03265 case INC_CALL_RINGING:
03266 case INC_CALL_LIMIT:
03267 if (*call_limit > 0 ) {
03268 if (*inuse >= *call_limit) {
03269 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);
03270 if (u)
03271 ASTOBJ_UNREF(u, sip_destroy_user);
03272 else
03273 ASTOBJ_UNREF(p, sip_destroy_peer);
03274 return -1;
03275 }
03276 }
03277 if (inringing && (event == INC_CALL_RINGING)) {
03278 if (!ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03279 (*inringing)++;
03280 ast_set_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03281 }
03282 }
03283
03284 (*inuse)++;
03285 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
03286 if (option_debug > 1 || sipdebug) {
03287 ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
03288 }
03289 break;
03290
03291 case DEC_CALL_RINGING:
03292 if (inringing) {
03293 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03294 if (*inringing > 0)
03295 (*inringing)--;
03296 else if (!ast_test_flag(&p->flags[0], SIP_REALTIME) || ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03297 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", p->name);
03298 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03299 }
03300 }
03301 break;
03302
03303 default:
03304 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
03305 }
03306 if (p) {
03307 ast_device_state_changed("SIP/%s", p->name);
03308 ASTOBJ_UNREF(p, sip_destroy_peer);
03309 } else
03310 ASTOBJ_UNREF(u, sip_destroy_user);
03311 return 0;
03312 }
03313
03314
03315 static void sip_destroy(struct sip_pvt *p)
03316 {
03317 ast_mutex_lock(&iflock);
03318 if (option_debug > 2)
03319 ast_log(LOG_DEBUG, "Destroying SIP dialog %s\n", p->callid);
03320 __sip_destroy(p, 1);
03321 ast_mutex_unlock(&iflock);
03322 }
03323
03324
03325 static int hangup_sip2cause(int cause)
03326 {
03327
03328
03329 switch(cause) {
03330 case 401:
03331 return AST_CAUSE_CALL_REJECTED;
03332 case 403:
03333 return AST_CAUSE_CALL_REJECTED;
03334 case 404:
03335 return AST_CAUSE_UNALLOCATED;
03336 case 405:
03337 return AST_CAUSE_INTERWORKING;
03338 case 407:
03339 return AST_CAUSE_CALL_REJECTED;
03340 case 408:
03341 return AST_CAUSE_NO_USER_RESPONSE;
03342 case 409:
03343 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
03344 case 410:
03345 return AST_CAUSE_UNALLOCATED;
03346 case 411:
03347 return AST_CAUSE_INTERWORKING;
03348 case 413:
03349 return AST_CAUSE_INTERWORKING;
03350 case 414:
03351 return AST_CAUSE_INTERWORKING;
03352 case 415:
03353 return AST_CAUSE_INTERWORKING;
03354 case 420:
03355 return AST_CAUSE_NO_ROUTE_DESTINATION;
03356 case 480:
03357 return AST_CAUSE_NO_ANSWER;
03358 case 481:
03359 return AST_CAUSE_INTERWORKING;
03360 case 482:
03361 return AST_CAUSE_INTERWORKING;
03362 case 483:
03363 return AST_CAUSE_NO_ANSWER;
03364 case 484:
03365 return AST_CAUSE_INVALID_NUMBER_FORMAT;
03366 case 485:
03367 return AST_CAUSE_UNALLOCATED;
03368 case 486:
03369 return AST_CAUSE_BUSY;
03370 case 487:
03371 return AST_CAUSE_INTERWORKING;
03372 case 488:
03373 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03374 case 491:
03375 return AST_CAUSE_INTERWORKING;
03376 case 493:
03377 return AST_CAUSE_INTERWORKING;
03378 case 500:
03379 return AST_CAUSE_FAILURE;
03380 case 501:
03381 return AST_CAUSE_FACILITY_REJECTED;
03382 case 502:
03383 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
03384 case 503:
03385 return AST_CAUSE_CONGESTION;
03386 case 504:
03387 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
03388 case 505:
03389 return AST_CAUSE_INTERWORKING;
03390 case 600:
03391 return AST_CAUSE_USER_BUSY;
03392 case 603:
03393 return AST_CAUSE_CALL_REJECTED;
03394 case 604:
03395 return AST_CAUSE_UNALLOCATED;
03396 case 606:
03397 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03398 default:
03399 return AST_CAUSE_NORMAL;
03400 }
03401
03402 return 0;
03403 }
03404
03405
03406
03407
03408
03409
03410
03411
03412
03413
03414
03415
03416
03417
03418
03419
03420
03421
03422
03423
03424
03425
03426
03427
03428
03429
03430
03431
03432
03433
03434
03435
03436
03437 static const char *hangup_cause2sip(int cause)
03438 {
03439 switch (cause) {
03440 case AST_CAUSE_UNALLOCATED:
03441 case AST_CAUSE_NO_ROUTE_DESTINATION:
03442 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
03443 return "404 Not Found";
03444 case AST_CAUSE_CONGESTION:
03445 case AST_CAUSE_SWITCH_CONGESTION:
03446 return "503 Service Unavailable";
03447 case AST_CAUSE_NO_USER_RESPONSE:
03448 return "408 Request Timeout";
03449 case AST_CAUSE_NO_ANSWER:
03450 return "480 Temporarily unavailable";
03451 case AST_CAUSE_CALL_REJECTED:
03452 return "403 Forbidden";
03453 case AST_CAUSE_NUMBER_CHANGED:
03454 return "410 Gone";
03455 case AST_CAUSE_NORMAL_UNSPECIFIED:
03456 return "480 Temporarily unavailable";
03457 case AST_CAUSE_INVALID_NUMBER_FORMAT:
03458 return "484 Address incomplete";
03459 case AST_CAUSE_USER_BUSY:
03460 return "486 Busy here";
03461 case AST_CAUSE_FAILURE:
03462 return "500 Server internal failure";
03463 case AST_CAUSE_FACILITY_REJECTED:
03464 return "501 Not Implemented";
03465 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
03466 return "503 Service Unavailable";
03467
03468 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
03469 return "502 Bad Gateway";
03470 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
03471 return "488 Not Acceptable Here";
03472
03473 case AST_CAUSE_NOTDEFINED:
03474 default:
03475 if (option_debug)
03476 ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
03477 return NULL;
03478 }
03479
03480
03481 return 0;
03482 }
03483
03484
03485
03486
03487 static int sip_hangup(struct ast_channel *ast)
03488 {
03489 struct sip_pvt *p = ast->tech_pvt;
03490 int needcancel = FALSE;
03491 int needdestroy = 0;
03492 struct ast_channel *oldowner = ast;
03493
03494 if (!p) {
03495 if (option_debug)
03496 ast_log(LOG_DEBUG, "Asked to hangup channel that was not connected\n");
03497 return 0;
03498 }
03499
03500 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
03501 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03502 if (option_debug && sipdebug)
03503 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03504 update_call_counter(p, DEC_CALL_LIMIT);
03505 }
03506 if (option_debug >3)
03507 ast_log(LOG_DEBUG, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
03508 if (p->autokillid > -1 && sip_cancel_destroy(p))
03509 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03510 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03511 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
03512 ast_clear_flag(&p->flags[0], SIP_NEEDDESTROY);
03513 p->owner->tech_pvt = NULL;
03514 p->owner = NULL;
03515 return 0;
03516 }
03517 if (option_debug) {
03518 if (ast_test_flag(ast, AST_FLAG_ZOMBIE) && p->refer && option_debug)
03519 ast_log(LOG_DEBUG, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
03520 else {
03521 if (option_debug)
03522 ast_log(LOG_DEBUG, "Hangup call %s, SIP callid %s)\n", ast->name, p->callid);
03523 }
03524 }
03525 if (option_debug && ast_test_flag(ast, AST_FLAG_ZOMBIE))
03526 ast_log(LOG_DEBUG, "Hanging up zombie call. Be scared.\n");
03527
03528 ast_mutex_lock(&p->lock);
03529 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03530 if (option_debug && sipdebug)
03531 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03532 update_call_counter(p, DEC_CALL_LIMIT);
03533 }
03534
03535
03536 if (p->owner != ast) {
03537 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
03538 ast_mutex_unlock(&p->lock);
03539 return 0;
03540 }
03541
03542 if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING || (p->invitestate < INV_COMPLETED && ast->_state != AST_STATE_UP)) {
03543 needcancel = TRUE;
03544 if (option_debug > 3)
03545 ast_log(LOG_DEBUG, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
03546 }
03547
03548 stop_media_flows(p);
03549
03550 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", p->owner ? ast_cause2str(p->owner->hangupcause) : "Unknown");
03551
03552
03553 if (p->vad)
03554 ast_dsp_free(p->vad);
03555
03556 p->owner = NULL;
03557 ast->tech_pvt = NULL;
03558
03559 ast_module_unref(ast_module_info->self);
03560
03561
03562
03563
03564
03565
03566
03567 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE))
03568 needdestroy = 1;
03569 else if (p->invitestate != INV_CALLING)
03570 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03571
03572
03573 if (!ast_test_flag(&p->flags[0], SIP_ALREADYGONE) && !ast_strlen_zero(p->initreq.data)) {
03574 if (needcancel) {
03575 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03576
03577 __sip_pretend_ack(p);
03578 p->invitestate = INV_CANCELLED;
03579
03580
03581 if (p->invitestate == INV_CALLING) {
03582
03583 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03584
03585 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03586 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
03587 } else {
03588
03589 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
03590
03591
03592 needdestroy = 0;
03593 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03594 }
03595 if ( p->initid != -1 ) {
03596
03597
03598 update_call_counter(p, INC_CALL_LIMIT);
03599 }
03600 } else {
03601 const char *res;
03602 if (ast->hangupcause && (res = hangup_cause2sip(ast->hangupcause)))
03603 transmit_response_reliable(p, res, &p->initreq);
03604 else
03605 transmit_response_reliable(p, "603 Declined", &p->initreq);
03606 p->invitestate = INV_TERMINATED;
03607 }
03608 } else {
03609 if (!p->pendinginvite) {
03610 char *audioqos = "";
03611 char *videoqos = "";
03612 if (p->rtp)
03613 audioqos = ast_rtp_get_quality(p->rtp, NULL);
03614 if (p->vrtp)
03615 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
03616
03617 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03618
03619
03620 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
03621 if (p->rtp)
03622 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
03623 if (p->vrtp)
03624 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
03625 }
03626 if (p->rtp && oldowner)
03627 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", audioqos);
03628 if (p->vrtp && oldowner)
03629 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", videoqos);
03630 } else {
03631
03632
03633 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03634 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
03635 AST_SCHED_DEL(sched, p->waitid);
03636 if (sip_cancel_destroy(p))
03637 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03638 }
03639 }
03640 }
03641 if (needdestroy)
03642 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
03643 ast_mutex_unlock(&p->lock);
03644 return 0;
03645 }
03646
03647
03648 static void try_suggested_sip_codec(struct sip_pvt *p)
03649 {
03650 int fmt;
03651 const char *codec;
03652
03653 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
03654 if (!codec)
03655 return;
03656
03657 fmt = ast_getformatbyname(codec);
03658 if (fmt) {
03659 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
03660 if (p->jointcapability & fmt) {
03661 p->jointcapability &= fmt;
03662 p->capability &= fmt;
03663 } else
03664 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
03665 } else
03666 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
03667 return;
03668 }
03669
03670
03671
03672 static int sip_answer(struct ast_channel *ast)
03673 {
03674 int res = 0;
03675 struct sip_pvt *p = ast->tech_pvt;
03676
03677 ast_mutex_lock(&p->lock);
03678 if (ast->_state != AST_STATE_UP) {
03679 try_suggested_sip_codec(p);
03680
03681 ast_setstate(ast, AST_STATE_UP);
03682 if (option_debug)
03683 ast_log(LOG_DEBUG, "SIP answering channel: %s\n", ast->name);
03684 if (p->t38.state == T38_PEER_DIRECT) {
03685 p->t38.state = T38_ENABLED;
03686 if (option_debug > 1)
03687 ast_log(LOG_DEBUG,"T38State change to %d on channel %s\n", p->t38.state, ast->name);
03688 res = transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
03689 } else {
03690 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
03691 }
03692 }
03693 ast_mutex_unlock(&p->lock);
03694 return res;
03695 }
03696
03697
03698 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
03699 {
03700 struct sip_pvt *p = ast->tech_pvt;
03701 int res = 0;
03702
03703 switch (frame->frametype) {
03704 case AST_FRAME_VOICE:
03705 if (!(frame->subclass & ast->nativeformats)) {
03706 char s1[512], s2[512], s3[512];
03707 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %s(%d) read/write = %s(%d)/%s(%d)\n",
03708 frame->subclass,
03709 ast_getformatname_multiple(s1, sizeof(s1) - 1, ast->nativeformats & AST_FORMAT_AUDIO_MASK),
03710 ast->nativeformats & AST_FORMAT_AUDIO_MASK,
03711 ast_getformatname_multiple(s2, sizeof(s2) - 1, ast->readformat),
03712 ast->readformat,
03713 ast_getformatname_multiple(s3, sizeof(s3) - 1, ast->writeformat),
03714 ast->writeformat);
03715 return 0;
03716 }
03717 if (p) {
03718 ast_mutex_lock(&p->lock);
03719 if (p->rtp) {
03720
03721 if ((ast->_state != AST_STATE_UP) &&
03722 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03723 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03724 ast_rtp_update_source(p->rtp);
03725 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03726 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03727 }
03728 p->lastrtptx = time(NULL);
03729 res = ast_rtp_write(p->rtp, frame);
03730 }
03731 ast_mutex_unlock(&p->lock);
03732 }
03733 break;
03734 case AST_FRAME_VIDEO:
03735 if (p) {
03736 ast_mutex_lock(&p->lock);
03737 if (p->vrtp) {
03738
03739 if ((ast->_state != AST_STATE_UP) &&
03740 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03741 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03742 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03743 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03744 }
03745 p->lastrtptx = time(NULL);
03746 res = ast_rtp_write(p->vrtp, frame);
03747 }
03748 ast_mutex_unlock(&p->lock);
03749 }
03750 break;
03751 case AST_FRAME_IMAGE:
03752 return 0;
03753 break;
03754 case AST_FRAME_MODEM:
03755 if (p) {
03756 ast_mutex_lock(&p->lock);
03757
03758
03759
03760
03761 if (p->udptl && ast->_state == AST_STATE_UP)
03762 res = ast_udptl_write(p->udptl, frame);
03763 ast_mutex_unlock(&p->lock);
03764 }
03765 break;
03766 default:
03767 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
03768 return 0;
03769 }
03770
03771 return res;
03772 }
03773
03774
03775
03776 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03777 {
03778 int ret = -1;
03779 struct sip_pvt *p;
03780
03781 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE) && option_debug)
03782 ast_log(LOG_DEBUG, "New channel is zombie\n");
03783 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE) && option_debug)
03784 ast_log(LOG_DEBUG, "Old channel is zombie\n");
03785
03786 if (!newchan || !newchan->tech_pvt) {
03787 if (!newchan)
03788 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
03789 else
03790 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
03791 return -1;
03792 }
03793 p = newchan->tech_pvt;
03794
03795 if (!p) {
03796 ast_log(LOG_WARNING, "No pvt after masquerade. Strange things may happen\n");
03797 return -1;
03798 }
03799
03800 ast_mutex_lock(&p->lock);
03801 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
03802 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
03803 if (p->owner != oldchan)
03804 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
03805 else {
03806 p->owner = newchan;
03807
03808
03809
03810
03811
03812
03813 sip_set_rtp_peer(newchan, NULL, NULL, 0, 0);
03814 ret = 0;
03815 }
03816 if (option_debug > 2)
03817 ast_log(LOG_DEBUG, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
03818
03819 ast_mutex_unlock(&p->lock);
03820 return ret;
03821 }
03822
03823 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
03824 {
03825 struct sip_pvt *p = ast->tech_pvt;
03826 int res = 0;
03827
03828 ast_mutex_lock(&p->lock);
03829 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
03830 case SIP_DTMF_INBAND:
03831 res = -1;
03832 break;
03833 case SIP_DTMF_RFC2833:
03834 if (p->rtp)
03835 ast_rtp_senddigit_begin(p->rtp, digit);
03836 break;
03837 default:
03838 break;
03839 }
03840 ast_mutex_unlock(&p->lock);
03841
03842 return res;
03843 }
03844
03845
03846
03847 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
03848 {
03849 struct sip_pvt *p = ast->tech_pvt;
03850 int res = 0;
03851
03852 ast_mutex_lock(&p->lock);
03853 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
03854 case SIP_DTMF_INFO:
03855 transmit_info_with_digit(p, digit, duration);
03856 break;
03857 case SIP_DTMF_RFC2833:
03858 if (p->rtp)
03859 ast_rtp_senddigit_end(p->rtp, digit);
03860 break;
03861 case SIP_DTMF_INBAND:
03862 res = -1;
03863 break;
03864 }
03865 ast_mutex_unlock(&p->lock);
03866
03867 return res;
03868 }
03869
03870
03871 static int sip_transfer(struct ast_channel *ast, const char *dest)
03872 {
03873 struct sip_pvt *p = ast->tech_pvt;
03874 int res;
03875
03876 if (dest == NULL)
03877 dest = "";
03878 ast_mutex_lock(&p->lock);
03879 if (ast->_state == AST_STATE_RING)
03880 res = sip_sipredirect(p, dest);
03881 else
03882 res = transmit_refer(p, dest);
03883 ast_mutex_unlock(&p->lock);
03884 return res;
03885 }
03886
03887
03888
03889
03890
03891
03892 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
03893 {
03894 struct sip_pvt *p = ast->tech_pvt;
03895 int res = 0;
03896
03897 ast_mutex_lock(&p->lock);
03898 switch(condition) {
03899 case AST_CONTROL_RINGING:
03900 if (ast->_state == AST_STATE_RING) {
03901 p->invitestate = INV_EARLY_MEDIA;
03902 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
03903 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
03904
03905 transmit_response(p, "180 Ringing", &p->initreq);
03906 ast_set_flag(&p->flags[0], SIP_RINGING);
03907 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
03908 break;
03909 } else {
03910
03911 }
03912 }
03913 res = -1;
03914 break;
03915 case AST_CONTROL_BUSY:
03916 if (ast->_state != AST_STATE_UP) {
03917 transmit_response(p, "486 Busy Here", &p->initreq);
03918 p->invitestate = INV_COMPLETED;
03919 sip_alreadygone(p);
03920 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
03921 break;
03922 }
03923 res = -1;
03924 break;
03925 case AST_CONTROL_CONGESTION:
03926 if (ast->_state != AST_STATE_UP) {
03927 transmit_response(p, "503 Service Unavailable", &p->initreq);
03928 p->invitestate = INV_COMPLETED;
03929 sip_alreadygone(p);
03930 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
03931 break;
03932 }
03933 res = -1;
03934 break;
03935 case AST_CONTROL_PROCEEDING:
03936 if ((ast->_state != AST_STATE_UP) &&
03937 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03938 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03939 transmit_response(p, "100 Trying", &p->initreq);
03940 p->invitestate = INV_PROCEEDING;
03941 break;
03942 }
03943 res = -1;
03944 break;
03945 case AST_CONTROL_PROGRESS:
03946 if ((ast->_state != AST_STATE_UP) &&
03947 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03948 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03949 p->invitestate = INV_EARLY_MEDIA;
03950 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03951 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03952 break;
03953 }
03954 res = -1;
03955 break;
03956 case AST_CONTROL_HOLD:
03957 ast_rtp_update_source(p->rtp);
03958 ast_moh_start(ast, data, p->mohinterpret);
03959 break;
03960 case AST_CONTROL_UNHOLD:
03961 ast_rtp_update_source(p->rtp);
03962 ast_moh_stop(ast);
03963 break;
03964 case AST_CONTROL_VIDUPDATE:
03965 if (p->vrtp && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
03966 transmit_info_with_vidupdate(p);
03967
03968 } else
03969 res = -1;
03970 break;
03971 case AST_CONTROL_SRCUPDATE:
03972 ast_rtp_update_source(p->rtp);
03973 break;
03974 case AST_CONTROL_SRCCHANGE:
03975 ast_rtp_change_source(p->rtp);
03976 break;
03977 case -1:
03978 res = -1;
03979 break;
03980 default:
03981 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
03982 res = -1;
03983 break;
03984 }
03985 ast_mutex_unlock(&p->lock);
03986 return res;
03987 }
03988
03989
03990
03991
03992
03993
03994
03995 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
03996 {
03997 struct ast_channel *tmp;
03998 struct ast_variable *v = NULL;
03999 int fmt;
04000 int what;
04001 int needvideo = 0, video = 0;
04002 char *decoded_exten;
04003 {
04004 const char *my_name;
04005
04006 if (title)
04007 my_name = title;
04008 else if ( (my_name = strchr(i->fromdomain,':')) )
04009 my_name++;
04010 else
04011 my_name = i->fromdomain;
04012
04013 ast_mutex_unlock(&i->lock);
04014
04015 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, "SIP/%s-%08x", my_name, (int)(long) i);
04016
04017 }
04018 if (!tmp) {
04019 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
04020 ast_mutex_lock(&i->lock);
04021 return NULL;
04022 }
04023 ast_mutex_lock(&i->lock);
04024
04025 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO)
04026 tmp->tech = &sip_tech_info;
04027 else
04028 tmp->tech = &sip_tech;
04029
04030
04031
04032 if (i->jointcapability) {
04033 what = i->jointcapability;
04034 video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
04035 } else if (i->capability) {
04036 what = i->capability;
04037 video = i->capability & AST_FORMAT_VIDEO_MASK;
04038 } else {
04039 what = global_capability;
04040 video = global_capability & AST_FORMAT_VIDEO_MASK;
04041 }
04042
04043
04044 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video;
04045 if (option_debug > 2) {
04046 char buf[SIPBUFSIZE];
04047 ast_log(LOG_DEBUG, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
04048 ast_log(LOG_DEBUG, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
04049 ast_log(LOG_DEBUG, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
04050 ast_log(LOG_DEBUG, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
04051 if (i->prefcodec)
04052 ast_log(LOG_DEBUG, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
04053 }
04054
04055
04056 fmt = ast_best_codec(tmp->nativeformats);
04057
04058
04059
04060
04061
04062 if (i->vrtp) {
04063 if (i->prefcodec)
04064 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
04065 else
04066 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
04067 }
04068
04069 if (option_debug > 2) {
04070 if (needvideo)
04071 ast_log(LOG_DEBUG, "This channel can handle video! HOLLYWOOD next!\n");
04072 else
04073 ast_log(LOG_DEBUG, "This channel will not be able to handle video.\n");
04074 }
04075
04076
04077
04078 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
04079 i->vad = ast_dsp_new();
04080 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
04081 if (global_relaxdtmf)
04082 ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
04083 }
04084 if (i->rtp) {
04085 tmp->fds[0] = ast_rtp_fd(i->rtp);
04086 tmp->fds[1] = ast_rtcp_fd(i->rtp);
04087 }
04088 if (needvideo && i->vrtp) {
04089 tmp->fds[2] = ast_rtp_fd(i->vrtp);
04090 tmp->fds[3] = ast_rtcp_fd(i->vrtp);
04091 }
04092 if (i->udptl) {
04093 tmp->fds[5] = ast_udptl_fd(i->udptl);
04094 }
04095 if (state == AST_STATE_RING)
04096 tmp->rings = 1;
04097 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
04098 tmp->writeformat = fmt;
04099 tmp->rawwriteformat = fmt;
04100 tmp->readformat = fmt;
04101 tmp->rawreadformat = fmt;
04102 tmp->tech_pvt = i;
04103
04104 tmp->callgroup = i->callgroup;
04105 tmp->pickupgroup = i->pickupgroup;
04106 tmp->cid.cid_pres = i->callingpres;
04107 if (!ast_strlen_zero(i->accountcode))
04108 ast_string_field_set(tmp, accountcode, i->accountcode);
04109 if (i->amaflags)
04110 tmp->amaflags = i->amaflags;
04111 if (!ast_strlen_zero(i->language))
04112 ast_string_field_set(tmp, language, i->language);
04113 i->owner = tmp;
04114 ast_module_ref(ast_module_info->self);
04115 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
04116
04117
04118
04119
04120 decoded_exten = ast_strdupa(i->exten);
04121 ast_uri_decode(decoded_exten);
04122 ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
04123
04124
04125
04126 tmp->cid.cid_ani = ast_strdup(i->cid_num);
04127 if (!ast_strlen_zero(i->rdnis))
04128 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
04129
04130 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
04131 tmp->cid.cid_dnid = ast_strdup(i->exten);
04132
04133 tmp->priority = 1;
04134 if (!ast_strlen_zero(i->uri))
04135 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
04136 if (!ast_strlen_zero(i->domain))
04137 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
04138 if (!ast_strlen_zero(i->useragent))
04139 pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
04140 if (!ast_strlen_zero(i->callid))
04141 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
04142 if (i->rtp)
04143 ast_jb_configure(tmp, &global_jbconf);
04144
04145
04146 if (i->udptl && i->t38.state == T38_PEER_DIRECT)
04147 pbx_builtin_setvar_helper(tmp, "_T38CALL", "1");
04148
04149
04150 for (v = i->chanvars ; v ; v = v->next)
04151 pbx_builtin_setvar_helper(tmp, v->name, v->value);
04152
04153 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
04154 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
04155 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
04156 ast_hangup(tmp);
04157 tmp = NULL;
04158 }
04159
04160 if (!ast_test_flag(&i->flags[0], SIP_NO_HISTORY))
04161 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
04162
04163 return tmp;
04164 }
04165
04166
04167 static char *get_body_by_line(const char *line, const char *name, int nameLen)
04168 {
04169 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=')
04170 return ast_skip_blanks(line + nameLen + 1);
04171
04172 return "";
04173 }
04174
04175
04176
04177
04178
04179 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
04180 {
04181 int len = strlen(name);
04182
04183 while (*start < req->sdp_end) {
04184 const char *r = get_body_by_line(req->line[(*start)++], name, len);
04185 if (r[0] != '\0')
04186 return r;
04187 }
04188
04189 return "";
04190 }
04191
04192
04193 static const char *get_sdp(struct sip_request *req, const char *name)
04194 {
04195 int dummy = 0;
04196
04197 return get_sdp_iterate(&dummy, req, name);
04198 }
04199
04200
04201 static char *get_body(struct sip_request *req, char *name)
04202 {
04203 int x;
04204 int len = strlen(name);
04205 char *r;
04206
04207 for (x = 0; x < req->lines; x++) {
04208 r = get_body_by_line(req->line[x], name, len);
04209 if (r[0] != '\0')
04210 return r;
04211 }
04212
04213 return "";
04214 }
04215
04216
04217 static const char *find_alias(const char *name, const char *_default)
04218 {
04219
04220 static const struct cfalias {
04221 char * const fullname;
04222 char * const shortname;
04223 } aliases[] = {
04224 { "Content-Type", "c" },
04225 { "Content-Encoding", "e" },
04226 { "From", "f" },
04227 { "Call-ID", "i" },
04228 { "Contact", "m" },
04229 { "Content-Length", "l" },
04230 { "Subject", "s" },
04231 { "To", "t" },
04232 { "Supported", "k" },
04233 { "Refer-To", "r" },
04234 { "Referred-By", "b" },
04235 { "Allow-Events", "u" },
04236 { "Event", "o" },
04237 { "Via", "v" },
04238 { "Accept-Contact", "a" },
04239 { "Reject-Contact", "j" },
04240 { "Request-Disposition", "d" },
04241 { "Session-Expires", "x" },
04242 { "Identity", "y" },
04243 { "Identity-Info", "n" },
04244 };
04245 int x;
04246
04247 for (x=0; x<sizeof(aliases) / sizeof(aliases[0]); x++)
04248 if (!strcasecmp(aliases[x].fullname, name))
04249 return aliases[x].shortname;
04250
04251 return _default;
04252 }
04253
04254 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
04255 {
04256 int pass;
04257
04258
04259
04260
04261
04262
04263
04264
04265
04266
04267 for (pass = 0; name && pass < 2;pass++) {
04268 int x, len = strlen(name);
04269 for (x=*start; x<req->headers; x++) {
04270 if (!strncasecmp(req->header[x], name, len)) {
04271 char *r = req->header[x] + len;
04272 if (pedanticsipchecking)
04273 r = ast_skip_blanks(r);
04274
04275 if (*r == ':') {
04276 *start = x+1;
04277 return ast_skip_blanks(r+1);
04278 }
04279 }
04280 }
04281 if (pass == 0)
04282 name = find_alias(name, NULL);
04283 }
04284
04285
04286 return "";
04287 }
04288
04289
04290 static const char *get_header(const struct sip_request *req, const char *name)
04291 {
04292 int start = 0;
04293 return __get_header(req, name, &start);
04294 }
04295
04296
04297 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
04298 {
04299
04300 struct ast_frame *f;
04301
04302 if (!p->rtp) {
04303
04304 return &ast_null_frame;
04305 }
04306
04307 switch(ast->fdno) {
04308 case 0:
04309 f = ast_rtp_read(p->rtp);
04310 break;
04311 case 1:
04312 f = ast_rtcp_read(p->rtp);
04313 break;
04314 case 2:
04315 f = ast_rtp_read(p->vrtp);
04316 break;
04317 case 3:
04318 f = ast_rtcp_read(p->vrtp);
04319 break;
04320 case 5:
04321 f = ast_udptl_read(p->udptl);
04322 break;
04323 default:
04324 f = &ast_null_frame;
04325 }
04326
04327 if (f && (f->frametype == AST_FRAME_DTMF) &&
04328 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833))
04329 return &ast_null_frame;
04330
04331
04332 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
04333 return f;
04334
04335 if (f && f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
04336 if (!(f->subclass & p->jointcapability)) {
04337 if (option_debug) {
04338 ast_log(LOG_DEBUG, "Bogus frame of format '%s' received from '%s'!\n",
04339 ast_getformatname(f->subclass), p->owner->name);
04340 }
04341 return &ast_null_frame;
04342 }
04343 if (option_debug)
04344 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
04345 p->owner->nativeformats = (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
04346 ast_set_read_format(p->owner, p->owner->readformat);
04347 ast_set_write_format(p->owner, p->owner->writeformat);
04348 }
04349
04350 if (f && (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
04351 f = ast_dsp_process(p->owner, p->vad, f);
04352 if (f && f->frametype == AST_FRAME_DTMF) {
04353 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && f->subclass == 'f') {
04354 if (option_debug)
04355 ast_log(LOG_DEBUG, "Fax CNG detected on %s\n", ast->name);
04356 *faxdetect = 1;
04357 } else if (option_debug) {
04358 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
04359 }
04360 }
04361 }
04362
04363 return f;
04364 }
04365
04366
04367 static struct ast_frame *sip_read(struct ast_channel *ast)
04368 {
04369 struct ast_frame *fr;
04370 struct sip_pvt *p = ast->tech_pvt;
04371 int faxdetected = FALSE;
04372
04373 ast_mutex_lock(&p->lock);
04374 fr = sip_rtp_read(ast, p, &faxdetected);
04375 p->lastrtprx = time(NULL);
04376
04377
04378
04379 if (faxdetected && ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && (p->t38.state == T38_DISABLED) && !(ast_bridged_channel(ast))) {
04380 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
04381 if (!p->pendinginvite) {
04382 if (option_debug > 2)
04383 ast_log(LOG_DEBUG, "Sending reinvite on SIP (%s) for T.38 negotiation.\n",ast->name);
04384 p->t38.state = T38_LOCAL_REINVITE;
04385 transmit_reinvite_with_t38_sdp(p);
04386 if (option_debug > 1)
04387 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, ast->name);
04388 }
04389 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04390 if (option_debug > 2)
04391 ast_log(LOG_DEBUG, "Deferring reinvite on SIP (%s) - it will be re-negotiated for T.38\n", ast->name);
04392 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04393 }
04394 }
04395
04396
04397 if (fr->frametype == AST_FRAME_VOICE && p->invitestate != INV_EARLY_MEDIA && ast->_state != AST_STATE_UP) {
04398 fr = &ast_null_frame;
04399 }
04400
04401 ast_mutex_unlock(&p->lock);
04402 return fr;
04403 }
04404
04405
04406
04407 static char *generate_random_string(char *buf, size_t size)
04408 {
04409 long val[4];
04410 int x;
04411
04412 for (x=0; x<4; x++)
04413 val[x] = ast_random();
04414 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
04415
04416 return buf;
04417 }
04418
04419
04420 static void build_callid_pvt(struct sip_pvt *pvt)
04421 {
04422 char buf[33];
04423
04424 const char *host = S_OR(pvt->fromdomain, ast_inet_ntoa(pvt->ourip));
04425
04426 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04427
04428 }
04429
04430
04431 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain)
04432 {
04433 char buf[33];
04434
04435 const char *host = S_OR(fromdomain, ast_inet_ntoa(ourip));
04436
04437 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04438 }
04439
04440
04441 static void make_our_tag(char *tagbuf, size_t len)
04442 {
04443 snprintf(tagbuf, len, "as%08lx", ast_random());
04444 }
04445
04446
04447 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
04448 int useglobal_nat, const int intended_method)
04449 {
04450 struct sip_pvt *p;
04451
04452 if (!(p = ast_calloc(1, sizeof(*p))))
04453 return NULL;
04454
04455 if (ast_string_field_init(p, 512)) {
04456 free(p);
04457 return NULL;
04458 }
04459
04460 ast_mutex_init(&p->lock);
04461
04462 p->method = intended_method;
04463 p->initid = -1;
04464 p->waitid = -1;
04465 p->autokillid = -1;
04466 p->subscribed = NONE;
04467 p->stateid = -1;
04468 p->prefs = default_prefs;
04469
04470 if (intended_method != SIP_OPTIONS)
04471 p->timer_t1 = 500;
04472
04473 if (sin) {
04474 p->sa = *sin;
04475 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
04476 p->ourip = __ourip;
04477 } else
04478 p->ourip = __ourip;
04479
04480
04481 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
04482 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
04483
04484 ast_set2_flag(&p->flags[0], !recordhistory, SIP_NO_HISTORY);
04485
04486 p->branch = ast_random();
04487 make_our_tag(p->tag, sizeof(p->tag));
04488 p->ocseq = INITIAL_CSEQ;
04489
04490 if (sip_methods[intended_method].need_rtp) {
04491 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04492
04493 if (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT))
04494 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04495 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT))
04496 p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
04497 if (!p->rtp || (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) && !p->vrtp)) {
04498 ast_log(LOG_WARNING, "Unable to create RTP audio %s session: %s\n",
04499 ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "and video" : "", strerror(errno));
04500 ast_mutex_destroy(&p->lock);
04501 if (p->chanvars) {
04502 ast_variables_destroy(p->chanvars);
04503 p->chanvars = NULL;
04504 }
04505 free(p);
04506 return NULL;
04507 }
04508 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04509 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04510 ast_rtp_settos(p->rtp, global_tos_audio);
04511 ast_rtp_set_rtptimeout(p->rtp, global_rtptimeout);
04512 ast_rtp_set_rtpholdtimeout(p->rtp, global_rtpholdtimeout);
04513 ast_rtp_set_rtpkeepalive(p->rtp, global_rtpkeepalive);
04514 if (p->vrtp) {
04515 ast_rtp_settos(p->vrtp, global_tos_video);
04516 ast_rtp_setdtmf(p->vrtp, 0);
04517 ast_rtp_setdtmfcompensate(p->vrtp, 0);
04518 ast_rtp_set_rtptimeout(p->vrtp, global_rtptimeout);
04519 ast_rtp_set_rtpholdtimeout(p->vrtp, global_rtpholdtimeout);
04520 ast_rtp_set_rtpkeepalive(p->vrtp, global_rtpkeepalive);
04521 }
04522 if (p->udptl)
04523 ast_udptl_settos(p->udptl, global_tos_audio);
04524 p->maxcallbitrate = default_maxcallbitrate;
04525 p->autoframing = global_autoframing;
04526 ast_rtp_codec_setpref(p->rtp, &p->prefs);
04527 }
04528
04529 if (useglobal_nat && sin) {
04530
04531 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
04532 p->recv = *sin;
04533 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04534 }
04535
04536 if (p->method != SIP_REGISTER)
04537 ast_string_field_set(p, fromdomain, default_fromdomain);
04538 build_via(p);
04539 if (!callid)
04540 build_callid_pvt(p);
04541 else
04542 ast_string_field_set(p, callid, callid);
04543
04544 ast_string_field_set(p, mohinterpret, default_mohinterpret);
04545 ast_string_field_set(p, mohsuggest, default_mohsuggest);
04546 p->capability = global_capability;
04547 p->allowtransfer = global_allowtransfer;
04548 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
04549 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
04550 p->noncodeccapability |= AST_RTP_DTMF;
04551 if (p->udptl) {
04552 p->t38.capability = global_t38_capability;
04553 if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY)
04554 p->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
04555 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_FEC)
04556 p->t38.capability |= T38FAX_UDP_EC_FEC;
04557 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_NONE)
04558 p->t38.capability |= T38FAX_UDP_EC_NONE;
04559 p->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
04560 p->t38.jointcapability = p->t38.capability;
04561 }
04562 ast_string_field_set(p, context, default_context);
04563
04564
04565 ast_mutex_lock(&iflock);
04566 p->next = iflist;
04567 iflist = p;
04568 ast_mutex_unlock(&iflock);
04569 if (option_debug)
04570 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");
04571 return p;
04572 }
04573
04574
04575
04576 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
04577 {
04578 struct sip_pvt *p = NULL;
04579 char *tag = "";
04580 char totag[128];
04581 char fromtag[128];
04582 const char *callid = get_header(req, "Call-ID");
04583 const char *from = get_header(req, "From");
04584 const char *to = get_header(req, "To");
04585 const char *cseq = get_header(req, "Cseq");
04586
04587
04588
04589 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
04590 ast_strlen_zero(from) || ast_strlen_zero(cseq))
04591 return NULL;
04592
04593 if (pedanticsipchecking) {
04594
04595
04596
04597
04598
04599
04600 if (gettag(req, "To", totag, sizeof(totag)))
04601 ast_set_flag(req, SIP_PKT_WITH_TOTAG);
04602 gettag(req, "From", fromtag, sizeof(fromtag));
04603
04604 tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
04605
04606 if (option_debug > 4 )
04607 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);
04608 }
04609
04610 ast_mutex_lock(&iflock);
04611 for (p = iflist; p; p = p->next) {
04612
04613 int found = FALSE;
04614 if (ast_strlen_zero(p->callid))
04615 continue;
04616 if (req->method == SIP_REGISTER)
04617 found = (!strcmp(p->callid, callid));
04618 else
04619 found = (!strcmp(p->callid, callid) &&
04620 (!pedanticsipchecking || ast_strlen_zero(tag) || ast_strlen_zero(p->theirtag) || !strcmp(p->theirtag, tag))) ;
04621
04622 if (option_debug > 4)
04623 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);
04624
04625
04626 if (pedanticsipchecking && found && req->method != SIP_RESPONSE) {
04627 if (p->tag[0] == '\0' && totag[0]) {
04628
04629 found = FALSE;
04630 } else if (totag[0]) {
04631 if (strcmp(totag, p->tag)) {
04632 found = FALSE;
04633 }
04634 }
04635 if (!found && option_debug > 4)
04636 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);
04637 }
04638
04639
04640 if (found) {
04641
04642 ast_mutex_lock(&p->lock);
04643 ast_mutex_unlock(&iflock);
04644 return p;
04645 }
04646 }
04647 ast_mutex_unlock(&iflock);
04648
04649
04650 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
04651 if (intended_method == SIP_REFER) {
04652
04653 transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
04654 } else if (intended_method == SIP_NOTIFY) {
04655
04656
04657 transmit_response_using_temp(callid, sin, 1, intended_method, req, "489 Bad event");
04658 } else {
04659
04660 if ((p = sip_alloc(callid, sin, 1, intended_method))) {
04661
04662 ast_mutex_lock(&p->lock);
04663 } else {
04664
04665
04666
04667
04668
04669
04670
04671
04672 transmit_response_using_temp(callid, sin, 1, intended_method, req, "500 Server internal error");
04673 if (option_debug > 3)
04674 ast_log(LOG_DEBUG, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
04675 }
04676 }
04677 return p;
04678 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
04679
04680 transmit_response_using_temp(callid, sin, 1, intended_method, req, "501 Method Not Implemented");
04681 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
04682
04683
04684
04685 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 Call leg/transaction does not exist");
04686 }
04687
04688
04689
04690 return p;
04691 }
04692
04693
04694 static int sip_register(char *value, int lineno)
04695 {
04696 struct sip_registry *reg;
04697 int portnum = 0;
04698 char username[256] = "";
04699 char *hostname=NULL, *secret=NULL, *authuser=NULL;
04700 char *porta=NULL;
04701 char *contact=NULL;
04702
04703 if (!value)
04704 return -1;
04705 ast_copy_string(username, value, sizeof(username));
04706
04707 hostname = strrchr(username, '@');
04708 if (hostname)
04709 *hostname++ = '\0';
04710 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
04711 ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
04712 return -1;
04713 }
04714
04715 secret = strchr(username, ':');
04716 if (secret) {
04717 *secret++ = '\0';
04718 authuser = strchr(secret, ':');
04719 if (authuser)
04720 *authuser++ = '\0';
04721 }
04722
04723 contact = strchr(hostname, '/');
04724 if (contact)
04725 *contact++ = '\0';
04726 if (ast_strlen_zero(contact))
04727 contact = "s";
04728 porta = strchr(hostname, ':');
04729 if (porta) {
04730 *porta++ = '\0';
04731 portnum = atoi(porta);
04732 if (portnum == 0) {
04733 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
04734 return -1;
04735 }
04736 }
04737 if (!(reg = ast_calloc(1, sizeof(*reg)))) {
04738 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
04739 return -1;
04740 }
04741
04742 if (ast_string_field_init(reg, 256)) {
04743 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry strings\n");
04744 free(reg);
04745 return -1;
04746 }
04747
04748 regobjs++;
04749 ASTOBJ_INIT(reg);
04750 ast_string_field_set(reg, contact, contact);
04751 if (!ast_strlen_zero(username))
04752 ast_string_field_set(reg, username, username);
04753 if (hostname)
04754 ast_string_field_set(reg, hostname, hostname);
04755 if (authuser)
04756 ast_string_field_set(reg, authuser, authuser);
04757 if (secret)
04758 ast_string_field_set(reg, secret, secret);
04759 reg->expire = -1;
04760 reg->timeout = -1;
04761 reg->refresh = default_expiry;
04762 reg->portno = portnum;
04763 reg->callid_valid = FALSE;
04764 reg->ocseq = INITIAL_CSEQ;
04765 ASTOBJ_CONTAINER_LINK(®l, reg);
04766 ASTOBJ_UNREF(reg,sip_registry_destroy);
04767 return 0;
04768 }
04769
04770
04771
04772 static int lws2sws(char *msgbuf, int len)
04773 {
04774 int h = 0, t = 0;
04775 int lws = 0;
04776
04777 for (; h < len;) {
04778
04779 if (msgbuf[h] == '\r') {
04780 h++;
04781 continue;
04782 }
04783
04784 if (msgbuf[h] == '\n') {
04785
04786 if (h + 1 == len)
04787 break;
04788
04789 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
04790
04791 h++;
04792 continue;
04793 }
04794
04795 msgbuf[t++] = msgbuf[h++];
04796 lws = 0;
04797 continue;
04798 }
04799 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
04800 if (lws) {
04801 h++;
04802 continue;
04803 }
04804 msgbuf[t++] = msgbuf[h++];
04805 lws = 1;
04806 continue;
04807 }
04808 msgbuf[t++] = msgbuf[h++];
04809 if (lws)
04810 lws = 0;
04811 }
04812 msgbuf[t] = '\0';
04813 return t;
04814 }
04815
04816
04817
04818
04819 static int parse_request(struct sip_request *req)
04820 {
04821
04822 char *c;
04823 int f = 0;
04824
04825 c = req->data;
04826
04827
04828 req->header[f] = c;
04829 while(*c) {
04830 if (*c == '\n') {
04831
04832 *c = 0;
04833
04834 if (sipdebug && option_debug > 3)
04835 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
04836 if (ast_strlen_zero(req->header[f])) {
04837
04838 c++;
04839 break;
04840 }
04841 if (f >= SIP_MAX_HEADERS - 1) {
04842 ast_log(LOG_WARNING, "Too many SIP headers. Ignoring.\n");
04843 } else
04844 f++;
04845 req->header[f] = c + 1;
04846 } else if (*c == '\r') {
04847
04848 *c = 0;
04849 }
04850 c++;
04851 }
04852
04853 if (!ast_strlen_zero(req->header[f])) {
04854 if (sipdebug && option_debug > 3)
04855 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
04856 f++;
04857 }
04858 req->headers = f;
04859
04860 f = 0;
04861 req->line[f] = c;
04862 while(*c) {
04863 if (*c == '\n') {
04864
04865 *c = 0;
04866 if (sipdebug && option_debug > 3)
04867 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
04868 if (f >= SIP_MAX_LINES - 1) {
04869 ast_log(LOG_WARNING, "Too many SDP lines. Ignoring.\n");
04870 } else
04871 f++;
04872 req->line[f] = c + 1;
04873 } else if (*c == '\r') {
04874
04875 *c = 0;
04876 }
04877 c++;
04878 }
04879
04880 if (!ast_strlen_zero(req->line[f]))
04881 f++;
04882 req->lines = f;
04883 if (*c)
04884 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
04885
04886 return determine_firstline_parts(req);
04887 }
04888
04889
04890
04891
04892
04893
04894
04895
04896
04897 static int find_sdp(struct sip_request *req)
04898 {
04899 const char *content_type;
04900 const char *content_length;
04901 const char *search;
04902 char *boundary;
04903 unsigned int x;
04904 int boundaryisquoted = FALSE;
04905 int found_application_sdp = FALSE;
04906 int found_end_of_headers = FALSE;
04907
04908 content_length = get_header(req, "Content-Length");
04909
04910 if (!ast_strlen_zero(content_length)) {
04911 if (sscanf(content_length, "%ud", &x) != 1) {
04912 ast_log(LOG_WARNING, "Invalid Content-Length: %s\n", content_length);
04913 return 0;
04914 }
04915
04916
04917
04918 if (x == 0)
04919 return 0;
04920 }
04921
04922 content_type = get_header(req, "Content-Type");
04923
04924
04925 if (!strcasecmp(content_type, "application/sdp")) {
04926 req->sdp_start = 0;
04927 req->sdp_end = req->lines;
04928 return req->lines ? 1 : 0;
04929 }
04930
04931
04932 if (strncasecmp(content_type, "multipart/mixed", 15))
04933 return 0;
04934
04935
04936 if ((search = strcasestr(content_type, ";boundary=")))
04937 search += 10;
04938 else if ((search = strcasestr(content_type, "; boundary=")))
04939 search += 11;
04940 else
04941 return 0;
04942
04943 if (ast_strlen_zero(search))
04944 return 0;
04945
04946
04947 if (*search == '\"') {
04948 search++;
04949 boundaryisquoted = TRUE;
04950 }
04951
04952
04953
04954 boundary = ast_strdupa(search - 2);
04955 boundary[0] = boundary[1] = '-';
04956
04957 if (boundaryisquoted)
04958 boundary[strlen(boundary) - 1] = '\0';
04959
04960
04961
04962
04963 for (x = 0; x < (req->lines ); x++) {
04964 if(!strncasecmp(req->line[x], boundary, strlen(boundary))){
04965 if(found_application_sdp && found_end_of_headers){
04966 req->sdp_end = x-1;
04967 return 1;
04968 }
04969 found_application_sdp = FALSE;
04970 }
04971 if(!strcasecmp(req->line[x], "Content-Type: application/sdp"))
04972 found_application_sdp = TRUE;
04973
04974 if(strlen(req->line[x]) == 0 ){
04975 if(found_application_sdp && !found_end_of_headers){
04976 req->sdp_start = x;
04977 found_end_of_headers = TRUE;
04978 }
04979 }
04980 }
04981 if(found_application_sdp && found_end_of_headers) {
04982 req->sdp_end = x;
04983 return TRUE;
04984 }
04985 return FALSE;
04986 }
04987
04988
04989 static void change_hold_state(struct sip_pvt *dialog, struct sip_request *req, int holdstate, int sendonly)
04990 {
04991 if (global_notifyhold && (!holdstate || !ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD)))
04992 sip_peer_hold(dialog, holdstate);
04993 if (global_callevents)
04994 manager_event(EVENT_FLAG_CALL, holdstate ? "Hold" : "Unhold",
04995 "Channel: %s\r\n"
04996 "Uniqueid: %s\r\n",
04997 dialog->owner->name,
04998 dialog->owner->uniqueid);
04999 append_history(dialog, holdstate ? "Hold" : "Unhold", "%s", req->data);
05000 if (!holdstate) {
05001 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD);
05002 return;
05003 }
05004
05005
05006 if (sendonly == 1)
05007 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
05008 else if (sendonly == 2)
05009 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
05010 else
05011 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
05012 return;
05013 }
05014
05015
05016
05017
05018
05019
05020 static int process_sdp(struct sip_pvt *p, struct sip_request *req)
05021 {
05022 const char *m;
05023 const char *c;
05024 const char *a;
05025 char host[258];
05026 int len = -1;
05027 int portno = -1;
05028 int vportno = -1;
05029 int udptlportno = -1;
05030 int peert38capability = 0;
05031 char s[256];
05032 int old = 0;
05033
05034
05035 int peercapability = 0, peernoncodeccapability = 0;
05036 int vpeercapability = 0, vpeernoncodeccapability = 0;
05037 struct sockaddr_in sin;
05038 struct sockaddr_in vsin;
05039
05040 const char *codecs;
05041 struct hostent *hp;
05042 struct hostent *vhp = NULL;
05043 struct ast_hostent audiohp;
05044 struct ast_hostent videohp;
05045 int codec;
05046 int destiterator = 0;
05047 int iterator;
05048 int sendonly = -1;
05049 int numberofports;
05050 struct ast_rtp *newaudiortp, *newvideortp;
05051 int newjointcapability;
05052 int newpeercapability;
05053 int newnoncodeccapability;
05054 int numberofmediastreams = 0;
05055 int debug = sip_debug_test_pvt(p);
05056
05057 int found_rtpmap_codecs[SDP_MAX_RTPMAP_CODECS];
05058 int last_rtpmap_codec=0;
05059
05060 if (!p->rtp) {
05061 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
05062 return -1;
05063 }
05064
05065
05066 newaudiortp = alloca(ast_rtp_alloc_size());
05067 memset(newaudiortp, 0, ast_rtp_alloc_size());
05068 ast_rtp_new_init(newaudiortp);
05069 ast_rtp_pt_clear(newaudiortp);
05070
05071 newvideortp = alloca(ast_rtp_alloc_size());
05072 memset(newvideortp, 0, ast_rtp_alloc_size());
05073 ast_rtp_new_init(newvideortp);
05074 ast_rtp_pt_clear(newvideortp);
05075
05076
05077 p->lastrtprx = p->lastrtptx = time(NULL);
05078
05079
05080
05081 m = get_sdp(req, "m");
05082 destiterator = req->sdp_start;
05083 c = get_sdp_iterate(&destiterator, req, "c");
05084 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
05085 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
05086 return -1;
05087 }
05088
05089
05090 if (sscanf(c, "IN IP4 %256s", host) != 1) {
05091 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
05092 return -1;
05093 }
05094
05095
05096 hp = ast_gethostbyname(host, &audiohp);
05097 if (!hp) {
05098 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
05099 return -1;
05100 }
05101 vhp = hp;
05102
05103 iterator = req->sdp_start;
05104 ast_set_flag(&p->flags[0], SIP_NOVIDEO);
05105
05106
05107
05108 while ((m = get_sdp_iterate(&iterator, req, "m"))[0] != '\0') {
05109 int x;
05110 int audio = FALSE;
05111
05112 numberofports = 1;
05113 if ((sscanf(m, "audio %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2) ||
05114 (sscanf(m, "audio %d RTP/AVP %n", &x, &len) == 1)) {
05115 audio = TRUE;
05116 numberofmediastreams++;
05117
05118 portno = x;
05119
05120 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
05121 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
05122 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
05123 return -1;
05124 }
05125 if (debug)
05126 ast_verbose("Found RTP audio format %d\n", codec);
05127 ast_rtp_set_m_type(newaudiortp, codec);
05128 }
05129 } else if ((sscanf(m, "video %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2) ||
05130 (sscanf(m, "video %d RTP/AVP %n", &x, &len) == 1)) {
05131
05132 ast_clear_flag(&p->flags[0], SIP_NOVIDEO);
05133 numberofmediastreams++;
05134 vportno = x;
05135
05136 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
05137 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
05138 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
05139 return -1;
05140 }
05141 if (debug)
05142 ast_verbose("Found RTP video format %d\n", codec);
05143 ast_rtp_set_m_type(newvideortp, codec);
05144 }
05145 } else if (p->udptl && ( (sscanf(m, "image %d udptl t38%n", &x, &len) == 1) ||
05146 (sscanf(m, "image %d UDPTL t38%n", &x, &len) == 1) )) {
05147 if (debug)
05148 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
05149 udptlportno = x;
05150 numberofmediastreams++;
05151
05152 if (p->owner && p->lastinvite) {
05153 p->t38.state = T38_PEER_REINVITE;
05154 if (option_debug > 1)
05155 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>" );
05156 } else {
05157 p->t38.state = T38_PEER_DIRECT;
05158 if (option_debug > 1)
05159 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05160 }
05161 } else
05162 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
05163 if (numberofports > 1)
05164 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
05165
05166
05167
05168 c = get_sdp_iterate(&destiterator, req, "c");
05169 if (!ast_strlen_zero(c)) {
05170 if (sscanf(c, "IN IP4 %256s", host) != 1) {
05171 ast_log(LOG_WARNING, "Invalid secondary host in c= line, '%s'\n", c);
05172 } else {
05173
05174 if (audio) {
05175 if ( !(hp = ast_gethostbyname(host, &audiohp))) {
05176 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in secondary c= line, '%s'\n", c);
05177 return -2;
05178 }
05179 } else if (!(vhp = ast_gethostbyname(host, &videohp))) {
05180 ast_log(LOG_WARNING, "Unable to lookup RTP video host in secondary c= line, '%s'\n", c);
05181 return -2;
05182 }
05183 }
05184
05185 }
05186 }
05187 if (portno == -1 && vportno == -1 && udptlportno == -1)
05188
05189
05190 return -2;
05191
05192 if (numberofmediastreams > 2)
05193
05194 return -3;
05195
05196
05197 sin.sin_family = AF_INET;
05198 vsin.sin_family = AF_INET;
05199 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
05200 if (vhp)
05201 memcpy(&vsin.sin_addr, vhp->h_addr, sizeof(vsin.sin_addr));
05202
05203
05204 if (p->udptl) {
05205 if (udptlportno > 0) {
05206 sin.sin_port = htons(udptlportno);
05207 if (ast_test_flag(&p->flags[0], SIP_NAT) && ast_test_flag(&p->flags[1], SIP_PAGE2_UDPTL_DESTINATION)) {
05208 struct sockaddr_in peer;
05209 ast_rtp_get_peer(p->rtp, &peer);
05210 if (peer.sin_addr.s_addr) {
05211 memcpy(&sin.sin_addr, &peer.sin_addr, sizeof(&sin.sin_addr));
05212 if (debug) {
05213 ast_log(LOG_DEBUG, "Peer T.38 UDPTL is set behind NAT and with destination, destination address now %s\n", ast_inet_ntoa(sin.sin_addr));
05214 }
05215 }
05216 }
05217 ast_udptl_set_peer(p->udptl, &sin);
05218 if (debug)
05219 ast_log(LOG_DEBUG,"Peer T.38 UDPTL is at port %s:%d\n",ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05220 } else {
05221 ast_udptl_stop(p->udptl);
05222 if (debug)
05223 ast_log(LOG_DEBUG, "Peer doesn't provide T.38 UDPTL\n");
05224 }
05225 }
05226
05227
05228 if (p->rtp) {
05229 if (portno > 0) {
05230 sin.sin_port = htons(portno);
05231 ast_rtp_set_peer(p->rtp, &sin);
05232 if (debug)
05233 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05234 } else {
05235 if (udptlportno > 0) {
05236 if (debug)
05237 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session. Callid %s\n", p->callid);
05238 } else {
05239 ast_rtp_stop(p->rtp);
05240 if (debug)
05241 ast_verbose("Peer doesn't provide audio. Callid %s\n", p->callid);
05242 }
05243 }
05244 }
05245
05246 if (vportno != -1)
05247 vsin.sin_port = htons(vportno);
05248
05249
05250
05251
05252
05253 iterator = req->sdp_start;
05254 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
05255 char* mimeSubtype = ast_strdupa(a);
05256 if (option_debug > 1) {
05257 int breakout = FALSE;
05258
05259
05260 if (!strncasecmp(a, "rtcp:", (size_t) 5)) {
05261 if (debug)
05262 ast_verbose("Got unsupported a:rtcp in SDP offer \n");
05263 breakout = TRUE;
05264 } else if (!strncasecmp(a, "fmtp:", (size_t) 5)) {
05265
05266
05267
05268
05269 if (debug)
05270 ast_verbose("Got unsupported a:fmtp in SDP offer \n");
05271 breakout = TRUE;
05272 } else if (!strncasecmp(a, "framerate:", (size_t) 10)) {
05273
05274 if (debug)
05275 ast_verbose("Got unsupported a:framerate in SDP offer \n");
05276 breakout = TRUE;
05277 } else if (!strncasecmp(a, "maxprate:", (size_t) 9)) {
05278
05279 if (debug)
05280 ast_verbose("Got unsupported a:maxprate in SDP offer \n");
05281 breakout = TRUE;
05282 } else if (!strncasecmp(a, "crypto:", (size_t) 7)) {
05283
05284 if (debug)
05285 ast_verbose("Got unsupported a:crypto in SDP offer \n");
05286 breakout = TRUE;
05287 }
05288 if (breakout)
05289 continue;
05290 }
05291 if (!strcasecmp(a, "sendonly")) {
05292 if (sendonly == -1)
05293 sendonly = 1;
05294 continue;
05295 } else if (!strcasecmp(a, "inactive")) {
05296 if (sendonly == -1)
05297 sendonly = 2;
05298 continue;
05299 } else if (!strcasecmp(a, "sendrecv")) {
05300 if (sendonly == -1)
05301 sendonly = 0;
05302 continue;
05303 } else if (strlen(a) > 5 && !strncasecmp(a, "ptime", 5)) {
05304 char *tmp = strrchr(a, ':');
05305 long int framing = 0;
05306 if (tmp) {
05307 tmp++;
05308 framing = strtol(tmp, NULL, 10);
05309 if (framing == LONG_MIN || framing == LONG_MAX) {
05310 framing = 0;
05311 if (option_debug)
05312 ast_log(LOG_DEBUG, "Can't read framing from SDP: %s\n", a);
05313 }
05314 }
05315 if (framing && p->autoframing) {
05316 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
05317 int codec_n;
05318 int format = 0;
05319 for (codec_n = 0; codec_n < MAX_RTP_PT; codec_n++) {
05320 format = ast_rtp_codec_getformat(codec_n);
05321 if (!format)
05322 continue;
05323 if (option_debug)
05324 ast_log(LOG_DEBUG, "Setting framing for %d to %ld\n", format, framing);
05325 ast_codec_pref_setsize(pref, format, framing);
05326 }
05327 ast_rtp_codec_setpref(p->rtp, pref);
05328 }
05329 continue;
05330 } else if (sscanf(a, "rtpmap: %u %[^/]/", &codec, mimeSubtype) == 2) {
05331
05332 int found = FALSE;
05333
05334
05335
05336 if (last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
05337
05338 if(ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
05339 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0) != -1) {
05340 if (debug)
05341 ast_verbose("Found audio description format %s for ID %d\n", mimeSubtype, codec);
05342 found_rtpmap_codecs[last_rtpmap_codec] = codec;
05343 last_rtpmap_codec++;
05344 found = TRUE;
05345
05346 } else if (p->vrtp) {
05347 if(ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0) != -1) {
05348 if (debug)
05349 ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec);
05350 found_rtpmap_codecs[last_rtpmap_codec] = codec;
05351 last_rtpmap_codec++;
05352 found = TRUE;
05353 }
05354 }
05355 } else {
05356 if (debug)
05357 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
05358 }
05359
05360 if (!found) {
05361
05362
05363
05364 ast_rtp_unset_m_type(newaudiortp, codec);
05365 ast_rtp_unset_m_type(newvideortp, codec);
05366 if (debug)
05367 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
05368 }
05369 }
05370 }
05371
05372 if (udptlportno != -1) {
05373 int found = 0, x;
05374
05375 old = 0;
05376
05377
05378 iterator = req->sdp_start;
05379 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
05380 if ((sscanf(a, "T38FaxMaxBuffer:%d", &x) == 1)) {
05381 found = 1;
05382 if (option_debug > 2)
05383 ast_log(LOG_DEBUG, "MaxBufferSize:%d\n",x);
05384 } else if ((sscanf(a, "T38MaxBitRate:%d", &x) == 1)) {
05385 found = 1;
05386 if (option_debug > 2)
05387 ast_log(LOG_DEBUG,"T38MaxBitRate: %d\n",x);
05388 switch (x) {
05389 case 14400:
05390 peert38capability |= T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05391 break;
05392 case 12000:
05393 peert38capability |= T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05394 break;
05395 case 9600:
05396 peert38capability |= T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05397 break;
05398 case 7200:
05399 peert38capability |= T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05400 break;
05401 case 4800:
05402 peert38capability |= T38FAX_RATE_4800 | T38FAX_RATE_2400;
05403 break;
05404 case 2400:
05405 peert38capability |= T38FAX_RATE_2400;
05406 break;
05407 }
05408 } else if ((sscanf(a, "T38FaxVersion:%d", &x) == 1)) {
05409 found = 1;
05410 if (option_debug > 2)
05411 ast_log(LOG_DEBUG, "FaxVersion: %d\n",x);
05412 if (x == 0)
05413 peert38capability |= T38FAX_VERSION_0;
05414 else if (x == 1)
05415 peert38capability |= T38FAX_VERSION_1;
05416 } else if ((sscanf(a, "T38FaxMaxDatagram:%d", &x) == 1)) {
05417 found = 1;
05418 if (option_debug > 2)
05419 ast_log(LOG_DEBUG, "FaxMaxDatagram: %d\n",x);
05420 ast_udptl_set_far_max_datagram(p->udptl, x);
05421 ast_udptl_set_local_max_datagram(p->udptl, x);
05422 } else if ((sscanf(a, "T38FaxFillBitRemoval:%d", &x) == 1)) {
05423 found = 1;
05424 if (option_debug > 2)
05425 ast_log(LOG_DEBUG, "FillBitRemoval: %d\n",x);
05426 if (x == 1)
05427 peert38capability |= T38FAX_FILL_BIT_REMOVAL;
05428 } else if ((sscanf(a, "T38FaxTranscodingMMR:%d", &x) == 1)) {
05429 found = 1;
05430 if (option_debug > 2)
05431 ast_log(LOG_DEBUG, "Transcoding MMR: %d\n",x);
05432 if (x == 1)
05433 peert38capability |= T38FAX_TRANSCODING_MMR;
05434 }
05435 if ((sscanf(a, "T38FaxTranscodingJBIG:%d", &x) == 1)) {
05436 found = 1;
05437 if (option_debug > 2)
05438 ast_log(LOG_DEBUG, "Transcoding JBIG: %d\n",x);
05439 if (x == 1)
05440 peert38capability |= T38FAX_TRANSCODING_JBIG;
05441 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
05442 found = 1;
05443 if (option_debug > 2)
05444 ast_log(LOG_DEBUG, "RateManagement: %s\n", s);
05445 if (!strcasecmp(s, "localTCF"))
05446 peert38capability |= T38FAX_RATE_MANAGEMENT_LOCAL_TCF;
05447 else if (!strcasecmp(s, "transferredTCF"))
05448 peert38capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
05449 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
05450 found = 1;
05451 if (option_debug > 2)
05452 ast_log(LOG_DEBUG, "UDP EC: %s\n", s);
05453 if (!strcasecmp(s, "t38UDPRedundancy")) {
05454 peert38capability |= T38FAX_UDP_EC_REDUNDANCY;
05455 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
05456 } else if (!strcasecmp(s, "t38UDPFEC")) {
05457 peert38capability |= T38FAX_UDP_EC_FEC;
05458 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
05459 } else {
05460 peert38capability |= T38FAX_UDP_EC_NONE;
05461 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
05462 }
05463 }
05464 }
05465 if (found) {
05466 p->t38.peercapability = peert38capability;
05467 p->t38.jointcapability = (peert38capability & 255);
05468 peert38capability &= (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400);
05469 p->t38.jointcapability |= (peert38capability & p->t38.capability);
05470 }
05471 if (debug)
05472 ast_log(LOG_DEBUG, "Our T38 capability = (%d), peer T38 capability (%d), joint T38 capability (%d)\n",
05473 p->t38.capability,
05474 p->t38.peercapability,
05475 p->t38.jointcapability);
05476 } else {
05477 p->t38.state = T38_DISABLED;
05478 if (option_debug > 2)
05479 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05480 }
05481
05482
05483 ast_rtp_get_current_formats(newaudiortp, &peercapability, &peernoncodeccapability);
05484 ast_rtp_get_current_formats(newvideortp, &vpeercapability, &vpeernoncodeccapability);
05485
05486 newjointcapability = p->capability & (peercapability | vpeercapability);
05487 newpeercapability = (peercapability | vpeercapability);
05488 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
05489
05490
05491 if (debug) {
05492
05493 char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE];
05494
05495 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s, combined - %s\n",
05496 ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
05497 ast_getformatname_multiple(s2, SIPBUFSIZE, newpeercapability),
05498 ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
05499 ast_getformatname_multiple(s4, SIPBUFSIZE, newjointcapability));
05500
05501 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
05502 ast_rtp_lookup_mime_multiple(s1, SIPBUFSIZE, p->noncodeccapability, 0, 0),
05503 ast_rtp_lookup_mime_multiple(s2, SIPBUFSIZE, peernoncodeccapability, 0, 0),
05504 ast_rtp_lookup_mime_multiple(s3, SIPBUFSIZE, newnoncodeccapability, 0, 0));
05505 }
05506 if (!newjointcapability) {
05507
05508 if (!p->t38.jointcapability || !udptlportno) {
05509 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
05510
05511 return -1;
05512 } else {
05513 if (option_debug > 2)
05514 ast_log(LOG_DEBUG, "Have T.38 but no audio codecs, accepting offer anyway\n");
05515 return 0;
05516 }
05517 }
05518
05519
05520
05521 p->jointcapability = newjointcapability;
05522 p->peercapability = newpeercapability;
05523 p->jointnoncodeccapability = newnoncodeccapability;
05524
05525 ast_rtp_pt_copy(p->rtp, newaudiortp);
05526 if (p->vrtp)
05527 ast_rtp_pt_copy(p->vrtp, newvideortp);
05528
05529 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
05530 ast_clear_flag(&p->flags[0], SIP_DTMF);
05531 if (newnoncodeccapability & AST_RTP_DTMF) {
05532
05533 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
05534
05535 ast_rtp_setdtmf(p->rtp, 1);
05536 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
05537 } else {
05538 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
05539 }
05540 }
05541
05542
05543 if (p->rtp && sin.sin_port) {
05544 ast_rtp_set_peer(p->rtp, &sin);
05545 if (debug)
05546 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05547 }
05548
05549
05550 if (p->vrtp && vsin.sin_port) {
05551 ast_rtp_set_peer(p->vrtp, &vsin);
05552 if (debug)
05553 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
05554 }
05555
05556
05557 if (option_debug > 1) {
05558 char buf[SIPBUFSIZE];
05559 ast_log(LOG_DEBUG, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
05560 }
05561
05562 if (!p->owner)
05563 return 0;
05564
05565 if (option_debug > 3)
05566 ast_log(LOG_DEBUG, "We have an owner, now see if we need to change this call\n");
05567
05568 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
05569 if (debug) {
05570 char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
05571 ast_log(LOG_DEBUG, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
05572 ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
05573 ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
05574 }
05575 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability);
05576 ast_set_read_format(p->owner, p->owner->readformat);
05577 ast_set_write_format(p->owner, p->owner->writeformat);
05578 }
05579
05580 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1)) {
05581 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
05582
05583 ast_queue_frame(p->owner, &ast_null_frame);
05584 } else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1)) {
05585 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
05586 S_OR(p->mohsuggest, NULL),
05587 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
05588 if (sendonly)
05589 ast_rtp_stop(p->rtp);
05590
05591
05592 ast_queue_frame(p->owner, &ast_null_frame);
05593 }
05594
05595
05596 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1))
05597 change_hold_state(p, req, FALSE, sendonly);
05598 else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1))
05599 change_hold_state(p, req, TRUE, sendonly);
05600 return 0;
05601 }
05602
05603
05604
05605 static int add_header(struct sip_request *req, const char *var, const char *value)
05606 {
05607 int maxlen = sizeof(req->data) - 4 - req->len;
05608
05609 if (req->headers == SIP_MAX_HEADERS) {
05610 ast_log(LOG_WARNING, "Out of SIP header space\n");
05611 return -1;
05612 }
05613
05614 if (req->lines) {
05615 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
05616 return -1;
05617 }
05618
05619 if (maxlen <= 0) {
05620 ast_log(LOG_WARNING, "Out of space, can't add anymore (%s:%s)\n", var, value);
05621 return -1;
05622 }
05623
05624 req->header[req->headers] = req->data + req->len;
05625
05626 if (compactheaders)
05627 var = find_alias(var, var);
05628
05629 snprintf(req->header[req->headers], maxlen, "%s: %s\r\n", var, value);
05630 req->len += strlen(req->header[req->headers]);
05631 req->headers++;
05632
05633 return 0;
05634 }
05635
05636
05637 static int add_header_contentLength(struct sip_request *req, int len)
05638 {
05639 char clen[10];
05640
05641 snprintf(clen, sizeof(clen), "%d", len);
05642 return add_header(req, "Content-Length", clen);
05643 }
05644
05645
05646 static int add_line(struct sip_request *req, const char *line)
05647 {
05648 if (req->lines == SIP_MAX_LINES) {
05649 ast_log(LOG_WARNING, "Out of SIP line space\n");
05650 return -1;
05651 }
05652 if (!req->lines) {
05653
05654 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
05655 req->len += strlen(req->data + req->len);
05656 }
05657 if (req->len >= sizeof(req->data) - 4) {
05658 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
05659 return -1;
05660 }
05661 req->line[req->lines] = req->data + req->len;
05662 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
05663 req->len += strlen(req->line[req->lines]);
05664 req->lines++;
05665 return 0;
05666 }
05667
05668
05669 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
05670 {
05671 const char *tmp = get_header(orig, field);
05672
05673 if (!ast_strlen_zero(tmp))
05674 return add_header(req, field, tmp);
05675 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
05676 return -1;
05677 }
05678
05679
05680 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
05681 {
05682 int start = 0;
05683 int copied = 0;
05684 for (;;) {
05685 const char *tmp = __get_header(orig, field, &start);
05686
05687 if (ast_strlen_zero(tmp))
05688 break;
05689
05690 add_header(req, field, tmp);
05691 copied++;
05692 }
05693 return copied ? 0 : -1;
05694 }
05695
05696
05697
05698
05699
05700
05701
05702 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
05703 {
05704 int copied = 0;
05705 int start = 0;
05706
05707 for (;;) {
05708 char new[512];
05709 const char *oh = __get_header(orig, field, &start);
05710
05711 if (ast_strlen_zero(oh))
05712 break;
05713
05714 if (!copied) {
05715 char leftmost[512], *others, *rport;
05716
05717
05718 ast_copy_string(leftmost, oh, sizeof(leftmost));
05719 others = strchr(leftmost, ',');
05720 if (others)
05721 *others++ = '\0';
05722
05723
05724 rport = strstr(leftmost, ";rport");
05725 if (rport && *(rport+6) == '=')
05726 rport = NULL;
05727
05728
05729 if (rport && ((ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_ALWAYS) || (ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_RFC3581))) {
05730
05731 char *end;
05732
05733 rport = strstr(leftmost, ";rport");
05734
05735 if (rport) {
05736 end = strchr(rport + 1, ';');
05737 if (end)
05738 memmove(rport, end, strlen(end) + 1);
05739 else
05740 *rport = '\0';
05741 }
05742
05743
05744 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
05745 leftmost, ast_inet_ntoa(p->recv.sin_addr),
05746 ntohs(p->recv.sin_port),
05747 others ? "," : "", others ? others : "");
05748 } else {
05749
05750 snprintf(new, sizeof(new), "%s;received=%s%s%s",
05751 leftmost, ast_inet_ntoa(p->recv.sin_addr),
05752 others ? "," : "", others ? others : "");
05753 }
05754 oh = new;
05755 }
05756 add_header(req, field, oh);
05757 copied++;
05758 }
05759 if (!copied) {
05760 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
05761 return -1;
05762 }
05763 return 0;
05764 }
05765
05766
05767 static void add_route(struct sip_request *req, struct sip_route *route)
05768 {
05769 char r[SIPBUFSIZE*2], *p;
05770 int n, rem = sizeof(r);
05771
05772 if (!route)
05773 return;
05774
05775 p = r;
05776 for (;route ; route = route->next) {
05777 n = strlen(route->hop);
05778 if (rem < n+3)
05779 break;
05780 if (p != r) {
05781 *p++ = ',';
05782 --rem;
05783 }
05784 *p++ = '<';
05785 ast_copy_string(p, route->hop, rem);
05786 p += n;
05787 *p++ = '>';
05788 rem -= (n+2);
05789 }
05790 *p = '\0';
05791 add_header(req, "Route", r);
05792 }
05793
05794
05795 static void set_destination(struct sip_pvt *p, char *uri)
05796 {
05797 char *h, *maddr, hostname[256];
05798 int port, hn;
05799 struct hostent *hp;
05800 struct ast_hostent ahp;
05801 int debug=sip_debug_test_pvt(p);
05802
05803
05804
05805
05806 if (debug)
05807 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
05808
05809
05810 h = strchr(uri, '@');
05811 if (h)
05812 ++h;
05813 else {
05814 h = uri;
05815 if (strncasecmp(h, "sip:", 4) == 0)
05816 h += 4;
05817 else if (strncasecmp(h, "sips:", 5) == 0)
05818 h += 5;
05819 }
05820 hn = strcspn(h, ":;>") + 1;
05821 if (hn > sizeof(hostname))
05822 hn = sizeof(hostname);
05823 ast_copy_string(hostname, h, hn);
05824
05825 h += hn - 1;
05826
05827
05828 if (*h == ':') {
05829
05830 ++h;
05831 port = strtol(h, &h, 10);
05832 }
05833 else
05834 port = STANDARD_SIP_PORT;
05835
05836
05837 maddr = strstr(h, "maddr=");
05838 if (maddr) {
05839 maddr += 6;
05840 hn = strspn(maddr, "0123456789.") + 1;
05841 if (hn > sizeof(hostname))
05842 hn = sizeof(hostname);
05843 ast_copy_string(hostname, maddr, hn);
05844 }
05845
05846 hp = ast_gethostbyname(hostname, &ahp);
05847 if (hp == NULL) {
05848 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
05849 return;
05850 }
05851 p->sa.sin_family = AF_INET;
05852 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
05853 p->sa.sin_port = htons(port);
05854 if (debug)
05855 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(p->sa.sin_addr), port);
05856 }
05857
05858
05859 static int init_resp(struct sip_request *resp, const char *msg)
05860 {
05861
05862 memset(resp, 0, sizeof(*resp));
05863 resp->method = SIP_RESPONSE;
05864 resp->header[0] = resp->data;
05865 snprintf(resp->header[0], sizeof(resp->data), "SIP/2.0 %s\r\n", msg);
05866 resp->len = strlen(resp->header[0]);
05867 resp->headers++;
05868 return 0;
05869 }
05870
05871
05872 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
05873 {
05874
05875 memset(req, 0, sizeof(*req));
05876 req->method = sipmethod;
05877 req->header[0] = req->data;
05878 snprintf(req->header[0], sizeof(req->data), "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
05879 req->len = strlen(req->header[0]);
05880 req->headers++;
05881 return 0;
05882 }
05883
05884
05885
05886 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
05887 {
05888 char newto[256];
05889 const char *ot;
05890
05891 init_resp(resp, msg);
05892 copy_via_headers(p, resp, req, "Via");
05893 if (msg[0] == '1' || msg[0] == '2')
05894 copy_all_header(resp, req, "Record-Route");
05895 copy_header(resp, req, "From");
05896 ot = get_header(req, "To");
05897 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
05898
05899
05900 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
05901 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
05902 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
05903 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
05904 else
05905 ast_copy_string(newto, ot, sizeof(newto));
05906 ot = newto;
05907 }
05908 add_header(resp, "To", ot);
05909 copy_header(resp, req, "Call-ID");
05910 copy_header(resp, req, "CSeq");
05911 if (!ast_strlen_zero(global_useragent))
05912 add_header(resp, "User-Agent", global_useragent);
05913 add_header(resp, "Allow", ALLOWED_METHODS);
05914 add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
05915 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
05916
05917
05918 char tmp[256];
05919
05920 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
05921 add_header(resp, "Expires", tmp);
05922 if (p->expiry) {
05923 char contact[SIPBUFSIZE];
05924 snprintf(contact, sizeof(contact), "%s;expires=%d", p->our_contact, p->expiry);
05925 add_header(resp, "Contact", contact);
05926 }
05927 } else if (msg[0] != '4' && !ast_strlen_zero(p->our_contact)) {
05928 add_header(resp, "Contact", p->our_contact);
05929 }
05930 return 0;
05931 }
05932
05933
05934 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
05935 {
05936 struct sip_request *orig = &p->initreq;
05937 char stripped[80];
05938 char tmp[80];
05939 char newto[256];
05940 const char *c;
05941 const char *ot, *of;
05942 int is_strict = FALSE;
05943
05944 memset(req, 0, sizeof(struct sip_request));
05945
05946 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
05947
05948 if (!seqno) {
05949 p->ocseq++;
05950 seqno = p->ocseq;
05951 }
05952
05953 if (newbranch) {
05954 p->branch ^= ast_random();
05955 build_via(p);
05956 }
05957
05958
05959 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop,";lr") == NULL) {
05960 is_strict = TRUE;
05961 if (sipdebug)
05962 ast_log(LOG_DEBUG, "Strict routing enforced for session %s\n", p->callid);
05963 }
05964
05965 if (sipmethod == SIP_CANCEL)
05966 c = p->initreq.rlPart2;
05967 else if (sipmethod == SIP_ACK) {
05968
05969
05970 if (!ast_strlen_zero(p->okcontacturi))
05971 c = is_strict ? p->route->hop : p->okcontacturi;
05972 else
05973 c = p->initreq.rlPart2;
05974 } else if (!ast_strlen_zero(p->okcontacturi))
05975 c = is_strict ? p->route->hop : p->okcontacturi;
05976 else if (!ast_strlen_zero(p->uri))
05977 c = p->uri;
05978 else {
05979 char *n;
05980
05981 ast_copy_string(stripped, get_header(orig, (ast_test_flag(&p->flags[0], SIP_OUTGOING)) ? "To" : "From"),
05982 sizeof(stripped));
05983 n = get_in_brackets(stripped);
05984 c = strsep(&n, ";");
05985 }
05986 init_req(req, sipmethod, c);
05987
05988 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
05989
05990 add_header(req, "Via", p->via);
05991 if (p->route) {
05992 set_destination(p, p->route->hop);
05993 add_route(req, is_strict ? p->route->next : p->route);
05994 }
05995
05996 ot = get_header(orig, "To");
05997 of = get_header(orig, "From");
05998
05999
06000
06001 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
06002
06003
06004 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_strlen_zero(p->theirtag))
06005 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
06006 else if (!ast_test_flag(&p->flags[0], SIP_OUTGOING))
06007 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
06008 else
06009 snprintf(newto, sizeof(newto), "%s", ot);
06010 ot = newto;
06011 }
06012
06013 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06014 add_header(req, "From", of);
06015 add_header(req, "To", ot);
06016 } else {
06017 add_header(req, "From", ot);
06018 add_header(req, "To", of);
06019 }
06020
06021 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
06022 add_header(req, "Contact", p->our_contact);
06023
06024 copy_header(req, orig, "Call-ID");
06025 add_header(req, "CSeq", tmp);
06026
06027 if (!ast_strlen_zero(global_useragent))
06028 add_header(req, "User-Agent", global_useragent);
06029 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
06030
06031 if (!ast_strlen_zero(p->rpid))
06032 add_header(req, "Remote-Party-ID", p->rpid);
06033
06034 return 0;
06035 }
06036
06037
06038 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06039 {
06040 struct sip_request resp;
06041 int seqno = 0;
06042
06043 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
06044 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
06045 return -1;
06046 }
06047 respprep(&resp, p, msg, req);
06048 add_header_contentLength(&resp, 0);
06049
06050
06051 if (p->method == SIP_INVITE && msg[0] != '1' && p->owner && p->owner->hangupcause) {
06052 char buf[10];
06053
06054 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
06055 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
06056 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
06057 }
06058 return send_response(p, &resp, reliable, seqno);
06059 }
06060
06061 static void temp_pvt_cleanup(void *data)
06062 {
06063 struct sip_pvt *p = data;
06064
06065 ast_string_field_free_memory(p);
06066
06067 free(data);
06068 }
06069
06070
06071 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)
06072 {
06073 struct sip_pvt *p = NULL;
06074
06075 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
06076 ast_log(LOG_NOTICE, "Failed to get temporary pvt\n");
06077 return -1;
06078 }
06079
06080
06081 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
06082 ast_set_flag(&p->flags[0], SIP_NO_HISTORY);
06083 if (ast_string_field_init(p, 512))
06084 return -1;
06085 }
06086
06087
06088 p->method = intended_method;
06089
06090 if (sin) {
06091 p->sa = *sin;
06092 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
06093 p->ourip = __ourip;
06094 } else
06095 p->ourip = __ourip;
06096
06097 p->branch = ast_random();
06098 make_our_tag(p->tag, sizeof(p->tag));
06099 p->ocseq = INITIAL_CSEQ;
06100
06101 if (useglobal_nat && sin) {
06102 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
06103 p->recv = *sin;
06104 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
06105 }
06106 check_via(p, req);
06107
06108 ast_string_field_set(p, fromdomain, default_fromdomain);
06109 build_via(p);
06110 ast_string_field_set(p, callid, callid);
06111
06112
06113 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
06114
06115
06116 ast_string_field_reset_all(p);
06117
06118 return 0;
06119 }
06120
06121
06122 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
06123 {
06124 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
06125 }
06126
06127
06128 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
06129 {
06130 struct sip_request resp;
06131 respprep(&resp, p, msg, req);
06132 append_date(&resp);
06133 add_header(&resp, "Unsupported", unsupported);
06134 add_header_contentLength(&resp, 0);
06135 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
06136 }
06137
06138
06139
06140
06141 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
06142 {
06143 return __transmit_response(p, msg, req, XMIT_CRITICAL);
06144 }
06145
06146
06147 static void append_date(struct sip_request *req)
06148 {
06149 char tmpdat[256];
06150 struct tm tm;
06151 time_t t = time(NULL);
06152
06153 gmtime_r(&t, &tm);
06154 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
06155 add_header(req, "Date", tmpdat);
06156 }
06157
06158
06159 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
06160 {
06161 struct sip_request resp;
06162 respprep(&resp, p, msg, req);
06163 append_date(&resp);
06164 add_header_contentLength(&resp, 0);
06165 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
06166 }
06167
06168
06169 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06170 {
06171 struct sip_request resp;
06172 respprep(&resp, p, msg, req);
06173 add_header(&resp, "Accept", "application/sdp");
06174 add_header_contentLength(&resp, 0);
06175 return send_response(p, &resp, reliable, 0);
06176 }
06177
06178
06179 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)
06180 {
06181 struct sip_request resp;
06182 char tmp[512];
06183 int seqno = 0;
06184
06185 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
06186 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
06187 return -1;
06188 }
06189
06190
06191 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
06192 respprep(&resp, p, msg, req);
06193 add_header(&resp, header, tmp);
06194 add_header_contentLength(&resp, 0);
06195 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
06196 return send_response(p, &resp, reliable, seqno);
06197 }
06198
06199
06200 static int add_text(struct sip_request *req, const char *text)
06201 {
06202
06203 add_header(req, "Content-Type", "text/plain");
06204 add_header_contentLength(req, strlen(text));
06205 add_line(req, text);
06206 return 0;
06207 }
06208
06209
06210
06211 static int add_digit(struct sip_request *req, char digit, unsigned int duration)
06212 {
06213 char tmp[256];
06214
06215 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
06216 add_header(req, "Content-Type", "application/dtmf-relay");
06217 add_header_contentLength(req, strlen(tmp));
06218 add_line(req, tmp);
06219 return 0;
06220 }
06221
06222
06223
06224 static int add_vidupdate(struct sip_request *req)
06225 {
06226 const char *xml_is_a_huge_waste_of_space =
06227 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
06228 " <media_control>\r\n"
06229 " <vc_primitive>\r\n"
06230 " <to_encoder>\r\n"
06231 " <picture_fast_update>\r\n"
06232 " </picture_fast_update>\r\n"
06233 " </to_encoder>\r\n"
06234 " </vc_primitive>\r\n"
06235 " </media_control>\r\n";
06236 add_header(req, "Content-Type", "application/media_control+xml");
06237 add_header_contentLength(req, strlen(xml_is_a_huge_waste_of_space));
06238 add_line(req, xml_is_a_huge_waste_of_space);
06239 return 0;
06240 }
06241
06242
06243 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
06244 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
06245 int debug, int *min_packet_size)
06246 {
06247 int rtp_code;
06248 struct ast_format_list fmt;
06249
06250
06251 if (debug)
06252 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
06253 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
06254 return;
06255
06256 if (p->rtp) {
06257 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
06258 fmt = ast_codec_pref_getsize(pref, codec);
06259 } else
06260 return;
06261 ast_build_string(m_buf, m_size, " %d", rtp_code);
06262 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
06263 ast_rtp_lookup_mime_subtype(1, codec,
06264 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
06265 sample_rate);
06266 if (codec == AST_FORMAT_G729A) {
06267
06268 ast_build_string(a_buf, a_size, "a=fmtp:%d annexb=no\r\n", rtp_code);
06269 } else if (codec == AST_FORMAT_G723_1) {
06270
06271 ast_build_string(a_buf, a_size, "a=fmtp:%d annexa=no\r\n", rtp_code);
06272 } else if (codec == AST_FORMAT_ILBC) {
06273
06274 ast_build_string(a_buf, a_size, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
06275 }
06276
06277 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
06278 *min_packet_size = fmt.cur_ms;
06279
06280
06281 if ((*min_packet_size) == 0 && fmt.cur_ms)
06282 *min_packet_size = fmt.cur_ms;
06283 }
06284
06285
06286 static int t38_get_rate(int t38cap)
06287 {
06288 int maxrate = (t38cap & (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400));
06289
06290 if (maxrate & T38FAX_RATE_14400) {
06291 if (option_debug > 1)
06292 ast_log(LOG_DEBUG, "T38MaxFaxRate 14400 found\n");
06293 return 14400;
06294 } else if (maxrate & T38FAX_RATE_12000) {
06295 if (option_debug > 1)
06296 ast_log(LOG_DEBUG, "T38MaxFaxRate 12000 found\n");
06297 return 12000;
06298 } else if (maxrate & T38FAX_RATE_9600) {
06299 if (option_debug > 1)
06300 ast_log(LOG_DEBUG, "T38MaxFaxRate 9600 found\n");
06301 return 9600;
06302 } else if (maxrate & T38FAX_RATE_7200) {
06303 if (option_debug > 1)
06304 ast_log(LOG_DEBUG, "T38MaxFaxRate 7200 found\n");
06305 return 7200;
06306 } else if (maxrate & T38FAX_RATE_4800) {
06307 if (option_debug > 1)
06308 ast_log(LOG_DEBUG, "T38MaxFaxRate 4800 found\n");
06309 return 4800;
06310 } else if (maxrate & T38FAX_RATE_2400) {
06311 if (option_debug > 1)
06312 ast_log(LOG_DEBUG, "T38MaxFaxRate 2400 found\n");
06313 return 2400;
06314 } else {
06315 if (option_debug > 1)
06316 ast_log(LOG_DEBUG, "Strange, T38MaxFaxRate NOT found in peers T38 SDP.\n");
06317 return 0;
06318 }
06319 }
06320
06321
06322 static int add_t38_sdp(struct sip_request *resp, struct sip_pvt *p)
06323 {
06324 int len = 0;
06325 int x = 0;
06326 struct sockaddr_in udptlsin;
06327 char v[256] = "";
06328 char s[256] = "";
06329 char o[256] = "";
06330 char c[256] = "";
06331 char t[256] = "";
06332 char m_modem[256];
06333 char a_modem[1024];
06334 char *m_modem_next = m_modem;
06335 size_t m_modem_left = sizeof(m_modem);
06336 char *a_modem_next = a_modem;
06337 size_t a_modem_left = sizeof(a_modem);
06338 struct sockaddr_in udptldest = { 0, };
06339 int debug;
06340
06341 debug = sip_debug_test_pvt(p);
06342 len = 0;
06343 if (!p->udptl) {
06344 ast_log(LOG_WARNING, "No way to add SDP without an UDPTL structure\n");
06345 return -1;
06346 }
06347
06348 if (!p->sessionid) {
06349 p->sessionid = getpid();
06350 p->sessionversion = p->sessionid;
06351 } else
06352 p->sessionversion++;
06353
06354
06355 ast_udptl_get_us(p->udptl, &udptlsin);
06356
06357
06358 if (p->udptlredirip.sin_addr.s_addr) {
06359 udptldest.sin_port = p->udptlredirip.sin_port;
06360 udptldest.sin_addr = p->udptlredirip.sin_addr;
06361 } else {
06362 udptldest.sin_addr = p->ourip;
06363 udptldest.sin_port = udptlsin.sin_port;
06364 }
06365
06366 if (debug)
06367 ast_log(LOG_DEBUG, "T.38 UDPTL is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(udptlsin.sin_port));
06368
06369
06370
06371
06372 if (debug) {
06373 ast_log(LOG_DEBUG, "Our T38 capability (%d), peer T38 capability (%d), joint capability (%d)\n",
06374 p->t38.capability,
06375 p->t38.peercapability,
06376 p->t38.jointcapability);
06377 }
06378 snprintf(v, sizeof(v), "v=0\r\n");
06379 snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(udptldest.sin_addr));
06380 snprintf(s, sizeof(s), "s=session\r\n");
06381 snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(udptldest.sin_addr));
06382 snprintf(t, sizeof(t), "t=0 0\r\n");
06383 ast_build_string(&m_modem_next, &m_modem_left, "m=image %d udptl t38\r\n", ntohs(udptldest.sin_port));
06384
06385 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_0)
06386 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:0\r\n");
06387 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_1)
06388 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:1\r\n");
06389 if ((x = t38_get_rate(p->t38.jointcapability)))
06390 ast_build_string(&a_modem_next, &a_modem_left, "a=T38MaxBitRate:%d\r\n",x);
06391 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxFillBitRemoval:%d\r\n", (p->t38.jointcapability & T38FAX_FILL_BIT_REMOVAL) ? 1 : 0);
06392 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingMMR:%d\r\n", (p->t38.jointcapability & T38FAX_TRANSCODING_MMR) ? 1 : 0);
06393 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingJBIG:%d\r\n", (p->t38.jointcapability & T38FAX_TRANSCODING_JBIG) ? 1 : 0);
06394 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxRateManagement:%s\r\n", (p->t38.jointcapability & T38FAX_RATE_MANAGEMENT_LOCAL_TCF) ? "localTCF" : "transferredTCF");
06395 x = ast_udptl_get_local_max_datagram(p->udptl);
06396 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxBuffer:%d\r\n",x);
06397 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxDatagram:%d\r\n",x);
06398 if (p->t38.jointcapability != T38FAX_UDP_EC_NONE)
06399 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxUdpEC:%s\r\n", (p->t38.jointcapability & T38FAX_UDP_EC_REDUNDANCY) ? "t38UDPRedundancy" : "t38UDPFEC");
06400 len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m_modem) + strlen(a_modem);
06401 add_header(resp, "Content-Type", "application/sdp");
06402 add_header_contentLength(resp, len);
06403 add_line(resp, v);
06404 add_line(resp, o);
06405 add_line(resp, s);
06406 add_line(resp, c);
06407 add_line(resp, t);
06408 add_line(resp, m_modem);
06409 add_line(resp, a_modem);
06410
06411
06412 p->lastrtprx = p->lastrtptx = time(NULL);
06413
06414 return 0;
06415 }
06416
06417
06418
06419 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
06420 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
06421 int debug)
06422 {
06423 int rtp_code;
06424
06425 if (debug)
06426 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format, 0));
06427 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
06428 return;
06429
06430 ast_build_string(m_buf, m_size, " %d", rtp_code);
06431 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
06432 ast_rtp_lookup_mime_subtype(0, format, 0),
06433 sample_rate);
06434 if (format == AST_RTP_DTMF)
06435
06436 ast_build_string(a_buf, a_size, "a=fmtp:%d 0-16\r\n", rtp_code);
06437 }
06438
06439
06440
06441
06442
06443
06444 #define SDP_SAMPLE_RATE(x) 8000
06445
06446
06447 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p)
06448 {
06449 int len = 0;
06450 int alreadysent = 0;
06451
06452 struct sockaddr_in sin;
06453 struct sockaddr_in vsin;
06454 struct sockaddr_in dest;
06455 struct sockaddr_in vdest = { 0, };
06456
06457
06458 char *version = "v=0\r\n";
06459 char *subject = "s=session\r\n";
06460 char owner[256];
06461 char connection[256];
06462 char *stime = "t=0 0\r\n";
06463 char bandwidth[256] = "";
06464 char *hold;
06465 char m_audio[256];
06466 char m_video[256];
06467 char a_audio[1024];
06468 char a_video[1024];
06469 char *m_audio_next = m_audio;
06470 char *m_video_next = m_video;
06471 size_t m_audio_left = sizeof(m_audio);
06472 size_t m_video_left = sizeof(m_video);
06473 char *a_audio_next = a_audio;
06474 char *a_video_next = a_video;
06475 size_t a_audio_left = sizeof(a_audio);
06476 size_t a_video_left = sizeof(a_video);
06477
06478 int x;
06479 int capability;
06480 int needvideo = FALSE;
06481 int debug = sip_debug_test_pvt(p);
06482 int min_audio_packet_size = 0;
06483 int min_video_packet_size = 0;
06484
06485 m_video[0] = '\0';
06486
06487 if (!p->rtp) {
06488 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
06489 return AST_FAILURE;
06490 }
06491
06492
06493 if (!p->sessionid) {
06494 p->sessionid = getpid();
06495 p->sessionversion = p->sessionid;
06496 } else
06497 p->sessionversion++;
06498
06499
06500 ast_rtp_get_us(p->rtp, &sin);
06501 if (p->vrtp)
06502 ast_rtp_get_us(p->vrtp, &vsin);
06503
06504
06505 if (p->redirip.sin_addr.s_addr) {
06506 dest.sin_port = p->redirip.sin_port;
06507 dest.sin_addr = p->redirip.sin_addr;
06508 } else {
06509 dest.sin_addr = p->ourip;
06510 dest.sin_port = sin.sin_port;
06511 }
06512
06513 capability = p->jointcapability;
06514
06515
06516 if (option_debug > 1) {
06517 char codecbuf[SIPBUFSIZE];
06518 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");
06519 ast_log(LOG_DEBUG, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
06520 }
06521
06522 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
06523 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_RTP)) {
06524 ast_build_string(&m_audio_next, &m_audio_left, " %d", 191);
06525 ast_build_string(&a_audio_next, &a_audio_left, "a=rtpmap:%d %s/%d\r\n", 191, "t38", 8000);
06526 }
06527 #endif
06528
06529
06530 if ((capability & AST_FORMAT_VIDEO_MASK) && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
06531 if (p->vrtp) {
06532 needvideo = TRUE;
06533 if (option_debug > 1)
06534 ast_log(LOG_DEBUG, "This call needs video offers!\n");
06535 } else if (option_debug > 1)
06536 ast_log(LOG_DEBUG, "This call needs video offers, but there's no video support enabled!\n");
06537 }
06538
06539
06540
06541
06542 if (needvideo) {
06543
06544 if (p->vredirip.sin_addr.s_addr) {
06545 vdest.sin_addr = p->vredirip.sin_addr;
06546 vdest.sin_port = p->vredirip.sin_port;
06547 } else {
06548 vdest.sin_addr = p->ourip;
06549 vdest.sin_port = vsin.sin_port;
06550 }
06551 ast_build_string(&m_video_next, &m_video_left, "m=video %d RTP/AVP", ntohs(vdest.sin_port));
06552
06553
06554 if (p->maxcallbitrate)
06555 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
06556 if (debug)
06557 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(vsin.sin_port));
06558 }
06559
06560 if (debug)
06561 ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(sin.sin_port));
06562
06563
06564
06565
06566
06567
06568 snprintf(owner, sizeof(owner), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(dest.sin_addr));
06569 snprintf(connection, sizeof(connection), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
06570 ast_build_string(&m_audio_next, &m_audio_left, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
06571
06572 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR)
06573 hold = "a=recvonly\r\n";
06574 else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE)
06575 hold = "a=inactive\r\n";
06576 else
06577 hold = "a=sendrecv\r\n";
06578
06579
06580
06581
06582
06583
06584
06585
06586
06587
06588 if (capability & p->prefcodec) {
06589 int codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
06590
06591 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
06592 &m_audio_next, &m_audio_left,
06593 &a_audio_next, &a_audio_left,
06594 debug, &min_audio_packet_size);
06595 alreadysent |= codec;
06596 }
06597
06598
06599 for (x = 0; x < 32; x++) {
06600 int codec;
06601
06602 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
06603 break;
06604
06605 if (!(capability & codec))
06606 continue;
06607
06608 if (alreadysent & codec)
06609 continue;
06610
06611 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
06612 &m_audio_next, &m_audio_left,
06613 &a_audio_next, &a_audio_left,
06614 debug, &min_audio_packet_size);
06615 alreadysent |= codec;
06616 }
06617
06618
06619 for (x = 1; x <= (needvideo ? AST_FORMAT_MAX_VIDEO : AST_FORMAT_MAX_AUDIO); x <<= 1) {
06620 if (!(capability & x))
06621 continue;
06622
06623 if (alreadysent & x)
06624 continue;
06625
06626 if (x <= AST_FORMAT_MAX_AUDIO)
06627 add_codec_to_sdp(p, x, SDP_SAMPLE_RATE(x),
06628 &m_audio_next, &m_audio_left,
06629 &a_audio_next, &a_audio_left,
06630 debug, &min_audio_packet_size);
06631 else
06632 add_codec_to_sdp(p, x, 90000,
06633 &m_video_next, &m_video_left,
06634 &a_video_next, &a_video_left,
06635 debug, &min_video_packet_size);
06636 }
06637
06638
06639 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
06640 if (!(p->jointnoncodeccapability & x))
06641 continue;
06642
06643 add_noncodec_to_sdp(p, x, 8000,
06644 &m_audio_next, &m_audio_left,
06645 &a_audio_next, &a_audio_left,
06646 debug);
06647 }
06648
06649 if (option_debug > 2)
06650 ast_log(LOG_DEBUG, "-- Done with adding codecs to SDP\n");
06651
06652 if (!p->owner || !ast_internal_timing_enabled(p->owner))
06653 ast_build_string(&a_audio_next, &a_audio_left, "a=silenceSupp:off - - - -\r\n");
06654
06655 if (min_audio_packet_size)
06656 ast_build_string(&a_audio_next, &a_audio_left, "a=ptime:%d\r\n", min_audio_packet_size);
06657
06658 if (min_video_packet_size)
06659 ast_build_string(&a_video_next, &a_video_left, "a=ptime:%d\r\n", min_video_packet_size);
06660
06661 if ((m_audio_left < 2) || (m_video_left < 2) || (a_audio_left == 0) || (a_video_left == 0))
06662 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
06663
06664 ast_build_string(&m_audio_next, &m_audio_left, "\r\n");
06665 if (needvideo)
06666 ast_build_string(&m_video_next, &m_video_left, "\r\n");
06667
06668 len = strlen(version) + strlen(subject) + strlen(owner) + strlen(connection) + strlen(stime) + strlen(m_audio) + strlen(a_audio) + strlen(hold);
06669 if (needvideo)
06670 len += strlen(m_video) + strlen(a_video) + strlen(bandwidth) + strlen(hold);
06671
06672 add_header(resp, "Content-Type", "application/sdp");
06673 add_header_contentLength(resp, len);
06674 add_line(resp, version);
06675 add_line(resp, owner);
06676 add_line(resp, subject);
06677 add_line(resp, connection);
06678 if (needvideo)
06679 add_line(resp, bandwidth);
06680 add_line(resp, stime);
06681 add_line(resp, m_audio);
06682 add_line(resp, a_audio);
06683 add_line(resp, hold);
06684 if (needvideo) {
06685 add_line(resp, m_video);
06686 add_line(resp, a_video);
06687 add_line(resp, hold);
06688 }
06689
06690
06691 p->lastrtprx = p->lastrtptx = time(NULL);
06692
06693 if (option_debug > 2) {
06694 char buf[SIPBUFSIZE];
06695 ast_log(LOG_DEBUG, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
06696 }
06697
06698 return AST_SUCCESS;
06699 }
06700
06701
06702 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
06703 {
06704 struct sip_request resp;
06705 int seqno;
06706
06707 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
06708 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
06709 return -1;
06710 }
06711 respprep(&resp, p, msg, req);
06712 if (p->udptl) {
06713 ast_udptl_offered_from_local(p->udptl, 0);
06714 add_t38_sdp(&resp, p);
06715 } else
06716 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
06717 if (retrans && !p->pendinginvite)
06718 p->pendinginvite = seqno;
06719 return send_response(p, &resp, retrans, seqno);
06720 }
06721
06722
06723 static void copy_request(struct sip_request *dst, const struct sip_request *src)
06724 {
06725 long offset;
06726 int x;
06727 offset = ((void *)dst) - ((void *)src);
06728
06729 memcpy(dst, src, sizeof(*dst));
06730
06731 for (x=0; x < src->headers; x++)
06732 dst->header[x] += offset;
06733 for (x=0; x < src->lines; x++)
06734 dst->line[x] += offset;
06735 dst->rlPart1 += offset;
06736 dst->rlPart2 += offset;
06737 }
06738
06739
06740
06741
06742 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06743 {
06744 struct sip_request resp;
06745 int seqno;
06746 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
06747 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
06748 return -1;
06749 }
06750 respprep(&resp, p, msg, req);
06751 if (p->rtp) {
06752 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06753 if (option_debug)
06754 ast_log(LOG_DEBUG, "Setting framing from config on incoming call\n");
06755 ast_rtp_codec_setpref(p->rtp, &p->prefs);
06756 }
06757 try_suggested_sip_codec(p);
06758 add_sdp(&resp, p);
06759 } else
06760 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
06761 if (reliable && !p->pendinginvite)
06762 p->pendinginvite = seqno;
06763 return send_response(p, &resp, reliable, seqno);
06764 }
06765
06766
06767 static int determine_firstline_parts(struct sip_request *req)
06768 {
06769 char *e = ast_skip_blanks(req->header[0]);
06770
06771 if (!*e)
06772 return -1;
06773 req->rlPart1 = e;
06774 e = ast_skip_nonblanks(e);
06775 if (*e)
06776 *e++ = '\0';
06777
06778 e = ast_skip_blanks(e);
06779 if ( !*e )
06780 return -1;
06781 ast_trim_blanks(e);
06782
06783 if (!strcasecmp(req->rlPart1, "SIP/2.0") ) {
06784 if (strlen(e) < 3)
06785 return -1;
06786 req->rlPart2 = e;
06787 } else {
06788 if ( *e == '<' ) {
06789 ast_log(LOG_WARNING, "bogus uri in <> %s\n", e);
06790 e++;
06791 if (!*e)
06792 return -1;
06793 }
06794 req->rlPart2 = e;
06795 e = ast_skip_nonblanks(e);
06796 if (*e)
06797 *e++ = '\0';
06798 e = ast_skip_blanks(e);
06799 if (strcasecmp(e, "SIP/2.0") ) {
06800 ast_log(LOG_WARNING, "Bad request protocol %s\n", e);
06801 return -1;
06802 }
06803 }
06804 return 1;
06805 }
06806
06807
06808
06809
06810
06811
06812
06813 static int transmit_reinvite_with_sdp(struct sip_pvt *p)
06814 {
06815 struct sip_request req;
06816
06817 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
06818
06819 add_header(&req, "Allow", ALLOWED_METHODS);
06820 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06821 if (sipdebug)
06822 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
06823 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
06824 append_history(p, "ReInv", "Re-invite sent");
06825 add_sdp(&req, p);
06826
06827 initialize_initreq(p, &req);
06828 p->lastinvite = p->ocseq;
06829 ast_set_flag(&p->flags[0], SIP_OUTGOING);
06830 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
06831 }
06832
06833
06834
06835
06836
06837 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p)
06838 {
06839 struct sip_request req;
06840
06841 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
06842
06843 add_header(&req, "Allow", ALLOWED_METHODS);
06844 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06845 if (sipdebug)
06846 add_header(&req, "X-asterisk-info", "SIP re-invite (T38 switchover)");
06847 ast_udptl_offered_from_local(p->udptl, 1);
06848 add_t38_sdp(&req, p);
06849
06850 initialize_initreq(p, &req);
06851 ast_set_flag(&p->flags[0], SIP_OUTGOING);
06852 p->lastinvite = p->ocseq;
06853 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
06854 }
06855
06856
06857 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
06858 {
06859 char stripped[SIPBUFSIZE];
06860 char *c;
06861
06862 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
06863 c = get_in_brackets(stripped);
06864 c = strsep(&c, ";");
06865 if (!ast_strlen_zero(c))
06866 ast_string_field_set(p, uri, c);
06867 }
06868
06869
06870 static void build_contact(struct sip_pvt *p)
06871 {
06872
06873 if (ourport != STANDARD_SIP_PORT)
06874 ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip), ourport);
06875 else
06876 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip));
06877 }
06878
06879
06880 static void build_rpid(struct sip_pvt *p)
06881 {
06882 int send_pres_tags = TRUE;
06883 const char *privacy=NULL;
06884 const char *screen=NULL;
06885 char buf[256];
06886 const char *clid = default_callerid;
06887 const char *clin = NULL;
06888 const char *fromdomain;
06889
06890 if (!ast_strlen_zero(p->rpid) || !ast_strlen_zero(p->rpid_from))
06891 return;
06892
06893 if (p->owner && p->owner->cid.cid_num)
06894 clid = p->owner->cid.cid_num;
06895 if (p->owner && p->owner->cid.cid_name)
06896 clin = p->owner->cid.cid_name;
06897 if (ast_strlen_zero(clin))
06898 clin = clid;
06899
06900 switch (p->callingpres) {
06901 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
06902 privacy = "off";
06903 screen = "no";
06904 break;
06905 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
06906 privacy = "off";
06907 screen = "yes";
06908 break;
06909 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
06910 privacy = "off";
06911 screen = "no";
06912 break;
06913 case AST_PRES_ALLOWED_NETWORK_NUMBER:
06914 privacy = "off";
06915 screen = "yes";
06916 break;
06917 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
06918 privacy = "full";
06919 screen = "no";
06920 break;
06921 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
06922 privacy = "full";
06923 screen = "yes";
06924 break;
06925 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
06926 privacy = "full";
06927 screen = "no";
06928 break;
06929 case AST_PRES_PROHIB_NETWORK_NUMBER:
06930 privacy = "full";
06931 screen = "yes";
06932 break;
06933 case AST_PRES_NUMBER_NOT_AVAILABLE:
06934 send_pres_tags = FALSE;
06935 break;
06936 default:
06937 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
06938 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
06939 privacy = "full";
06940 else
06941 privacy = "off";
06942 screen = "no";
06943 break;
06944 }
06945
06946 fromdomain = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip));
06947
06948 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
06949 if (send_pres_tags)
06950 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
06951 ast_string_field_set(p, rpid, buf);
06952
06953 ast_string_field_build(p, rpid_from, "\"%s\" <sip:%s@%s>;tag=%s", clin,
06954 S_OR(p->fromuser, clid),
06955 fromdomain, p->tag);
06956 }
06957
06958
06959 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
06960 {
06961 char invite_buf[256] = "";
06962 char *invite = invite_buf;
06963 size_t invite_max = sizeof(invite_buf);
06964 char from[256];
06965 char to[256];
06966 char tmp[SIPBUFSIZE/2];
06967 char tmp2[SIPBUFSIZE/2];
06968 const char *l = NULL, *n = NULL;
06969 const char *urioptions = "";
06970
06971 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
06972 const char *s = p->username;
06973
06974
06975
06976
06977
06978
06979 if (*s == '+')
06980 s++;
06981 for (; *s; s++) {
06982 if (!strchr(AST_DIGIT_ANYNUM, *s) )
06983 break;
06984 }
06985
06986 if (*s)
06987 urioptions = ";user=phone";
06988 }
06989
06990
06991 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
06992
06993 if (p->owner) {
06994 l = p->owner->cid.cid_num;
06995 n = p->owner->cid.cid_name;
06996 }
06997
06998 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID) &&
06999 ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
07000 l = CALLERID_UNKNOWN;
07001 n = l;
07002 }
07003 if (ast_strlen_zero(l))
07004 l = default_callerid;
07005 if (ast_strlen_zero(n))
07006 n = l;
07007
07008 if (!ast_strlen_zero(p->fromuser))
07009 l = p->fromuser;
07010 else
07011 ast_string_field_set(p, fromuser, l);
07012
07013
07014 if (!ast_strlen_zero(p->fromname))
07015 n = p->fromname;
07016 else
07017 ast_string_field_set(p, fromname, n);
07018
07019 if (pedanticsipchecking) {
07020 ast_uri_encode(n, tmp, sizeof(tmp), 0);
07021 n = tmp;
07022 ast_uri_encode(l, tmp2, sizeof(tmp2), 0);
07023 l = tmp2;
07024 }
07025
07026 if (ourport != STANDARD_SIP_PORT && ast_strlen_zero(p->fromdomain))
07027 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), ourport, p->tag);
07028 else
07029 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), p->tag);
07030
07031
07032 if (!ast_strlen_zero(p->fullcontact)) {
07033
07034 ast_build_string(&invite, &invite_max, "%s", p->fullcontact);
07035 } else {
07036
07037 ast_build_string(&invite, &invite_max, "sip:");
07038 if (!ast_strlen_zero(p->username)) {
07039 n = p->username;
07040 if (pedanticsipchecking) {
07041 ast_uri_encode(n, tmp, sizeof(tmp), 0);
07042 n = tmp;
07043 }
07044 ast_build_string(&invite, &invite_max, "%s@", n);
07045 }
07046 ast_build_string(&invite, &invite_max, "%s", p->tohost);
07047 if (ntohs(p->sa.sin_port) != STANDARD_SIP_PORT)
07048 ast_build_string(&invite, &invite_max, ":%d", ntohs(p->sa.sin_port));
07049 ast_build_string(&invite, &invite_max, "%s", urioptions);
07050 }
07051
07052
07053 if (p->options && !ast_strlen_zero(p->options->uri_options))
07054 ast_build_string(&invite, &invite_max, ";%s", p->options->uri_options);
07055
07056 ast_string_field_set(p, uri, invite_buf);
07057
07058 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
07059
07060 snprintf(to, sizeof(to), "<%s%s>;tag=%s", (strncasecmp(p->uri, "sip:", 4) ? "" : "sip:"), p->uri, p->theirtag);
07061 } else if (p->options && p->options->vxml_url) {
07062
07063 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
07064 } else
07065 snprintf(to, sizeof(to), "<%s>", p->uri);
07066
07067 init_req(req, sipmethod, p->uri);
07068 snprintf(tmp, sizeof(tmp), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
07069
07070 add_header(req, "Via", p->via);
07071
07072
07073
07074 if (ast_test_flag(&p->flags[0], SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
07075 build_rpid(p);
07076 add_header(req, "From", p->rpid_from);
07077 } else
07078 add_header(req, "From", from);
07079 add_header(req, "To", to);
07080 ast_string_field_set(p, exten, l);
07081 build_contact(p);
07082 add_header(req, "Contact", p->our_contact);
07083 add_header(req, "Call-ID", p->callid);
07084 add_header(req, "CSeq", tmp);
07085 if (!ast_strlen_zero(global_useragent))
07086 add_header(req, "User-Agent", global_useragent);
07087 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07088 if (!ast_strlen_zero(p->rpid))
07089 add_header(req, "Remote-Party-ID", p->rpid);
07090 }
07091
07092
07093 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
07094 {
07095 struct sip_request req;
07096
07097 req.method = sipmethod;
07098 if (init) {
07099
07100 p->branch ^= ast_random();
07101 build_via(p);
07102 if (init > 1)
07103 initreqprep(&req, p, sipmethod);
07104 else
07105 reqprep(&req, p, sipmethod, 0, 1);
07106 } else
07107 reqprep(&req, p, sipmethod, 0, 1);
07108
07109 if (p->options && p->options->auth)
07110 add_header(&req, p->options->authheader, p->options->auth);
07111 append_date(&req);
07112 if (sipmethod == SIP_REFER) {
07113 if (p->refer) {
07114 char buf[SIPBUFSIZE];
07115 if (!ast_strlen_zero(p->refer->refer_to))
07116 add_header(&req, "Refer-To", p->refer->refer_to);
07117 if (!ast_strlen_zero(p->refer->referred_by)) {
07118 snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
07119 add_header(&req, "Referred-By", buf);
07120 }
07121 }
07122 }
07123
07124
07125 if (p->options && p->options->replaces && !ast_strlen_zero(p->options->replaces)) {
07126 add_header(&req, "Replaces", p->options->replaces);
07127 add_header(&req, "Require", "replaces");
07128 }
07129
07130 add_header(&req, "Allow", ALLOWED_METHODS);
07131 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07132 if (p->options && p->options->addsipheaders && p->owner) {
07133 struct ast_channel *chan = p->owner;
07134 struct varshead *headp;
07135
07136 ast_channel_lock(chan);
07137
07138 headp = &chan->varshead;
07139
07140 if (!headp)
07141 ast_log(LOG_WARNING,"No Headp for the channel...ooops!\n");
07142 else {
07143 const struct ast_var_t *current;
07144 AST_LIST_TRAVERSE(headp, current, entries) {
07145
07146 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
07147 char *content, *end;
07148 const char *header = ast_var_value(current);
07149 char *headdup = ast_strdupa(header);
07150
07151
07152 if (*headdup == '"')
07153 headdup++;
07154 if ((content = strchr(headdup, ':'))) {
07155 *content++ = '\0';
07156 content = ast_skip_blanks(content);
07157
07158 end = content + strlen(content) -1;
07159 if (*end == '"')
07160 *end = '\0';
07161
07162 add_header(&req, headdup, content);
07163 if (sipdebug)
07164 ast_log(LOG_DEBUG, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
07165 }
07166 }
07167 }
07168 }
07169
07170 ast_channel_unlock(chan);
07171 }
07172 if (sdp) {
07173 if (p->udptl && (p->t38.state == T38_LOCAL_DIRECT || p->t38.state == T38_LOCAL_REINVITE)) {
07174 ast_udptl_offered_from_local(p->udptl, 1);
07175 if (option_debug)
07176 ast_log(LOG_DEBUG, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
07177 add_t38_sdp(&req, p);
07178 } else if (p->rtp)
07179 add_sdp(&req, p);
07180 } else {
07181 add_header_contentLength(&req, 0);
07182 }
07183
07184 if (!p->initreq.headers)
07185 initialize_initreq(p, &req);
07186 p->lastinvite = p->ocseq;
07187 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
07188 }
07189
07190
07191 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
07192 {
07193 char tmp[4000], from[256], to[256];
07194 char *t = tmp, *c, *mfrom, *mto;
07195 size_t maxbytes = sizeof(tmp);
07196 struct sip_request req;
07197 char hint[AST_MAX_EXTENSION];
07198 char *statestring = "terminated";
07199 const struct cfsubscription_types *subscriptiontype;
07200 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
07201 char *pidfstate = "--";
07202 char *pidfnote= "Ready";
07203
07204 memset(from, 0, sizeof(from));
07205 memset(to, 0, sizeof(to));
07206 memset(tmp, 0, sizeof(tmp));
07207
07208 switch (state) {
07209 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
07210 statestring = (global_notifyringing) ? "early" : "confirmed";
07211 local_state = NOTIFY_INUSE;
07212 pidfstate = "busy";
07213 pidfnote = "Ringing";
07214 break;
07215 case AST_EXTENSION_RINGING:
07216 statestring = "early";
07217 local_state = NOTIFY_INUSE;
07218 pidfstate = "busy";
07219 pidfnote = "Ringing";
07220 break;
07221 case AST_EXTENSION_INUSE:
07222 statestring = "confirmed";
07223 local_state = NOTIFY_INUSE;
07224 pidfstate = "busy";
07225 pidfnote = "On the phone";
07226 break;
07227 case AST_EXTENSION_BUSY:
07228 statestring = "confirmed";
07229 local_state = NOTIFY_CLOSED;
07230 pidfstate = "busy";
07231 pidfnote = "On the phone";
07232 break;
07233 case AST_EXTENSION_UNAVAILABLE:
07234 statestring = "terminated";
07235 local_state = NOTIFY_CLOSED;
07236 pidfstate = "away";
07237 pidfnote = "Unavailable";
07238 break;
07239 case AST_EXTENSION_ONHOLD:
07240 statestring = "confirmed";
07241 local_state = NOTIFY_CLOSED;
07242 pidfstate = "busy";
07243 pidfnote = "On Hold";
07244 break;
07245 case AST_EXTENSION_NOT_INUSE:
07246 default:
07247
07248 break;
07249 }
07250
07251 subscriptiontype = find_subscription_type(p->subscribed);
07252
07253
07254 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
07255 char *hint2 = hint, *individual_hint = NULL;
07256 int hint_count = 0, unavailable_count = 0;
07257
07258 while ((individual_hint = strsep(&hint2, "&"))) {
07259 hint_count++;
07260
07261 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE)
07262 unavailable_count++;
07263 }
07264
07265
07266
07267
07268 if (hint_count > 0 && hint_count == unavailable_count) {
07269 local_state = NOTIFY_CLOSED;
07270 pidfstate = "away";
07271 pidfnote = "Not online";
07272 }
07273 }
07274
07275 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
07276 c = get_in_brackets(from);
07277 if (strncasecmp(c, "sip:", 4)) {
07278 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
07279 return -1;
07280 }
07281 mfrom = strsep(&c, ";");
07282
07283 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
07284 c = get_in_brackets(to);
07285 if (strncasecmp(c, "sip:", 4)) {
07286 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
07287 return -1;
07288 }
07289 mto = strsep(&c, ";");
07290
07291 reqprep(&req, p, SIP_NOTIFY, 0, 1);
07292
07293
07294 add_header(&req, "Event", subscriptiontype->event);
07295 add_header(&req, "Content-Type", subscriptiontype->mediatype);
07296 switch(state) {
07297 case AST_EXTENSION_DEACTIVATED:
07298 if (timeout)
07299 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07300 else {
07301 add_header(&req, "Subscription-State", "terminated;reason=probation");
07302 add_header(&req, "Retry-After", "60");
07303 }
07304 break;
07305 case AST_EXTENSION_REMOVED:
07306 add_header(&req, "Subscription-State", "terminated;reason=noresource");
07307 break;
07308 default:
07309 if (p->expiry)
07310 add_header(&req, "Subscription-State", "active");
07311 else
07312 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07313 }
07314 switch (p->subscribed) {
07315 case XPIDF_XML:
07316 case CPIM_PIDF_XML:
07317 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
07318 ast_build_string(&t, &maxbytes, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n");
07319 ast_build_string(&t, &maxbytes, "<presence>\n");
07320 ast_build_string(&t, &maxbytes, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
07321 ast_build_string(&t, &maxbytes, "<atom id=\"%s\">\n", p->exten);
07322 ast_build_string(&t, &maxbytes, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
07323 ast_build_string(&t, &maxbytes, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
07324 ast_build_string(&t, &maxbytes, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
07325 ast_build_string(&t, &maxbytes, "</address>\n</atom>\n</presence>\n");
07326 break;
07327 case PIDF_XML:
07328 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
07329 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);
07330 ast_build_string(&t, &maxbytes, "<pp:person><status>\n");
07331 if (pidfstate[0] != '-')
07332 ast_build_string(&t, &maxbytes, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
07333 ast_build_string(&t, &maxbytes, "</status></pp:person>\n");
07334 ast_build_string(&t, &maxbytes, "<note>%s</note>\n", pidfnote);
07335 ast_build_string(&t, &maxbytes, "<tuple id=\"%s\">\n", p->exten);
07336 ast_build_string(&t, &maxbytes, "<contact priority=\"1\">%s</contact>\n", mto);
07337 if (pidfstate[0] == 'b')
07338 ast_build_string(&t, &maxbytes, "<status><basic>open</basic></status>\n");
07339 else
07340 ast_build_string(&t, &maxbytes, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
07341 ast_build_string(&t, &maxbytes, "</tuple>\n</presence>\n");
07342 break;
07343 case DIALOG_INFO_XML:
07344 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
07345 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);
07346 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
07347 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
07348 else
07349 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\">\n", p->exten);
07350 ast_build_string(&t, &maxbytes, "<state>%s</state>\n", statestring);
07351 if (state == AST_EXTENSION_ONHOLD) {
07352 ast_build_string(&t, &maxbytes, "<local>\n<target uri=\"%s\">\n"
07353 "<param pname=\"+sip.rendering\" pvalue=\"no\">\n"
07354 "</target>\n</local>\n", mto);
07355 }
07356 ast_build_string(&t, &maxbytes, "</dialog>\n</dialog-info>\n");
07357 break;
07358 case NONE:
07359 default:
07360 break;
07361 }
07362
07363 if (t > tmp + sizeof(tmp))
07364 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07365
07366 add_header_contentLength(&req, strlen(tmp));
07367 add_line(&req, tmp);
07368 p->pendinginvite = p->ocseq;
07369
07370 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07371 }
07372
07373
07374
07375
07376
07377
07378
07379 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
07380 {
07381 struct sip_request req;
07382 char tmp[500];
07383 char *t = tmp;
07384 size_t maxbytes = sizeof(tmp);
07385
07386 initreqprep(&req, p, SIP_NOTIFY);
07387 add_header(&req, "Event", "message-summary");
07388 add_header(&req, "Content-Type", default_notifymime);
07389
07390 ast_build_string(&t, &maxbytes, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
07391 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n",
07392 S_OR(vmexten, default_vmexten), S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)));
07393
07394
07395
07396 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)"));
07397
07398 if (p->subscribed) {
07399 if (p->expiry)
07400 add_header(&req, "Subscription-State", "active");
07401 else
07402 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07403 }
07404
07405 if (t > tmp + sizeof(tmp))
07406 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07407
07408 add_header_contentLength(&req, strlen(tmp));
07409 add_line(&req, tmp);
07410
07411 if (!p->initreq.headers)
07412 initialize_initreq(p, &req);
07413 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07414 }
07415
07416
07417 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req)
07418 {
07419 if (!p->initreq.headers)
07420 initialize_initreq(p, req);
07421 return send_request(p, req, XMIT_UNRELIABLE, p->ocseq);
07422 }
07423
07424
07425 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
07426 {
07427 struct sip_request req;
07428 char tmp[SIPBUFSIZE/2];
07429
07430 reqprep(&req, p, SIP_NOTIFY, 0, 1);
07431 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
07432 add_header(&req, "Event", tmp);
07433 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
07434 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
07435 add_header(&req, "Allow", ALLOWED_METHODS);
07436 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07437
07438 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
07439 add_header_contentLength(&req, strlen(tmp));
07440 add_line(&req, tmp);
07441
07442 if (!p->initreq.headers)
07443 initialize_initreq(p, &req);
07444
07445 p->lastnoninvite = p->ocseq;
07446
07447 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07448 }
07449
07450
07451 static char *regstate2str(enum sipregistrystate regstate)
07452 {
07453 switch(regstate) {
07454 case REG_STATE_FAILED:
07455 return "Failed";
07456 case REG_STATE_UNREGISTERED:
07457 return "Unregistered";
07458 case REG_STATE_REGSENT:
07459 return "Request Sent";
07460 case REG_STATE_AUTHSENT:
07461 return "Auth. Sent";
07462 case REG_STATE_REGISTERED:
07463 return "Registered";
07464 case REG_STATE_REJECTED:
07465 return "Rejected";
07466 case REG_STATE_TIMEOUT:
07467 return "Timeout";
07468 case REG_STATE_NOAUTH:
07469 return "No Authentication";
07470 default:
07471 return "Unknown";
07472 }
07473 }
07474
07475
07476 static int sip_reregister(const void *data)
07477 {
07478
07479 struct sip_registry *r= ASTOBJ_REF((struct sip_registry *) data);
07480
07481
07482 if (!r)
07483 return 0;
07484
07485 if (r->call && !ast_test_flag(&r->call->flags[0], SIP_NO_HISTORY))
07486 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
07487
07488
07489 if (sipdebug)
07490 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
07491
07492 r->expire = -1;
07493 __sip_do_register(r);
07494 ASTOBJ_UNREF(r, sip_registry_destroy);
07495 return 0;
07496 }
07497
07498
07499 static int __sip_do_register(struct sip_registry *r)
07500 {
07501 int res;
07502
07503 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
07504 return res;
07505 }
07506
07507
07508 static int sip_reg_timeout(const void *data)
07509 {
07510
07511
07512 struct sip_registry *r = ASTOBJ_REF((struct sip_registry *) data);
07513 struct sip_pvt *p;
07514 int res;
07515
07516
07517 if (!r)
07518 return 0;
07519
07520 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
07521 if (r->call) {
07522
07523
07524 p = r->call;
07525 ast_mutex_lock(&p->lock);
07526 if (p->registry)
07527 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
07528 r->call = NULL;
07529 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
07530
07531 __sip_pretend_ack(p);
07532 ast_mutex_unlock(&p->lock);
07533 }
07534
07535 if (global_regattempts_max && (r->regattempts > global_regattempts_max)) {
07536
07537
07538
07539 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
07540 r->regstate = REG_STATE_FAILED;
07541 } else {
07542 r->regstate = REG_STATE_UNREGISTERED;
07543 r->timeout = -1;
07544 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
07545 }
07546 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));
07547 ASTOBJ_UNREF(r, sip_registry_destroy);
07548 return 0;
07549 }
07550
07551
07552 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
07553 {
07554 struct sip_request req;
07555 char from[256];
07556 char to[256];
07557 char tmp[80];
07558 char addr[80];
07559 struct sip_pvt *p;
07560
07561
07562 if ( r == NULL || ((auth==NULL) && (r->regstate==REG_STATE_REGSENT || r->regstate==REG_STATE_AUTHSENT))) {
07563 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
07564 return 0;
07565 }
07566
07567 if (r->call) {
07568 if (!auth) {
07569 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
07570 return 0;
07571 } else {
07572 p = r->call;
07573 make_our_tag(p->tag, sizeof(p->tag));
07574 ast_string_field_free(p, theirtag);
07575 }
07576 } else {
07577
07578 if (!r->callid_valid) {
07579 build_callid_registry(r, __ourip, default_fromdomain);
07580 r->callid_valid = TRUE;
07581 }
07582
07583 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER))) {
07584 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
07585 return 0;
07586 }
07587 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
07588 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
07589
07590 if (create_addr(p, r->hostname)) {
07591
07592
07593 sip_destroy(p);
07594
07595 if (r->timeout > -1)
07596 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
07597 else
07598 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);
07599
07600 AST_SCHED_DEL(sched, r->timeout);
07601 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
07602 r->regattempts++;
07603 return 0;
07604 }
07605
07606 ast_string_field_set(r, callid, p->callid);
07607 if (r->portno) {
07608 p->sa.sin_port = htons(r->portno);
07609 p->recv.sin_port = htons(r->portno);
07610 } else
07611 r->portno = ntohs(p->sa.sin_port);
07612 ast_set_flag(&p->flags[0], SIP_OUTGOING);
07613 r->call=p;
07614 p->registry = ASTOBJ_REF(r);
07615 if (!ast_strlen_zero(r->secret))
07616 ast_string_field_set(p, peersecret, r->secret);
07617 if (!ast_strlen_zero(r->md5secret))
07618 ast_string_field_set(p, peermd5secret, r->md5secret);
07619
07620
07621 if (!ast_strlen_zero(r->authuser)) {
07622 ast_string_field_set(p, peername, r->authuser);
07623 ast_string_field_set(p, authname, r->authuser);
07624 } else if (!ast_strlen_zero(r->username)) {
07625 ast_string_field_set(p, peername, r->username);
07626 ast_string_field_set(p, authname, r->username);
07627 ast_string_field_set(p, fromuser, r->username);
07628 }
07629 if (!ast_strlen_zero(r->username))
07630 ast_string_field_set(p, username, r->username);
07631
07632 ast_string_field_set(p, exten, r->contact);
07633
07634
07635
07636
07637
07638
07639 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
07640 p->ourip = bindaddr.sin_addr;
07641 build_contact(p);
07642 }
07643
07644
07645 if (auth == NULL) {
07646 if (r->timeout > -1)
07647 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
07648 AST_SCHED_DEL(sched, r->timeout);
07649 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
07650 if (option_debug)
07651 ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
07652 }
07653
07654 if (strchr(r->username, '@')) {
07655 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
07656 if (!ast_strlen_zero(p->theirtag))
07657 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
07658 else
07659 snprintf(to, sizeof(to), "<sip:%s>", r->username);
07660 } else {
07661 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
07662 if (!ast_strlen_zero(p->theirtag))
07663 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
07664 else
07665 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
07666 }
07667
07668
07669
07670 if (!ast_strlen_zero(p->fromdomain)) {
07671 if (r->portno && r->portno != STANDARD_SIP_PORT)
07672 snprintf(addr, sizeof(addr), "sip:%s:%d", p->fromdomain, r->portno);
07673 else
07674 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
07675 } else {
07676 if (r->portno && r->portno != STANDARD_SIP_PORT)
07677 snprintf(addr, sizeof(addr), "sip:%s:%d", r->hostname, r->portno);
07678 else
07679 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
07680 }
07681 ast_string_field_set(p, uri, addr);
07682
07683 p->branch ^= ast_random();
07684
07685 init_req(&req, sipmethod, addr);
07686
07687
07688 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
07689 p->ocseq = r->ocseq;
07690
07691 build_via(p);
07692 add_header(&req, "Via", p->via);
07693 add_header(&req, "From", from);
07694 add_header(&req, "To", to);
07695 add_header(&req, "Call-ID", p->callid);
07696 add_header(&req, "CSeq", tmp);
07697 if (!ast_strlen_zero(global_useragent))
07698 add_header(&req, "User-Agent", global_useragent);
07699 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07700
07701
07702 if (auth)
07703 add_header(&req, authheader, auth);
07704 else if (!ast_strlen_zero(r->nonce)) {
07705 char digest[1024];
07706
07707
07708 if (sipdebug)
07709 ast_log(LOG_DEBUG, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
07710 ast_string_field_set(p, realm, r->realm);
07711 ast_string_field_set(p, nonce, r->nonce);
07712 ast_string_field_set(p, domain, r->domain);
07713 ast_string_field_set(p, opaque, r->opaque);
07714 ast_string_field_set(p, qop, r->qop);
07715 r->noncecount++;
07716 p->noncecount = r->noncecount;
07717
07718 memset(digest,0,sizeof(digest));
07719 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
07720 add_header(&req, "Authorization", digest);
07721 else
07722 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
07723
07724 }
07725
07726 snprintf(tmp, sizeof(tmp), "%d", default_expiry);
07727 add_header(&req, "Expires", tmp);
07728 add_header(&req, "Contact", p->our_contact);
07729 add_header(&req, "Event", "registration");
07730 add_header_contentLength(&req, 0);
07731
07732 initialize_initreq(p, &req);
07733 if (sip_debug_test_pvt(p))
07734 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
07735 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
07736 r->regattempts++;
07737 if (option_debug > 3)
07738 ast_verbose("REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
07739 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
07740 }
07741
07742
07743 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
07744 {
07745 struct sip_request req;
07746
07747 reqprep(&req, p, SIP_MESSAGE, 0, 1);
07748 add_text(&req, text);
07749 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07750 }
07751
07752
07753 static int sip_refer_allocate(struct sip_pvt *p)
07754 {
07755 p->refer = ast_calloc(1, sizeof(struct sip_refer));
07756 return p->refer ? 1 : 0;
07757 }
07758
07759
07760
07761
07762
07763
07764 static int transmit_refer(struct sip_pvt *p, const char *dest)
07765 {
07766 struct sip_request req = {
07767 .headers = 0,
07768 };
07769 char from[256];
07770 const char *of;
07771 char *c;
07772 char referto[256];
07773 char *ttag, *ftag;
07774 char *theirtag = ast_strdupa(p->theirtag);
07775
07776 if (option_debug || sipdebug)
07777 ast_log(LOG_DEBUG, "SIP transfer of %s to %s\n", p->callid, dest);
07778
07779
07780 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
07781 of = get_header(&p->initreq, "To");
07782 ttag = theirtag;
07783 ftag = p->tag;
07784 } else {
07785 of = get_header(&p->initreq, "From");
07786 ftag = theirtag;
07787 ttag = p->tag;
07788 }
07789
07790 ast_copy_string(from, of, sizeof(from));
07791 of = get_in_brackets(from);
07792 ast_string_field_set(p, from, of);
07793 if (strncasecmp(of, "sip:", 4))
07794 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
07795 else
07796 of += 4;
07797
07798 if ((c = strchr(dest, '@')))
07799 c = NULL;
07800 else if ((c = strchr(of, '@')))
07801 *c++ = '\0';
07802 if (c)
07803 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
07804 else
07805 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
07806
07807
07808 sip_refer_allocate(p);
07809 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
07810 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
07811 p->refer->status = REFER_SENT;
07812
07813 reqprep(&req, p, SIP_REFER, 0, 1);
07814
07815 add_header(&req, "Refer-To", referto);
07816 add_header(&req, "Allow", ALLOWED_METHODS);
07817 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07818 if (!ast_strlen_zero(p->our_contact))
07819 add_header(&req, "Referred-By", p->our_contact);
07820
07821 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07822
07823
07824
07825
07826
07827
07828
07829
07830 }
07831
07832
07833
07834 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
07835 {
07836 struct sip_request req;
07837
07838 reqprep(&req, p, SIP_INFO, 0, 1);
07839 add_digit(&req, digit, duration);
07840 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07841 }
07842
07843
07844 static int transmit_info_with_vidupdate(struct sip_pvt *p)
07845 {
07846 struct sip_request req;
07847
07848 reqprep(&req, p, SIP_INFO, 0, 1);
07849 add_vidupdate(&req);
07850 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07851 }
07852
07853
07854
07855
07856 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
07857 {
07858 struct sip_request resp;
07859
07860 if (sipmethod == SIP_ACK)
07861 p->invitestate = INV_CONFIRMED;
07862
07863 reqprep(&resp, p, sipmethod, seqno, newbranch);
07864 add_header_contentLength(&resp, 0);
07865 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
07866 }
07867
07868
07869 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
07870 {
07871 struct sip_request resp;
07872
07873 reqprep(&resp, p, sipmethod, seqno, newbranch);
07874 if (!ast_strlen_zero(p->realm)) {
07875 char digest[1024];
07876
07877 memset(digest, 0, sizeof(digest));
07878 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
07879 if (p->options && p->options->auth_type == PROXY_AUTH)
07880 add_header(&resp, "Proxy-Authorization", digest);
07881 else if (p->options && p->options->auth_type == WWW_AUTH)
07882 add_header(&resp, "Authorization", digest);
07883 else
07884 add_header(&resp, "Proxy-Authorization", digest);
07885 } else
07886 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
07887 }
07888
07889
07890 if (sipmethod == SIP_BYE && p->owner && p->owner->hangupcause) {
07891 char buf[10];
07892
07893 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
07894 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
07895 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
07896 }
07897
07898 add_header_contentLength(&resp, 0);
07899 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
07900 }
07901
07902
07903 static void destroy_association(struct sip_peer *peer)
07904 {
07905 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE)) {
07906 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
07907 ast_update_realtime("sippeers", "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "username", "", "regserver", "", NULL);
07908 else
07909 ast_db_del("SIP/Registry", peer->name);
07910 }
07911 }
07912
07913
07914 static int expire_register(const void *data)
07915 {
07916 struct sip_peer *peer = (struct sip_peer *)data;
07917
07918 if (!peer)
07919 return 0;
07920
07921 memset(&peer->addr, 0, sizeof(peer->addr));
07922
07923 destroy_association(peer);
07924
07925 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
07926 register_peer_exten(peer, FALSE);
07927 peer->expire = -1;
07928 ast_device_state_changed("SIP/%s", peer->name);
07929
07930
07931
07932
07933 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT) ||
07934 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
07935 struct sip_peer *peer_ptr = peer_ptr;
07936 peer_ptr = ASTOBJ_CONTAINER_UNLINK(&peerl, peer);
07937 if (peer_ptr) {
07938 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
07939 }
07940 }
07941
07942 ASTOBJ_UNREF(peer, sip_destroy_peer);
07943
07944 return 0;
07945 }
07946
07947
07948 static int sip_poke_peer_s(const void *data)
07949 {
07950 struct sip_peer *peer = (struct sip_peer *) data;
07951
07952 peer->pokeexpire = -1;
07953
07954 sip_poke_peer(peer);
07955
07956 ASTOBJ_UNREF(peer, sip_destroy_peer);
07957
07958 return 0;
07959 }
07960
07961
07962 static void reg_source_db(struct sip_peer *peer)
07963 {
07964 char data[256];
07965 struct in_addr in;
07966 int expiry;
07967 int port;
07968 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
07969
07970 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
07971 return;
07972 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
07973 return;
07974
07975 scan = data;
07976 addr = strsep(&scan, ":");
07977 port_str = strsep(&scan, ":");
07978 expiry_str = strsep(&scan, ":");
07979 username = strsep(&scan, ":");
07980 contact = scan;
07981
07982 if (!inet_aton(addr, &in))
07983 return;
07984
07985 if (port_str)
07986 port = atoi(port_str);
07987 else
07988 return;
07989
07990 if (expiry_str)
07991 expiry = atoi(expiry_str);
07992 else
07993 return;
07994
07995 if (username)
07996 ast_copy_string(peer->username, username, sizeof(peer->username));
07997 if (contact)
07998 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
07999
08000 if (option_debug > 1)
08001 ast_log(LOG_DEBUG, "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
08002 peer->name, peer->username, ast_inet_ntoa(in), port, expiry);
08003
08004 memset(&peer->addr, 0, sizeof(peer->addr));
08005 peer->addr.sin_family = AF_INET;
08006 peer->addr.sin_addr = in;
08007 peer->addr.sin_port = htons(port);
08008 if (sipsock < 0) {
08009
08010 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
08011 struct sip_peer *peer_ptr = peer;
08012 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08013 }
08014 peer->pokeexpire = ast_sched_add(sched, ast_random() % 5000 + 1, sip_poke_peer_s, ASTOBJ_REF(peer));
08015 if (peer->pokeexpire == -1) {
08016 struct sip_peer *peer_ptr = peer;
08017 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08018 }
08019 } else
08020 sip_poke_peer(peer);
08021 if (!AST_SCHED_DEL(sched, peer->expire)) {
08022 struct sip_peer *peer_ptr = peer;
08023 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08024 }
08025 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, ASTOBJ_REF(peer));
08026 if (peer->expire == -1) {
08027 struct sip_peer *peer_ptr = peer;
08028 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08029 }
08030 register_peer_exten(peer, TRUE);
08031 }
08032
08033
08034 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
08035 {
08036 char contact[SIPBUFSIZE];
08037 char *c;
08038
08039
08040 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
08041 c = get_in_brackets(contact);
08042
08043
08044 ast_string_field_set(pvt, fullcontact, c);
08045
08046
08047 ast_string_field_set(pvt, okcontacturi, c);
08048
08049
08050
08051 return TRUE;
08052 }
08053
08054
08055 static int set_address_from_contact(struct sip_pvt *pvt)
08056 {
08057 struct hostent *hp;
08058 struct ast_hostent ahp;
08059 int port;
08060 char *c, *host, *pt;
08061 char contact_buf[256];
08062 char *contact;
08063
08064 if (ast_test_flag(&pvt->flags[0], SIP_NAT_ROUTE)) {
08065
08066
08067 pvt->sa = pvt->recv;
08068 return 0;
08069 }
08070
08071
08072 ast_copy_string(contact_buf, pvt->fullcontact, sizeof(contact_buf));
08073 contact = contact_buf;
08074
08075
08076 if (strncasecmp(contact, "sip:", 4)) {
08077 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
08078 } else
08079 contact += 4;
08080
08081
08082
08083
08084
08085 host = strchr(contact, '@');
08086 if (!host) {
08087 host = contact;
08088 c = NULL;
08089 } else {
08090 *host++ = '\0';
08091 }
08092 pt = strchr(host, ':');
08093 if (pt) {
08094 *pt++ = '\0';
08095 port = atoi(pt);
08096 } else
08097 port = STANDARD_SIP_PORT;
08098
08099 contact = strsep(&contact, ";");
08100 host = strsep(&host, ";");
08101
08102
08103
08104 hp = ast_gethostbyname(host, &ahp);
08105 if (!hp) {
08106 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't resolve in DNS) : '%s'\n", host);
08107 return -1;
08108 }
08109 pvt->sa.sin_family = AF_INET;
08110 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
08111 pvt->sa.sin_port = htons(port);
08112
08113 return 0;
08114 }
08115
08116
08117
08118 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
08119 {
08120 char contact[SIPBUFSIZE];
08121 char data[SIPBUFSIZE];
08122 const char *expires = get_header(req, "Expires");
08123 int expiry = atoi(expires);
08124 char *curi, *n, *pt;
08125 int port;
08126 const char *useragent;
08127 struct hostent *hp;
08128 struct ast_hostent ahp;
08129 struct sockaddr_in oldsin;
08130
08131 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
08132
08133 if (ast_strlen_zero(expires)) {
08134 expires = strcasestr(contact, ";expires=");
08135 if (expires) {
08136
08137 expires = strsep((char **) &expires, ";");
08138 if (sscanf(expires + 9, "%d", &expiry) != 1)
08139 expiry = default_expiry;
08140 } else {
08141
08142 expiry = default_expiry;
08143 }
08144 }
08145
08146
08147 curi = contact;
08148 if (strchr(contact, '<') == NULL)
08149 strsep(&curi, ";");
08150 curi = get_in_brackets(contact);
08151
08152
08153
08154
08155
08156 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
08157
08158 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact))
08159 pvt->expiry = ast_sched_when(sched, peer->expire);
08160 return PARSE_REGISTER_QUERY;
08161 } else if (!strcasecmp(curi, "*") || !expiry) {
08162
08163 memset(&peer->addr, 0, sizeof(peer->addr));
08164 if (!AST_SCHED_DEL(sched, peer->expire)) {
08165 struct sip_peer *peer_ptr = peer;
08166 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08167 }
08168
08169 destroy_association(peer);
08170
08171 register_peer_exten(peer, 0);
08172 peer->fullcontact[0] = '\0';
08173 peer->useragent[0] = '\0';
08174 peer->sipoptions = 0;
08175 peer->lastms = 0;
08176
08177 if (option_verbose > 2)
08178 ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", peer->name);
08179 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\n", peer->name);
08180 return PARSE_REGISTER_UPDATE;
08181 }
08182
08183
08184 ast_copy_string(peer->fullcontact, curi, sizeof(peer->fullcontact));
08185
08186
08187 ast_string_field_build(pvt, our_contact, "<%s>", curi);
08188
08189
08190 if (strncasecmp(curi, "sip:", 4)) {
08191 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", curi);
08192 } else
08193 curi += 4;
08194
08195 curi = strsep(&curi, ";");
08196
08197 n = strchr(curi, '@');
08198 if (!n) {
08199 n = curi;
08200 curi = NULL;
08201 } else
08202 *n++ = '\0';
08203 pt = strchr(n, ':');
08204 if (pt) {
08205 *pt++ = '\0';
08206 port = atoi(pt);
08207 } else
08208 port = STANDARD_SIP_PORT;
08209 oldsin = peer->addr;
08210 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
08211
08212 hp = ast_gethostbyname(n, &ahp);
08213 if (!hp) {
08214 ast_log(LOG_WARNING, "Invalid host '%s'\n", n);
08215 return PARSE_REGISTER_FAILED;
08216 }
08217 peer->addr.sin_family = AF_INET;
08218 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
08219 peer->addr.sin_port = htons(port);
08220 } else {
08221
08222
08223 peer->addr = pvt->recv;
08224 }
08225
08226
08227 peer->sipoptions = pvt->sipoptions;
08228
08229 if (curi && ast_strlen_zero(peer->username))
08230 ast_copy_string(peer->username, curi, sizeof(peer->username));
08231
08232 if (!AST_SCHED_DEL(sched, peer->expire)) {
08233 struct sip_peer *peer_ptr = peer;
08234 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08235 }
08236 if (expiry > max_expiry)
08237 expiry = max_expiry;
08238 if (expiry < min_expiry)
08239 expiry = min_expiry;
08240 if (ast_test_flag(&peer->flags[0], SIP_REALTIME)) {
08241 peer->expire = -1;
08242 } else {
08243 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, ASTOBJ_REF(peer));
08244 if (peer->expire == -1) {
08245 struct sip_peer *peer_ptr = peer;
08246 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
08247 }
08248 }
08249 pvt->expiry = expiry;
08250 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);
08251 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
08252 ast_db_put("SIP/Registry", peer->name, data);
08253 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
08254
08255
08256 if (inaddrcmp(&peer->addr, &oldsin)) {
08257 sip_poke_peer(peer);
08258 if (option_verbose > 2)
08259 ast_verbose(VERBOSE_PREFIX_3 "Registered SIP '%s' at %s port %d expires %d\n", peer->name, ast_inet_ntoa(peer->addr.sin_addr), ntohs(peer->addr.sin_port), expiry);
08260 register_peer_exten(peer, 1);
08261 }
08262
08263
08264 useragent = get_header(req, "User-Agent");
08265 if (strcasecmp(useragent, peer->useragent)) {
08266 ast_copy_string(peer->useragent, useragent, sizeof(peer->useragent));
08267 if (option_verbose > 3)
08268 ast_verbose(VERBOSE_PREFIX_3 "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
08269 }
08270 return PARSE_REGISTER_UPDATE;
08271 }
08272
08273
08274 static void free_old_route(struct sip_route *route)
08275 {
08276 struct sip_route *next;
08277
08278 while (route) {
08279 next = route->next;
08280 free(route);
08281 route = next;
08282 }
08283 }
08284
08285
08286 static void list_route(struct sip_route *route)
08287 {
08288 if (!route)
08289 ast_verbose("list_route: no route\n");
08290 else {
08291 for (;route; route = route->next)
08292 ast_verbose("list_route: hop: <%s>\n", route->hop);
08293 }
08294 }
08295
08296
08297 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
08298 {
08299 struct sip_route *thishop, *head, *tail;
08300 int start = 0;
08301 int len;
08302 const char *rr, *contact, *c;
08303
08304
08305 if (p->route && p->route_persistant) {
08306 if (option_debug)
08307 ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
08308 return;
08309 }
08310
08311 if (p->route) {
08312 free_old_route(p->route);
08313 p->route = NULL;
08314 }
08315
08316
08317 p->route_persistant = 1;
08318
08319
08320
08321
08322
08323
08324 head = NULL;
08325 tail = head;
08326
08327 for (;;) {
08328
08329 rr = __get_header(req, "Record-Route", &start);
08330 if (*rr == '\0')
08331 break;
08332 for (; (rr = strchr(rr, '<')) ; rr += len) {
08333 ++rr;
08334 len = strcspn(rr, ">") + 1;
08335
08336 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
08337
08338 ast_copy_string(thishop->hop, rr, len);
08339 if (option_debug > 1)
08340 ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
08341
08342 if (backwards) {
08343
08344 thishop->next = head;
08345 head = thishop;
08346
08347 if (!tail)
08348 tail = thishop;
08349 } else {
08350 thishop->next = NULL;
08351
08352 if (tail)
08353 tail->next = thishop;
08354 else
08355 head = thishop;
08356 tail = thishop;
08357 }
08358 }
08359 }
08360 }
08361
08362
08363 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop,";lr") == NULL) ) {
08364
08365
08366 contact = get_header(req, "Contact");
08367 if (!ast_strlen_zero(contact)) {
08368 if (option_debug > 1)
08369 ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
08370
08371 c = strchr(contact, '<');
08372 if (c) {
08373
08374 ++c;
08375 len = strcspn(c, ">") + 1;
08376 } else {
08377
08378 c = contact;
08379 len = strlen(contact) + 1;
08380 }
08381 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
08382
08383 ast_copy_string(thishop->hop, c, len);
08384 thishop->next = NULL;
08385
08386 if (tail)
08387 tail->next = thishop;
08388 else
08389 head = thishop;
08390 }
08391 }
08392 }
08393
08394
08395 p->route = head;
08396
08397
08398 if (sip_debug_test_pvt(p))
08399 list_route(p->route);
08400 }
08401
08402 AST_THREADSTORAGE(check_auth_buf, check_auth_buf_init);
08403 #define CHECK_AUTH_BUF_INITLEN 256
08404
08405
08406
08407
08408
08409
08410 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
08411 const char *secret, const char *md5secret, int sipmethod,
08412 char *uri, enum xmittype reliable, int ignore)
08413 {
08414 const char *response = "407 Proxy Authentication Required";
08415 const char *reqheader = "Proxy-Authorization";
08416 const char *respheader = "Proxy-Authenticate";
08417 const char *authtoken;
08418 char a1_hash[256];
08419 char resp_hash[256]="";
08420 char *c;
08421 int wrongnonce = FALSE;
08422 int good_response;
08423 const char *usednonce = p->randdata;
08424 struct ast_dynamic_str *buf;
08425 int res;
08426
08427
08428 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
08429 struct x {
08430 const char *key;
08431 const char *s;
08432 } *i, keys[] = {
08433 [K_RESP] = { "response=", "" },
08434 [K_URI] = { "uri=", "" },
08435 [K_USER] = { "username=", "" },
08436 [K_NONCE] = { "nonce=", "" },
08437 [K_LAST] = { NULL, NULL}
08438 };
08439
08440
08441 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
08442 return AUTH_SUCCESSFUL;
08443 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
08444
08445
08446
08447 response = "401 Unauthorized";
08448 reqheader = "Authorization";
08449 respheader = "WWW-Authenticate";
08450 }
08451 authtoken = get_header(req, reqheader);
08452 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
08453
08454
08455 if (!reliable) {
08456
08457
08458 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08459
08460 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08461 }
08462 return AUTH_CHALLENGE_SENT;
08463 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
08464
08465 ast_string_field_build(p, randdata, "%08lx", ast_random());
08466 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08467
08468 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08469 return AUTH_CHALLENGE_SENT;
08470 }
08471
08472
08473
08474
08475
08476
08477 if (!(buf = ast_dynamic_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN)))
08478 return AUTH_SECRET_FAILED;
08479
08480
08481 res = ast_dynamic_str_thread_set(&buf, 0, &check_auth_buf, "%s", authtoken);
08482
08483 if (res == AST_DYNSTR_BUILD_FAILED)
08484 return AUTH_SECRET_FAILED;
08485
08486 c = buf->str;
08487
08488 while(c && *(c = ast_skip_blanks(c)) ) {
08489 for (i = keys; i->key != NULL; i++) {
08490 const char *separator = ",";
08491
08492 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
08493 continue;
08494
08495 c += strlen(i->key);
08496 if (*c == '"') {
08497 c++;
08498 separator = "\"";
08499 }
08500 i->s = c;
08501 strsep(&c, separator);
08502 break;
08503 }
08504 if (i->key == NULL)
08505 strsep(&c, " ,");
08506 }
08507
08508
08509 if (strcmp(username, keys[K_USER].s)) {
08510 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
08511 username, keys[K_USER].s);
08512
08513 return AUTH_USERNAME_MISMATCH;
08514 }
08515
08516
08517 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
08518 wrongnonce = TRUE;
08519 usednonce = keys[K_NONCE].s;
08520 }
08521
08522 if (!ast_strlen_zero(md5secret))
08523 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
08524 else {
08525 char a1[256];
08526 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
08527 ast_md5_hash(a1_hash, a1);
08528 }
08529
08530
08531 {
08532 char a2[256];
08533 char a2_hash[256];
08534 char resp[256];
08535
08536 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
08537 S_OR(keys[K_URI].s, uri));
08538 ast_md5_hash(a2_hash, a2);
08539 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
08540 ast_md5_hash(resp_hash, resp);
08541 }
08542
08543 good_response = keys[K_RESP].s &&
08544 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
08545 if (wrongnonce) {
08546 if (good_response) {
08547 if (sipdebug)
08548 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "To"));
08549
08550 ast_string_field_build(p, randdata, "%08lx", ast_random());
08551 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
08552 } else {
08553
08554 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
08555 if (sipdebug)
08556 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
08557 ast_string_field_build(p, randdata, "%08lx", ast_random());
08558 } else {
08559 if (sipdebug)
08560 ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", get_header(req, "To"));
08561 }
08562 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
08563 }
08564
08565
08566 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08567 return AUTH_CHALLENGE_SENT;
08568 }
08569 if (good_response) {
08570 append_history(p, "AuthOK", "Auth challenge succesful for %s", username);
08571 return AUTH_SUCCESSFUL;
08572 }
08573
08574
08575
08576
08577
08578
08579 return AUTH_SECRET_FAILED;
08580 }
08581
08582
08583 static void sip_peer_hold(struct sip_pvt *p, int hold)
08584 {
08585 struct sip_peer *peer = find_peer(p->peername, NULL, 1);
08586
08587 if (!peer)
08588 return;
08589
08590
08591 if (hold)
08592 peer->onHold++;
08593 else
08594 peer->onHold--;
08595
08596
08597 ast_device_state_changed("SIP/%s", peer->name);
08598
08599 return;
08600 }
08601
08602
08603
08604
08605 static int cb_extensionstate(char *context, char* exten, int state, void *data, char *cid_num, char *cid_name)
08606 {
08607 struct sip_pvt *p = data;
08608
08609 ast_mutex_lock(&p->lock);
08610
08611 switch(state) {
08612 case AST_EXTENSION_DEACTIVATED:
08613 case AST_EXTENSION_REMOVED:
08614 if (p->autokillid > -1 && sip_cancel_destroy(p))
08615 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
08616 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08617 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);
08618 p->stateid = -1;
08619 p->subscribed = NONE;
08620 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
08621 break;
08622 default:
08623 p->laststate = state;
08624 break;
08625 }
08626 if (p->subscribed != NONE) {
08627 if (!p->pendinginvite) {
08628 transmit_state_notify(p, state, 1, FALSE);
08629 } else {
08630
08631
08632 ast_set_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
08633 }
08634 }
08635 if (option_verbose > 1)
08636 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,
08637 ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE) ? "(queued)" : "");
08638
08639
08640 ast_mutex_unlock(&p->lock);
08641
08642 return 0;
08643 }
08644
08645
08646
08647
08648 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, int reliable)
08649 {
08650 ast_string_field_build(p, randdata, "%08lx", ast_random());
08651 transmit_response_with_auth(p, "401 Unauthorized", req, p->randdata, reliable, "WWW-Authenticate", 0);
08652 }
08653
08654
08655
08656
08657
08658
08659 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
08660 struct sip_request *req, char *uri)
08661 {
08662 enum check_auth_result res = AUTH_NOT_FOUND;
08663 struct sip_peer *peer;
08664 char tmp[256];
08665 char *name, *c;
08666 char *t;
08667 char *domain;
08668
08669
08670 t = uri;
08671 while(*t && (*t > 32) && (*t != ';'))
08672 t++;
08673 *t = '\0';
08674
08675 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
08676 if (pedanticsipchecking)
08677 ast_uri_decode(tmp);
08678
08679 c = get_in_brackets(tmp);
08680 c = strsep(&c, ";");
08681
08682 if (!strncasecmp(c, "sip:", 4)) {
08683 name = c + 4;
08684 } else {
08685 name = c;
08686 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(sin->sin_addr));
08687 }
08688
08689
08690 if ((c = strchr(name, '@'))) {
08691 *c++ = '\0';
08692 domain = c;
08693 if ((c = strchr(domain, ':')))
08694 *c = '\0';
08695 if (!AST_LIST_EMPTY(&domain_list)) {
08696 if (!check_sip_domain(domain, NULL, 0)) {
08697 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
08698 return AUTH_UNKNOWN_DOMAIN;
08699 }
08700 }
08701 }
08702
08703 ast_string_field_set(p, exten, name);
08704 build_contact(p);
08705 peer = find_peer(name, NULL, 1);
08706 if (!(peer && ast_apply_ha(peer->ha, sin))) {
08707
08708 if (peer) {
08709 ASTOBJ_UNREF(peer, sip_destroy_peer);
08710 res = AUTH_ACL_FAILED;
08711 } else
08712 res = AUTH_NOT_FOUND;
08713 }
08714 if (peer) {
08715
08716 if (p->rtp) {
08717 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
08718 p->autoframing = peer->autoframing;
08719 }
08720 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
08721 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
08722 res = AUTH_PEER_NOT_DYNAMIC;
08723 } else {
08724 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
08725 transmit_response(p, "100 Trying", req);
08726 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, ast_test_flag(req, SIP_PKT_IGNORE)))) {
08727 if (sip_cancel_destroy(p))
08728 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
08729
08730
08731
08732 switch (parse_register_contact(p, peer, req)) {
08733 case PARSE_REGISTER_FAILED:
08734 ast_log(LOG_WARNING, "Failed to parse contact info\n");
08735 transmit_response_with_date(p, "400 Bad Request", req);
08736 peer->lastmsgssent = -1;
08737 res = 0;
08738 break;
08739 case PARSE_REGISTER_QUERY:
08740 transmit_response_with_date(p, "200 OK", req);
08741 peer->lastmsgssent = -1;
08742 res = 0;
08743 break;
08744 case PARSE_REGISTER_UPDATE:
08745 update_peer(peer, p->expiry);
08746
08747 transmit_response_with_date(p, "200 OK", req);
08748 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
08749 peer->lastmsgssent = -1;
08750 res = 0;
08751 break;
08752 }
08753 }
08754 }
08755 }
08756 if (!peer && autocreatepeer) {
08757
08758 peer = temp_peer(name);
08759 if (peer) {
08760 ASTOBJ_CONTAINER_LINK(&peerl, peer);
08761 if (sip_cancel_destroy(p))
08762 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
08763 switch (parse_register_contact(p, peer, req)) {
08764 case PARSE_REGISTER_FAILED:
08765 ast_log(LOG_WARNING, "Failed to parse contact info\n");
08766 transmit_response_with_date(p, "400 Bad Request", req);
08767 peer->lastmsgssent = -1;
08768 res = 0;
08769 break;
08770 case PARSE_REGISTER_QUERY:
08771 transmit_response_with_date(p, "200 OK", req);
08772 peer->lastmsgssent = -1;
08773 res = 0;
08774 break;
08775 case PARSE_REGISTER_UPDATE:
08776
08777 transmit_response_with_date(p, "200 OK", req);
08778 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
08779 peer->lastmsgssent = -1;
08780 res = 0;
08781 break;
08782 }
08783 }
08784 }
08785 if (!res) {
08786 ast_device_state_changed("SIP/%s", peer->name);
08787 }
08788 if (res < 0) {
08789 switch (res) {
08790 case AUTH_SECRET_FAILED:
08791
08792 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
08793 break;
08794 case AUTH_USERNAME_MISMATCH:
08795
08796
08797
08798
08799 transmit_response(p, "403 Authentication user name does not match account name", &p->initreq);
08800 break;
08801 case AUTH_NOT_FOUND:
08802 case AUTH_PEER_NOT_DYNAMIC:
08803 case AUTH_ACL_FAILED:
08804 if (global_alwaysauthreject) {
08805 transmit_fake_auth_response(p, &p->initreq, 1);
08806 } else {
08807
08808 if (res == AUTH_PEER_NOT_DYNAMIC)
08809 transmit_response(p, "403 Forbidden", &p->initreq);
08810 else
08811 transmit_response(p, "404 Not found", &p->initreq);
08812 }
08813 break;
08814 default:
08815 break;
08816 }
08817 }
08818 if (peer)
08819 ASTOBJ_UNREF(peer, sip_destroy_peer);
08820
08821 return res;
08822 }
08823
08824
08825 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
08826 {
08827 char tmp[256], *c, *a;
08828 struct sip_request *req;
08829
08830 req = oreq;
08831 if (!req)
08832 req = &p->initreq;
08833 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
08834 if (ast_strlen_zero(tmp))
08835 return 0;
08836 c = get_in_brackets(tmp);
08837 if (strncasecmp(c, "sip:", 4)) {
08838 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", c);
08839 return -1;
08840 }
08841 c += 4;
08842 a = c;
08843 strsep(&a, "@;");
08844 if (sip_debug_test_pvt(p))
08845 ast_verbose("RDNIS is %s\n", c);
08846 ast_string_field_set(p, rdnis, c);
08847
08848 return 0;
08849 }
08850
08851
08852
08853
08854 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
08855 {
08856 char tmp[256] = "", *uri, *a;
08857 char tmpf[256] = "", *from;
08858 struct sip_request *req;
08859 char *colon;
08860
08861 req = oreq;
08862 if (!req)
08863 req = &p->initreq;
08864
08865
08866 if (req->rlPart2)
08867 ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
08868
08869 if (pedanticsipchecking)
08870 ast_uri_decode(tmp);
08871
08872 uri = get_in_brackets(tmp);
08873
08874 if (strncasecmp(uri, "sip:", 4)) {
08875 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
08876 return -1;
08877 }
08878 uri += 4;
08879
08880
08881 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
08882 if (!ast_strlen_zero(tmpf)) {
08883 if (pedanticsipchecking)
08884 ast_uri_decode(tmpf);
08885 from = get_in_brackets(tmpf);
08886 } else {
08887 from = NULL;
08888 }
08889
08890 if (!ast_strlen_zero(from)) {
08891 if (strncasecmp(from, "sip:", 4)) {
08892 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
08893 return -1;
08894 }
08895 from += 4;
08896 if ((a = strchr(from, '@')))
08897 *a++ = '\0';
08898 else
08899 a = from;
08900 from = strsep(&from, ";");
08901 a = strsep(&a, ";");
08902 ast_string_field_set(p, fromdomain, a);
08903 }
08904
08905
08906
08907
08908 if ((a = strchr(uri, '@'))) {
08909 *a++ = '\0';
08910 } else {
08911 a = uri;
08912 uri = "s";
08913 }
08914 colon = strchr(a, ':');
08915 if (colon)
08916 *colon = '\0';
08917
08918 uri = strsep(&uri, ";");
08919 a = strsep(&a, ";");
08920
08921 ast_string_field_set(p, domain, a);
08922
08923 if (!AST_LIST_EMPTY(&domain_list)) {
08924 char domain_context[AST_MAX_EXTENSION];
08925
08926 domain_context[0] = '\0';
08927 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
08928 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
08929 if (option_debug)
08930 ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
08931 return -2;
08932 }
08933 }
08934
08935 if (!ast_strlen_zero(domain_context))
08936 ast_string_field_set(p, context, domain_context);
08937 }
08938
08939
08940 if (req->method == SIP_SUBSCRIBE && !ast_strlen_zero(p->subscribecontext))
08941 ast_string_field_set(p, context, p->subscribecontext);
08942
08943 if (sip_debug_test_pvt(p))
08944 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
08945
08946
08947 if (req->method == SIP_SUBSCRIBE) {
08948 char hint[AST_MAX_EXTENSION];
08949 return (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten) ? 0 : -1);
08950 } else {
08951
08952
08953
08954
08955
08956 char *decoded_uri = ast_strdupa(uri);
08957 ast_uri_decode(decoded_uri);
08958 if (ast_exists_extension(NULL, p->context, uri, 1, S_OR(p->cid_num, from)) || ast_exists_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from)) ||
08959 !strcmp(uri, ast_pickup_ext())) {
08960 if (!oreq)
08961 ast_string_field_set(p, exten, uri);
08962 return 0;
08963 }
08964 }
08965
08966
08967 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
08968 ast_canmatch_extension(NULL, p->context, uri, 1, S_OR(p->cid_num, from))) ||
08969 !strncmp(uri, ast_pickup_ext(), strlen(uri))) {
08970 return 1;
08971 }
08972
08973 return -1;
08974 }
08975
08976
08977
08978
08979
08980
08981 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
08982 {
08983 struct sip_pvt *sip_pvt_ptr;
08984
08985 ast_mutex_lock(&iflock);
08986
08987 if (option_debug > 3 && totag)
08988 ast_log(LOG_DEBUG, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
08989
08990
08991 for (sip_pvt_ptr = iflist; sip_pvt_ptr; sip_pvt_ptr = sip_pvt_ptr->next) {
08992 if (!strcmp(sip_pvt_ptr->callid, callid)) {
08993 int match = 1;
08994 char *ourtag = sip_pvt_ptr->tag;
08995
08996
08997 ast_mutex_lock(&sip_pvt_ptr->lock);
08998
08999
09000
09001
09002
09003 if (pedanticsipchecking && (strcmp(fromtag, sip_pvt_ptr->theirtag) || (!ast_strlen_zero(totag) && strcmp(totag, ourtag))))
09004 match = 0;
09005
09006 if (!match) {
09007 ast_mutex_unlock(&sip_pvt_ptr->lock);
09008 continue;
09009 }
09010
09011 if (option_debug > 3 && totag)
09012 ast_log(LOG_DEBUG, "Matched %s call - their tag is %s Our tag is %s\n",
09013 ast_test_flag(&sip_pvt_ptr->flags[0], SIP_OUTGOING) ? "OUTGOING": "INCOMING",
09014 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
09015
09016
09017 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
09018 DEADLOCK_AVOIDANCE(&sip_pvt_ptr->lock);
09019 }
09020 break;
09021 }
09022 }
09023 ast_mutex_unlock(&iflock);
09024 if (option_debug > 3 && !sip_pvt_ptr)
09025 ast_log(LOG_DEBUG, "Found no match for callid %s to-tag %s from-tag %s\n", callid, totag, fromtag);
09026 return sip_pvt_ptr;
09027 }
09028
09029
09030
09031 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
09032 {
09033
09034 const char *p_referred_by = NULL;
09035 char *h_refer_to = NULL;
09036 char *h_referred_by = NULL;
09037 char *refer_to;
09038 const char *p_refer_to;
09039 char *referred_by_uri = NULL;
09040 char *ptr;
09041 struct sip_request *req = NULL;
09042 const char *transfer_context = NULL;
09043 struct sip_refer *referdata;
09044
09045
09046 req = outgoing_req;
09047 referdata = transferer->refer;
09048
09049 if (!req)
09050 req = &transferer->initreq;
09051
09052 p_refer_to = get_header(req, "Refer-To");
09053 if (ast_strlen_zero(p_refer_to)) {
09054 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
09055 return -2;
09056 }
09057 h_refer_to = ast_strdupa(p_refer_to);
09058 refer_to = get_in_brackets(h_refer_to);
09059 if (pedanticsipchecking)
09060 ast_uri_decode(refer_to);
09061
09062 if (strncasecmp(refer_to, "sip:", 4)) {
09063 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
09064 return -3;
09065 }
09066 refer_to += 4;
09067
09068
09069 p_referred_by = get_header(req, "Referred-By");
09070 if (!ast_strlen_zero(p_referred_by)) {
09071 char *lessthan;
09072 h_referred_by = ast_strdupa(p_referred_by);
09073 if (pedanticsipchecking)
09074 ast_uri_decode(h_referred_by);
09075
09076
09077 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
09078 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
09079 *(lessthan - 1) = '\0';
09080 }
09081
09082 referred_by_uri = get_in_brackets(h_referred_by);
09083 if(strncasecmp(referred_by_uri, "sip:", 4)) {
09084 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
09085 referred_by_uri = (char *) NULL;
09086 } else {
09087 referred_by_uri += 4;
09088 }
09089 }
09090
09091
09092 if ((ptr = strchr(refer_to, '?'))) {
09093 *ptr++ = '\0';
09094 if (!strncasecmp(ptr, "REPLACES=", 9)) {
09095 char *to = NULL, *from = NULL;
09096
09097
09098 referdata->attendedtransfer = 1;
09099 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
09100 ast_uri_decode(referdata->replaces_callid);
09101 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
09102 *ptr++ = '\0';
09103 }
09104
09105 if (ptr) {
09106
09107 to = strcasestr(ptr, "to-tag=");
09108 from = strcasestr(ptr, "from-tag=");
09109 }
09110
09111
09112 if (to) {
09113 ptr = to + 7;
09114 if ((to = strchr(ptr, '&')))
09115 *to = '\0';
09116 if ((to = strchr(ptr, ';')))
09117 *to = '\0';
09118 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
09119 }
09120
09121 if (from) {
09122 ptr = from + 9;
09123 if ((to = strchr(ptr, '&')))
09124 *to = '\0';
09125 if ((to = strchr(ptr, ';')))
09126 *to = '\0';
09127 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
09128 }
09129
09130 if (option_debug > 1) {
09131 if (!pedanticsipchecking)
09132 ast_log(LOG_DEBUG,"Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
09133 else
09134 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>" );
09135 }
09136 }
09137 }
09138
09139 if ((ptr = strchr(refer_to, '@'))) {
09140 char *urioption = NULL, *domain;
09141 *ptr++ = '\0';
09142
09143 if ((urioption = strchr(ptr, ';')))
09144 *urioption++ = '\0';
09145
09146 domain = ptr;
09147 if ((ptr = strchr(domain, ':')))
09148 *ptr = '\0';
09149
09150
09151 ast_copy_string(referdata->refer_to_domain, domain, sizeof(referdata->refer_to_domain));
09152 if (urioption)
09153 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
09154 }
09155
09156 if ((ptr = strchr(refer_to, ';')))
09157 *ptr = '\0';
09158 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
09159
09160 if (referred_by_uri) {
09161 if ((ptr = strchr(referred_by_uri, ';')))
09162 *ptr = '\0';
09163 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
09164 } else {
09165 referdata->referred_by[0] = '\0';
09166 }
09167
09168
09169 if (transferer->owner)
09170 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
09171
09172
09173 if (ast_strlen_zero(transfer_context)) {
09174 transfer_context = S_OR(transferer->owner->macrocontext,
09175 S_OR(transferer->context, default_context));
09176 }
09177
09178 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
09179
09180
09181 if (ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
09182 if (sip_debug_test_pvt(transferer)) {
09183 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
09184 }
09185
09186 return 0;
09187 }
09188 if (sip_debug_test_pvt(transferer))
09189 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
09190
09191
09192 return -1;
09193 }
09194
09195
09196
09197 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
09198 {
09199 char tmp[256] = "", *c, *a;
09200 struct sip_request *req = oreq ? oreq : &p->initreq;
09201 struct sip_refer *referdata = NULL;
09202 const char *transfer_context = NULL;
09203
09204 if (!p->refer && !sip_refer_allocate(p))
09205 return -1;
09206
09207 referdata = p->refer;
09208
09209 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
09210 c = get_in_brackets(tmp);
09211
09212 if (pedanticsipchecking)
09213 ast_uri_decode(c);
09214
09215 if (strncasecmp(c, "sip:", 4)) {
09216 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
09217 return -1;
09218 }
09219 c += 4;
09220 if ((a = strchr(c, ';')))
09221 *a = '\0';
09222
09223 if ((a = strchr(c, '@'))) {
09224 *a++ = '\0';
09225 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
09226 }
09227
09228 if (sip_debug_test_pvt(p))
09229 ast_verbose("Looking for %s in %s\n", c, p->context);
09230
09231 if (p->owner)
09232 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
09233
09234
09235 if (ast_strlen_zero(transfer_context)) {
09236 transfer_context = S_OR(p->owner->macrocontext,
09237 S_OR(p->context, default_context));
09238 }
09239 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
09240
09241 if (option_debug)
09242 ast_log(LOG_DEBUG,"SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
09243 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
09244 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
09245 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
09246 referdata->refer_call = NULL;
09247
09248 ast_string_field_set(p, context, transfer_context);
09249 return 0;
09250 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
09251 return 1;
09252 }
09253
09254 return -1;
09255 }
09256
09257 static void check_via(struct sip_pvt *p, const struct sip_request *req)
09258 {
09259 char via[512];
09260 char *c, *pt;
09261 struct hostent *hp;
09262 struct ast_hostent ahp;
09263
09264 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
09265
09266
09267 c = strchr(via, ',');
09268 if (c)
09269 *c = '\0';
09270
09271
09272 c = strstr(via, ";rport");
09273 if (c && (c[6] != '='))
09274 ast_set_flag(&p->flags[0], SIP_NAT_ROUTE);
09275
09276 c = strchr(via, ';');
09277 if (c)
09278 *c = '\0';
09279
09280 c = strchr(via, ' ');
09281 if (c) {
09282 *c = '\0';
09283 c = ast_skip_blanks(c+1);
09284 if (strcasecmp(via, "SIP/2.0/UDP")) {
09285 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
09286 return;
09287 }
09288 pt = strchr(c, ':');
09289 if (pt)
09290 *pt++ = '\0';
09291 hp = ast_gethostbyname(c, &ahp);
09292 if (!hp) {
09293 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
09294 return;
09295 }
09296 memset(&p->sa, 0, sizeof(p->sa));
09297 p->sa.sin_family = AF_INET;
09298 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
09299 p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
09300
09301 if (sip_debug_test_pvt(p)) {
09302 const struct sockaddr_in *dst = sip_real_dst(p);
09303 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
09304 }
09305 }
09306 }
09307
09308
09309 static char *get_calleridname(const char *input, char *output, size_t outputsize)
09310 {
09311 const char *end = strchr(input,'<');
09312 const char *tmp = strchr(input,'"');
09313 int bytes = 0;
09314 int maxbytes = outputsize - 1;
09315
09316 if (!end || end == input)
09317 return NULL;
09318
09319 end--;
09320
09321 if (tmp && tmp <= end) {
09322
09323
09324
09325 end = strchr(tmp+1, '"');
09326 if (!end)
09327 return NULL;
09328 bytes = (int) (end - tmp);
09329
09330 if (bytes > maxbytes)
09331 bytes = maxbytes;
09332 ast_copy_string(output, tmp + 1, bytes);
09333 } else {
09334
09335
09336 input = ast_skip_blanks(input);
09337
09338 while(*end && *end < 33 && end > input)
09339 end--;
09340 if (end >= input) {
09341 bytes = (int) (end - input) + 2;
09342
09343 if (bytes > maxbytes)
09344 bytes = maxbytes;
09345 ast_copy_string(output, input, bytes);
09346 } else
09347 return NULL;
09348 }
09349 return output;
09350 }
09351
09352
09353
09354
09355
09356 static int get_rpid_num(const char *input, char *output, int maxlen)
09357 {
09358 char *start;
09359 char *end;
09360
09361 start = strchr(input,':');
09362 if (!start) {
09363 output[0] = '\0';
09364 return 0;
09365 }
09366 start++;
09367
09368
09369 ast_copy_string(output,start,maxlen);
09370 output[maxlen-1] = '\0';
09371
09372 end = strchr(output,'@');
09373 if (end)
09374 *end = '\0';
09375 else
09376 output[0] = '\0';
09377 if (strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
09378 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
09379
09380 return 0;
09381 }
09382
09383
09384
09385
09386
09387
09388
09389 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
09390 int sipmethod, char *uri, enum xmittype reliable,
09391 struct sockaddr_in *sin, struct sip_peer **authpeer)
09392 {
09393 struct sip_user *user = NULL;
09394 struct sip_peer *peer;
09395 char from[256], *c;
09396 char *of;
09397 char rpid_num[50];
09398 const char *rpid;
09399 enum check_auth_result res = AUTH_SUCCESSFUL;
09400 char *t;
09401 char calleridname[50];
09402 int debug=sip_debug_test_addr(sin);
09403 struct ast_variable *tmpvar = NULL, *v = NULL;
09404 char *uri2 = ast_strdupa(uri);
09405
09406
09407 t = uri2;
09408 while (*t && *t > 32 && *t != ';')
09409 t++;
09410 *t = '\0';
09411 ast_copy_string(from, get_header(req, "From"), sizeof(from));
09412 if (pedanticsipchecking)
09413 ast_uri_decode(from);
09414
09415 memset(calleridname, 0, sizeof(calleridname));
09416 get_calleridname(from, calleridname, sizeof(calleridname));
09417 if (calleridname[0])
09418 ast_string_field_set(p, cid_name, calleridname);
09419
09420 rpid = get_header(req, "Remote-Party-ID");
09421 memset(rpid_num, 0, sizeof(rpid_num));
09422 if (!ast_strlen_zero(rpid))
09423 p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
09424
09425 of = get_in_brackets(from);
09426 if (ast_strlen_zero(p->exten)) {
09427 t = uri2;
09428 if (!strncasecmp(t, "sip:", 4))
09429 t+= 4;
09430 ast_string_field_set(p, exten, t);
09431 t = strchr(p->exten, '@');
09432 if (t)
09433 *t = '\0';
09434 if (ast_strlen_zero(p->our_contact))
09435 build_contact(p);
09436 }
09437
09438 ast_string_field_set(p, from, of);
09439 if (strncasecmp(of, "sip:", 4)) {
09440 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
09441 } else
09442 of += 4;
09443
09444 if ((c = strchr(of, '@'))) {
09445 char *tmp;
09446 *c = '\0';
09447 if ((c = strchr(of, ':')))
09448 *c = '\0';
09449 tmp = ast_strdupa(of);
09450
09451
09452
09453 tmp = strsep(&tmp, ";");
09454 if (ast_is_shrinkable_phonenumber(tmp))
09455 ast_shrink_phone_number(tmp);
09456 ast_string_field_set(p, cid_num, tmp);
09457 }
09458
09459 if (!authpeer)
09460 user = find_user(of, 1);
09461
09462
09463 if (user && ast_apply_ha(user->ha, sin)) {
09464 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
09465 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09466
09467 for (v = user->chanvars ; v ; v = v->next) {
09468 if ((tmpvar = ast_variable_new(v->name, v->value))) {
09469 tmpvar->next = p->chanvars;
09470 p->chanvars = tmpvar;
09471 }
09472 }
09473 p->prefs = user->prefs;
09474
09475 if (p->rtp) {
09476 ast_rtp_codec_setpref(p->rtp, &p->prefs);
09477 p->autoframing = user->autoframing;
09478 }
09479
09480 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09481 char *tmp;
09482 if (*calleridname)
09483 ast_string_field_set(p, cid_name, calleridname);
09484 tmp = ast_strdupa(rpid_num);
09485 if (ast_is_shrinkable_phonenumber(tmp))
09486 ast_shrink_phone_number(tmp);
09487 ast_string_field_set(p, cid_num, tmp);
09488 }
09489
09490 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
09491
09492 if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09493 if (sip_cancel_destroy(p))
09494 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
09495 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
09496 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09497
09498 if (p->sipoptions)
09499 user->sipoptions = p->sipoptions;
09500
09501
09502 if (user->call_limit)
09503 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
09504 if (!ast_strlen_zero(user->context))
09505 ast_string_field_set(p, context, user->context);
09506 if (!ast_strlen_zero(user->cid_num)) {
09507 char *tmp = ast_strdupa(user->cid_num);
09508 if (ast_is_shrinkable_phonenumber(tmp))
09509 ast_shrink_phone_number(tmp);
09510 ast_string_field_set(p, cid_num, tmp);
09511 }
09512 if (!ast_strlen_zero(user->cid_name))
09513 ast_string_field_set(p, cid_name, user->cid_name);
09514 ast_string_field_set(p, username, user->name);
09515 ast_string_field_set(p, peername, user->name);
09516 ast_string_field_set(p, peersecret, user->secret);
09517 ast_string_field_set(p, peermd5secret, user->md5secret);
09518 ast_string_field_set(p, subscribecontext, user->subscribecontext);
09519 ast_string_field_set(p, accountcode, user->accountcode);
09520 ast_string_field_set(p, language, user->language);
09521 ast_string_field_set(p, mohsuggest, user->mohsuggest);
09522 ast_string_field_set(p, mohinterpret, user->mohinterpret);
09523 p->allowtransfer = user->allowtransfer;
09524 p->amaflags = user->amaflags;
09525 p->callgroup = user->callgroup;
09526 p->pickupgroup = user->pickupgroup;
09527 if (user->callingpres)
09528 p->callingpres = user->callingpres;
09529
09530
09531 p->capability = user->capability;
09532 p->jointcapability = user->capability;
09533 if (p->peercapability)
09534 p->jointcapability &= p->peercapability;
09535 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
09536 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
09537 p->noncodeccapability |= AST_RTP_DTMF;
09538 else
09539 p->noncodeccapability &= ~AST_RTP_DTMF;
09540 p->jointnoncodeccapability = p->noncodeccapability;
09541 if (p->t38.peercapability)
09542 p->t38.jointcapability &= p->t38.peercapability;
09543 p->maxcallbitrate = user->maxcallbitrate;
09544
09545 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
09546 ast_rtp_destroy(p->vrtp);
09547 p->vrtp = NULL;
09548 }
09549 }
09550 if (user && debug)
09551 ast_verbose("Found user '%s'\n", user->name);
09552 } else {
09553 if (user) {
09554 if (!authpeer && debug)
09555 ast_verbose("Found user '%s', but fails host access\n", user->name);
09556 ASTOBJ_UNREF(user,sip_destroy_user);
09557 }
09558 user = NULL;
09559 }
09560
09561 if (!user) {
09562
09563 if (sipmethod == SIP_SUBSCRIBE)
09564
09565 peer = find_peer(of, NULL, 1);
09566 else
09567
09568
09569
09570
09571 peer = find_peer(NULL, &p->recv, 1);
09572
09573 if (peer) {
09574
09575 if (p->rtp) {
09576 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
09577 p->autoframing = peer->autoframing;
09578 }
09579 if (debug)
09580 ast_verbose("Found peer '%s'\n", peer->name);
09581
09582
09583 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
09584 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09585
09586
09587 if (p->sipoptions)
09588 peer->sipoptions = p->sipoptions;
09589
09590
09591 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09592 char *tmp = ast_strdupa(rpid_num);
09593 if (*calleridname)
09594 ast_string_field_set(p, cid_name, calleridname);
09595 if (ast_is_shrinkable_phonenumber(tmp))
09596 ast_shrink_phone_number(tmp);
09597 ast_string_field_set(p, cid_num, tmp);
09598 }
09599 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
09600
09601 ast_string_field_set(p, peersecret, peer->secret);
09602 ast_string_field_set(p, peermd5secret, peer->md5secret);
09603 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
09604 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
09605 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
09606 if (peer->callingpres)
09607 p->callingpres = peer->callingpres;
09608 if (peer->maxms && peer->lastms)
09609 p->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
09610 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
09611
09612 ast_string_field_free(p, peersecret);
09613 ast_string_field_free(p, peermd5secret);
09614 }
09615 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09616 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
09617 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09618
09619 if (peer->call_limit)
09620 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
09621 ast_string_field_set(p, peername, peer->name);
09622 ast_string_field_set(p, authname, peer->name);
09623
09624
09625 for (v = peer->chanvars ; v ; v = v->next) {
09626 if ((tmpvar = ast_variable_new(v->name, v->value))) {
09627 tmpvar->next = p->chanvars;
09628 p->chanvars = tmpvar;
09629 }
09630 }
09631 if (authpeer) {
09632 (*authpeer) = ASTOBJ_REF(peer);
09633 }
09634
09635 if (!ast_strlen_zero(peer->username)) {
09636 ast_string_field_set(p, username, peer->username);
09637
09638
09639 ast_string_field_set(p, authname, peer->username);
09640 }
09641 if (!ast_strlen_zero(peer->cid_num)) {
09642 char *tmp = ast_strdupa(peer->cid_num);
09643 if (ast_is_shrinkable_phonenumber(tmp))
09644 ast_shrink_phone_number(tmp);
09645 ast_string_field_set(p, cid_num, tmp);
09646 }
09647 if (!ast_strlen_zero(peer->cid_name))
09648 ast_string_field_set(p, cid_name, peer->cid_name);
09649 ast_string_field_set(p, fullcontact, peer->fullcontact);
09650 if (!ast_strlen_zero(peer->context))
09651 ast_string_field_set(p, context, peer->context);
09652 ast_string_field_set(p, peersecret, peer->secret);
09653 ast_string_field_set(p, peermd5secret, peer->md5secret);
09654 ast_string_field_set(p, language, peer->language);
09655 ast_string_field_set(p, accountcode, peer->accountcode);
09656 p->amaflags = peer->amaflags;
09657 p->callgroup = peer->callgroup;
09658 p->pickupgroup = peer->pickupgroup;
09659 p->capability = peer->capability;
09660 p->prefs = peer->prefs;
09661 p->jointcapability = peer->capability;
09662 if (p->peercapability)
09663 p->jointcapability &= p->peercapability;
09664 p->maxcallbitrate = peer->maxcallbitrate;
09665 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
09666 ast_rtp_destroy(p->vrtp);
09667 p->vrtp = NULL;
09668 }
09669 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
09670 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
09671 p->noncodeccapability |= AST_RTP_DTMF;
09672 else
09673 p->noncodeccapability &= ~AST_RTP_DTMF;
09674 p->jointnoncodeccapability = p->noncodeccapability;
09675 if (p->t38.peercapability)
09676 p->t38.jointcapability &= p->t38.peercapability;
09677 }
09678 ASTOBJ_UNREF(peer, sip_destroy_peer);
09679 } else {
09680 if (debug)
09681 ast_verbose("Found no matching peer or user for '%s:%d'\n", ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
09682
09683
09684 if (!global_allowguest) {
09685 if (global_alwaysauthreject)
09686 res = AUTH_FAKE_AUTH;
09687 else
09688 res = AUTH_SECRET_FAILED;
09689 } else if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09690 char *tmp = ast_strdupa(rpid_num);
09691 if (*calleridname)
09692 ast_string_field_set(p, cid_name, calleridname);
09693 if (ast_is_shrinkable_phonenumber(tmp))
09694 ast_shrink_phone_number(tmp);
09695 ast_string_field_set(p, cid_num, tmp);
09696 }
09697 }
09698
09699 }
09700
09701 if (user)
09702 ASTOBJ_UNREF(user, sip_destroy_user);
09703 return res;
09704 }
09705
09706
09707
09708
09709 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin)
09710 {
09711 return check_user_full(p, req, sipmethod, uri, reliable, sin, NULL);
09712 }
09713
09714
09715 static int get_msg_text(char *buf, int len, struct sip_request *req)
09716 {
09717 int x;
09718 int y;
09719
09720 buf[0] = '\0';
09721 y = len - strlen(buf) - 5;
09722 if (y < 0)
09723 y = 0;
09724 for (x=0;x<req->lines;x++) {
09725 strncat(buf, req->line[x], y);
09726 y -= strlen(req->line[x]) + 1;
09727 if (y < 0)
09728 y = 0;
09729 if (y != 0)
09730 strcat(buf, "\n");
09731 }
09732 return 0;
09733 }
09734
09735
09736
09737
09738
09739 static void receive_message(struct sip_pvt *p, struct sip_request *req)
09740 {
09741 char buf[1024];
09742 struct ast_frame f;
09743 const char *content_type = get_header(req, "Content-Type");
09744
09745 if (strncmp(content_type, "text/plain", strlen("text/plain"))) {
09746 transmit_response(p, "415 Unsupported Media Type", req);
09747 if (!p->owner)
09748 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09749 return;
09750 }
09751
09752 if (get_msg_text(buf, sizeof(buf), req)) {
09753 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
09754 transmit_response(p, "202 Accepted", req);
09755 if (!p->owner)
09756 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09757 return;
09758 }
09759
09760 if (p->owner) {
09761 if (sip_debug_test_pvt(p))
09762 ast_verbose("Message received: '%s'\n", buf);
09763 memset(&f, 0, sizeof(f));
09764 f.frametype = AST_FRAME_TEXT;
09765 f.subclass = 0;
09766 f.offset = 0;
09767 f.data = buf;
09768 f.datalen = strlen(buf);
09769 ast_queue_frame(p->owner, &f);
09770 transmit_response(p, "202 Accepted", req);
09771 } else {
09772 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);
09773 transmit_response(p, "405 Method Not Allowed", req);
09774 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09775 }
09776 return;
09777 }
09778
09779
09780 static int sip_show_inuse(int fd, int argc, char *argv[])
09781 {
09782 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
09783 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
09784 char ilimits[40];
09785 char iused[40];
09786 int showall = FALSE;
09787
09788 if (argc < 3)
09789 return RESULT_SHOWUSAGE;
09790
09791 if (argc == 4 && !strcmp(argv[3],"all"))
09792 showall = TRUE;
09793
09794 ast_cli(fd, FORMAT, "* User name", "In use", "Limit");
09795 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09796 ASTOBJ_RDLOCK(iterator);
09797 if (iterator->call_limit)
09798 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
09799 else
09800 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
09801 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
09802 if (showall || iterator->call_limit)
09803 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
09804 ASTOBJ_UNLOCK(iterator);
09805 } while (0) );
09806
09807 ast_cli(fd, FORMAT, "* Peer name", "In use", "Limit");
09808
09809 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
09810 ASTOBJ_RDLOCK(iterator);
09811 if (iterator->call_limit)
09812 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
09813 else
09814 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
09815 snprintf(iused, sizeof(iused), "%d/%d", iterator->inUse, iterator->inRinging);
09816 if (showall || iterator->call_limit)
09817 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
09818 ASTOBJ_UNLOCK(iterator);
09819 } while (0) );
09820
09821 return RESULT_SUCCESS;
09822 #undef FORMAT
09823 #undef FORMAT2
09824 }
09825
09826
09827 static char *transfermode2str(enum transfermodes mode)
09828 {
09829 if (mode == TRANSFER_OPENFORALL)
09830 return "open";
09831 else if (mode == TRANSFER_CLOSED)
09832 return "closed";
09833 return "strict";
09834 }
09835
09836
09837 static char *nat2str(int nat)
09838 {
09839 switch(nat) {
09840 case SIP_NAT_NEVER:
09841 return "No";
09842 case SIP_NAT_ROUTE:
09843 return "Route";
09844 case SIP_NAT_ALWAYS:
09845 return "Always";
09846 case SIP_NAT_RFC3581:
09847 return "RFC3581";
09848 default:
09849 return "Unknown";
09850 }
09851 }
09852
09853
09854
09855
09856 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
09857 {
09858 int res = 0;
09859 if (peer->maxms) {
09860 if (peer->lastms < 0) {
09861 ast_copy_string(status, "UNREACHABLE", statuslen);
09862 } else if (peer->lastms > peer->maxms) {
09863 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
09864 res = 1;
09865 } else if (peer->lastms) {
09866 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
09867 res = 1;
09868 } else {
09869 ast_copy_string(status, "UNKNOWN", statuslen);
09870 }
09871 } else {
09872 ast_copy_string(status, "Unmonitored", statuslen);
09873
09874 res = -1;
09875 }
09876 return res;
09877 }
09878
09879
09880 static int sip_show_users(int fd, int argc, char *argv[])
09881 {
09882 regex_t regexbuf;
09883 int havepattern = FALSE;
09884
09885 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
09886
09887 switch (argc) {
09888 case 5:
09889 if (!strcasecmp(argv[3], "like")) {
09890 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
09891 return RESULT_SHOWUSAGE;
09892 havepattern = TRUE;
09893 } else
09894 return RESULT_SHOWUSAGE;
09895 case 3:
09896 break;
09897 default:
09898 return RESULT_SHOWUSAGE;
09899 }
09900
09901 ast_cli(fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
09902 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09903 ASTOBJ_RDLOCK(iterator);
09904
09905 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09906 ASTOBJ_UNLOCK(iterator);
09907 continue;
09908 }
09909
09910 ast_cli(fd, FORMAT, iterator->name,
09911 iterator->secret,
09912 iterator->accountcode,
09913 iterator->context,
09914 iterator->ha ? "Yes" : "No",
09915 nat2str(ast_test_flag(&iterator->flags[0], SIP_NAT)));
09916 ASTOBJ_UNLOCK(iterator);
09917 } while (0)
09918 );
09919
09920 if (havepattern)
09921 regfree(®exbuf);
09922
09923 return RESULT_SUCCESS;
09924 #undef FORMAT
09925 }
09926
09927 static char mandescr_show_peers[] =
09928 "Description: Lists SIP peers in text format with details on current status.\n"
09929 "Variables: \n"
09930 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
09931
09932
09933
09934 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
09935 {
09936 const char *id = astman_get_header(m,"ActionID");
09937 const char *a[] = {"sip", "show", "peers"};
09938 char idtext[256] = "";
09939 int total = 0;
09940
09941 if (!ast_strlen_zero(id))
09942 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
09943
09944 astman_send_ack(s, m, "Peer status list will follow");
09945
09946 _sip_show_peers(-1, &total, s, m, 3, a);
09947
09948 astman_append(s,
09949 "Event: PeerlistComplete\r\n"
09950 "ListItems: %d\r\n"
09951 "%s"
09952 "\r\n", total, idtext);
09953 return 0;
09954 }
09955
09956
09957 static int sip_show_peers(int fd, int argc, char *argv[])
09958 {
09959 return _sip_show_peers(fd, NULL, NULL, NULL, argc, (const char **) argv);
09960 }
09961
09962
09963 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
09964 {
09965 regex_t regexbuf;
09966 int havepattern = FALSE;
09967
09968 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s %-10s\n"
09969 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s %-10s\n"
09970
09971 char name[256];
09972 int total_peers = 0;
09973 int peers_mon_online = 0;
09974 int peers_mon_offline = 0;
09975 int peers_unmon_offline = 0;
09976 int peers_unmon_online = 0;
09977 const char *id;
09978 char idtext[256] = "";
09979 int realtimepeers;
09980
09981 realtimepeers = ast_check_realtime("sippeers");
09982
09983 if (s) {
09984 id = astman_get_header(m,"ActionID");
09985 if (!ast_strlen_zero(id))
09986 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
09987 }
09988
09989 switch (argc) {
09990 case 5:
09991 if (!strcasecmp(argv[3], "like")) {
09992 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
09993 return RESULT_SHOWUSAGE;
09994 havepattern = TRUE;
09995 } else
09996 return RESULT_SHOWUSAGE;
09997 case 3:
09998 break;
09999 default:
10000 return RESULT_SHOWUSAGE;
10001 }
10002
10003 if (!s)
10004 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
10005
10006 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
10007 char status[20] = "";
10008 char srch[2000];
10009 char pstatus;
10010
10011 ASTOBJ_RDLOCK(iterator);
10012
10013 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
10014 ASTOBJ_UNLOCK(iterator);
10015 continue;
10016 }
10017
10018 if (!ast_strlen_zero(iterator->username) && !s)
10019 snprintf(name, sizeof(name), "%s/%s", iterator->name, iterator->username);
10020 else
10021 ast_copy_string(name, iterator->name, sizeof(name));
10022
10023 pstatus = peer_status(iterator, status, sizeof(status));
10024 if (pstatus == 1)
10025 peers_mon_online++;
10026 else if (pstatus == 0)
10027 peers_mon_offline++;
10028 else {
10029 if (iterator->addr.sin_port == 0)
10030 peers_unmon_offline++;
10031 else
10032 peers_unmon_online++;
10033 }
10034
10035 snprintf(srch, sizeof(srch), FORMAT, name,
10036 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
10037 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
10038 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
10039 iterator->ha ? " A " : " ",
10040 ntohs(iterator->addr.sin_port), status,
10041 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
10042
10043 if (!s) {
10044 ast_cli(fd, FORMAT, name,
10045 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
10046 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
10047 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
10048 iterator->ha ? " A " : " ",
10049
10050 ntohs(iterator->addr.sin_port), status,
10051 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
10052 } else {
10053
10054 astman_append(s,
10055 "Event: PeerEntry\r\n%s"
10056 "Channeltype: SIP\r\n"
10057 "ObjectName: %s\r\n"
10058 "ChanObjectType: peer\r\n"
10059 "IPaddress: %s\r\n"
10060 "IPport: %d\r\n"
10061 "Dynamic: %s\r\n"
10062 "Natsupport: %s\r\n"
10063 "VideoSupport: %s\r\n"
10064 "ACL: %s\r\n"
10065 "Status: %s\r\n"
10066 "RealtimeDevice: %s\r\n\r\n",
10067 idtext,
10068 iterator->name,
10069 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "-none-",
10070 ntohs(iterator->addr.sin_port),
10071 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no",
10072 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? "yes" : "no",
10073 ast_test_flag(&iterator->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
10074 iterator->ha ? "yes" : "no",
10075 status,
10076 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "yes":"no") : "no");
10077 }
10078
10079 ASTOBJ_UNLOCK(iterator);
10080
10081 total_peers++;
10082 } while(0) );
10083
10084 if (!s)
10085 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
10086 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
10087
10088 if (havepattern)
10089 regfree(®exbuf);
10090
10091 if (total)
10092 *total = total_peers;
10093
10094
10095 return RESULT_SUCCESS;
10096 #undef FORMAT
10097 #undef FORMAT2
10098 }
10099
10100
10101 static int sip_show_objects(int fd, int argc, char *argv[])
10102 {
10103 char tmp[256];
10104 if (argc != 3)
10105 return RESULT_SHOWUSAGE;
10106 ast_cli(fd, "-= User objects: %d static, %d realtime =-\n\n", suserobjs, ruserobjs);
10107 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &userl);
10108 ast_cli(fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
10109 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &peerl);
10110 ast_cli(fd, "-= Registry objects: %d =-\n\n", regobjs);
10111 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), ®l);
10112 return RESULT_SUCCESS;
10113 }
10114
10115 static void print_group(int fd, ast_group_t group, int crlf)
10116 {
10117 char buf[256];
10118 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
10119 }
10120
10121
10122 static const char *dtmfmode2str(int mode)
10123 {
10124 switch (mode) {
10125 case SIP_DTMF_RFC2833:
10126 return "rfc2833";
10127 case SIP_DTMF_INFO:
10128 return "info";
10129 case SIP_DTMF_INBAND:
10130 return "inband";
10131 case SIP_DTMF_AUTO:
10132 return "auto";
10133 }
10134 return "<error>";
10135 }
10136
10137
10138 static const char *insecure2str(int port, int invite)
10139 {
10140 if (port && invite)
10141 return "port,invite";
10142 else if (port)
10143 return "port";
10144 else if (invite)
10145 return "invite";
10146 else
10147 return "no";
10148 }
10149
10150
10151
10152
10153 static void cleanup_stale_contexts(char *new, char *old)
10154 {
10155 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
10156
10157 while ((oldcontext = strsep(&old, "&"))) {
10158 stalecontext = '\0';
10159 ast_copy_string(newlist, new, sizeof(newlist));
10160 stringp = newlist;
10161 while ((newcontext = strsep(&stringp, "&"))) {
10162 if (strcmp(newcontext, oldcontext) == 0) {
10163
10164 stalecontext = '\0';
10165 break;
10166 } else if (strcmp(newcontext, oldcontext)) {
10167 stalecontext = oldcontext;
10168 }
10169
10170 }
10171 if (stalecontext)
10172 ast_context_destroy(ast_context_find(stalecontext), "SIP");
10173 }
10174 }
10175
10176
10177 static int sip_prune_realtime(int fd, int argc, char *argv[])
10178 {
10179 struct sip_peer *peer;
10180 struct sip_user *user;
10181 int pruneuser = FALSE;
10182 int prunepeer = FALSE;
10183 int multi = FALSE;
10184 char *name = NULL;
10185 regex_t regexbuf;
10186
10187 switch (argc) {
10188 case 4:
10189 if (!strcasecmp(argv[3], "user"))
10190 return RESULT_SHOWUSAGE;
10191 if (!strcasecmp(argv[3], "peer"))
10192 return RESULT_SHOWUSAGE;
10193 if (!strcasecmp(argv[3], "like"))
10194 return RESULT_SHOWUSAGE;
10195 if (!strcasecmp(argv[3], "all")) {
10196 multi = TRUE;
10197 pruneuser = prunepeer = TRUE;
10198 } else {
10199 pruneuser = prunepeer = TRUE;
10200 name = argv[3];
10201 }
10202 break;
10203 case 5:
10204 if (!strcasecmp(argv[4], "like"))
10205 return RESULT_SHOWUSAGE;
10206 if (!strcasecmp(argv[3], "all"))
10207 return RESULT_SHOWUSAGE;
10208 if (!strcasecmp(argv[3], "like")) {
10209 multi = TRUE;
10210 name = argv[4];
10211 pruneuser = prunepeer = TRUE;
10212 } else if (!strcasecmp(argv[3], "user")) {
10213 pruneuser = TRUE;
10214 if (!strcasecmp(argv[4], "all"))
10215 multi = TRUE;
10216 else
10217 name = argv[4];
10218 } else if (!strcasecmp(argv[3], "peer")) {
10219 prunepeer = TRUE;
10220 if (!strcasecmp(argv[4], "all"))
10221 multi = TRUE;
10222 else
10223 name = argv[4];
10224 } else
10225 return RESULT_SHOWUSAGE;
10226 break;
10227 case 6:
10228 if (strcasecmp(argv[4], "like"))
10229 return RESULT_SHOWUSAGE;
10230 if (!strcasecmp(argv[3], "user")) {
10231 pruneuser = TRUE;
10232 name = argv[5];
10233 } else if (!strcasecmp(argv[3], "peer")) {
10234 prunepeer = TRUE;
10235 name = argv[5];
10236 } else
10237 return RESULT_SHOWUSAGE;
10238 break;
10239 default:
10240 return RESULT_SHOWUSAGE;
10241 }
10242
10243 if (multi && name) {
10244 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
10245 return RESULT_SHOWUSAGE;
10246 }
10247
10248 if (multi) {
10249 if (prunepeer) {
10250 int pruned = 0;
10251
10252 ASTOBJ_CONTAINER_WRLOCK(&peerl);
10253 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
10254 ASTOBJ_RDLOCK(iterator);
10255 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
10256 ASTOBJ_UNLOCK(iterator);
10257 continue;
10258 };
10259 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
10260 ASTOBJ_MARK(iterator);
10261 pruned++;
10262 }
10263 ASTOBJ_UNLOCK(iterator);
10264 } while (0) );
10265 if (pruned) {
10266 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
10267 ast_cli(fd, "%d peers pruned.\n", pruned);
10268 } else
10269 ast_cli(fd, "No peers found to prune.\n");
10270 ASTOBJ_CONTAINER_UNLOCK(&peerl);
10271 }
10272 if (pruneuser) {
10273 int pruned = 0;
10274
10275 ASTOBJ_CONTAINER_WRLOCK(&userl);
10276 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
10277 ASTOBJ_RDLOCK(iterator);
10278 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
10279 ASTOBJ_UNLOCK(iterator);
10280 continue;
10281 };
10282 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
10283 ASTOBJ_MARK(iterator);
10284 pruned++;
10285 }
10286 ASTOBJ_UNLOCK(iterator);
10287 } while (0) );
10288 if (pruned) {
10289 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
10290 ast_cli(fd, "%d users pruned.\n", pruned);
10291 } else
10292 ast_cli(fd, "No users found to prune.\n");
10293 ASTOBJ_CONTAINER_UNLOCK(&userl);
10294 }
10295 } else {
10296 if (prunepeer) {
10297 if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
10298 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
10299 ast_cli(fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
10300 ASTOBJ_CONTAINER_LINK(&peerl, peer);
10301 } else
10302 ast_cli(fd, "Peer '%s' pruned.\n", name);
10303 ASTOBJ_UNREF(peer, sip_destroy_peer);
10304 } else
10305 ast_cli(fd, "Peer '%s' not found.\n", name);
10306 }
10307 if (pruneuser) {
10308 if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
10309 if (!ast_test_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
10310 ast_cli(fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
10311 ASTOBJ_CONTAINER_LINK(&userl, user);
10312 } else
10313 ast_cli(fd, "User '%s' pruned.\n", name);
10314 ASTOBJ_UNREF(user, sip_destroy_user);
10315 } else
10316 ast_cli(fd, "User '%s' not found.\n", name);
10317 }
10318 }
10319
10320 return RESULT_SUCCESS;
10321 }
10322
10323
10324 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
10325 {
10326 int x, codec;
10327
10328 for(x = 0; x < 32 ; x++) {
10329 codec = ast_codec_pref_index(pref, x);
10330 if (!codec)
10331 break;
10332 ast_cli(fd, "%s", ast_getformatname(codec));
10333 ast_cli(fd, ":%d", pref->framing[x]);
10334 if (x < 31 && ast_codec_pref_index(pref, x + 1))
10335 ast_cli(fd, ",");
10336 }
10337 if (!x)
10338 ast_cli(fd, "none");
10339 }
10340
10341
10342 static const char *domain_mode_to_text(const enum domain_mode mode)
10343 {
10344 switch (mode) {
10345 case SIP_DOMAIN_AUTO:
10346 return "[Automatic]";
10347 case SIP_DOMAIN_CONFIG:
10348 return "[Configured]";
10349 }
10350
10351 return "";
10352 }
10353
10354
10355 static int sip_show_domains(int fd, int argc, char *argv[])
10356 {
10357 struct domain *d;
10358 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
10359
10360 if (AST_LIST_EMPTY(&domain_list)) {
10361 ast_cli(fd, "SIP Domain support not enabled.\n\n");
10362 return RESULT_SUCCESS;
10363 } else {
10364 ast_cli(fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
10365 AST_LIST_LOCK(&domain_list);
10366 AST_LIST_TRAVERSE(&domain_list, d, list)
10367 ast_cli(fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
10368 domain_mode_to_text(d->mode));
10369 AST_LIST_UNLOCK(&domain_list);
10370 ast_cli(fd, "\n");
10371 return RESULT_SUCCESS;
10372 }
10373 }
10374 #undef FORMAT
10375
10376 static char mandescr_show_peer[] =
10377 "Description: Show one SIP peer with details on current status.\n"
10378 "Variables: \n"
10379 " Peer: <name> The peer name you want to check.\n"
10380 " ActionID: <id> Optional action ID for this AMI transaction.\n";
10381
10382
10383 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
10384 {
10385 const char *a[4];
10386 const char *peer;
10387 int ret;
10388
10389 peer = astman_get_header(m,"Peer");
10390 if (ast_strlen_zero(peer)) {
10391 astman_send_error(s, m, "Peer: <name> missing.\n");
10392 return 0;
10393 }
10394 a[0] = "sip";
10395 a[1] = "show";
10396 a[2] = "peer";
10397 a[3] = peer;
10398
10399 ret = _sip_show_peer(1, -1, s, m, 4, a);
10400 astman_append(s, "\r\n\r\n" );
10401 return ret;
10402 }
10403
10404
10405
10406
10407 static int sip_show_peer(int fd, int argc, char *argv[])
10408 {
10409 return _sip_show_peer(0, fd, NULL, NULL, argc, (const char **) argv);
10410 }
10411
10412
10413 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
10414 {
10415 char status[30] = "";
10416 char cbuf[256];
10417 struct sip_peer *peer;
10418 char codec_buf[512];
10419 struct ast_codec_pref *pref;
10420 struct ast_variable *v;
10421 struct sip_auth *auth;
10422 int x = 0, codec = 0, load_realtime;
10423 int realtimepeers;
10424
10425 realtimepeers = ast_check_realtime("sippeers");
10426
10427 if (argc < 4)
10428 return RESULT_SHOWUSAGE;
10429
10430 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
10431 peer = find_peer(argv[3], NULL, load_realtime);
10432 if (s) {
10433 if (peer) {
10434 const char *id = astman_get_header(m,"ActionID");
10435
10436 astman_append(s, "Response: Success\r\n");
10437 if (!ast_strlen_zero(id))
10438 astman_append(s, "ActionID: %s\r\n",id);
10439 } else {
10440 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.\n", argv[3]);
10441 astman_send_error(s, m, cbuf);
10442 return 0;
10443 }
10444 }
10445 if (peer && type==0 ) {
10446 ast_cli(fd,"\n\n");
10447 ast_cli(fd, " * Name : %s\n", peer->name);
10448 if (realtimepeers) {
10449 ast_cli(fd, " Realtime peer: %s\n", ast_test_flag(&peer->flags[0], SIP_REALTIME) ? "Yes, cached" : "No");
10450 }
10451 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
10452 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
10453 for (auth = peer->auth; auth; auth = auth->next) {
10454 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
10455 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
10456 }
10457 ast_cli(fd, " Context : %s\n", peer->context);
10458 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
10459 ast_cli(fd, " Language : %s\n", peer->language);
10460 if (!ast_strlen_zero(peer->accountcode))
10461 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
10462 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
10463 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
10464 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
10465 if (!ast_strlen_zero(peer->fromuser))
10466 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
10467 if (!ast_strlen_zero(peer->fromdomain))
10468 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
10469 ast_cli(fd, " Callgroup : ");
10470 print_group(fd, peer->callgroup, 0);
10471 ast_cli(fd, " Pickupgroup : ");
10472 print_group(fd, peer->pickupgroup, 0);
10473 ast_cli(fd, " Mailbox : %s\n", peer->mailbox);
10474 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
10475 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
10476 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
10477 ast_cli(fd, " Dynamic : %s\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Yes":"No"));
10478 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
10479 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
10480 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
10481 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)));
10482 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
10483 ast_cli(fd, " ACL : %s\n", (peer->ha?"Yes":"No"));
10484 ast_cli(fd, " T38 pt UDPTL : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)?"Yes":"No");
10485 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
10486 ast_cli(fd, " T38 pt RTP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_RTP)?"Yes":"No");
10487 ast_cli(fd, " T38 pt TCP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_TCP)?"Yes":"No");
10488 #endif
10489 ast_cli(fd, " CanReinvite : %s\n", ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Yes":"No");
10490 ast_cli(fd, " PromiscRedir : %s\n", ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Yes":"No");
10491 ast_cli(fd, " User=Phone : %s\n", ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Yes":"No");
10492 ast_cli(fd, " Video Support: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Yes":"No");
10493 ast_cli(fd, " Trust RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_TRUSTRPID) ? "Yes" : "No");
10494 ast_cli(fd, " Send RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_SENDRPID) ? "Yes" : "No");
10495 ast_cli(fd, " Subscriptions: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
10496 ast_cli(fd, " Overlap dial : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
10497
10498
10499 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
10500 ast_cli(fd, " LastMsg : %d\n", peer->lastmsg);
10501 ast_cli(fd, " ToHost : %s\n", peer->tohost);
10502 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));
10503 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
10504 if (!ast_strlen_zero(global_regcontext))
10505 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
10506 ast_cli(fd, " Def. Username: %s\n", peer->username);
10507 ast_cli(fd, " SIP Options : ");
10508 if (peer->sipoptions) {
10509 int lastoption = -1;
10510 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
10511 if (sip_options[x].id != lastoption) {
10512 if (peer->sipoptions & sip_options[x].id)
10513 ast_cli(fd, "%s ", sip_options[x].text);
10514 lastoption = x;
10515 }
10516 }
10517 } else
10518 ast_cli(fd, "(none)");
10519
10520 ast_cli(fd, "\n");
10521 ast_cli(fd, " Codecs : ");
10522 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
10523 ast_cli(fd, "%s\n", codec_buf);
10524 ast_cli(fd, " Codec Order : (");
10525 print_codec_to_cli(fd, &peer->prefs);
10526 ast_cli(fd, ")\n");
10527
10528 ast_cli(fd, " Auto-Framing: %s \n", peer->autoframing ? "Yes" : "No");
10529 ast_cli(fd, " Status : ");
10530 peer_status(peer, status, sizeof(status));
10531 ast_cli(fd, "%s\n",status);
10532 ast_cli(fd, " Useragent : %s\n", peer->useragent);
10533 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
10534 if (peer->chanvars) {
10535 ast_cli(fd, " Variables :\n");
10536 for (v = peer->chanvars ; v ; v = v->next)
10537 ast_cli(fd, " %s = %s\n", v->name, v->value);
10538 }
10539 ast_cli(fd,"\n");
10540 ASTOBJ_UNREF(peer,sip_destroy_peer);
10541 } else if (peer && type == 1) {
10542 char buf[256];
10543 astman_append(s, "Channeltype: SIP\r\n");
10544 astman_append(s, "ObjectName: %s\r\n", peer->name);
10545 astman_append(s, "ChanObjectType: peer\r\n");
10546 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
10547 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
10548 astman_append(s, "Context: %s\r\n", peer->context);
10549 astman_append(s, "Language: %s\r\n", peer->language);
10550 if (!ast_strlen_zero(peer->accountcode))
10551 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
10552 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
10553 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
10554 if (!ast_strlen_zero(peer->fromuser))
10555 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
10556 if (!ast_strlen_zero(peer->fromdomain))
10557 astman_append(s, "SIP-FromDomain: %s\r\n", peer->fromdomain);
10558 astman_append(s, "Callgroup: ");
10559 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->callgroup));
10560 astman_append(s, "Pickupgroup: ");
10561 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->pickupgroup));
10562 astman_append(s, "VoiceMailbox: %s\r\n", peer->mailbox);
10563 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
10564 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
10565 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
10566 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
10567 astman_append(s, "Dynamic: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Y":"N"));
10568 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
10569 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched,peer->expire));
10570 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)));
10571 astman_append(s, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
10572 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
10573 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Y":"N"));
10574 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
10575 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
10576 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
10577
10578
10579 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
10580 astman_append(s, "SIPLastMsg: %d\r\n", peer->lastmsg);
10581 astman_append(s, "ToHost: %s\r\n", peer->tohost);
10582 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));
10583 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));
10584 astman_append(s, "Default-Username: %s\r\n", peer->username);
10585 if (!ast_strlen_zero(global_regcontext))
10586 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
10587 astman_append(s, "Codecs: ");
10588 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
10589 astman_append(s, "%s\r\n", codec_buf);
10590 astman_append(s, "CodecOrder: ");
10591 pref = &peer->prefs;
10592 for(x = 0; x < 32 ; x++) {
10593 codec = ast_codec_pref_index(pref,x);
10594 if (!codec)
10595 break;
10596 astman_append(s, "%s", ast_getformatname(codec));
10597 if (x < 31 && ast_codec_pref_index(pref,x+1))
10598 astman_append(s, ",");
10599 }
10600
10601 astman_append(s, "\r\n");
10602 astman_append(s, "Status: ");
10603 peer_status(peer, status, sizeof(status));
10604 astman_append(s, "%s\r\n", status);
10605 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
10606 astman_append(s, "Reg-Contact : %s\r\n", peer->fullcontact);
10607 if (peer->chanvars) {
10608 for (v = peer->chanvars ; v ; v = v->next) {
10609 astman_append(s, "ChanVariable:\n");
10610 astman_append(s, " %s,%s\r\n", v->name, v->value);
10611 }
10612 }
10613
10614 ASTOBJ_UNREF(peer,sip_destroy_peer);
10615
10616 } else {
10617 ast_cli(fd,"Peer %s not found.\n", argv[3]);
10618 ast_cli(fd,"\n");
10619 }
10620
10621 return RESULT_SUCCESS;
10622 }
10623
10624
10625 static int sip_show_user(int fd, int argc, char *argv[])
10626 {
10627 char cbuf[256];
10628 struct sip_user *user;
10629 struct ast_variable *v;
10630 int load_realtime;
10631
10632 if (argc < 4)
10633 return RESULT_SHOWUSAGE;
10634
10635
10636 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
10637
10638 user = find_user(argv[3], load_realtime);
10639 if (user) {
10640 ast_cli(fd,"\n\n");
10641 ast_cli(fd, " * Name : %s\n", user->name);
10642 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
10643 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
10644 ast_cli(fd, " Context : %s\n", user->context);
10645 ast_cli(fd, " Language : %s\n", user->language);
10646 if (!ast_strlen_zero(user->accountcode))
10647 ast_cli(fd, " Accountcode : %s\n", user->accountcode);
10648 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
10649 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
10650 ast_cli(fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
10651 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
10652 ast_cli(fd, " Call limit : %d\n", user->call_limit);
10653 ast_cli(fd, " Callgroup : ");
10654 print_group(fd, user->callgroup, 0);
10655 ast_cli(fd, " Pickupgroup : ");
10656 print_group(fd, user->pickupgroup, 0);
10657 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
10658 ast_cli(fd, " ACL : %s\n", (user->ha?"Yes":"No"));
10659 ast_cli(fd, " Codec Order : (");
10660 print_codec_to_cli(fd, &user->prefs);
10661 ast_cli(fd, ")\n");
10662
10663 ast_cli(fd, " Auto-Framing: %s \n", user->autoframing ? "Yes" : "No");
10664 if (user->chanvars) {
10665 ast_cli(fd, " Variables :\n");
10666 for (v = user->chanvars ; v ; v = v->next)
10667 ast_cli(fd, " %s = %s\n", v->name, v->value);
10668 }
10669 ast_cli(fd,"\n");
10670 ASTOBJ_UNREF(user,sip_destroy_user);
10671 } else {
10672 ast_cli(fd,"User %s not found.\n", argv[3]);
10673 ast_cli(fd,"\n");
10674 }
10675
10676 return RESULT_SUCCESS;
10677 }
10678
10679
10680 static int sip_show_registry(int fd, int argc, char *argv[])
10681 {
10682 #define FORMAT2 "%-30.30s %-12.12s %8.8s %-20.20s %-25.25s\n"
10683 #define FORMAT "%-30.30s %-12.12s %8d %-20.20s %-25.25s\n"
10684 char host[80];
10685 char tmpdat[256];
10686 struct tm tm;
10687
10688
10689 if (argc != 3)
10690 return RESULT_SHOWUSAGE;
10691 ast_cli(fd, FORMAT2, "Host", "Username", "Refresh", "State", "Reg.Time");
10692 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
10693 ASTOBJ_RDLOCK(iterator);
10694 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
10695 if (iterator->regtime) {
10696 ast_localtime(&iterator->regtime, &tm, NULL);
10697 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
10698 } else {
10699 tmpdat[0] = 0;
10700 }
10701 ast_cli(fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
10702 ASTOBJ_UNLOCK(iterator);
10703 } while(0));
10704 return RESULT_SUCCESS;
10705 #undef FORMAT
10706 #undef FORMAT2
10707 }
10708
10709
10710 static int sip_show_settings(int fd, int argc, char *argv[])
10711 {
10712 int realtimepeers;
10713 int realtimeusers;
10714 char codec_buf[SIPBUFSIZE];
10715
10716 realtimepeers = ast_check_realtime("sippeers");
10717 realtimeusers = ast_check_realtime("sipusers");
10718
10719 if (argc != 3)
10720 return RESULT_SHOWUSAGE;
10721 ast_cli(fd, "\n\nGlobal Settings:\n");
10722 ast_cli(fd, "----------------\n");
10723 ast_cli(fd, " SIP Port: %d\n", ntohs(bindaddr.sin_port));
10724 ast_cli(fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
10725 ast_cli(fd, " Videosupport: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "Yes" : "No");
10726 ast_cli(fd, " AutoCreatePeer: %s\n", autocreatepeer ? "Yes" : "No");
10727 ast_cli(fd, " Allow unknown access: %s\n", global_allowguest ? "Yes" : "No");
10728 ast_cli(fd, " Allow subscriptions: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
10729 ast_cli(fd, " Allow overlap dialing: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
10730 ast_cli(fd, " Promsic. redir: %s\n", ast_test_flag(&global_flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
10731 ast_cli(fd, " SIP domain support: %s\n", AST_LIST_EMPTY(&domain_list) ? "No" : "Yes");
10732 ast_cli(fd, " Call to non-local dom.: %s\n", allow_external_domains ? "Yes" : "No");
10733 ast_cli(fd, " URI user is phone no: %s\n", ast_test_flag(&global_flags[0], SIP_USEREQPHONE) ? "Yes" : "No");
10734 ast_cli(fd, " Our auth realm %s\n", global_realm);
10735 ast_cli(fd, " Realm. auth: %s\n", authl ? "Yes": "No");
10736 ast_cli(fd, " Always auth rejects: %s\n", global_alwaysauthreject ? "Yes" : "No");
10737 ast_cli(fd, " Call limit peers only: %s\n", global_limitonpeers ? "Yes" : "No");
10738 ast_cli(fd, " Direct RTP setup: %s\n", global_directrtpsetup ? "Yes" : "No");
10739 ast_cli(fd, " User Agent: %s\n", global_useragent);
10740 ast_cli(fd, " MWI checking interval: %d secs\n", global_mwitime);
10741 ast_cli(fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
10742 ast_cli(fd, " Caller ID: %s\n", default_callerid);
10743 ast_cli(fd, " From: Domain: %s\n", default_fromdomain);
10744 ast_cli(fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
10745 ast_cli(fd, " Call Events: %s\n", global_callevents ? "On" : "Off");
10746 ast_cli(fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
10747 ast_cli(fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
10748 ast_cli(fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
10749 ast_cli(fd, " T38 fax pt UDPTL: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL) ? "Yes" : "No");
10750 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
10751 ast_cli(fd, " T38 fax pt RTP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_RTP) ? "Yes" : "No");
10752 ast_cli(fd, " T38 fax pt TCP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_TCP) ? "Yes" : "No");
10753 #endif
10754 ast_cli(fd, " RFC2833 Compensation: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE) ? "Yes" : "No");
10755 if (!realtimepeers && !realtimeusers)
10756 ast_cli(fd, " SIP realtime: Disabled\n" );
10757 else
10758 ast_cli(fd, " SIP realtime: Enabled\n" );
10759
10760 ast_cli(fd, "\nGlobal Signalling Settings:\n");
10761 ast_cli(fd, "---------------------------\n");
10762 ast_cli(fd, " Codecs: ");
10763 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, global_capability);
10764 ast_cli(fd, "%s\n", codec_buf);
10765 ast_cli(fd, " Codec Order: ");
10766 print_codec_to_cli(fd, &default_prefs);
10767 ast_cli(fd, "\n");
10768 ast_cli(fd, " T1 minimum: %d\n", global_t1min);
10769 ast_cli(fd, " Relax DTMF: %s\n", global_relaxdtmf ? "Yes" : "No");
10770 ast_cli(fd, " Compact SIP headers: %s\n", compactheaders ? "Yes" : "No");
10771 ast_cli(fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
10772 ast_cli(fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
10773 ast_cli(fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
10774 ast_cli(fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
10775 ast_cli(fd, " DNS SRV lookup: %s\n", srvlookup ? "Yes" : "No");
10776 ast_cli(fd, " Pedantic SIP support: %s\n", pedanticsipchecking ? "Yes" : "No");
10777 ast_cli(fd, " Reg. min duration %d secs\n", min_expiry);
10778 ast_cli(fd, " Reg. max duration: %d secs\n", max_expiry);
10779 ast_cli(fd, " Reg. default duration: %d secs\n", default_expiry);
10780 ast_cli(fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
10781 ast_cli(fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
10782 ast_cli(fd, " Notify ringing state: %s\n", global_notifyringing ? "Yes" : "No");
10783 ast_cli(fd, " Notify hold state: %s\n", global_notifyhold ? "Yes" : "No");
10784 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(global_allowtransfer));
10785 ast_cli(fd, " Max Call Bitrate: %d kbps\r\n", default_maxcallbitrate);
10786 ast_cli(fd, " Auto-Framing: %s \r\n", global_autoframing ? "Yes" : "No");
10787 ast_cli(fd, "\nDefault Settings:\n");
10788 ast_cli(fd, "-----------------\n");
10789 ast_cli(fd, " Context: %s\n", default_context);
10790 ast_cli(fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags[0], SIP_NAT)));
10791 ast_cli(fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
10792 ast_cli(fd, " Qualify: %d\n", default_qualify);
10793 ast_cli(fd, " Use ClientCode: %s\n", ast_test_flag(&global_flags[0], SIP_USECLIENTCODE) ? "Yes" : "No");
10794 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" );
10795 ast_cli(fd, " Language: %s\n", S_OR(default_language, "(Defaults to English)"));
10796 ast_cli(fd, " MOH Interpret: %s\n", default_mohinterpret);
10797 ast_cli(fd, " MOH Suggest: %s\n", default_mohsuggest);
10798 ast_cli(fd, " Voice Mail Extension: %s\n", default_vmexten);
10799
10800
10801 if (realtimepeers || realtimeusers) {
10802 ast_cli(fd, "\nRealtime SIP Settings:\n");
10803 ast_cli(fd, "----------------------\n");
10804 ast_cli(fd, " Realtime Peers: %s\n", realtimepeers ? "Yes" : "No");
10805 ast_cli(fd, " Realtime Users: %s\n", realtimeusers ? "Yes" : "No");
10806 ast_cli(fd, " Cache Friends: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? "Yes" : "No");
10807 ast_cli(fd, " Update: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) ? "Yes" : "No");
10808 ast_cli(fd, " Ignore Reg. Expire: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) ? "Yes" : "No");
10809 ast_cli(fd, " Save sys. name: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME) ? "Yes" : "No");
10810 ast_cli(fd, " Auto Clear: %d\n", global_rtautoclear);
10811 }
10812 ast_cli(fd, "\n----\n");
10813 return RESULT_SUCCESS;
10814 }
10815
10816
10817 static const char *subscription_type2str(enum subscriptiontype subtype)
10818 {
10819 int i;
10820
10821 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
10822 if (subscription_types[i].type == subtype) {
10823 return subscription_types[i].text;
10824 }
10825 }
10826 return subscription_types[0].text;
10827 }
10828
10829
10830 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
10831 {
10832 int i;
10833
10834 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
10835 if (subscription_types[i].type == subtype) {
10836 return &subscription_types[i];
10837 }
10838 }
10839 return &subscription_types[0];
10840 }
10841
10842
10843 static int sip_show_channels(int fd, int argc, char *argv[])
10844 {
10845 return __sip_show_channels(fd, argc, argv, 0);
10846 }
10847
10848
10849 static int sip_show_subscriptions(int fd, int argc, char *argv[])
10850 {
10851 return __sip_show_channels(fd, argc, argv, 1);
10852 }
10853
10854
10855 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions)
10856 {
10857 #define FORMAT3 "%-15.15s %-10.10s %-11.11s %-15.15s %-13.13s %-15.15s %-10.10s\n"
10858 #define FORMAT2 "%-15.15s %-10.10s %-11.11s %-11.11s %-15.15s %-7.7s %-15.15s\n"
10859 #define FORMAT "%-15.15s %-10.10s %-11.11s %5.5d/%5.5d %-15.15s %-3.3s %-3.3s %-15.15s %-10.10s\n"
10860 struct sip_pvt *cur;
10861 int numchans = 0;
10862 char *referstatus = NULL;
10863
10864 if (argc != 3)
10865 return RESULT_SHOWUSAGE;
10866 ast_mutex_lock(&iflock);
10867 cur = iflist;
10868 if (!subscriptions)
10869 ast_cli(fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Seq (Tx/Rx)", "Format", "Hold", "Last Message");
10870 else
10871 ast_cli(fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox");
10872 for (; cur; cur = cur->next) {
10873 referstatus = "";
10874 if (cur->refer) {
10875 referstatus = referstatus2str(cur->refer->status);
10876 }
10877 if (cur->subscribed == NONE && !subscriptions) {
10878 char formatbuf[SIPBUFSIZE/2];
10879 ast_cli(fd, FORMAT, ast_inet_ntoa(cur->sa.sin_addr),
10880 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
10881 cur->callid,
10882 cur->ocseq, cur->icseq,
10883 ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
10884 ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD) ? "Yes" : "No",
10885 ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY) ? "(d)" : "",
10886 cur->lastmsg ,
10887 referstatus
10888 );
10889 numchans++;
10890 }
10891 if (cur->subscribed != NONE && subscriptions) {
10892 ast_cli(fd, FORMAT3, ast_inet_ntoa(cur->sa.sin_addr),
10893 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
10894 cur->callid,
10895
10896 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
10897 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
10898 subscription_type2str(cur->subscribed),
10899 cur->subscribed == MWI_NOTIFICATION ? (cur->relatedpeer ? cur->relatedpeer->mailbox : "<none>") : "<none>"
10900 );
10901 numchans++;
10902 }
10903 }
10904 ast_mutex_unlock(&iflock);
10905 if (!subscriptions)
10906 ast_cli(fd, "%d active SIP channel%s\n", numchans, (numchans != 1) ? "s" : "");
10907 else
10908 ast_cli(fd, "%d active SIP subscription%s\n", numchans, (numchans != 1) ? "s" : "");
10909 return RESULT_SUCCESS;
10910 #undef FORMAT
10911 #undef FORMAT2
10912 #undef FORMAT3
10913 }
10914
10915
10916 static char *complete_sipch(const char *line, const char *word, int pos, int state)
10917 {
10918 int which=0;
10919 struct sip_pvt *cur;
10920 char *c = NULL;
10921 int wordlen = strlen(word);
10922
10923 if (pos != 3) {
10924 return NULL;
10925 }
10926
10927 ast_mutex_lock(&iflock);
10928 for (cur = iflist; cur; cur = cur->next) {
10929 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
10930 c = ast_strdup(cur->callid);
10931 break;
10932 }
10933 }
10934 ast_mutex_unlock(&iflock);
10935 return c;
10936 }
10937
10938
10939 static char *complete_sip_peer(const char *word, int state, int flags2)
10940 {
10941 char *result = NULL;
10942 int wordlen = strlen(word);
10943 int which = 0;
10944
10945 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
10946
10947 if (!strncasecmp(word, iterator->name, wordlen) &&
10948 (!flags2 || ast_test_flag(&iterator->flags[1], flags2)) &&
10949 ++which > state)
10950 result = ast_strdup(iterator->name);
10951 } while(0) );
10952 return result;
10953 }
10954
10955
10956 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
10957 {
10958 if (pos == 3)
10959 return complete_sip_peer(word, state, 0);
10960
10961 return NULL;
10962 }
10963
10964
10965 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state)
10966 {
10967 if (pos == 3)
10968 return complete_sip_peer(word, state, 0);
10969
10970 return NULL;
10971 }
10972
10973
10974 static char *complete_sip_user(const char *word, int state, int flags2)
10975 {
10976 char *result = NULL;
10977 int wordlen = strlen(word);
10978 int which = 0;
10979
10980 ASTOBJ_CONTAINER_TRAVERSE(&userl, !result, do {
10981
10982 if (!strncasecmp(word, iterator->name, wordlen)) {
10983 if (flags2 && !ast_test_flag(&iterator->flags[1], flags2))
10984 continue;
10985 if (++which > state) {
10986 result = ast_strdup(iterator->name);
10987 }
10988 }
10989 } while(0) );
10990 return result;
10991 }
10992
10993
10994 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
10995 {
10996 if (pos == 3)
10997 return complete_sip_user(word, state, 0);
10998
10999 return NULL;
11000 }
11001
11002
11003 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
11004 {
11005 char *c = NULL;
11006
11007 if (pos == 2) {
11008 int which = 0;
11009 char *cat = NULL;
11010 int wordlen = strlen(word);
11011
11012
11013
11014 if (!notify_types)
11015 return NULL;
11016
11017 while ( (cat = ast_category_browse(notify_types, cat)) ) {
11018 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
11019 c = ast_strdup(cat);
11020 break;
11021 }
11022 }
11023 return c;
11024 }
11025
11026 if (pos > 2)
11027 return complete_sip_peer(word, state, 0);
11028
11029 return NULL;
11030 }
11031
11032
11033 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state)
11034 {
11035 if (pos == 4)
11036 return complete_sip_peer(word, state, SIP_PAGE2_RTCACHEFRIENDS);
11037 return NULL;
11038 }
11039
11040
11041 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state)
11042 {
11043 if (pos == 4)
11044 return complete_sip_user(word, state, SIP_PAGE2_RTCACHEFRIENDS);
11045
11046 return NULL;
11047 }
11048
11049
11050 static int sip_show_channel(int fd, int argc, char *argv[])
11051 {
11052 struct sip_pvt *cur;
11053 size_t len;
11054 int found = 0;
11055
11056 if (argc != 4)
11057 return RESULT_SHOWUSAGE;
11058 len = strlen(argv[3]);
11059 ast_mutex_lock(&iflock);
11060 for (cur = iflist; cur; cur = cur->next) {
11061 if (!strncasecmp(cur->callid, argv[3], len)) {
11062 char formatbuf[SIPBUFSIZE/2];
11063 ast_cli(fd,"\n");
11064 if (cur->subscribed != NONE)
11065 ast_cli(fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
11066 else
11067 ast_cli(fd, " * SIP Call\n");
11068 ast_cli(fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
11069 ast_cli(fd, " Call-ID: %s\n", cur->callid);
11070 ast_cli(fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
11071 ast_cli(fd, " Our Codec Capability: %d\n", cur->capability);
11072 ast_cli(fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
11073 ast_cli(fd, " Their Codec Capability: %d\n", cur->peercapability);
11074 ast_cli(fd, " Joint Codec Capability: %d\n", cur->jointcapability);
11075 ast_cli(fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
11076 ast_cli(fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
11077 ast_cli(fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(cur->sa.sin_addr), ntohs(cur->sa.sin_port));
11078 ast_cli(fd, " Received Address: %s:%d\n", ast_inet_ntoa(cur->recv.sin_addr), ntohs(cur->recv.sin_port));
11079 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
11080 ast_cli(fd, " NAT Support: %s\n", nat2str(ast_test_flag(&cur->flags[0], SIP_NAT)));
11081 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)" );
11082 ast_cli(fd, " Our Tag: %s\n", cur->tag);
11083 ast_cli(fd, " Their Tag: %s\n", cur->theirtag);
11084 ast_cli(fd, " SIP User agent: %s\n", cur->useragent);
11085 if (!ast_strlen_zero(cur->username))
11086 ast_cli(fd, " Username: %s\n", cur->username);
11087 if (!ast_strlen_zero(cur->peername))
11088 ast_cli(fd, " Peername: %s\n", cur->peername);
11089 if (!ast_strlen_zero(cur->uri))
11090 ast_cli(fd, " Original uri: %s\n", cur->uri);
11091 if (!ast_strlen_zero(cur->cid_num))
11092 ast_cli(fd, " Caller-ID: %s\n", cur->cid_num);
11093 ast_cli(fd, " Need Destroy: %d\n", ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY));
11094 ast_cli(fd, " Last Message: %s\n", cur->lastmsg);
11095 ast_cli(fd, " Promiscuous Redir: %s\n", ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
11096 ast_cli(fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
11097 ast_cli(fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
11098 ast_cli(fd, " SIP Options: ");
11099 if (cur->sipoptions) {
11100 int x;
11101 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
11102 if (cur->sipoptions & sip_options[x].id)
11103 ast_cli(fd, "%s ", sip_options[x].text);
11104 }
11105 } else
11106 ast_cli(fd, "(none)\n");
11107 ast_cli(fd, "\n\n");
11108 found++;
11109 }
11110 }
11111 ast_mutex_unlock(&iflock);
11112 if (!found)
11113 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
11114 return RESULT_SUCCESS;
11115 }
11116
11117
11118 static int sip_show_history(int fd, int argc, char *argv[])
11119 {
11120 struct sip_pvt *cur;
11121 size_t len;
11122 int found = 0;
11123
11124 if (argc != 4)
11125 return RESULT_SHOWUSAGE;
11126 if (!recordhistory)
11127 ast_cli(fd, "\n***Note: History recording is currently DISABLED. Use 'sip history' to ENABLE.\n");
11128 len = strlen(argv[3]);
11129 ast_mutex_lock(&iflock);
11130 for (cur = iflist; cur; cur = cur->next) {
11131 if (!strncasecmp(cur->callid, argv[3], len)) {
11132 struct sip_history *hist;
11133 int x = 0;
11134
11135 ast_cli(fd,"\n");
11136 if (cur->subscribed != NONE)
11137 ast_cli(fd, " * Subscription\n");
11138 else
11139 ast_cli(fd, " * SIP Call\n");
11140 if (cur->history)
11141 AST_LIST_TRAVERSE(cur->history, hist, list)
11142 ast_cli(fd, "%d. %s\n", ++x, hist->event);
11143 if (x == 0)
11144 ast_cli(fd, "Call '%s' has no history\n", cur->callid);
11145 found++;
11146 }
11147 }
11148 ast_mutex_unlock(&iflock);
11149 if (!found)
11150 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
11151 return RESULT_SUCCESS;
11152 }
11153
11154
11155 static void sip_dump_history(struct sip_pvt *dialog)
11156 {
11157 int x = 0;
11158 struct sip_history *hist;
11159 static int errmsg = 0;
11160
11161 if (!dialog)
11162 return;
11163
11164 if (!option_debug && !sipdebug) {
11165 if (!errmsg) {
11166 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
11167 errmsg = 1;
11168 }
11169 return;
11170 }
11171
11172 ast_log(LOG_DEBUG, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
11173 if (dialog->subscribed)
11174 ast_log(LOG_DEBUG, " * Subscription\n");
11175 else
11176 ast_log(LOG_DEBUG, " * SIP Call\n");
11177 if (dialog->history)
11178 AST_LIST_TRAVERSE(dialog->history, hist, list)
11179 ast_log(LOG_DEBUG, " %-3.3d. %s\n", ++x, hist->event);
11180 if (!x)
11181 ast_log(LOG_DEBUG, "Call '%s' has no history\n", dialog->callid);
11182 ast_log(LOG_DEBUG, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
11183 }
11184
11185
11186
11187
11188 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
11189 {
11190 char buf[1024];
11191 unsigned int event;
11192 const char *c = get_header(req, "Content-Type");
11193
11194
11195 if (!strcasecmp(c, "application/dtmf-relay") ||
11196 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
11197 unsigned int duration = 0;
11198
11199
11200 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
11201 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
11202 transmit_response(p, "200 OK", req);
11203 return;
11204 } else {
11205 ast_copy_string(buf, c, sizeof(buf));
11206 }
11207
11208 if (!ast_strlen_zero((c = get_body(req, "Duration"))))
11209 duration = atoi(c);
11210 if (!duration)
11211 duration = 100;
11212
11213 if (!p->owner) {
11214 transmit_response(p, "481 Call leg/transaction does not exist", req);
11215 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11216 return;
11217 }
11218
11219 if (ast_strlen_zero(buf)) {
11220 transmit_response(p, "200 OK", req);
11221 return;
11222 }
11223
11224 if (buf[0] == '*')
11225 event = 10;
11226 else if (buf[0] == '#')
11227 event = 11;
11228 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
11229 event = 12 + buf[0] - 'A';
11230 else
11231 event = atoi(buf);
11232 if (event == 16) {
11233
11234 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
11235 ast_queue_frame(p->owner, &f);
11236 if (sipdebug)
11237 ast_verbose("* DTMF-relay event received: FLASH\n");
11238 } else {
11239
11240 struct ast_frame f = { AST_FRAME_DTMF, };
11241 if (event < 10) {
11242 f.subclass = '0' + event;
11243 } else if (event < 11) {
11244 f.subclass = '*';
11245 } else if (event < 12) {
11246 f.subclass = '#';
11247 } else if (event < 16) {
11248 f.subclass = 'A' + (event - 12);
11249 }
11250 f.len = duration;
11251 ast_queue_frame(p->owner, &f);
11252 if (sipdebug)
11253 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
11254 }
11255 transmit_response(p, "200 OK", req);
11256 return;
11257 } else if (!strcasecmp(c, "application/media_control+xml")) {
11258
11259 if (p->owner)
11260 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
11261 transmit_response(p, "200 OK", req);
11262 return;
11263 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
11264
11265 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
11266 if (p->owner && p->owner->cdr)
11267 ast_cdr_setuserfield(p->owner, c);
11268 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
11269 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
11270 transmit_response(p, "200 OK", req);
11271 } else {
11272 transmit_response(p, "403 Unauthorized", req);
11273 }
11274 return;
11275 } else if (ast_strlen_zero(c = get_header(req, "Content-Length")) || !strcasecmp(c, "0")) {
11276
11277 transmit_response(p, "200 OK", req);
11278 return;
11279 }
11280
11281
11282
11283
11284 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
11285 transmit_response(p, "415 Unsupported media type", req);
11286 return;
11287 }
11288
11289
11290 static int sip_do_debug_ip(int fd, int argc, char *argv[])
11291 {
11292 struct hostent *hp;
11293 struct ast_hostent ahp;
11294 int port = 0;
11295 char *p, *arg;
11296
11297
11298 if (argc != 5)
11299 return RESULT_SHOWUSAGE;
11300 p = arg = argv[4];
11301 strsep(&p, ":");
11302 if (p)
11303 port = atoi(p);
11304 hp = ast_gethostbyname(arg, &ahp);
11305 if (hp == NULL)
11306 return RESULT_SHOWUSAGE;
11307
11308 debugaddr.sin_family = AF_INET;
11309 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
11310 debugaddr.sin_port = htons(port);
11311 if (port == 0)
11312 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(debugaddr.sin_addr));
11313 else
11314 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), port);
11315
11316 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11317
11318 return RESULT_SUCCESS;
11319 }
11320
11321
11322 static int sip_do_debug_peer(int fd, int argc, char *argv[])
11323 {
11324 struct sip_peer *peer;
11325 if (argc != 5)
11326 return RESULT_SHOWUSAGE;
11327 peer = find_peer(argv[4], NULL, 1);
11328 if (peer) {
11329 if (peer->addr.sin_addr.s_addr) {
11330 debugaddr.sin_family = AF_INET;
11331 debugaddr.sin_addr = peer->addr.sin_addr;
11332 debugaddr.sin_port = peer->addr.sin_port;
11333 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
11334 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11335 } else
11336 ast_cli(fd, "Unable to get IP address of peer '%s'\n", argv[4]);
11337 ASTOBJ_UNREF(peer,sip_destroy_peer);
11338 } else
11339 ast_cli(fd, "No such peer '%s'\n", argv[4]);
11340 return RESULT_SUCCESS;
11341 }
11342
11343
11344 static int sip_do_debug(int fd, int argc, char *argv[])
11345 {
11346 int oldsipdebug = sipdebug_console;
11347 if (argc != 3) {
11348 if (argc != 5)
11349 return RESULT_SHOWUSAGE;
11350 else if (strcmp(argv[3], "ip") == 0)
11351 return sip_do_debug_ip(fd, argc, argv);
11352 else if (strcmp(argv[3], "peer") == 0)
11353 return sip_do_debug_peer(fd, argc, argv);
11354 else
11355 return RESULT_SHOWUSAGE;
11356 }
11357 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11358 memset(&debugaddr, 0, sizeof(debugaddr));
11359 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
11360 return RESULT_SUCCESS;
11361 }
11362
11363 static int sip_do_debug_deprecated(int fd, int argc, char *argv[])
11364 {
11365 int oldsipdebug = sipdebug_console;
11366 char *newargv[6] = { "sip", "set", "debug", NULL };
11367 if (argc != 2) {
11368 if (argc != 4)
11369 return RESULT_SHOWUSAGE;
11370 else if (strcmp(argv[2], "ip") == 0) {
11371 newargv[3] = argv[2];
11372 newargv[4] = argv[3];
11373 return sip_do_debug_ip(fd, argc + 1, newargv);
11374 } else if (strcmp(argv[2], "peer") == 0) {
11375 newargv[3] = argv[2];
11376 newargv[4] = argv[3];
11377 return sip_do_debug_peer(fd, argc + 1, newargv);
11378 } else
11379 return RESULT_SHOWUSAGE;
11380 }
11381 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11382 memset(&debugaddr, 0, sizeof(debugaddr));
11383 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
11384 return RESULT_SUCCESS;
11385 }
11386
11387
11388 static int sip_notify(int fd, int argc, char *argv[])
11389 {
11390 struct ast_variable *varlist;
11391 int i;
11392
11393 if (argc < 4)
11394 return RESULT_SHOWUSAGE;
11395
11396 if (!notify_types) {
11397 ast_cli(fd, "No %s file found, or no types listed there\n", notify_config);
11398 return RESULT_FAILURE;
11399 }
11400
11401 varlist = ast_variable_browse(notify_types, argv[2]);
11402
11403 if (!varlist) {
11404 ast_cli(fd, "Unable to find notify type '%s'\n", argv[2]);
11405 return RESULT_FAILURE;
11406 }
11407
11408 for (i = 3; i < argc; i++) {
11409 struct sip_pvt *p;
11410 struct sip_request req;
11411 struct ast_variable *var;
11412
11413 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY))) {
11414 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
11415 return RESULT_FAILURE;
11416 }
11417
11418 if (create_addr(p, argv[i])) {
11419
11420 sip_destroy(p);
11421 ast_cli(fd, "Could not create address for '%s'\n", argv[i]);
11422 continue;
11423 }
11424
11425 initreqprep(&req, p, SIP_NOTIFY);
11426
11427 for (var = varlist; var; var = var->next)
11428 add_header(&req, var->name, ast_unescape_semicolon(var->value));
11429
11430
11431 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
11432 p->ourip = __ourip;
11433 build_via(p);
11434 build_callid_pvt(p);
11435 ast_cli(fd, "Sending NOTIFY of type '%s' to '%s'\n", argv[2], argv[i]);
11436 transmit_sip_request(p, &req);
11437 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11438 }
11439
11440 return RESULT_SUCCESS;
11441 }
11442
11443
11444 static int sip_no_debug(int fd, int argc, char *argv[])
11445 {
11446 if (argc != 4)
11447 return RESULT_SHOWUSAGE;
11448 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11449 ast_cli(fd, "SIP Debugging Disabled\n");
11450 return RESULT_SUCCESS;
11451 }
11452
11453 static int sip_no_debug_deprecated(int fd, int argc, char *argv[])
11454 {
11455 if (argc != 3)
11456 return RESULT_SHOWUSAGE;
11457 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11458 ast_cli(fd, "SIP Debugging Disabled\n");
11459 return RESULT_SUCCESS;
11460 }
11461
11462
11463 static int sip_do_history(int fd, int argc, char *argv[])
11464 {
11465 if (argc != 2) {
11466 return RESULT_SHOWUSAGE;
11467 }
11468 recordhistory = TRUE;
11469 ast_cli(fd, "SIP History Recording Enabled (use 'sip show history')\n");
11470 return RESULT_SUCCESS;
11471 }
11472
11473
11474 static int sip_no_history(int fd, int argc, char *argv[])
11475 {
11476 if (argc != 3) {
11477 return RESULT_SHOWUSAGE;
11478 }
11479 recordhistory = FALSE;
11480 ast_cli(fd, "SIP History Recording Disabled\n");
11481 return RESULT_SUCCESS;
11482 }
11483
11484
11485 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader)
11486 {
11487 char digest[1024];
11488 p->authtries++;
11489 memset(digest,0,sizeof(digest));
11490 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
11491
11492
11493 if (sip_debug_test_pvt(p) && p->registry)
11494 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
11495
11496 return -1;
11497 }
11498 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
11499 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
11500 if (sip_debug_test_pvt(p) && p->registry)
11501 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
11502 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
11503 }
11504
11505
11506 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader, int sipmethod, int init)
11507 {
11508 char digest[1024];
11509
11510 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
11511 return -2;
11512
11513 p->authtries++;
11514 if (option_debug > 1)
11515 ast_log(LOG_DEBUG, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
11516 memset(digest, 0, sizeof(digest));
11517 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
11518
11519 return -1;
11520 }
11521
11522 p->options->auth = digest;
11523 p->options->authheader = respheader;
11524 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
11525 }
11526
11527
11528
11529
11530
11531 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
11532 {
11533 char tmp[512];
11534 char *c;
11535 char oldnonce[256];
11536
11537
11538 const struct x {
11539 const char *key;
11540 int field_index;
11541 } *i, keys[] = {
11542 { "realm=", ast_string_field_index(p, realm) },
11543 { "nonce=", ast_string_field_index(p, nonce) },
11544 { "opaque=", ast_string_field_index(p, opaque) },
11545 { "qop=", ast_string_field_index(p, qop) },
11546 { "domain=", ast_string_field_index(p, domain) },
11547 { NULL, 0 },
11548 };
11549
11550 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
11551 if (ast_strlen_zero(tmp))
11552 return -1;
11553 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
11554 ast_log(LOG_WARNING, "missing Digest.\n");
11555 return -1;
11556 }
11557 c = tmp + strlen("Digest ");
11558 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
11559 while (c && *(c = ast_skip_blanks(c))) {
11560 for (i = keys; i->key != NULL; i++) {
11561 char *src, *separator;
11562 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
11563 continue;
11564
11565 c += strlen(i->key);
11566 if (*c == '"') {
11567 src = ++c;
11568 separator = "\"";
11569 } else {
11570 src = c;
11571 separator = ",";
11572 }
11573 strsep(&c, separator);
11574 ast_string_field_index_set(p, i->field_index, src);
11575 break;
11576 }
11577 if (i->key == NULL)
11578 strsep(&c, ",");
11579 }
11580
11581 if (strcmp(p->nonce, oldnonce))
11582 p->noncecount = 0;
11583
11584
11585 if (p->registry) {
11586 struct sip_registry *r = p->registry;
11587
11588 if (strcmp(r->nonce, p->nonce)) {
11589 ast_string_field_set(r, realm, p->realm);
11590 ast_string_field_set(r, nonce, p->nonce);
11591 ast_string_field_set(r, domain, p->domain);
11592 ast_string_field_set(r, opaque, p->opaque);
11593 ast_string_field_set(r, qop, p->qop);
11594 r->noncecount = 0;
11595 }
11596 }
11597 return build_reply_digest(p, sipmethod, digest, digest_len);
11598 }
11599
11600
11601
11602
11603
11604
11605 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
11606 {
11607 char a1[256];
11608 char a2[256];
11609 char a1_hash[256];
11610 char a2_hash[256];
11611 char resp[256];
11612 char resp_hash[256];
11613 char uri[256];
11614 char opaque[256] = "";
11615 char cnonce[80];
11616 const char *username;
11617 const char *secret;
11618 const char *md5secret;
11619 struct sip_auth *auth = NULL;
11620
11621 if (!ast_strlen_zero(p->domain))
11622 ast_copy_string(uri, p->domain, sizeof(uri));
11623 else if (!ast_strlen_zero(p->uri))
11624 ast_copy_string(uri, p->uri, sizeof(uri));
11625 else
11626 snprintf(uri, sizeof(uri), "sip:%s@%s",p->username, ast_inet_ntoa(p->sa.sin_addr));
11627
11628 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
11629
11630
11631 if ((auth = find_realm_authentication(authl, p->realm))) {
11632 ast_log(LOG_WARNING, "use realm [%s] from peer [%s][%s]\n",
11633 auth->username, p->peername, p->username);
11634 username = auth->username;
11635 secret = auth->secret;
11636 md5secret = auth->md5secret;
11637 if (sipdebug)
11638 ast_log(LOG_DEBUG,"Using realm %s authentication for call %s\n", p->realm, p->callid);
11639 } else {
11640
11641 username = p->authname;
11642 secret = p->peersecret;
11643 md5secret = p->peermd5secret;
11644 }
11645 if (ast_strlen_zero(username))
11646 return -1;
11647
11648
11649 snprintf(a1,sizeof(a1),"%s:%s:%s", username, p->realm, secret);
11650 snprintf(a2,sizeof(a2),"%s:%s", sip_methods[method].text, uri);
11651 if (!ast_strlen_zero(md5secret))
11652 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
11653 else
11654 ast_md5_hash(a1_hash,a1);
11655 ast_md5_hash(a2_hash,a2);
11656
11657 p->noncecount++;
11658 if (!ast_strlen_zero(p->qop))
11659 snprintf(resp,sizeof(resp),"%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
11660 else
11661 snprintf(resp,sizeof(resp),"%s:%s:%s", a1_hash, p->nonce, a2_hash);
11662 ast_md5_hash(resp_hash, resp);
11663
11664
11665 if (!ast_strlen_zero(p->opaque)) {
11666 snprintf(opaque, sizeof(opaque), ", opaque=\"%s\"", p->opaque);
11667 }
11668
11669
11670 if (!ast_strlen_zero(p->qop))
11671 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);
11672 else
11673 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);
11674
11675 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
11676
11677 return 0;
11678 }
11679
11680 static char show_domains_usage[] =
11681 "Usage: sip show domains\n"
11682 " Lists all configured SIP local domains.\n"
11683 " Asterisk only responds to SIP messages to local domains.\n";
11684
11685 static char notify_usage[] =
11686 "Usage: sip notify <type> <peer> [<peer>...]\n"
11687 " Send a NOTIFY message to a SIP peer or peers\n"
11688 " Message types are defined in sip_notify.conf\n";
11689
11690 static char show_users_usage[] =
11691 "Usage: sip show users [like <pattern>]\n"
11692 " Lists all known SIP users.\n"
11693 " Optional regular expression pattern is used to filter the user list.\n";
11694
11695 static char show_user_usage[] =
11696 "Usage: sip show user <name> [load]\n"
11697 " Shows all details on one SIP user and the current status.\n"
11698 " Option \"load\" forces lookup of peer in realtime storage.\n";
11699
11700 static char show_inuse_usage[] =
11701 "Usage: sip show inuse [all]\n"
11702 " List all SIP users and peers usage counters and limits.\n"
11703 " Add option \"all\" to show all devices, not only those with a limit.\n";
11704
11705 static char show_channels_usage[] =
11706 "Usage: sip show channels\n"
11707 " Lists all currently active SIP channels.\n";
11708
11709 static char show_channel_usage[] =
11710 "Usage: sip show channel <channel>\n"
11711 " Provides detailed status on a given SIP channel.\n";
11712
11713 static char show_history_usage[] =
11714 "Usage: sip show history <channel>\n"
11715 " Provides detailed dialog history on a given SIP channel.\n";
11716
11717 static char show_peers_usage[] =
11718 "Usage: sip show peers [like <pattern>]\n"
11719 " Lists all known SIP peers.\n"
11720 " Optional regular expression pattern is used to filter the peer list.\n";
11721
11722 static char show_peer_usage[] =
11723 "Usage: sip show peer <name> [load]\n"
11724 " Shows all details on one SIP peer and the current status.\n"
11725 " Option \"load\" forces lookup of peer in realtime storage.\n";
11726
11727 static char prune_realtime_usage[] =
11728 "Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
11729 " Prunes object(s) from the cache.\n"
11730 " Optional regular expression pattern is used to filter the objects.\n";
11731
11732 static char show_reg_usage[] =
11733 "Usage: sip show registry\n"
11734 " Lists all registration requests and status.\n";
11735
11736 static char debug_usage[] =
11737 "Usage: sip set debug\n"
11738 " Enables dumping of SIP packets for debugging purposes\n\n"
11739 " sip set debug ip <host[:PORT]>\n"
11740 " Enables dumping of SIP packets to and from host.\n\n"
11741 " sip set debug peer <peername>\n"
11742 " Enables dumping of SIP packets to and from host.\n"
11743 " Require peer to be registered.\n";
11744
11745 static char no_debug_usage[] =
11746 "Usage: sip set debug off\n"
11747 " Disables dumping of SIP packets for debugging purposes\n";
11748
11749 static char no_history_usage[] =
11750 "Usage: sip history off\n"
11751 " Disables recording of SIP dialog history for debugging purposes\n";
11752
11753 static char history_usage[] =
11754 "Usage: sip history\n"
11755 " Enables recording of SIP dialog history for debugging purposes.\n"
11756 "Use 'sip show history' to view the history of a call number.\n";
11757
11758 static char sip_reload_usage[] =
11759 "Usage: sip reload\n"
11760 " Reloads SIP configuration from sip.conf\n";
11761
11762 static char show_subscriptions_usage[] =
11763 "Usage: sip show subscriptions\n"
11764 " Lists active SIP subscriptions for extension states\n";
11765
11766 static char show_objects_usage[] =
11767 "Usage: sip show objects\n"
11768 " Lists status of known SIP objects\n";
11769
11770 static char show_settings_usage[] =
11771 "Usage: sip show settings\n"
11772 " Provides detailed list of the configuration of the SIP channel.\n";
11773
11774
11775 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
11776 {
11777 struct sip_pvt *p;
11778 const char *content = NULL;
11779 AST_DECLARE_APP_ARGS(args,
11780 AST_APP_ARG(header);
11781 AST_APP_ARG(number);
11782 );
11783 int i, number, start = 0;
11784
11785 if (ast_strlen_zero(data)) {
11786 ast_log(LOG_WARNING, "This function requires a header name.\n");
11787 return -1;
11788 }
11789
11790 ast_channel_lock(chan);
11791 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
11792 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
11793 ast_channel_unlock(chan);
11794 return -1;
11795 }
11796
11797 AST_STANDARD_APP_ARGS(args, data);
11798 if (!args.number) {
11799 number = 1;
11800 } else {
11801 sscanf(args.number, "%d", &number);
11802 if (number < 1)
11803 number = 1;
11804 }
11805
11806 p = chan->tech_pvt;
11807
11808
11809 if (!p) {
11810 ast_channel_unlock(chan);
11811 return -1;
11812 }
11813
11814 for (i = 0; i < number; i++)
11815 content = __get_header(&p->initreq, args.header, &start);
11816
11817 if (ast_strlen_zero(content)) {
11818 ast_channel_unlock(chan);
11819 return -1;
11820 }
11821
11822 ast_copy_string(buf, content, len);
11823 ast_channel_unlock(chan);
11824
11825 return 0;
11826 }
11827
11828 static struct ast_custom_function sip_header_function = {
11829 .name = "SIP_HEADER",
11830 .synopsis = "Gets the specified SIP header",
11831 .syntax = "SIP_HEADER(<name>[,<number>])",
11832 .desc = "Since there are several headers (such as Via) which can occur multiple\n"
11833 "times, SIP_HEADER takes an optional second argument to specify which header with\n"
11834 "that name to retrieve. Headers start at offset 1.\n",
11835 .read = func_header_read,
11836 };
11837
11838
11839 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11840 {
11841 if (ast_strlen_zero(data)) {
11842 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
11843 return -1;
11844 }
11845 if (check_sip_domain(data, NULL, 0))
11846 ast_copy_string(buf, data, len);
11847 else
11848 buf[0] = '\0';
11849 return 0;
11850 }
11851
11852 static struct ast_custom_function checksipdomain_function = {
11853 .name = "CHECKSIPDOMAIN",
11854 .synopsis = "Checks if domain is a local domain",
11855 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
11856 .read = func_check_sipdomain,
11857 .desc = "This function checks if the domain in the argument is configured\n"
11858 "as a local SIP domain that this Asterisk server is configured to handle.\n"
11859 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
11860 "Check the domain= configuration in sip.conf\n",
11861 };
11862
11863
11864 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11865 {
11866 struct sip_peer *peer;
11867 char *colname;
11868
11869 if ((colname = strchr(data, ':')))
11870 *colname++ = '\0';
11871 else if ((colname = strchr(data, '|')))
11872 *colname++ = '\0';
11873 else
11874 colname = "ip";
11875
11876 if (!(peer = find_peer(data, NULL, 1)))
11877 return -1;
11878
11879 if (!strcasecmp(colname, "ip")) {
11880 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
11881 } else if (!strcasecmp(colname, "status")) {
11882 peer_status(peer, buf, len);
11883 } else if (!strcasecmp(colname, "language")) {
11884 ast_copy_string(buf, peer->language, len);
11885 } else if (!strcasecmp(colname, "regexten")) {
11886 ast_copy_string(buf, peer->regexten, len);
11887 } else if (!strcasecmp(colname, "limit")) {
11888 snprintf(buf, len, "%d", peer->call_limit);
11889 } else if (!strcasecmp(colname, "curcalls")) {
11890 snprintf(buf, len, "%d", peer->inUse);
11891 } else if (!strcasecmp(colname, "accountcode")) {
11892 ast_copy_string(buf, peer->accountcode, len);
11893 } else if (!strcasecmp(colname, "useragent")) {
11894 ast_copy_string(buf, peer->useragent, len);
11895 } else if (!strcasecmp(colname, "mailbox")) {
11896 ast_copy_string(buf, peer->mailbox, len);
11897 } else if (!strcasecmp(colname, "context")) {
11898 ast_copy_string(buf, peer->context, len);
11899 } else if (!strcasecmp(colname, "expire")) {
11900 snprintf(buf, len, "%d", peer->expire);
11901 } else if (!strcasecmp(colname, "dynamic")) {
11902 ast_copy_string(buf, (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no"), len);
11903 } else if (!strcasecmp(colname, "callerid_name")) {
11904 ast_copy_string(buf, peer->cid_name, len);
11905 } else if (!strcasecmp(colname, "callerid_num")) {
11906 ast_copy_string(buf, peer->cid_num, len);
11907 } else if (!strcasecmp(colname, "codecs")) {
11908 ast_getformatname_multiple(buf, len -1, peer->capability);
11909 } else if (!strncasecmp(colname, "codec[", 6)) {
11910 char *codecnum;
11911 int index = 0, codec = 0;
11912
11913 codecnum = colname + 6;
11914 codecnum = strsep(&codecnum, "]");
11915 index = atoi(codecnum);
11916 if((codec = ast_codec_pref_index(&peer->prefs, index))) {
11917 ast_copy_string(buf, ast_getformatname(codec), len);
11918 }
11919 }
11920
11921 ASTOBJ_UNREF(peer, sip_destroy_peer);
11922
11923 return 0;
11924 }
11925
11926
11927 struct ast_custom_function sippeer_function = {
11928 .name = "SIPPEER",
11929 .synopsis = "Gets SIP peer information",
11930 .syntax = "SIPPEER(<peername>[|item])",
11931 .read = function_sippeer,
11932 .desc = "Valid items are:\n"
11933 "- ip (default) The IP address.\n"
11934 "- mailbox The configured mailbox.\n"
11935 "- context The configured context.\n"
11936 "- expire The epoch time of the next expire.\n"
11937 "- dynamic Is it dynamic? (yes/no).\n"
11938 "- callerid_name The configured Caller ID name.\n"
11939 "- callerid_num The configured Caller ID number.\n"
11940 "- codecs The configured codecs.\n"
11941 "- status Status (if qualify=yes).\n"
11942 "- regexten Registration extension\n"
11943 "- limit Call limit (call-limit)\n"
11944 "- curcalls Current amount of calls \n"
11945 " Only available if call-limit is set\n"
11946 "- language Default language for peer\n"
11947 "- accountcode Account code for this peer\n"
11948 "- useragent Current user agent id for peer\n"
11949 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
11950 "\n"
11951 };
11952
11953
11954 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11955 {
11956 struct sip_pvt *p;
11957
11958 *buf = 0;
11959
11960 if (!data) {
11961 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
11962 return -1;
11963 }
11964
11965 ast_channel_lock(chan);
11966 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
11967 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
11968 ast_channel_unlock(chan);
11969 return -1;
11970 }
11971
11972 p = chan->tech_pvt;
11973
11974
11975 if (!p) {
11976 ast_channel_unlock(chan);
11977 return -1;
11978 }
11979
11980 if (!strcasecmp(data, "peerip")) {
11981 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", len);
11982 } else if (!strcasecmp(data, "recvip")) {
11983 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", len);
11984 } else if (!strcasecmp(data, "from")) {
11985 ast_copy_string(buf, p->from, len);
11986 } else if (!strcasecmp(data, "uri")) {
11987 ast_copy_string(buf, p->uri, len);
11988 } else if (!strcasecmp(data, "useragent")) {
11989 ast_copy_string(buf, p->useragent, len);
11990 } else if (!strcasecmp(data, "peername")) {
11991 ast_copy_string(buf, p->peername, len);
11992 } else if (!strcasecmp(data, "t38passthrough")) {
11993 if (p->t38.state == T38_DISABLED)
11994 ast_copy_string(buf, "0", sizeof("0"));
11995 else
11996 ast_copy_string(buf, "1", sizeof("1"));
11997 } else {
11998 ast_channel_unlock(chan);
11999 return -1;
12000 }
12001 ast_channel_unlock(chan);
12002
12003 return 0;
12004 }
12005
12006
12007 static struct ast_custom_function sipchaninfo_function = {
12008 .name = "SIPCHANINFO",
12009 .synopsis = "Gets the specified SIP parameter from the current channel",
12010 .syntax = "SIPCHANINFO(item)",
12011 .read = function_sipchaninfo_read,
12012 .desc = "Valid items are:\n"
12013 "- peerip The IP address of the peer.\n"
12014 "- recvip The source IP address of the peer.\n"
12015 "- from The URI from the From: header.\n"
12016 "- uri The URI from the Contact: header.\n"
12017 "- useragent The useragent.\n"
12018 "- peername The name of the peer.\n"
12019 "- t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
12020 };
12021
12022
12023 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
12024 {
12025 char tmp[SIPBUFSIZE];
12026 char *s, *e, *uri, *t;
12027 char *domain;
12028
12029 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
12030 if ((t = strchr(tmp, ',')))
12031 *t = '\0';
12032 s = get_in_brackets(tmp);
12033 uri = ast_strdupa(s);
12034 if (ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
12035 if (!strncasecmp(s, "sip:", 4))
12036 s += 4;
12037 e = strchr(s, ';');
12038 if (e)
12039 *e = '\0';
12040 if (option_debug)
12041 ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
12042 if (p->owner)
12043 ast_string_field_build(p->owner, call_forward, "SIP/%s", s);
12044 } else {
12045 e = strchr(tmp, '@');
12046 if (e) {
12047 *e++ = '\0';
12048 domain = e;
12049 } else {
12050
12051 domain = tmp;
12052 }
12053 e = strchr(s, ';');
12054 if (e)
12055 *e = '\0';
12056 e = strchr(domain, ';');
12057 if (e)
12058 *e = '\0';
12059
12060 if (!strncasecmp(s, "sip:", 4))
12061 s += 4;
12062 if (option_debug > 1)
12063 ast_log(LOG_DEBUG, "Received 302 Redirect to extension '%s' (domain %s)\n", s, domain);
12064 if (p->owner) {
12065 pbx_builtin_setvar_helper(p->owner, "SIPREDIRECTURI", uri);
12066 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
12067 ast_string_field_set(p->owner, call_forward, s);
12068 }
12069 }
12070 }
12071
12072
12073 static void check_pendings(struct sip_pvt *p)
12074 {
12075 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
12076
12077 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA)
12078 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
12079
12080
12081 else {
12082
12083
12084 if (p->pendinginvite)
12085 return;
12086
12087
12088 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
12089 }
12090 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
12091 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12092 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
12093
12094 if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
12095 if (option_debug)
12096 ast_log(LOG_DEBUG, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
12097 } else {
12098 if (option_debug)
12099 ast_log(LOG_DEBUG, "Sending pending reinvite on '%s'\n", p->callid);
12100
12101 transmit_reinvite_with_sdp(p);
12102 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
12103 }
12104 }
12105 }
12106
12107
12108
12109
12110
12111 static int sip_reinvite_retry(const void *data)
12112 {
12113 struct sip_pvt *p = (struct sip_pvt *) data;
12114
12115 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
12116 p->waitid = -1;
12117 return 0;
12118 }
12119
12120
12121
12122 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
12123 {
12124 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
12125 int res = 0;
12126 int xmitres = 0;
12127 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
12128 struct ast_channel *bridgepeer = NULL;
12129
12130 if (option_debug > 3) {
12131 if (reinvite)
12132 ast_log(LOG_DEBUG, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
12133 else
12134 ast_log(LOG_DEBUG, "SIP response %d to standard invite\n", resp);
12135 }
12136
12137 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
12138 if (option_debug)
12139 ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
12140 return;
12141 }
12142
12143
12144
12145 AST_SCHED_DEL(sched, p->initid);
12146
12147
12148
12149
12150 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 182 && resp != 183)
12151 resp = 183;
12152
12153
12154 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
12155 p->invitestate = INV_PROCEEDING;
12156
12157
12158 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
12159 p->invitestate = INV_COMPLETED;
12160
12161
12162 switch (resp) {
12163 case 100:
12164 case 101:
12165 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12166 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12167 check_pendings(p);
12168 break;
12169
12170 case 180:
12171 case 182:
12172 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12173 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12174 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12175 ast_queue_control(p->owner, AST_CONTROL_RINGING);
12176 if (p->owner->_state != AST_STATE_UP) {
12177 ast_setstate(p->owner, AST_STATE_RINGING);
12178 }
12179 }
12180 if (find_sdp(req)) {
12181 if (p->invitestate != INV_CANCELLED)
12182 p->invitestate = INV_EARLY_MEDIA;
12183 res = process_sdp(p, req);
12184 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12185
12186 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
12187 }
12188 }
12189 check_pendings(p);
12190 break;
12191
12192 case 183:
12193 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12194 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12195
12196 if (find_sdp(req)) {
12197 if (p->invitestate != INV_CANCELLED)
12198 p->invitestate = INV_EARLY_MEDIA;
12199 res = process_sdp(p, req);
12200 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12201
12202 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
12203 }
12204 }
12205 check_pendings(p);
12206 break;
12207
12208 case 200:
12209 if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
12210 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12211 p->authtries = 0;
12212 if (find_sdp(req)) {
12213 if ((res = process_sdp(p, req)) && !ast_test_flag(req, SIP_PKT_IGNORE))
12214 if (!reinvite)
12215
12216
12217 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
12218 }
12219
12220
12221
12222
12223 if (outgoing) {
12224 update_call_counter(p, DEC_CALL_RINGING);
12225 parse_ok_contact(p, req);
12226 if(set_address_from_contact(p)) {
12227
12228
12229
12230
12231
12232
12233
12234 if (!ast_test_flag(req, SIP_PKT_IGNORE))
12235 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
12236 }
12237
12238
12239 if (!reinvite)
12240 build_route(p, req, 1);
12241 }
12242
12243 if (p->owner && (p->owner->_state == AST_STATE_UP) && (bridgepeer = ast_bridged_channel(p->owner))) {
12244 struct sip_pvt *bridgepvt = NULL;
12245
12246 if (!bridgepeer->tech) {
12247 ast_log(LOG_WARNING, "Ooooh.. no tech! That's REALLY bad\n");
12248 break;
12249 }
12250 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
12251 bridgepvt = (struct sip_pvt*)(bridgepeer->tech_pvt);
12252 if (bridgepvt->udptl) {
12253 if (p->t38.state == T38_PEER_REINVITE) {
12254 sip_handle_t38_reinvite(bridgepeer, p, 0);
12255 ast_rtp_set_rtptimers_onhold(p->rtp);
12256 if (p->vrtp)
12257 ast_rtp_set_rtptimers_onhold(p->vrtp);
12258 } else if (p->t38.state == T38_DISABLED && bridgepeer && (bridgepvt->t38.state == T38_ENABLED)) {
12259 ast_log(LOG_WARNING, "RTP re-invite after T38 session not handled yet !\n");
12260
12261
12262 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12263 }
12264 } else {
12265 if (option_debug > 1)
12266 ast_log(LOG_DEBUG, "Strange... The other side of the bridge does not have a udptl struct\n");
12267 ast_mutex_lock(&bridgepvt->lock);
12268 bridgepvt->t38.state = T38_DISABLED;
12269 ast_mutex_unlock(&bridgepvt->lock);
12270 if (option_debug)
12271 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->tech->type);
12272 p->t38.state = T38_DISABLED;
12273 if (option_debug > 1)
12274 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
12275 }
12276 } else {
12277
12278 if (option_debug > 1)
12279 ast_log(LOG_DEBUG, "Strange... The other side of the bridge is not a SIP channel\n");
12280 p->t38.state = T38_DISABLED;
12281 if (option_debug > 1)
12282 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
12283 }
12284 }
12285 if ((p->t38.state == T38_LOCAL_REINVITE) || (p->t38.state == T38_LOCAL_DIRECT)) {
12286
12287 p->t38.state = T38_ENABLED;
12288 if (option_debug)
12289 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
12290 }
12291
12292 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
12293 if (!reinvite) {
12294 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
12295 } else {
12296 ast_queue_frame(p->owner, &ast_null_frame);
12297 }
12298 } else {
12299
12300
12301
12302 if (!ast_test_flag(req, SIP_PKT_IGNORE))
12303 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
12304 }
12305
12306 p->invitestate = INV_TERMINATED;
12307 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
12308 check_pendings(p);
12309 break;
12310 case 407:
12311 case 401:
12312
12313 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12314 if (p->options)
12315 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
12316
12317
12318 ast_string_field_free(p, theirtag);
12319 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
12320 char *authenticate = (resp == 401 ? "WWW-Authenticate" : "Proxy-Authenticate");
12321 char *authorization = (resp == 401 ? "Authorization" : "Proxy-Authorization");
12322 if (p->authtries < MAX_AUTHTRIES)
12323 p->invitestate = INV_CALLING;
12324 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, authenticate, authorization, SIP_INVITE, 1)) {
12325 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
12326 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12327 sip_alreadygone(p);
12328 if (p->owner)
12329 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12330 }
12331 }
12332 break;
12333
12334 case 403:
12335
12336 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12337 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
12338 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner)
12339 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12340 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12341 sip_alreadygone(p);
12342 break;
12343
12344 case 404:
12345 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12346 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12347 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12348 sip_alreadygone(p);
12349 break;
12350
12351 case 408:
12352 case 481:
12353
12354 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
12355 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12356 if (p->owner)
12357 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12358 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12359 break;
12360 case 487:
12361
12362
12363
12364 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12365 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE)) {
12366 ast_queue_hangup(p->owner);
12367 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
12368 } else if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
12369 update_call_counter(p, DEC_CALL_LIMIT);
12370 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
12371 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12372 sip_alreadygone(p);
12373 }
12374 break;
12375 case 488:
12376 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12377 if (reinvite && p->udptl) {
12378
12379
12380
12381
12382
12383 p->t38.state = T38_DISABLED;
12384
12385 ast_rtp_set_rtptimers_onhold(p->rtp);
12386 ast_log(LOG_ERROR, "Got error on T.38 re-invite. Bad configuration. Peer needs to have T.38 disabled.\n");
12387
12388
12389
12390
12391
12392 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12393 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12394 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12395 } else if (p->udptl && p->t38.state == T38_LOCAL_DIRECT) {
12396
12397
12398
12399 p->t38.state = T38_DISABLED;
12400
12401 ast_rtp_set_rtptimers_onhold(p->rtp);
12402 ast_log(LOG_ERROR, "Got error on T.38 initial invite. Bailing out.\n");
12403
12404
12405 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12406 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12407 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12408 sip_alreadygone(p);
12409 } else {
12410
12411 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12412 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12413 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12414
12415 if (!reinvite)
12416 sip_alreadygone(p);
12417 }
12418 break;
12419 case 491:
12420
12421
12422
12423
12424 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12425 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE)) {
12426 if (p->owner->_state != AST_STATE_UP) {
12427 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12428 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12429 } else {
12430
12431
12432
12433 int wait = 3 + ast_random() % 5;
12434 p->waitid = ast_sched_add(sched, wait, sip_reinvite_retry, p);
12435 if (option_debug > 2)
12436 ast_log(LOG_DEBUG, "Reinvite race. Waiting %d secs before retry\n", wait);
12437 }
12438 }
12439 break;
12440
12441 case 501:
12442 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12443 if (p->owner)
12444 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12445 break;
12446 }
12447 if (xmitres == XMIT_ERROR)
12448 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
12449 }
12450
12451
12452
12453
12454 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
12455 {
12456 char *auth = "Proxy-Authenticate";
12457 char *auth2 = "Proxy-Authorization";
12458
12459
12460 if (!p->refer)
12461 return;
12462
12463 switch (resp) {
12464 case 202:
12465
12466
12467 p->refer->status = REFER_ACCEPTED;
12468
12469 if (option_debug > 2)
12470 ast_log(LOG_DEBUG, "Got 202 accepted on transfer\n");
12471
12472 break;
12473
12474 case 401:
12475 case 407:
12476 if (ast_strlen_zero(p->authname)) {
12477 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s:%d but we have no matching peer or realm auth!\n",
12478 ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12479 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12480 }
12481 if (resp == 401) {
12482 auth = "WWW-Authenticate";
12483 auth2 = "Authorization";
12484 }
12485 if ((p->authtries > 1) || do_proxy_auth(p, req, auth, auth2, SIP_REFER, 0)) {
12486 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
12487 p->refer->status = REFER_NOAUTH;
12488 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12489 }
12490 break;
12491 case 481:
12492
12493
12494
12495
12496 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
12497 if (p->owner)
12498 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12499 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12500 break;
12501
12502 case 500:
12503 case 501:
12504
12505
12506 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
12507 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12508 p->refer->status = REFER_FAILED;
12509 break;
12510 case 603:
12511 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
12512 p->refer->status = REFER_FAILED;
12513 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12514 break;
12515 }
12516 }
12517
12518
12519 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
12520 {
12521 int expires, expires_ms;
12522 struct sip_registry *r;
12523 r=p->registry;
12524
12525 switch (resp) {
12526 case 401:
12527 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "WWW-Authenticate", "Authorization")) {
12528 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
12529 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12530 }
12531 break;
12532 case 403:
12533 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
12534 if (global_regattempts_max)
12535 p->registry->regattempts = global_regattempts_max+1;
12536 AST_SCHED_DEL(sched, r->timeout);
12537 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12538 break;
12539 case 404:
12540 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username,p->registry->hostname);
12541 if (global_regattempts_max)
12542 p->registry->regattempts = global_regattempts_max+1;
12543 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12544 r->call = NULL;
12545 AST_SCHED_DEL(sched, r->timeout);
12546 break;
12547 case 407:
12548 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization")) {
12549 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
12550 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12551 }
12552 break;
12553 case 408:
12554 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12555 r->call = NULL;
12556 AST_SCHED_DEL(sched, r->timeout);
12557 break;
12558 case 479:
12559 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username,p->registry->hostname);
12560 if (global_regattempts_max)
12561 p->registry->regattempts = global_regattempts_max+1;
12562 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12563 r->call = NULL;
12564 AST_SCHED_DEL(sched, r->timeout);
12565 break;
12566 case 200:
12567 if (!r) {
12568 ast_log(LOG_WARNING, "Got 200 OK on REGISTER that isn't a register\n");
12569 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12570 return 0;
12571 }
12572
12573 r->regstate = REG_STATE_REGISTERED;
12574 r->regtime = time(NULL);
12575 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
12576 r->regattempts = 0;
12577 if (option_debug)
12578 ast_log(LOG_DEBUG, "Registration successful\n");
12579 if (r->timeout > -1) {
12580 if (option_debug)
12581 ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
12582 }
12583 AST_SCHED_DEL(sched, r->timeout);
12584 r->call = NULL;
12585 p->registry = NULL;
12586
12587 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12588
12589
12590
12591
12592 AST_SCHED_DEL(sched, r->expire);
12593
12594
12595 expires = 0;
12596
12597
12598 if (!ast_strlen_zero(get_header(req, "Contact"))) {
12599 const char *contact = NULL;
12600 const char *tmptmp = NULL;
12601 int start = 0;
12602 for(;;) {
12603 contact = __get_header(req, "Contact", &start);
12604
12605 if(!ast_strlen_zero(contact)) {
12606 if( (tmptmp=strstr(contact, p->our_contact))) {
12607 contact=tmptmp;
12608 break;
12609 }
12610 } else
12611 break;
12612 }
12613 tmptmp = strcasestr(contact, "expires=");
12614 if (tmptmp) {
12615 if (sscanf(tmptmp + 8, "%d;", &expires) != 1)
12616 expires = 0;
12617 }
12618
12619 }
12620 if (!expires)
12621 expires=atoi(get_header(req, "expires"));
12622 if (!expires)
12623 expires=default_expiry;
12624
12625 expires_ms = expires * 1000;
12626 if (expires <= EXPIRY_GUARD_LIMIT)
12627 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT),EXPIRY_GUARD_MIN);
12628 else
12629 expires_ms -= EXPIRY_GUARD_SECS * 1000;
12630 if (sipdebug)
12631 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
12632
12633 r->refresh= (int) expires_ms / 1000;
12634
12635
12636 AST_SCHED_DEL(sched, r->expire);
12637 r->expire = ast_sched_add(sched, expires_ms, sip_reregister, r);
12638 ASTOBJ_UNREF(r, sip_registry_destroy);
12639 }
12640 return 1;
12641 }
12642
12643
12644 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
12645 {
12646 struct sip_peer *peer = p->relatedpeer;
12647 int statechanged, is_reachable, was_reachable;
12648 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
12649
12650
12651
12652
12653
12654
12655 if (pingtime < 1)
12656 pingtime = 1;
12657
12658
12659
12660
12661
12662 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
12663 is_reachable = pingtime <= peer->maxms;
12664 statechanged = peer->lastms == 0
12665 || was_reachable != is_reachable;
12666
12667 peer->lastms = pingtime;
12668 peer->call = NULL;
12669 if (statechanged) {
12670 const char *s = is_reachable ? "Reachable" : "Lagged";
12671
12672 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
12673 peer->name, s, pingtime, peer->maxms);
12674 ast_device_state_changed("SIP/%s", peer->name);
12675 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
12676 "Peer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
12677 peer->name, s, pingtime);
12678 }
12679
12680 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
12681 struct sip_peer *peer_ptr = peer;
12682 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
12683 }
12684
12685 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12686
12687
12688 peer->pokeexpire = ast_sched_add(sched,
12689 is_reachable ? DEFAULT_FREQ_OK : DEFAULT_FREQ_NOTOK,
12690 sip_poke_peer_s, ASTOBJ_REF(peer));
12691
12692 if (peer->pokeexpire == -1) {
12693 ASTOBJ_UNREF(peer, sip_destroy_peer);
12694 }
12695 }
12696
12697
12698 static void stop_media_flows(struct sip_pvt *p)
12699 {
12700
12701 if (p->rtp)
12702 ast_rtp_stop(p->rtp);
12703 if (p->vrtp)
12704 ast_rtp_stop(p->vrtp);
12705 if (p->udptl)
12706 ast_udptl_stop(p->udptl);
12707 }
12708
12709
12710
12711 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
12712 {
12713 struct ast_channel *owner;
12714 int sipmethod;
12715 int res = 1;
12716 const char *c = get_header(req, "Cseq");
12717 const char *msg = strchr(c, ' ');
12718
12719 if (!msg)
12720 msg = "";
12721 else
12722 msg++;
12723 sipmethod = find_sip_method(msg);
12724
12725 owner = p->owner;
12726 if (owner)
12727 owner->hangupcause = hangup_sip2cause(resp);
12728
12729
12730 if ((resp >= 100) && (resp <= 199))
12731 __sip_semi_ack(p, seqno, 0, sipmethod);
12732 else
12733 __sip_ack(p, seqno, 0, sipmethod);
12734
12735
12736 if (!p->owner && sipmethod == SIP_NOTIFY && p->pendinginvite)
12737 p->pendinginvite = 0;
12738
12739
12740 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
12741 char tag[128];
12742
12743 gettag(req, "To", tag, sizeof(tag));
12744 ast_string_field_set(p, theirtag, tag);
12745 }
12746 if (p->relatedpeer && p->method == SIP_OPTIONS) {
12747
12748
12749
12750 if (resp != 100)
12751 handle_response_peerpoke(p, resp, req);
12752 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
12753 switch(resp) {
12754 case 100:
12755 case 101:
12756 if (sipmethod == SIP_INVITE)
12757 handle_response_invite(p, resp, rest, req, seqno);
12758 break;
12759 case 183:
12760 if (sipmethod == SIP_INVITE)
12761 handle_response_invite(p, resp, rest, req, seqno);
12762 break;
12763 case 180:
12764 if (sipmethod == SIP_INVITE)
12765 handle_response_invite(p, resp, rest, req, seqno);
12766 break;
12767 case 182:
12768 if (sipmethod == SIP_INVITE)
12769 handle_response_invite(p, resp, rest, req, seqno);
12770 break;
12771 case 200:
12772 p->authtries = 0;
12773 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
12774
12775
12776
12777 } else if (sipmethod == SIP_INVITE) {
12778 handle_response_invite(p, resp, rest, req, seqno);
12779 } else if (sipmethod == SIP_NOTIFY) {
12780
12781 if (p->owner) {
12782 if (!p->refer) {
12783 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
12784 ast_queue_hangup(p->owner);
12785 } else if (option_debug > 3)
12786 ast_log(LOG_DEBUG, "Got OK on REFER Notify message\n");
12787 } else {
12788 if (p->subscribed == NONE)
12789 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12790 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
12791
12792 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
12793 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p, NULL, NULL);
12794 }
12795 }
12796 } else if (sipmethod == SIP_REGISTER)
12797 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12798 else if (sipmethod == SIP_BYE)
12799 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12800 break;
12801 case 202:
12802 if (sipmethod == SIP_REFER)
12803 handle_response_refer(p, resp, rest, req, seqno);
12804 break;
12805 case 401:
12806 if (sipmethod == SIP_INVITE)
12807 handle_response_invite(p, resp, rest, req, seqno);
12808 else if (sipmethod == SIP_REFER)
12809 handle_response_refer(p, resp, rest, req, seqno);
12810 else if (p->registry && sipmethod == SIP_REGISTER)
12811 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12812 else if (sipmethod == SIP_BYE) {
12813 if (ast_strlen_zero(p->authname)) {
12814 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
12815 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12816 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12817 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "WWW-Authenticate", "Authorization", sipmethod, 0)) {
12818 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
12819 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12820
12821
12822 }
12823 } else {
12824 ast_log(LOG_WARNING, "Got authentication request (401) on unknown %s to '%s'\n", sip_methods[sipmethod].text, get_header(req, "To"));
12825 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12826 }
12827 break;
12828 case 403:
12829 if (sipmethod == SIP_INVITE)
12830 handle_response_invite(p, resp, rest, req, seqno);
12831 else if (p->registry && sipmethod == SIP_REGISTER)
12832 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12833 else {
12834 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
12835 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12836 }
12837 break;
12838 case 404:
12839 if (p->registry && sipmethod == SIP_REGISTER)
12840 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12841 else if (sipmethod == SIP_INVITE)
12842 handle_response_invite(p, resp, rest, req, seqno);
12843 else if (owner)
12844 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12845 break;
12846 case 407:
12847 if (sipmethod == SIP_INVITE)
12848 handle_response_invite(p, resp, rest, req, seqno);
12849 else if (sipmethod == SIP_REFER)
12850 handle_response_refer(p, resp, rest, req, seqno);
12851 else if (p->registry && sipmethod == SIP_REGISTER)
12852 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12853 else if (sipmethod == SIP_BYE) {
12854 if (ast_strlen_zero(p->authname)) {
12855 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
12856 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12857 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12858 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization", sipmethod, 0)) {
12859 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
12860 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12861 }
12862 } else
12863 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12864
12865 break;
12866 case 408:
12867 if (sipmethod == SIP_INVITE)
12868 handle_response_invite(p, resp, rest, req, seqno);
12869 else if (sipmethod == SIP_REGISTER)
12870 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12871 else if (sipmethod == SIP_BYE) {
12872 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12873 if (option_debug)
12874 ast_log(LOG_DEBUG, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
12875 } else {
12876 if (owner)
12877 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12878 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12879 }
12880 break;
12881 case 481:
12882 if (sipmethod == SIP_INVITE) {
12883 handle_response_invite(p, resp, rest, req, seqno);
12884 } else if (sipmethod == SIP_REFER) {
12885 handle_response_refer(p, resp, rest, req, seqno);
12886 } else if (sipmethod == SIP_BYE) {
12887
12888
12889 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12890 } else if (sipmethod == SIP_CANCEL) {
12891
12892
12893 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12894 } else {
12895 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12896
12897 }
12898 break;
12899 case 487:
12900 if (sipmethod == SIP_INVITE)
12901 handle_response_invite(p, resp, rest, req, seqno);
12902 break;
12903 case 488:
12904 if (sipmethod == SIP_INVITE)
12905 handle_response_invite(p, resp, rest, req, seqno);
12906 break;
12907 case 491:
12908 if (sipmethod == SIP_INVITE)
12909 handle_response_invite(p, resp, rest, req, seqno);
12910 else {
12911 if (option_debug)
12912 ast_log(LOG_DEBUG, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
12913 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12914 }
12915 break;
12916 case 501:
12917 if (sipmethod == SIP_INVITE)
12918 handle_response_invite(p, resp, rest, req, seqno);
12919 else if (sipmethod == SIP_REFER)
12920 handle_response_refer(p, resp, rest, req, seqno);
12921 else
12922 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(p->sa.sin_addr), msg);
12923 break;
12924 case 603:
12925 if (sipmethod == SIP_REFER) {
12926 handle_response_refer(p, resp, rest, req, seqno);
12927 break;
12928 }
12929
12930 default:
12931 if ((resp >= 300) && (resp < 700)) {
12932
12933 if ((option_verbose > 2) && (resp != 487))
12934 ast_verbose(VERBOSE_PREFIX_3 "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
12935
12936 if (sipmethod == SIP_INVITE)
12937 stop_media_flows(p);
12938
12939
12940 switch(resp) {
12941 case 300:
12942 case 301:
12943 case 302:
12944 case 305:
12945 parse_moved_contact(p, req);
12946
12947 case 486:
12948 case 600:
12949 case 603:
12950 if (p->owner)
12951 ast_queue_control(p->owner, AST_CONTROL_BUSY);
12952 break;
12953 case 482:
12954
12955
12956
12957
12958 if (option_debug)
12959 ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
12960 if (p->owner)
12961 ast_string_field_build(p->owner, call_forward,
12962 "Local/%s@%s", p->username, p->context);
12963
12964 case 480:
12965 case 404:
12966 case 410:
12967 case 400:
12968 case 500:
12969 if (sipmethod == SIP_REFER) {
12970 handle_response_refer(p, resp, rest, req, seqno);
12971 break;
12972 }
12973
12974 case 502:
12975 case 503:
12976 case 504:
12977 if (owner)
12978 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12979 break;
12980 default:
12981
12982 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO && sipmethod != SIP_BYE)
12983 ast_queue_hangup(p->owner);
12984 break;
12985 }
12986
12987 if (sipmethod == SIP_INVITE)
12988 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12989 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
12990 sip_alreadygone(p);
12991 if (!p->owner)
12992 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12993 } else if ((resp >= 100) && (resp < 200)) {
12994 if (sipmethod == SIP_INVITE) {
12995 if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
12996 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
12997 if (find_sdp(req))
12998 process_sdp(p, req);
12999 if (p->owner) {
13000
13001 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
13002 }
13003 }
13004 } else
13005 ast_log(LOG_NOTICE, "Dont know how to handle a %d %s response from %s\n", resp, rest, p->owner ? p->owner->name : ast_inet_ntoa(p->sa.sin_addr));
13006 }
13007 } else {
13008
13009
13010 if (ast_test_flag(req, SIP_PKT_DEBUG))
13011 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
13012
13013 if (sipmethod == SIP_INVITE && resp == 200) {
13014
13015
13016 char tag[128];
13017
13018 gettag(req, "To", tag, sizeof(tag));
13019 ast_string_field_set(p, theirtag, tag);
13020 }
13021
13022 switch(resp) {
13023 case 200:
13024 if (sipmethod == SIP_INVITE) {
13025 handle_response_invite(p, resp, rest, req, seqno);
13026 } else if (sipmethod == SIP_CANCEL) {
13027 if (option_debug)
13028 ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
13029
13030
13031 } else if (sipmethod == SIP_NOTIFY) {
13032
13033 if (p->owner) {
13034 if (p->refer) {
13035 if (option_debug)
13036 ast_log(LOG_DEBUG, "Got 200 OK on NOTIFY for transfer\n");
13037 } else
13038 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
13039
13040 } else {
13041 if (!p->subscribed && !p->refer)
13042 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13043 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
13044
13045 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
13046 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p, NULL, NULL);
13047 }
13048 }
13049 } else if (sipmethod == SIP_BYE)
13050 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13051 else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO)
13052
13053
13054 ;
13055 else if (sipmethod == SIP_BYE)
13056
13057 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13058 break;
13059 case 202:
13060 if (sipmethod == SIP_REFER)
13061 handle_response_refer(p, resp, rest, req, seqno);
13062 break;
13063 case 401:
13064 case 407:
13065 if (sipmethod == SIP_REFER)
13066 handle_response_refer(p, resp, rest, req, seqno);
13067 else if (sipmethod == SIP_INVITE)
13068 handle_response_invite(p, resp, rest, req, seqno);
13069 else if (sipmethod == SIP_BYE) {
13070 char *auth, *auth2;
13071
13072 auth = (resp == 407 ? "Proxy-Authenticate" : "WWW-Authenticate");
13073 auth2 = (resp == 407 ? "Proxy-Authorization" : "Authorization");
13074 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, auth, auth2, sipmethod, 0)) {
13075 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
13076 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13077 }
13078 }
13079 break;
13080 case 481:
13081 if (sipmethod == SIP_INVITE) {
13082
13083 handle_response_invite(p, resp, rest, req, seqno);
13084 } else if (sipmethod == SIP_BYE) {
13085 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13086 } else if (sipdebug) {
13087 ast_log (LOG_DEBUG, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
13088 }
13089 break;
13090 case 501:
13091 if (sipmethod == SIP_INVITE)
13092 handle_response_invite(p, resp, rest, req, seqno);
13093 else if (sipmethod == SIP_REFER)
13094 handle_response_refer(p, resp, rest, req, seqno);
13095 break;
13096 case 603:
13097 if (sipmethod == SIP_REFER) {
13098 handle_response_refer(p, resp, rest, req, seqno);
13099 break;
13100 }
13101
13102 default:
13103 if ((resp >= 100) && (resp < 200)) {
13104 if (sipmethod == SIP_INVITE) {
13105 if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
13106 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13107 }
13108 }
13109 if ((resp >= 300) && (resp < 700)) {
13110 if ((option_verbose > 2) && (resp != 487))
13111 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));
13112 switch(resp) {
13113 case 488:
13114 case 603:
13115 case 500:
13116 case 502:
13117 case 503:
13118 case 504:
13119
13120
13121 if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
13122 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13123 break;
13124 }
13125 }
13126 break;
13127 }
13128 }
13129 }
13130
13131
13132
13133
13134
13135
13136
13137 static void *sip_park_thread(void *stuff)
13138 {
13139 struct ast_channel *transferee, *transferer;
13140 struct sip_dual *d;
13141 struct sip_request req;
13142 int ext;
13143 int res;
13144
13145 d = stuff;
13146 transferee = d->chan1;
13147 transferer = d->chan2;
13148 copy_request(&req, &d->req);
13149 free(d);
13150
13151 if (!transferee || !transferer) {
13152 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
13153 return NULL;
13154 }
13155 if (option_debug > 3)
13156 ast_log(LOG_DEBUG, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
13157
13158 ast_channel_lock(transferee);
13159 if (ast_do_masquerade(transferee)) {
13160 ast_log(LOG_WARNING, "Masquerade failed.\n");
13161 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
13162 ast_channel_unlock(transferee);
13163 return NULL;
13164 }
13165 ast_channel_unlock(transferee);
13166
13167 res = ast_park_call(transferee, transferer, 0, &ext);
13168
13169
13170 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
13171 if (!res) {
13172 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
13173 } else {
13174
13175 sprintf(buf, "Call parked on extension '%d'", ext);
13176 transmit_message_with_text(transferer->tech_pvt, buf);
13177 }
13178 #endif
13179
13180
13181
13182 transmit_response(transferer->tech_pvt, "202 Accepted", &req);
13183 if (!res) {
13184
13185 append_history(transferer->tech_pvt, "SIPpark","Parked call on %d", ext);
13186 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
13187 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
13188 ast_hangup(transferer);
13189 if (option_debug)
13190 ast_log(LOG_DEBUG, "SIP Call parked on extension '%d'\n", ext);
13191 } else {
13192 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
13193 append_history(transferer->tech_pvt, "SIPpark","Parking failed\n");
13194 if (option_debug)
13195 ast_log(LOG_DEBUG, "SIP Call parked failed \n");
13196
13197 }
13198 return NULL;
13199 }
13200
13201
13202
13203
13204 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno)
13205 {
13206 struct sip_dual *d;
13207 struct ast_channel *transferee, *transferer;
13208
13209 pthread_t th;
13210
13211 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
13212 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "SIPPeer/%s", chan2->name);
13213 if ((!transferer) || (!transferee)) {
13214 if (transferee) {
13215 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13216 ast_hangup(transferee);
13217 }
13218 if (transferer) {
13219 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13220 ast_hangup(transferer);
13221 }
13222 return -1;
13223 }
13224
13225
13226 transferee->readformat = chan1->readformat;
13227 transferee->writeformat = chan1->writeformat;
13228
13229
13230 ast_channel_masquerade(transferee, chan1);
13231
13232
13233 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
13234 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
13235 transferee->priority = chan1->priority;
13236
13237
13238
13239
13240
13241 transferer->readformat = chan2->readformat;
13242 transferer->writeformat = chan2->writeformat;
13243
13244
13245
13246
13247 while (ast_channel_trylock(chan2)) {
13248 struct sip_pvt *pvt = chan2->tech_pvt;
13249 DEADLOCK_AVOIDANCE(&pvt->lock);
13250 }
13251 ast_channel_masquerade(transferer, chan2);
13252 ast_channel_unlock(chan2);
13253
13254
13255 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
13256 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
13257 transferer->priority = chan2->priority;
13258
13259 ast_channel_lock(transferer);
13260 if (ast_do_masquerade(transferer)) {
13261 ast_log(LOG_WARNING, "Masquerade failed :(\n");
13262 ast_channel_unlock(transferer);
13263 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13264 ast_hangup(transferer);
13265 return -1;
13266 }
13267 ast_channel_unlock(transferer);
13268 if (!transferer || !transferee) {
13269 if (!transferer) {
13270 if (option_debug)
13271 ast_log(LOG_DEBUG, "No transferer channel, giving up parking\n");
13272 }
13273 if (!transferee) {
13274 if (option_debug)
13275 ast_log(LOG_DEBUG, "No transferee channel, giving up parking\n");
13276 }
13277 return -1;
13278 }
13279 if ((d = ast_calloc(1, sizeof(*d)))) {
13280 pthread_attr_t attr;
13281
13282 pthread_attr_init(&attr);
13283 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
13284
13285
13286 copy_request(&d->req, req);
13287 d->chan1 = transferee;
13288 d->chan2 = transferer;
13289 d->seqno = seqno;
13290 if (ast_pthread_create_background(&th, &attr, sip_park_thread, d) < 0) {
13291
13292 free(d);
13293
13294 pthread_attr_destroy(&attr);
13295 return 0;
13296 }
13297 pthread_attr_destroy(&attr);
13298 }
13299 return -1;
13300 }
13301
13302
13303
13304
13305 static void ast_quiet_chan(struct ast_channel *chan)
13306 {
13307 if (chan && chan->_state == AST_STATE_UP) {
13308 if (ast_test_flag(chan, AST_FLAG_MOH))
13309 ast_moh_stop(chan);
13310 else if (chan->generatordata)
13311 ast_deactivate_generator(chan);
13312 }
13313 }
13314
13315
13316
13317 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
13318 {
13319 int res = 0;
13320 struct ast_channel *peera = NULL,
13321 *peerb = NULL,
13322 *peerc = NULL,
13323 *peerd = NULL;
13324
13325
13326
13327
13328 if (option_debug > 3) {
13329 ast_log(LOG_DEBUG, "Sip transfer:--------------------\n");
13330 if (transferer->chan1)
13331 ast_log(LOG_DEBUG, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
13332 else
13333 ast_log(LOG_DEBUG, "-- No transferer first channel - odd??? \n");
13334 if (target->chan1)
13335 ast_log(LOG_DEBUG, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
13336 else
13337 ast_log(LOG_DEBUG, "-- No target first channel ---\n");
13338 if (transferer->chan2)
13339 ast_log(LOG_DEBUG, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
13340 else
13341 ast_log(LOG_DEBUG, "-- No bridged call to transferee\n");
13342 if (target->chan2)
13343 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)");
13344 else
13345 ast_log(LOG_DEBUG, "-- No target second channel ---\n");
13346 ast_log(LOG_DEBUG, "-- END Sip transfer:--------------------\n");
13347 }
13348 if (transferer->chan2) {
13349 peera = transferer->chan1;
13350 peerb = target->chan1;
13351 peerc = transferer->chan2;
13352 peerd = target->chan2;
13353 if (option_debug > 2)
13354 ast_log(LOG_DEBUG, "SIP transfer: Four channels to handle\n");
13355 } else if (target->chan2) {
13356 peera = target->chan1;
13357 peerb = transferer->chan1;
13358 peerc = target->chan2;
13359 peerd = transferer->chan2;
13360 if (option_debug > 2)
13361 ast_log(LOG_DEBUG, "SIP transfer: Three channels to handle\n");
13362 }
13363
13364 if (peera && peerb && peerc && (peerb != peerc)) {
13365 ast_quiet_chan(peera);
13366 ast_quiet_chan(peerb);
13367 ast_quiet_chan(peerc);
13368 if (peerd)
13369 ast_quiet_chan(peerd);
13370
13371
13372 if (peera->cdr && peerb->cdr)
13373 peerb->cdr = ast_cdr_append(peerb->cdr, peera->cdr);
13374 else if (peera->cdr)
13375 peerb->cdr = peera->cdr;
13376 peera->cdr = NULL;
13377
13378 if (peerb->cdr && peerc->cdr)
13379 peerb->cdr = ast_cdr_append(peerb->cdr, peerc->cdr);
13380 else if (peerc->cdr)
13381 peerb->cdr = peerc->cdr;
13382 peerc->cdr = NULL;
13383
13384 if (option_debug > 3)
13385 ast_log(LOG_DEBUG, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
13386 if (ast_channel_masquerade(peerb, peerc)) {
13387 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
13388 res = -1;
13389 } else
13390 ast_log(LOG_DEBUG, "SIP transfer: Succeeded to masquerade channels.\n");
13391 return res;
13392 } else {
13393 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
13394 if (transferer->chan1)
13395 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
13396 if (target->chan1)
13397 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
13398 return -2;
13399 }
13400 return 0;
13401 }
13402
13403
13404
13405
13406
13407
13408 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
13409 {
13410 const char *thetag;
13411
13412 if (!tagbuf)
13413 return NULL;
13414 tagbuf[0] = '\0';
13415 thetag = get_header(req, header);
13416 thetag = strcasestr(thetag, ";tag=");
13417 if (thetag) {
13418 thetag += 5;
13419 ast_copy_string(tagbuf, thetag, tagbufsize);
13420 return strsep(&tagbuf, ";");
13421 }
13422 return NULL;
13423 }
13424
13425
13426 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
13427 {
13428
13429
13430 int res = 0;
13431 const char *event = get_header(req, "Event");
13432 char *eventid = NULL;
13433 char *sep;
13434
13435 if( (sep = strchr(event, ';')) ) {
13436 *sep++ = '\0';
13437 eventid = sep;
13438 }
13439
13440 if (option_debug > 1 && sipdebug)
13441 ast_log(LOG_DEBUG, "Got NOTIFY Event: %s\n", event);
13442
13443 if (strcmp(event, "refer")) {
13444
13445
13446 transmit_response(p, "489 Bad event", req);
13447 res = -1;
13448 } else {
13449
13450
13451
13452
13453
13454 char buf[1024];
13455 char *cmd, *code;
13456 int respcode;
13457 int success = TRUE;
13458
13459
13460
13461
13462
13463
13464
13465
13466 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
13467
13468 transmit_response(p, "400 Bad request", req);
13469 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13470 return -1;
13471 }
13472
13473
13474 if (get_msg_text(buf, sizeof(buf), req)) {
13475 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
13476 transmit_response(p, "400 Bad request", req);
13477 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13478 return -1;
13479 }
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501 if (option_debug > 2)
13502 ast_log(LOG_DEBUG, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
13503 cmd = ast_skip_blanks(buf);
13504 code = cmd;
13505
13506 while(*code && (*code > 32)) {
13507 code++;
13508 }
13509 *code++ = '\0';
13510 code = ast_skip_blanks(code);
13511 sep = code;
13512 sep++;
13513 while(*sep && (*sep > 32)) {
13514 sep++;
13515 }
13516 *sep++ = '\0';
13517 respcode = atoi(code);
13518 switch (respcode) {
13519 case 100:
13520 case 101:
13521
13522 break;
13523 case 183:
13524
13525 break;
13526 case 200:
13527
13528 break;
13529 case 301:
13530 case 302:
13531
13532 success = FALSE;
13533 break;
13534 case 503:
13535
13536 success = FALSE;
13537 break;
13538 case 603:
13539
13540 success = FALSE;
13541 break;
13542 }
13543 if (!success) {
13544 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
13545 }
13546
13547
13548 transmit_response(p, "200 OK", req);
13549 };
13550
13551 if (!p->lastinvite)
13552 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13553
13554 return res;
13555 }
13556
13557
13558 static int handle_request_options(struct sip_pvt *p, struct sip_request *req)
13559 {
13560 int res;
13561
13562 res = get_destination(p, req);
13563 build_contact(p);
13564
13565
13566
13567 if (ast_strlen_zero(p->context))
13568 ast_string_field_set(p, context, default_context);
13569
13570 if (ast_shutting_down())
13571 transmit_response_with_allow(p, "503 Unavailable", req, 0);
13572 else if (res < 0)
13573 transmit_response_with_allow(p, "404 Not Found", req, 0);
13574 else
13575 transmit_response_with_allow(p, "200 OK", req, 0);
13576
13577
13578
13579 if (!p->lastinvite)
13580 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13581
13582 return res;
13583 }
13584
13585
13586
13587 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin)
13588 {
13589 struct ast_frame *f;
13590 int earlyreplace = 0;
13591 int oneleggedreplace = 0;
13592 struct ast_channel *c = p->owner;
13593 struct ast_channel *replacecall = p->refer->refer_call->owner;
13594 struct ast_channel *targetcall;
13595
13596
13597 if (replacecall->_state == AST_STATE_RING)
13598 earlyreplace = 1;
13599
13600
13601 if (!(targetcall = ast_bridged_channel(replacecall))) {
13602
13603 if (!earlyreplace) {
13604 if (option_debug > 1)
13605 ast_log(LOG_DEBUG, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
13606 oneleggedreplace = 1;
13607 }
13608 }
13609 if (option_debug > 3 && targetcall && targetcall->_state == AST_STATE_RINGING)
13610 ast_log(LOG_DEBUG, "SIP transfer: Target channel is in ringing state\n");
13611
13612 if (option_debug > 3) {
13613 if (targetcall)
13614 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);
13615 else
13616 ast_log(LOG_DEBUG, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
13617 }
13618
13619 if (ignore) {
13620 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
13621
13622
13623
13624 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
13625
13626 ast_channel_unlock(c);
13627 ast_mutex_unlock(&p->refer->refer_call->lock);
13628 return 1;
13629 }
13630 if (!c) {
13631
13632 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
13633 transmit_response_reliable(p, "503 Service Unavailable", req);
13634 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
13635 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13636 ast_mutex_unlock(&p->refer->refer_call->lock);
13637 return 1;
13638 }
13639 append_history(p, "Xfer", "INVITE/Replace received");
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651 transmit_response(p, "100 Trying", req);
13652 ast_setstate(c, AST_STATE_RING);
13653
13654
13655
13656
13657
13658 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
13659
13660 ast_setstate(c, AST_STATE_UP);
13661
13662
13663 ast_quiet_chan(replacecall);
13664 ast_quiet_chan(targetcall);
13665 if (option_debug > 3)
13666 ast_log(LOG_DEBUG, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
13667
13668 if (!oneleggedreplace)
13669 ast_channel_unlock(c);
13670
13671
13672 ast_mutex_unlock(&p->refer->refer_call->lock);
13673
13674
13675 if (! earlyreplace && ! oneleggedreplace )
13676 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
13677
13678
13679 if(ast_channel_masquerade(replacecall, c))
13680 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
13681 else if (option_debug > 3)
13682 ast_log(LOG_DEBUG, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
13683
13684
13685
13686
13687
13688 ast_channel_unlock(c);
13689
13690 if (earlyreplace || oneleggedreplace ) {
13691
13692 if ((f = ast_read(replacecall))) {
13693 ast_frfree(f);
13694 f = NULL;
13695 if (option_debug > 3)
13696 ast_log(LOG_DEBUG, "Invite/Replace: Could successfully read frame from RING channel!\n");
13697 } else {
13698 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from RING channel \n");
13699 }
13700 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13701 if (!oneleggedreplace)
13702 ast_channel_unlock(replacecall);
13703 } else {
13704 if ((f = ast_read(replacecall))) {
13705
13706 ast_frfree(f);
13707 f = NULL;
13708 if (option_debug > 2)
13709 ast_log(LOG_DEBUG, "Invite/Replace: Could successfully read frame from channel! Masq done.\n");
13710 } else {
13711 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from channel. Transfer failed\n");
13712 }
13713 ast_channel_unlock(replacecall);
13714 }
13715 ast_mutex_unlock(&p->refer->refer_call->lock);
13716
13717 ast_setstate(c, AST_STATE_DOWN);
13718 if (option_debug > 3) {
13719 struct ast_channel *test;
13720 ast_log(LOG_DEBUG, "After transfer:----------------------------\n");
13721 ast_log(LOG_DEBUG, " -- C: %s State %s\n", c->name, ast_state2str(c->_state));
13722 if (replacecall)
13723 ast_log(LOG_DEBUG, " -- replacecall: %s State %s\n", replacecall->name, ast_state2str(replacecall->_state));
13724 if (p->owner) {
13725 ast_log(LOG_DEBUG, " -- P->owner: %s State %s\n", p->owner->name, ast_state2str(p->owner->_state));
13726 test = ast_bridged_channel(p->owner);
13727 if (test)
13728 ast_log(LOG_DEBUG, " -- Call bridged to P->owner: %s State %s\n", test->name, ast_state2str(test->_state));
13729 else
13730 ast_log(LOG_DEBUG, " -- No call bridged to C->owner \n");
13731 } else
13732 ast_log(LOG_DEBUG, " -- No channel yet \n");
13733 ast_log(LOG_DEBUG, "End After transfer:----------------------------\n");
13734 }
13735
13736 ast_channel_unlock(p->owner);
13737 if (!oneleggedreplace)
13738 ast_mutex_unlock(&p->lock);
13739
13740
13741 c->tech_pvt = NULL;
13742 ast_hangup(c);
13743 return 0;
13744 }
13745
13746
13747
13748
13749
13750
13751
13752
13753 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)
13754 {
13755 int res = 1;
13756 int gotdest;
13757 const char *p_replaces;
13758 char *replace_id = NULL;
13759 const char *required;
13760 unsigned int required_profile = 0;
13761 struct ast_channel *c = NULL;
13762 int reinvite = 0;
13763
13764
13765 if (!p->sipoptions) {
13766 const char *supported = get_header(req, "Supported");
13767 if (!ast_strlen_zero(supported))
13768 parse_sip_options(p, supported);
13769 }
13770
13771
13772 required = get_header(req, "Require");
13773 if (!ast_strlen_zero(required)) {
13774 required_profile = parse_sip_options(NULL, required);
13775 if (required_profile && required_profile != SIP_OPT_REPLACES) {
13776
13777 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
13778 ast_log(LOG_WARNING,"Received SIP INVITE with unsupported required extension: %s\n", required);
13779 p->invitestate = INV_COMPLETED;
13780 if (!p->lastinvite)
13781 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13782 return -1;
13783 }
13784 }
13785
13786
13787 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->owner->_state != AST_STATE_UP)) {
13788
13789
13790
13791
13792
13793 transmit_response(p, "482 Loop Detected", req);
13794 p->invitestate = INV_COMPLETED;
13795 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13796 return 0;
13797 }
13798
13799 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->pendinginvite) {
13800
13801 transmit_response(p, "491 Request Pending", req);
13802 if (option_debug)
13803 ast_log(LOG_DEBUG, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
13804
13805 return 0;
13806 }
13807
13808 p_replaces = get_header(req, "Replaces");
13809 if (!ast_strlen_zero(p_replaces)) {
13810
13811 char *ptr;
13812 char *fromtag = NULL;
13813 char *totag = NULL;
13814 char *start, *to;
13815 int error = 0;
13816
13817 if (p->owner) {
13818 if (option_debug > 2)
13819 ast_log(LOG_DEBUG, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
13820 transmit_response(p, "400 Bad request", req);
13821
13822 return -1;
13823 }
13824
13825 if (sipdebug && option_debug > 2)
13826 ast_log(LOG_DEBUG, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
13827
13828 replace_id = ast_strdupa(p_replaces);
13829 ast_uri_decode(replace_id);
13830
13831 if (!p->refer && !sip_refer_allocate(p)) {
13832 transmit_response(p, "500 Server Internal Error", req);
13833 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
13834 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13835 p->invitestate = INV_COMPLETED;
13836 return -1;
13837 }
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848 replace_id = ast_skip_blanks(replace_id);
13849
13850 start = replace_id;
13851 while ( (ptr = strsep(&start, ";")) ) {
13852 ptr = ast_skip_blanks(ptr);
13853 if ( (to = strcasestr(ptr, "to-tag=") ) )
13854 totag = to + 7;
13855 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
13856 fromtag = to + 9;
13857 fromtag = strsep(&fromtag, "&");
13858 }
13859 }
13860
13861 if (sipdebug && option_debug > 3)
13862 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>");
13863
13864
13865
13866
13867
13868 if ((p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
13869 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
13870 transmit_response(p, "481 Call Leg Does Not Exist (Replaces)", req);
13871 error = 1;
13872 }
13873
13874
13875
13876
13877
13878
13879
13880 if (p->refer->refer_call == p) {
13881 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
13882 p->refer->refer_call = NULL;
13883 transmit_response(p, "400 Bad request", req);
13884 error = 1;
13885 }
13886
13887 if (!error && !p->refer->refer_call->owner) {
13888
13889 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
13890
13891 transmit_response(p, "481 Call Leg Does Not Exist (Replace)", req);
13892 error = 1;
13893 }
13894
13895 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 ) {
13896 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
13897 transmit_response(p, "603 Declined (Replaces)", req);
13898 error = 1;
13899 }
13900
13901 if (error) {
13902 append_history(p, "Xfer", "INVITE/Replace Failed.");
13903 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13904 ast_mutex_unlock(&p->lock);
13905 if (p->refer->refer_call) {
13906 ast_mutex_unlock(&p->refer->refer_call->lock);
13907 ast_channel_unlock(p->refer->refer_call->owner);
13908 }
13909 p->invitestate = INV_COMPLETED;
13910 return -1;
13911 }
13912 }
13913
13914
13915
13916
13917
13918 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13919 int newcall = (p->initreq.headers ? TRUE : FALSE);
13920
13921 if (sip_cancel_destroy(p))
13922 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
13923
13924 p->pendinginvite = seqno;
13925 check_via(p, req);
13926
13927 copy_request(&p->initreq, req);
13928 if (!p->owner) {
13929 if (debug)
13930 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
13931 if (newcall)
13932 append_history(p, "Invite", "New call: %s", p->callid);
13933 parse_ok_contact(p, req);
13934 } else {
13935 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
13936
13937 if (find_sdp(req)) {
13938 if (process_sdp(p, req)) {
13939 transmit_response(p, "488 Not acceptable here", req);
13940 if (!p->lastinvite)
13941 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13942 return -1;
13943 }
13944 ast_queue_control(p->owner, AST_CONTROL_SRCCHANGE);
13945 } else {
13946 p->jointcapability = p->capability;
13947 if (option_debug > 2)
13948 ast_log(LOG_DEBUG, "Hm.... No sdp for the moment\n");
13949
13950
13951
13952 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
13953 change_hold_state(p, req, FALSE, 0);
13954 }
13955 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
13956 append_history(p, "ReInv", "Re-invite received");
13957 }
13958 } else if (debug)
13959 ast_verbose("Ignoring this INVITE request\n");
13960
13961
13962 if (!p->lastinvite && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner) {
13963
13964
13965 res = check_user(p, req, SIP_INVITE, e, XMIT_RELIABLE, sin);
13966 if (res == AUTH_CHALLENGE_SENT) {
13967 p->invitestate = INV_COMPLETED;
13968 return 0;
13969 }
13970 if (res < 0) {
13971 if (res == AUTH_FAKE_AUTH) {
13972 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
13973 transmit_fake_auth_response(p, req, 1);
13974 } else {
13975 ast_log(LOG_NOTICE, "Failed to authenticate user %s\n", get_header(req, "From"));
13976 transmit_response_reliable(p, "403 Forbidden", req);
13977 }
13978 p->invitestate = INV_COMPLETED;
13979 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13980 ast_string_field_free(p, theirtag);
13981 return 0;
13982 }
13983
13984
13985 if (find_sdp(req)) {
13986 if (process_sdp(p, req)) {
13987
13988 transmit_response_reliable(p, "488 Not acceptable here", req);
13989 p->invitestate = INV_COMPLETED;
13990 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13991 if (option_debug)
13992 ast_log(LOG_DEBUG, "No compatible codecs for this SIP call.\n");
13993 return -1;
13994 }
13995 } else {
13996 p->jointcapability = p->capability;
13997 if (option_debug > 1)
13998 ast_log(LOG_DEBUG, "No SDP in Invite, third party call control\n");
13999 }
14000
14001
14002
14003 if (p->owner)
14004 ast_queue_frame(p->owner, &ast_null_frame);
14005
14006
14007
14008 if (ast_strlen_zero(p->context))
14009 ast_string_field_set(p, context, default_context);
14010
14011
14012
14013 if (option_debug)
14014 ast_log(LOG_DEBUG, "Checking SIP call limits for device %s\n", p->username);
14015 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
14016 if (res < 0) {
14017 ast_log(LOG_NOTICE, "Failed to place call for user %s, too many calls\n", p->username);
14018 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
14019 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14020 p->invitestate = INV_COMPLETED;
14021 }
14022 return 0;
14023 }
14024 gotdest = get_destination(p, NULL);
14025 get_rdnis(p, NULL);
14026 extract_uri(p, req);
14027 build_contact(p);
14028
14029 if (p->rtp) {
14030 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
14031 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
14032 }
14033
14034 if (!replace_id && gotdest) {
14035 if (gotdest == 1 && ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP))
14036 transmit_response_reliable(p, "484 Address Incomplete", req);
14037 else {
14038 transmit_response_reliable(p, "404 Not Found", req);
14039 ast_log(LOG_NOTICE, "Call from '%s' to extension"
14040 " '%s' rejected because extension not found.\n",
14041 S_OR(p->username, p->peername), p->exten);
14042 }
14043 p->invitestate = INV_COMPLETED;
14044 update_call_counter(p, DEC_CALL_LIMIT);
14045 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14046 return 0;
14047 } else {
14048
14049
14050 if (ast_strlen_zero(p->exten))
14051 ast_string_field_set(p, exten, "s");
14052
14053
14054 make_our_tag(p->tag, sizeof(p->tag));
14055
14056 c = sip_new(p, AST_STATE_DOWN, S_OR(p->username, NULL));
14057 *recount = 1;
14058
14059
14060 build_route(p, req, 0);
14061
14062 if (c) {
14063
14064 ast_channel_lock(c);
14065 }
14066 }
14067 } else {
14068 if (option_debug > 1 && sipdebug) {
14069 if (!ast_test_flag(req, SIP_PKT_IGNORE))
14070 ast_log(LOG_DEBUG, "Got a SIP re-invite for call %s\n", p->callid);
14071 else
14072 ast_log(LOG_DEBUG, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
14073 }
14074 reinvite = 1;
14075 c = p->owner;
14076 }
14077
14078 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
14079 p->lastinvite = seqno;
14080
14081 if (replace_id) {
14082
14083 if (sipdebug && option_debug > 3)
14084 ast_log(LOG_DEBUG, "Sending this call to the invite/replcaes handler %s\n", p->callid);
14085 return handle_invite_replaces(p, req, debug, ast_test_flag(req, SIP_PKT_IGNORE), seqno, sin);
14086 }
14087
14088
14089 if (c) {
14090 switch(c->_state) {
14091 case AST_STATE_DOWN:
14092 if (option_debug > 1)
14093 ast_log(LOG_DEBUG, "%s: New call is still down.... Trying... \n", c->name);
14094 transmit_response(p, "100 Trying", req);
14095 p->invitestate = INV_PROCEEDING;
14096 ast_setstate(c, AST_STATE_RING);
14097 if (strcmp(p->exten, ast_pickup_ext())) {
14098 enum ast_pbx_result res;
14099
14100 res = ast_pbx_start(c);
14101
14102 switch(res) {
14103 case AST_PBX_FAILED:
14104 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
14105 p->invitestate = INV_COMPLETED;
14106 if (ast_test_flag(req, SIP_PKT_IGNORE))
14107 transmit_response(p, "503 Unavailable", req);
14108 else
14109 transmit_response_reliable(p, "503 Unavailable", req);
14110 break;
14111 case AST_PBX_CALL_LIMIT:
14112 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
14113 p->invitestate = INV_COMPLETED;
14114 if (ast_test_flag(req, SIP_PKT_IGNORE))
14115 transmit_response(p, "480 Temporarily Unavailable", req);
14116 else
14117 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
14118 break;
14119 case AST_PBX_SUCCESS:
14120
14121 break;
14122 }
14123
14124 if (res) {
14125
14126
14127 ast_mutex_unlock(&c->lock);
14128 ast_mutex_unlock(&p->lock);
14129 ast_hangup(c);
14130 ast_mutex_lock(&p->lock);
14131 c = NULL;
14132 }
14133 } else {
14134 ast_channel_unlock(c);
14135 *nounlock = 1;
14136 if (ast_pickup_call(c)) {
14137 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
14138 if (ast_test_flag(req, SIP_PKT_IGNORE))
14139 transmit_response(p, "503 Unavailable", req);
14140 else
14141 transmit_response_reliable(p, "503 Unavailable", req);
14142 sip_alreadygone(p);
14143
14144 ast_mutex_unlock(&p->lock);
14145 c->hangupcause = AST_CAUSE_CALL_REJECTED;
14146 } else {
14147 ast_mutex_unlock(&p->lock);
14148 ast_setstate(c, AST_STATE_DOWN);
14149 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
14150 }
14151 p->invitestate = INV_COMPLETED;
14152 ast_hangup(c);
14153 ast_mutex_lock(&p->lock);
14154 c = NULL;
14155 }
14156 break;
14157 case AST_STATE_RING:
14158 transmit_response(p, "100 Trying", req);
14159 p->invitestate = INV_PROCEEDING;
14160 break;
14161 case AST_STATE_RINGING:
14162 transmit_response(p, "180 Ringing", req);
14163 p->invitestate = INV_PROCEEDING;
14164 break;
14165 case AST_STATE_UP:
14166 if (option_debug > 1)
14167 ast_log(LOG_DEBUG, "%s: This call is UP.... \n", c->name);
14168
14169 transmit_response(p, "100 Trying", req);
14170
14171 if (p->t38.state == T38_PEER_REINVITE) {
14172 struct ast_channel *bridgepeer = NULL;
14173 struct sip_pvt *bridgepvt = NULL;
14174
14175 if ((bridgepeer = ast_bridged_channel(p->owner))) {
14176
14177
14178 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
14179 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
14180 if (bridgepvt->t38.state == T38_DISABLED) {
14181 if (bridgepvt->udptl) {
14182
14183 sip_handle_t38_reinvite(bridgepeer, p, 1);
14184 } else {
14185 ast_log(LOG_WARNING, "Strange... The other side of the bridge don't have udptl struct\n");
14186 ast_mutex_lock(&bridgepvt->lock);
14187 bridgepvt->t38.state = T38_DISABLED;
14188 ast_mutex_unlock(&bridgepvt->lock);
14189 if (option_debug > 1)
14190 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->name);
14191 if (ast_test_flag(req, SIP_PKT_IGNORE))
14192 transmit_response(p, "488 Not acceptable here", req);
14193 else
14194 transmit_response_reliable(p, "488 Not acceptable here", req);
14195
14196 }
14197 } else {
14198
14199 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
14200 p->t38.state = T38_ENABLED;
14201 if (option_debug)
14202 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
14203 }
14204 } else {
14205
14206 if (ast_test_flag(req, SIP_PKT_IGNORE))
14207 transmit_response(p, "488 Not acceptable here", req);
14208 else
14209 transmit_response_reliable(p, "488 Not acceptable here", req);
14210 p->t38.state = T38_DISABLED;
14211 if (option_debug > 1)
14212 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
14213
14214 if (!p->lastinvite)
14215 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14216 }
14217 } else {
14218
14219 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
14220 p->t38.state = T38_ENABLED;
14221 if (option_debug)
14222 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
14223 }
14224 } else if (p->t38.state == T38_DISABLED) {
14225 int sendok = TRUE;
14226
14227
14228
14229 struct ast_channel *bridgepeer = NULL;
14230 struct sip_pvt *bridgepvt = NULL;
14231 if ((bridgepeer = ast_bridged_channel(p->owner))) {
14232 if ((bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) && !ast_check_hangup(bridgepeer)) {
14233 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
14234
14235 if (bridgepvt->t38.state == T38_ENABLED) {
14236 ast_log(LOG_WARNING, "RTP re-invite after T38 session not handled yet !\n");
14237
14238 if (ast_test_flag(req, SIP_PKT_IGNORE))
14239 transmit_response(p, "488 Not Acceptable Here (unsupported)", req);
14240 else
14241 transmit_response_reliable(p, "488 Not Acceptable Here (unsupported)", req);
14242 sendok = FALSE;
14243 }
14244
14245 }
14246 }
14247
14248 if (sendok)
14249
14250 transmit_response_with_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (ast_test_flag(req, SIP_PKT_IGNORE) ? XMIT_UNRELIABLE : XMIT_CRITICAL)));
14251 }
14252 p->invitestate = INV_TERMINATED;
14253 break;
14254 default:
14255 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
14256 transmit_response(p, "100 Trying", req);
14257 break;
14258 }
14259 } else {
14260 if (p && (p->autokillid == -1)) {
14261 const char *msg;
14262
14263 if (!p->jointcapability)
14264 msg = "488 Not Acceptable Here (codec error)";
14265 else {
14266 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
14267 msg = "503 Unavailable";
14268 }
14269 if (ast_test_flag(req, SIP_PKT_IGNORE))
14270 transmit_response(p, msg, req);
14271 else
14272 transmit_response_reliable(p, msg, req);
14273 p->invitestate = INV_COMPLETED;
14274 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14275 }
14276 }
14277 return res;
14278 }
14279
14280
14281
14282 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno)
14283 {
14284 struct sip_dual target;
14285
14286 int res = 0;
14287 struct sip_pvt *targetcall_pvt;
14288
14289
14290 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
14291 transferer->refer->replaces_callid_fromtag))) {
14292 if (transferer->refer->localtransfer) {
14293
14294 transmit_response(transferer, "202 Accepted", req);
14295
14296
14297 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
14298 append_history(transferer, "Xfer", "Refer failed");
14299 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
14300 transferer->refer->status = REFER_FAILED;
14301 return -1;
14302 }
14303
14304 if (option_debug > 2)
14305 ast_log(LOG_DEBUG, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
14306 return 0;
14307 }
14308
14309
14310 transmit_response(transferer, "202 Accepted", req);
14311 append_history(transferer, "Xfer", "Refer accepted");
14312 if (!targetcall_pvt->owner) {
14313 if (option_debug > 3)
14314 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No owner of target call\n");
14315
14316 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
14317 append_history(transferer, "Xfer", "Refer failed");
14318 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
14319 transferer->refer->status = REFER_FAILED;
14320 ast_mutex_unlock(&targetcall_pvt->lock);
14321 ast_channel_unlock(current->chan1);
14322 return -1;
14323 }
14324
14325
14326 target.chan1 = targetcall_pvt->owner;
14327 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
14328
14329 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
14330
14331 if (option_debug > 3) {
14332 if (target.chan2)
14333 ast_log(LOG_DEBUG, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
14334 else if (target.chan1->_state != AST_STATE_RING)
14335 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No target channel\n");
14336 else
14337 ast_log(LOG_DEBUG, "SIP attended transfer: Attempting transfer in ringing state\n");
14338 }
14339 }
14340
14341
14342 if (option_debug > 3 && sipdebug) {
14343 if (current->chan2)
14344 ast_log(LOG_DEBUG, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
14345 else
14346 ast_log(LOG_DEBUG, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
14347 }
14348
14349 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14350
14351
14352 res = attempt_transfer(current, &target);
14353 ast_mutex_unlock(&targetcall_pvt->lock);
14354 if (res) {
14355
14356 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
14357 append_history(transferer, "Xfer", "Refer failed");
14358 transferer->refer->status = REFER_FAILED;
14359 if (targetcall_pvt->owner)
14360 ast_channel_unlock(targetcall_pvt->owner);
14361
14362 if (res != -2)
14363 ast_hangup(transferer->owner);
14364 else
14365 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14366 } else {
14367
14368
14369
14370 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
14371 append_history(transferer, "Xfer", "Refer succeeded");
14372 transferer->refer->status = REFER_200OK;
14373 if (targetcall_pvt->owner) {
14374 if (option_debug)
14375 ast_log(LOG_DEBUG, "SIP attended transfer: Unlocking channel %s\n", targetcall_pvt->owner->name);
14376 ast_channel_unlock(targetcall_pvt->owner);
14377 }
14378 }
14379 return 1;
14380 }
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock)
14447 {
14448 struct sip_dual current;
14449
14450
14451 int res = 0;
14452
14453 if (ast_test_flag(req, SIP_PKT_DEBUG))
14454 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");
14455
14456 if (!p->owner) {
14457
14458
14459 if (option_debug > 2)
14460 ast_log(LOG_DEBUG, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
14461 transmit_response(p, "603 Declined (No dialog)", req);
14462 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
14463 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
14464 sip_alreadygone(p);
14465 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14466 }
14467 return 0;
14468 }
14469
14470
14471
14472 if (p->allowtransfer == TRANSFER_CLOSED ) {
14473
14474 transmit_response(p, "603 Declined (policy)", req);
14475 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
14476
14477 return 0;
14478 }
14479
14480 if(!ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
14481
14482 transmit_response(p, "491 Request pending", req);
14483 append_history(p, "Xfer", "Refer failed. Request pending.");
14484 return 0;
14485 }
14486
14487
14488 if (!p->refer && !sip_refer_allocate(p)) {
14489 transmit_response(p, "500 Internal Server Error", req);
14490 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
14491 return -3;
14492 }
14493
14494 res = get_refer_info(p, req);
14495
14496 p->refer->status = REFER_SENT;
14497
14498 if (res != 0) {
14499 switch (res) {
14500 case -2:
14501 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
14502 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
14503 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
14504 ast_log(LOG_DEBUG, "SIP transfer to black hole can't be handled (no refer-to: )\n");
14505 break;
14506 case -3:
14507 transmit_response(p, "603 Declined (Non sip: uri)", req);
14508 append_history(p, "Xfer", "Refer failed. Non SIP uri");
14509 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
14510 ast_log(LOG_DEBUG, "SIP transfer to non-SIP uri denied\n");
14511 break;
14512 default:
14513
14514 transmit_response(p, "202 Accepted", req);
14515 append_history(p, "Xfer", "Refer failed. Bad extension.");
14516 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
14517 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14518 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
14519 ast_log(LOG_DEBUG, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
14520 break;
14521 }
14522 return 0;
14523 }
14524 if (ast_strlen_zero(p->context))
14525 ast_string_field_set(p, context, default_context);
14526
14527
14528 if (allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
14529 p->refer->localtransfer = 1;
14530 if (sipdebug && option_debug > 2)
14531 ast_log(LOG_DEBUG, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
14532 } else if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
14533
14534 p->refer->localtransfer = 1;
14535 } else if (sipdebug && option_debug > 2)
14536 ast_log(LOG_DEBUG, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
14537
14538
14539
14540 if (ignore)
14541 return res;
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569 current.chan1 = p->owner;
14570
14571
14572 current.chan2 = ast_bridged_channel(current.chan1);
14573
14574 if (sipdebug && option_debug > 2)
14575 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>");
14576
14577 if (!current.chan2 && !p->refer->attendedtransfer) {
14578
14579
14580
14581 if (sipdebug && option_debug > 2)
14582 ast_log(LOG_DEBUG,"Refused SIP transfer on non-bridged channel.\n");
14583 p->refer->status = REFER_FAILED;
14584 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
14585 transmit_response(p, "603 Declined", req);
14586 return -1;
14587 }
14588
14589 if (current.chan2) {
14590 if (sipdebug && option_debug > 3)
14591 ast_log(LOG_DEBUG, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
14592
14593 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
14594 }
14595
14596 ast_set_flag(&p->flags[0], SIP_GOTREFER);
14597
14598
14599 if (p->refer->attendedtransfer) {
14600 if ((res = local_attended_transfer(p, ¤t, req, seqno)))
14601 return res;
14602
14603 if (sipdebug && option_debug > 3)
14604 ast_log(LOG_DEBUG, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
14605
14606 }
14607
14608
14609
14610 if (p->refer->localtransfer && !strcmp(p->refer->refer_to, ast_parking_ext())) {
14611
14612 *nounlock = 1;
14613 ast_channel_unlock(current.chan1);
14614 copy_request(¤t.req, req);
14615 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14616 p->refer->status = REFER_200OK;
14617 append_history(p, "Xfer", "REFER to call parking.");
14618 if (sipdebug && option_debug > 3)
14619 ast_log(LOG_DEBUG, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
14620 sip_park(current.chan2, current.chan1, req, seqno);
14621 return res;
14622 }
14623
14624
14625 transmit_response(p, "202 Accepted", req);
14626
14627 if (current.chan1 && current.chan2) {
14628 if (option_debug > 2)
14629 ast_log(LOG_DEBUG, "chan1->name: %s\n", current.chan1->name);
14630 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
14631 }
14632 if (current.chan2) {
14633 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
14634 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
14635 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
14636
14637 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
14638
14639 if (p->refer->referred_by)
14640 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
14641 }
14642
14643 if (p->refer->replaces_callid && !ast_strlen_zero(p->refer->replaces_callid)) {
14644 char tempheader[SIPBUFSIZE];
14645 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
14646 p->refer->replaces_callid_totag ? ";to-tag=" : "",
14647 p->refer->replaces_callid_totag,
14648 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
14649 p->refer->replaces_callid_fromtag);
14650 if (current.chan2)
14651 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
14652 }
14653
14654
14655 *nounlock = 1;
14656 ast_channel_unlock(current.chan1);
14657
14658
14659
14660
14661 if (!p->refer->attendedtransfer)
14662 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
14663
14664
14665
14666
14667
14668 if (!current.chan2) {
14669
14670
14671
14672
14673
14674
14675
14676 p->refer->status = REFER_FAILED;
14677 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
14678 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14679 append_history(p, "Xfer", "Refer failed (only bridged calls).");
14680 return -1;
14681 }
14682 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14683
14684
14685
14686 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
14687
14688 if (!res) {
14689
14690 if (option_debug > 2)
14691 ast_log(LOG_DEBUG, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
14692 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
14693 if (p->refer->localtransfer)
14694 p->refer->status = REFER_200OK;
14695 if (p->owner)
14696 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
14697 append_history(p, "Xfer", "Refer succeeded.");
14698 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14699
14700
14701 res = 0;
14702 } else {
14703 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14704 if (option_debug > 2)
14705 ast_log(LOG_DEBUG, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
14706 append_history(p, "Xfer", "Refer failed.");
14707
14708 p->refer->status = REFER_FAILED;
14709 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
14710 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14711 res = -1;
14712 }
14713 return res;
14714 }
14715
14716
14717 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
14718 {
14719
14720 check_via(p, req);
14721 sip_alreadygone(p);
14722
14723
14724
14725
14726
14727
14728 if (p->invitestate == INV_TERMINATED)
14729 __sip_pretend_ack(p);
14730 else
14731 p->invitestate = INV_CANCELLED;
14732
14733 if (p->owner && p->owner->_state == AST_STATE_UP) {
14734
14735 transmit_response(p, "200 OK", req);
14736 if (option_debug)
14737 ast_log(LOG_DEBUG, "Got CANCEL on an answered call. Ignoring... \n");
14738 return 0;
14739 }
14740
14741 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
14742 update_call_counter(p, DEC_CALL_LIMIT);
14743
14744 stop_media_flows(p);
14745 if (p->owner)
14746 ast_queue_hangup(p->owner);
14747 else
14748 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14749 if (p->initreq.len > 0) {
14750 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
14751 transmit_response(p, "200 OK", req);
14752 return 1;
14753 } else {
14754 transmit_response(p, "481 Call Leg Does Not Exist", req);
14755 return 0;
14756 }
14757 }
14758
14759 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen)
14760 {
14761 struct ast_rtp_quality qos;
14762 struct sip_pvt *p = chan->tech_pvt;
14763 char *all = "", *parse = ast_strdupa(preparse);
14764 AST_DECLARE_APP_ARGS(args,
14765 AST_APP_ARG(param);
14766 AST_APP_ARG(type);
14767 AST_APP_ARG(field);
14768 );
14769 AST_STANDARD_APP_ARGS(args, parse);
14770
14771
14772 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
14773 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
14774 return 0;
14775 }
14776
14777 if (strcasecmp(args.param, "rtpqos"))
14778 return 0;
14779
14780
14781 if (ast_strlen_zero(args.type))
14782 args.type = "audio";
14783 if (ast_strlen_zero(args.field))
14784 args.field = "all";
14785
14786 memset(buf, 0, buflen);
14787 memset(&qos, 0, sizeof(qos));
14788
14789 if (strcasecmp(args.type, "AUDIO") == 0) {
14790 all = ast_rtp_get_quality(p->rtp, &qos);
14791 } else if (strcasecmp(args.type, "VIDEO") == 0) {
14792 all = ast_rtp_get_quality(p->vrtp, &qos);
14793 }
14794
14795 if (strcasecmp(args.field, "local_ssrc") == 0)
14796 snprintf(buf, buflen, "%u", qos.local_ssrc);
14797 else if (strcasecmp(args.field, "local_lostpackets") == 0)
14798 snprintf(buf, buflen, "%u", qos.local_lostpackets);
14799 else if (strcasecmp(args.field, "local_jitter") == 0)
14800 snprintf(buf, buflen, "%.0lf", qos.local_jitter * 1000.0);
14801 else if (strcasecmp(args.field, "local_count") == 0)
14802 snprintf(buf, buflen, "%u", qos.local_count);
14803 else if (strcasecmp(args.field, "remote_ssrc") == 0)
14804 snprintf(buf, buflen, "%u", qos.remote_ssrc);
14805 else if (strcasecmp(args.field, "remote_lostpackets") == 0)
14806 snprintf(buf, buflen, "%u", qos.remote_lostpackets);
14807 else if (strcasecmp(args.field, "remote_jitter") == 0)
14808 snprintf(buf, buflen, "%.0lf", qos.remote_jitter * 1000.0);
14809 else if (strcasecmp(args.field, "remote_count") == 0)
14810 snprintf(buf, buflen, "%u", qos.remote_count);
14811 else if (strcasecmp(args.field, "rtt") == 0)
14812 snprintf(buf, buflen, "%.0lf", qos.rtt * 1000.0);
14813 else if (strcasecmp(args.field, "all") == 0)
14814 ast_copy_string(buf, all, buflen);
14815 else {
14816 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
14817 return -1;
14818 }
14819 return 0;
14820 }
14821
14822
14823 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
14824 {
14825 struct ast_channel *c=NULL;
14826 int res;
14827 struct ast_channel *bridged_to;
14828
14829
14830 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner)
14831 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
14832
14833 p->invitestate = INV_TERMINATED;
14834
14835 copy_request(&p->initreq, req);
14836 check_via(p, req);
14837 sip_alreadygone(p);
14838
14839
14840 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY) || p->owner) {
14841 char *audioqos, *videoqos;
14842 if (p->rtp) {
14843 audioqos = ast_rtp_get_quality(p->rtp, NULL);
14844 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
14845 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
14846 if (p->owner)
14847 pbx_builtin_setvar_helper(p->owner, "RTPAUDIOQOS", audioqos);
14848 }
14849 if (p->vrtp) {
14850 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
14851 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
14852 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
14853 if (p->owner)
14854 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", videoqos);
14855 }
14856 }
14857
14858 stop_media_flows(p);
14859
14860 if (!ast_strlen_zero(get_header(req, "Also"))) {
14861 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
14862 ast_inet_ntoa(p->recv.sin_addr));
14863 if (ast_strlen_zero(p->context))
14864 ast_string_field_set(p, context, default_context);
14865 res = get_also_info(p, req);
14866 if (!res) {
14867 c = p->owner;
14868 if (c) {
14869 bridged_to = ast_bridged_channel(c);
14870 if (bridged_to) {
14871
14872 ast_queue_control(c, AST_CONTROL_UNHOLD);
14873 ast_async_goto(bridged_to, p->context, p->refer->refer_to,1);
14874 } else
14875 ast_queue_hangup(p->owner);
14876 }
14877 } else {
14878 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(p->recv.sin_addr));
14879 if (p->owner)
14880 ast_queue_hangup(p->owner);
14881 }
14882 } else if (p->owner) {
14883 ast_queue_hangup(p->owner);
14884 if (option_debug > 2)
14885 ast_log(LOG_DEBUG, "Received bye, issuing owner hangup\n");
14886 } else {
14887 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14888 if (option_debug > 2)
14889 ast_log(LOG_DEBUG, "Received bye, no owner, selfdestruct soon.\n");
14890 }
14891 transmit_response(p, "200 OK", req);
14892
14893 return 1;
14894 }
14895
14896
14897 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
14898 {
14899 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
14900 if (ast_test_flag(req, SIP_PKT_DEBUG))
14901 ast_verbose("Receiving message!\n");
14902 receive_message(p, req);
14903 } else
14904 transmit_response(p, "202 Accepted", req);
14905 return 1;
14906 }
14907
14908
14909 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
14910 {
14911 int gotdest;
14912 int res = 0;
14913 int firststate = AST_EXTENSION_REMOVED;
14914 struct sip_peer *authpeer = NULL;
14915 const char *eventheader = get_header(req, "Event");
14916 const char *accept = get_header(req, "Accept");
14917 int resubscribe = (p->subscribed != NONE);
14918 char *temp, *event;
14919
14920 if (p->initreq.headers) {
14921
14922 if (p->initreq.method != SIP_SUBSCRIBE) {
14923
14924
14925 transmit_response(p, "403 Forbidden (within dialog)", req);
14926
14927 if (option_debug)
14928 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);
14929 return 0;
14930 } else if (ast_test_flag(req, SIP_PKT_DEBUG)) {
14931 if (option_debug) {
14932 if (resubscribe)
14933 ast_log(LOG_DEBUG, "Got a re-subscribe on existing subscription %s\n", p->callid);
14934 else
14935 ast_log(LOG_DEBUG, "Got a new subscription %s (possibly with auth)\n", p->callid);
14936 }
14937 }
14938 }
14939
14940
14941
14942
14943 if (!global_allowsubscribe) {
14944 transmit_response(p, "403 Forbidden (policy)", req);
14945 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14946 return 0;
14947 }
14948
14949 if (!ast_test_flag(req, SIP_PKT_IGNORE) && !resubscribe) {
14950 const char *to = get_header(req, "To");
14951 char totag[128];
14952
14953
14954 if (!ast_strlen_zero(to) && gettag(req, "To", totag, sizeof(totag))) {
14955 if (ast_test_flag(req, SIP_PKT_DEBUG))
14956 ast_verbose("Received resubscription for a dialog we no longer know about. Telling remote side to subscribe again.\n");
14957 transmit_response(p, "481 Subscription does not exist", req);
14958 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14959 return 0;
14960 }
14961
14962
14963 if (ast_test_flag(req, SIP_PKT_DEBUG))
14964 ast_verbose("Creating new subscription\n");
14965
14966 copy_request(&p->initreq, req);
14967 check_via(p, req);
14968 } else if (ast_test_flag(req, SIP_PKT_DEBUG) && ast_test_flag(req, SIP_PKT_IGNORE))
14969 ast_verbose("Ignoring this SUBSCRIBE request\n");
14970
14971
14972 if (ast_strlen_zero(eventheader)) {
14973 transmit_response(p, "489 Bad Event", req);
14974 if (option_debug > 1)
14975 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: <none>\n");
14976 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14977 return 0;
14978 }
14979
14980 if ( (strchr(eventheader, ';'))) {
14981 event = ast_strdupa(eventheader);
14982 temp = strchr(event, ';');
14983 *temp = '\0';
14984
14985 } else
14986 event = (char *) eventheader;
14987
14988
14989 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, &authpeer);
14990
14991 if (res == AUTH_CHALLENGE_SENT) {
14992 if (authpeer)
14993 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14994 return 0;
14995 }
14996 if (res < 0) {
14997 if (res == AUTH_FAKE_AUTH) {
14998 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
14999 transmit_fake_auth_response(p, req, 1);
15000 } else {
15001 ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
15002 transmit_response_reliable(p, "403 Forbidden", req);
15003 }
15004 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15005 if (authpeer)
15006 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
15007 return 0;
15008 }
15009
15010
15011 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
15012 transmit_response(p, "403 Forbidden (policy)", req);
15013 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15014 if (authpeer)
15015 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
15016 return 0;
15017 }
15018
15019
15020 gotdest = get_destination(p, NULL);
15021
15022
15023 parse_ok_contact(p, req);
15024
15025 build_contact(p);
15026 if (strcmp(event, "message-summary") && gotdest) {
15027 transmit_response(p, "404 Not Found", req);
15028 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15029 if (authpeer)
15030 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
15031 return 0;
15032 }
15033
15034
15035 if (ast_strlen_zero(p->tag))
15036 make_our_tag(p->tag, sizeof(p->tag));
15037
15038 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
15039 if (authpeer)
15040 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
15041
15042
15043
15044
15045
15046 if (strstr(p->useragent, "Polycom")) {
15047 p->subscribed = XPIDF_XML;
15048 } else if (strstr(accept, "application/pidf+xml")) {
15049 p->subscribed = PIDF_XML;
15050 } else if (strstr(accept, "application/dialog-info+xml")) {
15051 p->subscribed = DIALOG_INFO_XML;
15052
15053 } else if (strstr(accept, "application/cpim-pidf+xml")) {
15054 p->subscribed = CPIM_PIDF_XML;
15055 } else if (strstr(accept, "application/xpidf+xml")) {
15056 p->subscribed = XPIDF_XML;
15057 } else if (ast_strlen_zero(accept)) {
15058 if (p->subscribed == NONE) {
15059 transmit_response(p, "489 Bad Event", req);
15060
15061 ast_log(LOG_WARNING,"SUBSCRIBE failure: no Accept header: pvt: stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
15062 p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
15063 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15064 return 0;
15065 }
15066
15067
15068 } else {
15069
15070 char mybuf[200];
15071 snprintf(mybuf,sizeof(mybuf),"489 Bad Event (format %s)", accept);
15072 transmit_response(p, mybuf, req);
15073
15074 ast_log(LOG_WARNING,"SUBSCRIBE failure: unrecognized format: '%s' pvt: subscribed: %d, stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
15075 accept, (int)p->subscribed, p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
15076 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15077 return 0;
15078 }
15079 } else if (!strcmp(event, "message-summary")) {
15080 if (!ast_strlen_zero(accept) && strcmp(accept, "application/simple-message-summary")) {
15081
15082 transmit_response(p, "406 Not Acceptable", req);
15083 if (option_debug > 1)
15084 ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", accept);
15085 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15086 if (authpeer)
15087 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
15088 return 0;
15089 }
15090
15091
15092
15093
15094
15095 if (!authpeer || ast_strlen_zero(authpeer->mailbox)) {
15096 transmit_response(p, "404 Not found (no mailbox)", req);
15097 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15098 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
15099 if (authpeer)
15100 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
15101 return 0;
15102 }
15103
15104 p->subscribed = MWI_NOTIFICATION;
15105 if (authpeer->mwipvt && authpeer->mwipvt != p)
15106
15107 sip_destroy(authpeer->mwipvt);
15108 authpeer->mwipvt = p;
15109 p->relatedpeer = ASTOBJ_REF(authpeer);
15110 } else {
15111 transmit_response(p, "489 Bad Event", req);
15112 if (option_debug > 1)
15113 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
15114 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15115 if (authpeer)
15116 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
15117 return 0;
15118 }
15119
15120 if (p->subscribed != MWI_NOTIFICATION && !resubscribe) {
15121 if (p->stateid > -1)
15122 ast_extension_state_del(p->stateid, cb_extensionstate);
15123 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
15124 }
15125
15126 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
15127 p->lastinvite = seqno;
15128 if (p && !ast_test_flag(&p->flags[0], SIP_NEEDDESTROY)) {
15129 p->expiry = atoi(get_header(req, "Expires"));
15130
15131
15132 if (p->expiry > max_expiry)
15133 p->expiry = max_expiry;
15134 if (p->expiry < min_expiry && p->expiry > 0)
15135 p->expiry = min_expiry;
15136
15137 if (sipdebug || option_debug > 1) {
15138 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
15139 ast_log(LOG_DEBUG, "Adding subscription for mailbox notification - peer %s Mailbox %s\n", p->relatedpeer->name, p->relatedpeer->mailbox);
15140 else
15141 ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
15142 }
15143 if (p->autokillid > -1 && sip_cancel_destroy(p))
15144 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15145 if (p->expiry > 0)
15146 sip_scheddestroy(p, (p->expiry + 10) * 1000);
15147
15148 if (p->subscribed == MWI_NOTIFICATION) {
15149 transmit_response(p, "200 OK", req);
15150 if (p->relatedpeer) {
15151 ASTOBJ_WRLOCK(p->relatedpeer);
15152 sip_send_mwi_to_peer(p->relatedpeer);
15153 ASTOBJ_UNLOCK(p->relatedpeer);
15154 }
15155 } else {
15156 struct sip_pvt *p_old;
15157
15158 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
15159
15160 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));
15161 transmit_response(p, "404 Not found", req);
15162 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15163 return 0;
15164 }
15165
15166 transmit_response(p, "200 OK", req);
15167 transmit_state_notify(p, firststate, 1, FALSE);
15168 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
15169
15170 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
15171
15172
15173
15174
15175
15176
15177 ast_mutex_lock(&iflock);
15178 for (p_old = iflist; p_old; p_old = p_old->next) {
15179 if (p_old == p)
15180 continue;
15181 if (p_old->initreq.method != SIP_SUBSCRIBE)
15182 continue;
15183 if (p_old->subscribed == NONE)
15184 continue;
15185 ast_mutex_lock(&p_old->lock);
15186 if (!strcmp(p_old->username, p->username)) {
15187 if (!strcmp(p_old->exten, p->exten) &&
15188 !strcmp(p_old->context, p->context)) {
15189 ast_set_flag(&p_old->flags[0], SIP_NEEDDESTROY);
15190 ast_mutex_unlock(&p_old->lock);
15191 break;
15192 }
15193 }
15194 ast_mutex_unlock(&p_old->lock);
15195 }
15196 ast_mutex_unlock(&iflock);
15197 }
15198 if (!p->expiry)
15199 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15200 }
15201 return 1;
15202 }
15203
15204
15205 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e)
15206 {
15207 enum check_auth_result res;
15208
15209
15210 if (ast_test_flag(req, SIP_PKT_DEBUG))
15211 ast_verbose("Using latest REGISTER request as basis request\n");
15212 copy_request(&p->initreq, req);
15213 check_via(p, req);
15214 if ((res = register_verify(p, sin, req, e)) < 0) {
15215 const char *reason;
15216
15217 switch (res) {
15218 case AUTH_SECRET_FAILED:
15219 reason = "Wrong password";
15220 break;
15221 case AUTH_USERNAME_MISMATCH:
15222 reason = "Username/auth name mismatch";
15223 break;
15224 case AUTH_NOT_FOUND:
15225 reason = "No matching peer found";
15226 break;
15227 case AUTH_UNKNOWN_DOMAIN:
15228 reason = "Not a local domain";
15229 break;
15230 case AUTH_PEER_NOT_DYNAMIC:
15231 reason = "Peer is not supposed to register";
15232 break;
15233 case AUTH_ACL_FAILED:
15234 reason = "Device does not match ACL";
15235 break;
15236 default:
15237 reason = "Unknown failure";
15238 break;
15239 }
15240 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
15241 get_header(req, "To"), ast_inet_ntoa(sin->sin_addr),
15242 reason);
15243 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
15244 } else
15245 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
15246
15247 if (res < 1) {
15248
15249
15250 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15251 }
15252 return res;
15253 }
15254
15255
15256
15257
15258 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
15259 {
15260
15261
15262 const char *cmd;
15263 const char *cseq;
15264 const char *useragent;
15265 int seqno;
15266 int len;
15267 int ignore = FALSE;
15268 int respid;
15269 int res = 0;
15270 int debug = sip_debug_test_pvt(p);
15271 char *e;
15272 int error = 0;
15273
15274
15275 cseq = get_header(req, "Cseq");
15276 cmd = req->header[0];
15277
15278
15279 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq)) {
15280 ast_log(LOG_ERROR, "Missing Cseq. Dropping this SIP message, it's incomplete.\n");
15281 error = 1;
15282 }
15283 if (!error && sscanf(cseq, "%d%n", &seqno, &len) != 1) {
15284 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
15285 error = 1;
15286 }
15287 if (error) {
15288 if (!p->initreq.headers)
15289 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15290 return -1;
15291 }
15292
15293
15294 cmd = req->rlPart1;
15295 e = req->rlPart2;
15296
15297
15298 useragent = get_header(req, "User-Agent");
15299 if (!ast_strlen_zero(useragent))
15300 ast_string_field_set(p, useragent, useragent);
15301
15302
15303 if (req->method == SIP_RESPONSE) {
15304
15305 if (!p->initreq.headers) {
15306 if (option_debug)
15307 ast_log(LOG_DEBUG, "That's odd... Got a response on a call we dont know about. Cseq %d Cmd %s\n", seqno, cmd);
15308 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15309 return 0;
15310 } else if (p->ocseq && (p->ocseq < seqno) && (seqno != p->lastnoninvite)) {
15311 if (option_debug)
15312 ast_log(LOG_DEBUG, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
15313 return -1;
15314 } else if (p->ocseq && (p->ocseq != seqno) && (seqno != p->lastnoninvite)) {
15315
15316
15317 ignore = TRUE;
15318 ast_set_flag(req, SIP_PKT_IGNORE);
15319 ast_set_flag(req, SIP_PKT_IGNORE_RESP);
15320 append_history(p, "Ignore", "Ignoring this retransmit\n");
15321 } else if (e) {
15322 e = ast_skip_blanks(e);
15323 if (sscanf(e, "%d %n", &respid, &len) != 1) {
15324 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
15325 } else {
15326 if (respid <= 0) {
15327 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
15328 return 0;
15329 }
15330
15331 if ((respid == 200) || ((respid >= 300) && (respid <= 399)))
15332 extract_uri(p, req);
15333 handle_response(p, respid, e + len, req, ignore, seqno);
15334 }
15335 }
15336 return 0;
15337 }
15338
15339
15340
15341
15342
15343 p->method = req->method;
15344 if (option_debug > 3)
15345 ast_log(LOG_DEBUG, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
15346
15347 if (p->icseq && (p->icseq > seqno) ) {
15348 if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
15349 if (option_debug > 2)
15350 ast_log(LOG_DEBUG, "Got CANCEL or ACK on INVITE with transactions in between.\n");
15351 } else {
15352 if (option_debug)
15353 ast_log(LOG_DEBUG, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
15354 if (req->method != SIP_ACK)
15355 transmit_response(p, "503 Server error", req);
15356 return -1;
15357 }
15358 } else if (p->icseq &&
15359 p->icseq == seqno &&
15360 req->method != SIP_ACK &&
15361 (p->method != SIP_CANCEL || ast_test_flag(&p->flags[0], SIP_ALREADYGONE))) {
15362
15363
15364
15365 ignore = 2;
15366 ast_set_flag(req, SIP_PKT_IGNORE);
15367 ast_set_flag(req, SIP_PKT_IGNORE_REQ);
15368 if (option_debug > 2)
15369 ast_log(LOG_DEBUG, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
15370 }
15371
15372 if (seqno >= p->icseq)
15373
15374
15375
15376 p->icseq = seqno;
15377
15378
15379 if (ast_strlen_zero(p->theirtag)) {
15380 char tag[128];
15381
15382 gettag(req, "From", tag, sizeof(tag));
15383 ast_string_field_set(p, theirtag, tag);
15384 }
15385 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
15386
15387 if (pedanticsipchecking) {
15388
15389
15390
15391
15392 if (!p->initreq.headers && ast_test_flag(req, SIP_PKT_WITH_TOTAG)) {
15393
15394 if (!ast_test_flag(req, SIP_PKT_IGNORE) && req->method == SIP_INVITE) {
15395 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
15396
15397 } else if (req->method != SIP_ACK) {
15398 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
15399 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15400 }
15401 return res;
15402 }
15403 }
15404
15405 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY)) {
15406 transmit_response(p, "400 Bad request", req);
15407 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15408 return -1;
15409 }
15410
15411
15412 switch (p->method) {
15413 case SIP_OPTIONS:
15414 res = handle_request_options(p, req);
15415 break;
15416 case SIP_INVITE:
15417 res = handle_request_invite(p, req, debug, seqno, sin, recount, e, nounlock);
15418 break;
15419 case SIP_REFER:
15420 res = handle_request_refer(p, req, debug, ignore, seqno, nounlock);
15421 break;
15422 case SIP_CANCEL:
15423 res = handle_request_cancel(p, req);
15424 break;
15425 case SIP_BYE:
15426 res = handle_request_bye(p, req);
15427 break;
15428 case SIP_MESSAGE:
15429 res = handle_request_message(p, req);
15430 break;
15431 case SIP_SUBSCRIBE:
15432 res = handle_request_subscribe(p, req, sin, seqno, e);
15433 break;
15434 case SIP_REGISTER:
15435 res = handle_request_register(p, req, sin, e);
15436 break;
15437 case SIP_INFO:
15438 if (ast_test_flag(req, SIP_PKT_DEBUG))
15439 ast_verbose("Receiving INFO!\n");
15440 if (!ignore)
15441 handle_request_info(p, req);
15442 else
15443 transmit_response(p, "200 OK", req);
15444 break;
15445 case SIP_NOTIFY:
15446 res = handle_request_notify(p, req, sin, seqno, e);
15447 break;
15448 case SIP_ACK:
15449
15450 if (seqno == p->pendinginvite) {
15451 p->invitestate = INV_TERMINATED;
15452 p->pendinginvite = 0;
15453 __sip_ack(p, seqno, FLAG_RESPONSE, 0);
15454 if (find_sdp(req)) {
15455 if (process_sdp(p, req))
15456 return -1;
15457 }
15458 check_pendings(p);
15459 }
15460
15461 if (!p->lastinvite && ast_strlen_zero(p->randdata))
15462 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15463 break;
15464 default:
15465 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
15466 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
15467 cmd, ast_inet_ntoa(p->sa.sin_addr));
15468
15469 if (!p->initreq.headers)
15470 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15471 break;
15472 }
15473 return res;
15474 }
15475
15476
15477
15478
15479
15480
15481 static int sipsock_read(int *id, int fd, short events, void *ignore)
15482 {
15483 struct sip_request req;
15484 struct sockaddr_in sin = { 0, };
15485 struct sip_pvt *p;
15486 int res;
15487 socklen_t len = sizeof(sin);
15488 int nounlock;
15489 int recount = 0;
15490 int lockretry;
15491
15492 memset(&req, 0, sizeof(req));
15493 res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
15494 if (res < 0) {
15495 #if !defined(__FreeBSD__)
15496 if (errno == EAGAIN)
15497 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
15498 else
15499 #endif
15500 if (errno != ECONNREFUSED)
15501 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
15502 return 1;
15503 }
15504 if (option_debug && res == sizeof(req.data) - 1)
15505 ast_log(LOG_DEBUG, "Received packet exceeds buffer. Data is possibly lost\n");
15506
15507 req.data[res] = '\0';
15508 req.len = res;
15509 if(sip_debug_test_addr(&sin))
15510 ast_set_flag(&req, SIP_PKT_DEBUG);
15511 if (pedanticsipchecking)
15512 req.len = lws2sws(req.data, req.len);
15513 if (ast_test_flag(&req, SIP_PKT_DEBUG))
15514 ast_verbose("\n<--- SIP read from %s:%d --->\n%s\n<------------->\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), req.data);
15515
15516 if(parse_request(&req) == -1)
15517 return 1;
15518
15519 req.method = find_sip_method(req.rlPart1);
15520
15521 if (ast_test_flag(&req, SIP_PKT_DEBUG))
15522 ast_verbose("--- (%d headers %d lines)%s ---\n", req.headers, req.lines, (req.headers + req.lines == 0) ? " Nat keepalive" : "");
15523
15524 if (req.headers < 2)
15525 return 1;
15526
15527
15528 for (lockretry = 100; lockretry > 0; lockretry--) {
15529 ast_mutex_lock(&netlock);
15530
15531
15532 p = find_call(&req, &sin, req.method);
15533 if (p == NULL) {
15534 if (option_debug)
15535 ast_log(LOG_DEBUG, "Invalid SIP message - rejected , no callid, len %d\n", req.len);
15536 ast_mutex_unlock(&netlock);
15537 return 1;
15538 }
15539
15540
15541 if (!p->owner || !ast_channel_trylock(p->owner))
15542 break;
15543 if (option_debug)
15544 ast_log(LOG_DEBUG, "Failed to grab owner channel lock, trying again. (SIP call %s)\n", p->callid);
15545 ast_mutex_unlock(&p->lock);
15546 ast_mutex_unlock(&netlock);
15547
15548 usleep(1);
15549 }
15550 p->recv = sin;
15551
15552 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
15553 append_history(p, "Rx", "%s / %s / %s", req.data, get_header(&req, "CSeq"), req.rlPart2);
15554
15555 if (!lockretry) {
15556 if (p->owner)
15557 ast_log(LOG_ERROR, "We could NOT get the channel lock for %s! \n", S_OR(p->owner->name, "- no channel name ??? - "));
15558 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
15559 if (req.method != SIP_ACK)
15560 transmit_response(p, "503 Server error", &req);
15561
15562 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
15563 return 1;
15564 }
15565 nounlock = 0;
15566 if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
15567
15568 if (option_debug)
15569 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
15570 }
15571
15572 if (p->owner && !nounlock)
15573 ast_channel_unlock(p->owner);
15574 ast_mutex_unlock(&p->lock);
15575 ast_mutex_unlock(&netlock);
15576 if (recount)
15577 ast_update_use_count();
15578
15579 return 1;
15580 }
15581
15582
15583 static int sip_send_mwi_to_peer(struct sip_peer *peer)
15584 {
15585
15586 struct sip_pvt *p;
15587 int newmsgs, oldmsgs;
15588
15589
15590 if (!peer->addr.sin_addr.s_addr && !peer->defaddr.sin_addr.s_addr)
15591 return 0;
15592
15593
15594 ast_app_inboxcount(peer->mailbox, &newmsgs, &oldmsgs);
15595
15596 peer->lastmsgcheck = time(NULL);
15597
15598
15599 if (((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)) == peer->lastmsgssent) {
15600 return 0;
15601 }
15602
15603
15604 peer->lastmsgssent = ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs));
15605
15606 if (peer->mwipvt) {
15607
15608 p = peer->mwipvt;
15609 } else {
15610
15611 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY)))
15612 return -1;
15613 if (create_addr_from_peer(p, peer)) {
15614
15615 sip_destroy(p);
15616 return 0;
15617 }
15618
15619 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
15620 p->ourip = __ourip;
15621 build_via(p);
15622 build_callid_pvt(p);
15623
15624 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15625 }
15626
15627 ast_set_flag(&p->flags[0], SIP_OUTGOING);
15628 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
15629 return 0;
15630 }
15631
15632
15633 static int does_peer_need_mwi(struct sip_peer *peer)
15634 {
15635 time_t t = time(NULL);
15636
15637 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
15638 !peer->mwipvt) {
15639 peer->lastmsgcheck = t;
15640 return FALSE;
15641 }
15642
15643 if (!ast_strlen_zero(peer->mailbox) && (t - peer->lastmsgcheck) > global_mwitime)
15644 return TRUE;
15645
15646 return FALSE;
15647 }
15648
15649
15650
15651
15652
15653
15654 static void *do_monitor(void *data)
15655 {
15656 int res;
15657 struct sip_pvt *sip;
15658 struct sip_peer *peer = NULL;
15659 time_t t;
15660 int fastrestart = FALSE;
15661 int lastpeernum = -1;
15662 int curpeernum;
15663 int reloading;
15664
15665
15666 if (sipsock > -1)
15667 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
15668
15669
15670 for(;;) {
15671
15672 ast_mutex_lock(&sip_reload_lock);
15673 reloading = sip_reloading;
15674 sip_reloading = FALSE;
15675 ast_mutex_unlock(&sip_reload_lock);
15676 if (reloading) {
15677 if (option_verbose > 0)
15678 ast_verbose(VERBOSE_PREFIX_1 "Reloading SIP\n");
15679 sip_do_reload(sip_reloadreason);
15680
15681
15682 if (sipsock > -1) {
15683 if (sipsock_read_id)
15684 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
15685 else
15686 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
15687 } else if (sipsock_read_id) {
15688 ast_io_remove(io, sipsock_read_id);
15689 sipsock_read_id = NULL;
15690 }
15691 }
15692 restartsearch:
15693
15694 ast_mutex_lock(&iflock);
15695 t = time(NULL);
15696
15697
15698
15699
15700 for (sip = iflist; !fastrestart && sip; sip = sip->next) {
15701
15702
15703
15704
15705
15706 if (ast_mutex_trylock(&sip->lock))
15707 continue;
15708
15709
15710 if (sip->rtp && sip->owner &&
15711 (sip->owner->_state == AST_STATE_UP) &&
15712 !sip->redirip.sin_addr.s_addr &&
15713 sip->t38.state != T38_ENABLED) {
15714 if (sip->lastrtptx &&
15715 ast_rtp_get_rtpkeepalive(sip->rtp) &&
15716 (t > sip->lastrtptx + ast_rtp_get_rtpkeepalive(sip->rtp))) {
15717
15718 sip->lastrtptx = time(NULL);
15719 ast_rtp_sendcng(sip->rtp, 0);
15720 }
15721 if (sip->lastrtprx &&
15722 (ast_rtp_get_rtptimeout(sip->rtp) || ast_rtp_get_rtpholdtimeout(sip->rtp)) &&
15723 (t > sip->lastrtprx + ast_rtp_get_rtptimeout(sip->rtp))) {
15724
15725 struct sockaddr_in sin;
15726 ast_rtp_get_peer(sip->rtp, &sin);
15727 if (sin.sin_addr.s_addr ||
15728 (ast_rtp_get_rtpholdtimeout(sip->rtp) &&
15729 (t > sip->lastrtprx + ast_rtp_get_rtpholdtimeout(sip->rtp)))) {
15730
15731 if (ast_rtp_get_rtptimeout(sip->rtp)) {
15732 while (sip->owner && ast_channel_trylock(sip->owner)) {
15733 DEADLOCK_AVOIDANCE(&sip->lock);
15734 }
15735 if (sip->owner) {
15736 ast_log(LOG_NOTICE,
15737 "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
15738 sip->owner->name,
15739 (long) (t - sip->lastrtprx));
15740
15741 ast_softhangup_nolock(sip->owner, AST_SOFTHANGUP_DEV);
15742 ast_channel_unlock(sip->owner);
15743
15744
15745
15746
15747 ast_rtp_set_rtptimeout(sip->rtp, 0);
15748 ast_rtp_set_rtpholdtimeout(sip->rtp, 0);
15749 if (sip->vrtp) {
15750 ast_rtp_set_rtptimeout(sip->vrtp, 0);
15751 ast_rtp_set_rtpholdtimeout(sip->vrtp, 0);
15752 }
15753 }
15754 }
15755 }
15756 }
15757 }
15758
15759 if (ast_test_flag(&sip->flags[0], SIP_NEEDDESTROY) && !sip->packets &&
15760 !sip->owner) {
15761 ast_mutex_unlock(&sip->lock);
15762 __sip_destroy(sip, 1);
15763 ast_mutex_unlock(&iflock);
15764 usleep(1);
15765 goto restartsearch;
15766 }
15767 ast_mutex_unlock(&sip->lock);
15768 }
15769 ast_mutex_unlock(&iflock);
15770
15771 pthread_testcancel();
15772
15773 res = ast_sched_wait(sched);
15774 if ((res < 0) || (res > 1000))
15775 res = 1000;
15776
15777 if (fastrestart)
15778 res = 1;
15779 res = ast_io_wait(io, res);
15780 if (option_debug && res > 20)
15781 ast_log(LOG_DEBUG, "chan_sip: ast_io_wait ran %d all at once\n", res);
15782 ast_mutex_lock(&monlock);
15783 if (res >= 0) {
15784 res = ast_sched_runq(sched);
15785 if (option_debug && res >= 20)
15786 ast_log(LOG_DEBUG, "chan_sip: ast_sched_runq ran %d all at once\n", res);
15787 }
15788
15789
15790 t = time(NULL);
15791 fastrestart = FALSE;
15792 curpeernum = 0;
15793 peer = NULL;
15794
15795 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !peer, do {
15796 if ((curpeernum > lastpeernum) && does_peer_need_mwi(iterator)) {
15797 fastrestart = TRUE;
15798 lastpeernum = curpeernum;
15799 peer = ASTOBJ_REF(iterator);
15800 };
15801 curpeernum++;
15802 } while (0)
15803 );
15804
15805 if (peer) {
15806 ASTOBJ_WRLOCK(peer);
15807 sip_send_mwi_to_peer(peer);
15808 ASTOBJ_UNLOCK(peer);
15809 ASTOBJ_UNREF(peer,sip_destroy_peer);
15810 } else {
15811
15812 lastpeernum = -1;
15813 }
15814 ast_mutex_unlock(&monlock);
15815 }
15816
15817 return NULL;
15818
15819 }
15820
15821
15822 static int restart_monitor(void)
15823 {
15824
15825 if (monitor_thread == AST_PTHREADT_STOP)
15826 return 0;
15827 ast_mutex_lock(&monlock);
15828 if (monitor_thread == pthread_self()) {
15829 ast_mutex_unlock(&monlock);
15830 ast_log(LOG_WARNING, "Cannot kill myself\n");
15831 return -1;
15832 }
15833 if (monitor_thread != AST_PTHREADT_NULL) {
15834
15835 pthread_kill(monitor_thread, SIGURG);
15836 } else {
15837
15838 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
15839 ast_mutex_unlock(&monlock);
15840 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
15841 return -1;
15842 }
15843 }
15844 ast_mutex_unlock(&monlock);
15845 return 0;
15846 }
15847
15848
15849 static int sip_poke_noanswer(const void *data)
15850 {
15851 struct sip_peer *peer = (struct sip_peer *)data;
15852
15853 peer->pokeexpire = -1;
15854 if (peer->lastms > -1) {
15855 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
15856 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
15857 }
15858 if (peer->call)
15859 sip_destroy(peer->call);
15860 peer->call = NULL;
15861 peer->lastms = -1;
15862 ast_device_state_changed("SIP/%s", peer->name);
15863
15864
15865 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
15866 struct sip_peer *peer_ptr = peer;
15867 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
15868 }
15869
15870
15871
15872 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
15873 if (peer->pokeexpire == -1) {
15874 ASTOBJ_UNREF(peer, sip_destroy_peer);
15875 }
15876
15877 return 0;
15878 }
15879
15880
15881
15882
15883 static int sip_poke_peer(struct sip_peer *peer)
15884 {
15885 struct sip_pvt *p;
15886 int xmitres = 0;
15887
15888 if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
15889
15890
15891 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
15892 struct sip_peer *peer_ptr = peer;
15893 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
15894 }
15895 peer->lastms = 0;
15896 peer->call = NULL;
15897 return 0;
15898 }
15899 if (peer->call) {
15900 if (sipdebug)
15901 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
15902 sip_destroy(peer->call);
15903 }
15904 if (!(p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS)))
15905 return -1;
15906
15907 p->sa = peer->addr;
15908 p->recv = peer->addr;
15909 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
15910 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
15911
15912
15913 if (!ast_strlen_zero(peer->fullcontact))
15914 ast_string_field_set(p, fullcontact, peer->fullcontact);
15915
15916 if (!ast_strlen_zero(peer->tohost))
15917 ast_string_field_set(p, tohost, peer->tohost);
15918 else
15919 ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr));
15920
15921
15922 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
15923 p->ourip = __ourip;
15924 build_via(p);
15925 build_callid_pvt(p);
15926
15927 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
15928 struct sip_peer *peer_ptr = peer;
15929 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
15930 }
15931
15932 p->relatedpeer = ASTOBJ_REF(peer);
15933 ast_set_flag(&p->flags[0], SIP_OUTGOING);
15934 #ifdef VOCAL_DATA_HACK
15935 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
15936 xmitres = transmit_invite(p, SIP_INVITE, 0, 2);
15937 #else
15938 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2);
15939 #endif
15940 gettimeofday(&peer->ps, NULL);
15941 if (xmitres == XMIT_ERROR) {
15942 sip_poke_noanswer(ASTOBJ_REF(peer));
15943 } else {
15944 if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
15945 struct sip_peer *peer_ptr = peer;
15946 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
15947 }
15948 peer->pokeexpire = ast_sched_add(sched, peer->maxms * 2, sip_poke_noanswer, ASTOBJ_REF(peer));
15949 if (peer->pokeexpire == -1) {
15950 struct sip_peer *peer_ptr = peer;
15951 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
15952 }
15953 }
15954
15955 return 0;
15956 }
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991 static int sip_devicestate(void *data)
15992 {
15993 char *host;
15994 char *tmp;
15995
15996 struct hostent *hp;
15997 struct ast_hostent ahp;
15998 struct sip_peer *p;
15999
16000 int res = AST_DEVICE_INVALID;
16001
16002
16003 host = ast_strdupa(data ? data : "");
16004 if ((tmp = strchr(host, '@')))
16005 host = tmp + 1;
16006
16007 if (option_debug > 2)
16008 ast_log(LOG_DEBUG, "Checking device state for peer %s\n", host);
16009
16010 if ((p = find_peer(host, NULL, 1))) {
16011 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023 if (p->onHold)
16024
16025 res = AST_DEVICE_ONHOLD;
16026 else if (p->inRinging) {
16027 if (p->inRinging == p->inUse)
16028 res = AST_DEVICE_RINGING;
16029 else
16030 res = AST_DEVICE_RINGINUSE;
16031 } else if (p->call_limit && (p->inUse == p->call_limit))
16032
16033 res = AST_DEVICE_BUSY;
16034 else if (p->call_limit && p->inUse)
16035
16036 res = AST_DEVICE_INUSE;
16037 else if (p->maxms && ((p->lastms > p->maxms) || (p->lastms < 0)))
16038
16039 res = AST_DEVICE_UNAVAILABLE;
16040 else
16041 res = AST_DEVICE_NOT_INUSE;
16042 } else {
16043
16044 res = AST_DEVICE_UNAVAILABLE;
16045 }
16046 ASTOBJ_UNREF(p,sip_destroy_peer);
16047 } else {
16048 char *port = strchr(host, ':');
16049 if (port)
16050 *port = '\0';
16051 hp = ast_gethostbyname(host, &ahp);
16052 if (hp)
16053 res = AST_DEVICE_UNKNOWN;
16054 }
16055
16056 return res;
16057 }
16058
16059
16060
16061 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
16062 {
16063 int oldformat;
16064 struct sip_pvt *p;
16065 struct ast_channel *tmpc = NULL;
16066 char *ext, *host;
16067 char tmp[256];
16068 char *dest = data;
16069
16070 oldformat = format;
16071 if (!(format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1))) {
16072 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));
16073 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
16074 return NULL;
16075 }
16076 if (option_debug)
16077 ast_log(LOG_DEBUG, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
16078
16079 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE))) {
16080 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", (char *)data);
16081 *cause = AST_CAUSE_SWITCH_CONGESTION;
16082 return NULL;
16083 }
16084
16085 ast_set_flag(&p->flags[1], SIP_PAGE2_OUTGOING_CALL);
16086
16087 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
16088 sip_destroy(p);
16089 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
16090 *cause = AST_CAUSE_SWITCH_CONGESTION;
16091 return NULL;
16092 }
16093
16094 ast_copy_string(tmp, dest, sizeof(tmp));
16095 host = strchr(tmp, '@');
16096 if (host) {
16097 *host++ = '\0';
16098 ext = tmp;
16099 } else {
16100 ext = strchr(tmp, '/');
16101 if (ext)
16102 *ext++ = '\0';
16103 host = tmp;
16104 }
16105
16106 if (create_addr(p, host)) {
16107 *cause = AST_CAUSE_UNREGISTERED;
16108 if (option_debug > 2)
16109 ast_log(LOG_DEBUG, "Cant create SIP call - target device not registred\n");
16110 sip_destroy(p);
16111 return NULL;
16112 }
16113 if (ast_strlen_zero(p->peername) && ext)
16114 ast_string_field_set(p, peername, ext);
16115
16116 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
16117 p->ourip = __ourip;
16118 build_via(p);
16119 build_callid_pvt(p);
16120
16121
16122
16123
16124
16125 if (ext) {
16126 ast_string_field_set(p, username, ext);
16127 ast_string_field_free(p, fullcontact);
16128 }
16129 #if 0
16130 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
16131 #endif
16132 p->prefcodec = oldformat;
16133 ast_mutex_lock(&p->lock);
16134 tmpc = sip_new(p, AST_STATE_DOWN, host);
16135 ast_mutex_unlock(&p->lock);
16136 if (!tmpc)
16137 sip_destroy(p);
16138 ast_update_use_count();
16139 restart_monitor();
16140 return tmpc;
16141 }
16142
16143
16144
16145
16146
16147
16148
16149 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno)
16150 {
16151 static int dep_insecure_very = 0;
16152 static int dep_insecure_yes = 0;
16153
16154 if (ast_strlen_zero(value))
16155 return;
16156
16157 if (!strcasecmp(value, "very")) {
16158 ast_set_flag(flags, SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
16159 if(!dep_insecure_very) {
16160 if(lineno != -1)
16161 ast_log(LOG_WARNING, "insecure=very at line %d is deprecated; use insecure=port,invite instead\n", lineno);
16162 else
16163 ast_log(LOG_WARNING, "insecure=very is deprecated; use insecure=port,invite instead\n");
16164 dep_insecure_very = 1;
16165 }
16166 }
16167 else if (ast_true(value)) {
16168 ast_set_flag(flags, SIP_INSECURE_PORT);
16169 if(!dep_insecure_yes) {
16170 if(lineno != -1)
16171 ast_log(LOG_WARNING, "insecure=%s at line %d is deprecated; use insecure=port instead\n", value, lineno);
16172 else
16173 ast_log(LOG_WARNING, "insecure=%s is deprecated; use insecure=port instead\n", value);
16174 dep_insecure_yes = 1;
16175 }
16176 }
16177 else if (!ast_false(value)) {
16178 char buf[64];
16179 char *word, *next;
16180 ast_copy_string(buf, value, sizeof(buf));
16181 next = buf;
16182 while ((word = strsep(&next, ","))) {
16183 if (!strcasecmp(word, "port"))
16184 ast_set_flag(flags, SIP_INSECURE_PORT);
16185 else if (!strcasecmp(word, "invite"))
16186 ast_set_flag(flags, SIP_INSECURE_INVITE);
16187 else
16188 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
16189 }
16190 }
16191 }
16192
16193
16194
16195
16196
16197
16198
16199
16200 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
16201 {
16202 int res = 1;
16203
16204 if (!strcasecmp(v->name, "trustrpid")) {
16205 ast_set_flag(&mask[0], SIP_TRUSTRPID);
16206 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
16207 } else if (!strcasecmp(v->name, "sendrpid")) {
16208 ast_set_flag(&mask[0], SIP_SENDRPID);
16209 ast_set2_flag(&flags[0], ast_true(v->value), SIP_SENDRPID);
16210 } else if (!strcasecmp(v->name, "g726nonstandard")) {
16211 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
16212 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
16213 } else if (!strcasecmp(v->name, "useclientcode")) {
16214 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
16215 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
16216 } else if (!strcasecmp(v->name, "dtmfmode")) {
16217 ast_set_flag(&mask[0], SIP_DTMF);
16218 ast_clear_flag(&flags[0], SIP_DTMF);
16219 if (!strcasecmp(v->value, "inband"))
16220 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
16221 else if (!strcasecmp(v->value, "rfc2833"))
16222 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
16223 else if (!strcasecmp(v->value, "info"))
16224 ast_set_flag(&flags[0], SIP_DTMF_INFO);
16225 else if (!strcasecmp(v->value, "auto"))
16226 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
16227 else {
16228 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
16229 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
16230 }
16231 } else if (!strcasecmp(v->name, "nat")) {
16232 ast_set_flag(&mask[0], SIP_NAT);
16233 ast_clear_flag(&flags[0], SIP_NAT);
16234 if (!strcasecmp(v->value, "never"))
16235 ast_set_flag(&flags[0], SIP_NAT_NEVER);
16236 else if (!strcasecmp(v->value, "route"))
16237 ast_set_flag(&flags[0], SIP_NAT_ROUTE);
16238 else if (ast_true(v->value))
16239 ast_set_flag(&flags[0], SIP_NAT_ALWAYS);
16240 else
16241 ast_set_flag(&flags[0], SIP_NAT_RFC3581);
16242 } else if (!strcasecmp(v->name, "canreinvite")) {
16243 ast_set_flag(&mask[0], SIP_REINVITE);
16244 ast_clear_flag(&flags[0], SIP_REINVITE);
16245 if(ast_true(v->value)) {
16246 ast_set_flag(&flags[0], SIP_CAN_REINVITE | SIP_CAN_REINVITE_NAT);
16247 } else if (!ast_false(v->value)) {
16248 char buf[64];
16249 char *word, *next = buf;
16250
16251 ast_copy_string(buf, v->value, sizeof(buf));
16252 while ((word = strsep(&next, ","))) {
16253 if(!strcasecmp(word, "update")) {
16254 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_CAN_REINVITE);
16255 } else if(!strcasecmp(word, "nonat")) {
16256 ast_set_flag(&flags[0], SIP_CAN_REINVITE);
16257 ast_clear_flag(&flags[0], SIP_CAN_REINVITE_NAT);
16258 } else {
16259 ast_log(LOG_WARNING, "Unknown canreinvite mode '%s' on line %d\n", v->value, v->lineno);
16260 }
16261 }
16262 }
16263 } else if (!strcasecmp(v->name, "insecure")) {
16264 ast_set_flag(&mask[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
16265 ast_clear_flag(&flags[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
16266 set_insecure_flags(flags, v->value, v->lineno);
16267 } else if (!strcasecmp(v->name, "progressinband")) {
16268 ast_set_flag(&mask[0], SIP_PROG_INBAND);
16269 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
16270 if (ast_true(v->value))
16271 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
16272 else if (strcasecmp(v->value, "never"))
16273 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
16274 } else if (!strcasecmp(v->name, "promiscredir")) {
16275 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
16276 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
16277 } else if (!strcasecmp(v->name, "videosupport")) {
16278 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
16279 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
16280 } else if (!strcasecmp(v->name, "allowoverlap")) {
16281 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
16282 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
16283 } else if (!strcasecmp(v->name, "allowsubscribe")) {
16284 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
16285 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
16286 } else if (!strcasecmp(v->name, "t38pt_udptl")) {
16287 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_UDPTL);
16288 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);
16289 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
16290 } else if (!strcasecmp(v->name, "t38pt_rtp")) {
16291 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_RTP);
16292 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);
16293 } else if (!strcasecmp(v->name, "t38pt_tcp")) {
16294 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_TCP);
16295 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);
16296 #endif
16297 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
16298 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
16299 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
16300 } else if (!strcasecmp(v->name, "buggymwi")) {
16301 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
16302 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
16303 } else if (!strcasecmp(v->name, "t38pt_usertpsource")) {
16304 ast_set_flag(&mask[1], SIP_PAGE2_UDPTL_DESTINATION);
16305 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_UDPTL_DESTINATION);
16306 } else
16307 res = 0;
16308
16309 return res;
16310 }
16311
16312
16313 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
16314 {
16315 struct domain *d;
16316
16317 if (ast_strlen_zero(domain)) {
16318 ast_log(LOG_WARNING, "Zero length domain.\n");
16319 return 1;
16320 }
16321
16322 if (!(d = ast_calloc(1, sizeof(*d))))
16323 return 0;
16324
16325 ast_copy_string(d->domain, domain, sizeof(d->domain));
16326
16327 if (!ast_strlen_zero(context))
16328 ast_copy_string(d->context, context, sizeof(d->context));
16329
16330 d->mode = mode;
16331
16332 AST_LIST_LOCK(&domain_list);
16333 AST_LIST_INSERT_TAIL(&domain_list, d, list);
16334 AST_LIST_UNLOCK(&domain_list);
16335
16336 if (sipdebug)
16337 ast_log(LOG_DEBUG, "Added local SIP domain '%s'\n", domain);
16338
16339 return 1;
16340 }
16341
16342
16343 static int check_sip_domain(const char *domain, char *context, size_t len)
16344 {
16345 struct domain *d;
16346 int result = 0;
16347
16348 AST_LIST_LOCK(&domain_list);
16349 AST_LIST_TRAVERSE(&domain_list, d, list) {
16350 if (strcasecmp(d->domain, domain))
16351 continue;
16352
16353 if (len && !ast_strlen_zero(d->context))
16354 ast_copy_string(context, d->context, len);
16355
16356 result = 1;
16357 break;
16358 }
16359 AST_LIST_UNLOCK(&domain_list);
16360
16361 return result;
16362 }
16363
16364
16365 static void clear_sip_domains(void)
16366 {
16367 struct domain *d;
16368
16369 AST_LIST_LOCK(&domain_list);
16370 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
16371 free(d);
16372 AST_LIST_UNLOCK(&domain_list);
16373 }
16374
16375
16376
16377 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno)
16378 {
16379 char authcopy[256];
16380 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
16381 char *stringp;
16382 struct sip_auth *a, *b, *auth;
16383
16384 if (ast_strlen_zero(configuration))
16385 return authlist;
16386
16387 if (option_debug)
16388 ast_log(LOG_DEBUG, "Auth config :: %s\n", configuration);
16389
16390 ast_copy_string(authcopy, configuration, sizeof(authcopy));
16391 stringp = authcopy;
16392
16393 username = stringp;
16394 realm = strrchr(stringp, '@');
16395 if (realm)
16396 *realm++ = '\0';
16397 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
16398 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
16399 return authlist;
16400 }
16401 stringp = username;
16402 username = strsep(&stringp, ":");
16403 if (username) {
16404 secret = strsep(&stringp, ":");
16405 if (!secret) {
16406 stringp = username;
16407 md5secret = strsep(&stringp,"#");
16408 }
16409 }
16410 if (!(auth = ast_calloc(1, sizeof(*auth))))
16411 return authlist;
16412
16413 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
16414 ast_copy_string(auth->username, username, sizeof(auth->username));
16415 if (secret)
16416 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
16417 if (md5secret)
16418 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
16419
16420
16421 for (b = NULL, a = authlist; a ; b = a, a = a->next)
16422 ;
16423 if (b)
16424 b->next = auth;
16425 else
16426 authlist = auth;
16427
16428 if (option_verbose > 2)
16429 ast_verbose("Added authentication for realm %s\n", realm);
16430
16431 return authlist;
16432
16433 }
16434
16435
16436 static int clear_realm_authentication(struct sip_auth *authlist)
16437 {
16438 struct sip_auth *a = authlist;
16439 struct sip_auth *b;
16440
16441 while (a) {
16442 b = a;
16443 a = a->next;
16444 free(b);
16445 }
16446
16447 return 1;
16448 }
16449
16450
16451 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
16452 {
16453 struct sip_auth *a;
16454
16455 for (a = authlist; a; a = a->next) {
16456 if (!strcasecmp(a->realm, realm))
16457 break;
16458 }
16459
16460 return a;
16461 }
16462
16463
16464 static struct sip_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
16465 {
16466 struct sip_user *user;
16467 int format;
16468 struct ast_ha *oldha = NULL;
16469 char *varname = NULL, *varval = NULL;
16470 struct ast_variable *tmpvar = NULL;
16471 struct ast_flags userflags[2] = {{(0)}};
16472 struct ast_flags mask[2] = {{(0)}};
16473
16474
16475 if (!(user = ast_calloc(1, sizeof(*user))))
16476 return NULL;
16477
16478 suserobjs++;
16479 ASTOBJ_INIT(user);
16480 ast_copy_string(user->name, name, sizeof(user->name));
16481 oldha = user->ha;
16482 user->ha = NULL;
16483 ast_copy_flags(&user->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
16484 ast_copy_flags(&user->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
16485 user->capability = global_capability;
16486 user->allowtransfer = global_allowtransfer;
16487 user->maxcallbitrate = default_maxcallbitrate;
16488 user->autoframing = global_autoframing;
16489 user->prefs = default_prefs;
16490
16491 strcpy(user->context, default_context);
16492 strcpy(user->language, default_language);
16493 strcpy(user->mohinterpret, default_mohinterpret);
16494 strcpy(user->mohsuggest, default_mohsuggest);
16495
16496 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
16497 if (handle_common_options(&userflags[0], &mask[0], v))
16498 continue;
16499
16500 if (!strcasecmp(v->name, "context")) {
16501 ast_copy_string(user->context, v->value, sizeof(user->context));
16502 } else if (!strcasecmp(v->name, "subscribecontext")) {
16503 ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
16504 } else if (!strcasecmp(v->name, "setvar")) {
16505 varname = ast_strdupa(v->value);
16506 if ((varval = strchr(varname,'='))) {
16507 *varval++ = '\0';
16508 if ((tmpvar = ast_variable_new(varname, varval))) {
16509 tmpvar->next = user->chanvars;
16510 user->chanvars = tmpvar;
16511 }
16512 }
16513 } else if (!strcasecmp(v->name, "permit") ||
16514 !strcasecmp(v->name, "deny")) {
16515 user->ha = ast_append_ha(v->name, v->value, user->ha);
16516 } else if (!strcasecmp(v->name, "allowtransfer")) {
16517 user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
16518 } else if (!strcasecmp(v->name, "secret")) {
16519 ast_copy_string(user->secret, v->value, sizeof(user->secret));
16520 } else if (!strcasecmp(v->name, "md5secret")) {
16521 ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
16522 } else if (!strcasecmp(v->name, "callerid")) {
16523 ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
16524 } else if (!strcasecmp(v->name, "fullname")) {
16525 ast_copy_string(user->cid_name, v->value, sizeof(user->cid_name));
16526 } else if (!strcasecmp(v->name, "cid_number")) {
16527 ast_copy_string(user->cid_num, v->value, sizeof(user->cid_num));
16528 } else if (!strcasecmp(v->name, "callgroup")) {
16529 user->callgroup = ast_get_group(v->value);
16530 } else if (!strcasecmp(v->name, "pickupgroup")) {
16531 user->pickupgroup = ast_get_group(v->value);
16532 } else if (!strcasecmp(v->name, "language")) {
16533 ast_copy_string(user->language, v->value, sizeof(user->language));
16534 } else if (!strcasecmp(v->name, "mohinterpret")
16535 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
16536 ast_copy_string(user->mohinterpret, v->value, sizeof(user->mohinterpret));
16537 } else if (!strcasecmp(v->name, "mohsuggest")) {
16538 ast_copy_string(user->mohsuggest, v->value, sizeof(user->mohsuggest));
16539 } else if (!strcasecmp(v->name, "accountcode")) {
16540 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
16541 } else if (!strcasecmp(v->name, "call-limit")) {
16542 user->call_limit = atoi(v->value);
16543 if (user->call_limit < 0)
16544 user->call_limit = 0;
16545 } else if (!strcasecmp(v->name, "amaflags")) {
16546 format = ast_cdr_amaflags2int(v->value);
16547 if (format < 0) {
16548 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
16549 } else {
16550 user->amaflags = format;
16551 }
16552 } else if (!strcasecmp(v->name, "allow")) {
16553 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
16554 } else if (!strcasecmp(v->name, "disallow")) {
16555 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
16556 } else if (!strcasecmp(v->name, "autoframing")) {
16557 user->autoframing = ast_true(v->value);
16558 } else if (!strcasecmp(v->name, "callingpres")) {
16559 user->callingpres = ast_parse_caller_presentation(v->value);
16560 if (user->callingpres == -1)
16561 user->callingpres = atoi(v->value);
16562 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
16563 user->maxcallbitrate = atoi(v->value);
16564 if (user->maxcallbitrate < 0)
16565 user->maxcallbitrate = default_maxcallbitrate;
16566 }
16567
16568
16569
16570 }
16571 ast_copy_flags(&user->flags[0], &userflags[0], mask[0].flags);
16572 ast_copy_flags(&user->flags[1], &userflags[1], mask[1].flags);
16573 if (ast_test_flag(&user->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
16574 global_allowsubscribe = TRUE;
16575 ast_free_ha(oldha);
16576 return user;
16577 }
16578
16579
16580 static void set_peer_defaults(struct sip_peer *peer)
16581 {
16582 if (peer->expire == 0) {
16583
16584
16585
16586 peer->expire = -1;
16587 peer->pokeexpire = -1;
16588 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
16589 }
16590 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
16591 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
16592 strcpy(peer->context, default_context);
16593 strcpy(peer->subscribecontext, default_subscribecontext);
16594 strcpy(peer->language, default_language);
16595 strcpy(peer->mohinterpret, default_mohinterpret);
16596 strcpy(peer->mohsuggest, default_mohsuggest);
16597 peer->addr.sin_family = AF_INET;
16598 peer->defaddr.sin_family = AF_INET;
16599 peer->capability = global_capability;
16600 peer->maxcallbitrate = default_maxcallbitrate;
16601 peer->rtptimeout = global_rtptimeout;
16602 peer->rtpholdtimeout = global_rtpholdtimeout;
16603 peer->rtpkeepalive = global_rtpkeepalive;
16604 peer->allowtransfer = global_allowtransfer;
16605 peer->autoframing = global_autoframing;
16606 strcpy(peer->vmexten, default_vmexten);
16607 peer->secret[0] = '\0';
16608 peer->md5secret[0] = '\0';
16609 peer->cid_num[0] = '\0';
16610 peer->cid_name[0] = '\0';
16611 peer->fromdomain[0] = '\0';
16612 peer->fromuser[0] = '\0';
16613 peer->regexten[0] = '\0';
16614 peer->mailbox[0] = '\0';
16615 peer->callgroup = 0;
16616 peer->pickupgroup = 0;
16617 peer->maxms = default_qualify;
16618 peer->prefs = default_prefs;
16619 }
16620
16621
16622 static struct sip_peer *temp_peer(const char *name)
16623 {
16624 struct sip_peer *peer;
16625
16626 if (!(peer = ast_calloc(1, sizeof(*peer))))
16627 return NULL;
16628
16629 apeerobjs++;
16630 ASTOBJ_INIT(peer);
16631 set_peer_defaults(peer);
16632
16633 ast_copy_string(peer->name, name, sizeof(peer->name));
16634
16635 ast_set_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT);
16636 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16637 peer->prefs = default_prefs;
16638 reg_source_db(peer);
16639
16640 return peer;
16641 }
16642
16643
16644 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
16645 {
16646 struct sip_peer *peer = NULL;
16647 struct ast_ha *oldha = NULL;
16648 int obproxyfound=0;
16649 int found=0;
16650 int firstpass=1;
16651 int format=0;
16652 time_t regseconds = 0;
16653 char *varname = NULL, *varval = NULL;
16654 struct ast_variable *tmpvar = NULL;
16655 struct ast_flags peerflags[2] = {{(0)}};
16656 struct ast_flags mask[2] = {{(0)}};
16657 char fullcontact[sizeof(peer->fullcontact)] = "";
16658
16659 if (!realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS))
16660
16661
16662
16663
16664
16665 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
16666
16667 if (peer) {
16668
16669 found = 1;
16670 if (!(peer->objflags & ASTOBJ_FLAG_MARKED))
16671 firstpass = 0;
16672 } else {
16673 if (!(peer = ast_calloc(1, sizeof(*peer))))
16674 return NULL;
16675
16676 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS))
16677 rpeerobjs++;
16678 else
16679 speerobjs++;
16680 ASTOBJ_INIT(peer);
16681 }
16682
16683 if (firstpass) {
16684 peer->lastmsgssent = -1;
16685 oldha = peer->ha;
16686 peer->ha = NULL;
16687 set_peer_defaults(peer);
16688 }
16689 if (!found && name)
16690 ast_copy_string(peer->name, name, sizeof(peer->name));
16691
16692
16693 if (peer->chanvars) {
16694 ast_variables_destroy(peer->chanvars);
16695 peer->chanvars = NULL;
16696
16697 }
16698
16699
16700 clear_realm_authentication(peer->auth);
16701 peer->auth = NULL;
16702
16703 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
16704 if (handle_common_options(&peerflags[0], &mask[0], v))
16705 continue;
16706 if (realtime && !strcasecmp(v->name, "regseconds")) {
16707 ast_get_time_t(v->value, ®seconds, 0, NULL);
16708 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
16709 inet_aton(v->value, &(peer->addr.sin_addr));
16710 } else if (realtime && !strcasecmp(v->name, "name"))
16711 ast_copy_string(peer->name, v->value, sizeof(peer->name));
16712 else if (realtime && !strcasecmp(v->name, "fullcontact")) {
16713
16714 if (!ast_strlen_zero(fullcontact)) {
16715 strncat(fullcontact, ";", sizeof(fullcontact) - strlen(fullcontact) - 1);
16716 strncat(fullcontact, v->value, sizeof(fullcontact) - strlen(fullcontact) - 1);
16717 } else {
16718 ast_copy_string(fullcontact, v->value, sizeof(fullcontact));
16719 ast_set_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT);
16720 }
16721 } else if (!strcasecmp(v->name, "secret"))
16722 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
16723 else if (!strcasecmp(v->name, "md5secret"))
16724 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
16725 else if (!strcasecmp(v->name, "auth"))
16726 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
16727 else if (!strcasecmp(v->name, "callerid")) {
16728 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
16729 } else if (!strcasecmp(v->name, "fullname")) {
16730 ast_copy_string(peer->cid_name, v->value, sizeof(peer->cid_name));
16731 } else if (!strcasecmp(v->name, "cid_number")) {
16732 ast_copy_string(peer->cid_num, v->value, sizeof(peer->cid_num));
16733 } else if (!strcasecmp(v->name, "context")) {
16734 ast_copy_string(peer->context, v->value, sizeof(peer->context));
16735 } else if (!strcasecmp(v->name, "subscribecontext")) {
16736 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
16737 } else if (!strcasecmp(v->name, "fromdomain")) {
16738 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
16739 } else if (!strcasecmp(v->name, "usereqphone")) {
16740 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
16741 } else if (!strcasecmp(v->name, "fromuser")) {
16742 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
16743 } else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "outboundproxy")) {
16744 if (!strcasecmp(v->value, "dynamic")) {
16745 if (!strcasecmp(v->name, "outboundproxy") || obproxyfound) {
16746 ast_log(LOG_WARNING, "You can't have a dynamic outbound proxy, you big silly head at line %d.\n", v->lineno);
16747 } else {
16748
16749 if (!found || !ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
16750
16751
16752 memset(&peer->addr.sin_addr, 0, 4);
16753 if (peer->addr.sin_port) {
16754
16755 peer->defaddr.sin_port = peer->addr.sin_port;
16756 peer->addr.sin_port = 0;
16757 }
16758 }
16759 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16760 }
16761 } else {
16762
16763 if (!AST_SCHED_DEL(sched, peer->expire)) {
16764 struct sip_peer *peer_ptr = peer;
16765 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
16766 }
16767 ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16768 if (!obproxyfound || !strcasecmp(v->name, "outboundproxy")) {
16769 if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
16770 ASTOBJ_UNREF(peer, sip_destroy_peer);
16771 return NULL;
16772 }
16773 }
16774 if (!strcasecmp(v->name, "outboundproxy"))
16775 obproxyfound=1;
16776 else {
16777 ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
16778 if (!peer->addr.sin_port)
16779 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
16780 }
16781 }
16782 } else if (!strcasecmp(v->name, "defaultip")) {
16783 if (ast_get_ip(&peer->defaddr, v->value)) {
16784 ASTOBJ_UNREF(peer, sip_destroy_peer);
16785 return NULL;
16786 }
16787 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
16788 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
16789 } else if (!strcasecmp(v->name, "port")) {
16790 if (!realtime && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC))
16791 peer->defaddr.sin_port = htons(atoi(v->value));
16792 else
16793 peer->addr.sin_port = htons(atoi(v->value));
16794 } else if (!strcasecmp(v->name, "callingpres")) {
16795 peer->callingpres = ast_parse_caller_presentation(v->value);
16796 if (peer->callingpres == -1)
16797 peer->callingpres = atoi(v->value);
16798 } else if (!strcasecmp(v->name, "username")) {
16799 ast_copy_string(peer->username, v->value, sizeof(peer->username));
16800 } else if (!strcasecmp(v->name, "language")) {
16801 ast_copy_string(peer->language, v->value, sizeof(peer->language));
16802 } else if (!strcasecmp(v->name, "regexten")) {
16803 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
16804 } else if (!strcasecmp(v->name, "call-limit") || !strcasecmp(v->name, "incominglimit")) {
16805 peer->call_limit = atoi(v->value);
16806 if (peer->call_limit < 0)
16807 peer->call_limit = 0;
16808 } else if (!strcasecmp(v->name, "amaflags")) {
16809 format = ast_cdr_amaflags2int(v->value);
16810 if (format < 0) {
16811 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
16812 } else {
16813 peer->amaflags = format;
16814 }
16815 } else if (!strcasecmp(v->name, "accountcode")) {
16816 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
16817 } else if (!strcasecmp(v->name, "mohinterpret")
16818 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
16819 ast_copy_string(peer->mohinterpret, v->value, sizeof(peer->mohinterpret));
16820 } else if (!strcasecmp(v->name, "mohsuggest")) {
16821 ast_copy_string(peer->mohsuggest, v->value, sizeof(peer->mohsuggest));
16822 } else if (!strcasecmp(v->name, "mailbox")) {
16823 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
16824 } else if (!strcasecmp(v->name, "subscribemwi")) {
16825 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
16826 } else if (!strcasecmp(v->name, "vmexten")) {
16827 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
16828 } else if (!strcasecmp(v->name, "callgroup")) {
16829 peer->callgroup = ast_get_group(v->value);
16830 } else if (!strcasecmp(v->name, "allowtransfer")) {
16831 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
16832 } else if (!strcasecmp(v->name, "pickupgroup")) {
16833 peer->pickupgroup = ast_get_group(v->value);
16834 } else if (!strcasecmp(v->name, "allow")) {
16835 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
16836 } else if (!strcasecmp(v->name, "disallow")) {
16837 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
16838 } else if (!strcasecmp(v->name, "autoframing")) {
16839 peer->autoframing = ast_true(v->value);
16840 } else if (!strcasecmp(v->name, "rtptimeout")) {
16841 if ((sscanf(v->value, "%d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
16842 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16843 peer->rtptimeout = global_rtptimeout;
16844 }
16845 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
16846 if ((sscanf(v->value, "%d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
16847 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16848 peer->rtpholdtimeout = global_rtpholdtimeout;
16849 }
16850 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
16851 if ((sscanf(v->value, "%d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
16852 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
16853 peer->rtpkeepalive = global_rtpkeepalive;
16854 }
16855 } else if (!strcasecmp(v->name, "setvar")) {
16856
16857 varname = ast_strdupa(v->value);
16858 if ((varval = strchr(varname, '='))) {
16859 *varval++ = '\0';
16860 if ((tmpvar = ast_variable_new(varname, varval))) {
16861 tmpvar->next = peer->chanvars;
16862 peer->chanvars = tmpvar;
16863 }
16864 }
16865 } else if (!strcasecmp(v->name, "qualify")) {
16866 if (!strcasecmp(v->value, "no")) {
16867 peer->maxms = 0;
16868 } else if (!strcasecmp(v->value, "yes")) {
16869 peer->maxms = default_qualify ? default_qualify : DEFAULT_MAXMS;
16870 } else if (sscanf(v->value, "%d", &peer->maxms) != 1) {
16871 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);
16872 peer->maxms = 0;
16873 }
16874 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
16875 peer->maxcallbitrate = atoi(v->value);
16876 if (peer->maxcallbitrate < 0)
16877 peer->maxcallbitrate = default_maxcallbitrate;
16878 }
16879 }
16880 if (!ast_strlen_zero(fullcontact)) {
16881 ast_copy_string(peer->fullcontact, fullcontact, sizeof(peer->fullcontact));
16882 }
16883
16884 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && realtime) {
16885 time_t nowtime = time(NULL);
16886
16887 if ((nowtime - regseconds) > 0) {
16888 destroy_association(peer);
16889 memset(&peer->addr, 0, sizeof(peer->addr));
16890 if (option_debug)
16891 ast_log(LOG_DEBUG, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
16892 }
16893 }
16894 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
16895 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
16896 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
16897 global_allowsubscribe = TRUE;
16898 if (!found && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !ast_test_flag(&peer->flags[0], SIP_REALTIME))
16899 reg_source_db(peer);
16900 ASTOBJ_UNMARK(peer);
16901 ast_free_ha(oldha);
16902 return peer;
16903 }
16904
16905
16906
16907
16908
16909
16910
16911 static int reload_config(enum channelreloadreason reason)
16912 {
16913 struct ast_config *cfg, *ucfg;
16914 struct ast_variable *v;
16915 struct sip_peer *peer;
16916 struct sip_user *user;
16917 struct ast_hostent ahp;
16918 char *cat, *stringp, *context, *oldregcontext;
16919 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
16920 struct hostent *hp;
16921 int format;
16922 struct ast_flags dummy[2];
16923 int auto_sip_domains = FALSE;
16924 struct sockaddr_in old_bindaddr = bindaddr;
16925 int registry_count = 0, peer_count = 0, user_count = 0;
16926 unsigned int temp_tos = 0;
16927 struct ast_flags debugflag = {0};
16928
16929 cfg = ast_config_load(config);
16930
16931
16932 if (!cfg) {
16933 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
16934 return -1;
16935 }
16936
16937 if (option_debug > 3)
16938 ast_log(LOG_DEBUG, "--------------- SIP reload started\n");
16939
16940 clear_realm_authentication(authl);
16941 clear_sip_domains();
16942 authl = NULL;
16943
16944
16945
16946 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
16947 ASTOBJ_RDLOCK(iterator);
16948 if (iterator->call) {
16949 if (option_debug > 2)
16950 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
16951
16952 sip_destroy(iterator->call);
16953 }
16954 ASTOBJ_UNLOCK(iterator);
16955
16956 } while(0));
16957
16958
16959 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
16960 if (option_debug > 3)
16961 ast_log(LOG_DEBUG, "--------------- Done destroying user list\n");
16962 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
16963 if (option_debug > 3)
16964 ast_log(LOG_DEBUG, "--------------- Done destroying registry list\n");
16965 ASTOBJ_CONTAINER_MARKALL(&peerl);
16966
16967
16968 ast_copy_string(oldcontexts, global_regcontext, sizeof(oldcontexts));
16969 oldregcontext = oldcontexts;
16970
16971
16972
16973 ast_copy_flags(&debugflag, &global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
16974 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
16975 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
16976 ast_copy_flags(&global_flags[1], &debugflag, SIP_PAGE2_DEBUG_CONSOLE);
16977
16978
16979 memset(&bindaddr, 0, sizeof(bindaddr));
16980 ast_free_ha(localaddr);
16981 memset(&localaddr, 0, sizeof(localaddr));
16982 memset(&externip, 0, sizeof(externip));
16983 memset(&default_prefs, 0 , sizeof(default_prefs));
16984 outboundproxyip.sin_port = htons(STANDARD_SIP_PORT);
16985 outboundproxyip.sin_family = AF_INET;
16986 ourport = STANDARD_SIP_PORT;
16987 srvlookup = DEFAULT_SRVLOOKUP;
16988 global_tos_sip = DEFAULT_TOS_SIP;
16989 global_tos_audio = DEFAULT_TOS_AUDIO;
16990 global_tos_video = DEFAULT_TOS_VIDEO;
16991 externhost[0] = '\0';
16992 externexpire = 0;
16993 externrefresh = 10;
16994 memset(&outboundproxyip, 0, sizeof(outboundproxyip));
16995
16996
16997 allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
16998 global_regcontext[0] = '\0';
16999 expiry = DEFAULT_EXPIRY;
17000 global_notifyringing = DEFAULT_NOTIFYRINGING;
17001 global_limitonpeers = FALSE;
17002 global_directrtpsetup = FALSE;
17003 global_notifyhold = FALSE;
17004 global_alwaysauthreject = 0;
17005 global_allowsubscribe = FALSE;
17006 ast_copy_string(global_useragent, DEFAULT_USERAGENT, sizeof(global_useragent));
17007 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
17008 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME))
17009 ast_copy_string(global_realm, DEFAULT_REALM, sizeof(global_realm));
17010 else
17011 ast_copy_string(global_realm, ast_config_AST_SYSTEM_NAME, sizeof(global_realm));
17012 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
17013 compactheaders = DEFAULT_COMPACTHEADERS;
17014 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
17015 global_regattempts_max = 0;
17016 pedanticsipchecking = DEFAULT_PEDANTIC;
17017 global_mwitime = DEFAULT_MWITIME;
17018 autocreatepeer = DEFAULT_AUTOCREATEPEER;
17019 global_autoframing = 0;
17020 global_allowguest = DEFAULT_ALLOWGUEST;
17021 global_rtptimeout = 0;
17022 global_rtpholdtimeout = 0;
17023 global_rtpkeepalive = 0;
17024 global_allowtransfer = TRANSFER_OPENFORALL;
17025 global_rtautoclear = 120;
17026 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
17027 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
17028 ast_set_flag(&global_flags[1], SIP_PAGE2_RTUPDATE);
17029
17030
17031 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
17032 default_subscribecontext[0] = '\0';
17033 default_language[0] = '\0';
17034 default_fromdomain[0] = '\0';
17035 default_qualify = DEFAULT_QUALIFY;
17036 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
17037 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
17038 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
17039 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
17040 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
17041 ast_set_flag(&global_flags[0], SIP_NAT_RFC3581);
17042 ast_set_flag(&global_flags[0], SIP_CAN_REINVITE);
17043
17044
17045 dumphistory = FALSE;
17046 recordhistory = FALSE;
17047 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
17048
17049
17050 global_relaxdtmf = FALSE;
17051 global_callevents = FALSE;
17052 global_t1min = DEFAULT_T1MIN;
17053
17054 global_matchexterniplocally = FALSE;
17055
17056
17057 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
17058
17059 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT);
17060
17061
17062 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
17063 if (handle_common_options(&global_flags[0], &dummy[0], v))
17064 continue;
17065
17066 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
17067 continue;
17068
17069
17070 if (!strcasecmp(v->name, "context")) {
17071 ast_copy_string(default_context, v->value, sizeof(default_context));
17072 } else if (!strcasecmp(v->name, "subscribecontext")) {
17073 ast_copy_string(default_subscribecontext, v->value, sizeof(default_subscribecontext));
17074 } else if (!strcasecmp(v->name, "allowguest")) {
17075 global_allowguest = ast_true(v->value) ? 1 : 0;
17076 } else if (!strcasecmp(v->name, "realm")) {
17077 ast_copy_string(global_realm, v->value, sizeof(global_realm));
17078 } else if (!strcasecmp(v->name, "useragent")) {
17079 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
17080 if (option_debug)
17081 ast_log(LOG_DEBUG, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
17082 } else if (!strcasecmp(v->name, "allowtransfer")) {
17083 global_allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
17084 } else if (!strcasecmp(v->name, "rtcachefriends")) {
17085 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
17086 } else if (!strcasecmp(v->name, "rtsavesysname")) {
17087 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTSAVE_SYSNAME);
17088 } else if (!strcasecmp(v->name, "rtupdate")) {
17089 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTUPDATE);
17090 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
17091 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_IGNOREREGEXPIRE);
17092 } else if (!strcasecmp(v->name, "t1min")) {
17093 global_t1min = atoi(v->value);
17094 } else if (!strcasecmp(v->name, "rtautoclear")) {
17095 int i = atoi(v->value);
17096 if (i > 0)
17097 global_rtautoclear = i;
17098 else
17099 i = 0;
17100 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
17101 } else if (!strcasecmp(v->name, "usereqphone")) {
17102 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
17103 } else if (!strcasecmp(v->name, "relaxdtmf")) {
17104 global_relaxdtmf = ast_true(v->value);
17105 } else if (!strcasecmp(v->name, "checkmwi")) {
17106 if ((sscanf(v->value, "%d", &global_mwitime) != 1) || (global_mwitime < 0)) {
17107 ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d. Using default (10).\n", v->value, v->lineno);
17108 global_mwitime = DEFAULT_MWITIME;
17109 }
17110 } else if (!strcasecmp(v->name, "vmexten")) {
17111 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
17112 } else if (!strcasecmp(v->name, "rtptimeout")) {
17113 if ((sscanf(v->value, "%d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
17114 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
17115 global_rtptimeout = 0;
17116 }
17117 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
17118 if ((sscanf(v->value, "%d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
17119 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
17120 global_rtpholdtimeout = 0;
17121 }
17122 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
17123 if ((sscanf(v->value, "%d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
17124 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
17125 global_rtpkeepalive = 0;
17126 }
17127 } else if (!strcasecmp(v->name, "compactheaders")) {
17128 compactheaders = ast_true(v->value);
17129 } else if (!strcasecmp(v->name, "notifymimetype")) {
17130 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
17131 } else if (!strncasecmp(v->name, "limitonpeer", 11)) {
17132 global_limitonpeers = ast_true(v->value);
17133 } else if (!strcasecmp(v->name, "directrtpsetup")) {
17134 global_directrtpsetup = ast_true(v->value);
17135 } else if (!strcasecmp(v->name, "notifyringing")) {
17136 global_notifyringing = ast_true(v->value);
17137 } else if (!strcasecmp(v->name, "notifyhold")) {
17138 global_notifyhold = ast_true(v->value);
17139 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
17140 global_alwaysauthreject = ast_true(v->value);
17141 } else if (!strcasecmp(v->name, "mohinterpret")
17142 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
17143 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
17144 } else if (!strcasecmp(v->name, "mohsuggest")) {
17145 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
17146 } else if (!strcasecmp(v->name, "language")) {
17147 ast_copy_string(default_language, v->value, sizeof(default_language));
17148 } else if (!strcasecmp(v->name, "regcontext")) {
17149 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
17150 stringp = newcontexts;
17151
17152 cleanup_stale_contexts(stringp, oldregcontext);
17153
17154 while ((context = strsep(&stringp, "&"))) {
17155 if (!ast_context_find(context))
17156 ast_context_create(NULL, context,"SIP");
17157 }
17158 ast_copy_string(global_regcontext, v->value, sizeof(global_regcontext));
17159 } else if (!strcasecmp(v->name, "callerid")) {
17160 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
17161 } else if (!strcasecmp(v->name, "fromdomain")) {
17162 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
17163 } else if (!strcasecmp(v->name, "outboundproxy")) {
17164 if (ast_get_ip_or_srv(&outboundproxyip, v->value, srvlookup ? "_sip._udp" : NULL) < 0)
17165 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", v->value);
17166 } else if (!strcasecmp(v->name, "outboundproxyport")) {
17167
17168 sscanf(v->value, "%d", &format);
17169 outboundproxyip.sin_port = htons(format);
17170 } else if (!strcasecmp(v->name, "autocreatepeer")) {
17171 autocreatepeer = ast_true(v->value);
17172 } else if (!strcasecmp(v->name, "srvlookup")) {
17173 srvlookup = ast_true(v->value);
17174 } else if (!strcasecmp(v->name, "pedantic")) {
17175 pedanticsipchecking = ast_true(v->value);
17176 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
17177 max_expiry = atoi(v->value);
17178 if (max_expiry < 1)
17179 max_expiry = DEFAULT_MAX_EXPIRY;
17180 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
17181 min_expiry = atoi(v->value);
17182 if (min_expiry < 1)
17183 min_expiry = DEFAULT_MIN_EXPIRY;
17184 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
17185 default_expiry = atoi(v->value);
17186 if (default_expiry < 1)
17187 default_expiry = DEFAULT_DEFAULT_EXPIRY;
17188 } else if (!strcasecmp(v->name, "sipdebug")) {
17189 if (ast_true(v->value))
17190 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
17191 } else if (!strcasecmp(v->name, "dumphistory")) {
17192 dumphistory = ast_true(v->value);
17193 } else if (!strcasecmp(v->name, "recordhistory")) {
17194 recordhistory = ast_true(v->value);
17195 } else if (!strcasecmp(v->name, "registertimeout")) {
17196 global_reg_timeout = atoi(v->value);
17197 if (global_reg_timeout < 1)
17198 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
17199 } else if (!strcasecmp(v->name, "registerattempts")) {
17200 global_regattempts_max = atoi(v->value);
17201 } else if (!strcasecmp(v->name, "bindaddr")) {
17202 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
17203 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
17204 } else {
17205 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
17206 }
17207 } else if (!strcasecmp(v->name, "localnet")) {
17208 struct ast_ha *na;
17209 if (!(na = ast_append_ha("d", v->value, localaddr)))
17210 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
17211 else
17212 localaddr = na;
17213 } else if (!strcasecmp(v->name, "localmask")) {
17214 ast_log(LOG_WARNING, "Use of localmask is no long supported -- use localnet with mask syntax\n");
17215 } else if (!strcasecmp(v->name, "externip")) {
17216 if (!(hp = ast_gethostbyname(v->value, &ahp)))
17217 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
17218 else
17219 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
17220 externexpire = 0;
17221 } else if (!strcasecmp(v->name, "externhost")) {
17222 ast_copy_string(externhost, v->value, sizeof(externhost));
17223 if (!(hp = ast_gethostbyname(externhost, &ahp)))
17224 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
17225 else
17226 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
17227 externexpire = time(NULL);
17228 } else if (!strcasecmp(v->name, "externrefresh")) {
17229 if (sscanf(v->value, "%d", &externrefresh) != 1) {
17230 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
17231 externrefresh = 10;
17232 }
17233 } else if (!strcasecmp(v->name, "allow")) {
17234 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 1);
17235 } else if (!strcasecmp(v->name, "disallow")) {
17236 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 0);
17237 } else if (!strcasecmp(v->name, "autoframing")) {
17238 global_autoframing = ast_true(v->value);
17239 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
17240 allow_external_domains = ast_true(v->value);
17241 } else if (!strcasecmp(v->name, "autodomain")) {
17242 auto_sip_domains = ast_true(v->value);
17243 } else if (!strcasecmp(v->name, "domain")) {
17244 char *domain = ast_strdupa(v->value);
17245 char *context = strchr(domain, ',');
17246
17247 if (context)
17248 *context++ = '\0';
17249
17250 if (option_debug && ast_strlen_zero(context))
17251 ast_log(LOG_DEBUG, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
17252 if (ast_strlen_zero(domain))
17253 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
17254 else
17255 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, context ? ast_strip(context) : "");
17256 } else if (!strcasecmp(v->name, "register")) {
17257 if (sip_register(v->value, v->lineno) == 0)
17258 registry_count++;
17259 } else if (!strcasecmp(v->name, "tos")) {
17260 if (!ast_str2tos(v->value, &temp_tos)) {
17261 global_tos_sip = temp_tos;
17262 global_tos_audio = temp_tos;
17263 global_tos_video = temp_tos;
17264 ast_log(LOG_WARNING, "tos value at line %d is deprecated. See doc/ip-tos.txt for more information.\n", v->lineno);
17265 } else
17266 ast_log(LOG_WARNING, "Invalid tos value at line %d, See doc/ip-tos.txt for more information.\n", v->lineno);
17267 } else if (!strcasecmp(v->name, "tos_sip")) {
17268 if (ast_str2tos(v->value, &global_tos_sip))
17269 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, recommended value is 'cs3'. See doc/ip-tos.txt.\n", v->lineno);
17270 } else if (!strcasecmp(v->name, "tos_audio")) {
17271 if (ast_str2tos(v->value, &global_tos_audio))
17272 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, recommended value is 'ef'. See doc/ip-tos.txt.\n", v->lineno);
17273 } else if (!strcasecmp(v->name, "tos_video")) {
17274 if (ast_str2tos(v->value, &global_tos_video))
17275 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, recommended value is 'af41'. See doc/ip-tos.txt.\n", v->lineno);
17276 } else if (!strcasecmp(v->name, "bindport")) {
17277 if (sscanf(v->value, "%d", &ourport) == 1) {
17278 bindaddr.sin_port = htons(ourport);
17279 } else {
17280 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
17281 }
17282 } else if (!strcasecmp(v->name, "qualify")) {
17283 if (!strcasecmp(v->value, "no")) {
17284 default_qualify = 0;
17285 } else if (!strcasecmp(v->value, "yes")) {
17286 default_qualify = DEFAULT_MAXMS;
17287 } else if (sscanf(v->value, "%d", &default_qualify) != 1) {
17288 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
17289 default_qualify = 0;
17290 }
17291 } else if (!strcasecmp(v->name, "callevents")) {
17292 global_callevents = ast_true(v->value);
17293 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
17294 default_maxcallbitrate = atoi(v->value);
17295 if (default_maxcallbitrate < 0)
17296 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
17297 } else if (!strcasecmp(v->name, "matchexterniplocally")) {
17298 global_matchexterniplocally = ast_true(v->value);
17299 }
17300 }
17301
17302 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
17303 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
17304 allow_external_domains = 1;
17305 }
17306
17307
17308 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
17309
17310 if (!strcasecmp(v->name, "auth"))
17311 authl = add_realm_authentication(authl, v->value, v->lineno);
17312 }
17313
17314 ucfg = ast_config_load("users.conf");
17315 if (ucfg) {
17316 struct ast_variable *gen;
17317 int genhassip, genregistersip;
17318 const char *hassip, *registersip;
17319
17320 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
17321 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
17322 gen = ast_variable_browse(ucfg, "general");
17323 cat = ast_category_browse(ucfg, NULL);
17324 while (cat) {
17325 if (strcasecmp(cat, "general")) {
17326 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
17327 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
17328 if (ast_true(hassip) || (!hassip && genhassip)) {
17329 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
17330 if (user) {
17331 ASTOBJ_CONTAINER_LINK(&userl,user);
17332 ASTOBJ_UNREF(user, sip_destroy_user);
17333 user_count++;
17334 }
17335 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
17336 if (peer) {
17337 ast_device_state_changed("SIP/%s", peer->name);
17338 ASTOBJ_CONTAINER_LINK(&peerl,peer);
17339 ASTOBJ_UNREF(peer, sip_destroy_peer);
17340 peer_count++;
17341 }
17342 }
17343 if (ast_true(registersip) || (!registersip && genregistersip)) {
17344 char tmp[256];
17345 const char *host = ast_variable_retrieve(ucfg, cat, "host");
17346 const char *username = ast_variable_retrieve(ucfg, cat, "username");
17347 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
17348 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
17349 if (!host)
17350 host = ast_variable_retrieve(ucfg, "general", "host");
17351 if (!username)
17352 username = ast_variable_retrieve(ucfg, "general", "username");
17353 if (!secret)
17354 secret = ast_variable_retrieve(ucfg, "general", "secret");
17355 if (!contact)
17356 contact = "s";
17357 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
17358 if (!ast_strlen_zero(secret))
17359 snprintf(tmp, sizeof(tmp), "%s:%s@%s/%s", username, secret, host, contact);
17360 else
17361 snprintf(tmp, sizeof(tmp), "%s@%s/%s", username, host, contact);
17362 if (sip_register(tmp, 0) == 0)
17363 registry_count++;
17364 }
17365 }
17366 }
17367 cat = ast_category_browse(ucfg, cat);
17368 }
17369 ast_config_destroy(ucfg);
17370 }
17371
17372
17373
17374 cat = NULL;
17375 while ( (cat = ast_category_browse(cfg, cat)) ) {
17376 const char *utype;
17377 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
17378 continue;
17379 utype = ast_variable_retrieve(cfg, cat, "type");
17380 if (!utype) {
17381 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
17382 continue;
17383 } else {
17384 int is_user = 0, is_peer = 0;
17385 if (!strcasecmp(utype, "user"))
17386 is_user = 1;
17387 else if (!strcasecmp(utype, "friend"))
17388 is_user = is_peer = 1;
17389 else if (!strcasecmp(utype, "peer"))
17390 is_peer = 1;
17391 else {
17392 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
17393 continue;
17394 }
17395 if (is_user) {
17396 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
17397 if (user) {
17398 ASTOBJ_CONTAINER_LINK(&userl,user);
17399 ASTOBJ_UNREF(user, sip_destroy_user);
17400 user_count++;
17401 }
17402 }
17403 if (is_peer) {
17404 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
17405 if (peer) {
17406 ASTOBJ_CONTAINER_LINK(&peerl,peer);
17407 ASTOBJ_UNREF(peer, sip_destroy_peer);
17408 peer_count++;
17409 }
17410 }
17411 }
17412 }
17413 if (ast_find_ourip(&__ourip, bindaddr)) {
17414 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
17415 ast_config_destroy(cfg);
17416 return 0;
17417 }
17418 if (!ntohs(bindaddr.sin_port))
17419 bindaddr.sin_port = ntohs(STANDARD_SIP_PORT);
17420 bindaddr.sin_family = AF_INET;
17421 ast_mutex_lock(&netlock);
17422 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
17423 close(sipsock);
17424 sipsock = -1;
17425 }
17426 if (sipsock < 0) {
17427 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
17428 if (sipsock < 0) {
17429 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
17430 ast_config_destroy(cfg);
17431 return -1;
17432 } else {
17433
17434 const int reuseFlag = 1;
17435
17436 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
17437 (const char*)&reuseFlag,
17438 sizeof reuseFlag);
17439
17440 ast_enable_packet_fragmentation(sipsock);
17441
17442 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
17443 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
17444 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
17445 strerror(errno));
17446 close(sipsock);
17447 sipsock = -1;
17448 } else {
17449 if (option_verbose > 1) {
17450 ast_verbose(VERBOSE_PREFIX_2 "SIP Listening on %s:%d\n",
17451 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
17452 ast_verbose(VERBOSE_PREFIX_2 "Using SIP TOS: %s\n", ast_tos2str(global_tos_sip));
17453 }
17454 if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &global_tos_sip, sizeof(global_tos_sip)))
17455 ast_log(LOG_WARNING, "Unable to set SIP TOS to %s\n", ast_tos2str(global_tos_sip));
17456 }
17457 }
17458 }
17459 ast_mutex_unlock(&netlock);
17460
17461
17462
17463
17464
17465 if (auto_sip_domains) {
17466 char temp[MAXHOSTNAMELEN];
17467
17468
17469 if (bindaddr.sin_addr.s_addr)
17470 add_sip_domain(ast_inet_ntoa(bindaddr.sin_addr), SIP_DOMAIN_AUTO, NULL);
17471 else
17472 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
17473
17474
17475 if (externip.sin_addr.s_addr)
17476 add_sip_domain(ast_inet_ntoa(externip.sin_addr), SIP_DOMAIN_AUTO, NULL);
17477
17478
17479 if (!ast_strlen_zero(externhost))
17480 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
17481
17482
17483 if (!gethostname(temp, sizeof(temp)))
17484 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
17485 }
17486
17487
17488 ast_config_destroy(cfg);
17489
17490
17491 if (notify_types)
17492 ast_config_destroy(notify_types);
17493 notify_types = ast_config_load(notify_config);
17494
17495
17496 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);
17497
17498 return 0;
17499 }
17500
17501 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
17502 {
17503 struct sip_pvt *p;
17504 struct ast_udptl *udptl = NULL;
17505
17506 p = chan->tech_pvt;
17507 if (!p)
17508 return NULL;
17509
17510 ast_mutex_lock(&p->lock);
17511 if (p->udptl && ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
17512 udptl = p->udptl;
17513 ast_mutex_unlock(&p->lock);
17514 return udptl;
17515 }
17516
17517 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
17518 {
17519 struct sip_pvt *p;
17520
17521 p = chan->tech_pvt;
17522 if (!p)
17523 return -1;
17524 ast_mutex_lock(&p->lock);
17525 if (udptl)
17526 ast_udptl_get_peer(udptl, &p->udptlredirip);
17527 else
17528 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
17529 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
17530 if (!p->pendinginvite) {
17531 if (option_debug > 2) {
17532 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);
17533 }
17534 transmit_reinvite_with_t38_sdp(p);
17535 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
17536 if (option_debug > 2) {
17537 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);
17538 }
17539 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
17540 }
17541 }
17542
17543 p->lastrtprx = p->lastrtptx = time(NULL);
17544 ast_mutex_unlock(&p->lock);
17545 return 0;
17546 }
17547
17548
17549
17550
17551
17552
17553 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite)
17554 {
17555 struct sip_pvt *p;
17556 int flag = 0;
17557
17558 p = chan->tech_pvt;
17559 if (!p || !pvt->udptl)
17560 return -1;
17561
17562
17563 ast_mutex_lock(&p->lock);
17564
17565
17566
17567 p->t38.jointcapability = p->t38.peercapability = pvt->t38.jointcapability;
17568
17569 ast_udptl_set_far_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
17570 ast_udptl_set_local_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
17571 ast_udptl_set_error_correction_scheme(p->udptl, ast_udptl_get_error_correction_scheme(pvt->udptl));
17572
17573 if (reinvite) {
17574
17575
17576
17577
17578
17579 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
17580 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
17581 flag =1;
17582 } else {
17583 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
17584 }
17585 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
17586 if (!p->pendinginvite) {
17587 if (option_debug > 2) {
17588 if (flag)
17589 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));
17590 else
17591 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));
17592 }
17593 transmit_reinvite_with_t38_sdp(p);
17594 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
17595 if (option_debug > 2) {
17596 if (flag)
17597 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));
17598 else
17599 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));
17600 }
17601 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
17602 }
17603 }
17604
17605 p->lastrtprx = p->lastrtptx = time(NULL);
17606 ast_mutex_unlock(&p->lock);
17607 return 0;
17608 } else {
17609 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
17610 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
17611 flag = 1;
17612 } else {
17613 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
17614 }
17615 if (option_debug > 2) {
17616 if (flag)
17617 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));
17618 else
17619 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));
17620 }
17621 pvt->t38.state = T38_ENABLED;
17622 p->t38.state = T38_ENABLED;
17623 if (option_debug > 1) {
17624 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", pvt->t38.state, pvt->owner ? pvt->owner->name : "<none>");
17625 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
17626 }
17627 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
17628 p->lastrtprx = p->lastrtptx = time(NULL);
17629 ast_mutex_unlock(&p->lock);
17630 return 0;
17631 }
17632 }
17633
17634
17635
17636 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
17637 {
17638 struct sip_pvt *p = NULL;
17639 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
17640
17641 if (!(p = chan->tech_pvt))
17642 return AST_RTP_GET_FAILED;
17643
17644 ast_mutex_lock(&p->lock);
17645 if (!(p->rtp)) {
17646 ast_mutex_unlock(&p->lock);
17647 return AST_RTP_GET_FAILED;
17648 }
17649
17650 *rtp = p->rtp;
17651
17652 if (ast_rtp_getnat(*rtp) && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT))
17653 res = AST_RTP_TRY_PARTIAL;
17654 else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
17655 res = AST_RTP_TRY_NATIVE;
17656 else if (ast_test_flag(&global_jbconf, AST_JB_FORCED))
17657 res = AST_RTP_GET_FAILED;
17658
17659 ast_mutex_unlock(&p->lock);
17660
17661 return res;
17662 }
17663
17664
17665 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
17666 {
17667 struct sip_pvt *p = NULL;
17668 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
17669
17670 if (!(p = chan->tech_pvt))
17671 return AST_RTP_GET_FAILED;
17672
17673 ast_mutex_lock(&p->lock);
17674 if (!(p->vrtp)) {
17675 ast_mutex_unlock(&p->lock);
17676 return AST_RTP_GET_FAILED;
17677 }
17678
17679 *rtp = p->vrtp;
17680
17681 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
17682 res = AST_RTP_TRY_NATIVE;
17683
17684 ast_mutex_unlock(&p->lock);
17685
17686 return res;
17687 }
17688
17689
17690 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
17691 {
17692 struct sip_pvt *p;
17693 int changed = 0;
17694
17695 p = chan->tech_pvt;
17696 if (!p)
17697 return -1;
17698
17699
17700 if (chan->_state != AST_STATE_UP && !global_directrtpsetup)
17701 return 0;
17702
17703 ast_mutex_lock(&p->lock);
17704 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
17705
17706 ast_mutex_unlock(&p->lock);
17707 return 0;
17708 }
17709
17710
17711
17712
17713 if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
17714 ast_mutex_unlock(&p->lock);
17715 return 0;
17716 }
17717
17718 if (rtp) {
17719 changed |= ast_rtp_get_peer(rtp, &p->redirip);
17720 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
17721 memset(&p->redirip, 0, sizeof(p->redirip));
17722 changed = 1;
17723 }
17724 if (vrtp) {
17725 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
17726 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
17727 memset(&p->vredirip, 0, sizeof(p->vredirip));
17728 changed = 1;
17729 }
17730 if (codecs) {
17731 if ((p->redircodecs != codecs)) {
17732 p->redircodecs = codecs;
17733 changed = 1;
17734 }
17735 if ((p->capability & codecs) != p->capability) {
17736 p->jointcapability &= codecs;
17737 p->capability &= codecs;
17738 changed = 1;
17739 }
17740 }
17741 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
17742 if (chan->_state != AST_STATE_UP) {
17743 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
17744 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
17745 if (option_debug)
17746 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));
17747 } else if (!p->pendinginvite) {
17748 if (option_debug > 2) {
17749 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));
17750 }
17751 transmit_reinvite_with_sdp(p);
17752 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
17753 if (option_debug > 2) {
17754 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));
17755 }
17756
17757 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
17758 }
17759 }
17760
17761 p->lastrtprx = p->lastrtptx = time(NULL);
17762 ast_mutex_unlock(&p->lock);
17763 return 0;
17764 }
17765
17766 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
17767 static char *descrip_dtmfmode = "SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
17768 static char *app_dtmfmode = "SIPDtmfMode";
17769
17770 static char *app_sipaddheader = "SIPAddHeader";
17771 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
17772
17773 static char *descrip_sipaddheader = ""
17774 " SIPAddHeader(Header: Content)\n"
17775 "Adds a header to a SIP call placed with DIAL.\n"
17776 "Remember to user the X-header if you are adding non-standard SIP\n"
17777 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
17778 "Adding the wrong headers may jeopardize the SIP dialog.\n"
17779 "Always returns 0\n";
17780
17781
17782
17783 static int sip_dtmfmode(struct ast_channel *chan, void *data)
17784 {
17785 struct sip_pvt *p;
17786 char *mode;
17787 if (data)
17788 mode = (char *)data;
17789 else {
17790 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
17791 return 0;
17792 }
17793 ast_channel_lock(chan);
17794 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
17795 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
17796 ast_channel_unlock(chan);
17797 return 0;
17798 }
17799 p = chan->tech_pvt;
17800 if (!p) {
17801 ast_channel_unlock(chan);
17802 return 0;
17803 }
17804 ast_mutex_lock(&p->lock);
17805 if (!strcasecmp(mode,"info")) {
17806 ast_clear_flag(&p->flags[0], SIP_DTMF);
17807 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
17808 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
17809 } else if (!strcasecmp(mode,"rfc2833")) {
17810 ast_clear_flag(&p->flags[0], SIP_DTMF);
17811 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
17812 p->jointnoncodeccapability |= AST_RTP_DTMF;
17813 } else if (!strcasecmp(mode,"inband")) {
17814 ast_clear_flag(&p->flags[0], SIP_DTMF);
17815 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
17816 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
17817 } else
17818 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n",mode);
17819 if (p->rtp)
17820 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
17821 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
17822 if (!p->vad) {
17823 p->vad = ast_dsp_new();
17824 ast_dsp_set_features(p->vad, DSP_FEATURE_DTMF_DETECT);
17825 }
17826 } else {
17827 if (p->vad) {
17828 ast_dsp_free(p->vad);
17829 p->vad = NULL;
17830 }
17831 }
17832 ast_mutex_unlock(&p->lock);
17833 ast_channel_unlock(chan);
17834 return 0;
17835 }
17836
17837
17838 static int sip_addheader(struct ast_channel *chan, void *data)
17839 {
17840 int no = 0;
17841 int ok = FALSE;
17842 char varbuf[30];
17843 char *inbuf = (char *) data;
17844
17845 if (ast_strlen_zero(inbuf)) {
17846 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
17847 return 0;
17848 }
17849 ast_channel_lock(chan);
17850
17851
17852 while (!ok && no <= 50) {
17853 no++;
17854 snprintf(varbuf, sizeof(varbuf), "_SIPADDHEADER%.2d", no);
17855
17856
17857 if( (pbx_builtin_getvar_helper(chan, (const char *) varbuf + 1) == (const char *) NULL) )
17858 ok = TRUE;
17859 }
17860 if (ok) {
17861 pbx_builtin_setvar_helper (chan, varbuf, inbuf);
17862 if (sipdebug)
17863 ast_log(LOG_DEBUG,"SIP Header added \"%s\" as %s\n", inbuf, varbuf);
17864 } else {
17865 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
17866 }
17867 ast_channel_unlock(chan);
17868 return 0;
17869 }
17870
17871
17872
17873
17874
17875
17876
17877 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
17878 {
17879 char *cdest;
17880 char *extension, *host, *port;
17881 char tmp[80];
17882
17883 cdest = ast_strdupa(dest);
17884
17885 extension = strsep(&cdest, "@");
17886 host = strsep(&cdest, ":");
17887 port = strsep(&cdest, ":");
17888 if (ast_strlen_zero(extension)) {
17889 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
17890 return 0;
17891 }
17892
17893
17894 if (!host) {
17895 char *localtmp;
17896 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
17897 if (ast_strlen_zero(tmp)) {
17898 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
17899 return 0;
17900 }
17901 if ((localtmp = strcasestr(tmp, "sip:")) && (localtmp = strchr(localtmp, '@'))) {
17902 char lhost[80], lport[80];
17903 memset(lhost, 0, sizeof(lhost));
17904 memset(lport, 0, sizeof(lport));
17905 localtmp++;
17906
17907 sscanf(localtmp, "%[^<>:; ]:%[^<>:; ]", lhost, lport);
17908 if (ast_strlen_zero(lhost)) {
17909 ast_log(LOG_ERROR, "Can't find the host address\n");
17910 return 0;
17911 }
17912 host = ast_strdupa(lhost);
17913 if (!ast_strlen_zero(lport)) {
17914 port = ast_strdupa(lport);
17915 }
17916 }
17917 }
17918
17919 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
17920 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
17921
17922 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
17923 sip_alreadygone(p);
17924 return 0;
17925 }
17926
17927
17928 static int sip_get_codec(struct ast_channel *chan)
17929 {
17930 struct sip_pvt *p = chan->tech_pvt;
17931 return p->peercapability ? p->peercapability : p->capability;
17932 }
17933
17934
17935
17936
17937
17938 static void sip_poke_all_peers(void)
17939 {
17940 int ms = 0;
17941
17942 if (!speerobjs)
17943 return;
17944
17945 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
17946 ASTOBJ_WRLOCK(iterator);
17947 if (!AST_SCHED_DEL(sched, iterator->pokeexpire)) {
17948 struct sip_peer *peer_ptr = iterator;
17949 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17950 }
17951 ms += 100;
17952 iterator->pokeexpire = ast_sched_add(sched, ms, sip_poke_peer_s, ASTOBJ_REF(iterator));
17953 if (iterator->pokeexpire == -1) {
17954 struct sip_peer *peer_ptr = iterator;
17955 ASTOBJ_UNREF(peer_ptr, sip_destroy_peer);
17956 }
17957 ASTOBJ_UNLOCK(iterator);
17958 } while (0)
17959 );
17960 }
17961
17962
17963 static void sip_send_all_registers(void)
17964 {
17965 int ms;
17966 int regspacing;
17967 if (!regobjs)
17968 return;
17969 regspacing = default_expiry * 1000/regobjs;
17970 if (regspacing > 100)
17971 regspacing = 100;
17972 ms = regspacing;
17973 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
17974 ASTOBJ_WRLOCK(iterator);
17975 AST_SCHED_DEL(sched, iterator->expire);
17976 ms += regspacing;
17977 iterator->expire = ast_sched_add(sched, ms, sip_reregister, iterator);
17978 ASTOBJ_UNLOCK(iterator);
17979 } while (0)
17980 );
17981 }
17982
17983
17984 static int sip_do_reload(enum channelreloadreason reason)
17985 {
17986 reload_config(reason);
17987
17988
17989 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
17990 if (option_debug > 3)
17991 ast_log(LOG_DEBUG, "--------------- Done destroying pruned peers\n");
17992
17993
17994 sip_poke_all_peers();
17995
17996
17997 sip_send_all_registers();
17998
17999 if (option_debug > 3)
18000 ast_log(LOG_DEBUG, "--------------- SIP reload done\n");
18001
18002 return 0;
18003 }
18004
18005
18006 static int sip_reload(int fd, int argc, char *argv[])
18007 {
18008 ast_mutex_lock(&sip_reload_lock);
18009 if (sip_reloading)
18010 ast_verbose("Previous SIP reload not yet done\n");
18011 else {
18012 sip_reloading = TRUE;
18013 if (fd)
18014 sip_reloadreason = CHANNEL_CLI_RELOAD;
18015 else
18016 sip_reloadreason = CHANNEL_MODULE_RELOAD;
18017 }
18018 ast_mutex_unlock(&sip_reload_lock);
18019 restart_monitor();
18020
18021 return 0;
18022 }
18023
18024
18025 static int reload(void)
18026 {
18027 return sip_reload(0, 0, NULL);
18028 }
18029
18030 static struct ast_cli_entry cli_sip_debug_deprecated =
18031 { { "sip", "debug", NULL },
18032 sip_do_debug_deprecated, "Enable SIP debugging",
18033 debug_usage };
18034
18035 static struct ast_cli_entry cli_sip_no_debug_deprecated =
18036 { { "sip", "no", "debug", NULL },
18037 sip_no_debug_deprecated, "Disable SIP debugging",
18038 debug_usage };
18039
18040 static struct ast_cli_entry cli_sip[] = {
18041 { { "sip", "show", "channels", NULL },
18042 sip_show_channels, "List active SIP channels",
18043 show_channels_usage },
18044
18045 { { "sip", "show", "domains", NULL },
18046 sip_show_domains, "List our local SIP domains.",
18047 show_domains_usage },
18048
18049 { { "sip", "show", "inuse", NULL },
18050 sip_show_inuse, "List all inuse/limits",
18051 show_inuse_usage },
18052
18053 { { "sip", "show", "objects", NULL },
18054 sip_show_objects, "List all SIP object allocations",
18055 show_objects_usage },
18056
18057 { { "sip", "show", "peers", NULL },
18058 sip_show_peers, "List defined SIP peers",
18059 show_peers_usage },
18060
18061 { { "sip", "show", "registry", NULL },
18062 sip_show_registry, "List SIP registration status",
18063 show_reg_usage },
18064
18065 { { "sip", "show", "settings", NULL },
18066 sip_show_settings, "Show SIP global settings",
18067 show_settings_usage },
18068
18069 { { "sip", "show", "subscriptions", NULL },
18070 sip_show_subscriptions, "List active SIP subscriptions",
18071 show_subscriptions_usage },
18072
18073 { { "sip", "show", "users", NULL },
18074 sip_show_users, "List defined SIP users",
18075 show_users_usage },
18076
18077 { { "sip", "notify", NULL },
18078 sip_notify, "Send a notify packet to a SIP peer",
18079 notify_usage, complete_sipnotify },
18080
18081 { { "sip", "show", "channel", NULL },
18082 sip_show_channel, "Show detailed SIP channel info",
18083 show_channel_usage, complete_sipch },
18084
18085 { { "sip", "show", "history", NULL },
18086 sip_show_history, "Show SIP dialog history",
18087 show_history_usage, complete_sipch },
18088
18089 { { "sip", "show", "peer", NULL },
18090 sip_show_peer, "Show details on specific SIP peer",
18091 show_peer_usage, complete_sip_show_peer },
18092
18093 { { "sip", "show", "user", NULL },
18094 sip_show_user, "Show details on specific SIP user",
18095 show_user_usage, complete_sip_show_user },
18096
18097 { { "sip", "prune", "realtime", NULL },
18098 sip_prune_realtime, "Prune cached Realtime object(s)",
18099 prune_realtime_usage },
18100
18101 { { "sip", "prune", "realtime", "peer", NULL },
18102 sip_prune_realtime, "Prune cached Realtime peer(s)",
18103 prune_realtime_usage, complete_sip_prune_realtime_peer },
18104
18105 { { "sip", "prune", "realtime", "user", NULL },
18106 sip_prune_realtime, "Prune cached Realtime user(s)",
18107 prune_realtime_usage, complete_sip_prune_realtime_user },
18108
18109 { { "sip", "set", "debug", NULL },
18110 sip_do_debug, "Enable SIP debugging",
18111 debug_usage, NULL, &cli_sip_debug_deprecated },
18112
18113 { { "sip", "set", "debug", "ip", NULL },
18114 sip_do_debug, "Enable SIP debugging on IP",
18115 debug_usage },
18116
18117 { { "sip", "set", "debug", "peer", NULL },
18118 sip_do_debug, "Enable SIP debugging on Peername",
18119 debug_usage, complete_sip_debug_peer },
18120
18121 { { "sip", "set", "debug", "off", NULL },
18122 sip_no_debug, "Disable SIP debugging",
18123 no_debug_usage, NULL, &cli_sip_no_debug_deprecated },
18124
18125 { { "sip", "history", NULL },
18126 sip_do_history, "Enable SIP history",
18127 history_usage },
18128
18129 { { "sip", "history", "off", NULL },
18130 sip_no_history, "Disable SIP history",
18131 no_history_usage },
18132
18133 { { "sip", "reload", NULL },
18134 sip_reload, "Reload SIP configuration",
18135 sip_reload_usage },
18136 };
18137
18138
18139 static int load_module(void)
18140 {
18141 ASTOBJ_CONTAINER_INIT(&userl);
18142 ASTOBJ_CONTAINER_INIT(&peerl);
18143 ASTOBJ_CONTAINER_INIT(®l);
18144
18145 if (!(sched = sched_context_create())) {
18146 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
18147 return AST_MODULE_LOAD_FAILURE;
18148 }
18149
18150 if (!(io = io_context_create())) {
18151 ast_log(LOG_ERROR, "Unable to create I/O context\n");
18152 sched_context_destroy(sched);
18153 return AST_MODULE_LOAD_FAILURE;
18154 }
18155
18156 sip_reloadreason = CHANNEL_MODULE_LOAD;
18157
18158 if(reload_config(sip_reloadreason))
18159 return AST_MODULE_LOAD_DECLINE;
18160
18161
18162 if (ast_channel_register(&sip_tech)) {
18163 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
18164 io_context_destroy(io);
18165 sched_context_destroy(sched);
18166 return AST_MODULE_LOAD_FAILURE;
18167 }
18168
18169
18170 ast_cli_register_multiple(cli_sip, sizeof(cli_sip)/ sizeof(struct ast_cli_entry));
18171
18172
18173 ast_rtp_proto_register(&sip_rtp);
18174
18175
18176 ast_udptl_proto_register(&sip_udptl);
18177
18178
18179 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
18180 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
18181
18182
18183 ast_custom_function_register(&sip_header_function);
18184 ast_custom_function_register(&sippeer_function);
18185 ast_custom_function_register(&sipchaninfo_function);
18186 ast_custom_function_register(&checksipdomain_function);
18187
18188
18189 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM, manager_sip_show_peers,
18190 "List SIP peers (text format)", mandescr_show_peers);
18191 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM, manager_sip_show_peer,
18192 "Show SIP peer (text format)", mandescr_show_peer);
18193
18194 sip_poke_all_peers();
18195 sip_send_all_registers();
18196
18197
18198 restart_monitor();
18199
18200 return AST_MODULE_LOAD_SUCCESS;
18201 }
18202
18203
18204 static int unload_module(void)
18205 {
18206 struct sip_pvt *p, *pl;
18207
18208
18209 ast_channel_unregister(&sip_tech);
18210
18211
18212 ast_custom_function_unregister(&sipchaninfo_function);
18213 ast_custom_function_unregister(&sippeer_function);
18214 ast_custom_function_unregister(&sip_header_function);
18215 ast_custom_function_unregister(&checksipdomain_function);
18216
18217
18218 ast_unregister_application(app_dtmfmode);
18219 ast_unregister_application(app_sipaddheader);
18220
18221
18222 ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
18223
18224
18225 ast_rtp_proto_unregister(&sip_rtp);
18226
18227
18228 ast_udptl_proto_unregister(&sip_udptl);
18229
18230
18231 ast_manager_unregister("SIPpeers");
18232 ast_manager_unregister("SIPshowpeer");
18233
18234 ast_mutex_lock(&iflock);
18235
18236 for (p = iflist; p ; p = p->next) {
18237 if (p->owner)
18238 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
18239 }
18240 ast_mutex_unlock(&iflock);
18241
18242 ast_mutex_lock(&monlock);
18243 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
18244 pthread_cancel(monitor_thread);
18245 pthread_kill(monitor_thread, SIGURG);
18246 pthread_join(monitor_thread, NULL);
18247 }
18248 monitor_thread = AST_PTHREADT_STOP;
18249 ast_mutex_unlock(&monlock);
18250
18251 restartdestroy:
18252 ast_mutex_lock(&iflock);
18253
18254 p = iflist;
18255 while (p) {
18256 pl = p;
18257 p = p->next;
18258 if (__sip_destroy(pl, TRUE) < 0) {
18259
18260 iflist = p;
18261 ast_mutex_unlock(&iflock);
18262 usleep(1);
18263 goto restartdestroy;
18264 }
18265 }
18266 iflist = NULL;
18267 ast_mutex_unlock(&iflock);
18268
18269
18270 ast_free_ha(localaddr);
18271
18272 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
18273 ASTOBJ_CONTAINER_DESTROY(&userl);
18274 ASTOBJ_CONTAINER_DESTROYALL(&peerl, sip_destroy_peer);
18275 ASTOBJ_CONTAINER_DESTROY(&peerl);
18276 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
18277 ASTOBJ_CONTAINER_DESTROY(®l);
18278
18279 clear_realm_authentication(authl);
18280 clear_sip_domains();
18281 close(sipsock);
18282 sched_context_destroy(sched);
18283
18284 return 0;
18285 }
18286
18287 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)",
18288 .load = load_module,
18289 .unload = unload_module,
18290 .reload = reload,
18291 );