00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 #include "asterisk.h"
00096
00097 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 153114 $")
00098
00099 #include <stdio.h>
00100 #include <ctype.h>
00101 #include <string.h>
00102 #include <unistd.h>
00103 #include <sys/socket.h>
00104 #include <sys/ioctl.h>
00105 #include <net/if.h>
00106 #include <errno.h>
00107 #include <stdlib.h>
00108 #include <fcntl.h>
00109 #include <netdb.h>
00110 #include <signal.h>
00111 #include <sys/signal.h>
00112 #include <netinet/in.h>
00113 #include <netinet/in_systm.h>
00114 #include <arpa/inet.h>
00115 #include <netinet/ip.h>
00116 #include <regex.h>
00117
00118 #include "asterisk/lock.h"
00119 #include "asterisk/channel.h"
00120 #include "asterisk/config.h"
00121 #include "asterisk/logger.h"
00122 #include "asterisk/module.h"
00123 #include "asterisk/pbx.h"
00124 #include "asterisk/options.h"
00125 #include "asterisk/sched.h"
00126 #include "asterisk/io.h"
00127 #include "asterisk/rtp.h"
00128 #include "asterisk/udptl.h"
00129 #include "asterisk/acl.h"
00130 #include "asterisk/manager.h"
00131 #include "asterisk/callerid.h"
00132 #include "asterisk/cli.h"
00133 #include "asterisk/app.h"
00134 #include "asterisk/musiconhold.h"
00135 #include "asterisk/dsp.h"
00136 #include "asterisk/features.h"
00137 #include "asterisk/srv.h"
00138 #include "asterisk/astdb.h"
00139 #include "asterisk/causes.h"
00140 #include "asterisk/utils.h"
00141 #include "asterisk/file.h"
00142 #include "asterisk/astobj.h"
00143 #include "asterisk/devicestate.h"
00144 #include "asterisk/linkedlists.h"
00145 #include "asterisk/stringfields.h"
00146 #include "asterisk/monitor.h"
00147 #include "asterisk/localtime.h"
00148 #include "asterisk/abstract_jb.h"
00149 #include "asterisk/compiler.h"
00150 #include "asterisk/threadstorage.h"
00151 #include "asterisk/translate.h"
00152
00153 #ifndef FALSE
00154 #define FALSE 0
00155 #endif
00156
00157 #ifndef TRUE
00158 #define TRUE 1
00159 #endif
00160
00161 #define SIPBUFSIZE 512
00162
00163 #define XMIT_ERROR -2
00164
00165 #define VIDEO_CODEC_MASK 0x1fc0000
00166 #ifndef IPTOS_MINCOST
00167 #define IPTOS_MINCOST 0x02
00168 #endif
00169
00170 #define SIP_RESERVED ";/?:@&=+$,# "
00171
00172
00173
00174 #define DEFAULT_DEFAULT_EXPIRY 120
00175 #define DEFAULT_MIN_EXPIRY 60
00176 #define DEFAULT_MAX_EXPIRY 3600
00177 #define DEFAULT_REGISTRATION_TIMEOUT 20
00178 #define DEFAULT_MAX_FORWARDS "70"
00179
00180
00181
00182 #define EXPIRY_GUARD_SECS 15
00183 #define EXPIRY_GUARD_LIMIT 30
00184
00185 #define EXPIRY_GUARD_MIN 500
00186
00187
00188
00189 #define EXPIRY_GUARD_PCT 0.20
00190
00191 #define DEFAULT_EXPIRY 900
00192
00193 static int min_expiry = DEFAULT_MIN_EXPIRY;
00194 static int max_expiry = DEFAULT_MAX_EXPIRY;
00195 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00196 static int expiry = DEFAULT_EXPIRY;
00197
00198 #ifndef MAX
00199 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00200 #endif
00201
00202 #define CALLERID_UNKNOWN "Unknown"
00203
00204 #define DEFAULT_MAXMS 2000
00205 #define DEFAULT_FREQ_OK 60 * 1000
00206 #define DEFAULT_FREQ_NOTOK 10 * 1000
00207
00208 #define DEFAULT_RETRANS 1000
00209 #define MAX_RETRANS 6
00210 #define SIP_TRANS_TIMEOUT 32000
00211
00212
00213 #define DEFAULT_TRANS_TIMEOUT -1
00214 #define MAX_AUTHTRIES 3
00215
00216 #define SIP_MAX_HEADERS 64
00217 #define SIP_MAX_LINES 64
00218 #define SIP_MAX_PACKET 4096
00219
00220 #define SDP_MAX_RTPMAP_CODECS 32
00221
00222 #define INITIAL_CSEQ 101
00223
00224
00225 static struct ast_jb_conf default_jbconf =
00226 {
00227 .flags = 0,
00228 .max_size = -1,
00229 .resync_threshold = -1,
00230 .impl = ""
00231 };
00232 static struct ast_jb_conf global_jbconf;
00233
00234 static const char config[] = "sip.conf";
00235 static const char notify_config[] = "sip_notify.conf";
00236
00237 #define RTP 1
00238 #define NO_RTP 0
00239
00240
00241
00242
00243 enum transfermodes {
00244 TRANSFER_OPENFORALL,
00245 TRANSFER_CLOSED,
00246 };
00247
00248
00249 enum sip_result {
00250 AST_SUCCESS = 0,
00251 AST_FAILURE = -1,
00252 };
00253
00254
00255
00256
00257 enum invitestates {
00258 INV_NONE = 0,
00259 INV_CALLING = 1,
00260 INV_PROCEEDING = 2,
00261 INV_EARLY_MEDIA = 3,
00262 INV_COMPLETED = 4,
00263 INV_CONFIRMED = 5,
00264 INV_TERMINATED = 6,
00265
00266 INV_CANCELLED = 7,
00267 };
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 enum xmittype {
00278 XMIT_CRITICAL = 2,
00279
00280 XMIT_RELIABLE = 1,
00281 XMIT_UNRELIABLE = 0,
00282 };
00283
00284 enum parse_register_result {
00285 PARSE_REGISTER_FAILED,
00286 PARSE_REGISTER_UPDATE,
00287 PARSE_REGISTER_QUERY,
00288 };
00289
00290 enum subscriptiontype {
00291 NONE = 0,
00292 XPIDF_XML,
00293 DIALOG_INFO_XML,
00294 CPIM_PIDF_XML,
00295 PIDF_XML,
00296 MWI_NOTIFICATION
00297 };
00298
00299 static const struct cfsubscription_types {
00300 enum subscriptiontype type;
00301 const char * const event;
00302 const char * const mediatype;
00303 const char * const text;
00304 } subscription_types[] = {
00305 { NONE, "-", "unknown", "unknown" },
00306
00307 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00308 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00309 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00310 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00311 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00312 };
00313
00314
00315 enum sipmethod {
00316 SIP_UNKNOWN,
00317 SIP_RESPONSE,
00318 SIP_REGISTER,
00319 SIP_OPTIONS,
00320 SIP_NOTIFY,
00321 SIP_INVITE,
00322 SIP_ACK,
00323 SIP_PRACK,
00324 SIP_BYE,
00325 SIP_REFER,
00326 SIP_SUBSCRIBE,
00327 SIP_MESSAGE,
00328 SIP_UPDATE,
00329 SIP_INFO,
00330 SIP_CANCEL,
00331 SIP_PUBLISH,
00332 SIP_PING,
00333 };
00334
00335
00336
00337
00338
00339
00340 enum sip_auth_type {
00341 PROXY_AUTH,
00342 WWW_AUTH,
00343 };
00344
00345
00346 enum check_auth_result {
00347 AUTH_SUCCESSFUL = 0,
00348 AUTH_CHALLENGE_SENT = 1,
00349 AUTH_SECRET_FAILED = -1,
00350 AUTH_USERNAME_MISMATCH = -2,
00351 AUTH_NOT_FOUND = -3,
00352 AUTH_FAKE_AUTH = -4,
00353 AUTH_UNKNOWN_DOMAIN = -5,
00354 AUTH_PEER_NOT_DYNAMIC = -6,
00355 AUTH_ACL_FAILED = -7,
00356 };
00357
00358
00359 enum sipregistrystate {
00360 REG_STATE_UNREGISTERED = 0,
00361 REG_STATE_REGSENT,
00362 REG_STATE_AUTHSENT,
00363 REG_STATE_REGISTERED,
00364 REG_STATE_REJECTED,
00365 REG_STATE_TIMEOUT,
00366 REG_STATE_NOAUTH,
00367 REG_STATE_FAILED,
00368 };
00369
00370 #define CAN_NOT_CREATE_DIALOG 0
00371 #define CAN_CREATE_DIALOG 1
00372 #define CAN_CREATE_DIALOG_UNSUPPORTED_METHOD 2
00373
00374
00375 static const struct cfsip_methods {
00376 enum sipmethod id;
00377 int need_rtp;
00378 char * const text;
00379 int can_create;
00380 } sip_methods[] = {
00381 { SIP_UNKNOWN, RTP, "-UNKNOWN-", CAN_CREATE_DIALOG },
00382 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00383 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00384 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00385 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00386 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00387 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00388 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00389 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00390 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00391 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", CAN_CREATE_DIALOG },
00392 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00393 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00394 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00395 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00396 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
00397 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00398 };
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 #define SUPPORTED 1
00411 #define NOT_SUPPORTED 0
00412
00413 #define SIP_OPT_REPLACES (1 << 0)
00414 #define SIP_OPT_100REL (1 << 1)
00415 #define SIP_OPT_TIMER (1 << 2)
00416 #define SIP_OPT_EARLY_SESSION (1 << 3)
00417 #define SIP_OPT_JOIN (1 << 4)
00418 #define SIP_OPT_PATH (1 << 5)
00419 #define SIP_OPT_PREF (1 << 6)
00420 #define SIP_OPT_PRECONDITION (1 << 7)
00421 #define SIP_OPT_PRIVACY (1 << 8)
00422 #define SIP_OPT_SDP_ANAT (1 << 9)
00423 #define SIP_OPT_SEC_AGREE (1 << 10)
00424 #define SIP_OPT_EVENTLIST (1 << 11)
00425 #define SIP_OPT_GRUU (1 << 12)
00426 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00427 #define SIP_OPT_NOREFERSUB (1 << 14)
00428 #define SIP_OPT_HISTINFO (1 << 15)
00429 #define SIP_OPT_RESPRIORITY (1 << 16)
00430
00431
00432
00433 static const struct cfsip_options {
00434 int id;
00435 int supported;
00436 char * const text;
00437 } sip_options[] = {
00438
00439 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00440
00441 { SIP_OPT_REPLACES, SUPPORTED, "replace" },
00442
00443 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00444
00445 { SIP_OPT_TIMER, NOT_SUPPORTED, "timer" },
00446
00447 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00448
00449 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00450
00451 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00452
00453 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00454
00455 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00456
00457 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00458
00459 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00460
00461 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00462
00463 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00464
00465 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00466
00467 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "tdialog" },
00468
00469 { SIP_OPT_NOREFERSUB, NOT_SUPPORTED, "norefersub" },
00470
00471 { SIP_OPT_HISTINFO, NOT_SUPPORTED, "histinfo" },
00472
00473 { SIP_OPT_RESPRIORITY, NOT_SUPPORTED, "resource-priority" },
00474 };
00475
00476
00477
00478 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
00479
00480
00481 #define SUPPORTED_EXTENSIONS "replaces"
00482
00483
00484 #define STANDARD_SIP_PORT 5060
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 #define DEFAULT_CONTEXT "default"
00498 #define DEFAULT_MOHINTERPRET "default"
00499 #define DEFAULT_MOHSUGGEST ""
00500 #define DEFAULT_VMEXTEN "asterisk"
00501 #define DEFAULT_CALLERID "asterisk"
00502 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00503 #define DEFAULT_MWITIME 10
00504 #define DEFAULT_ALLOWGUEST TRUE
00505 #define DEFAULT_SRVLOOKUP TRUE
00506 #define DEFAULT_COMPACTHEADERS FALSE
00507 #define DEFAULT_TOS_SIP 0
00508 #define DEFAULT_TOS_AUDIO 0
00509 #define DEFAULT_TOS_VIDEO 0
00510 #define DEFAULT_ALLOW_EXT_DOM TRUE
00511 #define DEFAULT_REALM "asterisk"
00512 #define DEFAULT_NOTIFYRINGING TRUE
00513 #define DEFAULT_PEDANTIC FALSE
00514 #define DEFAULT_AUTOCREATEPEER FALSE
00515 #define DEFAULT_QUALIFY FALSE
00516 #define DEFAULT_T1MIN 100
00517 #define DEFAULT_MAX_CALL_BITRATE (384)
00518 #ifndef DEFAULT_USERAGENT
00519 #define DEFAULT_USERAGENT "Asterisk PBX"
00520 #endif
00521
00522
00523
00524
00525 static char default_context[AST_MAX_CONTEXT];
00526 static char default_subscribecontext[AST_MAX_CONTEXT];
00527 static char default_language[MAX_LANGUAGE];
00528 static char default_callerid[AST_MAX_EXTENSION];
00529 static char default_fromdomain[AST_MAX_EXTENSION];
00530 static char default_notifymime[AST_MAX_EXTENSION];
00531 static int default_qualify;
00532 static char default_vmexten[AST_MAX_EXTENSION];
00533 static char default_mohinterpret[MAX_MUSICCLASS];
00534 static char default_mohsuggest[MAX_MUSICCLASS];
00535
00536 static int default_maxcallbitrate;
00537 static struct ast_codec_pref default_prefs;
00538
00539
00540 static int global_directrtpsetup;
00541 static int global_limitonpeers;
00542 static int global_rtautoclear;
00543 static int global_notifyringing;
00544 static int global_notifyhold;
00545 static int global_alwaysauthreject;
00546 static int srvlookup;
00547 static int pedanticsipchecking;
00548 static int autocreatepeer;
00549 static int global_relaxdtmf;
00550 static int global_rtptimeout;
00551 static int global_rtpholdtimeout;
00552 static int global_rtpkeepalive;
00553 static int global_reg_timeout;
00554 static int global_regattempts_max;
00555 static int global_allowguest;
00556 static int global_allowsubscribe;
00557
00558 static int global_mwitime;
00559 static unsigned int global_tos_sip;
00560 static unsigned int global_tos_audio;
00561 static unsigned int global_tos_video;
00562 static int compactheaders;
00563 static int recordhistory;
00564 static int dumphistory;
00565 static char global_realm[MAXHOSTNAMELEN];
00566 static char global_regcontext[AST_MAX_CONTEXT];
00567 static char global_useragent[AST_MAX_EXTENSION];
00568 static int allow_external_domains;
00569 static int global_callevents;
00570 static int global_t1min;
00571 static int global_autoframing;
00572 static enum transfermodes global_allowtransfer;
00573
00574 static int global_matchexterniplocally;
00575
00576
00577 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00578
00579
00580 static struct ast_ha *global_contact_ha = NULL;
00581 static int global_dynamic_exclude_static = 0;
00582
00583
00584 static int suserobjs = 0;
00585 static int ruserobjs = 0;
00586 static int speerobjs = 0;
00587 static int rpeerobjs = 0;
00588 static int apeerobjs = 0;
00589 static int regobjs = 0;
00590
00591 static struct ast_flags global_flags[2] = {{0}};
00592
00593
00594 AST_MUTEX_DEFINE_STATIC(iflock);
00595
00596
00597
00598 AST_MUTEX_DEFINE_STATIC(netlock);
00599
00600 AST_MUTEX_DEFINE_STATIC(monlock);
00601
00602 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00603
00604
00605
00606 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00607
00608 static int sip_reloading = FALSE;
00609 static enum channelreloadreason sip_reloadreason;
00610
00611 static struct sched_context *sched;
00612 static struct io_context *io;
00613 static int *sipsock_read_id;
00614
00615 #define DEC_CALL_LIMIT 0
00616 #define INC_CALL_LIMIT 1
00617 #define DEC_CALL_RINGING 2
00618 #define INC_CALL_RINGING 3
00619
00620
00621 struct sip_request {
00622 char *rlPart1;
00623 char *rlPart2;
00624 int len;
00625 int headers;
00626 int method;
00627 int lines;
00628 unsigned int flags;
00629 char *header[SIP_MAX_HEADERS];
00630 char *line[SIP_MAX_LINES];
00631 char data[SIP_MAX_PACKET];
00632 unsigned int sdp_start;
00633 unsigned int sdp_end;
00634 };
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 struct sip_dual {
00657 struct ast_channel *chan1;
00658 struct ast_channel *chan2;
00659 struct sip_request req;
00660 int seqno;
00661 };
00662
00663 struct sip_pkt;
00664
00665
00666 struct sip_invite_param {
00667 const char *distinctive_ring;
00668 int addsipheaders;
00669 const char *uri_options;
00670 const char *vxml_url;
00671 char *auth;
00672 char *authheader;
00673 enum sip_auth_type auth_type;
00674 const char *replaces;
00675 int transfer;
00676 };
00677
00678
00679 struct sip_route {
00680 struct sip_route *next;
00681 char hop[0];
00682 };
00683
00684
00685 enum domain_mode {
00686 SIP_DOMAIN_AUTO,
00687 SIP_DOMAIN_CONFIG,
00688 };
00689
00690
00691
00692
00693
00694 struct domain {
00695 char domain[MAXHOSTNAMELEN];
00696 char context[AST_MAX_EXTENSION];
00697 enum domain_mode mode;
00698 AST_LIST_ENTRY(domain) list;
00699 };
00700
00701 static AST_LIST_HEAD_STATIC(domain_list, domain);
00702
00703
00704
00705 struct sip_history {
00706 AST_LIST_ENTRY(sip_history) list;
00707 char event[0];
00708 };
00709
00710 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00711
00712
00713 struct sip_auth {
00714 char realm[AST_MAX_EXTENSION];
00715 char username[256];
00716 char secret[256];
00717 char md5secret[256];
00718 struct sip_auth *next;
00719 };
00720
00721
00722 #define SIP_ALREADYGONE (1 << 0)
00723 #define SIP_NEEDDESTROY (1 << 1)
00724 #define SIP_NOVIDEO (1 << 2)
00725 #define SIP_RINGING (1 << 3)
00726 #define SIP_PROGRESS_SENT (1 << 4)
00727 #define SIP_NEEDREINVITE (1 << 5)
00728 #define SIP_PENDINGBYE (1 << 6)
00729 #define SIP_GOTREFER (1 << 7)
00730 #define SIP_PROMISCREDIR (1 << 8)
00731 #define SIP_TRUSTRPID (1 << 9)
00732 #define SIP_USEREQPHONE (1 << 10)
00733 #define SIP_REALTIME (1 << 11)
00734 #define SIP_USECLIENTCODE (1 << 12)
00735 #define SIP_OUTGOING (1 << 13)
00736 #define SIP_FREE_BIT (1 << 14)
00737 #define SIP_DEFER_BYE_ON_TRANSFER (1 << 15)
00738 #define SIP_DTMF (3 <<