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 #include "asterisk.h"
00092
00093 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 78416 $")
00094
00095 #include <stdio.h>
00096 #include <ctype.h>
00097 #include <string.h>
00098 #include <unistd.h>
00099 #include <sys/socket.h>
00100 #include <sys/ioctl.h>
00101 #include <net/if.h>
00102 #include <errno.h>
00103 #include <stdlib.h>
00104 #include <fcntl.h>
00105 #include <netdb.h>
00106 #include <signal.h>
00107 #include <sys/signal.h>
00108 #include <netinet/in.h>
00109 #include <netinet/in_systm.h>
00110 #include <arpa/inet.h>
00111 #include <netinet/ip.h>
00112 #include <regex.h>
00113
00114 #include "asterisk/lock.h"
00115 #include "asterisk/channel.h"
00116 #include "asterisk/config.h"
00117 #include "asterisk/logger.h"
00118 #include "asterisk/module.h"
00119 #include "asterisk/pbx.h"
00120 #include "asterisk/options.h"
00121 #include "asterisk/sched.h"
00122 #include "asterisk/io.h"
00123 #include "asterisk/rtp.h"
00124 #include "asterisk/udptl.h"
00125 #include "asterisk/acl.h"
00126 #include "asterisk/manager.h"
00127 #include "asterisk/callerid.h"
00128 #include "asterisk/cli.h"
00129 #include "asterisk/app.h"
00130 #include "asterisk/musiconhold.h"
00131 #include "asterisk/dsp.h"
00132 #include "asterisk/features.h"
00133 #include "asterisk/srv.h"
00134 #include "asterisk/astdb.h"
00135 #include "asterisk/causes.h"
00136 #include "asterisk/utils.h"
00137 #include "asterisk/file.h"
00138 #include "asterisk/astobj.h"
00139 #include "asterisk/devicestate.h"
00140 #include "asterisk/linkedlists.h"
00141 #include "asterisk/stringfields.h"
00142 #include "asterisk/monitor.h"
00143 #include "asterisk/localtime.h"
00144 #include "asterisk/abstract_jb.h"
00145 #include "asterisk/compiler.h"
00146 #include "asterisk/threadstorage.h"
00147 #include "asterisk/translate.h"
00148
00149 #ifndef FALSE
00150 #define FALSE 0
00151 #endif
00152
00153 #ifndef TRUE
00154 #define TRUE 1
00155 #endif
00156
00157 #define XMIT_ERROR -2
00158
00159 #define VIDEO_CODEC_MASK 0x1fc0000
00160 #ifndef IPTOS_MINCOST
00161 #define IPTOS_MINCOST 0x02
00162 #endif
00163
00164
00165
00166 #define DEFAULT_DEFAULT_EXPIRY 120
00167 #define DEFAULT_MIN_EXPIRY 60
00168 #define DEFAULT_MAX_EXPIRY 3600
00169 #define DEFAULT_REGISTRATION_TIMEOUT 20
00170 #define DEFAULT_MAX_FORWARDS "70"
00171
00172
00173
00174 #define EXPIRY_GUARD_SECS 15
00175 #define EXPIRY_GUARD_LIMIT 30
00176
00177 #define EXPIRY_GUARD_MIN 500
00178
00179
00180
00181 #define EXPIRY_GUARD_PCT 0.20
00182
00183 #define DEFAULT_EXPIRY 900
00184
00185 static int min_expiry = DEFAULT_MIN_EXPIRY;
00186 static int max_expiry = DEFAULT_MAX_EXPIRY;
00187 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00188 static int expiry = DEFAULT_EXPIRY;
00189
00190 #ifndef MAX
00191 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00192 #endif
00193
00194 #define CALLERID_UNKNOWN "Unknown"
00195
00196 #define DEFAULT_MAXMS 2000
00197 #define DEFAULT_FREQ_OK 60 * 1000
00198 #define DEFAULT_FREQ_NOTOK 10 * 1000
00199
00200 #define DEFAULT_RETRANS 1000
00201 #define MAX_RETRANS 6
00202 #define SIP_TRANS_TIMEOUT 32000
00203
00204
00205 #define DEFAULT_TRANS_TIMEOUT -1
00206 #define MAX_AUTHTRIES 3
00207
00208 #define SIP_MAX_HEADERS 64
00209 #define SIP_MAX_LINES 64
00210 #define SIP_MAX_PACKET 4096
00211
00212 #define INITIAL_CSEQ 101
00213
00214
00215 static struct ast_jb_conf default_jbconf =
00216 {
00217 .flags = 0,
00218 .max_size = -1,
00219 .resync_threshold = -1,
00220 .impl = ""
00221 };
00222 static struct ast_jb_conf global_jbconf;
00223
00224 static const char config[] = "sip.conf";
00225 static const char notify_config[] = "sip_notify.conf";
00226
00227 #define RTP 1
00228 #define NO_RTP 0
00229
00230
00231
00232
00233 enum transfermodes {
00234 TRANSFER_OPENFORALL,
00235 TRANSFER_CLOSED,
00236 };
00237
00238
00239 enum sip_result {
00240 AST_SUCCESS = 0,
00241 AST_FAILURE = -1,
00242 };
00243
00244
00245
00246
00247 enum invitestates {
00248 INV_NONE = 0,
00249 INV_CALLING = 1,
00250 INV_PROCEEDING = 2,
00251 INV_EARLY_MEDIA = 3,
00252 INV_COMPLETED = 4,
00253 INV_CONFIRMED = 5,
00254 INV_TERMINATED = 6,
00255
00256 INV_CANCELLED = 7,
00257 };
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 enum xmittype {
00268 XMIT_CRITICAL = 2,
00269
00270 XMIT_RELIABLE = 1,
00271 XMIT_UNRELIABLE = 0,
00272 };
00273
00274 enum parse_register_result {
00275 PARSE_REGISTER_FAILED,
00276 PARSE_REGISTER_UPDATE,
00277 PARSE_REGISTER_QUERY,
00278 };
00279
00280 enum subscriptiontype {
00281 NONE = 0,
00282 XPIDF_XML,
00283 DIALOG_INFO_XML,
00284 CPIM_PIDF_XML,
00285 PIDF_XML,
00286 MWI_NOTIFICATION
00287 };
00288
00289 static const struct cfsubscription_types {
00290 enum subscriptiontype type;
00291 const char * const event;
00292 const char * const mediatype;
00293 const char * const text;
00294 } subscription_types[] = {
00295 { NONE, "-", "unknown", "unknown" },
00296
00297 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00298 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00299 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00300 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00301 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00302 };
00303
00304
00305 enum sipmethod {
00306 SIP_UNKNOWN,
00307 SIP_RESPONSE,
00308 SIP_REGISTER,
00309 SIP_OPTIONS,
00310 SIP_NOTIFY,
00311 SIP_INVITE,
00312 SIP_ACK,
00313 SIP_PRACK,
00314 SIP_BYE,
00315 SIP_REFER,
00316 SIP_SUBSCRIBE,
00317 SIP_MESSAGE,
00318 SIP_UPDATE,
00319 SIP_INFO,
00320 SIP_CANCEL,
00321 SIP_PUBLISH,
00322 SIP_PING,
00323 };
00324
00325
00326
00327
00328
00329
00330 enum sip_auth_type {
00331 PROXY_AUTH,
00332 WWW_AUTH,
00333 };
00334
00335
00336 enum check_auth_result {
00337 AUTH_SUCCESSFUL = 0,
00338 AUTH_CHALLENGE_SENT = 1,
00339 AUTH_SECRET_FAILED = -1,
00340 AUTH_USERNAME_MISMATCH = -2,
00341 AUTH_NOT_FOUND = -3,
00342 AUTH_FAKE_AUTH = -4,
00343 AUTH_UNKNOWN_DOMAIN = -5,
00344 AUTH_PEER_NOT_DYNAMIC = -6,
00345 AUTH_ACL_FAILED = -7,
00346 };
00347
00348
00349 enum sipregistrystate {
00350 REG_STATE_UNREGISTERED = 0,
00351 REG_STATE_REGSENT,
00352 REG_STATE_AUTHSENT,
00353 REG_STATE_REGISTERED,
00354 REG_STATE_REJECTED,
00355 REG_STATE_TIMEOUT,
00356 REG_STATE_NOAUTH,
00357 REG_STATE_FAILED,
00358 };
00359
00360 #define CAN_NOT_CREATE_DIALOG 0
00361 #define CAN_CREATE_DIALOG 1
00362 #define CAN_CREATE_DIALOG_UNSUPPORTED_METHOD 2
00363
00364
00365 static const struct cfsip_methods {
00366 enum sipmethod id;
00367 int need_rtp;
00368 char * const text;
00369 int can_create;
00370 } sip_methods[] = {
00371 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00372 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00373 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00374 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00375 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00376 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00377 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00378 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00379 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00380 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00381 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00382 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00383 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00384 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00385 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00386 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00387 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00388 };
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 #define SUPPORTED 1
00401 #define NOT_SUPPORTED 0
00402
00403 #define SIP_OPT_REPLACES (1 << 0)
00404 #define SIP_OPT_100REL (1 << 1)
00405 #define SIP_OPT_TIMER (1 << 2)
00406 #define SIP_OPT_EARLY_SESSION (1 << 3)
00407 #define SIP_OPT_JOIN (1 << 4)
00408 #define SIP_OPT_PATH (1 << 5)
00409 #define SIP_OPT_PREF (1 << 6)
00410 #define SIP_OPT_PRECONDITION (1 << 7)
00411 #define SIP_OPT_PRIVACY (1 << 8)
00412 #define SIP_OPT_SDP_ANAT (1 << 9)
00413 #define SIP_OPT_SEC_AGREE (1 << 10)
00414 #define SIP_OPT_EVENTLIST (1 << 11)
00415 #define SIP_OPT_GRUU (1 << 12)
00416 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00417 #define SIP_OPT_NOREFERSUB (1 << 14)
00418 #define SIP_OPT_HISTINFO (1 << 15)
00419 #define SIP_OPT_RESPRIORITY (1 << 16)
00420
00421
00422
00423 static const struct cfsip_options {
00424 int id;
00425 int supported;
00426 char * const text;
00427 } sip_options[] = {
00428
00429 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00430
00431 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00432
00433 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00434
00435 { SIP_OPT_TIMER, NOT_SUPPORTED, "timer" },
00436
00437 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00438
00439 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00440
00441 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00442
00443 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00444
00445 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00446
00447 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00448
00449 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00450
00451 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00452
00453 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00454
00455 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00456
00457 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00458
00459 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00460
00461 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00462
00463 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00464 };
00465
00466
00467
00468 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
00469
00470
00471 #define SUPPORTED_EXTENSIONS "replaces"
00472
00473
00474 #define STANDARD_SIP_PORT 5060
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 #define DEFAULT_CONTEXT "default"
00488 #define DEFAULT_MOHINTERPRET "default"
00489 #define DEFAULT_MOHSUGGEST ""
00490 #define DEFAULT_VMEXTEN "asterisk"
00491 #define DEFAULT_CALLERID "asterisk"
00492 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00493 #define DEFAULT_MWITIME 10
00494 #define DEFAULT_ALLOWGUEST TRUE
00495 #define DEFAULT_SRVLOOKUP FALSE
00496 #define DEFAULT_COMPACTHEADERS FALSE
00497 #define DEFAULT_TOS_SIP 0
00498 #define DEFAULT_TOS_AUDIO 0
00499 #define DEFAULT_TOS_VIDEO 0
00500 #define DEFAULT_ALLOW_EXT_DOM TRUE
00501 #define DEFAULT_REALM "asterisk"
00502 #define DEFAULT_NOTIFYRINGING TRUE
00503 #define DEFAULT_PEDANTIC FALSE
00504 #define DEFAULT_AUTOCREATEPEER FALSE
00505 #define DEFAULT_QUALIFY FALSE
00506 #define DEFAULT_T1MIN 100
00507 #define DEFAULT_MAX_CALL_BITRATE (384)
00508 #ifndef DEFAULT_USERAGENT
00509 #define DEFAULT_USERAGENT "Asterisk PBX"
00510 #endif
00511
00512
00513
00514
00515 static char default_context[AST_MAX_CONTEXT];
00516 static char default_subscribecontext[AST_MAX_CONTEXT];
00517 static char default_language[MAX_LANGUAGE];
00518 static char default_callerid[AST_MAX_EXTENSION];
00519 static char default_fromdomain[AST_MAX_EXTENSION];
00520 static char default_notifymime[AST_MAX_EXTENSION];
00521 static int default_qualify;
00522 static char default_vmexten[AST_MAX_EXTENSION];
00523 static char default_mohinterpret[MAX_MUSICCLASS];
00524 static char default_mohsuggest[MAX_MUSICCLASS];
00525
00526 static int default_maxcallbitrate;
00527 static struct ast_codec_pref default_prefs;
00528
00529
00530 static int global_directrtpsetup;
00531 static int global_limitonpeers;
00532 static int global_rtautoclear;
00533 static int global_notifyringing;
00534 static int global_notifyhold;
00535 static int global_alwaysauthreject;
00536 static int srvlookup;
00537 static int pedanticsipchecking;
00538 static int autocreatepeer;
00539 static int global_relaxdtmf;
00540 static int global_rtptimeout;
00541 static int global_rtpholdtimeout;
00542 static int global_rtpkeepalive;
00543 static int global_reg_timeout;
00544 static int global_regattempts_max;
00545 static int global_allowguest;
00546 static int global_allowsubscribe;
00547
00548 static int global_mwitime;
00549 static unsigned int global_tos_sip;
00550 static unsigned int global_tos_audio;
00551 static unsigned int global_tos_video;
00552 static int compactheaders;
00553 static int recordhistory;
00554 static int dumphistory;
00555 static char global_realm[MAXHOSTNAMELEN];
00556 static char global_regcontext[AST_MAX_CONTEXT];
00557 static char global_useragent[AST_MAX_EXTENSION];
00558 static int allow_external_domains;
00559 static int global_callevents;
00560 static int global_t1min;
00561 static int global_autoframing;
00562 static enum transfermodes global_allowtransfer;
00563
00564 static int global_matchexterniplocally;
00565
00566
00567 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00568
00569
00570 static int suserobjs = 0;
00571 static int ruserobjs = 0;
00572 static int speerobjs = 0;
00573 static int rpeerobjs = 0;
00574 static int apeerobjs = 0;
00575 static int regobjs = 0;
00576
00577 static struct ast_flags global_flags[2] = {{0}};
00578
00579
00580 AST_MUTEX_DEFINE_STATIC(iflock);
00581
00582
00583
00584 AST_MUTEX_DEFINE_STATIC(netlock);
00585
00586 AST_MUTEX_DEFINE_STATIC(monlock);
00587
00588 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00589
00590
00591
00592 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00593
00594 static int sip_reloading = FALSE;
00595 static enum channelreloadreason sip_reloadreason;
00596
00597 static struct sched_context *sched;
00598 static struct io_context *io;
00599 static int *sipsock_read_id;
00600
00601 #define DEC_CALL_LIMIT 0
00602 #define INC_CALL_LIMIT 1
00603 #define DEC_CALL_RINGING 2
00604 #define INC_CALL_RINGING 3
00605
00606
00607 struct sip_request {
00608 char *rlPart1;
00609 char *rlPart2;
00610 int len;
00611 int headers;
00612 int method;
00613 int lines;
00614 unsigned int flags;
00615 char *header[SIP_MAX_HEADERS];
00616 char *line[SIP_MAX_LINES];
00617 char data[SIP_MAX_PACKET];
00618 unsigned int sdp_start;
00619 unsigned int sdp_end;
00620 };
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642 struct sip_dual {
00643 struct ast_channel *chan1;
00644 struct ast_channel *chan2;
00645 struct sip_request req;
00646 int seqno;
00647 };
00648
00649 struct sip_pkt;
00650
00651
00652 struct sip_invite_param {
00653 const char *distinctive_ring;
00654 int addsipheaders;
00655 const char *uri_options;
00656 const char *vxml_url;
00657 char *auth;
00658 char *authheader;
00659 enum sip_auth_type auth_type;
00660 const char *replaces;
00661 int transfer;
00662 };
00663
00664
00665 struct sip_route {
00666 struct sip_route *next;
00667 char hop[0];
00668 };
00669
00670
00671 enum domain_mode {
00672 SIP_DOMAIN_AUTO,
00673 SIP_DOMAIN_CONFIG,
00674 };
00675
00676
00677
00678
00679
00680 struct domain {
00681 char domain[MAXHOSTNAMELEN];
00682 char context[AST_MAX_EXTENSION];
00683 enum domain_mode mode;
00684 AST_LIST_ENTRY(domain) list;
00685 };
00686
00687 static AST_LIST_HEAD_STATIC(domain_list, domain);
00688
00689
00690
00691 struct sip_history {
00692 AST_LIST_ENTRY(sip_history) list;
00693 char event[0];
00694 };
00695
00696 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00697
00698
00699 struct sip_auth {
00700 char realm[AST_MAX_EXTENSION];
00701 char username[256];
00702 char secret[256];
00703 char md5secret[256];
00704 struct sip_auth *next;
00705 };
00706
00707
00708 #define SIP_ALREADYGONE (1 << 0)
00709 #define SIP_NEEDDESTROY (1 << 1)
00710 #define SIP_NOVIDEO (1 << 2)
00711 #define SIP_RINGING (1 << 3)
00712 #define SIP_PROGRESS_SENT (1 << 4)
00713 #define SIP_NEEDREINVITE (1 << 5)
00714 #define SIP_PENDINGBYE (1 << 6)
00715 #define SIP_GOTREFER (1 << 7)
00716 #define SIP_PROMISCREDIR (1 << 8)
00717 #define SIP_TRUSTRPID (1 << 9)
00718 #define SIP_USEREQPHONE (1 << 10)
00719 #define SIP_REALTIME (1 << 11)
00720 #define SIP_USECLIENTCODE (1 << 12)
00721 #define SIP_OUTGOING (1 << 13)
00722 #define SIP_FREE_BIT (1 << 14)
00723 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 15)
00724 #define SIP_DTMF (3 << 16)
00725 #define SIP_DTMF_RFC2833 (0 << 16)
00726 #define SIP_DTMF_INBAND (1 << 16)
00727 #define SIP_DTMF_INFO (2 << 16)
00728 #define SIP_DTMF_AUTO (3 << 16)
00729
00730 #define SIP_NAT (3 << 18)
00731 #define SIP_NAT_NEVER (0 << 18)
00732 #define SIP_NAT_RFC3581 (1 << 18)
00733 #define SIP_NAT_ROUTE (2 << 18)
00734 #define SIP_NAT_ALWAYS (3 << 18)
00735
00736 #define SIP_REINVITE (7 << 20)
00737 #define SIP_CAN_REINVITE (1 << 20)
00738 #define SIP_CAN_REINVITE_NAT (2 << 20)
00739 #define SIP_REINVITE_UPDATE (4 << 20)
00740
00741 #define SIP_INSECURE_PORT (1 << 23)
00742 #define SIP_INSECURE_INVITE (1 << 24)
00743
00744 #define SIP_PROG_INBAND (3 << 25)
00745 #define SIP_PROG_INBAND_NEVER (0 << 25)
00746 #define SIP_PROG_INBAND_NO (1 << 25)
00747 #define SIP_PROG_INBAND_YES (2 << 25)
00748 #define SIP_NO_HISTORY (1 << 27)
00749 #define SIP_CALL_LIMIT (1 << 28)
00750 #define SIP_SENDRPID (1 << 29)
00751 #define SIP_INC_COUNT (1 << 30)
00752 #define SIP_G726_NONSTANDARD (1 << 31)
00753
00754 #define SIP_FLAGS_TO_COPY \
00755 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
00756 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
00757 SIP_USEREQPHONE | SIP_INSECURE_PORT | SIP_INSECURE_INVITE)
00758
00759
00760
00761 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
00762 #define SIP_PAGE2_RTUPDATE (1 << 1)
00763 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
00764 #define SIP_PAGE2_RT_FROMCONTACT (1 << 4)
00765 #define SIP_PAGE2_RTSAVE_SYSNAME (1 << 5)
00766
00767 #define SIP_PAGE2_IGNOREREGEXPIRE (1 << 10)
00768 #define SIP_PAGE2_DEBUG (3 << 11)
00769 #define SIP_PAGE2_DEBUG_CONFIG (1 << 11)
00770 #define SIP_PAGE2_DEBUG_CONSOLE (1 << 12)
00771 #define SIP_PAGE2_DYNAMIC (1 << 13)
00772 #define SIP_PAGE2_SELFDESTRUCT (1 << 14)
00773 #define SIP_PAGE2_VIDEOSUPPORT (1 << 15)
00774 #define SIP_PAGE2_ALLOWSUBSCRIBE (1 << 16)
00775 #define SIP_PAGE2_ALLOWOVERLAP (1 << 17)
00776 #define SIP_PAGE2_SUBSCRIBEMWIONLY (1 << 18)
00777 #define SIP_PAGE2_INC_RINGING (1 << 19)
00778 #define SIP_PAGE2_T38SUPPORT (7 << 20)
00779 #define SIP_PAGE2_T38SUPPORT_UDPTL (1 << 20)
00780 #define SIP_PAGE2_T38SUPPORT_RTP (2 << 20)
00781 #define SIP_PAGE2_T38SUPPORT_TCP (4 << 20)
00782 #define SIP_PAGE2_CALL_ONHOLD (3 << 23)
00783 #define SIP_PAGE2_CALL_ONHOLD_ACTIVE (1 << 23)
00784 #define SIP_PAGE2_CALL_ONHOLD_ONEDIR (2 << 23)
00785 #define SIP_PAGE2_CALL_ONHOLD_INACTIVE (3 << 23)
00786 #define SIP_PAGE2_RFC2833_COMPENSATE (1 << 25)
00787 #define SIP_PAGE2_BUGGY_MWI (1 << 26)
00788 #define SIP_PAGE2_OUTGOING_CALL (1 << 27)
00789
00790 #define SIP_PAGE2_FLAGS_TO_COPY \
00791 (SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_VIDEOSUPPORT | \
00792 SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | SIP_PAGE2_BUGGY_MWI)
00793
00794
00795 #define SIP_PKT_DEBUG (1 << 0)
00796 #define SIP_PKT_WITH_TOTAG (1 << 1)
00797 #define SIP_PKT_IGNORE (1 << 2)
00798 #define SIP_PKT_IGNORE_RESP (1 << 3)
00799 #define SIP_PKT_IGNORE_REQ (1 << 4)
00800
00801
00802 #define T38FAX_FILL_BIT_REMOVAL (1 << 0)
00803 #define T38FAX_TRANSCODING_MMR (1 << 1)
00804 #define T38FAX_TRANSCODING_JBIG (1 << 2)
00805
00806 #define T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF (0 << 3)
00807 #define T38FAX_RATE_MANAGEMENT_LOCAL_TCF (1 << 3)
00808
00809 #define T38FAX_UDP_EC_NONE (0 << 4)
00810 #define T38FAX_UDP_EC_FEC (1 << 4)
00811 #define T38FAX_UDP_EC_REDUNDANCY (2 << 4)
00812
00813 #define T38FAX_VERSION (3 << 6)
00814 #define T38FAX_VERSION_0 (0 << 6)
00815 #define T38FAX_VERSION_1 (1 << 6)
00816
00817 #define T38FAX_RATE_2400 (1 << 8)
00818 #define T38FAX_RATE_4800 (1 << 9)
00819 #define T38FAX_RATE_7200 (1 << 10)
00820 #define T38FAX_RATE_9600 (1 << 11)
00821 #define T38FAX_RATE_12000 (1 << 12)
00822 #define T38FAX_RATE_14400 (1 << 13)
00823
00824
00825 static int global_t38_capability = T38FAX_VERSION_0 | T38FAX_RATE_2400 | T38FAX_RATE_4800 | T38FAX_RATE_7200 | T38FAX_RATE_9600;
00826
00827 #define sipdebug ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG)
00828 #define sipdebug_config ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG)
00829 #define sipdebug_console ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE)
00830
00831
00832 enum t38state {
00833 T38_DISABLED = 0,
00834 T38_LOCAL_DIRECT,
00835 T38_LOCAL_REINVITE,
00836 T38_PEER_DIRECT,
00837 T38_PEER_REINVITE,
00838 T38_ENABLED
00839 };
00840
00841
00842 struct t38properties {
00843 struct ast_flags t38support;
00844 int capability;
00845 int peercapability;
00846 int jointcapability;
00847 enum t38state state;
00848 };
00849
00850
00851 enum referstatus {
00852 REFER_IDLE,
00853 REFER_SENT,
00854 REFER_RECEIVED,
00855 REFER_CONFIRMED,
00856 REFER_ACCEPTED,
00857 REFER_RINGING,
00858 REFER_200OK,
00859 REFER_FAILED,
00860 REFER_NOAUTH
00861 };
00862
00863 static const struct c_referstatusstring {
00864 enum referstatus status;
00865 char *text;
00866 } referstatusstrings[] = {
00867 { REFER_IDLE, "<none>" },
00868 { REFER_SENT, "Request sent" },
00869 { REFER_RECEIVED, "Request received" },
00870 { REFER_ACCEPTED, "Accepted" },
00871 { REFER_RINGING, "Target ringing" },
00872 { REFER_200OK, "Done" },
00873 { REFER_FAILED, "Failed" },
00874 { REFER_NOAUTH, "Failed - auth failure" }
00875 } ;
00876
00877
00878
00879 struct sip_refer {
00880 char refer_to[AST_MAX_EXTENSION];
00881 char refer_to_domain[AST_MAX_EXTENSION];
00882 char refer_to_urioption[AST_MAX_EXTENSION];
00883 char refer_to_context[AST_MAX_EXTENSION];
00884 char referred_by[AST_MAX_EXTENSION];
00885 char referred_by_name[AST_MAX_EXTENSION];
00886 char refer_contact[AST_MAX_EXTENSION];
00887 char replaces_callid[BUFSIZ];
00888 char replaces_callid_totag[BUFSIZ/2];
00889 char replaces_callid_fromtag[BUFSIZ/2];
00890 struct sip_pvt *refer_call;
00891 int attendedtransfer;
00892 int localtransfer;
00893 enum referstatus status;
00894 };
00895
00896
00897 static struct sip_pvt {
00898 ast_mutex_t lock;
00899 int method;
00900 enum invitestates invitestate;
00901 AST_DECLARE_STRING_FIELDS(
00902 AST_STRING_FIELD(callid);
00903 AST_STRING_FIELD(randdata);
00904 AST_STRING_FIELD(accountcode);
00905 AST_STRING_FIELD(realm);
00906 AST_STRING_FIELD(nonce);
00907 AST_STRING_FIELD(opaque);
00908 AST_STRING_FIELD(qop);
00909 AST_STRING_FIELD(domain);
00910 AST_STRING_FIELD(from);
00911 AST_STRING_FIELD(useragent);
00912 AST_STRING_FIELD(exten);
00913 AST_STRING_FIELD(context);
00914 AST_STRING_FIELD(subscribecontext);
00915 AST_STRING_FIELD(subscribeuri);
00916 AST_STRING_FIELD(fromdomain);
00917 AST_STRING_FIELD(fromuser);
00918 AST_STRING_FIELD(fromname);
00919 AST_STRING_FIELD(tohost);
00920 AST_STRING_FIELD(language);
00921 AST_STRING_FIELD(mohinterpret);
00922 AST_STRING_FIELD(mohsuggest);
00923 AST_STRING_FIELD(rdnis);
00924 AST_STRING_FIELD(theirtag);
00925 AST_STRING_FIELD(username);
00926 AST_STRING_FIELD(peername);
00927 AST_STRING_FIELD(authname);
00928 AST_STRING_FIELD(uri);
00929 AST_STRING_FIELD(okcontacturi);
00930 AST_STRING_FIELD(peersecret);
00931 AST_STRING_FIELD(peermd5secret);
00932 AST_STRING_FIELD(cid_num);
00933 AST_STRING_FIELD(cid_name);
00934 AST_STRING_FIELD(via);
00935 AST_STRING_FIELD(fullcontact);
00936 AST_STRING_FIELD(our_contact);
00937 AST_STRING_FIELD(rpid);
00938 AST_STRING_FIELD(rpid_from);
00939 );
00940 unsigned int ocseq;
00941 unsigned int icseq;
00942 ast_group_t callgroup;
00943 ast_group_t pickupgroup;
00944 int lastinvite;
00945 struct ast_flags flags[2];
00946 int timer_t1;
00947 unsigned int sipoptions;
00948 struct ast_codec_pref prefs;
00949 int capability;
00950 int jointcapability;
00951 int peercapability;
00952 int prefcodec;
00953 int noncodeccapability;
00954 int jointnoncodeccapability;
00955 int redircodecs;
00956 int maxcallbitrate;
00957 struct t38properties t38;
00958 struct sockaddr_in udptlredirip;
00959 struct ast_udptl *udptl;
00960 int callingpres;
00961 int authtries;
00962 int expiry;
00963 long branch;
00964 char tag[11];
00965 int sessionid;
00966 int sessionversion;
00967 struct sockaddr_in sa;
00968 struct sockaddr_in redirip;
00969 struct sockaddr_in vredirip;
00970 time_t lastrtprx;
00971 time_t lastrtptx;
00972 int rtptimeout;
00973 struct sockaddr_in recv;
00974 struct in_addr ourip;
00975 struct ast_channel *owner;
00976 struct sip_route *route;
00977 int route_persistant;
00978 struct sip_auth *peerauth;
00979 int noncecount;
00980 char lastmsg[256];
00981 int amaflags;
00982 int pendinginvite;
00983 struct sip_request initreq;
00984
00985
00986 int maxtime;
00987 int initid;
00988 int autokillid;
00989 enum transfermodes allowtransfer;
00990 struct sip_refer *refer;
00991 enum subscriptiontype subscribed;
00992 int stateid;
00993 int laststate;
00994 int dialogver;
00995
00996 struct ast_dsp *vad;
00997
00998 struct sip_peer *relatedpeer;
00999
01000 struct sip_registry *registry;
01001 struct ast_rtp *rtp;
01002 struct ast_rtp *vrtp;
01003 struct sip_pkt *packets;
01004 struct sip_history_head *history;
01005 struct ast_variable *chanvars;
01006 struct sip_pvt *next;
01007 struct sip_invite_param *options;
01008 int autoframing;
01009 } *iflist = NULL;
01010
01011 #define FLAG_RESPONSE (1 << 0)
01012 #define FLAG_FATAL (1 << 1)
01013
01014
01015 struct sip_pkt {
01016 struct sip_pkt *next;
01017 int retrans;
01018 int method;
01019 int seqno;
01020 unsigned int flags;
01021 struct sip_pvt *owner;
01022 int retransid;
01023 int timer_a;
01024 int timer_t1;
01025 int packetlen;
01026 char data[0];
01027 };
01028
01029
01030 struct sip_user {
01031
01032 ASTOBJ_COMPONENTS(struct sip_user);
01033 char secret[80];
01034 char md5secret[80];
01035 char context[AST_MAX_CONTEXT];
01036 char subscribecontext[AST_MAX_CONTEXT];
01037 char cid_num[80];
01038 char cid_name[80];
01039 char accountcode[AST_MAX_ACCOUNT_CODE];
01040 char language[MAX_LANGUAGE];
01041 char mohinterpret[MAX_MUSICCLASS];
01042 char mohsuggest[MAX_MUSICCLASS];
01043 char useragent[256];
01044 struct ast_codec_pref prefs;
01045 ast_group_t callgroup;
01046 ast_group_t pickupgroup;
01047 unsigned int sipoptions;
01048 struct ast_flags flags[2];
01049 int amaflags;
01050 int callingpres;
01051 int capability;
01052 int inUse;
01053 int call_limit;
01054 enum transfermodes allowtransfer;
01055 struct ast_ha *ha;
01056 struct ast_variable *chanvars;
01057 int maxcallbitrate;
01058 int autoframing;
01059 };
01060
01061
01062
01063 struct sip_peer {
01064 ASTOBJ_COMPONENTS(struct sip_peer);
01065
01066 char secret[80];
01067 char md5secret[80];
01068 struct sip_auth *auth;
01069 char context[AST_MAX_CONTEXT];
01070 char subscribecontext[AST_MAX_CONTEXT];
01071 char username[80];
01072 char accountcode[AST_MAX_ACCOUNT_CODE];
01073 int amaflags;
01074 char tohost[MAXHOSTNAMELEN];
01075 char regexten[AST_MAX_EXTENSION];
01076 char fromuser[80];
01077 char fromdomain[MAXHOSTNAMELEN];
01078 char fullcontact[256];
01079 char cid_num[80];
01080 char cid_name[80];
01081 int callingpres;
01082 int inUse;
01083 int inRinging;
01084 int onHold;
01085 int call_limit;
01086 enum transfermodes allowtransfer;
01087 char vmexten[AST_MAX_EXTENSION];
01088 char mailbox[AST_MAX_EXTENSION];
01089 char language[MAX_LANGUAGE];
01090 char mohinterpret[MAX_MUSICCLASS];
01091 char mohsuggest[MAX_MUSICCLASS];
01092 char useragent[256];
01093 struct ast_codec_pref prefs;
01094 int lastmsgssent;
01095 time_t lastmsgcheck;
01096 unsigned int sipoptions;
01097 struct ast_flags flags[2];
01098 int expire;
01099 int capability;
01100 int rtptimeout;
01101 int rtpholdtimeout;
01102 int rtpkeepalive;
01103 ast_group_t callgroup;
01104 ast_group_t pickupgroup;
01105 struct sockaddr_in addr;
01106 int maxcallbitrate;
01107
01108
01109 struct sip_pvt *call;
01110 int pokeexpire;
01111 int lastms;
01112 int maxms;
01113 struct timeval ps;
01114
01115 struct sockaddr_in defaddr;
01116 struct ast_ha *ha;
01117 struct ast_variable *chanvars;
01118 struct sip_pvt *mwipvt;
01119 int lastmsg;
01120 int autoframing;
01121 };
01122
01123
01124
01125
01126 struct sip_registry {
01127 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
01128 AST_DECLARE_STRING_FIELDS(
01129 AST_STRING_FIELD(callid);
01130 AST_STRING_FIELD(realm);
01131 AST_STRING_FIELD(nonce);
01132 AST_STRING_FIELD(opaque);
01133 AST_STRING_FIELD(qop);
01134 AST_STRING_FIELD(domain);
01135 AST_STRING_FIELD(username);
01136 AST_STRING_FIELD(authuser);
01137 AST_STRING_FIELD(hostname);
01138 AST_STRING_FIELD(secret);
01139 AST_STRING_FIELD(md5secret);
01140 AST_STRING_FIELD(contact);
01141 AST_STRING_FIELD(random);
01142 );
01143 int portno;
01144 int expire;
01145 int regattempts;
01146 int timeout;
01147 int refresh;
01148 struct sip_pvt *call;
01149 enum sipregistrystate regstate;
01150 time_t regtime;
01151 int callid_valid;
01152 unsigned int ocseq;
01153 struct sockaddr_in us;
01154 int noncecount;
01155 char lastmsg[256];
01156 };
01157
01158
01159
01160
01161 static struct ast_user_list {
01162 ASTOBJ_CONTAINER_COMPONENTS(struct sip_user);
01163 } userl;
01164
01165
01166 static struct ast_peer_list {
01167 ASTOBJ_CONTAINER_COMPONENTS(struct sip_peer);
01168 } peerl;
01169
01170
01171 static struct ast_register_list {
01172 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01173 int recheck;
01174 } regl;
01175
01176 static void temp_pvt_cleanup(void *);
01177
01178
01179 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01180
01181
01182 static struct sip_auth *authl = NULL;
01183
01184
01185
01186 static int sipsock = -1;
01187 static struct sockaddr_in bindaddr = { 0, };
01188 static struct sockaddr_in externip;
01189 static char externhost[MAXHOSTNAMELEN];
01190 static time_t externexpire = 0;
01191 static int externrefresh = 10;
01192 static struct ast_ha *localaddr;
01193 static struct in_addr __ourip;
01194 static struct sockaddr_in outboundproxyip;
01195 static int ourport;
01196 static struct sockaddr_in debugaddr;
01197
01198 static struct ast_config *notify_types;
01199
01200
01201
01202
01203
01204
01205 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
01206 static int sip_devicestate(void *data);
01207 static int sip_sendtext(struct ast_channel *ast, const char *text);
01208 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01209 static int sip_hangup(struct ast_channel *ast);
01210 static int sip_answer(struct ast_channel *ast);
01211 static struct ast_frame *sip_read(struct ast_channel *ast);
01212 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01213 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01214 static int sip_transfer(struct ast_channel *ast, const char *dest);
01215 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01216 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01217 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01218
01219
01220 static int sipsock_read(int *id, int fd, short events, void *ignore);
01221 static int __sip_xmit(struct sip_pvt *p, char *data, int len);
01222 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod);
01223 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01224 static int retrans_pkt(void *data);
01225 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req);
01226 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);
01227 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01228 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01229 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01230 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01231 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01232 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);
01233 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01234 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, int reliable);
01235 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01236 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01237 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
01238 static int transmit_reinvite_with_sdp(struct sip_pvt *p);
01239 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01240 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01241 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01242 static int transmit_refer(struct sip_pvt *p, const char *dest);
01243 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten);
01244 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01245 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01246 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01247 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01248 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01249 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01250 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
01251 static int sip_send_mwi_to_peer(struct sip_peer *peer);
01252 static int does_peer_need_mwi(struct sip_peer *peer);
01253
01254
01255 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
01256 int useglobal_nat, const int intended_method);
01257 static int __sip_autodestruct(void *data);
01258 static void sip_scheddestroy(struct sip_pvt *p, int ms);
01259 static void sip_cancel_destroy(struct sip_pvt *p);
01260 static void sip_destroy(struct sip_pvt *p);
01261 static void __sip_destroy(struct sip_pvt *p, int lockowner);
01262 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01263 static void __sip_pretend_ack(struct sip_pvt *p);
01264 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01265 static int auto_congest(void *nothing);
01266 static int update_call_counter(struct sip_pvt *fup, int event);
01267 static int hangup_sip2cause(int cause);
01268 static const char *hangup_cause2sip(int cause);
01269 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method);
01270 static void free_old_route(struct sip_route *route);
01271 static void list_route(struct sip_route *route);
01272 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01273 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
01274 struct sip_request *req, char *uri);
01275 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01276 static void check_pendings(struct sip_pvt *p);
01277 static void *sip_park_thread(void *stuff);
01278 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno);
01279 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01280
01281
01282 static void try_suggested_sip_codec(struct sip_pvt *p);
01283 static const char* get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01284 static const char *get_sdp(struct sip_request *req, const char *name);
01285 static int find_sdp(struct sip_request *req);
01286 static int process_sdp(struct sip_pvt *p, struct sip_request *req);
01287 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
01288 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01289 int debug, int *min_packet_size);
01290 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
01291 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01292 int debug);
01293 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p);
01294 static void stop_media_flows(struct sip_pvt *p);
01295
01296
01297 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01298 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01299 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01300 const char *secret, const char *md5secret, int sipmethod,
01301 char *uri, enum xmittype reliable, int ignore);
01302 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01303 int sipmethod, char *uri, enum xmittype reliable,
01304 struct sockaddr_in *sin, struct sip_peer **authpeer);
01305 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin);
01306
01307
01308 static int check_sip_domain(const char *domain, char *context, size_t len);
01309 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01310 static void clear_sip_domains(void);
01311
01312
01313 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno);
01314 static int clear_realm_authentication(struct sip_auth *authlist);
01315 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
01316
01317
01318 static int sip_do_reload(enum channelreloadreason reason);
01319 static int reload_config(enum channelreloadreason reason);
01320 static int expire_register(void *data);
01321 static void *do_monitor(void *data);
01322 static int restart_monitor(void);
01323 static int sip_send_mwi_to_peer(struct sip_peer *peer);
01324 static void sip_destroy(struct sip_pvt *p);
01325 static int sip_addrcmp(char *name, struct sockaddr_in *sin);
01326 static int sip_refer_allocate(struct sip_pvt *p);
01327 static void ast_quiet_chan(struct ast_channel *chan);
01328 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01329
01330
01331 static int cb_extensionstate(char *context, char* exten, int state, void *data);
01332 static int sip_devicestate(void *data);
01333 static int sip_poke_noanswer(void *data);
01334 static int sip_poke_peer(struct sip_peer *peer);
01335 static void sip_poke_all_peers(void);
01336 static void sip_peer_hold(struct sip_pvt *p, int hold);
01337
01338
01339 static const char *sip_nat_mode(const struct sip_pvt *p);
01340 static int sip_show_inuse(int fd, int argc, char *argv[]);
01341 static char *transfermode2str(enum transfermodes mode) attribute_const;
01342 static char *nat2str(int nat) attribute_const;
01343 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01344 static int sip_show_users(int fd, int argc, char *argv[]);
01345 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01346 static int sip_show_peers(int fd, int argc, char *argv[]);
01347 static int sip_show_objects(int fd, int argc, char *argv[]);
01348 static void print_group(int fd, ast_group_t group, int crlf);
01349 static const char *dtmfmode2str(int mode) attribute_const;
01350 static const char *insecure2str(int port, int invite) attribute_const;
01351 static void cleanup_stale_contexts(char *new, char *old);
01352 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01353 static const char *domain_mode_to_text(const enum domain_mode mode);
01354 static int sip_show_domains(int fd, int argc, char *argv[]);
01355 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01356 static int sip_show_peer(int fd, int argc, char *argv[]);
01357 static int sip_show_user(int fd, int argc, char *argv[]);
01358 static int sip_show_registry(int fd, int argc, char *argv[]);
01359 static int sip_show_settings(int fd, int argc, char *argv[]);
01360 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01361 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01362 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01363 static int sip_show_channels(int fd, int argc, char *argv[]);
01364 static int sip_show_subscriptions(int fd, int argc, char *argv[]);
01365 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01366 static char *complete_sipch(const char *line, const char *word, int pos, int state);
01367 static char *complete_sip_peer(const char *word, int state, int flags2);
01368 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01369 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state);
01370 static char *complete_sip_user(const char *word, int state, int flags2);
01371 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
01372 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01373 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state);
01374 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state);
01375 static int sip_show_channel(int fd, int argc, char *argv[]);
01376 static int sip_show_history(int fd, int argc, char *argv[]);
01377 static int sip_do_debug_ip(int fd, int argc, char *argv[]);
01378 static int sip_do_debug_peer(int fd, int argc, char *argv[]);
01379 static int sip_do_debug(int fd, int argc, char *argv[]);
01380 static int sip_no_debug(int fd, int argc, char *argv[]);
01381 static int sip_notify(int fd, int argc, char *argv[]);
01382 static int sip_do_history(int fd, int argc, char *argv[]);
01383 static int sip_no_history(int fd, int argc, char *argv[]);
01384 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
01385 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01386 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01387 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01388 static int sip_dtmfmode(struct ast_channel *chan, void *data);
01389 static int sip_addheader(struct ast_channel *chan, void *data);
01390 static int sip_do_reload(enum channelreloadreason reason);
01391 static int sip_reload(int fd, int argc, char *argv[]);
01392 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen);
01393
01394
01395
01396
01397
01398 static void sip_dump_history(struct sip_pvt *dialog);
01399 static inline int sip_debug_test_addr(const struct sockaddr_in *addr);
01400 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01401 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01402 static void sip_dump_history(struct sip_pvt *dialog);
01403
01404
01405 static struct sip_peer *temp_peer(const char *name);
01406 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01407 static struct sip_user *build_user(const char *name, struct ast_variable *v, int realtime);
01408 static int update_call_counter(struct sip_pvt *fup, int event);
01409 static void sip_destroy_peer(struct sip_peer *peer);
01410 static void sip_destroy_user(struct sip_user *user);
01411 static int sip_poke_peer(struct sip_peer *peer);
01412 static int sip_poke_peer_s(void *data);
01413 static void set_peer_defaults(struct sip_peer *peer);
01414 static struct sip_peer *temp_peer(const char *name);
01415 static void register_peer_exten(struct sip_peer *peer, int onoff);
01416 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime);
01417 static struct sip_user *find_user(const char *name, int realtime);
01418 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01419 static int expire_register(void *data);
01420 static void reg_source_db(struct sip_peer *peer);
01421 static void destroy_association(struct sip_peer *peer);
01422 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01423
01424
01425 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey);
01426 static struct sip_user *realtime_user(const char *username);
01427 static void update_peer(struct sip_peer *p, int expiry);
01428 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin);
01429 static int sip_prune_realtime(int fd, int argc, char *argv[]);
01430
01431
01432 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us);
01433 static void sip_registry_destroy(struct sip_registry *reg);
01434 static int sip_register(char *value, int lineno);
01435 static char *regstate2str(enum sipregistrystate regstate) attribute_const;
01436 static int sip_reregister(void *data);
01437 static int __sip_do_register(struct sip_registry *r);
01438 static int sip_reg_timeout(void *data);
01439 static void sip_send_all_registers(void);
01440
01441
01442 static void append_date(struct sip_request *req);
01443 static int determine_firstline_parts(struct sip_request *req);
01444 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01445 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01446 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
01447 static int find_sip_method(const char *msg);
01448 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
01449 static void parse_request(struct sip_request *req);
01450 static const char *get_header(const struct sip_request *req, const char *name);
01451 static char *referstatus2str(enum referstatus rstatus) attribute_pure;
01452 static int method_match(enum sipmethod id, const char *name);
01453 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
01454 static char *get_in_brackets(char *tmp);
01455 static const char *find_alias(const char *name, const char *_default);
01456 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
01457 static int lws2sws(char *msgbuf, int len);
01458 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
01459 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
01460 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
01461 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
01462 static int set_address_from_contact(struct sip_pvt *pvt);
01463 static void check_via(struct sip_pvt *p, struct sip_request *req);
01464 static char *get_calleridname(const char *input, char *output, size_t outputsize);
01465 static int get_rpid_num(const char *input, char *output, int maxlen);
01466 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq);
01467 static int get_destination(struct sip_pvt *p, struct sip_request *oreq);
01468 static int get_msg_text(char *buf, int len, struct sip_request *req);
01469 static void free_old_route(struct sip_route *route);
01470 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
01471
01472
01473 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
01474 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
01475 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
01476 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod);
01477 static int init_resp(struct sip_request *resp, const char *msg);
01478 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
01479 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p);
01480 static void build_via(struct sip_pvt *p);
01481 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
01482 static int create_addr(struct sip_pvt *dialog, const char *opeer);
01483 static char *generate_random_string(char *buf, size_t size);
01484 static void build_callid_pvt(struct sip_pvt *pvt);
01485 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain);
01486 static void make_our_tag(char *tagbuf, size_t len);
01487 static int add_header(struct sip_request *req, const char *var, const char *value);
01488 static int add_header_contentLength(struct sip_request *req, int len);
01489 static int add_line(struct sip_request *req, const char *line);
01490 static int add_text(struct sip_request *req, const char *text);
01491 static int add_digit(struct sip_request *req, char digit, unsigned int duration);
01492 static int add_vidupdate(struct sip_request *req);
01493 static void add_route(struct sip_request *req, struct sip_route *route);
01494 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01495 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01496 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
01497 static void set_destination(struct sip_pvt *p, char *uri);
01498 static void append_date(struct sip_request *req);
01499 static void build_contact(struct sip_pvt *p);
01500 static void build_rpid(struct sip_pvt *p);
01501
01502
01503 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock);
01504 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);
01505 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock);
01506 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
01507 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e);
01508 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
01509 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
01510 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01511 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
01512 static int handle_request_options(struct sip_pvt *p, struct sip_request *req);
01513 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin);
01514 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01515 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno);
01516
01517
01518 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01519 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01520 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno);
01521 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno);
01522
01523
01524 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active);
01525 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01526 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01527 static int sip_get_codec(struct ast_channel *chan);
01528 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect);
01529
01530
01531 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite);
01532 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
01533 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p);
01534 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
01535 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
01536
01537
01538 static const struct ast_channel_tech sip_tech = {
01539 .type = "SIP",
01540 .description = "Session Initiation Protocol (SIP)",
01541 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01542 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01543 .requester = sip_request_call,
01544 .devicestate = sip_devicestate,
01545 .call = sip_call,
01546 .hangup = sip_hangup,
01547 .answer = sip_answer,
01548 .read = sip_read,
01549 .write = sip_write,
01550 .write_video = sip_write,
01551 .indicate = sip_indicate,
01552 .transfer = sip_transfer,
01553 .fixup = sip_fixup,
01554 .send_digit_begin = sip_senddigit_begin,
01555 .send_digit_end = sip_senddigit_end,
01556 .bridge = ast_rtp_bridge,
01557 .send_text = sip_sendtext,
01558 .func_channel_read = acf_channel_read,
01559 };
01560
01561
01562
01563
01564 static const struct ast_channel_tech sip_tech_info = {
01565 .type = "SIP",
01566 .description = "Session Initiation Protocol (SIP)",
01567 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01568 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01569 .requester = sip_request_call,
01570 .devicestate = sip_devicestate,
01571 .call = sip_call,
01572 .hangup = sip_hangup,
01573 .answer = sip_answer,
01574 .read = sip_read,
01575 .write = sip_write,
01576 .write_video = sip_write,
01577 .indicate = sip_indicate,
01578 .transfer = sip_transfer,
01579 .fixup = sip_fixup,
01580 .send_digit_end = sip_senddigit_end,
01581 .bridge = ast_rtp_bridge,
01582 .send_text = sip_sendtext,
01583 .func_channel_read = acf_channel_read,
01584 };
01585
01586
01587
01588 #define UNLINK(element, head, prev) do { \
01589 if (prev) \
01590 (prev)->next = (element)->next; \
01591 else \
01592 (head) = (element)->next; \
01593 } while (0)
01594
01595
01596 static struct ast_rtp_protocol sip_rtp = {
01597 type: "SIP",
01598 get_rtp_info: sip_get_rtp_peer,
01599 get_vrtp_info: sip_get_vrtp_peer,
01600 set_rtp_peer: sip_set_rtp_peer,
01601 get_codec: sip_get_codec,
01602 };
01603
01604
01605 static struct ast_udptl_protocol sip_udptl = {
01606 type: "SIP",
01607 get_udptl_info: sip_get_udptl_peer,
01608 set_udptl_peer: sip_set_udptl_peer,
01609 };
01610
01611
01612 static char *referstatus2str(enum referstatus rstatus)
01613 {
01614 int i = (sizeof(referstatusstrings) / sizeof(referstatusstrings[0]));
01615 int x;
01616
01617 for (x = 0; x < i; x++) {
01618 if (referstatusstrings[x].status == rstatus)
01619 return (char *) referstatusstrings[x].text;
01620 }
01621 return "";
01622 }
01623
01624
01625
01626
01627 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
01628 {
01629 if (p->initreq.headers && option_debug) {
01630 ast_log(LOG_DEBUG, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
01631 }
01632
01633 copy_request(&p->initreq, req);
01634 parse_request(&p->initreq);
01635 if (ast_test_flag(req, SIP_PKT_DEBUG))
01636 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
01637 }
01638
01639 static void sip_alreadygone(struct sip_pvt *dialog)
01640 {
01641 if (option_debug > 2)
01642 ast_log(LOG_DEBUG, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
01643 ast_set_flag(&dialog->flags[0], SIP_ALREADYGONE);
01644 }
01645
01646
01647
01648
01649
01650
01651
01652
01653 static int method_match(enum sipmethod id, const char *name)
01654 {
01655 int len = strlen(sip_methods[id].text);
01656 int l_name = name ? strlen(name) : 0;
01657
01658 return (l_name >= len && name[len] < 33 &&
01659 !strncasecmp(sip_methods[id].text, name, len));
01660 }
01661
01662
01663 static int find_sip_method(const char *msg)
01664 {
01665 int i, res = 0;
01666
01667 if (ast_strlen_zero(msg))
01668 return 0;
01669 for (i = 1; i < (sizeof(sip_methods) / sizeof(sip_methods[0])) && !res; i++) {
01670 if (method_match(i, msg))
01671 res = sip_methods[i].id;
01672 }
01673 return res;
01674 }
01675
01676
01677 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
01678 {
01679 char *next, *sep;
01680 char *temp;
01681 unsigned int profile = 0;
01682 int i, found;
01683
01684 if (ast_strlen_zero(supported) )
01685 return 0;
01686 temp = ast_strdupa(supported);
01687
01688 if (option_debug > 2 && sipdebug)
01689 ast_log(LOG_DEBUG, "Begin: parsing SIP \"Supported: %s\"\n", supported);
01690
01691 for (next = temp; next; next = sep) {
01692 found = FALSE;
01693 if ( (sep = strchr(next, ',')) != NULL)
01694 *sep++ = '\0';
01695 next = ast_skip_blanks(next);
01696 if (option_debug > 2 && sipdebug)
01697 ast_log(LOG_DEBUG, "Found SIP option: -%s-\n", next);
01698 for (i=0; i < (sizeof(sip_options) / sizeof(sip_options[0])); i++) {
01699 if (!strcasecmp(next, sip_options[i].text)) {
01700 profile |= sip_options[i].id;
01701 found = TRUE;
01702 if (option_debug > 2 && sipdebug)
01703 ast_log(LOG_DEBUG, "Matched SIP option: %s\n", next);
01704 break;
01705 }
01706 }
01707 if (!found && option_debug > 2 && sipdebug) {
01708 if (!strncasecmp(next, "x-", 2))
01709 ast_log(LOG_DEBUG, "Found private SIP option, not supported: %s\n", next);
01710 else
01711 ast_log(LOG_DEBUG, "Found no match for SIP option: %s (Please file bug report!)\n", next);
01712 }
01713 }
01714
01715 if (pvt)
01716 pvt->sipoptions = profile;
01717 return profile;
01718 }
01719
01720
01721 static inline int sip_debug_test_addr(const struct sockaddr_in *addr)
01722 {
01723 if (!sipdebug)
01724 return 0;
01725 if (debugaddr.sin_addr.s_addr) {
01726 if (((ntohs(debugaddr.sin_port) != 0)
01727 && (debugaddr.sin_port != addr->sin_port))
01728 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
01729 return 0;
01730 }
01731 return 1;
01732 }
01733
01734
01735 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
01736 {
01737 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
01738 }
01739
01740
01741 static const char *sip_nat_mode(const struct sip_pvt *p)
01742 {
01743 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
01744 }
01745
01746
01747 static inline int sip_debug_test_pvt(struct sip_pvt *p)
01748 {
01749 if (!sipdebug)
01750 return 0;
01751 return sip_debug_test_addr(sip_real_dst(p));
01752 }
01753
01754
01755 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
01756 {
01757 int res;
01758 const struct sockaddr_in *dst = sip_real_dst(p);
01759 res = sendto(sipsock, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
01760
01761 if (res == -1) {
01762 switch (errno) {
01763 case EBADF:
01764 case EHOSTUNREACH:
01765 case ENETDOWN:
01766 case ENETUNREACH:
01767 res = XMIT_ERROR;
01768 }
01769 }
01770 if (res != len)
01771 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));
01772 return res;
01773 }
01774
01775
01776
01777 static void build_via(struct sip_pvt *p)
01778 {
01779
01780 const char *rport = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_RFC3581 ? ";rport" : "";
01781
01782
01783 ast_string_field_build(p, via, "SIP/2.0/UDP %s:%d;branch=z9hG4bK%08x%s",
01784 ast_inet_ntoa(p->ourip), ourport, p->branch, rport);
01785 }
01786
01787
01788
01789
01790
01791
01792
01793 static enum sip_result ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us)
01794 {
01795 struct sockaddr_in theirs, ours;
01796
01797
01798 ast_ouraddrfor(them, us);
01799 theirs.sin_addr = *them;
01800 ours.sin_addr = *us;
01801
01802 if (localaddr && externip.sin_addr.s_addr &&
01803 (ast_apply_ha(localaddr, &theirs)) &&
01804 (!global_matchexterniplocally || !ast_apply_ha(localaddr, &ours))) {
01805 if (externexpire && time(NULL) >= externexpire) {
01806 struct ast_hostent ahp;
01807 struct hostent *hp;
01808
01809 externexpire = time(NULL) + externrefresh;
01810 if ((hp = ast_gethostbyname(externhost, &ahp))) {
01811 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
01812 } else
01813 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
01814 }
01815 *us = externip.sin_addr;
01816 if (option_debug) {
01817 ast_log(LOG_DEBUG, "Target address %s is not local, substituting externip\n",
01818 ast_inet_ntoa(*(struct in_addr *)&them->s_addr));
01819 }
01820 } else if (bindaddr.sin_addr.s_addr)
01821 *us = bindaddr.sin_addr;
01822 return AST_SUCCESS;
01823 }
01824
01825
01826
01827 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
01828
01829 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01830 __attribute__ ((format (printf, 2, 3)));
01831
01832
01833 static void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
01834 {
01835 char buf[80], *c = buf;
01836 struct sip_history *hist;
01837 int l;
01838
01839 vsnprintf(buf, sizeof(buf), fmt, ap);
01840 strsep(&c, "\r\n");
01841 l = strlen(buf) + 1;
01842 if (!(hist = ast_calloc(1, sizeof(*hist) + l)))
01843 return;
01844 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
01845 free(hist);
01846 return;
01847 }
01848 memcpy(hist->event, buf, l);
01849 AST_LIST_INSERT_TAIL(p->history, hist, list);
01850 }
01851
01852
01853 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01854 {
01855 va_list ap;
01856
01857 if (!p)
01858 return;
01859 va_start(ap, fmt);
01860 append_history_va(p, fmt, ap);
01861 va_end(ap);
01862
01863 return;
01864 }
01865
01866
01867 static int retrans_pkt(void *data)
01868 {
01869 struct sip_pkt *pkt = data, *prev, *cur = NULL;
01870 int reschedule = DEFAULT_RETRANS;
01871 int xmitres = 0;
01872
01873
01874 ast_mutex_lock(&pkt->owner->lock);
01875
01876 if (pkt->retrans < MAX_RETRANS) {
01877 pkt->retrans++;
01878 if (!pkt->timer_t1) {
01879 if (sipdebug && option_debug > 3)
01880 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);
01881 } else {
01882 int siptimer_a;
01883
01884 if (sipdebug && option_debug > 3)
01885 ast_log(LOG_DEBUG, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
01886 if (!pkt->timer_a)
01887 pkt->timer_a = 2 ;
01888 else
01889 pkt->timer_a = 2 * pkt->timer_a;
01890
01891
01892 siptimer_a = pkt->timer_t1 * pkt->timer_a;
01893 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
01894 siptimer_a = 4000;
01895
01896
01897 reschedule = siptimer_a;
01898 if (option_debug > 3)
01899 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);
01900 }
01901
01902 if (sip_debug_test_pvt(pkt->owner)) {
01903 const struct sockaddr_in *dst = sip_real_dst(pkt->owner);
01904 ast_verbose("Retransmitting #%d (%s) to %s:%d:\n%s\n---\n",
01905 pkt->retrans, sip_nat_mode(pkt->owner),
01906 ast_inet_ntoa(dst->sin_addr),
01907 ntohs(dst->sin_port), pkt->data);
01908 }
01909
01910 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data);
01911 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
01912 ast_mutex_unlock(&pkt->owner->lock);
01913 if (xmitres == XMIT_ERROR)
01914 ast_log(LOG_WARNING, "Network error on retransmit in dialog %s\n", pkt->owner->callid);
01915 else
01916 return reschedule;
01917 }
01918
01919 if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
01920 if (ast_test_flag(pkt, FLAG_FATAL) || sipdebug)
01921 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");
01922 } else if ((pkt->method == SIP_OPTIONS) && sipdebug) {
01923 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) \n", pkt->owner->callid);
01924 }
01925 if (xmitres == XMIT_ERROR) {
01926 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission of transaction in call id %s \n", pkt->owner->callid);
01927 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
01928 } else
01929 append_history(pkt->owner, "MaxRetries", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
01930
01931 pkt->retransid = -1;
01932
01933 if (ast_test_flag(pkt, FLAG_FATAL)) {
01934 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
01935 ast_mutex_unlock(&pkt->owner->lock);
01936 usleep(1);
01937 ast_mutex_lock(&pkt->owner->lock);
01938 }
01939
01940 if (pkt->owner->owner && !pkt->owner->owner->hangupcause)
01941 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
01942
01943 if (pkt->owner->owner) {
01944 sip_alreadygone(pkt->owner);
01945 ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet.\n", pkt->owner->callid);
01946 ast_queue_hangup(pkt->owner->owner);
01947 ast_channel_unlock(pkt->owner->owner);
01948 } else {
01949
01950
01951
01952 if (pkt->method != SIP_OPTIONS) {
01953 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
01954 sip_alreadygone(pkt->owner);
01955 if (option_debug)
01956 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
01957 }
01958 }
01959 }
01960
01961 if (pkt->method == SIP_BYE) {
01962
01963 if (pkt->owner->owner)
01964 ast_channel_unlock(pkt->owner->owner);
01965 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
01966 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
01967 }
01968
01969
01970 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
01971 if (cur == pkt)
01972 break;
01973 }
01974 if (cur) {
01975 if (prev)
01976 prev->next = cur->next;
01977 else
01978 pkt->owner->packets = cur->next;
01979 ast_mutex_unlock(&pkt->owner->lock);
01980 free(cur);
01981 pkt = NULL;
01982 } else
01983 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
01984 if (pkt)
01985 ast_mutex_unlock(&pkt->owner->lock);
01986 return 0;
01987 }
01988
01989
01990
01991
01992 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod)
01993 {
01994 struct sip_pkt *pkt;
01995 int siptimer_a = DEFAULT_RETRANS;
01996 int xmitres = 0;
01997
01998 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1)))
01999 return AST_FAILURE;
02000 memcpy(pkt->data, data, len);
02001 pkt->method = sipmethod;
02002 pkt->packetlen = len;
02003 pkt->next = p->packets;
02004 pkt->owner = p;
02005 pkt->seqno = seqno;
02006 if (resp)
02007 ast_set_flag(pkt, FLAG_RESPONSE);
02008 pkt->data[len] = '\0';
02009 pkt->timer_t1 = p->timer_t1;
02010 if (fatal)
02011 ast_set_flag(pkt, FLAG_FATAL);
02012 if (pkt->timer_t1)
02013 siptimer_a = pkt->timer_t1 * 2;
02014
02015
02016 pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
02017 if (option_debug > 3 && sipdebug)
02018 ast_log(LOG_DEBUG, "*** SIP TIMER: Initalizing retransmit timer on packet: Id #%d\n", pkt->retransid);
02019 pkt->next = p->packets;
02020 p->packets = pkt;
02021 if (sipmethod == SIP_INVITE) {
02022
02023 p->pendinginvite = seqno;
02024 }
02025
02026 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
02027
02028 if (xmitres == XMIT_ERROR) {
02029 append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
02030 ast_sched_del(sched, pkt->retransid);
02031 pkt->retransid = -1;
02032 return AST_FAILURE;
02033 } else
02034 return AST_SUCCESS;
02035 }
02036
02037
02038 static int __sip_autodestruct(void *data)
02039 {
02040 struct sip_pvt *p = data;
02041
02042
02043 if (p->subscribed) {
02044 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
02045 p->subscribed = NONE;
02046 append_history(p, "Subscribestatus", "timeout");
02047 if (option_debug > 2)
02048 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP subsription %s\n", p->callid ? p->callid : "<unknown>");
02049 return 10000;
02050 }
02051
02052
02053 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
02054 ASTOBJ_UNREF(p->relatedpeer,sip_destroy_peer);
02055
02056
02057 p->autokillid = -1;
02058
02059 if (option_debug)
02060 ast_log(LOG_DEBUG, "Auto destroying SIP dialog '%s'\n", p->callid);
02061 append_history(p, "AutoDestroy", "%s", p->callid);
02062 if (p->owner) {
02063 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
02064 ast_queue_hangup(p->owner);
02065 } else if (p->refer) {
02066 if (option_debug > 2)
02067 ast_log(LOG_DEBUG, "Finally hanging up channel after transfer: %s\n", p->callid);
02068 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
02069 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
02070 } else
02071 sip_destroy(p);
02072 return 0;
02073 }
02074
02075
02076 static void sip_scheddestroy(struct sip_pvt *p, int ms)
02077 {
02078 if (ms < 0) {
02079 if (p->timer_t1 == 0)
02080 p->timer_t1 = 500;
02081 ms = p->timer_t1 * 64;
02082 }
02083 if (sip_debug_test_pvt(p))
02084 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
02085 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
02086 append_history(p, "SchedDestroy", "%d ms", ms);
02087
02088 if (p->autokillid > -1)
02089 ast_sched_del(sched, p->autokillid);
02090 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, p);
02091 }
02092
02093
02094 static void sip_cancel_destroy(struct sip_pvt *p)
02095 {
02096 if (p->autokillid > -1) {
02097 ast_sched_del(sched, p->autokillid);
02098 append_history(p, "CancelDestroy", "");
02099 p->autokillid = -1;
02100 }
02101 }
02102
02103
02104 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02105 {
02106 struct sip_pkt *cur, *prev = NULL;
02107
02108
02109 char *msg;
02110 int res = FALSE;
02111
02112 msg = sip_methods[sipmethod].text;
02113
02114 ast_mutex_lock(&p->lock);
02115 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
02116 if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
02117 ((ast_test_flag(cur, FLAG_RESPONSE)) ||
02118 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
02119 if (!resp && (seqno == p->pendinginvite)) {
02120 if (option_debug)
02121 ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
02122 p->pendinginvite = 0;
02123 }
02124
02125 res = TRUE;
02126 UNLINK(cur, p->packets, prev);
02127 if (cur->retransid > -1) {
02128 if (sipdebug && option_debug > 3)
02129 ast_log(LOG_DEBUG, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
02130 ast_sched_del(sched, cur->retransid);
02131 cur->retransid = -1;
02132 }
02133 free(cur);
02134 break;
02135 }
02136 }
02137 ast_mutex_unlock(&p->lock);
02138 if (option_debug)
02139 ast_log(LOG_DEBUG, "Stopping retransmission on '%s' of %s %d: Match %s\n", p->callid, resp ? "Response" : "Request", seqno, res ? "Not Found" : "Found");
02140 }
02141
02142
02143
02144 static void __sip_pretend_ack(struct sip_pvt *p)
02145 {
02146 struct sip_pkt *cur = NULL;
02147
02148 while (p->packets) {
02149 int method;
02150 if (cur == p->packets) {
02151 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
02152 return;
02153 }
02154 cur = p->packets;
02155 method = (cur->method) ? cur->method : find_sip_method(cur->data);
02156 __sip_ack(p, cur->seqno, ast_test_flag(cur, FLAG_RESPONSE), method);
02157 }
02158 }
02159
02160
02161 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02162 {
02163 struct sip_pkt *cur;
02164 int res = -1;
02165
02166 for (cur = p->packets; cur; cur = cur->next) {
02167 if (cur->seqno == seqno && ast_test_flag(cur, FLAG_RESPONSE) == resp &&
02168 (ast_test_flag(cur, FLAG_RESPONSE) || method_match(sipmethod, cur->data))) {
02169
02170 if (cur->retransid > -1) {
02171 if (option_debug > 3 && sipdebug)
02172 ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
02173 ast_sched_del(sched, cur->retransid);
02174 cur->retransid = -1;
02175 }
02176 res = 0;
02177 break;
02178 }
02179 }
02180 if (option_debug)
02181 ast_log(LOG_DEBUG, "(Provisional) Stopping retransmission (but retaining packet) on '%s' %s %d: %s\n", p->callid, resp ? "Response" : "Request", seqno, res ? "Not Found" : "Found");
02182 return res;
02183 }
02184
02185
02186
02187 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
02188 {
02189 memset(dst, 0, sizeof(*dst));
02190 memcpy(dst->data, src->data, sizeof(dst->data));
02191 dst->len = src->len;
02192 parse_request(dst);
02193 }
02194
02195
02196 static void add_blank(struct sip_request *req)
02197 {
02198 if (!req->lines) {
02199
02200 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
02201 req->len += strlen(req->data + req->len);
02202 }
02203 }
02204
02205
02206 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02207 {
02208 int res;
02209
02210 add_blank(req);
02211 if (sip_debug_test_pvt(p)) {
02212 const struct sockaddr_in *dst = sip_real_dst(p);
02213
02214 ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
02215 reliable ? "Reliably " : "", sip_nat_mode(p),
02216 ast_inet_ntoa(dst->sin_addr),
02217 ntohs(dst->sin_port), req->data);
02218 }
02219 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02220 struct sip_request tmp;
02221 parse_copy(&tmp, req);
02222 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"),
02223 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? tmp.rlPart2 : sip_methods[tmp.method].text);
02224 }
02225 res = (reliable) ?
02226 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02227 __sip_xmit(p, req->data, req->len);
02228 if (res > 0)
02229 return 0;
02230 return res;
02231 }
02232
02233
02234 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02235 {
02236 int res;
02237
02238 add_blank(req);
02239 if (sip_debug_test_pvt(p)) {
02240 if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
02241 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);
02242 else
02243 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);
02244 }
02245 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02246 struct sip_request tmp;
02247 parse_copy(&tmp, req);
02248 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
02249 }
02250 res = (reliable) ?
02251 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02252 __sip_xmit(p, req->data, req->len);
02253 return res;
02254 }
02255
02256
02257
02258
02259
02260 static const char *find_closing_quote(const char *start, const char *lim)
02261 {
02262 char last_char = '\0';
02263 const char *s;
02264 for (s = start; *s && s != lim; last_char = *s++) {
02265 if (*s == '"' && last_char != '\\')
02266 break;
02267 }
02268 return s;
02269 }
02270
02271
02272
02273
02274
02275
02276
02277
02278
02279
02280
02281
02282 static char *get_in_brackets(char *tmp)
02283 {
02284 const char *parse = tmp;
02285 char *first_bracket;
02286
02287
02288
02289
02290
02291 while ( (first_bracket = strchr(parse, '<')) ) {
02292 char *first_quote = strchr(parse, '"');
02293
02294 if (!first_quote || first_quote > first_bracket)
02295 break;
02296
02297 parse = find_closing_quote(first_quote + 1, NULL);
02298 if (!*parse) {
02299
02300 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
02301 break;
02302 }
02303 parse++;
02304 }
02305 if (first_bracket) {
02306 char *second_bracket = strchr(first_bracket + 1, '>');
02307 if (second_bracket) {
02308 *second_bracket = '\0';
02309 tmp = first_bracket + 1;
02310 } else {
02311 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
02312 }
02313 }
02314 return tmp;
02315 }
02316
02317
02318
02319 static int sip_sendtext(struct ast_channel *ast, const char *text)
02320 {
02321 struct sip_pvt *p = ast->tech_pvt;
02322 int debug = sip_debug_test_pvt(p);
02323
02324 if (debug)
02325 ast_verbose("Sending text %s on %s\n", text, ast->name);
02326 if (!p)
02327 return -1;
02328 if (ast_strlen_zero(text))
02329 return 0;
02330 if (debug)
02331 ast_verbose("Really sending text %s on %s\n", text, ast->name);
02332 transmit_message_with_text(p, text);
02333 return 0;
02334 }
02335
02336
02337
02338
02339
02340
02341 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey)
02342 {
02343 char port[10];
02344 char ipaddr[INET_ADDRSTRLEN];
02345 char regseconds[20];
02346
02347 char *sysname = ast_config_AST_SYSTEM_NAME;
02348 char *syslabel = NULL;
02349
02350 time_t nowtime = time(NULL) + expirey;
02351 const char *fc = fullcontact ? "fullcontact" : NULL;
02352
02353 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
02354 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
02355 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
02356
02357 if (ast_strlen_zero(sysname))
02358 sysname = NULL;
02359 else if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME))
02360 syslabel = "regserver";
02361
02362 if (fc)
02363 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02364 "port", port, "regseconds", regseconds,
02365 "username", username, fc, fullcontact, syslabel, sysname, NULL);
02366 else
02367 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02368 "port", port, "regseconds", regseconds,
02369 "username", username, syslabel, sysname, NULL);
02370 }
02371
02372
02373 static void register_peer_exten(struct sip_peer *peer, int onoff)
02374 {
02375 char multi[256];
02376 char *stringp, *ext, *context;
02377
02378
02379
02380
02381
02382 if (ast_strlen_zero(global_regcontext))
02383 return;
02384
02385 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
02386 stringp = multi;
02387 while ((ext = strsep(&stringp, "&"))) {
02388 if ((context = strchr(ext, '@'))) {
02389 *context++ = '\0';
02390 if (!ast_context_find(context)) {
02391 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
02392 continue;
02393 }
02394 } else {
02395 context = global_regcontext;
02396 }
02397 if (onoff)
02398 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
02399 ast_strdup(peer->name), ast_free, "SIP");
02400 else
02401 ast_context_remove_extension(context, ext, 1, NULL);
02402 }
02403 }
02404
02405
02406 static void sip_destroy_peer(struct sip_peer *peer)
02407 {
02408 if (option_debug > 2)
02409 ast_log(LOG_DEBUG, "Destroying SIP peer %s\n", peer->name);
02410
02411
02412 if (peer->call)
02413 sip_destroy(peer->call);
02414
02415 if (peer->mwipvt)
02416 sip_destroy(peer->mwipvt);
02417
02418 if (peer->chanvars) {
02419 ast_variables_destroy(peer->chanvars);
02420 peer->chanvars = NULL;
02421 }
02422 if (peer->expire > -1)
02423 ast_sched_del(sched, peer->expire);
02424
02425 if (peer->pokeexpire > -1)
02426 ast_sched_del(sched, peer->pokeexpire);
02427 register_peer_exten(peer, FALSE);
02428 ast_free_ha(peer->ha);
02429 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT))
02430 apeerobjs--;
02431 else if (ast_test_flag(&peer->flags[0], SIP_REALTIME))
02432 rpeerobjs--;
02433 else
02434 speerobjs--;
02435 clear_realm_authentication(peer->auth);
02436 peer->auth = NULL;
02437 free(peer);
02438 }
02439
02440
02441 static void update_peer(struct sip_peer *p, int expiry)
02442 {
02443 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02444 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) &&
02445 (ast_test_flag(&p->flags[0], SIP_REALTIME) || rtcachefriends)) {
02446 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, expiry);
02447 }
02448 }
02449
02450
02451
02452
02453
02454
02455
02456 static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_in *sin)
02457 {
02458 struct sip_peer *peer=NULL;
02459 struct ast_variable *var;
02460 struct ast_config *peerlist = NULL;
02461 struct ast_variable *tmp;
02462 struct ast_flags flags = {0};
02463 const char *iabuf = NULL;
02464 char portstring[6];
02465 const char *insecure;
02466 char *cat = NULL;
02467 unsigned short portnum;
02468
02469
02470 if (newpeername)
02471 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
02472 else if (sin) {
02473 iabuf = ast_inet_ntoa(sin->sin_addr);
02474 portnum = ntohs(sin->sin_port);
02475 sprintf(portstring, "%d", portnum);
02476 var = ast_load_realtime("sippeers", "host", iabuf, "port", portstring, NULL);
02477 if (!var)
02478 var = ast_load_realtime("sippeers", "ipaddr", iabuf, "port", portstring, NULL);
02479 if (!var) {
02480 peerlist = ast_load_realtime_multientry("sippeers", "host", iabuf, NULL);
02481 if(peerlist){
02482 while((cat = ast_category_browse(peerlist, cat)))
02483 {
02484 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02485 set_insecure_flags(&flags, insecure, -1);
02486 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02487 var = ast_category_root(peerlist, cat);
02488 break;
02489 }
02490 }
02491 }
02492 if(!var) {
02493 ast_config_destroy(peerlist);
02494 peerlist = NULL;
02495 cat = NULL;
02496 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", iabuf, NULL);
02497 if(peerlist) {
02498 while((cat = ast_category_browse(peerlist, cat)))
02499 {
02500 insecure = ast_variable_retrieve(peerlist, cat, "insecure");
02501 set_insecure_flags(&flags, insecure, -1);
02502 if(ast_test_flag(&flags, SIP_INSECURE_PORT)) {
02503 var = ast_category_root(peerlist, cat);
02504 break;
02505 }
02506 }
02507 }
02508 }
02509 }
02510 } else
02511 return NULL;
02512
02513 if (!var) {
02514 if(peerlist)
02515 ast_config_destroy(peerlist);
02516 return NULL;
02517 }
02518
02519 for (tmp = var; tmp; tmp = tmp->next) {
02520
02521 if (!strcasecmp(tmp->name, "type") &&
02522 !strcasecmp(tmp->value, "user")) {
02523 ast_variables_destroy(var);
02524 return NULL;
02525 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
02526 newpeername = tmp->value;
02527 }
02528 }
02529
02530 if (!newpeername) {
02531 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", iabuf);
02532 if(peerlist)
02533 ast_config_destroy(peerlist);
02534 else
02535 ast_variables_destroy(var);
02536 return NULL;
02537 }
02538
02539
02540 peer = build_peer(newpeername, var, NULL, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
02541 if (!peer) {
02542 if(peerlist)
02543 ast_config_destroy(peerlist);
02544 else
02545 ast_variables_destroy(var);
02546 return NULL;
02547 }
02548
02549 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
02550
02551 ast_copy_flags(&peer->flags[1],&global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
02552 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
02553 if (peer->expire > -1) {
02554 ast_sched_del(sched, peer->expire);
02555 }
02556 peer->expire = ast_sched_add(sched, (global_rtautoclear) * 1000, expire_register, (void *)peer);
02557 }
02558 ASTOBJ_CONTAINER_LINK(&peerl,peer);
02559 } else {
02560 ast_set_flag(&peer->flags[0], SIP_REALTIME);
02561 }
02562 if(peerlist)
02563 ast_config_destroy(peerlist);
02564 else
02565 ast_variables_destroy(var);
02566 return peer;
02567 }
02568
02569
02570 static int sip_addrcmp(char *name, struct sockaddr_in *sin)
02571 {
02572
02573 struct sip_peer *p = (struct sip_peer *) name;
02574 return !(!inaddrcmp(&p->addr, sin) ||
02575 (ast_test_flag(&p->flags[0], SIP_INSECURE_PORT) &&
02576 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
02577 }
02578
02579
02580
02581
02582 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
02583 {
02584 struct sip_peer *p = NULL;
02585
02586 if (peer)
02587 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
02588 else
02589 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, name, sip_addr_hashfunc, 1, sip_addrcmp);
02590
02591 if (!p && realtime)
02592 p = realtime_peer(peer, sin);
02593
02594 return p;
02595 }
02596
02597
02598 static void sip_destroy_user(struct sip_user *user)
02599 {
02600 if (option_debug > 2)
02601 ast_log(LOG_DEBUG, "Destroying user object from memory: %s\n", user->name);
02602 ast_free_ha(user->ha);
02603 if (user->chanvars) {
02604 ast_variables_destroy(user->chanvars);
02605 user->chanvars = NULL;
02606 }
02607 if (ast_test_flag(&user->flags[0], SIP_REALTIME))
02608 ruserobjs--;
02609 else
02610 suserobjs--;
02611 free(user);
02612 }
02613
02614
02615
02616
02617 static struct sip_user *realtime_user(const char *username)
02618 {
02619 struct ast_variable *var;
02620 struct ast_variable *tmp;
02621 struct sip_user *user = NULL;
02622
02623 var = ast_load_realtime("sipusers", "name", username, NULL);
02624
02625 if (!var)
02626 return NULL;
02627
02628 for (tmp = var; tmp; tmp = tmp->next) {
02629 if (!strcasecmp(tmp->name, "type") &&
02630 !strcasecmp(tmp->value, "peer")) {
02631 ast_variables_destroy(var);
02632 return NULL;
02633 }
02634 }
02635
02636 user = build_user(username, var, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
02637
02638 if (!user) {
02639 ast_variables_destroy(var);
02640 return NULL;
02641 }
02642
02643 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
02644 ast_set_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02645 suserobjs++;
02646 ASTOBJ_CONTAINER_LINK(&userl,user);
02647 } else {
02648
02649 suserobjs--;
02650 ruserobjs++;
02651 ast_set_flag(&user->flags[0], SIP_REALTIME);
02652 }
02653 ast_variables_destroy(var);
02654 return user;
02655 }
02656
02657
02658
02659
02660
02661 static struct sip_user *find_user(const char *name, int realtime)
02662 {
02663 struct sip_user *u = ASTOBJ_CONTAINER_FIND(&userl, name);
02664 if (!u && realtime)
02665 u = realtime_user(name);
02666 return u;
02667 }
02668
02669
02670 static void do_setnat(struct sip_pvt *p, int natflags)
02671 {
02672 const char *mode = natflags ? "On" : "Off";
02673
02674 if (p->rtp) {
02675 if (option_debug)
02676 ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", mode);
02677 ast_rtp_setnat(p->rtp, natflags);
02678 }
02679 if (p->vrtp) {
02680 if (option_debug)
02681 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", mode);
02682 ast_rtp_setnat(p->vrtp, natflags);
02683 }
02684 if (p->udptl) {
02685 if (option_debug)
02686 ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", mode);
02687 ast_udptl_setnat(p->udptl, natflags);
02688 }
02689 }
02690
02691
02692
02693
02694 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
02695 {
02696 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
02697 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
02698 dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
02699 dialog->recv = dialog->sa;
02700 } else
02701 return -1;
02702
02703 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
02704 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
02705 dialog->capability = peer->capability;
02706 if ((!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(dialog->capability & AST_FORMAT_VIDEO_MASK)) && dialog->vrtp) {
02707 ast_rtp_destroy(dialog->vrtp);
02708 dialog->vrtp = NULL;
02709 }
02710 dialog->prefs = peer->prefs;
02711 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
02712 dialog->t38.capability = global_t38_capability;
02713 if (dialog->udptl) {
02714 if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_FEC )
02715 dialog->t38.capability |= T38FAX_UDP_EC_FEC;
02716 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY )
02717 dialog->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
02718 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_NONE )
02719 dialog->t38.capability |= T38FAX_UDP_EC_NONE;
02720 dialog->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
02721 if (option_debug > 1)
02722 ast_log(LOG_DEBUG,"Our T38 capability (%d)\n", dialog->t38.capability);
02723 }
02724 dialog->t38.jointcapability = dialog->t38.capability;
02725 } else if (dialog->udptl) {
02726 ast_udptl_destroy(dialog->udptl);
02727 dialog->udptl = NULL;
02728 }
02729 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE );
02730
02731 if (dialog->rtp) {
02732 ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
02733 ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
02734 ast_rtp_set_rtptimeout(dialog->rtp, peer->rtptimeout);
02735 ast_rtp_set_rtpholdtimeout(dialog->rtp, peer->rtpholdtimeout);
02736 ast_rtp_set_rtpkeepalive(dialog->rtp, peer->rtpkeepalive);
02737
02738 ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
02739 dialog->autoframing = peer->autoframing;
02740 }
02741 if (dialog->vrtp) {
02742 ast_rtp_setdtmf(dialog->vrtp, 0);
02743 ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
02744 ast_rtp_set_rtptimeout(dialog->vrtp, peer->rtptimeout);
02745 ast_rtp_set_rtpholdtimeout(dialog->vrtp, peer->rtpholdtimeout);
02746 ast_rtp_set_rtpkeepalive(dialog->vrtp, peer->rtpkeepalive);
02747 }
02748
02749 ast_string_field_set(dialog, peername, peer->name);
02750 ast_string_field_set(dialog, authname, peer->username);
02751 ast_string_field_set(dialog, username, peer->username);
02752 ast_string_field_set(dialog, peersecret, peer->secret);
02753 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
02754 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
02755 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
02756 ast_string_field_set(dialog, tohost, peer->tohost);
02757 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
02758 if (!dialog->initreq.headers && !ast_strlen_zero(peer->fromdomain)) {
02759 char *tmpcall;
02760 char *c;
02761 tmpcall = ast_strdupa(dialog->callid);
02762 c = strchr(tmpcall, '@');
02763 if (c) {
02764 *c = '\0';
02765 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
02766 }
02767 }
02768 if (ast_strlen_zero(dialog->tohost))
02769 ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
02770 if (!ast_strlen_zero(peer->fromdomain))
02771 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
02772 if (!ast_strlen_zero(peer->fromuser))
02773 ast_string_field_set(dialog, fromuser, peer->fromuser);
02774 if (!ast_strlen_zero(peer->language))
02775 ast_string_field_set(dialog, language, peer->language);
02776 dialog->maxtime = peer->maxms;
02777 dialog->callgroup = peer->callgroup;
02778 dialog->pickupgroup = peer->pickupgroup;
02779 dialog->allowtransfer = peer->allowtransfer;
02780
02781
02782 if (peer->maxms && peer->lastms)
02783 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
02784 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
02785 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
02786 dialog->noncodeccapability |= AST_RTP_DTMF;
02787 else
02788 dialog->noncodeccapability &= ~AST_RTP_DTMF;
02789 dialog->jointnoncodeccapability = dialog->noncodeccapability;
02790 ast_string_field_set(dialog, context, peer->context);
02791 dialog->rtptimeout = peer->rtptimeout;
02792 if (peer->call_limit)
02793 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
02794 dialog->maxcallbitrate = peer->maxcallbitrate;
02795
02796 return 0;
02797 }
02798
02799
02800
02801
02802 static int create_addr(struct sip_pvt *dialog, const char *opeer)
02803 {
02804 struct hostent *hp;
02805 struct ast_hostent ahp;
02806 struct sip_peer *p;
02807 char *port;
02808 int portno;
02809 char host[MAXHOSTNAMELEN], *hostn;
02810 char peer[256];
02811
02812 ast_copy_string(peer, opeer, sizeof(peer));
02813 port = strchr(peer, ':');
02814 if (port)
02815 *port++ = '\0';
02816 dialog->sa.sin_family = AF_INET;
02817 dialog->timer_t1 = 500;
02818 p = find_peer(peer, NULL, 1);
02819
02820 if (p) {
02821 int res = create_addr_from_peer(dialog, p);
02822 ASTOBJ_UNREF(p, sip_destroy_peer);
02823 return res;
02824 }
02825 hostn = peer;
02826 portno = port ? atoi(port) : STANDARD_SIP_PORT;
02827 if (srvlookup) {
02828 char service[MAXHOSTNAMELEN];
02829 int tportno;
02830 int ret;
02831
02832 snprintf(service, sizeof(service), "_sip._udp.%s", peer);
02833 ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
02834 if (ret > 0) {
02835 hostn = host;
02836 portno = tportno;
02837 }
02838 }
02839 hp = ast_gethostbyname(hostn, &ahp);
02840 if (!hp) {
02841 ast_log(LOG_WARNING, "No such host: %s\n", peer);
02842 return -1;
02843 }
02844 ast_string_field_set(dialog, tohost, peer);
02845 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
02846 dialog->sa.sin_port = htons(portno);
02847 dialog->recv = dialog->sa;
02848 return 0;
02849 }
02850
02851
02852 static int auto_congest(void *nothing)
02853 {
02854 struct sip_pvt *p = nothing;
02855
02856 ast_mutex_lock(&p->lock);
02857 p->initid = -1;
02858 if (p->owner) {
02859
02860 if (!ast_channel_trylock(p->owner)) {
02861 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
02862 append_history(p, "Cong", "Auto-congesting (timer)");
02863 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
02864 ast_channel_unlock(p->owner);
02865 }
02866 }
02867 ast_mutex_unlock(&p->lock);
02868 return 0;
02869 }
02870
02871
02872
02873
02874 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
02875 {
02876 int res, xmitres = 0;
02877 struct sip_pvt *p;
02878 struct varshead *headp;
02879 struct ast_var_t *current;
02880 const char *referer = NULL;
02881
02882 p = ast->tech_pvt;
02883 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
02884 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
02885 return -1;
02886 }
02887
02888
02889 headp=&ast->varshead;
02890 AST_LIST_TRAVERSE(headp,current,entries) {
02891
02892 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
02893 p->options->vxml_url = ast_var_value(current);
02894 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
02895 p->options->uri_options = ast_var_value(current);
02896 } else if (!p->options->distinctive_ring && !strcasecmp(ast_var_name(current), "ALERT_INFO")) {
02897
02898 p->options->distinctive_ring = ast_var_value(current);
02899 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
02900
02901 p->options->addsipheaders = 1;
02902 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
02903
02904 p->options->transfer = 1;
02905 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
02906
02907 referer = ast_var_value(current);
02908 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
02909
02910 p->options->replaces = ast_var_value(current);
02911 } else if (!strcasecmp(ast_var_name(current), "T38CALL")) {
02912 p->t38.state = T38_LOCAL_DIRECT;
02913 if (option_debug)
02914 ast_log(LOG_DEBUG,"T38State change to %d on channel %s\n", p->t38.state, ast->name);
02915 }
02916
02917 }
02918
02919 res = 0;
02920 ast_set_flag(&p->flags[0], SIP_OUTGOING);
02921
02922 if (p->options->transfer) {
02923 char buf[BUFSIZ/2];
02924
02925 if (referer) {
02926 if (sipdebug && option_debug > 2)
02927 ast_log(LOG_DEBUG, "Call for %s transfered by %s\n", p->username, referer);
02928 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
02929 } else
02930 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
02931 ast_string_field_set(p, cid_name, buf);
02932 }
02933 if (option_debug)
02934 ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
02935
02936 res = update_call_counter(p, INC_CALL_RINGING);
02937 if ( res != -1 ) {
02938 p->callingpres = ast->cid.cid_pres;
02939 p->jointcapability = ast_translate_available_formats(p->capability, p->prefcodec);
02940 p->jointnoncodeccapability = p->noncodeccapability;
02941
02942
02943 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
02944 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
02945 res = -1;
02946 } else {
02947 p->t38.jointcapability = p->t38.capability;
02948 if (option_debug > 1)
02949 ast_log(LOG_DEBUG,"Our T38 capability (%d), joint T38 capability (%d)\n", p->t38.capability, p->t38.jointcapability);
02950 xmitres = transmit_invite(p, SIP_INVITE, 1, 2);
02951 if (xmitres == XMIT_ERROR)
02952 return -1;
02953
02954 p->invitestate = INV_CALLING;
02955
02956
02957 p->initid = ast_sched_add(sched, p->maxtime ? (p->maxtime * 4) : SIP_TRANS_TIMEOUT, auto_congest, p);
02958 }
02959 }
02960 return res;
02961 }
02962
02963
02964
02965 static void sip_registry_destroy(struct sip_registry *reg)
02966 {
02967
02968 if (option_debug > 2)
02969 ast_log(LOG_DEBUG, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
02970
02971 if (reg->call) {
02972
02973
02974 reg->call->registry = NULL;
02975 if (option_debug > 2)
02976 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
02977 sip_destroy(reg->call);
02978 }
02979 if (reg->expire > -1)
02980 ast_sched_del(sched, reg->expire);
02981 if (reg->timeout > -1)
02982 ast_sched_del(sched, reg->timeout);
02983 ast_string_field_free_pools(reg);
02984 regobjs--;
02985 free(reg);
02986
02987 }
02988
02989
02990 static void __sip_destroy(struct sip_pvt *p, int lockowner)
02991 {
02992 struct sip_pvt *cur, *prev = NULL;
02993 struct sip_pkt *cp;
02994
02995 if (sip_debug_test_pvt(p) || option_debug > 2)
02996 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
02997
02998 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
02999 update_call_counter(p, DEC_CALL_LIMIT);
03000 if (option_debug > 1)
03001 ast_log(LOG_DEBUG, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
03002 }
03003
03004
03005 if (p->relatedpeer && p->relatedpeer->mwipvt)
03006 p->relatedpeer->mwipvt = NULL;
03007
03008 if (dumphistory)
03009 sip_dump_history(p);
03010
03011 if (p->options)
03012 free(p->options);
03013
03014 if (p->stateid > -1)
03015 ast_extension_state_del(p->stateid, NULL);
03016 if (p->initid > -1)
03017 ast_sched_del(sched, p->initid);
03018 if (p->autokillid > -1)
03019 ast_sched_del(sched, p->autokillid);
03020
03021 if (p->rtp)
03022 ast_rtp_destroy(p->rtp);
03023 if (p->vrtp)
03024 ast_rtp_destroy(p->vrtp);
03025 if (p->udptl)
03026 ast_udptl_destroy(p->udptl);
03027 if (p->refer)
03028 free(p->refer);
03029 if (p->route) {
03030 free_old_route(p->route);
03031 p->route = NULL;
03032 }
03033 if (p->registry) {
03034 if (p->registry->call == p)
03035 p->registry->call = NULL;
03036 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
03037 }
03038
03039
03040 if (p->owner) {
03041 if (lockowner)
03042 ast_channel_lock(p->owner);
03043 if (option_debug)
03044 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
03045 p->owner->tech_pvt = NULL;
03046 if (lockowner)
03047 ast_channel_unlock(p->owner);
03048 }
03049
03050 if (p->history) {
03051 struct sip_history *hist;
03052 while( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) )
03053 free(hist);
03054 free(p->history);
03055 p->history = NULL;
03056 }
03057
03058 for (prev = NULL, cur = iflist; cur; prev = cur, cur = cur->next) {
03059 if (cur == p) {
03060 UNLINK(cur, iflist, prev);
03061 break;
03062 }
03063 }
03064 if (!cur) {
03065 ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
03066 return;
03067 }
03068
03069
03070 while((cp = p->packets)) {
03071 p->packets = p->packets->next;
03072 if (cp->retransid > -1)
03073 ast_sched_del(sched, cp->retransid);
03074 free(cp);
03075 }
03076 if (p->chanvars) {
03077 ast_variables_destroy(p->chanvars);
03078 p->chanvars = NULL;
03079 }
03080 ast_mutex_destroy(&p->lock);
03081
03082 ast_string_field_free_pools(p);
03083
03084 free(p);
03085 }
03086
03087
03088
03089
03090
03091
03092
03093
03094
03095
03096
03097
03098
03099
03100
03101 static int update_call_counter(struct sip_pvt *fup, int event)
03102 {
03103 char name[256];
03104 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
03105 int outgoing = ast_test_flag(&fup->flags[1], SIP_PAGE2_OUTGOING_CALL);
03106 struct sip_user *u = NULL;
03107 struct sip_peer *p = NULL;
03108
03109 if (option_debug > 2)
03110 ast_log(LOG_DEBUG, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
03111
03112
03113
03114 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
03115 return 0;
03116
03117 ast_copy_string(name, fup->username, sizeof(name));
03118
03119
03120 if (global_limitonpeers == FALSE && !outgoing && (u = find_user(name, 1))) {
03121 inuse = &u->inUse;
03122 call_limit = &u->call_limit;
03123 inringing = NULL;
03124 } else if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, 1) ) ) {
03125 inuse = &p->inUse;
03126 call_limit = &p->call_limit;
03127 inringing = &p->inRinging;
03128 ast_copy_string(name, fup->peername, sizeof(name));
03129 }
03130 if (!p && !u) {
03131 if (option_debug > 1)
03132 ast_log(LOG_DEBUG, "%s is not a local device, no call limit\n", name);
03133 return 0;
03134 }
03135
03136 switch(event) {
03137
03138 case DEC_CALL_LIMIT:
03139 if ( *inuse > 0 ) {
03140 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
03141 (*inuse)--;
03142 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
03143 }
03144 } else {
03145 *inuse = 0;
03146 }
03147 if (inringing) {
03148 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03149 if (*inringing > 0)
03150 (*inringing)--;
03151 else if (!ast_test_flag(&fup->flags[0], SIP_REALTIME) || ast_test_flag(&fup->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03152 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", fup->peername);
03153 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03154 }
03155 }
03156 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold) {
03157 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
03158 sip_peer_hold(fup, 0);
03159 }
03160 if (option_debug > 1 || sipdebug) {
03161 ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
03162 }
03163 break;
03164
03165 case INC_CALL_RINGING:
03166 case INC_CALL_LIMIT:
03167 if (*call_limit > 0 ) {
03168 if (*inuse >= *call_limit) {
03169 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);
03170 if (u)
03171 ASTOBJ_UNREF(u, sip_destroy_user);
03172 else
03173 ASTOBJ_UNREF(p, sip_destroy_peer);
03174 return -1;
03175 }
03176 }
03177 if (inringing && (event == INC_CALL_RINGING)) {
03178 if (!ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03179 (*inringing)++;
03180 ast_set_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03181 }
03182 }
03183
03184 (*inuse)++;
03185 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
03186 if (option_debug > 1 || sipdebug) {
03187 ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
03188 }
03189 break;
03190
03191 case DEC_CALL_RINGING:
03192 if (inringing) {
03193 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03194 if (*inringing > 0)
03195 (*inringing)--;
03196 else if (!ast_test_flag(&fup->flags[0], SIP_REALTIME) || ast_test_flag(&fup->flags[1], SIP_PAGE2_RTCACHEFRIENDS))
03197 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", p->name);
03198 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03199 }
03200 }
03201 break;
03202
03203 default:
03204 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
03205 }
03206 if (p) {
03207 ast_device_state_changed("SIP/%s", p->name);
03208 ASTOBJ_UNREF(p, sip_destroy_peer);
03209 } else
03210 ASTOBJ_UNREF(u, sip_destroy_user);
03211 return 0;
03212 }
03213
03214
03215 static void sip_destroy(struct sip_pvt *p)
03216 {
03217 ast_mutex_lock(&iflock);
03218 if (option_debug > 2)
03219 ast_log(LOG_DEBUG, "Destroying SIP dialog %s\n", p->callid);
03220 __sip_destroy(p, 1);
03221 ast_mutex_unlock(&iflock);
03222 }
03223
03224
03225 static int hangup_sip2cause(int cause)
03226 {
03227
03228
03229 switch(cause) {
03230 case 401:
03231 return AST_CAUSE_CALL_REJECTED;
03232 case 403:
03233 return AST_CAUSE_CALL_REJECTED;
03234 case 404:
03235 return AST_CAUSE_UNALLOCATED;
03236 case 405:
03237 return AST_CAUSE_INTERWORKING;
03238 case 407:
03239 return AST_CAUSE_CALL_REJECTED;
03240 case 408:
03241 return AST_CAUSE_NO_USER_RESPONSE;
03242 case 409:
03243 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
03244 case 410:
03245 return AST_CAUSE_UNALLOCATED;
03246 case 411:
03247 return AST_CAUSE_INTERWORKING;
03248 case 413:
03249 return AST_CAUSE_INTERWORKING;
03250 case 414:
03251 return AST_CAUSE_INTERWORKING;
03252 case 415:
03253 return AST_CAUSE_INTERWORKING;
03254 case 420:
03255 return AST_CAUSE_NO_ROUTE_DESTINATION;
03256 case 480:
03257 return AST_CAUSE_NO_ANSWER;
03258 case 481:
03259 return AST_CAUSE_INTERWORKING;
03260 case 482:
03261 return AST_CAUSE_INTERWORKING;
03262 case 483:
03263 return AST_CAUSE_NO_ANSWER;
03264 case 484:
03265 return AST_CAUSE_INVALID_NUMBER_FORMAT;
03266 case 485:
03267 return AST_CAUSE_UNALLOCATED;
03268 case 486:
03269 return AST_CAUSE_BUSY;
03270 case 487:
03271 return AST_CAUSE_INTERWORKING;
03272 case 488:
03273 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03274 case 491:
03275 return AST_CAUSE_INTERWORKING;
03276 case 493:
03277 return AST_CAUSE_INTERWORKING;
03278 case 500:
03279 return AST_CAUSE_FAILURE;
03280 case 501:
03281 return AST_CAUSE_FACILITY_REJECTED;
03282 case 502:
03283 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
03284 case 503:
03285 return AST_CAUSE_CONGESTION;
03286 case 504:
03287 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
03288 case 505:
03289 return AST_CAUSE_INTERWORKING;
03290 case 600:
03291 return AST_CAUSE_USER_BUSY;
03292 case 603:
03293 return AST_CAUSE_CALL_REJECTED;
03294 case 604:
03295 return AST_CAUSE_UNALLOCATED;
03296 case 606:
03297 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03298 default:
03299 return AST_CAUSE_NORMAL;
03300 }
03301
03302 return 0;
03303 }
03304
03305
03306
03307
03308
03309
03310
03311
03312
03313
03314
03315
03316
03317
03318
03319
03320
03321
03322
03323
03324
03325
03326
03327
03328
03329
03330
03331
03332
03333
03334
03335
03336
03337 static const char *hangup_cause2sip(int cause)
03338 {
03339 switch (cause) {
03340 case AST_CAUSE_UNALLOCATED:
03341 case AST_CAUSE_NO_ROUTE_DESTINATION:
03342 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
03343 return "404 Not Found";
03344 case AST_CAUSE_CONGESTION:
03345 case AST_CAUSE_SWITCH_CONGESTION:
03346 return "503 Service Unavailable";
03347 case AST_CAUSE_NO_USER_RESPONSE:
03348 return "408 Request Timeout";
03349 case AST_CAUSE_NO_ANSWER:
03350 return "480 Temporarily unavailable";
03351 case AST_CAUSE_CALL_REJECTED:
03352 return "403 Forbidden";
03353 case AST_CAUSE_NUMBER_CHANGED:
03354 return "410 Gone";
03355 case AST_CAUSE_NORMAL_UNSPECIFIED:
03356 return "480 Temporarily unavailable";
03357 case AST_CAUSE_INVALID_NUMBER_FORMAT:
03358 return "484 Address incomplete";
03359 case AST_CAUSE_USER_BUSY:
03360 return "486 Busy here";
03361 case AST_CAUSE_FAILURE:
03362 return "500 Server internal failure";
03363 case AST_CAUSE_FACILITY_REJECTED:
03364 return "501 Not Implemented";
03365 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
03366 return "503 Service Unavailable";
03367
03368 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
03369 return "502 Bad Gateway";
03370 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
03371 return "488 Not Acceptable Here";
03372
03373 case AST_CAUSE_NOTDEFINED:
03374 default:
03375 if (option_debug)
03376 ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
03377 return NULL;
03378 }
03379
03380
03381 return 0;
03382 }
03383
03384
03385
03386
03387 static int sip_hangup(struct ast_channel *ast)
03388 {
03389 struct sip_pvt *p = ast->tech_pvt;
03390 int needcancel = FALSE;
03391 int needdestroy = 0;
03392 struct ast_channel *oldowner = ast;
03393
03394 if (!p) {
03395 if (option_debug)
03396 ast_log(LOG_DEBUG, "Asked to hangup channel that was not connected\n");
03397 return 0;
03398 }
03399
03400 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
03401 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03402 if (option_debug && sipdebug)
03403 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03404 update_call_counter(p, DEC_CALL_LIMIT);
03405 }
03406 if (option_debug >3)
03407 ast_log(LOG_DEBUG, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
03408 if (p->autokillid > -1)
03409 sip_cancel_destroy(p);
03410 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03411 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
03412 ast_clear_flag(&p->flags[0], SIP_NEEDDESTROY);
03413 p->owner->tech_pvt = NULL;
03414 p->owner = NULL;
03415 return 0;
03416 }
03417 if (option_debug) {
03418 if (ast_test_flag(ast, AST_FLAG_ZOMBIE) && p->refer && option_debug)
03419 ast_log(LOG_DEBUG, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
03420 else {
03421 if (option_debug)
03422 ast_log(LOG_DEBUG, "Hangup call %s, SIP callid %s)\n", ast->name, p->callid);
03423 }
03424 }
03425 if (option_debug && ast_test_flag(ast, AST_FLAG_ZOMBIE))
03426 ast_log(LOG_DEBUG, "Hanging up zombie call. Be scared.\n");
03427
03428 ast_mutex_lock(&p->lock);
03429 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
03430 if (option_debug && sipdebug)
03431 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03432 update_call_counter(p, DEC_CALL_LIMIT);
03433 }
03434
03435
03436 if (p->owner != ast) {
03437 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
03438 ast_mutex_unlock(&p->lock);
03439 return 0;
03440 }
03441
03442 if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING || (p->invitestate < INV_COMPLETED && ast->_state != AST_STATE_UP)) {
03443 needcancel = TRUE;
03444 if (option_debug > 3)
03445 ast_log(LOG_DEBUG, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
03446 }
03447
03448 stop_media_flows(p);
03449
03450 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", p->owner ? ast_cause2str(p->owner->hangupcause) : "Unknown");
03451
03452
03453 if (p->vad)
03454 ast_dsp_free(p->vad);
03455
03456 p->owner = NULL;
03457 ast->tech_pvt = NULL;
03458
03459 ast_module_unref(ast_module_info->self);
03460
03461
03462
03463
03464
03465
03466
03467 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE))
03468 needdestroy = 1;
03469 else if (p->invitestate != INV_CALLING)
03470 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03471
03472
03473 if (!ast_test_flag(&p->flags[0], SIP_ALREADYGONE) && !ast_strlen_zero(p->initreq.data)) {
03474 if (needcancel) {
03475 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03476
03477 __sip_pretend_ack(p);
03478
03479
03480 if (p->invitestate == INV_CALLING) {
03481
03482 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03483
03484 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03485 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
03486 } else {
03487
03488 transmit_request(p, SIP_CANCEL, p->ocseq, XMIT_RELIABLE, FALSE);
03489
03490
03491 needdestroy = 0;
03492 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03493 }
03494 if ( p->initid != -1 ) {
03495
03496
03497 update_call_counter(p, INC_CALL_LIMIT);
03498 }
03499 } else {
03500 const char *res;
03501 if (ast->hangupcause && (res = hangup_cause2sip(ast->hangupcause)))
03502 transmit_response_reliable(p, res, &p->initreq);
03503 else
03504 transmit_response_reliable(p, "603 Declined", &p->initreq);
03505 }
03506 } else {
03507 if (!p->pendinginvite) {
03508 char *audioqos = "";
03509 char *videoqos = "";
03510 if (p->rtp)
03511 audioqos = ast_rtp_get_quality(p->rtp, NULL);
03512 if (p->vrtp)
03513 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
03514
03515 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03516
03517
03518 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
03519 if (p->rtp)
03520 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
03521 if (p->vrtp)
03522 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
03523 }
03524 if (p->rtp && oldowner)
03525 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", audioqos);
03526 if (p->vrtp && oldowner)
03527 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", videoqos);
03528 } else {
03529
03530
03531 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03532 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
03533 sip_cancel_destroy(p);
03534 }
03535 }
03536 }
03537 if (needdestroy)
03538 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
03539 ast_mutex_unlock(&p->lock);
03540 return 0;
03541 }
03542
03543
03544 static void try_suggested_sip_codec(struct sip_pvt *p)
03545 {
03546 int fmt;
03547 const char *codec;
03548
03549 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
03550 if (!codec)
03551 return;
03552
03553 fmt = ast_getformatbyname(codec);
03554 if (fmt) {
03555 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
03556 if (p->jointcapability & fmt) {
03557 p->jointcapability &= fmt;
03558 p->capability &= fmt;
03559 } else
03560 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
03561 } else
03562 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
03563 return;
03564 }
03565
03566
03567
03568 static int sip_answer(struct ast_channel *ast)
03569 {
03570 int res = 0;
03571 struct sip_pvt *p = ast->tech_pvt;
03572
03573 ast_mutex_lock(&p->lock);
03574 if (ast->_state != AST_STATE_UP) {
03575 try_suggested_sip_codec(p);
03576
03577 ast_setstate(ast, AST_STATE_UP);
03578 if (option_debug)
03579 ast_log(LOG_DEBUG, "SIP answering channel: %s\n", ast->name);
03580 if (p->t38.state == T38_PEER_DIRECT) {
03581 p->t38.state = T38_ENABLED;
03582 if (option_debug > 1)
03583 ast_log(LOG_DEBUG,"T38State change to %d on channel %s\n", p->t38.state, ast->name);
03584 res = transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
03585 } else
03586 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
03587 }
03588 ast_mutex_unlock(&p->lock);
03589 return res;
03590 }
03591
03592
03593 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
03594 {
03595 struct sip_pvt *p = ast->tech_pvt;
03596 int res = 0;
03597
03598 switch (frame->frametype) {
03599 case AST_FRAME_VOICE:
03600 if (!(frame->subclass & ast->nativeformats)) {
03601 char s1[512], s2[512], s3[512];
03602 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %s(%d) read/write = %s(%d)/%s(%d)\n",
03603 frame->subclass,
03604 ast_getformatname_multiple(s1, sizeof(s1) - 1, ast->nativeformats & AST_FORMAT_AUDIO_MASK),
03605 ast->nativeformats & AST_FORMAT_AUDIO_MASK,
03606 ast_getformatname_multiple(s2, sizeof(s2) - 1, ast->readformat),
03607 ast->readformat,
03608 ast_getformatname_multiple(s3, sizeof(s3) - 1, ast->writeformat),
03609 ast->writeformat);
03610 return 0;
03611 }
03612 if (p) {
03613 ast_mutex_lock(&p->lock);
03614 if (p->rtp) {
03615
03616 if ((ast->_state != AST_STATE_UP) &&
03617 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03618 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03619 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03620 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03621 }
03622 p->lastrtptx = time(NULL);
03623 res = ast_rtp_write(p->rtp, frame);
03624 }
03625 ast_mutex_unlock(&p->lock);
03626 }
03627 break;
03628 case AST_FRAME_VIDEO:
03629 if (p) {
03630 ast_mutex_lock(&p->lock);
03631 if (p->vrtp) {
03632
03633 if ((ast->_state != AST_STATE_UP) &&
03634 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03635 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03636 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03637 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03638 }
03639 p->lastrtptx = time(NULL);
03640 res = ast_rtp_write(p->vrtp, frame);
03641 }
03642 ast_mutex_unlock(&p->lock);
03643 }
03644 break;
03645 case AST_FRAME_IMAGE:
03646 return 0;
03647 break;
03648 case AST_FRAME_MODEM:
03649 if (p) {
03650 ast_mutex_lock(&p->lock);
03651
03652
03653
03654
03655 if (p->udptl && ast->_state == AST_STATE_UP)
03656 res = ast_udptl_write(p->udptl, frame);
03657 ast_mutex_unlock(&p->lock);
03658 }
03659 break;
03660 default:
03661 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
03662 return 0;
03663 }
03664
03665 return res;
03666 }
03667
03668
03669
03670 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03671 {
03672 int ret = -1;
03673 struct sip_pvt *p;
03674
03675 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE) && option_debug)
03676 ast_log(LOG_DEBUG, "New channel is zombie\n");
03677 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE) && option_debug)
03678 ast_log(LOG_DEBUG, "Old channel is zombie\n");
03679
03680 if (!newchan || !newchan->tech_pvt) {
03681 if (!newchan)
03682 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
03683 else
03684 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
03685 return -1;
03686 }
03687 p = newchan->tech_pvt;
03688
03689 if (!p) {
03690 ast_log(LOG_WARNING, "No pvt after masquerade. Strange things may happen\n");
03691 return -1;
03692 }
03693
03694 ast_mutex_lock(&p->lock);
03695 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
03696 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
03697 if (p->owner != oldchan)
03698 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
03699 else {
03700 p->owner = newchan;
03701 ret = 0;
03702 }
03703 if (option_debug > 2)
03704 ast_log(LOG_DEBUG, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
03705
03706 ast_mutex_unlock(&p->lock);
03707 return ret;
03708 }
03709
03710 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
03711 {
03712 struct sip_pvt *p = ast->tech_pvt;
03713 int res = 0;
03714
03715 ast_mutex_lock(&p->lock);
03716 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
03717 case SIP_DTMF_INBAND:
03718 res = -1;
03719 break;
03720 case SIP_DTMF_RFC2833:
03721 if (p->rtp)
03722 ast_rtp_senddigit_begin(p->rtp, digit);
03723 break;
03724 default:
03725 break;
03726 }
03727 ast_mutex_unlock(&p->lock);
03728
03729 return res;
03730 }
03731
03732
03733
03734 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
03735 {
03736 struct sip_pvt *p = ast->tech_pvt;
03737 int res = 0;
03738
03739 ast_mutex_lock(&p->lock);
03740 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
03741 case SIP_DTMF_INFO:
03742 transmit_info_with_digit(p, digit, duration);
03743 break;
03744 case SIP_DTMF_RFC2833:
03745 if (p->rtp)
03746 ast_rtp_senddigit_end(p->rtp, digit);
03747 break;
03748 case SIP_DTMF_INBAND:
03749 res = -1;
03750 break;
03751 }
03752 ast_mutex_unlock(&p->lock);
03753
03754 return res;
03755 }
03756
03757
03758 static int sip_transfer(struct ast_channel *ast, const char *dest)
03759 {
03760 struct sip_pvt *p = ast->tech_pvt;
03761 int res;
03762
03763 if (dest == NULL)
03764 dest = "";
03765 ast_mutex_lock(&p->lock);
03766 if (ast->_state == AST_STATE_RING)
03767 res = sip_sipredirect(p, dest);
03768 else
03769 res = transmit_refer(p, dest);
03770 ast_mutex_unlock(&p->lock);
03771 return res;
03772 }
03773
03774
03775
03776
03777
03778
03779 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
03780 {
03781 struct sip_pvt *p = ast->tech_pvt;
03782 int res = 0;
03783
03784 ast_mutex_lock(&p->lock);
03785 switch(condition) {
03786 case AST_CONTROL_RINGING:
03787 if (ast->_state == AST_STATE_RING) {
03788 p->invitestate = INV_EARLY_MEDIA;
03789 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
03790 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
03791
03792 transmit_response(p, "180 Ringing", &p->initreq);
03793 ast_set_flag(&p->flags[0], SIP_RINGING);
03794 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
03795 break;
03796 } else {
03797
03798 }
03799 }
03800 res = -1;
03801 break;
03802 case AST_CONTROL_BUSY:
03803 if (ast->_state != AST_STATE_UP) {
03804 transmit_response(p, "486 Busy Here", &p->initreq);
03805 p->invitestate = INV_COMPLETED;
03806 sip_alreadygone(p);
03807 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
03808 break;
03809 }
03810 res = -1;
03811 break;
03812 case AST_CONTROL_CONGESTION:
03813 if (ast->_state != AST_STATE_UP) {
03814 transmit_response(p, "503 Service Unavailable", &p->initreq);
03815 p->invitestate = INV_COMPLETED;
03816 sip_alreadygone(p);
03817 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
03818 break;
03819 }
03820 res = -1;
03821 break;
03822 case AST_CONTROL_PROCEEDING:
03823 if ((ast->_state != AST_STATE_UP) &&
03824 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03825 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03826 transmit_response(p, "100 Trying", &p->initreq);
03827 p->invitestate = INV_PROCEEDING;
03828 break;
03829 }
03830 res = -1;
03831 break;
03832 case AST_CONTROL_PROGRESS:
03833 if ((ast->_state != AST_STATE_UP) &&
03834 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03835 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03836 p->invitestate = INV_EARLY_MEDIA;
03837 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03838 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03839 break;
03840 }
03841 res = -1;
03842 break;
03843 case AST_CONTROL_HOLD:
03844 ast_moh_start(ast, data, p->mohinterpret);
03845 break;
03846 case AST_CONTROL_UNHOLD:
03847 ast_moh_stop(ast);
03848 break;
03849 case AST_CONTROL_VIDUPDATE:
03850 if (p->vrtp && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
03851 transmit_info_with_vidupdate(p);
03852
03853 } else
03854 res = -1;
03855 break;
03856 case -1:
03857 res = -1;
03858 break;
03859 default:
03860 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
03861 res = -1;
03862 break;
03863 }
03864 ast_mutex_unlock(&p->lock);
03865 return res;
03866 }
03867
03868
03869
03870
03871
03872
03873
03874
03875 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
03876 {
03877 struct ast_channel *tmp;
03878 struct ast_variable *v = NULL;
03879 int fmt;
03880 int what;
03881 int needvideo = 0;
03882 {
03883 const char *my_name;
03884
03885 if (title)
03886 my_name = title;
03887 else if ( (my_name = strchr(i->fromdomain,':')) )
03888 my_name++;
03889 else
03890 my_name = i->fromdomain;
03891
03892 ast_mutex_unlock(&i->lock);
03893
03894 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);
03895
03896 }
03897 if (!tmp) {
03898 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
03899 return NULL;
03900 }
03901 ast_mutex_lock(&i->lock);
03902
03903 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO)
03904 tmp->tech = &sip_tech_info;
03905 else
03906 tmp->tech = &sip_tech;
03907
03908
03909
03910 if (i->jointcapability)
03911 what = i->jointcapability;
03912 else if (i->capability)
03913 what = i->capability;
03914 else
03915 what = global_capability;
03916
03917
03918 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
03919 if (option_debug > 2) {
03920 char buf[BUFSIZ];
03921 ast_log(LOG_DEBUG, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, BUFSIZ, tmp->nativeformats));
03922 ast_log(LOG_DEBUG, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, BUFSIZ, i->jointcapability));
03923 ast_log(LOG_DEBUG, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, BUFSIZ, i->capability));
03924 ast_log(LOG_DEBUG, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, BUFSIZ, ast_codec_choose(&i->prefs, what, 1)));
03925 if (i->prefcodec)
03926 ast_log(LOG_DEBUG, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, BUFSIZ, i->prefcodec));
03927 }
03928
03929
03930 fmt = ast_best_codec(tmp->nativeformats);
03931
03932
03933
03934
03935
03936 if (i->vrtp) {
03937 if (i->prefcodec)
03938 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
03939 else
03940 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
03941 }
03942
03943 if (option_debug > 2) {
03944 if (needvideo)
03945 ast_log(LOG_DEBUG, "This channel can handle video! HOLLYWOOD next!\n");
03946 else
03947 ast_log(LOG_DEBUG, "This channel will not be able to handle video.\n");
03948 }
03949
03950
03951
03952 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
03953 i->vad = ast_dsp_new();
03954 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
03955 if (global_relaxdtmf)
03956 ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
03957 }
03958 if (i->rtp) {
03959 tmp->fds[0] = ast_rtp_fd(i->rtp);
03960 tmp->fds[1] = ast_rtcp_fd(i->rtp);
03961 }
03962 if (needvideo && i->vrtp) {
03963 tmp->fds[2] = ast_rtp_fd(i->vrtp);
03964 tmp->fds[3] = ast_rtcp_fd(i->vrtp);
03965 }
03966 if (i->udptl) {
03967 tmp->fds[5] = ast_udptl_fd(i->udptl);
03968 }
03969 if (state == AST_STATE_RING)
03970 tmp->rings = 1;
03971 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
03972 tmp->writeformat = fmt;
03973 tmp->rawwriteformat = fmt;
03974 tmp->readformat = fmt;
03975 tmp->rawreadformat = fmt;
03976 tmp->tech_pvt = i;
03977
03978 tmp->callgroup = i->callgroup;
03979 tmp->pickupgroup = i->pickupgroup;
03980 tmp->cid.cid_pres = i->callingpres;
03981 if (!ast_strlen_zero(i->accountcode))
03982 ast_string_field_set(tmp, accountcode, i->accountcode);
03983 if (i->amaflags)
03984 tmp->amaflags = i->amaflags;
03985 if (!ast_strlen_zero(i->language))
03986 ast_string_field_set(tmp, language, i->language);
03987 i->owner = tmp;
03988 ast_module_ref(ast_module_info->self);
03989 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
03990 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
03991
03992
03993
03994
03995 tmp->cid.cid_num = ast_strdup(i->cid_num);
03996 tmp->cid.cid_ani = ast_strdup(i->cid_num);
03997 tmp->cid.cid_name = ast_strdup(i->cid_name);
03998 if (!ast_strlen_zero(i->rdnis))
03999 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
04000
04001 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
04002 tmp->cid.cid_dnid = ast_strdup(i->exten);
04003
04004 tmp->priority = 1;
04005 if (!ast_strlen_zero(i->uri))
04006 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
04007 if (!ast_strlen_zero(i->domain))
04008 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
04009 if (!ast_strlen_zero(i->useragent))
04010 pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
04011 if (!ast_strlen_zero(i->callid))
04012 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
04013 if (i->rtp)
04014 ast_jb_configure(tmp, &global_jbconf);
04015
04016
04017 for (v = i->chanvars ; v ; v = v->next)
04018 pbx_builtin_setvar_helper(tmp, v->name, v->value);
04019
04020 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
04021 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
04022 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
04023 ast_hangup(tmp);
04024 tmp = NULL;
04025 }
04026
04027 if (!ast_test_flag(&i->flags[0], SIP_NO_HISTORY))
04028 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
04029
04030 return tmp;
04031 }
04032
04033
04034 static char *get_body_by_line(const char *line, const char *name, int nameLen)
04035 {
04036 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=')
04037 return ast_skip_blanks(line + nameLen + 1);
04038
04039 return "";
04040 }
04041
04042
04043
04044
04045
04046 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
04047 {
04048 int len = strlen(name);
04049
04050 while (*start < req->sdp_end) {
04051 const char *r = get_body_by_line(req->line[(*start)++], name, len);
04052 if (r[0] != '\0')
04053 return r;
04054 }
04055
04056 return "";
04057 }
04058
04059
04060 static const char *get_sdp(struct sip_request *req, const char *name)
04061 {
04062 int dummy = 0;
04063
04064 return get_sdp_iterate(&dummy, req, name);
04065 }
04066
04067
04068 static char *get_body(struct sip_request *req, char *name)
04069 {
04070 int x;
04071 int len = strlen(name);
04072 char *r;
04073
04074 for (x = 0; x < req->lines; x++) {
04075 r = get_body_by_line(req->line[x], name, len);
04076 if (r[0] != '\0')
04077 return r;
04078 }
04079
04080 return "";
04081 }
04082
04083
04084 static const char *find_alias(const char *name, const char *_default)
04085 {
04086
04087 static const struct cfalias {
04088 char * const fullname;
04089 char * const shortname;
04090 } aliases[] = {
04091 { "Content-Type", "c" },
04092 { "Content-Encoding", "e" },
04093 { "From", "f" },
04094 { "Call-ID", "i" },
04095 { "Contact", "m" },
04096 { "Content-Length", "l" },
04097 { "Subject", "s" },
04098 { "To", "t" },
04099 { "Supported", "k" },
04100 { "Refer-To", "r" },
04101 { "Referred-By", "b" },
04102 { "Allow-Events", "u" },
04103 { "Event", "o" },
04104 { "Via", "v" },
04105 { "Accept-Contact", "a" },
04106 { "Reject-Contact", "j" },
04107 { "Request-Disposition", "d" },
04108 { "Session-Expires", "x" },
04109 { "Identity", "y" },
04110 { "Identity-Info", "n" },
04111 };
04112 int x;
04113
04114 for (x=0; x<sizeof(aliases) / sizeof(aliases[0]); x++)
04115 if (!strcasecmp(aliases[x].fullname, name))
04116 return aliases[x].shortname;
04117
04118 return _default;
04119 }
04120
04121 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
04122 {
04123 int pass;
04124
04125
04126
04127
04128
04129
04130
04131
04132
04133
04134 for (pass = 0; name && pass < 2;pass++) {
04135 int x, len = strlen(name);
04136 for (x=*start; x<req->headers; x++) {
04137 if (!strncasecmp(req->header[x], name, len)) {
04138 char *r = req->header[x] + len;
04139 if (pedanticsipchecking)
04140 r = ast_skip_blanks(r);
04141
04142 if (*r == ':') {
04143 *start = x+1;
04144 return ast_skip_blanks(r+1);
04145 }
04146 }
04147 }
04148 if (pass == 0)
04149 name = find_alias(name, NULL);
04150 }
04151
04152
04153 return "";
04154 }
04155
04156
04157 static const char *get_header(const struct sip_request *req, const char *name)
04158 {
04159 int start = 0;
04160 return __get_header(req, name, &start);
04161 }
04162
04163
04164 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
04165 {
04166
04167 struct ast_frame *f;
04168
04169 if (!p->rtp) {
04170
04171 return &ast_null_frame;
04172 }
04173
04174 switch(ast->fdno) {
04175 case 0:
04176 f = ast_rtp_read(p->rtp);
04177 break;
04178 case 1:
04179 f = ast_rtcp_read(p->rtp);
04180 break;
04181 case 2:
04182 f = ast_rtp_read(p->vrtp);
04183 break;
04184 case 3:
04185 f = ast_rtcp_read(p->vrtp);
04186 break;
04187 case 5:
04188 f = ast_udptl_read(p->udptl);
04189 break;
04190 default:
04191 f = &ast_null_frame;
04192 }
04193
04194 if (f && (f->frametype == AST_FRAME_DTMF) &&
04195 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833))
04196 return &ast_null_frame;
04197
04198
04199 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
04200 return f;
04201
04202 if (f && f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
04203 if (!(f->subclass & p->jointcapability)) {
04204 if (option_debug) {
04205 ast_log(LOG_DEBUG, "Bogus frame of format '%s' received from '%s'!\n",
04206 ast_getformatname(f->subclass), p->owner->name);
04207 }
04208 return &ast_null_frame;
04209 }
04210 if (option_debug)
04211 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
04212 p->owner->nativeformats = (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
04213 ast_set_read_format(p->owner, p->owner->readformat);
04214 ast_set_write_format(p->owner, p->owner->writeformat);
04215 }
04216
04217 if (f && (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
04218 f = ast_dsp_process(p->owner, p->vad, f);
04219 if (f && f->frametype == AST_FRAME_DTMF) {
04220 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && f->subclass == 'f') {
04221 if (option_debug)
04222 ast_log(LOG_DEBUG, "Fax CNG detected on %s\n", ast->name);
04223 *faxdetect = 1;
04224 } else if (option_debug) {
04225 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
04226 }
04227 }
04228 }
04229
04230 return f;
04231 }
04232
04233
04234 static struct ast_frame *sip_read(struct ast_channel *ast)
04235 {
04236 struct ast_frame *fr;
04237 struct sip_pvt *p = ast->tech_pvt;
04238 int faxdetected = FALSE;
04239
04240 ast_mutex_lock(&p->lock);
04241 fr = sip_rtp_read(ast, p, &faxdetected);
04242 p->lastrtprx = time(NULL);
04243
04244
04245
04246 if (faxdetected && ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && (p->t38.state == T38_DISABLED) && !(ast_bridged_channel(ast))) {
04247 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
04248 if (!p->pendinginvite) {
04249 if (option_debug > 2)
04250 ast_log(LOG_DEBUG, "Sending reinvite on SIP (%s) for T.38 negotiation.\n",ast->name);
04251 p->t38.state = T38_LOCAL_REINVITE;
04252 transmit_reinvite_with_t38_sdp(p);
04253 if (option_debug > 1)
04254 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, ast->name);
04255 }
04256 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04257 if (option_debug > 2)
04258 ast_log(LOG_DEBUG, "Deferring reinvite on SIP (%s) - it will be re-negotiated for T.38\n", ast->name);
04259 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04260 }
04261 }
04262
04263 ast_mutex_unlock(&p->lock);
04264 return fr;
04265 }
04266
04267
04268
04269 static char *generate_random_string(char *buf, size_t size)
04270 {
04271 long val[4];
04272 int x;
04273
04274 for (x=0; x<4; x++)
04275 val[x] = ast_random();
04276 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
04277
04278 return buf;
04279 }
04280
04281
04282 static void build_callid_pvt(struct sip_pvt *pvt)
04283 {
04284 char buf[33];
04285
04286 const char *host = S_OR(pvt->fromdomain, ast_inet_ntoa(pvt->ourip));
04287
04288 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04289
04290 }
04291
04292
04293 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain)
04294 {
04295 char buf[33];
04296
04297 const char *host = S_OR(fromdomain, ast_inet_ntoa(ourip));
04298
04299 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04300 }
04301
04302
04303 static void make_our_tag(char *tagbuf, size_t len)
04304 {
04305 snprintf(tagbuf, len, "as%08lx", ast_random());
04306 }
04307
04308
04309 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
04310 int useglobal_nat, const int intended_method)
04311 {
04312 struct sip_pvt *p;
04313
04314 if (!(p = ast_calloc(1, sizeof(*p))))
04315 return NULL;
04316
04317 if (ast_string_field_init(p, 512)) {
04318 free(p);
04319 return NULL;
04320 }
04321
04322 ast_mutex_init(&p->lock);
04323
04324 p->method = intended_method;
04325 p->initid = -1;
04326 p->autokillid = -1;
04327 p->subscribed = NONE;
04328 p->stateid = -1;
04329 p->prefs = default_prefs;
04330
04331 if (intended_method != SIP_OPTIONS)
04332 p->timer_t1 = 500;
04333
04334 if (sin) {
04335 p->sa = *sin;
04336 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
04337 p->ourip = __ourip;
04338 } else
04339 p->ourip = __ourip;
04340
04341
04342 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
04343 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
04344
04345 ast_set2_flag(&p->flags[0], !recordhistory, SIP_NO_HISTORY);
04346
04347 p->branch = ast_random();
04348 make_our_tag(p->tag, sizeof(p->tag));
04349 p->ocseq = INITIAL_CSEQ;
04350
04351 if (sip_methods[intended_method].need_rtp) {
04352 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04353
04354 if (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT))
04355 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04356 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT))
04357 p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
04358 if (!p->rtp || (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) && !p->vrtp)) {
04359 ast_log(LOG_WARNING, "Unable to create RTP audio %s session: %s\n",
04360 ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "and video" : "", strerror(errno));
04361 ast_mutex_destroy(&p->lock);
04362 if (p->chanvars) {
04363 ast_variables_destroy(p->chanvars);
04364 p->chanvars = NULL;
04365 }
04366 free(p);
04367 return NULL;
04368 }
04369 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04370 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04371 ast_rtp_settos(p->rtp, global_tos_audio);
04372 ast_rtp_set_rtptimeout(p->rtp, global_rtptimeout);
04373 ast_rtp_set_rtpholdtimeout(p->rtp, global_rtpholdtimeout);
04374 ast_rtp_set_rtpkeepalive(p->rtp, global_rtpkeepalive);
04375 if (p->vrtp) {
04376 ast_rtp_settos(p->vrtp, global_tos_video);
04377 ast_rtp_setdtmf(p->vrtp, 0);
04378 ast_rtp_setdtmfcompensate(p->vrtp, 0);
04379 ast_rtp_set_rtptimeout(p->vrtp, global_rtptimeout);
04380 ast_rtp_set_rtpholdtimeout(p->vrtp, global_rtpholdtimeout);
04381 ast_rtp_set_rtpkeepalive(p->vrtp, global_rtpkeepalive);
04382 }
04383 if (p->udptl)
04384 ast_udptl_settos(p->udptl, global_tos_audio);
04385 p->maxcallbitrate = default_maxcallbitrate;
04386 }
04387
04388 if (useglobal_nat && sin) {
04389
04390 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
04391 p->recv = *sin;
04392 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04393 }
04394
04395 if (p->method != SIP_REGISTER)
04396 ast_string_field_set(p, fromdomain, default_fromdomain);
04397 build_via(p);
04398 if (!callid)
04399 build_callid_pvt(p);
04400 else
04401 ast_string_field_set(p, callid, callid);
04402
04403 ast_string_field_set(p, mohinterpret, default_mohinterpret);
04404 ast_string_field_set(p, mohsuggest, default_mohsuggest);
04405 p->capability = global_capability;
04406 p->allowtransfer = global_allowtransfer;
04407 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
04408 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
04409 p->noncodeccapability |= AST_RTP_DTMF;
04410 if (p->udptl) {
04411 p->t38.capability = global_t38_capability;
04412 if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY)
04413 p->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
04414 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_FEC)
04415 p->t38.capability |= T38FAX_UDP_EC_FEC;
04416 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_NONE)
04417 p->t38.capability |= T38FAX_UDP_EC_NONE;
04418 p->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
04419 p->t38.jointcapability = p->t38.capability;
04420 }
04421 ast_string_field_set(p, context, default_context);
04422
04423
04424 ast_mutex_lock(&iflock);
04425 p->next = iflist;
04426 iflist = p;
04427 ast_mutex_unlock(&iflock);
04428 if (option_debug)
04429 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");
04430 return p;
04431 }
04432
04433
04434
04435 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
04436 {
04437 struct sip_pvt *p = NULL;
04438 char *tag = "";
04439 char totag[128];
04440 char fromtag[128];
04441 const char *callid = get_header(req, "Call-ID");
04442 const char *from = get_header(req, "From");
04443 const char *to = get_header(req, "To");
04444 const char *cseq = get_header(req, "Cseq");
04445
04446
04447
04448 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
04449 ast_strlen_zero(from) || ast_strlen_zero(cseq))
04450 return NULL;
04451
04452 if (pedanticsipchecking) {
04453
04454
04455
04456
04457
04458
04459 if (gettag(req, "To", totag, sizeof(totag)))
04460 ast_set_flag(req, SIP_PKT_WITH_TOTAG);
04461 gettag(req, "From", fromtag, sizeof(fromtag));
04462
04463 tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
04464
04465 if (option_debug > 4 )
04466 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);
04467 }
04468
04469 ast_mutex_lock(&iflock);
04470 for (p = iflist; p; p = p->next) {
04471
04472 int found = FALSE;
04473 if (ast_strlen_zero(p->callid))
04474 continue;
04475 if (req->method == SIP_REGISTER)
04476 found = (!strcmp(p->callid, callid));
04477 else
04478 found = (!strcmp(p->callid, callid) &&
04479 (!pedanticsipchecking || !tag || ast_strlen_zero(p->theirtag) || !strcmp(p->theirtag, tag))) ;
04480
04481 if (option_debug > 4)
04482 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);
04483
04484
04485 if (pedanticsipchecking && found && req->method != SIP_RESPONSE) {
04486 if (p->tag[0] == '\0' && totag[0]) {
04487
04488 found = FALSE;
04489 } else if (totag[0]) {
04490 if (strcmp(totag, p->tag)) {
04491 found = FALSE;
04492 }
04493 }
04494 if (!found && option_debug > 4)
04495 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);
04496 }
04497
04498
04499 if (found) {
04500
04501 ast_mutex_lock(&p->lock);
04502 ast_mutex_unlock(&iflock);
04503 return p;
04504 }
04505 }
04506 ast_mutex_unlock(&iflock);
04507
04508
04509 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
04510 if (intended_method == SIP_REFER) {
04511
04512 transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
04513 } else if (intended_method == SIP_NOTIFY) {
04514
04515
04516 transmit_response_using_temp(callid, sin, 1, intended_method, req, "489 Bad event");
04517 } else {
04518
04519 if ((p = sip_alloc(callid, sin, 1, intended_method))) {
04520
04521 ast_mutex_lock(&p->lock);
04522 } else {
04523
04524
04525
04526
04527
04528
04529
04530
04531 transmit_response_using_temp(callid, sin, 1, intended_method, req, "500 Server internal error");
04532 if (option_debug > 3)
04533 ast_log(LOG_DEBUG, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
04534 }
04535 }
04536 return p;
04537 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
04538
04539 transmit_response_using_temp(callid, sin, 1, intended_method, req, "501 Method Not Implemented");
04540 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
04541
04542
04543
04544 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 Call leg/transaction does not exist");
04545 }
04546
04547
04548
04549 return p;
04550 }
04551
04552
04553 static int sip_register(char *value, int lineno)
04554 {
04555 struct sip_registry *reg;
04556 int portnum = 0;
04557 char username[256] = "";
04558 char *hostname=NULL, *secret=NULL, *authuser=NULL;
04559 char *porta=NULL;
04560 char *contact=NULL;
04561
04562 if (!value)
04563 return -1;
04564 ast_copy_string(username, value, sizeof(username));
04565
04566 hostname = strrchr(username, '@');
04567 if (hostname)
04568 *hostname++ = '\0';
04569 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
04570 ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
04571 return -1;
04572 }
04573
04574 secret = strchr(username, ':');
04575 if (secret) {
04576 *secret++ = '\0';
04577 authuser = strchr(secret, ':');
04578 if (authuser)
04579 *authuser++ = '\0';
04580 }
04581
04582 contact = strchr(hostname, '/');
04583 if (contact)
04584 *contact++ = '\0';
04585 if (ast_strlen_zero(contact))
04586 contact = "s";
04587 porta = strchr(hostname, ':');
04588 if (porta) {
04589 *porta++ = '\0';
04590 portnum = atoi(porta);
04591 if (portnum == 0) {
04592 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
04593 return -1;
04594 }
04595 }
04596 if (!(reg = ast_calloc(1, sizeof(*reg)))) {
04597 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
04598 return -1;
04599 }
04600
04601 if (ast_string_field_init(reg, 256)) {
04602 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry strings\n");
04603 free(reg);
04604 return -1;
04605 }
04606
04607 regobjs++;
04608 ASTOBJ_INIT(reg);
04609 ast_string_field_set(reg, contact, contact);
04610 if (!ast_strlen_zero(username))
04611 ast_string_field_set(reg, username, username);
04612 if (hostname)
04613 ast_string_field_set(reg, hostname, hostname);
04614 if (authuser)
04615 ast_string_field_set(reg, authuser, authuser);
04616 if (secret)
04617 ast_string_field_set(reg, secret, secret);
04618 reg->expire = -1;
04619 reg->timeout = -1;
04620 reg->refresh = default_expiry;
04621 reg->portno = portnum;
04622 reg->callid_valid = FALSE;
04623 reg->ocseq = INITIAL_CSEQ;
04624 ASTOBJ_CONTAINER_LINK(®l, reg);
04625 ASTOBJ_UNREF(reg,sip_registry_destroy);
04626 return 0;
04627 }
04628
04629
04630
04631 static int lws2sws(char *msgbuf, int len)
04632 {
04633 int h = 0, t = 0;
04634 int lws = 0;
04635
04636 for (; h < len;) {
04637
04638 if (msgbuf[h] == '\r') {
04639 h++;
04640 continue;
04641 }
04642
04643 if (msgbuf[h] == '\n') {
04644
04645 if (h + 1 == len)
04646 break;
04647
04648 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
04649
04650 h++;
04651 continue;
04652 }
04653
04654 msgbuf[t++] = msgbuf[h++];
04655 lws = 0;
04656 continue;
04657 }
04658 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
04659 if (lws) {
04660 h++;
04661 continue;
04662 }
04663 msgbuf[t++] = msgbuf[h++];
04664 lws = 1;
04665 continue;
04666 }
04667 msgbuf[t++] = msgbuf[h++];
04668 if (lws)
04669 lws = 0;
04670 }
04671 msgbuf[t] = '\0';
04672 return t;
04673 }
04674
04675
04676
04677
04678 static void parse_request(struct sip_request *req)
04679 {
04680
04681 char *c;
04682 int f = 0;
04683
04684 c = req->data;
04685
04686
04687 req->header[f] = c;
04688 while(*c) {
04689 if (*c == '\n') {
04690
04691 *c = 0;
04692
04693 if (sipdebug && option_debug > 3)
04694 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
04695 if (ast_strlen_zero(req->header[f])) {
04696
04697 c++;
04698 break;
04699 }
04700 if (f >= SIP_MAX_HEADERS - 1) {
04701 ast_log(LOG_WARNING, "Too many SIP headers. Ignoring.\n");
04702 } else
04703 f++;
04704 req->header[f] = c + 1;
04705 } else if (*c == '\r') {
04706
04707 *c = 0;
04708 }
04709 c++;
04710 }
04711
04712 if (!ast_strlen_zero(req->header[f])) {
04713 if (sipdebug && option_debug > 3)
04714 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
04715 f++;
04716 }
04717 req->headers = f;
04718
04719 f = 0;
04720 req->line[f] = c;
04721 while(*c) {
04722 if (*c == '\n') {
04723
04724 *c = 0;
04725 if (sipdebug && option_debug > 3)
04726 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
04727 if (f >= SIP_MAX_LINES - 1) {
04728 ast_log(LOG_WARNING, "Too many SDP lines. Ignoring.\n");
04729 } else
04730 f++;
04731 req->line[f] = c + 1;
04732 } else if (*c == '\r') {
04733
04734 *c = 0;
04735 }
04736 c++;
04737 }
04738
04739 if (!ast_strlen_zero(req->line[f]))
04740 f++;
04741 req->lines = f;
04742 if (*c)
04743 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
04744
04745 determine_firstline_parts(req);
04746 }
04747
04748
04749
04750
04751
04752
04753
04754
04755
04756 static int find_sdp(struct sip_request *req)
04757 {
04758 const char *content_type;
04759 const char *search;
04760 char *boundary;
04761 unsigned int x;
04762 int boundaryisquoted = FALSE;
04763
04764 content_type = get_header(req, "Content-Type");
04765
04766
04767 if (!strcasecmp(content_type, "application/sdp")) {
04768 req->sdp_start = 0;
04769 req->sdp_end = req->lines;
04770 return req->lines ? 1 : 0;
04771 }
04772
04773
04774 if (strncasecmp(content_type, "multipart/mixed", 15))
04775 return 0;
04776
04777
04778 if (!(search = strcasestr(content_type, ";boundary=")))
04779 return 0;
04780
04781 search += 10;
04782 if (ast_strlen_zero(search))
04783 return 0;
04784
04785
04786 if (*search == '\"') {
04787 search++;
04788 boundaryisquoted = TRUE;
04789 }
04790
04791
04792
04793 boundary = ast_strdupa(search - 2);
04794 boundary[0] = boundary[1] = '-';
04795
04796
04797 if (boundaryisquoted)
04798 boundary[strlen(boundary) - 1] = '\0';
04799
04800
04801
04802
04803 for (x = 0; x < (req->lines - 2); x++) {
04804 if (!strncasecmp(req->line[x], boundary, strlen(boundary)) &&
04805 !strcasecmp(req->line[x + 1], "Content-Type: application/sdp")) {
04806 x += 2;
04807 req->sdp_start = x;
04808
04809
04810 for ( ; x < req->lines; x++) {
04811 if (!strncasecmp(req->line[x], boundary, strlen(boundary)))
04812 break;
04813 }
04814 req->sdp_end = x;
04815 return 1;
04816 }
04817 }
04818
04819 return 0;
04820 }
04821
04822
04823 static void change_hold_state(struct sip_pvt *dialog, struct sip_request *req, int holdstate, int sendonly)
04824 {
04825 if (global_notifyhold)
04826 sip_peer_hold(dialog, holdstate);
04827 if (global_callevents)
04828 manager_event(EVENT_FLAG_CALL, holdstate ? "Hold" : "Unhold",
04829 "Channel: %s\r\n"
04830 "Uniqueid: %s\r\n",
04831 dialog->owner->name,
04832 dialog->owner->uniqueid);
04833 append_history(dialog, holdstate ? "Hold" : "Unhold", "%s", req->data);
04834 if (!holdstate) {
04835 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD);
04836 return;
04837 }
04838
04839
04840 if (sendonly == 1)
04841 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
04842 else if (sendonly == 2)
04843 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
04844 else
04845 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
04846 return;
04847 }
04848
04849
04850
04851
04852
04853
04854 static int process_sdp(struct sip_pvt *p, struct sip_request *req)
04855 {
04856 const char *m;
04857 const char *c;
04858 const char *a;
04859 char host[258];
04860 int len = -1;
04861 int portno = -1;
04862 int vportno = -1;
04863 int udptlportno = -1;
04864 int peert38capability = 0;
04865 char s[256];
04866 int old = 0;
04867
04868
04869 int peercapability = 0, peernoncodeccapability = 0;
04870 int vpeercapability = 0, vpeernoncodeccapability = 0;
04871 struct sockaddr_in sin;
04872 struct sockaddr_in vsin;
04873
04874 const char *codecs;
04875 struct hostent *hp;
04876 struct hostent *vhp = NULL;
04877 struct ast_hostent audiohp;
04878 struct ast_hostent videohp;
04879 int codec;
04880 int destiterator = 0;
04881 int iterator;
04882 int sendonly = -1;
04883 int numberofports;
04884 struct ast_rtp *newaudiortp, *newvideortp;
04885 int newjointcapability;
04886 int newpeercapability;
04887 int newnoncodeccapability;
04888 int numberofmediastreams = 0;
04889 int debug = sip_debug_test_pvt(p);
04890
04891 int found_rtpmap_codecs[32];
04892 int last_rtpmap_codec=0;
04893
04894 if (!p->rtp) {
04895 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
04896 return -1;
04897 }
04898
04899
04900 newaudiortp = alloca(ast_rtp_alloc_size());
04901 memset(newaudiortp, 0, ast_rtp_alloc_size());
04902 ast_rtp_new_init(newaudiortp);
04903 ast_rtp_pt_clear(newaudiortp);
04904
04905 newvideortp = alloca(ast_rtp_alloc_size());
04906 memset(newvideortp, 0, ast_rtp_alloc_size());
04907 ast_rtp_new_init(newvideortp);
04908 ast_rtp_pt_clear(newvideortp);
04909
04910
04911 p->lastrtprx = p->lastrtptx = time(NULL);
04912
04913
04914
04915 m = get_sdp(req, "m");
04916 destiterator = req->sdp_start;
04917 c = get_sdp_iterate(&destiterator, req, "c");
04918 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
04919 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
04920 return -1;
04921 }
04922
04923
04924 if (sscanf(c, "IN IP4 %256s", host) != 1) {
04925 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
04926 return -1;
04927 }
04928
04929
04930 hp = ast_gethostbyname(host, &audiohp);
04931 if (!hp) {
04932 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
04933 return -1;
04934 }
04935 vhp = hp;
04936
04937 iterator = req->sdp_start;
04938 ast_set_flag(&p->flags[0], SIP_NOVIDEO);
04939
04940
04941
04942 while ((m = get_sdp_iterate(&iterator, req, "m"))[0] != '\0') {
04943 int x;
04944 int audio = FALSE;
04945
04946 numberofports = 1;
04947 if ((sscanf(m, "audio %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2) ||
04948 (sscanf(m, "audio %d RTP/AVP %n", &x, &len) == 1)) {
04949 audio = TRUE;
04950 numberofmediastreams++;
04951
04952 portno = x;
04953
04954 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
04955 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
04956 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
04957 return -1;
04958 }
04959 if (debug)
04960 ast_verbose("Found RTP audio format %d\n", codec);
04961 ast_rtp_set_m_type(newaudiortp, codec);
04962 }
04963 } else if ((sscanf(m, "video %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2) ||
04964 (sscanf(m, "video %d RTP/AVP %n", &x, &len) == 1)) {
04965
04966 ast_clear_flag(&p->flags[0], SIP_NOVIDEO);
04967 numberofmediastreams++;
04968 vportno = x;
04969
04970 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
04971 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
04972 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
04973 return -1;
04974 }
04975 if (debug)
04976 ast_verbose("Found RTP video format %d\n", codec);
04977 ast_rtp_set_m_type(newvideortp, codec);
04978 }
04979 } else if (p->udptl && ( (sscanf(m, "image %d udptl t38%n", &x, &len) == 1) ||
04980 (sscanf(m, "image %d UDPTL t38%n", &x, &len) == 1) )) {
04981 if (debug)
04982 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
04983 udptlportno = x;
04984 numberofmediastreams++;
04985
04986 if (p->owner && p->lastinvite) {
04987 p->t38.state = T38_PEER_REINVITE;
04988 if (option_debug > 1)
04989 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>" );
04990 } else {
04991 p->t38.state = T38_PEER_DIRECT;
04992 if (option_debug > 1)
04993 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
04994 }
04995 } else
04996 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
04997 if (numberofports > 1)
04998 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
04999
05000
05001
05002 c = get_sdp_iterate(&destiterator, req, "c");
05003 if (!ast_strlen_zero(c)) {
05004 if (sscanf(c, "IN IP4 %256s", host) != 1) {
05005 ast_log(LOG_WARNING, "Invalid secondary host in c= line, '%s'\n", c);
05006 } else {
05007
05008 if (audio) {
05009 if ( !(hp = ast_gethostbyname(host, &audiohp))) {
05010 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in secondary c= line, '%s'\n", c);
05011 return -2;
05012 }
05013 } else if (!(vhp = ast_gethostbyname(host, &videohp))) {
05014 ast_log(LOG_WARNING, "Unable to lookup RTP video host in secondary c= line, '%s'\n", c);
05015 return -2;
05016 }
05017 }
05018
05019 }
05020 }
05021 if (portno == -1 && vportno == -1 && udptlportno == -1)
05022
05023
05024 return -2;
05025
05026 if (numberofmediastreams > 2)
05027
05028 return -3;
05029
05030
05031 sin.sin_family = AF_INET;
05032 vsin.sin_family = AF_INET;
05033 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
05034 if (vhp)
05035 memcpy(&vsin.sin_addr, vhp->h_addr, sizeof(vsin.sin_addr));
05036
05037
05038 if (p->udptl) {
05039 if (udptlportno > 0) {
05040 sin.sin_port = htons(udptlportno);
05041 ast_udptl_set_peer(p->udptl, &sin);
05042 if (debug)
05043 ast_log(LOG_DEBUG,"Peer T.38 UDPTL is at port %s:%d\n",ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05044 } else {
05045 ast_udptl_stop(p->udptl);
05046 if (debug)
05047 ast_log(LOG_DEBUG, "Peer doesn't provide T.38 UDPTL\n");
05048 }
05049 }
05050
05051
05052 if (p->rtp) {
05053 if (portno > 0) {
05054 sin.sin_port = htons(portno);
05055 ast_rtp_set_peer(p->rtp, &sin);
05056 if (debug)
05057 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05058 } else {
05059 if (udptlportno > 0) {
05060 if (debug)
05061 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session. Callid %s\n", p->callid);
05062 } else {
05063 ast_rtp_stop(p->rtp);
05064 if (debug)
05065 ast_verbose("Peer doesn't provide audio. Callid %s\n", p->callid);
05066 }
05067 }
05068 }
05069
05070 if (vportno != -1)
05071 vsin.sin_port = htons(vportno);
05072
05073
05074
05075
05076
05077 iterator = req->sdp_start;
05078 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
05079 char* mimeSubtype = ast_strdupa(a);
05080 if (option_debug > 1) {
05081 int breakout = FALSE;
05082
05083
05084 if (!strncasecmp(a, "rtcp:", (size_t) 5)) {
05085 if (debug)
05086 ast_verbose("Got unsupported a:rtcp in SDP offer \n");
05087 breakout = TRUE;
05088 } else if (!strncasecmp(a, "fmtp:", (size_t) 5)) {
05089
05090
05091
05092
05093 if (debug)
05094 ast_verbose("Got unsupported a:fmtp in SDP offer \n");
05095 breakout = TRUE;
05096 } else if (!strncasecmp(a, "framerate:", (size_t) 10)) {
05097
05098 if (debug)
05099 ast_verbose("Got unsupported a:framerate in SDP offer \n");
05100 breakout = TRUE;
05101 } else if (!strncasecmp(a, "maxprate:", (size_t) 9)) {
05102
05103 if (debug)
05104 ast_verbose("Got unsupported a:maxprate in SDP offer \n");
05105 breakout = TRUE;
05106 } else if (!strncasecmp(a, "crypto:", (size_t) 7)) {
05107
05108 if (debug)
05109 ast_verbose("Got unsupported a:crypto in SDP offer \n");
05110 breakout = TRUE;
05111 }
05112 if (breakout)
05113 continue;
05114 }
05115 if (!strcasecmp(a, "sendonly")) {
05116 if (sendonly == -1)
05117 sendonly = 1;
05118 continue;
05119 } else if (!strcasecmp(a, "inactive")) {
05120 if (sendonly == -1)
05121 sendonly = 2;
05122 continue;
05123 } else if (!strcasecmp(a, "sendrecv")) {
05124 if (sendonly == -1)
05125 sendonly = 0;
05126 continue;
05127 } else if (strlen(a) > 5 && !strncasecmp(a, "ptime", 5)) {
05128 char *tmp = strrchr(a, ':');
05129 long int framing = 0;
05130 if (tmp) {
05131 tmp++;
05132 framing = strtol(tmp, NULL, 10);
05133 if (framing == LONG_MIN || framing == LONG_MAX) {
05134 framing = 0;
05135 if (option_debug)
05136 ast_log(LOG_DEBUG, "Can't read framing from SDP: %s\n", a);
05137 }
05138 }
05139 if (framing && last_rtpmap_codec) {
05140 if (p->autoframing) {
05141 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
05142 int codec_n;
05143 int format = 0;
05144 for (codec_n = 0; codec_n < last_rtpmap_codec; codec_n++) {
05145 format = ast_rtp_codec_getformat(found_rtpmap_codecs[codec_n]);
05146 if (!format)
05147 continue;
05148 if (option_debug)
05149 ast_log(LOG_DEBUG, "Setting framing for %d to %ld\n", format, framing);
05150 ast_codec_pref_setsize(pref, format, framing);
05151 }
05152 ast_rtp_codec_setpref(p->rtp, pref);
05153 }
05154 }
05155 memset(&found_rtpmap_codecs, 0, sizeof(found_rtpmap_codecs));
05156 last_rtpmap_codec = 0;
05157 continue;
05158 } else if (sscanf(a, "rtpmap: %u %[^/]/", &codec, mimeSubtype) == 2) {
05159
05160 if (debug)
05161 ast_verbose("Found description format %s for ID %d\n", mimeSubtype, codec);
05162 found_rtpmap_codecs[last_rtpmap_codec] = codec;
05163 last_rtpmap_codec++;
05164
05165
05166 ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
05167 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0);
05168 if (p->vrtp)
05169 ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0);
05170 }
05171 }
05172
05173 if (udptlportno != -1) {
05174 int found = 0, x;
05175
05176 old = 0;
05177
05178
05179 iterator = req->sdp_start;
05180 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
05181 if ((sscanf(a, "T38FaxMaxBuffer:%d", &x) == 1)) {
05182 found = 1;
05183 if (option_debug > 2)
05184 ast_log(LOG_DEBUG, "MaxBufferSize:%d\n",x);
05185 } else if ((sscanf(a, "T38MaxBitRate:%d", &x) == 1)) {
05186 found = 1;
05187 if (option_debug > 2)
05188 ast_log(LOG_DEBUG,"T38MaxBitRate: %d\n",x);
05189 switch (x) {
05190 case 14400:
05191 peert38capability |= T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05192 break;
05193 case 12000:
05194 peert38capability |= T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05195 break;
05196 case 9600:
05197 peert38capability |= T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05198 break;
05199 case 7200:
05200 peert38capability |= T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05201 break;
05202 case 4800:
05203 peert38capability |= T38FAX_RATE_4800 | T38FAX_RATE_2400;
05204 break;
05205 case 2400:
05206 peert38capability |= T38FAX_RATE_2400;
05207 break;
05208 }
05209 } else if ((sscanf(a, "T38FaxVersion:%d", &x) == 1)) {
05210 found = 1;
05211 if (option_debug > 2)
05212 ast_log(LOG_DEBUG, "FaxVersion: %d\n",x);
05213 if (x == 0)
05214 peert38capability |= T38FAX_VERSION_0;
05215 else if (x == 1)
05216 peert38capability |= T38FAX_VERSION_1;
05217 } else if ((sscanf(a, "T38FaxMaxDatagram:%d", &x) == 1)) {
05218 found = 1;
05219 if (option_debug > 2)
05220 ast_log(LOG_DEBUG, "FaxMaxDatagram: %d\n",x);
05221 ast_udptl_set_far_max_datagram(p->udptl, x);
05222 ast_udptl_set_local_max_datagram(p->udptl, x);
05223 } else if ((sscanf(a, "T38FaxFillBitRemoval:%d", &x) == 1)) {
05224 found = 1;
05225 if (option_debug > 2)
05226 ast_log(LOG_DEBUG, "FillBitRemoval: %d\n",x);
05227 if (x == 1)
05228 peert38capability |= T38FAX_FILL_BIT_REMOVAL;
05229 } else if ((sscanf(a, "T38FaxTranscodingMMR:%d", &x) == 1)) {
05230 found = 1;
05231 if (option_debug > 2)
05232 ast_log(LOG_DEBUG, "Transcoding MMR: %d\n",x);
05233 if (x == 1)
05234 peert38capability |= T38FAX_TRANSCODING_MMR;
05235 }
05236 if ((sscanf(a, "T38FaxTranscodingJBIG:%d", &x) == 1)) {
05237 found = 1;
05238 if (option_debug > 2)
05239 ast_log(LOG_DEBUG, "Transcoding JBIG: %d\n",x);
05240 if (x == 1)
05241 peert38capability |= T38FAX_TRANSCODING_JBIG;
05242 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
05243 found = 1;
05244 if (option_debug > 2)
05245 ast_log(LOG_DEBUG, "RateManagement: %s\n", s);
05246 if (!strcasecmp(s, "localTCF"))
05247 peert38capability |= T38FAX_RATE_MANAGEMENT_LOCAL_TCF;
05248 else if (!strcasecmp(s, "transferredTCF"))
05249 peert38capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
05250 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
05251 found = 1;
05252 if (option_debug > 2)
05253 ast_log(LOG_DEBUG, "UDP EC: %s\n", s);
05254 if (!strcasecmp(s, "t38UDPRedundancy")) {
05255 peert38capability |= T38FAX_UDP_EC_REDUNDANCY;
05256 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
05257 } else if (!strcasecmp(s, "t38UDPFEC")) {
05258 peert38capability |= T38FAX_UDP_EC_FEC;
05259 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
05260 } else {
05261 peert38capability |= T38FAX_UDP_EC_NONE;
05262 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
05263 }
05264 }
05265 }
05266 if (found) {
05267 p->t38.peercapability = peert38capability;
05268 p->t38.jointcapability = (peert38capability & 255);
05269 peert38capability &= (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400);
05270 p->t38.jointcapability |= (peert38capability & p->t38.capability);
05271 }
05272 if (debug)
05273 ast_log(LOG_DEBUG, "Our T38 capability = (%d), peer T38 capability (%d), joint T38 capability (%d)\n",
05274 p->t38.capability,
05275 p->t38.peercapability,
05276 p->t38.jointcapability);
05277 } else {
05278 p->t38.state = T38_DISABLED;
05279 if (option_debug > 2)
05280 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05281 }
05282
05283
05284 ast_rtp_get_current_formats(newaudiortp, &peercapability, &peernoncodeccapability);
05285 ast_rtp_get_current_formats(newvideortp, &vpeercapability, &vpeernoncodeccapability);
05286
05287 newjointcapability = p->capability & (peercapability | vpeercapability);
05288 newpeercapability = (peercapability | vpeercapability);
05289 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
05290
05291
05292 if (debug) {
05293
05294 char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ], s4[BUFSIZ];
05295
05296 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s, combined - %s\n",
05297 ast_getformatname_multiple(s1, BUFSIZ, p->capability),
05298 ast_getformatname_multiple(s2, BUFSIZ, newpeercapability),
05299 ast_getformatname_multiple(s3, BUFSIZ, vpeercapability),
05300 ast_getformatname_multiple(s4, BUFSIZ, newjointcapability));
05301
05302 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
05303 ast_rtp_lookup_mime_multiple(s1, BUFSIZ, p->noncodeccapability, 0, 0),
05304 ast_rtp_lookup_mime_multiple(s2, BUFSIZ, peernoncodeccapability, 0, 0),
05305 ast_rtp_lookup_mime_multiple(s3, BUFSIZ, newnoncodeccapability, 0, 0));
05306 }
05307 if (!newjointcapability) {
05308
05309 if (!p->t38.jointcapability) {
05310 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
05311
05312 return -1;
05313 } else {
05314 if (option_debug > 2)
05315 ast_log(LOG_DEBUG, "Have T.38 but no audio codecs, accepting offer anyway\n");
05316 return 0;
05317 }
05318 }
05319
05320
05321
05322 p->jointcapability = newjointcapability;
05323 p->peercapability = newpeercapability;
05324 p->jointnoncodeccapability = newnoncodeccapability;
05325
05326 ast_rtp_pt_copy(p->rtp, newaudiortp);
05327 if (p->vrtp)
05328 ast_rtp_pt_copy(p->vrtp, newvideortp);
05329
05330 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
05331 ast_clear_flag(&p->flags[0], SIP_DTMF);
05332 if (newnoncodeccapability & AST_RTP_DTMF) {
05333
05334 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
05335
05336 ast_rtp_setdtmf(p->rtp, 1);
05337 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
05338 } else {
05339 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
05340 }
05341 }
05342
05343
05344 if (p->rtp && sin.sin_port) {
05345 ast_rtp_set_peer(p->rtp, &sin);
05346 if (debug)
05347 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05348 }
05349
05350
05351 if (p->vrtp && vsin.sin_port) {
05352 ast_rtp_set_peer(p->vrtp, &vsin);
05353 if (debug)
05354 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
05355 }
05356
05357
05358 if (option_debug > 1) {
05359 char buf[BUFSIZ];
05360 ast_log(LOG_DEBUG, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, BUFSIZ, p->jointcapability));
05361 }
05362
05363 if (!p->owner)
05364 return 0;
05365
05366 if (option_debug > 3)
05367 ast_log(LOG_DEBUG, "We have an owner, now see if we need to change this call\n");
05368
05369 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
05370 if (debug) {
05371 char s1[BUFSIZ], s2[BUFSIZ];
05372 ast_log(LOG_DEBUG, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
05373 ast_getformatname_multiple(s1, BUFSIZ, p->jointcapability),
05374 ast_getformatname_multiple(s2, BUFSIZ, p->owner->nativeformats));
05375 }
05376 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability);
05377 ast_set_read_format(p->owner, p->owner->readformat);
05378 ast_set_write_format(p->owner, p->owner->writeformat);
05379 }
05380
05381 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1)) {
05382 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
05383
05384 ast_queue_frame(p->owner, &ast_null_frame);
05385 } else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1)) {
05386 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
05387 S_OR(p->mohsuggest, NULL),
05388 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
05389 if (sendonly)
05390 ast_rtp_stop(p->rtp);
05391
05392
05393 ast_queue_frame(p->owner, &ast_null_frame);
05394 }
05395
05396
05397 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && sin.sin_addr.s_addr && (!sendonly || sendonly == -1))
05398 change_hold_state(p, req, FALSE, sendonly);
05399 else if (!sin.sin_addr.s_addr || (sendonly && sendonly != -1))
05400 change_hold_state(p, req, TRUE, sendonly);
05401 return 0;
05402 }
05403
05404
05405
05406 static int add_header(struct sip_request *req, const char *var, const char *value)
05407 {
05408 int maxlen = sizeof(req->data) - 4 - req->len;
05409
05410 if (req->headers == SIP_MAX_HEADERS) {
05411 ast_log(LOG_WARNING, "Out of SIP header space\n");
05412 return -1;
05413 }
05414
05415 if (req->lines) {
05416 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
05417 return -1;
05418 }
05419
05420 if (maxlen <= 0) {
05421 ast_log(LOG_WARNING, "Out of space, can't add anymore (%s:%s)\n", var, value);
05422 return -1;
05423 }
05424
05425 req->header[req->headers] = req->data + req->len;
05426
05427 if (compactheaders)
05428 var = find_alias(var, var);
05429
05430 snprintf(req->header[req->headers], maxlen, "%s: %s\r\n", var, value);
05431 req->len += strlen(req->header[req->headers]);
05432 req->headers++;
05433 if (req->headers < SIP_MAX_HEADERS)
05434 req->headers++;
05435 else
05436 ast_log(LOG_WARNING, "Out of SIP header space... Will generate broken SIP message\n");
05437
05438 return 0;
05439 }
05440
05441
05442 static int add_header_contentLength(struct sip_request *req, int len)
05443 {
05444 char clen[10];
05445
05446 snprintf(clen, sizeof(clen), "%d", len);
05447 return add_header(req, "Content-Length", clen);
05448 }
05449
05450
05451 static int add_line(struct sip_request *req, const char *line)
05452 {
05453 if (req->lines == SIP_MAX_LINES) {
05454 ast_log(LOG_WARNING, "Out of SIP line space\n");
05455 return -1;
05456 }
05457 if (!req->lines) {
05458
05459 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
05460 req->len += strlen(req->data + req->len);
05461 }
05462 if (req->len >= sizeof(req->data) - 4) {
05463 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
05464 return -1;
05465 }
05466 req->line[req->lines] = req->data + req->len;
05467 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
05468 req->len += strlen(req->line[req->lines]);
05469 req->lines++;
05470 return 0;
05471 }
05472
05473
05474 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
05475 {
05476 const char *tmp = get_header(orig, field);
05477
05478 if (!ast_strlen_zero(tmp))
05479 return add_header(req, field, tmp);
05480 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
05481 return -1;
05482 }
05483
05484
05485 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
05486 {
05487 int start = 0;
05488 int copied = 0;
05489 for (;;) {
05490 const char *tmp = __get_header(orig, field, &start);
05491
05492 if (ast_strlen_zero(tmp))
05493 break;
05494
05495 add_header(req, field, tmp);
05496 copied++;
05497 }
05498 return copied ? 0 : -1;
05499 }
05500
05501
05502
05503
05504
05505
05506
05507 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
05508 {
05509 int copied = 0;
05510 int start = 0;
05511
05512 for (;;) {
05513 char new[256];
05514 const char *oh = __get_header(orig, field, &start);
05515
05516 if (ast_strlen_zero(oh))
05517 break;
05518
05519 if (!copied) {
05520 char leftmost[256], *others, *rport;
05521
05522
05523 ast_copy_string(leftmost, oh, sizeof(leftmost));
05524 others = strchr(leftmost, ',');
05525 if (others)
05526 *others++ = '\0';
05527
05528
05529 rport = strstr(leftmost, ";rport");
05530 if (rport && *(rport+6) == '=')
05531 rport = NULL;
05532
05533
05534 if (rport && ((ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_ALWAYS) || (ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_RFC3581))) {
05535
05536 char *end;
05537
05538 rport = strstr(leftmost, ";rport");
05539
05540 if (rport) {
05541 end = strchr(rport + 1, ';');
05542 if (end)
05543 memmove(rport, end, strlen(end) + 1);
05544 else
05545 *rport = '\0';
05546 }
05547
05548
05549 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
05550 leftmost, ast_inet_ntoa(p->recv.sin_addr),
05551 ntohs(p->recv.sin_port),
05552 others ? "," : "", others ? others : "");
05553 } else {
05554
05555 snprintf(new, sizeof(new), "%s;received=%s%s%s",
05556 leftmost, ast_inet_ntoa(p->recv.sin_addr),
05557 others ? "," : "", others ? others : "");
05558 }
05559 oh = new;
05560 }
05561 add_header(req, field, oh);
05562 copied++;
05563 }
05564 if (!copied) {
05565 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
05566 return -1;
05567 }
05568 return 0;
05569 }
05570
05571
05572 static void add_route(struct sip_request *req, struct sip_route *route)
05573 {
05574 char r[BUFSIZ*2], *p;
05575 int n, rem = sizeof(r);
05576
05577 if (!route)
05578 return;
05579
05580 p = r;
05581 for (;route ; route = route->next) {
05582 n = strlen(route->hop);
05583 if (rem < n+3)
05584 break;
05585 if (p != r) {
05586 *p++ = ',';
05587 --rem;
05588 }
05589 *p++ = '<';
05590 ast_copy_string(p, route->hop, rem);
05591 p += n;
05592 *p++ = '>';
05593 rem -= (n+2);
05594 }
05595 *p = '\0';
05596 add_header(req, "Route", r);
05597 }
05598
05599
05600 static void set_destination(struct sip_pvt *p, char *uri)
05601 {
05602 char *h, *maddr, hostname[256];
05603 int port, hn;
05604 struct hostent *hp;
05605 struct ast_hostent ahp;
05606 int debug=sip_debug_test_pvt(p);
05607
05608
05609
05610
05611 if (debug)
05612 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
05613
05614
05615 h = strchr(uri, '@');
05616 if (h)
05617 ++h;
05618 else {
05619 h = uri;
05620 if (strncasecmp(h, "sip:", 4) == 0)
05621 h += 4;
05622 else if (strncasecmp(h, "sips:", 5) == 0)
05623 h += 5;
05624 }
05625 hn = strcspn(h, ":;>") + 1;
05626 if (hn > sizeof(hostname))
05627 hn = sizeof(hostname);
05628 ast_copy_string(hostname, h, hn);
05629
05630 h += hn - 1;
05631
05632
05633 if (*h == ':') {
05634
05635 ++h;
05636 port = strtol(h, &h, 10);
05637 }
05638 else
05639 port = STANDARD_SIP_PORT;
05640
05641
05642 maddr = strstr(h, "maddr=");
05643 if (maddr) {
05644 maddr += 6;
05645 hn = strspn(maddr, "0123456789.") + 1;
05646 if (hn > sizeof(hostname))
05647 hn = sizeof(hostname);
05648 ast_copy_string(hostname, maddr, hn);
05649 }
05650
05651 hp = ast_gethostbyname(hostname, &ahp);
05652 if (hp == NULL) {
05653 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
05654 return;
05655 }
05656 p->sa.sin_family = AF_INET;
05657 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
05658 p->sa.sin_port = htons(port);
05659 if (debug)
05660 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(p->sa.sin_addr), port);
05661 }
05662
05663
05664 static int init_resp(struct sip_request *resp, const char *msg)
05665 {
05666
05667 memset(resp, 0, sizeof(*resp));
05668 resp->method = SIP_RESPONSE;
05669 resp->header[0] = resp->data;
05670 snprintf(resp->header[0], sizeof(resp->data), "SIP/2.0 %s\r\n", msg);
05671 resp->len = strlen(resp->header[0]);
05672 resp->headers++;
05673 return 0;
05674 }
05675
05676
05677 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
05678 {
05679
05680 memset(req, 0, sizeof(*req));
05681 req->method = sipmethod;
05682 req->header[0] = req->data;
05683 snprintf(req->header[0], sizeof(req->data), "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
05684 req->len = strlen(req->header[0]);
05685 req->headers++;
05686 return 0;
05687 }
05688
05689
05690
05691 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
05692 {
05693 char newto[256];
05694 const char *ot;
05695
05696 init_resp(resp, msg);
05697 copy_via_headers(p, resp, req, "Via");
05698 if (msg[0] == '2')
05699 copy_all_header(resp, req, "Record-Route");
05700 copy_header(resp, req, "From");
05701 ot = get_header(req, "To");
05702 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
05703
05704
05705 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
05706 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
05707 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
05708 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
05709 else
05710 ast_copy_string(newto, ot, sizeof(newto));
05711 ot = newto;
05712 }
05713 add_header(resp, "To", ot);
05714 copy_header(resp, req, "Call-ID");
05715 copy_header(resp, req, "CSeq");
05716 if (!ast_strlen_zero(global_useragent))
05717 add_header(resp, "User-Agent", global_useragent);
05718 add_header(resp, "Allow", ALLOWED_METHODS);
05719 add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
05720 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
05721
05722
05723 char tmp[256];
05724
05725 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
05726 add_header(resp, "Expires", tmp);
05727 if (p->expiry) {
05728 char contact[BUFSIZ];
05729 snprintf(contact, sizeof(contact), "%s;expires=%d", p->our_contact, p->expiry);
05730 add_header(resp, "Contact", contact);
05731 }
05732 } else if (msg[0] != '4' && p->our_contact[0]) {
05733 add_header(resp, "Contact", p->our_contact);
05734 }
05735 return 0;
05736 }
05737
05738
05739 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
05740 {
05741 struct sip_request *orig = &p->initreq;
05742 char stripped[80];
05743 char tmp[80];
05744 char newto[256];
05745 const char *c;
05746 const char *ot, *of;
05747 int is_strict = FALSE;
05748
05749 memset(req, 0, sizeof(struct sip_request));
05750
05751 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
05752
05753 if (!seqno) {
05754 p->ocseq++;
05755 seqno = p->ocseq;
05756 }
05757
05758 if (newbranch) {
05759 p->branch ^= ast_random();
05760 build_via(p);
05761 }
05762
05763
05764 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop,";lr") == NULL) {
05765 is_strict = TRUE;
05766 if (sipdebug)
05767 ast_log(LOG_DEBUG, "Strict routing enforced for session %s\n", p->callid);
05768 }
05769
05770 if (sipmethod == SIP_CANCEL)
05771 c = p->initreq.rlPart2;
05772 else if (sipmethod == SIP_ACK) {
05773
05774
05775 if (!ast_strlen_zero(p->okcontacturi))
05776 c = is_strict ? p->route->hop : p->okcontacturi;
05777 else
05778 c = p->initreq.rlPart2;
05779 } else if (!ast_strlen_zero(p->okcontacturi))
05780 c = is_strict ? p->route->hop : p->okcontacturi;
05781 else if (!ast_strlen_zero(p->uri))
05782 c = p->uri;
05783 else {
05784 char *n;
05785
05786 ast_copy_string(stripped, get_header(orig, (ast_test_flag(&p->flags[0], SIP_OUTGOING)) ? "To" : "From"),
05787 sizeof(stripped));
05788 n = get_in_brackets(stripped);
05789 c = strsep(&n, ";");
05790 }
05791 init_req(req, sipmethod, c);
05792
05793 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
05794
05795 add_header(req, "Via", p->via);
05796 if (p->route) {
05797 set_destination(p, p->route->hop);
05798 add_route(req, is_strict ? p->route->next : p->route);
05799 }
05800
05801 ot = get_header(orig, "To");
05802 of = get_header(orig, "From");
05803
05804
05805
05806 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
05807
05808
05809 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_strlen_zero(p->theirtag))
05810 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
05811 else if (!ast_test_flag(&p->flags[0], SIP_OUTGOING))
05812 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
05813 else
05814 snprintf(newto, sizeof(newto), "%s", ot);
05815 ot = newto;
05816 }
05817
05818 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05819 add_header(req, "From", of);
05820 add_header(req, "To", ot);
05821 } else {
05822 add_header(req, "From", ot);
05823 add_header(req, "To", of);
05824 }
05825
05826 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
05827 add_header(req, "Contact", p->our_contact);
05828
05829 copy_header(req, orig, "Call-ID");
05830 add_header(req, "CSeq", tmp);
05831
05832 if (!ast_strlen_zero(global_useragent))
05833 add_header(req, "User-Agent", global_useragent);
05834 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
05835
05836 if (!ast_strlen_zero(p->rpid))
05837 add_header(req, "Remote-Party-ID", p->rpid);
05838
05839 return 0;
05840 }
05841
05842
05843 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
05844 {
05845 struct sip_request resp;
05846 int seqno = 0;
05847
05848 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
05849 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
05850 return -1;
05851 }
05852 respprep(&resp, p, msg, req);
05853 add_header_contentLength(&resp, 0);
05854
05855
05856 if (p->method == SIP_INVITE && msg[0] != '1' && p->owner && p->owner->hangupcause) {
05857 char buf[10];
05858
05859 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
05860 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
05861 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
05862 }
05863 return send_response(p, &resp, reliable, seqno);
05864 }
05865
05866 static void temp_pvt_cleanup(void *data)
05867 {
05868 struct sip_pvt *p = data;
05869
05870 ast_string_field_free_pools(p);
05871
05872 free(data);
05873 }
05874
05875
05876 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)
05877 {
05878 struct sip_pvt *p = NULL;
05879
05880 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
05881 ast_log(LOG_NOTICE, "Failed to get temporary pvt\n");
05882 return -1;
05883 }
05884
05885
05886 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
05887 ast_set_flag(&p->flags[0], SIP_NO_HISTORY);
05888 if (ast_string_field_init(p, 512))
05889 return -1;
05890 }
05891
05892
05893 p->method = intended_method;
05894
05895 if (sin) {
05896 p->sa = *sin;
05897 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
05898 p->ourip = __ourip;
05899 } else
05900 p->ourip = __ourip;
05901
05902 p->branch = ast_random();
05903 make_our_tag(p->tag, sizeof(p->tag));
05904 p->ocseq = INITIAL_CSEQ;
05905
05906 if (useglobal_nat && sin) {
05907 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
05908 p->recv = *sin;
05909 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
05910 }
05911
05912 ast_string_field_set(p, fromdomain, default_fromdomain);
05913 build_via(p);
05914 ast_string_field_set(p, callid, callid);
05915
05916
05917 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
05918
05919
05920 ast_string_field_free_all(p);
05921
05922 return 0;
05923 }
05924
05925
05926 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
05927 {
05928 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
05929 }
05930
05931
05932 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
05933 {
05934 struct sip_request resp;
05935 respprep(&resp, p, msg, req);
05936 append_date(&resp);
05937 add_header(&resp, "Unsupported", unsupported);
05938 add_header_contentLength(&resp, 0);
05939 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
05940 }
05941
05942
05943
05944
05945 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
05946 {
05947 return __transmit_response(p, msg, req, XMIT_CRITICAL);
05948 }
05949
05950
05951 static void append_date(struct sip_request *req)
05952 {
05953 char tmpdat[256];
05954 struct tm tm;
05955 time_t t = time(NULL);
05956
05957 gmtime_r(&t, &tm);
05958 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
05959 add_header(req, "Date", tmpdat);
05960 }
05961
05962
05963 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
05964 {
05965 struct sip_request resp;
05966 respprep(&resp, p, msg, req);
05967 append_date(&resp);
05968 add_header_contentLength(&resp, 0);
05969 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
05970 }
05971
05972
05973 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
05974 {
05975 struct sip_request resp;
05976 respprep(&resp, p, msg, req);
05977 add_header(&resp, "Accept", "application/sdp");
05978 add_header_contentLength(&resp, 0);
05979 return send_response(p, &resp, reliable, 0);
05980 }
05981
05982
05983 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)
05984 {
05985 struct sip_request resp;
05986 char tmp[512];
05987 int seqno = 0;
05988
05989 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
05990 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
05991 return -1;
05992 }
05993
05994
05995 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
05996 respprep(&resp, p, msg, req);
05997 add_header(&resp, header, tmp);
05998 add_header_contentLength(&resp, 0);
05999 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
06000 return send_response(p, &resp, reliable, seqno);
06001 }
06002
06003
06004 static int add_text(struct sip_request *req, const char *text)
06005 {
06006
06007 add_header(req, "Content-Type", "text/plain");
06008 add_header_contentLength(req, strlen(text));
06009 add_line(req, text);
06010 return 0;
06011 }
06012
06013
06014
06015 static int add_digit(struct sip_request *req, char digit, unsigned int duration)
06016 {
06017 char tmp[256];
06018
06019 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
06020 add_header(req, "Content-Type", "application/dtmf-relay");
06021 add_header_contentLength(req, strlen(tmp));
06022 add_line(req, tmp);
06023 return 0;
06024 }
06025
06026
06027
06028 static int add_vidupdate(struct sip_request *req)
06029 {
06030 const char *xml_is_a_huge_waste_of_space =
06031 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
06032 " <media_control>\r\n"
06033 " <vc_primitive>\r\n"
06034 " <to_encoder>\r\n"
06035 " <picture_fast_update>\r\n"
06036 " </picture_fast_update>\r\n"
06037 " </to_encoder>\r\n"
06038 " </vc_primitive>\r\n"
06039 " </media_control>\r\n";
06040 add_header(req, "Content-Type", "application/media_control+xml");
06041 add_header_contentLength(req, strlen(xml_is_a_huge_waste_of_space));
06042 add_line(req, xml_is_a_huge_waste_of_space);
06043 return 0;
06044 }
06045
06046
06047 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
06048 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
06049 int debug, int *min_packet_size)
06050 {
06051 int rtp_code;
06052 struct ast_format_list fmt;
06053
06054
06055 if (debug)
06056 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
06057 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
06058 return;
06059
06060 if (p->rtp) {
06061 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
06062 fmt = ast_codec_pref_getsize(pref, codec);
06063 } else
06064 return;
06065 ast_build_string(m_buf, m_size, " %d", rtp_code);
06066 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
06067 ast_rtp_lookup_mime_subtype(1, codec,
06068 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
06069 sample_rate);
06070 if (codec == AST_FORMAT_G729A) {
06071
06072 ast_build_string(a_buf, a_size, "a=fmtp:%d annexb=no\r\n", rtp_code);
06073 } else if (codec == AST_FORMAT_G723_1) {
06074
06075 ast_build_string(a_buf, a_size, "a=fmtp:%d annexa=no\r\n", rtp_code);
06076 } else if (codec == AST_FORMAT_ILBC) {
06077
06078 ast_build_string(a_buf, a_size, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
06079 }
06080
06081 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
06082 *min_packet_size = fmt.cur_ms;
06083
06084
06085 if ((*min_packet_size) == 0 && fmt.cur_ms)
06086 *min_packet_size = fmt.cur_ms;
06087 }
06088
06089
06090 static int t38_get_rate(int t38cap)
06091 {
06092 int maxrate = (t38cap & (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400));
06093
06094 if (maxrate & T38FAX_RATE_14400) {
06095 if (option_debug > 1)
06096 ast_log(LOG_DEBUG, "T38MaxFaxRate 14400 found\n");
06097 return 14400;
06098 } else if (maxrate & T38FAX_RATE_12000) {
06099 if (option_debug > 1)
06100 ast_log(LOG_DEBUG, "T38MaxFaxRate 12000 found\n");
06101 return 12000;
06102 } else if (maxrate & T38FAX_RATE_9600) {
06103 if (option_debug > 1)
06104 ast_log(LOG_DEBUG, "T38MaxFaxRate 9600 found\n");
06105 return 9600;
06106 } else if (maxrate & T38FAX_RATE_7200) {
06107 if (option_debug > 1)
06108 ast_log(LOG_DEBUG, "T38MaxFaxRate 7200 found\n");
06109 return 7200;
06110 } else if (maxrate & T38FAX_RATE_4800) {
06111 if (option_debug > 1)
06112 ast_log(LOG_DEBUG, "T38MaxFaxRate 4800 found\n");
06113 return 4800;
06114 } else if (maxrate & T38FAX_RATE_2400) {
06115 if (option_debug > 1)
06116 ast_log(LOG_DEBUG, "T38MaxFaxRate 2400 found\n");
06117 return 2400;
06118 } else {
06119 if (option_debug > 1)
06120 ast_log(LOG_DEBUG, "Strange, T38MaxFaxRate NOT found in peers T38 SDP.\n");
06121 return 0;
06122 }
06123 }
06124
06125
06126 static int add_t38_sdp(struct sip_request *resp, struct sip_pvt *p)
06127 {
06128 int len = 0;
06129 int x = 0;
06130 struct sockaddr_in udptlsin;
06131 char v[256] = "";
06132 char s[256] = "";
06133 char o[256] = "";
06134 char c[256] = "";
06135 char t[256] = "";
06136 char m_modem[256];
06137 char a_modem[1024];
06138 char *m_modem_next = m_modem;
06139 size_t m_modem_left = sizeof(m_modem);
06140 char *a_modem_next = a_modem;
06141 size_t a_modem_left = sizeof(a_modem);
06142 struct sockaddr_in udptldest = { 0, };
06143 int debug;
06144
06145 debug = sip_debug_test_pvt(p);
06146 len = 0;
06147 if (!p->udptl) {
06148 ast_log(LOG_WARNING, "No way to add SDP without an UDPTL structure\n");
06149 return -1;
06150 }
06151
06152 if (!p->sessionid) {
06153 p->sessionid = getpid();
06154 p->sessionversion = p->sessionid;
06155 } else
06156 p->sessionversion++;
06157
06158
06159 ast_udptl_get_us(p->udptl, &udptlsin);
06160
06161
06162 if (p->udptlredirip.sin_addr.s_addr) {
06163 udptldest.sin_port = p->udptlredirip.sin_port;
06164 udptldest.sin_addr = p->udptlredirip.sin_addr;
06165 } else {
06166 udptldest.sin_addr = p->ourip;
06167 udptldest.sin_port = udptlsin.sin_port;
06168 }
06169
06170 if (debug)
06171 ast_log(LOG_DEBUG, "T.38 UDPTL is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(udptlsin.sin_port));
06172
06173
06174
06175
06176 if (debug) {
06177 ast_log(LOG_DEBUG, "Our T38 capability (%d), peer T38 capability (%d), joint capability (%d)\n",
06178 p->t38.capability,
06179 p->t38.peercapability,
06180 p->t38.jointcapability);
06181 }
06182 snprintf(v, sizeof(v), "v=0\r\n");
06183 snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(udptldest.sin_addr));
06184 snprintf(s, sizeof(s), "s=session\r\n");
06185 snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(udptldest.sin_addr));
06186 snprintf(t, sizeof(t), "t=0 0\r\n");
06187 ast_build_string(&m_modem_next, &m_modem_left, "m=image %d udptl t38\r\n", ntohs(udptldest.sin_port));
06188
06189 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_0)
06190 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:0\r\n");
06191 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_1)
06192 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:1\r\n");
06193 if ((x = t38_get_rate(p->t38.jointcapability)))
06194 ast_build_string(&a_modem_next, &a_modem_left, "a=T38MaxBitRate:%d\r\n",x);
06195 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxFillBitRemoval:%d\r\n", (p->t38.jointcapability & T38FAX_FILL_BIT_REMOVAL) ? 1 : 0);
06196 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingMMR:%d\r\n", (p->t38.jointcapability & T38FAX_TRANSCODING_MMR) ? 1 : 0);
06197 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingJBIG:%d\r\n", (p->t38.jointcapability & T38FAX_TRANSCODING_JBIG) ? 1 : 0);
06198 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxRateManagement:%s\r\n", (p->t38.jointcapability & T38FAX_RATE_MANAGEMENT_LOCAL_TCF) ? "localTCF" : "transferredTCF");
06199 x = ast_udptl_get_local_max_datagram(p->udptl);
06200 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxBuffer:%d\r\n",x);
06201 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxDatagram:%d\r\n",x);
06202 if (p->t38.jointcapability != T38FAX_UDP_EC_NONE)
06203 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxUdpEC:%s\r\n", (p->t38.jointcapability & T38FAX_UDP_EC_REDUNDANCY) ? "t38UDPRedundancy" : "t38UDPFEC");
06204 len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m_modem) + strlen(a_modem);
06205 add_header(resp, "Content-Type", "application/sdp");
06206 add_header_contentLength(resp, len);
06207 add_line(resp, v);
06208 add_line(resp, o);
06209 add_line(resp, s);
06210 add_line(resp, c);
06211 add_line(resp, t);
06212 add_line(resp, m_modem);
06213 add_line(resp, a_modem);
06214
06215
06216 p->lastrtprx = p->lastrtptx = time(NULL);
06217
06218 return 0;
06219 }
06220
06221
06222
06223 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
06224 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
06225 int debug)
06226 {
06227 int rtp_code;
06228
06229 if (debug)
06230 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format, 0));
06231 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
06232 return;
06233
06234 ast_build_string(m_buf, m_size, " %d", rtp_code);
06235 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
06236 ast_rtp_lookup_mime_subtype(0, format, 0),
06237 sample_rate);
06238 if (format == AST_RTP_DTMF)
06239
06240 ast_build_string(a_buf, a_size, "a=fmtp:%d 0-16\r\n", rtp_code);
06241 }
06242
06243 #define SDP_SAMPLE_RATE(x) (x == AST_FORMAT_G722) ? 16000 : 8000
06244
06245
06246 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p)
06247 {
06248 int len = 0;
06249 int alreadysent = 0;
06250
06251 struct sockaddr_in sin;
06252 struct sockaddr_in vsin;
06253 struct sockaddr_in dest;
06254 struct sockaddr_in vdest = { 0, };
06255
06256
06257 char *version = "v=0\r\n";
06258 char *subject = "s=session\r\n";
06259 char owner[256];
06260 char connection[256];
06261 char *stime = "t=0 0\r\n";
06262 char bandwidth[256] = "";
06263 char *hold;
06264 char m_audio[256];
06265 char m_video[256];
06266 char a_audio[1024];
06267 char a_video[1024];
06268 char *m_audio_next = m_audio;
06269 char *m_video_next = m_video;
06270 size_t m_audio_left = sizeof(m_audio);
06271 size_t m_video_left = sizeof(m_video);
06272 char *a_audio_next = a_audio;
06273 char *a_video_next = a_video;
06274 size_t a_audio_left = sizeof(a_audio);
06275 size_t a_video_left = sizeof(a_video);
06276
06277 int x;
06278 int capability;
06279 int needvideo = FALSE;
06280 int debug = sip_debug_test_pvt(p);
06281 int min_audio_packet_size = 0;
06282 int min_video_packet_size = 0;
06283
06284 m_video[0] = '\0';
06285
06286 if (!p->rtp) {
06287 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
06288 return AST_FAILURE;
06289 }
06290
06291
06292 if (!p->sessionid) {
06293 p->sessionid = getpid();
06294 p->sessionversion = p->sessionid;
06295 } else
06296 p->sessionversion++;
06297
06298
06299 ast_rtp_get_us(p->rtp, &sin);
06300 if (p->vrtp)
06301 ast_rtp_get_us(p->vrtp, &vsin);
06302
06303
06304 if (p->redirip.sin_addr.s_addr) {
06305 dest.sin_port = p->redirip.sin_port;
06306 dest.sin_addr = p->redirip.sin_addr;
06307 } else {
06308 dest.sin_addr = p->ourip;
06309 dest.sin_port = sin.sin_port;
06310 }
06311
06312 capability = p->jointcapability;
06313
06314
06315 if (option_debug > 1) {
06316 char codecbuf[BUFSIZ];
06317 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");
06318 ast_log(LOG_DEBUG, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
06319 }
06320
06321 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
06322 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_RTP)) {
06323 ast_build_string(&m_audio_next, &m_audio_left, " %d", 191);
06324 ast_build_string(&a_audio_next, &a_audio_left, "a=rtpmap:%d %s/%d\r\n", 191, "t38", 8000);
06325 }
06326 #endif
06327
06328
06329 if ((capability & AST_FORMAT_VIDEO_MASK) && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
06330 if (p->vrtp) {
06331 needvideo = TRUE;
06332 if (option_debug > 1)
06333 ast_log(LOG_DEBUG, "This call needs video offers!\n");
06334 } else if (option_debug > 1)
06335 ast_log(LOG_DEBUG, "This call needs video offers, but there's no video support enabled!\n");
06336 }
06337
06338
06339
06340
06341 if (needvideo) {
06342
06343 if (p->vredirip.sin_addr.s_addr) {
06344 vdest.sin_addr = p->vredirip.sin_addr;
06345 vdest.sin_port = p->vredirip.sin_port;
06346 } else {
06347 vdest.sin_addr = p->ourip;
06348 vdest.sin_port = vsin.sin_port;
06349 }
06350 ast_build_string(&m_video_next, &m_video_left, "m=video %d RTP/AVP", ntohs(vdest.sin_port));
06351
06352
06353 if (p->maxcallbitrate)
06354 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
06355 if (debug)
06356 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(vsin.sin_port));
06357 }
06358
06359 if (debug)
06360 ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(sin.sin_port));
06361
06362
06363
06364
06365
06366
06367 snprintf(owner, sizeof(owner), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(dest.sin_addr));
06368 snprintf(connection, sizeof(connection), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
06369 ast_build_string(&m_audio_next, &m_audio_left, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
06370
06371 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR)
06372 hold = "a=recvonly\r\n";
06373 else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE)
06374 hold = "a=inactive\r\n";
06375 else
06376 hold = "a=sendrecv\r\n";
06377
06378
06379
06380
06381
06382
06383
06384
06385
06386
06387 if (capability & p->prefcodec) {
06388 int codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
06389
06390 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
06391 &m_audio_next, &m_audio_left,
06392 &a_audio_next, &a_audio_left,
06393 debug, &min_audio_packet_size);
06394 alreadysent |= codec;
06395 }
06396
06397
06398 for (x = 0; x < 32; x++) {
06399 int codec;
06400
06401 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
06402 break;
06403
06404 if (!(capability & codec))
06405 continue;
06406
06407 if (alreadysent & codec)
06408 continue;
06409
06410 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
06411 &m_audio_next, &m_audio_left,
06412 &a_audio_next, &a_audio_left,
06413 debug, &min_audio_packet_size);
06414 alreadysent |= codec;
06415 }
06416
06417
06418 for (x = 1; x <= (needvideo ? AST_FORMAT_MAX_VIDEO : AST_FORMAT_MAX_AUDIO); x <<= 1) {
06419 if (!(capability & x))
06420 continue;
06421
06422 if (alreadysent & x)
06423 continue;
06424
06425 if (x <= AST_FORMAT_MAX_AUDIO)
06426 add_codec_to_sdp(p, x, SDP_SAMPLE_RATE(x),
06427 &m_audio_next, &m_audio_left,
06428 &a_audio_next, &a_audio_left,
06429 debug, &min_audio_packet_size);
06430 else
06431 add_codec_to_sdp(p, x, 90000,
06432 &m_video_next, &m_video_left,
06433 &a_video_next, &a_video_left,
06434 debug, &min_video_packet_size);
06435 }
06436
06437
06438 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
06439 if (!(p->jointnoncodeccapability & x))
06440 continue;
06441
06442 add_noncodec_to_sdp(p, x, 8000,
06443 &m_audio_next, &m_audio_left,
06444 &a_audio_next, &a_audio_left,
06445 debug);
06446 }
06447
06448 if (option_debug > 2)
06449 ast_log(LOG_DEBUG, "-- Done with adding codecs to SDP\n");
06450
06451 if (!p->owner || !ast_internal_timing_enabled(p->owner))
06452 ast_build_string(&a_audio_next, &a_audio_left, "a=silenceSupp:off - - - -\r\n");
06453
06454 if (min_audio_packet_size)
06455 ast_build_string(&a_audio_next, &a_audio_left, "a=ptime:%d\r\n", min_audio_packet_size);
06456
06457 if (min_video_packet_size)
06458 ast_build_string(&a_video_next, &a_video_left, "a=ptime:%d\r\n", min_video_packet_size);
06459
06460 if ((m_audio_left < 2) || (m_video_left < 2) || (a_audio_left == 0) || (a_video_left == 0))
06461 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
06462
06463 ast_build_string(&m_audio_next, &m_audio_left, "\r\n");
06464 if (needvideo)
06465 ast_build_string(&m_video_next, &m_video_left, "\r\n");
06466
06467 len = strlen(version) + strlen(subject) + strlen(owner) + strlen(connection) + strlen(stime) + strlen(m_audio) + strlen(a_audio) + strlen(hold);
06468 if (needvideo)
06469 len += strlen(m_video) + strlen(a_video) + strlen(bandwidth) + strlen(hold);
06470
06471 add_header(resp, "Content-Type", "application/sdp");
06472 add_header_contentLength(resp, len);
06473 add_line(resp, version);
06474 add_line(resp, owner);
06475 add_line(resp, subject);
06476 add_line(resp, connection);
06477 if (needvideo)
06478 add_line(resp, bandwidth);
06479 add_line(resp, stime);
06480 add_line(resp, m_audio);
06481 add_line(resp, a_audio);
06482 add_line(resp, hold);
06483 if (needvideo) {
06484 add_line(resp, m_video);
06485 add_line(resp, a_video);
06486 add_line(resp, hold);
06487 }
06488
06489
06490 p->lastrtprx = p->lastrtptx = time(NULL);
06491
06492 if (option_debug > 2) {
06493 char buf[BUFSIZ];
06494 ast_log(LOG_DEBUG, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, BUFSIZ, capability));
06495 }
06496
06497 return AST_SUCCESS;
06498 }
06499
06500
06501 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
06502 {
06503 struct sip_request resp;
06504 int seqno;
06505
06506 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
06507 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
06508 return -1;
06509 }
06510 respprep(&resp, p, msg, req);
06511 if (p->udptl) {
06512 ast_udptl_offered_from_local(p->udptl, 0);
06513 add_t38_sdp(&resp, p);
06514 } else
06515 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
06516 if (retrans && !p->pendinginvite)
06517 p->pendinginvite = seqno;
06518 return send_response(p, &resp, retrans, seqno);
06519 }
06520
06521
06522 static void copy_request(struct sip_request *dst, const struct sip_request *src)
06523 {
06524 long offset;
06525 int x;
06526 offset = ((void *)dst) - ((void *)src);
06527
06528 memcpy(dst, src, sizeof(*dst));
06529
06530 for (x=0; x < src->headers; x++)
06531 dst->header[x] += offset;
06532 for (x=0; x < src->lines; x++)
06533 dst->line[x] += offset;
06534 dst->rlPart1 += offset;
06535 dst->rlPart2 += offset;
06536 }
06537
06538
06539
06540
06541 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06542 {
06543 struct sip_request resp;
06544 int seqno;
06545 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
06546 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
06547 return -1;
06548 }
06549 respprep(&resp, p, msg, req);
06550 if (p->rtp) {
06551 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06552 if (option_debug)
06553 ast_log(LOG_DEBUG, "Setting framing from config on incoming call\n");
06554 ast_rtp_codec_setpref(p->rtp, &p->prefs);
06555 }
06556 try_suggested_sip_codec(p);
06557 add_sdp(&resp, p);
06558 } else
06559 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
06560 if (reliable && !p->pendinginvite)
06561 p->pendinginvite = seqno;
06562 return send_response(p, &resp, reliable, seqno);
06563 }
06564
06565
06566 static int determine_firstline_parts(struct sip_request *req)
06567 {
06568 char *e = ast_skip_blanks(req->header[0]);
06569
06570 if (!*e)
06571 return -1;
06572 req->rlPart1 = e;
06573 e = ast_skip_nonblanks(e);
06574 if (*e)
06575 *e++ = '\0';
06576
06577 e = ast_skip_blanks(e);
06578 if ( !*e )
06579 return -1;
06580 ast_trim_blanks(e);
06581
06582 if (!strcasecmp(req->rlPart1, "SIP/2.0") ) {
06583 if (strlen(e) < 3)
06584 return -1;
06585 req->rlPart2 = e;
06586 } else {
06587 if ( *e == '<' ) {
06588 ast_log(LOG_WARNING, "bogus uri in <> %s\n", e);
06589 e++;
06590 if (!*e)
06591 return -1;
06592 }
06593 req->rlPart2 = e;
06594 e = ast_skip_nonblanks(e);
06595 if (*e)
06596 *e++ = '\0';
06597 e = ast_skip_blanks(e);
06598 if (strcasecmp(e, "SIP/2.0") ) {
06599 ast_log(LOG_WARNING, "Bad request protocol %s\n", e);
06600 return -1;
06601 }
06602 }
06603 return 1;
06604 }
06605
06606
06607
06608
06609
06610
06611
06612 static int transmit_reinvite_with_sdp(struct sip_pvt *p)
06613 {
06614 struct sip_request req;
06615
06616 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
06617
06618 add_header(&req, "Allow", ALLOWED_METHODS);
06619 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06620 if (sipdebug)
06621 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
06622 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
06623 append_history(p, "ReInv", "Re-invite sent");
06624 add_sdp(&req, p);
06625
06626 initialize_initreq(p, &req);
06627 p->lastinvite = p->ocseq;
06628 ast_set_flag(&p->flags[0], SIP_OUTGOING);
06629 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
06630 }
06631
06632
06633
06634
06635
06636 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p)
06637 {
06638 struct sip_request req;
06639
06640 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
06641
06642 add_header(&req, "Allow", ALLOWED_METHODS);
06643 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06644 if (sipdebug)
06645 add_header(&req, "X-asterisk-info", "SIP re-invite (T38 switchover)");
06646 ast_udptl_offered_from_local(p->udptl, 1);
06647 add_t38_sdp(&req, p);
06648
06649 initialize_initreq(p, &req);
06650 ast_set_flag(&p->flags[0], SIP_OUTGOING);
06651 p->lastinvite = p->ocseq;
06652 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
06653 }
06654
06655
06656 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
06657 {
06658 char stripped[BUFSIZ];
06659 char *c;
06660
06661 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
06662 c = get_in_brackets(stripped);
06663 c = strsep(&c, ";");
06664 if (!ast_strlen_zero(c))
06665 ast_string_field_set(p, uri, c);
06666 }
06667
06668
06669 static void build_contact(struct sip_pvt *p)
06670 {
06671
06672 if (ourport != STANDARD_SIP_PORT)
06673 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);
06674 else
06675 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip));
06676 }
06677
06678
06679 static void build_rpid(struct sip_pvt *p)
06680 {
06681 int send_pres_tags = TRUE;
06682 const char *privacy=NULL;
06683 const char *screen=NULL;
06684 char buf[256];
06685 const char *clid = default_callerid;
06686 const char *clin = NULL;
06687 const char *fromdomain;
06688
06689 if (!ast_strlen_zero(p->rpid) || !ast_strlen_zero(p->rpid_from))
06690 return;
06691
06692 if (p->owner && p->owner->cid.cid_num)
06693 clid = p->owner->cid.cid_num;
06694 if (p->owner && p->owner->cid.cid_name)
06695 clin = p->owner->cid.cid_name;
06696 if (ast_strlen_zero(clin))
06697 clin = clid;
06698
06699 switch (p->callingpres) {
06700 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
06701 privacy = "off";
06702 screen = "no";
06703 break;
06704 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
06705 privacy = "off";
06706 screen = "yes";
06707 break;
06708 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
06709 privacy = "off";
06710 screen = "no";
06711 break;
06712 case AST_PRES_ALLOWED_NETWORK_NUMBER:
06713 privacy = "off";
06714 screen = "yes";
06715 break;
06716 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
06717 privacy = "full";
06718 screen = "no";
06719 break;
06720 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
06721 privacy = "full";
06722 screen = "yes";
06723 break;
06724 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
06725 privacy = "full";
06726 screen = "no";
06727 break;
06728 case AST_PRES_PROHIB_NETWORK_NUMBER:
06729 privacy = "full";
06730 screen = "yes";
06731 break;
06732 case AST_PRES_NUMBER_NOT_AVAILABLE:
06733 send_pres_tags = FALSE;
06734 break;
06735 default:
06736 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
06737 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
06738 privacy = "full";
06739 else
06740 privacy = "off";
06741 screen = "no";
06742 break;
06743 }
06744
06745 fromdomain = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip));
06746
06747 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
06748 if (send_pres_tags)
06749 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
06750 ast_string_field_set(p, rpid, buf);
06751
06752 ast_string_field_build(p, rpid_from, "\"%s\" <sip:%s@%s>;tag=%s", clin,
06753 S_OR(p->fromuser, clid),
06754 fromdomain, p->tag);
06755 }
06756
06757
06758 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
06759 {
06760 char invite_buf[256] = "";
06761 char *invite = invite_buf;
06762 size_t invite_max = sizeof(invite_buf);
06763 char from[256];
06764 char to[256];
06765 char tmp[BUFSIZ/2];
06766 char tmp2[BUFSIZ/2];
06767 const char *l = NULL, *n = NULL;
06768 const char *urioptions = "";
06769
06770 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
06771 const char *s = p->username;
06772
06773
06774
06775
06776
06777
06778 if (*s == '+')
06779 s++;
06780 for (; *s; s++) {
06781 if (!strchr(AST_DIGIT_ANYNUM, *s) )
06782 break;
06783 }
06784
06785 if (*s)
06786 urioptions = ";user=phone";
06787 }
06788
06789
06790 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
06791
06792 if (p->owner) {
06793 l = p->owner->cid.cid_num;
06794 n = p->owner->cid.cid_name;
06795 }
06796
06797 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID) &&
06798 ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
06799 l = CALLERID_UNKNOWN;
06800 n = l;
06801 }
06802 if (ast_strlen_zero(l))
06803 l = default_callerid;
06804 if (ast_strlen_zero(n))
06805 n = l;
06806
06807 if (!ast_strlen_zero(p->fromuser))
06808 l = p->fromuser;
06809 else
06810 ast_string_field_set(p, fromuser, l);
06811
06812
06813 if (!ast_strlen_zero(p->fromname))
06814 n = p->fromname;
06815 else
06816 ast_string_field_set(p, fromname, n);
06817
06818 if (pedanticsipchecking) {
06819 ast_uri_encode(n, tmp, sizeof(tmp), 0);
06820 n = tmp;
06821 ast_uri_encode(l, tmp2, sizeof(tmp2), 0);
06822 l = tmp2;
06823 }
06824
06825 if (ourport != STANDARD_SIP_PORT && ast_strlen_zero(p->fromdomain))
06826 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);
06827 else
06828 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), p->tag);
06829
06830
06831 if (!ast_strlen_zero(p->fullcontact)) {
06832
06833 ast_build_string(&invite, &invite_max, "%s", p->fullcontact);
06834 } else {
06835
06836 ast_build_string(&invite, &invite_max, "sip:");
06837 if (!ast_strlen_zero(p->username)) {
06838 n = p->username;
06839 if (pedanticsipchecking) {
06840 ast_uri_encode(n, tmp, sizeof(tmp), 0);
06841 n = tmp;
06842 }
06843 ast_build_string(&invite, &invite_max, "%s@", n);
06844 }
06845 ast_build_string(&invite, &invite_max, "%s", p->tohost);
06846 if (ntohs(p->sa.sin_port) != STANDARD_SIP_PORT)
06847 ast_build_string(&invite, &invite_max, ":%d", ntohs(p->sa.sin_port));
06848 ast_build_string(&invite, &invite_max, "%s", urioptions);
06849 }
06850
06851
06852 if (p->options && p->options->uri_options)
06853 ast_build_string(&invite, &invite_max, ";%s", p->options->uri_options);
06854
06855 ast_string_field_set(p, uri, invite_buf);
06856
06857 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
06858
06859 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", p->uri, p->theirtag);
06860 } else if (p->options && p->options->vxml_url) {
06861
06862 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
06863 } else
06864 snprintf(to, sizeof(to), "<%s>", p->uri);
06865
06866 init_req(req, sipmethod, p->uri);
06867 snprintf(tmp, sizeof(tmp), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
06868
06869 add_header(req, "Via", p->via);
06870
06871
06872
06873 if (ast_test_flag(&p->flags[0], SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
06874 build_rpid(p);
06875 add_header(req, "From", p->rpid_from);
06876 } else
06877 add_header(req, "From", from);
06878 add_header(req, "To", to);
06879 ast_string_field_set(p, exten, l);
06880 build_contact(p);
06881 add_header(req, "Contact", p->our_contact);
06882 add_header(req, "Call-ID", p->callid);
06883 add_header(req, "CSeq", tmp);
06884 if (!ast_strlen_zero(global_useragent))
06885 add_header(req, "User-Agent", global_useragent);
06886 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
06887 if (!ast_strlen_zero(p->rpid))
06888 add_header(req, "Remote-Party-ID", p->rpid);
06889 }
06890
06891
06892 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
06893 {
06894 struct sip_request req;
06895
06896 req.method = sipmethod;
06897 if (init) {
06898
06899 p->branch ^= ast_random();
06900 build_via(p);
06901 if (init > 1)
06902 initreqprep(&req, p, sipmethod);
06903 else
06904 reqprep(&req, p, sipmethod, 0, 1);
06905 } else
06906 reqprep(&req, p, sipmethod, 0, 1);
06907
06908 if (p->options && p->options->auth)
06909 add_header(&req, p->options->authheader, p->options->auth);
06910 append_date(&req);
06911 if (sipmethod == SIP_REFER) {
06912 if (p->refer) {
06913 char buf[BUFSIZ];
06914 if (!ast_strlen_zero(p->refer->refer_to))
06915 add_header(&req, "Refer-To", p->refer->refer_to);
06916 if (!ast_strlen_zero(p->refer->referred_by)) {
06917 sprintf(buf, "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
06918 add_header(&req, "Referred-By", buf);
06919 }
06920 }
06921 }
06922
06923
06924 if (p->options && p->options->replaces && !ast_strlen_zero(p->options->replaces)) {
06925 add_header(&req, "Replaces", p->options->replaces);
06926 add_header(&req, "Require", "replaces");
06927 }
06928
06929 add_header(&req, "Allow", ALLOWED_METHODS);
06930 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06931 if (p->options && p->options->addsipheaders && p->owner) {
06932 struct ast_channel *ast = p->owner;
06933 struct varshead *headp = &ast->varshead;
06934
06935 if (!headp)
06936 ast_log(LOG_WARNING,"No Headp for the channel...ooops!\n");
06937 else {
06938 const struct ast_var_t *current;
06939 AST_LIST_TRAVERSE(headp, current, entries) {
06940
06941 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
06942 char *content, *end;
06943 const char *header = ast_var_value(current);
06944 char *headdup = ast_strdupa(header);
06945
06946
06947 if (*headdup == '"')
06948 headdup++;
06949 if ((content = strchr(headdup, ':'))) {
06950 *content++ = '\0';
06951 content = ast_skip_blanks(content);
06952
06953 end = content + strlen(content) -1;
06954 if (*end == '"')
06955 *end = '\0';
06956
06957 add_header(&req, headdup, content);
06958 if (sipdebug)
06959 ast_log(LOG_DEBUG, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
06960 }
06961 }
06962 }
06963 }
06964 }
06965 if (sdp) {
06966 if (p->udptl && p->t38.state == T38_LOCAL_DIRECT) {
06967 ast_udptl_offered_from_local(p->udptl, 1);
06968 if (option_debug)
06969 ast_log(LOG_DEBUG, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
06970 add_t38_sdp(&req, p);
06971 } else if (p->rtp)
06972 add_sdp(&req, p);
06973 } else {
06974 add_header_contentLength(&req, 0);
06975 }
06976
06977 if (!p->initreq.headers)
06978 initialize_initreq(p, &req);
06979 p->lastinvite = p->ocseq;
06980 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
06981 }
06982
06983
06984 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
06985 {
06986 char tmp[4000], from[256], to[256];
06987 char *t = tmp, *c, *mfrom, *mto;
06988 size_t maxbytes = sizeof(tmp);
06989 struct sip_request req;
06990 char hint[AST_MAX_EXTENSION];
06991 char *statestring = "terminated";
06992 const struct cfsubscription_types *subscriptiontype;
06993 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
06994 char *pidfstate = "--";
06995 char *pidfnote= "Ready";
06996
06997 memset(from, 0, sizeof(from));
06998 memset(to, 0, sizeof(to));
06999 memset(tmp, 0, sizeof(tmp));
07000
07001 switch (state) {
07002 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
07003 statestring = (global_notifyringing) ? "early" : "confirmed";
07004 local_state = NOTIFY_INUSE;
07005 pidfstate = "busy";
07006 pidfnote = "Ringing";
07007 break;
07008 case AST_EXTENSION_RINGING:
07009 statestring = "early";
07010 local_state = NOTIFY_INUSE;
07011 pidfstate = "busy";
07012 pidfnote = "Ringing";
07013 break;
07014 case AST_EXTENSION_INUSE:
07015 statestring = "confirmed";
07016 local_state = NOTIFY_INUSE;
07017 pidfstate = "busy";
07018 pidfnote = "On the phone";
07019 break;
07020 case AST_EXTENSION_BUSY:
07021 statestring = "confirmed";
07022 local_state = NOTIFY_CLOSED;
07023 pidfstate = "busy";
07024 pidfnote = "On the phone";
07025 break;
07026 case AST_EXTENSION_UNAVAILABLE:
07027 statestring = "terminated";
07028 local_state = NOTIFY_CLOSED;
07029 pidfstate = "away";
07030 pidfnote = "Unavailable";
07031 break;
07032 case AST_EXTENSION_ONHOLD:
07033 statestring = "confirmed";
07034 local_state = NOTIFY_INUSE;
07035 pidfstate = "busy";
07036 pidfnote = "On Hold";
07037 break;
07038 case AST_EXTENSION_NOT_INUSE:
07039 default:
07040
07041 break;
07042 }
07043
07044 subscriptiontype = find_subscription_type(p->subscribed);
07045
07046
07047 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
07048 char *hint2 = hint, *individual_hint = NULL;
07049 while ((individual_hint = strsep(&hint2, "&"))) {
07050
07051 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE) {
07052 local_state = NOTIFY_CLOSED;
07053 pidfstate = "away";
07054 pidfnote = "Not online";
07055 }
07056 }
07057 }
07058
07059 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
07060 c = get_in_brackets(from);
07061 if (strncasecmp(c, "sip:", 4)) {
07062 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
07063 return -1;
07064 }
07065 mfrom = strsep(&c, ";");
07066
07067 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
07068 c = get_in_brackets(to);
07069 if (strncasecmp(c, "sip:", 4)) {
07070 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
07071 return -1;
07072 }
07073 mto = strsep(&c, ";");
07074
07075 reqprep(&req, p, SIP_NOTIFY, 0, 1);
07076
07077
07078 add_header(&req, "Event", subscriptiontype->event);
07079 add_header(&req, "Content-Type", subscriptiontype->mediatype);
07080 switch(state) {
07081 case AST_EXTENSION_DEACTIVATED:
07082 if (timeout)
07083 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07084 else {
07085 add_header(&req, "Subscription-State", "terminated;reason=probation");
07086 add_header(&req, "Retry-After", "60");
07087 }
07088 break;
07089 case AST_EXTENSION_REMOVED:
07090 add_header(&req, "Subscription-State", "terminated;reason=noresource");
07091 break;
07092 default:
07093 if (p->expiry)
07094 add_header(&req, "Subscription-State", "active");
07095 else
07096 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07097 }
07098 switch (p->subscribed) {
07099 case XPIDF_XML:
07100 case CPIM_PIDF_XML:
07101 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
07102 ast_build_string(&t, &maxbytes, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n");
07103 ast_build_string(&t, &maxbytes, "<presence>\n");
07104 ast_build_string(&t, &maxbytes, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
07105 ast_build_string(&t, &maxbytes, "<atom id=\"%s\">\n", p->exten);
07106 ast_build_string(&t, &maxbytes, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
07107 ast_build_string(&t, &maxbytes, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
07108 ast_build_string(&t, &maxbytes, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
07109 ast_build_string(&t, &maxbytes, "</address>\n</atom>\n</presence>\n");
07110 break;
07111 case PIDF_XML:
07112 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
07113 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);
07114 ast_build_string(&t, &maxbytes, "<pp:person><status>\n");
07115 if (pidfstate[0] != '-')
07116 ast_build_string(&t, &maxbytes, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
07117 ast_build_string(&t, &maxbytes, "</status></pp:person>\n");
07118 ast_build_string(&t, &maxbytes, "<note>%s</note>\n", pidfnote);
07119 ast_build_string(&t, &maxbytes, "<tuple id=\"%s\">\n", p->exten);
07120 ast_build_string(&t, &maxbytes, "<contact priority=\"1\">%s</contact>\n", mto);
07121 if (pidfstate[0] == 'b')
07122 ast_build_string(&t, &maxbytes, "<status><basic>open</basic></status>\n");
07123 else
07124 ast_build_string(&t, &maxbytes, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
07125 ast_build_string(&t, &maxbytes, "</tuple>\n</presence>\n");
07126 break;
07127 case DIALOG_INFO_XML:
07128 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
07129 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);
07130 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
07131 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
07132 else
07133 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\">\n", p->exten);
07134 ast_build_string(&t, &maxbytes, "<state>%s</state>\n", statestring);
07135 if (state == AST_EXTENSION_ONHOLD) {
07136 ast_build_string(&t, &maxbytes, "<local>\n<target uri=\"%s\">\n"
07137 "<param pname=\"+sip.rendering\" pvalue=\"no\">\n"
07138 "</target>\n</local>\n", mto);
07139 }
07140 ast_build_string(&t, &maxbytes, "</dialog>\n</dialog-info>\n");
07141 break;
07142 case NONE:
07143 default:
07144 break;
07145 }
07146
07147 if (t > tmp + sizeof(tmp))
07148 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07149
07150 add_header_contentLength(&req, strlen(tmp));
07151 add_line(&req, tmp);
07152
07153 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07154 }
07155
07156
07157
07158
07159
07160
07161
07162 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
07163 {
07164 struct sip_request req;
07165 char tmp[500];
07166 char *t = tmp;
07167 size_t maxbytes = sizeof(tmp);
07168
07169 initreqprep(&req, p, SIP_NOTIFY);
07170 add_header(&req, "Event", "message-summary");
07171 add_header(&req, "Content-Type", default_notifymime);
07172
07173 ast_build_string(&t, &maxbytes, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
07174 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n",
07175 S_OR(vmexten, default_vmexten), S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)));
07176
07177
07178
07179 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)"));
07180
07181 if (p->subscribed) {
07182 if (p->expiry)
07183 add_header(&req, "Subscription-State", "active");
07184 else
07185 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07186 }
07187
07188 if (t > tmp + sizeof(tmp))
07189 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07190
07191 add_header_contentLength(&req, strlen(tmp));
07192 add_line(&req, tmp);
07193
07194 if (!p->initreq.headers)
07195 initialize_initreq(p, &req);
07196 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07197 }
07198
07199
07200 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req)
07201 {
07202 if (!p->initreq.headers)
07203 initialize_initreq(p, req);
07204 return send_request(p, req, XMIT_UNRELIABLE, p->ocseq);
07205 }
07206
07207
07208 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
07209 {
07210 struct sip_request req;
07211 char tmp[BUFSIZ/2];
07212
07213 reqprep(&req, p, SIP_NOTIFY, 0, 1);
07214 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
07215 add_header(&req, "Event", tmp);
07216 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
07217 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
07218 add_header(&req, "Allow", ALLOWED_METHODS);
07219 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07220
07221 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
07222 add_header_contentLength(&req, strlen(tmp));
07223 add_line(&req, tmp);
07224
07225 if (!p->initreq.headers)
07226 initialize_initreq(p, &req);
07227
07228 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07229 }
07230
07231
07232 static char *regstate2str(enum sipregistrystate regstate)
07233 {
07234 switch(regstate) {
07235 case REG_STATE_FAILED:
07236 return "Failed";
07237 case REG_STATE_UNREGISTERED:
07238 return "Unregistered";
07239 case REG_STATE_REGSENT:
07240 return "Request Sent";
07241 case REG_STATE_AUTHSENT:
07242 return "Auth. Sent";
07243 case REG_STATE_REGISTERED:
07244 return "Registered";
07245 case REG_STATE_REJECTED:
07246 return "Rejected";
07247 case REG_STATE_TIMEOUT:
07248 return "Timeout";
07249 case REG_STATE_NOAUTH:
07250 return "No Authentication";
07251 default:
07252 return "Unknown";
07253 }
07254 }
07255
07256
07257 static int sip_reregister(void *data)
07258 {
07259
07260 struct sip_registry *r= ASTOBJ_REF((struct sip_registry *) data);
07261
07262
07263 if (!r)
07264 return 0;
07265
07266 if (r->call && !ast_test_flag(&r->call->flags[0], SIP_NO_HISTORY))
07267 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
07268
07269
07270 if (sipdebug)
07271 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
07272
07273 r->expire = -1;
07274 __sip_do_register(r);
07275 ASTOBJ_UNREF(r, sip_registry_destroy);
07276 return 0;
07277 }
07278
07279
07280 static int __sip_do_register(struct sip_registry *r)
07281 {
07282 int res;
07283
07284 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
07285 return res;
07286 }
07287
07288
07289 static int sip_reg_timeout(void *data)
07290 {
07291
07292
07293 struct sip_registry *r = ASTOBJ_REF((struct sip_registry *) data);
07294 struct sip_pvt *p;
07295 int res;
07296
07297
07298 if (!r)
07299 return 0;
07300
07301 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
07302 if (r->call) {
07303
07304
07305 p = r->call;
07306 if (p->registry)
07307 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
07308 r->call = NULL;
07309 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
07310
07311 __sip_pretend_ack(p);
07312 }
07313
07314 if (global_regattempts_max && (r->regattempts > global_regattempts_max)) {
07315
07316
07317
07318 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
07319 r->regstate = REG_STATE_FAILED;
07320 } else {
07321 r->regstate = REG_STATE_UNREGISTERED;
07322 r->timeout = -1;
07323 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
07324 }
07325 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));
07326 ASTOBJ_UNREF(r, sip_registry_destroy);
07327 return 0;
07328 }
07329
07330
07331 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
07332 {
07333 struct sip_request req;
07334 char from[256];
07335 char to[256];
07336 char tmp[80];
07337 char addr[80];
07338 struct sip_pvt *p;
07339
07340
07341 if ( r == NULL || ((auth==NULL) && (r->regstate==REG_STATE_REGSENT || r->regstate==REG_STATE_AUTHSENT))) {
07342 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
07343 return 0;
07344 }
07345
07346 if (r->call) {
07347 if (!auth) {
07348 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
07349 return 0;
07350 } else {
07351 p = r->call;
07352 make_our_tag(p->tag, sizeof(p->tag));
07353 ast_string_field_free(p, theirtag);
07354 }
07355 } else {
07356
07357 if (!r->callid_valid) {
07358 build_callid_registry(r, __ourip, default_fromdomain);
07359 r->callid_valid = TRUE;
07360 }
07361
07362 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER))) {
07363 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
07364 return 0;
07365 }
07366 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
07367 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
07368
07369 if (create_addr(p, r->hostname)) {
07370
07371
07372 sip_destroy(p);
07373 if (r->timeout > -1) {
07374 ast_sched_del(sched, r->timeout);
07375 r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
07376 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
07377 } else {
07378 r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
07379 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);
07380 }
07381 r->regattempts++;
07382 return 0;
07383 }
07384
07385 ast_string_field_set(r, callid, p->callid);
07386 if (r->portno)
07387 p->sa.sin_port = htons(r->portno);
07388 else
07389 r->portno = ntohs(p->sa.sin_port);
07390 ast_set_flag(&p->flags[0], SIP_OUTGOING);
07391 r->call=p;
07392 p->registry = ASTOBJ_REF(r);
07393 if (!ast_strlen_zero(r->secret))
07394 ast_string_field_set(p, peersecret, r->secret);
07395 if (!ast_strlen_zero(r->md5secret))
07396 ast_string_field_set(p, peermd5secret, r->md5secret);
07397
07398
07399 if (!ast_strlen_zero(r->authuser)) {
07400 ast_string_field_set(p, peername, r->authuser);
07401 ast_string_field_set(p, authname, r->authuser);
07402 } else if (!ast_strlen_zero(r->username)) {
07403 ast_string_field_set(p, peername, r->username);
07404 ast_string_field_set(p, authname, r->username);
07405 ast_string_field_set(p, fromuser, r->username);
07406 }
07407 if (!ast_strlen_zero(r->username))
07408 ast_string_field_set(p, username, r->username);
07409
07410 ast_string_field_set(p, exten, r->contact);
07411
07412
07413
07414
07415
07416
07417 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
07418 p->ourip = bindaddr.sin_addr;
07419 build_contact(p);
07420 }
07421
07422
07423 if (auth == NULL) {
07424 if (r->timeout > -1) {
07425 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
07426 ast_sched_del(sched, r->timeout);
07427 }
07428 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
07429 if (option_debug)
07430 ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
07431 }
07432
07433 if (strchr(r->username, '@')) {
07434 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
07435 if (!ast_strlen_zero(p->theirtag))
07436 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
07437 else
07438 snprintf(to, sizeof(to), "<sip:%s>", r->username);
07439 } else {
07440 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
07441 if (!ast_strlen_zero(p->theirtag))
07442 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
07443 else
07444 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
07445 }
07446
07447
07448
07449 if (!ast_strlen_zero(p->fromdomain)) {
07450 if (r->portno && r->portno != STANDARD_SIP_PORT)
07451 snprintf(addr, sizeof(addr), "sip:%s:%d", p->fromdomain, r->portno);
07452 else
07453 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
07454 } else {
07455 if (r->portno && r->portno != STANDARD_SIP_PORT)
07456 snprintf(addr, sizeof(addr), "sip:%s:%d", r->hostname, r->portno);
07457 else
07458 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
07459 }
07460 ast_string_field_set(p, uri, addr);
07461
07462 p->branch ^= ast_random();
07463
07464 init_req(&req, sipmethod, addr);
07465
07466
07467 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
07468 p->ocseq = r->ocseq;
07469
07470 build_via(p);
07471 add_header(&req, "Via", p->via);
07472 add_header(&req, "From", from);
07473 add_header(&req, "To", to);
07474 add_header(&req, "Call-ID", p->callid);
07475 add_header(&req, "CSeq", tmp);
07476 if (!ast_strlen_zero(global_useragent))
07477 add_header(&req, "User-Agent", global_useragent);
07478 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07479
07480
07481 if (auth)
07482 add_header(&req, authheader, auth);
07483 else if (!ast_strlen_zero(r->nonce)) {
07484 char digest[1024];
07485
07486
07487 if (sipdebug)
07488 ast_log(LOG_DEBUG, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
07489 ast_string_field_set(p, realm, r->realm);
07490 ast_string_field_set(p, nonce, r->nonce);
07491 ast_string_field_set(p, domain, r->domain);
07492 ast_string_field_set(p, opaque, r->opaque);
07493 ast_string_field_set(p, qop, r->qop);
07494 r->noncecount++;
07495 p->noncecount = r->noncecount;
07496
07497 memset(digest,0,sizeof(digest));
07498 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
07499 add_header(&req, "Authorization", digest);
07500 else
07501 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
07502
07503 }
07504
07505 snprintf(tmp, sizeof(tmp), "%d", default_expiry);
07506 add_header(&req, "Expires", tmp);
07507 add_header(&req, "Contact", p->our_contact);
07508 add_header(&req, "Event", "registration");
07509 add_header_contentLength(&req, 0);
07510
07511 initialize_initreq(p, &req);
07512 if (sip_debug_test_pvt(p))
07513 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
07514 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
07515 r->regattempts++;
07516 if (option_debug > 3)
07517 ast_verbose("REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
07518 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
07519 }
07520
07521
07522 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
07523 {
07524 struct sip_request req;
07525
07526 reqprep(&req, p, SIP_MESSAGE, 0, 1);
07527 add_text(&req, text);
07528 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07529 }
07530
07531
07532 static int sip_refer_allocate(struct sip_pvt *p)
07533 {
07534 p->refer = ast_calloc(1, sizeof(struct sip_refer));
07535 return p->refer ? 1 : 0;
07536 }
07537
07538
07539
07540
07541
07542
07543 static int transmit_refer(struct sip_pvt *p, const char *dest)
07544 {
07545 struct sip_request req = {
07546 .headers = 0,
07547 };
07548 char from[256];
07549 const char *of;
07550 char *c;
07551 char referto[256];
07552 char *ttag, *ftag;
07553 char *theirtag = ast_strdupa(p->theirtag);
07554
07555 if (option_debug || sipdebug)
07556 ast_log(LOG_DEBUG, "SIP transfer of %s to %s\n", p->callid, dest);
07557
07558
07559 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
07560 of = get_header(&p->initreq, "To");
07561 ttag = theirtag;
07562 ftag = p->tag;
07563 } else {
07564 of = get_header(&p->initreq, "From");
07565 ftag = theirtag;
07566 ttag = p->tag;
07567 }
07568
07569 ast_copy_string(from, of, sizeof(from));
07570 of = get_in_brackets(from);
07571 ast_string_field_set(p, from, of);
07572 if (strncasecmp(of, "sip:", 4))
07573 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
07574 else
07575 of += 4;
07576
07577 if ((c = strchr(dest, '@')))
07578 c = NULL;
07579 else if ((c = strchr(of, '@')))
07580 *c++ = '\0';
07581 if (c)
07582 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
07583 else
07584 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
07585
07586
07587 sip_refer_allocate(p);
07588 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
07589 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
07590 p->refer->status = REFER_SENT;
07591
07592 reqprep(&req, p, SIP_REFER, 0, 1);
07593 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07594
07595 add_header(&req, "Refer-To", referto);
07596 add_header(&req, "Allow", ALLOWED_METHODS);
07597 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07598 if (!ast_strlen_zero(p->our_contact))
07599 add_header(&req, "Referred-By", p->our_contact);
07600
07601 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07602
07603
07604
07605
07606
07607
07608
07609
07610 }
07611
07612
07613
07614 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
07615 {
07616 struct sip_request req;
07617
07618 reqprep(&req, p, SIP_INFO, 0, 1);
07619 add_digit(&req, digit, duration);
07620 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07621 }
07622
07623
07624 static int transmit_info_with_vidupdate(struct sip_pvt *p)
07625 {
07626 struct sip_request req;
07627
07628 reqprep(&req, p, SIP_INFO, 0, 1);
07629 add_vidupdate(&req);
07630 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07631 }
07632
07633
07634
07635
07636 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
07637 {
07638 struct sip_request resp;
07639
07640 if (sipmethod == SIP_ACK)
07641 p->invitestate = INV_CONFIRMED;
07642
07643 reqprep(&resp, p, sipmethod, seqno, newbranch);
07644 add_header_contentLength(&resp, 0);
07645 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
07646 }
07647
07648
07649 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
07650 {
07651 struct sip_request resp;
07652
07653 reqprep(&resp, p, sipmethod, seqno, newbranch);
07654 if (!ast_strlen_zero(p->realm)) {
07655 char digest[1024];
07656
07657 memset(digest, 0, sizeof(digest));
07658 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
07659 if (p->options && p->options->auth_type == PROXY_AUTH)
07660 add_header(&resp, "Proxy-Authorization", digest);
07661 else if (p->options && p->options->auth_type == WWW_AUTH)
07662 add_header(&resp, "Authorization", digest);
07663 else
07664 add_header(&resp, "Proxy-Authorization", digest);
07665 } else
07666 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
07667 }
07668
07669
07670 if (sipmethod == SIP_BYE && p->owner && p->owner->hangupcause) {
07671 char buf[10];
07672
07673 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
07674 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
07675 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
07676 }
07677
07678 add_header_contentLength(&resp, 0);
07679 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
07680 }
07681
07682
07683 static void destroy_association(struct sip_peer *peer)
07684 {
07685 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE)) {
07686 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
07687 ast_update_realtime("sippeers", "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "username", "", "regserver", "", NULL);
07688 else
07689 ast_db_del("SIP/Registry", peer->name);
07690 }
07691 }
07692
07693
07694 static int expire_register(void *data)
07695 {
07696 struct sip_peer *peer = data;
07697
07698 if (!peer)
07699 return 0;
07700
07701 memset(&peer->addr, 0, sizeof(peer->addr));
07702
07703 destroy_association(peer);
07704
07705 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
07706 register_peer_exten(peer, FALSE);
07707 peer->expire = -1;
07708 ast_device_state_changed("SIP/%s", peer->name);
07709
07710
07711
07712
07713 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT) ||
07714 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
07715 peer = ASTOBJ_CONTAINER_UNLINK(&peerl, peer);
07716 ASTOBJ_UNREF(peer, sip_destroy_peer);
07717 }
07718
07719 return 0;
07720 }
07721
07722
07723 static int sip_poke_peer_s(void *data)
07724 {
07725 struct sip_peer *peer = data;
07726
07727 peer->pokeexpire = -1;
07728 sip_poke_peer(peer);
07729 return 0;
07730 }
07731
07732
07733 static void reg_source_db(struct sip_peer *peer)
07734 {
07735 char data[256];
07736 struct in_addr in;
07737 int expiry;
07738 int port;
07739 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
07740
07741 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
07742 return;
07743 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
07744 return;
07745
07746 scan = data;
07747 addr = strsep(&scan, ":");
07748 port_str = strsep(&scan, ":");
07749 expiry_str = strsep(&scan, ":");
07750 username = strsep(&scan, ":");
07751 contact = scan;
07752
07753 if (!inet_aton(addr, &in))
07754 return;
07755
07756 if (port_str)
07757 port = atoi(port_str);
07758 else
07759 return;
07760
07761 if (expiry_str)
07762 expiry = atoi(expiry_str);
07763 else
07764 return;
07765
07766 if (username)
07767 ast_copy_string(peer->username, username, sizeof(peer->username));
07768 if (contact)
07769 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
07770
07771 if (option_debug > 1)
07772 ast_log(LOG_DEBUG, "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
07773 peer->name, peer->username, ast_inet_ntoa(in), port, expiry);
07774
07775 memset(&peer->addr, 0, sizeof(peer->addr));
07776 peer->addr.sin_family = AF_INET;
07777 peer->addr.sin_addr = in;
07778 peer->addr.sin_port = htons(port);
07779 if (sipsock < 0) {
07780
07781 if (peer->pokeexpire > -1)
07782 ast_sched_del(sched, peer->pokeexpire);
07783 peer->pokeexpire = ast_sched_add(sched, ast_random() % 5000 + 1, sip_poke_peer_s, peer);
07784 } else
07785 sip_poke_peer(peer);
07786 if (peer->expire > -1)
07787 ast_sched_del(sched, peer->expire);
07788 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, peer);
07789 register_peer_exten(peer, TRUE);
07790 }
07791
07792
07793 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
07794 {
07795 char contact[BUFSIZ];
07796 char *c;
07797
07798
07799 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
07800 c = get_in_brackets(contact);
07801
07802
07803 ast_string_field_set(pvt, fullcontact, c);
07804
07805
07806 ast_string_field_set(pvt, okcontacturi, c);
07807
07808
07809
07810 return TRUE;
07811 }
07812
07813
07814 static int set_address_from_contact(struct sip_pvt *pvt)
07815 {
07816 struct hostent *hp;
07817 struct ast_hostent ahp;
07818 int port;
07819 char *c, *host, *pt;
07820 char *contact;
07821
07822
07823 if (ast_test_flag(&pvt->flags[0], SIP_NAT_ROUTE)) {
07824
07825
07826 pvt->sa = pvt->recv;
07827 return 0;
07828 }
07829
07830
07831
07832 contact = ast_strdupa(pvt->fullcontact);
07833
07834
07835 if (strncasecmp(contact, "sip:", 4)) {
07836 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
07837 } else
07838 contact += 4;
07839
07840
07841
07842
07843
07844 host = strchr(contact, '@');
07845 if (!host) {
07846 host = contact;
07847 c = NULL;
07848 } else {
07849 *host++ = '\0';
07850 }
07851 pt = strchr(host, ':');
07852 if (pt) {
07853 *pt++ = '\0';
07854 port = atoi(pt);
07855 } else
07856 port = STANDARD_SIP_PORT;
07857
07858 contact = strsep(&contact, ";");
07859 host = strsep(&host, ";");
07860
07861
07862
07863 hp = ast_gethostbyname(host, &ahp);
07864 if (!hp) {
07865 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't resolve in DNS) : '%s'\n", host);
07866 return -1;
07867 }
07868 pvt->sa.sin_family = AF_INET;
07869 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
07870 pvt->sa.sin_port = htons(port);
07871
07872 return 0;
07873 }
07874
07875
07876
07877 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
07878 {
07879 char contact[BUFSIZ];
07880 char data[BUFSIZ];
07881 const char *expires = get_header(req, "Expires");
07882 int expiry = atoi(expires);
07883 char *curi, *n, *pt;
07884 int port;
07885 const char *useragent;
07886 struct hostent *hp;
07887 struct ast_hostent ahp;
07888 struct sockaddr_in oldsin;
07889
07890 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
07891
07892 if (ast_strlen_zero(expires)) {
07893 expires = strcasestr(contact, ";expires=");
07894 if (expires) {
07895
07896 expires = strsep((char **) &expires, ";");
07897 if (sscanf(expires + 9, "%d", &expiry) != 1)
07898 expiry = default_expiry;
07899 } else {
07900
07901 expiry = default_expiry;
07902 }
07903 }
07904
07905
07906 curi = contact;
07907 if (strchr(contact, '<') == NULL)
07908 strsep(&curi, ";");
07909 curi = get_in_brackets(contact);
07910
07911
07912
07913
07914
07915 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
07916
07917 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact))
07918 pvt->expiry = ast_sched_when(sched, peer->expire);
07919 return PARSE_REGISTER_QUERY;
07920 } else if (!strcasecmp(curi, "*") || !expiry) {
07921
07922 memset(&peer->addr, 0, sizeof(peer->addr));
07923 if (peer->expire > -1)
07924 ast_sched_del(sched, peer->expire);
07925 peer->expire = -1;
07926
07927 destroy_association(peer);
07928
07929 register_peer_exten(peer, 0);
07930 peer->fullcontact[0] = '\0';
07931 peer->useragent[0] = '\0';
07932 peer->sipoptions = 0;
07933 peer->lastms = 0;
07934
07935 if (option_verbose > 2)
07936 ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", peer->name);
07937 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\n", peer->name);
07938 return PARSE_REGISTER_UPDATE;
07939 }
07940
07941
07942 ast_copy_string(peer->fullcontact, curi, sizeof(peer->fullcontact));
07943
07944
07945 ast_string_field_build(pvt, our_contact, "<%s>", curi);
07946
07947
07948 if (strncasecmp(curi, "sip:", 4)) {
07949 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", curi);
07950 } else
07951 curi += 4;
07952
07953 curi = strsep(&curi, ";");
07954
07955 n = strchr(curi, '@');
07956 if (!n) {
07957 n = curi;
07958 curi = NULL;
07959 } else
07960 *n++ = '\0';
07961 pt = strchr(n, ':');
07962 if (pt) {
07963 *pt++ = '\0';
07964 port = atoi(pt);
07965 } else
07966 port = STANDARD_SIP_PORT;
07967 oldsin = peer->addr;
07968 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
07969
07970 hp = ast_gethostbyname(n, &ahp);
07971 if (!hp) {
07972 ast_log(LOG_WARNING, "Invalid host '%s'\n", n);
07973 return PARSE_REGISTER_FAILED;
07974 }
07975 peer->addr.sin_family = AF_INET;
07976 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
07977 peer->addr.sin_port = htons(port);
07978 } else {
07979
07980
07981 peer->addr = pvt->recv;
07982 }
07983
07984
07985 peer->sipoptions = pvt->sipoptions;
07986
07987 if (curi && ast_strlen_zero(peer->username))
07988 ast_copy_string(peer->username, curi, sizeof(peer->username));
07989
07990 if (peer->expire > -1) {
07991 ast_sched_del(sched, peer->expire);
07992 peer->expire = -1;
07993 }
07994 if (expiry > max_expiry)
07995 expiry = max_expiry;
07996 if (expiry < min_expiry)
07997 expiry = min_expiry;
07998 peer->expire = ast_test_flag(&peer->flags[0], SIP_REALTIME) ? -1 :
07999 ast_sched_add(sched, (expiry + 10) * 1000, expire_register, peer);
08000 pvt->expiry = expiry;
08001 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);
08002 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
08003 ast_db_put("SIP/Registry", peer->name, data);
08004 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
08005
08006
08007 if (inaddrcmp(&peer->addr, &oldsin)) {
08008 sip_poke_peer(peer);
08009 if (option_verbose > 2)
08010 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);
08011 register_peer_exten(peer, 1);
08012 }
08013
08014
08015 useragent = get_header(req, "User-Agent");
08016 if (strcasecmp(useragent, peer->useragent)) {
08017 ast_copy_string(peer->useragent, useragent, sizeof(peer->useragent));
08018 if (option_verbose > 3)
08019 ast_verbose(VERBOSE_PREFIX_3 "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
08020 }
08021 return PARSE_REGISTER_UPDATE;
08022 }
08023
08024
08025 static void free_old_route(struct sip_route *route)
08026 {
08027 struct sip_route *next;
08028
08029 while (route) {
08030 next = route->next;
08031 free(route);
08032 route = next;
08033 }
08034 }
08035
08036
08037 static void list_route(struct sip_route *route)
08038 {
08039 if (!route)
08040 ast_verbose("list_route: no route\n");
08041 else {
08042 for (;route; route = route->next)
08043 ast_verbose("list_route: hop: <%s>\n", route->hop);
08044 }
08045 }
08046
08047
08048 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
08049 {
08050 struct sip_route *thishop, *head, *tail;
08051 int start = 0;
08052 int len;
08053 const char *rr, *contact, *c;
08054
08055
08056 if (p->route && p->route_persistant) {
08057 if (option_debug)
08058 ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
08059 return;
08060 }
08061
08062 if (p->route) {
08063 free_old_route(p->route);
08064 p->route = NULL;
08065 }
08066
08067 p->route_persistant = backwards;
08068
08069
08070
08071
08072
08073
08074 head = NULL;
08075 tail = head;
08076
08077 for (;;) {
08078
08079 rr = __get_header(req, "Record-Route", &start);
08080 if (*rr == '\0')
08081 break;
08082 for (; (rr = strchr(rr, '<')) ; rr += len) {
08083 ++rr;
08084 len = strcspn(rr, ">") + 1;
08085
08086 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
08087
08088 ast_copy_string(thishop->hop, rr, len);
08089 if (option_debug > 1)
08090 ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
08091
08092 if (backwards) {
08093
08094 thishop->next = head;
08095 head = thishop;
08096
08097 if (!tail)
08098 tail = thishop;
08099 } else {
08100 thishop->next = NULL;
08101
08102 if (tail)
08103 tail->next = thishop;
08104 else
08105 head = thishop;
08106 tail = thishop;
08107 }
08108 }
08109 }
08110 }
08111
08112
08113 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop,";lr") == NULL) ) {
08114
08115
08116 contact = get_header(req, "Contact");
08117 if (!ast_strlen_zero(contact)) {
08118 if (option_debug > 1)
08119 ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
08120
08121 c = strchr(contact, '<');
08122 if (c) {
08123
08124 ++c;
08125 len = strcspn(c, ">") + 1;
08126 } else {
08127
08128 c = contact;
08129 len = strlen(contact) + 1;
08130 }
08131 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
08132
08133 ast_copy_string(thishop->hop, c, len);
08134 thishop->next = NULL;
08135
08136 if (tail)
08137 tail->next = thishop;
08138 else
08139 head = thishop;
08140 }
08141 }
08142 }
08143
08144
08145 p->route = head;
08146
08147
08148 if (sip_debug_test_pvt(p))
08149 list_route(p->route);
08150 }
08151
08152
08153
08154
08155
08156
08157
08158 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
08159 const char *secret, const char *md5secret, int sipmethod,
08160 char *uri, enum xmittype reliable, int ignore)
08161 {
08162 const char *response = "407 Proxy Authentication Required";
08163 const char *reqheader = "Proxy-Authorization";
08164 const char *respheader = "Proxy-Authenticate";
08165 const char *authtoken;
08166 char a1_hash[256];
08167 char resp_hash[256]="";
08168 char tmp[BUFSIZ * 2];
08169 char *c;
08170 int wrongnonce = FALSE;
08171 int good_response;
08172 const char *usednonce = p->randdata;
08173
08174
08175 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
08176 struct x {
08177 const char *key;
08178 const char *s;
08179 } *i, keys[] = {
08180 [K_RESP] = { "response=", "" },
08181 [K_URI] = { "uri=", "" },
08182 [K_USER] = { "username=", "" },
08183 [K_NONCE] = { "nonce=", "" },
08184 [K_LAST] = { NULL, NULL}
08185 };
08186
08187
08188 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
08189 return AUTH_SUCCESSFUL;
08190 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
08191
08192
08193
08194 response = "401 Unauthorized";
08195 reqheader = "Authorization";
08196 respheader = "WWW-Authenticate";
08197 }
08198 authtoken = get_header(req, reqheader);
08199 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
08200
08201
08202 if (!reliable) {
08203
08204
08205 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08206
08207 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08208 }
08209 return AUTH_CHALLENGE_SENT;
08210 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
08211
08212 ast_string_field_build(p, randdata, "%08lx", ast_random());
08213 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08214
08215 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08216 return AUTH_CHALLENGE_SENT;
08217 }
08218
08219
08220
08221
08222
08223
08224
08225
08226 ast_copy_string(tmp, authtoken, sizeof(tmp));
08227 c = tmp;
08228
08229 while(c && *(c = ast_skip_blanks(c)) ) {
08230 for (i = keys; i->key != NULL; i++) {
08231 const char *separator = ",";
08232
08233 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
08234 continue;
08235
08236 c += strlen(i->key);
08237 if (*c == '"') {
08238 c++;
08239 separator = "\"";
08240 }
08241 i->s = c;
08242 strsep(&c, separator);
08243 break;
08244 }
08245 if (i->key == NULL)
08246 strsep(&c, " ,");
08247 }
08248
08249
08250 if (strcmp(username, keys[K_USER].s)) {
08251 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
08252 username, keys[K_USER].s);
08253
08254 return AUTH_USERNAME_MISMATCH;
08255 }
08256
08257
08258 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
08259 wrongnonce = TRUE;
08260 usednonce = keys[K_NONCE].s;
08261 }
08262
08263 if (!ast_strlen_zero(md5secret))
08264 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
08265 else {
08266 char a1[256];
08267 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
08268 ast_md5_hash(a1_hash, a1);
08269 }
08270
08271
08272 {
08273 char a2[256];
08274 char a2_hash[256];
08275 char resp[256];
08276
08277 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
08278 S_OR(keys[K_URI].s, uri));
08279 ast_md5_hash(a2_hash, a2);
08280 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
08281 ast_md5_hash(resp_hash, resp);
08282 }
08283
08284 good_response = keys[K_RESP].s &&
08285 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
08286 if (wrongnonce) {
08287 ast_string_field_build(p, randdata, "%08lx", ast_random());
08288 if (good_response) {
08289 if (sipdebug)
08290 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "To"));
08291
08292 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
08293 } else {
08294
08295 if (sipdebug)
08296 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
08297 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
08298 }
08299
08300
08301 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08302 return AUTH_CHALLENGE_SENT;
08303 }
08304 if (good_response) {
08305 append_history(p, "AuthOK", "Auth challenge succesful for %s", username);
08306 return AUTH_SUCCESSFUL;
08307 }
08308
08309
08310
08311 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08312 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08313
08314 return AUTH_CHALLENGE_SENT;
08315 }
08316
08317
08318 static void sip_peer_hold(struct sip_pvt *p, int hold)
08319 {
08320 struct sip_peer *peer = find_peer(p->peername, NULL, 1);
08321
08322 if (!peer)
08323 return;
08324
08325
08326 if (hold)
08327 peer->onHold++;
08328 else
08329 peer->onHold--;
08330
08331
08332 ast_device_state_changed("SIP/%s", peer->name);
08333
08334 return;
08335 }
08336
08337
08338
08339
08340 static int cb_extensionstate(char *context, char* exten, int state, void *data)
08341 {
08342 struct sip_pvt *p = data;
08343
08344 ast_mutex_lock(&p->lock);
08345
08346 switch(state) {
08347 case AST_EXTENSION_DEACTIVATED:
08348 case AST_EXTENSION_REMOVED:
08349 if (p->autokillid > -1)
08350 sip_cancel_destroy(p);
08351 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08352 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);
08353 p->stateid = -1;
08354 p->subscribed = NONE;
08355 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
08356 break;
08357 default:
08358 p->laststate = state;
08359 break;
08360 }
08361 if (p->subscribed != NONE)
08362 transmit_state_notify(p, state, 1, FALSE);
08363
08364 if (option_verbose > 1)
08365 ast_verbose(VERBOSE_PREFIX_1 "Extension Changed %s new state %s for Notify User %s\n", exten, ast_extension_state2str(state), p->username);
08366
08367 ast_mutex_unlock(&p->lock);
08368
08369 return 0;
08370 }
08371
08372
08373
08374
08375 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, int reliable)
08376 {
08377 ast_string_field_build(p, randdata, "%08lx", ast_random());
08378 transmit_response_with_auth(p, "401 Unauthorized", req, p->randdata, reliable, "WWW-Authenticate", 0);
08379 }
08380
08381
08382
08383
08384
08385
08386 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
08387 struct sip_request *req, char *uri)
08388 {
08389 enum check_auth_result res = AUTH_NOT_FOUND;
08390 struct sip_peer *peer;
08391 char tmp[256];
08392 char *name, *c;
08393 char *t;
08394 char *domain;
08395
08396
08397 t = uri;
08398 while(*t && (*t > 32) && (*t != ';'))
08399 t++;
08400 *t = '\0';
08401
08402 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
08403 if (pedanticsipchecking)
08404 ast_uri_decode(tmp);
08405
08406 c = get_in_brackets(tmp);
08407 c = strsep(&c, ";");
08408
08409 if (!strncasecmp(c, "sip:", 4)) {
08410 name = c + 4;
08411 } else {
08412 name = c;
08413 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(sin->sin_addr));
08414 }
08415
08416
08417 if ((c = strchr(name, '@'))) {
08418 *c++ = '\0';
08419 domain = c;
08420 if ((c = strchr(domain, ':')))
08421 *c = '\0';
08422 if (!AST_LIST_EMPTY(&domain_list)) {
08423 if (!check_sip_domain(domain, NULL, 0)) {
08424 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
08425 return AUTH_UNKNOWN_DOMAIN;
08426 }
08427 }
08428 }
08429
08430 ast_string_field_set(p, exten, name);
08431 build_contact(p);
08432 peer = find_peer(name, NULL, 1);
08433 if (!(peer && ast_apply_ha(peer->ha, sin))) {
08434
08435 if (peer) {
08436 ASTOBJ_UNREF(peer, sip_destroy_peer);
08437 peer = NULL;
08438 res = AUTH_ACL_FAILED;
08439 } else
08440 res = AUTH_NOT_FOUND;
08441 }
08442 if (peer) {
08443
08444 if (p->rtp) {
08445 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
08446 p->autoframing = peer->autoframing;
08447 }
08448 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
08449 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
08450 res = AUTH_PEER_NOT_DYNAMIC;
08451 } else {
08452 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
08453 transmit_response(p, "100 Trying", req);
08454 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, ast_test_flag(req, SIP_PKT_IGNORE)))) {
08455 sip_cancel_destroy(p);
08456
08457
08458
08459 switch (parse_register_contact(p, peer, req)) {
08460 case PARSE_REGISTER_FAILED:
08461 ast_log(LOG_WARNING, "Failed to parse contact info\n");
08462 transmit_response_with_date(p, "400 Bad Request", req);
08463 peer->lastmsgssent = -1;
08464 res = 0;
08465 break;
08466 case PARSE_REGISTER_QUERY:
08467 transmit_response_with_date(p, "200 OK", req);
08468 peer->lastmsgssent = -1;
08469 res = 0;
08470 break;
08471 case PARSE_REGISTER_UPDATE:
08472 update_peer(peer, p->expiry);
08473
08474 transmit_response_with_date(p, "200 OK", req);
08475 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
08476 peer->lastmsgssent = -1;
08477 res = 0;
08478 break;
08479 }
08480 }
08481 }
08482 }
08483 if (!peer && autocreatepeer) {
08484
08485 peer = temp_peer(name);
08486 if (peer) {
08487 ASTOBJ_CONTAINER_LINK(&peerl, peer);
08488 sip_cancel_destroy(p);
08489 switch (parse_register_contact(p, peer, req)) {
08490 case PARSE_REGISTER_FAILED:
08491 ast_log(LOG_WARNING, "Failed to parse contact info\n");
08492 transmit_response_with_date(p, "400 Bad Request", req);
08493 peer->lastmsgssent = -1;
08494 res = 0;
08495 break;
08496 case PARSE_REGISTER_QUERY:
08497 transmit_response_with_date(p, "200 OK", req);
08498 peer->lastmsgssent = -1;
08499 res = 0;
08500 break;
08501 case PARSE_REGISTER_UPDATE:
08502
08503 transmit_response_with_date(p, "200 OK", req);
08504 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
08505 peer->lastmsgssent = -1;
08506 res = 0;
08507 break;
08508 }
08509 }
08510 }
08511 if (!res) {
08512 ast_device_state_changed("SIP/%s", peer->name);
08513 }
08514 if (res < 0) {
08515 switch (res) {
08516 case AUTH_SECRET_FAILED:
08517
08518 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
08519 break;
08520 case AUTH_USERNAME_MISMATCH:
08521
08522
08523
08524
08525 transmit_response(p, "403 Authentication user name does not match account name", &p->initreq);
08526 break;
08527 case AUTH_NOT_FOUND:
08528 case AUTH_PEER_NOT_DYNAMIC:
08529 case AUTH_ACL_FAILED:
08530 if (global_alwaysauthreject) {
08531 transmit_fake_auth_response(p, &p->initreq, 1);
08532 } else {
08533
08534 if (res == AUTH_UNKNOWN_DOMAIN || res == AUTH_PEER_NOT_DYNAMIC)
08535 transmit_response(p, "403 Forbidden", &p->initreq);
08536 else
08537 transmit_response(p, "404 Not found", &p->initreq);
08538 }
08539 break;
08540 default:
08541 break;
08542 }
08543 }
08544 if (peer)
08545 ASTOBJ_UNREF(peer, sip_destroy_peer);
08546
08547 return res;
08548 }
08549
08550
08551 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
08552 {
08553 char tmp[256], *c, *a;
08554 struct sip_request *req;
08555
08556 req = oreq;
08557 if (!req)
08558 req = &p->initreq;
08559 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
08560 if (ast_strlen_zero(tmp))
08561 return 0;
08562 c = get_in_brackets(tmp);
08563 if (strncasecmp(c, "sip:", 4)) {
08564 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", c);
08565 return -1;
08566 }
08567 c += 4;
08568 a = c;
08569 strsep(&a, "@;");
08570 if (sip_debug_test_pvt(p))
08571 ast_verbose("RDNIS is %s\n", c);
08572 ast_string_field_set(p, rdnis, c);
08573
08574 return 0;
08575 }
08576
08577
08578
08579
08580 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
08581 {
08582 char tmp[256] = "", *uri, *a;
08583 char tmpf[256] = "", *from;
08584 struct sip_request *req;
08585 char *colon;
08586
08587 req = oreq;
08588 if (!req)
08589 req = &p->initreq;
08590
08591
08592 if (req->rlPart2)
08593 ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
08594
08595 if (pedanticsipchecking)
08596 ast_uri_decode(tmp);
08597
08598 uri = get_in_brackets(tmp);
08599
08600 if (strncasecmp(uri, "sip:", 4)) {
08601 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
08602 return -1;
08603 }
08604 uri += 4;
08605
08606
08607 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
08608 if (!ast_strlen_zero(tmpf)) {
08609 if (pedanticsipchecking)
08610 ast_uri_decode(tmpf);
08611 from = get_in_brackets(tmpf);
08612 } else {
08613 from = NULL;
08614 }
08615
08616 if (!ast_strlen_zero(from)) {
08617 if (strncasecmp(from, "sip:", 4)) {
08618 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
08619 return -1;
08620 }
08621 from += 4;
08622 if ((a = strchr(from, '@')))
08623 *a++ = '\0';
08624 else
08625 a = from;
08626 from = strsep(&from, ";");
08627 a = strsep(&a, ";");
08628 ast_string_field_set(p, fromdomain, a);
08629 }
08630
08631
08632
08633
08634 if ((a = strchr(uri, '@'))) {
08635 *a++ = '\0';
08636 } else {
08637 a = uri;
08638 uri = "s";
08639 }
08640 colon = strchr(a, ':');
08641 if (colon)
08642 *colon = '\0';
08643
08644 uri = strsep(&uri, ";");
08645 a = strsep(&a, ";");
08646
08647 ast_string_field_set(p, domain, a);
08648
08649 if (!AST_LIST_EMPTY(&domain_list)) {
08650 char domain_context[AST_MAX_EXTENSION];
08651
08652 domain_context[0] = '\0';
08653 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
08654 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
08655 if (option_debug)
08656 ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
08657 return -2;
08658 }
08659 }
08660
08661 if (!ast_strlen_zero(domain_context))
08662 ast_string_field_set(p, context, domain_context);
08663 }
08664
08665 if (sip_debug_test_pvt(p))
08666 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
08667
08668
08669
08670
08671 if (ast_exists_extension(NULL, p->context, uri, 1, from) ||
08672 !strcmp(uri, ast_pickup_ext())) {
08673 if (!oreq)
08674 ast_string_field_set(p, exten, uri);
08675 return 0;
08676 }
08677
08678
08679 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
08680 ast_canmatch_extension(NULL, p->context, uri, 1, from)) ||
08681 !strncmp(uri, ast_pickup_ext(), strlen(uri))) {
08682 return 1;
08683 }
08684
08685 return -1;
08686 }
08687
08688
08689
08690
08691
08692
08693 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
08694 {
08695 struct sip_pvt *sip_pvt_ptr;
08696
08697 ast_mutex_lock(&iflock);
08698
08699 if (option_debug > 3 && totag)
08700 ast_log(LOG_DEBUG, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
08701
08702
08703 for (sip_pvt_ptr = iflist; sip_pvt_ptr; sip_pvt_ptr = sip_pvt_ptr->next) {
08704 if (!strcmp(sip_pvt_ptr->callid, callid)) {
08705 int match = 1;
08706 char *ourtag = sip_pvt_ptr->tag;
08707
08708
08709 ast_mutex_lock(&sip_pvt_ptr->lock);
08710
08711
08712
08713
08714
08715 if (pedanticsipchecking && (strcmp(fromtag, sip_pvt_ptr->theirtag) || (!ast_strlen_zero(totag) && strcmp(totag, ourtag))))
08716 match = 0;
08717
08718 if (!match) {
08719 ast_mutex_unlock(&sip_pvt_ptr->lock);
08720 continue;
08721 }
08722
08723 if (option_debug > 3 && totag)
08724 ast_log(LOG_DEBUG, "Matched %s call - their tag is %s Our tag is %s\n",
08725 ast_test_flag(&sip_pvt_ptr->flags[0], SIP_OUTGOING) ? "OUTGOING": "INCOMING",
08726 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
08727
08728
08729 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
08730 ast_mutex_unlock(&sip_pvt_ptr->lock);
08731 usleep(1);
08732 ast_mutex_lock(&sip_pvt_ptr->lock);
08733 }
08734 break;
08735 }
08736 }
08737 ast_mutex_unlock(&iflock);
08738 if (option_debug > 3 && !sip_pvt_ptr)
08739 ast_log(LOG_DEBUG, "Found no match for callid %s to-tag %s from-tag %s\n", callid, totag, fromtag);
08740 return sip_pvt_ptr;
08741 }
08742
08743
08744
08745 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
08746 {
08747
08748 const char *p_referred_by = NULL;
08749 char *h_refer_to = NULL;
08750 char *h_referred_by = NULL;
08751 char *refer_to;
08752 const char *p_refer_to;
08753 char *referred_by_uri = NULL;
08754 char *ptr;
08755 struct sip_request *req = NULL;
08756 const char *transfer_context = NULL;
08757 struct sip_refer *referdata;
08758
08759
08760 req = outgoing_req;
08761 referdata = transferer->refer;
08762
08763 if (!req)
08764 req = &transferer->initreq;
08765
08766 p_refer_to = get_header(req, "Refer-To");
08767 if (ast_strlen_zero(p_refer_to)) {
08768 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
08769 return -2;
08770 }
08771 h_refer_to = ast_strdupa(p_refer_to);
08772 refer_to = get_in_brackets(h_refer_to);
08773 if (pedanticsipchecking)
08774 ast_uri_decode(refer_to);
08775
08776 if (strncasecmp(refer_to, "sip:", 4)) {
08777 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
08778 return -3;
08779 }
08780 refer_to += 4;
08781
08782
08783 p_referred_by = get_header(req, "Referred-By");
08784 if (!ast_strlen_zero(p_referred_by)) {
08785 char *lessthan;
08786 h_referred_by = ast_strdupa(p_referred_by);
08787 if (pedanticsipchecking)
08788 ast_uri_decode(h_referred_by);
08789
08790
08791 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
08792 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
08793 *(lessthan - 1) = '\0';
08794 }
08795
08796 referred_by_uri = get_in_brackets(h_referred_by);
08797 if(strncasecmp(referred_by_uri, "sip:", 4)) {
08798 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
08799 referred_by_uri = (char *) NULL;
08800 } else {
08801 referred_by_uri += 4;
08802 }
08803 }
08804
08805
08806 if ((ptr = strchr(refer_to, '?'))) {
08807 *ptr++ = '\0';
08808 if (!strncasecmp(ptr, "REPLACES=", 9)) {
08809 char *to = NULL, *from = NULL;
08810
08811
08812 referdata->attendedtransfer = 1;
08813 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
08814 ast_uri_decode(referdata->replaces_callid);
08815 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
08816 *ptr++ = '\0';
08817 }
08818
08819 if (ptr) {
08820
08821 to = strcasestr(ptr, "to-tag=");
08822 from = strcasestr(ptr, "from-tag=");
08823 }
08824
08825
08826 if (to) {
08827 ptr = to + 7;
08828 if ((to = strchr(ptr, '&')))
08829 *to = '\0';
08830 if ((to = strchr(ptr, ';')))
08831 *to = '\0';
08832 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
08833 }
08834
08835 if (from) {
08836 ptr = from + 9;
08837 if ((to = strchr(ptr, '&')))
08838 *to = '\0';
08839 if ((to = strchr(ptr, ';')))
08840 *to = '\0';
08841 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
08842 }
08843
08844 if (option_debug > 1) {
08845 if (!pedanticsipchecking)
08846 ast_log(LOG_DEBUG,"Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
08847 else
08848 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>" );
08849 }
08850 }
08851 }
08852
08853 if ((ptr = strchr(refer_to, '@'))) {
08854 char *urioption;
08855
08856 *ptr++ = '\0';
08857 if ((urioption = strchr(ptr, ';')))
08858 *urioption++ = '\0';
08859
08860 ast_copy_string(referdata->refer_to_domain, ptr, sizeof(referdata->refer_to_domain));
08861 if (urioption)
08862 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
08863 }
08864
08865 if ((ptr = strchr(refer_to, ';')))
08866 *ptr = '\0';
08867 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
08868
08869 if (referred_by_uri) {
08870 if ((ptr = strchr(referred_by_uri, ';')))
08871 *ptr = '\0';
08872 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
08873 } else {
08874 referdata->referred_by[0] = '\0';
08875 }
08876
08877
08878 if (transferer->owner)
08879 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
08880
08881
08882 if (ast_strlen_zero(transfer_context)) {
08883 transfer_context = S_OR(transferer->owner->macrocontext,
08884 S_OR(transferer->context, default_context));
08885 }
08886
08887 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
08888
08889
08890 if (ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
08891 if (sip_debug_test_pvt(transferer)) {
08892 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
08893 }
08894
08895 return 0;
08896 }
08897 if (sip_debug_test_pvt(transferer))
08898 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
08899
08900
08901 return -1;
08902 }
08903
08904
08905
08906 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
08907 {
08908 char tmp[256] = "", *c, *a;
08909 struct sip_request *req = oreq ? oreq : &p->initreq;
08910 struct sip_refer *referdata = p->refer;
08911 const char *transfer_context = NULL;
08912
08913 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
08914 c = get_in_brackets(tmp);
08915
08916 if (pedanticsipchecking)
08917 ast_uri_decode(c);
08918
08919 if (strncasecmp(c, "sip:", 4)) {
08920 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
08921 return -1;
08922 }
08923 c += 4;
08924 if ((a = strchr(c, ';')))
08925 *a = '\0';
08926
08927 if ((a = strchr(c, '@'))) {
08928 *a++ = '\0';
08929 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
08930 }
08931
08932 if (sip_debug_test_pvt(p))
08933 ast_verbose("Looking for %s in %s\n", c, p->context);
08934
08935 if (p->owner)
08936 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
08937
08938
08939 if (ast_strlen_zero(transfer_context)) {
08940 transfer_context = S_OR(p->owner->macrocontext,
08941 S_OR(p->context, default_context));
08942 }
08943 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
08944
08945 if (option_debug)
08946 ast_log(LOG_DEBUG,"SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
08947 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
08948 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
08949 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
08950 referdata->refer_call = NULL;
08951
08952 ast_string_field_set(p, context, transfer_context);
08953 return 0;
08954 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
08955 return 1;
08956 }
08957
08958 return -1;
08959 }
08960
08961 static void check_via(struct sip_pvt *p, struct sip_request *req)
08962 {
08963 char via[256];
08964 char *c, *pt;
08965 struct hostent *hp;
08966 struct ast_hostent ahp;
08967
08968 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
08969
08970
08971 c = strchr(via, ',');
08972 if (c)
08973 *c = '\0';
08974
08975
08976 c = strstr(via, ";rport");
08977 if (c && (c[6] != '='))
08978 ast_set_flag(&p->flags[0], SIP_NAT_ROUTE);
08979
08980 c = strchr(via, ';');
08981 if (c)
08982 *c = '\0';
08983
08984 c = strchr(via, ' ');
08985 if (c) {
08986 *c = '\0';
08987 c = ast_skip_blanks(c+1);
08988 if (strcasecmp(via, "SIP/2.0/UDP")) {
08989 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
08990 return;
08991 }
08992 pt = strchr(c, ':');
08993 if (pt)
08994 *pt++ = '\0';
08995 hp = ast_gethostbyname(c, &ahp);
08996 if (!hp) {
08997 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
08998 return;
08999 }
09000 memset(&p->sa, 0, sizeof(p->sa));
09001 p->sa.sin_family = AF_INET;
09002 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
09003 p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
09004
09005 if (sip_debug_test_pvt(p)) {
09006 const struct sockaddr_in *dst = sip_real_dst(p);
09007 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
09008 }
09009 }
09010 }
09011
09012
09013 static char *get_calleridname(const char *input, char *output, size_t outputsize)
09014 {
09015 const char *end = strchr(input,'<');
09016 const char *tmp = strchr(input,'"');
09017 int bytes = 0;
09018 int maxbytes = outputsize - 1;
09019
09020 if (!end || end == input)
09021 return NULL;
09022
09023 end--;
09024
09025 if (tmp && tmp <= end) {
09026
09027
09028
09029 end = strchr(tmp+1, '"');
09030 if (!end)
09031 return NULL;
09032 bytes = (int) (end - tmp);
09033
09034 if (bytes > maxbytes)
09035 bytes = maxbytes;
09036 ast_copy_string(output, tmp + 1, bytes);
09037 } else {
09038
09039
09040 input = ast_skip_blanks(input);
09041
09042 while(*end && *end < 33 && end > input)
09043 end--;
09044 if (end >= input) {
09045 bytes = (int) (end - input) + 2;
09046
09047 if (bytes > maxbytes)
09048 bytes = maxbytes;
09049 ast_copy_string(output, input, bytes);
09050 } else
09051 return NULL;
09052 }
09053 return output;
09054 }
09055
09056
09057
09058
09059
09060 static int get_rpid_num(const char *input, char *output, int maxlen)
09061 {
09062 char *start;
09063 char *end;
09064
09065 start = strchr(input,':');
09066 if (!start) {
09067 output[0] = '\0';
09068 return 0;
09069 }
09070 start++;
09071
09072
09073 ast_copy_string(output,start,maxlen);
09074 output[maxlen-1] = '\0';
09075
09076 end = strchr(output,'@');
09077 if (end)
09078 *end = '\0';
09079 else
09080 output[0] = '\0';
09081 if (strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
09082 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
09083
09084 return 0;
09085 }
09086
09087
09088
09089
09090
09091
09092
09093 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
09094 int sipmethod, char *uri, enum xmittype reliable,
09095 struct sockaddr_in *sin, struct sip_peer **authpeer)
09096 {
09097 struct sip_user *user = NULL;
09098 struct sip_peer *peer;
09099 char from[256], *c;
09100 char *of;
09101 char rpid_num[50];
09102 const char *rpid;
09103 enum check_auth_result res = AUTH_SUCCESSFUL;
09104 char *t;
09105 char calleridname[50];
09106 int debug=sip_debug_test_addr(sin);
09107 struct ast_variable *tmpvar = NULL, *v = NULL;
09108 char *uri2 = ast_strdupa(uri);
09109
09110
09111 t = uri2;
09112 while (*t && *t > 32 && *t != ';')
09113 t++;
09114 *t = '\0';
09115 ast_copy_string(from, get_header(req, "From"), sizeof(from));
09116 if (pedanticsipchecking)
09117 ast_uri_decode(from);
09118
09119 memset(calleridname, 0, sizeof(calleridname));
09120 get_calleridname(from, calleridname, sizeof(calleridname));
09121 if (calleridname[0])
09122 ast_string_field_set(p, cid_name, calleridname);
09123
09124 rpid = get_header(req, "Remote-Party-ID");
09125 memset(rpid_num, 0, sizeof(rpid_num));
09126 if (!ast_strlen_zero(rpid))
09127 p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
09128
09129 of = get_in_brackets(from);
09130 if (ast_strlen_zero(p->exten)) {
09131 t = uri2;
09132 if (!strncasecmp(t, "sip:", 4))
09133 t+= 4;
09134 ast_string_field_set(p, exten, t);
09135 t = strchr(p->exten, '@');
09136 if (t)
09137 *t = '\0';
09138 if (ast_strlen_zero(p->our_contact))
09139 build_contact(p);
09140 }
09141
09142 ast_string_field_set(p, from, of);
09143 if (strncasecmp(of, "sip:", 4)) {
09144 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
09145 } else
09146 of += 4;
09147
09148 if ((c = strchr(of, '@'))) {
09149 char *tmp;
09150 *c = '\0';
09151 if ((c = strchr(of, ':')))
09152 *c = '\0';
09153 tmp = ast_strdupa(of);
09154
09155
09156
09157 tmp = strsep(&tmp, ";");
09158 if (ast_is_shrinkable_phonenumber(tmp))
09159 ast_shrink_phone_number(tmp);
09160 ast_string_field_set(p, cid_num, tmp);
09161 }
09162 if (ast_strlen_zero(of))
09163 return AUTH_SUCCESSFUL;
09164
09165 if (!authpeer)
09166 user = find_user(of, 1);
09167
09168
09169 if (user && ast_apply_ha(user->ha, sin)) {
09170 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
09171 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09172
09173 for (v = user->chanvars ; v ; v = v->next) {
09174 if ((tmpvar = ast_variable_new(v->name, v->value))) {
09175 tmpvar->next = p->chanvars;
09176 p->chanvars = tmpvar;
09177 }
09178 }
09179 p->prefs = user->prefs;
09180
09181 if (p->rtp) {
09182 ast_rtp_codec_setpref(p->rtp, &p->prefs);
09183 p->autoframing = user->autoframing;
09184 }
09185
09186 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09187 char *tmp;
09188 if (*calleridname)
09189 ast_string_field_set(p, cid_name, calleridname);
09190 tmp = ast_strdupa(rpid_num);
09191 if (ast_is_shrinkable_phonenumber(tmp))
09192 ast_shrink_phone_number(tmp);
09193 ast_string_field_set(p, cid_num, tmp);
09194 }
09195
09196 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
09197
09198 if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09199 sip_cancel_destroy(p);
09200 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
09201 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09202
09203 if (p->sipoptions)
09204 user->sipoptions = p->sipoptions;
09205
09206
09207 if (user->call_limit)
09208 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
09209 if (!ast_strlen_zero(user->context))
09210 ast_string_field_set(p, context, user->context);
09211 if (!ast_strlen_zero(user->cid_num) && !ast_strlen_zero(p->cid_num)) {
09212 char *tmp = ast_strdupa(user->cid_num);
09213 if (ast_is_shrinkable_phonenumber(tmp))
09214 ast_shrink_phone_number(tmp);
09215 ast_string_field_set(p, cid_num, tmp);
09216 }
09217 if (!ast_strlen_zero(user->cid_name) && !ast_strlen_zero(p->cid_num))
09218 ast_string_field_set(p, cid_name, user->cid_name);
09219 ast_string_field_set(p, username, user->name);
09220 ast_string_field_set(p, peername, user->name);
09221 ast_string_field_set(p, peersecret, user->secret);
09222 ast_string_field_set(p, peermd5secret, user->md5secret);
09223 ast_string_field_set(p, subscribecontext, user->subscribecontext);
09224 ast_string_field_set(p, accountcode, user->accountcode);
09225 ast_string_field_set(p, language, user->language);
09226 ast_string_field_set(p, mohsuggest, user->mohsuggest);
09227 ast_string_field_set(p, mohinterpret, user->mohinterpret);
09228 p->allowtransfer = user->allowtransfer;
09229 p->amaflags = user->amaflags;
09230 p->callgroup = user->callgroup;
09231 p->pickupgroup = user->pickupgroup;
09232 if (user->callingpres)
09233 p->callingpres = user->callingpres;
09234
09235
09236 p->capability = user->capability;
09237 p->jointcapability = user->capability;
09238 if (p->peercapability)
09239 p->jointcapability &= p->peercapability;
09240 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
09241 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
09242 p->noncodeccapability |= AST_RTP_DTMF;
09243 else
09244 p->noncodeccapability &= ~AST_RTP_DTMF;
09245 p->jointnoncodeccapability = p->noncodeccapability;
09246 if (p->t38.peercapability)
09247 p->t38.jointcapability &= p->t38.peercapability;
09248 p->maxcallbitrate = user->maxcallbitrate;
09249
09250 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
09251 ast_rtp_destroy(p->vrtp);
09252 p->vrtp = NULL;
09253 }
09254 }
09255 if (user && debug)
09256 ast_verbose("Found user '%s'\n", user->name);
09257 } else {
09258 if (user) {
09259 if (!authpeer && debug)
09260 ast_verbose("Found user '%s', but fails host access\n", user->name);
09261 ASTOBJ_UNREF(user,sip_destroy_user);
09262 }
09263 user = NULL;
09264 }
09265
09266 if (!user) {
09267
09268 if (sipmethod == SIP_SUBSCRIBE)
09269
09270 peer = find_peer(of, NULL, 1);
09271 else
09272
09273
09274
09275
09276 peer = find_peer(NULL, &p->recv, 1);
09277
09278 if (peer) {
09279
09280 if (p->rtp) {
09281 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
09282 p->autoframing = peer->autoframing;
09283 }
09284 if (debug)
09285 ast_verbose("Found peer '%s'\n", peer->name);
09286
09287
09288 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
09289 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09290
09291
09292 if (p->sipoptions)
09293 peer->sipoptions = p->sipoptions;
09294
09295
09296 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09297 char *tmp = ast_strdupa(rpid_num);
09298 if (*calleridname)
09299 ast_string_field_set(p, cid_name, calleridname);
09300 if (ast_is_shrinkable_phonenumber(tmp))
09301 ast_shrink_phone_number(tmp);
09302 ast_string_field_set(p, cid_num, tmp);
09303 }
09304 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
09305
09306 ast_string_field_set(p, peersecret, peer->secret);
09307 ast_string_field_set(p, peermd5secret, peer->md5secret);
09308 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
09309 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
09310 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
09311 if (peer->callingpres)
09312 p->callingpres = peer->callingpres;
09313 if (peer->maxms && peer->lastms)
09314 p->timer_t1 = peer->lastms;
09315 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
09316
09317 ast_string_field_free(p, peersecret);
09318 ast_string_field_free(p, peermd5secret);
09319 }
09320 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09321 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
09322 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09323
09324 if (peer->call_limit)
09325 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
09326 ast_string_field_set(p, peername, peer->name);
09327 ast_string_field_set(p, authname, peer->name);
09328
09329
09330 for (v = peer->chanvars ; v ; v = v->next) {
09331 if ((tmpvar = ast_variable_new(v->name, v->value))) {
09332 tmpvar->next = p->chanvars;
09333 p->chanvars = tmpvar;
09334 }
09335 }
09336 if (authpeer) {
09337 (*authpeer) = ASTOBJ_REF(peer);
09338 }
09339
09340 if (!ast_strlen_zero(peer->username)) {
09341 ast_string_field_set(p, username, peer->username);
09342
09343
09344 ast_string_field_set(p, authname, peer->username);
09345 }
09346 if (!ast_strlen_zero(peer->cid_num) && !ast_strlen_zero(p->cid_num)) {
09347 char *tmp = ast_strdupa(peer->cid_num);
09348 if (ast_is_shrinkable_phonenumber(tmp))
09349 ast_shrink_phone_number(tmp);
09350 ast_string_field_set(p, cid_num, tmp);
09351 }
09352 if (!ast_strlen_zero(peer->cid_name) && !ast_strlen_zero(p->cid_name))
09353 ast_string_field_set(p, cid_name, peer->cid_name);
09354 ast_string_field_set(p, fullcontact, peer->fullcontact);
09355 if (!ast_strlen_zero(peer->context))
09356 ast_string_field_set(p, context, peer->context);
09357 ast_string_field_set(p, peersecret, peer->secret);
09358 ast_string_field_set(p, peermd5secret, peer->md5secret);
09359 ast_string_field_set(p, language, peer->language);
09360 ast_string_field_set(p, accountcode, peer->accountcode);
09361 p->amaflags = peer->amaflags;
09362 p->callgroup = peer->callgroup;
09363 p->pickupgroup = peer->pickupgroup;
09364 p->capability = peer->capability;
09365 p->prefs = peer->prefs;
09366 p->jointcapability = peer->capability;
09367 if (p->peercapability)
09368 p->jointcapability &= p->peercapability;
09369 p->maxcallbitrate = peer->maxcallbitrate;
09370 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
09371 ast_rtp_destroy(p->vrtp);
09372 p->vrtp = NULL;
09373 }
09374 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
09375 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
09376 p->noncodeccapability |= AST_RTP_DTMF;
09377 else
09378 p->noncodeccapability &= ~AST_RTP_DTMF;
09379 p->jointnoncodeccapability = p->noncodeccapability;
09380 if (p->t38.peercapability)
09381 p->t38.jointcapability &= p->t38.peercapability;
09382 }
09383 ASTOBJ_UNREF(peer, sip_destroy_peer);
09384 } else {
09385 if (debug)
09386 ast_verbose("Found no matching peer or user for '%s:%d'\n", ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
09387
09388
09389 if (!global_allowguest) {
09390 if (global_alwaysauthreject)
09391 res = AUTH_FAKE_AUTH;
09392 else
09393 res = AUTH_SECRET_FAILED;
09394 } else if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09395 char *tmp = ast_strdupa(rpid_num);
09396 if (*calleridname)
09397 ast_string_field_set(p, cid_name, calleridname);
09398 if (ast_is_shrinkable_phonenumber(tmp))
09399 ast_shrink_phone_number(tmp);
09400 ast_string_field_set(p, cid_num, tmp);
09401 }
09402 }
09403
09404 }
09405
09406 if (user)
09407 ASTOBJ_UNREF(user, sip_destroy_user);
09408 return res;
09409 }
09410
09411
09412
09413
09414 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin)
09415 {
09416 return check_user_full(p, req, sipmethod, uri, reliable, sin, NULL);
09417 }
09418
09419
09420 static int get_msg_text(char *buf, int len, struct sip_request *req)
09421 {
09422 int x;
09423 int y;
09424
09425 buf[0] = '\0';
09426 y = len - strlen(buf) - 5;
09427 if (y < 0)
09428 y = 0;
09429 for (x=0;x<req->lines;x++) {
09430 strncat(buf, req->line[x], y);
09431 y -= strlen(req->line[x]) + 1;
09432 if (y < 0)
09433 y = 0;
09434 if (y != 0)
09435 strcat(buf, "\n");
09436 }
09437 return 0;
09438 }
09439
09440
09441
09442
09443
09444 static void receive_message(struct sip_pvt *p, struct sip_request *req)
09445 {
09446 char buf[1024];
09447 struct ast_frame f;
09448 const char *content_type = get_header(req, "Content-Type");
09449
09450 if (strcmp(content_type, "text/plain")) {
09451 transmit_response(p, "415 Unsupported Media Type", req);
09452 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09453 return;
09454 }
09455
09456 if (get_msg_text(buf, sizeof(buf), req)) {
09457 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
09458 transmit_response(p, "202 Accepted", req);
09459 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09460 return;
09461 }
09462
09463 if (p->owner) {
09464 if (sip_debug_test_pvt(p))
09465 ast_verbose("Message received: '%s'\n", buf);
09466 memset(&f, 0, sizeof(f));
09467 f.frametype = AST_FRAME_TEXT;
09468 f.subclass = 0;
09469 f.offset = 0;
09470 f.data = buf;
09471 f.datalen = strlen(buf);
09472 ast_queue_frame(p->owner, &f);
09473 transmit_response(p, "202 Accepted", req);
09474 } else {
09475 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);
09476 transmit_response(p, "405 Method Not Allowed", req);
09477 }
09478 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09479 return;
09480 }
09481
09482
09483 static int sip_show_inuse(int fd, int argc, char *argv[])
09484 {
09485 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
09486 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
09487 char ilimits[40];
09488 char iused[40];
09489 int showall = FALSE;
09490
09491 if (argc < 3)
09492 return RESULT_SHOWUSAGE;
09493
09494 if (argc == 4 && !strcmp(argv[3],"all"))
09495 showall = TRUE;
09496
09497 ast_cli(fd, FORMAT, "* User name", "In use", "Limit");
09498 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09499 ASTOBJ_RDLOCK(iterator);
09500 if (iterator->call_limit)
09501 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
09502 else
09503 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
09504 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
09505 if (showall || iterator->call_limit)
09506 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
09507 ASTOBJ_UNLOCK(iterator);
09508 } while (0) );
09509
09510 ast_cli(fd, FORMAT, "* Peer name", "In use", "Limit");
09511
09512 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
09513 ASTOBJ_RDLOCK(iterator);
09514 if (iterator->call_limit)
09515 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
09516 else
09517 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
09518 snprintf(iused, sizeof(iused), "%d/%d", iterator->inUse, iterator->inRinging);
09519 if (showall || iterator->call_limit)
09520 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
09521 ASTOBJ_UNLOCK(iterator);
09522 } while (0) );
09523
09524 return RESULT_SUCCESS;
09525 #undef FORMAT
09526 #undef FORMAT2
09527 }
09528
09529
09530 static char *transfermode2str(enum transfermodes mode)
09531 {
09532 if (mode == TRANSFER_OPENFORALL)
09533 return "open";
09534 else if (mode == TRANSFER_CLOSED)
09535 return "closed";
09536 return "strict";
09537 }
09538
09539
09540 static char *nat2str(int nat)
09541 {
09542 switch(nat) {
09543 case SIP_NAT_NEVER:
09544 return "No";
09545 case SIP_NAT_ROUTE:
09546 return "Route";
09547 case SIP_NAT_ALWAYS:
09548 return "Always";
09549 case SIP_NAT_RFC3581:
09550 return "RFC3581";
09551 default:
09552 return "Unknown";
09553 }
09554 }
09555
09556
09557
09558
09559 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
09560 {
09561 int res = 0;
09562 if (peer->maxms) {
09563 if (peer->lastms < 0) {
09564 ast_copy_string(status, "UNREACHABLE", statuslen);
09565 } else if (peer->lastms > peer->maxms) {
09566 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
09567 res = 1;
09568 } else if (peer->lastms) {
09569 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
09570 res = 1;
09571 } else {
09572 ast_copy_string(status, "UNKNOWN", statuslen);
09573 }
09574 } else {
09575 ast_copy_string(status, "Unmonitored", statuslen);
09576
09577 res = -1;
09578 }
09579 return res;
09580 }
09581
09582
09583 static int sip_show_users(int fd, int argc, char *argv[])
09584 {
09585 regex_t regexbuf;
09586 int havepattern = FALSE;
09587
09588 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
09589
09590 switch (argc) {
09591 case 5:
09592 if (!strcasecmp(argv[3], "like")) {
09593 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
09594 return RESULT_SHOWUSAGE;
09595 havepattern = TRUE;
09596 } else
09597 return RESULT_SHOWUSAGE;
09598 case 3:
09599 break;
09600 default:
09601 return RESULT_SHOWUSAGE;
09602 }
09603
09604 ast_cli(fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
09605 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09606 ASTOBJ_RDLOCK(iterator);
09607
09608 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09609 ASTOBJ_UNLOCK(iterator);
09610 continue;
09611 }
09612
09613 ast_cli(fd, FORMAT, iterator->name,
09614 iterator->secret,
09615 iterator->accountcode,
09616 iterator->context,
09617 iterator->ha ? "Yes" : "No",
09618 nat2str(ast_test_flag(&iterator->flags[0], SIP_NAT)));
09619 ASTOBJ_UNLOCK(iterator);
09620 } while (0)
09621 );
09622
09623 if (havepattern)
09624 regfree(®exbuf);
09625
09626 return RESULT_SUCCESS;
09627 #undef FORMAT
09628 }
09629
09630 static char mandescr_show_peers[] =
09631 "Description: Lists SIP peers in text format with details on current status.\n"
09632 "Variables: \n"
09633 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
09634
09635
09636
09637 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
09638 {
09639 const char *id = astman_get_header(m,"ActionID");
09640 const char *a[] = {"sip", "show", "peers"};
09641 char idtext[256] = "";
09642 int total = 0;
09643
09644 if (!ast_strlen_zero(id))
09645 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
09646
09647 astman_send_ack(s, m, "Peer status list will follow");
09648
09649 _sip_show_peers(-1, &total, s, m, 3, a);
09650
09651 astman_append(s,
09652 "Event: PeerlistComplete\r\n"
09653 "ListItems: %d\r\n"
09654 "%s"
09655 "\r\n", total, idtext);
09656 return 0;
09657 }
09658
09659
09660 static int sip_show_peers(int fd, int argc, char *argv[])
09661 {
09662 return _sip_show_peers(fd, NULL, NULL, NULL, argc, (const char **) argv);
09663 }
09664
09665
09666 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
09667 {
09668 regex_t regexbuf;
09669 int havepattern = FALSE;
09670
09671 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s %-10s\n"
09672 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s %-10s\n"
09673
09674 char name[256];
09675 int total_peers = 0;
09676 int peers_mon_online = 0;
09677 int peers_mon_offline = 0;
09678 int peers_unmon_offline = 0;
09679 int peers_unmon_online = 0;
09680 const char *id;
09681 char idtext[256] = "";
09682 int realtimepeers;
09683
09684 realtimepeers = ast_check_realtime("sippeers");
09685
09686 if (s) {
09687 id = astman_get_header(m,"ActionID");
09688 if (!ast_strlen_zero(id))
09689 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
09690 }
09691
09692 switch (argc) {
09693 case 5:
09694 if (!strcasecmp(argv[3], "like")) {
09695 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
09696 return RESULT_SHOWUSAGE;
09697 havepattern = TRUE;
09698 } else
09699 return RESULT_SHOWUSAGE;
09700 case 3:
09701 break;
09702 default:
09703 return RESULT_SHOWUSAGE;
09704 }
09705
09706 if (!s)
09707 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
09708
09709 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
09710 char status[20] = "";
09711 char srch[2000];
09712 char pstatus;
09713
09714 ASTOBJ_RDLOCK(iterator);
09715
09716 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09717 ASTOBJ_UNLOCK(iterator);
09718 continue;
09719 }
09720
09721 if (!ast_strlen_zero(iterator->username) && !s)
09722 snprintf(name, sizeof(name), "%s/%s", iterator->name, iterator->username);
09723 else
09724 ast_copy_string(name, iterator->name, sizeof(name));
09725
09726 pstatus = peer_status(iterator, status, sizeof(status));
09727 if (pstatus == 1)
09728 peers_mon_online++;
09729 else if (pstatus == 0)
09730 peers_mon_offline++;
09731 else {
09732 if (iterator->addr.sin_port == 0)
09733 peers_unmon_offline++;
09734 else
09735 peers_unmon_online++;
09736 }
09737
09738 snprintf(srch, sizeof(srch), FORMAT, name,
09739 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
09740 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
09741 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
09742 iterator->ha ? " A " : " ",
09743 ntohs(iterator->addr.sin_port), status,
09744 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
09745
09746 if (!s) {
09747 ast_cli(fd, FORMAT, name,
09748 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
09749 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
09750 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
09751 iterator->ha ? " A " : " ",
09752
09753 ntohs(iterator->addr.sin_port), status,
09754 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
09755 } else {
09756
09757 astman_append(s,
09758 "Event: PeerEntry\r\n%s"
09759 "Channeltype: SIP\r\n"
09760 "ObjectName: %s\r\n"
09761 "ChanObjectType: peer\r\n"
09762 "IPaddress: %s\r\n"
09763 "IPport: %d\r\n"
09764 "Dynamic: %s\r\n"
09765 "Natsupport: %s\r\n"
09766 "VideoSupport: %s\r\n"
09767 "ACL: %s\r\n"
09768 "Status: %s\r\n"
09769 "RealtimeDevice: %s\r\n\r\n",
09770 idtext,
09771 iterator->name,
09772 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "-none-",
09773 ntohs(iterator->addr.sin_port),
09774 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no",
09775 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? "yes" : "no",
09776 ast_test_flag(&iterator->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
09777 iterator->ha ? "yes" : "no",
09778 status,
09779 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "yes":"no") : "no");
09780 }
09781
09782 ASTOBJ_UNLOCK(iterator);
09783
09784 total_peers++;
09785 } while(0) );
09786
09787 if (!s)
09788 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
09789 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
09790
09791 if (havepattern)
09792 regfree(®exbuf);
09793
09794 if (total)
09795 *total = total_peers;
09796
09797
09798 return RESULT_SUCCESS;
09799 #undef FORMAT
09800 #undef FORMAT2
09801 }
09802
09803
09804 static int sip_show_objects(int fd, int argc, char *argv[])
09805 {
09806 char tmp[256];
09807 if (argc != 3)
09808 return RESULT_SHOWUSAGE;
09809 ast_cli(fd, "-= User objects: %d static, %d realtime =-\n\n", suserobjs, ruserobjs);
09810 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &userl);
09811 ast_cli(fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
09812 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &peerl);
09813 ast_cli(fd, "-= Registry objects: %d =-\n\n", regobjs);
09814 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), ®l);
09815 return RESULT_SUCCESS;
09816 }
09817
09818 static void print_group(int fd, ast_group_t group, int crlf)
09819 {
09820 char buf[256];
09821 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
09822 }
09823
09824
09825 static const char *dtmfmode2str(int mode)
09826 {
09827 switch (mode) {
09828 case SIP_DTMF_RFC2833:
09829 return "rfc2833";
09830 case SIP_DTMF_INFO:
09831 return "info";
09832 case SIP_DTMF_INBAND:
09833 return "inband";
09834 case SIP_DTMF_AUTO:
09835 return "auto";
09836 }
09837 return "<error>";
09838 }
09839
09840
09841 static const char *insecure2str(int port, int invite)
09842 {
09843 if (port && invite)
09844 return "port,invite";
09845 else if (port)
09846 return "port";
09847 else if (invite)
09848 return "invite";
09849 else
09850 return "no";
09851 }
09852
09853
09854
09855
09856 static void cleanup_stale_contexts(char *new, char *old)
09857 {
09858 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
09859
09860 while ((oldcontext = strsep(&old, "&"))) {
09861 stalecontext = '\0';
09862 ast_copy_string(newlist, new, sizeof(newlist));
09863 stringp = newlist;
09864 while ((newcontext = strsep(&stringp, "&"))) {
09865 if (strcmp(newcontext, oldcontext) == 0) {
09866
09867 stalecontext = '\0';
09868 break;
09869 } else if (strcmp(newcontext, oldcontext)) {
09870 stalecontext = oldcontext;
09871 }
09872
09873 }
09874 if (stalecontext)
09875 ast_context_destroy(ast_context_find(stalecontext), "SIP");
09876 }
09877 }
09878
09879
09880 static int sip_prune_realtime(int fd, int argc, char *argv[])
09881 {
09882 struct sip_peer *peer;
09883 struct sip_user *user;
09884 int pruneuser = FALSE;
09885 int prunepeer = FALSE;
09886 int multi = FALSE;
09887 char *name = NULL;
09888 regex_t regexbuf;
09889
09890 switch (argc) {
09891 case 4:
09892 if (!strcasecmp(argv[3], "user"))
09893 return RESULT_SHOWUSAGE;
09894 if (!strcasecmp(argv[3], "peer"))
09895 return RESULT_SHOWUSAGE;
09896 if (!strcasecmp(argv[3], "like"))
09897 return RESULT_SHOWUSAGE;
09898 if (!strcasecmp(argv[3], "all")) {
09899 multi = TRUE;
09900 pruneuser = prunepeer = TRUE;
09901 } else {
09902 pruneuser = prunepeer = TRUE;
09903 name = argv[3];
09904 }
09905 break;
09906 case 5:
09907 if (!strcasecmp(argv[4], "like"))
09908 return RESULT_SHOWUSAGE;
09909 if (!strcasecmp(argv[3], "all"))
09910 return RESULT_SHOWUSAGE;
09911 if (!strcasecmp(argv[3], "like")) {
09912 multi = TRUE;
09913 name = argv[4];
09914 pruneuser = prunepeer = TRUE;
09915 } else if (!strcasecmp(argv[3], "user")) {
09916 pruneuser = TRUE;
09917 if (!strcasecmp(argv[4], "all"))
09918 multi = TRUE;
09919 else
09920 name = argv[4];
09921 } else if (!strcasecmp(argv[3], "peer")) {
09922 prunepeer = TRUE;
09923 if (!strcasecmp(argv[4], "all"))
09924 multi = TRUE;
09925 else
09926 name = argv[4];
09927 } else
09928 return RESULT_SHOWUSAGE;
09929 break;
09930 case 6:
09931 if (strcasecmp(argv[4], "like"))
09932 return RESULT_SHOWUSAGE;
09933 if (!strcasecmp(argv[3], "user")) {
09934 pruneuser = TRUE;
09935 name = argv[5];
09936 } else if (!strcasecmp(argv[3], "peer")) {
09937 prunepeer = TRUE;
09938 name = argv[5];
09939 } else
09940 return RESULT_SHOWUSAGE;
09941 break;
09942 default:
09943 return RESULT_SHOWUSAGE;
09944 }
09945
09946 if (multi && name) {
09947 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
09948 return RESULT_SHOWUSAGE;
09949 }
09950
09951 if (multi) {
09952 if (prunepeer) {
09953 int pruned = 0;
09954
09955 ASTOBJ_CONTAINER_WRLOCK(&peerl);
09956 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
09957 ASTOBJ_RDLOCK(iterator);
09958 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09959 ASTOBJ_UNLOCK(iterator);
09960 continue;
09961 };
09962 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
09963 ASTOBJ_MARK(iterator);
09964 pruned++;
09965 }
09966 ASTOBJ_UNLOCK(iterator);
09967 } while (0) );
09968 if (pruned) {
09969 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
09970 ast_cli(fd, "%d peers pruned.\n", pruned);
09971 } else
09972 ast_cli(fd, "No peers found to prune.\n");
09973 ASTOBJ_CONTAINER_UNLOCK(&peerl);
09974 }
09975 if (pruneuser) {
09976 int pruned = 0;
09977
09978 ASTOBJ_CONTAINER_WRLOCK(&userl);
09979 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09980 ASTOBJ_RDLOCK(iterator);
09981 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09982 ASTOBJ_UNLOCK(iterator);
09983 continue;
09984 };
09985 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
09986 ASTOBJ_MARK(iterator);
09987 pruned++;
09988 }
09989 ASTOBJ_UNLOCK(iterator);
09990 } while (0) );
09991 if (pruned) {
09992 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
09993 ast_cli(fd, "%d users pruned.\n", pruned);
09994 } else
09995 ast_cli(fd, "No users found to prune.\n");
09996 ASTOBJ_CONTAINER_UNLOCK(&userl);
09997 }
09998 } else {
09999 if (prunepeer) {
10000 if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
10001 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
10002 ast_cli(fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
10003 ASTOBJ_CONTAINER_LINK(&peerl, peer);
10004 } else
10005 ast_cli(fd, "Peer '%s' pruned.\n", name);
10006 ASTOBJ_UNREF(peer, sip_destroy_peer);
10007 } else
10008 ast_cli(fd, "Peer '%s' not found.\n", name);
10009 }
10010 if (pruneuser) {
10011 if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
10012 if (!ast_test_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
10013 ast_cli(fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
10014 ASTOBJ_CONTAINER_LINK(&userl, user);
10015 } else
10016 ast_cli(fd, "User '%s' pruned.\n", name);
10017 ASTOBJ_UNREF(user, sip_destroy_user);
10018 } else
10019 ast_cli(fd, "User '%s' not found.\n", name);
10020 }
10021 }
10022
10023 return RESULT_SUCCESS;
10024 }
10025
10026
10027 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
10028 {
10029 int x, codec;
10030
10031 for(x = 0; x < 32 ; x++) {
10032 codec = ast_codec_pref_index(pref, x);
10033 if (!codec)
10034 break;
10035 ast_cli(fd, "%s", ast_getformatname(codec));
10036 ast_cli(fd, ":%d", pref->framing[x]);
10037 if (x < 31 && ast_codec_pref_index(pref, x + 1))
10038 ast_cli(fd, ",");
10039 }
10040 if (!x)
10041 ast_cli(fd, "none");
10042 }
10043
10044
10045 static const char *domain_mode_to_text(const enum domain_mode mode)
10046 {
10047 switch (mode) {
10048 case SIP_DOMAIN_AUTO:
10049 return "[Automatic]";
10050 case SIP_DOMAIN_CONFIG:
10051 return "[Configured]";
10052 }
10053
10054 return "";
10055 }
10056
10057
10058 static int sip_show_domains(int fd, int argc, char *argv[])
10059 {
10060 struct domain *d;
10061 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
10062
10063 if (AST_LIST_EMPTY(&domain_list)) {
10064 ast_cli(fd, "SIP Domain support not enabled.\n\n");
10065 return RESULT_SUCCESS;
10066 } else {
10067 ast_cli(fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
10068 AST_LIST_LOCK(&domain_list);
10069 AST_LIST_TRAVERSE(&domain_list, d, list)
10070 ast_cli(fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
10071 domain_mode_to_text(d->mode));
10072 AST_LIST_UNLOCK(&domain_list);
10073 ast_cli(fd, "\n");
10074 return RESULT_SUCCESS;
10075 }
10076 }
10077 #undef FORMAT
10078
10079 static char mandescr_show_peer[] =
10080 "Description: Show one SIP peer with details on current status.\n"
10081 "Variables: \n"
10082 " Peer: <name> The peer name you want to check.\n"
10083 " ActionID: <id> Optional action ID for this AMI transaction.\n";
10084
10085
10086 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
10087 {
10088 const char *a[4];
10089 const char *peer;
10090 int ret;
10091
10092 peer = astman_get_header(m,"Peer");
10093 if (ast_strlen_zero(peer)) {
10094 astman_send_error(s, m, "Peer: <name> missing.\n");
10095 return 0;
10096 }
10097 a[0] = "sip";
10098 a[1] = "show";
10099 a[2] = "peer";
10100 a[3] = peer;
10101
10102 ret = _sip_show_peer(1, -1, s, m, 4, a);
10103 astman_append(s, "\r\n\r\n" );
10104 return ret;
10105 }
10106
10107
10108
10109
10110 static int sip_show_peer(int fd, int argc, char *argv[])
10111 {
10112 return _sip_show_peer(0, fd, NULL, NULL, argc, (const char **) argv);
10113 }
10114
10115
10116 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
10117 {
10118 char status[30] = "";
10119 char cbuf[256];
10120 struct sip_peer *peer;
10121 char codec_buf[512];
10122 struct ast_codec_pref *pref;
10123 struct ast_variable *v;
10124 struct sip_auth *auth;
10125 int x = 0, codec = 0, load_realtime;
10126 int realtimepeers;
10127
10128 realtimepeers = ast_check_realtime("sippeers");
10129
10130 if (argc < 4)
10131 return RESULT_SHOWUSAGE;
10132
10133 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
10134 peer = find_peer(argv[3], NULL, load_realtime);
10135 if (s) {
10136 if (peer) {
10137 const char *id = astman_get_header(m,"ActionID");
10138
10139 astman_append(s, "Response: Success\r\n");
10140 if (!ast_strlen_zero(id))
10141 astman_append(s, "ActionID: %s\r\n",id);
10142 } else {
10143 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.\n", argv[3]);
10144 astman_send_error(s, m, cbuf);
10145 return 0;
10146 }
10147 }
10148 if (peer && type==0 ) {
10149 ast_cli(fd,"\n\n");
10150 ast_cli(fd, " * Name : %s\n", peer->name);
10151 if (realtimepeers) {
10152 ast_cli(fd, " Realtime peer: %s\n", ast_test_flag(&peer->flags[0], SIP_REALTIME) ? "Yes, cached" : "No");
10153 }
10154 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
10155 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
10156 for (auth = peer->auth; auth; auth = auth->next) {
10157 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
10158 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
10159 }
10160 ast_cli(fd, " Context : %s\n", peer->context);
10161 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
10162 ast_cli(fd, " Language : %s\n", peer->language);
10163 if (!ast_strlen_zero(peer->accountcode))
10164 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
10165 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
10166 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
10167 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
10168 if (!ast_strlen_zero(peer->fromuser))
10169 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
10170 if (!ast_strlen_zero(peer->fromdomain))
10171 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
10172 ast_cli(fd, " Callgroup : ");
10173 print_group(fd, peer->callgroup, 0);
10174 ast_cli(fd, " Pickupgroup : ");
10175 print_group(fd, peer->pickupgroup, 0);
10176 ast_cli(fd, " Mailbox : %s\n", peer->mailbox);
10177 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
10178 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
10179 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
10180 ast_cli(fd, " Dynamic : %s\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Yes":"No"));
10181 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
10182 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
10183 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
10184 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)));
10185 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
10186 ast_cli(fd, " ACL : %s\n", (peer->ha?"Yes":"No"));
10187 ast_cli(fd, " T38 pt UDPTL : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)?"Yes":"No");
10188 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
10189 ast_cli(fd, " T38 pt RTP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_RTP)?"Yes":"No");
10190 ast_cli(fd, " T38 pt TCP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_TCP)?"Yes":"No");
10191 #endif
10192 ast_cli(fd, " CanReinvite : %s\n", ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Yes":"No");
10193 ast_cli(fd, " PromiscRedir : %s\n", ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Yes":"No");
10194 ast_cli(fd, " User=Phone : %s\n", ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Yes":"No");
10195 ast_cli(fd, " Video Support: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Yes":"No");
10196 ast_cli(fd, " Trust RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_TRUSTRPID) ? "Yes" : "No");
10197 ast_cli(fd, " Send RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_SENDRPID) ? "Yes" : "No");
10198 ast_cli(fd, " Subscriptions: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
10199 ast_cli(fd, " Overlap dial : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
10200
10201
10202 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
10203 ast_cli(fd, " LastMsg : %d\n", peer->lastmsg);
10204 ast_cli(fd, " ToHost : %s\n", peer->tohost);
10205 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));
10206 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
10207 if (!ast_strlen_zero(global_regcontext))
10208 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
10209 ast_cli(fd, " Def. Username: %s\n", peer->username);
10210 ast_cli(fd, " SIP Options : ");
10211 if (peer->sipoptions) {
10212 int lastoption = -1;
10213 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
10214 if (sip_options[x].id != lastoption) {
10215 if (peer->sipoptions & sip_options[x].id)
10216 ast_cli(fd, "%s ", sip_options[x].text);
10217 lastoption = x;
10218 }
10219 }
10220 } else
10221 ast_cli(fd, "(none)");
10222
10223 ast_cli(fd, "\n");
10224 ast_cli(fd, " Codecs : ");
10225 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
10226 ast_cli(fd, "%s\n", codec_buf);
10227 ast_cli(fd, " Codec Order : (");
10228 print_codec_to_cli(fd, &peer->prefs);
10229 ast_cli(fd, ")\n");
10230
10231 ast_cli(fd, " Auto-Framing: %s \n", peer->autoframing ? "Yes" : "No");
10232 ast_cli(fd, " Status : ");
10233 peer_status(peer, status, sizeof(status));
10234 ast_cli(fd, "%s\n",status);
10235 ast_cli(fd, " Useragent : %s\n", peer->useragent);
10236 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
10237 if (peer->chanvars) {
10238 ast_cli(fd, " Variables :\n");
10239 for (v = peer->chanvars ; v ; v = v->next)
10240 ast_cli(fd, " %s = %s\n", v->name, v->value);
10241 }
10242 ast_cli(fd,"\n");
10243 ASTOBJ_UNREF(peer,sip_destroy_peer);
10244 } else if (peer && type == 1) {
10245 char buf[256];
10246 astman_append(s, "Channeltype: SIP\r\n");
10247 astman_append(s, "ObjectName: %s\r\n", peer->name);
10248 astman_append(s, "ChanObjectType: peer\r\n");
10249 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
10250 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
10251 astman_append(s, "Context: %s\r\n", peer->context);
10252 astman_append(s, "Language: %s\r\n", peer->language);
10253 if (!ast_strlen_zero(peer->accountcode))
10254 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
10255 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
10256 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
10257 if (!ast_strlen_zero(peer->fromuser))
10258 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
10259 if (!ast_strlen_zero(peer->fromdomain))
10260 astman_append(s, "SIP-FromDomain: %s\r\n", peer->fromdomain);
10261 astman_append(s, "Callgroup: ");
10262 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->callgroup));
10263 astman_append(s, "Pickupgroup: ");
10264 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->pickupgroup));
10265 astman_append(s, "VoiceMailbox: %s\r\n", peer->mailbox);
10266 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
10267 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
10268 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
10269 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
10270 astman_append(s, "Dynamic: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Y":"N"));
10271 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
10272 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched,peer->expire));
10273 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)));
10274 astman_append(s, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
10275 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
10276 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Y":"N"));
10277 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
10278 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
10279 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
10280
10281
10282 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
10283 astman_append(s, "SIPLastMsg: %d\r\n", peer->lastmsg);
10284 astman_append(s, "ToHost: %s\r\n", peer->tohost);
10285 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));
10286 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));
10287 astman_append(s, "Default-Username: %s\r\n", peer->username);
10288 if (!ast_strlen_zero(global_regcontext))
10289 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
10290 astman_append(s, "Codecs: ");
10291 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
10292 astman_append(s, "%s\r\n", codec_buf);
10293 astman_append(s, "CodecOrder: ");
10294 pref = &peer->prefs;
10295 for(x = 0; x < 32 ; x++) {
10296 codec = ast_codec_pref_index(pref,x);
10297 if (!codec)
10298 break;
10299 astman_append(s, "%s", ast_getformatname(codec));
10300 if (x < 31 && ast_codec_pref_index(pref,x+1))
10301 astman_append(s, ",");
10302 }
10303
10304 astman_append(s, "\r\n");
10305 astman_append(s, "Status: ");
10306 peer_status(peer, status, sizeof(status));
10307 astman_append(s, "%s\r\n", status);
10308 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
10309 astman_append(s, "Reg-Contact : %s\r\n", peer->fullcontact);
10310 if (peer->chanvars) {
10311 for (v = peer->chanvars ; v ; v = v->next) {
10312 astman_append(s, "ChanVariable:\n");
10313 astman_append(s, " %s,%s\r\n", v->name, v->value);
10314 }
10315 }
10316
10317 ASTOBJ_UNREF(peer,sip_destroy_peer);
10318
10319 } else {
10320 ast_cli(fd,"Peer %s not found.\n", argv[3]);
10321 ast_cli(fd,"\n");
10322 }
10323
10324 return RESULT_SUCCESS;
10325 }
10326
10327
10328 static int sip_show_user(int fd, int argc, char *argv[])
10329 {
10330 char cbuf[256];
10331 struct sip_user *user;
10332 struct ast_variable *v;
10333 int load_realtime;
10334
10335 if (argc < 4)
10336 return RESULT_SHOWUSAGE;
10337
10338
10339 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
10340
10341 user = find_user(argv[3], load_realtime);
10342 if (user) {
10343 ast_cli(fd,"\n\n");
10344 ast_cli(fd, " * Name : %s\n", user->name);
10345 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
10346 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
10347 ast_cli(fd, " Context : %s\n", user->context);
10348 ast_cli(fd, " Language : %s\n", user->language);
10349 if (!ast_strlen_zero(user->accountcode))
10350 ast_cli(fd, " Accountcode : %s\n", user->accountcode);
10351 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
10352 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
10353 ast_cli(fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
10354 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
10355 ast_cli(fd, " Call limit : %d\n", user->call_limit);
10356 ast_cli(fd, " Callgroup : ");
10357 print_group(fd, user->callgroup, 0);
10358 ast_cli(fd, " Pickupgroup : ");
10359 print_group(fd, user->pickupgroup, 0);
10360 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
10361 ast_cli(fd, " ACL : %s\n", (user->ha?"Yes":"No"));
10362 ast_cli(fd, " Codec Order : (");
10363 print_codec_to_cli(fd, &user->prefs);
10364 ast_cli(fd, ")\n");
10365
10366 ast_cli(fd, " Auto-Framing: %s \n", user->autoframing ? "Yes" : "No");
10367 if (user->chanvars) {
10368 ast_cli(fd, " Variables :\n");
10369 for (v = user->chanvars ; v ; v = v->next)
10370 ast_cli(fd, " %s = %s\n", v->name, v->value);
10371 }
10372 ast_cli(fd,"\n");
10373 ASTOBJ_UNREF(user,sip_destroy_user);
10374 } else {
10375 ast_cli(fd,"User %s not found.\n", argv[3]);
10376 ast_cli(fd,"\n");
10377 }
10378
10379 return RESULT_SUCCESS;
10380 }
10381
10382
10383 static int sip_show_registry(int fd, int argc, char *argv[])
10384 {
10385 #define FORMAT2 "%-30.30s %-12.12s %8.8s %-20.20s %-25.25s\n"
10386 #define FORMAT "%-30.30s %-12.12s %8d %-20.20s %-25.25s\n"
10387 char host[80];
10388 char tmpdat[256];
10389 struct tm tm;
10390
10391
10392 if (argc != 3)
10393 return RESULT_SHOWUSAGE;
10394 ast_cli(fd, FORMAT2, "Host", "Username", "Refresh", "State", "Reg.Time");
10395 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
10396 ASTOBJ_RDLOCK(iterator);
10397 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
10398 if (iterator->regtime) {
10399 ast_localtime(&iterator->regtime, &tm, NULL);
10400 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
10401 } else {
10402 tmpdat[0] = 0;
10403 }
10404 ast_cli(fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
10405 ASTOBJ_UNLOCK(iterator);
10406 } while(0));
10407 return RESULT_SUCCESS;
10408 #undef FORMAT
10409 #undef FORMAT2
10410 }
10411
10412
10413 static int sip_show_settings(int fd, int argc, char *argv[])
10414 {
10415 int realtimepeers;
10416 int realtimeusers;
10417 char codec_buf[BUFSIZ];
10418
10419 realtimepeers = ast_check_realtime("sippeers");
10420 realtimeusers = ast_check_realtime("sipusers");
10421
10422 if (argc != 3)
10423 return RESULT_SHOWUSAGE;
10424 ast_cli(fd, "\n\nGlobal Settings:\n");
10425 ast_cli(fd, "----------------\n");
10426 ast_cli(fd, " SIP Port: %d\n", ntohs(bindaddr.sin_port));
10427 ast_cli(fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
10428 ast_cli(fd, " Videosupport: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "Yes" : "No");
10429 ast_cli(fd, " AutoCreatePeer: %s\n", autocreatepeer ? "Yes" : "No");
10430 ast_cli(fd, " Allow unknown access: %s\n", global_allowguest ? "Yes" : "No");
10431 ast_cli(fd, " Allow subscriptions: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
10432 ast_cli(fd, " Allow overlap dialing: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
10433 ast_cli(fd, " Promsic. redir: %s\n", ast_test_flag(&global_flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
10434 ast_cli(fd, " SIP domain support: %s\n", AST_LIST_EMPTY(&domain_list) ? "No" : "Yes");
10435 ast_cli(fd, " Call to non-local dom.: %s\n", allow_external_domains ? "Yes" : "No");
10436 ast_cli(fd, " URI user is phone no: %s\n", ast_test_flag(&global_flags[0], SIP_USEREQPHONE) ? "Yes" : "No");
10437 ast_cli(fd, " Our auth realm %s\n", global_realm);
10438 ast_cli(fd, " Realm. auth: %s\n", authl ? "Yes": "No");
10439 ast_cli(fd, " Always auth rejects: %s\n", global_alwaysauthreject ? "Yes" : "No");
10440 ast_cli(fd, " Call limit peers only: %s\n", global_limitonpeers ? "Yes" : "No");
10441 ast_cli(fd, " Direct RTP setup: %s\n", global_directrtpsetup ? "Yes" : "No");
10442 ast_cli(fd, " User Agent: %s\n", global_useragent);
10443 ast_cli(fd, " MWI checking interval: %d secs\n", global_mwitime);
10444 ast_cli(fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
10445 ast_cli(fd, " Caller ID: %s\n", default_callerid);
10446 ast_cli(fd, " From: Domain: %s\n", default_fromdomain);
10447 ast_cli(fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
10448 ast_cli(fd, " Call Events: %s\n", global_callevents ? "On" : "Off");
10449 ast_cli(fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
10450 ast_cli(fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
10451 ast_cli(fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
10452 ast_cli(fd, " T38 fax pt UDPTL: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL) ? "Yes" : "No");
10453 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
10454 ast_cli(fd, " T38 fax pt RTP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_RTP) ? "Yes" : "No");
10455 ast_cli(fd, " T38 fax pt TCP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_TCP) ? "Yes" : "No");
10456 #endif
10457 ast_cli(fd, " RFC2833 Compensation: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE) ? "Yes" : "No");
10458 if (!realtimepeers && !realtimeusers)
10459 ast_cli(fd, " SIP realtime: Disabled\n" );
10460 else
10461 ast_cli(fd, " SIP realtime: Enabled\n" );
10462
10463 ast_cli(fd, "\nGlobal Signalling Settings:\n");
10464 ast_cli(fd, "---------------------------\n");
10465 ast_cli(fd, " Codecs: ");
10466 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, global_capability);
10467 ast_cli(fd, "%s\n", codec_buf);
10468 ast_cli(fd, " Codec Order: ");
10469 print_codec_to_cli(fd, &default_prefs);
10470 ast_cli(fd, "\n");
10471 ast_cli(fd, " T1 minimum: %d\n", global_t1min);
10472 ast_cli(fd, " Relax DTMF: %s\n", global_relaxdtmf ? "Yes" : "No");
10473 ast_cli(fd, " Compact SIP headers: %s\n", compactheaders ? "Yes" : "No");
10474 ast_cli(fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
10475 ast_cli(fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
10476 ast_cli(fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
10477 ast_cli(fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
10478 ast_cli(fd, " DNS SRV lookup: %s\n", srvlookup ? "Yes" : "No");
10479 ast_cli(fd, " Pedantic SIP support: %s\n", pedanticsipchecking ? "Yes" : "No");
10480 ast_cli(fd, " Reg. min duration %d secs\n", min_expiry);
10481 ast_cli(fd, " Reg. max duration: %d secs\n", max_expiry);
10482 ast_cli(fd, " Reg. default duration: %d secs\n", default_expiry);
10483 ast_cli(fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
10484 ast_cli(fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
10485 ast_cli(fd, " Notify ringing state: %s\n", global_notifyringing ? "Yes" : "No");
10486 ast_cli(fd, " Notify hold state: %s\n", global_notifyhold ? "Yes" : "No");
10487 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(global_allowtransfer));
10488 ast_cli(fd, " Max Call Bitrate: %d kbps\r\n", default_maxcallbitrate);
10489 ast_cli(fd, " Auto-Framing: %s \r\n", global_autoframing ? "Yes" : "No");
10490 ast_cli(fd, "\nDefault Settings:\n");
10491 ast_cli(fd, "-----------------\n");
10492 ast_cli(fd, " Context: %s\n", default_context);
10493 ast_cli(fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags[0], SIP_NAT)));
10494 ast_cli(fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
10495 ast_cli(fd, " Qualify: %d\n", default_qualify);
10496 ast_cli(fd, " Use ClientCode: %s\n", ast_test_flag(&global_flags[0], SIP_USECLIENTCODE) ? "Yes" : "No");
10497 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" );
10498 ast_cli(fd, " Language: %s\n", S_OR(default_language, "(Defaults to English)"));
10499 ast_cli(fd, " MOH Interpret: %s\n", default_mohinterpret);
10500 ast_cli(fd, " MOH Suggest: %s\n", default_mohsuggest);
10501 ast_cli(fd, " Voice Mail Extension: %s\n", default_vmexten);
10502
10503
10504 if (realtimepeers || realtimeusers) {
10505 ast_cli(fd, "\nRealtime SIP Settings:\n");
10506 ast_cli(fd, "----------------------\n");
10507 ast_cli(fd, " Realtime Peers: %s\n", realtimepeers ? "Yes" : "No");
10508 ast_cli(fd, " Realtime Users: %s\n", realtimeusers ? "Yes" : "No");
10509 ast_cli(fd, " Cache Friends: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? "Yes" : "No");
10510 ast_cli(fd, " Update: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) ? "Yes" : "No");
10511 ast_cli(fd, " Ignore Reg. Expire: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) ? "Yes" : "No");
10512 ast_cli(fd, " Save sys. name: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME) ? "Yes" : "No");
10513 ast_cli(fd, " Auto Clear: %d\n", global_rtautoclear);
10514 }
10515 ast_cli(fd, "\n----\n");
10516 return RESULT_SUCCESS;
10517 }
10518
10519
10520 static const char *subscription_type2str(enum subscriptiontype subtype)
10521 {
10522 int i;
10523
10524 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
10525 if (subscription_types[i].type == subtype) {
10526 return subscription_types[i].text;
10527 }
10528 }
10529 return subscription_types[0].text;
10530 }
10531
10532
10533 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
10534 {
10535 int i;
10536
10537 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
10538 if (subscription_types[i].type == subtype) {
10539 return &subscription_types[i];
10540 }
10541 }
10542 return &subscription_types[0];
10543 }
10544
10545
10546 static int sip_show_channels(int fd, int argc, char *argv[])
10547 {
10548 return __sip_show_channels(fd, argc, argv, 0);
10549 }
10550
10551
10552 static int sip_show_subscriptions(int fd, int argc, char *argv[])
10553 {
10554 return __sip_show_channels(fd, argc, argv, 1);
10555 }
10556
10557
10558 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions)
10559 {
10560 #define FORMAT3 "%-15.15s %-10.10s %-11.11s %-15.15s %-13.13s %-15.15s %-10.10s\n"
10561 #define FORMAT2 "%-15.15s %-10.10s %-11.11s %-11.11s %-4.4s %-7.7s %-15.15s\n"
10562 #define FORMAT "%-15.15s %-10.10s %-11.11s %5.5d/%5.5d %-4.4s %-3.3s %-3.3s %-15.15s %-10.10s\n"
10563 struct sip_pvt *cur;
10564 int numchans = 0;
10565 char *referstatus = NULL;
10566
10567 if (argc != 3)
10568 return RESULT_SHOWUSAGE;
10569 ast_mutex_lock(&iflock);
10570 cur = iflist;
10571 if (!subscriptions)
10572 ast_cli(fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Seq (Tx/Rx)", "Format", "Hold", "Last Message");
10573 else
10574 ast_cli(fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox");
10575 for (; cur; cur = cur->next) {
10576 referstatus = "";
10577 if (cur->refer) {
10578 referstatus = referstatus2str(cur->refer->status);
10579 }
10580 if (cur->subscribed == NONE && !subscriptions) {
10581 ast_cli(fd, FORMAT, ast_inet_ntoa(cur->sa.sin_addr),
10582 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
10583 cur->callid,
10584 cur->ocseq, cur->icseq,
10585 ast_getformatname(cur->owner ? cur->owner->nativeformats : 0),
10586 ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD) ? "Yes" : "No",
10587 ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY) ? "(d)" : "",
10588 cur->lastmsg ,
10589 referstatus
10590 );
10591 numchans++;
10592 }
10593 if (cur->subscribed != NONE && subscriptions) {
10594 ast_cli(fd, FORMAT3, ast_inet_ntoa(cur->sa.sin_addr),
10595 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
10596 cur->callid,
10597
10598 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
10599 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
10600 subscription_type2str(cur->subscribed),
10601 cur->subscribed == MWI_NOTIFICATION ? (cur->relatedpeer ? cur->relatedpeer->mailbox : "<none>") : "<none>"
10602 );
10603 numchans++;
10604 }
10605 }
10606 ast_mutex_unlock(&iflock);
10607 if (!subscriptions)
10608 ast_cli(fd, "%d active SIP channel%s\n", numchans, (numchans != 1) ? "s" : "");
10609 else
10610 ast_cli(fd, "%d active SIP subscription%s\n", numchans, (numchans != 1) ? "s" : "");
10611 return RESULT_SUCCESS;
10612 #undef FORMAT
10613 #undef FORMAT2
10614 #undef FORMAT3
10615 }
10616
10617
10618 static char *complete_sipch(const char *line, const char *word, int pos, int state)
10619 {
10620 int which=0;
10621 struct sip_pvt *cur;
10622 char *c = NULL;
10623 int wordlen = strlen(word);
10624
10625 ast_mutex_lock(&iflock);
10626 for (cur = iflist; cur; cur = cur->next) {
10627 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
10628 c = ast_strdup(cur->callid);
10629 break;
10630 }
10631 }
10632 ast_mutex_unlock(&iflock);
10633 return c;
10634 }
10635
10636
10637 static char *complete_sip_peer(const char *word, int state, int flags2)
10638 {
10639 char *result = NULL;
10640 int wordlen = strlen(word);
10641 int which = 0;
10642
10643 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
10644
10645 if (!strncasecmp(word, iterator->name, wordlen) &&
10646 (!flags2 || ast_test_flag(&iterator->flags[1], flags2)) &&
10647 ++which > state)
10648 result = ast_strdup(iterator->name);
10649 } while(0) );
10650 return result;
10651 }
10652
10653
10654 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
10655 {
10656 if (pos == 3)
10657 return complete_sip_peer(word, state, 0);
10658
10659 return NULL;
10660 }
10661
10662
10663 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state)
10664 {
10665 if (pos == 3)
10666 return complete_sip_peer(word, state, 0);
10667
10668 return NULL;
10669 }
10670
10671
10672 static char *complete_sip_user(const char *word, int state, int flags2)
10673 {
10674 char *result = NULL;
10675 int wordlen = strlen(word);
10676 int which = 0;
10677
10678 ASTOBJ_CONTAINER_TRAVERSE(&userl, !result, do {
10679
10680 if (!strncasecmp(word, iterator->name, wordlen)) {
10681 if (flags2 && !ast_test_flag(&iterator->flags[1], flags2))
10682 continue;
10683 if (++which > state) {
10684 result = ast_strdup(iterator->name);
10685 }
10686 }
10687 } while(0) );
10688 return result;
10689 }
10690
10691
10692 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
10693 {
10694 if (pos == 3)
10695 return complete_sip_user(word, state, 0);
10696
10697 return NULL;
10698 }
10699
10700
10701 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
10702 {
10703 char *c = NULL;
10704
10705 if (pos == 2) {
10706 int which = 0;
10707 char *cat = NULL;
10708 int wordlen = strlen(word);
10709
10710
10711
10712 if (!notify_types)
10713 return NULL;
10714
10715 while ( (cat = ast_category_browse(notify_types, cat)) ) {
10716 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
10717 c = ast_strdup(cat);
10718 break;
10719 }
10720 }
10721 return c;
10722 }
10723
10724 if (pos > 2)
10725 return complete_sip_peer(word, state, 0);
10726
10727 return NULL;
10728 }
10729
10730
10731 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state)
10732 {
10733 if (pos == 4)
10734 return complete_sip_peer(word, state, SIP_PAGE2_RTCACHEFRIENDS);
10735 return NULL;
10736 }
10737
10738
10739 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state)
10740 {
10741 if (pos == 4)
10742 return complete_sip_user(word, state, SIP_PAGE2_RTCACHEFRIENDS);
10743
10744 return NULL;
10745 }
10746
10747
10748 static int sip_show_channel(int fd, int argc, char *argv[])
10749 {
10750 struct sip_pvt *cur;
10751 size_t len;
10752 int found = 0;
10753
10754 if (argc != 4)
10755 return RESULT_SHOWUSAGE;
10756 len = strlen(argv[3]);
10757 ast_mutex_lock(&iflock);
10758 for (cur = iflist; cur; cur = cur->next) {
10759 if (!strncasecmp(cur->callid, argv[3], len)) {
10760 char formatbuf[BUFSIZ/2];
10761 ast_cli(fd,"\n");
10762 if (cur->subscribed != NONE)
10763 ast_cli(fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
10764 else
10765 ast_cli(fd, " * SIP Call\n");
10766 ast_cli(fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
10767 ast_cli(fd, " Call-ID: %s\n", cur->callid);
10768 ast_cli(fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
10769 ast_cli(fd, " Our Codec Capability: %d\n", cur->capability);
10770 ast_cli(fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
10771 ast_cli(fd, " Their Codec Capability: %d\n", cur->peercapability);
10772 ast_cli(fd, " Joint Codec Capability: %d\n", cur->jointcapability);
10773 ast_cli(fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
10774 ast_cli(fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
10775 ast_cli(fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(cur->sa.sin_addr), ntohs(cur->sa.sin_port));
10776 ast_cli(fd, " Received Address: %s:%d\n", ast_inet_ntoa(cur->recv.sin_addr), ntohs(cur->recv.sin_port));
10777 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
10778 ast_cli(fd, " NAT Support: %s\n", nat2str(ast_test_flag(&cur->flags[0], SIP_NAT)));
10779 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)" );
10780 ast_cli(fd, " Our Tag: %s\n", cur->tag);
10781 ast_cli(fd, " Their Tag: %s\n", cur->theirtag);
10782 ast_cli(fd, " SIP User agent: %s\n", cur->useragent);
10783 if (!ast_strlen_zero(cur->username))
10784 ast_cli(fd, " Username: %s\n", cur->username);
10785 if (!ast_strlen_zero(cur->peername))
10786 ast_cli(fd, " Peername: %s\n", cur->peername);
10787 if (!ast_strlen_zero(cur->uri))
10788 ast_cli(fd, " Original uri: %s\n", cur->uri);
10789 if (!ast_strlen_zero(cur->cid_num))
10790 ast_cli(fd, " Caller-ID: %s\n", cur->cid_num);
10791 ast_cli(fd, " Need Destroy: %d\n", ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY));
10792 ast_cli(fd, " Last Message: %s\n", cur->lastmsg);
10793 ast_cli(fd, " Promiscuous Redir: %s\n", ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
10794 ast_cli(fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
10795 ast_cli(fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
10796 ast_cli(fd, " SIP Options: ");
10797 if (cur->sipoptions) {
10798 int x;
10799 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
10800 if (cur->sipoptions & sip_options[x].id)
10801 ast_cli(fd, "%s ", sip_options[x].text);
10802 }
10803 } else
10804 ast_cli(fd, "(none)\n");
10805 ast_cli(fd, "\n\n");
10806 found++;
10807 }
10808 }
10809 ast_mutex_unlock(&iflock);
10810 if (!found)
10811 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
10812 return RESULT_SUCCESS;
10813 }
10814
10815
10816 static int sip_show_history(int fd, int argc, char *argv[])
10817 {
10818 struct sip_pvt *cur;
10819 size_t len;
10820 int found = 0;
10821
10822 if (argc != 4)
10823 return RESULT_SHOWUSAGE;
10824 if (!recordhistory)
10825 ast_cli(fd, "\n***Note: History recording is currently DISABLED. Use 'sip history' to ENABLE.\n");
10826 len = strlen(argv[3]);
10827 ast_mutex_lock(&iflock);
10828 for (cur = iflist; cur; cur = cur->next) {
10829 if (!strncasecmp(cur->callid, argv[3], len)) {
10830 struct sip_history *hist;
10831 int x = 0;
10832
10833 ast_cli(fd,"\n");
10834 if (cur->subscribed != NONE)
10835 ast_cli(fd, " * Subscription\n");
10836 else
10837 ast_cli(fd, " * SIP Call\n");
10838 if (cur->history)
10839 AST_LIST_TRAVERSE(cur->history, hist, list)
10840 ast_cli(fd, "%d. %s\n", ++x, hist->event);
10841 if (x == 0)
10842 ast_cli(fd, "Call '%s' has no history\n", cur->callid);
10843 found++;
10844 }
10845 }
10846 ast_mutex_unlock(&iflock);
10847 if (!found)
10848 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
10849 return RESULT_SUCCESS;
10850 }
10851
10852
10853 static void sip_dump_history(struct sip_pvt *dialog)
10854 {
10855 int x = 0;
10856 struct sip_history *hist;
10857 static int errmsg = 0;
10858
10859 if (!dialog)
10860 return;
10861
10862 if (!option_debug && !sipdebug) {
10863 if (!errmsg) {
10864 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
10865 errmsg = 1;
10866 }
10867 return;
10868 }
10869
10870 ast_log(LOG_DEBUG, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
10871 if (dialog->subscribed)
10872 ast_log(LOG_DEBUG, " * Subscription\n");
10873 else
10874 ast_log(LOG_DEBUG, " * SIP Call\n");
10875 if (dialog->history)
10876 AST_LIST_TRAVERSE(dialog->history, hist, list)
10877 ast_log(LOG_DEBUG, " %-3.3d. %s\n", ++x, hist->event);
10878 if (!x)
10879 ast_log(LOG_DEBUG, "Call '%s' has no history\n", dialog->callid);
10880 ast_log(LOG_DEBUG, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
10881 }
10882
10883
10884
10885
10886 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
10887 {
10888 char buf[1024];
10889 unsigned int event;
10890 const char *c = get_header(req, "Content-Type");
10891
10892
10893 if (!strcasecmp(c, "application/dtmf-relay") ||
10894 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
10895 unsigned int duration = 0;
10896
10897
10898 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
10899 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
10900 transmit_response(p, "200 OK", req);
10901 return;
10902 } else {
10903 ast_copy_string(buf, c, sizeof(buf));
10904 }
10905
10906 if (!ast_strlen_zero((c = get_body(req, "Duration"))))
10907 duration = atoi(c);
10908 if (!duration)
10909 duration = 100;
10910
10911 if (!p->owner) {
10912 transmit_response(p, "481 Call leg/transaction does not exist", req);
10913 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10914 return;
10915 }
10916
10917 if (ast_strlen_zero(buf)) {
10918 transmit_response(p, "200 OK", req);
10919 return;
10920 }
10921
10922 if (buf[0] == '*')
10923 event = 10;
10924 else if (buf[0] == '#')
10925 event = 11;
10926 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
10927 event = 12 + buf[0] - 'A';
10928 else
10929 event = atoi(buf);
10930 if (event == 16) {
10931
10932 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
10933 ast_queue_frame(p->owner, &f);
10934 if (sipdebug)
10935 ast_verbose("* DTMF-relay event received: FLASH\n");
10936 } else {
10937
10938 struct ast_frame f = { AST_FRAME_DTMF, };
10939 if (event < 10) {
10940 f.subclass = '0' + event;
10941 } else if (event < 11) {
10942 f.subclass = '*';
10943 } else if (event < 12) {
10944 f.subclass = '#';
10945 } else if (event < 16) {
10946 f.subclass = 'A' + (event - 12);
10947 }
10948 f.len = duration;
10949 ast_queue_frame(p->owner, &f);
10950 if (sipdebug)
10951 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
10952 }
10953 transmit_response(p, "200 OK", req);
10954 return;
10955 } else if (!strcasecmp(c, "application/media_control+xml")) {
10956
10957 if (p->owner)
10958 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
10959 transmit_response(p, "200 OK", req);
10960 return;
10961 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
10962
10963 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
10964 if (p->owner && p->owner->cdr)
10965 ast_cdr_setuserfield(p->owner, c);
10966 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
10967 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
10968 transmit_response(p, "200 OK", req);
10969 } else {
10970 transmit_response(p, "403 Unauthorized", req);
10971 }
10972 return;
10973 }
10974
10975
10976
10977 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
10978 transmit_response(p, "415 Unsupported media type", req);
10979 return;
10980 }
10981
10982
10983 static int sip_do_debug_ip(int fd, int argc, char *argv[])
10984 {
10985 struct hostent *hp;
10986 struct ast_hostent ahp;
10987 int port = 0;
10988 char *p, *arg;
10989
10990
10991 if (argc != 5)
10992 return RESULT_SHOWUSAGE;
10993 p = arg = argv[4];
10994 strsep(&p, ":");
10995 if (p)
10996 port = atoi(p);
10997 hp = ast_gethostbyname(arg, &ahp);
10998 if (hp == NULL)
10999 return RESULT_SHOWUSAGE;
11000
11001 debugaddr.sin_family = AF_INET;
11002 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
11003 debugaddr.sin_port = htons(port);
11004 if (port == 0)
11005 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(debugaddr.sin_addr));
11006 else
11007 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), port);
11008
11009 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11010
11011 return RESULT_SUCCESS;
11012 }
11013
11014
11015 static int sip_do_debug_peer(int fd, int argc, char *argv[])
11016 {
11017 struct sip_peer *peer;
11018 if (argc != 5)
11019 return RESULT_SHOWUSAGE;
11020 peer = find_peer(argv[4], NULL, 1);
11021 if (peer) {
11022 if (peer->addr.sin_addr.s_addr) {
11023 debugaddr.sin_family = AF_INET;
11024 debugaddr.sin_addr = peer->addr.sin_addr;
11025 debugaddr.sin_port = peer->addr.sin_port;
11026 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
11027 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11028 } else
11029 ast_cli(fd, "Unable to get IP address of peer '%s'\n", argv[4]);
11030 ASTOBJ_UNREF(peer,sip_destroy_peer);
11031 } else
11032 ast_cli(fd, "No such peer '%s'\n", argv[4]);
11033 return RESULT_SUCCESS;
11034 }
11035
11036
11037 static int sip_do_debug(int fd, int argc, char *argv[])
11038 {
11039 int oldsipdebug = sipdebug_console;
11040 if (argc != 3) {
11041 if (argc != 5)
11042 return RESULT_SHOWUSAGE;
11043 else if (strcmp(argv[3], "ip") == 0)
11044 return sip_do_debug_ip(fd, argc, argv);
11045 else if (strcmp(argv[3], "peer") == 0)
11046 return sip_do_debug_peer(fd, argc, argv);
11047 else
11048 return RESULT_SHOWUSAGE;
11049 }
11050 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11051 memset(&debugaddr, 0, sizeof(debugaddr));
11052 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
11053 return RESULT_SUCCESS;
11054 }
11055
11056 static int sip_do_debug_deprecated(int fd, int argc, char *argv[])
11057 {
11058 int oldsipdebug = sipdebug_console;
11059 char *newargv[6] = { "sip", "set", "debug", NULL };
11060 if (argc != 2) {
11061 if (argc != 4)
11062 return RESULT_SHOWUSAGE;
11063 else if (strcmp(argv[2], "ip") == 0) {
11064 newargv[3] = argv[2];
11065 newargv[4] = argv[3];
11066 return sip_do_debug_ip(fd, argc + 1, newargv);
11067 } else if (strcmp(argv[2], "peer") == 0) {
11068 newargv[3] = argv[2];
11069 newargv[4] = argv[3];
11070 return sip_do_debug_peer(fd, argc + 1, newargv);
11071 } else
11072 return RESULT_SHOWUSAGE;
11073 }
11074 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11075 memset(&debugaddr, 0, sizeof(debugaddr));
11076 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
11077 return RESULT_SUCCESS;
11078 }
11079
11080
11081 static int sip_notify(int fd, int argc, char *argv[])
11082 {
11083 struct ast_variable *varlist;
11084 int i;
11085
11086 if (argc < 4)
11087 return RESULT_SHOWUSAGE;
11088
11089 if (!notify_types) {
11090 ast_cli(fd, "No %s file found, or no types listed there\n", notify_config);
11091 return RESULT_FAILURE;
11092 }
11093
11094 varlist = ast_variable_browse(notify_types, argv[2]);
11095
11096 if (!varlist) {
11097 ast_cli(fd, "Unable to find notify type '%s'\n", argv[2]);
11098 return RESULT_FAILURE;
11099 }
11100
11101 for (i = 3; i < argc; i++) {
11102 struct sip_pvt *p;
11103 struct sip_request req;
11104 struct ast_variable *var;
11105
11106 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY))) {
11107 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
11108 return RESULT_FAILURE;
11109 }
11110
11111 if (create_addr(p, argv[i])) {
11112
11113 sip_destroy(p);
11114 ast_cli(fd, "Could not create address for '%s'\n", argv[i]);
11115 continue;
11116 }
11117
11118 initreqprep(&req, p, SIP_NOTIFY);
11119
11120 for (var = varlist; var; var = var->next)
11121 add_header(&req, var->name, var->value);
11122
11123
11124 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
11125 p->ourip = __ourip;
11126 build_via(p);
11127 build_callid_pvt(p);
11128 ast_cli(fd, "Sending NOTIFY of type '%s' to '%s'\n", argv[2], argv[i]);
11129 transmit_sip_request(p, &req);
11130 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11131 }
11132
11133 return RESULT_SUCCESS;
11134 }
11135
11136
11137 static int sip_no_debug(int fd, int argc, char *argv[])
11138 {
11139 if (argc != 4)
11140 return RESULT_SHOWUSAGE;
11141 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11142 ast_cli(fd, "SIP Debugging Disabled\n");
11143 return RESULT_SUCCESS;
11144 }
11145
11146 static int sip_no_debug_deprecated(int fd, int argc, char *argv[])
11147 {
11148 if (argc != 3)
11149 return RESULT_SHOWUSAGE;
11150 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11151 ast_cli(fd, "SIP Debugging Disabled\n");
11152 return RESULT_SUCCESS;
11153 }
11154
11155
11156 static int sip_do_history(int fd, int argc, char *argv[])
11157 {
11158 if (argc != 2) {
11159 return RESULT_SHOWUSAGE;
11160 }
11161 recordhistory = TRUE;
11162 ast_cli(fd, "SIP History Recording Enabled (use 'sip show history')\n");
11163 return RESULT_SUCCESS;
11164 }
11165
11166
11167 static int sip_no_history(int fd, int argc, char *argv[])
11168 {
11169 if (argc != 3) {
11170 return RESULT_SHOWUSAGE;
11171 }
11172 recordhistory = FALSE;
11173 ast_cli(fd, "SIP History Recording Disabled\n");
11174 return RESULT_SUCCESS;
11175 }
11176
11177
11178 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader)
11179 {
11180 char digest[1024];
11181 p->authtries++;
11182 memset(digest,0,sizeof(digest));
11183 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
11184
11185
11186 if (sip_debug_test_pvt(p) && p->registry)
11187 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
11188
11189 return -1;
11190 }
11191 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
11192 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
11193 if (sip_debug_test_pvt(p) && p->registry)
11194 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
11195 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
11196 }
11197
11198
11199 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader, int sipmethod, int init)
11200 {
11201 char digest[1024];
11202
11203 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
11204 return -2;
11205
11206 p->authtries++;
11207 if (option_debug > 1)
11208 ast_log(LOG_DEBUG, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
11209 memset(digest, 0, sizeof(digest));
11210 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
11211
11212 return -1;
11213 }
11214
11215 p->options->auth = digest;
11216 p->options->authheader = respheader;
11217 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
11218 }
11219
11220
11221
11222
11223
11224 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
11225 {
11226 char tmp[512];
11227 char *c;
11228 char oldnonce[256];
11229
11230
11231 const struct x {
11232 const char *key;
11233 int field_index;
11234 } *i, keys[] = {
11235 { "realm=", ast_string_field_index(p, realm) },
11236 { "nonce=", ast_string_field_index(p, nonce) },
11237 { "opaque=", ast_string_field_index(p, opaque) },
11238 { "qop=", ast_string_field_index(p, qop) },
11239 { "domain=", ast_string_field_index(p, domain) },
11240 { NULL, 0 },
11241 };
11242
11243 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
11244 if (ast_strlen_zero(tmp))
11245 return -1;
11246 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
11247 ast_log(LOG_WARNING, "missing Digest.\n");
11248 return -1;
11249 }
11250 c = tmp + strlen("Digest ");
11251 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
11252 while (c && *(c = ast_skip_blanks(c))) {
11253 for (i = keys; i->key != NULL; i++) {
11254 char *src, *separator;
11255 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
11256 continue;
11257
11258 c += strlen(i->key);
11259 if (*c == '"') {
11260 src = ++c;
11261 separator = "\"";
11262 } else {
11263 src = c;
11264 separator = ",";
11265 }
11266 strsep(&c, separator);
11267 ast_string_field_index_set(p, i->field_index, src);
11268 break;
11269 }
11270 if (i->key == NULL)
11271 strsep(&c, ",");
11272 }
11273
11274 if (strcmp(p->nonce, oldnonce))
11275 p->noncecount = 0;
11276
11277
11278 if (p->registry) {
11279 struct sip_registry *r = p->registry;
11280
11281 if (strcmp(r->nonce, p->nonce)) {
11282 ast_string_field_set(r, realm, p->realm);
11283 ast_string_field_set(r, nonce, p->nonce);
11284 ast_string_field_set(r, domain, p->domain);
11285 ast_string_field_set(r, opaque, p->opaque);
11286 ast_string_field_set(r, qop, p->qop);
11287 r->noncecount = 0;
11288 }
11289 }
11290 return build_reply_digest(p, sipmethod, digest, digest_len);
11291 }
11292
11293
11294
11295
11296
11297
11298 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
11299 {
11300 char a1[256];
11301 char a2[256];
11302 char a1_hash[256];
11303 char a2_hash[256];
11304 char resp[256];
11305 char resp_hash[256];
11306 char uri[256];
11307 char cnonce[80];
11308 const char *username;
11309 const char *secret;
11310 const char *md5secret;
11311 struct sip_auth *auth = NULL;
11312
11313 if (!ast_strlen_zero(p->domain))
11314 ast_copy_string(uri, p->domain, sizeof(uri));
11315 else if (!ast_strlen_zero(p->uri))
11316 ast_copy_string(uri, p->uri, sizeof(uri));
11317 else
11318 snprintf(uri, sizeof(uri), "sip:%s@%s",p->username, ast_inet_ntoa(p->sa.sin_addr));
11319
11320 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
11321
11322
11323 if ((auth = find_realm_authentication(authl, p->realm))) {
11324 ast_log(LOG_WARNING, "use realm [%s] from peer [%s][%s]\n",
11325 auth->username, p->peername, p->username);
11326 username = auth->username;
11327 secret = auth->secret;
11328 md5secret = auth->md5secret;
11329 if (sipdebug)
11330 ast_log(LOG_DEBUG,"Using realm %s authentication for call %s\n", p->realm, p->callid);
11331 } else {
11332
11333 username = p->authname;
11334 secret = p->peersecret;
11335 md5secret = p->peermd5secret;
11336 }
11337 if (ast_strlen_zero(username))
11338 return -1;
11339
11340
11341 snprintf(a1,sizeof(a1),"%s:%s:%s", username, p->realm, secret);
11342 snprintf(a2,sizeof(a2),"%s:%s", sip_methods[method].text, uri);
11343 if (!ast_strlen_zero(md5secret))
11344 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
11345 else
11346 ast_md5_hash(a1_hash,a1);
11347 ast_md5_hash(a2_hash,a2);
11348
11349 p->noncecount++;
11350 if (!ast_strlen_zero(p->qop))
11351 snprintf(resp,sizeof(resp),"%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
11352 else
11353 snprintf(resp,sizeof(resp),"%s:%s:%s", a1_hash, p->nonce, a2_hash);
11354 ast_md5_hash(resp_hash, resp);
11355
11356 if (!ast_strlen_zero(p->qop))
11357 snprintf(digest, digest_len, "Digest username=\"%s\", realm=\"%s\", algorithm=MD5, uri=\"%s\", nonce=\"%s\", response=\"%s\", opaque=\"%s\", qop=auth, cnonce=\"%s\", nc=%08x", username, p->realm, uri, p->nonce, resp_hash, p->opaque, cnonce, p->noncecount);
11358 else
11359 snprintf(digest, digest_len, "Digest username=\"%s\", realm=\"%s\", algorithm=MD5, uri=\"%s\", nonce=\"%s\", response=\"%s\", opaque=\"%s\"", username, p->realm, uri, p->nonce, resp_hash, p->opaque);
11360
11361 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
11362
11363 return 0;
11364 }
11365
11366 static char show_domains_usage[] =
11367 "Usage: sip show domains\n"
11368 " Lists all configured SIP local domains.\n"
11369 " Asterisk only responds to SIP messages to local domains.\n";
11370
11371 static char notify_usage[] =
11372 "Usage: sip notify <type> <peer> [<peer>...]\n"
11373 " Send a NOTIFY message to a SIP peer or peers\n"
11374 " Message types are defined in sip_notify.conf\n";
11375
11376 static char show_users_usage[] =
11377 "Usage: sip show users [like <pattern>]\n"
11378 " Lists all known SIP users.\n"
11379 " Optional regular expression pattern is used to filter the user list.\n";
11380
11381 static char show_user_usage[] =
11382 "Usage: sip show user <name> [load]\n"
11383 " Shows all details on one SIP user and the current status.\n"
11384 " Option \"load\" forces lookup of peer in realtime storage.\n";
11385
11386 static char show_inuse_usage[] =
11387 "Usage: sip show inuse [all]\n"
11388 " List all SIP users and peers usage counters and limits.\n"
11389 " Add option \"all\" to show all devices, not only those with a limit.\n";
11390
11391 static char show_channels_usage[] =
11392 "Usage: sip show channels\n"
11393 " Lists all currently active SIP channels.\n";
11394
11395 static char show_channel_usage[] =
11396 "Usage: sip show channel <channel>\n"
11397 " Provides detailed status on a given SIP channel.\n";
11398
11399 static char show_history_usage[] =
11400 "Usage: sip show history <channel>\n"
11401 " Provides detailed dialog history on a given SIP channel.\n";
11402
11403 static char show_peers_usage[] =
11404 "Usage: sip show peers [like <pattern>]\n"
11405 " Lists all known SIP peers.\n"
11406 " Optional regular expression pattern is used to filter the peer list.\n";
11407
11408 static char show_peer_usage[] =
11409 "Usage: sip show peer <name> [load]\n"
11410 " Shows all details on one SIP peer and the current status.\n"
11411 " Option \"load\" forces lookup of peer in realtime storage.\n";
11412
11413 static char prune_realtime_usage[] =
11414 "Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
11415 " Prunes object(s) from the cache.\n"
11416 " Optional regular expression pattern is used to filter the objects.\n";
11417
11418 static char show_reg_usage[] =
11419 "Usage: sip show registry\n"
11420 " Lists all registration requests and status.\n";
11421
11422 static char debug_usage[] =
11423 "Usage: sip set debug\n"
11424 " Enables dumping of SIP packets for debugging purposes\n\n"
11425 " sip set debug ip <host[:PORT]>\n"
11426 " Enables dumping of SIP packets to and from host.\n\n"
11427 " sip set debug peer <peername>\n"
11428 " Enables dumping of SIP packets to and from host.\n"
11429 " Require peer to be registered.\n";
11430
11431 static char no_debug_usage[] =
11432 "Usage: sip set debug off\n"
11433 " Disables dumping of SIP packets for debugging purposes\n";
11434
11435 static char no_history_usage[] =
11436 "Usage: sip history off\n"
11437 " Disables recording of SIP dialog history for debugging purposes\n";
11438
11439 static char history_usage[] =
11440 "Usage: sip history\n"
11441 " Enables recording of SIP dialog history for debugging purposes.\n"
11442 "Use 'sip show history' to view the history of a call number.\n";
11443
11444 static char sip_reload_usage[] =
11445 "Usage: sip reload\n"
11446 " Reloads SIP configuration from sip.conf\n";
11447
11448 static char show_subscriptions_usage[] =
11449 "Usage: sip show subscriptions\n"
11450 " Lists active SIP subscriptions for extension states\n";
11451
11452 static char show_objects_usage[] =
11453 "Usage: sip show objects\n"
11454 " Lists status of known SIP objects\n";
11455
11456 static char show_settings_usage[] =
11457 "Usage: sip show settings\n"
11458 " Provides detailed list of the configuration of the SIP channel.\n";
11459
11460
11461 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
11462 {
11463 struct sip_pvt *p;
11464 const char *content = NULL;
11465 AST_DECLARE_APP_ARGS(args,
11466 AST_APP_ARG(header);
11467 AST_APP_ARG(number);
11468 );
11469 int i, number, start = 0;
11470
11471 if (ast_strlen_zero(data)) {
11472 ast_log(LOG_WARNING, "This function requires a header name.\n");
11473 return -1;
11474 }
11475
11476 ast_channel_lock(chan);
11477 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
11478 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
11479 ast_channel_unlock(chan);
11480 return -1;
11481 }
11482
11483 AST_STANDARD_APP_ARGS(args, data);
11484 if (!args.number) {
11485 number = 1;
11486 } else {
11487 sscanf(args.number, "%d", &number);
11488 if (number < 1)
11489 number = 1;
11490 }
11491
11492 p = chan->tech_pvt;
11493
11494
11495 if (!p) {
11496 ast_channel_unlock(chan);
11497 return -1;
11498 }
11499
11500 for (i = 0; i < number; i++)
11501 content = __get_header(&p->initreq, args.header, &start);
11502
11503 if (ast_strlen_zero(content)) {
11504 ast_channel_unlock(chan);
11505 return -1;
11506 }
11507
11508 ast_copy_string(buf, content, len);
11509 ast_channel_unlock(chan);
11510
11511 return 0;
11512 }
11513
11514 static struct ast_custom_function sip_header_function = {
11515 .name = "SIP_HEADER",
11516 .synopsis = "Gets the specified SIP header",
11517 .syntax = "SIP_HEADER(<name>[,<number>])",
11518 .desc = "Since there are several headers (such as Via) which can occur multiple\n"
11519 "times, SIP_HEADER takes an optional second argument to specify which header with\n"
11520 "that name to retrieve. Headers start at offset 1.\n",
11521 .read = func_header_read,
11522 };
11523
11524
11525 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11526 {
11527 if (ast_strlen_zero(data)) {
11528 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
11529 return -1;
11530 }
11531 if (check_sip_domain(data, NULL, 0))
11532 ast_copy_string(buf, data, len);
11533 else
11534 buf[0] = '\0';
11535 return 0;
11536 }
11537
11538 static struct ast_custom_function checksipdomain_function = {
11539 .name = "CHECKSIPDOMAIN",
11540 .synopsis = "Checks if domain is a local domain",
11541 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
11542 .read = func_check_sipdomain,
11543 .desc = "This function checks if the domain in the argument is configured\n"
11544 "as a local SIP domain that this Asterisk server is configured to handle.\n"
11545 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
11546 "Check the domain= configuration in sip.conf\n",
11547 };
11548
11549
11550 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11551 {
11552 struct sip_peer *peer;
11553 char *colname;
11554
11555 if ((colname = strchr(data, ':')))
11556 *colname++ = '\0';
11557 else if ((colname = strchr(data, '|')))
11558 *colname++ = '\0';
11559 else
11560 colname = "ip";
11561
11562 if (!(peer = find_peer(data, NULL, 1)))
11563 return -1;
11564
11565 if (!strcasecmp(colname, "ip")) {
11566 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
11567 } else if (!strcasecmp(colname, "status")) {
11568 peer_status(peer, buf, len);
11569 } else if (!strcasecmp(colname, "language")) {
11570 ast_copy_string(buf, peer->language, len);
11571 } else if (!strcasecmp(colname, "regexten")) {
11572 ast_copy_string(buf, peer->regexten, len);
11573 } else if (!strcasecmp(colname, "limit")) {
11574 snprintf(buf, len, "%d", peer->call_limit);
11575 } else if (!strcasecmp(colname, "curcalls")) {
11576 snprintf(buf, len, "%d", peer->inUse);
11577 } else if (!strcasecmp(colname, "accountcode")) {
11578 ast_copy_string(buf, peer->accountcode, len);
11579 } else if (!strcasecmp(colname, "useragent")) {
11580 ast_copy_string(buf, peer->useragent, len);
11581 } else if (!strcasecmp(colname, "mailbox")) {
11582 ast_copy_string(buf, peer->mailbox, len);
11583 } else if (!strcasecmp(colname, "context")) {
11584 ast_copy_string(buf, peer->context, len);
11585 } else if (!strcasecmp(colname, "expire")) {
11586 snprintf(buf, len, "%d", peer->expire);
11587 } else if (!strcasecmp(colname, "dynamic")) {
11588 ast_copy_string(buf, (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no"), len);
11589 } else if (!strcasecmp(colname, "callerid_name")) {
11590 ast_copy_string(buf, peer->cid_name, len);
11591 } else if (!strcasecmp(colname, "callerid_num")) {
11592 ast_copy_string(buf, peer->cid_num, len);
11593 } else if (!strcasecmp(colname, "codecs")) {
11594 ast_getformatname_multiple(buf, len -1, peer->capability);
11595 } else if (!strncasecmp(colname, "codec[", 6)) {
11596 char *codecnum;
11597 int index = 0, codec = 0;
11598
11599 codecnum = colname + 6;
11600 codecnum = strsep(&codecnum, "]");
11601 index = atoi(codecnum);
11602 if((codec = ast_codec_pref_index(&peer->prefs, index))) {
11603 ast_copy_string(buf, ast_getformatname(codec), len);
11604 }
11605 }
11606
11607 ASTOBJ_UNREF(peer, sip_destroy_peer);
11608
11609 return 0;
11610 }
11611
11612
11613 struct ast_custom_function sippeer_function = {
11614 .name = "SIPPEER",
11615 .synopsis = "Gets SIP peer information",
11616 .syntax = "SIPPEER(<peername>[|item])",
11617 .read = function_sippeer,
11618 .desc = "Valid items are:\n"
11619 "- ip (default) The IP address.\n"
11620 "- mailbox The configured mailbox.\n"
11621 "- context The configured context.\n"
11622 "- expire The epoch time of the next expire.\n"
11623 "- dynamic Is it dynamic? (yes/no).\n"
11624 "- callerid_name The configured Caller ID name.\n"
11625 "- callerid_num The configured Caller ID number.\n"
11626 "- codecs The configured codecs.\n"
11627 "- status Status (if qualify=yes).\n"
11628 "- regexten Registration extension\n"
11629 "- limit Call limit (call-limit)\n"
11630 "- curcalls Current amount of calls \n"
11631 " Only available if call-limit is set\n"
11632 "- language Default language for peer\n"
11633 "- accountcode Account code for this peer\n"
11634 "- useragent Current user agent id for peer\n"
11635 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
11636 "\n"
11637 };
11638
11639
11640 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11641 {
11642 struct sip_pvt *p;
11643
11644 *buf = 0;
11645
11646 if (!data) {
11647 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
11648 return -1;
11649 }
11650
11651 ast_channel_lock(chan);
11652 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
11653 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
11654 ast_channel_unlock(chan);
11655 return -1;
11656 }
11657
11658 p = chan->tech_pvt;
11659
11660
11661 if (!p) {
11662 ast_channel_unlock(chan);
11663 return -1;
11664 }
11665
11666 if (!strcasecmp(data, "peerip")) {
11667 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", len);
11668 } else if (!strcasecmp(data, "recvip")) {
11669 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", len);
11670 } else if (!strcasecmp(data, "from")) {
11671 ast_copy_string(buf, p->from, len);
11672 } else if (!strcasecmp(data, "uri")) {
11673 ast_copy_string(buf, p->uri, len);
11674 } else if (!strcasecmp(data, "useragent")) {
11675 ast_copy_string(buf, p->useragent, len);
11676 } else if (!strcasecmp(data, "peername")) {
11677 ast_copy_string(buf, p->peername, len);
11678 } else if (!strcasecmp(data, "t38passthrough")) {
11679 if (p->t38.state == T38_DISABLED)
11680 ast_copy_string(buf, "0", sizeof("0"));
11681 else
11682 ast_copy_string(buf, "1", sizeof("1"));
11683 } else {
11684 ast_channel_unlock(chan);
11685 return -1;
11686 }
11687 ast_channel_unlock(chan);
11688
11689 return 0;
11690 }
11691
11692
11693 static struct ast_custom_function sipchaninfo_function = {
11694 .name = "SIPCHANINFO",
11695 .synopsis = "Gets the specified SIP parameter from the current channel",
11696 .syntax = "SIPCHANINFO(item)",
11697 .read = function_sipchaninfo_read,
11698 .desc = "Valid items are:\n"
11699 "- peerip The IP address of the peer.\n"
11700 "- recvip The source IP address of the peer.\n"
11701 "- from The URI from the From: header.\n"
11702 "- uri The URI from the Contact: header.\n"
11703 "- useragent The useragent.\n"
11704 "- peername The name of the peer.\n"
11705 "- t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
11706 };
11707
11708
11709 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
11710 {
11711 char tmp[BUFSIZ];
11712 char *s, *e, *uri, *t;
11713 char *domain;
11714
11715 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
11716 if ((t = strchr(tmp, ',')))
11717 *t = '\0';
11718 s = get_in_brackets(tmp);
11719 uri = ast_strdupa(s);
11720 if (ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
11721 if (!strncasecmp(s, "sip:", 4))
11722 s += 4;
11723 e = strchr(s, ';');
11724 if (e)
11725 *e = '\0';
11726 if (option_debug)
11727 ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
11728 if (p->owner)
11729 ast_string_field_build(p->owner, call_forward, "SIP/%s", s);
11730 } else {
11731 e = strchr(tmp, '@');
11732 if (e) {
11733 *e++ = '\0';
11734 domain = e;
11735 } else {
11736
11737 domain = tmp;
11738 }
11739 e = strchr(s, ';');
11740 if (e)
11741 *e = '\0';
11742 e = strchr(domain, ';');
11743 if (e)
11744 *e = '\0';
11745
11746 if (!strncasecmp(s, "sip:", 4))
11747 s += 4;
11748 if (option_debug > 1)
11749 ast_log(LOG_DEBUG, "Received 302 Redirect to extension '%s' (domain %s)\n", s, domain);
11750 if (p->owner) {
11751 pbx_builtin_setvar_helper(p->owner, "SIPREDIRECTURI", uri);
11752 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
11753 ast_string_field_set(p->owner, call_forward, s);
11754 }
11755 }
11756 }
11757
11758
11759 static void check_pendings(struct sip_pvt *p)
11760 {
11761 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
11762
11763 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA)
11764 transmit_request(p, SIP_CANCEL, p->ocseq, XMIT_RELIABLE, FALSE);
11765
11766
11767 else
11768 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
11769 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
11770 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11771 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
11772 if (option_debug)
11773 ast_log(LOG_DEBUG, "Sending pending reinvite on '%s'\n", p->callid);
11774
11775 transmit_reinvite_with_sdp(p);
11776 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
11777 }
11778 }
11779
11780
11781 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
11782 {
11783 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
11784 int res = 0;
11785 int xmitres = 0;
11786 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
11787 struct ast_channel *bridgepeer = NULL;
11788
11789 if (option_debug > 3) {
11790 if (reinvite)
11791 ast_log(LOG_DEBUG, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
11792 else
11793 ast_log(LOG_DEBUG, "SIP response %d to standard invite\n", resp);
11794 }
11795
11796 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
11797 if (option_debug)
11798 ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
11799 return;
11800 }
11801
11802
11803 if (p->initid > -1) {
11804
11805 ast_sched_del(sched, p->initid);
11806 p->initid = -1;
11807 }
11808
11809
11810
11811
11812 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 183)
11813 resp = 183;
11814
11815
11816 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
11817 p->invitestate = INV_PROCEEDING;
11818
11819
11820 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
11821 p->invitestate = INV_COMPLETED;
11822
11823
11824 switch (resp) {
11825 case 100:
11826 case 101:
11827 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11828 sip_cancel_destroy(p);
11829 check_pendings(p);
11830 break;
11831
11832 case 180:
11833 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11834 sip_cancel_destroy(p);
11835 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11836 ast_queue_control(p->owner, AST_CONTROL_RINGING);
11837 if (p->owner->_state != AST_STATE_UP) {
11838 ast_setstate(p->owner, AST_STATE_RINGING);
11839 }
11840 }
11841 if (find_sdp(req)) {
11842 p->invitestate = INV_EARLY_MEDIA;
11843 res = process_sdp(p, req);
11844 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11845
11846 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
11847 }
11848 }
11849 check_pendings(p);
11850 break;
11851
11852 case 183:
11853 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11854 sip_cancel_destroy(p);
11855
11856 if (find_sdp(req)) {
11857 p->invitestate = INV_EARLY_MEDIA;
11858 res = process_sdp(p, req);
11859 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11860
11861 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
11862 }
11863 }
11864 check_pendings(p);
11865 break;
11866
11867 case 200:
11868 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11869 sip_cancel_destroy(p);
11870 p->authtries = 0;
11871 if (find_sdp(req)) {
11872 if ((res = process_sdp(p, req)) && !ast_test_flag(req, SIP_PKT_IGNORE))
11873 if (!reinvite)
11874
11875
11876 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
11877 }
11878
11879
11880
11881
11882 if (outgoing) {
11883 update_call_counter(p, DEC_CALL_RINGING);
11884 parse_ok_contact(p, req);
11885 if(set_address_from_contact(p)) {
11886
11887
11888
11889
11890
11891
11892
11893 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11894 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
11895 }
11896
11897
11898 build_route(p, req, 1);
11899 }
11900
11901 if (p->owner && (p->owner->_state == AST_STATE_UP) && (bridgepeer = ast_bridged_channel(p->owner))) {
11902 struct sip_pvt *bridgepvt = NULL;
11903
11904 if (!bridgepeer->tech) {
11905 ast_log(LOG_WARNING, "Ooooh.. no tech! That's REALLY bad\n");
11906 break;
11907 }
11908 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
11909 bridgepvt = (struct sip_pvt*)(bridgepeer->tech_pvt);
11910 if (bridgepvt->udptl) {
11911 if (p->t38.state == T38_PEER_REINVITE) {
11912 sip_handle_t38_reinvite(bridgepeer, p, 0);
11913 ast_rtp_set_rtptimers_onhold(p->rtp);
11914 if (p->vrtp)
11915 ast_rtp_set_rtptimers_onhold(p->vrtp);
11916 } else if (p->t38.state == T38_DISABLED && bridgepeer && (bridgepvt->t38.state == T38_ENABLED)) {
11917 ast_log(LOG_WARNING, "RTP re-invite after T38 session not handled yet !\n");
11918
11919
11920 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11921 }
11922 } else {
11923 if (option_debug > 1)
11924 ast_log(LOG_DEBUG, "Strange... The other side of the bridge does not have a udptl struct\n");
11925 ast_mutex_lock(&bridgepvt->lock);
11926 bridgepvt->t38.state = T38_DISABLED;
11927 ast_mutex_unlock(&bridgepvt->lock);
11928 if (option_debug)
11929 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->tech->type);
11930 p->t38.state = T38_DISABLED;
11931 if (option_debug > 1)
11932 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
11933 }
11934 } else {
11935
11936 if (option_debug > 1)
11937 ast_log(LOG_DEBUG, "Strange... The other side of the bridge is not a SIP channel\n");
11938 p->t38.state = T38_DISABLED;
11939 if (option_debug > 1)
11940 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
11941 }
11942 }
11943 if ((p->t38.state == T38_LOCAL_REINVITE) || (p->t38.state == T38_LOCAL_DIRECT)) {
11944
11945 p->t38.state = T38_ENABLED;
11946 if (option_debug)
11947 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
11948 }
11949
11950 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11951 if (!reinvite) {
11952 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
11953 } else {
11954 ast_queue_frame(p->owner, &ast_null_frame);
11955 }
11956 } else {
11957
11958
11959
11960 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11961 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
11962 }
11963
11964 p->invitestate = INV_TERMINATED;
11965 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
11966 check_pendings(p);
11967 break;
11968 case 407:
11969 case 401:
11970
11971 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11972 if (p->options)
11973 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
11974
11975
11976 ast_string_field_free(p, theirtag);
11977 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
11978 char *authenticate = (resp == 401 ? "WWW-Authenticate" : "Proxy-Authenticate");
11979 char *authorization = (resp == 401 ? "Authorization" : "Proxy-Authorization");
11980 if (p->authtries < MAX_AUTHTRIES)
11981 p->invitestate = INV_CALLING;
11982 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, authenticate, authorization, SIP_INVITE, 1)) {
11983 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
11984 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11985 sip_alreadygone(p);
11986 if (p->owner)
11987 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11988 }
11989 }
11990 break;
11991
11992 case 403:
11993
11994 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11995 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
11996 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner)
11997 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11998 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11999 sip_alreadygone(p);
12000 break;
12001
12002 case 404:
12003 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12004 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12005 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12006 sip_alreadygone(p);
12007 break;
12008
12009 case 408:
12010 case 481:
12011
12012 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
12013 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12014 if (p->owner)
12015 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12016 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12017 break;
12018 case 487:
12019
12020
12021
12022 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12023 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE)) {
12024 ast_queue_hangup(p->owner);
12025 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
12026 } else if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
12027 update_call_counter(p, DEC_CALL_LIMIT);
12028 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
12029 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12030 sip_alreadygone(p);
12031 }
12032 break;
12033 case 488:
12034 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12035 if (reinvite && p->udptl) {
12036
12037
12038
12039
12040
12041 p->t38.state = T38_DISABLED;
12042
12043 ast_rtp_set_rtptimers_onhold(p->rtp);
12044 ast_log(LOG_ERROR, "Got error on T.38 re-invite. Bad configuration. Peer needs to have T.38 disabled.\n");
12045
12046
12047
12048
12049
12050 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12051 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12052 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12053 } else {
12054
12055 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12056 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12057 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12058 }
12059 break;
12060 case 491:
12061
12062
12063
12064 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12065 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
12066 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12067 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12068 break;
12069
12070 case 501:
12071 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12072 if (p->owner)
12073 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12074 break;
12075 }
12076 if (xmitres == XMIT_ERROR)
12077 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
12078 }
12079
12080
12081
12082
12083 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
12084 {
12085 char *auth = "Proxy-Authenticate";
12086 char *auth2 = "Proxy-Authorization";
12087
12088
12089 if (!p->refer)
12090 return;
12091
12092 switch (resp) {
12093 case 202:
12094
12095
12096 p->refer->status = REFER_ACCEPTED;
12097
12098 if (option_debug > 2)
12099 ast_log(LOG_DEBUG, "Got 202 accepted on transfer\n");
12100
12101 break;
12102
12103 case 401:
12104 case 407:
12105 if (ast_strlen_zero(p->authname)) {
12106 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s:%d but we have no matching peer or realm auth!\n",
12107 ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12108 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12109 }
12110 if (resp == 401) {
12111 auth = "WWW-Authenticate";
12112 auth2 = "Authorization";
12113 }
12114 if ((p->authtries > 1) || do_proxy_auth(p, req, auth, auth2, SIP_REFER, 0)) {
12115 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
12116 p->refer->status = REFER_NOAUTH;
12117 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12118 }
12119 break;
12120 case 481:
12121
12122
12123
12124
12125 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
12126 if (p->owner)
12127 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12128 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12129 break;
12130
12131 case 500:
12132 case 501:
12133
12134
12135 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
12136 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12137 p->refer->status = REFER_FAILED;
12138 break;
12139 case 603:
12140 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
12141 p->refer->status = REFER_FAILED;
12142 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12143 break;
12144 }
12145 }
12146
12147
12148 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
12149 {
12150 int expires, expires_ms;
12151 struct sip_registry *r;
12152 r=p->registry;
12153
12154 switch (resp) {
12155 case 401:
12156 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "WWW-Authenticate", "Authorization")) {
12157 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
12158 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12159 }
12160 break;
12161 case 403:
12162 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
12163 if (global_regattempts_max)
12164 p->registry->regattempts = global_regattempts_max+1;
12165 ast_sched_del(sched, r->timeout);
12166 r->timeout = -1;
12167 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12168 break;
12169 case 404:
12170 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username,p->registry->hostname);
12171 if (global_regattempts_max)
12172 p->registry->regattempts = global_regattempts_max+1;
12173 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12174 r->call = NULL;
12175 ast_sched_del(sched, r->timeout);
12176 r->timeout = -1;
12177 break;
12178 case 407:
12179 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization")) {
12180 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
12181 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12182 }
12183 break;
12184 case 408:
12185 if (global_regattempts_max)
12186 p->registry->regattempts = global_regattempts_max+1;
12187 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12188 r->call = NULL;
12189 ast_sched_del(sched, r->timeout);
12190 r->timeout = -1;
12191 break;
12192 case 479:
12193 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username,p->registry->hostname);
12194 if (global_regattempts_max)
12195 p->registry->regattempts = global_regattempts_max+1;
12196 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12197 r->call = NULL;
12198 ast_sched_del(sched, r->timeout);
12199 r->timeout = -1;
12200 break;
12201 case 200:
12202 if (!r) {
12203 ast_log(LOG_WARNING, "Got 200 OK on REGISTER that isn't a register\n");
12204 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12205 return 0;
12206 }
12207
12208 r->regstate = REG_STATE_REGISTERED;
12209 r->regtime = time(NULL);
12210 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
12211 r->regattempts = 0;
12212 if (option_debug)
12213 ast_log(LOG_DEBUG, "Registration successful\n");
12214 if (r->timeout > -1) {
12215 if (option_debug)
12216 ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
12217 ast_sched_del(sched, r->timeout);
12218 }
12219 r->timeout=-1;
12220 r->call = NULL;
12221 p->registry = NULL;
12222
12223 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12224
12225
12226
12227
12228 if (r->expire > -1)
12229 ast_sched_del(sched, r->expire);
12230
12231
12232 expires = 0;
12233
12234
12235 if (!ast_strlen_zero(get_header(req, "Contact"))) {
12236 const char *contact = NULL;
12237 const char *tmptmp = NULL;
12238 int start = 0;
12239 for(;;) {
12240 contact = __get_header(req, "Contact", &start);
12241
12242 if(!ast_strlen_zero(contact)) {
12243 if( (tmptmp=strstr(contact, p->our_contact))) {
12244 contact=tmptmp;
12245 break;
12246 }
12247 } else
12248 break;
12249 }
12250 tmptmp = strcasestr(contact, "expires=");
12251 if (tmptmp) {
12252 if (sscanf(tmptmp + 8, "%d;", &expires) != 1)
12253 expires = 0;
12254 }
12255
12256 }
12257 if (!expires)
12258 expires=atoi(get_header(req, "expires"));
12259 if (!expires)
12260 expires=default_expiry;
12261
12262 expires_ms = expires * 1000;
12263 if (expires <= EXPIRY_GUARD_LIMIT)
12264 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT),EXPIRY_GUARD_MIN);
12265 else
12266 expires_ms -= EXPIRY_GUARD_SECS * 1000;
12267 if (sipdebug)
12268 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
12269
12270 r->refresh= (int) expires_ms / 1000;
12271
12272
12273 r->expire=ast_sched_add(sched, expires_ms, sip_reregister, r);
12274 ASTOBJ_UNREF(r, sip_registry_destroy);
12275 }
12276 return 1;
12277 }
12278
12279
12280 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
12281 {
12282 struct sip_peer *peer = p->relatedpeer;
12283 int statechanged, is_reachable, was_reachable;
12284 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
12285
12286
12287
12288
12289
12290
12291 if (pingtime < 1)
12292 pingtime = 1;
12293
12294
12295
12296
12297
12298 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
12299 is_reachable = pingtime <= peer->maxms;
12300 statechanged = peer->lastms == 0
12301 || was_reachable != is_reachable;
12302
12303 peer->lastms = pingtime;
12304 peer->call = NULL;
12305 if (statechanged) {
12306 const char *s = is_reachable ? "Reachable" : "Lagged";
12307
12308 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
12309 peer->name, s, pingtime, peer->maxms);
12310 ast_device_state_changed("SIP/%s", peer->name);
12311 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
12312 "Peer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
12313 peer->name, s, pingtime);
12314 }
12315
12316 if (peer->pokeexpire > -1)
12317 ast_sched_del(sched, peer->pokeexpire);
12318 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12319
12320
12321 peer->pokeexpire = ast_sched_add(sched,
12322 is_reachable ? DEFAULT_FREQ_OK : DEFAULT_FREQ_NOTOK,
12323 sip_poke_peer_s, peer);
12324 }
12325
12326
12327 static void stop_media_flows(struct sip_pvt *p)
12328 {
12329
12330 if (p->rtp)
12331 ast_rtp_stop(p->rtp);
12332 if (p->vrtp)
12333 ast_rtp_stop(p->vrtp);
12334 if (p->udptl)
12335 ast_udptl_stop(p->udptl);
12336 }
12337
12338
12339
12340 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
12341 {
12342 struct ast_channel *owner;
12343 int sipmethod;
12344 int res = 1;
12345 const char *c = get_header(req, "Cseq");
12346 const char *msg = strchr(c, ' ');
12347
12348 if (!msg)
12349 msg = "";
12350 else
12351 msg++;
12352 sipmethod = find_sip_method(msg);
12353
12354 owner = p->owner;
12355 if (owner)
12356 owner->hangupcause = hangup_sip2cause(resp);
12357
12358
12359 if ((resp >= 100) && (resp <= 199))
12360 __sip_semi_ack(p, seqno, 0, sipmethod);
12361 else
12362 __sip_ack(p, seqno, 0, sipmethod);
12363
12364
12365 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
12366 char tag[128];
12367
12368 gettag(req, "To", tag, sizeof(tag));
12369 ast_string_field_set(p, theirtag, tag);
12370 }
12371 if (p->relatedpeer && p->method == SIP_OPTIONS) {
12372
12373
12374
12375 if (resp != 100)
12376 handle_response_peerpoke(p, resp, req);
12377 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
12378 switch(resp) {
12379 case 100:
12380 case 101:
12381 if (sipmethod == SIP_INVITE)
12382 handle_response_invite(p, resp, rest, req, seqno);
12383 break;
12384 case 183:
12385 if (sipmethod == SIP_INVITE)
12386 handle_response_invite(p, resp, rest, req, seqno);
12387 break;
12388 case 180:
12389 if (sipmethod == SIP_INVITE)
12390 handle_response_invite(p, resp, rest, req, seqno);
12391 break;
12392 case 200:
12393 p->authtries = 0;
12394 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
12395
12396
12397
12398 } else if (sipmethod == SIP_INVITE) {
12399 handle_response_invite(p, resp, rest, req, seqno);
12400 } else if (sipmethod == SIP_NOTIFY) {
12401
12402 if (p->owner) {
12403 if (!p->refer) {
12404 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
12405 ast_queue_hangup(p->owner);
12406 } else if (option_debug > 3)
12407 ast_log(LOG_DEBUG, "Got OK on REFER Notify message\n");
12408 } else {
12409 if (p->subscribed == NONE)
12410 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12411 }
12412 } else if (sipmethod == SIP_REGISTER)
12413 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12414 else if (sipmethod == SIP_BYE)
12415 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12416 break;
12417 case 202:
12418 if (sipmethod == SIP_REFER)
12419 handle_response_refer(p, resp, rest, req, seqno);
12420 break;
12421 case 401:
12422 if (sipmethod == SIP_INVITE)
12423 handle_response_invite(p, resp, rest, req, seqno);
12424 else if (sipmethod == SIP_REFER)
12425 handle_response_refer(p, resp, rest, req, seqno);
12426 else if (p->registry && sipmethod == SIP_REGISTER)
12427 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12428 else if (sipmethod == SIP_BYE) {
12429 if (ast_strlen_zero(p->authname)) {
12430 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
12431 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12432 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12433 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "WWW-Authenticate", "Authorization", sipmethod, 0)) {
12434 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
12435 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12436
12437
12438 }
12439 } else {
12440 ast_log(LOG_WARNING, "Got authentication request (401) on unknown %s to '%s'\n", sip_methods[sipmethod].text, get_header(req, "To"));
12441 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12442 }
12443 break;
12444 case 403:
12445 if (sipmethod == SIP_INVITE)
12446 handle_response_invite(p, resp, rest, req, seqno);
12447 else if (p->registry && sipmethod == SIP_REGISTER)
12448 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12449 else {
12450 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
12451 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12452 }
12453 break;
12454 case 404:
12455 if (p->registry && sipmethod == SIP_REGISTER)
12456 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12457 else if (sipmethod == SIP_INVITE)
12458 handle_response_invite(p, resp, rest, req, seqno);
12459 else if (owner)
12460 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12461 break;
12462 case 407:
12463 if (sipmethod == SIP_INVITE)
12464 handle_response_invite(p, resp, rest, req, seqno);
12465 else if (sipmethod == SIP_REFER)
12466 handle_response_refer(p, resp, rest, req, seqno);
12467 else if (p->registry && sipmethod == SIP_REGISTER)
12468 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12469 else if (sipmethod == SIP_BYE) {
12470 if (ast_strlen_zero(p->authname)) {
12471 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
12472 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12473 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12474 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization", sipmethod, 0)) {
12475 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
12476 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12477 }
12478 } else
12479 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12480
12481 break;
12482 case 408:
12483 if (sipmethod == SIP_INVITE)
12484 handle_response_invite(p, resp, rest, req, seqno);
12485 else if (sipmethod == SIP_REGISTER)
12486 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12487 else if (sipmethod == SIP_BYE) {
12488 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12489 if (option_debug)
12490 ast_log(LOG_DEBUG, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
12491 } else {
12492 if (owner)
12493 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12494 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12495 }
12496 break;
12497 case 481:
12498 if (sipmethod == SIP_INVITE) {
12499 handle_response_invite(p, resp, rest, req, seqno);
12500 } else if (sipmethod == SIP_REFER) {
12501 handle_response_refer(p, resp, rest, req, seqno);
12502 } else if (sipmethod == SIP_BYE) {
12503
12504
12505 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12506 } else if (sipmethod == SIP_CANCEL) {
12507
12508
12509 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12510 } else {
12511 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12512
12513 }
12514 break;
12515 case 487:
12516 if (sipmethod == SIP_INVITE)
12517 handle_response_invite(p, resp, rest, req, seqno);
12518 break;
12519 case 488:
12520 if (sipmethod == SIP_INVITE)
12521 handle_response_invite(p, resp, rest, req, seqno);
12522 break;
12523 case 491:
12524 if (sipmethod == SIP_INVITE)
12525 handle_response_invite(p, resp, rest, req, seqno);
12526 else {
12527 if (option_debug)
12528 ast_log(LOG_DEBUG, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
12529 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12530 }
12531 break;
12532 case 501:
12533 if (sipmethod == SIP_INVITE)
12534 handle_response_invite(p, resp, rest, req, seqno);
12535 else if (sipmethod == SIP_REFER)
12536 handle_response_refer(p, resp, rest, req, seqno);
12537 else
12538 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(p->sa.sin_addr), msg);
12539 break;
12540 case 603:
12541 if (sipmethod == SIP_REFER) {
12542 handle_response_refer(p, resp, rest, req, seqno);
12543 break;
12544 }
12545
12546 default:
12547 if ((resp >= 300) && (resp < 700)) {
12548
12549 if ((option_verbose > 2) && (resp != 487))
12550 ast_verbose(VERBOSE_PREFIX_3 "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
12551
12552 if (sipmethod == SIP_INVITE)
12553 stop_media_flows(p);
12554
12555
12556 switch(resp) {
12557 case 300:
12558 case 301:
12559 case 302:
12560 case 305:
12561 parse_moved_contact(p, req);
12562
12563 case 486:
12564 case 600:
12565 case 603:
12566 if (p->owner)
12567 ast_queue_control(p->owner, AST_CONTROL_BUSY);
12568 break;
12569 case 482:
12570
12571
12572
12573
12574 if (option_debug)
12575 ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
12576 if (p->owner)
12577 ast_string_field_build(p->owner, call_forward,
12578 "Local/%s@%s", p->username, p->context);
12579
12580 case 480:
12581 case 404:
12582 case 410:
12583 case 400:
12584 case 500:
12585 if (sipmethod == SIP_REFER) {
12586 handle_response_refer(p, resp, rest, req, seqno);
12587 break;
12588 }
12589
12590 case 503:
12591 case 504:
12592 if (owner)
12593 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12594 break;
12595 default:
12596
12597 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO && sipmethod != SIP_BYE)
12598 ast_queue_hangup(p->owner);
12599 break;
12600 }
12601
12602 if (sipmethod == SIP_INVITE)
12603 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12604 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
12605 sip_alreadygone(p);
12606 if (!p->owner)
12607 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12608 } else if ((resp >= 100) && (resp < 200)) {
12609 if (sipmethod == SIP_INVITE) {
12610 if (!ast_test_flag(req, SIP_PKT_IGNORE))
12611 sip_cancel_destroy(p);
12612 if (find_sdp(req))
12613 process_sdp(p, req);
12614 if (p->owner) {
12615
12616 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
12617 }
12618 }
12619 } else
12620 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));
12621 }
12622 } else {
12623
12624
12625 if (ast_test_flag(req, SIP_PKT_DEBUG))
12626 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
12627
12628 if (sipmethod == SIP_INVITE && resp == 200) {
12629
12630
12631 char tag[128];
12632
12633 gettag(req, "To", tag, sizeof(tag));
12634 ast_string_field_set(p, theirtag, tag);
12635 }
12636
12637 switch(resp) {
12638 case 200:
12639 if (sipmethod == SIP_INVITE) {
12640 handle_response_invite(p, resp, rest, req, seqno);
12641 } else if (sipmethod == SIP_CANCEL) {
12642 if (option_debug)
12643 ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
12644
12645
12646 } else if (sipmethod == SIP_NOTIFY) {
12647
12648 if (p->owner) {
12649 if (p->refer) {
12650 if (option_debug)
12651 ast_log(LOG_DEBUG, "Got 200 OK on NOTIFY for transfer\n");
12652 } else
12653 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
12654
12655 } else {
12656 if (!p->subscribed && !p->refer)
12657 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12658 }
12659 } else if (sipmethod == SIP_BYE)
12660 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12661 else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO)
12662
12663
12664 ;
12665 else if (sipmethod == SIP_BYE)
12666
12667 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12668 break;
12669 case 202:
12670 if (sipmethod == SIP_REFER)
12671 handle_response_refer(p, resp, rest, req, seqno);
12672 break;
12673 case 401:
12674 case 407:
12675 if (sipmethod == SIP_REFER)
12676 handle_response_refer(p, resp, rest, req, seqno);
12677 else if (sipmethod == SIP_INVITE)
12678 handle_response_invite(p, resp, rest, req, seqno);
12679 else if (sipmethod == SIP_BYE) {
12680 char *auth, *auth2;
12681
12682 auth = (resp == 407 ? "Proxy-Authenticate" : "WWW-Authenticate");
12683 auth2 = (resp == 407 ? "Proxy-Authorization" : "Authorization");
12684 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, auth, auth2, sipmethod, 0)) {
12685 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
12686 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12687 }
12688 }
12689 break;
12690 case 481:
12691 if (sipmethod == SIP_INVITE) {
12692
12693 handle_response_invite(p, resp, rest, req, seqno);
12694 } else if (sipmethod == SIP_BYE) {
12695 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12696 } else if (sipdebug) {
12697 ast_log (LOG_DEBUG, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
12698 }
12699 break;
12700 case 501:
12701 if (sipmethod == SIP_INVITE)
12702 handle_response_invite(p, resp, rest, req, seqno);
12703 else if (sipmethod == SIP_REFER)
12704 handle_response_refer(p, resp, rest, req, seqno);
12705 break;
12706 case 603:
12707 if (sipmethod == SIP_REFER) {
12708 handle_response_refer(p, resp, rest, req, seqno);
12709 break;
12710 }
12711
12712 default:
12713 if ((resp >= 100) && (resp < 200)) {
12714 if (sipmethod == SIP_INVITE) {
12715 if (!ast_test_flag(req, SIP_PKT_IGNORE))
12716 sip_cancel_destroy(p);
12717 }
12718 }
12719 if ((resp >= 300) && (resp < 700)) {
12720 if ((option_verbose > 2) && (resp != 487))
12721 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));
12722 switch(resp) {
12723 case 488:
12724 case 603:
12725 case 500:
12726 case 503:
12727 case 504:
12728
12729 if (sipmethod == SIP_INVITE) {
12730 sip_cancel_destroy(p);
12731 }
12732 break;
12733 }
12734 }
12735 break;
12736 }
12737 }
12738 }
12739
12740
12741
12742
12743
12744
12745
12746 static void *sip_park_thread(void *stuff)
12747 {
12748 struct ast_channel *transferee, *transferer;
12749 struct sip_dual *d;
12750 struct sip_request req;
12751 int ext;
12752 int res;
12753
12754 d = stuff;
12755 transferee = d->chan1;
12756 transferer = d->chan2;
12757 copy_request(&req, &d->req);
12758 free(d);
12759
12760 if (!transferee || !transferer) {
12761 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
12762 return NULL;
12763 }
12764 if (option_debug > 3)
12765 ast_log(LOG_DEBUG, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
12766
12767 ast_channel_lock(transferee);
12768 if (ast_do_masquerade(transferee)) {
12769 ast_log(LOG_WARNING, "Masquerade failed.\n");
12770 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
12771 ast_channel_unlock(transferee);
12772 return NULL;
12773 }
12774 ast_channel_unlock(transferee);
12775
12776 res = ast_park_call(transferee, transferer, 0, &ext);
12777
12778
12779 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
12780 if (!res) {
12781 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
12782 } else {
12783
12784 sprintf(buf, "Call parked on extension '%d'", ext);
12785 transmit_message_with_text(transferer->tech_pvt, buf);
12786 }
12787 #endif
12788
12789
12790
12791 transmit_response(transferer->tech_pvt, "202 Accepted", &req);
12792 if (!res) {
12793
12794 append_history(transferer->tech_pvt, "SIPpark","Parked call on %d", ext);
12795 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
12796 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
12797 ast_hangup(transferer);
12798 if (option_debug)
12799 ast_log(LOG_DEBUG, "SIP Call parked on extension '%d'\n", ext);
12800 } else {
12801 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
12802 append_history(transferer->tech_pvt, "SIPpark","Parking failed\n");
12803 if (option_debug)
12804 ast_log(LOG_DEBUG, "SIP Call parked failed \n");
12805
12806 }
12807 return NULL;
12808 }
12809
12810
12811
12812
12813 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno)
12814 {
12815 struct sip_dual *d;
12816 struct ast_channel *transferee, *transferer;
12817
12818 pthread_t th;
12819
12820 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
12821 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "SIPPeer/%s", chan2->name);
12822 if ((!transferer) || (!transferee)) {
12823 if (transferee) {
12824 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
12825 ast_hangup(transferee);
12826 }
12827 if (transferer) {
12828 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
12829 ast_hangup(transferer);
12830 }
12831 return -1;
12832 }
12833
12834
12835 transferee->readformat = chan1->readformat;
12836 transferee->writeformat = chan1->writeformat;
12837
12838
12839 ast_channel_masquerade(transferee, chan1);
12840
12841
12842 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
12843 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
12844 transferee->priority = chan1->priority;
12845
12846
12847
12848
12849
12850 transferer->readformat = chan2->readformat;
12851 transferer->writeformat = chan2->writeformat;
12852
12853
12854 ast_channel_masquerade(transferer, chan2);
12855
12856
12857 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
12858 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
12859 transferer->priority = chan2->priority;
12860
12861 ast_channel_lock(transferer);
12862 if (ast_do_masquerade(transferer)) {
12863 ast_log(LOG_WARNING, "Masquerade failed :(\n");
12864 ast_channel_unlock(transferer);
12865 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
12866 ast_hangup(transferer);
12867 return -1;
12868 }
12869 ast_channel_unlock(transferer);
12870 if (!transferer || !transferee) {
12871 if (!transferer) {
12872 if (option_debug)
12873 ast_log(LOG_DEBUG, "No transferer channel, giving up parking\n");
12874 }
12875 if (!transferee) {
12876 if (option_debug)
12877 ast_log(LOG_DEBUG, "No transferee channel, giving up parking\n");
12878 }
12879 return -1;
12880 }
12881 if ((d = ast_calloc(1, sizeof(*d)))) {
12882 pthread_attr_t attr;
12883
12884 pthread_attr_init(&attr);
12885 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
12886
12887
12888 copy_request(&d->req, req);
12889 d->chan1 = transferee;
12890 d->chan2 = transferer;
12891 d->seqno = seqno;
12892 if (ast_pthread_create_background(&th, &attr, sip_park_thread, d) < 0) {
12893
12894 free(d);
12895
12896 pthread_attr_destroy(&attr);
12897 return 0;
12898 }
12899 pthread_attr_destroy(&attr);
12900 }
12901 return -1;
12902 }
12903
12904
12905
12906
12907 static void ast_quiet_chan(struct ast_channel *chan)
12908 {
12909 if (chan && chan->_state == AST_STATE_UP) {
12910 if (chan->generatordata)
12911 ast_deactivate_generator(chan);
12912 }
12913 }
12914
12915
12916
12917 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
12918 {
12919 int res = 0;
12920 struct ast_channel *peera = NULL,
12921 *peerb = NULL,
12922 *peerc = NULL,
12923 *peerd = NULL;
12924
12925
12926
12927
12928 if (option_debug > 3) {
12929 ast_log(LOG_DEBUG, "Sip transfer:--------------------\n");
12930 if (transferer->chan1)
12931 ast_log(LOG_DEBUG, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
12932 else
12933 ast_log(LOG_DEBUG, "-- No transferer first channel - odd??? \n");
12934 if (target->chan1)
12935 ast_log(LOG_DEBUG, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
12936 else
12937 ast_log(LOG_DEBUG, "-- No target first channel ---\n");
12938 if (transferer->chan2)
12939 ast_log(LOG_DEBUG, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
12940 else
12941 ast_log(LOG_DEBUG, "-- No bridged call to transferee\n");
12942 if (target->chan2)
12943 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)");
12944 else
12945 ast_log(LOG_DEBUG, "-- No target second channel ---\n");
12946 ast_log(LOG_DEBUG, "-- END Sip transfer:--------------------\n");
12947 }
12948 if (transferer->chan2) {
12949 peera = transferer->chan1;
12950 peerb = target->chan1;
12951 peerc = transferer->chan2;
12952 peerd = target->chan2;
12953 if (option_debug > 2)
12954 ast_log(LOG_DEBUG, "SIP transfer: Four channels to handle\n");
12955 } else if (target->chan2) {
12956 peera = target->chan1;
12957 peerb = transferer->chan1;
12958 peerc = target->chan2;
12959 peerd = transferer->chan2;
12960 if (option_debug > 2)
12961 ast_log(LOG_DEBUG, "SIP transfer: Three channels to handle\n");
12962 }
12963
12964 if (peera && peerb && peerc && (peerb != peerc)) {
12965 ast_quiet_chan(peera);
12966 ast_quiet_chan(peerb);
12967 ast_quiet_chan(peerc);
12968 if (peerd)
12969 ast_quiet_chan(peerd);
12970
12971
12972 if (peera->cdr && peerb->cdr)
12973 peerb->cdr = ast_cdr_append(peerb->cdr, peera->cdr);
12974 else if (peera->cdr)
12975 peerb->cdr = peera->cdr;
12976 peera->cdr = NULL;
12977
12978 if (peerb->cdr && peerc->cdr)
12979 peerb->cdr = ast_cdr_append(peerb->cdr, peerc->cdr);
12980 else if (peerc->cdr)
12981 peerb->cdr = peerc->cdr;
12982 peerc->cdr = NULL;
12983
12984 if (option_debug > 3)
12985 ast_log(LOG_DEBUG, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
12986 if (ast_channel_masquerade(peerb, peerc)) {
12987 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
12988 res = -1;
12989 } else
12990 ast_log(LOG_DEBUG, "SIP transfer: Succeeded to masquerade channels.\n");
12991 return res;
12992 } else {
12993 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
12994 if (transferer->chan1)
12995 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
12996 if (target->chan1)
12997 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
12998 return -2;
12999 }
13000 return 0;
13001 }
13002
13003
13004
13005
13006
13007
13008 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
13009 {
13010 const char *thetag;
13011
13012 if (!tagbuf)
13013 return NULL;
13014 tagbuf[0] = '\0';
13015 thetag = get_header(req, header);
13016 thetag = strcasestr(thetag, ";tag=");
13017 if (thetag) {
13018 thetag += 5;
13019 ast_copy_string(tagbuf, thetag, tagbufsize);
13020 return strsep(&tagbuf, ";");
13021 }
13022 return NULL;
13023 }
13024
13025
13026 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
13027 {
13028
13029
13030 int res = 0;
13031 const char *event = get_header(req, "Event");
13032 char *eventid = NULL;
13033 char *sep;
13034
13035 if( (sep = strchr(event, ';')) ) {
13036 *sep++ = '\0';
13037 eventid = sep;
13038 }
13039
13040 if (option_debug > 1 && sipdebug)
13041 ast_log(LOG_DEBUG, "Got NOTIFY Event: %s\n", event);
13042
13043 if (strcmp(event, "refer")) {
13044
13045
13046 transmit_response(p, "489 Bad event", req);
13047 res = -1;
13048 } else {
13049
13050
13051
13052
13053
13054 char buf[1024];
13055 char *cmd, *code;
13056 int respcode;
13057 int success = TRUE;
13058
13059
13060
13061
13062
13063
13064
13065
13066 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
13067
13068 transmit_response(p, "400 Bad request", req);
13069 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13070 return -1;
13071 }
13072
13073
13074 if (get_msg_text(buf, sizeof(buf), req)) {
13075 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
13076 transmit_response(p, "400 Bad request", req);
13077 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13078 return -1;
13079 }
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101 if (option_debug > 2)
13102 ast_log(LOG_DEBUG, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
13103 cmd = ast_skip_blanks(buf);
13104 code = cmd;
13105
13106 while(*code && (*code > 32)) {
13107 code++;
13108 }
13109 *code++ = '\0';
13110 code = ast_skip_blanks(code);
13111 sep = code;
13112 sep++;
13113 while(*sep && (*sep > 32)) {
13114 sep++;
13115 }
13116 *sep++ = '\0';
13117 respcode = atoi(code);
13118 switch (respcode) {
13119 case 100:
13120 case 101:
13121
13122 break;
13123 case 183:
13124
13125 break;
13126 case 200:
13127
13128 break;
13129 case 301:
13130 case 302:
13131
13132 success = FALSE;
13133 break;
13134 case 503:
13135
13136 success = FALSE;
13137 break;
13138 case 603:
13139
13140 success = FALSE;
13141 break;
13142 }
13143 if (!success) {
13144 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
13145 }
13146
13147
13148 transmit_response(p, "200 OK", req);
13149 };
13150
13151 if (!p->lastinvite)
13152 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13153
13154 return res;
13155 }
13156
13157
13158 static int handle_request_options(struct sip_pvt *p, struct sip_request *req)
13159 {
13160 int res;
13161
13162 res = get_destination(p, req);
13163 build_contact(p);
13164
13165 if (ast_strlen_zero(p->context))
13166 ast_string_field_set(p, context, default_context);
13167 if (res < 0)
13168 transmit_response_with_allow(p, "404 Not Found", req, 0);
13169 else
13170 transmit_response_with_allow(p, "200 OK", req, 0);
13171
13172
13173 if (!p->lastinvite)
13174 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13175
13176 return res;
13177 }
13178
13179
13180
13181 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin)
13182 {
13183 struct ast_frame *f;
13184 int earlyreplace = 0;
13185 int oneleggedreplace = 0;
13186 struct ast_channel *c = p->owner;
13187 struct ast_channel *replacecall = p->refer->refer_call->owner;
13188 struct ast_channel *targetcall;
13189
13190
13191 if (replacecall->_state == AST_STATE_RING)
13192 earlyreplace = 1;
13193
13194
13195 if (!(targetcall = ast_bridged_channel(replacecall))) {
13196
13197 if (!earlyreplace) {
13198 if (option_debug > 1)
13199 ast_log(LOG_DEBUG, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
13200 oneleggedreplace = 1;
13201 }
13202 }
13203 if (option_debug > 3 && targetcall && targetcall->_state == AST_STATE_RINGING)
13204 ast_log(LOG_DEBUG, "SIP transfer: Target channel is in ringing state\n");
13205
13206 if (option_debug > 3) {
13207 if (targetcall)
13208 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);
13209 else
13210 ast_log(LOG_DEBUG, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
13211 }
13212
13213 if (ignore) {
13214 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
13215
13216
13217
13218 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
13219
13220 ast_channel_unlock(c);
13221 ast_mutex_unlock(&p->refer->refer_call->lock);
13222 return 1;
13223 }
13224 if (!c) {
13225
13226 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
13227 transmit_response_reliable(p, "503 Service Unavailable", req);
13228 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
13229 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13230 ast_mutex_unlock(&p->refer->refer_call->lock);
13231 return 1;
13232 }
13233 append_history(p, "Xfer", "INVITE/Replace received");
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245 transmit_response(p, "100 Trying", req);
13246 ast_setstate(c, AST_STATE_RING);
13247
13248
13249
13250
13251
13252 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
13253
13254 ast_setstate(c, AST_STATE_UP);
13255
13256
13257 ast_quiet_chan(replacecall);
13258 ast_quiet_chan(targetcall);
13259 if (option_debug > 3)
13260 ast_log(LOG_DEBUG, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
13261
13262 ast_channel_unlock(c);
13263
13264
13265 ast_mutex_unlock(&p->refer->refer_call->lock);
13266
13267
13268 if (! earlyreplace && ! oneleggedreplace )
13269 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
13270
13271
13272 if(ast_channel_masquerade(replacecall, c))
13273 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
13274 else if (option_debug > 3)
13275 ast_log(LOG_DEBUG, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
13276
13277
13278
13279
13280
13281 ast_channel_unlock(c);
13282
13283 if (earlyreplace || oneleggedreplace ) {
13284
13285 if ((f = ast_read(replacecall))) {
13286 ast_frfree(f);
13287 f = NULL;
13288 if (option_debug > 3)
13289 ast_log(LOG_DEBUG, "Invite/Replace: Could successfully read frame from RING channel!\n");
13290 } else {
13291 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from RING channel \n");
13292 }
13293 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13294 ast_channel_unlock(replacecall);
13295 } else {
13296 if ((f = ast_read(replacecall))) {
13297
13298 ast_frfree(f);
13299 f = NULL;
13300 if (option_debug > 2)
13301 ast_log(LOG_DEBUG, "Invite/Replace: Could successfully read frame from channel! Masq done.\n");
13302 } else {
13303 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from channel. Transfer failed\n");
13304 }
13305 ast_channel_unlock(replacecall);
13306 }
13307 ast_mutex_unlock(&p->refer->refer_call->lock);
13308
13309 ast_setstate(c, AST_STATE_DOWN);
13310 if (option_debug > 3) {
13311 struct ast_channel *test;
13312 ast_log(LOG_DEBUG, "After transfer:----------------------------\n");
13313 ast_log(LOG_DEBUG, " -- C: %s State %s\n", c->name, ast_state2str(c->_state));
13314 if (replacecall)
13315 ast_log(LOG_DEBUG, " -- replacecall: %s State %s\n", replacecall->name, ast_state2str(replacecall->_state));
13316 if (p->owner) {
13317 ast_log(LOG_DEBUG, " -- P->owner: %s State %s\n", p->owner->name, ast_state2str(p->owner->_state));
13318 test = ast_bridged_channel(p->owner);
13319 if (test)
13320 ast_log(LOG_DEBUG, " -- Call bridged to P->owner: %s State %s\n", test->name, ast_state2str(test->_state));
13321 else
13322 ast_log(LOG_DEBUG, " -- No call bridged to C->owner \n");
13323 } else
13324 ast_log(LOG_DEBUG, " -- No channel yet \n");
13325 ast_log(LOG_DEBUG, "End After transfer:----------------------------\n");
13326 }
13327
13328 ast_channel_unlock(p->owner);
13329 ast_mutex_unlock(&p->lock);
13330
13331
13332 c->tech_pvt = NULL;
13333 ast_hangup(c);
13334 return 0;
13335 }
13336
13337
13338
13339
13340
13341
13342
13343
13344 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)
13345 {
13346 int res = 1;
13347 int gotdest;
13348 const char *p_replaces;
13349 char *replace_id = NULL;
13350 const char *required;
13351 unsigned int required_profile = 0;
13352 struct ast_channel *c = NULL;
13353 int reinvite = 0;
13354
13355
13356 if (!p->sipoptions) {
13357 const char *supported = get_header(req, "Supported");
13358 if (!ast_strlen_zero(supported))
13359 parse_sip_options(p, supported);
13360 }
13361
13362
13363 required = get_header(req, "Require");
13364 if (!ast_strlen_zero(required)) {
13365 required_profile = parse_sip_options(NULL, required);
13366 if (required_profile && required_profile != SIP_OPT_REPLACES) {
13367
13368 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
13369 ast_log(LOG_WARNING,"Received SIP INVITE with unsupported required extension: %s\n", required);
13370 p->invitestate = INV_COMPLETED;
13371 if (!p->lastinvite)
13372 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13373 return -1;
13374 }
13375 }
13376
13377
13378 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->owner->_state != AST_STATE_UP)) {
13379
13380
13381
13382
13383
13384 transmit_response(p, "482 Loop Detected", req);
13385 p->invitestate = INV_COMPLETED;
13386 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13387 return 0;
13388 }
13389
13390 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->pendinginvite) {
13391
13392 transmit_response(p, "491 Request Pending", req);
13393 if (option_debug)
13394 ast_log(LOG_DEBUG, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
13395
13396 return 0;
13397 }
13398
13399 p_replaces = get_header(req, "Replaces");
13400 if (!ast_strlen_zero(p_replaces)) {
13401
13402 char *ptr;
13403 char *fromtag = NULL;
13404 char *totag = NULL;
13405 char *start, *to;
13406 int error = 0;
13407
13408 if (p->owner) {
13409 if (option_debug > 2)
13410 ast_log(LOG_DEBUG, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
13411 transmit_response(p, "400 Bad request", req);
13412
13413 return -1;
13414 }
13415
13416 if (sipdebug && option_debug > 2)
13417 ast_log(LOG_DEBUG, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
13418
13419 replace_id = ast_strdupa(p_replaces);
13420 ast_uri_decode(replace_id);
13421
13422 if (!p->refer && !sip_refer_allocate(p)) {
13423 transmit_response(p, "500 Server Internal Error", req);
13424 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
13425 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13426 p->invitestate = INV_COMPLETED;
13427 return -1;
13428 }
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439 replace_id = ast_skip_blanks(replace_id);
13440
13441 start = replace_id;
13442 while ( (ptr = strsep(&start, ";")) ) {
13443 ptr = ast_skip_blanks(ptr);
13444 if ( (to = strcasestr(ptr, "to-tag=") ) )
13445 totag = to + 7;
13446 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
13447 fromtag = to + 9;
13448 fromtag = strsep(&fromtag, "&");
13449 }
13450 }
13451
13452 if (sipdebug && option_debug > 3)
13453 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>");
13454
13455
13456
13457
13458
13459 if ((p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
13460 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
13461 transmit_response(p, "481 Call Leg Does Not Exist (Replaces)", req);
13462 error = 1;
13463 }
13464
13465
13466
13467
13468
13469
13470
13471 if (p->refer->refer_call == p) {
13472 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
13473 p->refer->refer_call = NULL;
13474 transmit_response(p, "400 Bad request", req);
13475 error = 1;
13476 }
13477
13478 if (!error && !p->refer->refer_call->owner) {
13479
13480 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
13481
13482 transmit_response(p, "481 Call Leg Does Not Exist (Replace)", req);
13483 error = 1;
13484 }
13485
13486 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 ) {
13487 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
13488 transmit_response(p, "603 Declined (Replaces)", req);
13489 error = 1;
13490 }
13491
13492 if (error) {
13493 append_history(p, "Xfer", "INVITE/Replace Failed.");
13494 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13495 ast_mutex_unlock(&p->lock);
13496 if (p->refer->refer_call) {
13497 ast_mutex_unlock(&p->refer->refer_call->lock);
13498 ast_channel_unlock(p->refer->refer_call->owner);
13499 }
13500 p->invitestate = INV_COMPLETED;
13501 return -1;
13502 }
13503 }
13504
13505
13506
13507
13508
13509 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13510 int newcall = (p->initreq.headers ? TRUE : FALSE);
13511
13512 sip_cancel_destroy(p);
13513
13514 p->pendinginvite = seqno;
13515 check_via(p, req);
13516
13517 copy_request(&p->initreq, req);
13518 if (!p->owner) {
13519 if (debug)
13520 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
13521 if (newcall)
13522 append_history(p, "Invite", "New call: %s", p->callid);
13523 parse_ok_contact(p, req);
13524 } else {
13525 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
13526
13527 if (find_sdp(req)) {
13528 if (process_sdp(p, req)) {
13529 transmit_response(p, "488 Not acceptable here", req);
13530 if (!p->lastinvite)
13531 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13532 return -1;
13533 }
13534 } else {
13535 p->jointcapability = p->capability;
13536 if (option_debug > 2)
13537 ast_log(LOG_DEBUG, "Hm.... No sdp for the moment\n");
13538
13539
13540
13541 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
13542 change_hold_state(p, req, FALSE, 0);
13543 }
13544 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
13545 append_history(p, "ReInv", "Re-invite received");
13546 }
13547 } else if (debug)
13548 ast_verbose("Ignoring this INVITE request\n");
13549
13550
13551 if (!p->lastinvite && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner) {
13552
13553
13554 res = check_user(p, req, SIP_INVITE, e, XMIT_RELIABLE, sin);
13555 if (res == AUTH_CHALLENGE_SENT) {
13556 p->invitestate = INV_COMPLETED;
13557 return 0;
13558 }
13559 if (res < 0) {
13560 if (res == AUTH_FAKE_AUTH) {
13561 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
13562 transmit_fake_auth_response(p, req, 1);
13563 } else {
13564 ast_log(LOG_NOTICE, "Failed to authenticate user %s\n", get_header(req, "From"));
13565 transmit_response_reliable(p, "403 Forbidden", req);
13566 }
13567 p->invitestate = INV_COMPLETED;
13568 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13569 ast_string_field_free(p, theirtag);
13570 return 0;
13571 }
13572
13573
13574 if (find_sdp(req)) {
13575 if (process_sdp(p, req)) {
13576
13577 transmit_response_reliable(p, "488 Not acceptable here", req);
13578 p->invitestate = INV_COMPLETED;
13579 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13580 if (option_debug)
13581 ast_log(LOG_DEBUG, "No compatible codecs for this SIP call.\n");
13582 return -1;
13583 }
13584 } else {
13585 p->jointcapability = p->capability;
13586 if (option_debug > 1)
13587 ast_log(LOG_DEBUG, "No SDP in Invite, third party call control\n");
13588 }
13589
13590
13591
13592 if (p->owner)
13593 ast_queue_frame(p->owner, &ast_null_frame);
13594
13595
13596
13597 if (ast_strlen_zero(p->context))
13598 ast_string_field_set(p, context, default_context);
13599
13600
13601
13602 if (option_debug)
13603 ast_log(LOG_DEBUG, "Checking SIP call limits for device %s\n", p->username);
13604 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
13605 if (res < 0) {
13606 ast_log(LOG_NOTICE, "Failed to place call for user %s, too many calls\n", p->username);
13607 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
13608 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13609 p->invitestate = INV_COMPLETED;
13610 }
13611 return 0;
13612 }
13613 gotdest = get_destination(p, NULL);
13614 get_rdnis(p, NULL);
13615 extract_uri(p, req);
13616 build_contact(p);
13617
13618 if (p->rtp) {
13619 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
13620 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
13621 }
13622
13623 if (!replace_id && gotdest) {
13624 if (gotdest == 1 && ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP))
13625 transmit_response_reliable(p, "484 Address Incomplete", req);
13626 else
13627 transmit_response_reliable(p, "404 Not Found", req);
13628 p->invitestate = INV_COMPLETED;
13629 update_call_counter(p, DEC_CALL_LIMIT);
13630 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13631 return 0;
13632 } else {
13633
13634
13635 if (ast_strlen_zero(p->exten))
13636 ast_string_field_set(p, exten, "s");
13637
13638
13639 make_our_tag(p->tag, sizeof(p->tag));
13640
13641 c = sip_new(p, AST_STATE_DOWN, S_OR(p->username, NULL));
13642 *recount = 1;
13643
13644
13645 build_route(p, req, 0);
13646
13647 if (c) {
13648
13649 ast_channel_lock(c);
13650 }
13651 }
13652 } else {
13653 if (option_debug > 1 && sipdebug) {
13654 if (!ast_test_flag(req, SIP_PKT_IGNORE))
13655 ast_log(LOG_DEBUG, "Got a SIP re-invite for call %s\n", p->callid);
13656 else
13657 ast_log(LOG_DEBUG, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
13658 }
13659 reinvite = 1;
13660 c = p->owner;
13661 }
13662
13663 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
13664 p->lastinvite = seqno;
13665
13666 if (replace_id) {
13667
13668 if (sipdebug && option_debug > 3)
13669 ast_log(LOG_DEBUG, "Sending this call to the invite/replcaes handler %s\n", p->callid);
13670 return handle_invite_replaces(p, req, debug, ast_test_flag(req, SIP_PKT_IGNORE), seqno, sin);
13671 }
13672
13673
13674 if (c) {
13675 switch(c->_state) {
13676 case AST_STATE_DOWN:
13677 if (option_debug > 1)
13678 ast_log(LOG_DEBUG, "%s: New call is still down.... Trying... \n", c->name);
13679 transmit_response(p, "100 Trying", req);
13680 p->invitestate = INV_PROCEEDING;
13681 ast_setstate(c, AST_STATE_RING);
13682 if (strcmp(p->exten, ast_pickup_ext())) {
13683 enum ast_pbx_result res;
13684
13685 res = ast_pbx_start(c);
13686
13687 switch(res) {
13688 case AST_PBX_FAILED:
13689 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
13690 p->invitestate = INV_COMPLETED;
13691 if (ast_test_flag(req, SIP_PKT_IGNORE))
13692 transmit_response(p, "503 Unavailable", req);
13693 else
13694 transmit_response_reliable(p, "503 Unavailable", req);
13695 break;
13696 case AST_PBX_CALL_LIMIT:
13697 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
13698 p->invitestate = INV_COMPLETED;
13699 if (ast_test_flag(req, SIP_PKT_IGNORE))
13700 transmit_response(p, "480 Temporarily Unavailable", req);
13701 else
13702 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
13703 break;
13704 case AST_PBX_SUCCESS:
13705
13706 break;
13707 }
13708
13709 if (res) {
13710
13711
13712 ast_mutex_unlock(&c->lock);
13713 ast_mutex_unlock(&p->lock);
13714 ast_hangup(c);
13715 ast_mutex_lock(&p->lock);
13716 c = NULL;
13717 }
13718 } else {
13719 ast_channel_unlock(c);
13720 *nounlock = 1;
13721 if (ast_pickup_call(c)) {
13722 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
13723 if (ast_test_flag(req, SIP_PKT_IGNORE))
13724 transmit_response(p, "503 Unavailable", req);
13725 else
13726 transmit_response_reliable(p, "503 Unavailable", req);
13727 sip_alreadygone(p);
13728
13729 ast_mutex_unlock(&p->lock);
13730 c->hangupcause = AST_CAUSE_CALL_REJECTED;
13731 } else {
13732 ast_mutex_unlock(&p->lock);
13733 ast_setstate(c, AST_STATE_DOWN);
13734 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
13735 }
13736 p->invitestate = INV_COMPLETED;
13737 ast_hangup(c);
13738 ast_mutex_lock(&p->lock);
13739 c = NULL;
13740 }
13741 break;
13742 case AST_STATE_RING:
13743 transmit_response(p, "100 Trying", req);
13744 p->invitestate = INV_PROCEEDING;
13745 break;
13746 case AST_STATE_RINGING:
13747 transmit_response(p, "180 Ringing", req);
13748 p->invitestate = INV_PROCEEDING;
13749 break;
13750 case AST_STATE_UP:
13751 if (option_debug > 1)
13752 ast_log(LOG_DEBUG, "%s: This call is UP.... \n", c->name);
13753
13754 transmit_response(p, "100 Trying", req);
13755
13756 if (p->t38.state == T38_PEER_REINVITE) {
13757 struct ast_channel *bridgepeer = NULL;
13758 struct sip_pvt *bridgepvt = NULL;
13759
13760 if ((bridgepeer = ast_bridged_channel(p->owner))) {
13761
13762
13763 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
13764 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
13765 if (bridgepvt->t38.state == T38_DISABLED) {
13766 if (bridgepvt->udptl) {
13767
13768 sip_handle_t38_reinvite(bridgepeer, p, 1);
13769 } else {
13770 ast_log(LOG_WARNING, "Strange... The other side of the bridge don't have udptl struct\n");
13771 ast_mutex_lock(&bridgepvt->lock);
13772 bridgepvt->t38.state = T38_DISABLED;
13773 ast_mutex_unlock(&bridgepvt->lock);
13774 if (option_debug > 1)
13775 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->name);
13776 if (ast_test_flag(req, SIP_PKT_IGNORE))
13777 transmit_response(p, "488 Not acceptable here", req);
13778 else
13779 transmit_response_reliable(p, "488 Not acceptable here", req);
13780
13781 }
13782 } else {
13783
13784 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
13785 p->t38.state = T38_ENABLED;
13786 if (option_debug)
13787 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13788 }
13789 } else {
13790
13791 if (ast_test_flag(req, SIP_PKT_IGNORE))
13792 transmit_response(p, "488 Not acceptable here", req);
13793 else
13794 transmit_response_reliable(p, "488 Not acceptable here", req);
13795 p->t38.state = T38_DISABLED;
13796 if (option_debug > 1)
13797 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13798
13799 if (!p->lastinvite)
13800 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13801 }
13802 } else {
13803
13804 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
13805 p->t38.state = T38_ENABLED;
13806 if (option_debug)
13807 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13808 }
13809 } else if (p->t38.state == T38_DISABLED) {
13810 int sendok = TRUE;
13811
13812
13813
13814 struct ast_channel *bridgepeer = NULL;
13815 struct sip_pvt *bridgepvt = NULL;
13816 if ((bridgepeer = ast_bridged_channel(p->owner))) {
13817 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
13818 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
13819
13820 if (bridgepvt->t38.state == T38_ENABLED) {
13821 ast_log(LOG_WARNING, "RTP re-invite after T38 session not handled yet !\n");
13822
13823 if (ast_test_flag(req, SIP_PKT_IGNORE))
13824 transmit_response(p, "488 Not Acceptable Here (unsupported)", req);
13825 else
13826 transmit_response_reliable(p, "488 Not Acceptable Here (unsupported)", req);
13827 sendok = FALSE;
13828 }
13829
13830 }
13831 }
13832
13833 if (sendok)
13834
13835 transmit_response_with_sdp(p, "200 OK", req, (reinvite || ast_test_flag(req, SIP_PKT_IGNORE)) ? XMIT_UNRELIABLE : XMIT_CRITICAL);
13836 }
13837 p->invitestate = INV_TERMINATED;
13838 break;
13839 default:
13840 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
13841 transmit_response(p, "100 Trying", req);
13842 break;
13843 }
13844 } else {
13845 if (p && (p->autokillid == -1)) {
13846 const char *msg;
13847
13848 if (!p->jointcapability)
13849 msg = "488 Not Acceptable Here (codec error)";
13850 else {
13851 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
13852 msg = "503 Unavailable";
13853 }
13854 if (ast_test_flag(req, SIP_PKT_IGNORE))
13855 transmit_response(p, msg, req);
13856 else
13857 transmit_response_reliable(p, msg, req);
13858 p->invitestate = INV_COMPLETED;
13859 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13860 }
13861 }
13862 return res;
13863 }
13864
13865
13866
13867 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno)
13868 {
13869 struct sip_dual target;
13870
13871 int res = 0;
13872 struct sip_pvt *targetcall_pvt;
13873
13874
13875 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
13876 transferer->refer->replaces_callid_fromtag))) {
13877 if (transferer->refer->localtransfer) {
13878
13879 transmit_response(transferer, "202 Accepted", req);
13880
13881
13882 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
13883 append_history(transferer, "Xfer", "Refer failed");
13884 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
13885 transferer->refer->status = REFER_FAILED;
13886 return -1;
13887 }
13888
13889 if (option_debug > 2)
13890 ast_log(LOG_DEBUG, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
13891 return 0;
13892 }
13893
13894
13895 transmit_response(transferer, "202 Accepted", req);
13896 append_history(transferer, "Xfer", "Refer accepted");
13897 if (!targetcall_pvt->owner) {
13898 if (option_debug > 3)
13899 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No owner of target call\n");
13900
13901 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
13902 append_history(transferer, "Xfer", "Refer failed");
13903 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
13904 transferer->refer->status = REFER_FAILED;
13905 ast_mutex_unlock(&targetcall_pvt->lock);
13906 ast_channel_unlock(current->chan1);
13907 return -1;
13908 }
13909
13910
13911 target.chan1 = targetcall_pvt->owner;
13912 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
13913
13914 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
13915
13916 if (option_debug > 3) {
13917 if (target.chan2)
13918 ast_log(LOG_DEBUG, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
13919 else if (target.chan1->_state != AST_STATE_RING)
13920 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No target channel\n");
13921 else
13922 ast_log(LOG_DEBUG, "SIP attended transfer: Attempting transfer in ringing state\n");
13923 }
13924 }
13925
13926
13927 if (option_debug > 3 && sipdebug) {
13928 if (current->chan2)
13929 ast_log(LOG_DEBUG, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
13930 else
13931 ast_log(LOG_DEBUG, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
13932 }
13933
13934 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
13935
13936
13937 res = attempt_transfer(current, &target);
13938 ast_mutex_unlock(&targetcall_pvt->lock);
13939 if (res) {
13940
13941 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
13942 append_history(transferer, "Xfer", "Refer failed");
13943 transferer->refer->status = REFER_FAILED;
13944 if (targetcall_pvt->owner)
13945 ast_channel_unlock(targetcall_pvt->owner);
13946
13947 if (res != -2)
13948 ast_hangup(transferer->owner);
13949 else
13950 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
13951 } else {
13952
13953
13954
13955 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
13956 append_history(transferer, "Xfer", "Refer succeeded");
13957 transferer->refer->status = REFER_200OK;
13958 if (targetcall_pvt->owner) {
13959 if (option_debug)
13960 ast_log(LOG_DEBUG, "SIP attended transfer: Unlocking channel %s\n", targetcall_pvt->owner->name);
13961 ast_channel_unlock(targetcall_pvt->owner);
13962 }
13963 }
13964 return 1;
13965 }
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock)
14032 {
14033 struct sip_dual current;
14034
14035
14036 int res = 0;
14037
14038 if (ast_test_flag(req, SIP_PKT_DEBUG))
14039 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");
14040
14041 if (!p->owner) {
14042
14043
14044 if (option_debug > 2)
14045 ast_log(LOG_DEBUG, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
14046 transmit_response(p, "603 Declined (No dialog)", req);
14047 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
14048 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
14049 sip_alreadygone(p);
14050 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14051 }
14052 return 0;
14053 }
14054
14055
14056
14057 if (p->allowtransfer == TRANSFER_CLOSED ) {
14058
14059 transmit_response(p, "603 Declined (policy)", req);
14060 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
14061
14062 return 0;
14063 }
14064
14065 if(!ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
14066
14067 transmit_response(p, "491 Request pending", req);
14068 append_history(p, "Xfer", "Refer failed. Request pending.");
14069 return 0;
14070 }
14071
14072
14073 if (!p->refer && !sip_refer_allocate(p)) {
14074 transmit_response(p, "500 Internal Server Error", req);
14075 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
14076 return -3;
14077 }
14078
14079 res = get_refer_info(p, req);
14080
14081 p->refer->status = REFER_SENT;
14082
14083 if (res != 0) {
14084 switch (res) {
14085 case -2:
14086 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
14087 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
14088 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
14089 ast_log(LOG_DEBUG, "SIP transfer to black hole can't be handled (no refer-to: )\n");
14090 break;
14091 case -3:
14092 transmit_response(p, "603 Declined (Non sip: uri)", req);
14093 append_history(p, "Xfer", "Refer failed. Non SIP uri");
14094 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
14095 ast_log(LOG_DEBUG, "SIP transfer to non-SIP uri denied\n");
14096 break;
14097 default:
14098
14099 transmit_response(p, "202 Accepted", req);
14100 append_history(p, "Xfer", "Refer failed. Bad extension.");
14101 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
14102 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14103 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
14104 ast_log(LOG_DEBUG, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
14105 break;
14106 }
14107 return 0;
14108 }
14109 if (ast_strlen_zero(p->context))
14110 ast_string_field_set(p, context, default_context);
14111
14112
14113 if (allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
14114 p->refer->localtransfer = 1;
14115 if (sipdebug && option_debug > 2)
14116 ast_log(LOG_DEBUG, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
14117 } else if (AST_LIST_EMPTY(&domain_list)) {
14118
14119 p->refer->localtransfer = 1;
14120 } else
14121 if (sipdebug && option_debug > 2)
14122 ast_log(LOG_DEBUG, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
14123
14124
14125
14126 if (ignore)
14127 return res;
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155 current.chan1 = p->owner;
14156
14157
14158 current.chan2 = ast_bridged_channel(current.chan1);
14159
14160 if (sipdebug && option_debug > 2)
14161 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>");
14162
14163 if (!current.chan2 && !p->refer->attendedtransfer) {
14164
14165
14166
14167 if (sipdebug && option_debug > 2)
14168 ast_log(LOG_DEBUG,"Refused SIP transfer on non-bridged channel.\n");
14169 p->refer->status = REFER_FAILED;
14170 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
14171 transmit_response(p, "603 Declined", req);
14172 return -1;
14173 }
14174
14175 if (current.chan2) {
14176 if (sipdebug && option_debug > 3)
14177 ast_log(LOG_DEBUG, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
14178
14179 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
14180 }
14181
14182 ast_set_flag(&p->flags[0], SIP_GOTREFER);
14183
14184
14185 if (p->refer->attendedtransfer) {
14186 if ((res = local_attended_transfer(p, ¤t, req, seqno)))
14187 return res;
14188
14189 if (sipdebug && option_debug > 3)
14190 ast_log(LOG_DEBUG, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
14191
14192 }
14193
14194
14195
14196 if (p->refer->localtransfer && !strcmp(p->refer->refer_to, ast_parking_ext())) {
14197
14198 *nounlock = 1;
14199 ast_channel_unlock(current.chan1);
14200 copy_request(¤t.req, req);
14201 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14202 p->refer->status = REFER_200OK;
14203 append_history(p, "Xfer", "REFER to call parking.");
14204 if (sipdebug && option_debug > 3)
14205 ast_log(LOG_DEBUG, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
14206 sip_park(current.chan2, current.chan1, req, seqno);
14207 return res;
14208 }
14209
14210
14211 transmit_response(p, "202 Accepted", req);
14212
14213 if (current.chan1 && current.chan2) {
14214 if (option_debug > 2)
14215 ast_log(LOG_DEBUG, "chan1->name: %s\n", current.chan1->name);
14216 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
14217 }
14218 if (current.chan2) {
14219 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
14220 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
14221 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
14222
14223 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
14224
14225 if (p->refer->referred_by)
14226 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
14227 }
14228
14229 if (p->refer->replaces_callid && !ast_strlen_zero(p->refer->replaces_callid)) {
14230 char tempheader[BUFSIZ];
14231 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
14232 p->refer->replaces_callid_totag ? ";to-tag=" : "",
14233 p->refer->replaces_callid_totag,
14234 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
14235 p->refer->replaces_callid_fromtag);
14236 if (current.chan2)
14237 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
14238 }
14239
14240
14241 *nounlock = 1;
14242 ast_channel_unlock(current.chan1);
14243
14244
14245
14246
14247 if (!p->refer->attendedtransfer)
14248 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
14249
14250
14251
14252
14253
14254 if (!current.chan2) {
14255
14256
14257
14258
14259
14260
14261
14262 p->refer->status = REFER_FAILED;
14263 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
14264 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14265 append_history(p, "Xfer", "Refer failed (only bridged calls).");
14266 return -1;
14267 }
14268 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14269
14270
14271
14272 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
14273
14274 if (!res) {
14275
14276 if (option_debug > 2)
14277 ast_log(LOG_DEBUG, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
14278 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
14279 if (p->refer->localtransfer)
14280 p->refer->status = REFER_200OK;
14281 if (p->owner)
14282 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
14283 append_history(p, "Xfer", "Refer succeeded.");
14284 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14285
14286
14287 res = 0;
14288 } else {
14289 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14290 if (option_debug > 2)
14291 ast_log(LOG_DEBUG, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
14292 append_history(p, "Xfer", "Refer failed.");
14293
14294 p->refer->status = REFER_FAILED;
14295 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
14296 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14297 res = -1;
14298 }
14299 return res;
14300 }
14301
14302
14303 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
14304 {
14305
14306 check_via(p, req);
14307 sip_alreadygone(p);
14308 p->invitestate = INV_CANCELLED;
14309
14310 if (p->owner && p->owner->_state == AST_STATE_UP) {
14311
14312 transmit_response(p, "200 OK", req);
14313 if (option_debug)
14314 ast_log(LOG_DEBUG, "Got CANCEL on an answered call. Ignoring... \n");
14315 return 0;
14316 }
14317
14318 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
14319 update_call_counter(p, DEC_CALL_LIMIT);
14320
14321 stop_media_flows(p);
14322
14323 if (p->owner)
14324 ast_queue_hangup(p->owner);
14325 else
14326 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14327 if (p->initreq.len > 0) {
14328 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
14329 transmit_response(p, "200 OK", req);
14330 return 1;
14331 } else {
14332 transmit_response(p, "481 Call Leg Does Not Exist", req);
14333 return 0;
14334 }
14335 }
14336
14337 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen)
14338 {
14339 struct ast_rtp_quality qos;
14340 struct sip_pvt *p = chan->tech_pvt;
14341 char *all = "", *parse = ast_strdupa(preparse);
14342 AST_DECLARE_APP_ARGS(args,
14343 AST_APP_ARG(param);
14344 AST_APP_ARG(type);
14345 AST_APP_ARG(field);
14346 );
14347 AST_STANDARD_APP_ARGS(args, parse);
14348
14349
14350 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
14351 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
14352 return 0;
14353 }
14354
14355 if (strcasecmp(args.param, "rtpqos"))
14356 return 0;
14357
14358
14359 if (ast_strlen_zero(args.type))
14360 args.type = "audio";
14361 if (ast_strlen_zero(args.field))
14362 args.field = "all";
14363
14364 memset(buf, 0, buflen);
14365 memset(&qos, 0, sizeof(qos));
14366
14367 if (strcasecmp(args.type, "AUDIO") == 0) {
14368 all = ast_rtp_get_quality(p->rtp, &qos);
14369 } else if (strcasecmp(args.type, "VIDEO") == 0) {
14370 all = ast_rtp_get_quality(p->vrtp, &qos);
14371 }
14372
14373 if (strcasecmp(args.field, "local_ssrc") == 0)
14374 snprintf(buf, buflen, "%u", qos.local_ssrc);
14375 else if (strcasecmp(args.field, "local_lostpackets") == 0)
14376 snprintf(buf, buflen, "%u", qos.local_lostpackets);
14377 else if (strcasecmp(args.field, "local_jitter") == 0)
14378 snprintf(buf, buflen, "%.0lf", qos.local_jitter * 1000.0);
14379 else if (strcasecmp(args.field, "local_count") == 0)
14380 snprintf(buf, buflen, "%u", qos.local_count);
14381 else if (strcasecmp(args.field, "remote_ssrc") == 0)
14382 snprintf(buf, buflen, "%u", qos.remote_ssrc);
14383 else if (strcasecmp(args.field, "remote_lostpackets") == 0)
14384 snprintf(buf, buflen, "%u", qos.remote_lostpackets);
14385 else if (strcasecmp(args.field, "remote_jitter") == 0)
14386 snprintf(buf, buflen, "%.0lf", qos.remote_jitter * 1000.0);
14387 else if (strcasecmp(args.field, "remote_count") == 0)
14388 snprintf(buf, buflen, "%u", qos.remote_count);
14389 else if (strcasecmp(args.field, "rtt") == 0)
14390 snprintf(buf, buflen, "%.0lf", qos.rtt * 1000.0);
14391 else if (strcasecmp(args.field, "all") == 0)
14392 ast_copy_string(buf, all, buflen);
14393 else {
14394 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
14395 return -1;
14396 }
14397 return 0;
14398 }
14399
14400
14401 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
14402 {
14403 struct ast_channel *c=NULL;
14404 int res;
14405 struct ast_channel *bridged_to;
14406
14407
14408 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner)
14409 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
14410
14411 p->invitestate = INV_TERMINATED;
14412
14413 copy_request(&p->initreq, req);
14414 check_via(p, req);
14415 sip_alreadygone(p);
14416
14417
14418 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY) || p->owner) {
14419 char *audioqos, *videoqos;
14420 if (p->rtp) {
14421 audioqos = ast_rtp_get_quality(p->rtp, NULL);
14422 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
14423 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
14424 if (p->owner)
14425 pbx_builtin_setvar_helper(p->owner, "RTPAUDIOQOS", audioqos);
14426 }
14427 if (p->vrtp) {
14428 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
14429 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
14430 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
14431 if (p->owner)
14432 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", videoqos);
14433 }
14434 }
14435
14436 stop_media_flows(p);
14437
14438 if (!ast_strlen_zero(get_header(req, "Also"))) {
14439 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
14440 ast_inet_ntoa(p->recv.sin_addr));
14441 if (ast_strlen_zero(p->context))
14442 ast_string_field_set(p, context, default_context);
14443 res = get_also_info(p, req);
14444 if (!res) {
14445 c = p->owner;
14446 if (c) {
14447 bridged_to = ast_bridged_channel(c);
14448 if (bridged_to) {
14449
14450 ast_queue_control(c, AST_CONTROL_UNHOLD);
14451 ast_async_goto(bridged_to, p->context, p->refer->refer_to,1);
14452 } else
14453 ast_queue_hangup(p->owner);
14454 }
14455 } else {
14456 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(p->recv.sin_addr));
14457 if (p->owner)
14458 ast_queue_hangup(p->owner);
14459 }
14460 } else if (p->owner) {
14461 ast_queue_hangup(p->owner);
14462 if (option_debug > 2)
14463 ast_log(LOG_DEBUG, "Received bye, issuing owner hangup\n");
14464 } else {
14465 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14466 if (option_debug > 2)
14467 ast_log(LOG_DEBUG, "Received bye, no owner, selfdestruct soon.\n");
14468 }
14469 transmit_response(p, "200 OK", req);
14470
14471 return 1;
14472 }
14473
14474
14475 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
14476 {
14477 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
14478 if (ast_test_flag(req, SIP_PKT_DEBUG))
14479 ast_verbose("Receiving message!\n");
14480 receive_message(p, req);
14481 } else
14482 transmit_response(p, "202 Accepted", req);
14483 return 1;
14484 }
14485
14486
14487 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
14488 {
14489 int gotdest;
14490 int res = 0;
14491 int firststate = AST_EXTENSION_REMOVED;
14492 struct sip_peer *authpeer = NULL;
14493 const char *eventheader = get_header(req, "Event");
14494 const char *accept = get_header(req, "Accept");
14495 int resubscribe = (p->subscribed != NONE);
14496 char *temp, *event;
14497
14498 if (p->initreq.headers) {
14499
14500 if (p->initreq.method != SIP_SUBSCRIBE) {
14501
14502
14503 transmit_response(p, "403 Forbidden (within dialog)", req);
14504
14505 if (option_debug)
14506 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);
14507 return 0;
14508 } else if (ast_test_flag(req, SIP_PKT_DEBUG)) {
14509 if (option_debug) {
14510 if (resubscribe)
14511 ast_log(LOG_DEBUG, "Got a re-subscribe on existing subscription %s\n", p->callid);
14512 else
14513 ast_log(LOG_DEBUG, "Got a new subscription %s (possibly with auth)\n", p->callid);
14514 }
14515 }
14516 }
14517
14518
14519
14520
14521 if (!global_allowsubscribe) {
14522 transmit_response(p, "403 Forbidden (policy)", req);
14523 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14524 return 0;
14525 }
14526
14527 if (!ast_test_flag(req, SIP_PKT_IGNORE) && !resubscribe) {
14528
14529 if (ast_test_flag(req, SIP_PKT_DEBUG))
14530 ast_verbose("Creating new subscription\n");
14531
14532 copy_request(&p->initreq, req);
14533 check_via(p, req);
14534 } else if (ast_test_flag(req, SIP_PKT_DEBUG) && ast_test_flag(req, SIP_PKT_IGNORE))
14535 ast_verbose("Ignoring this SUBSCRIBE request\n");
14536
14537
14538 if (ast_strlen_zero(eventheader)) {
14539 transmit_response(p, "489 Bad Event", req);
14540 if (option_debug > 1)
14541 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: <none>\n");
14542 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14543 return 0;
14544 }
14545
14546 if ( (strchr(eventheader, ';'))) {
14547 event = ast_strdupa(eventheader);
14548 temp = strchr(event, ';');
14549 *temp = '\0';
14550
14551 } else
14552 event = (char *) eventheader;
14553
14554
14555 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, &authpeer);
14556
14557 if (res == AUTH_CHALLENGE_SENT) {
14558 if (authpeer)
14559 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14560 return 0;
14561 }
14562 if (res < 0) {
14563 if (res == AUTH_FAKE_AUTH) {
14564 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
14565 transmit_fake_auth_response(p, req, 1);
14566 } else {
14567 ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
14568 transmit_response_reliable(p, "403 Forbidden", req);
14569 }
14570 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14571 if (authpeer)
14572 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14573 return 0;
14574 }
14575
14576
14577 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
14578 transmit_response(p, "403 Forbidden (policy)", req);
14579 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14580 if (authpeer)
14581 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14582 return 0;
14583 }
14584
14585
14586 gotdest = get_destination(p, NULL);
14587
14588
14589
14590
14591
14592
14593 if (!ast_strlen_zero(p->subscribecontext))
14594 ast_string_field_set(p, context, p->subscribecontext);
14595 else if (ast_strlen_zero(p->context))
14596 ast_string_field_set(p, context, default_context);
14597
14598
14599 parse_ok_contact(p, req);
14600
14601 build_contact(p);
14602 if (gotdest) {
14603 transmit_response(p, "404 Not Found", req);
14604 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14605 if (authpeer)
14606 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14607 return 0;
14608 }
14609
14610
14611 if (ast_strlen_zero(p->tag))
14612 make_our_tag(p->tag, sizeof(p->tag));
14613
14614 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
14615 if (authpeer)
14616 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14617
14618
14619
14620
14621
14622 if (strstr(p->useragent, "Polycom")) {
14623 p->subscribed = XPIDF_XML;
14624 } else if (strstr(accept, "application/pidf+xml")) {
14625 p->subscribed = PIDF_XML;
14626 } else if (strstr(accept, "application/dialog-info+xml")) {
14627 p->subscribed = DIALOG_INFO_XML;
14628
14629 } else if (strstr(accept, "application/cpim-pidf+xml")) {
14630 p->subscribed = CPIM_PIDF_XML;
14631 } else if (strstr(accept, "application/xpidf+xml")) {
14632 p->subscribed = XPIDF_XML;
14633 } else if (ast_strlen_zero(accept)) {
14634 if (p->subscribed == NONE) {
14635 transmit_response(p, "489 Bad Event", req);
14636
14637 ast_log(LOG_WARNING,"SUBSCRIBE failure: no Accept header: pvt: stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
14638 p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
14639 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14640 return 0;
14641 }
14642
14643
14644 } else {
14645
14646 char mybuf[200];
14647 snprintf(mybuf,sizeof(mybuf),"489 Bad Event (format %s)", accept);
14648 transmit_response(p, mybuf, req);
14649
14650 ast_log(LOG_WARNING,"SUBSCRIBE failure: unrecognized format: '%s' pvt: subscribed: %d, stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
14651 accept, (int)p->subscribed, p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
14652 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14653 return 0;
14654 }
14655 } else if (!strcmp(event, "message-summary")) {
14656 if (!ast_strlen_zero(accept) && strcmp(accept, "application/simple-message-summary")) {
14657
14658 transmit_response(p, "406 Not Acceptable", req);
14659 if (option_debug > 1)
14660 ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", accept);
14661 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14662 if (authpeer)
14663 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14664 return 0;
14665 }
14666
14667
14668
14669
14670
14671 if (!authpeer || ast_strlen_zero(authpeer->mailbox)) {
14672 transmit_response(p, "404 Not found (no mailbox)", req);
14673 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14674 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
14675 if (authpeer)
14676 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14677 return 0;
14678 }
14679
14680 p->subscribed = MWI_NOTIFICATION;
14681 if (authpeer->mwipvt && authpeer->mwipvt != p)
14682
14683 sip_destroy(authpeer->mwipvt);
14684 authpeer->mwipvt = p;
14685 p->relatedpeer = authpeer;
14686 } else {
14687 transmit_response(p, "489 Bad Event", req);
14688 if (option_debug > 1)
14689 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
14690 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14691 if (authpeer)
14692 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14693 return 0;
14694 }
14695
14696 if (p->subscribed != MWI_NOTIFICATION && !resubscribe) {
14697 if (p->stateid > -1)
14698 ast_extension_state_del(p->stateid, cb_extensionstate);
14699 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
14700 }
14701
14702 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
14703 p->lastinvite = seqno;
14704 if (p && !ast_test_flag(&p->flags[0], SIP_NEEDDESTROY)) {
14705 p->expiry = atoi(get_header(req, "Expires"));
14706
14707
14708 if (p->expiry > max_expiry)
14709 p->expiry = max_expiry;
14710 if (p->expiry < min_expiry && p->expiry > 0)
14711 p->expiry = min_expiry;
14712
14713 if (sipdebug || option_debug > 1) {
14714 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
14715 ast_log(LOG_DEBUG, "Adding subscription for mailbox notification - peer %s Mailbox %s\n", p->relatedpeer->name, p->relatedpeer->mailbox);
14716 else
14717 ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
14718 }
14719 if (p->autokillid > -1)
14720 sip_cancel_destroy(p);
14721 if (p->expiry > 0)
14722 sip_scheddestroy(p, (p->expiry + 10) * 1000);
14723
14724 if (p->subscribed == MWI_NOTIFICATION) {
14725 transmit_response(p, "200 OK", req);
14726 if (p->relatedpeer) {
14727 ASTOBJ_WRLOCK(p->relatedpeer);
14728 sip_send_mwi_to_peer(p->relatedpeer);
14729 ASTOBJ_UNLOCK(p->relatedpeer);
14730 }
14731 } else {
14732 struct sip_pvt *p_old;
14733
14734 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
14735
14736 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));
14737 transmit_response(p, "404 Not found", req);
14738 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14739 return 0;
14740 }
14741
14742 transmit_response(p, "200 OK", req);
14743 transmit_state_notify(p, firststate, 1, FALSE);
14744 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
14745
14746 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
14747
14748
14749
14750
14751
14752
14753 ast_mutex_lock(&iflock);
14754 for (p_old = iflist; p_old; p_old = p_old->next) {
14755 if (p_old == p)
14756 continue;
14757 if (p_old->initreq.method != SIP_SUBSCRIBE)
14758 continue;
14759 if (p_old->subscribed == NONE)
14760 continue;
14761 ast_mutex_lock(&p_old->lock);
14762 if (!strcmp(p_old->username, p->username)) {
14763 if (!strcmp(p_old->exten, p->exten) &&
14764 !strcmp(p_old->context, p->context)) {
14765 ast_set_flag(&p_old->flags[0], SIP_NEEDDESTROY);
14766 ast_mutex_unlock(&p_old->lock);
14767 break;
14768 }
14769 }
14770 ast_mutex_unlock(&p_old->lock);
14771 }
14772 ast_mutex_unlock(&iflock);
14773 }
14774 if (!p->expiry)
14775 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14776 }
14777 return 1;
14778 }
14779
14780
14781 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e)
14782 {
14783 enum check_auth_result res;
14784
14785
14786 if (ast_test_flag(req, SIP_PKT_DEBUG))
14787 ast_verbose("Using latest REGISTER request as basis request\n");
14788 copy_request(&p->initreq, req);
14789 check_via(p, req);
14790 if ((res = register_verify(p, sin, req, e)) < 0) {
14791 const char *reason;
14792
14793 switch (res) {
14794 case AUTH_SECRET_FAILED:
14795 reason = "Wrong password";
14796 break;
14797 case AUTH_USERNAME_MISMATCH:
14798 reason = "Username/auth name mismatch";
14799 break;
14800 case AUTH_NOT_FOUND:
14801 reason = "No matching peer found";
14802 break;
14803 case AUTH_UNKNOWN_DOMAIN:
14804 reason = "Not a local domain";
14805 break;
14806 case AUTH_PEER_NOT_DYNAMIC:
14807 reason = "Peer is not supposed to register";
14808 break;
14809 case AUTH_ACL_FAILED:
14810 reason = "Device does not match ACL";
14811 break;
14812 default:
14813 reason = "Unknown failure";
14814 break;
14815 }
14816 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
14817 get_header(req, "To"), ast_inet_ntoa(sin->sin_addr),
14818 reason);
14819 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
14820 } else
14821 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
14822
14823 if (res < 1) {
14824
14825
14826 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14827 }
14828 return res;
14829 }
14830
14831
14832
14833
14834 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
14835 {
14836
14837
14838 const char *cmd;
14839 const char *cseq;
14840 const char *useragent;
14841 int seqno;
14842 int len;
14843 int ignore = FALSE;
14844 int respid;
14845 int res = 0;
14846 int debug = sip_debug_test_pvt(p);
14847 char *e;
14848 int error = 0;
14849
14850
14851 cseq = get_header(req, "Cseq");
14852 cmd = req->header[0];
14853
14854
14855 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq)) {
14856 ast_log(LOG_ERROR, "Missing Cseq. Dropping this SIP message, it's incomplete.\n");
14857 error = 1;
14858 }
14859 if (!error && sscanf(cseq, "%d%n", &seqno, &len) != 1) {
14860 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
14861 error = 1;
14862 }
14863 if (error) {
14864 if (!p->initreq.headers)
14865 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14866 return -1;
14867 }
14868
14869
14870 cmd = req->rlPart1;
14871 e = req->rlPart2;
14872
14873
14874 useragent = get_header(req, "User-Agent");
14875 if (!ast_strlen_zero(useragent))
14876 ast_string_field_set(p, useragent, useragent);
14877
14878
14879 if (req->method == SIP_RESPONSE) {
14880
14881 if (!p->initreq.headers) {
14882 if (option_debug)
14883 ast_log(LOG_DEBUG, "That's odd... Got a response on a call we dont know about. Cseq %d Cmd %s\n", seqno, cmd);
14884 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14885 return 0;
14886 } else if (p->ocseq && (p->ocseq < seqno)) {
14887 if (option_debug)
14888 ast_log(LOG_DEBUG, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
14889 return -1;
14890 } else if (p->ocseq && (p->ocseq != seqno)) {
14891
14892
14893 ignore = TRUE;
14894 ast_set_flag(req, SIP_PKT_IGNORE);
14895 ast_set_flag(req, SIP_PKT_IGNORE_RESP);
14896 append_history(p, "Ignore", "Ignoring this retransmit\n");
14897 } else if (e) {
14898 e = ast_skip_blanks(e);
14899 if (sscanf(e, "%d %n", &respid, &len) != 1) {
14900 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
14901 } else {
14902 if (respid <= 0) {
14903 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
14904 return 0;
14905 }
14906
14907 if ((respid == 200) || ((respid >= 300) && (respid <= 399)))
14908 extract_uri(p, req);
14909 handle_response(p, respid, e + len, req, ignore, seqno);
14910 }
14911 }
14912 return 0;
14913 }
14914
14915
14916
14917
14918
14919 p->method = req->method;
14920 if (option_debug > 3)
14921 ast_log(LOG_DEBUG, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
14922
14923 if (p->icseq && (p->icseq > seqno)) {
14924 if (option_debug)
14925 ast_log(LOG_DEBUG, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
14926 if (req->method != SIP_ACK)
14927 transmit_response(p, "503 Server error", req);
14928 return -1;
14929 } else if (p->icseq &&
14930 p->icseq == seqno &&
14931 req->method != SIP_ACK &&
14932 (p->method != SIP_CANCEL || ast_test_flag(&p->flags[0], SIP_ALREADYGONE))) {
14933
14934
14935
14936 ignore = 2;
14937 ast_set_flag(req, SIP_PKT_IGNORE);
14938 ast_set_flag(req, SIP_PKT_IGNORE_REQ);
14939 if (option_debug > 2)
14940 ast_log(LOG_DEBUG, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
14941 }
14942
14943 if (seqno >= p->icseq)
14944
14945
14946
14947 p->icseq = seqno;
14948
14949
14950 if (ast_strlen_zero(p->theirtag)) {
14951 char tag[128];
14952
14953 gettag(req, "From", tag, sizeof(tag));
14954 ast_string_field_set(p, theirtag, tag);
14955 }
14956 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
14957
14958 if (pedanticsipchecking) {
14959
14960
14961
14962
14963 if (!p->initreq.headers && ast_test_flag(req, SIP_PKT_WITH_TOTAG)) {
14964
14965 if (!ast_test_flag(req, SIP_PKT_IGNORE) && req->method == SIP_INVITE) {
14966 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
14967
14968 } else if (req->method != SIP_ACK) {
14969 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
14970 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14971 }
14972 return res;
14973 }
14974 }
14975
14976 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY)) {
14977 transmit_response(p, "400 Bad request", req);
14978 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14979 return -1;
14980 }
14981
14982
14983 switch (p->method) {
14984 case SIP_OPTIONS:
14985 res = handle_request_options(p, req);
14986 break;
14987 case SIP_INVITE:
14988 res = handle_request_invite(p, req, debug, seqno, sin, recount, e, nounlock);
14989 break;
14990 case SIP_REFER:
14991 res = handle_request_refer(p, req, debug, ignore, seqno, nounlock);
14992 break;
14993 case SIP_CANCEL:
14994 res = handle_request_cancel(p, req);
14995 break;
14996 case SIP_BYE:
14997 res = handle_request_bye(p, req);
14998 break;
14999 case SIP_MESSAGE:
15000 res = handle_request_message(p, req);
15001 break;
15002 case SIP_SUBSCRIBE:
15003 res = handle_request_subscribe(p, req, sin, seqno, e);
15004 break;
15005 case SIP_REGISTER:
15006 res = handle_request_register(p, req, sin, e);
15007 break;
15008 case SIP_INFO:
15009 if (ast_test_flag(req, SIP_PKT_DEBUG))
15010 ast_verbose("Receiving INFO!\n");
15011 if (!ignore)
15012 handle_request_info(p, req);
15013 else
15014 transmit_response(p, "200 OK", req);
15015 break;
15016 case SIP_NOTIFY:
15017 res = handle_request_notify(p, req, sin, seqno, e);
15018 break;
15019 case SIP_ACK:
15020
15021 if (seqno == p->pendinginvite) {
15022 p->invitestate = INV_TERMINATED;
15023 p->pendinginvite = 0;
15024 __sip_ack(p, seqno, FLAG_RESPONSE, 0);
15025 if (find_sdp(req)) {
15026 if (process_sdp(p, req))
15027 return -1;
15028 }
15029 check_pendings(p);
15030 }
15031
15032 if (!p->lastinvite && ast_strlen_zero(p->randdata))
15033 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15034 break;
15035 default:
15036 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
15037 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
15038 cmd, ast_inet_ntoa(p->sa.sin_addr));
15039
15040 if (!p->initreq.headers)
15041 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
15042 break;
15043 }
15044 return res;
15045 }
15046
15047
15048
15049
15050
15051
15052 static int sipsock_read(int *id, int fd, short events, void *ignore)
15053 {
15054 struct sip_request req;
15055 struct sockaddr_in sin = { 0, };
15056 struct sip_pvt *p;
15057 int res;
15058 socklen_t len = sizeof(sin);
15059 int nounlock;
15060 int recount = 0;
15061 int lockretry;
15062
15063 memset(&req, 0, sizeof(req));
15064 res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
15065 if (res < 0) {
15066 #if !defined(__FreeBSD__)
15067 if (errno == EAGAIN)
15068 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
15069 else
15070 #endif
15071 if (errno != ECONNREFUSED)
15072 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
15073 return 1;
15074 }
15075 if (option_debug && res == sizeof(req.data)) {
15076 ast_log(LOG_DEBUG, "Received packet exceeds buffer. Data is possibly lost\n");
15077 req.data[sizeof(req.data) - 1] = '\0';
15078 } else
15079 req.data[res] = '\0';
15080 req.len = res;
15081 if(sip_debug_test_addr(&sin))
15082 ast_set_flag(&req, SIP_PKT_DEBUG);
15083 if (pedanticsipchecking)
15084 req.len = lws2sws(req.data, req.len);
15085 if (ast_test_flag(&req, SIP_PKT_DEBUG))
15086 ast_verbose("\n<--- SIP read from %s:%d --->\n%s\n<------------->\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), req.data);
15087
15088 parse_request(&req);
15089 req.method = find_sip_method(req.rlPart1);
15090
15091 if (ast_test_flag(&req, SIP_PKT_DEBUG))
15092 ast_verbose("--- (%d headers %d lines)%s ---\n", req.headers, req.lines, (req.headers + req.lines == 0) ? " Nat keepalive" : "");
15093
15094 if (req.headers < 2)
15095 return 1;
15096
15097
15098 for (lockretry = 100; lockretry > 0; lockretry--) {
15099 ast_mutex_lock(&netlock);
15100
15101
15102 p = find_call(&req, &sin, req.method);
15103 if (p == NULL) {
15104 if (option_debug)
15105 ast_log(LOG_DEBUG, "Invalid SIP message - rejected , no callid, len %d\n", req.len);
15106 ast_mutex_unlock(&netlock);
15107 return 1;
15108 }
15109
15110
15111 if (!p->owner || !ast_channel_trylock(p->owner))
15112 break;
15113 if (option_debug)
15114 ast_log(LOG_DEBUG, "Failed to grab owner channel lock, trying again. (SIP call %s)\n", p->callid);
15115 ast_mutex_unlock(&p->lock);
15116 ast_mutex_unlock(&netlock);
15117
15118 usleep(1);
15119 }
15120 p->recv = sin;
15121
15122 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
15123 append_history(p, "Rx", "%s / %s / %s", req.data, get_header(&req, "CSeq"), req.rlPart2);
15124
15125 if (!lockretry) {
15126 if (p->owner)
15127 ast_log(LOG_ERROR, "We could NOT get the channel lock for %s! \n", S_OR(p->owner->name, "- no channel name ??? - "));
15128 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
15129 if (req.method != SIP_ACK)
15130 transmit_response(p, "503 Server error", &req);
15131
15132 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
15133 return 1;
15134 }
15135 nounlock = 0;
15136 if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
15137
15138 if (option_debug)
15139 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
15140 }
15141
15142 if (p->owner && !nounlock)
15143 ast_channel_unlock(p->owner);
15144 ast_mutex_unlock(&p->lock);
15145 ast_mutex_unlock(&netlock);
15146 if (recount)
15147 ast_update_use_count();
15148
15149 return 1;
15150 }
15151
15152
15153 static int sip_send_mwi_to_peer(struct sip_peer *peer)
15154 {
15155
15156 struct sip_pvt *p;
15157 int newmsgs, oldmsgs;
15158
15159
15160 if (!peer->addr.sin_addr.s_addr && !peer->defaddr.sin_addr.s_addr)
15161 return 0;
15162
15163
15164 ast_app_inboxcount(peer->mailbox, &newmsgs, &oldmsgs);
15165
15166 peer->lastmsgcheck = time(NULL);
15167
15168
15169 if (((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)) == peer->lastmsgssent) {
15170 return 0;
15171 }
15172
15173
15174 peer->lastmsgssent = ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs));
15175
15176 if (peer->mwipvt) {
15177
15178 p = peer->mwipvt;
15179 } else {
15180
15181 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY)))
15182 return -1;
15183 if (create_addr_from_peer(p, peer)) {
15184
15185 sip_destroy(p);
15186 return 0;
15187 }
15188
15189 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
15190 p->ourip = __ourip;
15191 build_via(p);
15192 build_callid_pvt(p);
15193
15194 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15195 }
15196
15197 ast_set_flag(&p->flags[0], SIP_OUTGOING);
15198 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
15199 return 0;
15200 }
15201
15202
15203 static int does_peer_need_mwi(struct sip_peer *peer)
15204 {
15205 time_t t = time(NULL);
15206
15207 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
15208 !peer->mwipvt) {
15209 peer->lastmsgcheck = t;
15210 return FALSE;
15211 }
15212
15213 if (!ast_strlen_zero(peer->mailbox) && (t - peer->lastmsgcheck) > global_mwitime)
15214 return TRUE;
15215
15216 return FALSE;
15217 }
15218
15219
15220
15221
15222
15223
15224 static void *do_monitor(void *data)
15225 {
15226 int res;
15227 struct sip_pvt *sip;
15228 struct sip_peer *peer = NULL;
15229 time_t t;
15230 int fastrestart = FALSE;
15231 int lastpeernum = -1;
15232 int curpeernum;
15233 int reloading;
15234
15235
15236 if (sipsock > -1)
15237 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
15238
15239
15240 for(;;) {
15241
15242 ast_mutex_lock(&sip_reload_lock);
15243 reloading = sip_reloading;
15244 sip_reloading = FALSE;
15245 ast_mutex_unlock(&sip_reload_lock);
15246 if (reloading) {
15247 if (option_verbose > 0)
15248 ast_verbose(VERBOSE_PREFIX_1 "Reloading SIP\n");
15249 sip_do_reload(sip_reloadreason);
15250
15251
15252 if (sipsock > -1) {
15253 if (sipsock_read_id)
15254 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
15255 else
15256 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
15257 }
15258 }
15259
15260 ast_mutex_lock(&iflock);
15261 restartsearch:
15262 t = time(NULL);
15263
15264
15265
15266
15267 for (sip = iflist; !fastrestart && sip; sip = sip->next) {
15268 ast_mutex_lock(&sip->lock);
15269
15270 if (sip->rtp && sip->owner &&
15271 (sip->owner->_state == AST_STATE_UP) &&
15272 !sip->redirip.sin_addr.s_addr &&
15273 sip->t38.state != T38_ENABLED) {
15274 if (sip->lastrtptx &&
15275 ast_rtp_get_rtpkeepalive(sip->rtp) &&
15276 (t > sip->lastrtptx + ast_rtp_get_rtpkeepalive(sip->rtp))) {
15277
15278 sip->lastrtptx = time(NULL);
15279 ast_rtp_sendcng(sip->rtp, 0);
15280 }
15281 if (sip->lastrtprx &&
15282 (ast_rtp_get_rtptimeout(sip->rtp) || ast_rtp_get_rtpholdtimeout(sip->rtp)) &&
15283 (t > sip->lastrtprx + ast_rtp_get_rtptimeout(sip->rtp))) {
15284
15285 struct sockaddr_in sin;
15286 ast_rtp_get_peer(sip->rtp, &sin);
15287 if (sin.sin_addr.s_addr ||
15288 (ast_rtp_get_rtpholdtimeout(sip->rtp) &&
15289 (t > sip->lastrtprx + ast_rtp_get_rtpholdtimeout(sip->rtp)))) {
15290
15291 if (ast_rtp_get_rtptimeout(sip->rtp)) {
15292 while (sip->owner && ast_channel_trylock(sip->owner)) {
15293 ast_mutex_unlock(&sip->lock);
15294 usleep(1);
15295 ast_mutex_lock(&sip->lock);
15296 }
15297 if (sip->owner) {
15298 if (!(ast_rtp_get_bridged(sip->rtp))) {
15299 ast_log(LOG_NOTICE,
15300 "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
15301 sip->owner->name,
15302 (long) (t - sip->lastrtprx));
15303
15304 ast_softhangup_nolock(sip->owner, AST_SOFTHANGUP_DEV);
15305 } else
15306 ast_log(LOG_NOTICE, "'%s' will not be disconnected in %ld seconds because it is directly bridged to another RTP stream\n", sip->owner->name, (long) (t - sip->lastrtprx));
15307 ast_channel_unlock(sip->owner);
15308
15309
15310
15311
15312 ast_rtp_set_rtptimeout(sip->rtp, 0);
15313 ast_rtp_set_rtpholdtimeout(sip->rtp, 0);
15314 if (sip->vrtp) {
15315 ast_rtp_set_rtptimeout(sip->vrtp, 0);
15316 ast_rtp_set_rtpholdtimeout(sip->vrtp, 0);
15317 }
15318 }
15319 }
15320 }
15321 }
15322 }
15323
15324 if (ast_test_flag(&sip->flags[0], SIP_NEEDDESTROY) && !sip->packets &&
15325 !sip->owner) {
15326 ast_mutex_unlock(&sip->lock);
15327 __sip_destroy(sip, 1);
15328 goto restartsearch;
15329 }
15330 ast_mutex_unlock(&sip->lock);
15331 }
15332 ast_mutex_unlock(&iflock);
15333
15334 pthread_testcancel();
15335
15336 res = ast_sched_wait(sched);
15337 if ((res < 0) || (res > 1000))
15338 res = 1000;
15339
15340 if (fastrestart)
15341 res = 1;
15342 res = ast_io_wait(io, res);
15343 if (option_debug && res > 20)
15344 ast_log(LOG_DEBUG, "chan_sip: ast_io_wait ran %d all at once\n", res);
15345 ast_mutex_lock(&monlock);
15346 if (res >= 0) {
15347 res = ast_sched_runq(sched);
15348 if (option_debug && res >= 20)
15349 ast_log(LOG_DEBUG, "chan_sip: ast_sched_runq ran %d all at once\n", res);
15350 }
15351
15352
15353 t = time(NULL);
15354 fastrestart = FALSE;
15355 curpeernum = 0;
15356 peer = NULL;
15357
15358 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !peer, do {
15359 if ((curpeernum > lastpeernum) && does_peer_need_mwi(iterator)) {
15360 fastrestart = TRUE;
15361 lastpeernum = curpeernum;
15362 peer = ASTOBJ_REF(iterator);
15363 };
15364 curpeernum++;
15365 } while (0)
15366 );
15367
15368 if (peer) {
15369 ASTOBJ_WRLOCK(peer);
15370 sip_send_mwi_to_peer(peer);
15371 ASTOBJ_UNLOCK(peer);
15372 ASTOBJ_UNREF(peer,sip_destroy_peer);
15373 } else {
15374
15375 lastpeernum = -1;
15376 }
15377 ast_mutex_unlock(&monlock);
15378 }
15379
15380 return NULL;
15381
15382 }
15383
15384
15385 static int restart_monitor(void)
15386 {
15387
15388 if (monitor_thread == AST_PTHREADT_STOP)
15389 return 0;
15390 ast_mutex_lock(&monlock);
15391 if (monitor_thread == pthread_self()) {
15392 ast_mutex_unlock(&monlock);
15393 ast_log(LOG_WARNING, "Cannot kill myself\n");
15394 return -1;
15395 }
15396 if (monitor_thread != AST_PTHREADT_NULL) {
15397
15398 pthread_kill(monitor_thread, SIGURG);
15399 } else {
15400
15401 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
15402 ast_mutex_unlock(&monlock);
15403 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
15404 return -1;
15405 }
15406 }
15407 ast_mutex_unlock(&monlock);
15408 return 0;
15409 }
15410
15411
15412 static int sip_poke_noanswer(void *data)
15413 {
15414 struct sip_peer *peer = data;
15415
15416 peer->pokeexpire = -1;
15417 if (peer->lastms > -1) {
15418 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
15419 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
15420 }
15421 if (peer->call)
15422 sip_destroy(peer->call);
15423 peer->call = NULL;
15424 peer->lastms = -1;
15425 ast_device_state_changed("SIP/%s", peer->name);
15426
15427 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
15428 return 0;
15429 }
15430
15431
15432
15433
15434 static int sip_poke_peer(struct sip_peer *peer)
15435 {
15436 struct sip_pvt *p;
15437 int xmitres = 0;
15438
15439 if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
15440
15441
15442 if (peer->pokeexpire > -1)
15443 ast_sched_del(sched, peer->pokeexpire);
15444 peer->lastms = 0;
15445 peer->pokeexpire = -1;
15446 peer->call = NULL;
15447 return 0;
15448 }
15449 if (peer->call) {
15450 if (sipdebug)
15451 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
15452 sip_destroy(peer->call);
15453 }
15454 if (!(p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS)))
15455 return -1;
15456
15457 p->sa = peer->addr;
15458 p->recv = peer->addr;
15459 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
15460 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
15461
15462
15463 if (!ast_strlen_zero(peer->fullcontact))
15464 ast_string_field_set(p, fullcontact, peer->fullcontact);
15465
15466 if (!ast_strlen_zero(peer->tohost))
15467 ast_string_field_set(p, tohost, peer->tohost);
15468 else
15469 ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr));
15470
15471
15472 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
15473 p->ourip = __ourip;
15474 build_via(p);
15475 build_callid_pvt(p);
15476
15477 if (peer->pokeexpire > -1)
15478 ast_sched_del(sched, peer->pokeexpire);
15479 p->relatedpeer = peer;
15480 ast_set_flag(&p->flags[0], SIP_OUTGOING);
15481 #ifdef VOCAL_DATA_HACK
15482 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
15483 xmitres = transmit_invite(p, SIP_INVITE, 0, 2);
15484 #else
15485 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2);
15486 #endif
15487 gettimeofday(&peer->ps, NULL);
15488 if (xmitres == XMIT_ERROR)
15489 sip_poke_noanswer(peer);
15490 else
15491 peer->pokeexpire = ast_sched_add(sched, DEFAULT_MAXMS * 2, sip_poke_noanswer, peer);
15492
15493 return 0;
15494 }
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529 static int sip_devicestate(void *data)
15530 {
15531 char *host;
15532 char *tmp;
15533
15534 struct hostent *hp;
15535 struct ast_hostent ahp;
15536 struct sip_peer *p;
15537
15538 int res = AST_DEVICE_INVALID;
15539
15540
15541 host = ast_strdupa(data ? data : "");
15542 if ((tmp = strchr(host, '@')))
15543 host = tmp + 1;
15544
15545 if (option_debug > 2)
15546 ast_log(LOG_DEBUG, "Checking device state for peer %s\n", host);
15547
15548 if ((p = find_peer(host, NULL, 1))) {
15549 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561 if (p->onHold)
15562
15563 res = AST_DEVICE_ONHOLD;
15564 else if (p->inRinging) {
15565 if (p->inRinging == p->inUse)
15566 res = AST_DEVICE_RINGING;
15567 else
15568 res = AST_DEVICE_RINGINUSE;
15569 } else if (p->call_limit && (p->inUse == p->call_limit))
15570
15571 res = AST_DEVICE_BUSY;
15572 else if (p->call_limit && p->inUse)
15573
15574 res = AST_DEVICE_INUSE;
15575 else if (p->maxms && (p->lastms > p->maxms))
15576
15577 res = AST_DEVICE_UNAVAILABLE;
15578 else
15579 res = AST_DEVICE_NOT_INUSE;
15580 } else {
15581
15582 res = AST_DEVICE_UNAVAILABLE;
15583 }
15584 ASTOBJ_UNREF(p,sip_destroy_peer);
15585 } else {
15586 hp = ast_gethostbyname(host, &ahp);
15587 if (hp)
15588 res = AST_DEVICE_UNKNOWN;
15589 }
15590
15591 return res;
15592 }
15593
15594
15595
15596 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
15597 {
15598 int oldformat;
15599 struct sip_pvt *p;
15600 struct ast_channel *tmpc = NULL;
15601 char *ext, *host;
15602 char tmp[256];
15603 char *dest = data;
15604
15605 oldformat = format;
15606 if (!(format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1))) {
15607 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));
15608 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
15609 return NULL;
15610 }
15611 if (option_debug)
15612 ast_log(LOG_DEBUG, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
15613
15614 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE))) {
15615 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", (char *)data);
15616 *cause = AST_CAUSE_SWITCH_CONGESTION;
15617 return NULL;
15618 }
15619
15620 ast_set_flag(&p->flags[1], SIP_PAGE2_OUTGOING_CALL);
15621
15622 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
15623 sip_destroy(p);
15624 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
15625 *cause = AST_CAUSE_SWITCH_CONGESTION;
15626 return NULL;
15627 }
15628
15629 ast_copy_string(tmp, dest, sizeof(tmp));
15630 host = strchr(tmp, '@');
15631 if (host) {
15632 *host++ = '\0';
15633 ext = tmp;
15634 } else {
15635 ext = strchr(tmp, '/');
15636 if (ext)
15637 *ext++ = '\0';
15638 host = tmp;
15639 }
15640
15641 if (create_addr(p, host)) {
15642 *cause = AST_CAUSE_UNREGISTERED;
15643 if (option_debug > 2)
15644 ast_log(LOG_DEBUG, "Cant create SIP call - target device not registred\n");
15645 sip_destroy(p);
15646 return NULL;
15647 }
15648 if (ast_strlen_zero(p->peername) && ext)
15649 ast_string_field_set(p, peername, ext);
15650
15651 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
15652 p->ourip = __ourip;
15653 build_via(p);
15654 build_callid_pvt(p);
15655
15656
15657
15658
15659
15660 if (ext) {
15661 ast_string_field_set(p, username, ext);
15662 ast_string_field_free(p, fullcontact);
15663 }
15664 #if 0
15665 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
15666 #endif
15667 p->prefcodec = oldformat;
15668 ast_mutex_lock(&p->lock);
15669 tmpc = sip_new(p, AST_STATE_DOWN, host);
15670 ast_mutex_unlock(&p->lock);
15671 if (!tmpc)
15672 sip_destroy(p);
15673 ast_update_use_count();
15674 restart_monitor();
15675 return tmpc;
15676 }
15677
15678
15679
15680
15681
15682
15683
15684 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno)
15685 {
15686 if (!strcasecmp(value, "very"))
15687 ast_set_flag(flags, SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
15688 else if (ast_true(value))
15689 ast_set_flag(flags, SIP_INSECURE_PORT);
15690 else if (!ast_false(value)) {
15691 char buf[64];
15692 char *word, *next;
15693 ast_copy_string(buf, value, sizeof(buf));
15694 next = buf;
15695 while ((word = strsep(&next, ","))) {
15696 if (!strcasecmp(word, "port"))
15697 ast_set_flag(flags, SIP_INSECURE_PORT);
15698 else if (!strcasecmp(word, "invite"))
15699 ast_set_flag(flags, SIP_INSECURE_INVITE);
15700 else
15701 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
15702 }
15703 }
15704 }
15705
15706
15707
15708
15709
15710
15711
15712
15713 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
15714 {
15715 int res = 1;
15716 static int dep_insecure_very = 0;
15717 static int dep_insecure_yes = 0;
15718
15719 if (!strcasecmp(v->name, "trustrpid")) {
15720 ast_set_flag(&mask[0], SIP_TRUSTRPID);
15721 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
15722 } else if (!strcasecmp(v->name, "sendrpid")) {
15723 ast_set_flag(&mask[0], SIP_SENDRPID);
15724 ast_set2_flag(&flags[0], ast_true(v->value), SIP_SENDRPID);
15725 } else if (!strcasecmp(v->name, "g726nonstandard")) {
15726 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
15727 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
15728 } else if (!strcasecmp(v->name, "useclientcode")) {
15729 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
15730 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
15731 } else if (!strcasecmp(v->name, "dtmfmode")) {
15732 ast_set_flag(&mask[0], SIP_DTMF);
15733 ast_clear_flag(&flags[0], SIP_DTMF);
15734 if (!strcasecmp(v->value, "inband"))
15735 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
15736 else if (!strcasecmp(v->value, "rfc2833"))
15737 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
15738 else if (!strcasecmp(v->value, "info"))
15739 ast_set_flag(&flags[0], SIP_DTMF_INFO);
15740 else if (!strcasecmp(v->value, "auto"))
15741 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
15742 else {
15743 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
15744 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
15745 }
15746 } else if (!strcasecmp(v->name, "nat")) {
15747 ast_set_flag(&mask[0], SIP_NAT);
15748 ast_clear_flag(&flags[0], SIP_NAT);
15749 if (!strcasecmp(v->value, "never"))
15750 ast_set_flag(&flags[0], SIP_NAT_NEVER);
15751 else if (!strcasecmp(v->value, "route"))
15752 ast_set_flag(&flags[0], SIP_NAT_ROUTE);
15753 else if (ast_true(v->value))
15754 ast_set_flag(&flags[0], SIP_NAT_ALWAYS);
15755 else
15756 ast_set_flag(&flags[0], SIP_NAT_RFC3581);
15757 } else if (!strcasecmp(v->name, "canreinvite")) {
15758 ast_set_flag(&mask[0], SIP_REINVITE);
15759 ast_clear_flag(&flags[0], SIP_REINVITE);
15760 set_insecure_flags(flags, v->value, v->lineno);
15761 } else if (!strcasecmp(v->name, "insecure")) {
15762 ast_set_flag(&mask[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
15763 ast_clear_flag(&flags[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
15764 if (!strcasecmp(v->value, "very")) {
15765 ast_set_flag(&flags[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
15766 if (!dep_insecure_very) {
15767 ast_log(LOG_WARNING, "insecure=very at line %d is deprecated; use insecure=port,invite instead\n", v->lineno);
15768 dep_insecure_very = 1;
15769 }
15770 }
15771 else if (ast_true(v->value)) {
15772 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
15773 if (!dep_insecure_yes) {
15774 ast_log(LOG_WARNING, "insecure=%s at line %d is deprecated; use insecure=port instead\n", v->value, v->lineno);
15775 dep_insecure_yes = 1;
15776 }
15777 }
15778 else if (!ast_false(v->value)) {
15779 char buf[64];
15780 char *word, *next;
15781
15782 ast_copy_string(buf, v->value, sizeof(buf));
15783 next = buf;
15784 while ((word = strsep(&next, ","))) {
15785 if (!strcasecmp(word, "port"))
15786 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
15787 else if (!strcasecmp(word, "invite"))
15788 ast_set_flag(&flags[0], SIP_INSECURE_INVITE);
15789 else
15790 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", v->value, v->lineno);
15791 }
15792 }
15793 } else if (!strcasecmp(v->name, "progressinband")) {
15794 ast_set_flag(&mask[0], SIP_PROG_INBAND);
15795 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
15796 if (ast_true(v->value))
15797 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
15798 else if (strcasecmp(v->value, "never"))
15799 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
15800 } else if (!strcasecmp(v->name, "promiscredir")) {
15801 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
15802 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
15803 } else if (!strcasecmp(v->name, "videosupport")) {
15804 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
15805 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
15806 } else if (!strcasecmp(v->name, "allowoverlap")) {
15807 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
15808 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
15809 } else if (!strcasecmp(v->name, "allowsubscribe")) {
15810 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
15811 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
15812 } else if (!strcasecmp(v->name, "t38pt_udptl")) {
15813 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_UDPTL);
15814 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);
15815 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
15816 } else if (!strcasecmp(v->name, "t38pt_rtp")) {
15817 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_RTP);
15818 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);
15819 } else if (!strcasecmp(v->name, "t38pt_tcp")) {
15820 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_TCP);
15821 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);
15822 #endif
15823 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
15824 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
15825 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
15826 } else if (!strcasecmp(v->name, "buggymwi")) {
15827 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
15828 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
15829 } else
15830 res = 0;
15831
15832 return res;
15833 }
15834
15835
15836 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
15837 {
15838 struct domain *d;
15839
15840 if (ast_strlen_zero(domain)) {
15841 ast_log(LOG_WARNING, "Zero length domain.\n");
15842 return 1;
15843 }
15844
15845 if (!(d = ast_calloc(1, sizeof(*d))))
15846 return 0;
15847
15848 ast_copy_string(d->domain, domain, sizeof(d->domain));
15849
15850 if (!ast_strlen_zero(context))
15851 ast_copy_string(d->context, context, sizeof(d->context));
15852
15853 d->mode = mode;
15854
15855 AST_LIST_LOCK(&domain_list);
15856 AST_LIST_INSERT_TAIL(&domain_list, d, list);
15857 AST_LIST_UNLOCK(&domain_list);
15858
15859 if (sipdebug)
15860 ast_log(LOG_DEBUG, "Added local SIP domain '%s'\n", domain);
15861
15862 return 1;
15863 }
15864
15865
15866 static int check_sip_domain(const char *domain, char *context, size_t len)
15867 {
15868 struct domain *d;
15869 int result = 0;
15870
15871 AST_LIST_LOCK(&domain_list);
15872 AST_LIST_TRAVERSE(&domain_list, d, list) {
15873 if (strcasecmp(d->domain, domain))
15874 continue;
15875
15876 if (len && !ast_strlen_zero(d->context))
15877 ast_copy_string(context, d->context, len);
15878
15879 result = 1;
15880 break;
15881 }
15882 AST_LIST_UNLOCK(&domain_list);
15883
15884 return result;
15885 }
15886
15887
15888 static void clear_sip_domains(void)
15889 {
15890 struct domain *d;
15891
15892 AST_LIST_LOCK(&domain_list);
15893 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
15894 free(d);
15895 AST_LIST_UNLOCK(&domain_list);
15896 }
15897
15898
15899
15900 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno)
15901 {
15902 char authcopy[256];
15903 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
15904 char *stringp;
15905 struct sip_auth *a, *b, *auth;
15906
15907 if (ast_strlen_zero(configuration))
15908 return authlist;
15909
15910 if (option_debug)
15911 ast_log(LOG_DEBUG, "Auth config :: %s\n", configuration);
15912
15913 ast_copy_string(authcopy, configuration, sizeof(authcopy));
15914 stringp = authcopy;
15915
15916 username = stringp;
15917 realm = strrchr(stringp, '@');
15918 if (realm)
15919 *realm++ = '\0';
15920 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
15921 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
15922 return authlist;
15923 }
15924 stringp = username;
15925 username = strsep(&stringp, ":");
15926 if (username) {
15927 secret = strsep(&stringp, ":");
15928 if (!secret) {
15929 stringp = username;
15930 md5secret = strsep(&stringp,"#");
15931 }
15932 }
15933 if (!(auth = ast_calloc(1, sizeof(*auth))))
15934 return authlist;
15935
15936 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
15937 ast_copy_string(auth->username, username, sizeof(auth->username));
15938 if (secret)
15939 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
15940 if (md5secret)
15941 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
15942
15943
15944 for (b = NULL, a = authlist; a ; b = a, a = a->next)
15945 ;
15946 if (b)
15947 b->next = auth;
15948 else
15949 authlist = auth;
15950
15951 if (option_verbose > 2)
15952 ast_verbose("Added authentication for realm %s\n", realm);
15953
15954 return authlist;
15955
15956 }
15957
15958
15959 static int clear_realm_authentication(struct sip_auth *authlist)
15960 {
15961 struct sip_auth *a = authlist;
15962 struct sip_auth *b;
15963
15964 while (a) {
15965 b = a;
15966 a = a->next;
15967 free(b);
15968 }
15969
15970 return 1;
15971 }
15972
15973
15974 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
15975 {
15976 struct sip_auth *a;
15977
15978 for (a = authlist; a; a = a->next) {
15979 if (!strcasecmp(a->realm, realm))
15980 break;
15981 }
15982
15983 return a;
15984 }
15985
15986
15987 static struct sip_user *build_user(const char *name, struct ast_variable *v, int realtime)
15988 {
15989 struct sip_user *user;
15990 int format;
15991 struct ast_ha *oldha = NULL;
15992 char *varname = NULL, *varval = NULL;
15993 struct ast_variable *tmpvar = NULL;
15994 struct ast_flags userflags[2] = {{(0)}};
15995 struct ast_flags mask[2] = {{(0)}};
15996
15997
15998 if (!(user = ast_calloc(1, sizeof(*user))))
15999 return NULL;
16000
16001 suserobjs++;
16002 ASTOBJ_INIT(user);
16003 ast_copy_string(user->name, name, sizeof(user->name));
16004 oldha = user->ha;
16005 user->ha = NULL;
16006 ast_copy_flags(&user->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
16007 ast_copy_flags(&user->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
16008 user->capability = global_capability;
16009 user->allowtransfer = global_allowtransfer;
16010 user->maxcallbitrate = default_maxcallbitrate;
16011 user->autoframing = global_autoframing;
16012 user->prefs = default_prefs;
16013
16014 strcpy(user->context, default_context);
16015 strcpy(user->language, default_language);
16016 strcpy(user->mohinterpret, default_mohinterpret);
16017 strcpy(user->mohsuggest, default_mohsuggest);
16018 for (; v; v = v->next) {
16019 if (handle_common_options(&userflags[0], &mask[0], v))
16020 continue;
16021
16022 if (!strcasecmp(v->name, "context")) {
16023 ast_copy_string(user->context, v->value, sizeof(user->context));
16024 } else if (!strcasecmp(v->name, "subscribecontext")) {
16025 ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
16026 } else if (!strcasecmp(v->name, "setvar")) {
16027 varname = ast_strdupa(v->value);
16028 if ((varval = strchr(varname,'='))) {
16029 *varval++ = '\0';
16030 if ((tmpvar = ast_variable_new(varname, varval))) {
16031 tmpvar->next = user->chanvars;
16032 user->chanvars = tmpvar;
16033 }
16034 }
16035 } else if (!strcasecmp(v->name, "permit") ||
16036 !strcasecmp(v->name, "deny")) {
16037 user->ha = ast_append_ha(v->name, v->value, user->ha);
16038 } else if (!strcasecmp(v->name, "allowtransfer")) {
16039 user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
16040 } else if (!strcasecmp(v->name, "secret")) {
16041 ast_copy_string(user->secret, v->value, sizeof(user->secret));
16042 } else if (!strcasecmp(v->name, "md5secret")) {
16043 ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
16044 } else if (!strcasecmp(v->name, "callerid")) {
16045 ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
16046 } else if (!strcasecmp(v->name, "fullname")) {
16047 ast_copy_string(user->cid_name, v->value, sizeof(user->cid_name));
16048 } else if (!strcasecmp(v->name, "cid_number")) {
16049 ast_copy_string(user->cid_num, v->value, sizeof(user->cid_num));
16050 } else if (!strcasecmp(v->name, "callgroup")) {
16051 user->callgroup = ast_get_group(v->value);
16052 } else if (!strcasecmp(v->name, "pickupgroup")) {
16053 user->pickupgroup = ast_get_group(v->value);
16054 } else if (!strcasecmp(v->name, "language")) {
16055 ast_copy_string(user->language, v->value, sizeof(user->language));
16056 } else if (!strcasecmp(v->name, "mohinterpret")
16057 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
16058 ast_copy_string(user->mohinterpret, v->value, sizeof(user->mohinterpret));
16059 } else if (!strcasecmp(v->name, "mohsuggest")) {
16060 ast_copy_string(user->mohsuggest, v->value, sizeof(user->mohsuggest));
16061 } else if (!strcasecmp(v->name, "accountcode")) {
16062 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
16063 } else if (!strcasecmp(v->name, "call-limit")) {
16064 user->call_limit = atoi(v->value);
16065 if (user->call_limit < 0)
16066 user->call_limit = 0;
16067 } else if (!strcasecmp(v->name, "amaflags")) {
16068 format = ast_cdr_amaflags2int(v->value);
16069 if (format < 0) {
16070 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
16071 } else {
16072 user->amaflags = format;
16073 }
16074 } else if (!strcasecmp(v->name, "allow")) {
16075 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
16076 } else if (!strcasecmp(v->name, "disallow")) {
16077 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
16078 } else if (!strcasecmp(v->name, "autoframing")) {
16079 user->autoframing = ast_true(v->value);
16080 } else if (!strcasecmp(v->name, "callingpres")) {
16081 user->callingpres = ast_parse_caller_presentation(v->value);
16082 if (user->callingpres == -1)
16083 user->callingpres = atoi(v->value);
16084 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
16085 user->maxcallbitrate = atoi(v->value);
16086 if (user->maxcallbitrate < 0)
16087 user->maxcallbitrate = default_maxcallbitrate;
16088 }
16089
16090
16091
16092 }
16093 ast_copy_flags(&user->flags[0], &userflags[0], mask[0].flags);
16094 ast_copy_flags(&user->flags[1], &userflags[1], mask[1].flags);
16095 if (ast_test_flag(&user->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
16096 global_allowsubscribe = TRUE;
16097 ast_free_ha(oldha);
16098 return user;
16099 }
16100
16101
16102 static void set_peer_defaults(struct sip_peer *peer)
16103 {
16104 if (peer->expire == 0) {
16105
16106
16107
16108 peer->expire = -1;
16109 peer->pokeexpire = -1;
16110 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
16111 }
16112 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
16113 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
16114 strcpy(peer->context, default_context);
16115 strcpy(peer->subscribecontext, default_subscribecontext);
16116 strcpy(peer->language, default_language);
16117 strcpy(peer->mohinterpret, default_mohinterpret);
16118 strcpy(peer->mohsuggest, default_mohsuggest);
16119 peer->addr.sin_family = AF_INET;
16120 peer->defaddr.sin_family = AF_INET;
16121 peer->capability = global_capability;
16122 peer->maxcallbitrate = default_maxcallbitrate;
16123 peer->rtptimeout = global_rtptimeout;
16124 peer->rtpholdtimeout = global_rtpholdtimeout;
16125 peer->rtpkeepalive = global_rtpkeepalive;
16126 peer->allowtransfer = global_allowtransfer;
16127 peer->autoframing = global_autoframing;
16128 strcpy(peer->vmexten, default_vmexten);
16129 peer->secret[0] = '\0';
16130 peer->md5secret[0] = '\0';
16131 peer->cid_num[0] = '\0';
16132 peer->cid_name[0] = '\0';
16133 peer->fromdomain[0] = '\0';
16134 peer->fromuser[0] = '\0';
16135 peer->regexten[0] = '\0';
16136 peer->mailbox[0] = '\0';
16137 peer->callgroup = 0;
16138 peer->pickupgroup = 0;
16139 peer->maxms = default_qualify;
16140 peer->prefs = default_prefs;
16141 }
16142
16143
16144 static struct sip_peer *temp_peer(const char *name)
16145 {
16146 struct sip_peer *peer;
16147
16148 if (!(peer = ast_calloc(1, sizeof(*peer))))
16149 return NULL;
16150
16151 apeerobjs++;
16152 ASTOBJ_INIT(peer);
16153 set_peer_defaults(peer);
16154
16155 ast_copy_string(peer->name, name, sizeof(peer->name));
16156
16157 ast_set_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT);
16158 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16159 peer->prefs = default_prefs;
16160 reg_source_db(peer);
16161
16162 return peer;
16163 }
16164
16165
16166 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
16167 {
16168 struct sip_peer *peer = NULL;
16169 struct ast_ha *oldha = NULL;
16170 int obproxyfound=0;
16171 int found=0;
16172 int firstpass=1;
16173 int format=0;
16174 time_t regseconds = 0;
16175 char *varname = NULL, *varval = NULL;
16176 struct ast_variable *tmpvar = NULL;
16177 struct ast_flags peerflags[2] = {{(0)}};
16178 struct ast_flags mask[2] = {{(0)}};
16179
16180
16181 if (!realtime)
16182
16183
16184
16185
16186
16187 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
16188
16189 if (peer) {
16190
16191 found = 1;
16192 if (!(peer->objflags & ASTOBJ_FLAG_MARKED))
16193 firstpass = 0;
16194 } else {
16195 if (!(peer = ast_calloc(1, sizeof(*peer))))
16196 return NULL;
16197
16198 if (realtime)
16199 rpeerobjs++;
16200 else
16201 speerobjs++;
16202 ASTOBJ_INIT(peer);
16203 }
16204
16205 if (firstpass) {
16206 peer->lastmsgssent = -1;
16207 oldha = peer->ha;
16208 peer->ha = NULL;
16209 set_peer_defaults(peer);
16210 }
16211 if (!found && name)
16212 ast_copy_string(peer->name, name, sizeof(peer->name));
16213
16214
16215 if (peer->chanvars) {
16216 ast_variables_destroy(peer->chanvars);
16217 peer->chanvars = NULL;
16218
16219 }
16220
16221
16222 clear_realm_authentication(peer->auth);
16223 peer->auth = NULL;
16224
16225 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
16226 if (handle_common_options(&peerflags[0], &mask[0], v))
16227 continue;
16228 if (realtime && !strcasecmp(v->name, "regseconds")) {
16229 ast_get_time_t(v->value, ®seconds, 0, NULL);
16230 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
16231 inet_aton(v->value, &(peer->addr.sin_addr));
16232 } else if (realtime && !strcasecmp(v->name, "name"))
16233 ast_copy_string(peer->name, v->value, sizeof(peer->name));
16234 else if (realtime && !strcasecmp(v->name, "fullcontact")) {
16235 ast_copy_string(peer->fullcontact, v->value, sizeof(peer->fullcontact));
16236 ast_set_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT);
16237 } else if (!strcasecmp(v->name, "secret"))
16238 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
16239 else if (!strcasecmp(v->name, "md5secret"))
16240 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
16241 else if (!strcasecmp(v->name, "auth"))
16242 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
16243 else if (!strcasecmp(v->name, "callerid")) {
16244 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
16245 } else if (!strcasecmp(v->name, "fullname")) {
16246 ast_copy_string(peer->cid_name, v->value, sizeof(peer->cid_name));
16247 } else if (!strcasecmp(v->name, "cid_number")) {
16248 ast_copy_string(peer->cid_num, v->value, sizeof(peer->cid_num));
16249 } else if (!strcasecmp(v->name, "context")) {
16250 ast_copy_string(peer->context, v->value, sizeof(peer->context));
16251 } else if (!strcasecmp(v->name, "subscribecontext")) {
16252 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
16253 } else if (!strcasecmp(v->name, "fromdomain")) {
16254 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
16255 } else if (!strcasecmp(v->name, "usereqphone")) {
16256 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
16257 } else if (!strcasecmp(v->name, "fromuser")) {
16258 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
16259 } else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "outboundproxy")) {
16260 if (!strcasecmp(v->value, "dynamic")) {
16261 if (!strcasecmp(v->name, "outboundproxy") || obproxyfound) {
16262 ast_log(LOG_WARNING, "You can't have a dynamic outbound proxy, you big silly head at line %d.\n", v->lineno);
16263 } else {
16264
16265 if (!found || !ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
16266
16267
16268 memset(&peer->addr.sin_addr, 0, 4);
16269 if (peer->addr.sin_port) {
16270
16271 peer->defaddr.sin_port = peer->addr.sin_port;
16272 peer->addr.sin_port = 0;
16273 }
16274 }
16275 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16276 }
16277 } else {
16278
16279 if (peer->expire > -1)
16280 ast_sched_del(sched, peer->expire);
16281 peer->expire = -1;
16282 ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16283 if (!obproxyfound || !strcasecmp(v->name, "outboundproxy")) {
16284 if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
16285 ASTOBJ_UNREF(peer, sip_destroy_peer);
16286 return NULL;
16287 }
16288 }
16289 if (!strcasecmp(v->name, "outboundproxy"))
16290 obproxyfound=1;
16291 else {
16292 ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
16293 if (!peer->addr.sin_port)
16294 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
16295 }
16296 }
16297 } else if (!strcasecmp(v->name, "defaultip")) {
16298 if (ast_get_ip(&peer->defaddr, v->value)) {
16299 ASTOBJ_UNREF(peer, sip_destroy_peer);
16300 return NULL;
16301 }
16302 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
16303 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
16304 } else if (!strcasecmp(v->name, "port")) {
16305 if (!realtime && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC))
16306 peer->defaddr.sin_port = htons(atoi(v->value));
16307 else
16308 peer->addr.sin_port = htons(atoi(v->value));
16309 } else if (!strcasecmp(v->name, "callingpres")) {
16310 peer->callingpres = ast_parse_caller_presentation(v->value);
16311 if (peer->callingpres == -1)
16312 peer->callingpres = atoi(v->value);
16313 } else if (!strcasecmp(v->name, "username")) {
16314 ast_copy_string(peer->username, v->value, sizeof(peer->username));
16315 } else if (!strcasecmp(v->name, "language")) {
16316 ast_copy_string(peer->language, v->value, sizeof(peer->language));
16317 } else if (!strcasecmp(v->name, "regexten")) {
16318 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
16319 } else if (!strcasecmp(v->name, "call-limit") || !strcasecmp(v->name, "incominglimit")) {
16320 peer->call_limit = atoi(v->value);
16321 if (peer->call_limit < 0)
16322 peer->call_limit = 0;
16323 } else if (!strcasecmp(v->name, "amaflags")) {
16324 format = ast_cdr_amaflags2int(v->value);
16325 if (format < 0) {
16326 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
16327 } else {
16328 peer->amaflags = format;
16329 }
16330 } else if (!strcasecmp(v->name, "accountcode")) {
16331 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
16332 } else if (!strcasecmp(v->name, "mohinterpret")
16333 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
16334 ast_copy_string(peer->mohinterpret, v->value, sizeof(peer->mohinterpret));
16335 } else if (!strcasecmp(v->name, "mohsuggest")) {
16336 ast_copy_string(peer->mohsuggest, v->value, sizeof(peer->mohsuggest));
16337 } else if (!strcasecmp(v->name, "mailbox")) {
16338 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
16339 } else if (!strcasecmp(v->name, "subscribemwi")) {
16340 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
16341 } else if (!strcasecmp(v->name, "vmexten")) {
16342 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
16343 } else if (!strcasecmp(v->name, "callgroup")) {
16344 peer->callgroup = ast_get_group(v->value);
16345 } else if (!strcasecmp(v->name, "allowtransfer")) {
16346 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
16347 } else if (!strcasecmp(v->name, "pickupgroup")) {
16348 peer->pickupgroup = ast_get_group(v->value);
16349 } else if (!strcasecmp(v->name, "allow")) {
16350 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
16351 } else if (!strcasecmp(v->name, "disallow")) {
16352 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
16353 } else if (!strcasecmp(v->name, "autoframing")) {
16354 peer->autoframing = ast_true(v->value);
16355 } else if (!strcasecmp(v->name, "rtptimeout")) {
16356 if ((sscanf(v->value, "%d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
16357 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16358 peer->rtptimeout = global_rtptimeout;
16359 }
16360 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
16361 if ((sscanf(v->value, "%d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
16362 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16363 peer->rtpholdtimeout = global_rtpholdtimeout;
16364 }
16365 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
16366 if ((sscanf(v->value, "%d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
16367 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
16368 peer->rtpkeepalive = global_rtpkeepalive;
16369 }
16370 } else if (!strcasecmp(v->name, "setvar")) {
16371
16372 varname = ast_strdupa(v->value);
16373 if ((varval = strchr(varname, '='))) {
16374 *varval++ = '\0';
16375 if ((tmpvar = ast_variable_new(varname, varval))) {
16376 tmpvar->next = peer->chanvars;
16377 peer->chanvars = tmpvar;
16378 }
16379 }
16380 } else if (!strcasecmp(v->name, "qualify")) {
16381 if (!strcasecmp(v->value, "no")) {
16382 peer->maxms = 0;
16383 } else if (!strcasecmp(v->value, "yes")) {
16384 peer->maxms = DEFAULT_MAXMS;
16385 } else if (sscanf(v->value, "%d", &peer->maxms) != 1) {
16386 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);
16387 peer->maxms = 0;
16388 }
16389 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
16390 peer->maxcallbitrate = atoi(v->value);
16391 if (peer->maxcallbitrate < 0)
16392 peer->maxcallbitrate = default_maxcallbitrate;
16393 }
16394 }
16395 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && realtime) {
16396 time_t nowtime = time(NULL);
16397
16398 if ((nowtime - regseconds) > 0) {
16399 destroy_association(peer);
16400 memset(&peer->addr, 0, sizeof(peer->addr));
16401 if (option_debug)
16402 ast_log(LOG_DEBUG, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
16403 }
16404 }
16405 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
16406 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
16407 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
16408 global_allowsubscribe = TRUE;
16409 if (!found && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !ast_test_flag(&peer->flags[0], SIP_REALTIME))
16410 reg_source_db(peer);
16411 ASTOBJ_UNMARK(peer);
16412 ast_free_ha(oldha);
16413 return peer;
16414 }
16415
16416
16417
16418
16419
16420
16421
16422 static int reload_config(enum channelreloadreason reason)
16423 {
16424 struct ast_config *cfg, *ucfg;
16425 struct ast_variable *v;
16426 struct sip_peer *peer;
16427 struct sip_user *user;
16428 struct ast_hostent ahp;
16429 char *cat, *stringp, *context, *oldregcontext;
16430 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
16431 struct hostent *hp;
16432 int format;
16433 struct ast_flags dummy[2];
16434 int auto_sip_domains = FALSE;
16435 struct sockaddr_in old_bindaddr = bindaddr;
16436 int registry_count = 0, peer_count = 0, user_count = 0;
16437 unsigned int temp_tos = 0;
16438 struct ast_flags debugflag = {0};
16439
16440 cfg = ast_config_load(config);
16441
16442
16443 if (!cfg) {
16444 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
16445 return -1;
16446 }
16447
16448
16449 ast_copy_string(oldcontexts, global_regcontext, sizeof(oldcontexts));
16450 oldregcontext = oldcontexts;
16451
16452
16453
16454 ast_copy_flags(&debugflag, &global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
16455 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
16456 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
16457 ast_copy_flags(&global_flags[1], &debugflag, SIP_PAGE2_DEBUG_CONSOLE);
16458
16459
16460 memset(&bindaddr, 0, sizeof(bindaddr));
16461 ast_free_ha(localaddr);
16462 memset(&localaddr, 0, sizeof(localaddr));
16463 memset(&externip, 0, sizeof(externip));
16464 memset(&default_prefs, 0 , sizeof(default_prefs));
16465 outboundproxyip.sin_port = htons(STANDARD_SIP_PORT);
16466 outboundproxyip.sin_family = AF_INET;
16467 ourport = STANDARD_SIP_PORT;
16468 srvlookup = DEFAULT_SRVLOOKUP;
16469 global_tos_sip = DEFAULT_TOS_SIP;
16470 global_tos_audio = DEFAULT_TOS_AUDIO;
16471 global_tos_video = DEFAULT_TOS_VIDEO;
16472 externhost[0] = '\0';
16473 externexpire = 0;
16474 externrefresh = 10;
16475 memset(&outboundproxyip, 0, sizeof(outboundproxyip));
16476
16477
16478 allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
16479 global_regcontext[0] = '\0';
16480 expiry = DEFAULT_EXPIRY;
16481 global_notifyringing = DEFAULT_NOTIFYRINGING;
16482 global_limitonpeers = FALSE;
16483 global_directrtpsetup = FALSE;
16484 global_notifyhold = FALSE;
16485 global_alwaysauthreject = 0;
16486 global_allowsubscribe = FALSE;
16487 ast_copy_string(global_useragent, DEFAULT_USERAGENT, sizeof(global_useragent));
16488 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
16489 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME))
16490 ast_copy_string(global_realm, DEFAULT_REALM, sizeof(global_realm));
16491 else
16492 ast_copy_string(global_realm, ast_config_AST_SYSTEM_NAME, sizeof(global_realm));
16493 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
16494 compactheaders = DEFAULT_COMPACTHEADERS;
16495 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
16496 global_regattempts_max = 0;
16497 pedanticsipchecking = DEFAULT_PEDANTIC;
16498 global_mwitime = DEFAULT_MWITIME;
16499 autocreatepeer = DEFAULT_AUTOCREATEPEER;
16500 global_autoframing = 0;
16501 global_allowguest = DEFAULT_ALLOWGUEST;
16502 global_rtptimeout = 0;
16503 global_rtpholdtimeout = 0;
16504 global_rtpkeepalive = 0;
16505 global_allowtransfer = TRANSFER_OPENFORALL;
16506 global_rtautoclear = 120;
16507 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
16508 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
16509 ast_set_flag(&global_flags[1], SIP_PAGE2_RTUPDATE);
16510
16511
16512 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
16513 default_subscribecontext[0] = '\0';
16514 default_language[0] = '\0';
16515 default_fromdomain[0] = '\0';
16516 default_qualify = DEFAULT_QUALIFY;
16517 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
16518 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
16519 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
16520 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
16521 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
16522 ast_set_flag(&global_flags[0], SIP_NAT_RFC3581);
16523 ast_set_flag(&global_flags[0], SIP_CAN_REINVITE);
16524
16525
16526 dumphistory = FALSE;
16527 recordhistory = FALSE;
16528 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
16529
16530
16531 global_relaxdtmf = FALSE;
16532 global_callevents = FALSE;
16533 global_t1min = DEFAULT_T1MIN;
16534
16535 global_matchexterniplocally = FALSE;
16536
16537
16538 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
16539
16540 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT);
16541
16542
16543 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
16544 if (handle_common_options(&global_flags[0], &dummy[0], v))
16545 continue;
16546
16547 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
16548 continue;
16549
16550
16551 if (!strcasecmp(v->name, "context")) {
16552 ast_copy_string(default_context, v->value, sizeof(default_context));
16553 } else if (!strcasecmp(v->name, "allowguest")) {
16554 global_allowguest = ast_true(v->value) ? 1 : 0;
16555 } else if (!strcasecmp(v->name, "realm")) {
16556 ast_copy_string(global_realm, v->value, sizeof(global_realm));
16557 } else if (!strcasecmp(v->name, "useragent")) {
16558 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
16559 if (option_debug)
16560 ast_log(LOG_DEBUG, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
16561 } else if (!strcasecmp(v->name, "allowtransfer")) {
16562 global_allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
16563 } else if (!strcasecmp(v->name, "rtcachefriends")) {
16564 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
16565 } else if (!strcasecmp(v->name, "rtsavesysname")) {
16566 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTSAVE_SYSNAME);
16567 } else if (!strcasecmp(v->name, "rtupdate")) {
16568 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTUPDATE);
16569 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
16570 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_IGNOREREGEXPIRE);
16571 } else if (!strcasecmp(v->name, "t1min")) {
16572 global_t1min = atoi(v->value);
16573 } else if (!strcasecmp(v->name, "rtautoclear")) {
16574 int i = atoi(v->value);
16575 if (i > 0)
16576 global_rtautoclear = i;
16577 else
16578 i = 0;
16579 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
16580 } else if (!strcasecmp(v->name, "usereqphone")) {
16581 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
16582 } else if (!strcasecmp(v->name, "relaxdtmf")) {
16583 global_relaxdtmf = ast_true(v->value);
16584 } else if (!strcasecmp(v->name, "checkmwi")) {
16585 if ((sscanf(v->value, "%d", &global_mwitime) != 1) || (global_mwitime < 0)) {
16586 ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d. Using default (10).\n", v->value, v->lineno);
16587 global_mwitime = DEFAULT_MWITIME;
16588 }
16589 } else if (!strcasecmp(v->name, "vmexten")) {
16590 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
16591 } else if (!strcasecmp(v->name, "rtptimeout")) {
16592 if ((sscanf(v->value, "%d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
16593 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16594 global_rtptimeout = 0;
16595 }
16596 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
16597 if ((sscanf(v->value, "%d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
16598 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16599 global_rtpholdtimeout = 0;
16600 }
16601 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
16602 if ((sscanf(v->value, "%d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
16603 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
16604 global_rtpkeepalive = 0;
16605 }
16606 } else if (!strcasecmp(v->name, "compactheaders")) {
16607 compactheaders = ast_true(v->value);
16608 } else if (!strcasecmp(v->name, "notifymimetype")) {
16609 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
16610 } else if (!strncasecmp(v->name, "limitonpeer", 11)) {
16611 global_limitonpeers = ast_true(v->value);
16612 } else if (!strcasecmp(v->name, "directrtpsetup")) {
16613 global_directrtpsetup = ast_true(v->value);
16614 } else if (!strcasecmp(v->name, "notifyringing")) {
16615 global_notifyringing = ast_true(v->value);
16616 } else if (!strcasecmp(v->name, "notifyhold")) {
16617 global_notifyhold = ast_true(v->value);
16618 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
16619 global_alwaysauthreject = ast_true(v->value);
16620 } else if (!strcasecmp(v->name, "mohinterpret")
16621 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
16622 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
16623 } else if (!strcasecmp(v->name, "mohsuggest")) {
16624 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
16625 } else if (!strcasecmp(v->name, "language")) {
16626 ast_copy_string(default_language, v->value, sizeof(default_language));
16627 } else if (!strcasecmp(v->name, "regcontext")) {
16628 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
16629 stringp = newcontexts;
16630
16631 cleanup_stale_contexts(stringp, oldregcontext);
16632
16633 while ((context = strsep(&stringp, "&"))) {
16634 if (!ast_context_find(context))
16635 ast_context_create(NULL, context,"SIP");
16636 }
16637 ast_copy_string(global_regcontext, v->value, sizeof(global_regcontext));
16638 } else if (!strcasecmp(v->name, "callerid")) {
16639 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
16640 } else if (!strcasecmp(v->name, "fromdomain")) {
16641 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
16642 } else if (!strcasecmp(v->name, "outboundproxy")) {
16643 if (ast_get_ip_or_srv(&outboundproxyip, v->value, srvlookup ? "_sip._udp" : NULL) < 0)
16644 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", v->value);
16645 } else if (!strcasecmp(v->name, "outboundproxyport")) {
16646
16647 sscanf(v->value, "%d", &format);
16648 outboundproxyip.sin_port = htons(format);
16649 } else if (!strcasecmp(v->name, "autocreatepeer")) {
16650 autocreatepeer = ast_true(v->value);
16651 } else if (!strcasecmp(v->name, "srvlookup")) {
16652 srvlookup = ast_true(v->value);
16653 } else if (!strcasecmp(v->name, "pedantic")) {
16654 pedanticsipchecking = ast_true(v->value);
16655 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
16656 max_expiry = atoi(v->value);
16657 if (max_expiry < 1)
16658 max_expiry = DEFAULT_MAX_EXPIRY;
16659 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
16660 min_expiry = atoi(v->value);
16661 if (min_expiry < 1)
16662 min_expiry = DEFAULT_MIN_EXPIRY;
16663 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
16664 default_expiry = atoi(v->value);
16665 if (default_expiry < 1)
16666 default_expiry = DEFAULT_DEFAULT_EXPIRY;
16667 } else if (!strcasecmp(v->name, "sipdebug")) {
16668 if (ast_true(v->value))
16669 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
16670 } else if (!strcasecmp(v->name, "dumphistory")) {
16671 dumphistory = ast_true(v->value);
16672 } else if (!strcasecmp(v->name, "recordhistory")) {
16673 recordhistory = ast_true(v->value);
16674 } else if (!strcasecmp(v->name, "registertimeout")) {
16675 global_reg_timeout = atoi(v->value);
16676 if (global_reg_timeout < 1)
16677 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
16678 } else if (!strcasecmp(v->name, "registerattempts")) {
16679 global_regattempts_max = atoi(v->value);
16680 } else if (!strcasecmp(v->name, "bindaddr")) {
16681 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
16682 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
16683 } else {
16684 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
16685 }
16686 } else if (!strcasecmp(v->name, "localnet")) {
16687 struct ast_ha *na;
16688 if (!(na = ast_append_ha("d", v->value, localaddr)))
16689 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
16690 else
16691 localaddr = na;
16692 } else if (!strcasecmp(v->name, "localmask")) {
16693 ast_log(LOG_WARNING, "Use of localmask is no long supported -- use localnet with mask syntax\n");
16694 } else if (!strcasecmp(v->name, "externip")) {
16695 if (!(hp = ast_gethostbyname(v->value, &ahp)))
16696 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
16697 else
16698 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
16699 externexpire = 0;
16700 } else if (!strcasecmp(v->name, "externhost")) {
16701 ast_copy_string(externhost, v->value, sizeof(externhost));
16702 if (!(hp = ast_gethostbyname(externhost, &ahp)))
16703 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
16704 else
16705 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
16706 externexpire = time(NULL);
16707 } else if (!strcasecmp(v->name, "externrefresh")) {
16708 if (sscanf(v->value, "%d", &externrefresh) != 1) {
16709 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
16710 externrefresh = 10;
16711 }
16712 } else if (!strcasecmp(v->name, "allow")) {
16713 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 1);
16714 } else if (!strcasecmp(v->name, "disallow")) {
16715 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 0);
16716 } else if (!strcasecmp(v->name, "autoframing")) {
16717 global_autoframing = ast_true(v->value);
16718 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
16719 allow_external_domains = ast_true(v->value);
16720 } else if (!strcasecmp(v->name, "autodomain")) {
16721 auto_sip_domains = ast_true(v->value);
16722 } else if (!strcasecmp(v->name, "domain")) {
16723 char *domain = ast_strdupa(v->value);
16724 char *context = strchr(domain, ',');
16725
16726 if (context)
16727 *context++ = '\0';
16728
16729 if (option_debug && ast_strlen_zero(context))
16730 ast_log(LOG_DEBUG, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
16731 if (ast_strlen_zero(domain))
16732 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
16733 else
16734 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, context ? ast_strip(context) : "");
16735 } else if (!strcasecmp(v->name, "register")) {
16736 if (sip_register(v->value, v->lineno) == 0)
16737 registry_count++;
16738 } else if (!strcasecmp(v->name, "tos")) {
16739 if (!ast_str2tos(v->value, &temp_tos)) {
16740 global_tos_sip = temp_tos;
16741 global_tos_audio = temp_tos;
16742 global_tos_video = temp_tos;
16743 ast_log(LOG_WARNING, "tos value at line %d is deprecated. See doc/ip-tos.txt for more information.\n", v->lineno);
16744 } else
16745 ast_log(LOG_WARNING, "Invalid tos value at line %d, See doc/ip-tos.txt for more information.\n", v->lineno);
16746 } else if (!strcasecmp(v->name, "tos_sip")) {
16747 if (ast_str2tos(v->value, &global_tos_sip))
16748 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, recommended value is 'cs3'. See doc/ip-tos.txt.\n", v->lineno);
16749 } else if (!strcasecmp(v->name, "tos_audio")) {
16750 if (ast_str2tos(v->value, &global_tos_audio))
16751 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, recommended value is 'ef'. See doc/ip-tos.txt.\n", v->lineno);
16752 } else if (!strcasecmp(v->name, "tos_video")) {
16753 if (ast_str2tos(v->value, &global_tos_video))
16754 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, recommended value is 'af41'. See doc/ip-tos.txt.\n", v->lineno);
16755 } else if (!strcasecmp(v->name, "bindport")) {
16756 if (sscanf(v->value, "%d", &ourport) == 1) {
16757 bindaddr.sin_port = htons(ourport);
16758 } else {
16759 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
16760 }
16761 } else if (!strcasecmp(v->name, "qualify")) {
16762 if (!strcasecmp(v->value, "no")) {
16763 default_qualify = 0;
16764 } else if (!strcasecmp(v->value, "yes")) {
16765 default_qualify = DEFAULT_MAXMS;
16766 } else if (sscanf(v->value, "%d", &default_qualify) != 1) {
16767 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
16768 default_qualify = 0;
16769 }
16770 } else if (!strcasecmp(v->name, "callevents")) {
16771 global_callevents = ast_true(v->value);
16772 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
16773 default_maxcallbitrate = atoi(v->value);
16774 if (default_maxcallbitrate < 0)
16775 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
16776 } else if (!strcasecmp(v->name, "matchexterniplocally")) {
16777 global_matchexterniplocally = ast_true(v->value);
16778 }
16779 }
16780
16781 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
16782 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
16783 allow_external_domains = 1;
16784 }
16785
16786
16787 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
16788
16789 if (!strcasecmp(v->name, "auth"))
16790 authl = add_realm_authentication(authl, v->value, v->lineno);
16791 }
16792
16793 ucfg = ast_config_load("users.conf");
16794 if (ucfg) {
16795 struct ast_variable *gen;
16796 int genhassip, genregistersip;
16797 const char *hassip, *registersip;
16798
16799 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
16800 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
16801 gen = ast_variable_browse(ucfg, "general");
16802 cat = ast_category_browse(ucfg, NULL);
16803 while (cat) {
16804 if (strcasecmp(cat, "general")) {
16805 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
16806 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
16807 if (ast_true(hassip) || (!hassip && genhassip)) {
16808 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
16809 if (peer) {
16810 ast_device_state_changed("SIP/%s", peer->name);
16811 ASTOBJ_CONTAINER_LINK(&peerl,peer);
16812 ASTOBJ_UNREF(peer, sip_destroy_peer);
16813 peer_count++;
16814 }
16815 }
16816 if (ast_true(registersip) || (!registersip && genregistersip)) {
16817 char tmp[256];
16818 const char *host = ast_variable_retrieve(ucfg, cat, "host");
16819 const char *username = ast_variable_retrieve(ucfg, cat, "username");
16820 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
16821 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
16822 if (!host)
16823 host = ast_variable_retrieve(ucfg, "general", "host");
16824 if (!username)
16825 username = ast_variable_retrieve(ucfg, "general", "username");
16826 if (!secret)
16827 secret = ast_variable_retrieve(ucfg, "general", "secret");
16828 if (!contact)
16829 contact = "s";
16830 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
16831 if (!ast_strlen_zero(secret))
16832 snprintf(tmp, sizeof(tmp), "%s:%s@%s/%s", username, secret, host, contact);
16833 else
16834 snprintf(tmp, sizeof(tmp), "%s@%s/%s", username, host, contact);
16835 if (sip_register(tmp, 0) == 0)
16836 registry_count++;
16837 }
16838 }
16839 }
16840 cat = ast_category_browse(ucfg, cat);
16841 }
16842 ast_config_destroy(ucfg);
16843 }
16844
16845
16846
16847 cat = NULL;
16848 while ( (cat = ast_category_browse(cfg, cat)) ) {
16849 const char *utype;
16850 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
16851 continue;
16852 utype = ast_variable_retrieve(cfg, cat, "type");
16853 if (!utype) {
16854 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
16855 continue;
16856 } else {
16857 int is_user = 0, is_peer = 0;
16858 if (!strcasecmp(utype, "user"))
16859 is_user = 1;
16860 else if (!strcasecmp(utype, "friend"))
16861 is_user = is_peer = 1;
16862 else if (!strcasecmp(utype, "peer"))
16863 is_peer = 1;
16864 else {
16865 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
16866 continue;
16867 }
16868 if (is_user) {
16869 user = build_user(cat, ast_variable_browse(cfg, cat), 0);
16870 if (user) {
16871 ASTOBJ_CONTAINER_LINK(&userl,user);
16872 ASTOBJ_UNREF(user, sip_destroy_user);
16873 user_count++;
16874 }
16875 }
16876 if (is_peer) {
16877 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
16878 if (peer) {
16879 ASTOBJ_CONTAINER_LINK(&peerl,peer);
16880 ASTOBJ_UNREF(peer, sip_destroy_peer);
16881 peer_count++;
16882 }
16883 }
16884 }
16885 }
16886 if (ast_find_ourip(&__ourip, bindaddr)) {
16887 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
16888 return 0;
16889 }
16890 if (!ntohs(bindaddr.sin_port))
16891 bindaddr.sin_port = ntohs(STANDARD_SIP_PORT);
16892 bindaddr.sin_family = AF_INET;
16893 ast_mutex_lock(&netlock);
16894 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
16895 close(sipsock);
16896 sipsock = -1;
16897 }
16898 if (sipsock < 0) {
16899 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
16900 if (sipsock < 0) {
16901 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
16902 return -1;
16903 } else {
16904
16905 const int reuseFlag = 1;
16906
16907 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
16908 (const char*)&reuseFlag,
16909 sizeof reuseFlag);
16910
16911 ast_enable_packet_fragmentation(sipsock);
16912
16913 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
16914 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
16915 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
16916 strerror(errno));
16917 close(sipsock);
16918 sipsock = -1;
16919 } else {
16920 if (option_verbose > 1) {
16921 ast_verbose(VERBOSE_PREFIX_2 "SIP Listening on %s:%d\n",
16922 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
16923 ast_verbose(VERBOSE_PREFIX_2 "Using SIP TOS: %s\n", ast_tos2str(global_tos_sip));
16924 }
16925 if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &global_tos_sip, sizeof(global_tos_sip)))
16926 ast_log(LOG_WARNING, "Unable to set SIP TOS to %s\n", ast_tos2str(global_tos_sip));
16927 }
16928 }
16929 }
16930 ast_mutex_unlock(&netlock);
16931
16932
16933
16934
16935
16936 if (auto_sip_domains) {
16937 char temp[MAXHOSTNAMELEN];
16938
16939
16940 if (bindaddr.sin_addr.s_addr)
16941 add_sip_domain(ast_inet_ntoa(bindaddr.sin_addr), SIP_DOMAIN_AUTO, NULL);
16942 else
16943 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
16944
16945
16946 if (externip.sin_addr.s_addr)
16947 add_sip_domain(ast_inet_ntoa(externip.sin_addr), SIP_DOMAIN_AUTO, NULL);
16948
16949
16950 if (!ast_strlen_zero(externhost))
16951 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
16952
16953
16954 if (!gethostname(temp, sizeof(temp)))
16955 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
16956 }
16957
16958
16959 ast_config_destroy(cfg);
16960
16961
16962 if (notify_types)
16963 ast_config_destroy(notify_types);
16964 notify_types = ast_config_load(notify_config);
16965
16966
16967 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\r\n", channelreloadreason2txt(reason), registry_count, peer_count, user_count);
16968
16969 return 0;
16970 }
16971
16972 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
16973 {
16974 struct sip_pvt *p;
16975 struct ast_udptl *udptl = NULL;
16976
16977 p = chan->tech_pvt;
16978 if (!p)
16979 return NULL;
16980
16981 ast_mutex_lock(&p->lock);
16982 if (p->udptl && ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
16983 udptl = p->udptl;
16984 ast_mutex_unlock(&p->lock);
16985 return udptl;
16986 }
16987
16988 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
16989 {
16990 struct sip_pvt *p;
16991
16992 p = chan->tech_pvt;
16993 if (!p)
16994 return -1;
16995 ast_mutex_lock(&p->lock);
16996 if (udptl)
16997 ast_udptl_get_peer(udptl, &p->udptlredirip);
16998 else
16999 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
17000 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
17001 if (!p->pendinginvite) {
17002 if (option_debug > 2) {
17003 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);
17004 }
17005 transmit_reinvite_with_t38_sdp(p);
17006 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
17007 if (option_debug > 2) {
17008 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);
17009 }
17010 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
17011 }
17012 }
17013
17014 p->lastrtprx = p->lastrtptx = time(NULL);
17015 ast_mutex_unlock(&p->lock);
17016 return 0;
17017 }
17018
17019
17020
17021
17022
17023
17024 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite)
17025 {
17026 struct sip_pvt *p;
17027 int flag = 0;
17028
17029 p = chan->tech_pvt;
17030 if (!p || !pvt->udptl)
17031 return -1;
17032
17033
17034 ast_mutex_lock(&p->lock);
17035
17036
17037
17038 p->t38.jointcapability = p->t38.peercapability = pvt->t38.jointcapability;
17039
17040 ast_udptl_set_far_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
17041 ast_udptl_set_local_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
17042 ast_udptl_set_error_correction_scheme(p->udptl, ast_udptl_get_error_correction_scheme(pvt->udptl));
17043
17044 if (reinvite) {
17045
17046
17047
17048
17049
17050 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
17051 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
17052 flag =1;
17053 } else {
17054 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
17055 }
17056 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
17057 if (!p->pendinginvite) {
17058 if (option_debug > 2) {
17059 if (flag)
17060 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));
17061 else
17062 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));
17063 }
17064 transmit_reinvite_with_t38_sdp(p);
17065 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
17066 if (option_debug > 2) {
17067 if (flag)
17068 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));
17069 else
17070 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));
17071 }
17072 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
17073 }
17074 }
17075
17076 p->lastrtprx = p->lastrtptx = time(NULL);
17077 ast_mutex_unlock(&p->lock);
17078 return 0;
17079 } else {
17080 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
17081 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
17082 flag = 1;
17083 } else {
17084 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
17085 }
17086 if (option_debug > 2) {
17087 if (flag)
17088 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));
17089 else
17090 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));
17091 }
17092 pvt->t38.state = T38_ENABLED;
17093 p->t38.state = T38_ENABLED;
17094 if (option_debug > 1) {
17095 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", pvt->t38.state, pvt->owner ? pvt->owner->name : "<none>");
17096 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
17097 }
17098 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
17099 p->lastrtprx = p->lastrtptx = time(NULL);
17100 ast_mutex_unlock(&p->lock);
17101 return 0;
17102 }
17103 }
17104
17105
17106
17107 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
17108 {
17109 struct sip_pvt *p = NULL;
17110 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
17111
17112 if (!(p = chan->tech_pvt))
17113 return AST_RTP_GET_FAILED;
17114
17115 ast_mutex_lock(&p->lock);
17116 if (!(p->rtp)) {
17117 ast_mutex_unlock(&p->lock);
17118 return AST_RTP_GET_FAILED;
17119 }
17120
17121 *rtp = p->rtp;
17122
17123 if (ast_rtp_getnat(*rtp) && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT))
17124 res = AST_RTP_TRY_PARTIAL;
17125 else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
17126 res = AST_RTP_TRY_NATIVE;
17127 else if (ast_test_flag(&global_jbconf, AST_JB_FORCED))
17128 res = AST_RTP_GET_FAILED;
17129
17130 ast_mutex_unlock(&p->lock);
17131
17132 return res;
17133 }
17134
17135
17136 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
17137 {
17138 struct sip_pvt *p = NULL;
17139 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
17140
17141 if (!(p = chan->tech_pvt))
17142 return AST_RTP_GET_FAILED;
17143
17144 ast_mutex_lock(&p->lock);
17145 if (!(p->vrtp)) {
17146 ast_mutex_unlock(&p->lock);
17147 return AST_RTP_GET_FAILED;
17148 }
17149
17150 *rtp = p->vrtp;
17151
17152 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
17153 res = AST_RTP_TRY_NATIVE;
17154
17155 ast_mutex_unlock(&p->lock);
17156
17157 return res;
17158 }
17159
17160
17161 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
17162 {
17163 struct sip_pvt *p;
17164 int changed = 0;
17165
17166 p = chan->tech_pvt;
17167 if (!p)
17168 return -1;
17169
17170
17171 if (chan->_state != AST_STATE_UP && !global_directrtpsetup)
17172 return 0;
17173
17174 ast_mutex_lock(&p->lock);
17175 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
17176
17177 ast_mutex_unlock(&p->lock);
17178 return 0;
17179 }
17180
17181
17182
17183
17184 if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
17185 ast_mutex_unlock(&p->lock);
17186 return 0;
17187 }
17188
17189 if (rtp) {
17190 changed |= ast_rtp_get_peer(rtp, &p->redirip);
17191 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
17192 memset(&p->redirip, 0, sizeof(p->redirip));
17193 changed = 1;
17194 }
17195 if (vrtp) {
17196 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
17197 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
17198 memset(&p->vredirip, 0, sizeof(p->vredirip));
17199 changed = 1;
17200 }
17201 if (codecs) {
17202 if ((p->redircodecs != codecs)) {
17203 p->redircodecs = codecs;
17204 changed = 1;
17205 }
17206 if ((p->capability & codecs) != p->capability) {
17207 p->jointcapability &= codecs;
17208 p->capability &= codecs;
17209 changed = 1;
17210 }
17211 }
17212 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
17213 if (chan->_state != AST_STATE_UP) {
17214 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
17215 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
17216 if (option_debug)
17217 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));
17218 } else if (!p->pendinginvite) {
17219 if (option_debug > 2) {
17220 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));
17221 }
17222 transmit_reinvite_with_sdp(p);
17223 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
17224 if (option_debug > 2) {
17225 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));
17226 }
17227
17228 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
17229 }
17230 }
17231
17232 p->lastrtprx = p->lastrtptx = time(NULL);
17233 ast_mutex_unlock(&p->lock);
17234 return 0;
17235 }
17236
17237 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
17238 static char *descrip_dtmfmode = "SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
17239 static char *app_dtmfmode = "SIPDtmfMode";
17240
17241 static char *app_sipaddheader = "SIPAddHeader";
17242 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
17243
17244 static char *descrip_sipaddheader = ""
17245 " SIPAddHeader(Header: Content)\n"
17246 "Adds a header to a SIP call placed with DIAL.\n"
17247 "Remember to user the X-header if you are adding non-standard SIP\n"
17248 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
17249 "Adding the wrong headers may jeopardize the SIP dialog.\n"
17250 "Always returns 0\n";
17251
17252
17253
17254 static int sip_dtmfmode(struct ast_channel *chan, void *data)
17255 {
17256 struct sip_pvt *p;
17257 char *mode;
17258 if (data)
17259 mode = (char *)data;
17260 else {
17261 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
17262 return 0;
17263 }
17264 ast_channel_lock(chan);
17265 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
17266 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
17267 ast_channel_unlock(chan);
17268 return 0;
17269 }
17270 p = chan->tech_pvt;
17271 if (!p) {
17272 ast_channel_unlock(chan);
17273 return 0;
17274 }
17275 ast_mutex_lock(&p->lock);
17276 if (!strcasecmp(mode,"info")) {
17277 ast_clear_flag(&p->flags[0], SIP_DTMF);
17278 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
17279 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
17280 } else if (!strcasecmp(mode,"rfc2833")) {
17281 ast_clear_flag(&p->flags[0], SIP_DTMF);
17282 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
17283 p->jointnoncodeccapability |= AST_RTP_DTMF;
17284 } else if (!strcasecmp(mode,"inband")) {
17285 ast_clear_flag(&p->flags[0], SIP_DTMF);
17286 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
17287 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
17288 } else
17289 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n",mode);
17290 if (p->rtp)
17291 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
17292 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
17293 if (!p->vad) {
17294 p->vad = ast_dsp_new();
17295 ast_dsp_set_features(p->vad, DSP_FEATURE_DTMF_DETECT);
17296 }
17297 } else {
17298 if (p->vad) {
17299 ast_dsp_free(p->vad);
17300 p->vad = NULL;
17301 }
17302 }
17303 ast_mutex_unlock(&p->lock);
17304 ast_channel_unlock(chan);
17305 return 0;
17306 }
17307
17308
17309 static int sip_addheader(struct ast_channel *chan, void *data)
17310 {
17311 int no = 0;
17312 int ok = FALSE;
17313 char varbuf[30];
17314 char *inbuf = (char *) data;
17315
17316 if (ast_strlen_zero(inbuf)) {
17317 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
17318 return 0;
17319 }
17320 ast_channel_lock(chan);
17321
17322
17323 while (!ok && no <= 50) {
17324 no++;
17325 snprintf(varbuf, sizeof(varbuf), "_SIPADDHEADER%.2d", no);
17326
17327
17328 if( (pbx_builtin_getvar_helper(chan, (const char *) varbuf + 1) == (const char *) NULL) )
17329 ok = TRUE;
17330 }
17331 if (ok) {
17332 pbx_builtin_setvar_helper (chan, varbuf, inbuf);
17333 if (sipdebug)
17334 ast_log(LOG_DEBUG,"SIP Header added \"%s\" as %s\n", inbuf, varbuf);
17335 } else {
17336 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
17337 }
17338 ast_channel_unlock(chan);
17339 return 0;
17340 }
17341
17342
17343
17344
17345
17346
17347
17348 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
17349 {
17350 char *cdest;
17351 char *extension, *host, *port;
17352 char tmp[80];
17353
17354 cdest = ast_strdupa(dest);
17355
17356 extension = strsep(&cdest, "@");
17357 host = strsep(&cdest, ":");
17358 port = strsep(&cdest, ":");
17359 if (ast_strlen_zero(extension)) {
17360 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
17361 return 0;
17362 }
17363
17364
17365 if (!host) {
17366 char *localtmp;
17367 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
17368 if (ast_strlen_zero(tmp)) {
17369 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
17370 return 0;
17371 }
17372 if ((localtmp = strcasestr(tmp, "sip:")) && (localtmp = strchr(localtmp, '@'))) {
17373 char lhost[80], lport[80];
17374 memset(lhost, 0, sizeof(lhost));
17375 memset(lport, 0, sizeof(lport));
17376 localtmp++;
17377
17378 sscanf(localtmp, "%[^<>:; ]:%[^<>:; ]", lhost, lport);
17379 if (ast_strlen_zero(lhost)) {
17380 ast_log(LOG_ERROR, "Can't find the host address\n");
17381 return 0;
17382 }
17383 host = ast_strdupa(lhost);
17384 if (!ast_strlen_zero(lport)) {
17385 port = ast_strdupa(lport);
17386 }
17387 }
17388 }
17389
17390 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
17391 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
17392
17393 sip_scheddestroy(p, 32000);
17394 sip_alreadygone(p);
17395 return 0;
17396 }
17397
17398
17399 static int sip_get_codec(struct ast_channel *chan)
17400 {
17401 struct sip_pvt *p = chan->tech_pvt;
17402 return p->peercapability ? p->peercapability : p->capability;
17403 }
17404
17405
17406
17407
17408
17409 static void sip_poke_all_peers(void)
17410 {
17411 int ms = 0;
17412
17413 if (!speerobjs)
17414 return;
17415
17416 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
17417 ASTOBJ_WRLOCK(iterator);
17418 if (iterator->pokeexpire > -1)
17419 ast_sched_del(sched, iterator->pokeexpire);
17420 ms += 100;
17421 iterator->pokeexpire = ast_sched_add(sched, ms, sip_poke_peer_s, iterator);
17422 ASTOBJ_UNLOCK(iterator);
17423 } while (0)
17424 );
17425 }
17426
17427
17428 static void sip_send_all_registers(void)
17429 {
17430 int ms;
17431 int regspacing;
17432 if (!regobjs)
17433 return;
17434 regspacing = default_expiry * 1000/regobjs;
17435 if (regspacing > 100)
17436 regspacing = 100;
17437 ms = regspacing;
17438 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
17439 ASTOBJ_WRLOCK(iterator);
17440 if (iterator->expire > -1)
17441 ast_sched_del(sched, iterator->expire);
17442 ms += regspacing;
17443 iterator->expire = ast_sched_add(sched, ms, sip_reregister, iterator);
17444 ASTOBJ_UNLOCK(iterator);
17445 } while (0)
17446 );
17447 }
17448
17449
17450 static int sip_do_reload(enum channelreloadreason reason)
17451 {
17452 if (option_debug > 3)
17453 ast_log(LOG_DEBUG, "--------------- SIP reload started\n");
17454
17455 clear_realm_authentication(authl);
17456 clear_sip_domains();
17457 authl = NULL;
17458
17459
17460
17461 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
17462 ASTOBJ_RDLOCK(iterator);
17463 if (iterator->call) {
17464 if (option_debug > 2)
17465 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
17466
17467 sip_destroy(iterator->call);
17468 }
17469 ASTOBJ_UNLOCK(iterator);
17470
17471 } while(0));
17472
17473
17474 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
17475 if (option_debug > 3)
17476 ast_log(LOG_DEBUG, "--------------- Done destroying user list\n");
17477 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
17478 if (option_debug > 3)
17479 ast_log(LOG_DEBUG, "--------------- Done destroying registry list\n");
17480 ASTOBJ_CONTAINER_MARKALL(&peerl);
17481 reload_config(reason);
17482
17483
17484 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
17485 if (option_debug > 3)
17486 ast_log(LOG_DEBUG, "--------------- Done destroying pruned peers\n");
17487
17488
17489 sip_poke_all_peers();
17490
17491
17492 sip_send_all_registers();
17493
17494 if (option_debug > 3)
17495 ast_log(LOG_DEBUG, "--------------- SIP reload done\n");
17496
17497 return 0;
17498 }
17499
17500
17501 static int sip_reload(int fd, int argc, char *argv[])
17502 {
17503 ast_mutex_lock(&sip_reload_lock);
17504 if (sip_reloading)
17505 ast_verbose("Previous SIP reload not yet done\n");
17506 else {
17507 sip_reloading = TRUE;
17508 if (fd)
17509 sip_reloadreason = CHANNEL_CLI_RELOAD;
17510 else
17511 sip_reloadreason = CHANNEL_MODULE_RELOAD;
17512 }
17513 ast_mutex_unlock(&sip_reload_lock);
17514 restart_monitor();
17515
17516 return 0;
17517 }
17518
17519
17520 static int reload(void)
17521 {
17522 return sip_reload(0, 0, NULL);
17523 }
17524
17525 static struct ast_cli_entry cli_sip_debug_deprecated =
17526 { { "sip", "debug", NULL },
17527 sip_do_debug_deprecated, "Enable SIP debugging",
17528 debug_usage };
17529
17530 static struct ast_cli_entry cli_sip_no_debug_deprecated =
17531 { { "sip", "no", "debug", NULL },
17532 sip_no_debug_deprecated, "Disable SIP debugging",
17533 debug_usage };
17534
17535 static struct ast_cli_entry cli_sip[] = {
17536 { { "sip", "show", "channels", NULL },
17537 sip_show_channels, "List active SIP channels",
17538 show_channels_usage },
17539
17540 { { "sip", "show", "domains", NULL },
17541 sip_show_domains, "List our local SIP domains.",
17542 show_domains_usage },
17543
17544 { { "sip", "show", "inuse", NULL },
17545 sip_show_inuse, "List all inuse/limits",
17546 show_inuse_usage },
17547
17548 { { "sip", "show", "objects", NULL },
17549 sip_show_objects, "List all SIP object allocations",
17550 show_objects_usage },
17551
17552 { { "sip", "show", "peers", NULL },
17553 sip_show_peers, "List defined SIP peers",
17554 show_peers_usage },
17555
17556 { { "sip", "show", "registry", NULL },
17557 sip_show_registry, "List SIP registration status",
17558 show_reg_usage },
17559
17560 { { "sip", "show", "settings", NULL },
17561 sip_show_settings, "Show SIP global settings",
17562 show_settings_usage },
17563
17564 { { "sip", "show", "subscriptions", NULL },
17565 sip_show_subscriptions, "List active SIP subscriptions",
17566 show_subscriptions_usage },
17567
17568 { { "sip", "show", "users", NULL },
17569 sip_show_users, "List defined SIP users",
17570 show_users_usage },
17571
17572 { { "sip", "notify", NULL },
17573 sip_notify, "Send a notify packet to a SIP peer",
17574 notify_usage, complete_sipnotify },
17575
17576 { { "sip", "show", "channel", NULL },
17577 sip_show_channel, "Show detailed SIP channel info",
17578 show_channel_usage, complete_sipch },
17579
17580 { { "sip", "show", "history", NULL },
17581 sip_show_history, "Show SIP dialog history",
17582 show_history_usage, complete_sipch },
17583
17584 { { "sip", "show", "peer", NULL },
17585 sip_show_peer, "Show details on specific SIP peer",
17586 show_peer_usage, complete_sip_show_peer },
17587
17588 { { "sip", "show", "user", NULL },
17589 sip_show_user, "Show details on specific SIP user",
17590 show_user_usage, complete_sip_show_user },
17591
17592 { { "sip", "prune", "realtime", NULL },
17593 sip_prune_realtime, "Prune cached Realtime object(s)",
17594 prune_realtime_usage },
17595
17596 { { "sip", "prune", "realtime", "peer", NULL },
17597 sip_prune_realtime, "Prune cached Realtime peer(s)",
17598 prune_realtime_usage, complete_sip_prune_realtime_peer },
17599
17600 { { "sip", "prune", "realtime", "user", NULL },
17601 sip_prune_realtime, "Prune cached Realtime user(s)",
17602 prune_realtime_usage, complete_sip_prune_realtime_user },
17603
17604 { { "sip", "set", "debug", NULL },
17605 sip_do_debug, "Enable SIP debugging",
17606 debug_usage, NULL, &cli_sip_debug_deprecated },
17607
17608 { { "sip", "set", "debug", "ip", NULL },
17609 sip_do_debug, "Enable SIP debugging on IP",
17610 debug_usage },
17611
17612 { { "sip", "set", "debug", "peer", NULL },
17613 sip_do_debug, "Enable SIP debugging on Peername",
17614 debug_usage, complete_sip_debug_peer },
17615
17616 { { "sip", "set", "debug", "off", NULL },
17617 sip_no_debug, "Disable SIP debugging",
17618 no_debug_usage, NULL, &cli_sip_no_debug_deprecated },
17619
17620 { { "sip", "history", NULL },
17621 sip_do_history, "Enable SIP history",
17622 history_usage },
17623
17624 { { "sip", "history", "off", NULL },
17625 sip_no_history, "Disable SIP history",
17626 no_history_usage },
17627
17628 { { "sip", "reload", NULL },
17629 sip_reload, "Reload SIP configuration",
17630 sip_reload_usage },
17631 };
17632
17633
17634 static int load_module(void)
17635 {
17636 ASTOBJ_CONTAINER_INIT(&userl);
17637 ASTOBJ_CONTAINER_INIT(&peerl);
17638 ASTOBJ_CONTAINER_INIT(®l);
17639
17640 if (!(sched = sched_context_create())) {
17641 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
17642 return AST_MODULE_LOAD_FAILURE;
17643 }
17644
17645 if (!(io = io_context_create())) {
17646 ast_log(LOG_ERROR, "Unable to create I/O context\n");
17647 sched_context_destroy(sched);
17648 return AST_MODULE_LOAD_FAILURE;
17649 }
17650
17651 sip_reloadreason = CHANNEL_MODULE_LOAD;
17652
17653 if(reload_config(sip_reloadreason))
17654 return AST_MODULE_LOAD_DECLINE;
17655
17656
17657 if (ast_channel_register(&sip_tech)) {
17658 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
17659 io_context_destroy(io);
17660 sched_context_destroy(sched);
17661 return AST_MODULE_LOAD_FAILURE;
17662 }
17663
17664
17665 ast_cli_register_multiple(cli_sip, sizeof(cli_sip)/ sizeof(struct ast_cli_entry));
17666
17667
17668 ast_rtp_proto_register(&sip_rtp);
17669
17670
17671 ast_udptl_proto_register(&sip_udptl);
17672
17673
17674 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
17675 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
17676
17677
17678 ast_custom_function_register(&sip_header_function);
17679 ast_custom_function_register(&sippeer_function);
17680 ast_custom_function_register(&sipchaninfo_function);
17681 ast_custom_function_register(&checksipdomain_function);
17682
17683
17684 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM, manager_sip_show_peers,
17685 "List SIP peers (text format)", mandescr_show_peers);
17686 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM, manager_sip_show_peer,
17687 "Show SIP peer (text format)", mandescr_show_peer);
17688
17689 sip_poke_all_peers();
17690 sip_send_all_registers();
17691
17692
17693 restart_monitor();
17694
17695 return AST_MODULE_LOAD_SUCCESS;
17696 }
17697
17698
17699 static int unload_module(void)
17700 {
17701 struct sip_pvt *p, *pl;
17702
17703
17704 ast_channel_unregister(&sip_tech);
17705
17706
17707 ast_custom_function_unregister(&sipchaninfo_function);
17708 ast_custom_function_unregister(&sippeer_function);
17709 ast_custom_function_unregister(&sip_header_function);
17710 ast_custom_function_unregister(&checksipdomain_function);
17711
17712
17713 ast_unregister_application(app_dtmfmode);
17714 ast_unregister_application(app_sipaddheader);
17715
17716
17717 ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
17718
17719
17720 ast_rtp_proto_unregister(&sip_rtp);
17721
17722
17723 ast_udptl_proto_unregister(&sip_udptl);
17724
17725
17726 ast_manager_unregister("SIPpeers");
17727 ast_manager_unregister("SIPshowpeer");
17728
17729 ast_mutex_lock(&iflock);
17730
17731 for (p = iflist; p ; p = p->next) {
17732 if (p->owner)
17733 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
17734 }
17735 ast_mutex_unlock(&iflock);
17736
17737 ast_mutex_lock(&monlock);
17738 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
17739 pthread_cancel(monitor_thread);
17740 pthread_kill(monitor_thread, SIGURG);
17741 pthread_join(monitor_thread, NULL);
17742 }
17743 monitor_thread = AST_PTHREADT_STOP;
17744 ast_mutex_unlock(&monlock);
17745
17746 ast_mutex_lock(&iflock);
17747
17748 p = iflist;
17749 while (p) {
17750 pl = p;
17751 p = p->next;
17752 __sip_destroy(pl, TRUE);
17753 }
17754 iflist = NULL;
17755 ast_mutex_unlock(&iflock);
17756
17757
17758 ast_free_ha(localaddr);
17759
17760 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
17761 ASTOBJ_CONTAINER_DESTROY(&userl);
17762 ASTOBJ_CONTAINER_DESTROYALL(&peerl, sip_destroy_peer);
17763 ASTOBJ_CONTAINER_DESTROY(&peerl);
17764 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
17765 ASTOBJ_CONTAINER_DESTROY(®l);
17766
17767 clear_realm_authentication(authl);
17768 clear_sip_domains();
17769 close(sipsock);
17770 sched_context_destroy(sched);
17771
17772 return 0;
17773 }
17774
17775 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)",
17776 .load = load_module,
17777 .unload = unload_module,
17778 .reload = reload,
17779 );