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$")
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 VIDEO_CODEC_MASK 0x1fc0000
00158 #ifndef IPTOS_MINCOST
00159 #define IPTOS_MINCOST 0x02
00160 #endif
00161
00162
00163
00164 #define DEFAULT_DEFAULT_EXPIRY 120
00165 #define DEFAULT_MIN_EXPIRY 60
00166 #define DEFAULT_MAX_EXPIRY 3600
00167 #define DEFAULT_REGISTRATION_TIMEOUT 20
00168 #define DEFAULT_MAX_FORWARDS "70"
00169
00170
00171
00172 #define EXPIRY_GUARD_SECS 15
00173 #define EXPIRY_GUARD_LIMIT 30
00174
00175 #define EXPIRY_GUARD_MIN 500
00176
00177
00178
00179 #define EXPIRY_GUARD_PCT 0.20
00180
00181 #define DEFAULT_EXPIRY 900
00182
00183 static int min_expiry = DEFAULT_MIN_EXPIRY;
00184 static int max_expiry = DEFAULT_MAX_EXPIRY;
00185 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00186 static int expiry = DEFAULT_EXPIRY;
00187
00188 #ifndef MAX
00189 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00190 #endif
00191
00192 #define CALLERID_UNKNOWN "Unknown"
00193
00194 #define DEFAULT_MAXMS 2000
00195 #define DEFAULT_FREQ_OK 60 * 1000
00196 #define DEFAULT_FREQ_NOTOK 10 * 1000
00197
00198 #define DEFAULT_RETRANS 1000
00199 #define MAX_RETRANS 6
00200 #define SIP_TRANS_TIMEOUT 32000
00201
00202
00203 #define DEFAULT_TRANS_TIMEOUT -1
00204 #define MAX_AUTHTRIES 3
00205
00206 #define SIP_MAX_HEADERS 64
00207 #define SIP_MAX_LINES 64
00208 #define SIP_MAX_PACKET 4096
00209
00210 #define INITIAL_CSEQ 101
00211
00212
00213 static struct ast_jb_conf default_jbconf =
00214 {
00215 .flags = 0,
00216 .max_size = -1,
00217 .resync_threshold = -1,
00218 .impl = ""
00219 };
00220 static struct ast_jb_conf global_jbconf;
00221
00222 static const char config[] = "sip.conf";
00223 static const char notify_config[] = "sip_notify.conf";
00224
00225 #define RTP 1
00226 #define NO_RTP 0
00227
00228
00229
00230
00231 enum transfermodes {
00232 TRANSFER_OPENFORALL,
00233 TRANSFER_CLOSED,
00234 };
00235
00236
00237 enum sip_result {
00238 AST_SUCCESS = 0,
00239 AST_FAILURE = -1,
00240 };
00241
00242
00243
00244
00245 enum invitestates {
00246 INV_NONE = 0,
00247 INV_CALLING = 1,
00248 INV_PROCEEDING = 2,
00249 INV_EARLY_MEDIA = 3,
00250 INV_COMPLETED = 4,
00251 INV_CONFIRMED = 5,
00252 INV_TERMINATED = 6,
00253
00254 INV_CANCELLED = 7,
00255 };
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 enum xmittype {
00266 XMIT_CRITICAL = 2,
00267
00268 XMIT_RELIABLE = 1,
00269 XMIT_UNRELIABLE = 0,
00270 };
00271
00272 enum parse_register_result {
00273 PARSE_REGISTER_FAILED,
00274 PARSE_REGISTER_UPDATE,
00275 PARSE_REGISTER_QUERY,
00276 };
00277
00278 enum subscriptiontype {
00279 NONE = 0,
00280 XPIDF_XML,
00281 DIALOG_INFO_XML,
00282 CPIM_PIDF_XML,
00283 PIDF_XML,
00284 MWI_NOTIFICATION
00285 };
00286
00287 static const struct cfsubscription_types {
00288 enum subscriptiontype type;
00289 const char * const event;
00290 const char * const mediatype;
00291 const char * const text;
00292 } subscription_types[] = {
00293 { NONE, "-", "unknown", "unknown" },
00294
00295 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00296 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00297 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00298 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00299 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00300 };
00301
00302
00303 enum sipmethod {
00304 SIP_UNKNOWN,
00305 SIP_RESPONSE,
00306 SIP_REGISTER,
00307 SIP_OPTIONS,
00308 SIP_NOTIFY,
00309 SIP_INVITE,
00310 SIP_ACK,
00311 SIP_PRACK,
00312 SIP_BYE,
00313 SIP_REFER,
00314 SIP_SUBSCRIBE,
00315 SIP_MESSAGE,
00316 SIP_UPDATE,
00317 SIP_INFO,
00318 SIP_CANCEL,
00319 SIP_PUBLISH,
00320 SIP_PING,
00321 };
00322
00323
00324
00325
00326
00327
00328 enum sip_auth_type {
00329 PROXY_AUTH,
00330 WWW_AUTH,
00331 };
00332
00333
00334 enum check_auth_result {
00335 AUTH_SUCCESSFUL = 0,
00336 AUTH_CHALLENGE_SENT = 1,
00337 AUTH_SECRET_FAILED = -1,
00338 AUTH_USERNAME_MISMATCH = -2,
00339 AUTH_NOT_FOUND = -3,
00340 AUTH_FAKE_AUTH = -4,
00341 AUTH_UNKNOWN_DOMAIN = -5,
00342 };
00343
00344
00345 enum sipregistrystate {
00346 REG_STATE_UNREGISTERED = 0,
00347 REG_STATE_REGSENT,
00348 REG_STATE_AUTHSENT,
00349 REG_STATE_REGISTERED,
00350 REG_STATE_REJECTED,
00351 REG_STATE_TIMEOUT,
00352 REG_STATE_NOAUTH,
00353 REG_STATE_FAILED,
00354 };
00355
00356 #define CAN_NOT_CREATE_DIALOG 0
00357 #define CAN_CREATE_DIALOG 1
00358 #define CAN_CREATE_DIALOG_UNSUPPORTED_METHOD 2
00359
00360
00361 static const struct cfsip_methods {
00362 enum sipmethod id;
00363 int need_rtp;
00364 char * const text;
00365 int can_create;
00366 } sip_methods[] = {
00367 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00368 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00369 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00370 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00371 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00372 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00373 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00374 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00375 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00376 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00377 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00378 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00379 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00380 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00381 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00382 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00383 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00384 };
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 #define SUPPORTED 1
00397 #define NOT_SUPPORTED 0
00398
00399 #define SIP_OPT_REPLACES (1 << 0)
00400 #define SIP_OPT_100REL (1 << 1)
00401 #define SIP_OPT_TIMER (1 << 2)
00402 #define SIP_OPT_EARLY_SESSION (1 << 3)
00403 #define SIP_OPT_JOIN (1 << 4)
00404 #define SIP_OPT_PATH (1 << 5)
00405 #define SIP_OPT_PREF (1 << 6)
00406 #define SIP_OPT_PRECONDITION (1 << 7)
00407 #define SIP_OPT_PRIVACY (1 << 8)
00408 #define SIP_OPT_SDP_ANAT (1 << 9)
00409 #define SIP_OPT_SEC_AGREE (1 << 10)
00410 #define SIP_OPT_EVENTLIST (1 << 11)
00411 #define SIP_OPT_GRUU (1 << 12)
00412 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00413 #define SIP_OPT_NOREFERSUB (1 << 14)
00414 #define SIP_OPT_HISTINFO (1 << 15)
00415 #define SIP_OPT_RESPRIORITY (1 << 16)
00416
00417
00418
00419 static const struct cfsip_options {
00420 int id;
00421 int supported;
00422 char * const text;
00423 } sip_options[] = {
00424
00425 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00426
00427 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00428
00429 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00430
00431 { SIP_OPT_TIMER, NOT_SUPPORTED, "timer" },
00432
00433 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00434
00435 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00436
00437 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00438
00439 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00440
00441 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00442
00443 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00444
00445 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00446
00447 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00448
00449 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00450
00451 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00452
00453 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00454
00455 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00456
00457 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00458
00459 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00460 };
00461
00462
00463
00464 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
00465
00466
00467 #define SUPPORTED_EXTENSIONS "replaces"
00468
00469
00470 #define STANDARD_SIP_PORT 5060
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 #define DEFAULT_CONTEXT "default"
00484 #define DEFAULT_MOHINTERPRET "default"
00485 #define DEFAULT_MOHSUGGEST ""
00486 #define DEFAULT_VMEXTEN "asterisk"
00487 #define DEFAULT_CALLERID "asterisk"
00488 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00489 #define DEFAULT_MWITIME 10
00490 #define DEFAULT_ALLOWGUEST TRUE
00491 #define DEFAULT_SRVLOOKUP FALSE
00492 #define DEFAULT_COMPACTHEADERS FALSE
00493 #define DEFAULT_TOS_SIP 0
00494 #define DEFAULT_TOS_AUDIO 0
00495 #define DEFAULT_TOS_VIDEO 0
00496 #define DEFAULT_ALLOW_EXT_DOM TRUE
00497 #define DEFAULT_REALM "asterisk"
00498 #define DEFAULT_NOTIFYRINGING TRUE
00499 #define DEFAULT_PEDANTIC FALSE
00500 #define DEFAULT_AUTOCREATEPEER FALSE
00501 #define DEFAULT_QUALIFY FALSE
00502 #define DEFAULT_T1MIN 100
00503 #define DEFAULT_MAX_CALL_BITRATE (384)
00504 #ifndef DEFAULT_USERAGENT
00505 #define DEFAULT_USERAGENT "Asterisk PBX"
00506 #endif
00507
00508
00509
00510
00511 static char default_context[AST_MAX_CONTEXT];
00512 static char default_subscribecontext[AST_MAX_CONTEXT];
00513 static char default_language[MAX_LANGUAGE];
00514 static char default_callerid[AST_MAX_EXTENSION];
00515 static char default_fromdomain[AST_MAX_EXTENSION];
00516 static char default_notifymime[AST_MAX_EXTENSION];
00517 static int default_qualify;
00518 static char default_vmexten[AST_MAX_EXTENSION];
00519 static char default_mohinterpret[MAX_MUSICCLASS];
00520 static char default_mohsuggest[MAX_MUSICCLASS];
00521
00522 static int default_maxcallbitrate;
00523 static struct ast_codec_pref default_prefs;
00524
00525
00526 static int global_directrtpsetup;
00527 static int global_limitonpeers;
00528 static int global_rtautoclear;
00529 static int global_notifyringing;
00530 static int global_notifyhold;
00531 static int global_alwaysauthreject;
00532 static int srvlookup;
00533 static int pedanticsipchecking;
00534 static int autocreatepeer;
00535 static int global_relaxdtmf;
00536 static int global_rtptimeout;
00537 static int global_rtpholdtimeout;
00538 static int global_rtpkeepalive;
00539 static int global_reg_timeout;
00540 static int global_regattempts_max;
00541 static int global_allowguest;
00542 static int global_allowsubscribe;
00543
00544 static int global_mwitime;
00545 static unsigned int global_tos_sip;
00546 static unsigned int global_tos_audio;
00547 static unsigned int global_tos_video;
00548 static int compactheaders;
00549 static int recordhistory;
00550 static int dumphistory;
00551 static char global_realm[MAXHOSTNAMELEN];
00552 static char global_regcontext[AST_MAX_CONTEXT];
00553 static char global_useragent[AST_MAX_EXTENSION];
00554 static int allow_external_domains;
00555 static int global_callevents;
00556 static int global_t1min;
00557 static int global_autoframing;
00558 static enum transfermodes global_allowtransfer;
00559
00560 static int global_matchexterniplocally;
00561
00562
00563 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00564
00565
00566 static int suserobjs = 0;
00567 static int ruserobjs = 0;
00568 static int speerobjs = 0;
00569 static int rpeerobjs = 0;
00570 static int apeerobjs = 0;
00571 static int regobjs = 0;
00572
00573 static struct ast_flags global_flags[2] = {{0}};
00574
00575
00576 AST_MUTEX_DEFINE_STATIC(iflock);
00577
00578
00579
00580 AST_MUTEX_DEFINE_STATIC(netlock);
00581
00582 AST_MUTEX_DEFINE_STATIC(monlock);
00583
00584 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00585
00586
00587
00588 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00589
00590 static int sip_reloading = FALSE;
00591 static enum channelreloadreason sip_reloadreason;
00592
00593 static struct sched_context *sched;
00594 static struct io_context *io;
00595 static int *sipsock_read_id;
00596
00597 #define DEC_CALL_LIMIT 0
00598 #define INC_CALL_LIMIT 1
00599 #define DEC_CALL_RINGING 2
00600 #define INC_CALL_RINGING 3
00601
00602
00603 struct sip_request {
00604 char *rlPart1;
00605 char *rlPart2;
00606 int len;
00607 int headers;
00608 int method;
00609 int lines;
00610 unsigned int flags;
00611 char *header[SIP_MAX_HEADERS];
00612 char *line[SIP_MAX_LINES];
00613 char data[SIP_MAX_PACKET];
00614 unsigned int sdp_start;
00615 unsigned int sdp_end;
00616 };
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638 struct sip_dual {
00639 struct ast_channel *chan1;
00640 struct ast_channel *chan2;
00641 struct sip_request req;
00642 int seqno;
00643 };
00644
00645 struct sip_pkt;
00646
00647
00648 struct sip_invite_param {
00649 const char *distinctive_ring;
00650 int addsipheaders;
00651 const char *uri_options;
00652 const char *vxml_url;
00653 char *auth;
00654 char *authheader;
00655 enum sip_auth_type auth_type;
00656 const char *replaces;
00657 int transfer;
00658 };
00659
00660
00661 struct sip_route {
00662 struct sip_route *next;
00663 char hop[0];
00664 };
00665
00666
00667 enum domain_mode {
00668 SIP_DOMAIN_AUTO,
00669 SIP_DOMAIN_CONFIG,
00670 };
00671
00672
00673
00674
00675
00676 struct domain {
00677 char domain[MAXHOSTNAMELEN];
00678 char context[AST_MAX_EXTENSION];
00679 enum domain_mode mode;
00680 AST_LIST_ENTRY(domain) list;
00681 };
00682
00683 static AST_LIST_HEAD_STATIC(domain_list, domain);
00684
00685
00686
00687 struct sip_history {
00688 AST_LIST_ENTRY(sip_history) list;
00689 char event[0];
00690 };
00691
00692 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00693
00694
00695 struct sip_auth {
00696 char realm[AST_MAX_EXTENSION];
00697 char username[256];
00698 char secret[256];
00699 char md5secret[256];
00700 struct sip_auth *next;
00701 };
00702
00703
00704 #define SIP_ALREADYGONE (1 << 0)
00705 #define SIP_NEEDDESTROY (1 << 1)
00706 #define SIP_NOVIDEO (1 << 2)
00707 #define SIP_RINGING (1 << 3)
00708 #define SIP_PROGRESS_SENT (1 << 4)
00709 #define SIP_NEEDREINVITE (1 << 5)
00710 #define SIP_PENDINGBYE (1 << 6)
00711 #define SIP_GOTREFER (1 << 7)
00712 #define SIP_PROMISCREDIR (1 << 8)
00713 #define SIP_TRUSTRPID (1 << 9)
00714 #define SIP_USEREQPHONE (1 << 10)
00715 #define SIP_REALTIME (1 << 11)
00716 #define SIP_USECLIENTCODE (1 << 12)
00717 #define SIP_OUTGOING (1 << 13)
00718 #define SIP_FREE_BIT (1 << 14)
00719 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 15)
00720 #define SIP_DTMF (3 << 16)
00721 #define SIP_DTMF_RFC2833 (0 << 16)
00722 #define SIP_DTMF_INBAND (1 << 16)
00723 #define SIP_DTMF_INFO (2 << 16)
00724 #define SIP_DTMF_AUTO (3 << 16)
00725
00726 #define SIP_NAT (3 << 18)
00727 #define SIP_NAT_NEVER (0 << 18)
00728 #define SIP_NAT_RFC3581 (1 << 18)
00729 #define SIP_NAT_ROUTE (2 << 18)
00730 #define SIP_NAT_ALWAYS (3 << 18)
00731
00732 #define SIP_REINVITE (7 << 20)
00733 #define SIP_CAN_REINVITE (1 << 20)
00734 #define SIP_CAN_REINVITE_NAT (2 << 20)
00735 #define SIP_REINVITE_UPDATE (4 << 20)
00736
00737 #define SIP_INSECURE_PORT (1 << 23)
00738 #define SIP_INSECURE_INVITE (1 << 24)
00739
00740 #define SIP_PROG_INBAND (3 << 25)
00741 #define SIP_PROG_INBAND_NEVER (0 << 25)
00742 #define SIP_PROG_INBAND_NO (1 << 25)
00743 #define SIP_PROG_INBAND_YES (2 << 25)
00744 #define SIP_NO_HISTORY (1 << 27)
00745 #define SIP_CALL_LIMIT (1 << 28)
00746 #define SIP_SENDRPID (1 << 29)
00747 #define SIP_INC_COUNT (1 << 30)
00748 #define SIP_G726_NONSTANDARD (1 << 31)
00749
00750 #define SIP_FLAGS_TO_COPY \
00751 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
00752 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
00753 SIP_USEREQPHONE | SIP_INSECURE_PORT | SIP_INSECURE_INVITE)
00754
00755
00756
00757 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
00758 #define SIP_PAGE2_RTUPDATE (1 << 1)
00759 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
00760 #define SIP_PAGE2_RT_FROMCONTACT (1 << 4)
00761 #define SIP_PAGE2_RTSAVE_SYSNAME (1 << 5)
00762
00763 #define SIP_PAGE2_IGNOREREGEXPIRE (1 << 10)
00764 #define SIP_PAGE2_DEBUG (3 << 11)
00765 #define SIP_PAGE2_DEBUG_CONFIG (1 << 11)
00766 #define SIP_PAGE2_DEBUG_CONSOLE (1 << 12)
00767 #define SIP_PAGE2_DYNAMIC (1 << 13)
00768 #define SIP_PAGE2_SELFDESTRUCT (1 << 14)
00769 #define SIP_PAGE2_VIDEOSUPPORT (1 << 15)
00770 #define SIP_PAGE2_ALLOWSUBSCRIBE (1 << 16)
00771 #define SIP_PAGE2_ALLOWOVERLAP (1 << 17)
00772 #define SIP_PAGE2_SUBSCRIBEMWIONLY (1 << 18)
00773 #define SIP_PAGE2_INC_RINGING (1 << 19)
00774 #define SIP_PAGE2_T38SUPPORT (7 << 20)
00775 #define SIP_PAGE2_T38SUPPORT_UDPTL (1 << 20)
00776 #define SIP_PAGE2_T38SUPPORT_RTP (2 << 20)
00777 #define SIP_PAGE2_T38SUPPORT_TCP (4 << 20)
00778 #define SIP_PAGE2_CALL_ONHOLD (3 << 23)
00779 #define SIP_PAGE2_CALL_ONHOLD_ONEDIR (1 << 23)
00780 #define SIP_PAGE2_CALL_ONHOLD_INACTIVE (1 << 24)
00781 #define SIP_PAGE2_RFC2833_COMPENSATE (1 << 25)
00782 #define SIP_PAGE2_BUGGY_MWI (1 << 26)
00783 #define SIP_PAGE2_OUTGOING_CALL (1 << 27)
00784
00785 #define SIP_PAGE2_FLAGS_TO_COPY \
00786 (SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_VIDEOSUPPORT | \
00787 SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | SIP_PAGE2_BUGGY_MWI)
00788
00789
00790 #define SIP_PKT_DEBUG (1 << 0)
00791 #define SIP_PKT_WITH_TOTAG (1 << 1)
00792 #define SIP_PKT_IGNORE (1 << 2)
00793 #define SIP_PKT_IGNORE_RESP (1 << 3)
00794 #define SIP_PKT_IGNORE_REQ (1 << 4)
00795
00796
00797 #define T38FAX_FILL_BIT_REMOVAL (1 << 0)
00798 #define T38FAX_TRANSCODING_MMR (1 << 1)
00799 #define T38FAX_TRANSCODING_JBIG (1 << 2)
00800
00801 #define T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF (0 << 3)
00802 #define T38FAX_RATE_MANAGEMENT_LOCAL_TCF (1 << 3)
00803
00804 #define T38FAX_UDP_EC_NONE (0 << 4)
00805 #define T38FAX_UDP_EC_FEC (1 << 4)
00806 #define T38FAX_UDP_EC_REDUNDANCY (2 << 4)
00807
00808 #define T38FAX_VERSION (3 << 6)
00809 #define T38FAX_VERSION_0 (0 << 6)
00810 #define T38FAX_VERSION_1 (1 << 6)
00811
00812 #define T38FAX_RATE_2400 (1 << 8)
00813 #define T38FAX_RATE_4800 (1 << 9)
00814 #define T38FAX_RATE_7200 (1 << 10)
00815 #define T38FAX_RATE_9600 (1 << 11)
00816 #define T38FAX_RATE_12000 (1 << 12)
00817 #define T38FAX_RATE_14400 (1 << 13)
00818
00819
00820 static int global_t38_capability = T38FAX_VERSION_0 | T38FAX_RATE_2400 | T38FAX_RATE_4800 | T38FAX_RATE_7200 | T38FAX_RATE_9600;
00821
00822 #define sipdebug ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG)
00823 #define sipdebug_config ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG)
00824 #define sipdebug_console ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE)
00825
00826
00827 enum t38state {
00828 T38_DISABLED = 0,
00829 T38_LOCAL_DIRECT,
00830 T38_LOCAL_REINVITE,
00831 T38_PEER_DIRECT,
00832 T38_PEER_REINVITE,
00833 T38_ENABLED
00834 };
00835
00836
00837 struct t38properties {
00838 struct ast_flags t38support;
00839 int capability;
00840 int peercapability;
00841 int jointcapability;
00842 enum t38state state;
00843 };
00844
00845
00846 enum referstatus {
00847 REFER_IDLE,
00848 REFER_SENT,
00849 REFER_RECEIVED,
00850 REFER_CONFIRMED,
00851 REFER_ACCEPTED,
00852 REFER_RINGING,
00853 REFER_200OK,
00854 REFER_FAILED,
00855 REFER_NOAUTH
00856 };
00857
00858 static const struct c_referstatusstring {
00859 enum referstatus status;
00860 char *text;
00861 } referstatusstrings[] = {
00862 { REFER_IDLE, "<none>" },
00863 { REFER_SENT, "Request sent" },
00864 { REFER_RECEIVED, "Request received" },
00865 { REFER_ACCEPTED, "Accepted" },
00866 { REFER_RINGING, "Target ringing" },
00867 { REFER_200OK, "Done" },
00868 { REFER_FAILED, "Failed" },
00869 { REFER_NOAUTH, "Failed - auth failure" }
00870 } ;
00871
00872
00873
00874 struct sip_refer {
00875 char refer_to[AST_MAX_EXTENSION];
00876 char refer_to_domain[AST_MAX_EXTENSION];
00877 char refer_to_urioption[AST_MAX_EXTENSION];
00878 char refer_to_context[AST_MAX_EXTENSION];
00879 char referred_by[AST_MAX_EXTENSION];
00880 char referred_by_name[AST_MAX_EXTENSION];
00881 char refer_contact[AST_MAX_EXTENSION];
00882 char replaces_callid[BUFSIZ];
00883 char replaces_callid_totag[BUFSIZ/2];
00884 char replaces_callid_fromtag[BUFSIZ/2];
00885 struct sip_pvt *refer_call;
00886 int attendedtransfer;
00887 int localtransfer;
00888 enum referstatus status;
00889 };
00890
00891
00892 static struct sip_pvt {
00893 ast_mutex_t lock;
00894 int method;
00895 enum invitestates invitestate;
00896 AST_DECLARE_STRING_FIELDS(
00897 AST_STRING_FIELD(callid);
00898 AST_STRING_FIELD(randdata);
00899 AST_STRING_FIELD(accountcode);
00900 AST_STRING_FIELD(realm);
00901 AST_STRING_FIELD(nonce);
00902 AST_STRING_FIELD(opaque);
00903 AST_STRING_FIELD(qop);
00904 AST_STRING_FIELD(domain);
00905 AST_STRING_FIELD(from);
00906 AST_STRING_FIELD(useragent);
00907 AST_STRING_FIELD(exten);
00908 AST_STRING_FIELD(context);
00909 AST_STRING_FIELD(subscribecontext);
00910 AST_STRING_FIELD(subscribeuri);
00911 AST_STRING_FIELD(fromdomain);
00912 AST_STRING_FIELD(fromuser);
00913 AST_STRING_FIELD(fromname);
00914 AST_STRING_FIELD(tohost);
00915 AST_STRING_FIELD(language);
00916 AST_STRING_FIELD(mohinterpret);
00917 AST_STRING_FIELD(mohsuggest);
00918 AST_STRING_FIELD(rdnis);
00919 AST_STRING_FIELD(theirtag);
00920 AST_STRING_FIELD(username);
00921 AST_STRING_FIELD(peername);
00922 AST_STRING_FIELD(authname);
00923 AST_STRING_FIELD(uri);
00924 AST_STRING_FIELD(okcontacturi);
00925 AST_STRING_FIELD(peersecret);
00926 AST_STRING_FIELD(peermd5secret);
00927 AST_STRING_FIELD(cid_num);
00928 AST_STRING_FIELD(cid_name);
00929 AST_STRING_FIELD(via);
00930 AST_STRING_FIELD(fullcontact);
00931 AST_STRING_FIELD(our_contact);
00932 AST_STRING_FIELD(rpid);
00933 AST_STRING_FIELD(rpid_from);
00934 );
00935 unsigned int ocseq;
00936 unsigned int icseq;
00937 ast_group_t callgroup;
00938 ast_group_t pickupgroup;
00939 int lastinvite;
00940 struct ast_flags flags[2];
00941 int timer_t1;
00942 unsigned int sipoptions;
00943 struct ast_codec_pref prefs;
00944 int capability;
00945 int jointcapability;
00946 int peercapability;
00947 int prefcodec;
00948 int noncodeccapability;
00949 int jointnoncodeccapability;
00950 int redircodecs;
00951 int maxcallbitrate;
00952 struct t38properties t38;
00953 struct sockaddr_in udptlredirip;
00954 struct ast_udptl *udptl;
00955 int callingpres;
00956 int authtries;
00957 int expiry;
00958 long branch;
00959 char tag[11];
00960 int sessionid;
00961 int sessionversion;
00962 struct sockaddr_in sa;
00963 struct sockaddr_in redirip;
00964 struct sockaddr_in vredirip;
00965 time_t lastrtprx;
00966 time_t lastrtptx;
00967 int rtptimeout;
00968 struct sockaddr_in recv;
00969 struct in_addr ourip;
00970 struct ast_channel *owner;
00971 struct sip_route *route;
00972 int route_persistant;
00973 struct sip_auth *peerauth;
00974 int noncecount;
00975 char lastmsg[256];
00976 int amaflags;
00977 int pendinginvite;
00978 struct sip_request initreq;
00979
00980
00981 int maxtime;
00982 int initid;
00983 int autokillid;
00984 enum transfermodes allowtransfer;
00985 struct sip_refer *refer;
00986 enum subscriptiontype subscribed;
00987 int stateid;
00988 int laststate;
00989 int dialogver;
00990
00991 struct ast_dsp *vad;
00992
00993 struct sip_peer *relatedpeer;
00994
00995 struct sip_registry *registry;
00996 struct ast_rtp *rtp;
00997 struct ast_rtp *vrtp;
00998 struct sip_pkt *packets;
00999 struct sip_history_head *history;
01000 struct ast_variable *chanvars;
01001 struct sip_pvt *next;
01002 struct sip_invite_param *options;
01003 int autoframing;
01004 } *iflist = NULL;
01005
01006 #define FLAG_RESPONSE (1 << 0)
01007 #define FLAG_FATAL (1 << 1)
01008
01009
01010 struct sip_pkt {
01011 struct sip_pkt *next;
01012 int retrans;
01013 int method;
01014 int seqno;
01015 unsigned int flags;
01016 struct sip_pvt *owner;
01017 int retransid;
01018 int timer_a;
01019 int timer_t1;
01020 int packetlen;
01021 char data[0];
01022 };
01023
01024
01025 struct sip_user {
01026
01027 ASTOBJ_COMPONENTS(struct sip_user);
01028 char secret[80];
01029 char md5secret[80];
01030 char context[AST_MAX_CONTEXT];
01031 char subscribecontext[AST_MAX_CONTEXT];
01032 char cid_num[80];
01033 char cid_name[80];
01034 char accountcode[AST_MAX_ACCOUNT_CODE];
01035 char language[MAX_LANGUAGE];
01036 char mohinterpret[MAX_MUSICCLASS];
01037 char mohsuggest[MAX_MUSICCLASS];
01038 char useragent[256];
01039 struct ast_codec_pref prefs;
01040 ast_group_t callgroup;
01041 ast_group_t pickupgroup;
01042 unsigned int sipoptions;
01043 struct ast_flags flags[2];
01044 int amaflags;
01045 int callingpres;
01046 int capability;
01047 int inUse;
01048 int call_limit;
01049 enum transfermodes allowtransfer;
01050 struct ast_ha *ha;
01051 struct ast_variable *chanvars;
01052 int maxcallbitrate;
01053 int autoframing;
01054 };
01055
01056
01057
01058 struct sip_peer {
01059 ASTOBJ_COMPONENTS(struct sip_peer);
01060
01061 char secret[80];
01062 char md5secret[80];
01063 struct sip_auth *auth;
01064 char context[AST_MAX_CONTEXT];
01065 char subscribecontext[AST_MAX_CONTEXT];
01066 char username[80];
01067 char accountcode[AST_MAX_ACCOUNT_CODE];
01068 int amaflags;
01069 char tohost[MAXHOSTNAMELEN];
01070 char regexten[AST_MAX_EXTENSION];
01071 char fromuser[80];
01072 char fromdomain[MAXHOSTNAMELEN];
01073 char fullcontact[256];
01074 char cid_num[80];
01075 char cid_name[80];
01076 int callingpres;
01077 int inUse;
01078 int inRinging;
01079 int onHold;
01080 int call_limit;
01081 enum transfermodes allowtransfer;
01082 char vmexten[AST_MAX_EXTENSION];
01083 char mailbox[AST_MAX_EXTENSION];
01084 char language[MAX_LANGUAGE];
01085 char mohinterpret[MAX_MUSICCLASS];
01086 char mohsuggest[MAX_MUSICCLASS];
01087 char useragent[256];
01088 struct ast_codec_pref prefs;
01089 int lastmsgssent;
01090 time_t lastmsgcheck;
01091 unsigned int sipoptions;
01092 struct ast_flags flags[2];
01093 int expire;
01094 int capability;
01095 int rtptimeout;
01096 int rtpholdtimeout;
01097 int rtpkeepalive;
01098 ast_group_t callgroup;
01099 ast_group_t pickupgroup;
01100 struct sockaddr_in addr;
01101 int maxcallbitrate;
01102
01103
01104 struct sip_pvt *call;
01105 int pokeexpire;
01106 int lastms;
01107 int maxms;
01108 struct timeval ps;
01109
01110 struct sockaddr_in defaddr;
01111 struct ast_ha *ha;
01112 struct ast_variable *chanvars;
01113 struct sip_pvt *mwipvt;
01114 int lastmsg;
01115 int autoframing;
01116 };
01117
01118
01119
01120
01121 struct sip_registry {
01122 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
01123 AST_DECLARE_STRING_FIELDS(
01124 AST_STRING_FIELD(callid);
01125 AST_STRING_FIELD(realm);
01126 AST_STRING_FIELD(nonce);
01127 AST_STRING_FIELD(opaque);
01128 AST_STRING_FIELD(qop);
01129 AST_STRING_FIELD(domain);
01130 AST_STRING_FIELD(username);
01131 AST_STRING_FIELD(authuser);
01132 AST_STRING_FIELD(hostname);
01133 AST_STRING_FIELD(secret);
01134 AST_STRING_FIELD(md5secret);
01135 AST_STRING_FIELD(contact);
01136 AST_STRING_FIELD(random);
01137 );
01138 int portno;
01139 int expire;
01140 int regattempts;
01141 int timeout;
01142 int refresh;
01143 struct sip_pvt *call;
01144 enum sipregistrystate regstate;
01145 time_t regtime;
01146 int callid_valid;
01147 unsigned int ocseq;
01148 struct sockaddr_in us;
01149 int noncecount;
01150 char lastmsg[256];
01151 };
01152
01153
01154
01155
01156 static struct ast_user_list {
01157 ASTOBJ_CONTAINER_COMPONENTS(struct sip_user);
01158 } userl;
01159
01160
01161 static struct ast_peer_list {
01162 ASTOBJ_CONTAINER_COMPONENTS(struct sip_peer);
01163 } peerl;
01164
01165
01166 static struct ast_register_list {
01167 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01168 int recheck;
01169 } regl;
01170
01171 static void temp_pvt_cleanup(void *);
01172
01173
01174 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01175
01176
01177 static struct sip_auth *authl = NULL;
01178
01179
01180
01181 static int sipsock = -1;
01182 static struct sockaddr_in bindaddr = { 0, };
01183 static struct sockaddr_in externip;
01184 static char externhost[MAXHOSTNAMELEN];
01185 static time_t externexpire = 0;
01186 static int externrefresh = 10;
01187 static struct ast_ha *localaddr;
01188 static struct in_addr __ourip;
01189 static struct sockaddr_in outboundproxyip;
01190 static int ourport;
01191 static struct sockaddr_in debugaddr;
01192
01193 static struct ast_config *notify_types;
01194
01195
01196
01197
01198
01199
01200 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
01201 static int sip_devicestate(void *data);
01202 static int sip_sendtext(struct ast_channel *ast, const char *text);
01203 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01204 static int sip_hangup(struct ast_channel *ast);
01205 static int sip_answer(struct ast_channel *ast);
01206 static struct ast_frame *sip_read(struct ast_channel *ast);
01207 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01208 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01209 static int sip_transfer(struct ast_channel *ast, const char *dest);
01210 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01211 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01212 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01213
01214
01215 static int sipsock_read(int *id, int fd, short events, void *ignore);
01216 static int __sip_xmit(struct sip_pvt *p, char *data, int len);
01217 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod);
01218 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01219 static int retrans_pkt(void *data);
01220 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req);
01221 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);
01222 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01223 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01224 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01225 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01226 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01227 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);
01228 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01229 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, int reliable);
01230 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01231 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01232 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
01233 static int transmit_reinvite_with_sdp(struct sip_pvt *p);
01234 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01235 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01236 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01237 static int transmit_refer(struct sip_pvt *p, const char *dest);
01238 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten);
01239 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01240 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01241 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01242 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01243 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01244 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01245 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
01246 static int sip_send_mwi_to_peer(struct sip_peer *peer);
01247 static int does_peer_need_mwi(struct sip_peer *peer);
01248
01249
01250 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
01251 int useglobal_nat, const int intended_method);
01252 static int __sip_autodestruct(void *data);
01253 static void sip_scheddestroy(struct sip_pvt *p, int ms);
01254 static void sip_cancel_destroy(struct sip_pvt *p);
01255 static void sip_destroy(struct sip_pvt *p);
01256 static void __sip_destroy(struct sip_pvt *p, int lockowner);
01257 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01258 static void __sip_pretend_ack(struct sip_pvt *p);
01259 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
01260 static int auto_congest(void *nothing);
01261 static int update_call_counter(struct sip_pvt *fup, int event);
01262 static int hangup_sip2cause(int cause);
01263 static const char *hangup_cause2sip(int cause);
01264 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method);
01265 static void free_old_route(struct sip_route *route);
01266 static void list_route(struct sip_route *route);
01267 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01268 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
01269 struct sip_request *req, char *uri);
01270 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01271 static void check_pendings(struct sip_pvt *p);
01272 static void *sip_park_thread(void *stuff);
01273 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno);
01274 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01275
01276
01277 static void try_suggested_sip_codec(struct sip_pvt *p);
01278 static const char* get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01279 static const char *get_sdp(struct sip_request *req, const char *name);
01280 static int find_sdp(struct sip_request *req);
01281 static int process_sdp(struct sip_pvt *p, struct sip_request *req);
01282 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
01283 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01284 int debug, int *min_packet_size);
01285 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
01286 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
01287 int debug);
01288 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p);
01289
01290
01291 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01292 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01293 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01294 const char *secret, const char *md5secret, int sipmethod,
01295 char *uri, enum xmittype reliable, int ignore);
01296 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01297 int sipmethod, char *uri, enum xmittype reliable,
01298 struct sockaddr_in *sin, struct sip_peer **authpeer);
01299 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin);
01300
01301
01302 static int check_sip_domain(const char *domain, char *context, size_t len);
01303 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01304 static void clear_sip_domains(void);
01305
01306
01307 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno);
01308 static int clear_realm_authentication(struct sip_auth *authlist);
01309 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
01310
01311
01312 static int sip_do_reload(enum channelreloadreason reason);
01313 static int reload_config(enum channelreloadreason reason);
01314 static int expire_register(void *data);
01315 static void *do_monitor(void *data);
01316 static int restart_monitor(void);
01317 static int sip_send_mwi_to_peer(struct sip_peer *peer);
01318 static void sip_destroy(struct sip_pvt *p);
01319 static int sip_addrcmp(char *name, struct sockaddr_in *sin);
01320 static int sip_refer_allocate(struct sip_pvt *p);
01321 static void ast_quiet_chan(struct ast_channel *chan);
01322 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01323
01324
01325 static int cb_extensionstate(char *context, char* exten, int state, void *data);
01326 static int sip_devicestate(void *data);
01327 static int sip_poke_noanswer(void *data);
01328 static int sip_poke_peer(struct sip_peer *peer);
01329 static void sip_poke_all_peers(void);
01330 static void sip_peer_hold(struct sip_pvt *p, int hold);
01331
01332
01333 static const char *sip_nat_mode(const struct sip_pvt *p);
01334 static int sip_show_inuse(int fd, int argc, char *argv[]);
01335 static char *transfermode2str(enum transfermodes mode) attribute_const;
01336 static char *nat2str(int nat) attribute_const;
01337 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01338 static int sip_show_users(int fd, int argc, char *argv[]);
01339 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01340 static int sip_show_peers(int fd, int argc, char *argv[]);
01341 static int sip_show_objects(int fd, int argc, char *argv[]);
01342 static void print_group(int fd, ast_group_t group, int crlf);
01343 static const char *dtmfmode2str(int mode) attribute_const;
01344 static const char *insecure2str(int port, int invite) attribute_const;
01345 static void cleanup_stale_contexts(char *new, char *old);
01346 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01347 static const char *domain_mode_to_text(const enum domain_mode mode);
01348 static int sip_show_domains(int fd, int argc, char *argv[]);
01349 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01350 static int sip_show_peer(int fd, int argc, char *argv[]);
01351 static int sip_show_user(int fd, int argc, char *argv[]);
01352 static int sip_show_registry(int fd, int argc, char *argv[]);
01353 static int sip_show_settings(int fd, int argc, char *argv[]);
01354 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01355 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01356 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01357 static int sip_show_channels(int fd, int argc, char *argv[]);
01358 static int sip_show_subscriptions(int fd, int argc, char *argv[]);
01359 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
01360 static char *complete_sipch(const char *line, const char *word, int pos, int state);
01361 static char *complete_sip_peer(const char *word, int state, int flags2);
01362 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01363 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state);
01364 static char *complete_sip_user(const char *word, int state, int flags2);
01365 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
01366 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01367 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state);
01368 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state);
01369 static int sip_show_channel(int fd, int argc, char *argv[]);
01370 static int sip_show_history(int fd, int argc, char *argv[]);
01371 static int sip_do_debug_ip(int fd, int argc, char *argv[]);
01372 static int sip_do_debug_peer(int fd, int argc, char *argv[]);
01373 static int sip_do_debug(int fd, int argc, char *argv[]);
01374 static int sip_no_debug(int fd, int argc, char *argv[]);
01375 static int sip_notify(int fd, int argc, char *argv[]);
01376 static int sip_do_history(int fd, int argc, char *argv[]);
01377 static int sip_no_history(int fd, int argc, char *argv[]);
01378 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
01379 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01380 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01381 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len);
01382 static int sip_dtmfmode(struct ast_channel *chan, void *data);
01383 static int sip_addheader(struct ast_channel *chan, void *data);
01384 static int sip_do_reload(enum channelreloadreason reason);
01385 static int sip_reload(int fd, int argc, char *argv[]);
01386 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen);
01387
01388
01389
01390
01391
01392 static void sip_dump_history(struct sip_pvt *dialog);
01393 static inline int sip_debug_test_addr(const struct sockaddr_in *addr);
01394 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01395 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01396 static void sip_dump_history(struct sip_pvt *dialog);
01397
01398
01399 static struct sip_peer *temp_peer(const char *name);
01400 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
01401 static struct sip_user *build_user(const char *name, struct ast_variable *v, int realtime);
01402 static int update_call_counter(struct sip_pvt *fup, int event);
01403 static void sip_destroy_peer(struct sip_peer *peer);
01404 static void sip_destroy_user(struct sip_user *user);
01405 static int sip_poke_peer(struct sip_peer *peer);
01406 static int sip_poke_peer_s(void *data);
01407 static void set_peer_defaults(struct sip_peer *peer);
01408 static struct sip_peer *temp_peer(const char *name);
01409 static void register_peer_exten(struct sip_peer *peer, int onoff);
01410 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime);
01411 static struct sip_user *find_user(const char *name, int realtime);
01412 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01413 static int expire_register(void *data);
01414 static void reg_source_db(struct sip_peer *peer);
01415 static void destroy_association(struct sip_peer *peer);
01416 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01417
01418
01419 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey);
01420 static struct sip_user *realtime_user(const char *username);
01421 static void update_peer(struct sip_peer *p, int expiry);
01422 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin);
01423 static int sip_prune_realtime(int fd, int argc, char *argv[]);
01424
01425
01426 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us);
01427 static void sip_registry_destroy(struct sip_registry *reg);
01428 static int sip_register(char *value, int lineno);
01429 static char *regstate2str(enum sipregistrystate regstate) attribute_const;
01430 static int sip_reregister(void *data);
01431 static int __sip_do_register(struct sip_registry *r);
01432 static int sip_reg_timeout(void *data);
01433 static void sip_send_all_registers(void);
01434
01435
01436 static void append_date(struct sip_request *req);
01437 static int determine_firstline_parts(struct sip_request *req);
01438 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01439 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01440 static int find_sip_method(const char *msg);
01441 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
01442 static void parse_request(struct sip_request *req);
01443 static const char *get_header(const struct sip_request *req, const char *name);
01444 static char *referstatus2str(enum referstatus rstatus) attribute_pure;
01445 static int method_match(enum sipmethod id, const char *name);
01446 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
01447 static char *get_in_brackets(char *tmp);
01448 static const char *find_alias(const char *name, const char *_default);
01449 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
01450 static int lws2sws(char *msgbuf, int len);
01451 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
01452 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
01453 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
01454 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
01455 static int set_address_from_contact(struct sip_pvt *pvt);
01456 static void check_via(struct sip_pvt *p, struct sip_request *req);
01457 static char *get_calleridname(const char *input, char *output, size_t outputsize);
01458 static int get_rpid_num(const char *input, char *output, int maxlen);
01459 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq);
01460 static int get_destination(struct sip_pvt *p, struct sip_request *oreq);
01461 static int get_msg_text(char *buf, int len, struct sip_request *req);
01462 static void free_old_route(struct sip_route *route);
01463 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
01464
01465
01466 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
01467 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
01468 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
01469 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod);
01470 static int init_resp(struct sip_request *resp, const char *msg);
01471 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
01472 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p);
01473 static void build_via(struct sip_pvt *p);
01474 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
01475 static int create_addr(struct sip_pvt *dialog, const char *opeer);
01476 static char *generate_random_string(char *buf, size_t size);
01477 static void build_callid_pvt(struct sip_pvt *pvt);
01478 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain);
01479 static void make_our_tag(char *tagbuf, size_t len);
01480 static int add_header(struct sip_request *req, const char *var, const char *value);
01481 static int add_header_contentLength(struct sip_request *req, int len);
01482 static int add_line(struct sip_request *req, const char *line);
01483 static int add_text(struct sip_request *req, const char *text);
01484 static int add_digit(struct sip_request *req, char digit, unsigned int duration);
01485 static int add_vidupdate(struct sip_request *req);
01486 static void add_route(struct sip_request *req, struct sip_route *route);
01487 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01488 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01489 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
01490 static void set_destination(struct sip_pvt *p, char *uri);
01491 static void append_date(struct sip_request *req);
01492 static void build_contact(struct sip_pvt *p);
01493 static void build_rpid(struct sip_pvt *p);
01494
01495
01496 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock);
01497 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);
01498 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock);
01499 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
01500 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e);
01501 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
01502 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
01503 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01504 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
01505 static int handle_request_options(struct sip_pvt *p, struct sip_request *req);
01506 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin);
01507 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e);
01508 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno);
01509
01510
01511 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01512 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno);
01513 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno);
01514 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno);
01515
01516
01517 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active);
01518 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01519 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
01520 static int sip_get_codec(struct ast_channel *chan);
01521 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect);
01522
01523
01524 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite);
01525 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
01526 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p);
01527 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
01528 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
01529
01530
01531 static const struct ast_channel_tech sip_tech = {
01532 .type = "SIP",
01533 .description = "Session Initiation Protocol (SIP)",
01534 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01535 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01536 .requester = sip_request_call,
01537 .devicestate = sip_devicestate,
01538 .call = sip_call,
01539 .hangup = sip_hangup,
01540 .answer = sip_answer,
01541 .read = sip_read,
01542 .write = sip_write,
01543 .write_video = sip_write,
01544 .indicate = sip_indicate,
01545 .transfer = sip_transfer,
01546 .fixup = sip_fixup,
01547 .send_digit_begin = sip_senddigit_begin,
01548 .send_digit_end = sip_senddigit_end,
01549 .bridge = ast_rtp_bridge,
01550 .send_text = sip_sendtext,
01551 .func_channel_read = acf_channel_read,
01552 };
01553
01554
01555
01556
01557 static const struct ast_channel_tech sip_tech_info = {
01558 .type = "SIP",
01559 .description = "Session Initiation Protocol (SIP)",
01560 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
01561 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01562 .requester = sip_request_call,
01563 .devicestate = sip_devicestate,
01564 .call = sip_call,
01565 .hangup = sip_hangup,
01566 .answer = sip_answer,
01567 .read = sip_read,
01568 .write = sip_write,
01569 .write_video = sip_write,
01570 .indicate = sip_indicate,
01571 .transfer = sip_transfer,
01572 .fixup = sip_fixup,
01573 .send_digit_end = sip_senddigit_end,
01574 .bridge = ast_rtp_bridge,
01575 .send_text = sip_sendtext,
01576 };
01577
01578
01579
01580 #define UNLINK(element, head, prev) do { \
01581 if (prev) \
01582 (prev)->next = (element)->next; \
01583 else \
01584 (head) = (element)->next; \
01585 } while (0)
01586
01587
01588 static struct ast_rtp_protocol sip_rtp = {
01589 type: "SIP",
01590 get_rtp_info: sip_get_rtp_peer,
01591 get_vrtp_info: sip_get_vrtp_peer,
01592 set_rtp_peer: sip_set_rtp_peer,
01593 get_codec: sip_get_codec,
01594 };
01595
01596
01597 static struct ast_udptl_protocol sip_udptl = {
01598 type: "SIP",
01599 get_udptl_info: sip_get_udptl_peer,
01600 set_udptl_peer: sip_set_udptl_peer,
01601 };
01602
01603
01604 static char *referstatus2str(enum referstatus rstatus)
01605 {
01606 int i = (sizeof(referstatusstrings) / sizeof(referstatusstrings[0]));
01607 int x;
01608
01609 for (x = 0; x < i; x++) {
01610 if (referstatusstrings[x].status == rstatus)
01611 return (char *) referstatusstrings[x].text;
01612 }
01613 return "";
01614 }
01615
01616
01617
01618
01619 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
01620 {
01621 if (p->initreq.headers && option_debug) {
01622 ast_log(LOG_DEBUG, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
01623 }
01624
01625 copy_request(&p->initreq, req);
01626 parse_request(&p->initreq);
01627 if (ast_test_flag(req, SIP_PKT_DEBUG))
01628 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
01629 }
01630
01631 static void sip_alreadygone(struct sip_pvt *dialog)
01632 {
01633 if (option_debug > 2)
01634 ast_log(LOG_DEBUG, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
01635 ast_set_flag(&dialog->flags[0], SIP_ALREADYGONE);
01636 }
01637
01638
01639
01640
01641
01642
01643
01644
01645 static int method_match(enum sipmethod id, const char *name)
01646 {
01647 int len = strlen(sip_methods[id].text);
01648 int l_name = name ? strlen(name) : 0;
01649
01650 return (l_name >= len && name[len] < 33 &&
01651 !strncasecmp(sip_methods[id].text, name, len));
01652 }
01653
01654
01655 static int find_sip_method(const char *msg)
01656 {
01657 int i, res = 0;
01658
01659 if (ast_strlen_zero(msg))
01660 return 0;
01661 for (i = 1; i < (sizeof(sip_methods) / sizeof(sip_methods[0])) && !res; i++) {
01662 if (method_match(i, msg))
01663 res = sip_methods[i].id;
01664 }
01665 return res;
01666 }
01667
01668
01669 static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
01670 {
01671 char *next, *sep;
01672 char *temp;
01673 unsigned int profile = 0;
01674 int i, found;
01675
01676 if (ast_strlen_zero(supported) )
01677 return 0;
01678 temp = ast_strdupa(supported);
01679
01680 if (option_debug > 2 && sipdebug)
01681 ast_log(LOG_DEBUG, "Begin: parsing SIP \"Supported: %s\"\n", supported);
01682
01683 for (next = temp; next; next = sep) {
01684 found = FALSE;
01685 if ( (sep = strchr(next, ',')) != NULL)
01686 *sep++ = '\0';
01687 next = ast_skip_blanks(next);
01688 if (option_debug > 2 && sipdebug)
01689 ast_log(LOG_DEBUG, "Found SIP option: -%s-\n", next);
01690 for (i=0; i < (sizeof(sip_options) / sizeof(sip_options[0])); i++) {
01691 if (!strcasecmp(next, sip_options[i].text)) {
01692 profile |= sip_options[i].id;
01693 found = TRUE;
01694 if (option_debug > 2 && sipdebug)
01695 ast_log(LOG_DEBUG, "Matched SIP option: %s\n", next);
01696 break;
01697 }
01698 }
01699 if (!found && option_debug > 2 && sipdebug) {
01700 if (!strncasecmp(next, "x-", 2))
01701 ast_log(LOG_DEBUG, "Found private SIP option, not supported: %s\n", next);
01702 else
01703 ast_log(LOG_DEBUG, "Found no match for SIP option: %s (Please file bug report!)\n", next);
01704 }
01705 }
01706
01707 if (pvt)
01708 pvt->sipoptions = profile;
01709 return profile;
01710 }
01711
01712
01713 static inline int sip_debug_test_addr(const struct sockaddr_in *addr)
01714 {
01715 if (!sipdebug)
01716 return 0;
01717 if (debugaddr.sin_addr.s_addr) {
01718 if (((ntohs(debugaddr.sin_port) != 0)
01719 && (debugaddr.sin_port != addr->sin_port))
01720 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
01721 return 0;
01722 }
01723 return 1;
01724 }
01725
01726
01727 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
01728 {
01729 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
01730 }
01731
01732
01733 static const char *sip_nat_mode(const struct sip_pvt *p)
01734 {
01735 return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
01736 }
01737
01738
01739 static inline int sip_debug_test_pvt(struct sip_pvt *p)
01740 {
01741 if (!sipdebug)
01742 return 0;
01743 return sip_debug_test_addr(sip_real_dst(p));
01744 }
01745
01746
01747 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
01748 {
01749 int res;
01750 const struct sockaddr_in *dst = sip_real_dst(p);
01751 res = sendto(sipsock, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
01752
01753 if (res != len)
01754 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));
01755 return res;
01756 }
01757
01758
01759
01760 static void build_via(struct sip_pvt *p)
01761 {
01762
01763 const char *rport = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_RFC3581 ? ";rport" : "";
01764
01765
01766 ast_string_field_build(p, via, "SIP/2.0/UDP %s:%d;branch=z9hG4bK%08x%s",
01767 ast_inet_ntoa(p->ourip), ourport, p->branch, rport);
01768 }
01769
01770
01771
01772
01773
01774
01775
01776 static enum sip_result ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us)
01777 {
01778 struct sockaddr_in theirs, ours;
01779
01780
01781 ast_ouraddrfor(them, us);
01782 theirs.sin_addr = *them;
01783 ours.sin_addr = *us;
01784
01785 if (localaddr && externip.sin_addr.s_addr &&
01786 (ast_apply_ha(localaddr, &theirs)) &&
01787 (!global_matchexterniplocally || !ast_apply_ha(localaddr, &ours))) {
01788 if (externexpire && time(NULL) >= externexpire) {
01789 struct ast_hostent ahp;
01790 struct hostent *hp;
01791
01792 externexpire = time(NULL) + externrefresh;
01793 if ((hp = ast_gethostbyname(externhost, &ahp))) {
01794 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
01795 } else
01796 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
01797 }
01798 *us = externip.sin_addr;
01799 if (option_debug) {
01800 ast_log(LOG_DEBUG, "Target address %s is not local, substituting externip\n",
01801 ast_inet_ntoa(*(struct in_addr *)&them->s_addr));
01802 }
01803 } else if (bindaddr.sin_addr.s_addr)
01804 *us = bindaddr.sin_addr;
01805 return AST_SUCCESS;
01806 }
01807
01808
01809
01810 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
01811
01812 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01813 __attribute__ ((format (printf, 2, 3)));
01814
01815
01816 static void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
01817 {
01818 char buf[80], *c = buf;
01819 struct sip_history *hist;
01820 int l;
01821
01822 vsnprintf(buf, sizeof(buf), fmt, ap);
01823 strsep(&c, "\r\n");
01824 l = strlen(buf) + 1;
01825 if (!(hist = ast_calloc(1, sizeof(*hist) + l)))
01826 return;
01827 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
01828 free(hist);
01829 return;
01830 }
01831 memcpy(hist->event, buf, l);
01832 AST_LIST_INSERT_TAIL(p->history, hist, list);
01833 }
01834
01835
01836 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
01837 {
01838 va_list ap;
01839
01840 if (!p)
01841 return;
01842 va_start(ap, fmt);
01843 append_history_va(p, fmt, ap);
01844 va_end(ap);
01845
01846 return;
01847 }
01848
01849
01850 static int retrans_pkt(void *data)
01851 {
01852 struct sip_pkt *pkt = data, *prev, *cur = NULL;
01853 int reschedule = DEFAULT_RETRANS;
01854
01855
01856 ast_mutex_lock(&pkt->owner->lock);
01857
01858 if (pkt->retrans < MAX_RETRANS) {
01859 pkt->retrans++;
01860 if (!pkt->timer_t1) {
01861 if (sipdebug && option_debug > 3)
01862 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);
01863 } else {
01864 int siptimer_a;
01865
01866 if (sipdebug && option_debug > 3)
01867 ast_log(LOG_DEBUG, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
01868 if (!pkt->timer_a)
01869 pkt->timer_a = 2 ;
01870 else
01871 pkt->timer_a = 2 * pkt->timer_a;
01872
01873
01874 siptimer_a = pkt->timer_t1 * pkt->timer_a;
01875 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
01876 siptimer_a = 4000;
01877
01878
01879 reschedule = siptimer_a;
01880 if (option_debug > 3)
01881 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);
01882 }
01883
01884 if (sip_debug_test_pvt(pkt->owner)) {
01885 const struct sockaddr_in *dst = sip_real_dst(pkt->owner);
01886 ast_verbose("Retransmitting #%d (%s) to %s:%d:\n%s\n---\n",
01887 pkt->retrans, sip_nat_mode(pkt->owner),
01888 ast_inet_ntoa(dst->sin_addr),
01889 ntohs(dst->sin_port), pkt->data);
01890 }
01891
01892 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data);
01893 __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
01894 ast_mutex_unlock(&pkt->owner->lock);
01895 return reschedule;
01896 }
01897
01898 if (pkt->owner && pkt->method != SIP_OPTIONS) {
01899 if (ast_test_flag(pkt, FLAG_FATAL) || sipdebug)
01900 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");
01901 } else {
01902 if ((pkt->method == SIP_OPTIONS) && sipdebug)
01903 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) \n", pkt->owner->callid);
01904 }
01905 append_history(pkt->owner, "MaxRetries", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
01906
01907 pkt->retransid = -1;
01908
01909 if (ast_test_flag(pkt, FLAG_FATAL)) {
01910 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
01911 ast_mutex_unlock(&pkt->owner->lock);
01912 usleep(1);
01913 ast_mutex_lock(&pkt->owner->lock);
01914 }
01915 if (pkt->owner->owner) {
01916 sip_alreadygone(pkt->owner);
01917 ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet.\n", pkt->owner->callid);
01918 ast_queue_hangup(pkt->owner->owner);
01919 ast_channel_unlock(pkt->owner->owner);
01920 } else {
01921
01922
01923
01924 if (pkt->method != SIP_OPTIONS)
01925 ast_set_flag(&pkt->owner->flags[0], SIP_NEEDDESTROY);
01926 }
01927 }
01928
01929 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
01930 if (cur == pkt)
01931 break;
01932 }
01933 if (cur) {
01934 if (prev)
01935 prev->next = cur->next;
01936 else
01937 pkt->owner->packets = cur->next;
01938 ast_mutex_unlock(&pkt->owner->lock);
01939 free(cur);
01940 pkt = NULL;
01941 } else
01942 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
01943 if (pkt)
01944 ast_mutex_unlock(&pkt->owner->lock);
01945 return 0;
01946 }
01947
01948
01949
01950
01951 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod)
01952 {
01953 struct sip_pkt *pkt;
01954 int siptimer_a = DEFAULT_RETRANS;
01955
01956 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1)))
01957 return AST_FAILURE;
01958 memcpy(pkt->data, data, len);
01959 pkt->method = sipmethod;
01960 pkt->packetlen = len;
01961 pkt->next = p->packets;
01962 pkt->owner = p;
01963 pkt->seqno = seqno;
01964 if (resp)
01965 ast_set_flag(pkt, FLAG_RESPONSE);
01966 pkt->data[len] = '\0';
01967 pkt->timer_t1 = p->timer_t1;
01968 if (fatal)
01969 ast_set_flag(pkt, FLAG_FATAL);
01970 if (pkt->timer_t1)
01971 siptimer_a = pkt->timer_t1 * 2;
01972
01973
01974 pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
01975 if (option_debug > 3 && sipdebug)
01976 ast_log(LOG_DEBUG, "*** SIP TIMER: Initalizing retransmit timer on packet: Id #%d\n", pkt->retransid);
01977 pkt->next = p->packets;
01978 p->packets = pkt;
01979
01980 __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
01981 if (sipmethod == SIP_INVITE) {
01982
01983 p->pendinginvite = seqno;
01984 }
01985 return AST_SUCCESS;
01986 }
01987
01988
01989 static int __sip_autodestruct(void *data)
01990 {
01991 struct sip_pvt *p = data;
01992
01993
01994 if (p->subscribed) {
01995 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
01996 p->subscribed = NONE;
01997 append_history(p, "Subscribestatus", "timeout");
01998 if (option_debug > 2)
01999 ast_log(LOG_DEBUG, "Re-scheduled destruction of SIP subsription %s\n", p->callid ? p->callid : "<unknown>");
02000 return 10000;
02001 }
02002
02003
02004 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
02005 ASTOBJ_UNREF(p->relatedpeer,sip_destroy_peer);
02006
02007
02008 p->autokillid = -1;
02009
02010 if (option_debug)
02011 ast_log(LOG_DEBUG, "Auto destroying SIP dialog '%s'\n", p->callid);
02012 append_history(p, "AutoDestroy", "%s", p->callid);
02013 if (p->owner) {
02014 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
02015 ast_queue_hangup(p->owner);
02016 } else if (p->refer) {
02017 if (option_debug > 2)
02018 ast_log(LOG_DEBUG, "Finally hanging up channel after transfer: %s\n", p->callid);
02019 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
02020 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
02021 } else
02022 sip_destroy(p);
02023 return 0;
02024 }
02025
02026
02027 static void sip_scheddestroy(struct sip_pvt *p, int ms)
02028 {
02029 if (ms < 0) {
02030 if (p->timer_t1 == 0)
02031 p->timer_t1 = 500;
02032 ms = p->timer_t1 * 64;
02033 }
02034 if (sip_debug_test_pvt(p))
02035 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
02036 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
02037 append_history(p, "SchedDestroy", "%d ms", ms);
02038
02039 if (p->autokillid > -1)
02040 ast_sched_del(sched, p->autokillid);
02041 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, p);
02042 }
02043
02044
02045 static void sip_cancel_destroy(struct sip_pvt *p)
02046 {
02047 if (p->autokillid > -1) {
02048 ast_sched_del(sched, p->autokillid);
02049 append_history(p, "CancelDestroy", "");
02050 p->autokillid = -1;
02051 }
02052 }
02053
02054
02055 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02056 {
02057 struct sip_pkt *cur, *prev = NULL;
02058
02059
02060 char *msg;
02061 int res = FALSE;
02062
02063 msg = sip_methods[sipmethod].text;
02064
02065 ast_mutex_lock(&p->lock);
02066 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
02067 if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
02068 ((ast_test_flag(cur, FLAG_RESPONSE)) ||
02069 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
02070 if (!resp && (seqno == p->pendinginvite)) {
02071 if (option_debug)
02072 ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
02073 p->pendinginvite = 0;
02074 }
02075
02076 res = TRUE;
02077 UNLINK(cur, p->packets, prev);
02078 if (cur->retransid > -1) {
02079 if (sipdebug && option_debug > 3)
02080 ast_log(LOG_DEBUG, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
02081 ast_sched_del(sched, cur->retransid);
02082 cur->retransid = -1;
02083 }
02084 free(cur);
02085 break;
02086 }
02087 }
02088 ast_mutex_unlock(&p->lock);
02089 if (option_debug)
02090 ast_log(LOG_DEBUG, "Stopping retransmission on '%s' of %s %d: Match %s\n", p->callid, resp ? "Response" : "Request", seqno, res ? "Not Found" : "Found");
02091 }
02092
02093
02094
02095 static void __sip_pretend_ack(struct sip_pvt *p)
02096 {
02097 struct sip_pkt *cur = NULL;
02098
02099 while (p->packets) {
02100 int method;
02101 if (cur == p->packets) {
02102 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
02103 return;
02104 }
02105 cur = p->packets;
02106 method = (cur->method) ? cur->method : find_sip_method(cur->data);
02107 __sip_ack(p, cur->seqno, ast_test_flag(cur, FLAG_RESPONSE), method);
02108 }
02109 }
02110
02111
02112 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
02113 {
02114 struct sip_pkt *cur;
02115 int res = -1;
02116
02117 for (cur = p->packets; cur; cur = cur->next) {
02118 if (cur->seqno == seqno && ast_test_flag(cur, FLAG_RESPONSE) == resp &&
02119 (ast_test_flag(cur, FLAG_RESPONSE) || method_match(sipmethod, cur->data))) {
02120
02121 if (cur->retransid > -1) {
02122 if (option_debug > 3 && sipdebug)
02123 ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
02124 ast_sched_del(sched, cur->retransid);
02125 cur->retransid = -1;
02126 }
02127 res = 0;
02128 break;
02129 }
02130 }
02131 if (option_debug)
02132 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");
02133 return res;
02134 }
02135
02136
02137
02138 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
02139 {
02140 memset(dst, 0, sizeof(*dst));
02141 memcpy(dst->data, src->data, sizeof(dst->data));
02142 dst->len = src->len;
02143 parse_request(dst);
02144 }
02145
02146
02147 static void add_blank(struct sip_request *req)
02148 {
02149 if (!req->lines) {
02150
02151 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
02152 req->len += strlen(req->data + req->len);
02153 }
02154 }
02155
02156
02157 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02158 {
02159 int res;
02160
02161 add_blank(req);
02162 if (sip_debug_test_pvt(p)) {
02163 const struct sockaddr_in *dst = sip_real_dst(p);
02164
02165 ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
02166 reliable ? "Reliably " : "", sip_nat_mode(p),
02167 ast_inet_ntoa(dst->sin_addr),
02168 ntohs(dst->sin_port), req->data);
02169 }
02170 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02171 struct sip_request tmp;
02172 parse_copy(&tmp, req);
02173 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"),
02174 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? tmp.rlPart2 : sip_methods[tmp.method].text);
02175 }
02176 res = (reliable) ?
02177 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
02178 __sip_xmit(p, req->data, req->len);
02179 if (res > 0)
02180 return 0;
02181 return res;
02182 }
02183
02184
02185 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
02186 {
02187 int res;
02188
02189 add_blank(req);
02190 if (sip_debug_test_pvt(p)) {
02191 if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
02192 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);
02193 else
02194 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);
02195 }
02196 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
02197 struct sip_request tmp;
02198 parse_copy(&tmp, req);
02199 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
02200 }
02201 res = (reliable) ?
02202 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable > 1), req->method) :
02203 __sip_xmit(p, req->data, req->len);
02204 return res;
02205 }
02206
02207
02208
02209
02210
02211 static const char *find_closing_quote(const char *start, const char *lim)
02212 {
02213 char last_char = '\0';
02214 const char *s;
02215 for (s = start; *s && s != lim; last_char = *s++) {
02216 if (*s == '"' && last_char != '\\')
02217 break;
02218 }
02219 return s;
02220 }
02221
02222
02223
02224
02225
02226
02227
02228
02229
02230
02231
02232
02233 static char *get_in_brackets(char *tmp)
02234 {
02235 const char *parse = tmp;
02236 char *first_bracket;
02237
02238
02239
02240
02241
02242 while ( (first_bracket = strchr(parse, '<')) ) {
02243 char *first_quote = strchr(parse, '"');
02244
02245 if (!first_quote || first_quote > first_bracket)
02246 break;
02247
02248 parse = find_closing_quote(first_quote + 1, NULL);
02249 if (!*parse) {
02250
02251 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
02252 break;
02253 }
02254 parse++;
02255 }
02256 if (first_bracket) {
02257 char *second_bracket = strchr(first_bracket + 1, '>');
02258 if (second_bracket) {
02259 *second_bracket = '\0';
02260 tmp = first_bracket + 1;
02261 } else {
02262 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
02263 }
02264 }
02265 return tmp;
02266 }
02267
02268
02269
02270 static int sip_sendtext(struct ast_channel *ast, const char *text)
02271 {
02272 struct sip_pvt *p = ast->tech_pvt;
02273 int debug = sip_debug_test_pvt(p);
02274
02275 if (debug)
02276 ast_verbose("Sending text %s on %s\n", text, ast->name);
02277 if (!p)
02278 return -1;
02279 if (ast_strlen_zero(text))
02280 return 0;
02281 if (debug)
02282 ast_verbose("Really sending text %s on %s\n", text, ast->name);
02283 transmit_message_with_text(p, text);
02284 return 0;
02285 }
02286
02287
02288
02289
02290
02291
02292 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey)
02293 {
02294 char port[10];
02295 char ipaddr[INET_ADDRSTRLEN];
02296 char regseconds[20];
02297
02298 char *sysname = ast_config_AST_SYSTEM_NAME;
02299 char *syslabel = NULL;
02300
02301 time_t nowtime = time(NULL) + expirey;
02302 const char *fc = fullcontact ? "fullcontact" : NULL;
02303
02304 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
02305 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
02306 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
02307
02308 if (ast_strlen_zero(sysname))
02309 sysname = NULL;
02310 else if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME))
02311 syslabel = "regserver";
02312
02313 if (fc)
02314 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02315 "port", port, "regseconds", regseconds,
02316 "username", username, fc, fullcontact, syslabel, sysname, NULL);
02317 else
02318 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
02319 "port", port, "regseconds", regseconds,
02320 "username", username, syslabel, sysname, NULL);
02321 }
02322
02323
02324 static void register_peer_exten(struct sip_peer *peer, int onoff)
02325 {
02326 char multi[256];
02327 char *stringp, *ext, *context;
02328
02329
02330
02331
02332
02333 if (ast_strlen_zero(global_regcontext))
02334 return;
02335
02336 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
02337 stringp = multi;
02338 while ((ext = strsep(&stringp, "&"))) {
02339 if ((context = strchr(ext, '@'))) {
02340 *context++ = '\0';
02341 if (!ast_context_find(context)) {
02342 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
02343 continue;
02344 }
02345 } else {
02346 context = global_regcontext;
02347 }
02348 if (onoff)
02349 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
02350 ast_strdup(peer->name), ast_free, "SIP");
02351 else
02352 ast_context_remove_extension(context, ext, 1, NULL);
02353 }
02354 }
02355
02356
02357 static void sip_destroy_peer(struct sip_peer *peer)
02358 {
02359 if (option_debug > 2)
02360 ast_log(LOG_DEBUG, "Destroying SIP peer %s\n", peer->name);
02361
02362
02363 if (peer->call)
02364 sip_destroy(peer->call);
02365
02366 if (peer->mwipvt)
02367 sip_destroy(peer->mwipvt);
02368
02369 if (peer->chanvars) {
02370 ast_variables_destroy(peer->chanvars);
02371 peer->chanvars = NULL;
02372 }
02373 if (peer->expire > -1)
02374 ast_sched_del(sched, peer->expire);
02375
02376 if (peer->pokeexpire > -1)
02377 ast_sched_del(sched, peer->pokeexpire);
02378 register_peer_exten(peer, FALSE);
02379 ast_free_ha(peer->ha);
02380 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT))
02381 apeerobjs--;
02382 else if (ast_test_flag(&peer->flags[0], SIP_REALTIME))
02383 rpeerobjs--;
02384 else
02385 speerobjs--;
02386 clear_realm_authentication(peer->auth);
02387 peer->auth = NULL;
02388 free(peer);
02389 }
02390
02391
02392 static void update_peer(struct sip_peer *p, int expiry)
02393 {
02394 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02395 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) &&
02396 (ast_test_flag(&p->flags[0], SIP_REALTIME) || rtcachefriends)) {
02397 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, expiry);
02398 }
02399 }
02400
02401
02402
02403
02404
02405
02406
02407 static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_in *sin)
02408 {
02409 struct sip_peer *peer;
02410 struct ast_variable *var = NULL;
02411 struct ast_variable *tmp;
02412 char ipaddr[INET_ADDRSTRLEN];
02413
02414
02415 if (newpeername)
02416 var = ast_load_realtime("sippeers", "name", newpeername, NULL);
02417 else if (sin) {
02418 ast_copy_string(ipaddr, ast_inet_ntoa(sin->sin_addr), sizeof(ipaddr));
02419 var = ast_load_realtime("sippeers", "host", ipaddr, NULL);
02420 if (!var)
02421 var = ast_load_realtime("sippeers", "ipaddr", ipaddr, NULL);
02422 }
02423
02424 if (!var)
02425 return NULL;
02426
02427 for (tmp = var; tmp; tmp = tmp->next) {
02428
02429 if (!strcasecmp(tmp->name, "type") &&
02430 !strcasecmp(tmp->value, "user")) {
02431 ast_variables_destroy(var);
02432 return NULL;
02433 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
02434 newpeername = tmp->value;
02435 }
02436 }
02437
02438 if (!newpeername) {
02439 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", ipaddr);
02440 ast_variables_destroy(var);
02441 return NULL;
02442 }
02443
02444
02445 peer = build_peer(newpeername, var, NULL, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
02446 if (!peer) {
02447 ast_variables_destroy(var);
02448 return NULL;
02449 }
02450
02451 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
02452
02453 ast_copy_flags(&peer->flags[1],&global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
02454 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
02455 if (peer->expire > -1) {
02456 ast_sched_del(sched, peer->expire);
02457 }
02458 peer->expire = ast_sched_add(sched, (global_rtautoclear) * 1000, expire_register, (void *)peer);
02459 }
02460 ASTOBJ_CONTAINER_LINK(&peerl,peer);
02461 } else {
02462 ast_set_flag(&peer->flags[0], SIP_REALTIME);
02463 }
02464 ast_variables_destroy(var);
02465
02466 return peer;
02467 }
02468
02469
02470 static int sip_addrcmp(char *name, struct sockaddr_in *sin)
02471 {
02472
02473 struct sip_peer *p = (struct sip_peer *) name;
02474 return !(!inaddrcmp(&p->addr, sin) ||
02475 (ast_test_flag(&p->flags[0], SIP_INSECURE_PORT) &&
02476 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
02477 }
02478
02479
02480
02481
02482 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
02483 {
02484 struct sip_peer *p = NULL;
02485
02486 if (peer)
02487 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
02488 else
02489 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, name, sip_addr_hashfunc, 1, sip_addrcmp);
02490
02491 if (!p && realtime)
02492 p = realtime_peer(peer, sin);
02493
02494 return p;
02495 }
02496
02497
02498 static void sip_destroy_user(struct sip_user *user)
02499 {
02500 if (option_debug > 2)
02501 ast_log(LOG_DEBUG, "Destroying user object from memory: %s\n", user->name);
02502 ast_free_ha(user->ha);
02503 if (user->chanvars) {
02504 ast_variables_destroy(user->chanvars);
02505 user->chanvars = NULL;
02506 }
02507 if (ast_test_flag(&user->flags[0], SIP_REALTIME))
02508 ruserobjs--;
02509 else
02510 suserobjs--;
02511 free(user);
02512 }
02513
02514
02515
02516
02517 static struct sip_user *realtime_user(const char *username)
02518 {
02519 struct ast_variable *var;
02520 struct ast_variable *tmp;
02521 struct sip_user *user = NULL;
02522
02523 var = ast_load_realtime("sipusers", "name", username, NULL);
02524
02525 if (!var)
02526 return NULL;
02527
02528 for (tmp = var; tmp; tmp = tmp->next) {
02529 if (!strcasecmp(tmp->name, "type") &&
02530 !strcasecmp(tmp->value, "peer")) {
02531 ast_variables_destroy(var);
02532 return NULL;
02533 }
02534 }
02535
02536 user = build_user(username, var, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
02537
02538 if (!user) {
02539 ast_variables_destroy(var);
02540 return NULL;
02541 }
02542
02543 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
02544 ast_set_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
02545 suserobjs++;
02546 ASTOBJ_CONTAINER_LINK(&userl,user);
02547 } else {
02548
02549 suserobjs--;
02550 ruserobjs++;
02551 ast_set_flag(&user->flags[0], SIP_REALTIME);
02552 }
02553 ast_variables_destroy(var);
02554 return user;
02555 }
02556
02557
02558
02559
02560
02561 static struct sip_user *find_user(const char *name, int realtime)
02562 {
02563 struct sip_user *u = ASTOBJ_CONTAINER_FIND(&userl, name);
02564 if (!u && realtime)
02565 u = realtime_user(name);
02566 return u;
02567 }
02568
02569
02570 static void do_setnat(struct sip_pvt *p, int natflags)
02571 {
02572 const char *mode = natflags ? "On" : "Off";
02573
02574 if (p->rtp) {
02575 if (option_debug)
02576 ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", mode);
02577 ast_rtp_setnat(p->rtp, natflags);
02578 }
02579 if (p->vrtp) {
02580 if (option_debug)
02581 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", mode);
02582 ast_rtp_setnat(p->vrtp, natflags);
02583 }
02584 if (p->udptl) {
02585 if (option_debug)
02586 ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", mode);
02587 ast_udptl_setnat(p->udptl, natflags);
02588 }
02589 }
02590
02591
02592
02593
02594 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
02595 {
02596 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
02597 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
02598 dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
02599 dialog->recv = dialog->sa;
02600 } else
02601 return -1;
02602
02603 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
02604 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
02605 dialog->capability = peer->capability;
02606 if ((!ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(dialog->capability & AST_FORMAT_VIDEO_MASK)) && dialog->vrtp) {
02607 ast_rtp_destroy(dialog->vrtp);
02608 dialog->vrtp = NULL;
02609 }
02610 dialog->prefs = peer->prefs;
02611 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
02612 dialog->t38.capability = global_t38_capability;
02613 if (dialog->udptl) {
02614 if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_FEC )
02615 dialog->t38.capability |= T38FAX_UDP_EC_FEC;
02616 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY )
02617 dialog->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
02618 else if (ast_udptl_get_error_correction_scheme(dialog->udptl) == UDPTL_ERROR_CORRECTION_NONE )
02619 dialog->t38.capability |= T38FAX_UDP_EC_NONE;
02620 dialog->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
02621 if (option_debug > 1)
02622 ast_log(LOG_DEBUG,"Our T38 capability (%d)\n", dialog->t38.capability);
02623 }
02624 dialog->t38.jointcapability = dialog->t38.capability;
02625 } else if (dialog->udptl) {
02626 ast_udptl_destroy(dialog->udptl);
02627 dialog->udptl = NULL;
02628 }
02629 do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE );
02630
02631 if (dialog->rtp) {
02632 ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
02633 ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
02634 ast_rtp_set_rtptimeout(dialog->rtp, peer->rtptimeout);
02635 ast_rtp_set_rtpholdtimeout(dialog->rtp, peer->rtpholdtimeout);
02636 ast_rtp_set_rtpkeepalive(dialog->rtp, peer->rtpkeepalive);
02637
02638 ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
02639 dialog->autoframing = peer->autoframing;
02640 }
02641 if (dialog->vrtp) {
02642 ast_rtp_setdtmf(dialog->vrtp, 0);
02643 ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
02644 ast_rtp_set_rtptimeout(dialog->vrtp, peer->rtptimeout);
02645 ast_rtp_set_rtpholdtimeout(dialog->vrtp, peer->rtpholdtimeout);
02646 ast_rtp_set_rtpkeepalive(dialog->vrtp, peer->rtpkeepalive);
02647 }
02648
02649 ast_string_field_set(dialog, peername, peer->username);
02650 ast_string_field_set(dialog, authname, peer->username);
02651 ast_string_field_set(dialog, username, peer->username);
02652 ast_string_field_set(dialog, peersecret, peer->secret);
02653 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
02654 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
02655 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
02656 ast_string_field_set(dialog, tohost, peer->tohost);
02657 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
02658 if (!dialog->initreq.headers && !ast_strlen_zero(peer->fromdomain)) {
02659 char *tmpcall;
02660 char *c;
02661 tmpcall = ast_strdupa(dialog->callid);
02662 c = strchr(tmpcall, '@');
02663 if (c) {
02664 *c = '\0';
02665 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
02666 }
02667 }
02668 if (ast_strlen_zero(dialog->tohost))
02669 ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
02670 if (!ast_strlen_zero(peer->fromdomain))
02671 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
02672 if (!ast_strlen_zero(peer->fromuser))
02673 ast_string_field_set(dialog, fromuser, peer->fromuser);
02674 dialog->maxtime = peer->maxms;
02675 dialog->callgroup = peer->callgroup;
02676 dialog->pickupgroup = peer->pickupgroup;
02677 dialog->allowtransfer = peer->allowtransfer;
02678
02679
02680 if (peer->maxms && peer->lastms)
02681 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
02682 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
02683 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
02684 dialog->noncodeccapability |= AST_RTP_DTMF;
02685 else
02686 dialog->noncodeccapability &= ~AST_RTP_DTMF;
02687 ast_string_field_set(dialog, context, peer->context);
02688 dialog->rtptimeout = peer->rtptimeout;
02689 if (peer->call_limit)
02690 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
02691 dialog->maxcallbitrate = peer->maxcallbitrate;
02692
02693 return 0;
02694 }
02695
02696
02697
02698
02699 static int create_addr(struct sip_pvt *dialog, const char *opeer)
02700 {
02701 struct hostent *hp;
02702 struct ast_hostent ahp;
02703 struct sip_peer *p;
02704 char *port;
02705 int portno;
02706 char host[MAXHOSTNAMELEN], *hostn;
02707 char peer[256];
02708
02709 ast_copy_string(peer, opeer, sizeof(peer));
02710 port = strchr(peer, ':');
02711 if (port)
02712 *port++ = '\0';
02713 dialog->sa.sin_family = AF_INET;
02714 dialog->timer_t1 = 500;
02715 p = find_peer(peer, NULL, 1);
02716
02717 if (p) {
02718 int res = create_addr_from_peer(dialog, p);
02719 ASTOBJ_UNREF(p, sip_destroy_peer);
02720 return res;
02721 }
02722 hostn = peer;
02723 portno = port ? atoi(port) : STANDARD_SIP_PORT;
02724 if (srvlookup) {
02725 char service[MAXHOSTNAMELEN];
02726 int tportno;
02727 int ret;
02728
02729 snprintf(service, sizeof(service), "_sip._udp.%s", peer);
02730 ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
02731 if (ret > 0) {
02732 hostn = host;
02733 portno = tportno;
02734 }
02735 }
02736 hp = ast_gethostbyname(hostn, &ahp);
02737 if (!hp) {
02738 ast_log(LOG_WARNING, "No such host: %s\n", peer);
02739 return -1;
02740 }
02741 ast_string_field_set(dialog, tohost, peer);
02742 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
02743 dialog->sa.sin_port = htons(portno);
02744 dialog->recv = dialog->sa;
02745 return 0;
02746 }
02747
02748
02749 static int auto_congest(void *nothing)
02750 {
02751 struct sip_pvt *p = nothing;
02752
02753 ast_mutex_lock(&p->lock);
02754 p->initid = -1;
02755 if (p->owner) {
02756
02757 if (!ast_channel_trylock(p->owner)) {
02758 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
02759 append_history(p, "Cong", "Auto-congesting (timer)");
02760 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
02761 ast_channel_unlock(p->owner);
02762 }
02763 }
02764 ast_mutex_unlock(&p->lock);
02765 return 0;
02766 }
02767
02768
02769
02770
02771 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
02772 {
02773 int res;
02774 struct sip_pvt *p;
02775 struct varshead *headp;
02776 struct ast_var_t *current;
02777 const char *referer = NULL;
02778
02779 p = ast->tech_pvt;
02780 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
02781 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
02782 return -1;
02783 }
02784
02785
02786 headp=&ast->varshead;
02787 AST_LIST_TRAVERSE(headp,current,entries) {
02788
02789 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
02790 p->options->vxml_url = ast_var_value(current);
02791 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
02792 p->options->uri_options = ast_var_value(current);
02793 } else if (!p->options->distinctive_ring && !strcasecmp(ast_var_name(current), "ALERT_INFO")) {
02794
02795 p->options->distinctive_ring = ast_var_value(current);
02796 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
02797
02798 p->options->addsipheaders = 1;
02799 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
02800
02801 p->options->transfer = 1;
02802 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
02803
02804 referer = ast_var_value(current);
02805 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
02806
02807 p->options->replaces = ast_var_value(current);
02808 } else if (!strcasecmp(ast_var_name(current), "T38CALL")) {
02809 p->t38.state = T38_LOCAL_DIRECT;
02810 if (option_debug)
02811 ast_log(LOG_DEBUG,"T38State change to %d on channel %s\n", p->t38.state, ast->name);
02812 }
02813
02814 }
02815
02816 res = 0;
02817 ast_set_flag(&p->flags[0], SIP_OUTGOING);
02818
02819 if (p->options->transfer) {
02820 char buf[BUFSIZ/2];
02821
02822 if (referer) {
02823 if (sipdebug && option_debug > 2)
02824 ast_log(LOG_DEBUG, "Call for %s transfered by %s\n", p->username, referer);
02825 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
02826 } else
02827 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
02828 ast_string_field_set(p, cid_name, buf);
02829 }
02830 if (option_debug)
02831 ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
02832
02833 res = update_call_counter(p, INC_CALL_RINGING);
02834 if ( res != -1 ) {
02835 p->callingpres = ast->cid.cid_pres;
02836 p->jointcapability = ast_translate_available_formats(p->capability, p->prefcodec);
02837 p->jointnoncodeccapability = p->noncodeccapability;
02838
02839
02840 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
02841 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
02842 res = -1;
02843 } else {
02844 p->t38.jointcapability = p->t38.capability;
02845 if (option_debug > 1)
02846 ast_log(LOG_DEBUG,"Our T38 capability (%d), joint T38 capability (%d)\n", p->t38.capability, p->t38.jointcapability);
02847 transmit_invite(p, SIP_INVITE, 1, 2);
02848 p->invitestate = INV_CALLING;
02849
02850
02851 p->initid = ast_sched_add(sched, p->maxtime ? (p->maxtime * 4) : SIP_TRANS_TIMEOUT, auto_congest, p);
02852 }
02853 }
02854 return res;
02855 }
02856
02857
02858
02859 static void sip_registry_destroy(struct sip_registry *reg)
02860 {
02861
02862 if (option_debug > 2)
02863 ast_log(LOG_DEBUG, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
02864
02865 if (reg->call) {
02866
02867
02868 reg->call->registry = NULL;
02869 if (option_debug > 2)
02870 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
02871 sip_destroy(reg->call);
02872 }
02873 if (reg->expire > -1)
02874 ast_sched_del(sched, reg->expire);
02875 if (reg->timeout > -1)
02876 ast_sched_del(sched, reg->timeout);
02877 ast_string_field_free_pools(reg);
02878 regobjs--;
02879 free(reg);
02880
02881 }
02882
02883
02884 static void __sip_destroy(struct sip_pvt *p, int lockowner)
02885 {
02886 struct sip_pvt *cur, *prev = NULL;
02887 struct sip_pkt *cp;
02888
02889 if (sip_debug_test_pvt(p) || option_debug > 2)
02890 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
02891
02892 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT)) {
02893 update_call_counter(p, DEC_CALL_LIMIT);
02894 if (option_debug > 1)
02895 ast_log(LOG_DEBUG, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
02896 }
02897
02898
02899 if (p->relatedpeer && p->relatedpeer->mwipvt)
02900 p->relatedpeer->mwipvt = NULL;
02901
02902 if (dumphistory)
02903 sip_dump_history(p);
02904
02905 if (p->options)
02906 free(p->options);
02907
02908 if (p->stateid > -1)
02909 ast_extension_state_del(p->stateid, NULL);
02910 if (p->initid > -1)
02911 ast_sched_del(sched, p->initid);
02912 if (p->autokillid > -1)
02913 ast_sched_del(sched, p->autokillid);
02914
02915 if (p->rtp)
02916 ast_rtp_destroy(p->rtp);
02917 if (p->vrtp)
02918 ast_rtp_destroy(p->vrtp);
02919 if (p->udptl)
02920 ast_udptl_destroy(p->udptl);
02921 if (p->refer)
02922 free(p->refer);
02923 if (p->route) {
02924 free_old_route(p->route);
02925 p->route = NULL;
02926 }
02927 if (p->registry) {
02928 if (p->registry->call == p)
02929 p->registry->call = NULL;
02930 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
02931 }
02932
02933
02934 if (p->owner) {
02935 if (lockowner)
02936 ast_channel_lock(p->owner);
02937 if (option_debug)
02938 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
02939 p->owner->tech_pvt = NULL;
02940 if (lockowner)
02941 ast_channel_unlock(p->owner);
02942 }
02943
02944 if (p->history) {
02945 struct sip_history *hist;
02946 while( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) )
02947 free(hist);
02948 free(p->history);
02949 p->history = NULL;
02950 }
02951
02952 for (prev = NULL, cur = iflist; cur; prev = cur, cur = cur->next) {
02953 if (cur == p) {
02954 UNLINK(cur, iflist, prev);
02955 break;
02956 }
02957 }
02958 if (!cur) {
02959 ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
02960 return;
02961 }
02962
02963
02964 while((cp = p->packets)) {
02965 p->packets = p->packets->next;
02966 if (cp->retransid > -1)
02967 ast_sched_del(sched, cp->retransid);
02968 free(cp);
02969 }
02970 if (p->chanvars) {
02971 ast_variables_destroy(p->chanvars);
02972 p->chanvars = NULL;
02973 }
02974 ast_mutex_destroy(&p->lock);
02975
02976 ast_string_field_free_pools(p);
02977
02978 free(p);
02979 }
02980
02981
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992
02993
02994
02995 static int update_call_counter(struct sip_pvt *fup, int event)
02996 {
02997 char name[256];
02998 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
02999 int outgoing = ast_test_flag(&fup->flags[1], SIP_PAGE2_OUTGOING_CALL);
03000 struct sip_user *u = NULL;
03001 struct sip_peer *p = NULL;
03002
03003 if (option_debug > 2)
03004 ast_log(LOG_DEBUG, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
03005
03006
03007 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT))
03008 return 0;
03009
03010 ast_copy_string(name, fup->username, sizeof(name));
03011
03012
03013 if (global_limitonpeers == FALSE && !outgoing && (u = find_user(name, 1))) {
03014 inuse = &u->inUse;
03015 call_limit = &u->call_limit;
03016 inringing = NULL;
03017 } else if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, 1) ) ) {
03018 inuse = &p->inUse;
03019 call_limit = &p->call_limit;
03020 inringing = &p->inRinging;
03021 ast_copy_string(name, fup->peername, sizeof(name));
03022 }
03023 if (!p && !u) {
03024 if (option_debug > 1)
03025 ast_log(LOG_DEBUG, "%s is not a local device, no call limit\n", name);
03026 return 0;
03027 }
03028
03029 switch(event) {
03030
03031 case DEC_CALL_LIMIT:
03032 if ( *inuse > 0 ) {
03033 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
03034 (*inuse)--;
03035 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
03036 }
03037 } else {
03038 *inuse = 0;
03039 }
03040 if (inringing) {
03041 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03042 if (*inringing > 0)
03043 (*inringing)--;
03044 else
03045 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", fup->peername);
03046 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03047 }
03048 }
03049 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold)
03050 sip_peer_hold(fup, 0);
03051 if (option_debug > 1 || sipdebug) {
03052 ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
03053 }
03054 break;
03055
03056 case INC_CALL_RINGING:
03057 case INC_CALL_LIMIT:
03058 if (*call_limit > 0 ) {
03059 if (*inuse >= *call_limit) {
03060 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);
03061 if (u)
03062 ASTOBJ_UNREF(u, sip_destroy_user);
03063 else
03064 ASTOBJ_UNREF(p, sip_destroy_peer);
03065 return -1;
03066 }
03067 }
03068 if (inringing && (event == INC_CALL_RINGING)) {
03069 if (!ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03070 (*inringing)++;
03071 ast_set_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03072 }
03073 }
03074
03075 (*inuse)++;
03076 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
03077 if (option_debug > 1 || sipdebug) {
03078 ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
03079 }
03080 break;
03081
03082 case DEC_CALL_RINGING:
03083 if (inringing) {
03084 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
03085 if (*inringing > 0)
03086 (*inringing)--;
03087 else
03088 ast_log(LOG_WARNING, "Inringing for peer '%s' < 0?\n", p->name);
03089 ast_clear_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING);
03090 }
03091 }
03092 break;
03093
03094 default:
03095 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
03096 }
03097 if (p) {
03098 ast_device_state_changed("SIP/%s", p->name);
03099 ASTOBJ_UNREF(p, sip_destroy_peer);
03100 } else
03101 ASTOBJ_UNREF(u, sip_destroy_user);
03102 return 0;
03103 }
03104
03105
03106 static void sip_destroy(struct sip_pvt *p)
03107 {
03108 ast_mutex_lock(&iflock);
03109 if (option_debug > 2)
03110 ast_log(LOG_DEBUG, "Destroying SIP dialog %s\n", p->callid);
03111 __sip_destroy(p, 1);
03112 ast_mutex_unlock(&iflock);
03113 }
03114
03115
03116 static int hangup_sip2cause(int cause)
03117 {
03118
03119
03120 switch(cause) {
03121 case 401:
03122 return AST_CAUSE_CALL_REJECTED;
03123 case 403:
03124 return AST_CAUSE_CALL_REJECTED;
03125 case 404:
03126 return AST_CAUSE_UNALLOCATED;
03127 case 405:
03128 return AST_CAUSE_INTERWORKING;
03129 case 407:
03130 return AST_CAUSE_CALL_REJECTED;
03131 case 408:
03132 return AST_CAUSE_NO_USER_RESPONSE;
03133 case 409:
03134 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
03135 case 410:
03136 return AST_CAUSE_UNALLOCATED;
03137 case 411:
03138 return AST_CAUSE_INTERWORKING;
03139 case 413:
03140 return AST_CAUSE_INTERWORKING;
03141 case 414:
03142 return AST_CAUSE_INTERWORKING;
03143 case 415:
03144 return AST_CAUSE_INTERWORKING;
03145 case 420:
03146 return AST_CAUSE_NO_ROUTE_DESTINATION;
03147 case 480:
03148 return AST_CAUSE_NO_ANSWER;
03149 case 481:
03150 return AST_CAUSE_INTERWORKING;
03151 case 482:
03152 return AST_CAUSE_INTERWORKING;
03153 case 483:
03154 return AST_CAUSE_NO_ANSWER;
03155 case 484:
03156 return AST_CAUSE_INVALID_NUMBER_FORMAT;
03157 case 485:
03158 return AST_CAUSE_UNALLOCATED;
03159 case 486:
03160 return AST_CAUSE_BUSY;
03161 case 487:
03162 return AST_CAUSE_INTERWORKING;
03163 case 488:
03164 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03165 case 491:
03166 return AST_CAUSE_INTERWORKING;
03167 case 493:
03168 return AST_CAUSE_INTERWORKING;
03169 case 500:
03170 return AST_CAUSE_FAILURE;
03171 case 501:
03172 return AST_CAUSE_FACILITY_REJECTED;
03173 case 502:
03174 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
03175 case 503:
03176 return AST_CAUSE_CONGESTION;
03177 case 504:
03178 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
03179 case 505:
03180 return AST_CAUSE_INTERWORKING;
03181 case 600:
03182 return AST_CAUSE_USER_BUSY;
03183 case 603:
03184 return AST_CAUSE_CALL_REJECTED;
03185 case 604:
03186 return AST_CAUSE_UNALLOCATED;
03187 case 606:
03188 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03189 default:
03190 return AST_CAUSE_NORMAL;
03191 }
03192
03193 return 0;
03194 }
03195
03196
03197
03198
03199
03200
03201
03202
03203
03204
03205
03206
03207
03208
03209
03210
03211
03212
03213
03214
03215
03216
03217
03218
03219
03220
03221
03222
03223
03224
03225
03226
03227
03228 static const char *hangup_cause2sip(int cause)
03229 {
03230 switch (cause) {
03231 case AST_CAUSE_UNALLOCATED:
03232 case AST_CAUSE_NO_ROUTE_DESTINATION:
03233 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
03234 return "404 Not Found";
03235 case AST_CAUSE_CONGESTION:
03236 case AST_CAUSE_SWITCH_CONGESTION:
03237 return "503 Service Unavailable";
03238 case AST_CAUSE_NO_USER_RESPONSE:
03239 return "408 Request Timeout";
03240 case AST_CAUSE_NO_ANSWER:
03241 return "480 Temporarily unavailable";
03242 case AST_CAUSE_CALL_REJECTED:
03243 return "403 Forbidden";
03244 case AST_CAUSE_NUMBER_CHANGED:
03245 return "410 Gone";
03246 case AST_CAUSE_NORMAL_UNSPECIFIED:
03247 return "480 Temporarily unavailable";
03248 case AST_CAUSE_INVALID_NUMBER_FORMAT:
03249 return "484 Address incomplete";
03250 case AST_CAUSE_USER_BUSY:
03251 return "486 Busy here";
03252 case AST_CAUSE_FAILURE:
03253 return "500 Server internal failure";
03254 case AST_CAUSE_FACILITY_REJECTED:
03255 return "501 Not Implemented";
03256 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
03257 return "503 Service Unavailable";
03258
03259 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
03260 return "502 Bad Gateway";
03261 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
03262 return "488 Not Acceptable Here";
03263
03264 case AST_CAUSE_NOTDEFINED:
03265 default:
03266 if (option_debug)
03267 ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
03268 return NULL;
03269 }
03270
03271
03272 return 0;
03273 }
03274
03275
03276
03277
03278 static int sip_hangup(struct ast_channel *ast)
03279 {
03280 struct sip_pvt *p = ast->tech_pvt;
03281 int needcancel = FALSE;
03282 int needdestroy = 0;
03283 struct ast_channel *oldowner = ast;
03284
03285 if (!p) {
03286 if (option_debug)
03287 ast_log(LOG_DEBUG, "Asked to hangup channel that was not connected\n");
03288 return 0;
03289 }
03290
03291 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
03292 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT)) {
03293 if (option_debug && sipdebug)
03294 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03295 update_call_counter(p, DEC_CALL_LIMIT);
03296 }
03297 if (option_debug >3)
03298 ast_log(LOG_DEBUG, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
03299 if (p->autokillid > -1)
03300 sip_cancel_destroy(p);
03301 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03302 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
03303 ast_clear_flag(&p->flags[0], SIP_NEEDDESTROY);
03304 p->owner->tech_pvt = NULL;
03305 p->owner = NULL;
03306 return 0;
03307 }
03308 if (option_debug) {
03309 if (ast_test_flag(ast, AST_FLAG_ZOMBIE) && p->refer && option_debug)
03310 ast_log(LOG_DEBUG, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
03311 else {
03312 if (option_debug)
03313 ast_log(LOG_DEBUG, "Hangup call %s, SIP callid %s)\n", ast->name, p->callid);
03314 }
03315 }
03316 if (option_debug && ast_test_flag(ast, AST_FLAG_ZOMBIE))
03317 ast_log(LOG_DEBUG, "Hanging up zombie call. Be scared.\n");
03318
03319 ast_mutex_lock(&p->lock);
03320 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT)) {
03321 if (option_debug && sipdebug)
03322 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
03323 update_call_counter(p, DEC_CALL_LIMIT);
03324 }
03325
03326
03327 if (p->owner != ast) {
03328 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
03329 ast_mutex_unlock(&p->lock);
03330 return 0;
03331 }
03332
03333 if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING || (p->invitestate < INV_COMPLETED && ast->_state != AST_STATE_UP)) {
03334 needcancel = TRUE;
03335 if (option_debug > 3)
03336 ast_log(LOG_DEBUG, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
03337 }
03338
03339
03340 if (p->vad)
03341 ast_dsp_free(p->vad);
03342
03343 p->owner = NULL;
03344 ast->tech_pvt = NULL;
03345
03346 ast_module_unref(ast_module_info->self);
03347
03348
03349
03350
03351
03352
03353
03354 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE))
03355 needdestroy = 1;
03356 else if (p->invitestate != INV_CALLING)
03357 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03358
03359
03360 if (!ast_test_flag(&p->flags[0], SIP_ALREADYGONE) && !ast_strlen_zero(p->initreq.data)) {
03361 if (needcancel) {
03362 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03363
03364 __sip_pretend_ack(p);
03365
03366
03367 if (p->invitestate == INV_CALLING) {
03368
03369 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03370
03371 } else {
03372
03373 transmit_request(p, SIP_CANCEL, p->ocseq, XMIT_RELIABLE, FALSE);
03374
03375
03376 needdestroy = 0;
03377 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03378 }
03379 if ( p->initid != -1 ) {
03380
03381
03382 update_call_counter(p, INC_CALL_LIMIT);
03383 }
03384 } else {
03385 const char *res;
03386 if (ast->hangupcause && (res = hangup_cause2sip(ast->hangupcause)))
03387 transmit_response_reliable(p, res, &p->initreq);
03388 else
03389 transmit_response_reliable(p, "603 Declined", &p->initreq);
03390 }
03391 } else {
03392 if (!p->pendinginvite) {
03393 char *audioqos = "";
03394 char *videoqos = "";
03395 if (p->rtp)
03396 audioqos = ast_rtp_get_quality(p->rtp, NULL);
03397 if (p->vrtp)
03398 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
03399
03400 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03401
03402
03403 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
03404 if (p->rtp)
03405 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
03406 if (p->vrtp)
03407 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
03408 }
03409 if (p->rtp && oldowner)
03410 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", audioqos);
03411 if (p->vrtp && oldowner)
03412 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", videoqos);
03413 } else {
03414
03415
03416 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
03417 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
03418 sip_cancel_destroy(p);
03419 }
03420 }
03421 }
03422 if (needdestroy)
03423 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
03424 ast_mutex_unlock(&p->lock);
03425 return 0;
03426 }
03427
03428
03429 static void try_suggested_sip_codec(struct sip_pvt *p)
03430 {
03431 int fmt;
03432 const char *codec;
03433
03434 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
03435 if (!codec)
03436 return;
03437
03438 fmt = ast_getformatbyname(codec);
03439 if (fmt) {
03440 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
03441 if (p->jointcapability & fmt) {
03442 p->jointcapability &= fmt;
03443 p->capability &= fmt;
03444 } else
03445 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
03446 } else
03447 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
03448 return;
03449 }
03450
03451
03452
03453 static int sip_answer(struct ast_channel *ast)
03454 {
03455 int res = 0;
03456 struct sip_pvt *p = ast->tech_pvt;
03457
03458 ast_mutex_lock(&p->lock);
03459 if (ast->_state != AST_STATE_UP) {
03460 try_suggested_sip_codec(p);
03461
03462 ast_setstate(ast, AST_STATE_UP);
03463 if (option_debug)
03464 ast_log(LOG_DEBUG, "SIP answering channel: %s\n", ast->name);
03465 if (p->t38.state == T38_PEER_DIRECT) {
03466 p->t38.state = T38_ENABLED;
03467 if (option_debug > 1)
03468 ast_log(LOG_DEBUG,"T38State change to %d on channel %s\n", p->t38.state, ast->name);
03469 res = transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
03470 } else
03471 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
03472 }
03473 ast_mutex_unlock(&p->lock);
03474 return res;
03475 }
03476
03477
03478 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
03479 {
03480 struct sip_pvt *p = ast->tech_pvt;
03481 int res = 0;
03482
03483 switch (frame->frametype) {
03484 case AST_FRAME_VOICE:
03485 if (!(frame->subclass & ast->nativeformats)) {
03486 char s1[512], s2[512], s3[512];
03487 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %s(%d) read/write = %s(%d)/%s(%d)\n",
03488 frame->subclass,
03489 ast_getformatname_multiple(s1, sizeof(s1) - 1, ast->nativeformats & AST_FORMAT_AUDIO_MASK),
03490 ast->nativeformats & AST_FORMAT_AUDIO_MASK,
03491 ast_getformatname_multiple(s2, sizeof(s2) - 1, ast->readformat),
03492 ast->readformat,
03493 ast_getformatname_multiple(s3, sizeof(s3) - 1, ast->writeformat),
03494 ast->writeformat);
03495 ast_frame_dump(ast->name, frame, "<<");
03496 ast_backtrace();
03497 return 0;
03498 }
03499 if (p) {
03500 ast_mutex_lock(&p->lock);
03501 if (p->rtp) {
03502
03503 if ((ast->_state != AST_STATE_UP) &&
03504 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03505 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03506 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03507 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03508 }
03509 p->lastrtptx = time(NULL);
03510 res = ast_rtp_write(p->rtp, frame);
03511 }
03512 ast_mutex_unlock(&p->lock);
03513 }
03514 break;
03515 case AST_FRAME_VIDEO:
03516 if (p) {
03517 ast_mutex_lock(&p->lock);
03518 if (p->vrtp) {
03519
03520 if ((ast->_state != AST_STATE_UP) &&
03521 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03522 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03523 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03524 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03525 }
03526 p->lastrtptx = time(NULL);
03527 res = ast_rtp_write(p->vrtp, frame);
03528 }
03529 ast_mutex_unlock(&p->lock);
03530 }
03531 break;
03532 case AST_FRAME_IMAGE:
03533 return 0;
03534 break;
03535 case AST_FRAME_MODEM:
03536 if (p) {
03537 ast_mutex_lock(&p->lock);
03538
03539
03540
03541
03542 if (p->udptl && ast->_state == AST_STATE_UP)
03543 res = ast_udptl_write(p->udptl, frame);
03544 ast_mutex_unlock(&p->lock);
03545 }
03546 break;
03547 default:
03548 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
03549 return 0;
03550 }
03551
03552 return res;
03553 }
03554
03555
03556
03557 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03558 {
03559 int ret = -1;
03560 struct sip_pvt *p;
03561
03562 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE) && option_debug)
03563 ast_log(LOG_DEBUG, "New channel is zombie\n");
03564 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE) && option_debug)
03565 ast_log(LOG_DEBUG, "Old channel is zombie\n");
03566
03567 if (!newchan || !newchan->tech_pvt) {
03568 if (!newchan)
03569 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
03570 else
03571 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
03572 return -1;
03573 }
03574 p = newchan->tech_pvt;
03575
03576 if (!p) {
03577 ast_log(LOG_WARNING, "No pvt after masquerade. Strange things may happen\n");
03578 return -1;
03579 }
03580
03581 ast_mutex_lock(&p->lock);
03582 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
03583 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
03584 if (p->owner != oldchan)
03585 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
03586 else {
03587 p->owner = newchan;
03588 ret = 0;
03589 }
03590 if (option_debug > 2)
03591 ast_log(LOG_DEBUG, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
03592
03593 ast_mutex_unlock(&p->lock);
03594 return ret;
03595 }
03596
03597 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
03598 {
03599 struct sip_pvt *p = ast->tech_pvt;
03600 int res = 0;
03601
03602 ast_mutex_lock(&p->lock);
03603 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
03604 case SIP_DTMF_INBAND:
03605 res = -1;
03606 break;
03607 case SIP_DTMF_RFC2833:
03608 if (p->rtp)
03609 ast_rtp_senddigit_begin(p->rtp, digit);
03610 break;
03611 default:
03612 break;
03613 }
03614 ast_mutex_unlock(&p->lock);
03615
03616 return res;
03617 }
03618
03619
03620
03621 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
03622 {
03623 struct sip_pvt *p = ast->tech_pvt;
03624 int res = 0;
03625
03626 ast_mutex_lock(&p->lock);
03627 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
03628 case SIP_DTMF_INFO:
03629 transmit_info_with_digit(p, digit, duration);
03630 break;
03631 case SIP_DTMF_RFC2833:
03632 if (p->rtp)
03633 ast_rtp_senddigit_end(p->rtp, digit);
03634 break;
03635 case SIP_DTMF_INBAND:
03636 res = -1;
03637 break;
03638 }
03639 ast_mutex_unlock(&p->lock);
03640
03641 return res;
03642 }
03643
03644
03645 static int sip_transfer(struct ast_channel *ast, const char *dest)
03646 {
03647 struct sip_pvt *p = ast->tech_pvt;
03648 int res;
03649
03650 if (dest == NULL)
03651 dest = "";
03652 ast_mutex_lock(&p->lock);
03653 if (ast->_state == AST_STATE_RING)
03654 res = sip_sipredirect(p, dest);
03655 else
03656 res = transmit_refer(p, dest);
03657 ast_mutex_unlock(&p->lock);
03658 return res;
03659 }
03660
03661
03662
03663
03664
03665
03666 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
03667 {
03668 struct sip_pvt *p = ast->tech_pvt;
03669 int res = 0;
03670
03671 ast_mutex_lock(&p->lock);
03672 switch(condition) {
03673 case AST_CONTROL_RINGING:
03674 if (ast->_state == AST_STATE_RING) {
03675 p->invitestate = INV_EARLY_MEDIA;
03676 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
03677 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
03678
03679 transmit_response(p, "180 Ringing", &p->initreq);
03680 ast_set_flag(&p->flags[0], SIP_RINGING);
03681 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
03682 break;
03683 } else {
03684
03685 }
03686 }
03687 res = -1;
03688 break;
03689 case AST_CONTROL_BUSY:
03690 if (ast->_state != AST_STATE_UP) {
03691 transmit_response(p, "486 Busy Here", &p->initreq);
03692 p->invitestate = INV_COMPLETED;
03693 sip_alreadygone(p);
03694 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
03695 break;
03696 }
03697 res = -1;
03698 break;
03699 case AST_CONTROL_CONGESTION:
03700 if (ast->_state != AST_STATE_UP) {
03701 transmit_response(p, "503 Service Unavailable", &p->initreq);
03702 p->invitestate = INV_COMPLETED;
03703 sip_alreadygone(p);
03704 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
03705 break;
03706 }
03707 res = -1;
03708 break;
03709 case AST_CONTROL_PROCEEDING:
03710 if ((ast->_state != AST_STATE_UP) &&
03711 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03712 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03713 transmit_response(p, "100 Trying", &p->initreq);
03714 p->invitestate = INV_PROCEEDING;
03715 break;
03716 }
03717 res = -1;
03718 break;
03719 case AST_CONTROL_PROGRESS:
03720 if ((ast->_state != AST_STATE_UP) &&
03721 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
03722 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
03723 p->invitestate = INV_EARLY_MEDIA;
03724 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
03725 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
03726 break;
03727 }
03728 res = -1;
03729 break;
03730 case AST_CONTROL_HOLD:
03731 ast_moh_start(ast, data, p->mohinterpret);
03732 break;
03733 case AST_CONTROL_UNHOLD:
03734 ast_moh_stop(ast);
03735 break;
03736 case AST_CONTROL_VIDUPDATE:
03737 if (p->vrtp && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
03738 transmit_info_with_vidupdate(p);
03739
03740 } else
03741 res = -1;
03742 break;
03743 case -1:
03744 res = -1;
03745 break;
03746 default:
03747 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
03748 res = -1;
03749 break;
03750 }
03751 ast_mutex_unlock(&p->lock);
03752 return res;
03753 }
03754
03755
03756
03757
03758
03759
03760
03761
03762 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
03763 {
03764 struct ast_channel *tmp;
03765 struct ast_variable *v = NULL;
03766 int fmt;
03767 int what;
03768 int needvideo = 0;
03769 {
03770 const char *my_name;
03771
03772 if (title)
03773 my_name = title;
03774 else if ( (my_name = strchr(i->fromdomain,':')) )
03775 my_name++;
03776 else
03777 my_name = i->fromdomain;
03778
03779 ast_mutex_unlock(&i->lock);
03780
03781 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);
03782
03783 }
03784 if (!tmp) {
03785 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
03786 return NULL;
03787 }
03788 ast_mutex_lock(&i->lock);
03789
03790 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO)
03791 tmp->tech = &sip_tech_info;
03792 else
03793 tmp->tech = &sip_tech;
03794
03795
03796
03797 if (i->jointcapability)
03798 what = i->jointcapability;
03799 else if (i->capability)
03800 what = i->capability;
03801 else
03802 what = global_capability;
03803
03804
03805 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
03806 if (option_debug > 2) {
03807 char buf[BUFSIZ];
03808 ast_log(LOG_DEBUG, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, BUFSIZ, tmp->nativeformats));
03809 ast_log(LOG_DEBUG, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, BUFSIZ, i->jointcapability));
03810 ast_log(LOG_DEBUG, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, BUFSIZ, i->capability));
03811 ast_log(LOG_DEBUG, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, BUFSIZ, ast_codec_choose(&i->prefs, what, 1)));
03812 if (i->prefcodec)
03813 ast_log(LOG_DEBUG, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, BUFSIZ, i->prefcodec));
03814 }
03815
03816
03817 fmt = ast_best_codec(tmp->nativeformats);
03818
03819
03820
03821
03822
03823 if (i->vrtp) {
03824 if (i->prefcodec)
03825 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
03826 else
03827 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
03828 }
03829
03830 if (option_debug > 2) {
03831 if (needvideo)
03832 ast_log(LOG_DEBUG, "This channel can handle video! HOLLYWOOD next!\n");
03833 else
03834 ast_log(LOG_DEBUG, "This channel will not be able to handle video.\n");
03835 }
03836
03837
03838
03839 if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
03840 i->vad = ast_dsp_new();
03841 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
03842 if (global_relaxdtmf)
03843 ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
03844 }
03845 if (i->rtp) {
03846 tmp->fds[0] = ast_rtp_fd(i->rtp);
03847 tmp->fds[1] = ast_rtcp_fd(i->rtp);
03848 }
03849 if (needvideo && i->vrtp) {
03850 tmp->fds[2] = ast_rtp_fd(i->vrtp);
03851 tmp->fds[3] = ast_rtcp_fd(i->vrtp);
03852 }
03853 if (i->udptl) {
03854 tmp->fds[5] = ast_udptl_fd(i->udptl);
03855 }
03856 if (state == AST_STATE_RING)
03857 tmp->rings = 1;
03858 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
03859 tmp->writeformat = fmt;
03860 tmp->rawwriteformat = fmt;
03861 tmp->readformat = fmt;
03862 tmp->rawreadformat = fmt;
03863 tmp->tech_pvt = i;
03864
03865 tmp->callgroup = i->callgroup;
03866 tmp->pickupgroup = i->pickupgroup;
03867 tmp->cid.cid_pres = i->callingpres;
03868 if (!ast_strlen_zero(i->accountcode))
03869 ast_string_field_set(tmp, accountcode, i->accountcode);
03870 if (i->amaflags)
03871 tmp->amaflags = i->amaflags;
03872 if (!ast_strlen_zero(i->language))
03873 ast_string_field_set(tmp, language, i->language);
03874 i->owner = tmp;
03875 ast_module_ref(ast_module_info->self);
03876 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
03877 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
03878
03879
03880
03881
03882 tmp->cid.cid_num = ast_strdup(i->cid_num);
03883 tmp->cid.cid_ani = ast_strdup(i->cid_num);
03884 tmp->cid.cid_name = ast_strdup(i->cid_name);
03885 if (!ast_strlen_zero(i->rdnis))
03886 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
03887
03888 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
03889 tmp->cid.cid_dnid = ast_strdup(i->exten);
03890
03891 tmp->priority = 1;
03892 if (!ast_strlen_zero(i->uri))
03893 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
03894 if (!ast_strlen_zero(i->domain))
03895 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
03896 if (!ast_strlen_zero(i->useragent))
03897 pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
03898 if (!ast_strlen_zero(i->callid))
03899 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
03900 if (i->rtp)
03901 ast_jb_configure(tmp, &global_jbconf);
03902 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
03903 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
03904 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
03905 ast_hangup(tmp);
03906 tmp = NULL;
03907 }
03908
03909 for (v = i->chanvars ; v ; v = v->next)
03910 pbx_builtin_setvar_helper(tmp,v->name,v->value);
03911
03912 if (!ast_test_flag(&i->flags[0], SIP_NO_HISTORY))
03913 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
03914
03915 return tmp;
03916 }
03917
03918
03919 static char *get_body_by_line(const char *line, const char *name, int nameLen)
03920 {
03921 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=')
03922 return ast_skip_blanks(line + nameLen + 1);
03923
03924 return "";
03925 }
03926
03927
03928
03929
03930
03931 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
03932 {
03933 int len = strlen(name);
03934
03935 while (*start < req->sdp_end) {
03936 const char *r = get_body_by_line(req->line[(*start)++], name, len);
03937 if (r[0] != '\0')
03938 return r;
03939 }
03940
03941 return "";
03942 }
03943
03944
03945 static const char *get_sdp(struct sip_request *req, const char *name)
03946 {
03947 int dummy = 0;
03948
03949 return get_sdp_iterate(&dummy, req, name);
03950 }
03951
03952
03953 static char *get_body(struct sip_request *req, char *name)
03954 {
03955 int x;
03956 int len = strlen(name);
03957 char *r;
03958
03959 for (x = 0; x < req->lines; x++) {
03960 r = get_body_by_line(req->line[x], name, len);
03961 if (r[0] != '\0')
03962 return r;
03963 }
03964
03965 return "";
03966 }
03967
03968
03969 static const char *find_alias(const char *name, const char *_default)
03970 {
03971
03972 static const struct cfalias {
03973 char * const fullname;
03974 char * const shortname;
03975 } aliases[] = {
03976 { "Content-Type", "c" },
03977 { "Content-Encoding", "e" },
03978 { "From", "f" },
03979 { "Call-ID", "i" },
03980 { "Contact", "m" },
03981 { "Content-Length", "l" },
03982 { "Subject", "s" },
03983 { "To", "t" },
03984 { "Supported", "k" },
03985 { "Refer-To", "r" },
03986 { "Referred-By", "b" },
03987 { "Allow-Events", "u" },
03988 { "Event", "o" },
03989 { "Via", "v" },
03990 { "Accept-Contact", "a" },
03991 { "Reject-Contact", "j" },
03992 { "Request-Disposition", "d" },
03993 { "Session-Expires", "x" },
03994 { "Identity", "y" },
03995 { "Identity-Info", "n" },
03996 };
03997 int x;
03998
03999 for (x=0; x<sizeof(aliases) / sizeof(aliases[0]); x++)
04000 if (!strcasecmp(aliases[x].fullname, name))
04001 return aliases[x].shortname;
04002
04003 return _default;
04004 }
04005
04006 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
04007 {
04008 int pass;
04009
04010
04011
04012
04013
04014
04015
04016
04017
04018
04019 for (pass = 0; name && pass < 2;pass++) {
04020 int x, len = strlen(name);
04021 for (x=*start; x<req->headers; x++) {
04022 if (!strncasecmp(req->header[x], name, len)) {
04023 char *r = req->header[x] + len;
04024 if (pedanticsipchecking)
04025 r = ast_skip_blanks(r);
04026
04027 if (*r == ':') {
04028 *start = x+1;
04029 return ast_skip_blanks(r+1);
04030 }
04031 }
04032 }
04033 if (pass == 0)
04034 name = find_alias(name, NULL);
04035 }
04036
04037
04038 return "";
04039 }
04040
04041
04042 static const char *get_header(const struct sip_request *req, const char *name)
04043 {
04044 int start = 0;
04045 return __get_header(req, name, &start);
04046 }
04047
04048
04049 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
04050 {
04051
04052 struct ast_frame *f;
04053
04054 if (!p->rtp) {
04055
04056 return &ast_null_frame;
04057 }
04058
04059 switch(ast->fdno) {
04060 case 0:
04061 f = ast_rtp_read(p->rtp);
04062 break;
04063 case 1:
04064 f = ast_rtcp_read(p->rtp);
04065 break;
04066 case 2:
04067 f = ast_rtp_read(p->vrtp);
04068 break;
04069 case 3:
04070 f = ast_rtcp_read(p->vrtp);
04071 break;
04072 case 5:
04073 f = ast_udptl_read(p->udptl);
04074 break;
04075 default:
04076 f = &ast_null_frame;
04077 }
04078
04079 if (f && (f->frametype == AST_FRAME_DTMF) &&
04080 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833))
04081 return &ast_null_frame;
04082
04083
04084 if (!p->owner || f->frametype != AST_FRAME_VOICE)
04085 return f;
04086
04087 if (f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
04088 if (!(f->subclass & p->jointcapability)) {
04089 if (option_debug) {
04090 ast_log(LOG_DEBUG, "Bogus frame of format '%s' received from '%s'!\n",
04091 ast_getformatname(f->subclass), p->owner->name);
04092 }
04093 return &ast_null_frame;
04094 }
04095 if (option_debug)
04096 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
04097 p->owner->nativeformats = (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
04098 ast_set_read_format(p->owner, p->owner->readformat);
04099 ast_set_write_format(p->owner, p->owner->writeformat);
04100 }
04101
04102 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
04103 f = ast_dsp_process(p->owner, p->vad, f);
04104 if (f && f->frametype == AST_FRAME_DTMF) {
04105 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && f->subclass == 'f') {
04106 if (option_debug)
04107 ast_log(LOG_DEBUG, "Fax CNG detected on %s\n", ast->name);
04108 *faxdetect = 1;
04109 } else if (option_debug) {
04110 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
04111 }
04112 }
04113 }
04114
04115 return f;
04116 }
04117
04118
04119 static struct ast_frame *sip_read(struct ast_channel *ast)
04120 {
04121 struct ast_frame *fr;
04122 struct sip_pvt *p;
04123
04124 if( ast == NULL )
04125 return NULL;
04126
04127 p = ast->tech_pvt;
04128 int faxdetected = FALSE;
04129
04130 if( p == NULL )
04131 return NULL;
04132
04133 ast_mutex_lock(&p->lock);
04134 fr = sip_rtp_read(ast, p, &faxdetected);
04135 p->lastrtprx = time(NULL);
04136
04137
04138
04139 if (faxdetected && ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_UDPTL) && (p->t38.state == T38_DISABLED) && !(ast_bridged_channel(ast))) {
04140 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
04141 if (!p->pendinginvite) {
04142 if (option_debug > 2)
04143 ast_log(LOG_DEBUG, "Sending reinvite on SIP (%s) for T.38 negotiation.\n",ast->name);
04144 p->t38.state = T38_LOCAL_REINVITE;
04145 transmit_reinvite_with_t38_sdp(p);
04146 if (option_debug > 1)
04147 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, ast->name);
04148 }
04149 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04150 if (option_debug > 2)
04151 ast_log(LOG_DEBUG, "Deferring reinvite on SIP (%s) - it will be re-negotiated for T.38\n", ast->name);
04152 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04153 }
04154 }
04155
04156 ast_mutex_unlock(&p->lock);
04157 return fr;
04158 }
04159
04160
04161
04162 static char *generate_random_string(char *buf, size_t size)
04163 {
04164 long val[4];
04165 int x;
04166
04167 for (x=0; x<4; x++)
04168 val[x] = ast_random();
04169 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
04170
04171 return buf;
04172 }
04173
04174
04175 static void build_callid_pvt(struct sip_pvt *pvt)
04176 {
04177 char buf[33];
04178
04179 const char *host = S_OR(pvt->fromdomain, ast_inet_ntoa(pvt->ourip));
04180
04181 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04182
04183 }
04184
04185
04186 static void build_callid_registry(struct sip_registry *reg, struct in_addr ourip, const char *fromdomain)
04187 {
04188 char buf[33];
04189
04190 const char *host = S_OR(fromdomain, ast_inet_ntoa(ourip));
04191
04192 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
04193 }
04194
04195
04196 static void make_our_tag(char *tagbuf, size_t len)
04197 {
04198 snprintf(tagbuf, len, "as%08lx", ast_random());
04199 }
04200
04201
04202 static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
04203 int useglobal_nat, const int intended_method)
04204 {
04205 struct sip_pvt *p;
04206
04207 if (!(p = ast_calloc(1, sizeof(*p))))
04208 return NULL;
04209
04210 if (ast_string_field_init(p, 512)) {
04211 free(p);
04212 return NULL;
04213 }
04214
04215 ast_mutex_init(&p->lock);
04216
04217 p->method = intended_method;
04218 p->initid = -1;
04219 p->autokillid = -1;
04220 p->subscribed = NONE;
04221 p->stateid = -1;
04222 p->prefs = default_prefs;
04223
04224 if (intended_method != SIP_OPTIONS)
04225 p->timer_t1 = 500;
04226
04227 if (sin) {
04228 p->sa = *sin;
04229 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
04230 p->ourip = __ourip;
04231 } else
04232 p->ourip = __ourip;
04233
04234
04235 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
04236 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
04237
04238 ast_set2_flag(&p->flags[0], !recordhistory, SIP_NO_HISTORY);
04239
04240 p->branch = ast_random();
04241 make_our_tag(p->tag, sizeof(p->tag));
04242 p->ocseq = INITIAL_CSEQ;
04243
04244 if (sip_methods[intended_method].need_rtp) {
04245 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04246
04247 if (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT))
04248 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
04249 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT))
04250 p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, bindaddr.sin_addr);
04251 if (!p->rtp || (ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) && !p->vrtp)) {
04252 ast_log(LOG_WARNING, "Unable to create RTP audio %s session: %s\n",
04253 ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "and video" : "", strerror(errno));
04254 ast_mutex_destroy(&p->lock);
04255 if (p->chanvars) {
04256 ast_variables_destroy(p->chanvars);
04257 p->chanvars = NULL;
04258 }
04259 free(p);
04260 return NULL;
04261 }
04262 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04263 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04264 ast_rtp_settos(p->rtp, global_tos_audio);
04265 ast_rtp_set_rtptimeout(p->rtp, global_rtptimeout);
04266 ast_rtp_set_rtpholdtimeout(p->rtp, global_rtpholdtimeout);
04267 ast_rtp_set_rtpkeepalive(p->rtp, global_rtpkeepalive);
04268 if (p->vrtp) {
04269 ast_rtp_settos(p->vrtp, global_tos_video);
04270 ast_rtp_setdtmf(p->vrtp, 0);
04271 ast_rtp_setdtmfcompensate(p->vrtp, 0);
04272 ast_rtp_set_rtptimeout(p->vrtp, global_rtptimeout);
04273 ast_rtp_set_rtpholdtimeout(p->vrtp, global_rtpholdtimeout);
04274 ast_rtp_set_rtpkeepalive(p->vrtp, global_rtpkeepalive);
04275 }
04276 if (p->udptl)
04277 ast_udptl_settos(p->udptl, global_tos_audio);
04278 p->maxcallbitrate = default_maxcallbitrate;
04279 }
04280
04281 if (useglobal_nat && sin) {
04282
04283 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
04284 p->recv = *sin;
04285 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
04286 }
04287
04288 if (p->method != SIP_REGISTER)
04289 ast_string_field_set(p, fromdomain, default_fromdomain);
04290 build_via(p);
04291 if (!callid)
04292 build_callid_pvt(p);
04293 else
04294 ast_string_field_set(p, callid, callid);
04295
04296 ast_string_field_set(p, mohinterpret, default_mohinterpret);
04297 ast_string_field_set(p, mohsuggest, default_mohsuggest);
04298 p->capability = global_capability;
04299 p->allowtransfer = global_allowtransfer;
04300 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
04301 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
04302 p->noncodeccapability |= AST_RTP_DTMF;
04303 if (p->udptl) {
04304 p->t38.capability = global_t38_capability;
04305 if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_REDUNDANCY)
04306 p->t38.capability |= T38FAX_UDP_EC_REDUNDANCY;
04307 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_FEC)
04308 p->t38.capability |= T38FAX_UDP_EC_FEC;
04309 else if (ast_udptl_get_error_correction_scheme(p->udptl) == UDPTL_ERROR_CORRECTION_NONE)
04310 p->t38.capability |= T38FAX_UDP_EC_NONE;
04311 p->t38.capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
04312 p->t38.jointcapability = p->t38.capability;
04313 }
04314 ast_string_field_set(p, context, default_context);
04315
04316
04317 ast_mutex_lock(&iflock);
04318 p->next = iflist;
04319 iflist = p;
04320 ast_mutex_unlock(&iflock);
04321 if (option_debug)
04322 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");
04323 return p;
04324 }
04325
04326
04327
04328 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
04329 {
04330 struct sip_pvt *p = NULL;
04331 char *tag = "";
04332 char totag[128];
04333 char fromtag[128];
04334 const char *callid = get_header(req, "Call-ID");
04335 const char *from = get_header(req, "From");
04336 const char *to = get_header(req, "To");
04337 const char *cseq = get_header(req, "Cseq");
04338
04339
04340
04341 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
04342 ast_strlen_zero(from) || ast_strlen_zero(cseq))
04343 return NULL;
04344
04345 if (pedanticsipchecking) {
04346
04347
04348
04349
04350
04351
04352 if (gettag(req, "To", totag, sizeof(totag)))
04353 ast_set_flag(req, SIP_PKT_WITH_TOTAG);
04354 gettag(req, "From", fromtag, sizeof(fromtag));
04355
04356 tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
04357
04358 if (option_debug > 4 )
04359 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);
04360 }
04361
04362 ast_mutex_lock(&iflock);
04363 for (p = iflist; p; p = p->next) {
04364
04365 int found = FALSE;
04366 if (ast_strlen_zero(p->callid))
04367 continue;
04368 if (req->method == SIP_REGISTER)
04369 found = (!strcmp(p->callid, callid));
04370 else
04371 found = (!strcmp(p->callid, callid) &&
04372 (!pedanticsipchecking || !tag || ast_strlen_zero(p->theirtag) || !strcmp(p->theirtag, tag))) ;
04373
04374 if (option_debug > 4)
04375 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);
04376
04377
04378 if (pedanticsipchecking && found && req->method != SIP_RESPONSE) {
04379 if (p->tag[0] == '\0' && totag[0]) {
04380
04381 found = FALSE;
04382 } else if (totag[0]) {
04383 if (strcmp(totag, p->tag)) {
04384 found = FALSE;
04385 }
04386 }
04387 if (!found && option_debug > 4)
04388 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);
04389 }
04390
04391
04392 if (found) {
04393
04394 ast_mutex_unlock(&iflock);
04395 ast_mutex_lock(&p->lock);
04396 return p;
04397 }
04398 }
04399 ast_mutex_unlock(&iflock);
04400
04401
04402 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
04403 if (intended_method == SIP_REFER) {
04404
04405 transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
04406 } else if (intended_method == SIP_NOTIFY) {
04407
04408
04409 transmit_response_using_temp(callid, sin, 1, intended_method, req, "489 Bad event");
04410 } else {
04411
04412 if ((p = sip_alloc(callid, sin, 1, intended_method))) {
04413
04414 ast_mutex_lock(&p->lock);
04415 } else {
04416
04417
04418
04419
04420
04421
04422
04423
04424 transmit_response_using_temp(callid, sin, 1, intended_method, req, "500 Server internal error");
04425 if (option_debug > 3)
04426 ast_log(LOG_DEBUG, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
04427 }
04428 }
04429 return p;
04430 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
04431
04432 transmit_response_using_temp(callid, sin, 1, intended_method, req, "501 Method Not Implemented");
04433 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
04434
04435
04436
04437 transmit_response_using_temp(callid, sin, 1, intended_method, req, "481 Call leg/transaction does not exist");
04438 }
04439
04440
04441
04442 return p;
04443 }
04444
04445
04446 static int sip_register(char *value, int lineno)
04447 {
04448 struct sip_registry *reg;
04449 int portnum = 0;
04450 char username[256] = "";
04451 char *hostname=NULL, *secret=NULL, *authuser=NULL;
04452 char *porta=NULL;
04453 char *contact=NULL;
04454
04455 if (!value)
04456 return -1;
04457 ast_copy_string(username, value, sizeof(username));
04458
04459 hostname = strrchr(username, '@');
04460 if (hostname)
04461 *hostname++ = '\0';
04462 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
04463 ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
04464 return -1;
04465 }
04466
04467 secret = strchr(username, ':');
04468 if (secret) {
04469 *secret++ = '\0';
04470 authuser = strchr(secret, ':');
04471 if (authuser)
04472 *authuser++ = '\0';
04473 }
04474
04475 contact = strchr(hostname, '/');
04476 if (contact)
04477 *contact++ = '\0';
04478 if (ast_strlen_zero(contact))
04479 contact = "s";
04480 porta = strchr(hostname, ':');
04481 if (porta) {
04482 *porta++ = '\0';
04483 portnum = atoi(porta);
04484 if (portnum == 0) {
04485 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
04486 return -1;
04487 }
04488 }
04489 if (!(reg = ast_calloc(1, sizeof(*reg)))) {
04490 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
04491 return -1;
04492 }
04493
04494 if (ast_string_field_init(reg, 256)) {
04495 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry strings\n");
04496 free(reg);
04497 return -1;
04498 }
04499
04500 regobjs++;
04501 ASTOBJ_INIT(reg);
04502 ast_string_field_set(reg, contact, contact);
04503 if (username)
04504 ast_string_field_set(reg, username, username);
04505 if (hostname)
04506 ast_string_field_set(reg, hostname, hostname);
04507 if (authuser)
04508 ast_string_field_set(reg, authuser, authuser);
04509 if (secret)
04510 ast_string_field_set(reg, secret, secret);
04511 reg->expire = -1;
04512 reg->timeout = -1;
04513 reg->refresh = default_expiry;
04514 reg->portno = portnum;
04515 reg->callid_valid = FALSE;
04516 reg->ocseq = INITIAL_CSEQ;
04517 ASTOBJ_CONTAINER_LINK(®l, reg);
04518 ASTOBJ_UNREF(reg,sip_registry_destroy);
04519 return 0;
04520 }
04521
04522
04523
04524 static int lws2sws(char *msgbuf, int len)
04525 {
04526 int h = 0, t = 0;
04527 int lws = 0;
04528
04529 for (; h < len;) {
04530
04531 if (msgbuf[h] == '\r') {
04532 h++;
04533 continue;
04534 }
04535
04536 if (msgbuf[h] == '\n') {
04537
04538 if (h + 1 == len)
04539 break;
04540
04541 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
04542
04543 h++;
04544 continue;
04545 }
04546
04547 msgbuf[t++] = msgbuf[h++];
04548 lws = 0;
04549 continue;
04550 }
04551 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
04552 if (lws) {
04553 h++;
04554 continue;
04555 }
04556 msgbuf[t++] = msgbuf[h++];
04557 lws = 1;
04558 continue;
04559 }
04560 msgbuf[t++] = msgbuf[h++];
04561 if (lws)
04562 lws = 0;
04563 }
04564 msgbuf[t] = '\0';
04565 return t;
04566 }
04567
04568
04569
04570
04571 static void parse_request(struct sip_request *req)
04572 {
04573
04574 char *c;
04575 int f = 0;
04576
04577 c = req->data;
04578
04579
04580 req->header[f] = c;
04581 while(*c) {
04582 if (*c == '\n') {
04583
04584 *c = 0;
04585
04586 if (sipdebug && option_debug > 3)
04587 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
04588 if (ast_strlen_zero(req->header[f])) {
04589
04590 c++;
04591 break;
04592 }
04593 if (f >= SIP_MAX_HEADERS - 1) {
04594 ast_log(LOG_WARNING, "Too many SIP headers. Ignoring.\n");
04595 } else
04596 f++;
04597 req->header[f] = c + 1;
04598 } else if (*c == '\r') {
04599
04600 *c = 0;
04601 }
04602 c++;
04603 }
04604
04605 if (!ast_strlen_zero(req->header[f])) {
04606 if (sipdebug && option_debug > 3)
04607 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
04608 f++;
04609 }
04610 req->headers = f;
04611
04612 f = 0;
04613 req->line[f] = c;
04614 while(*c) {
04615 if (*c == '\n') {
04616
04617 *c = 0;
04618 if (sipdebug && option_debug > 3)
04619 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
04620 if (f >= SIP_MAX_LINES - 1) {
04621 ast_log(LOG_WARNING, "Too many SDP lines. Ignoring.\n");
04622 } else
04623 f++;
04624 req->line[f] = c + 1;
04625 } else if (*c == '\r') {
04626
04627 *c = 0;
04628 }
04629 c++;
04630 }
04631
04632 if (!ast_strlen_zero(req->line[f]))
04633 f++;
04634 req->lines = f;
04635 if (*c)
04636 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
04637
04638 determine_firstline_parts(req);
04639 }
04640
04641
04642
04643
04644
04645
04646
04647
04648
04649 static int find_sdp(struct sip_request *req)
04650 {
04651 const char *content_type;
04652 const char *search;
04653 char *boundary;
04654 unsigned int x;
04655 int boundaryisquoted = FALSE;
04656
04657 content_type = get_header(req, "Content-Type");
04658
04659
04660 if (!strcasecmp(content_type, "application/sdp")) {
04661 req->sdp_start = 0;
04662 req->sdp_end = req->lines;
04663 return 1;
04664 }
04665
04666
04667 if (strncasecmp(content_type, "multipart/mixed", 15))
04668 return 0;
04669
04670
04671 if (!(search = strcasestr(content_type, ";boundary=")))
04672 return 0;
04673
04674 search += 10;
04675 if (ast_strlen_zero(search))
04676 return 0;
04677
04678
04679 if (*search == '\"') {
04680 search++;
04681 boundaryisquoted = TRUE;
04682 }
04683
04684
04685
04686 boundary = ast_strdupa(search - 2);
04687 boundary[0] = boundary[1] = '-';
04688
04689
04690 if (boundaryisquoted)
04691 boundary[strlen(boundary) - 1] = '\0';
04692
04693
04694
04695
04696 for (x = 0; x < (req->lines - 2); x++) {
04697 if (!strncasecmp(req->line[x], boundary, strlen(boundary)) &&
04698 !strcasecmp(req->line[x + 1], "Content-Type: application/sdp")) {
04699 x += 2;
04700 req->sdp_start = x;
04701
04702
04703 for ( ; x < req->lines; x++) {
04704 if (!strncasecmp(req->line[x], boundary, strlen(boundary)))
04705 break;
04706 }
04707 req->sdp_end = x;
04708 return 1;
04709 }
04710 }
04711
04712 return 0;
04713 }
04714
04715
04716
04717
04718
04719
04720 static int process_sdp(struct sip_pvt *p, struct sip_request *req)
04721 {
04722 const char *m;
04723 const char *c;
04724 const char *a;
04725 char host[258];
04726 int len = -1;
04727 int portno = -1;
04728 int vportno = -1;
04729 int udptlportno = -1;
04730 int peert38capability = 0;
04731 char s[256];
04732 int old = 0;
04733
04734
04735 int peercapability = 0, peernoncodeccapability = 0;
04736 int vpeercapability = 0, vpeernoncodeccapability = 0;
04737 struct sockaddr_in sin;
04738 struct sockaddr_in vsin;
04739
04740 const char *codecs;
04741 struct hostent *hp;
04742 struct hostent *vhp = NULL;
04743 struct ast_hostent audiohp;
04744 struct ast_hostent videohp;
04745 int codec;
04746 int destiterator = 0;
04747 int iterator;
04748 int sendonly = -1;
04749 int numberofports;
04750 struct ast_rtp *newaudiortp, *newvideortp;
04751 int newjointcapability;
04752 int newpeercapability;
04753 int newnoncodeccapability;
04754 int numberofmediastreams = 0;
04755 int debug = sip_debug_test_pvt(p);
04756
04757 int found_rtpmap_codecs[32];
04758 int last_rtpmap_codec=0;
04759
04760 if (!p->rtp) {
04761 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
04762 return -1;
04763 }
04764
04765
04766 newaudiortp = alloca(ast_rtp_alloc_size());
04767 memset(newaudiortp, 0, ast_rtp_alloc_size());
04768 ast_rtp_new_init(newaudiortp);
04769 ast_rtp_pt_clear(newaudiortp);
04770
04771 newvideortp = alloca(ast_rtp_alloc_size());
04772 memset(newvideortp, 0, ast_rtp_alloc_size());
04773 ast_rtp_new_init(newvideortp);
04774 ast_rtp_pt_clear(newvideortp);
04775
04776
04777 p->lastrtprx = p->lastrtptx = time(NULL);
04778
04779
04780
04781 m = get_sdp(req, "m");
04782 destiterator = req->sdp_start;
04783 c = get_sdp_iterate(&destiterator, req, "c");
04784 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
04785 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
04786 return -1;
04787 }
04788
04789
04790 if (sscanf(c, "IN IP4 %256s", host) != 1) {
04791 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
04792 return -1;
04793 }
04794
04795
04796 hp = ast_gethostbyname(host, &audiohp);
04797 if (!hp) {
04798 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
04799 return -1;
04800 }
04801 vhp = hp;
04802
04803 iterator = req->sdp_start;
04804 ast_set_flag(&p->flags[0], SIP_NOVIDEO);
04805
04806
04807
04808 while ((m = get_sdp_iterate(&iterator, req, "m"))[0] != '\0') {
04809 int x;
04810 int audio = FALSE;
04811
04812 numberofports = 1;
04813 if ((sscanf(m, "audio %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2) ||
04814 (sscanf(m, "audio %d RTP/AVP %n", &x, &len) == 1)) {
04815 audio = TRUE;
04816 numberofmediastreams++;
04817
04818 portno = x;
04819
04820 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
04821 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
04822 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
04823 return -1;
04824 }
04825 if (debug)
04826 ast_verbose("Found RTP audio format %d\n", codec);
04827 ast_rtp_set_m_type(newaudiortp, codec);
04828 }
04829 } else if ((sscanf(m, "video %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2) ||
04830 (sscanf(m, "video %d RTP/AVP %n", &x, &len) == 1)) {
04831
04832 ast_clear_flag(&p->flags[0], SIP_NOVIDEO);
04833 numberofmediastreams++;
04834 vportno = x;
04835
04836 for (codecs = m + len; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
04837 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
04838 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
04839 return -1;
04840 }
04841 if (debug)
04842 ast_verbose("Found RTP video format %d\n", codec);
04843 ast_rtp_set_m_type(newvideortp, codec);
04844 }
04845 } else if (p->udptl && ( (sscanf(m, "image %d udptl t38%n", &x, &len) == 1) ||
04846 (sscanf(m, "image %d UDPTL t38%n", &x, &len) == 1) )) {
04847 if (debug)
04848 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
04849 udptlportno = x;
04850 numberofmediastreams++;
04851
04852 if (p->owner && p->lastinvite) {
04853 p->t38.state = T38_PEER_REINVITE;
04854 if (option_debug > 1)
04855 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>" );
04856 } else {
04857 p->t38.state = T38_PEER_DIRECT;
04858 if (option_debug > 1)
04859 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
04860 }
04861 } else
04862 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
04863 if (numberofports > 1)
04864 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
04865
04866
04867
04868 c = get_sdp_iterate(&destiterator, req, "c");
04869 if (!ast_strlen_zero(c)) {
04870 if (sscanf(c, "IN IP4 %256s", host) != 1) {
04871 ast_log(LOG_WARNING, "Invalid secondary host in c= line, '%s'\n", c);
04872 } else {
04873
04874 if (audio) {
04875 if ( !(hp = ast_gethostbyname(host, &audiohp))) {
04876 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in secondary c= line, '%s'\n", c);
04877 return -2;
04878 }
04879 } else if (!(vhp = ast_gethostbyname(host, &videohp))) {
04880 ast_log(LOG_WARNING, "Unable to lookup RTP video host in secondary c= line, '%s'\n", c);
04881 return -2;
04882 }
04883 }
04884
04885 }
04886 }
04887 if (portno == -1 && vportno == -1 && udptlportno == -1)
04888
04889
04890 return -2;
04891
04892 if (numberofmediastreams > 2)
04893
04894 return -3;
04895
04896
04897 sin.sin_family = AF_INET;
04898 vsin.sin_family = AF_INET;
04899 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
04900 if (vhp)
04901 memcpy(&vsin.sin_addr, vhp->h_addr, sizeof(vsin.sin_addr));
04902
04903
04904 if (p->udptl) {
04905 if (udptlportno > 0) {
04906 sin.sin_port = htons(udptlportno);
04907 ast_udptl_set_peer(p->udptl, &sin);
04908 if (debug)
04909 ast_log(LOG_DEBUG,"Peer T.38 UDPTL is at port %s:%d\n",ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
04910 } else {
04911 ast_udptl_stop(p->udptl);
04912 if (debug)
04913 ast_log(LOG_DEBUG, "Peer doesn't provide T.38 UDPTL\n");
04914 }
04915 }
04916
04917
04918 if (p->rtp) {
04919 if (portno > 0) {
04920 sin.sin_port = htons(portno);
04921 ast_rtp_set_peer(p->rtp, &sin);
04922 if (debug)
04923 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
04924 } else {
04925 if (udptlportno > 0) {
04926 if (debug)
04927 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session. Callid %s\n", p->callid);
04928 } else {
04929 ast_rtp_stop(p->rtp);
04930 if (debug)
04931 ast_verbose("Peer doesn't provide audio. Callid %s\n", p->callid);
04932 }
04933 }
04934 }
04935
04936 if (vportno != -1)
04937 vsin.sin_port = htons(vportno);
04938
04939
04940
04941
04942
04943 iterator = req->sdp_start;
04944 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
04945 char* mimeSubtype = ast_strdupa(a);
04946 if (option_debug > 1) {
04947 int breakout = FALSE;
04948
04949
04950 if (!strncasecmp(a, "rtcp:", (size_t) 5)) {
04951 if (debug)
04952 ast_verbose("Got unsupported a:rtcp in SDP offer \n");
04953 breakout = TRUE;
04954 } else if (!strncasecmp(a, "fmtp:", (size_t) 5)) {
04955
04956
04957
04958
04959 if (debug)
04960 ast_verbose("Got unsupported a:fmtp in SDP offer \n");
04961 breakout = TRUE;
04962 } else if (!strncasecmp(a, "framerate:", (size_t) 10)) {
04963
04964 if (debug)
04965 ast_verbose("Got unsupported a:framerate in SDP offer \n");
04966 breakout = TRUE;
04967 } else if (!strncasecmp(a, "maxprate:", (size_t) 9)) {
04968
04969 if (debug)
04970 ast_verbose("Got unsupported a:maxprate in SDP offer \n");
04971 breakout = TRUE;
04972 } else if (!strncasecmp(a, "crypto:", (size_t) 7)) {
04973
04974 if (debug)
04975 ast_verbose("Got unsupported a:crypto in SDP offer \n");
04976 breakout = TRUE;
04977 }
04978 if (breakout)
04979 continue;
04980 }
04981 if (!strcasecmp(a, "sendonly")) {
04982 if (sendonly == -1)
04983 sendonly = 1;
04984 continue;
04985 } else if (!strcasecmp(a, "inactive")) {
04986 if (sendonly == -1)
04987 sendonly = 2;
04988 continue;
04989 } else if (!strcasecmp(a, "sendrecv")) {
04990 if (sendonly == -1)
04991 sendonly = 0;
04992 continue;
04993 } else if (strlen(a) > 5 && !strncasecmp(a, "ptime", 5)) {
04994 char *tmp = strrchr(a, ':');
04995 long int framing = 0;
04996 if (tmp) {
04997 tmp++;
04998 framing = strtol(tmp, NULL, 10);
04999 if (framing == LONG_MIN || framing == LONG_MAX) {
05000 framing = 0;
05001 if (option_debug)
05002 ast_log(LOG_DEBUG, "Can't read framing from SDP: %s\n", a);
05003 }
05004 }
05005 if (framing && last_rtpmap_codec) {
05006 if (p->autoframing) {
05007 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
05008 int codec_n;
05009 int format = 0;
05010 for (codec_n = 0; codec_n < last_rtpmap_codec; codec_n++) {
05011 format = ast_rtp_codec_getformat(found_rtpmap_codecs[codec_n]);
05012 if (!format)
05013 continue;
05014 if (option_debug)
05015 ast_log(LOG_DEBUG, "Setting framing for %d to %ld\n", format, framing);
05016 ast_codec_pref_setsize(pref, format, framing);
05017 }
05018 ast_rtp_codec_setpref(p->rtp, pref);
05019 }
05020 }
05021 memset(&found_rtpmap_codecs, 0, sizeof(found_rtpmap_codecs));
05022 last_rtpmap_codec = 0;
05023 continue;
05024 } else if (sscanf(a, "rtpmap: %u %[^/]/", &codec, mimeSubtype) == 2) {
05025
05026 if (debug)
05027 ast_verbose("Found description format %s for ID %d\n", mimeSubtype, codec);
05028 found_rtpmap_codecs[last_rtpmap_codec] = codec;
05029 last_rtpmap_codec++;
05030
05031
05032 ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
05033 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0);
05034 if (p->vrtp)
05035 ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0);
05036 }
05037 }
05038
05039 if (udptlportno != -1) {
05040 int found = 0, x;
05041
05042 old = 0;
05043
05044
05045 iterator = req->sdp_start;
05046 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
05047 if ((sscanf(a, "T38FaxMaxBuffer:%d", &x) == 1)) {
05048 found = 1;
05049 if (option_debug > 2)
05050 ast_log(LOG_DEBUG, "MaxBufferSize:%d\n",x);
05051 } else if ((sscanf(a, "T38MaxBitRate:%d", &x) == 1)) {
05052 found = 1;
05053 if (option_debug > 2)
05054 ast_log(LOG_DEBUG,"T38MaxBitRate: %d\n",x);
05055 switch (x) {
05056 case 14400:
05057 peert38capability |= T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05058 break;
05059 case 12000:
05060 peert38capability |= T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05061 break;
05062 case 9600:
05063 peert38capability |= T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05064 break;
05065 case 7200:
05066 peert38capability |= T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400;
05067 break;
05068 case 4800:
05069 peert38capability |= T38FAX_RATE_4800 | T38FAX_RATE_2400;
05070 break;
05071 case 2400:
05072 peert38capability |= T38FAX_RATE_2400;
05073 break;
05074 }
05075 } else if ((sscanf(a, "T38FaxVersion:%d", &x) == 1)) {
05076 found = 1;
05077 if (option_debug > 2)
05078 ast_log(LOG_DEBUG, "FaxVersion: %d\n",x);
05079 if (x == 0)
05080 peert38capability |= T38FAX_VERSION_0;
05081 else if (x == 1)
05082 peert38capability |= T38FAX_VERSION_1;
05083 } else if ((sscanf(a, "T38FaxMaxDatagram:%d", &x) == 1)) {
05084 found = 1;
05085 if (option_debug > 2)
05086 ast_log(LOG_DEBUG, "FaxMaxDatagram: %d\n",x);
05087 ast_udptl_set_far_max_datagram(p->udptl, x);
05088 ast_udptl_set_local_max_datagram(p->udptl, x);
05089 } else if ((sscanf(a, "T38FaxFillBitRemoval:%d", &x) == 1)) {
05090 found = 1;
05091 if (option_debug > 2)
05092 ast_log(LOG_DEBUG, "FillBitRemoval: %d\n",x);
05093 if (x == 1)
05094 peert38capability |= T38FAX_FILL_BIT_REMOVAL;
05095 } else if ((sscanf(a, "T38FaxTranscodingMMR:%d", &x) == 1)) {
05096 found = 1;
05097 if (option_debug > 2)
05098 ast_log(LOG_DEBUG, "Transcoding MMR: %d\n",x);
05099 if (x == 1)
05100 peert38capability |= T38FAX_TRANSCODING_MMR;
05101 }
05102 if ((sscanf(a, "T38FaxTranscodingJBIG:%d", &x) == 1)) {
05103 found = 1;
05104 if (option_debug > 2)
05105 ast_log(LOG_DEBUG, "Transcoding JBIG: %d\n",x);
05106 if (x == 1)
05107 peert38capability |= T38FAX_TRANSCODING_JBIG;
05108 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
05109 found = 1;
05110 if (option_debug > 2)
05111 ast_log(LOG_DEBUG, "RateManagement: %s\n", s);
05112 if (!strcasecmp(s, "localTCF"))
05113 peert38capability |= T38FAX_RATE_MANAGEMENT_LOCAL_TCF;
05114 else if (!strcasecmp(s, "transferredTCF"))
05115 peert38capability |= T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF;
05116 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
05117 found = 1;
05118 if (option_debug > 2)
05119 ast_log(LOG_DEBUG, "UDP EC: %s\n", s);
05120 if (!strcasecmp(s, "t38UDPRedundancy")) {
05121 peert38capability |= T38FAX_UDP_EC_REDUNDANCY;
05122 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
05123 } else if (!strcasecmp(s, "t38UDPFEC")) {
05124 peert38capability |= T38FAX_UDP_EC_FEC;
05125 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
05126 } else {
05127 peert38capability |= T38FAX_UDP_EC_NONE;
05128 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
05129 }
05130 }
05131 }
05132 if (found) {
05133 p->t38.peercapability = peert38capability;
05134 p->t38.jointcapability = (peert38capability & 255);
05135 peert38capability &= (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400);
05136 p->t38.jointcapability |= (peert38capability & p->t38.capability);
05137 }
05138 if (debug)
05139 ast_log(LOG_DEBUG, "Our T38 capability = (%d), peer T38 capability (%d), joint T38 capability (%d)\n",
05140 p->t38.capability,
05141 p->t38.peercapability,
05142 p->t38.jointcapability);
05143 } else {
05144 p->t38.state = T38_DISABLED;
05145 if (option_debug > 2)
05146 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
05147 }
05148
05149
05150 ast_rtp_get_current_formats(newaudiortp, &peercapability, &peernoncodeccapability);
05151 ast_rtp_get_current_formats(newvideortp, &vpeercapability, &vpeernoncodeccapability);
05152
05153 newjointcapability = p->capability & (peercapability | vpeercapability);
05154 newpeercapability = (peercapability | vpeercapability);
05155 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
05156
05157
05158 if (debug) {
05159
05160 char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ], s4[BUFSIZ];
05161
05162 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s, combined - %s\n",
05163 ast_getformatname_multiple(s1, BUFSIZ, p->capability),
05164 ast_getformatname_multiple(s2, BUFSIZ, newpeercapability),
05165 ast_getformatname_multiple(s3, BUFSIZ, vpeercapability),
05166 ast_getformatname_multiple(s4, BUFSIZ, newjointcapability));
05167
05168 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
05169 ast_rtp_lookup_mime_multiple(s1, BUFSIZ, p->noncodeccapability, 0, 0),
05170 ast_rtp_lookup_mime_multiple(s2, BUFSIZ, peernoncodeccapability, 0, 0),
05171 ast_rtp_lookup_mime_multiple(s3, BUFSIZ, newnoncodeccapability, 0, 0));
05172 }
05173 if (!newjointcapability) {
05174
05175 if (!p->t38.jointcapability) {
05176 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
05177
05178 return -1;
05179 } else {
05180 if (option_debug > 2)
05181 ast_log(LOG_DEBUG, "Have T.38 but no audio codecs, accepting offer anyway\n");
05182 return 0;
05183 }
05184 }
05185
05186
05187
05188 p->jointcapability = newjointcapability;
05189 p->peercapability = newpeercapability;
05190 p->jointnoncodeccapability = newnoncodeccapability;
05191
05192 ast_rtp_pt_copy(p->rtp, newaudiortp);
05193 if (p->vrtp)
05194 ast_rtp_pt_copy(p->vrtp, newvideortp);
05195
05196 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
05197 ast_clear_flag(&p->flags[0], SIP_DTMF);
05198 if (newnoncodeccapability & AST_RTP_DTMF) {
05199
05200 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
05201
05202 ast_rtp_setdtmf(p->rtp, 1);
05203 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
05204 } else {
05205 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
05206 }
05207 }
05208
05209
05210 if (p->rtp && sin.sin_port) {
05211 ast_rtp_set_peer(p->rtp, &sin);
05212 if (debug)
05213 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
05214 }
05215
05216
05217 if (p->vrtp && vsin.sin_port) {
05218 ast_rtp_set_peer(p->vrtp, &vsin);
05219 if (debug)
05220 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(vsin.sin_addr), ntohs(vsin.sin_port));
05221 }
05222
05223
05224 if (option_debug > 1) {
05225 char buf[BUFSIZ];
05226 ast_log(LOG_DEBUG, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, BUFSIZ, p->jointcapability));
05227 }
05228
05229 if (!p->owner)
05230 return 0;
05231
05232 if (option_debug > 3)
05233 ast_log(LOG_DEBUG, "We have an owner, now see if we need to change this call\n");
05234
05235 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
05236 if (debug) {
05237 char s1[BUFSIZ], s2[BUFSIZ];
05238 ast_log(LOG_DEBUG, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
05239 ast_getformatname_multiple(s1, BUFSIZ, p->jointcapability),
05240 ast_getformatname_multiple(s2, BUFSIZ, p->owner->nativeformats));
05241 }
05242 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability);
05243 ast_set_read_format(p->owner, p->owner->readformat);
05244 ast_set_write_format(p->owner, p->owner->writeformat);
05245 }
05246
05247 if (sin.sin_addr.s_addr && (!sendonly || sendonly == -1)) {
05248 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
05249
05250 ast_queue_frame(p->owner, &ast_null_frame);
05251 } else if (!sin.sin_addr.s_addr || sendonly) {
05252 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
05253 S_OR(p->mohsuggest, NULL),
05254 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
05255 if (sendonly)
05256 ast_rtp_stop(p->rtp);
05257
05258
05259 ast_queue_frame(p->owner, &ast_null_frame);
05260 }
05261
05262
05263 if (sin.sin_addr.s_addr && (!sendonly || sendonly == -1)) {
05264 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05265 append_history(p, "Unhold", "%s", req->data);
05266 if (global_callevents)
05267 manager_event(EVENT_FLAG_CALL, "Unhold",
05268 "Channel: %s\r\n"
05269 "Uniqueid: %s\r\n",
05270 p->owner->name,
05271 p->owner->uniqueid);
05272 if (global_notifyhold)
05273 sip_peer_hold(p, 0);
05274 }
05275 ast_clear_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD);
05276 } else if (!sin.sin_addr.s_addr || sendonly ) {
05277
05278 append_history(p, "Hold", "%s", req->data);
05279
05280 if (global_callevents && !ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05281 manager_event(EVENT_FLAG_CALL, "Hold",
05282 "Channel: %s\r\n"
05283 "Uniqueid: %s\r\n",
05284 p->owner->name,
05285 p->owner->uniqueid);
05286 }
05287 if (sendonly == 1)
05288 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
05289 else if (sendonly == 2)
05290 ast_set_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
05291 if (global_notifyhold)
05292 sip_peer_hold(p, 1);
05293 }
05294
05295 return 0;
05296 }
05297
05298
05299
05300 static int add_header(struct sip_request *req, const char *var, const char *value)
05301 {
05302 int maxlen = sizeof(req->data) - 4 - req->len;
05303
05304 if (req->headers == SIP_MAX_HEADERS) {
05305 ast_log(LOG_WARNING, "Out of SIP header space\n");
05306 return -1;
05307 }
05308
05309 if (req->lines) {
05310 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
05311 return -1;
05312 }
05313
05314 if (maxlen <= 0) {
05315 ast_log(LOG_WARNING, "Out of space, can't add anymore (%s:%s)\n", var, value);
05316 return -1;
05317 }
05318
05319 req->header[req->headers] = req->data + req->len;
05320
05321 if (compactheaders)
05322 var = find_alias(var, var);
05323
05324 snprintf(req->header[req->headers], maxlen, "%s: %s\r\n", var, value);
05325 req->len += strlen(req->header[req->headers]);
05326 req->headers++;
05327 if (req->headers < SIP_MAX_HEADERS)
05328 req->headers++;
05329 else
05330 ast_log(LOG_WARNING, "Out of SIP header space... Will generate broken SIP message\n");
05331
05332 return 0;
05333 }
05334
05335
05336 static int add_header_contentLength(struct sip_request *req, int len)
05337 {
05338 char clen[10];
05339
05340 snprintf(clen, sizeof(clen), "%d", len);
05341 return add_header(req, "Content-Length", clen);
05342 }
05343
05344
05345 static int add_line(struct sip_request *req, const char *line)
05346 {
05347 if (req->lines == SIP_MAX_LINES) {
05348 ast_log(LOG_WARNING, "Out of SIP line space\n");
05349 return -1;
05350 }
05351 if (!req->lines) {
05352
05353 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
05354 req->len += strlen(req->data + req->len);
05355 }
05356 if (req->len >= sizeof(req->data) - 4) {
05357 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
05358 return -1;
05359 }
05360 req->line[req->lines] = req->data + req->len;
05361 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
05362 req->len += strlen(req->line[req->lines]);
05363 req->lines++;
05364 return 0;
05365 }
05366
05367
05368 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
05369 {
05370 const char *tmp = get_header(orig, field);
05371
05372 if (!ast_strlen_zero(tmp))
05373 return add_header(req, field, tmp);
05374 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
05375 return -1;
05376 }
05377
05378
05379 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
05380 {
05381 int start = 0;
05382 int copied = 0;
05383 for (;;) {
05384 const char *tmp = __get_header(orig, field, &start);
05385
05386 if (ast_strlen_zero(tmp))
05387 break;
05388
05389 add_header(req, field, tmp);
05390 copied++;
05391 }
05392 return copied ? 0 : -1;
05393 }
05394
05395
05396
05397
05398
05399
05400
05401 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
05402 {
05403 int copied = 0;
05404 int start = 0;
05405
05406 for (;;) {
05407 char new[256];
05408 const char *oh = __get_header(orig, field, &start);
05409
05410 if (ast_strlen_zero(oh))
05411 break;
05412
05413 if (!copied) {
05414 char leftmost[256], *others, *rport;
05415
05416
05417 ast_copy_string(leftmost, oh, sizeof(leftmost));
05418 others = strchr(leftmost, ',');
05419 if (others)
05420 *others++ = '\0';
05421
05422
05423 rport = strstr(leftmost, ";rport");
05424 if (rport && *(rport+6) == '=')
05425 rport = NULL;
05426
05427
05428 if (rport && ((ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_ALWAYS) || (ast_test_flag(&p->flags[0], SIP_NAT) == SIP_NAT_RFC3581))) {
05429
05430 char *end;
05431
05432 rport = strstr(leftmost, ";rport");
05433
05434 if (rport) {
05435 end = strchr(rport + 1, ';');
05436 if (end)
05437 memmove(rport, end, strlen(end) + 1);
05438 else
05439 *rport = '\0';
05440 }
05441
05442
05443 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
05444 leftmost, ast_inet_ntoa(p->recv.sin_addr),
05445 ntohs(p->recv.sin_port),
05446 others ? "," : "", others ? others : "");
05447 } else {
05448
05449 snprintf(new, sizeof(new), "%s;received=%s%s%s",
05450 leftmost, ast_inet_ntoa(p->recv.sin_addr),
05451 others ? "," : "", others ? others : "");
05452 }
05453 oh = new;
05454 }
05455 add_header(req, field, oh);
05456 copied++;
05457 }
05458 if (!copied) {
05459 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
05460 return -1;
05461 }
05462 return 0;
05463 }
05464
05465
05466 static void add_route(struct sip_request *req, struct sip_route *route)
05467 {
05468 char r[BUFSIZ*2], *p;
05469 int n, rem = sizeof(r);
05470
05471 if (!route)
05472 return;
05473
05474 p = r;
05475 for (;route ; route = route->next) {
05476 n = strlen(route->hop);
05477 if (rem < n+3)
05478 break;
05479 if (p != r) {
05480 *p++ = ',';
05481 --rem;
05482 }
05483 *p++ = '<';
05484 ast_copy_string(p, route->hop, rem);
05485 p += n;
05486 *p++ = '>';
05487 rem -= (n+2);
05488 }
05489 *p = '\0';
05490 add_header(req, "Route", r);
05491 }
05492
05493
05494 static void set_destination(struct sip_pvt *p, char *uri)
05495 {
05496 char *h, *maddr, hostname[256];
05497 int port, hn;
05498 struct hostent *hp;
05499 struct ast_hostent ahp;
05500 int debug=sip_debug_test_pvt(p);
05501
05502
05503
05504
05505 if (debug)
05506 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
05507
05508
05509 h = strchr(uri, '@');
05510 if (h)
05511 ++h;
05512 else {
05513 h = uri;
05514 if (strncmp(h, "sip:", 4) == 0)
05515 h += 4;
05516 else if (strncmp(h, "sips:", 5) == 0)
05517 h += 5;
05518 }
05519 hn = strcspn(h, ":;>") + 1;
05520 if (hn > sizeof(hostname))
05521 hn = sizeof(hostname);
05522 ast_copy_string(hostname, h, hn);
05523
05524 h += hn - 1;
05525
05526
05527 if (*h == ':') {
05528
05529 ++h;
05530 port = strtol(h, &h, 10);
05531 }
05532 else
05533 port = STANDARD_SIP_PORT;
05534
05535
05536 maddr = strstr(h, "maddr=");
05537 if (maddr) {
05538 maddr += 6;
05539 hn = strspn(maddr, "0123456789.") + 1;
05540 if (hn > sizeof(hostname))
05541 hn = sizeof(hostname);
05542 ast_copy_string(hostname, maddr, hn);
05543 }
05544
05545 hp = ast_gethostbyname(hostname, &ahp);
05546 if (hp == NULL) {
05547 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
05548 return;
05549 }
05550 p->sa.sin_family = AF_INET;
05551 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
05552 p->sa.sin_port = htons(port);
05553 if (debug)
05554 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(p->sa.sin_addr), port);
05555 }
05556
05557
05558 static int init_resp(struct sip_request *resp, const char *msg)
05559 {
05560
05561 memset(resp, 0, sizeof(*resp));
05562 resp->method = SIP_RESPONSE;
05563 resp->header[0] = resp->data;
05564 snprintf(resp->header[0], sizeof(resp->data), "SIP/2.0 %s\r\n", msg);
05565 resp->len = strlen(resp->header[0]);
05566 resp->headers++;
05567 return 0;
05568 }
05569
05570
05571 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
05572 {
05573
05574 memset(req, 0, sizeof(*req));
05575 req->method = sipmethod;
05576 req->header[0] = req->data;
05577 snprintf(req->header[0], sizeof(req->data), "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
05578 req->len = strlen(req->header[0]);
05579 req->headers++;
05580 return 0;
05581 }
05582
05583
05584
05585 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
05586 {
05587 char newto[256];
05588 const char *ot;
05589
05590 init_resp(resp, msg);
05591 copy_via_headers(p, resp, req, "Via");
05592 if (msg[0] == '2')
05593 copy_all_header(resp, req, "Record-Route");
05594 copy_header(resp, req, "From");
05595 ot = get_header(req, "To");
05596 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
05597
05598
05599 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
05600 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
05601 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
05602 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
05603 else
05604 ast_copy_string(newto, ot, sizeof(newto));
05605 ot = newto;
05606 }
05607 add_header(resp, "To", ot);
05608 copy_header(resp, req, "Call-ID");
05609 copy_header(resp, req, "CSeq");
05610 if (!ast_strlen_zero(global_useragent))
05611 add_header(resp, "User-Agent", global_useragent);
05612 add_header(resp, "Allow", ALLOWED_METHODS);
05613 add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
05614 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
05615
05616
05617 char tmp[256];
05618
05619 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
05620 add_header(resp, "Expires", tmp);
05621 if (p->expiry) {
05622 char contact[BUFSIZ];
05623 snprintf(contact, sizeof(contact), "%s;expires=%d", p->our_contact, p->expiry);
05624 add_header(resp, "Contact", contact);
05625 }
05626 } else if (msg[0] != '4' && p->our_contact[0]) {
05627 add_header(resp, "Contact", p->our_contact);
05628 }
05629 return 0;
05630 }
05631
05632
05633 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
05634 {
05635 struct sip_request *orig = &p->initreq;
05636 char stripped[80];
05637 char tmp[80];
05638 char newto[256];
05639 const char *c;
05640 const char *ot, *of;
05641 int is_strict = FALSE;
05642
05643 memset(req, 0, sizeof(struct sip_request));
05644
05645 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
05646
05647 if (!seqno) {
05648 p->ocseq++;
05649 seqno = p->ocseq;
05650 }
05651
05652 if (newbranch) {
05653 p->branch ^= ast_random();
05654 build_via(p);
05655 }
05656
05657
05658 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop,";lr") == NULL) {
05659 is_strict = TRUE;
05660 if (sipdebug)
05661 ast_log(LOG_DEBUG, "Strict routing enforced for session %s\n", p->callid);
05662 }
05663
05664 if (sipmethod == SIP_CANCEL)
05665 c = p->initreq.rlPart2;
05666 else if (sipmethod == SIP_ACK) {
05667
05668
05669 if (!ast_strlen_zero(p->okcontacturi))
05670 c = is_strict ? p->route->hop : p->okcontacturi;
05671 else
05672 c = p->initreq.rlPart2;
05673 } else if (!ast_strlen_zero(p->okcontacturi))
05674 c = is_strict ? p->route->hop : p->okcontacturi;
05675 else if (!ast_strlen_zero(p->uri))
05676 c = p->uri;
05677 else {
05678 char *n;
05679
05680 ast_copy_string(stripped, get_header(orig, (ast_test_flag(&p->flags[0], SIP_OUTGOING)) ? "To" : "From"),
05681 sizeof(stripped));
05682 n = get_in_brackets(stripped);
05683 c = strsep(&n, ";");
05684 }
05685 init_req(req, sipmethod, c);
05686
05687 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
05688
05689 add_header(req, "Via", p->via);
05690 if (p->route) {
05691 set_destination(p, p->route->hop);
05692 add_route(req, is_strict ? p->route->next : p->route);
05693 }
05694
05695 ot = get_header(orig, "To");
05696 of = get_header(orig, "From");
05697
05698
05699
05700 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
05701
05702
05703 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_strlen_zero(p->theirtag))
05704 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
05705 else if (!ast_test_flag(&p->flags[0], SIP_OUTGOING))
05706 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
05707 else
05708 snprintf(newto, sizeof(newto), "%s", ot);
05709 ot = newto;
05710 }
05711
05712 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
05713 add_header(req, "From", of);
05714 add_header(req, "To", ot);
05715 } else {
05716 add_header(req, "From", ot);
05717 add_header(req, "To", of);
05718 }
05719
05720 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
05721 add_header(req, "Contact", p->our_contact);
05722
05723 copy_header(req, orig, "Call-ID");
05724 add_header(req, "CSeq", tmp);
05725
05726 if (!ast_strlen_zero(global_useragent))
05727 add_header(req, "User-Agent", global_useragent);
05728 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
05729
05730 if (!ast_strlen_zero(p->rpid))
05731 add_header(req, "Remote-Party-ID", p->rpid);
05732
05733 return 0;
05734 }
05735
05736
05737 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
05738 {
05739 struct sip_request resp;
05740 int seqno = 0;
05741
05742 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
05743 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
05744 return -1;
05745 }
05746 respprep(&resp, p, msg, req);
05747 add_header_contentLength(&resp, 0);
05748
05749
05750 if (p->method == SIP_INVITE && msg[0] != '1' && p->owner && p->owner->hangupcause) {
05751 char buf[10];
05752
05753 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
05754 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
05755 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
05756 }
05757 return send_response(p, &resp, reliable, seqno);
05758 }
05759
05760 static void temp_pvt_cleanup(void *data)
05761 {
05762 struct sip_pvt *p = data;
05763
05764 ast_string_field_free_pools(p);
05765
05766 free(data);
05767 }
05768
05769
05770 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)
05771 {
05772 struct sip_pvt *p = NULL;
05773
05774 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
05775 ast_log(LOG_NOTICE, "Failed to get temporary pvt\n");
05776 return -1;
05777 }
05778
05779
05780 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) {
05781 ast_set_flag(&p->flags[0], SIP_NO_HISTORY);
05782 if (ast_string_field_init(p, 512))
05783 return -1;
05784 }
05785
05786
05787 p->method = intended_method;
05788
05789 if (sin) {
05790 p->sa = *sin;
05791 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
05792 p->ourip = __ourip;
05793 } else
05794 p->ourip = __ourip;
05795
05796 p->branch = ast_random();
05797 make_our_tag(p->tag, sizeof(p->tag));
05798 p->ocseq = INITIAL_CSEQ;
05799
05800 if (useglobal_nat && sin) {
05801 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
05802 p->recv = *sin;
05803 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
05804 }
05805
05806 ast_string_field_set(p, fromdomain, default_fromdomain);
05807 build_via(p);
05808 ast_string_field_set(p, callid, callid);
05809
05810
05811 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
05812
05813
05814 ast_string_field_free_all(p);
05815
05816 return 0;
05817 }
05818
05819
05820 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
05821 {
05822 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
05823 }
05824
05825
05826 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
05827 {
05828 struct sip_request resp;
05829 respprep(&resp, p, msg, req);
05830 append_date(&resp);
05831 add_header(&resp, "Unsupported", unsupported);
05832 add_header_contentLength(&resp, 0);
05833 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
05834 }
05835
05836
05837
05838
05839 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
05840 {
05841 return __transmit_response(p, msg, req, XMIT_CRITICAL);
05842 }
05843
05844
05845 static void append_date(struct sip_request *req)
05846 {
05847 char tmpdat[256];
05848 struct tm tm;
05849 time_t t = time(NULL);
05850
05851 gmtime_r(&t, &tm);
05852 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
05853 add_header(req, "Date", tmpdat);
05854 }
05855
05856
05857 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
05858 {
05859 struct sip_request resp;
05860 respprep(&resp, p, msg, req);
05861 append_date(&resp);
05862 add_header_contentLength(&resp, 0);
05863 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
05864 }
05865
05866
05867 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
05868 {
05869 struct sip_request resp;
05870 respprep(&resp, p, msg, req);
05871 add_header(&resp, "Accept", "application/sdp");
05872 add_header_contentLength(&resp, 0);
05873 return send_response(p, &resp, reliable, 0);
05874 }
05875
05876
05877 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)
05878 {
05879 struct sip_request resp;
05880 char tmp[512];
05881 int seqno = 0;
05882
05883 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
05884 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
05885 return -1;
05886 }
05887
05888
05889 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
05890 respprep(&resp, p, msg, req);
05891 add_header(&resp, header, tmp);
05892 add_header_contentLength(&resp, 0);
05893 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
05894 return send_response(p, &resp, reliable, seqno);
05895 }
05896
05897
05898 static int add_text(struct sip_request *req, const char *text)
05899 {
05900
05901 add_header(req, "Content-Type", "text/plain");
05902 add_header_contentLength(req, strlen(text));
05903 add_line(req, text);
05904 return 0;
05905 }
05906
05907
05908
05909 static int add_digit(struct sip_request *req, char digit, unsigned int duration)
05910 {
05911 char tmp[256];
05912
05913 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
05914 add_header(req, "Content-Type", "application/dtmf-relay");
05915 add_header_contentLength(req, strlen(tmp));
05916 add_line(req, tmp);
05917 return 0;
05918 }
05919
05920
05921
05922 static int add_vidupdate(struct sip_request *req)
05923 {
05924 const char *xml_is_a_huge_waste_of_space =
05925 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
05926 " <media_control>\r\n"
05927 " <vc_primitive>\r\n"
05928 " <to_encoder>\r\n"
05929 " <picture_fast_update>\r\n"
05930 " </picture_fast_update>\r\n"
05931 " </to_encoder>\r\n"
05932 " </vc_primitive>\r\n"
05933 " </media_control>\r\n";
05934 add_header(req, "Content-Type", "application/media_control+xml");
05935 add_header_contentLength(req, strlen(xml_is_a_huge_waste_of_space));
05936 add_line(req, xml_is_a_huge_waste_of_space);
05937 return 0;
05938 }
05939
05940
05941 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
05942 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
05943 int debug, int *min_packet_size)
05944 {
05945 int rtp_code;
05946 struct ast_format_list fmt;
05947
05948
05949 if (debug)
05950 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
05951 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
05952 return;
05953
05954 if (p->rtp) {
05955 struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
05956 fmt = ast_codec_pref_getsize(pref, codec);
05957 } else
05958 return;
05959 ast_build_string(m_buf, m_size, " %d", rtp_code);
05960 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
05961 ast_rtp_lookup_mime_subtype(1, codec,
05962 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
05963 sample_rate);
05964 if (codec == AST_FORMAT_G729A) {
05965
05966 ast_build_string(a_buf, a_size, "a=fmtp:%d annexb=no\r\n", rtp_code);
05967 } else if (codec == AST_FORMAT_ILBC) {
05968
05969 ast_build_string(a_buf, a_size, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
05970 }
05971
05972 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
05973 *min_packet_size = fmt.cur_ms;
05974
05975
05976 if ((*min_packet_size) == 0 && fmt.cur_ms)
05977 *min_packet_size = fmt.cur_ms;
05978 }
05979
05980
05981 static int t38_get_rate(int t38cap)
05982 {
05983 int maxrate = (t38cap & (T38FAX_RATE_14400 | T38FAX_RATE_12000 | T38FAX_RATE_9600 | T38FAX_RATE_7200 | T38FAX_RATE_4800 | T38FAX_RATE_2400));
05984
05985 if (maxrate & T38FAX_RATE_14400) {
05986 if (option_debug > 1)
05987 ast_log(LOG_DEBUG, "T38MaxFaxRate 14400 found\n");
05988 return 14400;
05989 } else if (maxrate & T38FAX_RATE_12000) {
05990 if (option_debug > 1)
05991 ast_log(LOG_DEBUG, "T38MaxFaxRate 12000 found\n");
05992 return 12000;
05993 } else if (maxrate & T38FAX_RATE_9600) {
05994 if (option_debug > 1)
05995 ast_log(LOG_DEBUG, "T38MaxFaxRate 9600 found\n");
05996 return 9600;
05997 } else if (maxrate & T38FAX_RATE_7200) {
05998 if (option_debug > 1)
05999 ast_log(LOG_DEBUG, "T38MaxFaxRate 7200 found\n");
06000 return 7200;
06001 } else if (maxrate & T38FAX_RATE_4800) {
06002 if (option_debug > 1)
06003 ast_log(LOG_DEBUG, "T38MaxFaxRate 4800 found\n");
06004 return 4800;
06005 } else if (maxrate & T38FAX_RATE_2400) {
06006 if (option_debug > 1)
06007 ast_log(LOG_DEBUG, "T38MaxFaxRate 2400 found\n");
06008 return 2400;
06009 } else {
06010 if (option_debug > 1)
06011 ast_log(LOG_DEBUG, "Strange, T38MaxFaxRate NOT found in peers T38 SDP.\n");
06012 return 0;
06013 }
06014 }
06015
06016
06017 static int add_t38_sdp(struct sip_request *resp, struct sip_pvt *p)
06018 {
06019 int len = 0;
06020 int x = 0;
06021 struct sockaddr_in udptlsin;
06022 char v[256] = "";
06023 char s[256] = "";
06024 char o[256] = "";
06025 char c[256] = "";
06026 char t[256] = "";
06027 char m_modem[256];
06028 char a_modem[1024];
06029 char *m_modem_next = m_modem;
06030 size_t m_modem_left = sizeof(m_modem);
06031 char *a_modem_next = a_modem;
06032 size_t a_modem_left = sizeof(a_modem);
06033 struct sockaddr_in udptldest = { 0, };
06034 int debug;
06035
06036 debug = sip_debug_test_pvt(p);
06037 len = 0;
06038 if (!p->udptl) {
06039 ast_log(LOG_WARNING, "No way to add SDP without an UDPTL structure\n");
06040 return -1;
06041 }
06042
06043 if (!p->sessionid) {
06044 p->sessionid = getpid();
06045 p->sessionversion = p->sessionid;
06046 } else
06047 p->sessionversion++;
06048
06049
06050 ast_udptl_get_us(p->udptl, &udptlsin);
06051
06052
06053 if (p->udptlredirip.sin_addr.s_addr) {
06054 udptldest.sin_port = p->udptlredirip.sin_port;
06055 udptldest.sin_addr = p->udptlredirip.sin_addr;
06056 } else {
06057 udptldest.sin_addr = p->ourip;
06058 udptldest.sin_port = udptlsin.sin_port;
06059 }
06060
06061 if (debug)
06062 ast_log(LOG_DEBUG, "T.38 UDPTL is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(udptlsin.sin_port));
06063
06064
06065
06066
06067 if (debug) {
06068 ast_log(LOG_DEBUG, "Our T38 capability (%d), peer T38 capability (%d), joint capability (%d)\n",
06069 p->t38.capability,
06070 p->t38.peercapability,
06071 p->t38.jointcapability);
06072 }
06073 snprintf(v, sizeof(v), "v=0\r\n");
06074 snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(udptldest.sin_addr));
06075 snprintf(s, sizeof(s), "s=session\r\n");
06076 snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(udptldest.sin_addr));
06077 snprintf(t, sizeof(t), "t=0 0\r\n");
06078 ast_build_string(&m_modem_next, &m_modem_left, "m=image %d udptl t38\r\n", ntohs(udptldest.sin_port));
06079
06080 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_0)
06081 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:0\r\n");
06082 if ((p->t38.jointcapability & T38FAX_VERSION) == T38FAX_VERSION_1)
06083 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxVersion:1\r\n");
06084 if ((x = t38_get_rate(p->t38.jointcapability)))
06085 ast_build_string(&a_modem_next, &a_modem_left, "a=T38MaxBitRate:%d\r\n",x);
06086 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxFillBitRemoval:%d\r\n", (p->t38.jointcapability & T38FAX_FILL_BIT_REMOVAL) ? 1 : 0);
06087 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingMMR:%d\r\n", (p->t38.jointcapability & T38FAX_TRANSCODING_MMR) ? 1 : 0);
06088 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxTranscodingJBIG:%d\r\n", (p->t38.jointcapability & T38FAX_TRANSCODING_JBIG) ? 1 : 0);
06089 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxRateManagement:%s\r\n", (p->t38.jointcapability & T38FAX_RATE_MANAGEMENT_LOCAL_TCF) ? "localTCF" : "transferredTCF");
06090 x = ast_udptl_get_local_max_datagram(p->udptl);
06091 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxBuffer:%d\r\n",x);
06092 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxMaxDatagram:%d\r\n",x);
06093 if (p->t38.jointcapability != T38FAX_UDP_EC_NONE)
06094 ast_build_string(&a_modem_next, &a_modem_left, "a=T38FaxUdpEC:%s\r\n", (p->t38.jointcapability & T38FAX_UDP_EC_REDUNDANCY) ? "t38UDPRedundancy" : "t38UDPFEC");
06095 len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m_modem) + strlen(a_modem);
06096 add_header(resp, "Content-Type", "application/sdp");
06097 add_header_contentLength(resp, len);
06098 add_line(resp, v);
06099 add_line(resp, o);
06100 add_line(resp, s);
06101 add_line(resp, c);
06102 add_line(resp, t);
06103 add_line(resp, m_modem);
06104 add_line(resp, a_modem);
06105
06106
06107 p->lastrtprx = p->lastrtptx = time(NULL);
06108
06109 return 0;
06110 }
06111
06112
06113
06114 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
06115 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
06116 int debug)
06117 {
06118 int rtp_code;
06119
06120 if (debug)
06121 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format, 0));
06122 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
06123 return;
06124
06125 ast_build_string(m_buf, m_size, " %d", rtp_code);
06126 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
06127 ast_rtp_lookup_mime_subtype(0, format, 0),
06128 sample_rate);
06129 if (format == AST_RTP_DTMF)
06130
06131 ast_build_string(a_buf, a_size, "a=fmtp:%d 0-16\r\n", rtp_code);
06132 }
06133
06134 #define SDP_SAMPLE_RATE(x) (x == AST_FORMAT_G722) ? 16000 : 8000
06135
06136
06137 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p)
06138 {
06139 int len = 0;
06140 int alreadysent = 0;
06141
06142 struct sockaddr_in sin;
06143 struct sockaddr_in vsin;
06144 struct sockaddr_in dest;
06145 struct sockaddr_in vdest = { 0, };
06146
06147
06148 char *version = "v=0\r\n";
06149 char *subject = "s=session\r\n";
06150 char owner[256];
06151 char connection[256];
06152 char *stime = "t=0 0\r\n";
06153 char bandwidth[256] = "";
06154 char *hold;
06155 char m_audio[256];
06156 char m_video[256];
06157 char a_audio[1024];
06158 char a_video[1024];
06159 char *m_audio_next = m_audio;
06160 char *m_video_next = m_video;
06161 size_t m_audio_left = sizeof(m_audio);
06162 size_t m_video_left = sizeof(m_video);
06163 char *a_audio_next = a_audio;
06164 char *a_video_next = a_video;
06165 size_t a_audio_left = sizeof(a_audio);
06166 size_t a_video_left = sizeof(a_video);
06167
06168 int x;
06169 int capability;
06170 int needvideo = FALSE;
06171 int debug = sip_debug_test_pvt(p);
06172 int min_audio_packet_size = 0;
06173 int min_video_packet_size = 0;
06174
06175 m_video[0] = '\0';
06176
06177 if (!p->rtp) {
06178 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
06179 return AST_FAILURE;
06180 }
06181
06182
06183 if (!p->sessionid) {
06184 p->sessionid = getpid();
06185 p->sessionversion = p->sessionid;
06186 } else
06187 p->sessionversion++;
06188
06189
06190 ast_rtp_get_us(p->rtp, &sin);
06191 if (p->vrtp)
06192 ast_rtp_get_us(p->vrtp, &vsin);
06193
06194
06195 if (p->redirip.sin_addr.s_addr) {
06196 dest.sin_port = p->redirip.sin_port;
06197 dest.sin_addr = p->redirip.sin_addr;
06198 } else {
06199 dest.sin_addr = p->ourip;
06200 dest.sin_port = sin.sin_port;
06201 }
06202
06203 capability = p->jointcapability;
06204
06205
06206 if (option_debug > 1) {
06207 char codecbuf[BUFSIZ];
06208 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");
06209 ast_log(LOG_DEBUG, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
06210 }
06211
06212 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
06213 if (ast_test_flag(&p->t38.t38support, SIP_PAGE2_T38SUPPORT_RTP)) {
06214 ast_build_string(&m_audio_next, &m_audio_left, " %d", 191);
06215 ast_build_string(&a_audio_next, &a_audio_left, "a=rtpmap:%d %s/%d\r\n", 191, "t38", 8000);
06216 }
06217 #endif
06218
06219
06220 if ((capability & AST_FORMAT_VIDEO_MASK) && !ast_test_flag(&p->flags[0], SIP_NOVIDEO)) {
06221 if (p->vrtp) {
06222 needvideo = TRUE;
06223 if (option_debug > 1)
06224 ast_log(LOG_DEBUG, "This call needs video offers!\n");
06225 } else if (option_debug > 1)
06226 ast_log(LOG_DEBUG, "This call needs video offers, but there's no video support enabled!\n");
06227 }
06228
06229
06230
06231
06232 if (needvideo) {
06233
06234 if (p->vredirip.sin_addr.s_addr) {
06235 vdest.sin_addr = p->vredirip.sin_addr;
06236 vdest.sin_port = p->vredirip.sin_port;
06237 } else {
06238 vdest.sin_addr = p->ourip;
06239 vdest.sin_port = vsin.sin_port;
06240 }
06241 ast_build_string(&m_video_next, &m_video_left, "m=video %d RTP/AVP", ntohs(vdest.sin_port));
06242
06243
06244 if (p->maxcallbitrate)
06245 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
06246 if (debug)
06247 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(vsin.sin_port));
06248 }
06249
06250 if (debug)
06251 ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip), ntohs(sin.sin_port));
06252
06253
06254
06255
06256
06257
06258 snprintf(owner, sizeof(owner), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(dest.sin_addr));
06259 snprintf(connection, sizeof(connection), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
06260 ast_build_string(&m_audio_next, &m_audio_left, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
06261
06262 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR))
06263 hold = "a=recvonly\r\n";
06264 else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE))
06265 hold = "a=inactive\r\n";
06266 else
06267 hold = "a=sendrecv\r\n";
06268
06269
06270
06271
06272
06273
06274
06275
06276
06277
06278 if (capability & p->prefcodec) {
06279 int codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
06280
06281 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
06282 &m_audio_next, &m_audio_left,
06283 &a_audio_next, &a_audio_left,
06284 debug, &min_audio_packet_size);
06285 alreadysent |= codec;
06286 }
06287
06288
06289 for (x = 0; x < 32; x++) {
06290 int codec;
06291
06292 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
06293 break;
06294
06295 if (!(capability & codec))
06296 continue;
06297
06298 if (alreadysent & codec)
06299 continue;
06300
06301 add_codec_to_sdp(p, codec, SDP_SAMPLE_RATE(codec),
06302 &m_audio_next, &m_audio_left,
06303 &a_audio_next, &a_audio_left,
06304 debug, &min_audio_packet_size);
06305 alreadysent |= codec;
06306 }
06307
06308
06309 for (x = 1; x <= (needvideo ? AST_FORMAT_MAX_VIDEO : AST_FORMAT_MAX_AUDIO); x <<= 1) {
06310 if (!(capability & x))
06311 continue;
06312
06313 if (alreadysent & x)
06314 continue;
06315
06316 if (x <= AST_FORMAT_MAX_AUDIO)
06317 add_codec_to_sdp(p, x, SDP_SAMPLE_RATE(x),
06318 &m_audio_next, &m_audio_left,
06319 &a_audio_next, &a_audio_left,
06320 debug, &min_audio_packet_size);
06321 else
06322 add_codec_to_sdp(p, x, 90000,
06323 &m_video_next, &m_video_left,
06324 &a_video_next, &a_video_left,
06325 debug, &min_video_packet_size);
06326 }
06327
06328
06329 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
06330 if (!(p->jointnoncodeccapability & x))
06331 continue;
06332
06333 add_noncodec_to_sdp(p, x, 8000,
06334 &m_audio_next, &m_audio_left,
06335 &a_audio_next, &a_audio_left,
06336 debug);
06337 }
06338
06339 if (option_debug > 2)
06340 ast_log(LOG_DEBUG, "-- Done with adding codecs to SDP\n");
06341
06342 if (!p->owner || !ast_internal_timing_enabled(p->owner))
06343 ast_build_string(&a_audio_next, &a_audio_left, "a=silenceSupp:off - - - -\r\n");
06344
06345 if (min_audio_packet_size)
06346 ast_build_string(&a_audio_next, &a_audio_left, "a=ptime:%d\r\n", min_audio_packet_size);
06347
06348 if (min_video_packet_size)
06349 ast_build_string(&a_video_next, &a_video_left, "a=ptime:%d\r\n", min_video_packet_size);
06350
06351 if ((m_audio_left < 2) || (m_video_left < 2) || (a_audio_left == 0) || (a_video_left == 0))
06352 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
06353
06354 ast_build_string(&m_audio_next, &m_audio_left, "\r\n");
06355 if (needvideo)
06356 ast_build_string(&m_video_next, &m_video_left, "\r\n");
06357
06358 len = strlen(version) + strlen(subject) + strlen(owner) + strlen(connection) + strlen(stime) + strlen(m_audio) + strlen(a_audio) + strlen(hold);
06359 if (needvideo)
06360 len += strlen(m_video) + strlen(a_video) + strlen(bandwidth) + strlen(hold);
06361
06362 add_header(resp, "Content-Type", "application/sdp");
06363 add_header_contentLength(resp, len);
06364 add_line(resp, version);
06365 add_line(resp, owner);
06366 add_line(resp, subject);
06367 add_line(resp, connection);
06368 if (needvideo)
06369 add_line(resp, bandwidth);
06370 add_line(resp, stime);
06371 add_line(resp, m_audio);
06372 add_line(resp, a_audio);
06373 add_line(resp, hold);
06374 if (needvideo) {
06375 add_line(resp, m_video);
06376 add_line(resp, a_video);
06377 add_line(resp, hold);
06378 }
06379
06380
06381 p->lastrtprx = p->lastrtptx = time(NULL);
06382
06383 if (option_debug > 2) {
06384 char buf[BUFSIZ];
06385 ast_log(LOG_DEBUG, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, BUFSIZ, capability));
06386 }
06387
06388 return AST_SUCCESS;
06389 }
06390
06391
06392 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
06393 {
06394 struct sip_request resp;
06395 int seqno;
06396
06397 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
06398 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
06399 return -1;
06400 }
06401 respprep(&resp, p, msg, req);
06402 if (p->udptl) {
06403 ast_udptl_offered_from_local(p->udptl, 0);
06404 add_t38_sdp(&resp, p);
06405 } else
06406 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
06407 if (retrans && !p->pendinginvite)
06408 p->pendinginvite = seqno;
06409 return send_response(p, &resp, retrans, seqno);
06410 }
06411
06412
06413 static void copy_request(struct sip_request *dst, const struct sip_request *src)
06414 {
06415 long offset;
06416 int x;
06417 offset = ((void *)dst) - ((void *)src);
06418
06419 memcpy(dst, src, sizeof(*dst));
06420
06421 for (x=0; x < src->headers; x++)
06422 dst->header[x] += offset;
06423 for (x=0; x < src->lines; x++)
06424 dst->line[x] += offset;
06425 dst->rlPart1 += offset;
06426 dst->rlPart2 += offset;
06427 }
06428
06429
06430 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
06431 {
06432 struct sip_request resp;
06433 int seqno;
06434 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
06435 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
06436 return -1;
06437 }
06438 respprep(&resp, p, msg, req);
06439 if (p->rtp) {
06440 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06441 if (option_debug)
06442 ast_log(LOG_DEBUG, "Setting framing from config on incoming call\n");
06443 ast_rtp_codec_setpref(p->rtp, &p->prefs);
06444 }
06445 try_suggested_sip_codec(p);
06446 add_sdp(&resp, p);
06447 } else
06448 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
06449 if (reliable && !p->pendinginvite)
06450 p->pendinginvite = seqno;
06451 return send_response(p, &resp, reliable, seqno);
06452 }
06453
06454
06455 static int determine_firstline_parts(struct sip_request *req)
06456 {
06457 char *e = ast_skip_blanks(req->header[0]);
06458
06459 if (!*e)
06460 return -1;
06461 req->rlPart1 = e;
06462 e = ast_skip_nonblanks(e);
06463 if (*e)
06464 *e++ = '\0';
06465
06466 e = ast_skip_blanks(e);
06467 if ( !*e )
06468 return -1;
06469 ast_trim_blanks(e);
06470
06471 if (!strcasecmp(req->rlPart1, "SIP/2.0") ) {
06472 if (strlen(e) < 3)
06473 return -1;
06474 req->rlPart2 = e;
06475 } else {
06476 if ( *e == '<' ) {
06477 ast_log(LOG_WARNING, "bogus uri in <> %s\n", e);
06478 e++;
06479 if (!*e)
06480 return -1;
06481 }
06482 req->rlPart2 = e;
06483 e = ast_skip_nonblanks(e);
06484 if (*e)
06485 *e++ = '\0';
06486 e = ast_skip_blanks(e);
06487 if (strcasecmp(e, "SIP/2.0") ) {
06488 ast_log(LOG_WARNING, "Bad request protocol %s\n", e);
06489 return -1;
06490 }
06491 }
06492 return 1;
06493 }
06494
06495
06496
06497
06498
06499
06500
06501 static int transmit_reinvite_with_sdp(struct sip_pvt *p)
06502 {
06503 struct sip_request req;
06504
06505 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
06506
06507 add_header(&req, "Allow", ALLOWED_METHODS);
06508 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06509 if (sipdebug)
06510 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
06511 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
06512 append_history(p, "ReInv", "Re-invite sent");
06513 add_sdp(&req, p);
06514
06515 initialize_initreq(p, &req);
06516 p->lastinvite = p->ocseq;
06517 ast_set_flag(&p->flags[0], SIP_OUTGOING);
06518 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
06519 }
06520
06521
06522
06523
06524
06525 static int transmit_reinvite_with_t38_sdp(struct sip_pvt *p)
06526 {
06527 struct sip_request req;
06528
06529 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
06530
06531 add_header(&req, "Allow", ALLOWED_METHODS);
06532 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06533 if (sipdebug)
06534 add_header(&req, "X-asterisk-info", "SIP re-invite (T38 switchover)");
06535 ast_udptl_offered_from_local(p->udptl, 1);
06536 add_t38_sdp(&req, p);
06537
06538 initialize_initreq(p, &req);
06539 ast_set_flag(&p->flags[0], SIP_OUTGOING);
06540 p->lastinvite = p->ocseq;
06541 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
06542 }
06543
06544
06545 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
06546 {
06547 char stripped[BUFSIZ];
06548 char *c;
06549
06550 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
06551 c = get_in_brackets(stripped);
06552 c = strsep(&c, ";");
06553 if (!ast_strlen_zero(c))
06554 ast_string_field_set(p, uri, c);
06555 }
06556
06557
06558 static void build_contact(struct sip_pvt *p)
06559 {
06560
06561 if (ourport != STANDARD_SIP_PORT)
06562 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);
06563 else
06564 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip));
06565 }
06566
06567
06568 static void build_rpid(struct sip_pvt *p)
06569 {
06570 int send_pres_tags = TRUE;
06571 const char *privacy=NULL;
06572 const char *screen=NULL;
06573 char buf[256];
06574 const char *clid = default_callerid;
06575 const char *clin = NULL;
06576 const char *fromdomain;
06577
06578 if (!ast_strlen_zero(p->rpid) || !ast_strlen_zero(p->rpid_from))
06579 return;
06580
06581 if (p->owner && p->owner->cid.cid_num)
06582 clid = p->owner->cid.cid_num;
06583 if (p->owner && p->owner->cid.cid_name)
06584 clin = p->owner->cid.cid_name;
06585 if (ast_strlen_zero(clin))
06586 clin = clid;
06587
06588 switch (p->callingpres) {
06589 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
06590 privacy = "off";
06591 screen = "no";
06592 break;
06593 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
06594 privacy = "off";
06595 screen = "yes";
06596 break;
06597 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
06598 privacy = "off";
06599 screen = "no";
06600 break;
06601 case AST_PRES_ALLOWED_NETWORK_NUMBER:
06602 privacy = "off";
06603 screen = "yes";
06604 break;
06605 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
06606 privacy = "full";
06607 screen = "no";
06608 break;
06609 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
06610 privacy = "full";
06611 screen = "yes";
06612 break;
06613 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
06614 privacy = "full";
06615 screen = "no";
06616 break;
06617 case AST_PRES_PROHIB_NETWORK_NUMBER:
06618 privacy = "full";
06619 screen = "yes";
06620 break;
06621 case AST_PRES_NUMBER_NOT_AVAILABLE:
06622 send_pres_tags = FALSE;
06623 break;
06624 default:
06625 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
06626 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
06627 privacy = "full";
06628 else
06629 privacy = "off";
06630 screen = "no";
06631 break;
06632 }
06633
06634 fromdomain = S_OR(p->fromdomain, ast_inet_ntoa(p->ourip));
06635
06636 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
06637 if (send_pres_tags)
06638 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
06639 ast_string_field_set(p, rpid, buf);
06640
06641 ast_string_field_build(p, rpid_from, "\"%s\" <sip:%s@%s>;tag=%s", clin,
06642 S_OR(p->fromuser, clid),
06643 fromdomain, p->tag);
06644 }
06645
06646
06647 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
06648 {
06649 char invite_buf[256] = "";
06650 char *invite = invite_buf;
06651 size_t invite_max = sizeof(invite_buf);
06652 char from[256];
06653 char to[256];
06654 char tmp[BUFSIZ/2];
06655 char tmp2[BUFSIZ/2];
06656 const char *l = NULL, *n = NULL;
06657 const char *urioptions = "";
06658
06659 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
06660 const char *s = p->username;
06661
06662
06663
06664
06665
06666
06667 if (*s == '+')
06668 s++;
06669 for (; *s; s++) {
06670 if (!strchr(AST_DIGIT_ANYNUM, *s) )
06671 break;
06672 }
06673
06674 if (*s)
06675 urioptions = ";user=phone";
06676 }
06677
06678
06679 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
06680
06681 if (p->owner) {
06682 l = p->owner->cid.cid_num;
06683 n = p->owner->cid.cid_name;
06684 }
06685
06686 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID) &&
06687 ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
06688 l = CALLERID_UNKNOWN;
06689 n = l;
06690 }
06691 if (ast_strlen_zero(l))
06692 l = default_callerid;
06693 if (ast_strlen_zero(n))
06694 n = l;
06695
06696 if (!ast_strlen_zero(p->fromuser))
06697 l = p->fromuser;
06698 else
06699 ast_string_field_set(p, fromuser, l);
06700
06701
06702 if (!ast_strlen_zero(p->fromname))
06703 n = p->fromname;
06704 else
06705 ast_string_field_set(p, fromname, n);
06706
06707 if (pedanticsipchecking) {
06708 ast_uri_encode(n, tmp, sizeof(tmp), 0);
06709 n = tmp;
06710 ast_uri_encode(l, tmp2, sizeof(tmp2), 0);
06711 l = tmp2;
06712 }
06713
06714 if (ourport != STANDARD_SIP_PORT && ast_strlen_zero(p->fromdomain))
06715 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);
06716 else
06717 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), p->tag);
06718
06719
06720 if (!ast_strlen_zero(p->fullcontact)) {
06721
06722 ast_build_string(&invite, &invite_max, "%s", p->fullcontact);
06723 } else {
06724
06725 ast_build_string(&invite, &invite_max, "sip:");
06726 if (!ast_strlen_zero(p->username)) {
06727 n = p->username;
06728 if (pedanticsipchecking) {
06729 ast_uri_encode(n, tmp, sizeof(tmp), 0);
06730 n = tmp;
06731 }
06732 ast_build_string(&invite, &invite_max, "%s@", n);
06733 }
06734 ast_build_string(&invite, &invite_max, "%s", p->tohost);
06735 if (ntohs(p->sa.sin_port) != STANDARD_SIP_PORT)
06736 ast_build_string(&invite, &invite_max, ":%d", ntohs(p->sa.sin_port));
06737 ast_build_string(&invite, &invite_max, "%s", urioptions);
06738 }
06739
06740
06741 if (p->options && p->options->uri_options)
06742 ast_build_string(&invite, &invite_max, ";%s", p->options->uri_options);
06743
06744 ast_string_field_set(p, uri, invite_buf);
06745
06746 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
06747
06748 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", p->uri, p->theirtag);
06749 } else if (p->options && p->options->vxml_url) {
06750
06751 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
06752 } else
06753 snprintf(to, sizeof(to), "<%s>", p->uri);
06754
06755 init_req(req, sipmethod, p->uri);
06756 snprintf(tmp, sizeof(tmp), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
06757
06758 add_header(req, "Via", p->via);
06759
06760
06761
06762 if (ast_test_flag(&p->flags[0], SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
06763 build_rpid(p);
06764 add_header(req, "From", p->rpid_from);
06765 } else
06766 add_header(req, "From", from);
06767 add_header(req, "To", to);
06768 ast_string_field_set(p, exten, l);
06769 build_contact(p);
06770 add_header(req, "Contact", p->our_contact);
06771 add_header(req, "Call-ID", p->callid);
06772 add_header(req, "CSeq", tmp);
06773 if (!ast_strlen_zero(global_useragent))
06774 add_header(req, "User-Agent", global_useragent);
06775 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
06776 if (!ast_strlen_zero(p->rpid))
06777 add_header(req, "Remote-Party-ID", p->rpid);
06778 }
06779
06780
06781 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
06782 {
06783 struct sip_request req;
06784
06785 req.method = sipmethod;
06786 if (init) {
06787
06788 p->branch ^= ast_random();
06789 build_via(p);
06790 if (init > 1)
06791 initreqprep(&req, p, sipmethod);
06792 else
06793 reqprep(&req, p, sipmethod, 0, 1);
06794 } else
06795 reqprep(&req, p, sipmethod, 0, 1);
06796
06797 if (p->options && p->options->auth)
06798 add_header(&req, p->options->authheader, p->options->auth);
06799 append_date(&req);
06800 if (sipmethod == SIP_REFER) {
06801 if (p->refer) {
06802 char buf[BUFSIZ];
06803 if (!ast_strlen_zero(p->refer->refer_to))
06804 add_header(&req, "Refer-To", p->refer->refer_to);
06805 if (!ast_strlen_zero(p->refer->referred_by)) {
06806 sprintf(buf, "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
06807 add_header(&req, "Referred-By", buf);
06808 }
06809 }
06810 }
06811
06812
06813 if (p->options && p->options->replaces && !ast_strlen_zero(p->options->replaces)) {
06814 add_header(&req, "Replaces", p->options->replaces);
06815 add_header(&req, "Require", "replaces");
06816 }
06817
06818 add_header(&req, "Allow", ALLOWED_METHODS);
06819 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
06820 if (p->options && p->options->addsipheaders && p->owner) {
06821 struct ast_channel *ast = p->owner;
06822 struct varshead *headp = &ast->varshead;
06823
06824 if (!headp)
06825 ast_log(LOG_WARNING,"No Headp for the channel...ooops!\n");
06826 else {
06827 const struct ast_var_t *current;
06828 AST_LIST_TRAVERSE(headp, current, entries) {
06829
06830 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
06831 char *content, *end;
06832 const char *header = ast_var_value(current);
06833 char *headdup = ast_strdupa(header);
06834
06835
06836 if (*headdup == '"')
06837 headdup++;
06838 if ((content = strchr(headdup, ':'))) {
06839 *content++ = '\0';
06840 content = ast_skip_blanks(content);
06841
06842 end = content + strlen(content) -1;
06843 if (*end == '"')
06844 *end = '\0';
06845
06846 add_header(&req, headdup, content);
06847 if (sipdebug)
06848 ast_log(LOG_DEBUG, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
06849 }
06850 }
06851 }
06852 }
06853 }
06854 if (sdp) {
06855 if (p->udptl && p->t38.state == T38_LOCAL_DIRECT) {
06856 ast_udptl_offered_from_local(p->udptl, 1);
06857 if (option_debug)
06858 ast_log(LOG_DEBUG, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
06859 add_t38_sdp(&req, p);
06860 } else if (p->rtp)
06861 add_sdp(&req, p);
06862 } else {
06863 add_header_contentLength(&req, 0);
06864 }
06865
06866 if (!p->initreq.headers)
06867 initialize_initreq(p, &req);
06868 p->lastinvite = p->ocseq;
06869 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
06870 }
06871
06872
06873 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
06874 {
06875 char tmp[4000], from[256], to[256];
06876 char *t = tmp, *c, *mfrom, *mto;
06877 size_t maxbytes = sizeof(tmp);
06878 struct sip_request req;
06879 char hint[AST_MAX_EXTENSION];
06880 char *statestring = "terminated";
06881 const struct cfsubscription_types *subscriptiontype;
06882 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
06883 char *pidfstate = "--";
06884 char *pidfnote= "Ready";
06885
06886 memset(from, 0, sizeof(from));
06887 memset(to, 0, sizeof(to));
06888 memset(tmp, 0, sizeof(tmp));
06889
06890 switch (state) {
06891 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
06892 statestring = (global_notifyringing) ? "early" : "confirmed";
06893 local_state = NOTIFY_INUSE;
06894 pidfstate = "busy";
06895 pidfnote = "Ringing";
06896 break;
06897 case AST_EXTENSION_RINGING:
06898 statestring = "early";
06899 local_state = NOTIFY_INUSE;
06900 pidfstate = "busy";
06901 pidfnote = "Ringing";
06902 break;
06903 case AST_EXTENSION_INUSE:
06904 statestring = "confirmed";
06905 local_state = NOTIFY_INUSE;
06906 pidfstate = "busy";
06907 pidfnote = "On the phone";
06908 break;
06909 case AST_EXTENSION_BUSY:
06910 statestring = "confirmed";
06911 local_state = NOTIFY_CLOSED;
06912 pidfstate = "busy";
06913 pidfnote = "On the phone";
06914 break;
06915 case AST_EXTENSION_UNAVAILABLE:
06916 statestring = "terminated";
06917 local_state = NOTIFY_CLOSED;
06918 pidfstate = "away";
06919 pidfnote = "Unavailable";
06920 break;
06921 case AST_EXTENSION_ONHOLD:
06922 statestring = "confirmed";
06923 local_state = NOTIFY_INUSE;
06924 pidfstate = "busy";
06925 pidfnote = "On Hold";
06926 break;
06927 case AST_EXTENSION_NOT_INUSE:
06928 default:
06929
06930 break;
06931 }
06932
06933 subscriptiontype = find_subscription_type(p->subscribed);
06934
06935
06936 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
06937
06938 if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
06939 local_state = NOTIFY_CLOSED;
06940 pidfstate = "away";
06941 pidfnote = "Not online";
06942 }
06943 }
06944
06945 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
06946 c = get_in_brackets(from);
06947 if (strncmp(c, "sip:", 4)) {
06948 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
06949 return -1;
06950 }
06951 mfrom = strsep(&c, ";");
06952
06953 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
06954 c = get_in_brackets(to);
06955 if (strncmp(c, "sip:", 4)) {
06956 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
06957 return -1;
06958 }
06959 mto = strsep(&c, ";");
06960
06961 reqprep(&req, p, SIP_NOTIFY, 0, 1);
06962
06963
06964 add_header(&req, "Event", subscriptiontype->event);
06965 add_header(&req, "Content-Type", subscriptiontype->mediatype);
06966 switch(state) {
06967 case AST_EXTENSION_DEACTIVATED:
06968 if (timeout)
06969 add_header(&req, "Subscription-State", "terminated;reason=timeout");
06970 else {
06971 add_header(&req, "Subscription-State", "terminated;reason=probation");
06972 add_header(&req, "Retry-After", "60");
06973 }
06974 break;
06975 case AST_EXTENSION_REMOVED:
06976 add_header(&req, "Subscription-State", "terminated;reason=noresource");
06977 break;
06978 default:
06979 if (p->expiry)
06980 add_header(&req, "Subscription-State", "active");
06981 else
06982 add_header(&req, "Subscription-State", "terminated;reason=timeout");
06983 }
06984 switch (p->subscribed) {
06985 case XPIDF_XML:
06986 case CPIM_PIDF_XML:
06987 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
06988 ast_build_string(&t, &maxbytes, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n");
06989 ast_build_string(&t, &maxbytes, "<presence>\n");
06990 ast_build_string(&t, &maxbytes, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
06991 ast_build_string(&t, &maxbytes, "<atom id=\"%s\">\n", p->exten);
06992 ast_build_string(&t, &maxbytes, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
06993 ast_build_string(&t, &maxbytes, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
06994 ast_build_string(&t, &maxbytes, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
06995 ast_build_string(&t, &maxbytes, "</address>\n</atom>\n</presence>\n");
06996 break;
06997 case PIDF_XML:
06998 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
06999 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);
07000 ast_build_string(&t, &maxbytes, "<pp:person><status>\n");
07001 if (pidfstate[0] != '-')
07002 ast_build_string(&t, &maxbytes, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
07003 ast_build_string(&t, &maxbytes, "</status></pp:person>\n");
07004 ast_build_string(&t, &maxbytes, "<note>%s</note>\n", pidfnote);
07005 ast_build_string(&t, &maxbytes, "<tuple id=\"%s\">\n", p->exten);
07006 ast_build_string(&t, &maxbytes, "<contact priority=\"1\">%s</contact>\n", mto);
07007 if (pidfstate[0] == 'b')
07008 ast_build_string(&t, &maxbytes, "<status><basic>open</basic></status>\n");
07009 else
07010 ast_build_string(&t, &maxbytes, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
07011 ast_build_string(&t, &maxbytes, "</tuple>\n</presence>\n");
07012 break;
07013 case DIALOG_INFO_XML:
07014 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
07015 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);
07016 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
07017 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
07018 else
07019 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\">\n", p->exten);
07020 ast_build_string(&t, &maxbytes, "<state>%s</state>\n", statestring);
07021 if (state == AST_EXTENSION_ONHOLD) {
07022 ast_build_string(&t, &maxbytes, "<local>\n<target uri=\"%s\">\n"
07023 "<param pname=\"+sip.rendering\" pvalue=\"no\">\n"
07024 "</target>\n</local>\n", mto);
07025 }
07026 ast_build_string(&t, &maxbytes, "</dialog>\n</dialog-info>\n");
07027 break;
07028 case NONE:
07029 default:
07030 break;
07031 }
07032
07033 if (t > tmp + sizeof(tmp))
07034 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07035
07036 add_header_contentLength(&req, strlen(tmp));
07037 add_line(&req, tmp);
07038
07039 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07040 }
07041
07042
07043
07044
07045
07046
07047
07048 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
07049 {
07050 struct sip_request req;
07051 char tmp[500];
07052 char *t = tmp;
07053 size_t maxbytes = sizeof(tmp);
07054
07055 initreqprep(&req, p, SIP_NOTIFY);
07056 add_header(&req, "Event", "message-summary");
07057 add_header(&req, "Content-Type", default_notifymime);
07058
07059 ast_build_string(&t, &maxbytes, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
07060 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n",
07061 S_OR(vmexten, default_vmexten), S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)));
07062
07063
07064
07065 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)"));
07066
07067 if (p->subscribed) {
07068 if (p->expiry)
07069 add_header(&req, "Subscription-State", "active");
07070 else
07071 add_header(&req, "Subscription-State", "terminated;reason=timeout");
07072 }
07073
07074 if (t > tmp + sizeof(tmp))
07075 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
07076
07077 add_header_contentLength(&req, strlen(tmp));
07078 add_line(&req, tmp);
07079
07080 if (!p->initreq.headers)
07081 initialize_initreq(p, &req);
07082 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07083 }
07084
07085
07086 static int transmit_sip_request(struct sip_pvt *p, struct sip_request *req)
07087 {
07088 if (!p->initreq.headers)
07089 initialize_initreq(p, req);
07090 return send_request(p, req, XMIT_UNRELIABLE, p->ocseq);
07091 }
07092
07093
07094 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
07095 {
07096 struct sip_request req;
07097 char tmp[BUFSIZ/2];
07098
07099 reqprep(&req, p, SIP_NOTIFY, 0, 1);
07100 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
07101 add_header(&req, "Event", tmp);
07102 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
07103 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
07104 add_header(&req, "Allow", ALLOWED_METHODS);
07105 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07106
07107 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
07108 add_header_contentLength(&req, strlen(tmp));
07109 add_line(&req, tmp);
07110
07111 if (!p->initreq.headers)
07112 initialize_initreq(p, &req);
07113
07114 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07115 }
07116
07117
07118 static char *regstate2str(enum sipregistrystate regstate)
07119 {
07120 switch(regstate) {
07121 case REG_STATE_FAILED:
07122 return "Failed";
07123 case REG_STATE_UNREGISTERED:
07124 return "Unregistered";
07125 case REG_STATE_REGSENT:
07126 return "Request Sent";
07127 case REG_STATE_AUTHSENT:
07128 return "Auth. Sent";
07129 case REG_STATE_REGISTERED:
07130 return "Registered";
07131 case REG_STATE_REJECTED:
07132 return "Rejected";
07133 case REG_STATE_TIMEOUT:
07134 return "Timeout";
07135 case REG_STATE_NOAUTH:
07136 return "No Authentication";
07137 default:
07138 return "Unknown";
07139 }
07140 }
07141
07142
07143 static int sip_reregister(void *data)
07144 {
07145
07146 struct sip_registry *r= ASTOBJ_REF((struct sip_registry *) data);
07147
07148
07149 if (!r)
07150 return 0;
07151
07152 if (r->call && !ast_test_flag(&r->call->flags[0], SIP_NO_HISTORY))
07153 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
07154
07155
07156 if (sipdebug)
07157 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
07158
07159 r->expire = -1;
07160 __sip_do_register(r);
07161 ASTOBJ_UNREF(r, sip_registry_destroy);
07162 return 0;
07163 }
07164
07165
07166 static int __sip_do_register(struct sip_registry *r)
07167 {
07168 int res;
07169
07170 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
07171 return res;
07172 }
07173
07174
07175 static int sip_reg_timeout(void *data)
07176 {
07177
07178
07179 struct sip_registry *r = ASTOBJ_REF((struct sip_registry *) data);
07180 struct sip_pvt *p;
07181 int res;
07182
07183
07184 if (!r)
07185 return 0;
07186
07187 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
07188 if (r->call) {
07189
07190
07191 p = r->call;
07192 if (p->registry)
07193 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
07194 r->call = NULL;
07195 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
07196
07197 __sip_pretend_ack(p);
07198 }
07199
07200 if (global_regattempts_max && (r->regattempts > global_regattempts_max)) {
07201
07202
07203
07204 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
07205 r->regstate = REG_STATE_FAILED;
07206 } else {
07207 r->regstate = REG_STATE_UNREGISTERED;
07208 r->timeout = -1;
07209 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
07210 }
07211 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));
07212 ASTOBJ_UNREF(r, sip_registry_destroy);
07213 return 0;
07214 }
07215
07216
07217 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
07218 {
07219 struct sip_request req;
07220 char from[256];
07221 char to[256];
07222 char tmp[80];
07223 char addr[80];
07224 struct sip_pvt *p;
07225
07226
07227 if ( r == NULL || ((auth==NULL) && (r->regstate==REG_STATE_REGSENT || r->regstate==REG_STATE_AUTHSENT))) {
07228 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
07229 return 0;
07230 }
07231
07232 if (r->call) {
07233 if (!auth) {
07234 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
07235 return 0;
07236 } else {
07237 p = r->call;
07238 make_our_tag(p->tag, sizeof(p->tag));
07239 ast_string_field_free(p, theirtag);
07240 }
07241 } else {
07242
07243 if (!r->callid_valid) {
07244 build_callid_registry(r, __ourip, default_fromdomain);
07245 r->callid_valid = TRUE;
07246 }
07247
07248 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER))) {
07249 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
07250 return 0;
07251 }
07252 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
07253 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
07254
07255 if (create_addr(p, r->hostname)) {
07256
07257
07258 sip_destroy(p);
07259 if (r->timeout > -1) {
07260 ast_sched_del(sched, r->timeout);
07261 r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
07262 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
07263 } else {
07264 r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
07265 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);
07266 }
07267 r->regattempts++;
07268 return 0;
07269 }
07270
07271 ast_string_field_set(r, callid, p->callid);
07272 if (r->portno)
07273 p->sa.sin_port = htons(r->portno);
07274 else
07275 r->portno = ntohs(p->sa.sin_port);
07276 ast_set_flag(&p->flags[0], SIP_OUTGOING);
07277 r->call=p;
07278 p->registry = ASTOBJ_REF(r);
07279 if (!ast_strlen_zero(r->secret))
07280 ast_string_field_set(p, peersecret, r->secret);
07281 if (!ast_strlen_zero(r->md5secret))
07282 ast_string_field_set(p, peermd5secret, r->md5secret);
07283
07284
07285 if (!ast_strlen_zero(r->authuser)) {
07286 ast_string_field_set(p, peername, r->authuser);
07287 ast_string_field_set(p, authname, r->authuser);
07288 } else if (!ast_strlen_zero(r->username)) {
07289 ast_string_field_set(p, peername, r->username);
07290 ast_string_field_set(p, authname, r->username);
07291 ast_string_field_set(p, fromuser, r->username);
07292 }
07293 if (!ast_strlen_zero(r->username))
07294 ast_string_field_set(p, username, r->username);
07295
07296 ast_string_field_set(p, exten, r->contact);
07297
07298
07299
07300
07301
07302
07303 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
07304 p->ourip = bindaddr.sin_addr;
07305 build_contact(p);
07306 }
07307
07308
07309 if (auth == NULL) {
07310 if (r->timeout > -1) {
07311 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
07312 ast_sched_del(sched, r->timeout);
07313 }
07314 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
07315 if (option_debug)
07316 ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
07317 }
07318
07319 if (strchr(r->username, '@')) {
07320 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
07321 if (!ast_strlen_zero(p->theirtag))
07322 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
07323 else
07324 snprintf(to, sizeof(to), "<sip:%s>", r->username);
07325 } else {
07326 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
07327 if (!ast_strlen_zero(p->theirtag))
07328 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
07329 else
07330 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
07331 }
07332
07333
07334
07335 if (!ast_strlen_zero(p->fromdomain)) {
07336 if (r->portno && r->portno != STANDARD_SIP_PORT)
07337 snprintf(addr, sizeof(addr), "sip:%s:%d", p->fromdomain, r->portno);
07338 else
07339 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
07340 } else {
07341 if (r->portno && r->portno != STANDARD_SIP_PORT)
07342 snprintf(addr, sizeof(addr), "sip:%s:%d", r->hostname, r->portno);
07343 else
07344 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
07345 }
07346 ast_string_field_set(p, uri, addr);
07347
07348 p->branch ^= ast_random();
07349
07350 init_req(&req, sipmethod, addr);
07351
07352
07353 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
07354 p->ocseq = r->ocseq;
07355
07356 build_via(p);
07357 add_header(&req, "Via", p->via);
07358 add_header(&req, "From", from);
07359 add_header(&req, "To", to);
07360 add_header(&req, "Call-ID", p->callid);
07361 add_header(&req, "CSeq", tmp);
07362 if (!ast_strlen_zero(global_useragent))
07363 add_header(&req, "User-Agent", global_useragent);
07364 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07365
07366
07367 if (auth)
07368 add_header(&req, authheader, auth);
07369 else if (!ast_strlen_zero(r->nonce)) {
07370 char digest[1024];
07371
07372
07373 if (sipdebug)
07374 ast_log(LOG_DEBUG, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
07375 ast_string_field_set(p, realm, r->realm);
07376 ast_string_field_set(p, nonce, r->nonce);
07377 ast_string_field_set(p, domain, r->domain);
07378 ast_string_field_set(p, opaque, r->opaque);
07379 ast_string_field_set(p, qop, r->qop);
07380 r->noncecount++;
07381 p->noncecount = r->noncecount;
07382
07383 memset(digest,0,sizeof(digest));
07384 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
07385 add_header(&req, "Authorization", digest);
07386 else
07387 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
07388
07389 }
07390
07391 snprintf(tmp, sizeof(tmp), "%d", default_expiry);
07392 add_header(&req, "Expires", tmp);
07393 add_header(&req, "Contact", p->our_contact);
07394 add_header(&req, "Event", "registration");
07395 add_header_contentLength(&req, 0);
07396
07397 initialize_initreq(p, &req);
07398 if (sip_debug_test_pvt(p))
07399 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
07400 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
07401 r->regattempts++;
07402 if (option_debug > 3)
07403 ast_verbose("REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
07404 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
07405 }
07406
07407
07408 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
07409 {
07410 struct sip_request req;
07411
07412 reqprep(&req, p, SIP_MESSAGE, 0, 1);
07413 add_text(&req, text);
07414 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07415 }
07416
07417
07418 static int sip_refer_allocate(struct sip_pvt *p)
07419 {
07420 p->refer = ast_calloc(1, sizeof(struct sip_refer));
07421 return p->refer ? 1 : 0;
07422 }
07423
07424
07425
07426
07427
07428
07429 static int transmit_refer(struct sip_pvt *p, const char *dest)
07430 {
07431 struct sip_request req = {
07432 .headers = 0,
07433 };
07434 char from[256];
07435 const char *of;
07436 char *c;
07437 char referto[256];
07438 char *ttag, *ftag;
07439 char *theirtag = ast_strdupa(p->theirtag);
07440
07441 if (option_debug || sipdebug)
07442 ast_log(LOG_DEBUG, "SIP transfer of %s to %s\n", p->callid, dest);
07443
07444
07445 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
07446 of = get_header(&p->initreq, "To");
07447 ttag = theirtag;
07448 ftag = p->tag;
07449 } else {
07450 of = get_header(&p->initreq, "From");
07451 ftag = theirtag;
07452 ttag = p->tag;
07453 }
07454
07455 ast_copy_string(from, of, sizeof(from));
07456 of = get_in_brackets(from);
07457 ast_string_field_set(p, from, of);
07458 if (strncmp(of, "sip:", 4))
07459 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
07460 else
07461 of += 4;
07462
07463 if ((c = strchr(dest, '@')))
07464 c = NULL;
07465 else if ((c = strchr(of, '@')))
07466 *c++ = '\0';
07467 if (c)
07468 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
07469 else
07470 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
07471
07472
07473 sip_refer_allocate(p);
07474 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
07475 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
07476 p->refer->status = REFER_SENT;
07477
07478 reqprep(&req, p, SIP_REFER, 0, 1);
07479 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
07480
07481 add_header(&req, "Refer-To", referto);
07482 add_header(&req, "Allow", ALLOWED_METHODS);
07483 add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
07484 if (!ast_strlen_zero(p->our_contact))
07485 add_header(&req, "Referred-By", p->our_contact);
07486
07487 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07488
07489
07490
07491
07492
07493
07494
07495
07496 }
07497
07498
07499
07500 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
07501 {
07502 struct sip_request req;
07503
07504 reqprep(&req, p, SIP_INFO, 0, 1);
07505 add_digit(&req, digit, duration);
07506 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07507 }
07508
07509
07510 static int transmit_info_with_vidupdate(struct sip_pvt *p)
07511 {
07512 struct sip_request req;
07513
07514 reqprep(&req, p, SIP_INFO, 0, 1);
07515 add_vidupdate(&req);
07516 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
07517 }
07518
07519
07520 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
07521 {
07522 struct sip_request resp;
07523
07524 if (sipmethod == SIP_ACK)
07525 p->invitestate = INV_CONFIRMED;
07526
07527 reqprep(&resp, p, sipmethod, seqno, newbranch);
07528 add_header_contentLength(&resp, 0);
07529 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
07530 }
07531
07532
07533 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
07534 {
07535 struct sip_request resp;
07536
07537 reqprep(&resp, p, sipmethod, seqno, newbranch);
07538 if (!ast_strlen_zero(p->realm)) {
07539 char digest[1024];
07540
07541 memset(digest, 0, sizeof(digest));
07542 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
07543 if (p->options && p->options->auth_type == PROXY_AUTH)
07544 add_header(&resp, "Proxy-Authorization", digest);
07545 else if (p->options && p->options->auth_type == WWW_AUTH)
07546 add_header(&resp, "Authorization", digest);
07547 else
07548 add_header(&resp, "Proxy-Authorization", digest);
07549 } else
07550 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
07551 }
07552
07553
07554 if (sipmethod == SIP_BYE && p->owner && p->owner->hangupcause) {
07555 char buf[10];
07556
07557 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
07558 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
07559 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
07560 }
07561
07562 add_header_contentLength(&resp, 0);
07563 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
07564 }
07565
07566
07567 static void destroy_association(struct sip_peer *peer)
07568 {
07569 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE)) {
07570 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
07571 ast_update_realtime("sippeers", "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "username", "", "regserver", "", NULL);
07572 else
07573 ast_db_del("SIP/Registry", peer->name);
07574 }
07575 }
07576
07577
07578 static int expire_register(void *data)
07579 {
07580 struct sip_peer *peer = data;
07581
07582 if (!peer)
07583 return 0;
07584
07585 memset(&peer->addr, 0, sizeof(peer->addr));
07586
07587 destroy_association(peer);
07588
07589 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
07590 register_peer_exten(peer, FALSE);
07591 peer->expire = -1;
07592 ast_device_state_changed("SIP/%s", peer->name);
07593
07594
07595
07596
07597 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT) ||
07598 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
07599 peer = ASTOBJ_CONTAINER_UNLINK(&peerl, peer);
07600 ASTOBJ_UNREF(peer, sip_destroy_peer);
07601 }
07602
07603 return 0;
07604 }
07605
07606
07607 static int sip_poke_peer_s(void *data)
07608 {
07609 struct sip_peer *peer = data;
07610
07611 peer->pokeexpire = -1;
07612 sip_poke_peer(peer);
07613 return 0;
07614 }
07615
07616
07617 static void reg_source_db(struct sip_peer *peer)
07618 {
07619 char data[256];
07620 struct in_addr in;
07621 int expiry;
07622 int port;
07623 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
07624
07625 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
07626 return;
07627 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
07628 return;
07629
07630 scan = data;
07631 addr = strsep(&scan, ":");
07632 port_str = strsep(&scan, ":");
07633 expiry_str = strsep(&scan, ":");
07634 username = strsep(&scan, ":");
07635 contact = scan;
07636
07637 if (!inet_aton(addr, &in))
07638 return;
07639
07640 if (port_str)
07641 port = atoi(port_str);
07642 else
07643 return;
07644
07645 if (expiry_str)
07646 expiry = atoi(expiry_str);
07647 else
07648 return;
07649
07650 if (username)
07651 ast_copy_string(peer->username, username, sizeof(peer->username));
07652 if (contact)
07653 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
07654
07655 if (option_debug > 1)
07656 ast_log(LOG_DEBUG, "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
07657 peer->name, peer->username, ast_inet_ntoa(in), port, expiry);
07658
07659 memset(&peer->addr, 0, sizeof(peer->addr));
07660 peer->addr.sin_family = AF_INET;
07661 peer->addr.sin_addr = in;
07662 peer->addr.sin_port = htons(port);
07663 if (sipsock < 0) {
07664
07665 if (peer->pokeexpire > -1)
07666 ast_sched_del(sched, peer->pokeexpire);
07667 peer->pokeexpire = ast_sched_add(sched, ast_random() % 5000 + 1, sip_poke_peer_s, peer);
07668 } else
07669 sip_poke_peer(peer);
07670 if (peer->expire > -1)
07671 ast_sched_del(sched, peer->expire);
07672 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, peer);
07673 register_peer_exten(peer, TRUE);
07674 }
07675
07676
07677 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
07678 {
07679 char contact[BUFSIZ];
07680 char *c;
07681
07682
07683 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
07684 c = get_in_brackets(contact);
07685
07686
07687 ast_string_field_set(pvt, fullcontact, c);
07688
07689
07690 ast_string_field_set(pvt, okcontacturi, c);
07691
07692
07693
07694 return TRUE;
07695 }
07696
07697
07698 static int set_address_from_contact(struct sip_pvt *pvt)
07699 {
07700 struct hostent *hp;
07701 struct ast_hostent ahp;
07702 int port;
07703 char *c, *host, *pt;
07704 char *contact;
07705
07706
07707 if (ast_test_flag(&pvt->flags[0], SIP_NAT_ROUTE)) {
07708
07709
07710 pvt->sa = pvt->recv;
07711 return 0;
07712 }
07713
07714
07715
07716 contact = ast_strdupa(pvt->fullcontact);
07717
07718
07719
07720 if (strncasecmp(contact, "sip:", 4)) {
07721 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
07722 } else
07723 contact += 4;
07724
07725
07726
07727 contact = strsep(&contact, ";");
07728
07729
07730 host = strchr(contact, '@');
07731 if (!host) {
07732 host = contact;
07733 c = NULL;
07734 } else {
07735 *host++ = '\0';
07736 }
07737 pt = strchr(host, ':');
07738 if (pt) {
07739 *pt++ = '\0';
07740 port = atoi(pt);
07741 } else
07742 port = STANDARD_SIP_PORT;
07743
07744
07745
07746 hp = ast_gethostbyname(host, &ahp);
07747 if (!hp) {
07748 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't resolve in DNS) : '%s'\n", host);
07749 return -1;
07750 }
07751 pvt->sa.sin_family = AF_INET;
07752 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
07753 pvt->sa.sin_port = htons(port);
07754
07755 return 0;
07756 }
07757
07758
07759
07760 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
07761 {
07762 char contact[BUFSIZ];
07763 char data[BUFSIZ];
07764 const char *expires = get_header(req, "Expires");
07765 int expiry = atoi(expires);
07766 char *curi, *n, *pt;
07767 int port;
07768 const char *useragent;
07769 struct hostent *hp;
07770 struct ast_hostent ahp;
07771 struct sockaddr_in oldsin;
07772
07773 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
07774
07775 if (ast_strlen_zero(expires)) {
07776 expires = strcasestr(contact, ";expires=");
07777 if (expires) {
07778
07779 expires = strsep((char **) &expires, ";");
07780 if (sscanf(expires + 9, "%d", &expiry) != 1)
07781 expiry = default_expiry;
07782 } else {
07783
07784 expiry = default_expiry;
07785 }
07786 }
07787
07788
07789 curi = contact;
07790 if (strchr(contact, '<') == NULL)
07791 strsep(&curi, ";");
07792 curi = get_in_brackets(contact);
07793
07794
07795
07796
07797
07798 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
07799
07800 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact))
07801 pvt->expiry = ast_sched_when(sched, peer->expire);
07802 return PARSE_REGISTER_QUERY;
07803 } else if (!strcasecmp(curi, "*") || !expiry) {
07804
07805 memset(&peer->addr, 0, sizeof(peer->addr));
07806 if (peer->expire > -1)
07807 ast_sched_del(sched, peer->expire);
07808 peer->expire = -1;
07809
07810 destroy_association(peer);
07811
07812 register_peer_exten(peer, 0);
07813 peer->fullcontact[0] = '\0';
07814 peer->useragent[0] = '\0';
07815 peer->sipoptions = 0;
07816 peer->lastms = 0;
07817
07818 if (option_verbose > 2)
07819 ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", peer->name);
07820 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\n", peer->name);
07821 return PARSE_REGISTER_UPDATE;
07822 }
07823
07824
07825 ast_copy_string(peer->fullcontact, curi, sizeof(peer->fullcontact));
07826
07827
07828 ast_string_field_build(pvt, our_contact, "<%s>", curi);
07829
07830
07831 if (strncasecmp(curi, "sip:", 4)) {
07832 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", curi);
07833 } else
07834 curi += 4;
07835
07836 curi = strsep(&curi, ";");
07837
07838 n = strchr(curi, '@');
07839 if (!n) {
07840 n = curi;
07841 curi = NULL;
07842 } else
07843 *n++ = '\0';
07844 pt = strchr(n, ':');
07845 if (pt) {
07846 *pt++ = '\0';
07847 port = atoi(pt);
07848 } else
07849 port = STANDARD_SIP_PORT;
07850 oldsin = peer->addr;
07851 if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
07852
07853 hp = ast_gethostbyname(n, &ahp);
07854 if (!hp) {
07855 ast_log(LOG_WARNING, "Invalid host '%s'\n", n);
07856 return PARSE_REGISTER_FAILED;
07857 }
07858 peer->addr.sin_family = AF_INET;
07859 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
07860 peer->addr.sin_port = htons(port);
07861 } else {
07862
07863
07864 peer->addr = pvt->recv;
07865 }
07866
07867
07868 peer->sipoptions = pvt->sipoptions;
07869
07870 if (curi)
07871 ast_copy_string(peer->username, curi, sizeof(peer->username));
07872 else
07873 peer->username[0] = '\0';
07874
07875 if (peer->expire > -1) {
07876 ast_sched_del(sched, peer->expire);
07877 peer->expire = -1;
07878 }
07879 if (expiry > max_expiry)
07880 expiry = max_expiry;
07881 if (expiry < min_expiry)
07882 expiry = min_expiry;
07883 peer->expire = ast_test_flag(&peer->flags[0], SIP_REALTIME) ? -1 :
07884 ast_sched_add(sched, (expiry + 10) * 1000, expire_register, peer);
07885 pvt->expiry = expiry;
07886 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);
07887 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT))
07888 ast_db_put("SIP/Registry", peer->name, data);
07889 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
07890
07891
07892 if (inaddrcmp(&peer->addr, &oldsin)) {
07893 sip_poke_peer(peer);
07894 if (option_verbose > 2)
07895 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);
07896 register_peer_exten(peer, 1);
07897 }
07898
07899
07900 useragent = get_header(req, "User-Agent");
07901 if (strcasecmp(useragent, peer->useragent)) {
07902 ast_copy_string(peer->useragent, useragent, sizeof(peer->useragent));
07903 if (option_verbose > 3)
07904 ast_verbose(VERBOSE_PREFIX_3 "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
07905 }
07906 return PARSE_REGISTER_UPDATE;
07907 }
07908
07909
07910 static void free_old_route(struct sip_route *route)
07911 {
07912 struct sip_route *next;
07913
07914 while (route) {
07915 next = route->next;
07916 free(route);
07917 route = next;
07918 }
07919 }
07920
07921
07922 static void list_route(struct sip_route *route)
07923 {
07924 if (!route)
07925 ast_verbose("list_route: no route\n");
07926 else {
07927 for (;route; route = route->next)
07928 ast_verbose("list_route: hop: <%s>\n", route->hop);
07929 }
07930 }
07931
07932
07933 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
07934 {
07935 struct sip_route *thishop, *head, *tail;
07936 int start = 0;
07937 int len;
07938 const char *rr, *contact, *c;
07939
07940
07941 if (p->route && p->route_persistant) {
07942 if (option_debug)
07943 ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
07944 return;
07945 }
07946
07947 if (p->route) {
07948 free_old_route(p->route);
07949 p->route = NULL;
07950 }
07951
07952 p->route_persistant = backwards;
07953
07954
07955
07956
07957
07958
07959 head = NULL;
07960 tail = head;
07961
07962 for (;;) {
07963
07964 rr = __get_header(req, "Record-Route", &start);
07965 if (*rr == '\0')
07966 break;
07967 for (; (rr = strchr(rr, '<')) ; rr += len) {
07968 ++rr;
07969 len = strcspn(rr, ">") + 1;
07970
07971 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
07972
07973 ast_copy_string(thishop->hop, rr, len);
07974 if (option_debug > 1)
07975 ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
07976
07977 if (backwards) {
07978
07979 thishop->next = head;
07980 head = thishop;
07981
07982 if (!tail)
07983 tail = thishop;
07984 } else {
07985 thishop->next = NULL;
07986
07987 if (tail)
07988 tail->next = thishop;
07989 else
07990 head = thishop;
07991 tail = thishop;
07992 }
07993 }
07994 }
07995 }
07996
07997
07998 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop,";lr") == NULL) ) {
07999
08000
08001 contact = get_header(req, "Contact");
08002 if (!ast_strlen_zero(contact)) {
08003 if (option_debug > 1)
08004 ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
08005
08006 c = strchr(contact, '<');
08007 if (c) {
08008
08009 ++c;
08010 len = strcspn(c, ">") + 1;
08011 } else {
08012
08013 c = contact;
08014 len = strlen(contact) + 1;
08015 }
08016 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
08017
08018 ast_copy_string(thishop->hop, c, len);
08019 thishop->next = NULL;
08020
08021 if (tail)
08022 tail->next = thishop;
08023 else
08024 head = thishop;
08025 }
08026 }
08027 }
08028
08029
08030 p->route = head;
08031
08032
08033 if (sip_debug_test_pvt(p))
08034 list_route(p->route);
08035 }
08036
08037
08038
08039
08040
08041
08042
08043 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
08044 const char *secret, const char *md5secret, int sipmethod,
08045 char *uri, enum xmittype reliable, int ignore)
08046 {
08047 const char *response = "407 Proxy Authentication Required";
08048 const char *reqheader = "Proxy-Authorization";
08049 const char *respheader = "Proxy-Authenticate";
08050 const char *authtoken;
08051 char a1_hash[256];
08052 char resp_hash[256]="";
08053 char tmp[BUFSIZ * 2];
08054 char *c;
08055 int wrongnonce = FALSE;
08056 int good_response;
08057 const char *usednonce = p->randdata;
08058
08059
08060 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
08061 struct x {
08062 const char *key;
08063 const char *s;
08064 } *i, keys[] = {
08065 [K_RESP] = { "response=", "" },
08066 [K_URI] = { "uri=", "" },
08067 [K_USER] = { "username=", "" },
08068 [K_NONCE] = { "nonce=", "" },
08069 [K_LAST] = { NULL, NULL}
08070 };
08071
08072
08073 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
08074 return AUTH_SUCCESSFUL;
08075 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
08076
08077
08078
08079 response = "401 Unauthorized";
08080 reqheader = "Authorization";
08081 respheader = "WWW-Authenticate";
08082 }
08083 authtoken = get_header(req, reqheader);
08084 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
08085
08086
08087 if (!reliable) {
08088
08089
08090 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08091
08092 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08093 }
08094 return AUTH_CHALLENGE_SENT;
08095 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
08096
08097 ast_string_field_build(p, randdata, "%08lx", ast_random());
08098 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08099
08100 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08101 return AUTH_CHALLENGE_SENT;
08102 }
08103
08104
08105
08106
08107
08108
08109
08110
08111 ast_copy_string(tmp, authtoken, sizeof(tmp));
08112 c = tmp;
08113
08114 while(c && *(c = ast_skip_blanks(c)) ) {
08115 for (i = keys; i->key != NULL; i++) {
08116 const char *separator = ",";
08117
08118 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
08119 continue;
08120
08121 c += strlen(i->key);
08122 if (*c == '"') {
08123 c++;
08124 separator = "\"";
08125 }
08126 i->s = c;
08127 strsep(&c, separator);
08128 break;
08129 }
08130 if (i->key == NULL)
08131 strsep(&c, " ,");
08132 }
08133
08134
08135 if (strcmp(username, keys[K_USER].s)) {
08136 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
08137 username, keys[K_USER].s);
08138
08139 return AUTH_USERNAME_MISMATCH;
08140 }
08141
08142
08143 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
08144 wrongnonce = TRUE;
08145 usednonce = keys[K_NONCE].s;
08146 }
08147
08148 if (!ast_strlen_zero(md5secret))
08149 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
08150 else {
08151 char a1[256];
08152 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
08153 ast_md5_hash(a1_hash, a1);
08154 }
08155
08156
08157 {
08158 char a2[256];
08159 char a2_hash[256];
08160 char resp[256];
08161
08162 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
08163 S_OR(keys[K_URI].s, uri));
08164 ast_md5_hash(a2_hash, a2);
08165 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
08166 ast_md5_hash(resp_hash, resp);
08167 }
08168
08169 good_response = keys[K_RESP].s &&
08170 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
08171 if (wrongnonce) {
08172 ast_string_field_build(p, randdata, "%08lx", ast_random());
08173 if (good_response) {
08174 if (sipdebug)
08175 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "To"));
08176
08177 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
08178 } else {
08179
08180 if (sipdebug)
08181 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
08182 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
08183 }
08184
08185
08186 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08187 return AUTH_CHALLENGE_SENT;
08188 }
08189 if (good_response) {
08190 append_history(p, "AuthOK", "Auth challenge succesful for %s", username);
08191 return AUTH_SUCCESSFUL;
08192 }
08193
08194
08195
08196 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
08197 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08198
08199 return AUTH_CHALLENGE_SENT;
08200 }
08201
08202
08203 static void sip_peer_hold(struct sip_pvt *p, int hold)
08204 {
08205 struct sip_peer *peer = find_peer(p->peername, NULL, 1);
08206
08207 if (!peer)
08208 return;
08209
08210
08211 if (hold)
08212 peer->onHold++;
08213 else
08214 peer->onHold--;
08215
08216
08217 ast_device_state_changed("SIP/%s", peer->name);
08218
08219 return;
08220 }
08221
08222
08223
08224
08225 static int cb_extensionstate(char *context, char* exten, int state, void *data)
08226 {
08227 struct sip_pvt *p = data;
08228
08229 switch(state) {
08230 case AST_EXTENSION_DEACTIVATED:
08231 case AST_EXTENSION_REMOVED:
08232 if (p->autokillid > -1)
08233 sip_cancel_destroy(p);
08234 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
08235 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);
08236 p->stateid = -1;
08237 p->subscribed = NONE;
08238 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
08239 break;
08240 default:
08241 p->laststate = state;
08242 break;
08243 }
08244 if (p->subscribed != NONE)
08245 transmit_state_notify(p, state, 1, FALSE);
08246
08247 if (option_verbose > 1)
08248 ast_verbose(VERBOSE_PREFIX_1 "Extension Changed %s new state %s for Notify User %s\n", exten, ast_extension_state2str(state), p->username);
08249 return 0;
08250 }
08251
08252
08253
08254
08255 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, int reliable)
08256 {
08257 ast_string_field_build(p, randdata, "%08lx", ast_random());
08258 transmit_response_with_auth(p, "401 Unauthorized", req, p->randdata, reliable, "WWW-Authenticate", 0);
08259 }
08260
08261
08262
08263
08264
08265
08266 static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr_in *sin,
08267 struct sip_request *req, char *uri)
08268 {
08269 enum check_auth_result res = AUTH_NOT_FOUND;
08270 struct sip_peer *peer;
08271 char tmp[256];
08272 char *name, *c;
08273 char *t;
08274 char *domain;
08275
08276
08277 t = uri;
08278 while(*t && (*t > 32) && (*t != ';'))
08279 t++;
08280 *t = '\0';
08281
08282 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
08283 if (pedanticsipchecking)
08284 ast_uri_decode(tmp);
08285
08286 c = get_in_brackets(tmp);
08287 c = strsep(&c, ";");
08288
08289 if (!strncmp(c, "sip:", 4)) {
08290 name = c + 4;
08291 } else {
08292 name = c;
08293 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(sin->sin_addr));
08294 }
08295
08296
08297 if ((c = strchr(name, '@'))) {
08298 *c++ = '\0';
08299 domain = c;
08300 if ((c = strchr(domain, ':')))
08301 *c = '\0';
08302 if (!AST_LIST_EMPTY(&domain_list)) {
08303 if (!check_sip_domain(domain, NULL, 0)) {
08304 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
08305 return AUTH_UNKNOWN_DOMAIN;
08306 }
08307 }
08308 }
08309
08310 ast_string_field_set(p, exten, name);
08311 build_contact(p);
08312 peer = find_peer(name, NULL, 1);
08313 if (!(peer && ast_apply_ha(peer->ha, sin))) {
08314
08315 if (peer)
08316 ASTOBJ_UNREF(peer, sip_destroy_peer);
08317 peer = NULL;
08318 }
08319 if (peer) {
08320
08321 if (p->rtp) {
08322 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
08323 p->autoframing = peer->autoframing;
08324 }
08325 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
08326 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
08327 } else {
08328 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
08329 transmit_response(p, "100 Trying", req);
08330 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, ast_test_flag(req, SIP_PKT_IGNORE)))) {
08331 sip_cancel_destroy(p);
08332
08333
08334
08335 switch (parse_register_contact(p, peer, req)) {
08336 case PARSE_REGISTER_FAILED:
08337 ast_log(LOG_WARNING, "Failed to parse contact info\n");
08338 transmit_response_with_date(p, "400 Bad Request", req);
08339 peer->lastmsgssent = -1;
08340 res = 0;
08341 break;
08342 case PARSE_REGISTER_QUERY:
08343 transmit_response_with_date(p, "200 OK", req);
08344 peer->lastmsgssent = -1;
08345 res = 0;
08346 break;
08347 case PARSE_REGISTER_UPDATE:
08348 update_peer(peer, p->expiry);
08349
08350 transmit_response_with_date(p, "200 OK", req);
08351 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
08352 peer->lastmsgssent = -1;
08353 res = 0;
08354 break;
08355 }
08356 }
08357 }
08358 }
08359 if (!peer && autocreatepeer) {
08360
08361 peer = temp_peer(name);
08362 if (peer) {
08363 ASTOBJ_CONTAINER_LINK(&peerl, peer);
08364 sip_cancel_destroy(p);
08365 switch (parse_register_contact(p, peer, req)) {
08366 case PARSE_REGISTER_FAILED:
08367 ast_log(LOG_WARNING, "Failed to parse contact info\n");
08368 transmit_response_with_date(p, "400 Bad Request", req);
08369 peer->lastmsgssent = -1;
08370 res = 0;
08371 break;
08372 case PARSE_REGISTER_QUERY:
08373 transmit_response_with_date(p, "200 OK", req);
08374 peer->lastmsgssent = -1;
08375 res = 0;
08376 break;
08377 case PARSE_REGISTER_UPDATE:
08378
08379 transmit_response_with_date(p, "200 OK", req);
08380 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
08381 peer->lastmsgssent = -1;
08382 res = 0;
08383 break;
08384 }
08385 }
08386 }
08387 if (!res) {
08388 ast_device_state_changed("SIP/%s", peer->name);
08389 }
08390 if (res < 0) {
08391 switch (res) {
08392 case AUTH_SECRET_FAILED:
08393
08394 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
08395 break;
08396 case AUTH_USERNAME_MISMATCH:
08397
08398
08399
08400
08401 transmit_response(p, "403 Authentication user name does not match account name", &p->initreq);
08402 break;
08403 case AUTH_NOT_FOUND:
08404 if (global_alwaysauthreject) {
08405 transmit_fake_auth_response(p, &p->initreq, 1);
08406 } else {
08407
08408 transmit_response(p, "404 Not found", &p->initreq);
08409 }
08410 break;
08411 default:
08412 break;
08413 }
08414 if (option_debug > 1) {
08415 const char *reason = "";
08416
08417 switch (res) {
08418 case AUTH_SECRET_FAILED:
08419 reason = "Bad password";
08420 break;
08421 case AUTH_USERNAME_MISMATCH:
08422 reason = "Bad digest user";
08423 break;
08424 case AUTH_NOT_FOUND:
08425 reason = "Peer not found";
08426 break;
08427 default:
08428 break;
08429 }
08430 ast_log(LOG_DEBUG, "SIP REGISTER attempt failed for %s : %s\n",
08431 peer->name, reason);
08432 }
08433 }
08434 if (peer)
08435 ASTOBJ_UNREF(peer, sip_destroy_peer);
08436
08437 return res;
08438 }
08439
08440
08441 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
08442 {
08443 char tmp[256], *c, *a;
08444 struct sip_request *req;
08445
08446 req = oreq;
08447 if (!req)
08448 req = &p->initreq;
08449 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
08450 if (ast_strlen_zero(tmp))
08451 return 0;
08452 c = get_in_brackets(tmp);
08453 if (strncmp(c, "sip:", 4)) {
08454 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", c);
08455 return -1;
08456 }
08457 c += 4;
08458 a = c;
08459 strsep(&a, "@;");
08460 if (sip_debug_test_pvt(p))
08461 ast_verbose("RDNIS is %s\n", c);
08462 ast_string_field_set(p, rdnis, c);
08463
08464 return 0;
08465 }
08466
08467
08468
08469
08470 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
08471 {
08472 char tmp[256] = "", *uri, *a;
08473 char tmpf[256] = "", *from;
08474 struct sip_request *req;
08475 char *colon;
08476
08477 req = oreq;
08478 if (!req)
08479 req = &p->initreq;
08480
08481
08482 if (req->rlPart2)
08483 ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
08484
08485 if (pedanticsipchecking)
08486 ast_uri_decode(tmp);
08487
08488 uri = get_in_brackets(tmp);
08489
08490 if (strncmp(uri, "sip:", 4)) {
08491 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
08492 return -1;
08493 }
08494 uri += 4;
08495
08496
08497 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
08498 if (!ast_strlen_zero(tmpf)) {
08499 if (pedanticsipchecking)
08500 ast_uri_decode(tmpf);
08501 from = get_in_brackets(tmpf);
08502 } else {
08503 from = NULL;
08504 }
08505
08506 if (!ast_strlen_zero(from)) {
08507 if (strncmp(from, "sip:", 4)) {
08508 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
08509 return -1;
08510 }
08511 from += 4;
08512 if ((a = strchr(from, '@')))
08513 *a++ = '\0';
08514 else
08515 a = from;
08516 from = strsep(&from, ";");
08517 a = strsep(&a, ";");
08518 ast_string_field_set(p, fromdomain, a);
08519 }
08520
08521
08522
08523
08524 if ((a = strchr(uri, '@'))) {
08525 *a++ = '\0';
08526 } else {
08527 a = uri;
08528 uri = "s";
08529 }
08530 colon = strchr(a, ':');
08531 if (colon)
08532 *colon = '\0';
08533
08534 uri = strsep(&uri, ";");
08535 a = strsep(&a, ";");
08536
08537 ast_string_field_set(p, domain, a);
08538
08539 if (!AST_LIST_EMPTY(&domain_list)) {
08540 char domain_context[AST_MAX_EXTENSION];
08541
08542 domain_context[0] = '\0';
08543 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
08544 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
08545 if (option_debug)
08546 ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
08547 return -2;
08548 }
08549 }
08550
08551 if (!ast_strlen_zero(domain_context))
08552 ast_string_field_set(p, context, domain_context);
08553 }
08554
08555 if (sip_debug_test_pvt(p))
08556 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
08557
08558
08559
08560
08561 if (ast_exists_extension(NULL, p->context, uri, 1, from) ||
08562 !strcmp(uri, ast_pickup_ext())) {
08563 if (!oreq)
08564 ast_string_field_set(p, exten, uri);
08565 return 0;
08566 }
08567
08568
08569 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
08570 ast_canmatch_extension(NULL, p->context, uri, 1, from)) ||
08571 !strncmp(uri, ast_pickup_ext(), strlen(uri))) {
08572 return 1;
08573 }
08574
08575 return -1;
08576 }
08577
08578
08579
08580
08581
08582
08583 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
08584 {
08585 struct sip_pvt *sip_pvt_ptr;
08586
08587 ast_mutex_lock(&iflock);
08588
08589 if (option_debug > 3 && totag)
08590 ast_log(LOG_DEBUG, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
08591
08592
08593 for (sip_pvt_ptr = iflist; sip_pvt_ptr; sip_pvt_ptr = sip_pvt_ptr->next) {
08594 if (!strcmp(sip_pvt_ptr->callid, callid)) {
08595 int match = 1;
08596 char *ourtag = sip_pvt_ptr->tag;
08597
08598
08599 ast_mutex_lock(&sip_pvt_ptr->lock);
08600
08601
08602
08603
08604
08605 if (pedanticsipchecking && (strcmp(fromtag, sip_pvt_ptr->theirtag) || strcmp(totag, ourtag)))
08606 match = 0;
08607
08608 if (!match) {
08609 ast_mutex_unlock(&sip_pvt_ptr->lock);
08610 continue;
08611 }
08612
08613 if (option_debug > 3 && totag)
08614 ast_log(LOG_DEBUG, "Matched %s call - their tag is %s Our tag is %s\n",
08615 ast_test_flag(&sip_pvt_ptr->flags[0], SIP_OUTGOING) ? "OUTGOING": "INCOMING",
08616 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
08617
08618
08619 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
08620 ast_mutex_unlock(&sip_pvt_ptr->lock);
08621 usleep(1);
08622 ast_mutex_lock(&sip_pvt_ptr->lock);
08623 }
08624 break;
08625 }
08626 }
08627 ast_mutex_unlock(&iflock);
08628 if (option_debug > 3 && !sip_pvt_ptr)
08629 ast_log(LOG_DEBUG, "Found no match for callid %s to-tag %s from-tag %s\n", callid, totag, fromtag);
08630 return sip_pvt_ptr;
08631 }
08632
08633
08634
08635 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
08636 {
08637
08638 const char *p_referred_by = NULL;
08639 char *h_refer_to = NULL;
08640 char *h_referred_by = NULL;
08641 char *refer_to;
08642 const char *p_refer_to;
08643 char *referred_by_uri = NULL;
08644 char *ptr;
08645 struct sip_request *req = NULL;
08646 const char *transfer_context = NULL;
08647 struct sip_refer *referdata;
08648
08649
08650 req = outgoing_req;
08651 referdata = transferer->refer;
08652
08653 if (!req)
08654 req = &transferer->initreq;
08655
08656 p_refer_to = get_header(req, "Refer-To");
08657 if (ast_strlen_zero(p_refer_to)) {
08658 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
08659 return -2;
08660 }
08661 h_refer_to = ast_strdupa(p_refer_to);
08662 refer_to = get_in_brackets(h_refer_to);
08663 if (pedanticsipchecking)
08664 ast_uri_decode(refer_to);
08665
08666 if (strncasecmp(refer_to, "sip:", 4)) {
08667 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
08668 return -3;
08669 }
08670 refer_to += 4;
08671
08672
08673 p_referred_by = get_header(req, "Referred-By");
08674 if (!ast_strlen_zero(p_referred_by)) {
08675 char *lessthan;
08676 h_referred_by = ast_strdupa(p_referred_by);
08677 if (pedanticsipchecking)
08678 ast_uri_decode(h_referred_by);
08679
08680
08681 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
08682 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
08683 *(lessthan - 1) = '\0';
08684 }
08685
08686 referred_by_uri = get_in_brackets(h_referred_by);
08687 if(strncasecmp(referred_by_uri, "sip:", 4)) {
08688 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
08689 referred_by_uri = (char *) NULL;
08690 } else {
08691 referred_by_uri += 4;
08692 }
08693 }
08694
08695
08696 if ((ptr = strchr(refer_to, '?'))) {
08697 *ptr++ = '\0';
08698 if (!strncasecmp(ptr, "REPLACES=", 9)) {
08699 char *to = NULL, *from = NULL;
08700
08701
08702 referdata->attendedtransfer = 1;
08703 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
08704 ast_uri_decode(referdata->replaces_callid);
08705 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
08706 *ptr++ = '\0';
08707 }
08708
08709 if (ptr) {
08710
08711 to = strcasestr(ptr, "to-tag=");
08712 from = strcasestr(ptr, "from-tag=");
08713 }
08714
08715
08716 if (to) {
08717 ptr = to + 7;
08718 if ((to = strchr(ptr, '&')))
08719 *to = '\0';
08720 if ((to = strchr(ptr, ';')))
08721 *to = '\0';
08722 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
08723 }
08724
08725 if (from) {
08726 ptr = from + 9;
08727 if ((to = strchr(ptr, '&')))
08728 *to = '\0';
08729 if ((to = strchr(ptr, ';')))
08730 *to = '\0';
08731 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
08732 }
08733
08734 if (option_debug > 1) {
08735 if (!pedanticsipchecking)
08736 ast_log(LOG_DEBUG,"Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
08737 else
08738 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>" );
08739 }
08740 }
08741 }
08742
08743 if ((ptr = strchr(refer_to, '@'))) {
08744 char *urioption;
08745
08746 *ptr++ = '\0';
08747 if ((urioption = strchr(ptr, ';')))
08748 *urioption++ = '\0';
08749
08750 ast_copy_string(referdata->refer_to_domain, ptr, sizeof(referdata->refer_to_domain));
08751 if (urioption)
08752 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
08753 }
08754
08755 if ((ptr = strchr(refer_to, ';')))
08756 *ptr = '\0';
08757 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
08758
08759 if (referred_by_uri) {
08760 if ((ptr = strchr(referred_by_uri, ';')))
08761 *ptr = '\0';
08762 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
08763 } else {
08764 referdata->referred_by[0] = '\0';
08765 }
08766
08767
08768 if (transferer->owner)
08769 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
08770
08771
08772 if (ast_strlen_zero(transfer_context)) {
08773 transfer_context = S_OR(transferer->owner->macrocontext,
08774 S_OR(transferer->context, default_context));
08775 }
08776
08777 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
08778
08779
08780 if (ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
08781 if (sip_debug_test_pvt(transferer)) {
08782 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
08783 }
08784
08785 return 0;
08786 }
08787 if (sip_debug_test_pvt(transferer))
08788 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
08789
08790
08791 return -1;
08792 }
08793
08794
08795
08796 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
08797 {
08798 char tmp[256] = "", *c, *a;
08799 struct sip_request *req = oreq ? oreq : &p->initreq;
08800 struct sip_refer *referdata = p->refer;
08801 const char *transfer_context = NULL;
08802
08803 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
08804 c = get_in_brackets(tmp);
08805
08806 if (pedanticsipchecking)
08807 ast_uri_decode(c);
08808
08809 if (strncmp(c, "sip:", 4)) {
08810 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
08811 return -1;
08812 }
08813 c += 4;
08814 if ((a = strchr(c, ';')))
08815 *a = '\0';
08816
08817 if ((a = strchr(c, '@'))) {
08818 *a++ = '\0';
08819 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
08820 }
08821
08822 if (sip_debug_test_pvt(p))
08823 ast_verbose("Looking for %s in %s\n", c, p->context);
08824
08825 if (p->owner)
08826 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
08827
08828
08829 if (ast_strlen_zero(transfer_context)) {
08830 transfer_context = S_OR(p->owner->macrocontext,
08831 S_OR(p->context, default_context));
08832 }
08833 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
08834
08835 if (option_debug)
08836 ast_log(LOG_DEBUG,"SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
08837 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
08838 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
08839 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
08840 referdata->refer_call = NULL;
08841
08842 ast_string_field_set(p, context, transfer_context);
08843 return 0;
08844 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
08845 return 1;
08846 }
08847
08848 return -1;
08849 }
08850
08851 static void check_via(struct sip_pvt *p, struct sip_request *req)
08852 {
08853 char via[256];
08854 char *c, *pt;
08855 struct hostent *hp;
08856 struct ast_hostent ahp;
08857
08858 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
08859
08860
08861 c = strchr(via, ',');
08862 if (c)
08863 *c = '\0';
08864
08865
08866 c = strstr(via, ";rport");
08867 if (c && (c[6] != '='))
08868 ast_set_flag(&p->flags[0], SIP_NAT_ROUTE);
08869
08870 c = strchr(via, ';');
08871 if (c)
08872 *c = '\0';
08873
08874 c = strchr(via, ' ');
08875 if (c) {
08876 *c = '\0';
08877 c = ast_skip_blanks(c+1);
08878 if (strcasecmp(via, "SIP/2.0/UDP")) {
08879 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
08880 return;
08881 }
08882 pt = strchr(c, ':');
08883 if (pt)
08884 *pt++ = '\0';
08885 hp = ast_gethostbyname(c, &ahp);
08886 if (!hp) {
08887 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
08888 return;
08889 }
08890 memset(&p->sa, 0, sizeof(p->sa));
08891 p->sa.sin_family = AF_INET;
08892 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
08893 p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
08894
08895 if (sip_debug_test_pvt(p)) {
08896 const struct sockaddr_in *dst = sip_real_dst(p);
08897 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
08898 }
08899 }
08900 }
08901
08902
08903 static char *get_calleridname(const char *input, char *output, size_t outputsize)
08904 {
08905 const char *end = strchr(input,'<');
08906 const char *tmp = strchr(input,'"');
08907 int bytes = 0;
08908 int maxbytes = outputsize - 1;
08909
08910 if (!end || end == input)
08911 return NULL;
08912
08913 end--;
08914
08915 if (tmp && tmp <= end) {
08916
08917
08918
08919 end = strchr(tmp+1, '"');
08920 if (!end)
08921 return NULL;
08922 bytes = (int) (end - tmp);
08923
08924 if (bytes > maxbytes)
08925 bytes = maxbytes;
08926 ast_copy_string(output, tmp + 1, bytes);
08927 } else {
08928
08929
08930 input = ast_skip_blanks(input);
08931
08932 while(*end && *end < 33 && end > input)
08933 end--;
08934 if (end >= input) {
08935 bytes = (int) (end - input) + 2;
08936
08937 if (bytes > maxbytes)
08938 bytes = maxbytes;
08939 ast_copy_string(output, input, bytes);
08940 } else
08941 return NULL;
08942 }
08943 return output;
08944 }
08945
08946
08947
08948
08949
08950 static int get_rpid_num(const char *input, char *output, int maxlen)
08951 {
08952 char *start;
08953 char *end;
08954
08955 start = strchr(input,':');
08956 if (!start) {
08957 output[0] = '\0';
08958 return 0;
08959 }
08960 start++;
08961
08962
08963 ast_copy_string(output,start,maxlen);
08964 output[maxlen-1] = '\0';
08965
08966 end = strchr(output,'@');
08967 if (end)
08968 *end = '\0';
08969 else
08970 output[0] = '\0';
08971 if (strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
08972 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
08973
08974 return 0;
08975 }
08976
08977
08978
08979
08980
08981
08982
08983 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
08984 int sipmethod, char *uri, enum xmittype reliable,
08985 struct sockaddr_in *sin, struct sip_peer **authpeer)
08986 {
08987 struct sip_user *user = NULL;
08988 struct sip_peer *peer;
08989 char from[256], *c;
08990 char *of;
08991 char rpid_num[50];
08992 const char *rpid;
08993 enum check_auth_result res = AUTH_SUCCESSFUL;
08994 char *t;
08995 char calleridname[50];
08996 int debug=sip_debug_test_addr(sin);
08997 struct ast_variable *tmpvar = NULL, *v = NULL;
08998 char *uri2 = ast_strdupa(uri);
08999
09000
09001 t = uri2;
09002 while (*t && *t > 32 && *t != ';')
09003 t++;
09004 *t = '\0';
09005 ast_copy_string(from, get_header(req, "From"), sizeof(from));
09006 if (pedanticsipchecking)
09007 ast_uri_decode(from);
09008
09009 memset(calleridname, 0, sizeof(calleridname));
09010 get_calleridname(from, calleridname, sizeof(calleridname));
09011 if (calleridname[0])
09012 ast_string_field_set(p, cid_name, calleridname);
09013
09014 rpid = get_header(req, "Remote-Party-ID");
09015 memset(rpid_num, 0, sizeof(rpid_num));
09016 if (!ast_strlen_zero(rpid))
09017 p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
09018
09019 of = get_in_brackets(from);
09020 if (ast_strlen_zero(p->exten)) {
09021 t = uri2;
09022 if (!strncmp(t, "sip:", 4))
09023 t+= 4;
09024 ast_string_field_set(p, exten, t);
09025 t = strchr(p->exten, '@');
09026 if (t)
09027 *t = '\0';
09028 if (ast_strlen_zero(p->our_contact))
09029 build_contact(p);
09030 }
09031
09032 ast_string_field_set(p, from, of);
09033 if (strncmp(of, "sip:", 4)) {
09034 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
09035 } else
09036 of += 4;
09037
09038 if ((c = strchr(of, '@'))) {
09039 char *tmp;
09040 *c = '\0';
09041 if ((c = strchr(of, ':')))
09042 *c = '\0';
09043 tmp = ast_strdupa(of);
09044
09045
09046
09047 tmp = strsep(&tmp, ";");
09048 if (ast_is_shrinkable_phonenumber(tmp))
09049 ast_shrink_phone_number(tmp);
09050 ast_string_field_set(p, cid_num, tmp);
09051 }
09052 if (ast_strlen_zero(of))
09053 return AUTH_SUCCESSFUL;
09054
09055 if (!authpeer)
09056 user = find_user(of, 1);
09057
09058
09059 if (user && ast_apply_ha(user->ha, sin)) {
09060 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
09061 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09062
09063 for (v = user->chanvars ; v ; v = v->next) {
09064 if ((tmpvar = ast_variable_new(v->name, v->value))) {
09065 tmpvar->next = p->chanvars;
09066 p->chanvars = tmpvar;
09067 }
09068 }
09069 p->prefs = user->prefs;
09070
09071 if (p->rtp) {
09072 ast_rtp_codec_setpref(p->rtp, &p->prefs);
09073 p->autoframing = user->autoframing;
09074 }
09075
09076 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09077 char *tmp;
09078 if (*calleridname)
09079 ast_string_field_set(p, cid_name, calleridname);
09080 tmp = ast_strdupa(rpid_num);
09081 if (ast_is_shrinkable_phonenumber(tmp))
09082 ast_shrink_phone_number(tmp);
09083 ast_string_field_set(p, cid_num, tmp);
09084 }
09085
09086 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
09087
09088 if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09089 sip_cancel_destroy(p);
09090 ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
09091 ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09092
09093 if (p->sipoptions)
09094 user->sipoptions = p->sipoptions;
09095
09096
09097 if (user->call_limit)
09098 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
09099 if (!ast_strlen_zero(user->context))
09100 ast_string_field_set(p, context, user->context);
09101 if (!ast_strlen_zero(user->cid_num) && !ast_strlen_zero(p->cid_num)) {
09102 char *tmp = ast_strdupa(user->cid_num);
09103 if (ast_is_shrinkable_phonenumber(tmp))
09104 ast_shrink_phone_number(tmp);
09105 ast_string_field_set(p, cid_num, tmp);
09106 }
09107 if (!ast_strlen_zero(user->cid_name) && !ast_strlen_zero(p->cid_num))
09108 ast_string_field_set(p, cid_name, user->cid_name);
09109 ast_string_field_set(p, username, user->name);
09110 ast_string_field_set(p, peername, user->name);
09111 ast_string_field_set(p, peersecret, user->secret);
09112 ast_string_field_set(p, peermd5secret, user->md5secret);
09113 ast_string_field_set(p, subscribecontext, user->subscribecontext);
09114 ast_string_field_set(p, accountcode, user->accountcode);
09115 ast_string_field_set(p, language, user->language);
09116 ast_string_field_set(p, mohsuggest, user->mohsuggest);
09117 ast_string_field_set(p, mohinterpret, user->mohinterpret);
09118 p->allowtransfer = user->allowtransfer;
09119 p->amaflags = user->amaflags;
09120 p->callgroup = user->callgroup;
09121 p->pickupgroup = user->pickupgroup;
09122 if (user->callingpres)
09123 p->callingpres = user->callingpres;
09124
09125
09126 p->capability = user->capability;
09127 p->jointcapability = user->capability;
09128 if (p->peercapability)
09129 p->jointcapability &= p->peercapability;
09130 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
09131 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
09132 p->noncodeccapability |= AST_RTP_DTMF;
09133 else
09134 p->noncodeccapability &= ~AST_RTP_DTMF;
09135 p->jointnoncodeccapability = p->noncodeccapability;
09136 if (p->t38.peercapability)
09137 p->t38.jointcapability &= p->t38.peercapability;
09138 p->maxcallbitrate = user->maxcallbitrate;
09139
09140 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
09141 ast_rtp_destroy(p->vrtp);
09142 p->vrtp = NULL;
09143 }
09144 }
09145 if (user && debug)
09146 ast_verbose("Found user '%s'\n", user->name);
09147 } else {
09148 if (user) {
09149 if (!authpeer && debug)
09150 ast_verbose("Found user '%s', but fails host access\n", user->name);
09151 ASTOBJ_UNREF(user,sip_destroy_user);
09152 }
09153 user = NULL;
09154 }
09155
09156 if (!user) {
09157
09158 if (sipmethod == SIP_SUBSCRIBE)
09159
09160 peer = find_peer(of, NULL, 1);
09161 else
09162
09163
09164
09165
09166 peer = find_peer(NULL, &p->recv, 1);
09167
09168 if (peer) {
09169
09170 if (p->rtp) {
09171 ast_rtp_codec_setpref(p->rtp, &peer->prefs);
09172 p->autoframing = peer->autoframing;
09173 }
09174 if (debug)
09175 ast_verbose("Found peer '%s'\n", peer->name);
09176
09177
09178 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
09179 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09180
09181
09182 if (p->sipoptions)
09183 peer->sipoptions = p->sipoptions;
09184
09185
09186 if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
09187 char *tmp = ast_strdupa(rpid_num);
09188 if (*calleridname)
09189 ast_string_field_set(p, cid_name, calleridname);
09190 if (ast_is_shrinkable_phonenumber(tmp))
09191 ast_shrink_phone_number(tmp);
09192 ast_string_field_set(p, cid_num, tmp);
09193 }
09194 do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
09195
09196 ast_string_field_set(p, peersecret, peer->secret);
09197 ast_string_field_set(p, peermd5secret, peer->md5secret);
09198 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
09199 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
09200 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
09201 if (peer->callingpres)
09202 p->callingpres = peer->callingpres;
09203 if (peer->maxms && peer->lastms)
09204 p->timer_t1 = peer->lastms;
09205 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
09206
09207 ast_string_field_free(p, peersecret);
09208 ast_string_field_free(p, peermd5secret);
09209 }
09210 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
09211 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
09212 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
09213
09214 if (peer->call_limit)
09215 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
09216 ast_string_field_set(p, peername, peer->name);
09217 ast_string_field_set(p, authname, peer->name);
09218
09219
09220 for (v = peer->chanvars ; v ; v = v->next) {
09221 if ((tmpvar = ast_variable_new(v->name, v->value))) {
09222 tmpvar->next = p->chanvars;
09223 p->chanvars = tmpvar;
09224 }
09225 }
09226 if (authpeer) {
09227 (*authpeer) = ASTOBJ_REF(peer);
09228 }
09229
09230 if (!ast_strlen_zero(peer->username)) {
09231 ast_string_field_set(p, username, peer->username);
09232
09233
09234 ast_string_field_set(p, authname, peer->username);
09235 }
09236 if (!ast_strlen_zero(peer->cid_num) && !ast_strlen_zero(p->cid_num)) {
09237 char *tmp = ast_strdupa(peer->cid_num);
09238 if (ast_is_shrinkable_phonenumber(tmp))
09239 ast_shrink_phone_number(tmp);
09240 ast_string_field_set(p, cid_num, tmp);
09241 }
09242 if (!ast_strlen_zero(peer->cid_name) && !ast_strlen_zero(p->cid_name))
09243 ast_string_field_set(p, cid_name, peer->cid_name);
09244 ast_string_field_set(p, fullcontact, peer->fullcontact);
09245 if (!ast_strlen_zero(peer->context))
09246 ast_string_field_set(p, context, peer->context);
09247 ast_string_field_set(p, peersecret, peer->secret);
09248 ast_string_field_set(p, peermd5secret, peer->md5secret);
09249 ast_string_field_set(p, language, peer->language);
09250 ast_string_field_set(p, accountcode, peer->accountcode);
09251 p->amaflags = peer->amaflags;
09252 p->callgroup = peer->callgroup;
09253 p->pickupgroup = peer->pickupgroup;
09254 p->capability = peer->capability;
09255 p->prefs = peer->prefs;
09256 p->jointcapability = peer->capability;
09257 if (p->peercapability)
09258 p->jointcapability &= p->peercapability;
09259 p->maxcallbitrate = peer->maxcallbitrate;
09260 if ((!ast_test_flag(&p->flags[1], SIP_PAGE2_VIDEOSUPPORT) || !(p->capability & AST_FORMAT_VIDEO_MASK)) && p->vrtp) {
09261 ast_rtp_destroy(p->vrtp);
09262 p->vrtp = NULL;
09263 }
09264 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
09265 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
09266 p->noncodeccapability |= AST_RTP_DTMF;
09267 else
09268 p->noncodeccapability &= ~AST_RTP_DTMF;
09269 p->jointnoncodeccapability = p->noncodeccapability;
09270 if (p->t38.peercapability)
09271 p->t38.jointcapability &= p->t38.peercapability;
09272 }
09273 ASTOBJ_UNREF(peer, sip_destroy_peer);
09274 } else {
09275 if (debug)
09276 ast_verbose("Found no matching peer or user for '%s:%d'\n", ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
09277
09278
09279 if (!global_allowguest) {
09280 if (global_alwaysauthreject)
09281 res = AUTH_FAKE_AUTH;
09282 else
09283 res = AUTH_SECRET_FAILED;
09284 }
09285 }
09286
09287 }
09288
09289 if (user)
09290 ASTOBJ_UNREF(user, sip_destroy_user);
09291 return res;
09292 }
09293
09294
09295
09296
09297 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, enum xmittype reliable, struct sockaddr_in *sin)
09298 {
09299 return check_user_full(p, req, sipmethod, uri, reliable, sin, NULL);
09300 }
09301
09302
09303 static int get_msg_text(char *buf, int len, struct sip_request *req)
09304 {
09305 int x;
09306 int y;
09307
09308 buf[0] = '\0';
09309 y = len - strlen(buf) - 5;
09310 if (y < 0)
09311 y = 0;
09312 for (x=0;x<req->lines;x++) {
09313 strncat(buf, req->line[x], y);
09314 y -= strlen(req->line[x]) + 1;
09315 if (y < 0)
09316 y = 0;
09317 if (y != 0)
09318 strcat(buf, "\n");
09319 }
09320 return 0;
09321 }
09322
09323
09324
09325
09326
09327 static void receive_message(struct sip_pvt *p, struct sip_request *req)
09328 {
09329 char buf[1024];
09330 struct ast_frame f;
09331 const char *content_type = get_header(req, "Content-Type");
09332
09333 if (strcmp(content_type, "text/plain")) {
09334 transmit_response(p, "415 Unsupported Media Type", req);
09335 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09336 return;
09337 }
09338
09339 if (get_msg_text(buf, sizeof(buf), req)) {
09340 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
09341 transmit_response(p, "202 Accepted", req);
09342 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09343 return;
09344 }
09345
09346 if (p->owner) {
09347 if (sip_debug_test_pvt(p))
09348 ast_verbose("Message received: '%s'\n", buf);
09349 memset(&f, 0, sizeof(f));
09350 f.frametype = AST_FRAME_TEXT;
09351 f.subclass = 0;
09352 f.offset = 0;
09353 f.data = buf;
09354 f.datalen = strlen(buf);
09355 ast_queue_frame(p->owner, &f);
09356 transmit_response(p, "202 Accepted", req);
09357 } else {
09358 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);
09359 transmit_response(p, "405 Method Not Allowed", req);
09360 }
09361 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
09362 return;
09363 }
09364
09365
09366 static int sip_show_inuse(int fd, int argc, char *argv[])
09367 {
09368 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
09369 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
09370 char ilimits[40];
09371 char iused[40];
09372 int showall = FALSE;
09373
09374 if (argc < 3)
09375 return RESULT_SHOWUSAGE;
09376
09377 if (argc == 4 && !strcmp(argv[3],"all"))
09378 showall = TRUE;
09379
09380 ast_cli(fd, FORMAT, "* User name", "In use", "Limit");
09381 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09382 ASTOBJ_RDLOCK(iterator);
09383 if (iterator->call_limit)
09384 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
09385 else
09386 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
09387 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
09388 if (showall || iterator->call_limit)
09389 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
09390 ASTOBJ_UNLOCK(iterator);
09391 } while (0) );
09392
09393 ast_cli(fd, FORMAT, "* Peer name", "In use", "Limit");
09394
09395 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
09396 ASTOBJ_RDLOCK(iterator);
09397 if (iterator->call_limit)
09398 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
09399 else
09400 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
09401 snprintf(iused, sizeof(iused), "%d/%d", iterator->inUse, iterator->inRinging);
09402 if (showall || iterator->call_limit)
09403 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
09404 ASTOBJ_UNLOCK(iterator);
09405 } while (0) );
09406
09407 return RESULT_SUCCESS;
09408 #undef FORMAT
09409 #undef FORMAT2
09410 }
09411
09412
09413 static char *transfermode2str(enum transfermodes mode)
09414 {
09415 if (mode == TRANSFER_OPENFORALL)
09416 return "open";
09417 else if (mode == TRANSFER_CLOSED)
09418 return "closed";
09419 return "strict";
09420 }
09421
09422
09423 static char *nat2str(int nat)
09424 {
09425 switch(nat) {
09426 case SIP_NAT_NEVER:
09427 return "No";
09428 case SIP_NAT_ROUTE:
09429 return "Route";
09430 case SIP_NAT_ALWAYS:
09431 return "Always";
09432 case SIP_NAT_RFC3581:
09433 return "RFC3581";
09434 default:
09435 return "Unknown";
09436 }
09437 }
09438
09439
09440
09441
09442 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
09443 {
09444 int res = 0;
09445 if (peer->maxms) {
09446 if (peer->lastms < 0) {
09447 ast_copy_string(status, "UNREACHABLE", statuslen);
09448 } else if (peer->lastms > peer->maxms) {
09449 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
09450 res = 1;
09451 } else if (peer->lastms) {
09452 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
09453 res = 1;
09454 } else {
09455 ast_copy_string(status, "UNKNOWN", statuslen);
09456 }
09457 } else {
09458 ast_copy_string(status, "Unmonitored", statuslen);
09459
09460 res = -1;
09461 }
09462 return res;
09463 }
09464
09465
09466 static int sip_show_users(int fd, int argc, char *argv[])
09467 {
09468 regex_t regexbuf;
09469 int havepattern = FALSE;
09470
09471 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
09472
09473 switch (argc) {
09474 case 5:
09475 if (!strcasecmp(argv[3], "like")) {
09476 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
09477 return RESULT_SHOWUSAGE;
09478 havepattern = TRUE;
09479 } else
09480 return RESULT_SHOWUSAGE;
09481 case 3:
09482 break;
09483 default:
09484 return RESULT_SHOWUSAGE;
09485 }
09486
09487 ast_cli(fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
09488 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09489 ASTOBJ_RDLOCK(iterator);
09490
09491 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09492 ASTOBJ_UNLOCK(iterator);
09493 continue;
09494 }
09495
09496 ast_cli(fd, FORMAT, iterator->name,
09497 iterator->secret,
09498 iterator->accountcode,
09499 iterator->context,
09500 iterator->ha ? "Yes" : "No",
09501 nat2str(ast_test_flag(&iterator->flags[0], SIP_NAT)));
09502 ASTOBJ_UNLOCK(iterator);
09503 } while (0)
09504 );
09505
09506 if (havepattern)
09507 regfree(®exbuf);
09508
09509 return RESULT_SUCCESS;
09510 #undef FORMAT
09511 }
09512
09513 static char mandescr_show_peers[] =
09514 "Description: Lists SIP peers in text format with details on current status.\n"
09515 "Variables: \n"
09516 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
09517
09518
09519
09520 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
09521 {
09522 const char *id = astman_get_header(m,"ActionID");
09523 const char *a[] = {"sip", "show", "peers"};
09524 char idtext[256] = "";
09525 int total = 0;
09526
09527 if (!ast_strlen_zero(id))
09528 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
09529
09530 astman_send_ack(s, m, "Peer status list will follow");
09531
09532 _sip_show_peers(-1, &total, s, m, 3, a);
09533
09534 astman_append(s,
09535 "Event: PeerlistComplete\r\n"
09536 "ListItems: %d\r\n"
09537 "%s"
09538 "\r\n", total, idtext);
09539 return 0;
09540 }
09541
09542
09543 static int sip_show_peers(int fd, int argc, char *argv[])
09544 {
09545 return _sip_show_peers(fd, NULL, NULL, NULL, argc, (const char **) argv);
09546 }
09547
09548
09549 static int _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
09550 {
09551 regex_t regexbuf;
09552 int havepattern = FALSE;
09553
09554 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s %-10s\n"
09555 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s %-10s\n"
09556
09557 char name[256];
09558 int total_peers = 0;
09559 int peers_mon_online = 0;
09560 int peers_mon_offline = 0;
09561 int peers_unmon_offline = 0;
09562 int peers_unmon_online = 0;
09563 const char *id;
09564 char idtext[256] = "";
09565 int realtimepeers;
09566
09567 realtimepeers = ast_check_realtime("sippeers");
09568
09569 if (s) {
09570 id = astman_get_header(m,"ActionID");
09571 if (!ast_strlen_zero(id))
09572 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
09573 }
09574
09575 switch (argc) {
09576 case 5:
09577 if (!strcasecmp(argv[3], "like")) {
09578 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
09579 return RESULT_SHOWUSAGE;
09580 havepattern = TRUE;
09581 } else
09582 return RESULT_SHOWUSAGE;
09583 case 3:
09584 break;
09585 default:
09586 return RESULT_SHOWUSAGE;
09587 }
09588
09589 if (!s)
09590 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
09591
09592 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
09593 char status[20] = "";
09594 char srch[2000];
09595 char pstatus;
09596
09597 ASTOBJ_RDLOCK(iterator);
09598
09599 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09600 ASTOBJ_UNLOCK(iterator);
09601 continue;
09602 }
09603
09604 if (!ast_strlen_zero(iterator->username) && !s)
09605 snprintf(name, sizeof(name), "%s/%s", iterator->name, iterator->username);
09606 else
09607 ast_copy_string(name, iterator->name, sizeof(name));
09608
09609 pstatus = peer_status(iterator, status, sizeof(status));
09610 if (pstatus == 1)
09611 peers_mon_online++;
09612 else if (pstatus == 0)
09613 peers_mon_offline++;
09614 else {
09615 if (iterator->addr.sin_port == 0)
09616 peers_unmon_offline++;
09617 else
09618 peers_unmon_online++;
09619 }
09620
09621 snprintf(srch, sizeof(srch), FORMAT, name,
09622 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
09623 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
09624 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
09625 iterator->ha ? " A " : " ",
09626 ntohs(iterator->addr.sin_port), status,
09627 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
09628
09629 if (!s) {
09630 ast_cli(fd, FORMAT, name,
09631 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "(Unspecified)",
09632 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? " D " : " ",
09633 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? " N " : " ",
09634 iterator->ha ? " A " : " ",
09635
09636 ntohs(iterator->addr.sin_port), status,
09637 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "Cached RT":"") : "");
09638 } else {
09639
09640 astman_append(s,
09641 "Event: PeerEntry\r\n%s"
09642 "Channeltype: SIP\r\n"
09643 "ObjectName: %s\r\n"
09644 "ChanObjectType: peer\r\n"
09645 "IPaddress: %s\r\n"
09646 "IPport: %d\r\n"
09647 "Dynamic: %s\r\n"
09648 "Natsupport: %s\r\n"
09649 "VideoSupport: %s\r\n"
09650 "ACL: %s\r\n"
09651 "Status: %s\r\n"
09652 "RealtimeDevice: %s\r\n\r\n",
09653 idtext,
09654 iterator->name,
09655 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iterator->addr.sin_addr) : "-none-",
09656 ntohs(iterator->addr.sin_port),
09657 ast_test_flag(&iterator->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no",
09658 ast_test_flag(&iterator->flags[0], SIP_NAT_ROUTE) ? "yes" : "no",
09659 ast_test_flag(&iterator->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
09660 iterator->ha ? "yes" : "no",
09661 status,
09662 realtimepeers ? (ast_test_flag(&iterator->flags[0], SIP_REALTIME) ? "yes":"no") : "no");
09663 }
09664
09665 ASTOBJ_UNLOCK(iterator);
09666
09667 total_peers++;
09668 } while(0) );
09669
09670 if (!s)
09671 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
09672 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
09673
09674 if (havepattern)
09675 regfree(®exbuf);
09676
09677 if (total)
09678 *total = total_peers;
09679
09680
09681 return RESULT_SUCCESS;
09682 #undef FORMAT
09683 #undef FORMAT2
09684 }
09685
09686
09687 static int sip_show_objects(int fd, int argc, char *argv[])
09688 {
09689 char tmp[256];
09690 if (argc != 3)
09691 return RESULT_SHOWUSAGE;
09692 ast_cli(fd, "-= User objects: %d static, %d realtime =-\n\n", suserobjs, ruserobjs);
09693 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &userl);
09694 ast_cli(fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
09695 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &peerl);
09696 ast_cli(fd, "-= Registry objects: %d =-\n\n", regobjs);
09697 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), ®l);
09698 return RESULT_SUCCESS;
09699 }
09700
09701 static void print_group(int fd, ast_group_t group, int crlf)
09702 {
09703 char buf[256];
09704 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
09705 }
09706
09707
09708 static const char *dtmfmode2str(int mode)
09709 {
09710 switch (mode) {
09711 case SIP_DTMF_RFC2833:
09712 return "rfc2833";
09713 case SIP_DTMF_INFO:
09714 return "info";
09715 case SIP_DTMF_INBAND:
09716 return "inband";
09717 case SIP_DTMF_AUTO:
09718 return "auto";
09719 }
09720 return "<error>";
09721 }
09722
09723
09724 static const char *insecure2str(int port, int invite)
09725 {
09726 if (port && invite)
09727 return "port,invite";
09728 else if (port)
09729 return "port";
09730 else if (invite)
09731 return "invite";
09732 else
09733 return "no";
09734 }
09735
09736
09737
09738
09739 static void cleanup_stale_contexts(char *new, char *old)
09740 {
09741 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
09742
09743 while ((oldcontext = strsep(&old, "&"))) {
09744 stalecontext = '\0';
09745 ast_copy_string(newlist, new, sizeof(newlist));
09746 stringp = newlist;
09747 while ((newcontext = strsep(&stringp, "&"))) {
09748 if (strcmp(newcontext, oldcontext) == 0) {
09749
09750 stalecontext = '\0';
09751 break;
09752 } else if (strcmp(newcontext, oldcontext)) {
09753 stalecontext = oldcontext;
09754 }
09755
09756 }
09757 if (stalecontext)
09758 ast_context_destroy(ast_context_find(stalecontext), "SIP");
09759 }
09760 }
09761
09762
09763 static int sip_prune_realtime(int fd, int argc, char *argv[])
09764 {
09765 struct sip_peer *peer;
09766 struct sip_user *user;
09767 int pruneuser = FALSE;
09768 int prunepeer = FALSE;
09769 int multi = FALSE;
09770 char *name = NULL;
09771 regex_t regexbuf;
09772
09773 switch (argc) {
09774 case 4:
09775 if (!strcasecmp(argv[3], "user"))
09776 return RESULT_SHOWUSAGE;
09777 if (!strcasecmp(argv[3], "peer"))
09778 return RESULT_SHOWUSAGE;
09779 if (!strcasecmp(argv[3], "like"))
09780 return RESULT_SHOWUSAGE;
09781 if (!strcasecmp(argv[3], "all")) {
09782 multi = TRUE;
09783 pruneuser = prunepeer = TRUE;
09784 } else {
09785 pruneuser = prunepeer = TRUE;
09786 name = argv[3];
09787 }
09788 break;
09789 case 5:
09790 if (!strcasecmp(argv[4], "like"))
09791 return RESULT_SHOWUSAGE;
09792 if (!strcasecmp(argv[3], "all"))
09793 return RESULT_SHOWUSAGE;
09794 if (!strcasecmp(argv[3], "like")) {
09795 multi = TRUE;
09796 name = argv[4];
09797 pruneuser = prunepeer = TRUE;
09798 } else if (!strcasecmp(argv[3], "user")) {
09799 pruneuser = TRUE;
09800 if (!strcasecmp(argv[4], "all"))
09801 multi = TRUE;
09802 else
09803 name = argv[4];
09804 } else if (!strcasecmp(argv[3], "peer")) {
09805 prunepeer = TRUE;
09806 if (!strcasecmp(argv[4], "all"))
09807 multi = TRUE;
09808 else
09809 name = argv[4];
09810 } else
09811 return RESULT_SHOWUSAGE;
09812 break;
09813 case 6:
09814 if (strcasecmp(argv[4], "like"))
09815 return RESULT_SHOWUSAGE;
09816 if (!strcasecmp(argv[3], "user")) {
09817 pruneuser = TRUE;
09818 name = argv[5];
09819 } else if (!strcasecmp(argv[3], "peer")) {
09820 prunepeer = TRUE;
09821 name = argv[5];
09822 } else
09823 return RESULT_SHOWUSAGE;
09824 break;
09825 default:
09826 return RESULT_SHOWUSAGE;
09827 }
09828
09829 if (multi && name) {
09830 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
09831 return RESULT_SHOWUSAGE;
09832 }
09833
09834 if (multi) {
09835 if (prunepeer) {
09836 int pruned = 0;
09837
09838 ASTOBJ_CONTAINER_WRLOCK(&peerl);
09839 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
09840 ASTOBJ_RDLOCK(iterator);
09841 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09842 ASTOBJ_UNLOCK(iterator);
09843 continue;
09844 };
09845 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
09846 ASTOBJ_MARK(iterator);
09847 pruned++;
09848 }
09849 ASTOBJ_UNLOCK(iterator);
09850 } while (0) );
09851 if (pruned) {
09852 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
09853 ast_cli(fd, "%d peers pruned.\n", pruned);
09854 } else
09855 ast_cli(fd, "No peers found to prune.\n");
09856 ASTOBJ_CONTAINER_UNLOCK(&peerl);
09857 }
09858 if (pruneuser) {
09859 int pruned = 0;
09860
09861 ASTOBJ_CONTAINER_WRLOCK(&userl);
09862 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
09863 ASTOBJ_RDLOCK(iterator);
09864 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
09865 ASTOBJ_UNLOCK(iterator);
09866 continue;
09867 };
09868 if (ast_test_flag(&iterator->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
09869 ASTOBJ_MARK(iterator);
09870 pruned++;
09871 }
09872 ASTOBJ_UNLOCK(iterator);
09873 } while (0) );
09874 if (pruned) {
09875 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
09876 ast_cli(fd, "%d users pruned.\n", pruned);
09877 } else
09878 ast_cli(fd, "No users found to prune.\n");
09879 ASTOBJ_CONTAINER_UNLOCK(&userl);
09880 }
09881 } else {
09882 if (prunepeer) {
09883 if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
09884 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
09885 ast_cli(fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
09886 ASTOBJ_CONTAINER_LINK(&peerl, peer);
09887 } else
09888 ast_cli(fd, "Peer '%s' pruned.\n", name);
09889 ASTOBJ_UNREF(peer, sip_destroy_peer);
09890 } else
09891 ast_cli(fd, "Peer '%s' not found.\n", name);
09892 }
09893 if (pruneuser) {
09894 if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
09895 if (!ast_test_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
09896 ast_cli(fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
09897 ASTOBJ_CONTAINER_LINK(&userl, user);
09898 } else
09899 ast_cli(fd, "User '%s' pruned.\n", name);
09900 ASTOBJ_UNREF(user, sip_destroy_user);
09901 } else
09902 ast_cli(fd, "User '%s' not found.\n", name);
09903 }
09904 }
09905
09906 return RESULT_SUCCESS;
09907 }
09908
09909
09910 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
09911 {
09912 int x, codec;
09913
09914 for(x = 0; x < 32 ; x++) {
09915 codec = ast_codec_pref_index(pref, x);
09916 if (!codec)
09917 break;
09918 ast_cli(fd, "%s", ast_getformatname(codec));
09919 ast_cli(fd, ":%d", pref->framing[x]);
09920 if (x < 31 && ast_codec_pref_index(pref, x + 1))
09921 ast_cli(fd, ",");
09922 }
09923 if (!x)
09924 ast_cli(fd, "none");
09925 }
09926
09927
09928 static const char *domain_mode_to_text(const enum domain_mode mode)
09929 {
09930 switch (mode) {
09931 case SIP_DOMAIN_AUTO:
09932 return "[Automatic]";
09933 case SIP_DOMAIN_CONFIG:
09934 return "[Configured]";
09935 }
09936
09937 return "";
09938 }
09939
09940
09941 static int sip_show_domains(int fd, int argc, char *argv[])
09942 {
09943 struct domain *d;
09944 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
09945
09946 if (AST_LIST_EMPTY(&domain_list)) {
09947 ast_cli(fd, "SIP Domain support not enabled.\n\n");
09948 return RESULT_SUCCESS;
09949 } else {
09950 ast_cli(fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
09951 AST_LIST_LOCK(&domain_list);
09952 AST_LIST_TRAVERSE(&domain_list, d, list)
09953 ast_cli(fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
09954 domain_mode_to_text(d->mode));
09955 AST_LIST_UNLOCK(&domain_list);
09956 ast_cli(fd, "\n");
09957 return RESULT_SUCCESS;
09958 }
09959 }
09960 #undef FORMAT
09961
09962 static char mandescr_show_peer[] =
09963 "Description: Show one SIP peer with details on current status.\n"
09964 "Variables: \n"
09965 " Peer: <name> The peer name you want to check.\n"
09966 " ActionID: <id> Optional action ID for this AMI transaction.\n";
09967
09968
09969 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
09970 {
09971 const char *a[4];
09972 const char *peer;
09973 int ret;
09974
09975 peer = astman_get_header(m,"Peer");
09976 if (ast_strlen_zero(peer)) {
09977 astman_send_error(s, m, "Peer: <name> missing.\n");
09978 return 0;
09979 }
09980 a[0] = "sip";
09981 a[1] = "show";
09982 a[2] = "peer";
09983 a[3] = peer;
09984
09985 ret = _sip_show_peer(1, -1, s, m, 4, a);
09986 astman_append(s, "\r\n\r\n" );
09987 return ret;
09988 }
09989
09990
09991
09992
09993 static int sip_show_peer(int fd, int argc, char *argv[])
09994 {
09995 return _sip_show_peer(0, fd, NULL, NULL, argc, (const char **) argv);
09996 }
09997
09998
09999 static int _sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
10000 {
10001 char status[30] = "";
10002 char cbuf[256];
10003 struct sip_peer *peer;
10004 char codec_buf[512];
10005 struct ast_codec_pref *pref;
10006 struct ast_variable *v;
10007 struct sip_auth *auth;
10008 int x = 0, codec = 0, load_realtime;
10009 int realtimepeers;
10010
10011 realtimepeers = ast_check_realtime("sippeers");
10012
10013 if (argc < 4)
10014 return RESULT_SHOWUSAGE;
10015
10016 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
10017 peer = find_peer(argv[3], NULL, load_realtime);
10018 if (s) {
10019 if (peer) {
10020 const char *id = astman_get_header(m,"ActionID");
10021
10022 astman_append(s, "Response: Success\r\n");
10023 if (!ast_strlen_zero(id))
10024 astman_append(s, "ActionID: %s\r\n",id);
10025 } else {
10026 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.\n", argv[3]);
10027 astman_send_error(s, m, cbuf);
10028 return 0;
10029 }
10030 }
10031 if (peer && type==0 ) {
10032 ast_cli(fd,"\n\n");
10033 ast_cli(fd, " * Name : %s\n", peer->name);
10034 if (realtimepeers) {
10035 ast_cli(fd, " Realtime peer: %s\n", ast_test_flag(&peer->flags[0], SIP_REALTIME) ? "Yes, cached" : "No");
10036 }
10037 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
10038 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
10039 for (auth = peer->auth; auth; auth = auth->next) {
10040 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
10041 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
10042 }
10043 ast_cli(fd, " Context : %s\n", peer->context);
10044 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
10045 ast_cli(fd, " Language : %s\n", peer->language);
10046 if (!ast_strlen_zero(peer->accountcode))
10047 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
10048 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
10049 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
10050 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
10051 if (!ast_strlen_zero(peer->fromuser))
10052 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
10053 if (!ast_strlen_zero(peer->fromdomain))
10054 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
10055 ast_cli(fd, " Callgroup : ");
10056 print_group(fd, peer->callgroup, 0);
10057 ast_cli(fd, " Pickupgroup : ");
10058 print_group(fd, peer->pickupgroup, 0);
10059 ast_cli(fd, " Mailbox : %s\n", peer->mailbox);
10060 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
10061 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
10062 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
10063 ast_cli(fd, " Dynamic : %s\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Yes":"No"));
10064 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
10065 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
10066 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
10067 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)));
10068 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
10069 ast_cli(fd, " ACL : %s\n", (peer->ha?"Yes":"No"));
10070 ast_cli(fd, " T38 pt UDPTL : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)?"Yes":"No");
10071 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
10072 ast_cli(fd, " T38 pt RTP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_RTP)?"Yes":"No");
10073 ast_cli(fd, " T38 pt TCP : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT_TCP)?"Yes":"No");
10074 #endif
10075 ast_cli(fd, " CanReinvite : %s\n", ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Yes":"No");
10076 ast_cli(fd, " PromiscRedir : %s\n", ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Yes":"No");
10077 ast_cli(fd, " User=Phone : %s\n", ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Yes":"No");
10078 ast_cli(fd, " Video Support: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Yes":"No");
10079 ast_cli(fd, " Trust RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_TRUSTRPID) ? "Yes" : "No");
10080 ast_cli(fd, " Send RPID : %s\n", ast_test_flag(&peer->flags[0], SIP_SENDRPID) ? "Yes" : "No");
10081 ast_cli(fd, " Subscriptions: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
10082 ast_cli(fd, " Overlap dial : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
10083
10084
10085 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
10086 ast_cli(fd, " LastMsg : %d\n", peer->lastmsg);
10087 ast_cli(fd, " ToHost : %s\n", peer->tohost);
10088 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));
10089 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
10090 if (!ast_strlen_zero(global_regcontext))
10091 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
10092 ast_cli(fd, " Def. Username: %s\n", peer->username);
10093 ast_cli(fd, " SIP Options : ");
10094 if (peer->sipoptions) {
10095 int lastoption = -1;
10096 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
10097 if (sip_options[x].id != lastoption) {
10098 if (peer->sipoptions & sip_options[x].id)
10099 ast_cli(fd, "%s ", sip_options[x].text);
10100 lastoption = x;
10101 }
10102 }
10103 } else
10104 ast_cli(fd, "(none)");
10105
10106 ast_cli(fd, "\n");
10107 ast_cli(fd, " Codecs : ");
10108 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
10109 ast_cli(fd, "%s\n", codec_buf);
10110 ast_cli(fd, " Codec Order : (");
10111 print_codec_to_cli(fd, &peer->prefs);
10112 ast_cli(fd, ")\n");
10113
10114 ast_cli(fd, " Auto-Framing: %s \n", peer->autoframing ? "Yes" : "No");
10115 ast_cli(fd, " Status : ");
10116 peer_status(peer, status, sizeof(status));
10117 ast_cli(fd, "%s\n",status);
10118 ast_cli(fd, " Useragent : %s\n", peer->useragent);
10119 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
10120 if (peer->chanvars) {
10121 ast_cli(fd, " Variables :\n");
10122 for (v = peer->chanvars ; v ; v = v->next)
10123 ast_cli(fd, " %s = %s\n", v->name, v->value);
10124 }
10125 ast_cli(fd,"\n");
10126 ASTOBJ_UNREF(peer,sip_destroy_peer);
10127 } else if (peer && type == 1) {
10128 char buf[256];
10129 astman_append(s, "Channeltype: SIP\r\n");
10130 astman_append(s, "ObjectName: %s\r\n", peer->name);
10131 astman_append(s, "ChanObjectType: peer\r\n");
10132 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
10133 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
10134 astman_append(s, "Context: %s\r\n", peer->context);
10135 astman_append(s, "Language: %s\r\n", peer->language);
10136 if (!ast_strlen_zero(peer->accountcode))
10137 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
10138 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
10139 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
10140 if (!ast_strlen_zero(peer->fromuser))
10141 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
10142 if (!ast_strlen_zero(peer->fromdomain))
10143 astman_append(s, "SIP-FromDomain: %s\r\n", peer->fromdomain);
10144 astman_append(s, "Callgroup: ");
10145 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->callgroup));
10146 astman_append(s, "Pickupgroup: ");
10147 astman_append(s, "%s\r\n", ast_print_group(buf, sizeof(buf), peer->pickupgroup));
10148 astman_append(s, "VoiceMailbox: %s\r\n", peer->mailbox);
10149 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
10150 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
10151 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
10152 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
10153 astman_append(s, "Dynamic: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)?"Y":"N"));
10154 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
10155 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched,peer->expire));
10156 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)));
10157 astman_append(s, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(&peer->flags[0], SIP_NAT)));
10158 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
10159 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_CAN_REINVITE)?"Y":"N"));
10160 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
10161 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
10162 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
10163
10164
10165 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
10166 astman_append(s, "SIPLastMsg: %d\r\n", peer->lastmsg);
10167 astman_append(s, "ToHost: %s\r\n", peer->tohost);
10168 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));
10169 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));
10170 astman_append(s, "Default-Username: %s\r\n", peer->username);
10171 if (!ast_strlen_zero(global_regcontext))
10172 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
10173 astman_append(s, "Codecs: ");
10174 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
10175 astman_append(s, "%s\r\n", codec_buf);
10176 astman_append(s, "CodecOrder: ");
10177 pref = &peer->prefs;
10178 for(x = 0; x < 32 ; x++) {
10179 codec = ast_codec_pref_index(pref,x);
10180 if (!codec)
10181 break;
10182 astman_append(s, "%s", ast_getformatname(codec));
10183 if (x < 31 && ast_codec_pref_index(pref,x+1))
10184 astman_append(s, ",");
10185 }
10186
10187 astman_append(s, "\r\n");
10188 astman_append(s, "Status: ");
10189 peer_status(peer, status, sizeof(status));
10190 astman_append(s, "%s\r\n", status);
10191 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
10192 astman_append(s, "Reg-Contact : %s\r\n", peer->fullcontact);
10193 if (peer->chanvars) {
10194 for (v = peer->chanvars ; v ; v = v->next) {
10195 astman_append(s, "ChanVariable:\n");
10196 astman_append(s, " %s,%s\r\n", v->name, v->value);
10197 }
10198 }
10199
10200 ASTOBJ_UNREF(peer,sip_destroy_peer);
10201
10202 } else {
10203 ast_cli(fd,"Peer %s not found.\n", argv[3]);
10204 ast_cli(fd,"\n");
10205 }
10206
10207 return RESULT_SUCCESS;
10208 }
10209
10210
10211 static int sip_show_user(int fd, int argc, char *argv[])
10212 {
10213 char cbuf[256];
10214 struct sip_user *user;
10215 struct ast_variable *v;
10216 int load_realtime;
10217
10218 if (argc < 4)
10219 return RESULT_SHOWUSAGE;
10220
10221
10222 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
10223
10224 user = find_user(argv[3], load_realtime);
10225 if (user) {
10226 ast_cli(fd,"\n\n");
10227 ast_cli(fd, " * Name : %s\n", user->name);
10228 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
10229 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
10230 ast_cli(fd, " Context : %s\n", user->context);
10231 ast_cli(fd, " Language : %s\n", user->language);
10232 if (!ast_strlen_zero(user->accountcode))
10233 ast_cli(fd, " Accountcode : %s\n", user->accountcode);
10234 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
10235 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
10236 ast_cli(fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
10237 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
10238 ast_cli(fd, " Call limit : %d\n", user->call_limit);
10239 ast_cli(fd, " Callgroup : ");
10240 print_group(fd, user->callgroup, 0);
10241 ast_cli(fd, " Pickupgroup : ");
10242 print_group(fd, user->pickupgroup, 0);
10243 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
10244 ast_cli(fd, " ACL : %s\n", (user->ha?"Yes":"No"));
10245 ast_cli(fd, " Codec Order : (");
10246 print_codec_to_cli(fd, &user->prefs);
10247 ast_cli(fd, ")\n");
10248
10249 ast_cli(fd, " Auto-Framing: %s \n", user->autoframing ? "Yes" : "No");
10250 if (user->chanvars) {
10251 ast_cli(fd, " Variables :\n");
10252 for (v = user->chanvars ; v ; v = v->next)
10253 ast_cli(fd, " %s = %s\n", v->name, v->value);
10254 }
10255 ast_cli(fd,"\n");
10256 ASTOBJ_UNREF(user,sip_destroy_user);
10257 } else {
10258 ast_cli(fd,"User %s not found.\n", argv[3]);
10259 ast_cli(fd,"\n");
10260 }
10261
10262 return RESULT_SUCCESS;
10263 }
10264
10265
10266 static int sip_show_registry(int fd, int argc, char *argv[])
10267 {
10268 #define FORMAT2 "%-30.30s %-12.12s %8.8s %-20.20s %-25.25s\n"
10269 #define FORMAT "%-30.30s %-12.12s %8d %-20.20s %-25.25s\n"
10270 char host[80];
10271 char tmpdat[256];
10272 struct tm tm;
10273
10274
10275 if (argc != 3)
10276 return RESULT_SHOWUSAGE;
10277 ast_cli(fd, FORMAT2, "Host", "Username", "Refresh", "State", "Reg.Time");
10278 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
10279 ASTOBJ_RDLOCK(iterator);
10280 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
10281 if (iterator->regtime) {
10282 ast_localtime(&iterator->regtime, &tm, NULL);
10283 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
10284 } else {
10285 tmpdat[0] = 0;
10286 }
10287 ast_cli(fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
10288 ASTOBJ_UNLOCK(iterator);
10289 } while(0));
10290 return RESULT_SUCCESS;
10291 #undef FORMAT
10292 #undef FORMAT2
10293 }
10294
10295
10296 static int sip_show_settings(int fd, int argc, char *argv[])
10297 {
10298 int realtimepeers;
10299 int realtimeusers;
10300 char codec_buf[BUFSIZ];
10301
10302 realtimepeers = ast_check_realtime("sippeers");
10303 realtimeusers = ast_check_realtime("sipusers");
10304
10305 if (argc != 3)
10306 return RESULT_SHOWUSAGE;
10307 ast_cli(fd, "\n\nGlobal Settings:\n");
10308 ast_cli(fd, "----------------\n");
10309 ast_cli(fd, " SIP Port: %d\n", ntohs(bindaddr.sin_port));
10310 ast_cli(fd, " Bindaddress: %s\n", ast_inet_ntoa(bindaddr.sin_addr));
10311 ast_cli(fd, " Videosupport: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "Yes" : "No");
10312 ast_cli(fd, " AutoCreatePeer: %s\n", autocreatepeer ? "Yes" : "No");
10313 ast_cli(fd, " Allow unknown access: %s\n", global_allowguest ? "Yes" : "No");
10314 ast_cli(fd, " Allow subscriptions: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
10315 ast_cli(fd, " Allow overlap dialing: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
10316 ast_cli(fd, " Promsic. redir: %s\n", ast_test_flag(&global_flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
10317 ast_cli(fd, " SIP domain support: %s\n", AST_LIST_EMPTY(&domain_list) ? "No" : "Yes");
10318 ast_cli(fd, " Call to non-local dom.: %s\n", allow_external_domains ? "Yes" : "No");
10319 ast_cli(fd, " URI user is phone no: %s\n", ast_test_flag(&global_flags[0], SIP_USEREQPHONE) ? "Yes" : "No");
10320 ast_cli(fd, " Our auth realm %s\n", global_realm);
10321 ast_cli(fd, " Realm. auth: %s\n", authl ? "Yes": "No");
10322 ast_cli(fd, " Always auth rejects: %s\n", global_alwaysauthreject ? "Yes" : "No");
10323 ast_cli(fd, " Call limit peers only: %s\n", global_limitonpeers ? "Yes" : "No");
10324 ast_cli(fd, " Direct RTP setup: %s\n", global_directrtpsetup ? "Yes" : "No");
10325 ast_cli(fd, " User Agent: %s\n", global_useragent);
10326 ast_cli(fd, " MWI checking interval: %d secs\n", global_mwitime);
10327 ast_cli(fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
10328 ast_cli(fd, " Caller ID: %s\n", default_callerid);
10329 ast_cli(fd, " From: Domain: %s\n", default_fromdomain);
10330 ast_cli(fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
10331 ast_cli(fd, " Call Events: %s\n", global_callevents ? "On" : "Off");
10332 ast_cli(fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
10333 ast_cli(fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
10334 ast_cli(fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
10335 ast_cli(fd, " T38 fax pt UDPTL: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL) ? "Yes" : "No");
10336 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
10337 ast_cli(fd, " T38 fax pt RTP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_RTP) ? "Yes" : "No");
10338 ast_cli(fd, " T38 fax pt TCP: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_TCP) ? "Yes" : "No");
10339 #endif
10340 ast_cli(fd, " RFC2833 Compensation: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE) ? "Yes" : "No");
10341 ast_cli(fd, " Jitterbuffer enabled: %s\n", ast_test_flag(&global_jbconf, AST_JB_ENABLED) ? "Yes" : "No");
10342 ast_cli(fd, " Jitterbuffer forced: %s\n", ast_test_flag(&global_jbconf, AST_JB_FORCED) ? "Yes" : "No");
10343 ast_cli(fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
10344 ast_cli(fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
10345 ast_cli(fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
10346 ast_cli(fd, " Jitterbuffer log: %s\n", ast_test_flag(&global_jbconf, AST_JB_LOG) ? "Yes" : "No");
10347 if (!realtimepeers && !realtimeusers)
10348 ast_cli(fd, " SIP realtime: Disabled\n" );
10349 else
10350 ast_cli(fd, " SIP realtime: Enabled\n" );
10351
10352 ast_cli(fd, "\nGlobal Signalling Settings:\n");
10353 ast_cli(fd, "---------------------------\n");
10354 ast_cli(fd, " Codecs: ");
10355 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, global_capability);
10356 ast_cli(fd, "%s\n", codec_buf);
10357 ast_cli(fd, " Codec Order: ");
10358 print_codec_to_cli(fd, &default_prefs);
10359 ast_cli(fd, "\n");
10360 ast_cli(fd, " T1 minimum: %d\n", global_t1min);
10361 ast_cli(fd, " Relax DTMF: %s\n", global_relaxdtmf ? "Yes" : "No");
10362 ast_cli(fd, " Compact SIP headers: %s\n", compactheaders ? "Yes" : "No");
10363 ast_cli(fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
10364 ast_cli(fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
10365 ast_cli(fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
10366 ast_cli(fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
10367 ast_cli(fd, " DNS SRV lookup: %s\n", srvlookup ? "Yes" : "No");
10368 ast_cli(fd, " Pedantic SIP support: %s\n", pedanticsipchecking ? "Yes" : "No");
10369 ast_cli(fd, " Reg. min duration %d secs\n", min_expiry);
10370 ast_cli(fd, " Reg. max duration: %d secs\n", max_expiry);
10371 ast_cli(fd, " Reg. default duration: %d secs\n", default_expiry);
10372 ast_cli(fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
10373 ast_cli(fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
10374 ast_cli(fd, " Notify ringing state: %s\n", global_notifyringing ? "Yes" : "No");
10375 ast_cli(fd, " Notify hold state: %s\n", global_notifyhold ? "Yes" : "No");
10376 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(global_allowtransfer));
10377 ast_cli(fd, " Max Call Bitrate: %d kbps\r\n", default_maxcallbitrate);
10378 ast_cli(fd, " Auto-Framing: %s \r\n", global_autoframing ? "Yes" : "No");
10379 ast_cli(fd, "\nDefault Settings:\n");
10380 ast_cli(fd, "-----------------\n");
10381 ast_cli(fd, " Context: %s\n", default_context);
10382 ast_cli(fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags[0], SIP_NAT)));
10383 ast_cli(fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
10384 ast_cli(fd, " Qualify: %d\n", default_qualify);
10385 ast_cli(fd, " Use ClientCode: %s\n", ast_test_flag(&global_flags[0], SIP_USECLIENTCODE) ? "Yes" : "No");
10386 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" );
10387 ast_cli(fd, " Language: %s\n", S_OR(default_language, "(Defaults to English)"));
10388 ast_cli(fd, " MOH Interpret: %s\n", default_mohinterpret);
10389 ast_cli(fd, " MOH Suggest: %s\n", default_mohsuggest);
10390 ast_cli(fd, " Voice Mail Extension: %s\n", default_vmexten);
10391
10392
10393 if (realtimepeers || realtimeusers) {
10394 ast_cli(fd, "\nRealtime SIP Settings:\n");
10395 ast_cli(fd, "----------------------\n");
10396 ast_cli(fd, " Realtime Peers: %s\n", realtimepeers ? "Yes" : "No");
10397 ast_cli(fd, " Realtime Users: %s\n", realtimeusers ? "Yes" : "No");
10398 ast_cli(fd, " Cache Friends: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? "Yes" : "No");
10399 ast_cli(fd, " Update: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTUPDATE) ? "Yes" : "No");
10400 ast_cli(fd, " Ignore Reg. Expire: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) ? "Yes" : "No");
10401 ast_cli(fd, " Save sys. name: %s\n", ast_test_flag(&global_flags[1], SIP_PAGE2_RTSAVE_SYSNAME) ? "Yes" : "No");
10402 ast_cli(fd, " Auto Clear: %d\n", global_rtautoclear);
10403 }
10404 ast_cli(fd, "\n----\n");
10405 return RESULT_SUCCESS;
10406 }
10407
10408
10409 static const char *subscription_type2str(enum subscriptiontype subtype)
10410 {
10411 int i;
10412
10413 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
10414 if (subscription_types[i].type == subtype) {
10415 return subscription_types[i].text;
10416 }
10417 }
10418 return subscription_types[0].text;
10419 }
10420
10421
10422 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
10423 {
10424 int i;
10425
10426 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
10427 if (subscription_types[i].type == subtype) {
10428 return &subscription_types[i];
10429 }
10430 }
10431 return &subscription_types[0];
10432 }
10433
10434
10435 static int sip_show_channels(int fd, int argc, char *argv[])
10436 {
10437 return __sip_show_channels(fd, argc, argv, 0);
10438 }
10439
10440
10441 static int sip_show_subscriptions(int fd, int argc, char *argv[])
10442 {
10443 return __sip_show_channels(fd, argc, argv, 1);
10444 }
10445
10446
10447 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions)
10448 {
10449 #define FORMAT3 "%-15.15s %-10.10s %-11.11s %-15.15s %-13.13s %-15.15s %-10.10s\n"
10450 #define FORMAT2 "%-15.15s %-10.10s %-11.11s %-11.11s %-4.4s %-7.7s %-15.15s\n"
10451 #define FORMAT "%-15.15s %-10.10s %-11.11s %5.5d/%5.5d %-4.4s %-3.3s %-3.3s %-15.15s %-10.10s\n"
10452 struct sip_pvt *cur;
10453 int numchans = 0;
10454 char *referstatus = NULL;
10455
10456 if (argc != 3)
10457 return RESULT_SHOWUSAGE;
10458 ast_mutex_lock(&iflock);
10459 cur = iflist;
10460 if (!subscriptions)
10461 ast_cli(fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Seq (Tx/Rx)", "Format", "Hold", "Last Message");
10462 else
10463 ast_cli(fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox");
10464 for (; cur; cur = cur->next) {
10465 referstatus = "";
10466 if (cur->refer) {
10467 referstatus = referstatus2str(cur->refer->status);
10468 }
10469 if (cur->subscribed == NONE && !subscriptions) {
10470 ast_cli(fd, FORMAT, ast_inet_ntoa(cur->sa.sin_addr),
10471 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
10472 cur->callid,
10473 cur->ocseq, cur->icseq,
10474 ast_getformatname(cur->owner ? cur->owner->nativeformats : 0),
10475 ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD) ? "Yes" : "No",
10476 ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY) ? "(d)" : "",
10477 cur->lastmsg ,
10478 referstatus
10479 );
10480 numchans++;
10481 }
10482 if (cur->subscribed != NONE && subscriptions) {
10483 ast_cli(fd, FORMAT3, ast_inet_ntoa(cur->sa.sin_addr),
10484 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
10485 cur->callid,
10486
10487 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
10488 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
10489 subscription_type2str(cur->subscribed),
10490 cur->subscribed == MWI_NOTIFICATION ? (cur->relatedpeer ? cur->relatedpeer->mailbox : "<none>") : "<none>"
10491 );
10492 numchans++;
10493 }
10494 }
10495 ast_mutex_unlock(&iflock);
10496 if (!subscriptions)
10497 ast_cli(fd, "%d active SIP channel%s\n", numchans, (numchans != 1) ? "s" : "");
10498 else
10499 ast_cli(fd, "%d active SIP subscription%s\n", numchans, (numchans != 1) ? "s" : "");
10500 return RESULT_SUCCESS;
10501 #undef FORMAT
10502 #undef FORMAT2
10503 #undef FORMAT3
10504 }
10505
10506
10507 static char *complete_sipch(const char *line, const char *word, int pos, int state)
10508 {
10509 int which=0;
10510 struct sip_pvt *cur;
10511 char *c = NULL;
10512 int wordlen = strlen(word);
10513
10514 ast_mutex_lock(&iflock);
10515 for (cur = iflist; cur; cur = cur->next) {
10516 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
10517 c = ast_strdup(cur->callid);
10518 break;
10519 }
10520 }
10521 ast_mutex_unlock(&iflock);
10522 return c;
10523 }
10524
10525
10526 static char *complete_sip_peer(const char *word, int state, int flags2)
10527 {
10528 char *result = NULL;
10529 int wordlen = strlen(word);
10530 int which = 0;
10531
10532 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
10533
10534 if (!strncasecmp(word, iterator->name, wordlen) &&
10535 (!flags2 || ast_test_flag(&iterator->flags[1], flags2)) &&
10536 ++which > state)
10537 result = ast_strdup(iterator->name);
10538 } while(0) );
10539 return result;
10540 }
10541
10542
10543 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
10544 {
10545 if (pos == 3)
10546 return complete_sip_peer(word, state, 0);
10547
10548 return NULL;
10549 }
10550
10551
10552 static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state)
10553 {
10554 if (pos == 3)
10555 return complete_sip_peer(word, state, 0);
10556
10557 return NULL;
10558 }
10559
10560
10561 static char *complete_sip_user(const char *word, int state, int flags2)
10562 {
10563 char *result = NULL;
10564 int wordlen = strlen(word);
10565 int which = 0;
10566
10567 ASTOBJ_CONTAINER_TRAVERSE(&userl, !result, do {
10568
10569 if (!strncasecmp(word, iterator->name, wordlen)) {
10570 if (flags2 && !ast_test_flag(&iterator->flags[1], flags2))
10571 continue;
10572 if (++which > state) {
10573 result = ast_strdup(iterator->name);
10574 }
10575 }
10576 } while(0) );
10577 return result;
10578 }
10579
10580
10581 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
10582 {
10583 if (pos == 3)
10584 return complete_sip_user(word, state, 0);
10585
10586 return NULL;
10587 }
10588
10589
10590 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
10591 {
10592 char *c = NULL;
10593
10594 if (pos == 2) {
10595 int which = 0;
10596 char *cat = NULL;
10597 int wordlen = strlen(word);
10598
10599
10600
10601 if (!notify_types)
10602 return NULL;
10603
10604 while ( (cat = ast_category_browse(notify_types, cat)) ) {
10605 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
10606 c = ast_strdup(cat);
10607 break;
10608 }
10609 }
10610 return c;
10611 }
10612
10613 if (pos > 2)
10614 return complete_sip_peer(word, state, 0);
10615
10616 return NULL;
10617 }
10618
10619
10620 static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state)
10621 {
10622 if (pos == 4)
10623 return complete_sip_peer(word, state, SIP_PAGE2_RTCACHEFRIENDS);
10624 return NULL;
10625 }
10626
10627
10628 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state)
10629 {
10630 if (pos == 4)
10631 return complete_sip_user(word, state, SIP_PAGE2_RTCACHEFRIENDS);
10632
10633 return NULL;
10634 }
10635
10636
10637 static int sip_show_channel(int fd, int argc, char *argv[])
10638 {
10639 struct sip_pvt *cur;
10640 size_t len;
10641 int found = 0;
10642
10643 if (argc != 4)
10644 return RESULT_SHOWUSAGE;
10645 len = strlen(argv[3]);
10646 ast_mutex_lock(&iflock);
10647 for (cur = iflist; cur; cur = cur->next) {
10648 if (!strncasecmp(cur->callid, argv[3], len)) {
10649 char formatbuf[BUFSIZ/2];
10650 ast_cli(fd,"\n");
10651 if (cur->subscribed != NONE)
10652 ast_cli(fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
10653 else
10654 ast_cli(fd, " * SIP Call\n");
10655 ast_cli(fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
10656 ast_cli(fd, " Call-ID: %s\n", cur->callid);
10657 ast_cli(fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
10658 ast_cli(fd, " Our Codec Capability: %d\n", cur->capability);
10659 ast_cli(fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
10660 ast_cli(fd, " Their Codec Capability: %d\n", cur->peercapability);
10661 ast_cli(fd, " Joint Codec Capability: %d\n", cur->jointcapability);
10662 ast_cli(fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
10663 ast_cli(fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
10664 ast_cli(fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(cur->sa.sin_addr), ntohs(cur->sa.sin_port));
10665 ast_cli(fd, " Received Address: %s:%d\n", ast_inet_ntoa(cur->recv.sin_addr), ntohs(cur->recv.sin_port));
10666 ast_cli(fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
10667 ast_cli(fd, " NAT Support: %s\n", nat2str(ast_test_flag(&cur->flags[0], SIP_NAT)));
10668 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)" );
10669 ast_cli(fd, " Our Tag: %s\n", cur->tag);
10670 ast_cli(fd, " Their Tag: %s\n", cur->theirtag);
10671 ast_cli(fd, " SIP User agent: %s\n", cur->useragent);
10672 if (!ast_strlen_zero(cur->username))
10673 ast_cli(fd, " Username: %s\n", cur->username);
10674 if (!ast_strlen_zero(cur->peername))
10675 ast_cli(fd, " Peername: %s\n", cur->peername);
10676 if (!ast_strlen_zero(cur->uri))
10677 ast_cli(fd, " Original uri: %s\n", cur->uri);
10678 if (!ast_strlen_zero(cur->cid_num))
10679 ast_cli(fd, " Caller-ID: %s\n", cur->cid_num);
10680 ast_cli(fd, " Need Destroy: %d\n", ast_test_flag(&cur->flags[0], SIP_NEEDDESTROY));
10681 ast_cli(fd, " Last Message: %s\n", cur->lastmsg);
10682 ast_cli(fd, " Promiscuous Redir: %s\n", ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR) ? "Yes" : "No");
10683 ast_cli(fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
10684 ast_cli(fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
10685 ast_cli(fd, " SIP Options: ");
10686 if (cur->sipoptions) {
10687 int x;
10688 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
10689 if (cur->sipoptions & sip_options[x].id)
10690 ast_cli(fd, "%s ", sip_options[x].text);
10691 }
10692 } else
10693 ast_cli(fd, "(none)\n");
10694 ast_cli(fd, "\n\n");
10695 found++;
10696 }
10697 }
10698 ast_mutex_unlock(&iflock);
10699 if (!found)
10700 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
10701 return RESULT_SUCCESS;
10702 }
10703
10704
10705 static int sip_show_history(int fd, int argc, char *argv[])
10706 {
10707 struct sip_pvt *cur;
10708 size_t len;
10709 int found = 0;
10710
10711 if (argc != 4)
10712 return RESULT_SHOWUSAGE;
10713 if (!recordhistory)
10714 ast_cli(fd, "\n***Note: History recording is currently DISABLED. Use 'sip history' to ENABLE.\n");
10715 len = strlen(argv[3]);
10716 ast_mutex_lock(&iflock);
10717 for (cur = iflist; cur; cur = cur->next) {
10718 if (!strncasecmp(cur->callid, argv[3], len)) {
10719 struct sip_history *hist;
10720 int x = 0;
10721
10722 ast_cli(fd,"\n");
10723 if (cur->subscribed != NONE)
10724 ast_cli(fd, " * Subscription\n");
10725 else
10726 ast_cli(fd, " * SIP Call\n");
10727 if (cur->history)
10728 AST_LIST_TRAVERSE(cur->history, hist, list)
10729 ast_cli(fd, "%d. %s\n", ++x, hist->event);
10730 if (x == 0)
10731 ast_cli(fd, "Call '%s' has no history\n", cur->callid);
10732 found++;
10733 }
10734 }
10735 ast_mutex_unlock(&iflock);
10736 if (!found)
10737 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
10738 return RESULT_SUCCESS;
10739 }
10740
10741
10742 static void sip_dump_history(struct sip_pvt *dialog)
10743 {
10744 int x = 0;
10745 struct sip_history *hist;
10746 static int errmsg = 0;
10747
10748 if (!dialog)
10749 return;
10750
10751 if (!option_debug && !sipdebug) {
10752 if (!errmsg) {
10753 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
10754 errmsg = 1;
10755 }
10756 return;
10757 }
10758
10759 ast_log(LOG_DEBUG, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
10760 if (dialog->subscribed)
10761 ast_log(LOG_DEBUG, " * Subscription\n");
10762 else
10763 ast_log(LOG_DEBUG, " * SIP Call\n");
10764 if (dialog->history)
10765 AST_LIST_TRAVERSE(dialog->history, hist, list)
10766 ast_log(LOG_DEBUG, " %-3.3d. %s\n", ++x, hist->event);
10767 if (!x)
10768 ast_log(LOG_DEBUG, "Call '%s' has no history\n", dialog->callid);
10769 ast_log(LOG_DEBUG, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
10770 }
10771
10772
10773
10774
10775 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
10776 {
10777 char buf[1024];
10778 unsigned int event;
10779 const char *c = get_header(req, "Content-Type");
10780
10781
10782 if (!strcasecmp(c, "application/dtmf-relay") ||
10783 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
10784 unsigned int duration = 0;
10785
10786
10787 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
10788 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
10789 transmit_response(p, "200 OK", req);
10790 return;
10791 } else {
10792 ast_copy_string(buf, c, sizeof(buf));
10793 }
10794
10795 if (!ast_strlen_zero((c = get_body(req, "Duration"))))
10796 duration = atoi(c);
10797 if (!duration)
10798 duration = 100;
10799
10800 if (!p->owner) {
10801 transmit_response(p, "481 Call leg/transaction does not exist", req);
10802 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
10803 return;
10804 }
10805
10806 if (ast_strlen_zero(buf)) {
10807 transmit_response(p, "200 OK", req);
10808 return;
10809 }
10810
10811 if (buf[0] == '*')
10812 event = 10;
10813 else if (buf[0] == '#')
10814 event = 11;
10815 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
10816 event = 12 + buf[0] - 'A';
10817 else
10818 event = atoi(buf);
10819 if (event == 16) {
10820
10821 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
10822 ast_queue_frame(p->owner, &f);
10823 if (sipdebug)
10824 ast_verbose("* DTMF-relay event received: FLASH\n");
10825 } else {
10826
10827 struct ast_frame f = { AST_FRAME_DTMF, };
10828 if (event < 10) {
10829 f.subclass = '0' + event;
10830 } else if (event < 11) {
10831 f.subclass = '*';
10832 } else if (event < 12) {
10833 f.subclass = '#';
10834 } else if (event < 16) {
10835 f.subclass = 'A' + (event - 12);
10836 }
10837 f.len = duration;
10838 ast_queue_frame(p->owner, &f);
10839 if (sipdebug)
10840 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
10841 }
10842 transmit_response(p, "200 OK", req);
10843 return;
10844 } else if (!strcasecmp(c, "application/media_control+xml")) {
10845
10846 if (p->owner)
10847 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
10848 transmit_response(p, "200 OK", req);
10849 return;
10850 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
10851
10852 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
10853 if (p->owner && p->owner->cdr)
10854 ast_cdr_setuserfield(p->owner, c);
10855 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
10856 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
10857 transmit_response(p, "200 OK", req);
10858 } else {
10859 transmit_response(p, "403 Unauthorized", req);
10860 }
10861 return;
10862 }
10863
10864
10865
10866 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
10867 transmit_response(p, "415 Unsupported media type", req);
10868 return;
10869 }
10870
10871
10872 static int sip_do_debug_ip(int fd, int argc, char *argv[])
10873 {
10874 struct hostent *hp;
10875 struct ast_hostent ahp;
10876 int port = 0;
10877 char *p, *arg;
10878
10879
10880 if (argc != 5)
10881 return RESULT_SHOWUSAGE;
10882 p = arg = argv[4];
10883 strsep(&p, ":");
10884 if (p)
10885 port = atoi(p);
10886 hp = ast_gethostbyname(arg, &ahp);
10887 if (hp == NULL)
10888 return RESULT_SHOWUSAGE;
10889
10890 debugaddr.sin_family = AF_INET;
10891 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
10892 debugaddr.sin_port = htons(port);
10893 if (port == 0)
10894 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(debugaddr.sin_addr));
10895 else
10896 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), port);
10897
10898 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
10899
10900 return RESULT_SUCCESS;
10901 }
10902
10903
10904 static int sip_do_debug_peer(int fd, int argc, char *argv[])
10905 {
10906 struct sip_peer *peer;
10907 if (argc != 5)
10908 return RESULT_SHOWUSAGE;
10909 peer = find_peer(argv[4], NULL, 1);
10910 if (peer) {
10911 if (peer->addr.sin_addr.s_addr) {
10912 debugaddr.sin_family = AF_INET;
10913 debugaddr.sin_addr = peer->addr.sin_addr;
10914 debugaddr.sin_port = peer->addr.sin_port;
10915 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
10916 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
10917 } else
10918 ast_cli(fd, "Unable to get IP address of peer '%s'\n", argv[4]);
10919 ASTOBJ_UNREF(peer,sip_destroy_peer);
10920 } else
10921 ast_cli(fd, "No such peer '%s'\n", argv[4]);
10922 return RESULT_SUCCESS;
10923 }
10924
10925
10926 static int sip_do_debug(int fd, int argc, char *argv[])
10927 {
10928 int oldsipdebug = sipdebug_console;
10929 if (argc != 3) {
10930 if (argc != 5)
10931 return RESULT_SHOWUSAGE;
10932 else if (strcmp(argv[3], "ip") == 0)
10933 return sip_do_debug_ip(fd, argc, argv);
10934 else if (strcmp(argv[3], "peer") == 0)
10935 return sip_do_debug_peer(fd, argc, argv);
10936 else
10937 return RESULT_SHOWUSAGE;
10938 }
10939 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
10940 memset(&debugaddr, 0, sizeof(debugaddr));
10941 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
10942 return RESULT_SUCCESS;
10943 }
10944
10945 static int sip_do_debug_deprecated(int fd, int argc, char *argv[])
10946 {
10947 int oldsipdebug = sipdebug_console;
10948 char *newargv[6] = { "sip", "set", "debug", NULL };
10949 if (argc != 2) {
10950 if (argc != 4)
10951 return RESULT_SHOWUSAGE;
10952 else if (strcmp(argv[2], "ip") == 0) {
10953 newargv[3] = argv[2];
10954 newargv[4] = argv[3];
10955 return sip_do_debug_ip(fd, argc + 1, newargv);
10956 } else if (strcmp(argv[2], "peer") == 0) {
10957 newargv[3] = argv[2];
10958 newargv[4] = argv[3];
10959 return sip_do_debug_peer(fd, argc + 1, newargv);
10960 } else
10961 return RESULT_SHOWUSAGE;
10962 }
10963 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
10964 memset(&debugaddr, 0, sizeof(debugaddr));
10965 ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
10966 return RESULT_SUCCESS;
10967 }
10968
10969
10970 static int sip_notify(int fd, int argc, char *argv[])
10971 {
10972 struct ast_variable *varlist;
10973 int i;
10974
10975 if (argc < 4)
10976 return RESULT_SHOWUSAGE;
10977
10978 if (!notify_types) {
10979 ast_cli(fd, "No %s file found, or no types listed there\n", notify_config);
10980 return RESULT_FAILURE;
10981 }
10982
10983 varlist = ast_variable_browse(notify_types, argv[2]);
10984
10985 if (!varlist) {
10986 ast_cli(fd, "Unable to find notify type '%s'\n", argv[2]);
10987 return RESULT_FAILURE;
10988 }
10989
10990 for (i = 3; i < argc; i++) {
10991 struct sip_pvt *p;
10992 struct sip_request req;
10993 struct ast_variable *var;
10994
10995 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY))) {
10996 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
10997 return RESULT_FAILURE;
10998 }
10999
11000 if (create_addr(p, argv[i])) {
11001
11002 sip_destroy(p);
11003 ast_cli(fd, "Could not create address for '%s'\n", argv[i]);
11004 continue;
11005 }
11006
11007 initreqprep(&req, p, SIP_NOTIFY);
11008
11009 for (var = varlist; var; var = var->next)
11010 add_header(&req, var->name, var->value);
11011
11012
11013 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
11014 p->ourip = __ourip;
11015 build_via(p);
11016 build_callid_pvt(p);
11017 ast_cli(fd, "Sending NOTIFY of type '%s' to '%s'\n", argv[2], argv[i]);
11018 transmit_sip_request(p, &req);
11019 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11020 }
11021
11022 return RESULT_SUCCESS;
11023 }
11024
11025
11026 static int sip_no_debug(int fd, int argc, char *argv[])
11027 {
11028 if (argc != 4)
11029 return RESULT_SHOWUSAGE;
11030 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11031 ast_cli(fd, "SIP Debugging Disabled\n");
11032 return RESULT_SUCCESS;
11033 }
11034
11035 static int sip_no_debug_deprecated(int fd, int argc, char *argv[])
11036 {
11037 if (argc != 3)
11038 return RESULT_SHOWUSAGE;
11039 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
11040 ast_cli(fd, "SIP Debugging Disabled\n");
11041 return RESULT_SUCCESS;
11042 }
11043
11044
11045 static int sip_do_history(int fd, int argc, char *argv[])
11046 {
11047 if (argc != 2) {
11048 return RESULT_SHOWUSAGE;
11049 }
11050 recordhistory = TRUE;
11051 ast_cli(fd, "SIP History Recording Enabled (use 'sip show history')\n");
11052 return RESULT_SUCCESS;
11053 }
11054
11055
11056 static int sip_no_history(int fd, int argc, char *argv[])
11057 {
11058 if (argc != 3) {
11059 return RESULT_SHOWUSAGE;
11060 }
11061 recordhistory = FALSE;
11062 ast_cli(fd, "SIP History Recording Disabled\n");
11063 return RESULT_SUCCESS;
11064 }
11065
11066
11067 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader)
11068 {
11069 char digest[1024];
11070 p->authtries++;
11071 memset(digest,0,sizeof(digest));
11072 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
11073
11074
11075 if (sip_debug_test_pvt(p) && p->registry)
11076 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
11077
11078 return -1;
11079 }
11080 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
11081 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
11082 if (sip_debug_test_pvt(p) && p->registry)
11083 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
11084 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
11085 }
11086
11087
11088 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader, int sipmethod, int init)
11089 {
11090 char digest[1024];
11091
11092 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
11093 return -2;
11094
11095 p->authtries++;
11096 if (option_debug > 1)
11097 ast_log(LOG_DEBUG, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
11098 memset(digest, 0, sizeof(digest));
11099 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
11100
11101 return -1;
11102 }
11103
11104 p->options->auth = digest;
11105 p->options->authheader = respheader;
11106 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
11107 }
11108
11109
11110
11111
11112
11113 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
11114 {
11115 char tmp[512];
11116 char *c;
11117 char oldnonce[256];
11118
11119
11120 const struct x {
11121 const char *key;
11122 int field_index;
11123 } *i, keys[] = {
11124 { "realm=", ast_string_field_index(p, realm) },
11125 { "nonce=", ast_string_field_index(p, nonce) },
11126 { "opaque=", ast_string_field_index(p, opaque) },
11127 { "qop=", ast_string_field_index(p, qop) },
11128 { "domain=", ast_string_field_index(p, domain) },
11129 { NULL, 0 },
11130 };
11131
11132 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
11133 if (ast_strlen_zero(tmp))
11134 return -1;
11135 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
11136 ast_log(LOG_WARNING, "missing Digest.\n");
11137 return -1;
11138 }
11139 c = tmp + strlen("Digest ");
11140 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
11141 while (c && *(c = ast_skip_blanks(c))) {
11142 for (i = keys; i->key != NULL; i++) {
11143 char *src, *separator;
11144 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
11145 continue;
11146
11147 c += strlen(i->key);
11148 if (*c == '"') {
11149 src = ++c;
11150 separator = "\"";
11151 } else {
11152 src = c;
11153 separator = ",";
11154 }
11155 strsep(&c, separator);
11156 ast_string_field_index_set(p, i->field_index, src);
11157 break;
11158 }
11159 if (i->key == NULL)
11160 strsep(&c, ",");
11161 }
11162
11163 if (strcmp(p->nonce, oldnonce))
11164 p->noncecount = 0;
11165
11166
11167 if (p->registry) {
11168 struct sip_registry *r = p->registry;
11169
11170 if (strcmp(r->nonce, p->nonce)) {
11171 ast_string_field_set(r, realm, p->realm);
11172 ast_string_field_set(r, nonce, p->nonce);
11173 ast_string_field_set(r, domain, p->domain);
11174 ast_string_field_set(r, opaque, p->opaque);
11175 ast_string_field_set(r, qop, p->qop);
11176 r->noncecount = 0;
11177 }
11178 }
11179 return build_reply_digest(p, sipmethod, digest, digest_len);
11180 }
11181
11182
11183
11184
11185
11186
11187 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
11188 {
11189 char a1[256];
11190 char a2[256];
11191 char a1_hash[256];
11192 char a2_hash[256];
11193 char resp[256];
11194 char resp_hash[256];
11195 char uri[256];
11196 char cnonce[80];
11197 const char *username;
11198 const char *secret;
11199 const char *md5secret;
11200 struct sip_auth *auth = NULL;
11201
11202 if (!ast_strlen_zero(p->domain))
11203 ast_copy_string(uri, p->domain, sizeof(uri));
11204 else if (!ast_strlen_zero(p->uri))
11205 ast_copy_string(uri, p->uri, sizeof(uri));
11206 else
11207 snprintf(uri, sizeof(uri), "sip:%s@%s",p->username, ast_inet_ntoa(p->sa.sin_addr));
11208
11209 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
11210
11211
11212 if ((auth = find_realm_authentication(authl, p->realm))) {
11213 ast_log(LOG_WARNING, "use realm [%s] from peer [%s][%s]\n",
11214 auth->username, p->peername, p->username);
11215 username = auth->username;
11216 secret = auth->secret;
11217 md5secret = auth->md5secret;
11218 if (sipdebug)
11219 ast_log(LOG_DEBUG,"Using realm %s authentication for call %s\n", p->realm, p->callid);
11220 } else {
11221
11222 username = p->authname;
11223 secret = p->peersecret;
11224 md5secret = p->peermd5secret;
11225 }
11226 if (ast_strlen_zero(username))
11227 return -1;
11228
11229
11230 snprintf(a1,sizeof(a1),"%s:%s:%s", username, p->realm, secret);
11231 snprintf(a2,sizeof(a2),"%s:%s", sip_methods[method].text, uri);
11232 if (!ast_strlen_zero(md5secret))
11233 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
11234 else
11235 ast_md5_hash(a1_hash,a1);
11236 ast_md5_hash(a2_hash,a2);
11237
11238 p->noncecount++;
11239 if (!ast_strlen_zero(p->qop))
11240 snprintf(resp,sizeof(resp),"%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
11241 else
11242 snprintf(resp,sizeof(resp),"%s:%s:%s", a1_hash, p->nonce, a2_hash);
11243 ast_md5_hash(resp_hash, resp);
11244
11245 if (!ast_strlen_zero(p->qop))
11246 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);
11247 else
11248 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);
11249
11250 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
11251
11252 return 0;
11253 }
11254
11255 static char show_domains_usage[] =
11256 "Usage: sip show domains\n"
11257 " Lists all configured SIP local domains.\n"
11258 " Asterisk only responds to SIP messages to local domains.\n";
11259
11260 static char notify_usage[] =
11261 "Usage: sip notify <type> <peer> [<peer>...]\n"
11262 " Send a NOTIFY message to a SIP peer or peers\n"
11263 " Message types are defined in sip_notify.conf\n";
11264
11265 static char show_users_usage[] =
11266 "Usage: sip show users [like <pattern>]\n"
11267 " Lists all known SIP users.\n"
11268 " Optional regular expression pattern is used to filter the user list.\n";
11269
11270 static char show_user_usage[] =
11271 "Usage: sip show user <name> [load]\n"
11272 " Shows all details on one SIP user and the current status.\n"
11273 " Option \"load\" forces lookup of peer in realtime storage.\n";
11274
11275 static char show_inuse_usage[] =
11276 "Usage: sip show inuse [all]\n"
11277 " List all SIP users and peers usage counters and limits.\n"
11278 " Add option \"all\" to show all devices, not only those with a limit.\n";
11279
11280 static char show_channels_usage[] =
11281 "Usage: sip show channels\n"
11282 " Lists all currently active SIP channels.\n";
11283
11284 static char show_channel_usage[] =
11285 "Usage: sip show channel <channel>\n"
11286 " Provides detailed status on a given SIP channel.\n";
11287
11288 static char show_history_usage[] =
11289 "Usage: sip show history <channel>\n"
11290 " Provides detailed dialog history on a given SIP channel.\n";
11291
11292 static char show_peers_usage[] =
11293 "Usage: sip show peers [like <pattern>]\n"
11294 " Lists all known SIP peers.\n"
11295 " Optional regular expression pattern is used to filter the peer list.\n";
11296
11297 static char show_peer_usage[] =
11298 "Usage: sip show peer <name> [load]\n"
11299 " Shows all details on one SIP peer and the current status.\n"
11300 " Option \"load\" forces lookup of peer in realtime storage.\n";
11301
11302 static char prune_realtime_usage[] =
11303 "Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
11304 " Prunes object(s) from the cache.\n"
11305 " Optional regular expression pattern is used to filter the objects.\n";
11306
11307 static char show_reg_usage[] =
11308 "Usage: sip show registry\n"
11309 " Lists all registration requests and status.\n";
11310
11311 static char debug_usage[] =
11312 "Usage: sip set debug\n"
11313 " Enables dumping of SIP packets for debugging purposes\n\n"
11314 " sip set debug ip <host[:PORT]>\n"
11315 " Enables dumping of SIP packets to and from host.\n\n"
11316 " sip set debug peer <peername>\n"
11317 " Enables dumping of SIP packets to and from host.\n"
11318 " Require peer to be registered.\n";
11319
11320 static char no_debug_usage[] =
11321 "Usage: sip set debug off\n"
11322 " Disables dumping of SIP packets for debugging purposes\n";
11323
11324 static char no_history_usage[] =
11325 "Usage: sip history off\n"
11326 " Disables recording of SIP dialog history for debugging purposes\n";
11327
11328 static char history_usage[] =
11329 "Usage: sip history\n"
11330 " Enables recording of SIP dialog history for debugging purposes.\n"
11331 "Use 'sip show history' to view the history of a call number.\n";
11332
11333 static char sip_reload_usage[] =
11334 "Usage: sip reload\n"
11335 " Reloads SIP configuration from sip.conf\n";
11336
11337 static char show_subscriptions_usage[] =
11338 "Usage: sip show subscriptions\n"
11339 " Lists active SIP subscriptions for extension states\n";
11340
11341 static char show_objects_usage[] =
11342 "Usage: sip show objects\n"
11343 " Lists status of known SIP objects\n";
11344
11345 static char show_settings_usage[] =
11346 "Usage: sip show settings\n"
11347 " Provides detailed list of the configuration of the SIP channel.\n";
11348
11349
11350 static int func_header_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
11351 {
11352 struct sip_pvt *p;
11353 const char *content = NULL;
11354 AST_DECLARE_APP_ARGS(args,
11355 AST_APP_ARG(header);
11356 AST_APP_ARG(number);
11357 );
11358 int i, number, start = 0;
11359
11360 if (ast_strlen_zero(data)) {
11361 ast_log(LOG_WARNING, "This function requires a header name.\n");
11362 return -1;
11363 }
11364
11365 ast_channel_lock(chan);
11366 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
11367 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
11368 ast_channel_unlock(chan);
11369 return -1;
11370 }
11371
11372 AST_STANDARD_APP_ARGS(args, data);
11373 if (!args.number) {
11374 number = 1;
11375 } else {
11376 sscanf(args.number, "%d", &number);
11377 if (number < 1)
11378 number = 1;
11379 }
11380
11381 p = chan->tech_pvt;
11382
11383
11384 if (!p) {
11385 ast_channel_unlock(chan);
11386 return -1;
11387 }
11388
11389 for (i = 0; i < number; i++)
11390 content = __get_header(&p->initreq, args.header, &start);
11391
11392 if (ast_strlen_zero(content)) {
11393 ast_channel_unlock(chan);
11394 return -1;
11395 }
11396
11397 ast_copy_string(buf, content, len);
11398 ast_channel_unlock(chan);
11399
11400 return 0;
11401 }
11402
11403 static struct ast_custom_function sip_header_function = {
11404 .name = "SIP_HEADER",
11405 .synopsis = "Gets the specified SIP header",
11406 .syntax = "SIP_HEADER(<name>[,<number>])",
11407 .desc = "Since there are several headers (such as Via) which can occur multiple\n"
11408 "times, SIP_HEADER takes an optional second argument to specify which header with\n"
11409 "that name to retrieve. Headers start at offset 1.\n",
11410 .read = func_header_read,
11411 };
11412
11413
11414 static int func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11415 {
11416 if (ast_strlen_zero(data)) {
11417 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
11418 return -1;
11419 }
11420 if (check_sip_domain(data, NULL, 0))
11421 ast_copy_string(buf, data, len);
11422 else
11423 buf[0] = '\0';
11424 return 0;
11425 }
11426
11427 static struct ast_custom_function checksipdomain_function = {
11428 .name = "CHECKSIPDOMAIN",
11429 .synopsis = "Checks if domain is a local domain",
11430 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
11431 .read = func_check_sipdomain,
11432 .desc = "This function checks if the domain in the argument is configured\n"
11433 "as a local SIP domain that this Asterisk server is configured to handle.\n"
11434 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
11435 "Check the domain= configuration in sip.conf\n",
11436 };
11437
11438
11439 static int function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11440 {
11441 struct sip_peer *peer;
11442 char *colname;
11443
11444 if ((colname = strchr(data, ':')))
11445 *colname++ = '\0';
11446 else if ((colname = strchr(data, '|')))
11447 *colname++ = '\0';
11448 else
11449 colname = "ip";
11450
11451 if (!(peer = find_peer(data, NULL, 1)))
11452 return -1;
11453
11454 if (!strcasecmp(colname, "ip")) {
11455 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
11456 } else if (!strcasecmp(colname, "status")) {
11457 peer_status(peer, buf, len);
11458 } else if (!strcasecmp(colname, "language")) {
11459 ast_copy_string(buf, peer->language, len);
11460 } else if (!strcasecmp(colname, "regexten")) {
11461 ast_copy_string(buf, peer->regexten, len);
11462 } else if (!strcasecmp(colname, "limit")) {
11463 snprintf(buf, len, "%d", peer->call_limit);
11464 } else if (!strcasecmp(colname, "curcalls")) {
11465 snprintf(buf, len, "%d", peer->inUse);
11466 } else if (!strcasecmp(colname, "accountcode")) {
11467 ast_copy_string(buf, peer->accountcode, len);
11468 } else if (!strcasecmp(colname, "useragent")) {
11469 ast_copy_string(buf, peer->useragent, len);
11470 } else if (!strcasecmp(colname, "mailbox")) {
11471 ast_copy_string(buf, peer->mailbox, len);
11472 } else if (!strcasecmp(colname, "context")) {
11473 ast_copy_string(buf, peer->context, len);
11474 } else if (!strcasecmp(colname, "expire")) {
11475 snprintf(buf, len, "%d", peer->expire);
11476 } else if (!strcasecmp(colname, "dynamic")) {
11477 ast_copy_string(buf, (ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) ? "yes" : "no"), len);
11478 } else if (!strcasecmp(colname, "callerid_name")) {
11479 ast_copy_string(buf, peer->cid_name, len);
11480 } else if (!strcasecmp(colname, "callerid_num")) {
11481 ast_copy_string(buf, peer->cid_num, len);
11482 } else if (!strcasecmp(colname, "codecs")) {
11483 ast_getformatname_multiple(buf, len -1, peer->capability);
11484 } else if (!strncasecmp(colname, "codec[", 6)) {
11485 char *codecnum;
11486 int index = 0, codec = 0;
11487
11488 codecnum = colname + 6;
11489 codecnum = strsep(&codecnum, "]");
11490 index = atoi(codecnum);
11491 if((codec = ast_codec_pref_index(&peer->prefs, index))) {
11492 ast_copy_string(buf, ast_getformatname(codec), len);
11493 }
11494 }
11495
11496 ASTOBJ_UNREF(peer, sip_destroy_peer);
11497
11498 return 0;
11499 }
11500
11501
11502 struct ast_custom_function sippeer_function = {
11503 .name = "SIPPEER",
11504 .synopsis = "Gets SIP peer information",
11505 .syntax = "SIPPEER(<peername>[|item])",
11506 .read = function_sippeer,
11507 .desc = "Valid items are:\n"
11508 "- ip (default) The IP address.\n"
11509 "- mailbox The configured mailbox.\n"
11510 "- context The configured context.\n"
11511 "- expire The epoch time of the next expire.\n"
11512 "- dynamic Is it dynamic? (yes/no).\n"
11513 "- callerid_name The configured Caller ID name.\n"
11514 "- callerid_num The configured Caller ID number.\n"
11515 "- codecs The configured codecs.\n"
11516 "- status Status (if qualify=yes).\n"
11517 "- regexten Registration extension\n"
11518 "- limit Call limit (call-limit)\n"
11519 "- curcalls Current amount of calls \n"
11520 " Only available if call-limit is set\n"
11521 "- language Default language for peer\n"
11522 "- accountcode Account code for this peer\n"
11523 "- useragent Current user agent id for peer\n"
11524 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
11525 "\n"
11526 };
11527
11528
11529 static int function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
11530 {
11531 struct sip_pvt *p;
11532
11533 *buf = 0;
11534
11535 if (!data) {
11536 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
11537 return -1;
11538 }
11539
11540 ast_channel_lock(chan);
11541 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
11542 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
11543 ast_channel_unlock(chan);
11544 return -1;
11545 }
11546
11547 p = chan->tech_pvt;
11548
11549
11550 if (!p) {
11551 ast_channel_unlock(chan);
11552 return -1;
11553 }
11554
11555 if (!strcasecmp(data, "peerip")) {
11556 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", len);
11557 } else if (!strcasecmp(data, "recvip")) {
11558 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", len);
11559 } else if (!strcasecmp(data, "from")) {
11560 ast_copy_string(buf, p->from, len);
11561 } else if (!strcasecmp(data, "uri")) {
11562 ast_copy_string(buf, p->uri, len);
11563 } else if (!strcasecmp(data, "useragent")) {
11564 ast_copy_string(buf, p->useragent, len);
11565 } else if (!strcasecmp(data, "peername")) {
11566 ast_copy_string(buf, p->peername, len);
11567 } else if (!strcasecmp(data, "t38passthrough")) {
11568 if (p->t38.state == T38_DISABLED)
11569 ast_copy_string(buf, "0", sizeof("0"));
11570 else
11571 ast_copy_string(buf, "1", sizeof("1"));
11572 } else {
11573 ast_channel_unlock(chan);
11574 return -1;
11575 }
11576 ast_channel_unlock(chan);
11577
11578 return 0;
11579 }
11580
11581
11582 static struct ast_custom_function sipchaninfo_function = {
11583 .name = "SIPCHANINFO",
11584 .synopsis = "Gets the specified SIP parameter from the current channel",
11585 .syntax = "SIPCHANINFO(item)",
11586 .read = function_sipchaninfo_read,
11587 .desc = "Valid items are:\n"
11588 "- peerip The IP address of the peer.\n"
11589 "- recvip The source IP address of the peer.\n"
11590 "- from The URI from the From: header.\n"
11591 "- uri The URI from the Contact: header.\n"
11592 "- useragent The useragent.\n"
11593 "- peername The name of the peer.\n"
11594 "- t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
11595 };
11596
11597
11598 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
11599 {
11600 char tmp[BUFSIZ];
11601 char *s, *e;
11602 char *domain;
11603
11604 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
11605 s = get_in_brackets(tmp);
11606 s = strsep(&s, ";");
11607 if (ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
11608 if (!strncasecmp(s, "sip:", 4))
11609 s += 4;
11610 e = strchr(s, '/');
11611 if (e)
11612 *e = '\0';
11613 if (option_debug)
11614 ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
11615 if (p->owner)
11616 ast_string_field_build(p->owner, call_forward, "SIP/%s", s);
11617 } else {
11618 e = strchr(tmp, '@');
11619 if (e) {
11620 *e++ = '\0';
11621 domain = e;
11622 } else {
11623
11624 domain = tmp;
11625 }
11626 e = strchr(tmp, '/');
11627 if (e)
11628 *e = '\0';
11629 if (!strncasecmp(s, "sip:", 4))
11630 s += 4;
11631 if (option_debug > 1)
11632 ast_log(LOG_DEBUG, "Received 302 Redirect to extension '%s' (domain %s)\n", s, domain);
11633 if (p->owner) {
11634 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
11635 ast_string_field_set(p->owner, call_forward, s);
11636 }
11637 }
11638 }
11639
11640
11641 static void check_pendings(struct sip_pvt *p)
11642 {
11643 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
11644
11645 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA)
11646 transmit_request(p, SIP_CANCEL, p->ocseq, XMIT_RELIABLE, FALSE);
11647
11648
11649 else
11650 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
11651 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
11652 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11653 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
11654 if (option_debug)
11655 ast_log(LOG_DEBUG, "Sending pending reinvite on '%s'\n", p->callid);
11656
11657 transmit_reinvite_with_sdp(p);
11658 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
11659 }
11660 }
11661
11662
11663 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
11664 {
11665 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
11666 int res = 0;
11667 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
11668 struct ast_channel *bridgepeer = NULL;
11669
11670 if (option_debug > 3) {
11671 if (reinvite)
11672 ast_log(LOG_DEBUG, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
11673 else
11674 ast_log(LOG_DEBUG, "SIP response %d to standard invite\n", resp);
11675 }
11676
11677 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
11678 if (option_debug)
11679 ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
11680 return;
11681 }
11682
11683
11684 if (p->initid > -1) {
11685
11686 ast_sched_del(sched, p->initid);
11687 p->initid = -1;
11688 }
11689
11690
11691
11692
11693 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 183)
11694 resp = 183;
11695
11696
11697 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
11698 p->invitestate = INV_PROCEEDING;
11699
11700
11701 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
11702 p->invitestate = INV_COMPLETED;
11703
11704
11705 switch (resp) {
11706 case 100:
11707 case 101:
11708 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11709 sip_cancel_destroy(p);
11710 check_pendings(p);
11711 break;
11712
11713 case 180:
11714 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11715 sip_cancel_destroy(p);
11716 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11717 ast_queue_control(p->owner, AST_CONTROL_RINGING);
11718 if (p->owner->_state != AST_STATE_UP) {
11719 ast_setstate(p->owner, AST_STATE_RINGING);
11720 }
11721 }
11722 if (find_sdp(req)) {
11723 p->invitestate = INV_EARLY_MEDIA;
11724 res = process_sdp(p, req);
11725 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11726
11727 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
11728 }
11729 }
11730 check_pendings(p);
11731 break;
11732
11733 case 183:
11734 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11735 sip_cancel_destroy(p);
11736
11737 if (find_sdp(req)) {
11738 p->invitestate = INV_EARLY_MEDIA;
11739 res = process_sdp(p, req);
11740 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11741
11742 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
11743 }
11744 }
11745 check_pendings(p);
11746 break;
11747
11748 case 200:
11749 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11750 sip_cancel_destroy(p);
11751 p->authtries = 0;
11752 if (find_sdp(req)) {
11753 if ((res = process_sdp(p, req)) && !ast_test_flag(req, SIP_PKT_IGNORE))
11754 if (!reinvite)
11755
11756
11757 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
11758 }
11759
11760
11761
11762
11763 if (outgoing) {
11764 update_call_counter(p, DEC_CALL_RINGING);
11765 parse_ok_contact(p, req);
11766 if(set_address_from_contact(p)) {
11767
11768
11769
11770
11771
11772
11773
11774 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11775 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
11776 }
11777
11778
11779 build_route(p, req, 1);
11780 }
11781
11782 if (p->owner && (p->owner->_state == AST_STATE_UP) && (bridgepeer = ast_bridged_channel(p->owner))) {
11783 struct sip_pvt *bridgepvt = NULL;
11784
11785 if (!bridgepeer->tech) {
11786 ast_log(LOG_WARNING, "Ooooh.. no tech! That's REALLY bad\n");
11787 break;
11788 }
11789 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
11790 bridgepvt = (struct sip_pvt*)(bridgepeer->tech_pvt);
11791 if (bridgepvt->udptl) {
11792 if (p->t38.state == T38_PEER_REINVITE) {
11793 sip_handle_t38_reinvite(bridgepeer, p, 0);
11794 ast_rtp_set_rtptimers_onhold(p->rtp);
11795 if (p->vrtp)
11796 ast_rtp_set_rtptimers_onhold(p->vrtp);
11797 } else if (p->t38.state == T38_DISABLED && bridgepeer && (bridgepvt->t38.state == T38_ENABLED)) {
11798 ast_log(LOG_WARNING, "RTP re-inivte after T38 session not handled yet !\n");
11799
11800
11801 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11802 }
11803 } else {
11804 if (option_debug > 1)
11805 ast_log(LOG_DEBUG, "Strange... The other side of the bridge does not have a udptl struct\n");
11806 ast_mutex_lock(&bridgepvt->lock);
11807 bridgepvt->t38.state = T38_DISABLED;
11808 ast_mutex_unlock(&bridgepvt->lock);
11809 if (option_debug)
11810 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->tech->type);
11811 p->t38.state = T38_DISABLED;
11812 if (option_debug > 1)
11813 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
11814 }
11815 } else {
11816
11817 if (option_debug > 1)
11818 ast_log(LOG_DEBUG, "Strange... The other side of the bridge is not a SIP channel\n");
11819 p->t38.state = T38_DISABLED;
11820 if (option_debug > 1)
11821 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
11822 }
11823 }
11824 if ((p->t38.state == T38_LOCAL_REINVITE) || (p->t38.state == T38_LOCAL_DIRECT)) {
11825
11826 p->t38.state = T38_ENABLED;
11827 if (option_debug)
11828 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
11829 }
11830
11831 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
11832 if (!reinvite) {
11833 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
11834 } else {
11835 ast_queue_frame(p->owner, &ast_null_frame);
11836 }
11837 } else {
11838
11839
11840
11841 if (!ast_test_flag(req, SIP_PKT_IGNORE))
11842 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
11843 }
11844
11845 p->invitestate = INV_TERMINATED;
11846 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
11847 check_pendings(p);
11848 break;
11849 case 407:
11850 case 401:
11851
11852 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11853 if (p->options)
11854 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
11855
11856
11857 ast_string_field_free(p, theirtag);
11858 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
11859 char *authenticate = (resp == 401 ? "WWW-Authenticate" : "Proxy-Authenticate");
11860 char *authorization = (resp == 401 ? "Authorization" : "Proxy-Authorization");
11861 if (p->authtries < MAX_AUTHTRIES)
11862 p->invitestate = INV_CALLING;
11863 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, authenticate, authorization, SIP_INVITE, 1)) {
11864 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
11865 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11866 sip_alreadygone(p);
11867 if (p->owner)
11868 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11869 }
11870 }
11871 break;
11872
11873 case 403:
11874
11875 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11876 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
11877 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner)
11878 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11879 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11880 sip_alreadygone(p);
11881 break;
11882
11883 case 404:
11884 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11885 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
11886 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11887 sip_alreadygone(p);
11888 break;
11889
11890 case 481:
11891
11892 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
11893 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11894 if (p->owner)
11895 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11896 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
11897 break;
11898 case 487:
11899
11900
11901
11902 transmit_request(p, SIP_ACK, seqno, 0, 0);
11903 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
11904 ast_queue_hangup(p->owner);
11905 else if (!ast_test_flag(req, SIP_PKT_IGNORE))
11906 update_call_counter(p, DEC_CALL_LIMIT);
11907 break;
11908 case 488:
11909 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11910 if (reinvite && p->udptl) {
11911
11912
11913
11914
11915
11916 p->t38.state = T38_DISABLED;
11917
11918 ast_rtp_set_rtptimers_onhold(p->rtp);
11919 ast_log(LOG_ERROR, "Got error on T.38 re-invite. Bad configuration. Peer needs to have T.38 disabled.\n");
11920
11921
11922
11923
11924
11925 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
11926 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11927 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11928 } else {
11929
11930 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
11931 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11932 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11933 }
11934 break;
11935 case 491:
11936
11937
11938
11939 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
11940 if (p->owner && !ast_test_flag(req, SIP_PKT_IGNORE))
11941 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11942 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11943 break;
11944
11945 case 501:
11946 transmit_request(p, SIP_ACK, seqno, 0, 0);
11947 if (p->owner)
11948 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
11949 break;
11950 }
11951 }
11952
11953
11954
11955
11956 static void handle_response_refer(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int seqno)
11957 {
11958 char *auth = "Proxy-Authenticate";
11959 char *auth2 = "Proxy-Authorization";
11960
11961
11962 if (!p->refer)
11963 return;
11964
11965 switch (resp) {
11966 case 202:
11967
11968
11969 p->refer->status = REFER_ACCEPTED;
11970
11971 if (option_debug > 2)
11972 ast_log(LOG_DEBUG, "Got 202 accepted on transfer\n");
11973
11974 break;
11975
11976 case 401:
11977 case 407:
11978 if (ast_strlen_zero(p->authname)) {
11979 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s:%d but we have no matching peer or realm auth!\n",
11980 ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
11981 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11982 }
11983 if (resp == 401) {
11984 auth = "WWW-Authenticate";
11985 auth2 = "Authorization";
11986 }
11987 if ((p->authtries > 1) || do_proxy_auth(p, req, auth, auth2, SIP_REFER, 0)) {
11988 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
11989 p->refer->status = REFER_NOAUTH;
11990 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
11991 }
11992 break;
11993 case 481:
11994
11995
11996
11997
11998 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
11999 if (p->owner)
12000 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12001 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12002 break;
12003
12004 case 500:
12005 case 501:
12006
12007
12008 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
12009 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12010 p->refer->status = REFER_FAILED;
12011 break;
12012 case 603:
12013 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
12014 p->refer->status = REFER_FAILED;
12015 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12016 break;
12017 }
12018 }
12019
12020
12021 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
12022 {
12023 int expires, expires_ms;
12024 struct sip_registry *r;
12025 r=p->registry;
12026
12027 switch (resp) {
12028 case 401:
12029 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "WWW-Authenticate", "Authorization")) {
12030 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
12031 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12032 }
12033 break;
12034 case 403:
12035 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
12036 if (global_regattempts_max)
12037 p->registry->regattempts = global_regattempts_max+1;
12038 ast_sched_del(sched, r->timeout);
12039 r->timeout = -1;
12040 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12041 break;
12042 case 404:
12043 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username,p->registry->hostname);
12044 if (global_regattempts_max)
12045 p->registry->regattempts = global_regattempts_max+1;
12046 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12047 r->call = NULL;
12048 ast_sched_del(sched, r->timeout);
12049 r->timeout = -1;
12050 break;
12051 case 407:
12052 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization")) {
12053 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
12054 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12055 }
12056 break;
12057 case 479:
12058 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username,p->registry->hostname);
12059 if (global_regattempts_max)
12060 p->registry->regattempts = global_regattempts_max+1;
12061 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12062 r->call = NULL;
12063 ast_sched_del(sched, r->timeout);
12064 r->timeout = -1;
12065 break;
12066 case 200:
12067 if (!r) {
12068 ast_log(LOG_WARNING, "Got 200 OK on REGISTER that isn't a register\n");
12069 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12070 return 0;
12071 }
12072
12073 r->regstate = REG_STATE_REGISTERED;
12074 r->regtime = time(NULL);
12075 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
12076 r->regattempts = 0;
12077 if (option_debug)
12078 ast_log(LOG_DEBUG, "Registration successful\n");
12079 if (r->timeout > -1) {
12080 if (option_debug)
12081 ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
12082 ast_sched_del(sched, r->timeout);
12083 }
12084 r->timeout=-1;
12085 r->call = NULL;
12086 p->registry = NULL;
12087
12088 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12089
12090
12091
12092
12093 if (r->expire > -1)
12094 ast_sched_del(sched, r->expire);
12095
12096
12097 expires = 0;
12098
12099
12100 if (!ast_strlen_zero(get_header(req, "Contact"))) {
12101 const char *contact = NULL;
12102 const char *tmptmp = NULL;
12103 int start = 0;
12104 for(;;) {
12105 contact = __get_header(req, "Contact", &start);
12106
12107 if(!ast_strlen_zero(contact)) {
12108 if( (tmptmp=strstr(contact, p->our_contact))) {
12109 contact=tmptmp;
12110 break;
12111 }
12112 } else
12113 break;
12114 }
12115 tmptmp = strcasestr(contact, "expires=");
12116 if (tmptmp) {
12117 if (sscanf(tmptmp + 8, "%d;", &expires) != 1)
12118 expires = 0;
12119 }
12120
12121 }
12122 if (!expires)
12123 expires=atoi(get_header(req, "expires"));
12124 if (!expires)
12125 expires=default_expiry;
12126
12127 expires_ms = expires * 1000;
12128 if (expires <= EXPIRY_GUARD_LIMIT)
12129 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT),EXPIRY_GUARD_MIN);
12130 else
12131 expires_ms -= EXPIRY_GUARD_SECS * 1000;
12132 if (sipdebug)
12133 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
12134
12135 r->refresh= (int) expires_ms / 1000;
12136
12137
12138 r->expire=ast_sched_add(sched, expires_ms, sip_reregister, r);
12139 ASTOBJ_UNREF(r, sip_registry_destroy);
12140 }
12141 return 1;
12142 }
12143
12144
12145 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
12146 {
12147 struct sip_peer *peer = p->relatedpeer;
12148 int statechanged, is_reachable, was_reachable;
12149 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
12150
12151
12152
12153
12154
12155
12156 if (pingtime < 1)
12157 pingtime = 1;
12158
12159
12160
12161
12162
12163 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
12164 is_reachable = pingtime <= peer->maxms;
12165 statechanged = peer->lastms == 0
12166 || was_reachable != is_reachable;
12167
12168 peer->lastms = pingtime;
12169 peer->call = NULL;
12170 if (statechanged) {
12171 const char *s = is_reachable ? "Reachable" : "Lagged";
12172
12173 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
12174 peer->name, s, pingtime, peer->maxms);
12175 ast_device_state_changed("SIP/%s", peer->name);
12176 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
12177 "Peer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
12178 peer->name, s, pingtime);
12179 }
12180
12181 if (peer->pokeexpire > -1)
12182 ast_sched_del(sched, peer->pokeexpire);
12183 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12184
12185
12186 peer->pokeexpire = ast_sched_add(sched,
12187 is_reachable ? DEFAULT_FREQ_OK : DEFAULT_FREQ_NOTOK,
12188 sip_poke_peer_s, peer);
12189 }
12190
12191
12192 static void stop_media_flows(struct sip_pvt *p)
12193 {
12194
12195 if (p->rtp)
12196 ast_rtp_stop(p->rtp);
12197 if (p->vrtp)
12198 ast_rtp_stop(p->vrtp);
12199 if (p->udptl)
12200 ast_udptl_stop(p->udptl);
12201 }
12202
12203
12204
12205 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
12206 {
12207 struct ast_channel *owner;
12208 int sipmethod;
12209 int res = 1;
12210 const char *c = get_header(req, "Cseq");
12211 const char *msg = strchr(c, ' ');
12212
12213 if (!msg)
12214 msg = "";
12215 else
12216 msg++;
12217 sipmethod = find_sip_method(msg);
12218
12219 owner = p->owner;
12220 if (owner)
12221 owner->hangupcause = hangup_sip2cause(resp);
12222
12223
12224 if ((resp >= 100) && (resp <= 199))
12225 __sip_semi_ack(p, seqno, 0, sipmethod);
12226 else
12227 __sip_ack(p, seqno, 0, sipmethod);
12228
12229
12230 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
12231 char tag[128];
12232
12233 gettag(req, "To", tag, sizeof(tag));
12234 ast_string_field_set(p, theirtag, tag);
12235 }
12236 if (p->relatedpeer && p->method == SIP_OPTIONS) {
12237
12238
12239
12240 if (resp != 100)
12241 handle_response_peerpoke(p, resp, req);
12242 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
12243 switch(resp) {
12244 case 100:
12245 case 101:
12246 if (sipmethod == SIP_INVITE)
12247 handle_response_invite(p, resp, rest, req, seqno);
12248 break;
12249 case 183:
12250 if (sipmethod == SIP_INVITE)
12251 handle_response_invite(p, resp, rest, req, seqno);
12252 break;
12253 case 180:
12254 if (sipmethod == SIP_INVITE)
12255 handle_response_invite(p, resp, rest, req, seqno);
12256 break;
12257 case 200:
12258 p->authtries = 0;
12259 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
12260
12261
12262
12263 } else if (sipmethod == SIP_INVITE) {
12264 handle_response_invite(p, resp, rest, req, seqno);
12265 } else if (sipmethod == SIP_NOTIFY) {
12266
12267 if (p->owner) {
12268 if (!p->refer) {
12269 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
12270 ast_queue_hangup(p->owner);
12271 } else if (option_debug > 3)
12272 ast_log(LOG_DEBUG, "Got OK on REFER Notify message\n");
12273 } else {
12274 if (p->subscribed == NONE)
12275 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12276 }
12277 } else if (sipmethod == SIP_REGISTER)
12278 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12279 else if (sipmethod == SIP_BYE)
12280 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12281 break;
12282 case 202:
12283 if (sipmethod == SIP_REFER)
12284 handle_response_refer(p, resp, rest, req, seqno);
12285 break;
12286 case 401:
12287 if (sipmethod == SIP_INVITE)
12288 handle_response_invite(p, resp, rest, req, seqno);
12289 else if (sipmethod == SIP_REFER)
12290 handle_response_refer(p, resp, rest, req, seqno);
12291 else if (p->registry && sipmethod == SIP_REGISTER)
12292 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12293 else {
12294 ast_log(LOG_WARNING, "Got authentication request (401) on unknown %s to '%s'\n", sip_methods[sipmethod].text, get_header(req, "To"));
12295 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12296 }
12297 break;
12298 case 403:
12299 if (sipmethod == SIP_INVITE)
12300 handle_response_invite(p, resp, rest, req, seqno);
12301 else if (p->registry && sipmethod == SIP_REGISTER)
12302 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12303 else {
12304 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
12305 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12306 }
12307 break;
12308 case 404:
12309 if (p->registry && sipmethod == SIP_REGISTER)
12310 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12311 else if (sipmethod == SIP_INVITE)
12312 handle_response_invite(p, resp, rest, req, seqno);
12313 else if (owner)
12314 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12315 break;
12316 case 407:
12317 if (sipmethod == SIP_INVITE)
12318 handle_response_invite(p, resp, rest, req, seqno);
12319 else if (sipmethod == SIP_REFER)
12320 handle_response_refer(p, resp, rest, req, seqno);
12321 else if (p->registry && sipmethod == SIP_REGISTER)
12322 res = handle_response_register(p, resp, rest, req, ignore, seqno);
12323 else if (sipmethod == SIP_BYE) {
12324 if (ast_strlen_zero(p->authname))
12325 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
12326 msg, ast_inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
12327 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12328 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization", sipmethod, 0)) {
12329 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
12330 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12331 }
12332 } else
12333 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12334
12335 break;
12336 case 481:
12337 if (sipmethod == SIP_INVITE) {
12338 handle_response_invite(p, resp, rest, req, seqno);
12339 } else if (sipmethod == SIP_REFER) {
12340 handle_response_refer(p, resp, rest, req, seqno);
12341 } else if (sipmethod == SIP_BYE) {
12342
12343
12344 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12345 } else if (sipmethod == SIP_CANCEL) {
12346
12347
12348 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12349 } else {
12350 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
12351
12352 }
12353 break;
12354 case 487:
12355 if (sipmethod == SIP_INVITE)
12356 handle_response_invite(p, resp, rest, req, seqno);
12357 break;
12358 case 488:
12359 if (sipmethod == SIP_INVITE)
12360 handle_response_invite(p, resp, rest, req, seqno);
12361 break;
12362 case 491:
12363 if (sipmethod == SIP_INVITE)
12364 handle_response_invite(p, resp, rest, req, seqno);
12365 else {
12366 if (option_debug)
12367 ast_log(LOG_DEBUG, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
12368 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12369 }
12370 break;
12371 case 501:
12372 if (sipmethod == SIP_INVITE)
12373 handle_response_invite(p, resp, rest, req, seqno);
12374 else if (sipmethod == SIP_REFER)
12375 handle_response_refer(p, resp, rest, req, seqno);
12376 else
12377 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(p->sa.sin_addr), msg);
12378 break;
12379 case 603:
12380 if (sipmethod == SIP_REFER) {
12381 handle_response_refer(p, resp, rest, req, seqno);
12382 break;
12383 }
12384
12385 default:
12386 if ((resp >= 300) && (resp < 700)) {
12387
12388 if ((option_verbose > 2) && (resp != 487))
12389 ast_verbose(VERBOSE_PREFIX_3 "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(p->sa.sin_addr));
12390
12391 if (sipmethod == SIP_INVITE)
12392 stop_media_flows(p);
12393
12394
12395 switch(resp) {
12396 case 300:
12397 case 301:
12398 case 302:
12399 case 305:
12400 parse_moved_contact(p, req);
12401
12402 case 486:
12403 case 600:
12404 case 603:
12405 if (p->owner)
12406 ast_queue_control(p->owner, AST_CONTROL_BUSY);
12407 break;
12408 case 482:
12409
12410
12411
12412
12413 if (option_debug)
12414 ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
12415 if (p->owner)
12416 ast_string_field_build(p->owner, call_forward,
12417 "Local/%s@%s", p->username, p->context);
12418
12419 case 480:
12420 case 404:
12421 case 410:
12422 case 400:
12423 case 500:
12424 if (sipmethod == SIP_REFER) {
12425 handle_response_refer(p, resp, rest, req, seqno);
12426 break;
12427 }
12428
12429 case 503:
12430 case 504:
12431 if (owner)
12432 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
12433 break;
12434 default:
12435
12436 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
12437 ast_queue_hangup(p->owner);
12438 break;
12439 }
12440
12441 if (sipmethod == SIP_INVITE)
12442 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
12443 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
12444 sip_alreadygone(p);
12445 if (!p->owner)
12446 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12447 } else if ((resp >= 100) && (resp < 200)) {
12448 if (sipmethod == SIP_INVITE) {
12449 if (!ast_test_flag(req, SIP_PKT_IGNORE))
12450 sip_cancel_destroy(p);
12451 if (find_sdp(req))
12452 process_sdp(p, req);
12453 if (p->owner) {
12454
12455 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
12456 }
12457 }
12458 } else
12459 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));
12460 }
12461 } else {
12462
12463
12464 if (ast_test_flag(req, SIP_PKT_DEBUG))
12465 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
12466
12467 if (sipmethod == SIP_INVITE && resp == 200) {
12468
12469
12470 char tag[128];
12471
12472 gettag(req, "To", tag, sizeof(tag));
12473 ast_string_field_set(p, theirtag, tag);
12474 }
12475
12476 switch(resp) {
12477 case 200:
12478 if (sipmethod == SIP_INVITE) {
12479 handle_response_invite(p, resp, rest, req, seqno);
12480 } else if (sipmethod == SIP_CANCEL) {
12481 if (option_debug)
12482 ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
12483
12484
12485 } else if (sipmethod == SIP_NOTIFY) {
12486
12487 if (p->owner) {
12488 if (p->refer) {
12489 if (option_debug)
12490 ast_log(LOG_DEBUG, "Got 200 OK on NOTIFY for transfer\n");
12491 } else
12492 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
12493
12494 } else {
12495 if (!p->subscribed && !p->refer)
12496 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12497 }
12498 } else if (sipmethod == SIP_BYE)
12499 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12500 else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO)
12501
12502
12503 ;
12504 else if (sipmethod == SIP_BYE)
12505
12506 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12507 break;
12508 case 202:
12509 if (sipmethod == SIP_REFER)
12510 handle_response_refer(p, resp, rest, req, seqno);
12511 break;
12512 case 401:
12513 case 407:
12514 if (sipmethod == SIP_REFER)
12515 handle_response_refer(p, resp, rest, req, seqno);
12516 else if (sipmethod == SIP_INVITE)
12517 handle_response_invite(p, resp, rest, req, seqno);
12518 else if (sipmethod == SIP_BYE) {
12519 char *auth, *auth2;
12520
12521 auth = (resp == 407 ? "Proxy-Authenticate" : "WWW-Authenticate");
12522 auth2 = (resp == 407 ? "Proxy-Authorization" : "Authorization");
12523 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, auth, auth2, sipmethod, 0)) {
12524 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
12525 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12526 }
12527 }
12528 break;
12529 case 481:
12530 if (sipmethod == SIP_INVITE) {
12531
12532 handle_response_invite(p, resp, rest, req, seqno);
12533 } else if (sipmethod == SIP_BYE) {
12534 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
12535 } else if (sipdebug) {
12536 ast_log (LOG_DEBUG, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
12537 }
12538 break;
12539 case 501:
12540 if (sipmethod == SIP_INVITE)
12541 handle_response_invite(p, resp, rest, req, seqno);
12542 else if (sipmethod == SIP_REFER)
12543 handle_response_refer(p, resp, rest, req, seqno);
12544 break;
12545 case 603:
12546 if (sipmethod == SIP_REFER) {
12547 handle_response_refer(p, resp, rest, req, seqno);
12548 break;
12549 }
12550
12551 default:
12552 if ((resp >= 100) && (resp < 200)) {
12553 if (sipmethod == SIP_INVITE) {
12554 if (!ast_test_flag(req, SIP_PKT_IGNORE))
12555 sip_cancel_destroy(p);
12556 }
12557 }
12558 if ((resp >= 300) && (resp < 700)) {
12559 if ((option_verbose > 2) && (resp != 487))
12560 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));
12561 switch(resp) {
12562 case 488:
12563 case 603:
12564 case 500:
12565 case 503:
12566 case 504:
12567
12568 if (sipmethod == SIP_INVITE) {
12569 sip_cancel_destroy(p);
12570 }
12571 break;
12572 }
12573 }
12574 break;
12575 }
12576 }
12577 }
12578
12579
12580
12581
12582
12583
12584
12585 static void *sip_park_thread(void *stuff)
12586 {
12587 struct ast_channel *transferee, *transferer;
12588 struct sip_dual *d;
12589 struct sip_request req;
12590 int ext;
12591 int res;
12592
12593 d = stuff;
12594 transferee = d->chan1;
12595 transferer = d->chan2;
12596 copy_request(&req, &d->req);
12597 free(d);
12598
12599 if (!transferee || !transferer) {
12600 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
12601 return NULL;
12602 }
12603 if (option_debug > 3)
12604 ast_log(LOG_DEBUG, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
12605
12606 ast_channel_lock(transferee);
12607 if (ast_do_masquerade(transferee)) {
12608 ast_log(LOG_WARNING, "Masquerade failed.\n");
12609 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
12610 ast_channel_unlock(transferee);
12611 return NULL;
12612 }
12613 ast_channel_unlock(transferee);
12614
12615 res = ast_park_call(transferee, transferer, 0, &ext);
12616
12617
12618 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
12619 if (!res) {
12620 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
12621 } else {
12622
12623 sprintf(buf, "Call parked on extension '%d'", ext);
12624 transmit_message_with_text(transferer->tech_pvt, buf);
12625 }
12626 #endif
12627
12628
12629
12630 transmit_response(transferer->tech_pvt, "202 Accepted", &req);
12631 if (!res) {
12632
12633 append_history(transferer->tech_pvt, "SIPpark","Parked call on %d", ext);
12634 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
12635 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
12636 ast_hangup(transferer);
12637 if (option_debug)
12638 ast_log(LOG_DEBUG, "SIP Call parked on extension '%d'\n", ext);
12639 } else {
12640 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
12641 append_history(transferer->tech_pvt, "SIPpark","Parking failed\n");
12642 if (option_debug)
12643 ast_log(LOG_DEBUG, "SIP Call parked failed \n");
12644
12645 }
12646 return NULL;
12647 }
12648
12649
12650
12651
12652 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno)
12653 {
12654 struct sip_dual *d;
12655 struct ast_channel *transferee, *transferer;
12656
12657 pthread_t th;
12658
12659 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
12660 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "SIPPeer/%s", chan2->name);
12661 if ((!transferer) || (!transferee)) {
12662 if (transferee) {
12663 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
12664 ast_hangup(transferee);
12665 }
12666 if (transferer) {
12667 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
12668 ast_hangup(transferer);
12669 }
12670 return -1;
12671 }
12672
12673
12674 transferee->readformat = chan1->readformat;
12675 transferee->writeformat = chan1->writeformat;
12676
12677
12678 ast_channel_masquerade(transferee, chan1);
12679
12680
12681 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
12682 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
12683 transferee->priority = chan1->priority;
12684
12685
12686
12687
12688
12689 transferer->readformat = chan2->readformat;
12690 transferer->writeformat = chan2->writeformat;
12691
12692
12693 ast_channel_masquerade(transferer, chan2);
12694
12695
12696 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
12697 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
12698 transferer->priority = chan2->priority;
12699
12700 ast_channel_lock(transferer);
12701 if (ast_do_masquerade(transferer)) {
12702 ast_log(LOG_WARNING, "Masquerade failed :(\n");
12703 ast_channel_unlock(transferer);
12704 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
12705 ast_hangup(transferer);
12706 return -1;
12707 }
12708 ast_channel_unlock(transferer);
12709 if (!transferer || !transferee) {
12710 if (!transferer) {
12711 if (option_debug)
12712 ast_log(LOG_DEBUG, "No transferer channel, giving up parking\n");
12713 }
12714 if (!transferee) {
12715 if (option_debug)
12716 ast_log(LOG_DEBUG, "No transferee channel, giving up parking\n");
12717 }
12718 return -1;
12719 }
12720 if ((d = ast_calloc(1, sizeof(*d)))) {
12721 pthread_attr_t attr;
12722
12723 pthread_attr_init(&attr);
12724 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
12725
12726
12727 copy_request(&d->req, req);
12728 d->chan1 = transferee;
12729 d->chan2 = transferer;
12730 d->seqno = seqno;
12731 if (ast_pthread_create_background(&th, &attr, sip_park_thread, d) < 0) {
12732
12733 free(d);
12734
12735 pthread_attr_destroy(&attr);
12736 return 0;
12737 }
12738 pthread_attr_destroy(&attr);
12739 }
12740 return -1;
12741 }
12742
12743
12744
12745
12746 static void ast_quiet_chan(struct ast_channel *chan)
12747 {
12748 if (chan && chan->_state == AST_STATE_UP) {
12749 if (chan->generatordata)
12750 ast_deactivate_generator(chan);
12751 }
12752 }
12753
12754
12755
12756 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
12757 {
12758 int res = 0;
12759 struct ast_channel *peera = NULL,
12760 *peerb = NULL,
12761 *peerc = NULL,
12762 *peerd = NULL;
12763
12764
12765
12766
12767 if (option_debug > 3) {
12768 ast_log(LOG_DEBUG, "Sip transfer:--------------------\n");
12769 if (transferer->chan1)
12770 ast_log(LOG_DEBUG, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
12771 else
12772 ast_log(LOG_DEBUG, "-- No transferer first channel - odd??? \n");
12773 if (target->chan1)
12774 ast_log(LOG_DEBUG, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
12775 else
12776 ast_log(LOG_DEBUG, "-- No target first channel ---\n");
12777 if (transferer->chan2)
12778 ast_log(LOG_DEBUG, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
12779 else
12780 ast_log(LOG_DEBUG, "-- No bridged call to transferee\n");
12781 if (target->chan2)
12782 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)");
12783 else
12784 ast_log(LOG_DEBUG, "-- No target second channel ---\n");
12785 ast_log(LOG_DEBUG, "-- END Sip transfer:--------------------\n");
12786 }
12787 if (transferer->chan2) {
12788 peera = transferer->chan1;
12789 peerb = target->chan1;
12790 peerc = transferer->chan2;
12791 peerd = target->chan2;
12792 if (option_debug > 2)
12793 ast_log(LOG_DEBUG, "SIP transfer: Four channels to handle\n");
12794 } else if (target->chan2) {
12795 peera = target->chan1;
12796 peerb = transferer->chan1;
12797 peerc = target->chan2;
12798 peerd = transferer->chan2;
12799 if (option_debug > 2)
12800 ast_log(LOG_DEBUG, "SIP transfer: Three channels to handle\n");
12801 }
12802
12803 if (peera && peerb && peerc && (peerb != peerc)) {
12804 ast_quiet_chan(peera);
12805 ast_quiet_chan(peerb);
12806 ast_quiet_chan(peerc);
12807 if (peerd)
12808 ast_quiet_chan(peerd);
12809
12810
12811 if (peera->cdr && peerb->cdr)
12812 peerb->cdr = ast_cdr_append(peerb->cdr, peera->cdr);
12813 else if (peera->cdr)
12814 peerb->cdr = peera->cdr;
12815 peera->cdr = NULL;
12816
12817 if (peerb->cdr && peerc->cdr)
12818 peerb->cdr = ast_cdr_append(peerb->cdr, peerc->cdr);
12819 else if (peerc->cdr)
12820 peerb->cdr = peerc->cdr;
12821 peerc->cdr = NULL;
12822
12823 if (option_debug > 3)
12824 ast_log(LOG_DEBUG, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
12825 if (ast_channel_masquerade(peerb, peerc)) {
12826 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
12827 res = -1;
12828 } else
12829 ast_log(LOG_DEBUG, "SIP transfer: Succeeded to masquerade channels.\n");
12830 return res;
12831 } else {
12832 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
12833 if (transferer->chan1)
12834 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
12835 if (target->chan1)
12836 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
12837 return -1;
12838 }
12839 return 0;
12840 }
12841
12842
12843
12844
12845
12846
12847 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
12848 {
12849 const char *thetag;
12850
12851 if (!tagbuf)
12852 return NULL;
12853 tagbuf[0] = '\0';
12854 thetag = get_header(req, header);
12855 thetag = strcasestr(thetag, ";tag=");
12856 if (thetag) {
12857 thetag += 5;
12858 ast_copy_string(tagbuf, thetag, tagbufsize);
12859 return strsep(&tagbuf, ";");
12860 }
12861 return NULL;
12862 }
12863
12864
12865 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
12866 {
12867
12868
12869 int res = 0;
12870 const char *event = get_header(req, "Event");
12871 char *eventid = NULL;
12872 char *sep;
12873
12874 if( (sep = strchr(event, ';')) ) {
12875 *sep++ = '\0';
12876 eventid = sep;
12877 }
12878
12879 if (option_debug > 1 && sipdebug)
12880 ast_log(LOG_DEBUG, "Got NOTIFY Event: %s\n", event);
12881
12882 if (strcmp(event, "refer")) {
12883
12884
12885 transmit_response(p, "489 Bad event", req);
12886 res = -1;
12887 } else {
12888
12889
12890
12891
12892
12893 char buf[1024];
12894 char *cmd, *code;
12895 int respcode;
12896 int success = TRUE;
12897
12898
12899
12900
12901
12902
12903
12904
12905 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
12906
12907 transmit_response(p, "400 Bad request", req);
12908 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12909 return -1;
12910 }
12911
12912
12913 if (get_msg_text(buf, sizeof(buf), req)) {
12914 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
12915 transmit_response(p, "400 Bad request", req);
12916 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12917 return -1;
12918 }
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940 if (option_debug > 2)
12941 ast_log(LOG_DEBUG, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
12942 cmd = ast_skip_blanks(buf);
12943 code = cmd;
12944
12945 while(*code && (*code > 32)) {
12946 code++;
12947 }
12948 *code++ = '\0';
12949 code = ast_skip_blanks(code);
12950 sep = code;
12951 sep++;
12952 while(*sep && (*sep > 32)) {
12953 sep++;
12954 }
12955 *sep++ = '\0';
12956 respcode = atoi(code);
12957 switch (respcode) {
12958 case 100:
12959 case 101:
12960
12961 break;
12962 case 183:
12963
12964 break;
12965 case 200:
12966
12967 break;
12968 case 301:
12969 case 302:
12970
12971 success = FALSE;
12972 break;
12973 case 503:
12974
12975 success = FALSE;
12976 break;
12977 case 603:
12978
12979 success = FALSE;
12980 break;
12981 }
12982 if (!success) {
12983 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
12984 }
12985
12986
12987 transmit_response(p, "200 OK", req);
12988 };
12989
12990 if (!p->lastinvite)
12991 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
12992
12993 return res;
12994 }
12995
12996
12997 static int handle_request_options(struct sip_pvt *p, struct sip_request *req)
12998 {
12999 int res;
13000
13001 res = get_destination(p, req);
13002 build_contact(p);
13003
13004 if (ast_strlen_zero(p->context))
13005 ast_string_field_set(p, context, default_context);
13006 if (res < 0)
13007 transmit_response_with_allow(p, "404 Not Found", req, 0);
13008 else
13009 transmit_response_with_allow(p, "200 OK", req, 0);
13010
13011
13012 if (!p->lastinvite)
13013 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13014
13015 return res;
13016 }
13017
13018
13019
13020 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin)
13021 {
13022 struct ast_frame *f;
13023 int earlyreplace = 0;
13024 int oneleggedreplace = 0;
13025 struct ast_channel *c = p->owner;
13026 struct ast_channel *replacecall = p->refer->refer_call->owner;
13027 struct ast_channel *targetcall;
13028
13029
13030 if (replacecall->_state == AST_STATE_RING)
13031 earlyreplace = 1;
13032
13033
13034 if (!(targetcall = ast_bridged_channel(replacecall))) {
13035
13036 if (!earlyreplace) {
13037 if (option_debug > 1)
13038 ast_log(LOG_DEBUG, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
13039 oneleggedreplace = 1;
13040 }
13041 }
13042 if (option_debug > 3 && targetcall && targetcall->_state == AST_STATE_RINGING)
13043 ast_log(LOG_DEBUG, "SIP transfer: Target channel is in ringing state\n");
13044
13045 if (option_debug > 3) {
13046 if (targetcall)
13047 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);
13048 else
13049 ast_log(LOG_DEBUG, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
13050 }
13051
13052 if (ignore) {
13053 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
13054
13055
13056
13057 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
13058
13059 ast_channel_unlock(c);
13060 ast_mutex_unlock(&p->refer->refer_call->lock);
13061 return 1;
13062 }
13063 if (!c) {
13064
13065 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
13066 transmit_response_reliable(p, "503 Service Unavailable", req);
13067 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
13068 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13069 ast_mutex_unlock(&p->refer->refer_call->lock);
13070 return 1;
13071 }
13072 append_history(p, "Xfer", "INVITE/Replace received");
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084 transmit_response(p, "100 Trying", req);
13085 ast_setstate(c, AST_STATE_RING);
13086
13087
13088
13089
13090
13091 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
13092 ast_setstate(c, AST_STATE_UP);
13093
13094
13095 ast_quiet_chan(replacecall);
13096 ast_quiet_chan(targetcall);
13097 if (option_debug > 3)
13098 ast_log(LOG_DEBUG, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
13099
13100 ast_channel_unlock(c);
13101
13102
13103 ast_mutex_unlock(&p->refer->refer_call->lock);
13104
13105
13106 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
13107
13108
13109 if(ast_channel_masquerade(replacecall, c))
13110 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
13111 else if (option_debug > 3)
13112 ast_log(LOG_DEBUG, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
13113
13114
13115
13116
13117
13118 ast_channel_unlock(c);
13119
13120 if (earlyreplace || oneleggedreplace ) {
13121
13122 if ((f = ast_read(replacecall))) {
13123 ast_frfree(f);
13124 f = NULL;
13125 if (option_debug > 3)
13126 ast_log(LOG_DEBUG, "Invite/Replace: Could successfully read frame from RING channel!\n");
13127 } else {
13128 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from RING channel \n");
13129 }
13130 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
13131 ast_channel_unlock(replacecall);
13132 } else {
13133 if ((f = ast_read(replacecall))) {
13134
13135 ast_frfree(f);
13136 f = NULL;
13137 if (option_debug > 2)
13138 ast_log(LOG_DEBUG, "Invite/Replace: Could successfully read frame from channel! Masq done.\n");
13139 } else {
13140 ast_log(LOG_WARNING, "Invite/Replace: Could not read frame from channel. Transfer failed\n");
13141 }
13142 ast_channel_unlock(replacecall);
13143 }
13144 ast_mutex_unlock(&p->refer->refer_call->lock);
13145
13146 ast_setstate(c, AST_STATE_DOWN);
13147 if (option_debug > 3) {
13148 struct ast_channel *test;
13149 ast_log(LOG_DEBUG, "After transfer:----------------------------\n");
13150 ast_log(LOG_DEBUG, " -- C: %s State %s\n", c->name, ast_state2str(c->_state));
13151 if (replacecall)
13152 ast_log(LOG_DEBUG, " -- replacecall: %s State %s\n", replacecall->name, ast_state2str(replacecall->_state));
13153 if (p->owner) {
13154 ast_log(LOG_DEBUG, " -- P->owner: %s State %s\n", p->owner->name, ast_state2str(p->owner->_state));
13155 test = ast_bridged_channel(p->owner);
13156 if (test)
13157 ast_log(LOG_DEBUG, " -- Call bridged to P->owner: %s State %s\n", test->name, ast_state2str(test->_state));
13158 else
13159 ast_log(LOG_DEBUG, " -- No call bridged to C->owner \n");
13160 } else
13161 ast_log(LOG_DEBUG, " -- No channel yet \n");
13162 ast_log(LOG_DEBUG, "End After transfer:----------------------------\n");
13163 }
13164
13165 ast_channel_unlock(p->owner);
13166 ast_mutex_unlock(&p->lock);
13167
13168
13169 c->tech_pvt = NULL;
13170 ast_hangup(c);
13171 return 0;
13172 }
13173
13174
13175
13176
13177
13178
13179
13180
13181 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)
13182 {
13183 int res = 1;
13184 int gotdest;
13185 const char *p_replaces;
13186 char *replace_id = NULL;
13187 const char *required;
13188 unsigned int required_profile = 0;
13189 struct ast_channel *c = NULL;
13190
13191
13192 if (!p->sipoptions) {
13193 const char *supported = get_header(req, "Supported");
13194 if (!ast_strlen_zero(supported))
13195 parse_sip_options(p, supported);
13196 }
13197
13198
13199 required = get_header(req, "Require");
13200 if (!ast_strlen_zero(required)) {
13201 required_profile = parse_sip_options(NULL, required);
13202 if (required_profile && required_profile != SIP_OPT_REPLACES) {
13203
13204 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
13205 ast_log(LOG_WARNING,"Received SIP INVITE with unsupported required extension: %s\n", required);
13206 p->invitestate = INV_COMPLETED;
13207 if (!p->lastinvite)
13208 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13209 return -1;
13210 }
13211 }
13212
13213
13214 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->owner->_state != AST_STATE_UP)) {
13215
13216
13217
13218
13219
13220 transmit_response(p, "482 Loop Detected", req);
13221 p->invitestate = INV_COMPLETED;
13222 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13223 return 0;
13224 }
13225
13226 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->pendinginvite) {
13227
13228 transmit_response(p, "491 Request Pending", req);
13229 if (option_debug)
13230 ast_log(LOG_DEBUG, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
13231
13232 return 0;
13233 }
13234
13235 p_replaces = get_header(req, "Replaces");
13236 if (!ast_strlen_zero(p_replaces)) {
13237
13238 char *ptr;
13239 char *fromtag = NULL;
13240 char *totag = NULL;
13241 char *start, *to;
13242 int error = 0;
13243
13244 if (p->owner) {
13245 if (option_debug > 2)
13246 ast_log(LOG_DEBUG, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
13247 transmit_response(p, "400 Bad request", req);
13248
13249 return -1;
13250 }
13251
13252 if (sipdebug && option_debug > 2)
13253 ast_log(LOG_DEBUG, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
13254
13255 replace_id = ast_strdupa(p_replaces);
13256 ast_uri_decode(replace_id);
13257
13258 if (!p->refer && !sip_refer_allocate(p)) {
13259 transmit_response(p, "500 Server Internal Error", req);
13260 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
13261 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13262 p->invitestate = INV_COMPLETED;
13263 return -1;
13264 }
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275 replace_id = ast_skip_blanks(replace_id);
13276
13277 start = replace_id;
13278 while ( (ptr = strsep(&start, ";")) ) {
13279 ptr = ast_skip_blanks(ptr);
13280 if ( (to = strcasestr(ptr, "to-tag=") ) )
13281 totag = to + 7;
13282 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
13283 fromtag = to + 9;
13284 fromtag = strsep(&fromtag, "&");
13285 }
13286 }
13287
13288 if (sipdebug && option_debug > 3)
13289 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>");
13290
13291
13292
13293
13294
13295 if ((p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
13296 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
13297 transmit_response(p, "481 Call Leg Does Not Exist (Replaces)", req);
13298 error = 1;
13299 }
13300
13301
13302
13303
13304
13305
13306
13307 if (p->refer->refer_call == p) {
13308 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
13309 p->refer->refer_call = NULL;
13310 transmit_response(p, "400 Bad request", req);
13311 error = 1;
13312 }
13313
13314 if (!error && !p->refer->refer_call->owner) {
13315
13316 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
13317
13318 transmit_response(p, "481 Call Leg Does Not Exist (Replace)", req);
13319 error = 1;
13320 }
13321
13322 if (!error && p->refer->refer_call->owner->_state != AST_STATE_RING && p->refer->refer_call->owner->_state != AST_STATE_UP ) {
13323 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
13324 transmit_response(p, "603 Declined (Replaces)", req);
13325 error = 1;
13326 }
13327
13328 if (error) {
13329 append_history(p, "Xfer", "INVITE/Replace Failed.");
13330 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13331 ast_mutex_unlock(&p->lock);
13332 if (p->refer->refer_call) {
13333 ast_mutex_unlock(&p->refer->refer_call->lock);
13334 ast_channel_unlock(p->refer->refer_call->owner);
13335 }
13336 p->invitestate = INV_COMPLETED;
13337 return -1;
13338 }
13339 }
13340
13341
13342
13343
13344
13345 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13346 int newcall = (p->initreq.headers ? TRUE : FALSE);
13347
13348 sip_cancel_destroy(p);
13349
13350 p->pendinginvite = seqno;
13351 check_via(p, req);
13352
13353 copy_request(&p->initreq, req);
13354 if (!p->owner) {
13355 if (debug)
13356 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
13357 if (newcall)
13358 append_history(p, "Invite", "New call: %s", p->callid);
13359 parse_ok_contact(p, req);
13360 } else {
13361 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
13362
13363 if (find_sdp(req)) {
13364 if (process_sdp(p, req)) {
13365 transmit_response(p, "488 Not acceptable here", req);
13366 if (!p->lastinvite)
13367 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13368 return -1;
13369 }
13370 } else {
13371 p->jointcapability = p->capability;
13372 if (option_debug)
13373 ast_log(LOG_DEBUG, "Hm.... No sdp for the moment\n");
13374 }
13375 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
13376 append_history(p, "ReInv", "Re-invite received");
13377 }
13378 } else if (debug)
13379 ast_verbose("Ignoring this INVITE request\n");
13380
13381
13382 if (!p->lastinvite && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner) {
13383
13384
13385 res = check_user(p, req, SIP_INVITE, e, XMIT_RELIABLE, sin);
13386 if (res == AUTH_CHALLENGE_SENT) {
13387 p->invitestate = INV_COMPLETED;
13388 return 0;
13389 }
13390 if (res < 0) {
13391 if (res == AUTH_FAKE_AUTH) {
13392 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
13393 transmit_fake_auth_response(p, req, 1);
13394 } else {
13395 ast_log(LOG_NOTICE, "Failed to authenticate user %s\n", get_header(req, "From"));
13396 transmit_response_reliable(p, "403 Forbidden", req);
13397 }
13398 p->invitestate = INV_COMPLETED;
13399 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13400 ast_string_field_free(p, theirtag);
13401 return 0;
13402 }
13403
13404
13405 if (find_sdp(req)) {
13406 if (process_sdp(p, req)) {
13407
13408 transmit_response_reliable(p, "488 Not acceptable here", req);
13409 p->invitestate = INV_COMPLETED;
13410 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13411 if (option_debug)
13412 ast_log(LOG_DEBUG, "No compatible codecs for this SIP call.\n");
13413 return -1;
13414 }
13415 } else {
13416 p->jointcapability = p->capability;
13417 if (option_debug > 1)
13418 ast_log(LOG_DEBUG, "No SDP in Invite, third party call control\n");
13419 }
13420
13421
13422
13423 if (p->owner)
13424 ast_queue_frame(p->owner, &ast_null_frame);
13425
13426
13427
13428 if (ast_strlen_zero(p->context))
13429 ast_string_field_set(p, context, default_context);
13430
13431
13432
13433 if (option_debug)
13434 ast_log(LOG_DEBUG, "Checking SIP call limits for device %s\n", p->username);
13435 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
13436 if (res < 0) {
13437 ast_log(LOG_NOTICE, "Failed to place call for user %s, too many calls\n", p->username);
13438 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
13439 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13440 p->invitestate = INV_COMPLETED;
13441 }
13442 return 0;
13443 }
13444 gotdest = get_destination(p, NULL);
13445 get_rdnis(p, NULL);
13446 extract_uri(p, req);
13447 build_contact(p);
13448
13449 if (p->rtp) {
13450 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
13451 ast_rtp_setdtmfcompensate(p->rtp, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
13452 }
13453
13454 if (!replace_id && gotdest) {
13455 if (gotdest == 1 && ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP))
13456 transmit_response_reliable(p, "484 Address Incomplete", req);
13457 else
13458 transmit_response_reliable(p, "404 Not Found", req);
13459 p->invitestate = INV_COMPLETED;
13460 update_call_counter(p, DEC_CALL_LIMIT);
13461 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13462 return 0;
13463 } else {
13464
13465
13466 if (ast_strlen_zero(p->exten))
13467 ast_string_field_set(p, exten, "s");
13468
13469
13470 make_our_tag(p->tag, sizeof(p->tag));
13471
13472 c = sip_new(p, AST_STATE_DOWN, S_OR(p->username, NULL));
13473 *recount = 1;
13474
13475
13476 build_route(p, req, 0);
13477
13478 if (c) {
13479
13480 ast_channel_lock(c);
13481 }
13482 }
13483 } else {
13484 if (option_debug > 1 && sipdebug) {
13485 if (!ast_test_flag(req, SIP_PKT_IGNORE))
13486 ast_log(LOG_DEBUG, "Got a SIP re-invite for call %s\n", p->callid);
13487 else
13488 ast_log(LOG_DEBUG, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
13489 }
13490 c = p->owner;
13491 }
13492
13493 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
13494 p->lastinvite = seqno;
13495
13496 if (replace_id) {
13497
13498 if (sipdebug && option_debug > 3)
13499 ast_log(LOG_DEBUG, "Sending this call to the invite/replcaes handler %s\n", p->callid);
13500 return handle_invite_replaces(p, req, debug, ast_test_flag(req, SIP_PKT_IGNORE), seqno, sin);
13501 }
13502
13503
13504 if (c) {
13505 switch(c->_state) {
13506 case AST_STATE_DOWN:
13507 if (option_debug > 1)
13508 ast_log(LOG_DEBUG, "%s: New call is still down.... Trying... \n", c->name);
13509 transmit_response(p, "100 Trying", req);
13510 p->invitestate = INV_PROCEEDING;
13511 ast_setstate(c, AST_STATE_RING);
13512 if (strcmp(p->exten, ast_pickup_ext())) {
13513 enum ast_pbx_result res;
13514
13515 res = ast_pbx_start(c);
13516
13517 switch(res) {
13518 case AST_PBX_FAILED:
13519 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
13520 p->invitestate = INV_COMPLETED;
13521 if (ast_test_flag(req, SIP_PKT_IGNORE))
13522 transmit_response(p, "503 Unavailable", req);
13523 else
13524 transmit_response_reliable(p, "503 Unavailable", req);
13525 break;
13526 case AST_PBX_CALL_LIMIT:
13527 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
13528 p->invitestate = INV_COMPLETED;
13529 if (ast_test_flag(req, SIP_PKT_IGNORE))
13530 transmit_response(p, "480 Temporarily Unavailable", req);
13531 else
13532 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
13533 break;
13534 case AST_PBX_SUCCESS:
13535
13536 break;
13537 }
13538
13539 if (res) {
13540
13541
13542 ast_mutex_unlock(&c->lock);
13543 ast_mutex_unlock(&p->lock);
13544 ast_hangup(c);
13545 ast_mutex_lock(&p->lock);
13546 c = NULL;
13547 }
13548 } else {
13549 ast_channel_unlock(c);
13550 if (ast_pickup_call(c)) {
13551 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
13552 if (ast_test_flag(req, SIP_PKT_IGNORE))
13553 transmit_response(p, "503 Unavailable", req);
13554 else
13555 transmit_response_reliable(p, "503 Unavailable", req);
13556 sip_alreadygone(p);
13557
13558 ast_mutex_unlock(&p->lock);
13559 c->hangupcause = AST_CAUSE_CALL_REJECTED;
13560 } else {
13561 ast_mutex_unlock(&p->lock);
13562 ast_setstate(c, AST_STATE_DOWN);
13563 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
13564 }
13565 p->invitestate = INV_COMPLETED;
13566 ast_hangup(c);
13567 ast_mutex_lock(&p->lock);
13568 c = NULL;
13569 }
13570 break;
13571 case AST_STATE_RING:
13572 transmit_response(p, "100 Trying", req);
13573 p->invitestate = INV_PROCEEDING;
13574 break;
13575 case AST_STATE_RINGING:
13576 transmit_response(p, "180 Ringing", req);
13577 p->invitestate = INV_PROCEEDING;
13578 break;
13579 case AST_STATE_UP:
13580 if (option_debug > 1)
13581 ast_log(LOG_DEBUG, "%s: This call is UP.... \n", c->name);
13582
13583 if (p->t38.state == T38_PEER_REINVITE) {
13584 struct ast_channel *bridgepeer = NULL;
13585 struct sip_pvt *bridgepvt = NULL;
13586
13587 if ((bridgepeer = ast_bridged_channel(p->owner))) {
13588
13589
13590 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
13591 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
13592 if (bridgepvt->t38.state == T38_DISABLED) {
13593 if (bridgepvt->udptl) {
13594
13595 sip_handle_t38_reinvite(bridgepeer, p, 1);
13596 } else {
13597 ast_log(LOG_WARNING, "Strange... The other side of the bridge don't have udptl struct\n");
13598 ast_mutex_lock(&bridgepvt->lock);
13599 bridgepvt->t38.state = T38_DISABLED;
13600 ast_mutex_unlock(&bridgepvt->lock);
13601 if (option_debug > 1)
13602 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", bridgepvt->t38.state, bridgepeer->name);
13603 if (ast_test_flag(req, SIP_PKT_IGNORE))
13604 transmit_response(p, "488 Not acceptable here", req);
13605 else
13606 transmit_response_reliable(p, "488 Not acceptable here", req);
13607
13608 }
13609 } else {
13610
13611 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
13612 p->t38.state = T38_ENABLED;
13613 if (option_debug)
13614 ast_log(LOG_DEBUG, "T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13615 }
13616 } else {
13617
13618 if (ast_test_flag(req, SIP_PKT_IGNORE))
13619 transmit_response(p, "488 Not acceptable here", req);
13620 else
13621 transmit_response_reliable(p, "488 Not acceptable here", req);
13622 p->t38.state = T38_DISABLED;
13623 if (option_debug > 1)
13624 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13625
13626 if (!p->lastinvite)
13627 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13628 }
13629 } else {
13630
13631 transmit_response_with_t38_sdp(p, "200 OK", req, XMIT_CRITICAL);
13632 p->t38.state = T38_ENABLED;
13633 if (option_debug)
13634 ast_log(LOG_DEBUG,"T38 state changed to %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
13635 }
13636 } else if (p->t38.state == T38_DISABLED) {
13637 int sendok = TRUE;
13638
13639
13640
13641 struct ast_channel *bridgepeer = NULL;
13642 struct sip_pvt *bridgepvt = NULL;
13643 if ((bridgepeer = ast_bridged_channel(p->owner))) {
13644 if (bridgepeer->tech == &sip_tech || bridgepeer->tech == &sip_tech_info) {
13645 bridgepvt = (struct sip_pvt*)bridgepeer->tech_pvt;
13646
13647 if (bridgepvt->t38.state == T38_ENABLED) {
13648 ast_log(LOG_WARNING, "RTP re-invite after T38 session not handled yet !\n");
13649
13650 if (ast_test_flag(req, SIP_PKT_IGNORE))
13651 transmit_response(p, "488 Not Acceptable Here (unsupported)", req);
13652 else
13653 transmit_response_reliable(p, "488 Not Acceptable Here (unsupported)", req);
13654 sendok = FALSE;
13655 }
13656
13657 }
13658 }
13659
13660 if (sendok)
13661 transmit_response_with_sdp(p, "200 OK", req, XMIT_CRITICAL);
13662
13663 }
13664 p->invitestate = INV_TERMINATED;
13665 break;
13666 default:
13667 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
13668 transmit_response(p, "100 Trying", req);
13669 break;
13670 }
13671 } else {
13672 if (p && (p->autokillid == -1)) {
13673 const char *msg;
13674
13675 if (!p->jointcapability)
13676 msg = "488 Not Acceptable Here (codec error)";
13677 else {
13678 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
13679 msg = "503 Unavailable";
13680 }
13681 if (ast_test_flag(req, SIP_PKT_IGNORE))
13682 transmit_response(p, msg, req);
13683 else
13684 transmit_response_reliable(p, msg, req);
13685 p->invitestate = INV_COMPLETED;
13686 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13687 }
13688 }
13689 return res;
13690 }
13691
13692
13693
13694 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno)
13695 {
13696 struct sip_dual target;
13697
13698 int res = 0;
13699 struct sip_pvt *targetcall_pvt;
13700
13701
13702 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
13703 transferer->refer->replaces_callid_fromtag))) {
13704 if (transferer->refer->localtransfer) {
13705
13706 transmit_response(transferer, "202 Accepted", req);
13707
13708
13709 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
13710 append_history(transferer, "Xfer", "Refer failed");
13711 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
13712 transferer->refer->status = REFER_FAILED;
13713 return -1;
13714 }
13715
13716 if (option_debug > 2)
13717 ast_log(LOG_DEBUG, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
13718 return 0;
13719 }
13720
13721
13722 transmit_response(transferer, "202 Accepted", req);
13723 append_history(transferer, "Xfer", "Refer accepted");
13724 if (!targetcall_pvt->owner) {
13725 if (option_debug > 3)
13726 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No owner of target call\n");
13727
13728 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
13729 append_history(transferer, "Xfer", "Refer failed");
13730 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
13731 transferer->refer->status = REFER_FAILED;
13732 ast_mutex_unlock(&targetcall_pvt->lock);
13733 ast_channel_unlock(current->chan1);
13734 ast_channel_unlock(targetcall_pvt->owner);
13735 return -1;
13736 }
13737
13738
13739 target.chan1 = targetcall_pvt->owner;
13740 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
13741
13742 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
13743
13744 if (option_debug > 3) {
13745 if (target.chan2)
13746 ast_log(LOG_DEBUG, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
13747 else if (target.chan1->_state != AST_STATE_RING)
13748 ast_log(LOG_DEBUG, "SIP attended transfer: Error: No target channel\n");
13749 else
13750 ast_log(LOG_DEBUG, "SIP attended transfer: Attempting transfer in ringing state\n");
13751 }
13752 }
13753
13754
13755 if (option_debug > 3 && sipdebug) {
13756 if (current->chan2)
13757 ast_log(LOG_DEBUG, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
13758 else
13759 ast_log(LOG_DEBUG, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
13760 }
13761
13762 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
13763
13764
13765 res = attempt_transfer(current, &target);
13766 ast_mutex_unlock(&targetcall_pvt->lock);
13767 if (res) {
13768
13769
13770 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy", TRUE);
13771 append_history(transferer, "Xfer", "Refer failed");
13772 if (targetcall_pvt->owner)
13773 ast_channel_unlock(targetcall_pvt->owner);
13774
13775 ast_hangup(transferer->owner);
13776 } else {
13777
13778
13779
13780 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
13781 append_history(transferer, "Xfer", "Refer succeeded");
13782 transferer->refer->status = REFER_200OK;
13783 if (targetcall_pvt->owner) {
13784 if (option_debug)
13785 ast_log(LOG_DEBUG, "SIP attended transfer: Unlocking channel %s\n", targetcall_pvt->owner->name);
13786 ast_channel_unlock(targetcall_pvt->owner);
13787 }
13788 }
13789 return 1;
13790 }
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock)
13857 {
13858 struct sip_dual current;
13859
13860
13861 int res = 0;
13862
13863 if (ast_test_flag(req, SIP_PKT_DEBUG))
13864 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");
13865
13866 if (!p->owner) {
13867
13868
13869 if (option_debug > 2)
13870 ast_log(LOG_DEBUG, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
13871 transmit_response(p, "603 Declined (No dialog)", req);
13872 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
13873 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
13874 sip_alreadygone(p);
13875 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
13876 }
13877 return 0;
13878 }
13879
13880
13881
13882 if (p->allowtransfer == TRANSFER_CLOSED ) {
13883
13884 transmit_response(p, "603 Declined (policy)", req);
13885 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
13886
13887 return 0;
13888 }
13889
13890 if(!ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
13891
13892 transmit_response(p, "491 Request pending", req);
13893 append_history(p, "Xfer", "Refer failed. Request pending.");
13894 return 0;
13895 }
13896
13897
13898 if (!p->refer && !sip_refer_allocate(p)) {
13899 transmit_response(p, "500 Internal Server Error", req);
13900 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
13901 return -3;
13902 }
13903
13904 res = get_refer_info(p, req);
13905
13906 p->refer->status = REFER_SENT;
13907
13908 if (res != 0) {
13909 switch (res) {
13910 case -2:
13911 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
13912 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
13913 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
13914 ast_log(LOG_DEBUG, "SIP transfer to black hole can't be handled (no refer-to: )\n");
13915 break;
13916 case -3:
13917 transmit_response(p, "603 Declined (Non sip: uri)", req);
13918 append_history(p, "Xfer", "Refer failed. Non SIP uri");
13919 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
13920 ast_log(LOG_DEBUG, "SIP transfer to non-SIP uri denied\n");
13921 break;
13922 default:
13923
13924 transmit_response(p, "202 Accepted", req);
13925 append_history(p, "Xfer", "Refer failed. Bad extension.");
13926 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
13927 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
13928 if (ast_test_flag(req, SIP_PKT_DEBUG) && option_debug)
13929 ast_log(LOG_DEBUG, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
13930 break;
13931 }
13932 return 0;
13933 }
13934 if (ast_strlen_zero(p->context))
13935 ast_string_field_set(p, context, default_context);
13936
13937
13938 if (allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
13939 p->refer->localtransfer = 1;
13940 if (sipdebug && option_debug > 2)
13941 ast_log(LOG_DEBUG, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
13942 } else if (AST_LIST_EMPTY(&domain_list)) {
13943
13944 p->refer->localtransfer = 1;
13945 } else
13946 if (sipdebug && option_debug > 2)
13947 ast_log(LOG_DEBUG, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
13948
13949
13950
13951 if (ignore)
13952 return res;
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980 current.chan1 = p->owner;
13981
13982
13983 current.chan2 = ast_bridged_channel(current.chan1);
13984
13985 if (sipdebug && option_debug > 2)
13986 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>");
13987
13988 if (!current.chan2 && !p->refer->attendedtransfer) {
13989
13990
13991
13992 if (sipdebug && option_debug > 2)
13993 ast_log(LOG_DEBUG,"Refused SIP transfer on non-bridged channel.\n");
13994 p->refer->status = REFER_FAILED;
13995 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
13996 transmit_response(p, "603 Declined", req);
13997 return -1;
13998 }
13999
14000 if (current.chan2) {
14001 if (sipdebug && option_debug > 3)
14002 ast_log(LOG_DEBUG, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
14003
14004 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
14005 }
14006
14007 ast_set_flag(&p->flags[0], SIP_GOTREFER);
14008
14009
14010 if (p->refer->attendedtransfer) {
14011 if ((res = local_attended_transfer(p, ¤t, req, seqno)))
14012 return res;
14013
14014 if (sipdebug && option_debug > 3)
14015 ast_log(LOG_DEBUG, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
14016
14017 }
14018
14019
14020
14021 if (p->refer->localtransfer && !strcmp(p->refer->refer_to, ast_parking_ext())) {
14022
14023 *nounlock = 1;
14024 ast_channel_unlock(current.chan1);
14025 copy_request(¤t.req, req);
14026 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14027 p->refer->status = REFER_200OK;
14028 append_history(p, "Xfer", "REFER to call parking.");
14029 if (sipdebug && option_debug > 3)
14030 ast_log(LOG_DEBUG, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
14031 sip_park(current.chan2, current.chan1, req, seqno);
14032 return res;
14033 }
14034
14035
14036 transmit_response(p, "202 Accepted", req);
14037
14038 if (current.chan1 && current.chan2) {
14039 if (option_debug > 2)
14040 ast_log(LOG_DEBUG, "chan1->name: %s\n", current.chan1->name);
14041 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
14042 }
14043 if (current.chan2) {
14044 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
14045 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
14046 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
14047
14048 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
14049
14050 if (p->refer->referred_by)
14051 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
14052 }
14053
14054 if (p->refer->replaces_callid && !ast_strlen_zero(p->refer->replaces_callid)) {
14055 char tempheader[BUFSIZ];
14056 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
14057 p->refer->replaces_callid_totag ? ";to-tag=" : "",
14058 p->refer->replaces_callid_totag,
14059 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
14060 p->refer->replaces_callid_fromtag);
14061 if (current.chan2)
14062 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
14063 }
14064
14065
14066 *nounlock = 1;
14067 ast_channel_unlock(current.chan1);
14068 ast_channel_unlock(current.chan2);
14069
14070
14071
14072
14073 if (!p->refer->attendedtransfer)
14074 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
14075
14076
14077
14078
14079
14080 if (!current.chan2) {
14081
14082
14083
14084
14085
14086
14087
14088 p->refer->status = REFER_FAILED;
14089 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
14090 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14091 append_history(p, "Xfer", "Refer failed (only bridged calls).");
14092 return -1;
14093 }
14094 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14095
14096
14097
14098 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
14099
14100 if (!res) {
14101
14102 if (option_debug > 2)
14103 ast_log(LOG_DEBUG, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
14104 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
14105 if (p->refer->localtransfer)
14106 p->refer->status = REFER_200OK;
14107 if (p->owner)
14108 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
14109 append_history(p, "Xfer", "Refer succeeded.");
14110 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14111
14112
14113 res = 0;
14114 } else {
14115 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
14116 if (option_debug > 2)
14117 ast_log(LOG_DEBUG, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
14118 append_history(p, "Xfer", "Refer failed.");
14119
14120 p->refer->status = REFER_FAILED;
14121 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
14122 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
14123 res = -1;
14124 }
14125 return res;
14126 }
14127
14128
14129 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
14130 {
14131
14132 check_via(p, req);
14133 sip_alreadygone(p);
14134 p->invitestate = INV_CANCELLED;
14135
14136 if (p->owner && p->owner->_state == AST_STATE_UP) {
14137
14138 transmit_response(p, "200 OK", req);
14139 if (option_debug)
14140 ast_log(LOG_DEBUG, "Got CANCEL on an answered call. Ignoring... \n");
14141 return 0;
14142 }
14143 stop_media_flows(p);
14144
14145 if (p->owner)
14146 ast_queue_hangup(p->owner);
14147 else
14148 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14149 if (p->initreq.len > 0) {
14150 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
14151 transmit_response(p, "200 OK", req);
14152 return 1;
14153 } else {
14154 transmit_response(p, "481 Call Leg Does Not Exist", req);
14155 return 0;
14156 }
14157 }
14158
14159 static int acf_channel_read(struct ast_channel *chan, char *funcname, char *preparse, char *buf, size_t buflen)
14160 {
14161 struct ast_rtp_quality qos;
14162 struct sip_pvt *p = chan->tech_pvt;
14163 char *all = "", *parse = ast_strdupa(preparse);
14164 AST_DECLARE_APP_ARGS(args,
14165 AST_APP_ARG(param);
14166 AST_APP_ARG(type);
14167 AST_APP_ARG(field);
14168 );
14169 AST_STANDARD_APP_ARGS(args, parse);
14170
14171
14172 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
14173 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
14174 return 0;
14175 }
14176
14177 if (strcasecmp(args.param, "rtpqos"))
14178 return 0;
14179
14180 memset(buf, 0, buflen);
14181 memset(&qos, 0, sizeof(qos));
14182
14183 if (strcasecmp(args.type, "AUDIO") == 0) {
14184 all = ast_rtp_get_quality(p->rtp, &qos);
14185 } else if (strcasecmp(args.type, "VIDEO") == 0) {
14186 all = ast_rtp_get_quality(p->vrtp, &qos);
14187 }
14188
14189 if (strcasecmp(args.field, "local_ssrc") == 0)
14190 snprintf(buf, buflen, "%u", qos.local_ssrc);
14191 else if (strcasecmp(args.field, "local_lostpackets") == 0)
14192 snprintf(buf, buflen, "%u", qos.local_lostpackets);
14193 else if (strcasecmp(args.field, "local_jitter") == 0)
14194 snprintf(buf, buflen, "%.0lf", qos.local_jitter * 1000.0);
14195 else if (strcasecmp(args.field, "local_count") == 0)
14196 snprintf(buf, buflen, "%u", qos.local_count);
14197 else if (strcasecmp(args.field, "remote_ssrc") == 0)
14198 snprintf(buf, buflen, "%u", qos.remote_ssrc);
14199 else if (strcasecmp(args.field, "remote_lostpackets") == 0)
14200 snprintf(buf, buflen, "%u", qos.remote_lostpackets);
14201 else if (strcasecmp(args.field, "remote_jitter") == 0)
14202 snprintf(buf, buflen, "%.0lf", qos.remote_jitter * 1000.0);
14203 else if (strcasecmp(args.field, "remote_count") == 0)
14204 snprintf(buf, buflen, "%u", qos.remote_count);
14205 else if (strcasecmp(args.field, "rtt") == 0)
14206 snprintf(buf, buflen, "%.0lf", qos.rtt * 1000.0);
14207 else if (strcasecmp(args.field, "all") == 0)
14208 ast_copy_string(buf, all, buflen);
14209 else {
14210 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
14211 return -1;
14212 }
14213 return 0;
14214 }
14215
14216
14217 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
14218 {
14219 struct ast_channel *c=NULL;
14220 int res;
14221 struct ast_channel *bridged_to;
14222
14223
14224 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner)
14225 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
14226
14227 p->invitestate = INV_TERMINATED;
14228
14229 copy_request(&p->initreq, req);
14230 check_via(p, req);
14231 sip_alreadygone(p);
14232
14233
14234 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY) || p->owner) {
14235 char *audioqos, *videoqos;
14236 if (p->rtp) {
14237 audioqos = ast_rtp_get_quality(p->rtp, NULL);
14238 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
14239 append_history(p, "RTCPaudio", "Quality:%s", audioqos);
14240 if (p->owner)
14241 pbx_builtin_setvar_helper(p->owner, "RTPAUDIOQOS", audioqos);
14242 }
14243 if (p->vrtp) {
14244 videoqos = ast_rtp_get_quality(p->vrtp, NULL);
14245 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
14246 append_history(p, "RTCPvideo", "Quality:%s", videoqos);
14247 if (p->owner)
14248 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", videoqos);
14249 }
14250 }
14251
14252 stop_media_flows(p);
14253
14254 if (!ast_strlen_zero(get_header(req, "Also"))) {
14255 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
14256 ast_inet_ntoa(p->recv.sin_addr));
14257 if (ast_strlen_zero(p->context))
14258 ast_string_field_set(p, context, default_context);
14259 res = get_also_info(p, req);
14260 if (!res) {
14261 c = p->owner;
14262 if (c) {
14263 bridged_to = ast_bridged_channel(c);
14264 if (bridged_to) {
14265
14266 ast_queue_control(c, AST_CONTROL_UNHOLD);
14267 ast_async_goto(bridged_to, p->context, p->refer->refer_to,1);
14268 } else
14269 ast_queue_hangup(p->owner);
14270 }
14271 } else {
14272 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(p->recv.sin_addr));
14273 if (p->owner)
14274 ast_queue_hangup(p->owner);
14275 }
14276 } else if (p->owner) {
14277 ast_queue_hangup(p->owner);
14278 if (option_debug > 2)
14279 ast_log(LOG_DEBUG, "Received bye, issuing owner hangup\n");
14280 } else {
14281 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14282 if (option_debug > 2)
14283 ast_log(LOG_DEBUG, "Received bye, no owner, selfdestruct soon.\n");
14284 }
14285 transmit_response(p, "200 OK", req);
14286
14287 return 1;
14288 }
14289
14290
14291 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
14292 {
14293 if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
14294 if (ast_test_flag(req, SIP_PKT_DEBUG))
14295 ast_verbose("Receiving message!\n");
14296 receive_message(p, req);
14297 } else
14298 transmit_response(p, "202 Accepted", req);
14299 return 1;
14300 }
14301
14302
14303 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int seqno, char *e)
14304 {
14305 int gotdest;
14306 int res = 0;
14307 int firststate = AST_EXTENSION_REMOVED;
14308 struct sip_peer *authpeer = NULL;
14309 const char *eventheader = get_header(req, "Event");
14310 const char *accept = get_header(req, "Accept");
14311 int resubscribe = (p->subscribed != NONE);
14312 char *temp, *event;
14313
14314 if (p->initreq.headers) {
14315
14316 if (p->initreq.method != SIP_SUBSCRIBE) {
14317
14318
14319 transmit_response(p, "403 Forbidden (within dialog)", req);
14320
14321 if (option_debug)
14322 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);
14323 return 0;
14324 } else if (ast_test_flag(req, SIP_PKT_DEBUG)) {
14325 if (option_debug) {
14326 if (resubscribe)
14327 ast_log(LOG_DEBUG, "Got a re-subscribe on existing subscription %s\n", p->callid);
14328 else
14329 ast_log(LOG_DEBUG, "Got a new subscription %s (possibly with auth)\n", p->callid);
14330 }
14331 }
14332 }
14333
14334
14335
14336
14337 if (!global_allowsubscribe) {
14338 transmit_response(p, "403 Forbidden (policy)", req);
14339 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14340 return 0;
14341 }
14342
14343 if (!ast_test_flag(req, SIP_PKT_IGNORE) && !resubscribe) {
14344
14345 if (ast_test_flag(req, SIP_PKT_DEBUG))
14346 ast_verbose("Creating new subscription\n");
14347
14348 copy_request(&p->initreq, req);
14349 check_via(p, req);
14350 } else if (ast_test_flag(req, SIP_PKT_DEBUG) && ast_test_flag(req, SIP_PKT_IGNORE))
14351 ast_verbose("Ignoring this SUBSCRIBE request\n");
14352
14353
14354 if (ast_strlen_zero(eventheader)) {
14355 transmit_response(p, "489 Bad Event", req);
14356 if (option_debug > 1)
14357 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: <none>\n");
14358 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14359 return 0;
14360 }
14361
14362 if ( (strchr(eventheader, ';'))) {
14363 event = ast_strdupa(eventheader);
14364 temp = strchr(event, ';');
14365 *temp = '\0';
14366
14367 } else
14368 event = (char *) eventheader;
14369
14370
14371 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, &authpeer);
14372
14373 if (res == AUTH_CHALLENGE_SENT) {
14374 if (authpeer)
14375 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14376 return 0;
14377 }
14378 if (res < 0) {
14379 if (res == AUTH_FAKE_AUTH) {
14380 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
14381 transmit_fake_auth_response(p, req, 1);
14382 } else {
14383 ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
14384 transmit_response_reliable(p, "403 Forbidden", req);
14385 }
14386 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14387 if (authpeer)
14388 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14389 return 0;
14390 }
14391
14392
14393 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
14394 transmit_response(p, "403 Forbidden (policy)", req);
14395 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14396 if (authpeer)
14397 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14398 return 0;
14399 }
14400
14401
14402 gotdest = get_destination(p, NULL);
14403
14404
14405
14406
14407
14408
14409 if (!ast_strlen_zero(p->subscribecontext))
14410 ast_string_field_set(p, context, p->subscribecontext);
14411 else if (ast_strlen_zero(p->context))
14412 ast_string_field_set(p, context, default_context);
14413
14414
14415 parse_ok_contact(p, req);
14416
14417 build_contact(p);
14418 if (gotdest) {
14419 transmit_response(p, "404 Not Found", req);
14420 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14421 if (authpeer)
14422 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14423 return 0;
14424 }
14425
14426
14427 if (ast_strlen_zero(p->tag))
14428 make_our_tag(p->tag, sizeof(p->tag));
14429
14430 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
14431 if (authpeer)
14432 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14433
14434
14435
14436
14437
14438 if (strstr(p->useragent, "Polycom")) {
14439 p->subscribed = XPIDF_XML;
14440 } else if (strstr(accept, "application/pidf+xml")) {
14441 p->subscribed = PIDF_XML;
14442 } else if (strstr(accept, "application/dialog-info+xml")) {
14443 p->subscribed = DIALOG_INFO_XML;
14444
14445 } else if (strstr(accept, "application/cpim-pidf+xml")) {
14446 p->subscribed = CPIM_PIDF_XML;
14447 } else if (strstr(accept, "application/xpidf+xml")) {
14448 p->subscribed = XPIDF_XML;
14449 } else if (ast_strlen_zero(accept)) {
14450 if (p->subscribed == NONE) {
14451 transmit_response(p, "489 Bad Event", req);
14452
14453 ast_log(LOG_WARNING,"SUBSCRIBE failure: no Accept header: pvt: stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
14454 p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
14455 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14456 return 0;
14457 }
14458
14459
14460 } else {
14461
14462 char mybuf[200];
14463 snprintf(mybuf,sizeof(mybuf),"489 Bad Event (format %s)", accept);
14464 transmit_response(p, mybuf, req);
14465
14466 ast_log(LOG_WARNING,"SUBSCRIBE failure: unrecognized format: '%s' pvt: subscribed: %d, stateid: %d, laststate: %d, dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
14467 accept, (int)p->subscribed, p->stateid, p->laststate, p->dialogver, p->subscribecontext, p->subscribeuri);
14468 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14469 return 0;
14470 }
14471 } else if (!strcmp(event, "message-summary")) {
14472 if (!ast_strlen_zero(accept) && strcmp(accept, "application/simple-message-summary")) {
14473
14474 transmit_response(p, "406 Not Acceptable", req);
14475 if (option_debug > 1)
14476 ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", accept);
14477 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14478 if (authpeer)
14479 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14480 return 0;
14481 }
14482
14483
14484
14485
14486
14487 if (!authpeer || ast_strlen_zero(authpeer->mailbox)) {
14488 transmit_response(p, "404 Not found (no mailbox)", req);
14489 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14490 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
14491 if (authpeer)
14492 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14493 return 0;
14494 }
14495
14496 p->subscribed = MWI_NOTIFICATION;
14497 if (authpeer->mwipvt && authpeer->mwipvt != p)
14498
14499 sip_destroy(authpeer->mwipvt);
14500 authpeer->mwipvt = p;
14501 p->relatedpeer = authpeer;
14502 } else {
14503 transmit_response(p, "489 Bad Event", req);
14504 if (option_debug > 1)
14505 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
14506 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14507 if (authpeer)
14508 ASTOBJ_UNREF(authpeer, sip_destroy_peer);
14509 return 0;
14510 }
14511
14512 if (p->subscribed != MWI_NOTIFICATION && !resubscribe)
14513 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
14514
14515 if (!ast_test_flag(req, SIP_PKT_IGNORE) && p)
14516 p->lastinvite = seqno;
14517 if (p && !ast_test_flag(&p->flags[0], SIP_NEEDDESTROY)) {
14518 p->expiry = atoi(get_header(req, "Expires"));
14519
14520
14521 if (p->expiry > max_expiry)
14522 p->expiry = max_expiry;
14523 if (p->expiry < min_expiry && p->expiry > 0)
14524 p->expiry = min_expiry;
14525
14526 if (sipdebug || option_debug > 1) {
14527 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
14528 ast_log(LOG_DEBUG, "Adding subscription for mailbox notification - peer %s Mailbox %s\n", p->relatedpeer->name, p->relatedpeer->mailbox);
14529 else
14530 ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
14531 }
14532 if (p->autokillid > -1)
14533 sip_cancel_destroy(p);
14534 if (p->expiry > 0)
14535 sip_scheddestroy(p, (p->expiry + 10) * 1000);
14536
14537 if (p->subscribed == MWI_NOTIFICATION) {
14538 transmit_response(p, "200 OK", req);
14539 if (p->relatedpeer) {
14540 ASTOBJ_WRLOCK(p->relatedpeer);
14541 sip_send_mwi_to_peer(p->relatedpeer);
14542 ASTOBJ_UNLOCK(p->relatedpeer);
14543 }
14544 } else {
14545 struct sip_pvt *p_old;
14546
14547 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
14548
14549 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));
14550 transmit_response(p, "404 Not found", req);
14551 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14552 return 0;
14553 }
14554
14555 transmit_response(p, "200 OK", req);
14556 transmit_state_notify(p, firststate, 1, FALSE);
14557 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
14558
14559 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
14560
14561
14562
14563
14564
14565
14566 ast_mutex_lock(&iflock);
14567 for (p_old = iflist; p_old; p_old = p_old->next) {
14568 if (p_old == p)
14569 continue;
14570 if (p_old->initreq.method != SIP_SUBSCRIBE)
14571 continue;
14572 if (p_old->subscribed == NONE)
14573 continue;
14574 ast_mutex_lock(&p_old->lock);
14575 if (!strcmp(p_old->username, p->username)) {
14576 if (!strcmp(p_old->exten, p->exten) &&
14577 !strcmp(p_old->context, p->context)) {
14578 ast_set_flag(&p_old->flags[0], SIP_NEEDDESTROY);
14579 ast_mutex_unlock(&p_old->lock);
14580 break;
14581 }
14582 }
14583 ast_mutex_unlock(&p_old->lock);
14584 }
14585 ast_mutex_unlock(&iflock);
14586 }
14587 if (!p->expiry)
14588 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14589 }
14590 return 1;
14591 }
14592
14593
14594 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, char *e)
14595 {
14596 enum check_auth_result res;
14597
14598
14599 if (ast_test_flag(req, SIP_PKT_DEBUG))
14600 ast_verbose("Using latest REGISTER request as basis request\n");
14601 copy_request(&p->initreq, req);
14602 check_via(p, req);
14603 if ((res = register_verify(p, sin, req, e)) < 0) {
14604 const char *reason = "";
14605
14606 switch (res) {
14607 case AUTH_SECRET_FAILED:
14608 reason = "Wrong password";
14609 break;
14610 case AUTH_USERNAME_MISMATCH:
14611 reason = "Username/auth name mismatch";
14612 break;
14613 case AUTH_NOT_FOUND:
14614 reason = "No matching peer found";
14615 break;
14616 case AUTH_UNKNOWN_DOMAIN:
14617 reason = "Not a local domain";
14618 break;
14619 default:
14620 break;
14621 }
14622 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
14623 get_header(req, "To"), ast_inet_ntoa(sin->sin_addr),
14624 reason);
14625 }
14626 if (res < 1) {
14627
14628
14629 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14630 }
14631 append_history(p, "RegRequest", "%s : Account %s", res ? "Failed": "Succeeded", get_header(req, "To"));
14632 return res;
14633 }
14634
14635
14636
14637
14638 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
14639 {
14640
14641
14642 const char *cmd;
14643 const char *cseq;
14644 const char *useragent;
14645 int seqno;
14646 int len;
14647 int ignore = FALSE;
14648 int respid;
14649 int res = 0;
14650 int debug = sip_debug_test_pvt(p);
14651 char *e;
14652 int error = 0;
14653
14654
14655 cseq = get_header(req, "Cseq");
14656 cmd = req->header[0];
14657
14658
14659 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq)) {
14660 ast_log(LOG_ERROR, "Missing Cseq. Dropping this SIP message, it's incomplete.\n");
14661 error = 1;
14662 }
14663 if (!error && sscanf(cseq, "%d%n", &seqno, &len) != 1) {
14664 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
14665 error = 1;
14666 }
14667 if (error) {
14668 if (!p->initreq.headers)
14669 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14670 return -1;
14671 }
14672
14673
14674 cmd = req->rlPart1;
14675 e = req->rlPart2;
14676
14677
14678 useragent = get_header(req, "User-Agent");
14679 if (!ast_strlen_zero(useragent))
14680 ast_string_field_set(p, useragent, useragent);
14681
14682
14683 if (req->method == SIP_RESPONSE) {
14684
14685 if (!p->initreq.headers) {
14686 if (option_debug)
14687 ast_log(LOG_DEBUG, "That's odd... Got a response on a call we dont know about. Cseq %d Cmd %s\n", seqno, cmd);
14688 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14689 return 0;
14690 } else if (p->ocseq && (p->ocseq < seqno)) {
14691 if (option_debug)
14692 ast_log(LOG_DEBUG, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
14693 return -1;
14694 } else if (p->ocseq && (p->ocseq != seqno)) {
14695
14696
14697 ignore = TRUE;
14698 ast_set_flag(req, SIP_PKT_IGNORE);
14699 ast_set_flag(req, SIP_PKT_IGNORE_RESP);
14700 append_history(p, "Ignore", "Ignoring this retransmit\n");
14701 } else if (e) {
14702 e = ast_skip_blanks(e);
14703 if (sscanf(e, "%d %n", &respid, &len) != 1) {
14704 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
14705 } else {
14706 if (respid <= 0) {
14707 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
14708 return 0;
14709 }
14710
14711 if ((respid == 200) || ((respid >= 300) && (respid <= 399)))
14712 extract_uri(p, req);
14713 handle_response(p, respid, e + len, req, ignore, seqno);
14714 }
14715 }
14716 return 0;
14717 }
14718
14719
14720
14721
14722
14723 p->method = req->method;
14724 if (option_debug > 3)
14725 ast_log(LOG_DEBUG, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
14726
14727 if (p->icseq && (p->icseq > seqno)) {
14728 if (option_debug)
14729 ast_log(LOG_DEBUG, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
14730 if (req->method != SIP_ACK)
14731 transmit_response(p, "503 Server error", req);
14732 return -1;
14733 } else if (p->icseq &&
14734 p->icseq == seqno &&
14735 req->method != SIP_ACK &&
14736 (p->method != SIP_CANCEL || ast_test_flag(&p->flags[0], SIP_ALREADYGONE))) {
14737
14738
14739
14740 ignore = 2;
14741 ast_set_flag(req, SIP_PKT_IGNORE);
14742 ast_set_flag(req, SIP_PKT_IGNORE_REQ);
14743 if (option_debug > 2)
14744 ast_log(LOG_DEBUG, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
14745 }
14746
14747 if (seqno >= p->icseq)
14748
14749
14750
14751 p->icseq = seqno;
14752
14753
14754 if (ast_strlen_zero(p->theirtag)) {
14755 char tag[128];
14756
14757 gettag(req, "From", tag, sizeof(tag));
14758 ast_string_field_set(p, theirtag, tag);
14759 }
14760 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
14761
14762 if (pedanticsipchecking) {
14763
14764
14765
14766
14767 if (!p->initreq.headers && ast_test_flag(req, SIP_PKT_WITH_TOTAG)) {
14768
14769 if (!ast_test_flag(req, SIP_PKT_IGNORE) && req->method == SIP_INVITE) {
14770 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
14771
14772 } else if (req->method != SIP_ACK) {
14773 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
14774 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14775 }
14776 return res;
14777 }
14778 }
14779
14780 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY)) {
14781 transmit_response(p, "400 Bad request", req);
14782 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14783 return -1;
14784 }
14785
14786
14787 switch (p->method) {
14788 case SIP_OPTIONS:
14789 res = handle_request_options(p, req);
14790 break;
14791 case SIP_INVITE:
14792 res = handle_request_invite(p, req, debug, seqno, sin, recount, e);
14793 break;
14794 case SIP_REFER:
14795 res = handle_request_refer(p, req, debug, ignore, seqno, nounlock);
14796 break;
14797 case SIP_CANCEL:
14798 res = handle_request_cancel(p, req);
14799 break;
14800 case SIP_BYE:
14801 res = handle_request_bye(p, req);
14802 break;
14803 case SIP_MESSAGE:
14804 res = handle_request_message(p, req);
14805 break;
14806 case SIP_SUBSCRIBE:
14807 res = handle_request_subscribe(p, req, sin, seqno, e);
14808 break;
14809 case SIP_REGISTER:
14810 res = handle_request_register(p, req, sin, e);
14811 break;
14812 case SIP_INFO:
14813 if (ast_test_flag(req, SIP_PKT_DEBUG))
14814 ast_verbose("Receiving INFO!\n");
14815 if (!ignore)
14816 handle_request_info(p, req);
14817 else
14818 transmit_response(p, "200 OK", req);
14819 break;
14820 case SIP_NOTIFY:
14821 res = handle_request_notify(p, req, sin, seqno, e);
14822 break;
14823 case SIP_ACK:
14824
14825 if (seqno == p->pendinginvite) {
14826 p->invitestate = INV_TERMINATED;
14827 p->pendinginvite = 0;
14828 __sip_ack(p, seqno, FLAG_RESPONSE, 0);
14829 if (find_sdp(req)) {
14830 if (process_sdp(p, req))
14831 return -1;
14832 }
14833 check_pendings(p);
14834 }
14835
14836 if (!p->lastinvite && ast_strlen_zero(p->randdata))
14837 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14838 break;
14839 default:
14840 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
14841 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
14842 cmd, ast_inet_ntoa(p->sa.sin_addr));
14843
14844 if (!p->initreq.headers)
14845 ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);
14846 break;
14847 }
14848 return res;
14849 }
14850
14851
14852
14853
14854
14855
14856 static int sipsock_read(int *id, int fd, short events, void *ignore)
14857 {
14858 struct sip_request req;
14859 struct sockaddr_in sin = { 0, };
14860 struct sip_pvt *p;
14861 int res;
14862 socklen_t len = sizeof(sin);
14863 int nounlock;
14864 int recount = 0;
14865 int lockretry;
14866
14867 memset(&req, 0, sizeof(req));
14868 res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
14869 if (res < 0) {
14870 #if !defined(__FreeBSD__)
14871 if (errno == EAGAIN)
14872 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
14873 else
14874 #endif
14875 if (errno != ECONNREFUSED)
14876 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
14877 return 1;
14878 }
14879 if (option_debug && res == sizeof(req.data)) {
14880 ast_log(LOG_DEBUG, "Received packet exceeds buffer. Data is possibly lost\n");
14881 req.data[sizeof(req.data) - 1] = '\0';
14882 } else
14883 req.data[res] = '\0';
14884 req.len = res;
14885 if(sip_debug_test_addr(&sin))
14886 ast_set_flag(&req, SIP_PKT_DEBUG);
14887 if (pedanticsipchecking)
14888 req.len = lws2sws(req.data, req.len);
14889 if (ast_test_flag(&req, SIP_PKT_DEBUG))
14890 ast_verbose("\n<--- SIP read from %s:%d --->\n%s\n<------------->\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), req.data);
14891
14892 parse_request(&req);
14893 req.method = find_sip_method(req.rlPart1);
14894
14895 if (ast_test_flag(&req, SIP_PKT_DEBUG))
14896 ast_verbose("--- (%d headers %d lines)%s ---\n", req.headers, req.lines, (req.headers + req.lines == 0) ? " Nat keepalive" : "");
14897
14898 if (req.headers < 2)
14899 return 1;
14900
14901
14902 for (lockretry = 100; lockretry > 0; lockretry--) {
14903 ast_mutex_lock(&netlock);
14904
14905
14906 p = find_call(&req, &sin, req.method);
14907 if (p == NULL) {
14908 if (option_debug)
14909 ast_log(LOG_DEBUG, "Invalid SIP message - rejected , no callid, len %d\n", req.len);
14910 ast_mutex_unlock(&netlock);
14911 return 1;
14912 }
14913
14914
14915 if (!p->owner || !ast_channel_trylock(p->owner))
14916 break;
14917 if (option_debug)
14918 ast_log(LOG_DEBUG, "Failed to grab owner channel lock, trying again. (SIP call %s)\n", p->callid);
14919 ast_mutex_unlock(&p->lock);
14920 ast_mutex_unlock(&netlock);
14921
14922 usleep(1);
14923 }
14924 p->recv = sin;
14925
14926 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
14927 append_history(p, "Rx", "%s / %s / %s", req.data, get_header(&req, "CSeq"), req.rlPart2);
14928
14929 if (!lockretry) {
14930 if (p->owner)
14931 ast_log(LOG_ERROR, "We could NOT get the channel lock for %s! \n", S_OR(p->owner->name, "- no channel name ??? - "));
14932 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
14933 if (req.method != SIP_ACK)
14934 transmit_response(p, "503 Server error", &req);
14935
14936 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
14937 return 1;
14938 }
14939 nounlock = 0;
14940 if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
14941
14942 if (option_debug)
14943 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
14944 }
14945
14946 if (p->owner && !nounlock)
14947 ast_channel_unlock(p->owner);
14948 ast_mutex_unlock(&p->lock);
14949 ast_mutex_unlock(&netlock);
14950 if (recount)
14951 ast_update_use_count();
14952
14953 return 1;
14954 }
14955
14956
14957 static int sip_send_mwi_to_peer(struct sip_peer *peer)
14958 {
14959
14960 struct sip_pvt *p;
14961 int newmsgs, oldmsgs;
14962
14963
14964 ast_app_inboxcount(peer->mailbox, &newmsgs, &oldmsgs);
14965
14966 peer->lastmsgcheck = time(NULL);
14967
14968
14969 if (((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)) == peer->lastmsgssent) {
14970 return 0;
14971 }
14972
14973
14974 peer->lastmsgssent = ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs));
14975
14976 if (peer->mwipvt) {
14977
14978 p = peer->mwipvt;
14979 } else {
14980
14981 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY)))
14982 return -1;
14983 if (create_addr_from_peer(p, peer)) {
14984
14985 sip_destroy(p);
14986 return 0;
14987 }
14988
14989 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
14990 p->ourip = __ourip;
14991 build_via(p);
14992 build_callid_pvt(p);
14993
14994 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14995 }
14996
14997 ast_set_flag(&p->flags[0], SIP_OUTGOING);
14998 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
14999 return 0;
15000 }
15001
15002
15003 static int does_peer_need_mwi(struct sip_peer *peer)
15004 {
15005 time_t t = time(NULL);
15006
15007 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
15008 !peer->mwipvt) {
15009 peer->lastmsgcheck = t;
15010 return FALSE;
15011 }
15012
15013 if (!ast_strlen_zero(peer->mailbox) && (t - peer->lastmsgcheck) > global_mwitime)
15014 return TRUE;
15015
15016 return FALSE;
15017 }
15018
15019
15020
15021
15022
15023
15024 static void *do_monitor(void *data)
15025 {
15026 int res;
15027 struct sip_pvt *sip;
15028 struct sip_peer *peer = NULL;
15029 time_t t;
15030 int fastrestart = FALSE;
15031 int lastpeernum = -1;
15032 int curpeernum;
15033 int reloading;
15034
15035
15036 if (sipsock > -1)
15037 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
15038
15039
15040 for(;;) {
15041
15042 ast_mutex_lock(&sip_reload_lock);
15043 reloading = sip_reloading;
15044 sip_reloading = FALSE;
15045 ast_mutex_unlock(&sip_reload_lock);
15046 if (reloading) {
15047 if (option_verbose > 0)
15048 ast_verbose(VERBOSE_PREFIX_1 "Reloading SIP\n");
15049 sip_do_reload(sip_reloadreason);
15050
15051
15052 if (sipsock > -1)
15053 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
15054 }
15055
15056 ast_mutex_lock(&iflock);
15057 restartsearch:
15058 t = time(NULL);
15059
15060
15061
15062
15063 for (sip = iflist; !fastrestart && sip; sip = sip->next) {
15064 ast_mutex_lock(&sip->lock);
15065
15066 if (sip->rtp && sip->owner &&
15067 (sip->owner->_state == AST_STATE_UP) &&
15068 !sip->redirip.sin_addr.s_addr) {
15069 if (sip->lastrtptx &&
15070 ast_rtp_get_rtpkeepalive(sip->rtp) &&
15071 (t > sip->lastrtptx + ast_rtp_get_rtpkeepalive(sip->rtp))) {
15072
15073 sip->lastrtptx = time(NULL);
15074 ast_rtp_sendcng(sip->rtp, 0);
15075 }
15076 if (sip->lastrtprx &&
15077 (ast_rtp_get_rtptimeout(sip->rtp) || ast_rtp_get_rtpholdtimeout(sip->rtp)) &&
15078 (t > sip->lastrtprx + ast_rtp_get_rtptimeout(sip->rtp))) {
15079
15080 struct sockaddr_in sin;
15081 ast_rtp_get_peer(sip->rtp, &sin);
15082 if (sin.sin_addr.s_addr ||
15083 (ast_rtp_get_rtpholdtimeout(sip->rtp) &&
15084 (t > sip->lastrtprx + ast_rtp_get_rtpholdtimeout(sip->rtp)))) {
15085
15086 if (ast_rtp_get_rtptimeout(sip->rtp)) {
15087 while (sip->owner && ast_channel_trylock(sip->owner)) {
15088 ast_mutex_unlock(&sip->lock);
15089 usleep(1);
15090 ast_mutex_lock(&sip->lock);
15091 }
15092 if (sip->owner) {
15093 if (!(ast_rtp_get_bridged(sip->rtp))) {
15094 ast_log(LOG_NOTICE,
15095 "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
15096 sip->owner->name,
15097 (long) (t - sip->lastrtprx));
15098
15099 ast_softhangup_nolock(sip->owner, AST_SOFTHANGUP_DEV);
15100 } else
15101 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));
15102 ast_channel_unlock(sip->owner);
15103
15104
15105
15106
15107 ast_rtp_set_rtptimeout(sip->rtp, 0);
15108 ast_rtp_set_rtpholdtimeout(sip->rtp, 0);
15109 if (sip->vrtp) {
15110 ast_rtp_set_rtptimeout(sip->vrtp, 0);
15111 ast_rtp_set_rtpholdtimeout(sip->vrtp, 0);
15112 }
15113 }
15114 }
15115 }
15116 }
15117 }
15118
15119 if (ast_test_flag(&sip->flags[0], SIP_NEEDDESTROY) && !sip->packets &&
15120 !sip->owner) {
15121 ast_mutex_unlock(&sip->lock);
15122 __sip_destroy(sip, 1);
15123 goto restartsearch;
15124 }
15125 ast_mutex_unlock(&sip->lock);
15126 }
15127 ast_mutex_unlock(&iflock);
15128
15129 pthread_testcancel();
15130
15131 res = ast_sched_wait(sched);
15132 if ((res < 0) || (res > 1000))
15133 res = 1000;
15134
15135 if (fastrestart)
15136 res = 1;
15137 res = ast_io_wait(io, res);
15138 if (option_debug && res > 20)
15139 ast_log(LOG_DEBUG, "chan_sip: ast_io_wait ran %d all at once\n", res);
15140 ast_mutex_lock(&monlock);
15141 if (res >= 0) {
15142 res = ast_sched_runq(sched);
15143 if (option_debug && res >= 20)
15144 ast_log(LOG_DEBUG, "chan_sip: ast_sched_runq ran %d all at once\n", res);
15145 }
15146
15147
15148 t = time(NULL);
15149 fastrestart = FALSE;
15150 curpeernum = 0;
15151 peer = NULL;
15152
15153 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !peer, do {
15154 if ((curpeernum > lastpeernum) && does_peer_need_mwi(iterator)) {
15155 fastrestart = TRUE;
15156 lastpeernum = curpeernum;
15157 peer = ASTOBJ_REF(iterator);
15158 };
15159 curpeernum++;
15160 } while (0)
15161 );
15162
15163 if (peer) {
15164 ASTOBJ_WRLOCK(peer);
15165 sip_send_mwi_to_peer(peer);
15166 ASTOBJ_UNLOCK(peer);
15167 ASTOBJ_UNREF(peer,sip_destroy_peer);
15168 } else {
15169
15170 lastpeernum = -1;
15171 }
15172 ast_mutex_unlock(&monlock);
15173 }
15174
15175 return NULL;
15176
15177 }
15178
15179
15180 static int restart_monitor(void)
15181 {
15182
15183 if (monitor_thread == AST_PTHREADT_STOP)
15184 return 0;
15185 ast_mutex_lock(&monlock);
15186 if (monitor_thread == pthread_self()) {
15187 ast_mutex_unlock(&monlock);
15188 ast_log(LOG_WARNING, "Cannot kill myself\n");
15189 return -1;
15190 }
15191 if (monitor_thread != AST_PTHREADT_NULL) {
15192
15193 pthread_kill(monitor_thread, SIGURG);
15194 } else {
15195
15196 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
15197 ast_mutex_unlock(&monlock);
15198 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
15199 return -1;
15200 }
15201 }
15202 ast_mutex_unlock(&monlock);
15203 return 0;
15204 }
15205
15206
15207 static int sip_poke_noanswer(void *data)
15208 {
15209 struct sip_peer *peer = data;
15210
15211 peer->pokeexpire = -1;
15212 if (peer->lastms > -1) {
15213 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
15214 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
15215 }
15216 if (peer->call)
15217 sip_destroy(peer->call);
15218 peer->call = NULL;
15219 peer->lastms = -1;
15220 ast_device_state_changed("SIP/%s", peer->name);
15221
15222 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
15223 return 0;
15224 }
15225
15226
15227
15228
15229 static int sip_poke_peer(struct sip_peer *peer)
15230 {
15231 struct sip_pvt *p;
15232
15233 if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
15234
15235
15236 if (peer->pokeexpire > -1)
15237 ast_sched_del(sched, peer->pokeexpire);
15238 peer->lastms = 0;
15239 peer->pokeexpire = -1;
15240 peer->call = NULL;
15241 return 0;
15242 }
15243 if (peer->call) {
15244 if (sipdebug)
15245 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
15246 sip_destroy(peer->call);
15247 }
15248 if (!(p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS)))
15249 return -1;
15250
15251 p->sa = peer->addr;
15252 p->recv = peer->addr;
15253 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
15254 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
15255
15256
15257 if (!ast_strlen_zero(peer->fullcontact))
15258 ast_string_field_set(p, fullcontact, peer->fullcontact);
15259
15260 if (!ast_strlen_zero(peer->tohost))
15261 ast_string_field_set(p, tohost, peer->tohost);
15262 else
15263 ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr));
15264
15265
15266 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
15267 p->ourip = __ourip;
15268 build_via(p);
15269 build_callid_pvt(p);
15270
15271 if (peer->pokeexpire > -1)
15272 ast_sched_del(sched, peer->pokeexpire);
15273 p->relatedpeer = peer;
15274 ast_set_flag(&p->flags[0], SIP_OUTGOING);
15275 #ifdef VOCAL_DATA_HACK
15276 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
15277 transmit_invite(p, SIP_INVITE, 0, 2);
15278 #else
15279 transmit_invite(p, SIP_OPTIONS, 0, 2);
15280 #endif
15281 gettimeofday(&peer->ps, NULL);
15282 peer->pokeexpire = ast_sched_add(sched, DEFAULT_MAXMS * 2, sip_poke_noanswer, peer);
15283
15284 return 0;
15285 }
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320 static int sip_devicestate(void *data)
15321 {
15322 char *host;
15323 char *tmp;
15324
15325 struct hostent *hp;
15326 struct ast_hostent ahp;
15327 struct sip_peer *p;
15328
15329 int res = AST_DEVICE_INVALID;
15330
15331
15332 host = ast_strdupa(data ? data : "");
15333 if ((tmp = strchr(host, '@')))
15334 host = tmp + 1;
15335
15336 if (option_debug > 2)
15337 ast_log(LOG_DEBUG, "Checking device state for peer %s\n", host);
15338
15339 if ((p = find_peer(host, NULL, 1))) {
15340 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352 if (p->onHold)
15353
15354 res = AST_DEVICE_ONHOLD;
15355 else if (p->inRinging) {
15356 if (p->inRinging == p->inUse)
15357 res = AST_DEVICE_RINGING;
15358 else
15359 res = AST_DEVICE_RINGINUSE;
15360 } else if (p->call_limit && (p->inUse == p->call_limit))
15361
15362 res = AST_DEVICE_BUSY;
15363 else if (p->call_limit && p->inUse)
15364
15365 res = AST_DEVICE_INUSE;
15366 else if (p->maxms && (p->lastms > p->maxms))
15367
15368 res = AST_DEVICE_UNAVAILABLE;
15369 else
15370 res = AST_DEVICE_NOT_INUSE;
15371 } else {
15372
15373 res = AST_DEVICE_UNAVAILABLE;
15374 }
15375 ASTOBJ_UNREF(p,sip_destroy_peer);
15376 } else {
15377 hp = ast_gethostbyname(host, &ahp);
15378 if (hp)
15379 res = AST_DEVICE_UNKNOWN;
15380 }
15381
15382 return res;
15383 }
15384
15385
15386
15387 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
15388 {
15389 int oldformat;
15390 struct sip_pvt *p;
15391 struct ast_channel *tmpc = NULL;
15392 char *ext, *host;
15393 char tmp[256];
15394 char *dest = data;
15395
15396 oldformat = format;
15397 if (!(format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1))) {
15398 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));
15399 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
15400 return NULL;
15401 }
15402 if (option_debug)
15403 ast_log(LOG_DEBUG, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
15404
15405 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE))) {
15406 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", (char *)data);
15407 *cause = AST_CAUSE_SWITCH_CONGESTION;
15408 return NULL;
15409 }
15410
15411 ast_set_flag(&p->flags[1], SIP_PAGE2_OUTGOING_CALL);
15412
15413 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
15414 sip_destroy(p);
15415 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
15416 *cause = AST_CAUSE_SWITCH_CONGESTION;
15417 return NULL;
15418 }
15419
15420 ast_copy_string(tmp, dest, sizeof(tmp));
15421 host = strchr(tmp, '@');
15422 if (host) {
15423 *host++ = '\0';
15424 ext = tmp;
15425 } else {
15426 ext = strchr(tmp, '/');
15427 if (ext)
15428 *ext++ = '\0';
15429 host = tmp;
15430 }
15431
15432 if (create_addr(p, host)) {
15433 *cause = AST_CAUSE_UNREGISTERED;
15434 if (option_debug > 2)
15435 ast_log(LOG_DEBUG, "Cant create SIP call - target device not registred\n");
15436 sip_destroy(p);
15437 return NULL;
15438 }
15439 if (ast_strlen_zero(p->peername) && ext)
15440 ast_string_field_set(p, peername, ext);
15441
15442 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
15443 p->ourip = __ourip;
15444 build_via(p);
15445 build_callid_pvt(p);
15446
15447
15448
15449
15450
15451 if (ext) {
15452 ast_string_field_set(p, username, ext);
15453 ast_string_field_free(p, fullcontact);
15454 }
15455 #if 0
15456 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
15457 #endif
15458 p->prefcodec = oldformat;
15459 ast_mutex_lock(&p->lock);
15460 tmpc = sip_new(p, AST_STATE_DOWN, host);
15461 ast_mutex_unlock(&p->lock);
15462 if (!tmpc)
15463 sip_destroy(p);
15464 ast_update_use_count();
15465 restart_monitor();
15466 return tmpc;
15467 }
15468
15469
15470
15471
15472
15473
15474
15475
15476 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
15477 {
15478 int res = 1;
15479 static int dep_insecure_very = 0;
15480 static int dep_insecure_yes = 0;
15481
15482 if (!strcasecmp(v->name, "trustrpid")) {
15483 ast_set_flag(&mask[0], SIP_TRUSTRPID);
15484 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
15485 } else if (!strcasecmp(v->name, "sendrpid")) {
15486 ast_set_flag(&mask[0], SIP_SENDRPID);
15487 ast_set2_flag(&flags[0], ast_true(v->value), SIP_SENDRPID);
15488 } else if (!strcasecmp(v->name, "g726nonstandard")) {
15489 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
15490 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
15491 } else if (!strcasecmp(v->name, "useclientcode")) {
15492 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
15493 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
15494 } else if (!strcasecmp(v->name, "dtmfmode")) {
15495 ast_set_flag(&mask[0], SIP_DTMF);
15496 ast_clear_flag(&flags[0], SIP_DTMF);
15497 if (!strcasecmp(v->value, "inband"))
15498 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
15499 else if (!strcasecmp(v->value, "rfc2833"))
15500 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
15501 else if (!strcasecmp(v->value, "info"))
15502 ast_set_flag(&flags[0], SIP_DTMF_INFO);
15503 else if (!strcasecmp(v->value, "auto"))
15504 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
15505 else {
15506 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
15507 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
15508 }
15509 } else if (!strcasecmp(v->name, "nat")) {
15510 ast_set_flag(&mask[0], SIP_NAT);
15511 ast_clear_flag(&flags[0], SIP_NAT);
15512 if (!strcasecmp(v->value, "never"))
15513 ast_set_flag(&flags[0], SIP_NAT_NEVER);
15514 else if (!strcasecmp(v->value, "route"))
15515 ast_set_flag(&flags[0], SIP_NAT_ROUTE);
15516 else if (ast_true(v->value))
15517 ast_set_flag(&flags[0], SIP_NAT_ALWAYS);
15518 else
15519 ast_set_flag(&flags[0], SIP_NAT_RFC3581);
15520 } else if (!strcasecmp(v->name, "canreinvite")) {
15521 ast_set_flag(&mask[0], SIP_REINVITE);
15522 ast_clear_flag(&flags[0], SIP_REINVITE);
15523 if (ast_true(v->value)) {
15524 ast_set_flag(&flags[0], SIP_CAN_REINVITE | SIP_CAN_REINVITE_NAT);
15525 } else if (!ast_false(v->value)) {
15526 char buf[64];
15527 char *word, *next = buf;
15528
15529 ast_copy_string(buf, v->value, sizeof(buf));
15530 while ((word = strsep(&next, ","))) {
15531 if (!strcasecmp(word, "update")) {
15532 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_CAN_REINVITE);
15533 } else if (!strcasecmp(word, "nonat")) {
15534 ast_set_flag(&flags[0], SIP_CAN_REINVITE);
15535 ast_clear_flag(&flags[0], SIP_CAN_REINVITE_NAT);
15536 } else {
15537 ast_log(LOG_WARNING, "Unknown canreinvite mode '%s' on line %d\n", v->value, v->lineno);
15538 }
15539 }
15540 }
15541 } else if (!strcasecmp(v->name, "insecure")) {
15542 ast_set_flag(&mask[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
15543 ast_clear_flag(&flags[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
15544 if (!strcasecmp(v->value, "very")) {
15545 ast_set_flag(&flags[0], SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
15546 if (!dep_insecure_very) {
15547 ast_log(LOG_WARNING, "insecure=very at line %d is deprecated; use insecure=port,invite instead\n", v->lineno);
15548 dep_insecure_very = 1;
15549 }
15550 }
15551 else if (ast_true(v->value)) {
15552 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
15553 if (!dep_insecure_yes) {
15554 ast_log(LOG_WARNING, "insecure=%s at line %d is deprecated; use insecure=port instead\n", v->value, v->lineno);
15555 dep_insecure_yes = 1;
15556 }
15557 }
15558 else if (!ast_false(v->value)) {
15559 char buf[64];
15560 char *word, *next;
15561
15562 ast_copy_string(buf, v->value, sizeof(buf));
15563 next = buf;
15564 while ((word = strsep(&next, ","))) {
15565 if (!strcasecmp(word, "port"))
15566 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
15567 else if (!strcasecmp(word, "invite"))
15568 ast_set_flag(&flags[0], SIP_INSECURE_INVITE);
15569 else
15570 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", v->value, v->lineno);
15571 }
15572 }
15573 } else if (!strcasecmp(v->name, "progressinband")) {
15574 ast_set_flag(&mask[0], SIP_PROG_INBAND);
15575 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
15576 if (ast_true(v->value))
15577 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
15578 else if (strcasecmp(v->value, "never"))
15579 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
15580 } else if (!strcasecmp(v->name, "promiscredir")) {
15581 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
15582 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
15583 } else if (!strcasecmp(v->name, "videosupport")) {
15584 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
15585 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
15586 } else if (!strcasecmp(v->name, "allowoverlap")) {
15587 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
15588 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
15589 } else if (!strcasecmp(v->name, "allowsubscribe")) {
15590 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
15591 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
15592 } else if (!strcasecmp(v->name, "t38pt_udptl")) {
15593 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_UDPTL);
15594 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);
15595 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
15596 } else if (!strcasecmp(v->name, "t38pt_rtp")) {
15597 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_RTP);
15598 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);
15599 } else if (!strcasecmp(v->name, "t38pt_tcp")) {
15600 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT_TCP);
15601 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);
15602 #endif
15603 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
15604 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
15605 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
15606 } else if (!strcasecmp(v->name, "buggymwi")) {
15607 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
15608 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
15609 } else
15610 res = 0;
15611
15612 return res;
15613 }
15614
15615
15616 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
15617 {
15618 struct domain *d;
15619
15620 if (ast_strlen_zero(domain)) {
15621 ast_log(LOG_WARNING, "Zero length domain.\n");
15622 return 1;
15623 }
15624
15625 if (!(d = ast_calloc(1, sizeof(*d))))
15626 return 0;
15627
15628 ast_copy_string(d->domain, domain, sizeof(d->domain));
15629
15630 if (!ast_strlen_zero(context))
15631 ast_copy_string(d->context, context, sizeof(d->context));
15632
15633 d->mode = mode;
15634
15635 AST_LIST_LOCK(&domain_list);
15636 AST_LIST_INSERT_TAIL(&domain_list, d, list);
15637 AST_LIST_UNLOCK(&domain_list);
15638
15639 if (sipdebug)
15640 ast_log(LOG_DEBUG, "Added local SIP domain '%s'\n", domain);
15641
15642 return 1;
15643 }
15644
15645
15646 static int check_sip_domain(const char *domain, char *context, size_t len)
15647 {
15648 struct domain *d;
15649 int result = 0;
15650
15651 AST_LIST_LOCK(&domain_list);
15652 AST_LIST_TRAVERSE(&domain_list, d, list) {
15653 if (strcasecmp(d->domain, domain))
15654 continue;
15655
15656 if (len && !ast_strlen_zero(d->context))
15657 ast_copy_string(context, d->context, len);
15658
15659 result = 1;
15660 break;
15661 }
15662 AST_LIST_UNLOCK(&domain_list);
15663
15664 return result;
15665 }
15666
15667
15668 static void clear_sip_domains(void)
15669 {
15670 struct domain *d;
15671
15672 AST_LIST_LOCK(&domain_list);
15673 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
15674 free(d);
15675 AST_LIST_UNLOCK(&domain_list);
15676 }
15677
15678
15679
15680 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno)
15681 {
15682 char authcopy[256];
15683 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
15684 char *stringp;
15685 struct sip_auth *a, *b, *auth;
15686
15687 if (ast_strlen_zero(configuration))
15688 return authlist;
15689
15690 if (option_debug)
15691 ast_log(LOG_DEBUG, "Auth config :: %s\n", configuration);
15692
15693 ast_copy_string(authcopy, configuration, sizeof(authcopy));
15694 stringp = authcopy;
15695
15696 username = stringp;
15697 realm = strrchr(stringp, '@');
15698 if (realm)
15699 *realm++ = '\0';
15700 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
15701 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
15702 return authlist;
15703 }
15704 stringp = username;
15705 username = strsep(&stringp, ":");
15706 if (username) {
15707 secret = strsep(&stringp, ":");
15708 if (!secret) {
15709 stringp = username;
15710 md5secret = strsep(&stringp,"#");
15711 }
15712 }
15713 if (!(auth = ast_calloc(1, sizeof(*auth))))
15714 return authlist;
15715
15716 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
15717 ast_copy_string(auth->username, username, sizeof(auth->username));
15718 if (secret)
15719 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
15720 if (md5secret)
15721 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
15722
15723
15724 for (b = NULL, a = authlist; a ; b = a, a = a->next)
15725 ;
15726 if (b)
15727 b->next = auth;
15728 else
15729 authlist = auth;
15730
15731 if (option_verbose > 2)
15732 ast_verbose("Added authentication for realm %s\n", realm);
15733
15734 return authlist;
15735
15736 }
15737
15738
15739 static int clear_realm_authentication(struct sip_auth *authlist)
15740 {
15741 struct sip_auth *a = authlist;
15742 struct sip_auth *b;
15743
15744 while (a) {
15745 b = a;
15746 a = a->next;
15747 free(b);
15748 }
15749
15750 return 1;
15751 }
15752
15753
15754 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
15755 {
15756 struct sip_auth *a;
15757
15758 for (a = authlist; a; a = a->next) {
15759 if (!strcasecmp(a->realm, realm))
15760 break;
15761 }
15762
15763 return a;
15764 }
15765
15766
15767 static struct sip_user *build_user(const char *name, struct ast_variable *v, int realtime)
15768 {
15769 struct sip_user *user;
15770 int format;
15771 struct ast_ha *oldha = NULL;
15772 char *varname = NULL, *varval = NULL;
15773 struct ast_variable *tmpvar = NULL;
15774 struct ast_flags userflags[2] = {{(0)}};
15775 struct ast_flags mask[2] = {{(0)}};
15776
15777
15778 if (!(user = ast_calloc(1, sizeof(*user))))
15779 return NULL;
15780
15781 suserobjs++;
15782 ASTOBJ_INIT(user);
15783 ast_copy_string(user->name, name, sizeof(user->name));
15784 oldha = user->ha;
15785 user->ha = NULL;
15786 ast_copy_flags(&user->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
15787 ast_copy_flags(&user->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
15788 user->capability = global_capability;
15789 user->allowtransfer = global_allowtransfer;
15790 user->maxcallbitrate = default_maxcallbitrate;
15791 user->autoframing = global_autoframing;
15792 user->prefs = default_prefs;
15793
15794 strcpy(user->context, default_context);
15795 strcpy(user->language, default_language);
15796 strcpy(user->mohinterpret, default_mohinterpret);
15797 strcpy(user->mohsuggest, default_mohsuggest);
15798 for (; v; v = v->next) {
15799 if (handle_common_options(&userflags[0], &mask[0], v))
15800 continue;
15801
15802 if (!strcasecmp(v->name, "context")) {
15803 ast_copy_string(user->context, v->value, sizeof(user->context));
15804 } else if (!strcasecmp(v->name, "subscribecontext")) {
15805 ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
15806 } else if (!strcasecmp(v->name, "setvar")) {
15807 varname = ast_strdupa(v->value);
15808 if ((varval = strchr(varname,'='))) {
15809 *varval++ = '\0';
15810 if ((tmpvar = ast_variable_new(varname, varval))) {
15811 tmpvar->next = user->chanvars;
15812 user->chanvars = tmpvar;
15813 }
15814 }
15815 } else if (!strcasecmp(v->name, "permit") ||
15816 !strcasecmp(v->name, "deny")) {
15817 user->ha = ast_append_ha(v->name, v->value, user->ha);
15818 } else if (!strcasecmp(v->name, "allowtransfer")) {
15819 user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
15820 } else if (!strcasecmp(v->name, "secret")) {
15821 ast_copy_string(user->secret, v->value, sizeof(user->secret));
15822 } else if (!strcasecmp(v->name, "md5secret")) {
15823 ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
15824 } else if (!strcasecmp(v->name, "callerid")) {
15825 ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
15826 } else if (!strcasecmp(v->name, "fullname")) {
15827 ast_copy_string(user->cid_name, v->value, sizeof(user->cid_name));
15828 } else if (!strcasecmp(v->name, "cid_number")) {
15829 ast_copy_string(user->cid_num, v->value, sizeof(user->cid_num));
15830 } else if (!strcasecmp(v->name, "callgroup")) {
15831 user->callgroup = ast_get_group(v->value);
15832 } else if (!strcasecmp(v->name, "pickupgroup")) {
15833 user->pickupgroup = ast_get_group(v->value);
15834 } else if (!strcasecmp(v->name, "language")) {
15835 ast_copy_string(user->language, v->value, sizeof(user->language));
15836 } else if (!strcasecmp(v->name, "mohinterpret")
15837 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
15838 ast_copy_string(user->mohinterpret, v->value, sizeof(user->mohinterpret));
15839 } else if (!strcasecmp(v->name, "mohsuggest")) {
15840 ast_copy_string(user->mohsuggest, v->value, sizeof(user->mohsuggest));
15841 } else if (!strcasecmp(v->name, "accountcode")) {
15842 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
15843 } else if (!strcasecmp(v->name, "call-limit")) {
15844 user->call_limit = atoi(v->value);
15845 if (user->call_limit < 0)
15846 user->call_limit = 0;
15847 } else if (!strcasecmp(v->name, "amaflags")) {
15848 format = ast_cdr_amaflags2int(v->value);
15849 if (format < 0) {
15850 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
15851 } else {
15852 user->amaflags = format;
15853 }
15854 } else if (!strcasecmp(v->name, "allow")) {
15855 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
15856 } else if (!strcasecmp(v->name, "disallow")) {
15857 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
15858 } else if (!strcasecmp(v->name, "autoframing")) {
15859 user->autoframing = ast_true(v->value);
15860 } else if (!strcasecmp(v->name, "callingpres")) {
15861 user->callingpres = ast_parse_caller_presentation(v->value);
15862 if (user->callingpres == -1)
15863 user->callingpres = atoi(v->value);
15864 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
15865 user->maxcallbitrate = atoi(v->value);
15866 if (user->maxcallbitrate < 0)
15867 user->maxcallbitrate = default_maxcallbitrate;
15868 }
15869
15870
15871
15872 }
15873 ast_copy_flags(&user->flags[0], &userflags[0], mask[0].flags);
15874 ast_copy_flags(&user->flags[1], &userflags[1], mask[1].flags);
15875 if (ast_test_flag(&user->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
15876 global_allowsubscribe = TRUE;
15877 ast_free_ha(oldha);
15878 return user;
15879 }
15880
15881
15882 static void set_peer_defaults(struct sip_peer *peer)
15883 {
15884 if (peer->expire == 0) {
15885
15886
15887
15888 peer->expire = -1;
15889 peer->pokeexpire = -1;
15890 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
15891 }
15892 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
15893 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
15894 strcpy(peer->context, default_context);
15895 strcpy(peer->subscribecontext, default_subscribecontext);
15896 strcpy(peer->language, default_language);
15897 strcpy(peer->mohinterpret, default_mohinterpret);
15898 strcpy(peer->mohsuggest, default_mohsuggest);
15899 peer->addr.sin_family = AF_INET;
15900 peer->defaddr.sin_family = AF_INET;
15901 peer->capability = global_capability;
15902 peer->maxcallbitrate = default_maxcallbitrate;
15903 peer->rtptimeout = global_rtptimeout;
15904 peer->rtpholdtimeout = global_rtpholdtimeout;
15905 peer->rtpkeepalive = global_rtpkeepalive;
15906 peer->allowtransfer = global_allowtransfer;
15907 peer->autoframing = global_autoframing;
15908 strcpy(peer->vmexten, default_vmexten);
15909 peer->secret[0] = '\0';
15910 peer->md5secret[0] = '\0';
15911 peer->cid_num[0] = '\0';
15912 peer->cid_name[0] = '\0';
15913 peer->fromdomain[0] = '\0';
15914 peer->fromuser[0] = '\0';
15915 peer->regexten[0] = '\0';
15916 peer->mailbox[0] = '\0';
15917 peer->callgroup = 0;
15918 peer->pickupgroup = 0;
15919 peer->maxms = default_qualify;
15920 peer->prefs = default_prefs;
15921 }
15922
15923
15924 static struct sip_peer *temp_peer(const char *name)
15925 {
15926 struct sip_peer *peer;
15927
15928 if (!(peer = ast_calloc(1, sizeof(*peer))))
15929 return NULL;
15930
15931 apeerobjs++;
15932 ASTOBJ_INIT(peer);
15933 set_peer_defaults(peer);
15934
15935 ast_copy_string(peer->name, name, sizeof(peer->name));
15936
15937 ast_set_flag(&peer->flags[1], SIP_PAGE2_SELFDESTRUCT);
15938 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
15939 peer->prefs = default_prefs;
15940 reg_source_db(peer);
15941
15942 return peer;
15943 }
15944
15945
15946 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
15947 {
15948 struct sip_peer *peer = NULL;
15949 struct ast_ha *oldha = NULL;
15950 int obproxyfound=0;
15951 int found=0;
15952 int firstpass=1;
15953 int format=0;
15954 time_t regseconds = 0;
15955 char *varname = NULL, *varval = NULL;
15956 struct ast_variable *tmpvar = NULL;
15957 struct ast_flags peerflags[2] = {{(0)}};
15958 struct ast_flags mask[2] = {{(0)}};
15959
15960
15961 if (!realtime)
15962
15963
15964
15965
15966
15967 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
15968
15969 if (peer) {
15970
15971 found = 1;
15972 if (!(peer->objflags & ASTOBJ_FLAG_MARKED))
15973 firstpass = 0;
15974 } else {
15975 if (!(peer = ast_calloc(1, sizeof(*peer))))
15976 return NULL;
15977
15978 if (realtime)
15979 rpeerobjs++;
15980 else
15981 speerobjs++;
15982 ASTOBJ_INIT(peer);
15983 }
15984
15985 if (firstpass) {
15986 peer->lastmsgssent = -1;
15987 oldha = peer->ha;
15988 peer->ha = NULL;
15989 set_peer_defaults(peer);
15990 }
15991 if (!found && name)
15992 ast_copy_string(peer->name, name, sizeof(peer->name));
15993
15994
15995 if (peer->chanvars) {
15996 ast_variables_destroy(peer->chanvars);
15997 peer->chanvars = NULL;
15998
15999 }
16000 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
16001 if (handle_common_options(&peerflags[0], &mask[0], v))
16002 continue;
16003 if (realtime && !strcasecmp(v->name, "regseconds")) {
16004 ast_get_time_t(v->value, ®seconds, 0, NULL);
16005 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
16006 inet_aton(v->value, &(peer->addr.sin_addr));
16007 } else if (realtime && !strcasecmp(v->name, "name"))
16008 ast_copy_string(peer->name, v->value, sizeof(peer->name));
16009 else if (realtime && !strcasecmp(v->name, "fullcontact")) {
16010 ast_copy_string(peer->fullcontact, v->value, sizeof(peer->fullcontact));
16011 ast_set_flag(&peer->flags[1], SIP_PAGE2_RT_FROMCONTACT);
16012 } else if (!strcasecmp(v->name, "secret"))
16013 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
16014 else if (!strcasecmp(v->name, "md5secret"))
16015 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
16016 else if (!strcasecmp(v->name, "auth"))
16017 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
16018 else if (!strcasecmp(v->name, "callerid")) {
16019 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
16020 } else if (!strcasecmp(v->name, "fullname")) {
16021 ast_copy_string(peer->cid_name, v->value, sizeof(peer->cid_name));
16022 } else if (!strcasecmp(v->name, "cid_number")) {
16023 ast_copy_string(peer->cid_num, v->value, sizeof(peer->cid_num));
16024 } else if (!strcasecmp(v->name, "context")) {
16025 ast_copy_string(peer->context, v->value, sizeof(peer->context));
16026 } else if (!strcasecmp(v->name, "subscribecontext")) {
16027 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
16028 } else if (!strcasecmp(v->name, "fromdomain")) {
16029 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
16030 } else if (!strcasecmp(v->name, "usereqphone")) {
16031 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
16032 } else if (!strcasecmp(v->name, "fromuser")) {
16033 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
16034 } else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "outboundproxy")) {
16035 if (!strcasecmp(v->value, "dynamic")) {
16036 if (!strcasecmp(v->name, "outboundproxy") || obproxyfound) {
16037 ast_log(LOG_WARNING, "You can't have a dynamic outbound proxy, you big silly head at line %d.\n", v->lineno);
16038 } else {
16039
16040 if (!found || !ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
16041
16042
16043 memset(&peer->addr.sin_addr, 0, 4);
16044 if (peer->addr.sin_port) {
16045
16046 peer->defaddr.sin_port = peer->addr.sin_port;
16047 peer->addr.sin_port = 0;
16048 }
16049 }
16050 ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16051 }
16052 } else {
16053
16054 if (peer->expire > -1)
16055 ast_sched_del(sched, peer->expire);
16056 peer->expire = -1;
16057 ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
16058 if (!obproxyfound || !strcasecmp(v->name, "outboundproxy")) {
16059 if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
16060 ASTOBJ_UNREF(peer, sip_destroy_peer);
16061 return NULL;
16062 }
16063 }
16064 if (!strcasecmp(v->name, "outboundproxy"))
16065 obproxyfound=1;
16066 else {
16067 ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
16068 if (!peer->addr.sin_port)
16069 peer->addr.sin_port = htons(STANDARD_SIP_PORT);
16070 }
16071 }
16072 } else if (!strcasecmp(v->name, "defaultip")) {
16073 if (ast_get_ip(&peer->defaddr, v->value)) {
16074 ASTOBJ_UNREF(peer, sip_destroy_peer);
16075 return NULL;
16076 }
16077 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
16078 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
16079 } else if (!strcasecmp(v->name, "port")) {
16080 if (!realtime && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC))
16081 peer->defaddr.sin_port = htons(atoi(v->value));
16082 else
16083 peer->addr.sin_port = htons(atoi(v->value));
16084 } else if (!strcasecmp(v->name, "callingpres")) {
16085 peer->callingpres = ast_parse_caller_presentation(v->value);
16086 if (peer->callingpres == -1)
16087 peer->callingpres = atoi(v->value);
16088 } else if (!strcasecmp(v->name, "username")) {
16089 ast_copy_string(peer->username, v->value, sizeof(peer->username));
16090 } else if (!strcasecmp(v->name, "language")) {
16091 ast_copy_string(peer->language, v->value, sizeof(peer->language));
16092 } else if (!strcasecmp(v->name, "regexten")) {
16093 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
16094 } else if (!strcasecmp(v->name, "call-limit") || !strcasecmp(v->name, "incominglimit")) {
16095 peer->call_limit = atoi(v->value);
16096 if (peer->call_limit < 0)
16097 peer->call_limit = 0;
16098 } else if (!strcasecmp(v->name, "amaflags")) {
16099 format = ast_cdr_amaflags2int(v->value);
16100 if (format < 0) {
16101 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
16102 } else {
16103 peer->amaflags = format;
16104 }
16105 } else if (!strcasecmp(v->name, "accountcode")) {
16106 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
16107 } else if (!strcasecmp(v->name, "mohinterpret")
16108 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
16109 ast_copy_string(peer->mohinterpret, v->value, sizeof(peer->mohinterpret));
16110 } else if (!strcasecmp(v->name, "mohsuggest")) {
16111 ast_copy_string(peer->mohsuggest, v->value, sizeof(peer->mohsuggest));
16112 } else if (!strcasecmp(v->name, "mailbox")) {
16113 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
16114 } else if (!strcasecmp(v->name, "subscribemwi")) {
16115 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
16116 } else if (!strcasecmp(v->name, "vmexten")) {
16117 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
16118 } else if (!strcasecmp(v->name, "callgroup")) {
16119 peer->callgroup = ast_get_group(v->value);
16120 } else if (!strcasecmp(v->name, "allowtransfer")) {
16121 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
16122 } else if (!strcasecmp(v->name, "pickupgroup")) {
16123 peer->pickupgroup = ast_get_group(v->value);
16124 } else if (!strcasecmp(v->name, "allow")) {
16125 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
16126 } else if (!strcasecmp(v->name, "disallow")) {
16127 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
16128 } else if (!strcasecmp(v->name, "autoframing")) {
16129 peer->autoframing = ast_true(v->value);
16130 } else if (!strcasecmp(v->name, "rtptimeout")) {
16131 if ((sscanf(v->value, "%d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
16132 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16133 peer->rtptimeout = global_rtptimeout;
16134 }
16135 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
16136 if ((sscanf(v->value, "%d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
16137 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16138 peer->rtpholdtimeout = global_rtpholdtimeout;
16139 }
16140 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
16141 if ((sscanf(v->value, "%d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
16142 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
16143 peer->rtpkeepalive = global_rtpkeepalive;
16144 }
16145 } else if (!strcasecmp(v->name, "setvar")) {
16146
16147 varname = ast_strdupa(v->value);
16148 if ((varval = strchr(varname, '='))) {
16149 *varval++ = '\0';
16150 if ((tmpvar = ast_variable_new(varname, varval))) {
16151 tmpvar->next = peer->chanvars;
16152 peer->chanvars = tmpvar;
16153 }
16154 }
16155 } else if (!strcasecmp(v->name, "qualify")) {
16156 if (!strcasecmp(v->value, "no")) {
16157 peer->maxms = 0;
16158 } else if (!strcasecmp(v->value, "yes")) {
16159 peer->maxms = DEFAULT_MAXMS;
16160 } else if (sscanf(v->value, "%d", &peer->maxms) != 1) {
16161 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);
16162 peer->maxms = 0;
16163 }
16164 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
16165 peer->maxcallbitrate = atoi(v->value);
16166 if (peer->maxcallbitrate < 0)
16167 peer->maxcallbitrate = default_maxcallbitrate;
16168 }
16169 }
16170 if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && realtime) {
16171 time_t nowtime = time(NULL);
16172
16173 if ((nowtime - regseconds) > 0) {
16174 destroy_association(peer);
16175 memset(&peer->addr, 0, sizeof(peer->addr));
16176 if (option_debug)
16177 ast_log(LOG_DEBUG, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
16178 }
16179 }
16180 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
16181 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
16182 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE))
16183 global_allowsubscribe = TRUE;
16184 if (!found && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !ast_test_flag(&peer->flags[0], SIP_REALTIME))
16185 reg_source_db(peer);
16186 ASTOBJ_UNMARK(peer);
16187 ast_free_ha(oldha);
16188 return peer;
16189 }
16190
16191
16192
16193
16194
16195
16196
16197 static int reload_config(enum channelreloadreason reason)
16198 {
16199 struct ast_config *cfg, *ucfg;
16200 struct ast_variable *v;
16201 struct sip_peer *peer;
16202 struct sip_user *user;
16203 struct ast_hostent ahp;
16204 char *cat, *stringp, *context, *oldregcontext;
16205 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
16206 struct hostent *hp;
16207 int format;
16208 struct ast_flags dummy[2];
16209 int auto_sip_domains = FALSE;
16210 struct sockaddr_in old_bindaddr = bindaddr;
16211 int registry_count = 0, peer_count = 0, user_count = 0;
16212 unsigned int temp_tos = 0;
16213 struct ast_flags debugflag = {0};
16214
16215 cfg = ast_config_load(config);
16216
16217
16218 if (!cfg) {
16219 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
16220 return -1;
16221 }
16222
16223
16224 ast_copy_string(oldcontexts, global_regcontext, sizeof(oldcontexts));
16225 oldregcontext = oldcontexts;
16226
16227
16228
16229 ast_copy_flags(&debugflag, &global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
16230 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
16231 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
16232 ast_copy_flags(&global_flags[1], &debugflag, SIP_PAGE2_DEBUG_CONSOLE);
16233
16234
16235 memset(&bindaddr, 0, sizeof(bindaddr));
16236 memset(&localaddr, 0, sizeof(localaddr));
16237 memset(&externip, 0, sizeof(externip));
16238 memset(&default_prefs, 0 , sizeof(default_prefs));
16239 outboundproxyip.sin_port = htons(STANDARD_SIP_PORT);
16240 outboundproxyip.sin_family = AF_INET;
16241 ourport = STANDARD_SIP_PORT;
16242 srvlookup = DEFAULT_SRVLOOKUP;
16243 global_tos_sip = DEFAULT_TOS_SIP;
16244 global_tos_audio = DEFAULT_TOS_AUDIO;
16245 global_tos_video = DEFAULT_TOS_VIDEO;
16246 externhost[0] = '\0';
16247 externexpire = 0;
16248 externrefresh = 10;
16249 memset(&outboundproxyip, 0, sizeof(outboundproxyip));
16250
16251
16252 allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
16253 global_regcontext[0] = '\0';
16254 expiry = DEFAULT_EXPIRY;
16255 global_notifyringing = DEFAULT_NOTIFYRINGING;
16256 global_limitonpeers = FALSE;
16257 global_directrtpsetup = FALSE;
16258 global_notifyhold = FALSE;
16259 global_alwaysauthreject = 0;
16260 global_allowsubscribe = FALSE;
16261 ast_copy_string(global_useragent, DEFAULT_USERAGENT, sizeof(global_useragent));
16262 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
16263 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME))
16264 ast_copy_string(global_realm, DEFAULT_REALM, sizeof(global_realm));
16265 else
16266 ast_copy_string(global_realm, ast_config_AST_SYSTEM_NAME, sizeof(global_realm));
16267 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
16268 compactheaders = DEFAULT_COMPACTHEADERS;
16269 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
16270 global_regattempts_max = 0;
16271 pedanticsipchecking = DEFAULT_PEDANTIC;
16272 global_mwitime = DEFAULT_MWITIME;
16273 autocreatepeer = DEFAULT_AUTOCREATEPEER;
16274 global_autoframing = 0;
16275 global_allowguest = DEFAULT_ALLOWGUEST;
16276 global_rtptimeout = 0;
16277 global_rtpholdtimeout = 0;
16278 global_rtpkeepalive = 0;
16279 global_allowtransfer = TRANSFER_OPENFORALL;
16280 global_rtautoclear = 120;
16281 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
16282 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
16283 ast_set_flag(&global_flags[1], SIP_PAGE2_RTUPDATE);
16284
16285
16286 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
16287 default_subscribecontext[0] = '\0';
16288 default_language[0] = '\0';
16289 default_fromdomain[0] = '\0';
16290 default_qualify = DEFAULT_QUALIFY;
16291 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
16292 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
16293 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
16294 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
16295 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
16296 ast_set_flag(&global_flags[0], SIP_NAT_RFC3581);
16297 ast_set_flag(&global_flags[0], SIP_CAN_REINVITE);
16298
16299
16300 dumphistory = FALSE;
16301 recordhistory = FALSE;
16302 ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
16303
16304
16305 global_relaxdtmf = FALSE;
16306 global_callevents = FALSE;
16307 global_t1min = DEFAULT_T1MIN;
16308
16309 global_matchexterniplocally = FALSE;
16310
16311
16312 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
16313
16314 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT);
16315
16316
16317 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
16318 if (handle_common_options(&global_flags[0], &dummy[0], v))
16319 continue;
16320
16321 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
16322 continue;
16323
16324
16325 if (!strcasecmp(v->name, "context")) {
16326 ast_copy_string(default_context, v->value, sizeof(default_context));
16327 } else if (!strcasecmp(v->name, "allowguest")) {
16328 global_allowguest = ast_true(v->value) ? 1 : 0;
16329 } else if (!strcasecmp(v->name, "realm")) {
16330 ast_copy_string(global_realm, v->value, sizeof(global_realm));
16331 } else if (!strcasecmp(v->name, "useragent")) {
16332 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
16333 if (option_debug)
16334 ast_log(LOG_DEBUG, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
16335 } else if (!strcasecmp(v->name, "allowtransfer")) {
16336 global_allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
16337 } else if (!strcasecmp(v->name, "rtcachefriends")) {
16338 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
16339 } else if (!strcasecmp(v->name, "rtsavesysname")) {
16340 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTSAVE_SYSNAME);
16341 } else if (!strcasecmp(v->name, "rtupdate")) {
16342 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTUPDATE);
16343 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
16344 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_IGNOREREGEXPIRE);
16345 } else if (!strcasecmp(v->name, "t1min")) {
16346 global_t1min = atoi(v->value);
16347 } else if (!strcasecmp(v->name, "rtautoclear")) {
16348 int i = atoi(v->value);
16349 if (i > 0)
16350 global_rtautoclear = i;
16351 else
16352 i = 0;
16353 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
16354 } else if (!strcasecmp(v->name, "usereqphone")) {
16355 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
16356 } else if (!strcasecmp(v->name, "relaxdtmf")) {
16357 global_relaxdtmf = ast_true(v->value);
16358 } else if (!strcasecmp(v->name, "checkmwi")) {
16359 if ((sscanf(v->value, "%d", &global_mwitime) != 1) || (global_mwitime < 0)) {
16360 ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d. Using default (10).\n", v->value, v->lineno);
16361 global_mwitime = DEFAULT_MWITIME;
16362 }
16363 } else if (!strcasecmp(v->name, "vmexten")) {
16364 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
16365 } else if (!strcasecmp(v->name, "rtptimeout")) {
16366 if ((sscanf(v->value, "%d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
16367 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16368 global_rtptimeout = 0;
16369 }
16370 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
16371 if ((sscanf(v->value, "%d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
16372 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
16373 global_rtpholdtimeout = 0;
16374 }
16375 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
16376 if ((sscanf(v->value, "%d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
16377 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
16378 global_rtpkeepalive = 0;
16379 }
16380 } else if (!strcasecmp(v->name, "compactheaders")) {
16381 compactheaders = ast_true(v->value);
16382 } else if (!strcasecmp(v->name, "notifymimetype")) {
16383 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
16384 } else if (!strncasecmp(v->name, "limitonpeer", 11)) {
16385 global_limitonpeers = ast_true(v->value);
16386 } else if (!strcasecmp(v->name, "directrtpsetup")) {
16387 global_directrtpsetup = ast_true(v->value);
16388 } else if (!strcasecmp(v->name, "notifyringing")) {
16389 global_notifyringing = ast_true(v->value);
16390 } else if (!strcasecmp(v->name, "notifyhold")) {
16391 global_notifyhold = ast_true(v->value);
16392 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
16393 global_alwaysauthreject = ast_true(v->value);
16394 } else if (!strcasecmp(v->name, "mohinterpret")
16395 || !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
16396 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
16397 } else if (!strcasecmp(v->name, "mohsuggest")) {
16398 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
16399 } else if (!strcasecmp(v->name, "language")) {
16400 ast_copy_string(default_language, v->value, sizeof(default_language));
16401 } else if (!strcasecmp(v->name, "regcontext")) {
16402 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
16403 stringp = newcontexts;
16404
16405 cleanup_stale_contexts(stringp, oldregcontext);
16406
16407 while ((context = strsep(&stringp, "&"))) {
16408 if (!ast_context_find(context))
16409 ast_context_create(NULL, context,"SIP");
16410 }
16411 ast_copy_string(global_regcontext, v->value, sizeof(global_regcontext));
16412 } else if (!strcasecmp(v->name, "callerid")) {
16413 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
16414 } else if (!strcasecmp(v->name, "fromdomain")) {
16415 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
16416 } else if (!strcasecmp(v->name, "outboundproxy")) {
16417 if (ast_get_ip_or_srv(&outboundproxyip, v->value, srvlookup ? "_sip._udp" : NULL) < 0)
16418 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", v->value);
16419 } else if (!strcasecmp(v->name, "outboundproxyport")) {
16420
16421 sscanf(v->value, "%d", &format);
16422 outboundproxyip.sin_port = htons(format);
16423 } else if (!strcasecmp(v->name, "autocreatepeer")) {
16424 autocreatepeer = ast_true(v->value);
16425 } else if (!strcasecmp(v->name, "srvlookup")) {
16426 srvlookup = ast_true(v->value);
16427 } else if (!strcasecmp(v->name, "pedantic")) {
16428 pedanticsipchecking = ast_true(v->value);
16429 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
16430 max_expiry = atoi(v->value);
16431 if (max_expiry < 1)
16432 max_expiry = DEFAULT_MAX_EXPIRY;
16433 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
16434 min_expiry = atoi(v->value);
16435 if (min_expiry < 1)
16436 min_expiry = DEFAULT_MIN_EXPIRY;
16437 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
16438 default_expiry = atoi(v->value);
16439 if (default_expiry < 1)
16440 default_expiry = DEFAULT_DEFAULT_EXPIRY;
16441 } else if (!strcasecmp(v->name, "sipdebug")) {
16442 if (ast_true(v->value))
16443 ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
16444 } else if (!strcasecmp(v->name, "dumphistory")) {
16445 dumphistory = ast_true(v->value);
16446 } else if (!strcasecmp(v->name, "recordhistory")) {
16447 recordhistory = ast_true(v->value);
16448 } else if (!strcasecmp(v->name, "registertimeout")) {
16449 global_reg_timeout = atoi(v->value);
16450 if (global_reg_timeout < 1)
16451 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
16452 } else if (!strcasecmp(v->name, "registerattempts")) {
16453 global_regattempts_max = atoi(v->value);
16454 } else if (!strcasecmp(v->name, "bindaddr")) {
16455 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
16456 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
16457 } else {
16458 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
16459 }
16460 } else if (!strcasecmp(v->name, "localnet")) {
16461 struct ast_ha *na;
16462 if (!(na = ast_append_ha("d", v->value, localaddr)))
16463 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
16464 else
16465 localaddr = na;
16466 } else if (!strcasecmp(v->name, "localmask")) {
16467 ast_log(LOG_WARNING, "Use of localmask is no long supported -- use localnet with mask syntax\n");
16468 } else if (!strcasecmp(v->name, "externip")) {
16469 if (!(hp = ast_gethostbyname(v->value, &ahp)))
16470 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
16471 else
16472 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
16473 externexpire = 0;
16474 } else if (!strcasecmp(v->name, "externhost")) {
16475 ast_copy_string(externhost, v->value, sizeof(externhost));
16476 if (!(hp = ast_gethostbyname(externhost, &ahp)))
16477 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
16478 else
16479 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
16480 externexpire = time(NULL);
16481 } else if (!strcasecmp(v->name, "externrefresh")) {
16482 if (sscanf(v->value, "%d", &externrefresh) != 1) {
16483 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
16484 externrefresh = 10;
16485 }
16486 } else if (!strcasecmp(v->name, "allow")) {
16487 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 1);
16488 } else if (!strcasecmp(v->name, "disallow")) {
16489 ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 0);
16490 } else if (!strcasecmp(v->name, "autoframing")) {
16491 global_autoframing = ast_true(v->value);
16492 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
16493 allow_external_domains = ast_true(v->value);
16494 } else if (!strcasecmp(v->name, "autodomain")) {
16495 auto_sip_domains = ast_true(v->value);
16496 } else if (!strcasecmp(v->name, "domain")) {
16497 char *domain = ast_strdupa(v->value);
16498 char *context = strchr(domain, ',');
16499
16500 if (context)
16501 *context++ = '\0';
16502
16503 if (option_debug && ast_strlen_zero(context))
16504 ast_log(LOG_DEBUG, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
16505 if (ast_strlen_zero(domain))
16506 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
16507 else
16508 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, context ? ast_strip(context) : "");
16509 } else if (!strcasecmp(v->name, "register")) {
16510 if (sip_register(v->value, v->lineno) == 0)
16511 registry_count++;
16512 } else if (!strcasecmp(v->name, "tos")) {
16513 if (!ast_str2tos(v->value, &temp_tos)) {
16514 global_tos_sip = temp_tos;
16515 global_tos_audio = temp_tos;
16516 global_tos_video = temp_tos;
16517 ast_log(LOG_WARNING, "tos value at line %d is deprecated. See doc/ip-tos.txt for more information.\n", v->lineno);
16518 } else
16519 ast_log(LOG_WARNING, "Invalid tos value at line %d, See doc/ip-tos.txt for more information.\n", v->lineno);
16520 } else if (!strcasecmp(v->name, "tos_sip")) {
16521 if (ast_str2tos(v->value, &global_tos_sip))
16522 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, recommended value is 'cs3'. See doc/ip-tos.txt.\n", v->lineno);
16523 } else if (!strcasecmp(v->name, "tos_audio")) {
16524 if (ast_str2tos(v->value, &global_tos_audio))
16525 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, recommended value is 'ef'. See doc/ip-tos.txt.\n", v->lineno);
16526 } else if (!strcasecmp(v->name, "tos_video")) {
16527 if (ast_str2tos(v->value, &global_tos_video))
16528 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, recommended value is 'af41'. See doc/ip-tos.txt.\n", v->lineno);
16529 } else if (!strcasecmp(v->name, "bindport")) {
16530 if (sscanf(v->value, "%d", &ourport) == 1) {
16531 bindaddr.sin_port = htons(ourport);
16532 } else {
16533 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
16534 }
16535 } else if (!strcasecmp(v->name, "qualify")) {
16536 if (!strcasecmp(v->value, "no")) {
16537 default_qualify = 0;
16538 } else if (!strcasecmp(v->value, "yes")) {
16539 default_qualify = DEFAULT_MAXMS;
16540 } else if (sscanf(v->value, "%d", &default_qualify) != 1) {
16541 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
16542 default_qualify = 0;
16543 }
16544 } else if (!strcasecmp(v->name, "callevents")) {
16545 global_callevents = ast_true(v->value);
16546 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
16547 default_maxcallbitrate = atoi(v->value);
16548 if (default_maxcallbitrate < 0)
16549 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
16550 } else if (!strcasecmp(v->name, "matchexterniplocally")) {
16551 global_matchexterniplocally = ast_true(v->value);
16552 }
16553 }
16554
16555 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
16556 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
16557 allow_external_domains = 1;
16558 }
16559
16560
16561 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
16562
16563 if (!strcasecmp(v->name, "auth"))
16564 authl = add_realm_authentication(authl, v->value, v->lineno);
16565 }
16566
16567 ucfg = ast_config_load("users.conf");
16568 if (ucfg) {
16569 struct ast_variable *gen;
16570 int genhassip, genregistersip;
16571 const char *hassip, *registersip;
16572
16573 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
16574 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
16575 gen = ast_variable_browse(ucfg, "general");
16576 cat = ast_category_browse(ucfg, NULL);
16577 while (cat) {
16578 if (strcasecmp(cat, "general")) {
16579 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
16580 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
16581 if (ast_true(hassip) || (!hassip && genhassip)) {
16582 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
16583 if (peer) {
16584 ASTOBJ_CONTAINER_LINK(&peerl,peer);
16585 ASTOBJ_UNREF(peer, sip_destroy_peer);
16586 peer_count++;
16587 }
16588 }
16589 if (ast_true(registersip) || (!registersip && genregistersip)) {
16590 char tmp[256];
16591 const char *host = ast_variable_retrieve(ucfg, cat, "host");
16592 const char *username = ast_variable_retrieve(ucfg, cat, "username");
16593 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
16594 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
16595 if (!host)
16596 host = ast_variable_retrieve(ucfg, "general", "host");
16597 if (!username)
16598 username = ast_variable_retrieve(ucfg, "general", "username");
16599 if (!secret)
16600 secret = ast_variable_retrieve(ucfg, "general", "secret");
16601 if (!contact)
16602 contact = "s";
16603 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
16604 if (!ast_strlen_zero(secret))
16605 snprintf(tmp, sizeof(tmp), "%s:%s@%s/%s", username, secret, host, contact);
16606 else
16607 snprintf(tmp, sizeof(tmp), "%s@%s/%s", username, host, contact);
16608 if (sip_register(tmp, 0) == 0)
16609 registry_count++;
16610 }
16611 }
16612 }
16613 cat = ast_category_browse(ucfg, cat);
16614 }
16615 ast_config_destroy(ucfg);
16616 }
16617
16618
16619
16620 cat = NULL;
16621 while ( (cat = ast_category_browse(cfg, cat)) ) {
16622 const char *utype;
16623 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
16624 continue;
16625 utype = ast_variable_retrieve(cfg, cat, "type");
16626 if (!utype) {
16627 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
16628 continue;
16629 } else {
16630 int is_user = 0, is_peer = 0;
16631 if (!strcasecmp(utype, "user"))
16632 is_user = 1;
16633 else if (!strcasecmp(utype, "friend"))
16634 is_user = is_peer = 1;
16635 else if (!strcasecmp(utype, "peer"))
16636 is_peer = 1;
16637 else {
16638 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
16639 continue;
16640 }
16641 if (is_user) {
16642 user = build_user(cat, ast_variable_browse(cfg, cat), 0);
16643 if (user) {
16644 ASTOBJ_CONTAINER_LINK(&userl,user);
16645 ASTOBJ_UNREF(user, sip_destroy_user);
16646 user_count++;
16647 }
16648 }
16649 if (is_peer) {
16650 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
16651 if (peer) {
16652 ASTOBJ_CONTAINER_LINK(&peerl,peer);
16653 ASTOBJ_UNREF(peer, sip_destroy_peer);
16654 peer_count++;
16655 }
16656 }
16657 }
16658 }
16659 if (ast_find_ourip(&__ourip, bindaddr)) {
16660 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
16661 return 0;
16662 }
16663 if (!ntohs(bindaddr.sin_port))
16664 bindaddr.sin_port = ntohs(STANDARD_SIP_PORT);
16665 bindaddr.sin_family = AF_INET;
16666 ast_mutex_lock(&netlock);
16667 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
16668 close(sipsock);
16669 sipsock = -1;
16670 }
16671 if (sipsock < 0) {
16672 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
16673 if (sipsock < 0) {
16674 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
16675 return -1;
16676 } else {
16677
16678 const int reuseFlag = 1;
16679
16680 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
16681 (const char*)&reuseFlag,
16682 sizeof reuseFlag);
16683
16684 ast_enable_packet_fragmentation(sipsock);
16685
16686 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
16687 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
16688 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
16689 strerror(errno));
16690 close(sipsock);
16691 sipsock = -1;
16692 } else {
16693 if (option_verbose > 1) {
16694 ast_verbose(VERBOSE_PREFIX_2 "SIP Listening on %s:%d\n",
16695 ast_inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
16696 ast_verbose(VERBOSE_PREFIX_2 "Using SIP TOS: %s\n", ast_tos2str(global_tos_sip));
16697 }
16698 if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &global_tos_sip, sizeof(global_tos_sip)))
16699 ast_log(LOG_WARNING, "Unable to set SIP TOS to %s\n", ast_tos2str(global_tos_sip));
16700 }
16701 }
16702 }
16703 ast_mutex_unlock(&netlock);
16704
16705
16706
16707
16708
16709 if (auto_sip_domains) {
16710 char temp[MAXHOSTNAMELEN];
16711
16712
16713 if (bindaddr.sin_addr.s_addr)
16714 add_sip_domain(ast_inet_ntoa(bindaddr.sin_addr), SIP_DOMAIN_AUTO, NULL);
16715 else
16716 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
16717
16718
16719 if (externip.sin_addr.s_addr)
16720 add_sip_domain(ast_inet_ntoa(externip.sin_addr), SIP_DOMAIN_AUTO, NULL);
16721
16722
16723 if (!ast_strlen_zero(externhost))
16724 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
16725
16726
16727 if (!gethostname(temp, sizeof(temp)))
16728 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
16729 }
16730
16731
16732 ast_config_destroy(cfg);
16733
16734
16735 if (notify_types)
16736 ast_config_destroy(notify_types);
16737 notify_types = ast_config_load(notify_config);
16738
16739
16740 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);
16741
16742 return 0;
16743 }
16744
16745 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
16746 {
16747 struct sip_pvt *p;
16748 struct ast_udptl *udptl = NULL;
16749
16750 p = chan->tech_pvt;
16751 if (!p)
16752 return NULL;
16753
16754 ast_mutex_lock(&p->lock);
16755 if (p->udptl && ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
16756 udptl = p->udptl;
16757 ast_mutex_unlock(&p->lock);
16758 return udptl;
16759 }
16760
16761 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
16762 {
16763 struct sip_pvt *p;
16764
16765 p = chan->tech_pvt;
16766 if (!p)
16767 return -1;
16768 ast_mutex_lock(&p->lock);
16769 if (udptl)
16770 ast_udptl_get_peer(udptl, &p->udptlredirip);
16771 else
16772 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
16773 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
16774 if (!p->pendinginvite) {
16775 if (option_debug > 2) {
16776 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);
16777 }
16778 transmit_reinvite_with_t38_sdp(p);
16779 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
16780 if (option_debug > 2) {
16781 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);
16782 }
16783 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
16784 }
16785 }
16786
16787 p->lastrtprx = p->lastrtptx = time(NULL);
16788 ast_mutex_unlock(&p->lock);
16789 return 0;
16790 }
16791
16792
16793
16794
16795
16796
16797 static int sip_handle_t38_reinvite(struct ast_channel *chan, struct sip_pvt *pvt, int reinvite)
16798 {
16799 struct sip_pvt *p;
16800 int flag = 0;
16801
16802 p = chan->tech_pvt;
16803 if (!p || !pvt->udptl)
16804 return -1;
16805
16806
16807 ast_mutex_lock(&p->lock);
16808
16809
16810
16811 p->t38.jointcapability = p->t38.peercapability = pvt->t38.jointcapability;
16812
16813 ast_udptl_set_far_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
16814 ast_udptl_set_local_max_datagram(p->udptl, ast_udptl_get_local_max_datagram(pvt->udptl));
16815 ast_udptl_set_error_correction_scheme(p->udptl, ast_udptl_get_error_correction_scheme(pvt->udptl));
16816
16817 if (reinvite) {
16818
16819
16820
16821
16822
16823 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
16824 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
16825 flag =1;
16826 } else {
16827 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
16828 }
16829 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
16830 if (!p->pendinginvite) {
16831 if (option_debug > 2) {
16832 if (flag)
16833 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));
16834 else
16835 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));
16836 }
16837 transmit_reinvite_with_t38_sdp(p);
16838 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
16839 if (option_debug > 2) {
16840 if (flag)
16841 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));
16842 else
16843 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));
16844 }
16845 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
16846 }
16847 }
16848
16849 p->lastrtprx = p->lastrtptx = time(NULL);
16850 ast_mutex_unlock(&p->lock);
16851 return 0;
16852 } else {
16853 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE) && ast_test_flag(&pvt->flags[0], SIP_CAN_REINVITE)) {
16854 ast_udptl_get_peer(pvt->udptl, &p->udptlredirip);
16855 flag = 1;
16856 } else {
16857 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
16858 }
16859 if (option_debug > 2) {
16860 if (flag)
16861 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));
16862 else
16863 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));
16864 }
16865 pvt->t38.state = T38_ENABLED;
16866 p->t38.state = T38_ENABLED;
16867 if (option_debug > 1) {
16868 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", pvt->t38.state, pvt->owner ? pvt->owner->name : "<none>");
16869 ast_log(LOG_DEBUG, "T38 changed state to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
16870 }
16871 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
16872 p->lastrtprx = p->lastrtptx = time(NULL);
16873 ast_mutex_unlock(&p->lock);
16874 return 0;
16875 }
16876 }
16877
16878
16879
16880 static enum ast_rtp_get_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
16881 {
16882 struct sip_pvt *p = NULL;
16883 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
16884
16885 if (!(p = chan->tech_pvt))
16886 return AST_RTP_GET_FAILED;
16887
16888 ast_mutex_lock(&p->lock);
16889 if (!(p->rtp)) {
16890 ast_mutex_unlock(&p->lock);
16891 return AST_RTP_GET_FAILED;
16892 }
16893
16894 *rtp = p->rtp;
16895
16896 if (ast_rtp_getnat(*rtp) && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT))
16897 res = AST_RTP_TRY_PARTIAL;
16898 else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
16899 res = AST_RTP_TRY_NATIVE;
16900 else if (ast_test_flag(&global_jbconf, AST_JB_FORCED))
16901 res = AST_RTP_GET_FAILED;
16902
16903 ast_mutex_unlock(&p->lock);
16904
16905 return res;
16906 }
16907
16908
16909 static enum ast_rtp_get_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
16910 {
16911 struct sip_pvt *p = NULL;
16912 enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
16913
16914 if (!(p = chan->tech_pvt))
16915 return AST_RTP_GET_FAILED;
16916
16917 ast_mutex_lock(&p->lock);
16918 if (!(p->vrtp)) {
16919 ast_mutex_unlock(&p->lock);
16920 return AST_RTP_GET_FAILED;
16921 }
16922
16923 *rtp = p->vrtp;
16924
16925 if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE))
16926 res = AST_RTP_TRY_NATIVE;
16927
16928 ast_mutex_unlock(&p->lock);
16929
16930 return res;
16931 }
16932
16933
16934 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
16935 {
16936 struct sip_pvt *p;
16937 int changed = 0;
16938
16939 p = chan->tech_pvt;
16940 if (!p)
16941 return -1;
16942
16943
16944 if (chan->_state != AST_STATE_UP && !global_directrtpsetup)
16945 return 0;
16946
16947 ast_mutex_lock(&p->lock);
16948 if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) {
16949
16950 ast_mutex_unlock(&p->lock);
16951 return 0;
16952 }
16953
16954
16955
16956
16957 if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
16958 ast_mutex_unlock(&p->lock);
16959 return 0;
16960 }
16961
16962 if (rtp) {
16963 changed |= ast_rtp_get_peer(rtp, &p->redirip);
16964 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
16965 memset(&p->redirip, 0, sizeof(p->redirip));
16966 changed = 1;
16967 }
16968 if (vrtp) {
16969 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
16970 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
16971 memset(&p->vredirip, 0, sizeof(p->vredirip));
16972 changed = 1;
16973 }
16974 if (codecs && (p->redircodecs != codecs)) {
16975 p->redircodecs = codecs;
16976 changed = 1;
16977 }
16978 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
16979 if (chan->_state != AST_STATE_UP) {
16980 if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
16981 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
16982 if (option_debug)
16983 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));
16984 } else if (!p->pendinginvite) {
16985 if (option_debug > 2) {
16986 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));
16987 }
16988 transmit_reinvite_with_sdp(p);
16989 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
16990 if (option_debug > 2) {
16991 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));
16992 }
16993
16994 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
16995 }
16996 }
16997
16998 p->lastrtprx = p->lastrtptx = time(NULL);
16999 ast_mutex_unlock(&p->lock);
17000 return 0;
17001 }
17002
17003 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
17004 static char *descrip_dtmfmode = "SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
17005 static char *app_dtmfmode = "SIPDtmfMode";
17006
17007 static char *app_sipaddheader = "SIPAddHeader";
17008 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
17009
17010 static char *descrip_sipaddheader = ""
17011 " SIPAddHeader(Header: Content)\n"
17012 "Adds a header to a SIP call placed with DIAL.\n"
17013 "Remember to user the X-header if you are adding non-standard SIP\n"
17014 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
17015 "Adding the wrong headers may jeopardize the SIP dialog.\n"
17016 "Always returns 0\n";
17017
17018
17019
17020 static int sip_dtmfmode(struct ast_channel *chan, void *data)
17021 {
17022 struct sip_pvt *p;
17023 char *mode;
17024 if (data)
17025 mode = (char *)data;
17026 else {
17027 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
17028 return 0;
17029 }
17030 ast_channel_lock(chan);
17031 if (chan->tech != &sip_tech && chan->tech != &sip_tech_info) {
17032 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
17033 ast_channel_unlock(chan);
17034 return 0;
17035 }
17036 p = chan->tech_pvt;
17037 if (!p) {
17038 ast_channel_unlock(chan);
17039 return 0;
17040 }
17041 ast_mutex_lock(&p->lock);
17042 if (!strcasecmp(mode,"info")) {
17043 ast_clear_flag(&p->flags[0], SIP_DTMF);
17044 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
17045 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
17046 } else if (!strcasecmp(mode,"rfc2833")) {
17047 ast_clear_flag(&p->flags[0], SIP_DTMF);
17048 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
17049 p->jointnoncodeccapability |= AST_RTP_DTMF;
17050 } else if (!strcasecmp(mode,"inband")) {
17051 ast_clear_flag(&p->flags[0], SIP_DTMF);
17052 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
17053 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
17054 } else
17055 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n",mode);
17056 if (p->rtp)
17057 ast_rtp_setdtmf(p->rtp, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
17058 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
17059 if (!p->vad) {
17060 p->vad = ast_dsp_new();
17061 ast_dsp_set_features(p->vad, DSP_FEATURE_DTMF_DETECT);
17062 }
17063 } else {
17064 if (p->vad) {
17065 ast_dsp_free(p->vad);
17066 p->vad = NULL;
17067 }
17068 }
17069 ast_mutex_unlock(&p->lock);
17070 ast_channel_unlock(chan);
17071 return 0;
17072 }
17073
17074
17075 static int sip_addheader(struct ast_channel *chan, void *data)
17076 {
17077 int no = 0;
17078 int ok = FALSE;
17079 char varbuf[30];
17080 char *inbuf = (char *) data;
17081
17082 if (ast_strlen_zero(inbuf)) {
17083 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
17084 return 0;
17085 }
17086 ast_channel_lock(chan);
17087
17088
17089 while (!ok && no <= 50) {
17090 no++;
17091 snprintf(varbuf, sizeof(varbuf), "_SIPADDHEADER%.2d", no);
17092
17093
17094 if( (pbx_builtin_getvar_helper(chan, (const char *) varbuf + 1) == (const char *) NULL) )
17095 ok = TRUE;
17096 }
17097 if (ok) {
17098 pbx_builtin_setvar_helper (chan, varbuf, inbuf);
17099 if (sipdebug)
17100 ast_log(LOG_DEBUG,"SIP Header added \"%s\" as %s\n", inbuf, varbuf);
17101 } else {
17102 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
17103 }
17104 ast_channel_unlock(chan);
17105 return 0;
17106 }
17107
17108
17109
17110
17111
17112
17113
17114 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
17115 {
17116 char *cdest;
17117 char *extension, *host, *port;
17118 char tmp[80];
17119
17120 cdest = ast_strdupa(dest);
17121
17122 extension = strsep(&cdest, "@");
17123 host = strsep(&cdest, ":");
17124 port = strsep(&cdest, ":");
17125 if (ast_strlen_zero(extension)) {
17126 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
17127 return 0;
17128 }
17129
17130
17131 if (!host) {
17132 char *localtmp;
17133 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
17134 if (ast_strlen_zero(tmp)) {
17135 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
17136 return 0;
17137 }
17138 if ((localtmp = strstr(tmp, "sip:")) && (localtmp = strchr(localtmp, '@'))) {
17139 char lhost[80], lport[80];
17140 memset(lhost, 0, sizeof(lhost));
17141 memset(lport, 0, sizeof(lport));
17142 localtmp++;
17143
17144 sscanf(localtmp, "%[^<>:; ]:%[^<>:; ]", lhost, lport);
17145 if (ast_strlen_zero(lhost)) {
17146 ast_log(LOG_ERROR, "Can't find the host address\n");
17147 return 0;
17148 }
17149 host = ast_strdupa(lhost);
17150 if (!ast_strlen_zero(lport)) {
17151 port = ast_strdupa(lport);
17152 }
17153 }
17154 }
17155
17156 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
17157 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
17158
17159 sip_scheddestroy(p, 32000);
17160
17161 return 0;
17162 }
17163
17164
17165 static int sip_get_codec(struct ast_channel *chan)
17166 {
17167 struct sip_pvt *p = chan->tech_pvt;
17168 return p->peercapability ? p->peercapability : p->capability;
17169 }
17170
17171
17172
17173
17174
17175 static void sip_poke_all_peers(void)
17176 {
17177 int ms = 0;
17178
17179 if (!speerobjs)
17180 return;
17181
17182 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
17183 ASTOBJ_WRLOCK(iterator);
17184 if (iterator->pokeexpire > -1)
17185 ast_sched_del(sched, iterator->pokeexpire);
17186 ms += 100;
17187 iterator->pokeexpire = ast_sched_add(sched, ms, sip_poke_peer_s, iterator);
17188 ASTOBJ_UNLOCK(iterator);
17189 } while (0)
17190 );
17191 }
17192
17193
17194 static void sip_send_all_registers(void)
17195 {
17196 int ms;
17197 int regspacing;
17198 if (!regobjs)
17199 return;
17200 regspacing = default_expiry * 1000/regobjs;
17201 if (regspacing > 100)
17202 regspacing = 100;
17203 ms = regspacing;
17204 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
17205 ASTOBJ_WRLOCK(iterator);
17206 if (iterator->expire > -1)
17207 ast_sched_del(sched, iterator->expire);
17208 ms += regspacing;
17209 iterator->expire = ast_sched_add(sched, ms, sip_reregister, iterator);
17210 ASTOBJ_UNLOCK(iterator);
17211 } while (0)
17212 );
17213 }
17214
17215
17216 static int sip_do_reload(enum channelreloadreason reason)
17217 {
17218 if (option_debug > 3)
17219 ast_log(LOG_DEBUG, "--------------- SIP reload started\n");
17220
17221 clear_realm_authentication(authl);
17222 clear_sip_domains();
17223 authl = NULL;
17224
17225
17226
17227 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
17228 ASTOBJ_RDLOCK(iterator);
17229 if (iterator->call) {
17230 if (option_debug > 2)
17231 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
17232
17233 sip_destroy(iterator->call);
17234 }
17235 ASTOBJ_UNLOCK(iterator);
17236
17237 } while(0));
17238
17239
17240 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
17241 if (option_debug > 3)
17242 ast_log(LOG_DEBUG, "--------------- Done destroying user list\n");
17243 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
17244 if (option_debug > 3)
17245 ast_log(LOG_DEBUG, "--------------- Done destroying registry list\n");
17246 ASTOBJ_CONTAINER_MARKALL(&peerl);
17247 reload_config(reason);
17248
17249
17250 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
17251 if (option_debug > 3)
17252 ast_log(LOG_DEBUG, "--------------- Done destroying pruned peers\n");
17253
17254
17255 sip_poke_all_peers();
17256
17257
17258 sip_send_all_registers();
17259
17260 if (option_debug > 3)
17261 ast_log(LOG_DEBUG, "--------------- SIP reload done\n");
17262
17263 return 0;
17264 }
17265
17266
17267 static int sip_reload(int fd, int argc, char *argv[])
17268 {
17269 ast_mutex_lock(&sip_reload_lock);
17270 if (sip_reloading)
17271 ast_verbose("Previous SIP reload not yet done\n");
17272 else {
17273 sip_reloading = TRUE;
17274 if (fd)
17275 sip_reloadreason = CHANNEL_CLI_RELOAD;
17276 else
17277 sip_reloadreason = CHANNEL_MODULE_RELOAD;
17278 }
17279 ast_mutex_unlock(&sip_reload_lock);
17280 restart_monitor();
17281
17282 return 0;
17283 }
17284
17285
17286 static int reload(void)
17287 {
17288 return sip_reload(0, 0, NULL);
17289 }
17290
17291 static struct ast_cli_entry cli_sip_debug_deprecated =
17292 { { "sip", "debug", NULL },
17293 sip_do_debug_deprecated, "Enable SIP debugging",
17294 debug_usage };
17295
17296 static struct ast_cli_entry cli_sip_no_debug_deprecated =
17297 { { "sip", "no", "debug", NULL },
17298 sip_no_debug_deprecated, "Disable SIP debugging",
17299 debug_usage };
17300
17301 static struct ast_cli_entry cli_sip[] = {
17302 { { "sip", "show", "channels", NULL },
17303 sip_show_channels, "List active SIP channels",
17304 show_channels_usage },
17305
17306 { { "sip", "show", "domains", NULL },
17307 sip_show_domains, "List our local SIP domains.",
17308 show_domains_usage },
17309
17310 { { "sip", "show", "inuse", NULL },
17311 sip_show_inuse, "List all inuse/limits",
17312 show_inuse_usage },
17313
17314 { { "sip", "show", "objects", NULL },
17315 sip_show_objects, "List all SIP object allocations",
17316 show_objects_usage },
17317
17318 { { "sip", "show", "peers", NULL },
17319 sip_show_peers, "List defined SIP peers",
17320 show_peers_usage },
17321
17322 { { "sip", "show", "registry", NULL },
17323 sip_show_registry, "List SIP registration status",
17324 show_reg_usage },
17325
17326 { { "sip", "show", "settings", NULL },
17327 sip_show_settings, "Show SIP global settings",
17328 show_settings_usage },
17329
17330 { { "sip", "show", "subscriptions", NULL },
17331 sip_show_subscriptions, "List active SIP subscriptions",
17332 show_subscriptions_usage },
17333
17334 { { "sip", "show", "users", NULL },
17335 sip_show_users, "List defined SIP users",
17336 show_users_usage },
17337
17338 { { "sip", "notify", NULL },
17339 sip_notify, "Send a notify packet to a SIP peer",
17340 notify_usage, complete_sipnotify },
17341
17342 { { "sip", "show", "channel", NULL },
17343 sip_show_channel, "Show detailed SIP channel info",
17344 show_channel_usage, complete_sipch },
17345
17346 { { "sip", "show", "history", NULL },
17347 sip_show_history, "Show SIP dialog history",
17348 show_history_usage, complete_sipch },
17349
17350 { { "sip", "show", "peer", NULL },
17351 sip_show_peer, "Show details on specific SIP peer",
17352 show_peer_usage, complete_sip_show_peer },
17353
17354 { { "sip", "show", "user", NULL },
17355 sip_show_user, "Show details on specific SIP user",
17356 show_user_usage, complete_sip_show_user },
17357
17358 { { "sip", "prune", "realtime", NULL },
17359 sip_prune_realtime, "Prune cached Realtime object(s)",
17360 prune_realtime_usage },
17361
17362 { { "sip", "prune", "realtime", "peer", NULL },
17363 sip_prune_realtime, "Prune cached Realtime peer(s)",
17364 prune_realtime_usage, complete_sip_prune_realtime_peer },
17365
17366 { { "sip", "prune", "realtime", "user", NULL },
17367 sip_prune_realtime, "Prune cached Realtime user(s)",
17368 prune_realtime_usage, complete_sip_prune_realtime_user },
17369
17370 { { "sip", "set", "debug", NULL },
17371 sip_do_debug, "Enable SIP debugging",
17372 debug_usage, NULL, &cli_sip_debug_deprecated },
17373
17374 { { "sip", "set", "debug", "ip", NULL },
17375 sip_do_debug, "Enable SIP debugging on IP",
17376 debug_usage },
17377
17378 { { "sip", "set", "debug", "peer", NULL },
17379 sip_do_debug, "Enable SIP debugging on Peername",
17380 debug_usage, complete_sip_debug_peer },
17381
17382 { { "sip", "set", "debug", "off", NULL },
17383 sip_no_debug, "Disable SIP debugging",
17384 no_debug_usage, NULL, &cli_sip_no_debug_deprecated },
17385
17386 { { "sip", "history", NULL },
17387 sip_do_history, "Enable SIP history",
17388 history_usage },
17389
17390 { { "sip", "history", "off", NULL },
17391 sip_no_history, "Disable SIP history",
17392 no_history_usage },
17393
17394 { { "sip", "reload", NULL },
17395 sip_reload, "Reload SIP configuration",
17396 sip_reload_usage },
17397 };
17398
17399
17400 static int load_module(void)
17401 {
17402 ASTOBJ_CONTAINER_INIT(&userl);
17403 ASTOBJ_CONTAINER_INIT(&peerl);
17404 ASTOBJ_CONTAINER_INIT(®l);
17405
17406 if (!(sched = sched_context_create())) {
17407 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
17408 return AST_MODULE_LOAD_FAILURE;
17409 }
17410
17411 if (!(io = io_context_create())) {
17412 ast_log(LOG_ERROR, "Unable to create I/O context\n");
17413 sched_context_destroy(sched);
17414 return AST_MODULE_LOAD_FAILURE;
17415 }
17416
17417 sip_reloadreason = CHANNEL_MODULE_LOAD;
17418
17419 if(reload_config(sip_reloadreason))
17420 return AST_MODULE_LOAD_DECLINE;
17421
17422
17423 if (ast_channel_register(&sip_tech)) {
17424 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
17425 io_context_destroy(io);
17426 sched_context_destroy(sched);
17427 return AST_MODULE_LOAD_FAILURE;
17428 }
17429
17430
17431 ast_cli_register_multiple(cli_sip, sizeof(cli_sip)/ sizeof(struct ast_cli_entry));
17432
17433
17434 ast_rtp_proto_register(&sip_rtp);
17435
17436
17437 ast_udptl_proto_register(&sip_udptl);
17438
17439
17440 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
17441 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
17442
17443
17444 ast_custom_function_register(&sip_header_function);
17445 ast_custom_function_register(&sippeer_function);
17446 ast_custom_function_register(&sipchaninfo_function);
17447 ast_custom_function_register(&checksipdomain_function);
17448
17449
17450 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM, manager_sip_show_peers,
17451 "List SIP peers (text format)", mandescr_show_peers);
17452 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM, manager_sip_show_peer,
17453 "Show SIP peer (text format)", mandescr_show_peer);
17454
17455 sip_poke_all_peers();
17456 sip_send_all_registers();
17457
17458
17459 restart_monitor();
17460
17461 return AST_MODULE_LOAD_SUCCESS;
17462 }
17463
17464
17465 static int unload_module(void)
17466 {
17467 struct sip_pvt *p, *pl;
17468
17469
17470 ast_channel_unregister(&sip_tech);
17471
17472
17473 ast_custom_function_unregister(&sipchaninfo_function);
17474 ast_custom_function_unregister(&sippeer_function);
17475 ast_custom_function_unregister(&sip_header_function);
17476 ast_custom_function_unregister(&checksipdomain_function);
17477
17478
17479 ast_unregister_application(app_dtmfmode);
17480 ast_unregister_application(app_sipaddheader);
17481
17482
17483 ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
17484
17485
17486 ast_rtp_proto_unregister(&sip_rtp);
17487
17488
17489 ast_udptl_proto_unregister(&sip_udptl);
17490
17491
17492 ast_manager_unregister("SIPpeers");
17493 ast_manager_unregister("SIPshowpeer");
17494
17495 ast_mutex_lock(&iflock);
17496
17497 for (p = iflist; p ; p = p->next) {
17498 if (p->owner)
17499 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
17500 }
17501 ast_mutex_unlock(&iflock);
17502
17503 ast_mutex_lock(&monlock);
17504 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
17505 pthread_cancel(monitor_thread);
17506 pthread_kill(monitor_thread, SIGURG);
17507 pthread_join(monitor_thread, NULL);
17508 }
17509 monitor_thread = AST_PTHREADT_STOP;
17510 ast_mutex_unlock(&monlock);
17511
17512 ast_mutex_lock(&iflock);
17513
17514 p = iflist;
17515 while (p) {
17516 pl = p;
17517 p = p->next;
17518 __sip_destroy(pl, TRUE);
17519 }
17520 iflist = NULL;
17521 ast_mutex_unlock(&iflock);
17522
17523
17524 ast_free_ha(localaddr);
17525
17526 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
17527 ASTOBJ_CONTAINER_DESTROY(&userl);
17528 ASTOBJ_CONTAINER_DESTROYALL(&peerl, sip_destroy_peer);
17529 ASTOBJ_CONTAINER_DESTROY(&peerl);
17530 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
17531 ASTOBJ_CONTAINER_DESTROY(®l);
17532
17533 clear_realm_authentication(authl);
17534 clear_sip_domains();
17535 close(sipsock);
17536 sched_context_destroy(sched);
17537
17538 return 0;
17539 }
17540
17541 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Session Initiation Protocol (SIP)",
17542 .load = load_module,
17543 .unload = unload_module,
17544 .reload = reload,
17545 );