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 #include <stdio.h>
00033 #include <ctype.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 #include <sys/socket.h>
00037 #include <sys/ioctl.h>
00038 #include <net/if.h>
00039 #include <errno.h>
00040 #include <stdlib.h>
00041 #include <fcntl.h>
00042 #include <netdb.h>
00043 #include <signal.h>
00044 #include <sys/signal.h>
00045 #include <netinet/in.h>
00046 #include <netinet/in_systm.h>
00047 #include <arpa/inet.h>
00048 #include <netinet/ip.h>
00049 #include <regex.h>
00050
00051 #include "asterisk.h"
00052
00053 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 42535 $")
00054
00055 #include "asterisk/lock.h"
00056 #include "asterisk/channel.h"
00057 #include "asterisk/config.h"
00058 #include "asterisk/logger.h"
00059 #include "asterisk/module.h"
00060 #include "asterisk/pbx.h"
00061 #include "asterisk/options.h"
00062 #include "asterisk/lock.h"
00063 #include "asterisk/sched.h"
00064 #include "asterisk/io.h"
00065 #include "asterisk/rtp.h"
00066 #include "asterisk/acl.h"
00067 #include "asterisk/manager.h"
00068 #include "asterisk/callerid.h"
00069 #include "asterisk/cli.h"
00070 #include "asterisk/app.h"
00071 #include "asterisk/musiconhold.h"
00072 #include "asterisk/dsp.h"
00073 #include "asterisk/features.h"
00074 #include "asterisk/acl.h"
00075 #include "asterisk/srv.h"
00076 #include "asterisk/astdb.h"
00077 #include "asterisk/causes.h"
00078 #include "asterisk/utils.h"
00079 #include "asterisk/file.h"
00080 #include "asterisk/astobj.h"
00081 #include "asterisk/dnsmgr.h"
00082 #include "asterisk/devicestate.h"
00083 #include "asterisk/linkedlists.h"
00084
00085 #ifdef OSP_SUPPORT
00086 #include "asterisk/astosp.h"
00087 #endif
00088
00089 #ifndef DEFAULT_USERAGENT
00090 #define DEFAULT_USERAGENT "Asterisk PBX"
00091 #endif
00092
00093 #define VIDEO_CODEC_MASK 0x1fc0000
00094 #ifndef IPTOS_MINCOST
00095 #define IPTOS_MINCOST 0x02
00096 #endif
00097
00098
00099
00100 #define SIPDUMPER
00101 #define DEFAULT_DEFAULT_EXPIRY 120
00102 #define DEFAULT_MAX_EXPIRY 3600
00103 #define DEFAULT_REGISTRATION_TIMEOUT 20
00104 #define DEFAULT_MAX_FORWARDS "70"
00105
00106
00107
00108 #define EXPIRY_GUARD_SECS 15
00109 #define EXPIRY_GUARD_LIMIT 30
00110
00111 #define EXPIRY_GUARD_MIN 500
00112
00113
00114
00115 #define EXPIRY_GUARD_PCT 0.20
00116
00117
00118 #define SIP_LEN_CONTACT 256
00119
00120 static int max_expiry = DEFAULT_MAX_EXPIRY;
00121 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00122
00123 #ifndef MAX
00124 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00125 #endif
00126
00127 #define CALLERID_UNKNOWN "Unknown"
00128
00129
00130
00131 #define DEFAULT_MAXMS 2000
00132 #define DEFAULT_FREQ_OK 60 * 1000
00133 #define DEFAULT_FREQ_NOTOK 10 * 1000
00134
00135 #define DEFAULT_RETRANS 1000
00136
00137 #define MAX_RETRANS 6
00138 #define MAX_AUTHTRIES 3
00139
00140
00141 #define DEBUG_READ 0
00142 #define DEBUG_SEND 1
00143
00144 static const char desc[] = "Session Initiation Protocol (SIP)";
00145 static const char channeltype[] = "SIP";
00146 static const char config[] = "sip.conf";
00147 static const char notify_config[] = "sip_notify.conf";
00148
00149 #define RTP 1
00150 #define NO_RTP 0
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 enum subscriptiontype {
00161 NONE = 0,
00162 TIMEOUT,
00163 XPIDF_XML,
00164 DIALOG_INFO_XML,
00165 CPIM_PIDF_XML,
00166 PIDF_XML
00167 };
00168
00169 static const struct cfsubscription_types {
00170 enum subscriptiontype type;
00171 const char * const event;
00172 const char * const mediatype;
00173 const char * const text;
00174 } subscription_types[] = {
00175 { NONE, "-", "unknown", "unknown" },
00176
00177 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00178 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00179 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00180 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" }
00181 };
00182
00183 enum sipmethod {
00184 SIP_UNKNOWN,
00185 SIP_RESPONSE,
00186 SIP_REGISTER,
00187 SIP_OPTIONS,
00188 SIP_NOTIFY,
00189 SIP_INVITE,
00190 SIP_ACK,
00191 SIP_PRACK,
00192 SIP_BYE,
00193 SIP_REFER,
00194 SIP_SUBSCRIBE,
00195 SIP_MESSAGE,
00196 SIP_UPDATE,
00197 SIP_INFO,
00198 SIP_CANCEL,
00199 SIP_PUBLISH,
00200 } sip_method_list;
00201
00202 enum sip_auth_type {
00203 PROXY_AUTH,
00204 WWW_AUTH,
00205 };
00206
00207
00208 static const struct cfsip_methods {
00209 enum sipmethod id;
00210 int need_rtp;
00211 char * const text;
00212 } sip_methods[] = {
00213 { SIP_UNKNOWN, RTP, "-UNKNOWN-" },
00214 { SIP_RESPONSE, NO_RTP, "SIP/2.0" },
00215 { SIP_REGISTER, NO_RTP, "REGISTER" },
00216 { SIP_OPTIONS, NO_RTP, "OPTIONS" },
00217 { SIP_NOTIFY, NO_RTP, "NOTIFY" },
00218 { SIP_INVITE, RTP, "INVITE" },
00219 { SIP_ACK, NO_RTP, "ACK" },
00220 { SIP_PRACK, NO_RTP, "PRACK" },
00221 { SIP_BYE, NO_RTP, "BYE" },
00222 { SIP_REFER, NO_RTP, "REFER" },
00223 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE" },
00224 { SIP_MESSAGE, NO_RTP, "MESSAGE" },
00225 { SIP_UPDATE, NO_RTP, "UPDATE" },
00226 { SIP_INFO, NO_RTP, "INFO" },
00227 { SIP_CANCEL, NO_RTP, "CANCEL" },
00228 { SIP_PUBLISH, NO_RTP, "PUBLISH" }
00229 };
00230
00231
00232 static const struct cfalias {
00233 char * const fullname;
00234 char * const shortname;
00235 } aliases[] = {
00236 { "Content-Type", "c" },
00237 { "Content-Encoding", "e" },
00238 { "From", "f" },
00239 { "Call-ID", "i" },
00240 { "Contact", "m" },
00241 { "Content-Length", "l" },
00242 { "Subject", "s" },
00243 { "To", "t" },
00244 { "Supported", "k" },
00245 { "Refer-To", "r" },
00246 { "Referred-By", "b" },
00247 { "Allow-Events", "u" },
00248 { "Event", "o" },
00249 { "Via", "v" },
00250 { "Accept-Contact", "a" },
00251 { "Reject-Contact", "j" },
00252 { "Request-Disposition", "d" },
00253 { "Session-Expires", "x" },
00254 };
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 #define SUPPORTED 1
00267 #define NOT_SUPPORTED 0
00268
00269 #define SIP_OPT_REPLACES (1 << 0)
00270 #define SIP_OPT_100REL (1 << 1)
00271 #define SIP_OPT_TIMER (1 << 2)
00272 #define SIP_OPT_EARLY_SESSION (1 << 3)
00273 #define SIP_OPT_JOIN (1 << 4)
00274 #define SIP_OPT_PATH (1 << 5)
00275 #define SIP_OPT_PREF (1 << 6)
00276 #define SIP_OPT_PRECONDITION (1 << 7)
00277 #define SIP_OPT_PRIVACY (1 << 8)
00278 #define SIP_OPT_SDP_ANAT (1 << 9)
00279 #define SIP_OPT_SEC_AGREE (1 << 10)
00280 #define SIP_OPT_EVENTLIST (1 << 11)
00281 #define SIP_OPT_GRUU (1 << 12)
00282 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00283
00284
00285
00286 static const struct cfsip_options {
00287 int id;
00288 int supported;
00289 char * const text;
00290 } sip_options[] = {
00291
00292 { SIP_OPT_REPLACES, SUPPORTED, "replaces" },
00293
00294 { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" },
00295
00296 { SIP_OPT_TIMER, NOT_SUPPORTED, "timer" },
00297
00298 { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED, "early-session" },
00299
00300 { SIP_OPT_JOIN, NOT_SUPPORTED, "join" },
00301
00302 { SIP_OPT_PATH, NOT_SUPPORTED, "path" },
00303
00304 { SIP_OPT_PREF, NOT_SUPPORTED, "pref" },
00305
00306 { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00307
00308 { SIP_OPT_PRIVACY, NOT_SUPPORTED, "privacy" },
00309
00310 { SIP_OPT_SDP_ANAT, NOT_SUPPORTED, "sdp-anat" },
00311
00312 { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00313
00314 { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00315
00316 { SIP_OPT_GRUU, NOT_SUPPORTED, "gruu" },
00317
00318 { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "target-dialog" },
00319 };
00320
00321
00322
00323 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
00324
00325
00326 #define SUPPORTED_EXTENSIONS "replaces"
00327
00328 #define DEFAULT_SIP_PORT 5060
00329 #define SIP_MAX_PACKET 4096
00330
00331 static char default_useragent[AST_MAX_EXTENSION] = DEFAULT_USERAGENT;
00332
00333 #define DEFAULT_CONTEXT "default"
00334 static char default_context[AST_MAX_CONTEXT] = DEFAULT_CONTEXT;
00335 static char default_subscribecontext[AST_MAX_CONTEXT];
00336
00337 #define DEFAULT_VMEXTEN "asterisk"
00338 static char global_vmexten[AST_MAX_EXTENSION] = DEFAULT_VMEXTEN;
00339
00340 static char default_language[MAX_LANGUAGE] = "";
00341
00342 #define DEFAULT_CALLERID "asterisk"
00343 static char default_callerid[AST_MAX_EXTENSION] = DEFAULT_CALLERID;
00344
00345 static char default_fromdomain[AST_MAX_EXTENSION] = "";
00346
00347 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00348 static char default_notifymime[AST_MAX_EXTENSION] = DEFAULT_NOTIFYMIME;
00349
00350 static int global_notifyringing = 1;
00351
00352 static int global_alwaysauthreject = 0;
00353
00354 static int default_qualify = 0;
00355
00356 static struct ast_flags global_flags = {0};
00357 static struct ast_flags global_flags_page2 = {0};
00358
00359 static int srvlookup = 0;
00360
00361 static int pedanticsipchecking = 0;
00362
00363 static int autocreatepeer = 0;
00364
00365 static int relaxdtmf = 0;
00366
00367 static int global_rtptimeout = 0;
00368
00369 static int global_rtpholdtimeout = 0;
00370
00371 static int global_rtpkeepalive = 0;
00372
00373 static int global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
00374 static int global_regattempts_max = 0;
00375
00376
00377 static int suserobjs = 0;
00378 static int ruserobjs = 0;
00379 static int speerobjs = 0;
00380 static int rpeerobjs = 0;
00381 static int apeerobjs = 0;
00382 static int regobjs = 0;
00383
00384 static int global_allowguest = 1;
00385
00386 #define DEFAULT_MWITIME 10
00387 static int global_mwitime = DEFAULT_MWITIME;
00388
00389 static int usecnt =0;
00390 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00391
00392 AST_MUTEX_DEFINE_STATIC(rand_lock);
00393
00394
00395 AST_MUTEX_DEFINE_STATIC(iflock);
00396
00397
00398
00399 AST_MUTEX_DEFINE_STATIC(netlock);
00400
00401 AST_MUTEX_DEFINE_STATIC(monlock);
00402
00403
00404
00405 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00406
00407 static int restart_monitor(void);
00408
00409
00410 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00411 static int noncodeccapability = AST_RTP_DTMF;
00412
00413 static struct in_addr __ourip;
00414 static struct sockaddr_in outboundproxyip;
00415 static int ourport;
00416
00417 #define SIP_DEBUG_CONFIG 1 << 0
00418 #define SIP_DEBUG_CONSOLE 1 << 1
00419 static int sipdebug = 0;
00420 static struct sockaddr_in debugaddr;
00421
00422 static int tos = 0;
00423
00424 static int videosupport = 0;
00425
00426 static int compactheaders = 0;
00427
00428 static int recordhistory = 0;
00429 static int dumphistory = 0;
00430
00431 static char global_musicclass[MAX_MUSICCLASS] = "";
00432 #define DEFAULT_REALM "asterisk"
00433 static char global_realm[MAXHOSTNAMELEN] = DEFAULT_REALM;
00434 static char regcontext[AST_MAX_CONTEXT] = "";
00435
00436 #define DEFAULT_EXPIRY 900
00437 static int expiry = DEFAULT_EXPIRY;
00438
00439 static struct sched_context *sched;
00440 static struct io_context *io;
00441
00442 #define SIP_MAX_HEADERS 64
00443 #define SIP_MAX_LINES 64
00444
00445 #define DEC_CALL_LIMIT 0
00446 #define INC_CALL_LIMIT 1
00447
00448 static struct ast_codec_pref prefs;
00449
00450
00451
00452 struct sip_request {
00453 char *rlPart1;
00454 char *rlPart2;
00455 int len;
00456 int headers;
00457 int method;
00458 char *header[SIP_MAX_HEADERS];
00459 int lines;
00460 char *line[SIP_MAX_LINES];
00461 char data[SIP_MAX_PACKET];
00462 int debug;
00463 unsigned int flags;
00464 unsigned int sdp_start;
00465 unsigned int sdp_end;
00466 };
00467
00468 struct sip_pkt;
00469
00470
00471 struct sip_invite_param {
00472 char *distinctive_ring;
00473 char *osptoken;
00474 int addsipheaders;
00475 char *uri_options;
00476 char *vxml_url;
00477 char *auth;
00478 char *authheader;
00479 enum sip_auth_type auth_type;
00480 };
00481
00482 struct sip_route {
00483 struct sip_route *next;
00484 char hop[0];
00485 };
00486
00487 enum domain_mode {
00488 SIP_DOMAIN_AUTO,
00489 SIP_DOMAIN_CONFIG,
00490 };
00491
00492 struct domain {
00493 char domain[MAXHOSTNAMELEN];
00494 char context[AST_MAX_EXTENSION];
00495 enum domain_mode mode;
00496 AST_LIST_ENTRY(domain) list;
00497 };
00498
00499 static AST_LIST_HEAD_STATIC(domain_list, domain);
00500
00501 int allow_external_domains;
00502
00503
00504 struct sip_history {
00505 char event[80];
00506 struct sip_history *next;
00507 };
00508
00509
00510 struct sip_auth {
00511 char realm[AST_MAX_EXTENSION];
00512 char username[256];
00513 char secret[256];
00514 char md5secret[256];
00515 struct sip_auth *next;
00516 };
00517
00518 #define SIP_ALREADYGONE (1 << 0)
00519 #define SIP_NEEDDESTROY (1 << 1)
00520 #define SIP_NOVIDEO (1 << 2)
00521 #define SIP_RINGING (1 << 3)
00522 #define SIP_PROGRESS_SENT (1 << 4)
00523 #define SIP_NEEDREINVITE (1 << 5)
00524 #define SIP_PENDINGBYE (1 << 6)
00525 #define SIP_GOTREFER (1 << 7)
00526 #define SIP_PROMISCREDIR (1 << 8)
00527 #define SIP_TRUSTRPID (1 << 9)
00528 #define SIP_USEREQPHONE (1 << 10)
00529 #define SIP_REALTIME (1 << 11)
00530 #define SIP_USECLIENTCODE (1 << 12)
00531 #define SIP_OUTGOING (1 << 13)
00532 #define SIP_SELFDESTRUCT (1 << 14)
00533 #define SIP_CAN_BYE (1 << 15)
00534
00535 #define SIP_DTMF (3 << 16)
00536 #define SIP_DTMF_RFC2833 (0 << 16)
00537 #define SIP_DTMF_INBAND (1 << 16)
00538 #define SIP_DTMF_INFO (2 << 16)
00539 #define SIP_DTMF_AUTO (3 << 16)
00540
00541 #define SIP_NAT (3 << 18)
00542 #define SIP_NAT_NEVER (0 << 18)
00543 #define SIP_NAT_RFC3581 (1 << 18)
00544 #define SIP_NAT_ROUTE (2 << 18)
00545 #define SIP_NAT_ALWAYS (3 << 18)
00546
00547 #define SIP_REINVITE (3 << 20)
00548 #define SIP_CAN_REINVITE (1 << 20)
00549 #define SIP_REINVITE_UPDATE (2 << 20)
00550
00551 #define SIP_INSECURE_PORT (1 << 22)
00552 #define SIP_INSECURE_INVITE (1 << 23)
00553
00554 #define SIP_PROG_INBAND (3 << 24)
00555 #define SIP_PROG_INBAND_NEVER (0 << 24)
00556 #define SIP_PROG_INBAND_NO (1 << 24)
00557 #define SIP_PROG_INBAND_YES (2 << 24)
00558
00559 #define SIP_OSPAUTH (3 << 26)
00560 #define SIP_OSPAUTH_NO (0 << 26)
00561 #define SIP_OSPAUTH_GATEWAY (1 << 26)
00562 #define SIP_OSPAUTH_PROXY (2 << 26)
00563 #define SIP_OSPAUTH_EXCLUSIVE (3 << 26)
00564
00565 #define SIP_CALL_ONHOLD (1 << 28)
00566 #define SIP_CALL_LIMIT (1 << 29)
00567
00568 #define SIP_SENDRPID (1 << 30)
00569
00570 #define SIP_INC_COUNT (1 << 31)
00571
00572 #define SIP_FLAGS_TO_COPY \
00573 (SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
00574 SIP_PROG_INBAND | SIP_OSPAUTH | SIP_USECLIENTCODE | SIP_NAT | \
00575 SIP_USEREQPHONE | SIP_INSECURE_PORT | SIP_INSECURE_INVITE)
00576
00577
00578 #define SIP_PAGE2_RTCACHEFRIENDS (1 << 0)
00579 #define SIP_PAGE2_RTUPDATE (1 << 1)
00580 #define SIP_PAGE2_RTAUTOCLEAR (1 << 2)
00581 #define SIP_PAGE2_IGNOREREGEXPIRE (1 << 3)
00582 #define SIP_PAGE2_RT_FROMCONTACT (1 << 4)
00583 #define SIP_PAGE2_DYNAMIC (1 << 5)
00584
00585
00586 #define SIP_PKT_DEBUG (1 << 0)
00587 #define SIP_PKT_WITH_TOTAG (1 << 1)
00588
00589 static int global_rtautoclear;
00590
00591
00592 static struct sip_pvt {
00593 ast_mutex_t lock;
00594 int method;
00595 char callid[128];
00596 char randdata[80];
00597 struct ast_codec_pref prefs;
00598 unsigned int ocseq;
00599 unsigned int icseq;
00600 ast_group_t callgroup;
00601 ast_group_t pickupgroup;
00602 int lastinvite;
00603 unsigned int flags;
00604 int timer_t1;
00605 unsigned int sipoptions;
00606 int capability;
00607 int jointcapability;
00608 int peercapability;
00609 int prefcodec;
00610 int noncodeccapability;
00611 int callingpres;
00612 int authtries;
00613 int expiry;
00614 int branch;
00615 char tag[11];
00616 int sessionid;
00617 int sessionversion;
00618 struct sockaddr_in sa;
00619 struct sockaddr_in redirip;
00620 struct sockaddr_in vredirip;
00621 int redircodecs;
00622 struct sockaddr_in recv;
00623 struct in_addr ourip;
00624 struct ast_channel *owner;
00625 char exten[AST_MAX_EXTENSION];
00626 char refer_to[AST_MAX_EXTENSION];
00627 char referred_by[AST_MAX_EXTENSION];
00628 char refer_contact[SIP_LEN_CONTACT];
00629 struct sip_pvt *refer_call;
00630 struct sip_route *route;
00631 int route_persistant;
00632 char from[256];
00633 char useragent[256];
00634 char context[AST_MAX_CONTEXT];
00635 char subscribecontext[AST_MAX_CONTEXT];
00636 char fromdomain[MAXHOSTNAMELEN];
00637 char fromuser[AST_MAX_EXTENSION];
00638 char fromname[AST_MAX_EXTENSION];
00639 char tohost[MAXHOSTNAMELEN];
00640 char language[MAX_LANGUAGE];
00641 char musicclass[MAX_MUSICCLASS];
00642 char rdnis[256];
00643 char theirtag[256];
00644 char username[256];
00645 char peername[256];
00646 char authname[256];
00647 char uri[256];
00648 char okcontacturi[SIP_LEN_CONTACT];
00649 char peersecret[256];
00650 char peermd5secret[256];
00651 struct sip_auth *peerauth;
00652 char cid_num[256];
00653 char cid_name[256];
00654 char via[256];
00655 char fullcontact[SIP_LEN_CONTACT];
00656 char accountcode[AST_MAX_ACCOUNT_CODE];
00657 char our_contact[SIP_LEN_CONTACT];
00658 char *rpid;
00659 char *rpid_from;
00660 char realm[MAXHOSTNAMELEN];
00661 char nonce[256];
00662 int noncecount;
00663 char opaque[256];
00664 char qop[80];
00665 char domain[MAXHOSTNAMELEN];
00666 char lastmsg[256];
00667 int amaflags;
00668 int pendinginvite;
00669 #ifdef OSP_SUPPORT
00670 int osphandle;
00671 time_t ospstart;
00672 unsigned int osptimelimit;
00673 #endif
00674 struct sip_request initreq;
00675
00676 int maxtime;
00677 int initid;
00678 int autokillid;
00679 time_t lastrtprx;
00680 time_t lastrtptx;
00681 int rtptimeout;
00682 int rtpholdtimeout;
00683 int rtpkeepalive;
00684 enum subscriptiontype subscribed;
00685 int stateid;
00686 int laststate;
00687 int dialogver;
00688
00689 struct ast_dsp *vad;
00690
00691 struct sip_peer *peerpoke;
00692 struct sip_registry *registry;
00693 struct ast_rtp *rtp;
00694 struct ast_rtp *vrtp;
00695 struct sip_pkt *packets;
00696 struct sip_history *history;
00697 struct ast_variable *chanvars;
00698 struct sip_pvt *next;
00699 struct sip_invite_param *options;
00700 } *iflist = NULL;
00701
00702 #define FLAG_RESPONSE (1 << 0)
00703 #define FLAG_FATAL (1 << 1)
00704
00705
00706 struct sip_pkt {
00707 struct sip_pkt *next;
00708 int retrans;
00709 int method;
00710 int seqno;
00711 unsigned int flags;
00712 struct sip_pvt *owner;
00713 int retransid;
00714 int timer_a;
00715 int timer_t1;
00716 int packetlen;
00717 char data[0];
00718 };
00719
00720
00721 struct sip_user {
00722
00723 ASTOBJ_COMPONENTS(struct sip_user);
00724 char secret[80];
00725 char md5secret[80];
00726 char context[AST_MAX_CONTEXT];
00727 char subscribecontext[AST_MAX_CONTEXT];
00728 char cid_num[80];
00729 char cid_name[80];
00730 char accountcode[AST_MAX_ACCOUNT_CODE];
00731 char language[MAX_LANGUAGE];
00732 char musicclass[MAX_MUSICCLASS];
00733 char useragent[256];
00734 struct ast_codec_pref prefs;
00735 ast_group_t callgroup;
00736 ast_group_t pickupgroup;
00737 unsigned int flags;
00738 unsigned int sipoptions;
00739 struct ast_flags flags_page2;
00740 int amaflags;
00741 int callingpres;
00742 int capability;
00743 int inUse;
00744 int call_limit;
00745 struct ast_ha *ha;
00746 struct ast_variable *chanvars;
00747 };
00748
00749
00750 struct sip_peer {
00751 ASTOBJ_COMPONENTS(struct sip_peer);
00752
00753 char secret[80];
00754 char md5secret[80];
00755 struct sip_auth *auth;
00756 char context[AST_MAX_CONTEXT];
00757 char subscribecontext[AST_MAX_CONTEXT];
00758 char username[80];
00759 char accountcode[AST_MAX_ACCOUNT_CODE];
00760 int amaflags;
00761 char tohost[MAXHOSTNAMELEN];
00762 char regexten[AST_MAX_EXTENSION];
00763 char fromuser[80];
00764 char fromdomain[MAXHOSTNAMELEN];
00765 char fullcontact[SIP_LEN_CONTACT];
00766 char cid_num[80];
00767 char cid_name[80];
00768 int callingpres;
00769 int inUse;
00770 int call_limit;
00771 char vmexten[AST_MAX_EXTENSION];
00772 char mailbox[AST_MAX_EXTENSION];
00773 char language[MAX_LANGUAGE];
00774 char musicclass[MAX_MUSICCLASS];
00775 char useragent[256];
00776 struct ast_codec_pref prefs;
00777 int lastmsgssent;
00778 time_t lastmsgcheck;
00779 unsigned int flags;
00780 unsigned int sipoptions;
00781 struct ast_flags flags_page2;
00782 int expire;
00783 int capability;
00784 int rtptimeout;
00785 int rtpholdtimeout;
00786 int rtpkeepalive;
00787 ast_group_t callgroup;
00788 ast_group_t pickupgroup;
00789 struct ast_dnsmgr_entry *dnsmgr;
00790 struct sockaddr_in addr;
00791
00792
00793 struct sip_pvt *call;
00794 int pokeexpire;
00795 int lastms;
00796 int maxms;
00797 struct timeval ps;
00798
00799 struct sockaddr_in defaddr;
00800 struct ast_ha *ha;
00801 struct ast_variable *chanvars;
00802 int lastmsg;
00803 };
00804
00805 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00806 static int sip_reloading = 0;
00807
00808
00809 #define REG_STATE_UNREGISTERED 0
00810 #define REG_STATE_REGSENT 1
00811 #define REG_STATE_AUTHSENT 2
00812 #define REG_STATE_REGISTERED 3
00813 #define REG_STATE_REJECTED 4
00814 #define REG_STATE_TIMEOUT 5
00815 #define REG_STATE_NOAUTH 6
00816 #define REG_STATE_FAILED 7
00817
00818
00819
00820 struct sip_registry {
00821 ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
00822 int portno;
00823 char username[80];
00824 char authuser[80];
00825 char hostname[MAXHOSTNAMELEN];
00826 char secret[80];
00827 char md5secret[80];
00828 char contact[SIP_LEN_CONTACT];
00829 char random[80];
00830 int expire;
00831 int regattempts;
00832 int timeout;
00833 int refresh;
00834 struct sip_pvt *call;
00835 int regstate;
00836 int callid_valid;
00837 char callid[128];
00838 unsigned int ocseq;
00839 struct sockaddr_in us;
00840
00841
00842 char realm[MAXHOSTNAMELEN];
00843 char nonce[256];
00844 char domain[MAXHOSTNAMELEN];
00845 char opaque[256];
00846 char qop[80];
00847 int noncecount;
00848
00849 char lastmsg[256];
00850 };
00851
00852
00853 static struct ast_user_list {
00854 ASTOBJ_CONTAINER_COMPONENTS(struct sip_user);
00855 } userl;
00856
00857
00858 static struct ast_peer_list {
00859 ASTOBJ_CONTAINER_COMPONENTS(struct sip_peer);
00860 } peerl;
00861
00862
00863 static struct ast_register_list {
00864 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
00865 int recheck;
00866 } regl;
00867
00868
00869 static int __sip_do_register(struct sip_registry *r);
00870
00871 static int sipsock = -1;
00872
00873
00874 static struct sockaddr_in bindaddr = { 0, };
00875 static struct sockaddr_in externip;
00876 static char externhost[MAXHOSTNAMELEN] = "";
00877 static time_t externexpire = 0;
00878 static int externrefresh = 10;
00879 static struct ast_ha *localaddr;
00880
00881
00882 struct ast_config *notify_types;
00883
00884 static struct sip_auth *authl;
00885
00886
00887 static int transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req);
00888 static int transmit_response_with_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
00889 static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported);
00890 static int transmit_response_with_auth(struct sip_pvt *p, char *msg, struct sip_request *req, char *rand, int reliable, char *header, int stale);
00891 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, int reliable, int newbranch);
00892 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int inc, int reliable, int newbranch);
00893 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sendsdp, int init);
00894 static int transmit_reinvite_with_sdp(struct sip_pvt *p);
00895 static int transmit_info_with_digit(struct sip_pvt *p, char digit);
00896 static int transmit_info_with_vidupdate(struct sip_pvt *p);
00897 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
00898 static int transmit_refer(struct sip_pvt *p, const char *dest);
00899 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
00900 static struct sip_peer *temp_peer(const char *name);
00901 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader, int sipmethod, int init);
00902 static void free_old_route(struct sip_route *route);
00903 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
00904 static int update_call_counter(struct sip_pvt *fup, int event);
00905 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int realtime);
00906 static struct sip_user *build_user(const char *name, struct ast_variable *v, int realtime);
00907 static int sip_do_reload(void);
00908 static int expire_register(void *data);
00909 static int callevents = 0;
00910
00911 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause);
00912 static int sip_devicestate(void *data);
00913 static int sip_sendtext(struct ast_channel *ast, const char *text);
00914 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
00915 static int sip_hangup(struct ast_channel *ast);
00916 static int sip_answer(struct ast_channel *ast);
00917 static struct ast_frame *sip_read(struct ast_channel *ast);
00918 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
00919 static int sip_indicate(struct ast_channel *ast, int condition);
00920 static int sip_transfer(struct ast_channel *ast, const char *dest);
00921 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00922 static int sip_senddigit(struct ast_channel *ast, char digit);
00923 static int clear_realm_authentication(struct sip_auth *authlist);
00924 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno);
00925 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, char *realm);
00926 static int check_sip_domain(const char *domain, char *context, size_t len);
00927 static void append_date(struct sip_request *req);
00928 static int determine_firstline_parts(struct sip_request *req);
00929 static void sip_dump_history(struct sip_pvt *dialog);
00930 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
00931 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int substate);
00932 static char *gettag(struct sip_request *req, char *header, char *tagbuf, int tagbufsize);
00933
00934
00935 static const struct ast_channel_tech sip_tech = {
00936 .type = channeltype,
00937 .description = "Session Initiation Protocol (SIP)",
00938 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
00939 .properties = AST_CHAN_TP_WANTSJITTER,
00940 .requester = sip_request_call,
00941 .devicestate = sip_devicestate,
00942 .call = sip_call,
00943 .hangup = sip_hangup,
00944 .answer = sip_answer,
00945 .read = sip_read,
00946 .write = sip_write,
00947 .write_video = sip_write,
00948 .indicate = sip_indicate,
00949 .transfer = sip_transfer,
00950 .fixup = sip_fixup,
00951 .send_digit = sip_senddigit,
00952 .bridge = ast_rtp_bridge,
00953 .send_text = sip_sendtext,
00954 };
00955
00956 #ifdef __AST_DEBUG_MALLOC
00957 static void FREE(void *ptr)
00958 {
00959 free(ptr);
00960 }
00961 #else
00962 #define FREE free
00963 #endif
00964
00965
00966
00967
00968
00969
00970
00971
00972 static force_inline int thread_safe_rand(void)
00973 {
00974 int val;
00975
00976 ast_mutex_lock(&rand_lock);
00977 val = rand();
00978 ast_mutex_unlock(&rand_lock);
00979
00980 return val;
00981 }
00982
00983
00984
00985
00986 int find_sip_method(char *msg)
00987 {
00988 int i, res = 0;
00989
00990 if (ast_strlen_zero(msg))
00991 return 0;
00992
00993 for (i = 1; (i < (sizeof(sip_methods) / sizeof(sip_methods[0]))) && !res; i++) {
00994 if (!strcasecmp(sip_methods[i].text, msg))
00995 res = sip_methods[i].id;
00996 }
00997 return res;
00998 }
00999
01000
01001 unsigned int parse_sip_options(struct sip_pvt *pvt, char *supported)
01002 {
01003 char *next = NULL;
01004 char *sep = NULL;
01005 char *temp = ast_strdupa(supported);
01006 int i;
01007 unsigned int profile = 0;
01008
01009 if (ast_strlen_zero(supported) )
01010 return 0;
01011
01012 if (option_debug > 2 && sipdebug)
01013 ast_log(LOG_DEBUG, "Begin: parsing SIP \"Supported: %s\"\n", supported);
01014
01015 next = temp;
01016 while (next) {
01017 char res=0;
01018 if ( (sep = strchr(next, ',')) != NULL) {
01019 *sep = '\0';
01020 sep++;
01021 }
01022 while (*next == ' ')
01023 next++;
01024 if (option_debug > 2 && sipdebug)
01025 ast_log(LOG_DEBUG, "Found SIP option: -%s-\n", next);
01026 for (i=0; (i < (sizeof(sip_options) / sizeof(sip_options[0]))) && !res; i++) {
01027 if (!strcasecmp(next, sip_options[i].text)) {
01028 profile |= sip_options[i].id;
01029 res = 1;
01030 if (option_debug > 2 && sipdebug)
01031 ast_log(LOG_DEBUG, "Matched SIP option: %s\n", next);
01032 }
01033 }
01034 if (!res)
01035 if (option_debug > 2 && sipdebug)
01036 ast_log(LOG_DEBUG, "Found no match for SIP option: %s (Please file bug report!)\n", next);
01037 next = sep;
01038 }
01039 if (pvt) {
01040 pvt->sipoptions = profile;
01041 if (option_debug)
01042 ast_log(LOG_DEBUG, "* SIP extension value: %d for call %s\n", profile, pvt->callid);
01043 }
01044 return profile;
01045 }
01046
01047
01048 static inline int sip_debug_test_addr(struct sockaddr_in *addr)
01049 {
01050 if (sipdebug == 0)
01051 return 0;
01052 if (debugaddr.sin_addr.s_addr) {
01053 if (((ntohs(debugaddr.sin_port) != 0)
01054 && (debugaddr.sin_port != addr->sin_port))
01055 || (debugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
01056 return 0;
01057 }
01058 return 1;
01059 }
01060
01061
01062 static inline int sip_debug_test_pvt(struct sip_pvt *p)
01063 {
01064 if (sipdebug == 0)
01065 return 0;
01066 return sip_debug_test_addr(((ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE) ? &p->recv : &p->sa));
01067 }
01068
01069
01070
01071 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
01072 {
01073 int res;
01074 char iabuf[INET_ADDRSTRLEN];
01075
01076 if (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE)
01077 res=sendto(sipsock, data, len, 0, (struct sockaddr *)&p->recv, sizeof(struct sockaddr_in));
01078 else
01079 res=sendto(sipsock, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_in));
01080
01081 if (res != len) {
01082 ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s:%d returned %d: %s\n", data, len, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), res, strerror(errno));
01083 }
01084 return res;
01085 }
01086
01087 static void sip_destroy(struct sip_pvt *p);
01088
01089
01090 static void build_via(struct sip_pvt *p, char *buf, int len)
01091 {
01092 char iabuf[INET_ADDRSTRLEN];
01093
01094
01095 if (ast_test_flag(p, SIP_NAT) & SIP_NAT_RFC3581)
01096 snprintf(buf, len, "SIP/2.0/UDP %s:%d;branch=z9hG4bK%08x;rport", ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip), ourport, p->branch);
01097 else
01098 snprintf(buf, len, "SIP/2.0/UDP %s:%d;branch=z9hG4bK%08x", ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip), ourport, p->branch);
01099 }
01100
01101
01102
01103 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us)
01104 {
01105
01106
01107
01108
01109
01110 struct sockaddr_in theirs;
01111 theirs.sin_addr = *them;
01112 if (localaddr && externip.sin_addr.s_addr &&
01113 ast_apply_ha(localaddr, &theirs)) {
01114 char iabuf[INET_ADDRSTRLEN];
01115 if (externexpire && (time(NULL) >= externexpire)) {
01116 struct ast_hostent ahp;
01117 struct hostent *hp;
01118 time(&externexpire);
01119 externexpire += externrefresh;
01120 if ((hp = ast_gethostbyname(externhost, &ahp))) {
01121 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
01122 } else
01123 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
01124 }
01125 memcpy(us, &externip.sin_addr, sizeof(struct in_addr));
01126 ast_inet_ntoa(iabuf, sizeof(iabuf), *(struct in_addr *)&them->s_addr);
01127 ast_log(LOG_DEBUG, "Target address %s is not local, substituting externip\n", iabuf);
01128 }
01129 else if (bindaddr.sin_addr.s_addr)
01130 memcpy(us, &bindaddr.sin_addr, sizeof(struct in_addr));
01131 else
01132 return ast_ouraddrfor(them, us);
01133 return 0;
01134 }
01135
01136
01137
01138 static int append_history(struct sip_pvt *p, const char *event, const char *data)
01139 {
01140 struct sip_history *hist, *prev;
01141 char *c;
01142
01143 if (!recordhistory || !p)
01144 return 0;
01145 if(!(hist = malloc(sizeof(struct sip_history)))) {
01146 ast_log(LOG_WARNING, "Can't allocate memory for history");
01147 return 0;
01148 }
01149 memset(hist, 0, sizeof(struct sip_history));
01150 snprintf(hist->event, sizeof(hist->event), "%-15s %s", event, data);
01151
01152 c = hist->event;
01153 while(*c) {
01154 if ((*c == '\r') || (*c == '\n')) {
01155 *c = '\0';
01156 break;
01157 }
01158 c++;
01159 }
01160
01161 prev = p->history;
01162 if (prev) {
01163 while(prev->next)
01164 prev = prev->next;
01165 prev->next = hist;
01166 } else {
01167 p->history = hist;
01168 }
01169 return 0;
01170 }
01171
01172
01173 static int retrans_pkt(void *data)
01174 {
01175 struct sip_pkt *pkt=data, *prev, *cur = NULL;
01176 char iabuf[INET_ADDRSTRLEN];
01177 int reschedule = DEFAULT_RETRANS;
01178
01179
01180 ast_mutex_lock(&pkt->owner->lock);
01181
01182 if (pkt->retrans < MAX_RETRANS) {
01183 char buf[80];
01184
01185 pkt->retrans++;
01186 if (!pkt->timer_t1) {
01187 if (sipdebug && option_debug > 3)
01188 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);
01189 } else {
01190 int siptimer_a;
01191
01192 if (sipdebug && option_debug > 3)
01193 ast_log(LOG_DEBUG, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n", pkt->retransid, pkt->retrans, sip_methods[pkt->method].text, pkt->method);
01194 if (!pkt->timer_a)
01195 pkt->timer_a = 2 ;
01196 else
01197 pkt->timer_a = 2 * pkt->timer_a;
01198
01199
01200 siptimer_a = pkt->timer_t1 * pkt->timer_a;
01201 if (pkt->method != SIP_INVITE && siptimer_a > 4000)
01202 siptimer_a = 4000;
01203
01204
01205 reschedule = siptimer_a;
01206 if (option_debug > 3)
01207 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);
01208 }
01209
01210 if (pkt->owner && sip_debug_test_pvt(pkt->owner)) {
01211 if (ast_test_flag(pkt->owner, SIP_NAT) & SIP_NAT_ROUTE)
01212 ast_verbose("Retransmitting #%d (NAT) to %s:%d:\n%s\n---\n", pkt->retrans, ast_inet_ntoa(iabuf, sizeof(iabuf), pkt->owner->recv.sin_addr), ntohs(pkt->owner->recv.sin_port), pkt->data);
01213 else
01214 ast_verbose("Retransmitting #%d (no NAT) to %s:%d:\n%s\n---\n", pkt->retrans, ast_inet_ntoa(iabuf, sizeof(iabuf), pkt->owner->sa.sin_addr), ntohs(pkt->owner->sa.sin_port), pkt->data);
01215 }
01216 snprintf(buf, sizeof(buf), "ReTx %d", reschedule);
01217
01218 append_history(pkt->owner, buf, pkt->data);
01219 __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
01220 ast_mutex_unlock(&pkt->owner->lock);
01221 return reschedule;
01222 }
01223
01224 if (pkt->owner && pkt->method != SIP_OPTIONS) {
01225 if (ast_test_flag(pkt, FLAG_FATAL) || sipdebug)
01226 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");
01227 } else {
01228 if (pkt->method == SIP_OPTIONS && sipdebug)
01229 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) \n", pkt->owner->callid);
01230 }
01231 append_history(pkt->owner, "MaxRetries", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
01232
01233 pkt->retransid = -1;
01234
01235 if (ast_test_flag(pkt, FLAG_FATAL)) {
01236 while(pkt->owner->owner && ast_mutex_trylock(&pkt->owner->owner->lock)) {
01237 ast_mutex_unlock(&pkt->owner->lock);
01238 usleep(1);
01239 ast_mutex_lock(&pkt->owner->lock);
01240 }
01241 if (pkt->owner->owner) {
01242 ast_set_flag(pkt->owner, SIP_ALREADYGONE);
01243 ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet.\n", pkt->owner->callid);
01244 ast_queue_hangup(pkt->owner->owner);
01245 ast_mutex_unlock(&pkt->owner->owner->lock);
01246 } else {
01247
01248 ast_set_flag(pkt->owner, SIP_NEEDDESTROY);
01249 }
01250 }
01251
01252 prev = NULL;
01253 cur = pkt->owner->packets;
01254 while(cur) {
01255 if (cur == pkt)
01256 break;
01257 prev = cur;
01258 cur = cur->next;
01259 }
01260 if (cur) {
01261 if (prev)
01262 prev->next = cur->next;
01263 else
01264 pkt->owner->packets = cur->next;
01265 ast_mutex_unlock(&pkt->owner->lock);
01266 free(cur);
01267 pkt = NULL;
01268 } else
01269 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
01270 if (pkt)
01271 ast_mutex_unlock(&pkt->owner->lock);
01272 return 0;
01273 }
01274
01275
01276 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *data, int len, int fatal, int sipmethod)
01277 {
01278 struct sip_pkt *pkt;
01279 int siptimer_a = DEFAULT_RETRANS;
01280
01281 pkt = malloc(sizeof(struct sip_pkt) + len + 1);
01282 if (!pkt)
01283 return -1;
01284 memset(pkt, 0, sizeof(struct sip_pkt));
01285 memcpy(pkt->data, data, len);
01286 pkt->method = sipmethod;
01287 pkt->packetlen = len;
01288 pkt->next = p->packets;
01289 pkt->owner = p;
01290 pkt->seqno = seqno;
01291 pkt->flags = resp;
01292 pkt->data[len] = '\0';
01293 pkt->timer_t1 = p->timer_t1;
01294 if (fatal)
01295 ast_set_flag(pkt, FLAG_FATAL);
01296 if (pkt->timer_t1)
01297 siptimer_a = pkt->timer_t1 * 2;
01298
01299
01300 pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
01301 if (option_debug > 3 && sipdebug)
01302 ast_log(LOG_DEBUG, "*** SIP TIMER: Initalizing retransmit timer on packet: Id #%d\n", pkt->retransid);
01303 pkt->next = p->packets;
01304 p->packets = pkt;
01305
01306 __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
01307 if (sipmethod == SIP_INVITE) {
01308
01309 p->pendinginvite = seqno;
01310 }
01311 return 0;
01312 }
01313
01314
01315 static int __sip_autodestruct(void *data)
01316 {
01317 struct sip_pvt *p = data;
01318
01319
01320
01321 if (p->subscribed) {
01322 p->subscribed = TIMEOUT;
01323 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, 1);
01324 p->subscribed = NONE;
01325 append_history(p, "Subscribestatus", "timeout");
01326 return 10000;
01327 }
01328
01329
01330 p->autokillid = -1;
01331
01332 ast_log(LOG_DEBUG, "Auto destroying call '%s'\n", p->callid);
01333 append_history(p, "AutoDestroy", "");
01334 if (p->owner) {
01335 ast_log(LOG_WARNING, "Autodestruct on call '%s' with owner in place\n", p->callid);
01336 ast_queue_hangup(p->owner);
01337 } else {
01338 sip_destroy(p);
01339 }
01340 return 0;
01341 }
01342
01343
01344 static int sip_scheddestroy(struct sip_pvt *p, int ms)
01345 {
01346 char tmp[80];
01347 if (sip_debug_test_pvt(p))
01348 ast_verbose("Scheduling destruction of call '%s' in %d ms\n", p->callid, ms);
01349 if (recordhistory) {
01350 snprintf(tmp, sizeof(tmp), "%d ms", ms);
01351 append_history(p, "SchedDestroy", tmp);
01352 }
01353
01354 if (p->autokillid > -1)
01355 ast_sched_del(sched, p->autokillid);
01356 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, p);
01357 return 0;
01358 }
01359
01360
01361 static int sip_cancel_destroy(struct sip_pvt *p)
01362 {
01363 if (p->autokillid > -1)
01364 ast_sched_del(sched, p->autokillid);
01365 append_history(p, "CancelDestroy", "");
01366 p->autokillid = -1;
01367 return 0;
01368 }
01369
01370
01371 static int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
01372 {
01373 struct sip_pkt *cur, *prev = NULL;
01374 int res = -1;
01375 int resetinvite = 0;
01376
01377 char *msg;
01378
01379 msg = sip_methods[sipmethod].text;
01380
01381 ast_mutex_lock(&p->lock);
01382 cur = p->packets;
01383 while(cur) {
01384 if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
01385 ((ast_test_flag(cur, FLAG_RESPONSE)) ||
01386 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
01387 if (!resp && (seqno == p->pendinginvite)) {
01388 ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
01389 p->pendinginvite = 0;
01390 resetinvite = 1;
01391 }
01392
01393 if (prev)
01394 prev->next = cur->next;
01395 else
01396 p->packets = cur->next;
01397 if (cur->retransid > -1) {
01398 if (sipdebug && option_debug > 3)
01399 ast_log(LOG_DEBUG, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
01400 ast_sched_del(sched, cur->retransid);
01401 }
01402 free(cur);
01403 res = 0;
01404 break;
01405 }
01406 prev = cur;
01407 cur = cur->next;
01408 }
01409 ast_mutex_unlock(&p->lock);
01410 ast_log(LOG_DEBUG, "Stopping retransmission on '%s' of %s %d: Match %s\n", p->callid, resp ? "Response" : "Request", seqno, res ? "Not Found" : "Found");
01411 return res;
01412 }
01413
01414
01415 static int __sip_pretend_ack(struct sip_pvt *p)
01416 {
01417 struct sip_pkt *cur=NULL;
01418
01419 while(p->packets) {
01420 if (cur == p->packets) {
01421 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
01422 return -1;
01423 }
01424 cur = p->packets;
01425 if (cur->method)
01426 __sip_ack(p, p->packets->seqno, (ast_test_flag(p->packets, FLAG_RESPONSE)), cur->method);
01427 else {
01428 char *c;
01429 char method[128];
01430 ast_copy_string(method, p->packets->data, sizeof(method));
01431 c = ast_skip_blanks(method);
01432 *c = '\0';
01433 __sip_ack(p, p->packets->seqno, (ast_test_flag(p->packets, FLAG_RESPONSE)), find_sip_method(method));
01434 }
01435 }
01436 return 0;
01437 }
01438
01439
01440 static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
01441 {
01442 struct sip_pkt *cur;
01443 int res = -1;
01444 char *msg = sip_methods[sipmethod].text;
01445
01446 cur = p->packets;
01447 while(cur) {
01448 if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
01449 ((ast_test_flag(cur, FLAG_RESPONSE)) ||
01450 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
01451
01452 if (cur->retransid > -1) {
01453 if (option_debug > 3 && sipdebug)
01454 ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, msg);
01455 ast_sched_del(sched, cur->retransid);
01456 }
01457 cur->retransid = -1;
01458 res = 0;
01459 break;
01460 }
01461 cur = cur->next;
01462 }
01463 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");
01464 return res;
01465 }
01466
01467 static void parse_request(struct sip_request *req);
01468 static char *get_header(struct sip_request *req, char *name);
01469 static void copy_request(struct sip_request *dst,struct sip_request *src);
01470
01471
01472 static void parse_copy(struct sip_request *dst, struct sip_request *src)
01473 {
01474 memset(dst, 0, sizeof(*dst));
01475 memcpy(dst->data, src->data, sizeof(dst->data));
01476 dst->len = src->len;
01477 parse_request(dst);
01478 }
01479
01480
01481 static int send_response(struct sip_pvt *p, struct sip_request *req, int reliable, int seqno)
01482 {
01483 int res;
01484 char iabuf[INET_ADDRSTRLEN];
01485 struct sip_request tmp;
01486 char tmpmsg[80];
01487
01488 if (sip_debug_test_pvt(p)) {
01489 if (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE)
01490 ast_verbose("%sTransmitting (NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr), ntohs(p->recv.sin_port), req->data);
01491 else
01492 ast_verbose("%sTransmitting (no NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), req->data);
01493 }
01494 if (reliable) {
01495 if (recordhistory) {
01496 parse_copy(&tmp, req);
01497 snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
01498 append_history(p, "TxRespRel", tmpmsg);
01499 }
01500 res = __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable > 1), req->method);
01501 } else {
01502 if (recordhistory) {
01503 parse_copy(&tmp, req);
01504 snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
01505 append_history(p, "TxResp", tmpmsg);
01506 }
01507 res = __sip_xmit(p, req->data, req->len);
01508 }
01509 if (res > 0)
01510 return 0;
01511 return res;
01512 }
01513
01514
01515 static int send_request(struct sip_pvt *p, struct sip_request *req, int reliable, int seqno)
01516 {
01517 int res;
01518 char iabuf[INET_ADDRSTRLEN];
01519 struct sip_request tmp;
01520 char tmpmsg[80];
01521
01522 if (sip_debug_test_pvt(p)) {
01523 if (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE)
01524 ast_verbose("%sTransmitting (NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr), ntohs(p->recv.sin_port), req->data);
01525 else
01526 ast_verbose("%sTransmitting (no NAT) to %s:%d:\n%s\n---\n", reliable ? "Reliably " : "", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), req->data);
01527 }
01528 if (reliable) {
01529 if (recordhistory) {
01530 parse_copy(&tmp, req);
01531 snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
01532 append_history(p, "TxReqRel", tmpmsg);
01533 }
01534 res = __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable > 1), req->method);
01535 } else {
01536 if (recordhistory) {
01537 parse_copy(&tmp, req);
01538 snprintf(tmpmsg, sizeof(tmpmsg), "%s / %s", tmp.data, get_header(&tmp, "CSeq"));
01539 append_history(p, "TxReq", tmpmsg);
01540 }
01541 res = __sip_xmit(p, req->data, req->len);
01542 }
01543 return res;
01544 }
01545
01546
01547
01548 static char *get_in_brackets(char *tmp)
01549 {
01550 char *parse;
01551 char *first_quote;
01552 char *first_bracket;
01553 char *second_bracket;
01554 char last_char;
01555
01556 parse = tmp;
01557 while (1) {
01558 first_quote = strchr(parse, '"');
01559 first_bracket = strchr(parse, '<');
01560 if (first_quote && first_bracket && (first_quote < first_bracket)) {
01561 last_char = '\0';
01562 for (parse = first_quote + 1; *parse; parse++) {
01563 if ((*parse == '"') && (last_char != '\\'))
01564 break;
01565 last_char = *parse;
01566 }
01567 if (!*parse) {
01568 ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
01569 return tmp;
01570 }
01571 parse++;
01572 continue;
01573 }
01574 if (first_bracket) {
01575 second_bracket = strchr(first_bracket + 1, '>');
01576 if (second_bracket) {
01577 *second_bracket = '\0';
01578 return first_bracket + 1;
01579 } else {
01580 ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
01581 return tmp;
01582 }
01583 }
01584 return tmp;
01585 }
01586 }
01587
01588
01589
01590 static int sip_sendtext(struct ast_channel *ast, const char *text)
01591 {
01592 struct sip_pvt *p = ast->tech_pvt;
01593 int debug=sip_debug_test_pvt(p);
01594
01595 if (debug)
01596 ast_verbose("Sending text %s on %s\n", text, ast->name);
01597 if (!p)
01598 return -1;
01599 if (ast_strlen_zero(text))
01600 return 0;
01601 if (debug)
01602 ast_verbose("Really sending text %s on %s\n", text, ast->name);
01603 transmit_message_with_text(p, text);
01604 return 0;
01605 }
01606
01607
01608 static void realtime_update_peer(const char *peername, struct sockaddr_in *sin, const char *username, const char *fullcontact, int expirey)
01609 {
01610 char port[10];
01611 char ipaddr[20];
01612 char regseconds[20];
01613 time_t nowtime;
01614
01615 time(&nowtime);
01616 nowtime += expirey;
01617 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
01618 ast_inet_ntoa(ipaddr, sizeof(ipaddr), sin->sin_addr);
01619 snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
01620
01621 if (fullcontact)
01622 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr, "port", port, "regseconds", regseconds, "username", username, "fullcontact", fullcontact, NULL);
01623 else
01624 ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr, "port", port, "regseconds", regseconds, "username", username, NULL);
01625 }
01626
01627
01628 static void register_peer_exten(struct sip_peer *peer, int onoff)
01629 {
01630 char multi[256];
01631 char *stringp, *ext;
01632 if (!ast_strlen_zero(regcontext)) {
01633 ast_copy_string(multi, ast_strlen_zero(peer->regexten) ? peer->name : peer->regexten, sizeof(multi));
01634 stringp = multi;
01635 while((ext = strsep(&stringp, "&"))) {
01636 if (onoff)
01637 ast_add_extension(regcontext, 1, ext, 1, NULL, NULL, "Noop", strdup(peer->name), FREE, channeltype);
01638 else
01639 ast_context_remove_extension(regcontext, ext, 1, NULL);
01640 }
01641 }
01642 }
01643
01644
01645 static void sip_destroy_peer(struct sip_peer *peer)
01646 {
01647
01648 if (peer->call)
01649 sip_destroy(peer->call);
01650 if (peer->chanvars) {
01651 ast_variables_destroy(peer->chanvars);
01652 peer->chanvars = NULL;
01653 }
01654 if (peer->expire > -1)
01655 ast_sched_del(sched, peer->expire);
01656 if (peer->pokeexpire > -1)
01657 ast_sched_del(sched, peer->pokeexpire);
01658 register_peer_exten(peer, 0);
01659 ast_free_ha(peer->ha);
01660 if (ast_test_flag(peer, SIP_SELFDESTRUCT))
01661 apeerobjs--;
01662 else if (ast_test_flag(peer, SIP_REALTIME))
01663 rpeerobjs--;
01664 else
01665 speerobjs--;
01666 clear_realm_authentication(peer->auth);
01667 peer->auth = (struct sip_auth *) NULL;
01668 if (peer->dnsmgr)
01669 ast_dnsmgr_release(peer->dnsmgr);
01670 free(peer);
01671 }
01672
01673
01674 static void update_peer(struct sip_peer *p, int expiry)
01675 {
01676 int rtcachefriends = ast_test_flag(&(p->flags_page2), SIP_PAGE2_RTCACHEFRIENDS);
01677 if (ast_test_flag((&global_flags_page2), SIP_PAGE2_RTUPDATE) &&
01678 (ast_test_flag(p, SIP_REALTIME) || rtcachefriends)) {
01679 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, expiry);
01680 }
01681 }
01682
01683
01684
01685
01686 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin)
01687 {
01688 struct sip_peer *peer=NULL;
01689 struct ast_variable *var;
01690 struct ast_variable *tmp;
01691 char *newpeername = (char *) peername;
01692 char iabuf[80];
01693
01694
01695 if (newpeername)
01696 var = ast_load_realtime("sippeers", "name", peername, NULL);
01697 else if (sin) {
01698 ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr);
01699 var = ast_load_realtime("sippeers", "host", iabuf, NULL);
01700 if (!var)
01701 var = ast_load_realtime("sippeers", "ipaddr", iabuf, NULL);
01702
01703 } else
01704 return NULL;
01705
01706 if (!var)
01707 return NULL;
01708
01709 tmp = var;
01710
01711 while(tmp) {
01712 if (!strcasecmp(tmp->name, "type") &&
01713 !strcasecmp(tmp->value, "user")) {
01714 ast_variables_destroy(var);
01715 return NULL;
01716 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
01717 newpeername = tmp->value;
01718 }
01719 tmp = tmp->next;
01720 }
01721
01722 if (!newpeername) {
01723 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", iabuf);
01724 ast_variables_destroy(var);
01725 return (struct sip_peer *) NULL;
01726 }
01727
01728
01729 peer = build_peer(newpeername, var, !ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS));
01730 if (!peer) {
01731 ast_variables_destroy(var);
01732 return (struct sip_peer *) NULL;
01733 }
01734
01735 if (ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
01736
01737 ast_copy_flags((&peer->flags_page2),(&global_flags_page2), SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
01738 if (ast_test_flag((&global_flags_page2), SIP_PAGE2_RTAUTOCLEAR)) {
01739 if (peer->expire > -1) {
01740 ast_sched_del(sched, peer->expire);
01741 }
01742 peer->expire = ast_sched_add(sched, (global_rtautoclear) * 1000, expire_register, (void *)peer);
01743 }
01744 ASTOBJ_CONTAINER_LINK(&peerl,peer);
01745 } else {
01746 ast_set_flag(peer, SIP_REALTIME);
01747 }
01748 ast_variables_destroy(var);
01749
01750 return peer;
01751 }
01752
01753
01754 static int sip_addrcmp(char *name, struct sockaddr_in *sin)
01755 {
01756
01757 struct sip_peer *p = (struct sip_peer *)name;
01758 return !(!inaddrcmp(&p->addr, sin) ||
01759 (ast_test_flag(p, SIP_INSECURE_PORT) &&
01760 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
01761 }
01762
01763
01764
01765
01766 static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
01767 {
01768 struct sip_peer *p = NULL;
01769
01770 if (peer)
01771 p = ASTOBJ_CONTAINER_FIND(&peerl,peer);
01772 else
01773 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl,sin,name,sip_addr_hashfunc,1,sip_addrcmp);
01774
01775 if (!p && realtime) {
01776 p = realtime_peer(peer, sin);
01777 }
01778
01779 return p;
01780 }
01781
01782
01783 static void sip_destroy_user(struct sip_user *user)
01784 {
01785 ast_free_ha(user->ha);
01786 if (user->chanvars) {
01787 ast_variables_destroy(user->chanvars);
01788 user->chanvars = NULL;
01789 }
01790 if (ast_test_flag(user, SIP_REALTIME))
01791 ruserobjs--;
01792 else
01793 suserobjs--;
01794 free(user);
01795 }
01796
01797
01798
01799
01800 static struct sip_user *realtime_user(const char *username)
01801 {
01802 struct ast_variable *var;
01803 struct ast_variable *tmp;
01804 struct sip_user *user = NULL;
01805
01806 var = ast_load_realtime("sipusers", "name", username, NULL);
01807
01808 if (!var)
01809 return NULL;
01810
01811 tmp = var;
01812 while (tmp) {
01813 if (!strcasecmp(tmp->name, "type") &&
01814 !strcasecmp(tmp->value, "peer")) {
01815 ast_variables_destroy(var);
01816 return NULL;
01817 }
01818 tmp = tmp->next;
01819 }
01820
01821
01822
01823 user = build_user(username, var, !ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS));
01824
01825 if (!user) {
01826 ast_variables_destroy(var);
01827 return NULL;
01828 }
01829
01830 if (ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
01831 ast_set_flag((&user->flags_page2), SIP_PAGE2_RTCACHEFRIENDS);
01832 suserobjs++;
01833 ASTOBJ_CONTAINER_LINK(&userl,user);
01834 } else {
01835
01836 suserobjs--;
01837 ruserobjs++;
01838 ast_set_flag(user, SIP_REALTIME);
01839 }
01840 ast_variables_destroy(var);
01841 return user;
01842 }
01843
01844
01845
01846
01847
01848 static struct sip_user *find_user(const char *name, int realtime)
01849 {
01850 struct sip_user *u = NULL;
01851 u = ASTOBJ_CONTAINER_FIND(&userl,name);
01852 if (!u && realtime) {
01853 u = realtime_user(name);
01854 }
01855 return u;
01856 }
01857
01858
01859 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer)
01860 {
01861 char *callhost;
01862
01863 if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
01864 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
01865 if (peer->addr.sin_addr.s_addr) {
01866 r->sa.sin_family = peer->addr.sin_family;
01867 r->sa.sin_addr = peer->addr.sin_addr;
01868 r->sa.sin_port = peer->addr.sin_port;
01869 } else {
01870 r->sa.sin_family = peer->defaddr.sin_family;
01871 r->sa.sin_addr = peer->defaddr.sin_addr;
01872 r->sa.sin_port = peer->defaddr.sin_port;
01873 }
01874 memcpy(&r->recv, &r->sa, sizeof(r->recv));
01875 } else {
01876 return -1;
01877 }
01878
01879 ast_copy_flags(r, peer, SIP_FLAGS_TO_COPY);
01880 r->capability = peer->capability;
01881 r->prefs = peer->prefs;
01882 if (r->rtp) {
01883 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", (ast_test_flag(r, SIP_NAT) & SIP_NAT_ROUTE));
01884 ast_rtp_setnat(r->rtp, (ast_test_flag(r, SIP_NAT) & SIP_NAT_ROUTE));
01885 }
01886 if (r->vrtp) {
01887 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", (ast_test_flag(r, SIP_NAT) & SIP_NAT_ROUTE));
01888 ast_rtp_setnat(r->vrtp, (ast_test_flag(r, SIP_NAT) & SIP_NAT_ROUTE));
01889 }
01890 ast_copy_string(r->peername, peer->username, sizeof(r->peername));
01891 ast_copy_string(r->authname, peer->username, sizeof(r->authname));
01892 ast_copy_string(r->username, peer->username, sizeof(r->username));
01893 ast_copy_string(r->peersecret, peer->secret, sizeof(r->peersecret));
01894 ast_copy_string(r->peermd5secret, peer->md5secret, sizeof(r->peermd5secret));
01895 ast_copy_string(r->tohost, peer->tohost, sizeof(r->tohost));
01896 ast_copy_string(r->fullcontact, peer->fullcontact, sizeof(r->fullcontact));
01897 if (!r->initreq.headers && !ast_strlen_zero(peer->fromdomain)) {
01898 if ((callhost = strchr(r->callid, '@'))) {
01899 strncpy(callhost + 1, peer->fromdomain, sizeof(r->callid) - (callhost - r->callid) - 2);
01900 }
01901 }
01902 if (ast_strlen_zero(r->tohost)) {
01903 if (peer->addr.sin_addr.s_addr)
01904 ast_inet_ntoa(r->tohost, sizeof(r->tohost), peer->addr.sin_addr);
01905 else
01906 ast_inet_ntoa(r->tohost, sizeof(r->tohost), peer->defaddr.sin_addr);
01907 }
01908 if (!ast_strlen_zero(peer->fromdomain))
01909 ast_copy_string(r->fromdomain, peer->fromdomain, sizeof(r->fromdomain));
01910 if (!ast_strlen_zero(peer->fromuser))
01911 ast_copy_string(r->fromuser, peer->fromuser, sizeof(r->fromuser));
01912 r->maxtime = peer->maxms;
01913 r->callgroup = peer->callgroup;
01914 r->pickupgroup = peer->pickupgroup;
01915
01916 if (peer->maxms && peer->lastms)
01917 r->timer_t1 = peer->lastms;
01918 if ((ast_test_flag(r, SIP_DTMF) == SIP_DTMF_RFC2833) || (ast_test_flag(r, SIP_DTMF) == SIP_DTMF_AUTO))
01919 r->noncodeccapability |= AST_RTP_DTMF;
01920 else
01921 r->noncodeccapability &= ~AST_RTP_DTMF;
01922 ast_copy_string(r->context, peer->context,sizeof(r->context));
01923 r->rtptimeout = peer->rtptimeout;
01924 r->rtpholdtimeout = peer->rtpholdtimeout;
01925 r->rtpkeepalive = peer->rtpkeepalive;
01926 if (peer->call_limit)
01927 ast_set_flag(r, SIP_CALL_LIMIT);
01928
01929 return 0;
01930 }
01931
01932
01933
01934
01935 static int create_addr(struct sip_pvt *dialog, char *opeer)
01936 {
01937 struct hostent *hp;
01938 struct ast_hostent ahp;
01939 struct sip_peer *p;
01940 int found=0;
01941 char *port;
01942 int portno;
01943 char host[MAXHOSTNAMELEN], *hostn;
01944 char peer[256];
01945
01946 ast_copy_string(peer, opeer, sizeof(peer));
01947 port = strchr(peer, ':');
01948 if (port) {
01949 *port = '\0';
01950 port++;
01951 }
01952 dialog->sa.sin_family = AF_INET;
01953 dialog->timer_t1 = 500;
01954 p = find_peer(peer, NULL, 1);
01955
01956 if (p) {
01957 found++;
01958 if (create_addr_from_peer(dialog, p))
01959 ASTOBJ_UNREF(p, sip_destroy_peer);
01960 }
01961 if (!p) {
01962 if (found)
01963 return -1;
01964
01965 hostn = peer;
01966 if (port)
01967 portno = atoi(port);
01968 else
01969 portno = DEFAULT_SIP_PORT;
01970 if (srvlookup) {
01971 char service[MAXHOSTNAMELEN];
01972 int tportno;
01973 int ret;
01974 snprintf(service, sizeof(service), "_sip._udp.%s", peer);
01975 ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
01976 if (ret > 0) {
01977 hostn = host;
01978 portno = tportno;
01979 }
01980 }
01981 hp = ast_gethostbyname(hostn, &ahp);
01982 if (hp) {
01983 ast_copy_string(dialog->tohost, peer, sizeof(dialog->tohost));
01984 memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
01985 dialog->sa.sin_port = htons(portno);
01986 memcpy(&dialog->recv, &dialog->sa, sizeof(dialog->recv));
01987 return 0;
01988 } else {
01989 ast_log(LOG_WARNING, "No such host: %s\n", peer);
01990 return -1;
01991 }
01992 } else {
01993 ASTOBJ_UNREF(p, sip_destroy_peer);
01994 return 0;
01995 }
01996 }
01997
01998
01999 static int auto_congest(void *nothing)
02000 {
02001 struct sip_pvt *p = nothing;
02002 ast_mutex_lock(&p->lock);
02003 p->initid = -1;
02004 if (p->owner) {
02005 if (!ast_mutex_trylock(&p->owner->lock)) {
02006 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
02007 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
02008 ast_mutex_unlock(&p->owner->lock);
02009 }
02010 }
02011 ast_mutex_unlock(&p->lock);
02012 return 0;
02013 }
02014
02015
02016
02017
02018
02019
02020 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
02021 {
02022 int res;
02023 struct sip_pvt *p;
02024 #ifdef OSP_SUPPORT
02025 char *osphandle = NULL;
02026 #endif
02027 struct varshead *headp;
02028 struct ast_var_t *current;
02029
02030
02031
02032 p = ast->tech_pvt;
02033 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
02034 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
02035 return -1;
02036 }
02037
02038
02039
02040
02041 headp=&ast->varshead;
02042 AST_LIST_TRAVERSE(headp,current,entries) {
02043
02044 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
02045 p->options->vxml_url = ast_var_value(current);
02046 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
02047 p->options->uri_options = ast_var_value(current);
02048 } else if (!p->options->distinctive_ring && !strcasecmp(ast_var_name(current), "ALERT_INFO")) {
02049
02050 p->options->distinctive_ring = ast_var_value(current);
02051 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
02052
02053 p->options->addsipheaders = 1;
02054 }
02055
02056
02057 #ifdef OSP_SUPPORT
02058 else if (!p->options->osptoken && !strcasecmp(ast_var_name(current), "OSPTOKEN")) {
02059 p->options->osptoken = ast_var_value(current);
02060 } else if (!osphandle && !strcasecmp(ast_var_name(current), "OSPHANDLE")) {
02061 osphandle = ast_var_value(current);
02062 }
02063 #endif
02064 }
02065
02066 res = 0;
02067 ast_set_flag(p, SIP_OUTGOING);
02068 #ifdef OSP_SUPPORT
02069 if (!p->options->osptoken || !osphandle || (sscanf(osphandle, "%d", &p->osphandle) != 1)) {
02070
02071 ast_log(LOG_DEBUG, "Disabling OSP support for this call. osptoken = %s, osphandle = %s\n", p->options->osptoken, osphandle);
02072 p->options->osptoken = NULL;
02073 osphandle = NULL;
02074 p->osphandle = -1;
02075 }
02076 #endif
02077 ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
02078 res = update_call_counter(p, INC_CALL_LIMIT);
02079 if ( res != -1 ) {
02080 p->callingpres = ast->cid.cid_pres;
02081 p->jointcapability = p->capability;
02082 transmit_invite(p, SIP_INVITE, 1, 2);
02083 if (p->maxtime) {
02084
02085 p->initid = ast_sched_add(sched, p->maxtime * 4, auto_congest, p);
02086 }
02087 }
02088 return res;
02089 }
02090
02091
02092
02093 static void sip_registry_destroy(struct sip_registry *reg)
02094 {
02095
02096 if (reg->call) {
02097
02098
02099 reg->call->registry = NULL;
02100 sip_destroy(reg->call);
02101 }
02102 if (reg->expire > -1)
02103 ast_sched_del(sched, reg->expire);
02104 if (reg->timeout > -1)
02105 ast_sched_del(sched, reg->timeout);
02106 regobjs--;
02107 free(reg);
02108
02109 }
02110
02111
02112 static void __sip_destroy(struct sip_pvt *p, int lockowner)
02113 {
02114 struct sip_pvt *cur, *prev = NULL;
02115 struct sip_pkt *cp;
02116 struct sip_history *hist;
02117
02118 if (sip_debug_test_pvt(p))
02119 ast_verbose("Destroying call '%s'\n", p->callid);
02120
02121 if (dumphistory)
02122 sip_dump_history(p);
02123
02124 if (p->options)
02125 free(p->options);
02126
02127 if (p->stateid > -1)
02128 ast_extension_state_del(p->stateid, NULL);
02129 if (p->initid > -1)
02130 ast_sched_del(sched, p->initid);
02131 if (p->autokillid > -1)
02132 ast_sched_del(sched, p->autokillid);
02133
02134 if (p->rtp) {
02135 ast_rtp_destroy(p->rtp);
02136 }
02137 if (p->vrtp) {
02138 ast_rtp_destroy(p->vrtp);
02139 }
02140 if (p->route) {
02141 free_old_route(p->route);
02142 p->route = NULL;
02143 }
02144 if (p->registry) {
02145 if (p->registry->call == p)
02146 p->registry->call = NULL;
02147 ASTOBJ_UNREF(p->registry,sip_registry_destroy);
02148 }
02149
02150 if (p->rpid)
02151 free(p->rpid);
02152
02153 if (p->rpid_from)
02154 free(p->rpid_from);
02155
02156
02157 if (p->owner) {
02158 if (lockowner)
02159 ast_mutex_lock(&p->owner->lock);
02160 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
02161 p->owner->tech_pvt = NULL;
02162 if (lockowner)
02163 ast_mutex_unlock(&p->owner->lock);
02164 }
02165
02166 while(p->history) {
02167 hist = p->history;
02168 p->history = p->history->next;
02169 free(hist);
02170 }
02171
02172 cur = iflist;
02173 while(cur) {
02174 if (cur == p) {
02175 if (prev)
02176 prev->next = cur->next;
02177 else
02178 iflist = cur->next;
02179 break;
02180 }
02181 prev = cur;
02182 cur = cur->next;
02183 }
02184 if (!cur) {
02185 ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
02186 return;
02187 }
02188 while((cp = p->packets)) {
02189 p->packets = p->packets->next;
02190 if (cp->retransid > -1) {
02191 ast_sched_del(sched, cp->retransid);
02192 }
02193 free(cp);
02194 }
02195 if (p->chanvars) {
02196 ast_variables_destroy(p->chanvars);
02197 p->chanvars = NULL;
02198 }
02199 ast_mutex_destroy(&p->lock);
02200 free(p);
02201 }
02202
02203
02204
02205
02206 static int update_call_counter(struct sip_pvt *fup, int event)
02207 {
02208 char name[256];
02209 int *inuse, *call_limit;
02210 int outgoing = ast_test_flag(fup, SIP_OUTGOING);
02211 struct sip_user *u = NULL;
02212 struct sip_peer *p = NULL;
02213
02214 if (option_debug > 2)
02215 ast_log(LOG_DEBUG, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
02216
02217
02218 if (!ast_test_flag(fup, SIP_CALL_LIMIT))
02219 return 0;
02220
02221 ast_copy_string(name, fup->username, sizeof(name));
02222
02223
02224 u = find_user(name, 1);
02225 if (u) {
02226 inuse = &u->inUse;
02227 call_limit = &u->call_limit;
02228 p = NULL;
02229 } else {
02230
02231 if (!p)
02232 p = find_peer(fup->peername, NULL, 1);
02233 if (p) {
02234 inuse = &p->inUse;
02235 call_limit = &p->call_limit;
02236 ast_copy_string(name, fup->peername, sizeof(name));
02237 } else {
02238 if (option_debug > 1)
02239 ast_log(LOG_DEBUG, "%s is not a local user, no call limit\n", name);
02240 return 0;
02241 }
02242 }
02243 switch(event) {
02244
02245 case DEC_CALL_LIMIT:
02246 if ( *inuse > 0 ) {
02247 if (ast_test_flag(fup,SIP_INC_COUNT))
02248 (*inuse)--;
02249 } else {
02250 *inuse = 0;
02251 }
02252 if (option_debug > 1 || sipdebug) {
02253 ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
02254 }
02255 break;
02256 case INC_CALL_LIMIT:
02257 if (*call_limit > 0 ) {
02258 if (*inuse >= *call_limit) {
02259 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);
02260 if (u)
02261 ASTOBJ_UNREF(u,sip_destroy_user);
02262 else
02263 ASTOBJ_UNREF(p,sip_destroy_peer);
02264 return -1;
02265 }
02266 }
02267 (*inuse)++;
02268 ast_set_flag(fup,SIP_INC_COUNT);
02269 if (option_debug > 1 || sipdebug) {
02270 ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
02271 }
02272 break;
02273 default:
02274 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
02275 }
02276 if (u)
02277 ASTOBJ_UNREF(u,sip_destroy_user);
02278 else
02279 ASTOBJ_UNREF(p,sip_destroy_peer);
02280 return 0;
02281 }
02282
02283
02284 static void sip_destroy(struct sip_pvt *p)
02285 {
02286 ast_mutex_lock(&iflock);
02287 __sip_destroy(p, 1);
02288 ast_mutex_unlock(&iflock);
02289 }
02290
02291
02292 static int transmit_response_reliable(struct sip_pvt *p, char *msg, struct sip_request *req, int fatal);
02293
02294
02295 static int hangup_sip2cause(int cause)
02296 {
02297
02298
02299 switch(cause) {
02300 case 603:
02301 case 403:
02302 case 487:
02303 return AST_CAUSE_CALL_REJECTED;
02304 case 404:
02305 return AST_CAUSE_UNALLOCATED;
02306 case 408:
02307 return AST_CAUSE_NO_USER_RESPONSE;
02308 case 480:
02309 return AST_CAUSE_FAILURE;
02310 case 483:
02311 return AST_CAUSE_NO_ANSWER;
02312 case 486:
02313 return AST_CAUSE_BUSY;
02314 case 488:
02315 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
02316 case 500:
02317 return AST_CAUSE_FAILURE;
02318 case 501:
02319 return AST_CAUSE_FACILITY_REJECTED;
02320 case 502:
02321 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
02322 case 503:
02323 return AST_CAUSE_CONGESTION;
02324 default:
02325 return AST_CAUSE_NORMAL;
02326 }
02327
02328 return 0;
02329 }
02330
02331
02332
02333
02334
02335
02336
02337
02338
02339
02340
02341
02342
02343
02344
02345
02346
02347
02348
02349
02350
02351
02352
02353
02354
02355
02356
02357
02358
02359
02360
02361
02362
02363
02364 static char *hangup_cause2sip(int cause)
02365 {
02366 switch(cause)
02367 {
02368 case AST_CAUSE_UNALLOCATED:
02369 case AST_CAUSE_NO_ROUTE_DESTINATION:
02370 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
02371 return "404 Not Found";
02372 case AST_CAUSE_CONGESTION:
02373 case AST_CAUSE_SWITCH_CONGESTION:
02374 return "503 Service Unavailable";
02375 case AST_CAUSE_NO_USER_RESPONSE:
02376 return "408 Request Timeout";
02377 case AST_CAUSE_NO_ANSWER:
02378 return "480 Temporarily unavailable";
02379 case AST_CAUSE_CALL_REJECTED:
02380 return "403 Forbidden";
02381 case AST_CAUSE_NUMBER_CHANGED:
02382 return "410 Gone";
02383 case AST_CAUSE_NORMAL_UNSPECIFIED:
02384 return "480 Temporarily unavailable";
02385 case AST_CAUSE_INVALID_NUMBER_FORMAT:
02386 return "484 Address incomplete";
02387 case AST_CAUSE_USER_BUSY:
02388 return "486 Busy here";
02389 case AST_CAUSE_FAILURE:
02390 return "500 Server internal failure";
02391 case AST_CAUSE_FACILITY_REJECTED:
02392 return "501 Not Implemented";
02393 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
02394 return "503 Service Unavailable";
02395
02396 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
02397 return "502 Bad Gateway";
02398 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
02399 return "488 Not Acceptable Here";
02400
02401 case AST_CAUSE_NOTDEFINED:
02402 default:
02403 ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
02404 return NULL;
02405 }
02406
02407
02408 return 0;
02409 }
02410
02411
02412
02413
02414 static int sip_hangup(struct ast_channel *ast)
02415 {
02416 struct sip_pvt *p = ast->tech_pvt;
02417 int needcancel = 0;
02418 int needdestroy = 0;
02419
02420 if (!p) {
02421 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
02422 return 0;
02423 }
02424 if (option_debug)
02425 ast_log(LOG_DEBUG, "Hangup call %s, SIP callid %s)\n", ast->name, p->callid);
02426
02427 ast_mutex_lock(&p->lock);
02428 #ifdef OSP_SUPPORT
02429 if ((p->osphandle > -1) && (ast->_state == AST_STATE_UP)) {
02430 ast_osp_terminate(p->osphandle, AST_CAUSE_NORMAL, p->ospstart, time(NULL) - p->ospstart);
02431 }
02432 #endif
02433 ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter\n", p->username);
02434 update_call_counter(p, DEC_CALL_LIMIT);
02435
02436 if (p->owner != ast) {
02437 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
02438 ast_mutex_unlock(&p->lock);
02439 return 0;
02440 }
02441
02442 if (ast->_state != AST_STATE_UP)
02443 needcancel = 1;
02444
02445
02446 if (p->vad) {
02447 ast_dsp_free(p->vad);
02448 }
02449 p->owner = NULL;
02450 ast->tech_pvt = NULL;
02451
02452 ast_mutex_lock(&usecnt_lock);
02453 usecnt--;
02454 ast_mutex_unlock(&usecnt_lock);
02455 ast_update_use_count();
02456
02457
02458
02459
02460
02461
02462
02463 if (ast_test_flag(p, SIP_ALREADYGONE))
02464 needdestroy = 1;
02465 else
02466 sip_scheddestroy(p, 32000);
02467
02468
02469 if (!ast_test_flag(p, SIP_ALREADYGONE) && !ast_strlen_zero(p->initreq.data)) {
02470 if (needcancel) {
02471 if (ast_test_flag(p, SIP_OUTGOING)) {
02472
02473 __sip_pretend_ack(p);
02474
02475
02476
02477 if (!ast_test_flag(p, SIP_CAN_BYE)) {
02478 ast_set_flag(p, SIP_PENDINGBYE);
02479
02480 } else {
02481
02482 transmit_request_with_auth(p, SIP_CANCEL, p->ocseq, 1, 0);
02483
02484
02485 }
02486 if ( p->initid != -1 ) {
02487
02488
02489 update_call_counter(p, INC_CALL_LIMIT);
02490 }
02491 } else {
02492 char *res;
02493 if (ast->hangupcause && ((res = hangup_cause2sip(ast->hangupcause)))) {
02494 transmit_response_reliable(p, res, &p->initreq, 1);
02495 } else
02496 transmit_response_reliable(p, "603 Declined", &p->initreq, 1);
02497 }
02498 } else {
02499 if (!p->pendinginvite) {
02500
02501 transmit_request_with_auth(p, SIP_BYE, 0, 1, 1);
02502 } else {
02503
02504
02505 ast_set_flag(p, SIP_PENDINGBYE);
02506 ast_clear_flag(p, SIP_NEEDREINVITE);
02507 }
02508 }
02509 }
02510 if (needdestroy)
02511 ast_set_flag(p, SIP_NEEDDESTROY);
02512 ast_mutex_unlock(&p->lock);
02513 return 0;
02514 }
02515
02516
02517 static void try_suggested_sip_codec(struct sip_pvt *p)
02518 {
02519 int fmt;
02520 char *codec;
02521
02522 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
02523 if (!codec)
02524 return;
02525
02526 fmt = ast_getformatbyname(codec);
02527 if (fmt) {
02528 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC) variable\n",codec);
02529 if (p->jointcapability & fmt) {
02530 p->jointcapability &= fmt;
02531 p->capability &= fmt;
02532 } else
02533 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
02534 } else
02535 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n",codec);
02536 return;
02537 }
02538
02539
02540
02541 static int sip_answer(struct ast_channel *ast)
02542 {
02543 int res = 0;
02544 struct sip_pvt *p = ast->tech_pvt;
02545
02546 ast_mutex_lock(&p->lock);
02547 if (ast->_state != AST_STATE_UP) {
02548 #ifdef OSP_SUPPORT
02549 time(&p->ospstart);
02550 #endif
02551 try_suggested_sip_codec(p);
02552
02553 ast_setstate(ast, AST_STATE_UP);
02554 if (option_debug)
02555 ast_log(LOG_DEBUG, "sip_answer(%s)\n", ast->name);
02556 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, 1);
02557 }
02558 ast_mutex_unlock(&p->lock);
02559 return res;
02560 }
02561
02562
02563 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
02564 {
02565 struct sip_pvt *p = ast->tech_pvt;
02566 int res = 0;
02567 switch (frame->frametype) {
02568 case AST_FRAME_VOICE:
02569 if (!(frame->subclass & ast->nativeformats)) {
02570 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
02571 frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
02572 return 0;
02573 }
02574 if (p) {
02575 ast_mutex_lock(&p->lock);
02576 if (p->rtp) {
02577
02578 if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
02579 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
02580 ast_set_flag(p, SIP_PROGRESS_SENT);
02581 }
02582 time(&p->lastrtptx);
02583 res = ast_rtp_write(p->rtp, frame);
02584 }
02585 ast_mutex_unlock(&p->lock);
02586 }
02587 break;
02588 case AST_FRAME_VIDEO:
02589 if (p) {
02590 ast_mutex_lock(&p->lock);
02591 if (p->vrtp) {
02592
02593 if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
02594 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
02595 ast_set_flag(p, SIP_PROGRESS_SENT);
02596 }
02597 time(&p->lastrtptx);
02598 res = ast_rtp_write(p->vrtp, frame);
02599 }
02600 ast_mutex_unlock(&p->lock);
02601 }
02602 break;
02603 case AST_FRAME_IMAGE:
02604 return 0;
02605 break;
02606 default:
02607 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
02608 return 0;
02609 }
02610
02611 return res;
02612 }
02613
02614
02615
02616 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
02617 {
02618 struct sip_pvt *p = newchan->tech_pvt;
02619 ast_mutex_lock(&p->lock);
02620 if (p->owner != oldchan) {
02621 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
02622 ast_mutex_unlock(&p->lock);
02623 return -1;
02624 }
02625 p->owner = newchan;
02626 ast_mutex_unlock(&p->lock);
02627 return 0;
02628 }
02629
02630
02631
02632 static int sip_senddigit(struct ast_channel *ast, char digit)
02633 {
02634 struct sip_pvt *p = ast->tech_pvt;
02635 int res = 0;
02636 ast_mutex_lock(&p->lock);
02637 switch (ast_test_flag(p, SIP_DTMF)) {
02638 case SIP_DTMF_INFO:
02639 transmit_info_with_digit(p, digit);
02640 break;
02641 case SIP_DTMF_RFC2833:
02642 if (p->rtp)
02643 ast_rtp_senddigit(p->rtp, digit);
02644 break;
02645 case SIP_DTMF_INBAND:
02646 res = -1;
02647 break;
02648 }
02649 ast_mutex_unlock(&p->lock);
02650 return res;
02651 }
02652
02653
02654
02655
02656 static int sip_transfer(struct ast_channel *ast, const char *dest)
02657 {
02658 struct sip_pvt *p = ast->tech_pvt;
02659 int res;
02660
02661 ast_mutex_lock(&p->lock);
02662 if (ast->_state == AST_STATE_RING)
02663 res = sip_sipredirect(p, dest);
02664 else
02665 res = transmit_refer(p, dest);
02666 ast_mutex_unlock(&p->lock);
02667 return res;
02668 }
02669
02670
02671
02672
02673 static int sip_indicate(struct ast_channel *ast, int condition)
02674 {
02675 struct sip_pvt *p = ast->tech_pvt;
02676 int res = 0;
02677
02678 ast_mutex_lock(&p->lock);
02679 switch(condition) {
02680 case AST_CONTROL_RINGING:
02681 if (ast->_state == AST_STATE_RING) {
02682 if (!ast_test_flag(p, SIP_PROGRESS_SENT) ||
02683 (ast_test_flag(p, SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
02684
02685 transmit_response(p, "180 Ringing", &p->initreq);
02686 ast_set_flag(p, SIP_RINGING);
02687 if (ast_test_flag(p, SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
02688 break;
02689 } else {
02690
02691 }
02692 }
02693 res = -1;
02694 break;
02695 case AST_CONTROL_BUSY:
02696 if (ast->_state != AST_STATE_UP) {
02697 transmit_response(p, "486 Busy Here", &p->initreq);
02698 ast_set_flag(p, SIP_ALREADYGONE);
02699 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
02700 break;
02701 }
02702 res = -1;
02703 break;
02704 case AST_CONTROL_CONGESTION:
02705 if (ast->_state != AST_STATE_UP) {
02706 transmit_response(p, "503 Service Unavailable", &p->initreq);
02707 ast_set_flag(p, SIP_ALREADYGONE);
02708 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
02709 break;
02710 }
02711 res = -1;
02712 break;
02713 case AST_CONTROL_PROCEEDING:
02714 if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
02715 transmit_response(p, "100 Trying", &p->initreq);
02716 break;
02717 }
02718 res = -1;
02719 break;
02720 case AST_CONTROL_PROGRESS:
02721 if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
02722 transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
02723 ast_set_flag(p, SIP_PROGRESS_SENT);
02724 break;
02725 }
02726 res = -1;
02727 break;
02728 case AST_CONTROL_HOLD:
02729 if (sipdebug)
02730 ast_log(LOG_DEBUG, "Bridged channel now on hold%s\n", p->callid);
02731 res = -1;
02732 break;
02733 case AST_CONTROL_UNHOLD:
02734 if (sipdebug)
02735 ast_log(LOG_DEBUG, "Bridged channel is back from hold, let's talk! : %s\n", p->callid);
02736 res = -1;
02737 break;
02738 case AST_CONTROL_VIDUPDATE:
02739 if (p->vrtp && !ast_test_flag(p, SIP_NOVIDEO)) {
02740 transmit_info_with_vidupdate(p);
02741 res = 0;
02742 } else
02743 res = -1;
02744 break;
02745 case -1:
02746 res = -1;
02747 break;
02748 default:
02749 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
02750 res = -1;
02751 break;
02752 }
02753 ast_mutex_unlock(&p->lock);
02754 return res;
02755 }
02756
02757
02758
02759
02760
02761 static struct ast_channel *sip_new(struct sip_pvt *i, int state, char *title)
02762 {
02763 struct ast_channel *tmp;
02764 struct ast_variable *v = NULL;
02765 int fmt;
02766 #ifdef OSP_SUPPORT
02767 char iabuf[INET_ADDRSTRLEN];
02768 char peer[MAXHOSTNAMELEN];
02769 #endif
02770
02771 ast_mutex_unlock(&i->lock);
02772
02773 tmp = ast_channel_alloc(1);
02774 ast_mutex_lock(&i->lock);
02775 if (!tmp) {
02776 ast_log(LOG_WARNING, "Unable to allocate SIP channel structure\n");
02777 return NULL;
02778 }
02779 tmp->tech = &sip_tech;
02780
02781
02782 if (i->jointcapability)
02783 tmp->nativeformats = ast_codec_choose(&i->prefs, i->jointcapability, 1);
02784 else if (i->capability)
02785 tmp->nativeformats = ast_codec_choose(&i->prefs, i->capability, 1);
02786 else
02787 tmp->nativeformats = ast_codec_choose(&i->prefs, global_capability, 1);
02788 fmt = ast_best_codec(tmp->nativeformats);
02789
02790 if (title)
02791 snprintf(tmp->name, sizeof(tmp->name), "SIP/%s-%08x", title, (int)(long) i);
02792 else if (strchr(i->fromdomain,':'))
02793 snprintf(tmp->name, sizeof(tmp->name), "SIP/%s-%08x", strchr(i->fromdomain,':') + 1, (int)(long) i);
02794 else
02795 snprintf(tmp->name, sizeof(tmp->name), "SIP/%s-%08x", i->fromdomain, (int)(long) i);
02796
02797 tmp->type = channeltype;
02798 if (ast_test_flag(i, SIP_DTMF) == SIP_DTMF_INBAND) {
02799 i->vad = ast_dsp_new();
02800 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
02801 if (relaxdtmf)
02802 ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
02803 }
02804 if (i->rtp) {
02805 tmp->fds[0] = ast_rtp_fd(i->rtp);
02806 tmp->fds[1] = ast_rtcp_fd(i->rtp);
02807 }
02808 if (i->vrtp) {
02809 tmp->fds[2] = ast_rtp_fd(i->vrtp);
02810 tmp->fds[3] = ast_rtcp_fd(i->vrtp);
02811 }
02812 if (state == AST_STATE_RING)
02813 tmp->rings = 1;
02814 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
02815 tmp->writeformat = fmt;
02816 tmp->rawwriteformat = fmt;
02817 tmp->readformat = fmt;
02818 tmp->rawreadformat = fmt;
02819 tmp->tech_pvt = i;
02820
02821 tmp->callgroup = i->callgroup;
02822 tmp->pickupgroup = i->pickupgroup;
02823 tmp->cid.cid_pres = i->callingpres;
02824 if (!ast_strlen_zero(i->accountcode))
02825 ast_copy_string(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode));
02826 if (i->amaflags)
02827 tmp->amaflags = i->amaflags;
02828 if (!ast_strlen_zero(i->language))
02829 ast_copy_string(tmp->language, i->language, sizeof(tmp->language));
02830 if (!ast_strlen_zero(i->musicclass))
02831 ast_copy_string(tmp->musicclass, i->musicclass, sizeof(tmp->musicclass));
02832 i->owner = tmp;
02833 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
02834 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
02835
02836 if (!ast_strlen_zero(i->cid_num))
02837 tmp->cid.cid_num = strdup(i->cid_num);
02838 if (!ast_strlen_zero(i->cid_name))
02839 tmp->cid.cid_name = strdup(i->cid_name);
02840 if (!ast_strlen_zero(i->rdnis))
02841 tmp->cid.cid_rdnis = strdup(i->rdnis);
02842 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
02843 tmp->cid.cid_dnid = strdup(i->exten);
02844
02845 tmp->priority = 1;
02846 if (!ast_strlen_zero(i->uri)) {
02847 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
02848 }
02849 if (!ast_strlen_zero(i->domain)) {
02850 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
02851 }
02852 if (!ast_strlen_zero(i->useragent)) {
02853 pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
02854 }
02855 if (!ast_strlen_zero(i->callid)) {
02856 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
02857 }
02858 #ifdef OSP_SUPPORT
02859 snprintf(peer, sizeof(peer), "[%s]:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), i->sa.sin_addr), ntohs(i->sa.sin_port));
02860 pbx_builtin_setvar_helper(tmp, "OSPPEER", peer);
02861 #endif
02862 ast_setstate(tmp, state);
02863 if (state != AST_STATE_DOWN) {
02864 if (ast_pbx_start(tmp)) {
02865 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
02866 ast_hangup(tmp);
02867 tmp = NULL;
02868 }
02869 }
02870
02871 for (v = i->chanvars ; v ; v = v->next)
02872 pbx_builtin_setvar_helper(tmp,v->name,v->value);
02873
02874 ast_mutex_lock(&usecnt_lock);
02875 usecnt++;
02876 ast_mutex_unlock(&usecnt_lock);
02877 ast_update_use_count();
02878
02879 return tmp;
02880 }
02881
02882
02883 static char *get_body_by_line(char *line, char *name, int nameLen)
02884 {
02885 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=') {
02886 return ast_skip_blanks(line + nameLen + 1);
02887 }
02888 return "";
02889 }
02890
02891
02892 static char *get_sdp(struct sip_request *req, char *name)
02893 {
02894 int x;
02895 int len = strlen(name);
02896 char *r;
02897
02898 for (x = req->sdp_start; x < req->sdp_end; x++) {
02899 r = get_body_by_line(req->line[x], name, len);
02900 if (r[0] != '\0')
02901 return r;
02902 }
02903 return "";
02904 }
02905
02906 static void sdpLineNum_iterator_init(int *iterator, struct sip_request *req)
02907 {
02908 *iterator = req->sdp_start;
02909 }
02910
02911 static char *get_sdp_iterate(int *iterator,
02912 struct sip_request *req, char *name)
02913 {
02914 int len = strlen(name);
02915 char *r;
02916
02917 while (*iterator < req->sdp_end) {
02918 r = get_body_by_line(req->line[(*iterator)++], name, len);
02919 if (r[0] != '\0')
02920 return r;
02921 }
02922 return "";
02923 }
02924
02925
02926 static char *get_body(struct sip_request *req, char *name)
02927 {
02928 int x;
02929 int len = strlen(name);
02930 char *r;
02931
02932 for (x = 0; x < req->lines; x++) {
02933 r = get_body_by_line(req->line[x], name, len);
02934 if (r[0] != '\0')
02935 return r;
02936 }
02937 return "";
02938 }
02939
02940 static char *find_alias(const char *name, char *_default)
02941 {
02942 int x;
02943 for (x=0;x<sizeof(aliases) / sizeof(aliases[0]); x++)
02944 if (!strcasecmp(aliases[x].fullname, name))
02945 return aliases[x].shortname;
02946 return _default;
02947 }
02948
02949 static char *__get_header(struct sip_request *req, char *name, int *start)
02950 {
02951 int pass;
02952
02953
02954
02955
02956
02957
02958
02959
02960
02961
02962 for (pass = 0; name && pass < 2;pass++) {
02963 int x, len = strlen(name);
02964 for (x=*start; x<req->headers; x++) {
02965 if (!strncasecmp(req->header[x], name, len)) {
02966 char *r = req->header[x] + len;
02967 if (pedanticsipchecking)
02968 r = ast_skip_blanks(r);
02969
02970 if (*r == ':') {
02971 *start = x+1;
02972 return ast_skip_blanks(r+1);
02973 }
02974 }
02975 }
02976 if (pass == 0)
02977 name = find_alias(name, NULL);
02978 }
02979
02980
02981 return "";
02982 }
02983
02984
02985 static char *get_header(struct sip_request *req, char *name)
02986 {
02987 int start = 0;
02988 return __get_header(req, name, &start);
02989 }
02990
02991
02992 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p)
02993 {
02994
02995 struct ast_frame *f;
02996 static struct ast_frame null_frame = { AST_FRAME_NULL, };
02997
02998 if (!p->rtp) {
02999
03000 return &null_frame;
03001 }
03002
03003 switch(ast->fdno) {
03004 case 0:
03005 f = ast_rtp_read(p->rtp);
03006 break;
03007 case 1:
03008 f = ast_rtcp_read(p->rtp);
03009 break;
03010 case 2:
03011 f = ast_rtp_read(p->vrtp);
03012 break;
03013 case 3:
03014 f = ast_rtcp_read(p->vrtp);
03015 break;
03016 default:
03017 f = &null_frame;
03018 }
03019
03020 if (f && (f->frametype == AST_FRAME_DTMF) && (ast_test_flag(p, SIP_DTMF) != SIP_DTMF_RFC2833))
03021 return &null_frame;
03022 if (p->owner) {
03023
03024 if (f->frametype == AST_FRAME_VOICE) {
03025 if (f->subclass != p->owner->nativeformats) {
03026 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
03027 p->owner->nativeformats = f->subclass;
03028 ast_set_read_format(p->owner, p->owner->readformat);
03029 ast_set_write_format(p->owner, p->owner->writeformat);
03030 }
03031 if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
03032 f = ast_dsp_process(p->owner, p->vad, f);
03033 if (f && (f->frametype == AST_FRAME_DTMF))
03034 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
03035 }
03036 }
03037 }
03038 return f;
03039 }
03040
03041
03042 static struct ast_frame *sip_read(struct ast_channel *ast)
03043 {
03044 struct ast_frame *fr;
03045 struct sip_pvt *p = ast->tech_pvt;
03046 ast_mutex_lock(&p->lock);
03047 fr = sip_rtp_read(ast, p);
03048 time(&p->lastrtprx);
03049 ast_mutex_unlock(&p->lock);
03050 return fr;
03051 }
03052
03053
03054 static void build_callid(char *callid, int len, struct in_addr ourip, char *fromdomain)
03055 {
03056 int res;
03057 int val;
03058 int x;
03059 char iabuf[INET_ADDRSTRLEN];
03060 for (x=0; x<4; x++) {
03061 val = thread_safe_rand();
03062 res = snprintf(callid, len, "%08x", val);
03063 len -= res;
03064 callid += res;
03065 }
03066 if (!ast_strlen_zero(fromdomain))
03067 snprintf(callid, len, "@%s", fromdomain);
03068 else
03069
03070 snprintf(callid, len, "@%s", ast_inet_ntoa(iabuf, sizeof(iabuf), ourip));
03071 }
03072
03073 static void make_our_tag(char *tagbuf, size_t len)
03074 {
03075 snprintf(tagbuf, len, "as%08x", thread_safe_rand());
03076 }
03077
03078
03079 static struct sip_pvt *sip_alloc(char *callid, struct sockaddr_in *sin, int useglobal_nat, const int intended_method)
03080 {
03081 struct sip_pvt *p;
03082
03083 if (!(p = calloc(1, sizeof(*p))))
03084 return NULL;
03085
03086 ast_mutex_init(&p->lock);
03087
03088 p->method = intended_method;
03089 p->initid = -1;
03090 p->autokillid = -1;
03091 p->subscribed = NONE;
03092 p->stateid = -1;
03093 p->prefs = prefs;
03094 if (intended_method != SIP_OPTIONS)
03095 p->timer_t1 = 500;
03096 #ifdef OSP_SUPPORT
03097 p->osphandle = -1;
03098 p->osptimelimit = 0;
03099 #endif
03100 if (sin) {
03101 memcpy(&p->sa, sin, sizeof(p->sa));
03102 if (ast_sip_ouraddrfor(&p->sa.sin_addr,&p->ourip))
03103 memcpy(&p->ourip, &__ourip, sizeof(p->ourip));
03104 } else {
03105 memcpy(&p->ourip, &__ourip, sizeof(p->ourip));
03106 }
03107
03108 p->branch = thread_safe_rand();
03109 make_our_tag(p->tag, sizeof(p->tag));
03110
03111 p->ocseq = 101;
03112
03113 if (sip_methods[intended_method].need_rtp) {
03114 p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
03115 if (videosupport)
03116 p->vrtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
03117 if (!p->rtp || (videosupport && !p->vrtp)) {
03118 ast_log(LOG_WARNING, "Unable to create RTP audio %s session: %s\n", videosupport ? "and video" : "", strerror(errno));
03119 ast_mutex_destroy(&p->lock);
03120 if (p->chanvars) {
03121 ast_variables_destroy(p->chanvars);
03122 p->chanvars = NULL;
03123 }
03124 free(p);
03125 return NULL;
03126 }
03127 ast_rtp_settos(p->rtp, tos);
03128 if (p->vrtp)
03129 ast_rtp_settos(p->vrtp, tos);
03130 p->rtptimeout = global_rtptimeout;
03131 p->rtpholdtimeout = global_rtpholdtimeout;
03132 p->rtpkeepalive = global_rtpkeepalive;
03133 }
03134
03135 if (useglobal_nat && sin) {
03136
03137 ast_copy_flags(p, &global_flags, SIP_NAT);
03138 memcpy(&p->recv, sin, sizeof(p->recv));
03139 if (p->rtp)
03140 ast_rtp_setnat(p->rtp, (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
03141 if (p->vrtp)
03142 ast_rtp_setnat(p->vrtp, (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
03143 }
03144
03145 if (p->method != SIP_REGISTER)
03146 ast_copy_string(p->fromdomain, default_fromdomain, sizeof(p->fromdomain));
03147 build_via(p, p->via, sizeof(p->via));
03148 if (!callid)
03149 build_callid(p->callid, sizeof(p->callid), p->ourip, p->fromdomain);
03150 else
03151 ast_copy_string(p->callid, callid, sizeof(p->callid));
03152 ast_copy_flags(p, &global_flags, SIP_FLAGS_TO_COPY);
03153
03154 strcpy(p->musicclass, global_musicclass);
03155 p->capability = global_capability;
03156 if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_RFC2833) || (ast_test_flag(p, SIP_DTMF) == SIP_DTMF_AUTO))
03157 p->noncodeccapability |= AST_RTP_DTMF;
03158 strcpy(p->context, default_context);
03159
03160
03161 ast_mutex_lock(&iflock);
03162 p->next = iflist;
03163 iflist = p;
03164 ast_mutex_unlock(&iflock);
03165 if (option_debug)
03166 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");
03167 return p;
03168 }
03169
03170
03171
03172 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
03173 {
03174 struct sip_pvt *p;
03175 char *callid;
03176 char *tag = "";
03177 char totag[128];
03178 char fromtag[128];
03179
03180 callid = get_header(req, "Call-ID");
03181
03182 if (pedanticsipchecking) {
03183
03184
03185
03186
03187
03188
03189 if (gettag(req, "To", totag, sizeof(totag)))
03190 ast_set_flag(req, SIP_PKT_WITH_TOTAG);
03191 gettag(req, "From", fromtag, sizeof(fromtag));
03192
03193 if (req->method == SIP_RESPONSE)
03194 tag = totag;
03195 else
03196 tag = fromtag;
03197
03198
03199 if (option_debug > 4 )
03200 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);
03201 }
03202
03203 ast_mutex_lock(&iflock);
03204 p = iflist;
03205 while(p) {
03206 int found = 0;
03207 if (req->method == SIP_REGISTER)
03208 found = (!strcmp(p->callid, callid));
03209 else
03210 found = (!strcmp(p->callid, callid) &&
03211 (!pedanticsipchecking || !tag || ast_strlen_zero(p->theirtag) || !strcmp(p->theirtag, tag))) ;
03212
03213 if (option_debug > 4)
03214 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);
03215
03216
03217 if (pedanticsipchecking && found && req->method != SIP_RESPONSE) {
03218 if (p->tag[0] == '\0' && totag[0]) {
03219
03220 found = 0;
03221 } else if (totag[0]) {
03222 if (strcmp(totag, p->tag)) {
03223 found = 0;
03224 }
03225 }
03226 if (!found && option_debug > 4)
03227 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);
03228 }
03229
03230
03231 if (found) {
03232
03233 ast_mutex_lock(&p->lock);
03234 ast_mutex_unlock(&iflock);
03235 return p;
03236 }
03237 p = p->next;
03238 }
03239 ast_mutex_unlock(&iflock);
03240 p = sip_alloc(callid, sin, 1, intended_method);
03241 if (p)
03242 ast_mutex_lock(&p->lock);
03243 return p;
03244 }
03245
03246
03247 static int sip_register(char *value, int lineno)
03248 {
03249 struct sip_registry *reg;
03250 char copy[256];
03251 char *username=NULL, *hostname=NULL, *secret=NULL, *authuser=NULL;
03252 char *porta=NULL;
03253 char *contact=NULL;
03254 char *stringp=NULL;
03255
03256 if (!value)
03257 return -1;
03258 ast_copy_string(copy, value, sizeof(copy));
03259 stringp=copy;
03260 username = stringp;
03261 hostname = strrchr(stringp, '@');
03262 if (hostname) {
03263 *hostname = '\0';
03264 hostname++;
03265 }
03266 if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
03267 ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
03268 return -1;
03269 }
03270 stringp=username;
03271 username = strsep(&stringp, ":");
03272 if (username) {
03273 secret = strsep(&stringp, ":");
03274 if (secret)
03275 authuser = strsep(&stringp, ":");
03276 }
03277 stringp = hostname;
03278 hostname = strsep(&stringp, "/");
03279 if (hostname)
03280 contact = strsep(&stringp, "/");
03281 if (ast_strlen_zero(contact))
03282 contact = "s";
03283 stringp=hostname;
03284 hostname = strsep(&stringp, ":");
03285 porta = strsep(&stringp, ":");
03286
03287 if (porta && !atoi(porta)) {
03288 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
03289 return -1;
03290 }
03291 reg = malloc(sizeof(struct sip_registry));
03292 if (!reg) {
03293 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
03294 return -1;
03295 }
03296 memset(reg, 0, sizeof(struct sip_registry));
03297 regobjs++;
03298 ASTOBJ_INIT(reg);
03299 ast_copy_string(reg->contact, contact, sizeof(reg->contact));
03300 if (username)
03301 ast_copy_string(reg->username, username, sizeof(reg->username));
03302 if (hostname)
03303 ast_copy_string(reg->hostname, hostname, sizeof(reg->hostname));
03304 if (authuser)
03305 ast_copy_string(reg->authuser, authuser, sizeof(reg->authuser));
03306 if (secret)
03307 ast_copy_string(reg->secret, secret, sizeof(reg->secret));
03308 reg->expire = -1;
03309 reg->timeout = -1;
03310 reg->refresh = default_expiry;
03311 reg->portno = porta ? atoi(porta) : 0;
03312 reg->callid_valid = 0;
03313 reg->ocseq = 101;
03314 ASTOBJ_CONTAINER_LINK(®l, reg);
03315 ASTOBJ_UNREF(reg,sip_registry_destroy);
03316 return 0;
03317 }
03318
03319
03320
03321 static int lws2sws(char *msgbuf, int len)
03322 {
03323 int h = 0, t = 0;
03324 int lws = 0;
03325
03326 for (; h < len;) {
03327
03328 if (msgbuf[h] == '\r') {
03329 h++;
03330 continue;
03331 }
03332
03333 if (msgbuf[h] == '\n') {
03334
03335 if (h + 1 == len)
03336 break;
03337
03338 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
03339
03340 h++;
03341 continue;
03342 }
03343
03344 msgbuf[t++] = msgbuf[h++];
03345 lws = 0;
03346 continue;
03347 }
03348 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
03349 if (lws) {
03350 h++;
03351 continue;
03352 }
03353 msgbuf[t++] = msgbuf[h++];
03354 lws = 1;
03355 continue;
03356 }
03357 msgbuf[t++] = msgbuf[h++];
03358 if (lws)
03359 lws = 0;
03360 }
03361 msgbuf[t] = '\0';
03362 return t;
03363 }
03364
03365
03366 static void parse_request(struct sip_request *req)
03367 {
03368
03369 char *c;
03370 int f = 0;
03371
03372 c = req->data;
03373
03374
03375 req->header[f] = c;
03376 while(*c) {
03377 if (*c == '\n') {
03378
03379 *c = 0;
03380
03381 if (sipdebug && option_debug > 3)
03382 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
03383 if (ast_strlen_zero(req->header[f])) {
03384
03385 c++;
03386 break;
03387 }
03388 if (f >= SIP_MAX_HEADERS - 1) {
03389 ast_log(LOG_WARNING, "Too many SIP headers. Ignoring.\n");
03390 } else
03391 f++;
03392 req->header[f] = c + 1;
03393 } else if (*c == '\r') {
03394
03395 *c = 0;
03396 }
03397 c++;
03398 }
03399
03400 if (!ast_strlen_zero(req->header[f])) {
03401 if (sipdebug && option_debug > 3)
03402 ast_log(LOG_DEBUG, "Header %d: %s (%d)\n", f, req->header[f], (int) strlen(req->header[f]));
03403 f++;
03404 }
03405 req->headers = f;
03406
03407 f = 0;
03408 req->line[f] = c;
03409 while(*c) {
03410 if (*c == '\n') {
03411
03412 *c = 0;
03413 if (sipdebug && option_debug > 3)
03414 ast_log(LOG_DEBUG, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
03415 if (f >= SIP_MAX_LINES - 1) {
03416 ast_log(LOG_WARNING, "Too many SDP lines. Ignoring.\n");
03417 } else
03418 f++;
03419 req->line[f] = c + 1;
03420 } else if (*c == '\r') {
03421
03422 *c = 0;
03423 }
03424 c++;
03425 }
03426
03427 if (!ast_strlen_zero(req->line[f]))
03428 f++;
03429 req->lines = f;
03430 if (*c)
03431 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
03432
03433 determine_firstline_parts(req);
03434 }
03435
03436
03437
03438
03439
03440
03441
03442
03443
03444 static int find_sdp(struct sip_request *req)
03445 {
03446 char *content_type;
03447 char *search;
03448 char *boundary;
03449 unsigned int x;
03450
03451 content_type = get_header(req, "Content-Type");
03452
03453
03454 if (!strcasecmp(content_type, "application/sdp")) {
03455 req->sdp_start = 0;
03456 req->sdp_end = req->lines;
03457 return 1;
03458 }
03459
03460
03461 if (strncasecmp(content_type, "multipart/mixed", 15))
03462 return 0;
03463
03464
03465 if (!(search = strcasestr(content_type, ";boundary=")))
03466 return 0;
03467
03468 search += 10;
03469
03470 if (ast_strlen_zero(search))
03471 return 0;
03472
03473
03474
03475 boundary = ast_strdupa(search - 2);
03476 boundary[0] = boundary[1] = '-';
03477
03478
03479
03480
03481 for (x = 0; x < (req->lines - 2); x++) {
03482 if (!strncasecmp(req->line[x], boundary, strlen(boundary)) &&
03483 !strcasecmp(req->line[x + 1], "Content-Type: application/sdp")) {
03484 req->sdp_start = x + 2;
03485
03486 for ( ; x < req->lines; x++) {
03487 if (!strncasecmp(req->line[x], boundary, strlen(boundary)))
03488 break;
03489 }
03490 req->sdp_end = x;
03491 return 1;
03492 }
03493 }
03494
03495 return 0;
03496 }
03497
03498
03499 static int process_sdp(struct sip_pvt *p, struct sip_request *req)
03500 {
03501 char *m;
03502 char *c;
03503 char *a;
03504 char host[258];
03505 char iabuf[INET_ADDRSTRLEN];
03506 int len = -1;
03507 int portno = -1;
03508 int vportno = -1;
03509 int peercapability, peernoncodeccapability;
03510 int vpeercapability=0, vpeernoncodeccapability=0;
03511 struct sockaddr_in sin;
03512 char *codecs;
03513 struct hostent *hp;
03514 struct ast_hostent ahp;
03515 int codec;
03516 int destiterator = 0;
03517 int iterator;
03518 int sendonly = 0;
03519 int x,y;
03520 int debug=sip_debug_test_pvt(p);
03521 struct ast_channel *bridgepeer = NULL;
03522
03523 if (!p->rtp) {
03524 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
03525 return -1;
03526 }
03527
03528
03529 time(&p->lastrtprx);
03530 time(&p->lastrtptx);
03531
03532 m = get_sdp(req, "m");
03533 sdpLineNum_iterator_init(&destiterator, req);
03534 c = get_sdp_iterate(&destiterator, req, "c");
03535 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
03536 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
03537 return -1;
03538 }
03539 if (sscanf(c, "IN IP4 %256s", host) != 1) {
03540 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
03541 return -1;
03542 }
03543
03544 hp = ast_gethostbyname(host, &ahp);
03545 if (!hp) {
03546 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
03547 return -1;
03548 }
03549 sdpLineNum_iterator_init(&iterator, req);
03550 ast_set_flag(p, SIP_NOVIDEO);
03551 while ((m = get_sdp_iterate(&iterator, req, "m"))[0] != '\0') {
03552 int found = 0;
03553 if ((sscanf(m, "audio %d/%d RTP/AVP %n", &x, &y, &len) == 2) ||
03554 (sscanf(m, "audio %d RTP/AVP %n", &x, &len) == 1)) {
03555 found = 1;
03556 portno = x;
03557
03558 ast_rtp_pt_clear(p->rtp);
03559 codecs = m + len;
03560 while(!ast_strlen_zero(codecs)) {
03561 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
03562 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
03563 return -1;
03564 }
03565 if (debug)
03566 ast_verbose("Found RTP audio format %d\n", codec);
03567 ast_rtp_set_m_type(p->rtp, codec);
03568 codecs = ast_skip_blanks(codecs + len);
03569 }
03570 }
03571 if (p->vrtp)
03572 ast_rtp_pt_clear(p->vrtp);
03573
03574 if (p->vrtp && (sscanf(m, "video %d RTP/AVP %n", &x, &len) == 1)) {
03575 found = 1;
03576 ast_clear_flag(p, SIP_NOVIDEO);
03577 vportno = x;
03578
03579 codecs = m + len;
03580 while(!ast_strlen_zero(codecs)) {
03581 if (sscanf(codecs, "%d%n", &codec, &len) != 1) {
03582 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
03583 return -1;
03584 }
03585 if (debug)
03586 ast_verbose("Found RTP video format %d\n", codec);
03587 ast_rtp_set_m_type(p->vrtp, codec);
03588 codecs = ast_skip_blanks(codecs + len);
03589 }
03590 }
03591 if (!found )
03592 ast_log(LOG_WARNING, "Unknown SDP media type in offer: %s\n", m);
03593 }
03594 if (portno == -1 && vportno == -1) {
03595
03596 return -2;
03597 }
03598
03599 c = get_sdp_iterate(&destiterator, req, "c");
03600 if (!ast_strlen_zero(c)) {
03601 if (sscanf(c, "IN IP4 %256s", host) != 1) {
03602 ast_log(LOG_WARNING, "Invalid secondary host in c= line, '%s'\n", c);
03603 } else {
03604
03605 hp = ast_gethostbyname(host, &ahp);
03606 if (!hp) {
03607 ast_log(LOG_WARNING, "Unable to lookup host in secondary c= line, '%s'\n", c);
03608 return -1;
03609 }
03610 }
03611 }
03612
03613 sin.sin_family = AF_INET;
03614 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
03615
03616
03617 sin.sin_port = htons(portno);
03618 if (p->rtp && sin.sin_port) {
03619 ast_rtp_set_peer(p->rtp, &sin);
03620 if (debug) {
03621 ast_verbose("Peer audio RTP is at port %s:%d\n", ast_inet_ntoa(iabuf,sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
03622 ast_log(LOG_DEBUG,"Peer audio RTP is at port %s:%d\n",ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
03623 }
03624 }
03625
03626 c = get_sdp_iterate(&destiterator, req, "c");
03627 if (!ast_strlen_zero(c)) {
03628 if (sscanf(c, "IN IP4 %256s", host) != 1) {
03629 ast_log(LOG_WARNING, "Invalid secondary host in c= line, '%s'\n", c);
03630 } else {
03631
03632 hp = ast_gethostbyname(host, &ahp);
03633 if (!hp) {
03634 ast_log(LOG_WARNING, "Unable to lookup host in secondary c= line, '%s'\n", c);
03635 return -1;
03636 }
03637 }
03638 }
03639
03640 sin.sin_port = htons(vportno);
03641 if (p->vrtp && sin.sin_port) {
03642 ast_rtp_set_peer(p->vrtp, &sin);
03643 if (debug) {
03644 ast_verbose("Peer video RTP is at port %s:%d\n", ast_inet_ntoa(iabuf,sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
03645 ast_log(LOG_DEBUG,"Peer video RTP is at port %s:%d\n",ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
03646 }
03647 }
03648
03649
03650
03651
03652 sdpLineNum_iterator_init(&iterator, req);
03653 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
03654 char* mimeSubtype = ast_strdupa(a);
03655 if (!strcasecmp(a, "sendonly") || !strcasecmp(a, "inactive")) {
03656 sendonly = 1;
03657 continue;
03658 }
03659 if (!strcasecmp(a, "sendrecv")) {
03660 sendonly = 0;
03661 }
03662 if (sscanf(a, "rtpmap: %u %[^/]/", &codec, mimeSubtype) != 2) continue;
03663 if (debug)
03664 ast_verbose("Found description format %s\n", mimeSubtype);
03665
03666 ast_rtp_set_rtpmap_type(p->rtp, codec, "audio", mimeSubtype);
03667 if (p->vrtp)
03668 ast_rtp_set_rtpmap_type(p->vrtp, codec, "video", mimeSubtype);
03669 }
03670
03671
03672 ast_rtp_get_current_formats(p->rtp,
03673 &peercapability, &peernoncodeccapability);
03674 if (p->vrtp)
03675 ast_rtp_get_current_formats(p->vrtp,
03676 &vpeercapability, &vpeernoncodeccapability);
03677 p->jointcapability = p->capability & (peercapability | vpeercapability);
03678 p->peercapability = (peercapability | vpeercapability);
03679 p->noncodeccapability = noncodeccapability & peernoncodeccapability;
03680
03681 if (ast_test_flag(p, SIP_DTMF) == SIP_DTMF_AUTO) {
03682 ast_clear_flag(p, SIP_DTMF);
03683 if (p->noncodeccapability & AST_RTP_DTMF) {
03684
03685 ast_set_flag(p, SIP_DTMF_RFC2833);
03686 } else {
03687 ast_set_flag(p, SIP_DTMF_INBAND);
03688 }
03689 }
03690
03691 if (debug) {
03692
03693 const unsigned slen=512;
03694 char s1[slen], s2[slen], s3[slen], s4[slen];
03695
03696 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s, combined - %s\n",
03697 ast_getformatname_multiple(s1, slen, p->capability),
03698 ast_getformatname_multiple(s2, slen, peercapability),
03699 ast_getformatname_multiple(s3, slen, vpeercapability),
03700 ast_getformatname_multiple(s4, slen, p->jointcapability));
03701
03702 ast_verbose("Non-codec capabilities: us - %s, peer - %s, combined - %s\n",
03703 ast_rtp_lookup_mime_multiple(s1, slen, noncodeccapability, 0),
03704 ast_rtp_lookup_mime_multiple(s2, slen, peernoncodeccapability, 0),
03705 ast_rtp_lookup_mime_multiple(s3, slen, p->noncodeccapability, 0));
03706 }
03707 if (!p->jointcapability) {
03708 ast_log(LOG_NOTICE, "No compatible codecs!\n");
03709 return -1;
03710 }
03711
03712 if (!p->owner)
03713 return 0;
03714
03715 if (!(p->owner->nativeformats & p->jointcapability)) {
03716 const unsigned slen=512;
03717 char s1[slen], s2[slen];
03718 ast_log(LOG_DEBUG, "Oooh, we need to change our formats since our peer supports only %s and not %s\n",
03719 ast_getformatname_multiple(s1, slen, p->jointcapability),
03720 ast_getformatname_multiple(s2, slen, p->owner->nativeformats));
03721 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1);
03722 ast_set_read_format(p->owner, p->owner->readformat);
03723 ast_set_write_format(p->owner, p->owner->writeformat);
03724 }
03725 if ((bridgepeer=ast_bridged_channel(p->owner))) {
03726
03727
03728 struct ast_frame af = { AST_FRAME_NULL, };
03729 if (sin.sin_addr.s_addr && !sendonly) {
03730 ast_moh_stop(bridgepeer);
03731
03732
03733 ast_queue_frame(p->owner, &af);
03734 } else {
03735
03736
03737 ast_moh_start(bridgepeer, NULL);
03738 if (sendonly)
03739 ast_rtp_stop(p->rtp);
03740
03741 ast_queue_frame(p->owner, &af);
03742 }
03743 }
03744
03745
03746 if (sin.sin_addr.s_addr && !sendonly) {
03747 append_history(p, "Unhold", req->data);
03748
03749 if (callevents && ast_test_flag(p, SIP_CALL_ONHOLD)) {
03750 manager_event(EVENT_FLAG_CALL, "Unhold",
03751 "Channel: %s\r\n"
03752 "Uniqueid: %s\r\n",
03753 p->owner->name,
03754 p->owner->uniqueid);
03755
03756 }
03757 ast_clear_flag(p, SIP_CALL_ONHOLD);
03758 } else {
03759
03760 append_history(p, "Hold", req->data);
03761
03762 if (callevents && !ast_test_flag(p, SIP_CALL_ONHOLD)) {
03763 manager_event(EVENT_FLAG_CALL, "Hold",
03764 "Channel: %s\r\n"
03765 "Uniqueid: %s\r\n",
03766 p->owner->name,
03767 p->owner->uniqueid);
03768 }
03769 ast_set_flag(p, SIP_CALL_ONHOLD);
03770 }
03771
03772 return 0;
03773 }
03774
03775
03776 static int add_header(struct sip_request *req, const char *var, const char *value)
03777 {
03778 int x = 0;
03779
03780 if (req->headers == SIP_MAX_HEADERS) {
03781 ast_log(LOG_WARNING, "Out of SIP header space\n");
03782 return -1;
03783 }
03784
03785 if (req->lines) {
03786 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
03787 return -1;
03788 }
03789
03790 if (req->len >= sizeof(req->data) - 4) {
03791 ast_log(LOG_WARNING, "Out of space, can't add anymore (%s:%s)\n", var, value);
03792 return -1;
03793 }
03794
03795 req->header[req->headers] = req->data + req->len;
03796
03797 if (compactheaders) {
03798 for (x = 0; x < (sizeof(aliases) / sizeof(aliases[0])); x++)
03799 if (!strcasecmp(aliases[x].fullname, var))
03800 var = aliases[x].shortname;
03801 }
03802
03803 snprintf(req->header[req->headers], sizeof(req->data) - req->len - 4, "%s: %s\r\n", var, value);
03804 req->len += strlen(req->header[req->headers]);
03805 req->headers++;
03806
03807 return 0;
03808 }
03809
03810
03811 static int add_header_contentLength(struct sip_request *req, int len)
03812 {
03813 char clen[10];
03814
03815 snprintf(clen, sizeof(clen), "%d", len);
03816 return add_header(req, "Content-Length", clen);
03817 }
03818
03819
03820 static int add_blank_header(struct sip_request *req)
03821 {
03822 if (req->headers == SIP_MAX_HEADERS) {
03823 ast_log(LOG_WARNING, "Out of SIP header space\n");
03824 return -1;
03825 }
03826 if (req->lines) {
03827 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
03828 return -1;
03829 }
03830 if (req->len >= sizeof(req->data) - 4) {
03831 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
03832 return -1;
03833 }
03834 req->header[req->headers] = req->data + req->len;
03835 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "\r\n");
03836 req->len += strlen(req->header[req->headers]);
03837 req->headers++;
03838 return 0;
03839 }
03840
03841
03842 static int add_line(struct sip_request *req, const char *line)
03843 {
03844 if (req->lines == SIP_MAX_LINES) {
03845 ast_log(LOG_WARNING, "Out of SIP line space\n");
03846 return -1;
03847 }
03848 if (!req->lines) {
03849
03850 snprintf(req->data + req->len, sizeof(req->data) - req->len, "\r\n");
03851 req->len += strlen(req->data + req->len);
03852 }
03853 if (req->len >= sizeof(req->data) - 4) {
03854 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
03855 return -1;
03856 }
03857 req->line[req->lines] = req->data + req->len;
03858 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
03859 req->len += strlen(req->line[req->lines]);
03860 req->lines++;
03861 return 0;
03862 }
03863
03864
03865 static int copy_header(struct sip_request *req, struct sip_request *orig, char *field)
03866 {
03867 char *tmp;
03868 tmp = get_header(orig, field);
03869 if (!ast_strlen_zero(tmp)) {
03870
03871 return add_header(req, field, tmp);
03872 }
03873 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
03874 return -1;
03875 }
03876
03877
03878 static int copy_all_header(struct sip_request *req, struct sip_request *orig, char *field)
03879 {
03880 char *tmp;
03881 int start = 0;
03882 int copied = 0;
03883 for (;;) {
03884 tmp = __get_header(orig, field, &start);
03885 if (!ast_strlen_zero(tmp)) {
03886
03887 add_header(req, field, tmp);
03888 copied++;
03889 } else
03890 break;
03891 }
03892 return copied ? 0 : -1;
03893 }
03894
03895
03896
03897
03898
03899
03900
03901 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, struct sip_request *orig, char *field)
03902 {
03903 char tmp[256], *oh, *end;
03904 int start = 0;
03905 int copied = 0;
03906 char iabuf[INET_ADDRSTRLEN];
03907
03908 for (;;) {
03909 oh = __get_header(orig, field, &start);
03910 if (!ast_strlen_zero(oh)) {
03911 if (!copied) {
03912 char *rport;
03913 char new[256];
03914
03915
03916 rport = strstr(oh, ";rport");
03917 if (rport && *(rport+6) == '=')
03918 rport = NULL;
03919
03920 if (rport && (ast_test_flag(p, SIP_NAT) == SIP_NAT_ALWAYS)) {
03921
03922 ast_copy_string(tmp, oh, sizeof(tmp));
03923
03924 rport = strstr(tmp, ";rport");
03925
03926 if (rport) {
03927 end = strchr(rport + 1, ';');
03928 if (end)
03929 memmove(rport, end, strlen(end) + 1);
03930 else
03931 *rport = '\0';
03932 }
03933
03934
03935
03936
03937
03938 snprintf(new, sizeof(new), "%s;received=%s;rport=%d", tmp, ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr), ntohs(p->recv.sin_port));
03939 } else {
03940
03941 snprintf(new, sizeof(new), "%s;received=%s", oh, ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr));
03942 }
03943 add_header(req, field, new);
03944 } else {
03945
03946 add_header(req, field, oh);
03947 }
03948 copied++;
03949 } else
03950 break;
03951 }
03952 if (!copied) {
03953 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
03954 return -1;
03955 }
03956 return 0;
03957 }
03958
03959
03960 static void add_route(struct sip_request *req, struct sip_route *route)
03961 {
03962 char r[BUFSIZ*2], *p;
03963 int n, rem = sizeof(r);
03964
03965 if (!route) return;
03966
03967 p = r;
03968 while (route) {
03969 n = strlen(route->hop);
03970 if ((n+3)>rem) break;
03971 if (p != r) {
03972 *p++ = ',';
03973 --rem;
03974 }
03975 *p++ = '<';
03976 ast_copy_string(p, route->hop, rem); p += n;
03977 *p++ = '>';
03978 rem -= (n+2);
03979 route = route->next;
03980 }
03981 *p = '\0';
03982 add_header(req, "Route", r);
03983 }
03984
03985
03986 static void set_destination(struct sip_pvt *p, char *uri)
03987 {
03988 char *h, *maddr, hostname[256];
03989 char iabuf[INET_ADDRSTRLEN];
03990 int port, hn;
03991 struct hostent *hp;
03992 struct ast_hostent ahp;
03993 int debug=sip_debug_test_pvt(p);
03994
03995
03996
03997
03998 if (debug)
03999 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
04000
04001
04002 h = strchr(uri, '@');
04003 if (h)
04004 ++h;
04005 else {
04006 h = uri;
04007 if (strncmp(h, "sip:", 4) == 0)
04008 h += 4;
04009 else if (strncmp(h, "sips:", 5) == 0)
04010 h += 5;
04011 }
04012 hn = strcspn(h, ":;>") + 1;
04013 if (hn > sizeof(hostname))
04014 hn = sizeof(hostname);
04015 ast_copy_string(hostname, h, hn);
04016 h += hn - 1;
04017
04018
04019 if (*h == ':') {
04020
04021 ++h;
04022 port = strtol(h, &h, 10);
04023 }
04024 else
04025 port = DEFAULT_SIP_PORT;
04026
04027
04028 maddr = strstr(h, "maddr=");
04029 if (maddr) {
04030 maddr += 6;
04031 hn = strspn(maddr, "0123456789.") + 1;
04032 if (hn > sizeof(hostname)) hn = sizeof(hostname);
04033 ast_copy_string(hostname, maddr, hn);
04034 }
04035
04036 hp = ast_gethostbyname(hostname, &ahp);
04037 if (hp == NULL) {
04038 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
04039 return;
04040 }
04041 p->sa.sin_family = AF_INET;
04042 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
04043 p->sa.sin_port = htons(port);
04044 if (debug)
04045 ast_verbose("set_destination: set destination to %s, port %d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), port);
04046 }
04047
04048
04049 static int init_resp(struct sip_request *req, char *resp, struct sip_request *orig)
04050 {
04051
04052 if (req->headers || req->len) {
04053 ast_log(LOG_WARNING, "Request already initialized?!?\n");
04054 return -1;
04055 }
04056 req->method = SIP_RESPONSE;
04057 req->header[req->headers] = req->data + req->len;
04058 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "SIP/2.0 %s\r\n", resp);
04059 req->len += strlen(req->header[req->headers]);
04060 req->headers++;
04061 return 0;
04062 }
04063
04064
04065 static int init_req(struct sip_request *req, int sipmethod, char *recip)
04066 {
04067
04068 if (req->headers || req->len) {
04069 ast_log(LOG_WARNING, "Request already initialized?!?\n");
04070 return -1;
04071 }
04072 req->header[req->headers] = req->data + req->len;
04073 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
04074 req->len += strlen(req->header[req->headers]);
04075 req->headers++;
04076 req->method = sipmethod;
04077 return 0;
04078 }
04079
04080
04081
04082 static int respprep(struct sip_request *resp, struct sip_pvt *p, char *msg, struct sip_request *req)
04083 {
04084 char newto[256], *ot;
04085
04086 memset(resp, 0, sizeof(*resp));
04087 init_resp(resp, msg, req);
04088 copy_via_headers(p, resp, req, "Via");
04089 if (msg[0] == '2')
04090 copy_all_header(resp, req, "Record-Route");
04091 copy_header(resp, req, "From");
04092 ot = get_header(req, "To");
04093 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
04094
04095
04096 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(p, SIP_OUTGOING))
04097 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
04098 else if (p->tag && !ast_test_flag(p, SIP_OUTGOING))
04099 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
04100 else {
04101 ast_copy_string(newto, ot, sizeof(newto));
04102 newto[sizeof(newto) - 1] = '\0';
04103 }
04104 ot = newto;
04105 }
04106 add_header(resp, "To", ot);
04107 copy_header(resp, req, "Call-ID");
04108 copy_header(resp, req, "CSeq");
04109 add_header(resp, "User-Agent", default_useragent);
04110 add_header(resp, "Allow", ALLOWED_METHODS);
04111 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
04112
04113
04114 char tmp[256];
04115
04116 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
04117 add_header(resp, "Expires", tmp);
04118 if (p->expiry) {
04119 char contact[SIP_LEN_CONTACT];
04120 snprintf(contact, sizeof(contact), "%s;expires=%d", p->our_contact, p->expiry);
04121 add_header(resp, "Contact", contact);
04122 }
04123 } else if (p->our_contact[0]) {
04124 add_header(resp, "Contact", p->our_contact);
04125 }
04126 return 0;
04127 }
04128
04129
04130 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
04131 {
04132 struct sip_request *orig = &p->initreq;
04133 char stripped[80];
04134 char tmp[80];
04135 char newto[256];
04136 char *c, *n;
04137 char *ot, *of;
04138 int is_strict = 0;
04139
04140 memset(req, 0, sizeof(struct sip_request));
04141
04142 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
04143
04144 if (!seqno) {
04145 p->ocseq++;
04146 seqno = p->ocseq;
04147 }
04148
04149 if (newbranch) {
04150 p->branch ^= thread_safe_rand();
04151 build_via(p, p->via, sizeof(p->via));
04152 }
04153
04154
04155 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop,";lr") == NULL)
04156 is_strict = 1;
04157
04158 if (sipmethod == SIP_CANCEL) {
04159 c = p->initreq.rlPart2;
04160 } else if (sipmethod == SIP_ACK) {
04161
04162
04163 if (!ast_strlen_zero(p->okcontacturi))
04164 c = is_strict ? p->route->hop : p->okcontacturi;
04165 else
04166 c = p->initreq.rlPart2;
04167 } else if (!ast_strlen_zero(p->okcontacturi)) {
04168 c = is_strict ? p->route->hop : p->okcontacturi;
04169 } else if (!ast_strlen_zero(p->uri)) {
04170 c = p->uri;
04171 } else {
04172
04173 c = get_header(orig, (ast_test_flag(p, SIP_OUTGOING)) ? "To" : "From");
04174 ast_copy_string(stripped, c, sizeof(stripped));
04175 c = get_in_brackets(stripped);
04176 n = strchr(c, ';');
04177 if (n)
04178 *n = '\0';
04179 }
04180 init_req(req, sipmethod, c);
04181
04182 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
04183
04184 add_header(req, "Via", p->via);
04185 if (p->route) {
04186 set_destination(p, p->route->hop);
04187 if (is_strict)
04188 add_route(req, p->route->next);
04189 else
04190 add_route(req, p->route);
04191 }
04192
04193 ot = get_header(orig, "To");
04194 of = get_header(orig, "From");
04195
04196
04197
04198 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
04199
04200
04201 if (ast_test_flag(p, SIP_OUTGOING) && !ast_strlen_zero(p->theirtag))
04202 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
04203 else if (!ast_test_flag(p, SIP_OUTGOING))
04204 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
04205 else
04206 snprintf(newto, sizeof(newto), "%s", ot);
04207 ot = newto;
04208 }
04209
04210 if (ast_test_flag(p, SIP_OUTGOING)) {
04211 add_header(req, "From", of);
04212 add_header(req, "To", ot);
04213 } else {
04214 add_header(req, "From", ot);
04215 add_header(req, "To", of);
04216 }
04217 add_header(req, "Contact", p->our_contact);
04218 copy_header(req, orig, "Call-ID");
04219 add_header(req, "CSeq", tmp);
04220
04221 add_header(req, "User-Agent", default_useragent);
04222 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
04223
04224 if (p->rpid)
04225 add_header(req, "Remote-Party-ID", p->rpid);
04226
04227 return 0;
04228 }
04229
04230
04231 static int __transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req, int reliable)
04232 {
04233 struct sip_request resp;
04234 int seqno = 0;
04235
04236 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
04237 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
04238 return -1;
04239 }
04240 respprep(&resp, p, msg, req);
04241 add_header_contentLength(&resp, 0);
04242
04243
04244 if (msg[0] != '1' && p->owner && p->owner->hangupcause) {
04245 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
04246 }
04247 add_blank_header(&resp);
04248 return send_response(p, &resp, reliable, seqno);
04249 }
04250
04251
04252 static int transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req)
04253 {
04254 return __transmit_response(p, msg, req, 0);
04255 }
04256
04257
04258 static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported)
04259 {
04260 struct sip_request resp;
04261 respprep(&resp, p, msg, req);
04262 append_date(&resp);
04263 add_header(&resp, "Unsupported", unsupported);
04264 return send_response(p, &resp, 0, 0);
04265 }
04266
04267
04268 static int transmit_response_reliable(struct sip_pvt *p, char *msg, struct sip_request *req, int fatal)
04269 {
04270 return __transmit_response(p, msg, req, fatal ? 2 : 1);
04271 }
04272
04273
04274 static void append_date(struct sip_request *req)
04275 {
04276 char tmpdat[256];
04277 struct tm tm;
04278 time_t t;
04279
04280 time(&t);
04281 gmtime_r(&t, &tm);
04282 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
04283 add_header(req, "Date", tmpdat);
04284 }
04285
04286
04287 static int transmit_response_with_date(struct sip_pvt *p, char *msg, struct sip_request *req)
04288 {
04289 struct sip_request resp;
04290 respprep(&resp, p, msg, req);
04291 append_date(&resp);
04292 add_header_contentLength(&resp, 0);
04293 add_blank_header(&resp);
04294 return send_response(p, &resp, 0, 0);
04295 }
04296
04297
04298 static int transmit_response_with_allow(struct sip_pvt *p, char *msg, struct sip_request *req, int reliable)
04299 {
04300 struct sip_request resp;
04301 respprep(&resp, p, msg, req);
04302 add_header(&resp, "Accept", "application/sdp");
04303 add_header_contentLength(&resp, 0);
04304 add_blank_header(&resp);
04305 return send_response(p, &resp, reliable, 0);
04306 }
04307
04308
04309 static int transmit_response_with_auth(struct sip_pvt *p, char *msg, struct sip_request *req, char *randdata, int reliable, char *header, int stale)
04310 {
04311 struct sip_request resp;
04312 char tmp[512];
04313 int seqno = 0;
04314
04315 if (reliable && (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1)) {
04316 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
04317 return -1;
04318 }
04319
04320
04321 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", global_realm, randdata, stale ? ", stale=true" : "");
04322 respprep(&resp, p, msg, req);
04323 add_header(&resp, header, tmp);
04324 add_header_contentLength(&resp, 0);
04325 add_blank_header(&resp);
04326 return send_response(p, &resp, reliable, seqno);
04327 }
04328
04329
04330 static int add_text(struct sip_request *req, const char *text)
04331 {
04332
04333 add_header(req, "Content-Type", "text/plain");
04334 add_header_contentLength(req, strlen(text));
04335 add_line(req, text);
04336 return 0;
04337 }
04338
04339
04340
04341 static int add_digit(struct sip_request *req, char digit)
04342 {
04343 char tmp[256];
04344
04345 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=250\r\n", digit);
04346 add_header(req, "Content-Type", "application/dtmf-relay");
04347 add_header_contentLength(req, strlen(tmp));
04348 add_line(req, tmp);
04349 return 0;
04350 }
04351
04352
04353
04354 static int add_vidupdate(struct sip_request *req)
04355 {
04356 const char *xml_is_a_huge_waste_of_space =
04357 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
04358 " <media_control>\r\n"
04359 " <vc_primitive>\r\n"
04360 " <to_encoder>\r\n"
04361 " <picture_fast_update>\r\n"
04362 " </picture_fast_update>\r\n"
04363 " </to_encoder>\r\n"
04364 " </vc_primitive>\r\n"
04365 " </media_control>\r\n";
04366 add_header(req, "Content-Type", "application/media_control+xml");
04367 add_header_contentLength(req, strlen(xml_is_a_huge_waste_of_space));
04368 add_line(req, xml_is_a_huge_waste_of_space);
04369 return 0;
04370 }
04371
04372 static void add_codec_to_sdp(const struct sip_pvt *p, int codec, int sample_rate,
04373 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
04374 int debug)
04375 {
04376 int rtp_code;
04377
04378 if (debug)
04379 ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
04380 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
04381 return;
04382
04383 ast_build_string(m_buf, m_size, " %d", rtp_code);
04384 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
04385 ast_rtp_lookup_mime_subtype(1, codec),
04386 sample_rate);
04387 if (codec == AST_FORMAT_G729A)
04388
04389 ast_build_string(a_buf, a_size, "a=fmtp:%d annexb=no\r\n", rtp_code);
04390 }
04391
04392 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
04393 char **m_buf, size_t *m_size, char **a_buf, size_t *a_size,
04394 int debug)
04395 {
04396 int rtp_code;
04397
04398 if (debug)
04399 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype(0, format));
04400 if ((rtp_code = ast_rtp_lookup_code(p->rtp, 0, format)) == -1)
04401 return;
04402
04403 ast_build_string(m_buf, m_size, " %d", rtp_code);
04404 ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
04405 ast_rtp_lookup_mime_subtype(0, format),
04406 sample_rate);
04407 if (format == AST_RTP_DTMF)
04408
04409 ast_build_string(a_buf, a_size, "a=fmtp:%d 0-16\r\n", rtp_code);
04410 }
04411
04412
04413 static int add_sdp(struct sip_request *resp, struct sip_pvt *p)
04414 {
04415 int len = 0;
04416 int pref_codec;
04417 int alreadysent = 0;
04418 struct sockaddr_in sin;
04419 struct sockaddr_in vsin;
04420 char v[256];
04421 char s[256];
04422 char o[256];
04423 char c[256];
04424 char t[256];
04425 char m_audio[256];
04426 char m_video[256];
04427 char a_audio[1024];
04428 char a_video[1024];
04429 char *m_audio_next = m_audio;
04430 char *m_video_next = m_video;
04431 size_t m_audio_left = sizeof(m_audio);
04432 size_t m_video_left = sizeof(m_video);
04433 char *a_audio_next = a_audio;
04434 char *a_video_next = a_video;
04435 size_t a_audio_left = sizeof(a_audio);
04436 size_t a_video_left = sizeof(a_video);
04437 char iabuf[INET_ADDRSTRLEN];
04438 int x;
04439 int capability;
04440 struct sockaddr_in dest;
04441 struct sockaddr_in vdest = { 0, };
04442 int debug;
04443
04444 debug = sip_debug_test_pvt(p);
04445
04446 len = 0;
04447 if (!p->rtp) {
04448 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
04449 return -1;
04450 }
04451 capability = p->jointcapability;
04452
04453 if (!p->sessionid) {
04454 p->sessionid = getpid();
04455 p->sessionversion = p->sessionid;
04456 } else
04457 p->sessionversion++;
04458 ast_rtp_get_us(p->rtp, &sin);
04459 if (p->vrtp)
04460 ast_rtp_get_us(p->vrtp, &vsin);
04461
04462 if (p->redirip.sin_addr.s_addr) {
04463 dest.sin_port = p->redirip.sin_port;
04464 dest.sin_addr = p->redirip.sin_addr;
04465 if (p->redircodecs)
04466 capability = p->redircodecs;
04467 } else {
04468 dest.sin_addr = p->ourip;
04469 dest.sin_port = sin.sin_port;
04470 }
04471
04472
04473 if (p->vrtp) {
04474 if (p->vredirip.sin_addr.s_addr) {
04475 vdest.sin_port = p->vredirip.sin_port;
04476 vdest.sin_addr = p->vredirip.sin_addr;
04477 } else {
04478 vdest.sin_addr = p->ourip;
04479 vdest.sin_port = vsin.sin_port;
04480 }
04481 }
04482 if (debug){
04483 ast_verbose("We're at %s port %d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip), ntohs(sin.sin_port));
04484 if (p->vrtp)
04485 ast_verbose("Video is at %s port %d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip), ntohs(vsin.sin_port));
04486 }
04487
04488
04489
04490
04491 snprintf(v, sizeof(v), "v=0\r\n");
04492 snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", p->sessionid, p->sessionversion, ast_inet_ntoa(iabuf, sizeof(iabuf), dest.sin_addr));
04493 snprintf(s, sizeof(s), "s=session\r\n");
04494 snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(iabuf, sizeof(iabuf), dest.sin_addr));
04495 snprintf(t, sizeof(t), "t=0 0\r\n");
04496
04497 ast_build_string(&m_audio_next, &m_audio_left, "m=audio %d RTP/AVP", ntohs(dest.sin_port));
04498 ast_build_string(&m_video_next, &m_video_left, "m=video %d RTP/AVP", ntohs(vdest.sin_port));
04499
04500
04501 if (capability & p->prefcodec) {
04502 if (p->prefcodec <= AST_FORMAT_MAX_AUDIO)
04503 add_codec_to_sdp(p, p->prefcodec, 8000,
04504 &m_audio_next, &m_audio_left,
04505 &a_audio_next, &a_audio_left,
04506 debug);
04507 else
04508 add_codec_to_sdp(p, p->prefcodec, 90000,
04509 &m_video_next, &m_video_left,
04510 &a_video_next, &a_video_left,
04511 debug);
04512 alreadysent |= p->prefcodec;
04513 }
04514
04515
04516 for (x = 0; x < 32; x++) {
04517 if (!(pref_codec = ast_codec_pref_index(&p->prefs, x)))
04518 break;
04519
04520 if (!(capability & pref_codec))
04521 continue;
04522
04523 if (alreadysent & pref_codec)
04524 continue;
04525
04526 if (pref_codec <= AST_FORMAT_MAX_AUDIO)
04527 add_codec_to_sdp(p, pref_codec, 8000,
04528 &m_audio_next, &m_audio_left,
04529 &a_audio_next, &a_audio_left,
04530 debug);
04531 else
04532 add_codec_to_sdp(p, pref_codec, 90000,
04533 &m_video_next, &m_video_left,
04534 &a_video_next, &a_video_left,
04535 debug);
04536 alreadysent |= pref_codec;
04537 }
04538
04539
04540 for (x = 1; x <= ((videosupport && p->vrtp) ? AST_FORMAT_MAX_VIDEO : AST_FORMAT_MAX_AUDIO); x <<= 1) {
04541 if (!(capability & x))
04542 continue;
04543
04544 if (alreadysent & x)
04545 continue;
04546
04547 if (x <= AST_FORMAT_MAX_AUDIO)
04548 add_codec_to_sdp(p, x, 8000,
04549 &m_audio_next, &m_audio_left,
04550 &a_audio_next, &a_audio_left,
04551 debug);
04552 else
04553 add_codec_to_sdp(p, x, 90000,
04554 &m_video_next, &m_video_left,
04555 &a_video_next, &a_video_left,
04556 debug);
04557 }
04558
04559 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
04560 if (!(p->noncodeccapability & x))
04561 continue;
04562
04563 add_noncodec_to_sdp(p, x, 8000,
04564 &m_audio_next, &m_audio_left,
04565 &a_audio_next, &a_audio_left,
04566 debug);
04567 }
04568
04569 ast_build_string(&a_audio_next, &a_audio_left, "a=silenceSupp:off - - - -\r\n");
04570
04571 if ((m_audio_left < 2) || (m_video_left < 2) || (a_audio_left == 0) || (a_video_left == 0))
04572 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
04573
04574 ast_build_string(&m_audio_next, &m_audio_left, "\r\n");
04575 ast_build_string(&m_video_next, &m_video_left, "\r\n");
04576
04577 len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m_audio) + strlen(a_audio);
04578 if ((p->vrtp) && (!ast_test_flag(p, SIP_NOVIDEO)) && (capability & VIDEO_CODEC_MASK))
04579 len += strlen(m_video) + strlen(a_video);
04580
04581 add_header(resp, "Content-Type", "application/sdp");
04582 add_header_contentLength(resp, len);
04583 add_line(resp, v);
04584 add_line(resp, o);
04585 add_line(resp, s);
04586 add_line(resp, c);
04587 add_line(resp, t);
04588 add_line(resp, m_audio);
04589 add_line(resp, a_audio);
04590 if ((p->vrtp) && (!ast_test_flag(p, SIP_NOVIDEO)) && (capability & VIDEO_CODEC_MASK)) {
04591 add_line(resp, m_video);
04592 add_line(resp, a_video);
04593 }
04594
04595
04596 time(&p->lastrtprx);
04597 time(&p->lastrtptx);
04598
04599 return 0;
04600 }
04601
04602
04603 static void copy_request(struct sip_request *dst, struct sip_request *src)
04604 {
04605 long offset;
04606 int x;
04607 offset = ((void *)dst) - ((void *)src);
04608
04609 memcpy(dst, src, sizeof(*dst));
04610
04611 for (x=0; x < src->headers; x++)
04612 dst->header[x] += offset;
04613 for (x=0; x < src->lines; x++)
04614 dst->line[x] += offset;
04615 }
04616
04617
04618 static int transmit_response_with_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
04619 {
04620 struct sip_request resp;
04621 int seqno;
04622 if (sscanf(get_header(req, "CSeq"), "%d ", &seqno) != 1) {
04623 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
04624 return -1;
04625 }
04626 respprep(&resp, p, msg, req);
04627 if (p->rtp) {
04628 ast_rtp_offered_from_local(p->rtp, 0);
04629 try_suggested_sip_codec(p);
04630 add_sdp(&resp, p);
04631 } else {
04632 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
04633 }
04634 return send_response(p, &resp, retrans, seqno);
04635 }
04636
04637
04638 static int determine_firstline_parts( struct sip_request *req )
04639 {
04640 char *e, *cmd;
04641 int len;
04642
04643 cmd = ast_skip_blanks(req->header[0]);
04644 if (!*cmd)
04645 return -1;
04646 req->rlPart1 = cmd;
04647 e = ast_skip_nonblanks(cmd);
04648
04649 if (*e)
04650 *e++ = '\0';
04651 e = ast_skip_blanks(e);
04652 if ( !*e )
04653 return -1;
04654
04655 if ( !strcasecmp(cmd, "SIP/2.0") ) {
04656
04657 req->rlPart2 = e;
04658 len = strlen( req->rlPart2 );
04659 if ( len < 2 ) {
04660 return -1;
04661 }
04662 ast_trim_blanks(e);
04663 } else {
04664
04665 if ( *e == '<' ) {
04666 e++;
04667 if ( !*e ) {
04668 return -1;
04669 }
04670 }
04671 req->rlPart2 = e;
04672 if ( ( e= strrchr( req->rlPart2, 'S' ) ) == NULL ) {
04673 return -1;
04674 }
04675
04676 while( isspace( *(--e) ) ) {}
04677 if ( *e == '>' ) {
04678 *e = '\0';
04679 } else {
04680 *(++e)= '\0';
04681 }
04682 }
04683 return 1;
04684 }
04685
04686
04687
04688
04689
04690
04691
04692 static int transmit_reinvite_with_sdp(struct sip_pvt *p)
04693 {
04694 struct sip_request req;
04695 if (ast_test_flag(p, SIP_REINVITE_UPDATE))
04696 reqprep(&req, p, SIP_UPDATE, 0, 1);
04697 else
04698 reqprep(&req, p, SIP_INVITE, 0, 1);
04699
04700 add_header(&req, "Allow", ALLOWED_METHODS);
04701 if (sipdebug)
04702 add_header(&req, "X-asterisk-info", "SIP re-invite (RTP bridge)");
04703 ast_rtp_offered_from_local(p->rtp, 1);
04704 add_sdp(&req, p);
04705
04706 copy_request(&p->initreq, &req);
04707 parse_request(&p->initreq);
04708 if (sip_debug_test_pvt(p))
04709 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
04710 p->lastinvite = p->ocseq;
04711 ast_set_flag(p, SIP_OUTGOING);
04712 return send_request(p, &req, 1, p->ocseq);
04713 }
04714
04715
04716 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
04717 {
04718 char stripped[256];
04719 char *c, *n;
04720 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
04721 c = get_in_brackets(stripped);
04722 n = strchr(c, ';');
04723 if (n)
04724 *n = '\0';
04725 if (!ast_strlen_zero(c))
04726 ast_copy_string(p->uri, c, sizeof(p->uri));
04727 }
04728
04729
04730 static void build_contact(struct sip_pvt *p)
04731 {
04732 char iabuf[INET_ADDRSTRLEN];
04733
04734
04735 if (ourport != 5060)
04736 snprintf(p->our_contact, sizeof(p->our_contact), "<sip:%s%s%s:%d>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip), ourport);
04737 else
04738 snprintf(p->our_contact, sizeof(p->our_contact), "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip));
04739 }
04740
04741
04742 static void build_rpid(struct sip_pvt *p)
04743 {
04744 int send_pres_tags = 1;
04745 const char *privacy=NULL;
04746 const char *screen=NULL;
04747 char buf[256];
04748 const char *clid = default_callerid;
04749 const char *clin = NULL;
04750 char iabuf[INET_ADDRSTRLEN];
04751 const char *fromdomain;
04752
04753 if (p->rpid || p->rpid_from)
04754 return;
04755
04756 if (p->owner && p->owner->cid.cid_num)
04757 clid = p->owner->cid.cid_num;
04758 if (p->owner && p->owner->cid.cid_name)
04759 clin = p->owner->cid.cid_name;
04760 if (ast_strlen_zero(clin))
04761 clin = clid;
04762
04763 switch (p->callingpres) {
04764 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
04765 privacy = "off";
04766 screen = "no";
04767 break;
04768 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
04769 privacy = "off";
04770 screen = "pass";
04771 break;
04772 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
04773 privacy = "off";
04774 screen = "fail";
04775 break;
04776 case AST_PRES_ALLOWED_NETWORK_NUMBER:
04777 privacy = "off";
04778 screen = "yes";
04779 break;
04780 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
04781 privacy = "full";
04782 screen = "no";
04783 break;
04784 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
04785 privacy = "full";
04786 screen = "pass";
04787 break;
04788 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
04789 privacy = "full";
04790 screen = "fail";
04791 break;
04792 case AST_PRES_PROHIB_NETWORK_NUMBER:
04793 privacy = "full";
04794 screen = "pass";
04795 break;
04796 case AST_PRES_NUMBER_NOT_AVAILABLE:
04797 send_pres_tags = 0;
04798 break;
04799 default:
04800 ast_log(LOG_WARNING, "Unsupported callingpres (%d)\n", p->callingpres);
04801 if ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
04802 privacy = "full";
04803 else
04804 privacy = "off";
04805 screen = "no";
04806 break;
04807 }
04808
04809 fromdomain = ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain;
04810
04811 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>", clin, clid, fromdomain);
04812 if (send_pres_tags)
04813 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ";privacy=%s;screen=%s", privacy, screen);
04814 p->rpid = strdup(buf);
04815
04816 snprintf(buf, sizeof(buf), "\"%s\" <sip:%s@%s>;tag=%s", clin,
04817 ast_strlen_zero(p->fromuser) ? clid : p->fromuser,
04818 fromdomain, p->tag);
04819 p->rpid_from = strdup(buf);
04820 }
04821
04822
04823 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod)
04824 {
04825 char invite_buf[256] = "";
04826 char *invite = invite_buf;
04827 size_t invite_max = sizeof(invite_buf);
04828 char from[256];
04829 char to[256];
04830 char tmp[BUFSIZ/2];
04831 char tmp2[BUFSIZ/2];
04832 char iabuf[INET_ADDRSTRLEN];
04833 char *l = NULL, *n = NULL;
04834 int x;
04835 char urioptions[256]="";
04836
04837 if (ast_test_flag(p, SIP_USEREQPHONE)) {
04838 char onlydigits = 1;
04839 x=0;
04840
04841
04842
04843
04844
04845
04846 if (p->username && p->username[0] == '+')
04847 x=1;
04848
04849 for (; x < strlen(p->username); x++) {
04850 if (!strchr(AST_DIGIT_ANYNUM, p->username[x])) {
04851 onlydigits = 0;
04852 break;
04853 }
04854 }
04855
04856
04857 if (onlydigits)
04858 strcpy(urioptions, ";user=phone");
04859 }
04860
04861
04862 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
04863
04864 if (p->owner) {
04865 l = p->owner->cid.cid_num;
04866 n = p->owner->cid.cid_name;
04867 }
04868
04869 if (!ast_test_flag(p, SIP_SENDRPID) && ((p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)) {
04870 l = CALLERID_UNKNOWN;
04871 n = l;
04872 }
04873 if (ast_strlen_zero(l))
04874 l = default_callerid;
04875 if (ast_strlen_zero(n))
04876 n = l;
04877
04878 if (!ast_strlen_zero(p->fromuser))
04879 l = p->fromuser;
04880 else
04881 ast_copy_string(p->fromuser, l, sizeof(p->fromuser));
04882
04883
04884 if (!ast_strlen_zero(p->fromname))
04885 n = p->fromname;
04886 else
04887 ast_copy_string(p->fromname, n, sizeof(p->fromname));
04888
04889 if (pedanticsipchecking) {
04890 ast_uri_encode(n, tmp, sizeof(tmp), 0);
04891 n = tmp;
04892 ast_uri_encode(l, tmp2, sizeof(tmp2), 0);
04893 l = tmp2;
04894 }
04895
04896 if ((ourport != 5060) && ast_strlen_zero(p->fromdomain))
04897 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain, ourport, p->tag);
04898 else
04899 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain, p->tag);
04900
04901
04902 if (!ast_strlen_zero(p->fullcontact)) {
04903
04904 ast_build_string(&invite, &invite_max, "%s", p->fullcontact);
04905 } else {
04906
04907 ast_build_string(&invite, &invite_max, "sip:");
04908 if (!ast_strlen_zero(p->username)) {
04909 n = p->username;
04910 if (pedanticsipchecking) {
04911 ast_uri_encode(n, tmp, sizeof(tmp), 0);
04912 n = tmp;
04913 }
04914 ast_build_string(&invite, &invite_max, "%s@", n);
04915 }
04916 ast_build_string(&invite, &invite_max, "%s", p->tohost);
04917 if (ntohs(p->sa.sin_port) != 5060)
04918 ast_build_string(&invite, &invite_max, ":%d", ntohs(p->sa.sin_port));
04919 ast_build_string(&invite, &invite_max, "%s", urioptions);
04920 }
04921
04922
04923 if (p->options && p->options->uri_options)
04924 ast_build_string(&invite, &invite_max, ";%s", p->options->uri_options);
04925
04926 ast_copy_string(p->uri, invite_buf, sizeof(p->uri));
04927
04928 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
04929
04930 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", p->uri, p->theirtag);
04931 } else if (p->options && p->options->vxml_url) {
04932
04933 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
04934 } else {
04935 snprintf(to, sizeof(to), "<%s>", p->uri);
04936 }
04937
04938 memset(req, 0, sizeof(struct sip_request));
04939 init_req(req, sipmethod, p->uri);
04940 snprintf(tmp, sizeof(tmp), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
04941
04942 add_header(req, "Via", p->via);
04943
04944
04945
04946 if (ast_test_flag(p, SIP_SENDRPID) && (sipmethod == SIP_INVITE)) {
04947 build_rpid(p);
04948 add_header(req, "From", p->rpid_from);
04949 } else {
04950 add_header(req, "From", from);
04951 }
04952 add_header(req, "To", to);
04953 ast_copy_string(p->exten, l, sizeof(p->exten));
04954 build_contact(p);
04955 add_header(req, "Contact", p->our_contact);
04956 add_header(req, "Call-ID", p->callid);
04957 add_header(req, "CSeq", tmp);
04958 add_header(req, "User-Agent", default_useragent);
04959 add_header(req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
04960 if (p->rpid)
04961 add_header(req, "Remote-Party-ID", p->rpid);
04962 }
04963
04964
04965 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init)
04966 {
04967 struct sip_request req;
04968
04969 req.method = sipmethod;
04970 if (init) {
04971
04972 p->branch ^= thread_safe_rand();
04973 build_via(p, p->via, sizeof(p->via));
04974 if (init > 1)
04975 initreqprep(&req, p, sipmethod);
04976 else
04977 reqprep(&req, p, sipmethod, 0, 1);
04978 } else
04979 reqprep(&req, p, sipmethod, 0, 1);
04980
04981 if (p->options && p->options->auth)
04982 add_header(&req, p->options->authheader, p->options->auth);
04983 append_date(&req);
04984 if (sipmethod == SIP_REFER) {
04985 if (!ast_strlen_zero(p->refer_to))
04986 add_header(&req, "Refer-To", p->refer_to);
04987 if (!ast_strlen_zero(p->referred_by))
04988 add_header(&req, "Referred-By", p->referred_by);
04989 }
04990 #ifdef OSP_SUPPORT
04991 if ((req.method != SIP_OPTIONS) && p->options && !ast_strlen_zero(p->options->osptoken)) {
04992 ast_log(LOG_DEBUG,"Adding OSP Token: %s\n", p->options->osptoken);
04993 add_header(&req, "P-OSP-Auth-Token", p->options->osptoken);
04994 }
04995 #endif
04996 if (p->options && !ast_strlen_zero(p->options->distinctive_ring))
04997 {
04998 add_header(&req, "Alert-Info", p->options->distinctive_ring);
04999 }
05000 add_header(&req, "Allow", ALLOWED_METHODS);
05001 if (p->options && p->options->addsipheaders ) {
05002 struct ast_channel *ast;
05003 char *header = (char *) NULL;
05004 char *content = (char *) NULL;
05005 char *end = (char *) NULL;
05006 struct varshead *headp = (struct varshead *) NULL;
05007 struct ast_var_t *current;
05008
05009 ast = p->owner;
05010 if (ast) {
05011 char *headdup;
05012 headp = &ast->varshead;
05013 if (!headp)
05014 ast_log(LOG_WARNING,"No Headp for the channel...ooops!\n");
05015 else {
05016 AST_LIST_TRAVERSE(headp, current, entries) {
05017
05018 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
05019 header = ast_var_value(current);
05020 headdup = ast_strdupa(header);
05021
05022 if (*headdup == '"')
05023 headdup++;
05024 if ((content = strchr(headdup, ':'))) {
05025 *content = '\0';
05026 content++;
05027
05028 while (*content == ' ')
05029 content++;
05030
05031 end = content + strlen(content) -1;
05032 if (*end == '"')
05033 *end = '\0';
05034
05035 add_header(&req, headdup, content);
05036 if (sipdebug)
05037 ast_log(LOG_DEBUG, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
05038 }
05039 }
05040 }
05041 }
05042 }
05043 }
05044 if (sdp && p->rtp) {
05045 ast_rtp_offered_from_local(p->rtp, 1);
05046 add_sdp(&req, p);
05047 } else {
05048 add_header_contentLength(&req, 0);
05049 add_blank_header(&req);
05050 }
05051
05052 if (!p->initreq.headers) {
05053
05054 copy_request(&p->initreq, &req);
05055 parse_request(&p->initreq);
05056 if (sip_debug_test_pvt(p))
05057 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
05058 }
05059 p->lastinvite = p->ocseq;
05060 return send_request(p, &req, init ? 2 : 1, p->ocseq);
05061 }
05062
05063
05064 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int substate)
05065 {
05066 char tmp[4000], from[256], to[256];
05067 char *t = tmp, *c, *a, *mfrom, *mto;
05068 size_t maxbytes = sizeof(tmp);
05069 struct sip_request req;
05070 char hint[AST_MAX_EXTENSION];
05071 char *statestring = "terminated";
05072 const struct cfsubscription_types *subscriptiontype;
05073 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
05074 char *pidfstate = "--";
05075 char *pidfnote= "Ready";
05076
05077 memset(from, 0, sizeof(from));
05078 memset(to, 0, sizeof(to));
05079 memset(tmp, 0, sizeof(tmp));
05080
05081 switch (state) {
05082 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
05083 if (global_notifyringing)
05084 statestring = "early";
05085 else
05086 statestring = "confirmed";
05087 local_state = NOTIFY_INUSE;
05088 pidfstate = "busy";
05089 pidfnote = "Ringing";
05090 break;
05091 case AST_EXTENSION_RINGING:
05092 statestring = "early";
05093 local_state = NOTIFY_INUSE;
05094 pidfstate = "busy";
05095 pidfnote = "Ringing";
05096 break;
05097 case AST_EXTENSION_INUSE:
05098 statestring = "confirmed";
05099 local_state = NOTIFY_INUSE;
05100 pidfstate = "busy";
05101 pidfnote = "On the phone";
05102 break;
05103 case AST_EXTENSION_BUSY:
05104 statestring = "confirmed";
05105 local_state = NOTIFY_CLOSED;
05106 pidfstate = "busy";
05107 pidfnote = "On the phone";
05108 break;
05109 case AST_EXTENSION_UNAVAILABLE:
05110 statestring = "confirmed";
05111 local_state = NOTIFY_CLOSED;
05112 pidfstate = "away";
05113 pidfnote = "Unavailable";
05114 break;
05115 case AST_EXTENSION_NOT_INUSE:
05116 default:
05117
05118 break;
05119 }
05120
05121 subscriptiontype = find_subscription_type(p->subscribed);
05122
05123
05124 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, p->exten)) {
05125
05126 if (ast_device_state(hint) == AST_DEVICE_UNAVAILABLE) {
05127 local_state = NOTIFY_CLOSED;
05128 pidfstate = "away";
05129 pidfnote = "Not online";
05130 }
05131 }
05132
05133 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
05134 c = get_in_brackets(from);
05135 if (strncmp(c, "sip:", 4)) {
05136 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
05137 return -1;
05138 }
05139 if ((a = strchr(c, ';')))
05140 *a = '\0';
05141 mfrom = c;
05142
05143 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
05144 c = get_in_brackets(to);
05145 if (strncmp(c, "sip:", 4)) {
05146 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
05147 return -1;
05148 }
05149 if ((a = strchr(c, ';')))
05150 *a = '\0';
05151 mto = c;
05152
05153 reqprep(&req, p, SIP_NOTIFY, 0, 1);
05154
05155
05156 add_header(&req, "Event", subscriptiontype->event);
05157 add_header(&req, "Content-Type", subscriptiontype->mediatype);
05158 switch(state) {
05159 case AST_EXTENSION_DEACTIVATED:
05160 if (p->subscribed == TIMEOUT)
05161 add_header(&req, "Subscription-State", "terminated;reason=timeout");
05162 else {
05163 add_header(&req, "Subscription-State", "terminated;reason=probation");
05164 add_header(&req, "Retry-After", "60");
05165 }
05166 break;
05167 case AST_EXTENSION_REMOVED:
05168 add_header(&req, "Subscription-State", "terminated;reason=noresource");
05169 break;
05170 break;
05171 default:
05172 if (p->expiry)
05173 add_header(&req, "Subscription-State", "active");
05174 else
05175 add_header(&req, "Subscription-State", "terminated;reason=timeout");
05176 }
05177 switch (p->subscribed) {
05178 case XPIDF_XML:
05179 case CPIM_PIDF_XML:
05180 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
05181 ast_build_string(&t, &maxbytes, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n");
05182 ast_build_string(&t, &maxbytes, "<presence>\n");
05183 ast_build_string(&t, &maxbytes, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
05184 ast_build_string(&t, &maxbytes, "<atom id=\"%s\">\n", p->exten);
05185 ast_build_string(&t, &maxbytes, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
05186 ast_build_string(&t, &maxbytes, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
05187 ast_build_string(&t, &maxbytes, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
05188 ast_build_string(&t, &maxbytes, "</address>\n</atom>\n</presence>\n");
05189 break;
05190 case PIDF_XML:
05191 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
05192 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);
05193 ast_build_string(&t, &maxbytes, "<pp:person><status>\n");
05194 if (pidfstate[0] != '-')
05195 ast_build_string(&t, &maxbytes, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
05196 ast_build_string(&t, &maxbytes, "</status></pp:person>\n");
05197 ast_build_string(&t, &maxbytes, "<note>%s</note>\n", pidfnote);
05198 ast_build_string(&t, &maxbytes, "<tuple id=\"%s\">\n", p->exten);
05199 ast_build_string(&t, &maxbytes, "<contact priority=\"1\">%s</contact>\n", mto);
05200 if (pidfstate[0] == 'b')
05201 ast_build_string(&t, &maxbytes, "<status><basic>open</basic></status>\n");
05202 else
05203 ast_build_string(&t, &maxbytes, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
05204 ast_build_string(&t, &maxbytes, "</tuple>\n</presence>\n");
05205 break;
05206 case DIALOG_INFO_XML:
05207 ast_build_string(&t, &maxbytes, "<?xml version=\"1.0\"?>\n");
05208 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);
05209 if ((state & AST_EXTENSION_RINGING) && global_notifyringing)
05210 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\" direction=\"recipient\">\n", p->exten);
05211 else
05212 ast_build_string(&t, &maxbytes, "<dialog id=\"%s\">\n", p->exten);
05213 ast_build_string(&t, &maxbytes, "<state>%s</state>\n", statestring);
05214 ast_build_string(&t, &maxbytes, "</dialog>\n</dialog-info>\n");
05215 break;
05216 case NONE:
05217 default:
05218 break;
05219 }
05220
05221 if (t > tmp + sizeof(tmp))
05222 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
05223
05224 add_header_contentLength(&req, strlen(tmp));
05225 add_line(&req, tmp);
05226
05227 return send_request(p, &req, 1, p->ocseq);
05228 }
05229
05230
05231
05232
05233
05234
05235
05236 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, char *vmexten)
05237 {
05238 struct sip_request req;
05239 char tmp[500];
05240 char *t = tmp;
05241 size_t maxbytes = sizeof(tmp);
05242 char iabuf[INET_ADDRSTRLEN];
05243
05244 initreqprep(&req, p, SIP_NOTIFY);
05245 add_header(&req, "Event", "message-summary");
05246 add_header(&req, "Content-Type", default_notifymime);
05247
05248 ast_build_string(&t, &maxbytes, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
05249 ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n", !ast_strlen_zero(vmexten) ? vmexten : global_vmexten, ast_strlen_zero(p->fromdomain) ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip) : p->fromdomain);
05250 ast_build_string(&t, &maxbytes, "Voice-Message: %d/%d (0/0)\r\n", newmsgs, oldmsgs);
05251
05252 if (t > tmp + sizeof(tmp))
05253 ast_log(LOG_WARNING, "Buffer overflow detected!! (Please file a bug report)\n");
05254
05255 add_header_contentLength(&req, strlen(tmp));
05256 add_line(&req, tmp);
05257
05258 if (!p->initreq.headers) {
05259 copy_request(&p->initreq, &req);
05260 parse_request(&p->initreq);
05261 if (sip_debug_test_pvt(p))
05262 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
05263 determine_firstline_parts(&p->initreq);
05264 }
05265
05266 return send_request(p, &req, 1, p->ocseq);
05267 }
05268
05269
05270 static int transmit_sip_request(struct sip_pvt *p,struct sip_request *req)
05271 {
05272 if (!p->initreq.headers) {
05273
05274 copy_request(&p->initreq, req);
05275 parse_request(&p->initreq);
05276 if (sip_debug_test_pvt(p))
05277 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
05278 determine_firstline_parts(&p->initreq);
05279 }
05280
05281 return send_request(p, req, 0, p->ocseq);
05282 }
05283
05284
05285
05286
05287
05288
05289 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq)
05290 {
05291 struct sip_request req;
05292 char tmp[20];
05293 reqprep(&req, p, SIP_NOTIFY, 0, 1);
05294 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
05295 add_header(&req, "Event", tmp);
05296 add_header(&req, "Subscription-state", "terminated;reason=noresource");
05297 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
05298
05299 strcpy(tmp, "SIP/2.0 200 OK");
05300 add_header_contentLength(&req, strlen(tmp));
05301 add_line(&req, tmp);
05302
05303 if (!p->initreq.headers) {
05304
05305 copy_request(&p->initreq, &req);
05306 parse_request(&p->initreq);
05307 if (sip_debug_test_pvt(p))
05308 ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
05309 determine_firstline_parts(&p->initreq);
05310 }
05311
05312 return send_request(p, &req, 1, p->ocseq);
05313 }
05314
05315 static char *regstate2str(int regstate)
05316 {
05317 switch(regstate) {
05318 case REG_STATE_FAILED:
05319 return "Failed";
05320 case REG_STATE_UNREGISTERED:
05321 return "Unregistered";
05322 case REG_STATE_REGSENT:
05323 return "Request Sent";
05324 case REG_STATE_AUTHSENT:
05325 return "Auth. Sent";
05326 case REG_STATE_REGISTERED:
05327 return "Registered";
05328 case REG_STATE_REJECTED:
05329 return "Rejected";
05330 case REG_STATE_TIMEOUT:
05331 return "Timeout";
05332 case REG_STATE_NOAUTH:
05333 return "No Authentication";
05334 default:
05335 return "Unknown";
05336 }
05337 }
05338
05339 static int transmit_register(struct sip_registry *r, int sipmethod, char *auth, char *authheader);
05340
05341
05342 static int sip_reregister(void *data)
05343 {
05344
05345 struct sip_registry *r= ASTOBJ_REF((struct sip_registry *) data);
05346
05347
05348 if (!r)
05349 return 0;
05350
05351 if (r->call && recordhistory) {
05352 char tmp[80];
05353 snprintf(tmp, sizeof(tmp), "Account: %s@%s", r->username, r->hostname);
05354 append_history(r->call, "RegistryRenew", tmp);
05355 }
05356
05357
05358 if (sipdebug)
05359 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
05360
05361 r->expire = -1;
05362 __sip_do_register(r);
05363 ASTOBJ_UNREF(r, sip_registry_destroy);
05364 return 0;
05365 }
05366
05367
05368 static int __sip_do_register(struct sip_registry *r)
05369 {
05370 int res;
05371
05372 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
05373 return res;
05374 }
05375
05376
05377 static int sip_reg_timeout(void *data)
05378 {
05379
05380
05381 struct sip_registry *r = ASTOBJ_REF((struct sip_registry *) data);
05382 struct sip_pvt *p;
05383 int res;
05384
05385
05386 if (!r)
05387 return 0;
05388
05389 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
05390 if (r->call) {
05391
05392
05393 p = r->call;
05394 if (p->registry)
05395 ASTOBJ_UNREF(p->registry, sip_registry_destroy);
05396 r->call = NULL;
05397 ast_set_flag(p, SIP_NEEDDESTROY);
05398
05399 __sip_pretend_ack(p);
05400 }
05401
05402 if (global_regattempts_max && (r->regattempts > global_regattempts_max)) {
05403
05404
05405
05406 ast_log(LOG_NOTICE, " -- Giving up forever trying to register '%s@%s'\n", r->username, r->hostname);
05407 r->regstate=REG_STATE_FAILED;
05408 } else {
05409 r->regstate=REG_STATE_UNREGISTERED;
05410 r->timeout = -1;
05411 res=transmit_register(r, SIP_REGISTER, NULL, NULL);
05412 }
05413 manager_event(EVENT_FLAG_SYSTEM, "Registry", "Channel: SIP\r\nUsername: %s\r\nDomain: %s\r\nStatus: %s\r\n", r->username, r->hostname, regstate2str(r->regstate));
05414 ASTOBJ_UNREF(r,sip_registry_destroy);
05415 return 0;
05416 }
05417
05418
05419 static int transmit_register(struct sip_registry *r, int sipmethod, char *auth, char *authheader)
05420 {
05421 struct sip_request req;
05422 char from[256];
05423 char to[256];
05424 char tmp[80];
05425 char via[80];
05426 char addr[80];
05427 struct sip_pvt *p;
05428
05429
05430 if ( r == NULL || ((auth==NULL) && (r->regstate==REG_STATE_REGSENT || r->regstate==REG_STATE_AUTHSENT))) {
05431 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
05432 return 0;
05433 }
05434
05435 if (r->call) {
05436 if (!auth) {
05437 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
05438 return 0;
05439 } else {
05440 p = r->call;
05441 make_our_tag(p->tag, sizeof(p->tag));
05442 p->theirtag[0]='\0';
05443 }
05444 } else {
05445
05446 if (!r->callid_valid) {
05447 build_callid(r->callid, sizeof(r->callid), __ourip, default_fromdomain);
05448 r->callid_valid = 1;
05449 }
05450
05451 p=sip_alloc( r->callid, NULL, 0, SIP_REGISTER);
05452 if (!p) {
05453 ast_log(LOG_WARNING, "Unable to allocate registration call\n");
05454 return 0;
05455 }
05456 if (recordhistory) {
05457 char tmp[80];
05458 snprintf(tmp, sizeof(tmp), "Account: %s@%s", r->username, r->hostname);
05459 append_history(p, "RegistryInit", tmp);
05460 }
05461
05462 if (create_addr(p, r->hostname)) {
05463
05464
05465 sip_destroy(p);
05466 if (r->timeout > -1) {
05467 ast_sched_del(sched, r->timeout);
05468 r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
05469 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
05470 } else {
05471 r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
05472 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);
05473 }
05474 r->regattempts++;
05475 return 0;
05476 }
05477
05478 ast_copy_string(r->callid, p->callid, sizeof(r->callid));
05479 if (r->portno)
05480 p->sa.sin_port = htons(r->portno);
05481 else
05482 r->portno = ntohs(p->sa.sin_port);
05483 ast_set_flag(p, SIP_OUTGOING);
05484 r->call=p;
05485 p->registry=ASTOBJ_REF(r);
05486 if (!ast_strlen_zero(r->secret))
05487 ast_copy_string(p->peersecret, r->secret, sizeof(p->peersecret));
05488 if (!ast_strlen_zero(r->md5secret))
05489 ast_copy_string(p->peermd5secret, r->md5secret, sizeof(p->peermd5secret));
05490
05491
05492 if (!ast_strlen_zero(r->authuser)) {
05493 ast_copy_string(p->peername, r->authuser, sizeof(p->peername));
05494 ast_copy_string(p->authname, r->authuser, sizeof(p->authname));
05495 } else {
05496 if (!ast_strlen_zero(r->username)) {
05497 ast_copy_string(p->peername, r->username, sizeof(p->peername));
05498 ast_copy_string(p->authname, r->username, sizeof(p->authname));
05499 ast_copy_string(p->fromuser, r->username, sizeof(p->fromuser));
05500 }
05501 }
05502 if (!ast_strlen_zero(r->username))
05503 ast_copy_string(p->username, r->username, sizeof(p->username));
05504
05505 ast_copy_string(p->exten, r->contact, sizeof(p->exten));
05506
05507
05508
05509
05510
05511
05512 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
05513 memcpy(&p->ourip, &bindaddr.sin_addr, sizeof(p->ourip));
05514 build_contact(p);
05515 }
05516
05517
05518 if (auth == NULL) {
05519 if (r->timeout > -1) {
05520 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
05521 ast_sched_del(sched, r->timeout);
05522 }
05523 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
05524 ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
05525 }
05526
05527 if (strchr(r->username, '@')) {
05528 snprintf(from, sizeof(from), "<sip:%s>;tag=%s", r->username, p->tag);
05529 if (!ast_strlen_zero(p->theirtag))
05530 snprintf(to, sizeof(to), "<sip:%s>;tag=%s", r->username, p->theirtag);
05531 else
05532 snprintf(to, sizeof(to), "<sip:%s>", r->username);
05533 } else {
05534 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->tag);
05535 if (!ast_strlen_zero(p->theirtag))
05536 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, p->tohost, p->theirtag);
05537 else
05538 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, p->tohost);
05539 }
05540
05541
05542
05543 if (!ast_strlen_zero(p->fromdomain))
05544 snprintf(addr, sizeof(addr), "sip:%s", p->fromdomain);
05545 else
05546 snprintf(addr, sizeof(addr), "sip:%s", r->hostname);
05547 ast_copy_string(p->uri, addr, sizeof(p->uri));
05548
05549 p->branch ^= thread_safe_rand();
05550
05551 memset(&req, 0, sizeof(req));
05552 init_req(&req, sipmethod, addr);
05553
05554
05555 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
05556 p->ocseq = r->ocseq;
05557
05558 build_via(p, via, sizeof(via));
05559 add_header(&req, "Via", via);
05560 add_header(&req, "From", from);
05561 add_header(&req, "To", to);
05562 add_header(&req, "Call-ID", p->callid);
05563 add_header(&req, "CSeq", tmp);
05564 add_header(&req, "User-Agent", default_useragent);
05565 add_header(&req, "Max-Forwards", DEFAULT_MAX_FORWARDS);
05566
05567
05568 if (auth)
05569 add_header(&req, authheader, auth);
05570 else if (!ast_strlen_zero(r->nonce)) {
05571 char digest[1024];
05572
05573
05574 if (sipdebug)
05575 ast_log(LOG_DEBUG, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
05576 ast_copy_string(p->realm, r->realm, sizeof(p->realm));
05577 ast_copy_string(p->nonce, r->nonce, sizeof(p->nonce));
05578 ast_copy_string(p->domain, r->domain, sizeof(p->domain));
05579 ast_copy_string(p->opaque, r->opaque, sizeof(p->opaque));
05580 ast_copy_string(p->qop, r->qop, sizeof(p->qop));
05581 p->noncecount = r->noncecount++;
05582
05583 memset(digest,0,sizeof(digest));
05584 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest)))
05585 add_header(&req, "Authorization", digest);
05586 else
05587 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
05588
05589 }
05590
05591 snprintf(tmp, sizeof(tmp), "%d", default_expiry);
05592 add_header(&req, "Expires", tmp);
05593 add_header(&req, "Contact", p->our_contact);
05594 add_header(&req, "Event", "registration");
05595 add_header_contentLength(&req, 0);
05596 add_blank_header(&req);
05597 copy_request(&p->initreq, &req);
05598 parse_request(&p->initreq);
05599 if (sip_debug_test_pvt(p)) {
05600 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
05601 }
05602 determine_firstline_parts(&p->initreq);
05603 r->regstate=auth?REG_STATE_AUTHSENT:REG_STATE_REGSENT;
05604 r->regattempts++;
05605 if (option_debug > 3)
05606 ast_verbose("REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
05607 return send_request(p, &req, 2, p->ocseq);
05608 }
05609
05610
05611 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
05612 {
05613 struct sip_request req;
05614 reqprep(&req, p, SIP_MESSAGE, 0, 1);
05615 add_text(&req, text);
05616 return send_request(p, &req, 1, p->ocseq);
05617 }
05618
05619
05620 static int transmit_refer(struct sip_pvt *p, const char *dest)
05621 {
05622 struct sip_request req;
05623 char from[256];
05624 char *of, *c;
05625 char referto[256];
05626
05627 if (ast_test_flag(p, SIP_OUTGOING))
05628 of = get_header(&p->initreq, "To");
05629 else
05630 of = get_header(&p->initreq, "From");
05631 ast_copy_string(from, of, sizeof(from));
05632 of = get_in_brackets(from);
05633 ast_copy_string(p->from,of,sizeof(p->from));
05634 if (strncmp(of, "sip:", 4)) {
05635 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
05636 } else
05637 of += 4;
05638
05639 if ((c = strchr(dest, '@'))) {
05640 c = NULL;
05641 } else if ((c = strchr(of, '@'))) {
05642 *c = '\0';
05643 c++;
05644 }
05645 if (c) {
05646 snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
05647 } else {
05648 snprintf(referto, sizeof(referto), "<sip:%s>", dest);
05649 }
05650
05651
05652 ast_copy_string(p->refer_to, referto, sizeof(p->refer_to));
05653 ast_copy_string(p->referred_by, p->our_contact, sizeof(p->referred_by));
05654
05655 reqprep(&req, p, SIP_REFER, 0, 1);
05656 add_header(&req, "Refer-To", referto);
05657 if (!ast_strlen_zero(p->our_contact))
05658 add_header(&req, "Referred-By", p->our_contact);
05659 add_blank_header(&req);
05660 return send_request(p, &req, 1, p->ocseq);
05661 }
05662
05663
05664
05665 static int transmit_info_with_digit(struct sip_pvt *p, char digit)
05666 {
05667 struct sip_request req;
05668 reqprep(&req, p, SIP_INFO, 0, 1);
05669 add_digit(&req, digit);
05670 return send_request(p, &req, 1, p->ocseq);
05671 }
05672
05673
05674 static int transmit_info_with_vidupdate(struct sip_pvt *p)
05675 {
05676 struct sip_request req;
05677 reqprep(&req, p, SIP_INFO, 0, 1);
05678 add_vidupdate(&req);
05679 return send_request(p, &req, 1, p->ocseq);
05680 }
05681
05682
05683 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, int reliable, int newbranch)
05684 {
05685 struct sip_request resp;
05686 reqprep(&resp, p, sipmethod, seqno, newbranch);
05687 add_header_contentLength(&resp, 0);
05688 add_blank_header(&resp);
05689 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
05690 }
05691
05692
05693 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, int reliable, int newbranch)
05694 {
05695 struct sip_request resp;
05696
05697 reqprep(&resp, p, sipmethod, seqno, newbranch);
05698 if (*p->realm) {
05699 char digest[1024];
05700
05701 memset(digest, 0, sizeof(digest));
05702 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
05703 if (p->options && p->options->auth_type == PROXY_AUTH)
05704 add_header(&resp, "Proxy-Authorization", digest);
05705 else if (p->options && p->options->auth_type == WWW_AUTH)
05706 add_header(&resp, "Authorization", digest);
05707 else
05708 add_header(&resp, "Proxy-Authorization", digest);
05709 } else
05710 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
05711 }
05712
05713
05714 if (sipmethod == SIP_BYE) {
05715 if (p->owner && p->owner->hangupcause) {
05716 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
05717 }
05718 }
05719
05720 add_header_contentLength(&resp, 0);
05721 add_blank_header(&resp);
05722 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
05723 }
05724
05725 static void destroy_association(struct sip_peer *peer)
05726 {
05727 if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_IGNOREREGEXPIRE)) {
05728 if (ast_test_flag(&(peer->flags_page2), SIP_PAGE2_RT_FROMCONTACT)) {
05729 ast_update_realtime("sippeers", "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "username", "", NULL);
05730 } else {
05731 ast_db_del("SIP/Registry", peer->name);
05732 }
05733 }
05734 }
05735
05736
05737 static int expire_register(void *data)
05738 {
05739 struct sip_peer *peer = data;
05740
05741 if (!peer)
05742 return 0;
05743
05744 memset(&peer->addr, 0, sizeof(peer->addr));
05745
05746 destroy_association(peer);
05747
05748 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
05749 register_peer_exten(peer, 0);
05750 peer->expire = -1;
05751 ast_device_state_changed("SIP/%s", peer->name);
05752
05753
05754
05755
05756 if (ast_test_flag(peer, SIP_SELFDESTRUCT) || ast_test_flag((&peer->flags_page2), SIP_PAGE2_RTAUTOCLEAR)) {
05757 peer = ASTOBJ_CONTAINER_UNLINK(&peerl, peer);
05758 ASTOBJ_UNREF(peer, sip_destroy_peer);
05759 }
05760
05761 return 0;
05762 }
05763
05764 static int sip_poke_peer(struct sip_peer *peer);
05765
05766 static int sip_poke_peer_s(void *data)
05767 {
05768 struct sip_peer *peer = data;
05769 peer->pokeexpire = -1;
05770 sip_poke_peer(peer);
05771 return 0;
05772 }
05773
05774
05775 static void reg_source_db(struct sip_peer *peer)
05776 {
05777 char data[256];
05778 char iabuf[INET_ADDRSTRLEN];
05779 struct in_addr in;
05780 int expiry;
05781 int port;
05782 char *scan, *addr, *port_str, *expiry_str, *username, *contact;
05783
05784 if (ast_test_flag(&(peer->flags_page2), SIP_PAGE2_RT_FROMCONTACT))
05785 return;
05786 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
05787 return;
05788
05789 scan = data;
05790 addr = strsep(&scan, ":");
05791 port_str = strsep(&scan, ":");
05792 expiry_str = strsep(&scan, ":");
05793 username = strsep(&scan, ":");
05794 contact = scan;
05795
05796 if (!inet_aton(addr, &in))
05797 return;
05798
05799 if (port_str)
05800 port = atoi(port_str);
05801 else
05802 return;
05803
05804 if (expiry_str)
05805 expiry = atoi(expiry_str);
05806 else
05807 return;
05808
05809 if (username)
05810 ast_copy_string(peer->username, username, sizeof(peer->username));
05811 if (contact)
05812 ast_copy_string(peer->fullcontact, contact, sizeof(peer->fullcontact));
05813
05814 if (option_verbose > 2)
05815 ast_verbose(VERBOSE_PREFIX_3 "SIP Seeding peer from astdb: '%s' at %s@%s:%d for %d\n",
05816 peer->name, peer->username, ast_inet_ntoa(iabuf, sizeof(iabuf), in), port, expiry);
05817
05818 memset(&peer->addr, 0, sizeof(peer->addr));
05819 peer->addr.sin_family = AF_INET;
05820 peer->addr.sin_addr = in;
05821 peer->addr.sin_port = htons(port);
05822 if (sipsock < 0) {
05823
05824 if (peer->pokeexpire > -1)
05825 ast_sched_del(sched, peer->pokeexpire);
05826 peer->pokeexpire = ast_sched_add(sched, thread_safe_rand() % 5000 + 1, sip_poke_peer_s, peer);
05827 } else
05828 sip_poke_peer(peer);
05829 if (peer->expire > -1)
05830 ast_sched_del(sched, peer->expire);
05831 peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, peer);
05832 register_peer_exten(peer, 1);
05833 }
05834
05835
05836 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
05837 {
05838 char contact[SIP_LEN_CONTACT];
05839 char *c, *n, *pt;
05840 int port;
05841 struct hostent *hp;
05842 struct ast_hostent ahp;
05843 struct sockaddr_in oldsin;
05844
05845
05846 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
05847 c = get_in_brackets(contact);
05848
05849
05850 ast_copy_string(pvt->fullcontact, c, sizeof(pvt->fullcontact));
05851
05852
05853 ast_copy_string(pvt->okcontacturi, c, sizeof(pvt->okcontacturi));
05854
05855
05856 if (strncasecmp(c, "sip:", 4)) {
05857 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", c);
05858 } else
05859 c += 4;
05860
05861
05862 n = strchr(c, ';');
05863 if (n)
05864 *n = '\0';
05865
05866
05867 n = strchr(c, '@');
05868 if (!n) {
05869 n = c;
05870 c = NULL;
05871 } else {
05872 *n = '\0';
05873 n++;
05874 }
05875 pt = strchr(n, ':');
05876 if (pt) {
05877 *pt = '\0';
05878 pt++;
05879 port = atoi(pt);
05880 } else
05881 port = DEFAULT_SIP_PORT;
05882
05883 memcpy(&oldsin, &pvt->sa, sizeof(oldsin));
05884
05885 if (!(ast_test_flag(pvt, SIP_NAT) & SIP_NAT_ROUTE)) {
05886
05887
05888 hp = ast_gethostbyname(n, &ahp);
05889 if (!hp) {
05890 ast_log(LOG_WARNING, "Invalid host '%s'\n", n);
05891 return -1;
05892 }
05893 pvt->sa.sin_family = AF_INET;
05894 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
05895 pvt->sa.sin_port = htons(port);
05896 } else {
05897
05898
05899 memcpy(&pvt->sa, &pvt->recv, sizeof(pvt->sa));
05900 }
05901 return 0;
05902 }
05903
05904
05905 enum parse_register_result {
05906 PARSE_REGISTER_FAILED,
05907 PARSE_REGISTER_UPDATE,
05908 PARSE_REGISTER_QUERY,
05909 };
05910
05911
05912 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req)
05913 {
05914 char contact[BUFSIZ];
05915 char data[BUFSIZ];
05916 char iabuf[INET_ADDRSTRLEN];
05917 char *expires = get_header(req, "Expires");
05918 int expiry = atoi(expires);
05919 char *c, *n, *pt;
05920 int port;
05921 char *useragent;
05922 struct hostent *hp;
05923 struct ast_hostent ahp;
05924 struct sockaddr_in oldsin;
05925
05926 if (ast_strlen_zero(expires)) {
05927 expires = strcasestr(get_header(req, "Contact"), ";expires=");
05928 if (expires) {
05929 char *ptr;
05930 if ((ptr = strchr(expires, ';')))
05931 *ptr = '\0';
05932 if (sscanf(expires + 9, "%d", &expiry) != 1)
05933 expiry = default_expiry;
05934 } else {
05935
05936 expiry = default_expiry;
05937 }
05938 }
05939
05940 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
05941 if (strchr(contact, '<') == NULL) {
05942 char *ptr = strchr(contact, ';');
05943 if (ptr)
05944 *ptr = '\0';
05945 }
05946 c = get_in_brackets(contact);
05947
05948
05949
05950
05951
05952 if (ast_strlen_zero(c) && ast_strlen_zero(expires)) {
05953
05954 if ((p->expire > -1) && !ast_strlen_zero(p->fullcontact)) {
05955 pvt->expiry = ast_sched_when(sched, p->expire);
05956 }
05957 return PARSE_REGISTER_QUERY;
05958 } else if (!strcasecmp(c, "*") || !expiry) {
05959
05960 memset(&p->addr, 0, sizeof(p->addr));
05961 if (p->expire > -1)
05962 ast_sched_del(sched, p->expire);
05963 p->expire = -1;
05964
05965 destroy_association(p);
05966
05967 register_peer_exten(p, 0);
05968 p->fullcontact[0] = '\0';
05969 p->useragent[0] = '\0';
05970 p->sipoptions = 0;
05971 p->lastms = 0;
05972
05973 if (option_verbose > 2)
05974 ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", p->name);
05975 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\n", p->name);
05976 return PARSE_REGISTER_UPDATE;
05977 }
05978 ast_copy_string(p->fullcontact, c, sizeof(p->fullcontact));
05979
05980 snprintf(pvt->our_contact, sizeof(pvt->our_contact) - 1, "<%s>", c);
05981
05982 if (strncasecmp(c, "sip:", 4)) {
05983 ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", c);
05984 } else
05985 c += 4;
05986
05987 n = strchr(c, ';');
05988 if (n) {
05989 *n = '\0';
05990 }
05991
05992 n = strchr(c, '@');
05993 if (!n) {
05994 n = c;
05995 c = NULL;
05996 } else {
05997 *n = '\0';
05998 n++;
05999 }
06000 pt = strchr(n, ':');
06001 if (pt) {
06002 *pt = '\0';
06003 pt++;
06004 port = atoi(pt);
06005 } else
06006 port = DEFAULT_SIP_PORT;
06007 memcpy(&oldsin, &p->addr, sizeof(oldsin));
06008 if (!(ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE)) {
06009
06010 hp = ast_gethostbyname(n, &ahp);
06011 if (!hp) {
06012 ast_log(LOG_WARNING, "Invalid host '%s'\n", n);
06013 return PARSE_REGISTER_FAILED;
06014 }
06015 p->addr.sin_family = AF_INET;
06016 memcpy(&p->addr.sin_addr, hp->h_addr, sizeof(p->addr.sin_addr));
06017 p->addr.sin_port = htons(port);
06018 } else {
06019
06020
06021 memcpy(&p->addr, &pvt->recv, sizeof(p->addr));
06022 }
06023
06024 if (c)
06025 ast_copy_string(p->username, c, sizeof(p->username));
06026 else
06027 p->username[0] = '\0';
06028
06029 if (p->expire > -1)
06030 ast_sched_del(sched, p->expire);
06031 if ((expiry < 1) || (expiry > max_expiry))
06032 expiry = max_expiry;
06033 if (!ast_test_flag(p, SIP_REALTIME))
06034 p->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, p);
06035 else
06036 p->expire = -1;
06037 pvt->expiry = expiry;
06038 snprintf(data, sizeof(data), "%s:%d:%d:%s:%s", ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr), ntohs(p->addr.sin_port), expiry, p->username, p->fullcontact);
06039 if (!ast_test_flag((&p->flags_page2), SIP_PAGE2_RT_FROMCONTACT))
06040 ast_db_put("SIP/Registry", p->name, data);
06041 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", p->name);
06042 if (inaddrcmp(&p->addr, &oldsin)) {
06043 sip_poke_peer(p);
06044 if (option_verbose > 2)
06045 ast_verbose(VERBOSE_PREFIX_3 "Registered SIP '%s' at %s port %d expires %d\n", p->name, ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr), ntohs(p->addr.sin_port), expiry);
06046 register_peer_exten(p, 1);
06047 }
06048
06049
06050 p->sipoptions = pvt->sipoptions;
06051
06052
06053 useragent = get_header(req, "User-Agent");
06054 if (useragent && strcasecmp(useragent, p->useragent)) {
06055 ast_copy_string(p->useragent, useragent, sizeof(p->useragent));
06056 if (option_verbose > 3) {
06057 ast_verbose(VERBOSE_PREFIX_3 "Saved useragent \"%s\" for peer %s\n",p->useragent,p->name);
06058 }
06059 }
06060 return PARSE_REGISTER_UPDATE;
06061 }
06062
06063
06064 static void free_old_route(struct sip_route *route)
06065 {
06066 struct sip_route *next;
06067 while (route) {
06068 next = route->next;
06069 free(route);
06070 route = next;
06071 }
06072 }
06073
06074
06075 static void list_route(struct sip_route *route)
06076 {
06077 if (!route) {
06078 ast_verbose("list_route: no route\n");
06079 return;
06080 }
06081 while (route) {
06082 ast_verbose("list_route: hop: <%s>\n", route->hop);
06083 route = route->next;
06084 }
06085 }
06086
06087
06088 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
06089 {
06090 struct sip_route *thishop, *head, *tail;
06091 int start = 0;
06092 int len;
06093 char *rr, *contact, *c;
06094
06095
06096 if (p->route && p->route_persistant) {
06097 ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
06098 return;
06099 }
06100
06101 if (p->route) {
06102 free_old_route(p->route);
06103 p->route = NULL;
06104 }
06105
06106 p->route_persistant = backwards;
06107
06108
06109 head = NULL; tail = head;
06110
06111 for (;;) {
06112
06113 rr = __get_header(req, "Record-Route", &start);
06114 if (*rr == '\0') break;
06115 for (;;) {
06116
06117
06118 rr = strchr(rr, '<');
06119 if (!rr) break;
06120 ++rr;
06121 len = strcspn(rr, ">") + 1;
06122
06123 thishop = malloc(sizeof(*thishop) + len);
06124 if (thishop) {
06125 ast_copy_string(thishop->hop, rr, len);
06126 ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
06127
06128 if (backwards) {
06129
06130 thishop->next = head;
06131 head = thishop;
06132
06133 if (!tail) tail = thishop;
06134 } else {
06135 thishop->next = NULL;
06136
06137 if (tail)
06138 tail->next = thishop;
06139 else
06140 head = thishop;
06141 tail = thishop;
06142 }
06143 }
06144 rr += len;
06145 }
06146 }
06147
06148
06149 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop,";lr") == NULL) ) {
06150
06151
06152 contact = get_header(req, "Contact");
06153 if (!ast_strlen_zero(contact)) {
06154 ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
06155
06156 c = strchr(contact, '<');
06157 if (c) {
06158
06159 ++c;
06160 len = strcspn(c, ">") + 1;
06161 } else {
06162
06163 c = contact;
06164 len = strlen(contact) + 1;
06165 }
06166 thishop = malloc(sizeof(*thishop) + len);
06167 if (thishop) {
06168 ast_copy_string(thishop->hop, c, len);
06169 thishop->next = NULL;
06170
06171 if (tail)
06172 tail->next = thishop;
06173 else
06174 head = thishop;
06175 }
06176 }
06177 }
06178
06179
06180 p->route = head;
06181
06182
06183 if (sip_debug_test_pvt(p))
06184 list_route(p->route);
06185 }
06186
06187 #ifdef OSP_SUPPORT
06188
06189 static int check_osptoken (struct sip_pvt *p, char *token)
06190 {
06191 char tmp[80];
06192
06193 if (ast_osp_validate (NULL, token, &p->osphandle, &p->osptimelimit, p->cid_num, p->sa.sin_addr, p->exten) < 1) {
06194 return (-1);
06195 } else {
06196 snprintf (tmp, sizeof (tmp), "%d", p->osphandle);
06197 pbx_builtin_setvar_helper (p->owner, "_OSPHANDLE", tmp);
06198 return (0);
06199 }
06200 }
06201 #endif
06202
06203
06204
06205
06206 static int check_auth(struct sip_pvt *p, struct sip_request *req, char *randdata, int randlen, char *username, char *secret, char *md5secret, int sipmethod, char *uri, int reliable, int ignore)
06207 {
06208 int res = -1;
06209 char *response = "407 Proxy Authentication Required";
06210 char *reqheader = "Proxy-Authorization";
06211 char *respheader = "Proxy-Authenticate";
06212 char *authtoken;
06213 #ifdef OSP_SUPPORT
06214 char *osptoken;
06215 #endif
06216
06217 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret)
06218 #ifdef OSP_SUPPORT
06219 && !ast_test_flag(p, SIP_OSPAUTH)
06220 && global_allowguest != 2
06221 #endif
06222 )
06223 return 0;
06224 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
06225
06226
06227
06228 response = "401 Unauthorized";
06229 reqheader = "Authorization";
06230 respheader = "WWW-Authenticate";
06231 }
06232 #ifdef OSP_SUPPORT
06233 else {
06234 ast_log (LOG_DEBUG, "Checking OSP Authentication!\n");
06235 osptoken = get_header (req, "P-OSP-Auth-Token");
06236 switch (ast_test_flag (p, SIP_OSPAUTH)) {
06237 case SIP_OSPAUTH_NO:
06238 break;
06239 case SIP_OSPAUTH_GATEWAY:
06240 if (ast_strlen_zero (osptoken)) {
06241 if (ast_strlen_zero (secret) && ast_strlen_zero (md5secret)) {
06242 return (0);
06243 }
06244 }
06245 else {
06246 return (check_osptoken (p, osptoken));
06247 }
06248 break;
06249 case SIP_OSPAUTH_PROXY:
06250 if (ast_strlen_zero (osptoken)) {
06251 return (0);
06252 }
06253 else {
06254 return (check_osptoken (p, osptoken));
06255 }
06256 break;
06257 case SIP_OSPAUTH_EXCLUSIVE:
06258 if (ast_strlen_zero (osptoken)) {
06259 return (-1);
06260 }
06261 else {
06262 return (check_osptoken (p, osptoken));
06263 }
06264 break;
06265 default:
06266 return (-1);
06267 }
06268 }
06269 #endif
06270 authtoken = get_header(req, reqheader);
06271 if (ignore && !ast_strlen_zero(randdata) && ast_strlen_zero(authtoken)) {
06272
06273
06274 if (!ast_strlen_zero(randdata)) {
06275 if (!reliable) {
06276
06277
06278 transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 0);
06279
06280 sip_scheddestroy(p, 15000);
06281 }
06282 res = 1;
06283 }
06284 } else if (ast_strlen_zero(randdata) || ast_strlen_zero(authtoken)) {
06285 snprintf(randdata, randlen, "%08x", thread_safe_rand());
06286 transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 0);
06287
06288 sip_scheddestroy(p, 15000);
06289 res = 1;
06290 } else {
06291
06292
06293 char a1[256];
06294 char a2[256];
06295 char a1_hash[256];
06296 char a2_hash[256];
06297 char resp[256];
06298 char resp_hash[256]="";
06299 char tmp[256];
06300 char *c;
06301 char *z;
06302 char *ua_hash ="";
06303 char *resp_uri ="";
06304 char *nonce = "";
06305 char *digestusername = "";
06306 int wrongnonce = 0;
06307 char *usednonce = randdata;
06308
06309
06310 ast_copy_string(tmp, authtoken, sizeof(tmp));
06311 c = tmp;
06312
06313 while(c) {
06314 c = ast_skip_blanks(c);
06315 if (!*c)
06316 break;
06317 if (!strncasecmp(c, "response=", strlen("response="))) {
06318 c+= strlen("response=");
06319 if ((*c == '\"')) {
06320 ua_hash=++c;
06321 if ((c = strchr(c,'\"')))
06322 *c = '\0';
06323
06324 } else {
06325 ua_hash=c;
06326 if ((c = strchr(c,',')))
06327 *c = '\0';
06328 }
06329
06330 } else if (!strncasecmp(c, "uri=", strlen("uri="))) {
06331 c+= strlen("uri=");
06332 if ((*c == '\"')) {
06333 resp_uri=++c;
06334 if ((c = strchr(c,'\"')))
06335 *c = '\0';
06336 } else {
06337 resp_uri=c;
06338 if ((c = strchr(c,',')))
06339 *c = '\0';
06340 }
06341
06342 } else if (!strncasecmp(c, "username=", strlen("username="))) {
06343 c+= strlen("username=");
06344 if ((*c == '\"')) {
06345 digestusername=++c;
06346 if((c = strchr(c,'\"')))
06347 *c = '\0';
06348 } else {
06349 digestusername=c;
06350 if((c = strchr(c,',')))
06351 *c = '\0';
06352 }
06353 } else if (!strncasecmp(c, "nonce=", strlen("nonce="))) {
06354 c+= strlen("nonce=");
06355 if ((*c == '\"')) {
06356 nonce=++c;
06357 if ((c = strchr(c,'\"')))
06358 *c = '\0';
06359 } else {
06360 nonce=c;
06361 if ((c = strchr(c,',')))
06362 *c = '\0';
06363 }
06364
06365 } else
06366 if ((z = strchr(c,' ')) || (z = strchr(c,','))) c=z;
06367 if (c)
06368 c++;
06369 }
06370
06371 if (strcmp(username, digestusername)) {
06372
06373 return -2;
06374 }
06375
06376
06377 if (strncasecmp(randdata, nonce, randlen)) {
06378 wrongnonce = 1;
06379 usednonce = nonce;
06380 }
06381
06382 snprintf(a1, sizeof(a1), "%s:%s:%s", username, global_realm, secret);
06383
06384 if (!ast_strlen_zero(resp_uri))
06385 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text, resp_uri);
06386 else
06387 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text, uri);
06388
06389 if (!ast_strlen_zero(md5secret))
06390 snprintf(a1_hash, sizeof(a1_hash), "%s", md5secret);
06391 else
06392 ast_md5_hash(a1_hash, a1);
06393
06394 ast_md5_hash(a2_hash, a2);
06395
06396 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
06397 ast_md5_hash(resp_hash, resp);
06398
06399 if (wrongnonce) {
06400
06401 snprintf(randdata, randlen, "%08x", thread_safe_rand());
06402 if (ua_hash && !strncasecmp(ua_hash, resp_hash, strlen(resp_hash))) {
06403 if (sipdebug)
06404 ast_log(LOG_NOTICE, "stale nonce received from '%s'\n", get_header(req, "To"));
06405
06406 transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 1);
06407 } else {
06408
06409 if (sipdebug)
06410 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
06411 transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 0);
06412 }
06413
06414
06415 sip_scheddestroy(p, 15000);
06416 return 1;
06417 }
06418
06419 if (ua_hash && !strncasecmp(ua_hash, resp_hash, strlen(resp_hash))) {
06420
06421 res = 0;
06422 }
06423 }
06424
06425 return res;
06426 }
06427
06428
06429
06430
06431 static int cb_extensionstate(char *context, char* exten, int state, void *data)
06432 {
06433 struct sip_pvt *p = data;
06434
06435 switch(state) {
06436 case AST_EXTENSION_DEACTIVATED:
06437 case AST_EXTENSION_REMOVED:
06438 if (p->autokillid > -1)
06439 sip_cancel_destroy(p);
06440 sip_scheddestroy(p, 15000);
06441 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);
06442 p->stateid = -1;
06443 p->subscribed = NONE;
06444 append_history(p, "Subscribestatus", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
06445 break;
06446 default:
06447 p->laststate = state;
06448 break;
06449 }
06450 transmit_state_notify(p, state, 1, 1);
06451
06452 if (option_debug > 1)
06453 ast_verbose(VERBOSE_PREFIX_1 "Extension Changed %s new state %s for Notify User %s\n", exten, ast_extension_state2str(state), p->username);
06454 return 0;
06455 }
06456
06457
06458
06459
06460 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, char *randdata, int randlen, int reliable)
06461 {
06462 snprintf(randdata, randlen, "%08x", thread_safe_rand());
06463 transmit_response_with_auth(p, "401 Unauthorized", req, randdata, reliable, "WWW-Authenticate", 0);
06464 }
06465
06466
06467 static int register_verify(struct sip_pvt *p, struct sockaddr_in *sin, struct sip_request *req, char *uri, int ignore)
06468 {
06469 int res = -3;
06470 struct sip_peer *peer;
06471 char tmp[256];
06472 char iabuf[INET_ADDRSTRLEN];
06473 char *name, *c;
06474 char *t;
06475 char *domain;
06476
06477
06478 t = uri;
06479 while(*t && (*t > 32) && (*t != ';'))
06480 t++;
06481 *t = '\0';
06482
06483 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
06484 if (pedanticsipchecking)
06485 ast_uri_decode(tmp);
06486
06487 c = get_in_brackets(tmp);
06488
06489 name = strchr(c, ';');
06490 if (name)
06491 *name = '\0';
06492
06493 if (!strncmp(c, "sip:", 4)) {
06494 name = c + 4;
06495 } else {
06496 name = c;
06497 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr));
06498 }
06499
06500
06501 if ((c = strchr(name, '@'))) {
06502 *c++ = '\0';
06503 domain = c;
06504 if ((c = strchr(domain, ':')))
06505 *c = '\0';
06506 if (!AST_LIST_EMPTY(&domain_list)) {
06507 if (!check_sip_domain(domain, NULL, 0)) {
06508 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
06509 return -3;
06510 }
06511 }
06512 }
06513
06514 ast_copy_string(p->exten, name, sizeof(p->exten));
06515 build_contact(p);
06516 peer = find_peer(name, NULL, 1);
06517 if (!(peer && ast_apply_ha(peer->ha, sin))) {
06518 if (peer)
06519 ASTOBJ_UNREF(peer,sip_destroy_peer);
06520 }
06521 if (peer) {
06522 if (!ast_test_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC)) {
06523 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
06524 } else {
06525 ast_copy_flags(p, peer, SIP_NAT);
06526 transmit_response(p, "100 Trying", req);
06527 if (!(res = check_auth(p, req, p->randdata, sizeof(p->randdata), peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, 0, ignore))) {
06528 sip_cancel_destroy(p);
06529 switch (parse_register_contact(p, peer, req)) {
06530 case PARSE_REGISTER_FAILED:
06531 ast_log(LOG_WARNING, "Failed to parse contact info\n");
06532 transmit_response_with_date(p, "400 Bad Request", req);
06533 peer->lastmsgssent = -1;
06534 res = 0;
06535 break;
06536 case PARSE_REGISTER_QUERY:
06537 transmit_response_with_date(p, "200 OK", req);
06538 peer->lastmsgssent = -1;
06539 res = 0;
06540 break;
06541 case PARSE_REGISTER_UPDATE:
06542 update_peer(peer, p->expiry);
06543
06544 transmit_response_with_date(p, "200 OK", req);
06545 peer->lastmsgssent = -1;
06546 res = 0;
06547 break;
06548 }
06549 }
06550 }
06551 }
06552 if (!peer && autocreatepeer) {
06553
06554 peer = temp_peer(name);
06555 if (peer) {
06556 ASTOBJ_CONTAINER_LINK(&peerl, peer);
06557 sip_cancel_destroy(p);
06558 switch (parse_register_contact(p, peer, req)) {
06559 case PARSE_REGISTER_FAILED:
06560 ast_log(LOG_WARNING, "Failed to parse contact info\n");
06561 transmit_response_with_date(p, "400 Bad Request", req);
06562 peer->lastmsgssent = -1;
06563 res = 0;
06564 break;
06565 case PARSE_REGISTER_QUERY:
06566 transmit_response_with_date(p, "200 OK", req);
06567 peer->lastmsgssent = -1;
06568 res = 0;
06569 break;
06570 case PARSE_REGISTER_UPDATE:
06571
06572 transmit_response_with_date(p, "200 OK", req);
06573 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Registered\r\n", peer->name);
06574 peer->lastmsgssent = -1;
06575 res = 0;
06576 break;
06577 }
06578 }
06579 }
06580 if (!res) {
06581 ast_device_state_changed("SIP/%s", peer->name);
06582 }
06583 if (res < 0) {
06584 switch (res) {
06585 case -1:
06586
06587 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
06588 break;
06589 case -2:
06590
06591
06592
06593
06594 transmit_response(p, "403 Authentication user name does not match account name", &p->initreq);
06595 break;
06596 case -3:
06597 if (global_alwaysauthreject) {
06598 transmit_fake_auth_response(p, &p->initreq, p->randdata, sizeof(p->randdata), 1);
06599 } else {
06600
06601 transmit_response(p, "404 Not found", &p->initreq);
06602 }
06603
06604 res = -2;
06605 break;
06606 }
06607 if (option_debug > 1) {
06608 ast_log(LOG_DEBUG, "SIP REGISTER attempt failed for %s : %s\n",
06609 peer->name,
06610 (res == -1) ? "Bad password" : ((res == -2 ) ? "Bad digest user" : "Peer not found"));
06611 }
06612 }
06613 if (peer)
06614 ASTOBJ_UNREF(peer,sip_destroy_peer);
06615
06616 return res;
06617 }
06618
06619
06620 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq)
06621 {
06622 char tmp[256], *c, *a;
06623 struct sip_request *req;
06624
06625 req = oreq;
06626 if (!req)
06627 req = &p->initreq;
06628 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
06629 if (ast_strlen_zero(tmp))
06630 return 0;
06631 c = get_in_brackets(tmp);
06632 if (strncmp(c, "sip:", 4)) {
06633 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", c);
06634 return -1;
06635 }
06636 c += 4;
06637 if ((a = strchr(c, '@')) || (a = strchr(c, ';'))) {
06638 *a = '\0';
06639 }
06640 if (sip_debug_test_pvt(p))
06641 ast_verbose("RDNIS is %s\n", c);
06642 ast_copy_string(p->rdnis, c, sizeof(p->rdnis));
06643
06644 return 0;
06645 }
06646
06647
06648 static int get_destination(struct sip_pvt *p, struct sip_request *oreq)
06649 {
06650 char tmp[256] = "", *uri, *a, *user, *domain, *opts;
06651 char tmpf[256], *from;
06652 struct sip_request *req;
06653 char *colon;
06654
06655 req = oreq;
06656 if (!req)
06657 req = &p->initreq;
06658 if (req->rlPart2)
06659 ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
06660 uri = get_in_brackets(tmp);
06661
06662 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
06663
06664 from = get_in_brackets(tmpf);
06665
06666 if (strncmp(uri, "sip:", 4)) {
06667 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", uri);
06668 return -1;
06669 }
06670 uri += 4;
06671 if (!ast_strlen_zero(from)) {
06672 if (strncmp(from, "sip:", 4)) {
06673 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
06674 return -1;
06675 }
06676 from += 4;
06677 } else
06678 from = NULL;
06679
06680 if (pedanticsipchecking) {
06681 ast_uri_decode(uri);
06682 ast_uri_decode(from);
06683 }
06684
06685
06686 if ((domain = strchr(uri, '@'))) {
06687 *domain++ = '\0';
06688 user = uri;
06689 } else {
06690
06691 domain = uri;
06692 user = "s";
06693 }
06694
06695
06696 if ((colon = strchr(domain, ':'))) {
06697 *colon = '\0';
06698 }
06699
06700
06701 if ((opts = strchr(user, ';'))) {
06702 *opts = '\0';
06703 }
06704
06705 ast_copy_string(p->domain, domain, sizeof(p->domain));
06706
06707 if (!AST_LIST_EMPTY(&domain_list)) {
06708 char domain_context[AST_MAX_EXTENSION];
06709
06710 domain_context[0] = '\0';
06711 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
06712 if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
06713 ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
06714 return -2;
06715 }
06716 }
06717
06718 if (!ast_strlen_zero(domain_context))
06719 ast_copy_string(p->context, domain_context, sizeof(p->context));
06720 }
06721
06722 if (from) {
06723 if ((a = strchr(from, ';')))
06724 *a = '\0';
06725 if ((a = strchr(from, '@'))) {
06726 *a = '\0';
06727 ast_copy_string(p->fromdomain, a + 1, sizeof(p->fromdomain));
06728 } else
06729 ast_copy_string(p->fromdomain, from, sizeof(p->fromdomain));
06730 }
06731 if (sip_debug_test_pvt(p))
06732 ast_verbose("Looking for %s in %s (domain %s)\n", user, p->context, p->domain);
06733
06734
06735 if (ast_exists_extension(NULL, p->context, user, 1, from) ||
06736 !strcmp(uri, ast_pickup_ext())) {
06737 if (!oreq)
06738 ast_copy_string(p->exten, user, sizeof(p->exten));
06739 return 0;
06740 }
06741
06742
06743 if (ast_canmatch_extension(NULL, p->context, user, 1, from) ||
06744 !strncmp(user, ast_pickup_ext(),strlen(user))) {
06745 return 1;
06746 }
06747
06748 return -1;
06749 }
06750
06751
06752 static struct sip_pvt *get_sip_pvt_byid_locked(char *callid)
06753 {
06754 struct sip_pvt *sip_pvt_ptr = NULL;
06755
06756
06757 ast_mutex_lock(&iflock);
06758 sip_pvt_ptr = iflist;
06759 while(sip_pvt_ptr) {
06760 if (!strcmp(sip_pvt_ptr->callid, callid)) {
06761
06762 ast_mutex_lock(&sip_pvt_ptr->lock);
06763 if (sip_pvt_ptr->owner) {
06764 while(ast_mutex_trylock(&sip_pvt_ptr->owner->lock)) {
06765 ast_mutex_unlock(&sip_pvt_ptr->lock);
06766 usleep(1);
06767 ast_mutex_lock(&sip_pvt_ptr->lock);
06768 if (!sip_pvt_ptr->owner)
06769 break;
06770 }
06771 }
06772 break;
06773 }
06774 sip_pvt_ptr = sip_pvt_ptr->next;
06775 }
06776 ast_mutex_unlock(&iflock);
06777 return sip_pvt_ptr;
06778 }
06779
06780
06781 static int get_refer_info(struct sip_pvt *sip_pvt, struct sip_request *outgoing_req)
06782 {
06783
06784 char *p_refer_to = NULL, *p_referred_by = NULL, *h_refer_to = NULL, *h_referred_by = NULL, *h_contact = NULL;
06785 char *replace_callid = "", *refer_to = NULL, *referred_by = NULL, *ptr = NULL;
06786 struct sip_request *req = NULL;
06787 struct sip_pvt *sip_pvt_ptr = NULL;
06788 struct ast_channel *chan = NULL, *peer = NULL;
06789 const char *transfercontext;
06790
06791 req = outgoing_req;
06792
06793 if (!req) {
06794 req = &sip_pvt->initreq;
06795 }
06796
06797 if (!( (p_refer_to = get_header(req, "Refer-To")) && (h_refer_to = ast_strdupa(p_refer_to)) )) {
06798 ast_log(LOG_WARNING, "No Refer-To Header That's illegal\n");
06799 return -1;
06800 }
06801
06802 refer_to = get_in_brackets(h_refer_to);
06803
06804 if (!( (p_referred_by = get_header(req, "Referred-By")) && (h_referred_by = ast_strdupa(p_referred_by)) )) {
06805 ast_log(LOG_WARNING, "No Referrred-By Header That's not illegal\n");
06806 return -1;
06807 } else {
06808 if (pedanticsipchecking) {
06809 ast_uri_decode(h_referred_by);
06810 }
06811 referred_by = get_in_brackets(h_referred_by);
06812 }
06813 h_contact = get_header(req, "Contact");
06814
06815 if (strncmp(refer_to, "sip:", 4)) {
06816 ast_log(LOG_WARNING, "Refer-to: Huh? Not a SIP header (%s)?\n", refer_to);
06817 return -1;
06818 }
06819
06820 if (strncmp(referred_by, "sip:", 4)) {
06821 ast_log(LOG_WARNING, "Referred-by: Huh? Not a SIP header (%s) Ignoring?\n", referred_by);
06822 referred_by = NULL;
06823 }
06824
06825 if (refer_to)
06826 refer_to += 4;
06827
06828 if (referred_by)
06829 referred_by += 4;
06830
06831 if ((ptr = strchr(refer_to, '?'))) {
06832
06833 *ptr = '\0';
06834 ptr++;
06835 if (!strncasecmp(ptr, "REPLACES=", 9)) {
06836 char *p;
06837 replace_callid = ast_strdupa(ptr + 9);
06838
06839
06840
06841
06842 ast_uri_decode(replace_callid);
06843 if ((ptr = strchr(replace_callid, '%')))
06844 *ptr = '\0';
06845 if ((ptr = strchr(replace_callid, ';')))
06846 *ptr = '\0';
06847
06848 p = ast_skip_blanks(replace_callid);
06849 if (p != replace_callid)
06850 memmove(replace_callid, p, strlen(p));
06851 }
06852 }
06853
06854 if ((ptr = strchr(refer_to, '@')))
06855 *ptr = '\0';
06856 if ((ptr = strchr(refer_to, ';')))
06857 *ptr = '\0';
06858
06859 if (referred_by) {
06860 if ((ptr = strchr(referred_by, '@')))
06861 *ptr = '\0';
06862 if ((ptr = strchr(referred_by, ';')))
06863 *ptr = '\0';
06864 }
06865
06866 transfercontext = pbx_builtin_getvar_helper(sip_pvt->owner, "TRANSFER_CONTEXT");
06867 if (ast_strlen_zero(transfercontext))
06868 transfercontext = sip_pvt->context;
06869
06870 if (sip_debug_test_pvt(sip_pvt)) {
06871 ast_verbose("Transfer to %s in %s\n", refer_to, transfercontext);
06872 if (referred_by)
06873 ast_verbose("Transfer from %s in %s\n", referred_by, sip_pvt->context);
06874 }
06875 if (!ast_strlen_zero(replace_callid)) {
06876
06877 ast_log(LOG_DEBUG,"Assigning Replace-Call-ID Info %s to REPLACE_CALL_ID\n",replace_callid);
06878
06879 ast_copy_string(sip_pvt->refer_to, "", sizeof(sip_pvt->refer_to));
06880 ast_copy_string(sip_pvt->referred_by, "", sizeof(sip_pvt->referred_by));
06881 ast_copy_string(sip_pvt->refer_contact, "", sizeof(sip_pvt->refer_contact));
06882 sip_pvt->refer_call = NULL;
06883 if ((sip_pvt_ptr = get_sip_pvt_byid_locked(replace_callid))) {
06884 sip_pvt->refer_call = sip_pvt_ptr;
06885 if (sip_pvt->refer_call == sip_pvt) {
06886 ast_log(LOG_NOTICE, "Supervised transfer attempted to transfer into same call id (%s == %s)!\n", replace_callid, sip_pvt->callid);
06887 sip_pvt->refer_call = NULL;
06888 } else
06889 return 0;
06890 } else {
06891 ast_log(LOG_NOTICE, "Supervised transfer requested, but unable to find callid '%s'. Both legs must reside on Asterisk box to transfer at this time.\n", replace_callid);
06892
06893
06894
06895 }
06896 } else if (ast_exists_extension(NULL, transfercontext, refer_to, 1, NULL) || !strcmp(refer_to, ast_parking_ext())) {
06897
06898
06899 ast_log(LOG_DEBUG,"Unsupervised transfer to (Refer-To): %s\n", refer_to);
06900 if (referred_by)
06901 ast_log(LOG_DEBUG,"Transferred by (Referred-by: ) %s \n", referred_by);
06902 ast_log(LOG_DEBUG,"Transfer Contact Info %s (REFER_CONTACT)\n", h_contact);
06903 ast_copy_string(sip_pvt->refer_to, refer_to, sizeof(sip_pvt->refer_to));
06904 if (referred_by)
06905 ast_copy_string(sip_pvt->referred_by, referred_by, sizeof(sip_pvt->referred_by));
06906 if (h_contact) {
06907 ast_copy_string(sip_pvt->refer_contact, h_contact, sizeof(sip_pvt->refer_contact));
06908 }
06909 sip_pvt->refer_call = NULL;
06910 if ((chan = sip_pvt->owner) && (peer = ast_bridged_channel(sip_pvt->owner))) {
06911 pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", peer->name);
06912 pbx_builtin_setvar_helper(peer, "BLINDTRANSFER", chan->name);
06913 }
06914 return 0;
06915 } else if (ast_canmatch_extension(NULL, transfercontext, refer_to, 1, NULL)) {
06916 return 1;
06917 }
06918
06919 return -1;
06920 }
06921
06922
06923 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
06924 {
06925 char tmp[256], *c, *a;
06926 struct sip_request *req;
06927
06928 req = oreq;
06929 if (!req)
06930 req = &p->initreq;
06931 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
06932
06933 c = get_in_brackets(tmp);
06934
06935
06936 if (strncmp(c, "sip:", 4)) {
06937 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
06938 return -1;
06939 }
06940 c += 4;
06941 if ((a = strchr(c, '@')))
06942 *a = '\0';
06943 if ((a = strchr(c, ';')))
06944 *a = '\0';
06945
06946 if (sip_debug_test_pvt(p)) {
06947 ast_verbose("Looking for %s in %s\n", c, p->context);
06948 }
06949 if (ast_exists_extension(NULL, p->context, c, 1, NULL)) {
06950
06951 ast_log(LOG_DEBUG,"Assigning Extension %s to REFER-TO\n", c);
06952 ast_copy_string(p->refer_to, c, sizeof(p->refer_to));
06953 ast_copy_string(p->referred_by, "", sizeof(p->referred_by));
06954 ast_copy_string(p->refer_contact, "", sizeof(p->refer_contact));
06955 p->refer_call = NULL;
06956 return 0;
06957 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
06958 return 1;
06959 }
06960
06961 return -1;
06962 }
06963
06964
06965 static int check_via(struct sip_pvt *p, struct sip_request *req)
06966 {
06967 char via[256];
06968 char iabuf[INET_ADDRSTRLEN];
06969 char *c, *pt;
06970 struct hostent *hp;
06971 struct ast_hostent ahp;
06972
06973 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
06974
06975
06976 c = strstr(via, ";rport");
06977 if (c && (c[6] != '='))
06978 ast_set_flag(p, SIP_NAT_ROUTE);
06979
06980 c = strchr(via, ';');
06981 if (c)
06982 *c = '\0';
06983
06984 c = strchr(via, ' ');
06985 if (c) {
06986 *c = '\0';
06987 c = ast_skip_blanks(c+1);
06988 if (strcasecmp(via, "SIP/2.0/UDP")) {
06989 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
06990 return -1;
06991 }
06992 pt = strchr(c, ':');
06993 if (pt)
06994 *pt++ = '\0';
06995 hp = ast_gethostbyname(c, &ahp);
06996 if (!hp) {
06997 ast_log(LOG_WARNING, "'%s' is not a valid host\n", c);
06998 return -1;
06999 }
07000 memset(&p->sa, 0, sizeof(p->sa));
07001 p->sa.sin_family = AF_INET;
07002 memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
07003 p->sa.sin_port = htons(pt ? atoi(pt) : DEFAULT_SIP_PORT);
07004
07005 if (sip_debug_test_pvt(p)) {
07006 c = (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE) ? "NAT" : "non-NAT";
07007 ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), c);
07008 }
07009 }
07010 return 0;
07011 }
07012
07013
07014 static char *get_calleridname(char *input, char *output, size_t outputsize)
07015 {
07016 char *end = strchr(input,'<');
07017 char *tmp = strchr(input,'\"');
07018 int bytes = 0;
07019 int maxbytes = outputsize - 1;
07020
07021 if (!end || (end == input)) return NULL;
07022
07023 end--;
07024
07025 if (tmp && tmp < end) {
07026 end = strchr(tmp+1, '\"');
07027 if (!end) return NULL;
07028 bytes = (int) (end - tmp);
07029
07030 if (bytes > maxbytes)
07031 bytes = maxbytes;
07032 ast_copy_string(output, tmp + 1, bytes);
07033 } else {
07034
07035
07036 input = ast_skip_blanks(input);
07037
07038 while(*end && (*end < 33) && end > input)
07039 end--;
07040 if (end >= input) {
07041 bytes = (int) (end - input) + 2;
07042
07043 if (bytes > maxbytes) {
07044 bytes = maxbytes;
07045 }
07046 ast_copy_string(output, input, bytes);
07047 }
07048 else
07049 return NULL;
07050 }
07051 return output;
07052 }
07053
07054
07055
07056
07057
07058 static int get_rpid_num(char *input,char *output, int maxlen)
07059 {
07060 char *start;
07061 char *end;
07062
07063 start = strchr(input,':');
07064 if (!start) {
07065 output[0] = '\0';
07066 return 0;
07067 }
07068 start++;
07069
07070
07071 ast_copy_string(output,start,maxlen);
07072 output[maxlen-1] = '\0';
07073
07074 end = strchr(output,'@');
07075 if (end)
07076 *end = '\0';
07077 else
07078 output[0] = '\0';
07079 if (strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
07080 return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
07081
07082 return 0;
07083 }
07084
07085
07086
07087
07088
07089 static int check_user_full(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, int reliable, struct sockaddr_in *sin, int ignore, char *mailbox, int mailboxlen)
07090 {
07091 struct sip_user *user = NULL;
07092 struct sip_peer *peer;
07093 char *of, from[256], *c;
07094 char *rpid,rpid_num[50];
07095 char iabuf[INET_ADDRSTRLEN];
07096 int res = 0;
07097 char *t;
07098 char calleridname[50];
07099 int debug=sip_debug_test_addr(sin);
07100 struct ast_variable *tmpvar = NULL, *v = NULL;
07101 char *uri2 = ast_strdupa(uri);
07102
07103
07104 t = uri2;
07105 while(*t && (*t > 32) && (*t != ';'))
07106 t++;
07107 *t = '\0';
07108 of = get_header(req, "From");
07109 if (pedanticsipchecking)
07110 ast_uri_decode(of);
07111
07112 ast_copy_string(from, of, sizeof(from));
07113
07114 memset(calleridname,0,sizeof(calleridname));
07115 get_calleridname(from, calleridname, sizeof(calleridname));
07116 if (calleridname[0])
07117 ast_copy_string(p->cid_name, calleridname, sizeof(p->cid_name));
07118
07119 rpid = get_header(req, "Remote-Party-ID");
07120 memset(rpid_num,0,sizeof(rpid_num));
07121 if (!ast_strlen_zero(rpid))
07122 p->callingpres = get_rpid_num(rpid,rpid_num, sizeof(rpid_num));
07123
07124 of = get_in_brackets(from);
07125 if (ast_strlen_zero(p->exten)) {
07126 t = uri2;
07127 if (!strncmp(t, "sip:", 4))
07128 t+= 4;
07129 ast_copy_string(p->exten, t, sizeof(p->exten));
07130 t = strchr(p->exten, '@');
07131 if (t)
07132 *t = '\0';
07133 if (ast_strlen_zero(p->our_contact))
07134 build_contact(p);
07135 }
07136
07137 ast_copy_string(p->from, of, sizeof(p->from));
07138 if (strncmp(of, "sip:", 4)) {
07139 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
07140 } else
07141 of += 4;
07142
07143 if ((c = strchr(of, '@'))) {
07144 *c = '\0';
07145 if ((c = strchr(of, ':')))
07146 *c = '\0';
07147 ast_copy_string(p->cid_num, of, sizeof(p->cid_num));
07148 ast_shrink_phone_number(p->cid_num);
07149 }
07150 if (ast_strlen_zero(of))
07151 return 0;
07152
07153 if (!mailbox)
07154 user = find_user(of, 1);
07155
07156
07157 if (user && ast_apply_ha(user->ha, sin)) {
07158 ast_copy_flags(p, user, SIP_FLAGS_TO_COPY);
07159
07160 for (v = user->chanvars ; v ; v = v->next) {
07161 if ((tmpvar = ast_variable_new(v->name, v->value))) {
07162 tmpvar->next = p->chanvars;
07163 p->chanvars = tmpvar;
07164 }
07165 }
07166 p->prefs = user->prefs;
07167
07168 if (!ast_strlen_zero(rpid_num) && ast_test_flag(p, SIP_TRUSTRPID)) {
07169 if (*calleridname)
07170 ast_copy_string(p->cid_name, calleridname, sizeof(p->cid_name));
07171 ast_copy_string(p->cid_num, rpid_num, sizeof(p->cid_num));
07172 ast_shrink_phone_number(p->cid_num);
07173 }
07174
07175 if (p->rtp) {
07176 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07177 ast_rtp_setnat(p->rtp, (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07178 }
07179 if (p->vrtp) {
07180 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07181 ast_rtp_setnat(p->vrtp, (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07182 }
07183 if (!(res = check_auth(p, req, p->randdata, sizeof(p->randdata), user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ignore))) {
07184 sip_cancel_destroy(p);
07185 ast_copy_flags(p, user, SIP_FLAGS_TO_COPY);
07186
07187 if (p->sipoptions)
07188 user->sipoptions = p->sipoptions;
07189
07190
07191 if (user->call_limit)
07192 ast_set_flag(p, SIP_CALL_LIMIT);
07193 if (!ast_strlen_zero(user->context))
07194 ast_copy_string(p->context, user->context, sizeof(p->context));
07195 if (!ast_strlen_zero(user->cid_num) && !ast_strlen_zero(p->cid_num)) {
07196 ast_copy_string(p->cid_num, user->cid_num, sizeof(p->cid_num));
07197 ast_shrink_phone_number(p->cid_num);
07198 }
07199 if (!ast_strlen_zero(user->cid_name) && !ast_strlen_zero(p->cid_num))
07200 ast_copy_string(p->cid_name, user->cid_name, sizeof(p->cid_name));
07201 ast_copy_string(p->peername, user->name, sizeof(p->peername));
07202 ast_copy_string(p->username, user->name, sizeof(p->username));
07203 ast_copy_string(p->peersecret, user->secret, sizeof(p->peersecret));
07204 ast_copy_string(p->subscribecontext, user->subscribecontext, sizeof(p->subscribecontext));
07205 ast_copy_string(p->peermd5secret, user->md5secret, sizeof(p->peermd5secret));
07206 ast_copy_string(p->accountcode, user->accountcode, sizeof(p->accountcode));
07207 ast_copy_string(p->language, user->language, sizeof(p->language));
07208 ast_copy_string(p->musicclass, user->musicclass, sizeof(p->musicclass));
07209 p->amaflags = user->amaflags;
07210 p->callgroup = user->callgroup;
07211 p->pickupgroup = user->pickupgroup;
07212 p->callingpres = user->callingpres;
07213 p->capability = user->capability;
07214 p->jointcapability = user->capability;
07215 if (p->peercapability)
07216 p->jointcapability &= p->peercapability;
07217 if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_RFC2833) || (ast_test_flag(p, SIP_DTMF) == SIP_DTMF_AUTO))
07218 p->noncodeccapability |= AST_RTP_DTMF;
07219 else
07220 p->noncodeccapability &= ~AST_RTP_DTMF;
07221 }
07222 if (user && debug)
07223 ast_verbose("Found user '%s'\n", user->name);
07224 } else {
07225 if (user) {
07226 if (!mailbox && debug)
07227 ast_verbose("Found user '%s', but fails host access\n", user->name);
07228 ASTOBJ_UNREF(user,sip_destroy_user);
07229 }
07230 user = NULL;
07231 }
07232
07233 if (!user) {
07234
07235 if (sipmethod == SIP_SUBSCRIBE)
07236
07237 peer = find_peer(of, NULL, 1);
07238 else
07239
07240
07241
07242
07243 peer = find_peer(NULL, &p->recv, 1);
07244
07245 if (peer) {
07246 if (debug)
07247 ast_verbose("Found peer '%s'\n", peer->name);
07248
07249 ast_copy_flags(p, peer, SIP_FLAGS_TO_COPY);
07250
07251
07252 if (p->sipoptions)
07253 peer->sipoptions = p->sipoptions;
07254
07255
07256 if (!ast_strlen_zero(rpid_num) && ast_test_flag(p, SIP_TRUSTRPID)) {
07257 if (*calleridname)
07258 ast_copy_string(p->cid_name, calleridname, sizeof(p->cid_name));
07259 ast_copy_string(p->cid_num, rpid_num, sizeof(p->cid_num));
07260 ast_shrink_phone_number(p->cid_num);
07261 }
07262 if (p->rtp) {
07263 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07264 ast_rtp_setnat(p->rtp, (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07265 }
07266 if (p->vrtp) {
07267 ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07268 ast_rtp_setnat(p->vrtp, (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
07269 }
07270 ast_copy_string(p->peersecret, peer->secret, sizeof(p->peersecret));
07271 p->peersecret[sizeof(p->peersecret)-1] = '\0';
07272 ast_copy_string(p->subscribecontext, peer->subscribecontext, sizeof(p->subscribecontext));
07273 ast_copy_string(p->peermd5secret, peer->md5secret, sizeof(p->peermd5secret));
07274 p->peermd5secret[sizeof(p->peermd5secret)-1] = '\0';
07275 p->callingpres = peer->callingpres;
07276 if (peer->maxms && peer->lastms)
07277 p->timer_t1 = peer->lastms;
07278 if (ast_test_flag(peer, SIP_INSECURE_INVITE)) {
07279
07280 p->peersecret[0] = '\0';
07281 p->peermd5secret[0] = '\0';
07282 }
07283 if (!(res = check_auth(p, req, p->randdata, sizeof(p->randdata), peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, ignore))) {
07284 ast_copy_flags(p, peer, SIP_FLAGS_TO_COPY);
07285
07286 if (peer->call_limit)
07287 ast_set_flag(p, SIP_CALL_LIMIT);
07288 ast_copy_string(p->peername, peer->name, sizeof(p->peername));
07289 ast_copy_string(p->authname, peer->name, sizeof(p->authname));
07290
07291 for (v = peer->chanvars ; v ; v = v->next) {
07292 if ((tmpvar = ast_variable_new(v->name, v->value))) {
07293 tmpvar->next = p->chanvars;
07294 p->chanvars = tmpvar;
07295 }
07296 }
07297 if (mailbox)
07298 snprintf(mailbox, mailboxlen, ",%s,", peer->mailbox);
07299 if (!ast_strlen_zero(peer->username)) {
07300 ast_copy_string(p->username, peer->username, sizeof(p->username));
07301
07302 ast_copy_string(p->authname, peer->username, sizeof(p->authname));
07303 }
07304 if (!ast_strlen_zero(peer->cid_num) && !ast_strlen_zero(p->cid_num)) {
07305 ast_copy_string(p->cid_num, peer->cid_num, sizeof(p->cid_num));
07306 ast_shrink_phone_number(p->cid_num);
07307 }
07308 if (!ast_strlen_zero(peer->cid_name) && !ast_strlen_zero(p->cid_name))
07309 ast_copy_string(p->cid_name, peer->cid_name, sizeof(p->cid_name));
07310 ast_copy_string(p->fullcontact, peer->fullcontact, sizeof(p->fullcontact));
07311 if (!ast_strlen_zero(peer->context))
07312 ast_copy_string(p->context, peer->context, sizeof(p->context));
07313 ast_copy_string(p->peersecret, peer->secret, sizeof(p->peersecret));
07314 ast_copy_string(p->peermd5secret, peer->md5secret, sizeof(p->peermd5secret));
07315 ast_copy_string(p->language, peer->language, sizeof(p->language));
07316 ast_copy_string(p->accountcode, peer->accountcode, sizeof(p->accountcode));
07317 p->amaflags = peer->amaflags;
07318 p->callgroup = peer->callgroup;
07319 p->pickupgroup = peer->pickupgroup;
07320 p->capability = peer->capability;
07321 p->prefs = peer->prefs;
07322 p->jointcapability = peer->capability;
07323 if (p->peercapability)
07324 p->jointcapability &= p->peercapability;
07325 if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_RFC2833) || (ast_test_flag(p, SIP_DTMF) == SIP_DTMF_AUTO))
07326 p->noncodeccapability |= AST_RTP_DTMF;
07327 else
07328 p->noncodeccapability &= ~AST_RTP_DTMF;
07329 }
07330 ASTOBJ_UNREF(peer,sip_destroy_peer);
07331 } else {
07332 if (debug)
07333 ast_verbose("Found no matching peer or user for '%s:%d'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr), ntohs(p->recv.sin_port));
07334
07335
07336 if (!global_allowguest) {
07337 if (global_alwaysauthreject)
07338 res = -4;
07339 else
07340 res = -1;
07341 #ifdef OSP_SUPPORT
07342 } else if (global_allowguest == 2) {
07343 ast_copy_flags(p, &global_flags, SIP_OSPAUTH);
07344 res = check_auth(p, req, p->randdata, sizeof(p->randdata), "", "", "", sipmethod, uri2, reliable, ignore);
07345 #endif
07346 }
07347 }
07348
07349 }
07350
07351 if (user)
07352 ASTOBJ_UNREF(user,sip_destroy_user);
07353 return res;
07354 }
07355
07356
07357 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, char *uri, int reliable, struct sockaddr_in *sin, int ignore)
07358 {
07359 return check_user_full(p, req, sipmethod, uri, reliable, sin, ignore, NULL, 0);
07360 }
07361
07362
07363 static int get_msg_text(char *buf, int len, struct sip_request *req)
07364 {
07365 int x;
07366 int y;
07367
07368 buf[0] = '\0';
07369 y = len - strlen(buf) - 5;
07370 if (y < 0)
07371 y = 0;
07372 for (x=0;x<req->lines;x++) {
07373 strncat(buf, req->line[x], y);
07374 y -= strlen(req->line[x]) + 1;
07375 if (y < 0)
07376 y = 0;
07377 if (y != 0)
07378 strcat(buf, "\n");
07379 }
07380 return 0;
07381 }
07382
07383
07384
07385
07386
07387 static void receive_message(struct sip_pvt *p, struct sip_request *req)
07388 {
07389 char buf[1024];
07390 struct ast_frame f;
07391 char *content_type;
07392
07393 content_type = get_header(req, "Content-Type");
07394 if (strcmp(content_type, "text/plain")) {
07395 transmit_response(p, "415 Unsupported Media Type", req);
07396 ast_set_flag(p, SIP_NEEDDESTROY);
07397 return;
07398 }
07399
07400 if (get_msg_text(buf, sizeof(buf), req)) {
07401 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
07402 transmit_response(p, "202 Accepted", req);
07403 ast_set_flag(p, SIP_NEEDDESTROY);
07404 return;
07405 }
07406
07407 if (p->owner) {
07408 if (sip_debug_test_pvt(p))
07409 ast_verbose("Message received: '%s'\n", buf);
07410 memset(&f, 0, sizeof(f));
07411 f.frametype = AST_FRAME_TEXT;
07412 f.subclass = 0;
07413 f.offset = 0;
07414 f.data = buf;
07415 f.datalen = strlen(buf);
07416 ast_queue_frame(p->owner, &f);
07417 transmit_response(p, "202 Accepted", req);
07418 } else {
07419 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);
07420 transmit_response(p, "405 Method Not Allowed", req);
07421 }
07422 ast_set_flag(p, SIP_NEEDDESTROY);
07423 return;
07424 }
07425
07426
07427
07428 static int sip_show_inuse(int fd, int argc, char *argv[]) {
07429 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
07430 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
07431 char ilimits[40];
07432 char iused[40];
07433 int showall = 0;
07434
07435 if (argc < 3)
07436 return RESULT_SHOWUSAGE;
07437
07438 if (argc == 4 && !strcmp(argv[3],"all"))
07439 showall = 1;
07440
07441 ast_cli(fd, FORMAT, "* User name", "In use", "Limit");
07442 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
07443 ASTOBJ_RDLOCK(iterator);
07444 if (iterator->call_limit)
07445 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
07446 else
07447 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
07448 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
07449 if (showall || iterator->call_limit)
07450 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
07451 ASTOBJ_UNLOCK(iterator);
07452 } while (0) );
07453
07454 ast_cli(fd, FORMAT, "* Peer name", "In use", "Limit");
07455
07456 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
07457 ASTOBJ_RDLOCK(iterator);
07458 if (iterator->call_limit)
07459 snprintf(ilimits, sizeof(ilimits), "%d", iterator->call_limit);
07460 else
07461 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
07462 snprintf(iused, sizeof(iused), "%d", iterator->inUse);
07463 if (showall || iterator->call_limit)
07464 ast_cli(fd, FORMAT2, iterator->name, iused, ilimits);
07465 ASTOBJ_UNLOCK(iterator);
07466 } while (0) );
07467
07468 return RESULT_SUCCESS;
07469 #undef FORMAT
07470 #undef FORMAT2
07471 }
07472
07473
07474 static char *nat2str(int nat)
07475 {
07476 switch(nat) {
07477 case SIP_NAT_NEVER:
07478 return "No";
07479 case SIP_NAT_ROUTE:
07480 return "Route";
07481 case SIP_NAT_ALWAYS:
07482 return "Always";
07483 case SIP_NAT_RFC3581:
07484 return "RFC3581";
07485 default:
07486 return "Unknown";
07487 }
07488 }
07489
07490
07491
07492 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
07493 {
07494 int res = 0;
07495 if (peer->maxms) {
07496 if (peer->lastms < 0) {
07497 ast_copy_string(status, "UNREACHABLE", statuslen);
07498 } else if (peer->lastms > peer->maxms) {
07499 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
07500 res = 1;
07501 } else if (peer->lastms) {
07502 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
07503 res = 1;
07504 } else {
07505 ast_copy_string(status, "UNKNOWN", statuslen);
07506 }
07507 } else {
07508 ast_copy_string(status, "Unmonitored", statuslen);
07509
07510 res = -1;
07511 }
07512 return res;
07513 }
07514
07515
07516 static int sip_show_users(int fd, int argc, char *argv[])
07517 {
07518 regex_t regexbuf;
07519 int havepattern = 0;
07520
07521 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
07522
07523 switch (argc) {
07524 case 5:
07525 if (!strcasecmp(argv[3], "like")) {
07526 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
07527 return RESULT_SHOWUSAGE;
07528 havepattern = 1;
07529 } else
07530 return RESULT_SHOWUSAGE;
07531 case 3:
07532 break;
07533 default:
07534 return RESULT_SHOWUSAGE;
07535 }
07536
07537 ast_cli(fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "NAT");
07538 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
07539 ASTOBJ_RDLOCK(iterator);
07540
07541 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
07542 ASTOBJ_UNLOCK(iterator);
07543 continue;
07544 }
07545
07546 ast_cli(fd, FORMAT, iterator->name,
07547 iterator->secret,
07548 iterator->accountcode,
07549 iterator->context,
07550 iterator->ha ? "Yes" : "No",
07551 nat2str(ast_test_flag(iterator, SIP_NAT)));
07552 ASTOBJ_UNLOCK(iterator);
07553 } while (0)
07554 );
07555
07556 if (havepattern)
07557 regfree(®exbuf);
07558
07559 return RESULT_SUCCESS;
07560 #undef FORMAT
07561 }
07562
07563 static char mandescr_show_peers[] =
07564 "Description: Lists SIP peers in text format with details on current status.\n"
07565 "Variables: \n"
07566 " ActionID: <id> Action ID for this transaction. Will be returned.\n";
07567
07568 static int _sip_show_peers(int fd, int *total, struct mansession *s, struct message *m, int argc, char *argv[]);
07569
07570
07571
07572 static int manager_sip_show_peers( struct mansession *s, struct message *m )
07573 {
07574 char *id = astman_get_header(m,"ActionID");
07575 char *a[] = { "sip", "show", "peers" };
07576 char idtext[256] = "";
07577 int total = 0;
07578
07579 if (!ast_strlen_zero(id))
07580 snprintf(idtext,256,"ActionID: %s\r\n",id);
07581
07582 astman_send_ack(s, m, "Peer status list will follow");
07583
07584 _sip_show_peers(s->fd, &total, s, m, 3, a);
07585
07586 ast_cli(s->fd,
07587 "Event: PeerlistComplete\r\n"
07588 "ListItems: %d\r\n"
07589 "%s"
07590 "\r\n", total, idtext);
07591 return 0;
07592 }
07593
07594
07595 static int sip_show_peers(int fd, int argc, char *argv[])
07596 {
07597 return _sip_show_peers(fd, NULL, NULL, NULL, argc, argv);
07598 }
07599
07600
07601 static int _sip_show_peers(int fd, int *total, struct mansession *s, struct message *m, int argc, char *argv[])
07602 {
07603 regex_t regexbuf;
07604 int havepattern = 0;
07605
07606 #define FORMAT2 "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8s %-10s\n"
07607 #define FORMAT "%-25.25s %-15.15s %-3.3s %-3.3s %-3.3s %-8d %-10s\n"
07608
07609 char name[256];
07610 char iabuf[INET_ADDRSTRLEN];
07611 int total_peers = 0;
07612 int peers_online = 0;
07613 int peers_offline = 0;
07614 char *id;
07615 char idtext[256] = "";
07616
07617 if (s) {
07618 id = astman_get_header(m,"ActionID");
07619 if (!ast_strlen_zero(id))
07620 snprintf(idtext,256,"ActionID: %s\r\n",id);
07621 }
07622
07623 switch (argc) {
07624 case 5:
07625 if (!strcasecmp(argv[3], "like")) {
07626 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
07627 return RESULT_SHOWUSAGE;
07628 havepattern = 1;
07629 } else
07630 return RESULT_SHOWUSAGE;
07631 case 3:
07632 break;
07633 default:
07634 return RESULT_SHOWUSAGE;
07635 }
07636
07637 if (!s) {
07638 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Nat", "ACL", "Port", "Status");
07639 }
07640
07641 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
07642 char status[20] = "";
07643 char srch[2000];
07644 char pstatus;
07645
07646 ASTOBJ_RDLOCK(iterator);
07647
07648 if (havepattern && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
07649 ASTOBJ_UNLOCK(iterator);
07650 continue;
07651 }
07652
07653 if (!ast_strlen_zero(iterator->username) && !s)
07654 snprintf(name, sizeof(name), "%s/%s", iterator->name, iterator->username);
07655 else
07656 ast_copy_string(name, iterator->name, sizeof(name));
07657
07658 pstatus = peer_status(iterator, status, sizeof(status));
07659 if (pstatus)
07660 peers_online++;
07661 else {
07662 if (pstatus == 0)
07663 peers_offline++;
07664 else {
07665
07666 if ( ntohs(iterator->addr.sin_port) == 0 ) {
07667 peers_offline++;
07668 } else {
07669 peers_online++;
07670 }
07671 }
07672 }
07673
07674 snprintf(srch, sizeof(srch), FORMAT, name,
07675 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), iterator->addr.sin_addr) : "(Unspecified)",
07676 ast_test_flag(&iterator->flags_page2, SIP_PAGE2_DYNAMIC) ? " D " : " ",
07677 (ast_test_flag(iterator, SIP_NAT) & SIP_NAT_ROUTE) ? " N " : " ",
07678 iterator->ha ? " A " : " ",
07679 ntohs(iterator->addr.sin_port), status);
07680
07681 if (!s) {
07682 ast_cli(fd, FORMAT, name,
07683 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), iterator->addr.sin_addr) : "(Unspecified)",
07684 ast_test_flag(&iterator->flags_page2, SIP_PAGE2_DYNAMIC) ? " D " : " ",
07685 (ast_test_flag(iterator, SIP_NAT) & SIP_NAT_ROUTE) ? " N " : " ",
07686 iterator->ha ? " A " : " ",
07687
07688 ntohs(iterator->addr.sin_port), status);
07689 } else {
07690
07691 ast_cli(fd,
07692 "Event: PeerEntry\r\n%s"
07693 "Channeltype: SIP\r\n"
07694 "ObjectName: %s\r\n"
07695 "ChanObjectType: peer\r\n"
07696 "IPaddress: %s\r\n"
07697 "IPport: %d\r\n"
07698 "Dynamic: %s\r\n"
07699 "Natsupport: %s\r\n"
07700 "ACL: %s\r\n"
07701 "Status: %s\r\n\r\n",
07702 idtext,
07703 iterator->name,
07704 iterator->addr.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), iterator->addr.sin_addr) : "-none-",
07705 ntohs(iterator->addr.sin_port),
07706 ast_test_flag(&iterator->flags_page2, SIP_PAGE2_DYNAMIC) ? "yes" : "no",
07707 (ast_test_flag(iterator, SIP_NAT) & SIP_NAT_ROUTE) ? "yes" : "no",
07708 iterator->ha ? "yes" : "no",
07709 status);
07710 }
07711
07712 ASTOBJ_UNLOCK(iterator);
07713
07714 total_peers++;
07715 } while(0) );
07716
07717 if (!s) {
07718 ast_cli(fd,"%d sip peers [%d online , %d offline]\n",total_peers,peers_online,peers_offline);
07719 }
07720
07721 if (havepattern)
07722 regfree(®exbuf);
07723
07724 if (total)
07725 *total = total_peers;
07726
07727
07728 return RESULT_SUCCESS;
07729 #undef FORMAT
07730 #undef FORMAT2
07731 }
07732
07733
07734 static int sip_show_objects(int fd, int argc, char *argv[])
07735 {
07736 char tmp[256];
07737 if (argc != 3)
07738 return RESULT_SHOWUSAGE;
07739 ast_cli(fd, "-= User objects: %d static, %d realtime =-\n\n", suserobjs, ruserobjs);
07740 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &userl);
07741 ast_cli(fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
07742 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), &peerl);
07743 ast_cli(fd, "-= Registry objects: %d =-\n\n", regobjs);
07744 ASTOBJ_CONTAINER_DUMP(fd, tmp, sizeof(tmp), ®l);
07745 return RESULT_SUCCESS;
07746 }
07747
07748 static void print_group(int fd, unsigned int group, int crlf)
07749 {
07750 char buf[256];
07751 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
07752 }
07753
07754
07755 static const char *dtmfmode2str(int mode)
07756 {
07757 switch (mode) {
07758 case SIP_DTMF_RFC2833:
07759 return "rfc2833";
07760 case SIP_DTMF_INFO:
07761 return "info";
07762 case SIP_DTMF_INBAND:
07763 return "inband";
07764 case SIP_DTMF_AUTO:
07765 return "auto";
07766 }
07767 return "<error>";
07768 }
07769
07770
07771 static const char *insecure2str(int port, int invite)
07772 {
07773 if (port && invite)
07774 return "port,invite";
07775 else if (port)
07776 return "port";
07777 else if (invite)
07778 return "invite";
07779 else
07780 return "no";
07781 }
07782
07783
07784 static int sip_prune_realtime(int fd, int argc, char *argv[])
07785 {
07786 struct sip_peer *peer;
07787 struct sip_user *user;
07788 int pruneuser = 0;
07789 int prunepeer = 0;
07790 int multi = 0;
07791 char *name = NULL;
07792 regex_t regexbuf;
07793
07794 switch (argc) {
07795 case 4:
07796 if (!strcasecmp(argv[3], "user"))
07797 return RESULT_SHOWUSAGE;
07798 if (!strcasecmp(argv[3], "peer"))
07799 return RESULT_SHOWUSAGE;
07800 if (!strcasecmp(argv[3], "like"))
07801 return RESULT_SHOWUSAGE;
07802 if (!strcasecmp(argv[3], "all")) {
07803 multi = 1;
07804 pruneuser = prunepeer = 1;
07805 } else {
07806 pruneuser = prunepeer = 1;
07807 name = argv[3];
07808 }
07809 break;
07810 case 5:
07811 if (!strcasecmp(argv[4], "like"))
07812 return RESULT_SHOWUSAGE;
07813 if (!strcasecmp(argv[3], "all"))
07814 return RESULT_SHOWUSAGE;
07815 if (!strcasecmp(argv[3], "like")) {
07816 multi = 1;
07817 name = argv[4];
07818 pruneuser = prunepeer = 1;
07819 } else if (!strcasecmp(argv[3], "user")) {
07820 pruneuser = 1;
07821 if (!strcasecmp(argv[4], "all"))
07822 multi = 1;
07823 else
07824 name = argv[4];
07825 } else if (!strcasecmp(argv[3], "peer")) {
07826 prunepeer = 1;
07827 if (!strcasecmp(argv[4], "all"))
07828 multi = 1;
07829 else
07830 name = argv[4];
07831 } else
07832 return RESULT_SHOWUSAGE;
07833 break;
07834 case 6:
07835 if (strcasecmp(argv[4], "like"))
07836 return RESULT_SHOWUSAGE;
07837 if (!strcasecmp(argv[3], "user")) {
07838 pruneuser = 1;
07839 name = argv[5];
07840 } else if (!strcasecmp(argv[3], "peer")) {
07841 prunepeer = 1;
07842 name = argv[5];
07843 } else
07844 return RESULT_SHOWUSAGE;
07845 break;
07846 default:
07847 return RESULT_SHOWUSAGE;
07848 }
07849
07850 if (multi && name) {
07851 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
07852 return RESULT_SHOWUSAGE;
07853 }
07854
07855 if (multi) {
07856 if (prunepeer) {
07857 int pruned = 0;
07858
07859 ASTOBJ_CONTAINER_WRLOCK(&peerl);
07860 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
07861 ASTOBJ_RDLOCK(iterator);
07862 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
07863 ASTOBJ_UNLOCK(iterator);
07864 continue;
07865 };
07866 if (ast_test_flag((&iterator->flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
07867 ASTOBJ_MARK(iterator);
07868 pruned++;
07869 }
07870 ASTOBJ_UNLOCK(iterator);
07871 } while (0) );
07872 if (pruned) {
07873 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
07874 ast_cli(fd, "%d peers pruned.\n", pruned);
07875 } else
07876 ast_cli(fd, "No peers found to prune.\n");
07877 ASTOBJ_CONTAINER_UNLOCK(&peerl);
07878 }
07879 if (pruneuser) {
07880 int pruned = 0;
07881
07882 ASTOBJ_CONTAINER_WRLOCK(&userl);
07883 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
07884 ASTOBJ_RDLOCK(iterator);
07885 if (name && regexec(®exbuf, iterator->name, 0, NULL, 0)) {
07886 ASTOBJ_UNLOCK(iterator);
07887 continue;
07888 };
07889 if (ast_test_flag((&iterator->flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
07890 ASTOBJ_MARK(iterator);
07891 pruned++;
07892 }
07893 ASTOBJ_UNLOCK(iterator);
07894 } while (0) );
07895 if (pruned) {
07896 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
07897 ast_cli(fd, "%d users pruned.\n", pruned);
07898 } else
07899 ast_cli(fd, "No users found to prune.\n");
07900 ASTOBJ_CONTAINER_UNLOCK(&userl);
07901 }
07902 } else {
07903 if (prunepeer) {
07904 if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
07905 if (!ast_test_flag((&peer->flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
07906 ast_cli(fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
07907 ASTOBJ_CONTAINER_LINK(&peerl, peer);
07908 } else
07909 ast_cli(fd, "Peer '%s' pruned.\n", name);
07910 ASTOBJ_UNREF(peer, sip_destroy_peer);
07911 } else
07912 ast_cli(fd, "Peer '%s' not found.\n", name);
07913 }
07914 if (pruneuser) {
07915 if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
07916 if (!ast_test_flag((&user->flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
07917 ast_cli(fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
07918 ASTOBJ_CONTAINER_LINK(&userl, user);
07919 } else
07920 ast_cli(fd, "User '%s' pruned.\n", name);
07921 ASTOBJ_UNREF(user, sip_destroy_user);
07922 } else
07923 ast_cli(fd, "User '%s' not found.\n", name);
07924 }
07925 }
07926
07927 return RESULT_SUCCESS;
07928 }
07929
07930
07931 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
07932 {
07933 int x, codec;
07934
07935 for(x = 0; x < 32 ; x++) {
07936 codec = ast_codec_pref_index(pref, x);
07937 if (!codec)
07938 break;
07939 ast_cli(fd, "%s", ast_getformatname(codec));
07940 if (x < 31 && ast_codec_pref_index(pref, x + 1))
07941 ast_cli(fd, ",");
07942 }
07943 if (!x)
07944 ast_cli(fd, "none");
07945 }
07946
07947 static const char *domain_mode_to_text(const enum domain_mode mode)
07948 {
07949 switch (mode) {
07950 case SIP_DOMAIN_AUTO:
07951 return "[Automatic]";
07952 case SIP_DOMAIN_CONFIG:
07953 return "[Configured]";
07954 }
07955
07956 return "";
07957 }
07958
07959
07960 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
07961 static int sip_show_domains(int fd, int argc, char *argv[])
07962 {
07963 struct domain *d;
07964
07965 if (AST_LIST_EMPTY(&domain_list)) {
07966 ast_cli(fd, "SIP Domain support not enabled.\n\n");
07967 return RESULT_SUCCESS;
07968 } else {
07969 ast_cli(fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
07970 AST_LIST_LOCK(&domain_list);
07971 AST_LIST_TRAVERSE(&domain_list, d, list)
07972 ast_cli(fd, FORMAT, d->domain, ast_strlen_zero(d->context) ? "(default)": d->context,
07973 domain_mode_to_text(d->mode));
07974 AST_LIST_UNLOCK(&domain_list);
07975 ast_cli(fd, "\n");
07976 return RESULT_SUCCESS;
07977 }
07978 }
07979 #undef FORMAT
07980
07981 static char mandescr_show_peer[] =
07982 "Description: Show one SIP peer with details on current status.\n"
07983 " The XML format is under development, feedback welcome! /oej\n"
07984 "Variables: \n"
07985 " Peer: <name> The peer name you want to check.\n"
07986 " ActionID: <id> Optional action ID for this AMI transaction.\n";
07987
07988 static int _sip_show_peer(int type, int fd, struct mansession *s, struct message *m, int argc, char *argv[]);
07989
07990
07991 static int manager_sip_show_peer( struct mansession *s, struct message *m )
07992 {
07993 char *id = astman_get_header(m,"ActionID");
07994 char *a[4];
07995 char *peer;
07996 int ret;
07997
07998 peer = astman_get_header(m,"Peer");
07999 if (ast_strlen_zero(peer)) {
08000 astman_send_error(s, m, "Peer: <name> missing.\n");
08001 return 0;
08002 }
08003 a[0] = "sip";
08004 a[1] = "show";
08005 a[2] = "peer";
08006 a[3] = peer;
08007
08008 if (!ast_strlen_zero(id))
08009 ast_cli(s->fd, "ActionID: %s\r\n",id);
08010 ret = _sip_show_peer(1, s->fd, s, m, 4, a );
08011 ast_cli( s->fd, "\r\n\r\n" );
08012 return ret;
08013 }
08014
08015
08016
08017
08018 static int sip_show_peer(int fd, int argc, char *argv[])
08019 {
08020 return _sip_show_peer(0, fd, NULL, NULL, argc, argv);
08021 }
08022
08023 static int _sip_show_peer(int type, int fd, struct mansession *s, struct message *m, int argc, char *argv[])
08024 {
08025 char status[30] = "";
08026 char cbuf[256];
08027 char iabuf[INET_ADDRSTRLEN];
08028 struct sip_peer *peer;
08029 char codec_buf[512];
08030 struct ast_codec_pref *pref;
08031 struct ast_variable *v;
08032 struct sip_auth *auth;
08033 int x = 0, codec = 0, load_realtime = 0;
08034
08035 if (argc < 4)
08036 return RESULT_SHOWUSAGE;
08037
08038 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? 1 : 0;
08039 peer = find_peer(argv[3], NULL, load_realtime);
08040 if (s) {
08041 if (peer)
08042 ast_cli(s->fd, "Response: Success\r\n");
08043 else {
08044 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.\n", argv[3]);
08045 astman_send_error(s, m, cbuf);
08046 return 0;
08047 }
08048 }
08049 if (peer && type==0 ) {
08050 ast_cli(fd,"\n\n");
08051 ast_cli(fd, " * Name : %s\n", peer->name);
08052 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
08053 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
08054 auth = peer->auth;
08055 while(auth) {
08056 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
08057 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
08058 auth = auth->next;
08059 }
08060 ast_cli(fd, " Context : %s\n", peer->context);
08061 ast_cli(fd, " Subscr.Cont. : %s\n", ast_strlen_zero(peer->subscribecontext)?"<Not set>":peer->subscribecontext);
08062 ast_cli(fd, " Language : %s\n", peer->language);
08063 if (!ast_strlen_zero(peer->accountcode))
08064 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
08065 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
08066 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
08067 if (!ast_strlen_zero(peer->fromuser))
08068 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
08069 if (!ast_strlen_zero(peer->fromdomain))
08070 ast_cli(fd, " FromDomain : %s\n", peer->fromdomain);
08071 ast_cli(fd, " Callgroup : ");
08072 print_group(fd, peer->callgroup, 0);
08073 ast_cli(fd, " Pickupgroup : ");
08074 print_group(fd, peer->pickupgroup, 0);
08075 ast_cli(fd, " Mailbox : %s\n", peer->mailbox);
08076 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
08077 ast_cli(fd, " LastMsgsSent : %d\n", peer->lastmsgssent);
08078 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
08079 ast_cli(fd, " Dynamic : %s\n", (ast_test_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC)?"Yes":"No"));
08080 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
08081 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
08082 ast_cli(fd, " Insecure : %s\n", insecure2str(ast_test_flag(peer, SIP_INSECURE_PORT), ast_test_flag(peer, SIP_INSECURE_INVITE)));
08083 ast_cli(fd, " Nat : %s\n", nat2str(ast_test_flag(peer, SIP_NAT)));
08084 ast_cli(fd, " ACL : %s\n", (peer->ha?"Yes":"No"));
08085 ast_cli(fd, " CanReinvite : %s\n", (ast_test_flag(peer, SIP_CAN_REINVITE)?"Yes":"No"));
08086 ast_cli(fd, " PromiscRedir : %s\n", (ast_test_flag(peer, SIP_PROMISCREDIR)?"Yes":"No"));
08087 ast_cli(fd, " User=Phone : %s\n", (ast_test_flag(peer, SIP_USEREQPHONE)?"Yes":"No"));
08088 ast_cli(fd, " Trust RPID : %s\n", (ast_test_flag(peer, SIP_TRUSTRPID) ? "Yes" : "No"));
08089 ast_cli(fd, " Send RPID : %s\n", (ast_test_flag(peer, SIP_SENDRPID) ? "Yes" : "No"));
08090
08091
08092 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(peer, SIP_DTMF)));
08093 ast_cli(fd, " LastMsg : %d\n", peer->lastmsg);
08094 ast_cli(fd, " ToHost : %s\n", peer->tohost);
08095 ast_cli(fd, " Addr->IP : %s Port %d\n", peer->addr.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), peer->addr.sin_addr) : "(Unspecified)", ntohs(peer->addr.sin_port));
08096 ast_cli(fd, " Defaddr->IP : %s Port %d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
08097 ast_cli(fd, " Def. Username: %s\n", peer->username);
08098 ast_cli(fd, " SIP Options : ");
08099 if (peer->sipoptions) {
08100 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
08101 if (peer->sipoptions & sip_options[x].id)
08102 ast_cli(fd, "%s ", sip_options[x].text);
08103 }
08104 } else
08105 ast_cli(fd, "(none)");
08106
08107 ast_cli(fd, "\n");
08108 ast_cli(fd, " Codecs : ");
08109 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
08110 ast_cli(fd, "%s\n", codec_buf);
08111 ast_cli(fd, " Codec Order : (");
08112 print_codec_to_cli(fd, &peer->prefs);
08113
08114 ast_cli(fd, ")\n");
08115
08116 ast_cli(fd, " Status : ");
08117 peer_status(peer, status, sizeof(status));
08118 ast_cli(fd, "%s\n",status);
08119 ast_cli(fd, " Useragent : %s\n", peer->useragent);
08120 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
08121 if (peer->chanvars) {
08122 ast_cli(fd, " Variables :\n");
08123 for (v = peer->chanvars ; v ; v = v->next)
08124 ast_cli(fd, " %s = %s\n", v->name, v->value);
08125 }
08126 ast_cli(fd,"\n");
08127 ASTOBJ_UNREF(peer,sip_destroy_peer);
08128 } else if (peer && type == 1) {
08129 ast_cli(fd, "Channeltype: SIP\r\n");
08130 ast_cli(fd, "ObjectName: %s\r\n", peer->name);
08131 ast_cli(fd, "ChanObjectType: peer\r\n");
08132 ast_cli(fd, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
08133 ast_cli(fd, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
08134 ast_cli(fd, "Context: %s\r\n", peer->context);
08135 ast_cli(fd, "Language: %s\r\n", peer->language);
08136 if (!ast_strlen_zero(peer->accountcode))
08137 ast_cli(fd, "Accountcode: %s\r\n", peer->accountcode);
08138 ast_cli(fd, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
08139 ast_cli(fd, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
08140 if (!ast_strlen_zero(peer->fromuser))
08141 ast_cli(fd, "SIP-FromUser: %s\r\n", peer->fromuser);
08142 if (!ast_strlen_zero(peer->fromdomain))
08143 ast_cli(fd, "SIP-FromDomain: %s\r\n", peer->fromdomain);
08144 ast_cli(fd, "Callgroup: ");
08145 print_group(fd, peer->callgroup, 1);
08146 ast_cli(fd, "Pickupgroup: ");
08147 print_group(fd, peer->pickupgroup, 1);
08148 ast_cli(fd, "VoiceMailbox: %s\r\n", peer->mailbox);
08149 ast_cli(fd, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
08150 ast_cli(fd, "Call limit: %d\r\n", peer->call_limit);
08151 ast_cli(fd, "Dynamic: %s\r\n", (ast_test_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC)?"Y":"N"));
08152 ast_cli(fd, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
08153 ast_cli(fd, "RegExpire: %ld seconds\r\n", ast_sched_when(sched,peer->expire));
08154 ast_cli(fd, "SIP-AuthInsecure: %s\r\n", insecure2str(ast_test_flag(peer, SIP_INSECURE_PORT), ast_test_flag(peer, SIP_INSECURE_INVITE)));
08155 ast_cli(fd, "SIP-NatSupport: %s\r\n", nat2str(ast_test_flag(peer, SIP_NAT)));
08156 ast_cli(fd, "ACL: %s\r\n", (peer->ha?"Y":"N"));
08157 ast_cli(fd, "SIP-CanReinvite: %s\r\n", (ast_test_flag(peer, SIP_CAN_REINVITE)?"Y":"N"));
08158 ast_cli(fd, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(peer, SIP_PROMISCREDIR)?"Y":"N"));
08159 ast_cli(fd, "SIP-UserPhone: %s\r\n", (ast_test_flag(peer, SIP_USEREQPHONE)?"Y":"N"));
08160
08161
08162 ast_cli(fd, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(peer, SIP_DTMF)));
08163 ast_cli(fd, "SIPLastMsg: %d\r\n", peer->lastmsg);
08164 ast_cli(fd, "ToHost: %s\r\n", peer->tohost);
08165 ast_cli(fd, "Address-IP: %s\r\nAddress-Port: %d\r\n", peer->addr.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), peer->addr.sin_addr) : "", ntohs(peer->addr.sin_port));
08166 ast_cli(fd, "Default-addr-IP: %s\r\nDefault-addr-port: %d\r\n", ast_inet_ntoa(iabuf, sizeof(iabuf), peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
08167 ast_cli(fd, "Default-Username: %s\r\n", peer->username);
08168 ast_cli(fd, "Codecs: ");
08169 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
08170 ast_cli(fd, "%s\r\n", codec_buf);
08171 ast_cli(fd, "CodecOrder: ");
08172 pref = &peer->prefs;
08173 for(x = 0; x < 32 ; x++) {
08174 codec = ast_codec_pref_index(pref,x);
08175 if (!codec)
08176 break;
08177 ast_cli(fd, "%s", ast_getformatname(codec));
08178 if (x < 31 && ast_codec_pref_index(pref,x+1))
08179 ast_cli(fd, ",");
08180 }
08181
08182 ast_cli(fd, "\r\n");
08183 ast_cli(fd, "Status: ");
08184 peer_status(peer, status, sizeof(status));
08185 ast_cli(fd, "%s\r\n", status);
08186 ast_cli(fd, "SIP-Useragent: %s\r\n", peer->useragent);
08187 ast_cli(fd, "Reg-Contact : %s\r\n", peer->fullcontact);
08188 if (peer->chanvars) {
08189 for (v = peer->chanvars ; v ; v = v->next) {
08190 ast_cli(fd, "ChanVariable:\n");
08191 ast_cli(fd, " %s,%s\r\n", v->name, v->value);
08192 }
08193 }
08194
08195 ASTOBJ_UNREF(peer,sip_destroy_peer);
08196
08197 } else {
08198 ast_cli(fd,"Peer %s not found.\n", argv[3]);
08199 ast_cli(fd,"\n");
08200 }
08201
08202 return RESULT_SUCCESS;
08203 }
08204
08205
08206 static int sip_show_user(int fd, int argc, char *argv[])
08207 {
08208 char cbuf[256];
08209 struct sip_user *user;
08210 struct ast_codec_pref *pref;
08211 struct ast_variable *v;
08212 int x = 0, codec = 0, load_realtime = 0;
08213
08214 if (argc < 4)
08215 return RESULT_SHOWUSAGE;
08216
08217
08218 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? 1 : 0;
08219
08220 user = find_user(argv[3], load_realtime);
08221 if (user) {
08222 ast_cli(fd,"\n\n");
08223 ast_cli(fd, " * Name : %s\n", user->name);
08224 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
08225 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
08226 ast_cli(fd, " Context : %s\n", user->context);
08227 ast_cli(fd, " Language : %s\n", user->language);
08228 if (!ast_strlen_zero(user->accountcode))
08229 ast_cli(fd, " Accountcode : %s\n", user->accountcode);
08230 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
08231 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
08232 ast_cli(fd, " Call limit : %d\n", user->call_limit);
08233 ast_cli(fd, " Callgroup : ");
08234 print_group(fd, user->callgroup, 0);
08235 ast_cli(fd, " Pickupgroup : ");
08236 print_group(fd, user->pickupgroup, 0);
08237 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
08238 ast_cli(fd, " ACL : %s\n", (user->ha?"Yes":"No"));
08239 ast_cli(fd, " Codec Order : (");
08240 pref = &user->prefs;
08241 for(x = 0; x < 32 ; x++) {
08242 codec = ast_codec_pref_index(pref,x);
08243 if (!codec)
08244 break;
08245 ast_cli(fd, "%s", ast_getformatname(codec));
08246 if (x < 31 && ast_codec_pref_index(pref,x+1))
08247 ast_cli(fd, "|");
08248 }
08249
08250 if (!x)
08251 ast_cli(fd, "none");
08252 ast_cli(fd, ")\n");
08253
08254 if (user->chanvars) {
08255 ast_cli(fd, " Variables :\n");
08256 for (v = user->chanvars ; v ; v = v->next)
08257 ast_cli(fd, " %s = %s\n", v->name, v->value);
08258 }
08259 ast_cli(fd,"\n");
08260 ASTOBJ_UNREF(user,sip_destroy_user);
08261 } else {
08262 ast_cli(fd,"User %s not found.\n", argv[3]);
08263 ast_cli(fd,"\n");
08264 }
08265
08266 return RESULT_SUCCESS;
08267 }
08268
08269
08270 static int sip_show_registry(int fd, int argc, char *argv[])
08271 {
08272 #define FORMAT2 "%-30.30s %-12.12s %8.8s %-20.20s\n"
08273 #define FORMAT "%-30.30s %-12.12s %8d %-20.20s\n"
08274 char host[80];
08275
08276 if (argc != 3)
08277 return RESULT_SHOWUSAGE;
08278 ast_cli(fd, FORMAT2, "Host", "Username", "Refresh", "State");
08279 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
08280 ASTOBJ_RDLOCK(iterator);
08281 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : DEFAULT_SIP_PORT);
08282 ast_cli(fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate));
08283 ASTOBJ_UNLOCK(iterator);
08284 } while(0));
08285 return RESULT_SUCCESS;
08286 #undef FORMAT
08287 #undef FORMAT2
08288 }
08289
08290
08291 static int sip_show_settings(int fd, int argc, char *argv[])
08292 {
08293 char tmp[BUFSIZ];
08294 int realtimepeers = 0;
08295 int realtimeusers = 0;
08296
08297 realtimepeers = ast_check_realtime("sippeers");
08298 realtimeusers = ast_check_realtime("sipusers");
08299
08300 if (argc != 3)
08301 return RESULT_SHOWUSAGE;
08302 ast_cli(fd, "\n\nGlobal Settings:\n");
08303 ast_cli(fd, "----------------\n");
08304 ast_cli(fd, " SIP Port: %d\n", ntohs(bindaddr.sin_port));
08305 ast_cli(fd, " Bindaddress: %s\n", ast_inet_ntoa(tmp, sizeof(tmp), bindaddr.sin_addr));
08306 ast_cli(fd, " Videosupport: %s\n", videosupport ? "Yes" : "No");
08307 ast_cli(fd, " AutoCreatePeer: %s\n", autocreatepeer ? "Yes" : "No");
08308 ast_cli(fd, " Allow unknown access: %s\n", global_allowguest ? "Yes" : "No");
08309 ast_cli(fd, " Promsic. redir: %s\n", ast_test_flag(&global_flags, SIP_PROMISCREDIR) ? "Yes" : "No");
08310 ast_cli(fd, " SIP domain support: %s\n", AST_LIST_EMPTY(&domain_list) ? "No" : "Yes");
08311 ast_cli(fd, " Call to non-local dom.: %s\n", allow_external_domains ? "Yes" : "No");
08312 ast_cli(fd, " URI user is phone no: %s\n", ast_test_flag(&global_flags, SIP_USEREQPHONE) ? "Yes" : "No");
08313 ast_cli(fd, " Our auth realm %s\n", global_realm);
08314 ast_cli(fd, " Realm. auth: %s\n", authl ? "Yes": "No");
08315 ast_cli(fd, " Always auth rejects: %s\n", global_alwaysauthreject ? "Yes" : "No");
08316 ast_cli(fd, " User Agent: %s\n", default_useragent);
08317 ast_cli(fd, " MWI checking interval: %d secs\n", global_mwitime);
08318 ast_cli(fd, " Reg. context: %s\n", ast_strlen_zero(regcontext) ? "(not set)" : regcontext);
08319 ast_cli(fd, " Caller ID: %s\n", default_callerid);
08320 ast_cli(fd, " From: Domain: %s\n", default_fromdomain);
08321 ast_cli(fd, " Record SIP history: %s\n", recordhistory ? "On" : "Off");
08322 ast_cli(fd, " Call Events: %s\n", callevents ? "On" : "Off");
08323 ast_cli(fd, " IP ToS: 0x%x\n", tos);
08324 #ifdef OSP_SUPPORT
08325 ast_cli(fd, " OSP Support: Yes\n");
08326 #else
08327 ast_cli(fd, " OSP Support: No\n");
08328 #endif
08329 if (!realtimepeers && !realtimeusers)
08330 ast_cli(fd, " SIP realtime: Disabled\n" );
08331 else
08332 ast_cli(fd, " SIP realtime: Enabled\n" );
08333
08334 ast_cli(fd, "\nGlobal Signalling Settings:\n");
08335 ast_cli(fd, "---------------------------\n");
08336 ast_cli(fd, " Codecs: ");
08337 print_codec_to_cli(fd, &prefs);
08338 ast_cli(fd, "\n");
08339 ast_cli(fd, " Relax DTMF: %s\n", relaxdtmf ? "Yes" : "No");
08340 ast_cli(fd, " Compact SIP headers: %s\n", compactheaders ? "Yes" : "No");
08341 ast_cli(fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
08342 ast_cli(fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
08343 ast_cli(fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
08344 ast_cli(fd, " DNS SRV lookup: %s\n", srvlookup ? "Yes" : "No");
08345 ast_cli(fd, " Pedantic SIP support: %s\n", pedanticsipchecking ? "Yes" : "No");
08346 ast_cli(fd, " Reg. max duration: %d secs\n", max_expiry);
08347 ast_cli(fd, " Reg. default duration: %d secs\n", default_expiry);
08348 ast_cli(fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
08349 ast_cli(fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
08350 ast_cli(fd, " Notify ringing state: %s\n", global_notifyringing ? "Yes" : "No");
08351 ast_cli(fd, "\nDefault Settings:\n");
08352 ast_cli(fd, "-----------------\n");
08353 ast_cli(fd, " Context: %s\n", default_context);
08354 ast_cli(fd, " Nat: %s\n", nat2str(ast_test_flag(&global_flags, SIP_NAT)));
08355 ast_cli(fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags, SIP_DTMF)));
08356 ast_cli(fd, " Qualify: %d\n", default_qualify);
08357 ast_cli(fd, " Use ClientCode: %s\n", ast_test_flag(&global_flags, SIP_USECLIENTCODE) ? "Yes" : "No");
08358 ast_cli(fd, " Progress inband: %s\n", (ast_test_flag(&global_flags, SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER) ? "Never" : (ast_test_flag(&global_flags, SIP_PROG_INBAND) == SIP_PROG_INBAND_NO) ? "No" : "Yes" );
08359 ast_cli(fd, " Language: %s\n", ast_strlen_zero(default_language) ? "(Defaults to English)" : default_language);
08360 ast_cli(fd, " Musicclass: %s\n", global_musicclass);
08361 ast_cli(fd, " Voice Mail Extension: %s\n", global_vmexten);
08362
08363
08364 if (realtimepeers || realtimeusers) {
08365 ast_cli(fd, "\nRealtime SIP Settings:\n");
08366 ast_cli(fd, "----------------------\n");
08367 ast_cli(fd, " Realtime Peers: %s\n", realtimepeers ? "Yes" : "No");
08368 ast_cli(fd, " Realtime Users: %s\n", realtimeusers ? "Yes" : "No");
08369 ast_cli(fd, " Cache Friends: %s\n", ast_test_flag(&global_flags_page2, SIP_PAGE2_RTCACHEFRIENDS) ? "Yes" : "No");
08370 ast_cli(fd, " Update: %s\n", ast_test_flag(&global_flags_page2, SIP_PAGE2_RTUPDATE) ? "Yes" : "No");
08371 ast_cli(fd, " Ignore Reg. Expire: %s\n", ast_test_flag(&global_flags_page2, SIP_PAGE2_IGNOREREGEXPIRE) ? "Yes" : "No");
08372 ast_cli(fd, " Auto Clear: %d\n", global_rtautoclear);
08373 }
08374 ast_cli(fd, "\n----\n");
08375 return RESULT_SUCCESS;
08376 }
08377
08378
08379 static const char *subscription_type2str(enum subscriptiontype subtype) {
08380 int i;
08381
08382 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
08383 if (subscription_types[i].type == subtype) {
08384 return subscription_types[i].text;
08385 }
08386 }
08387 return subscription_types[0].text;
08388 }
08389
08390
08391 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype) {
08392 int i;
08393
08394 for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
08395 if (subscription_types[i].type == subtype) {
08396 return &subscription_types[i];
08397 }
08398 }
08399 return &subscription_types[0];
08400 }
08401
08402
08403 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions);
08404
08405
08406 static int sip_show_channels(int fd, int argc, char *argv[])
08407 {
08408 return __sip_show_channels(fd, argc, argv, 0);
08409 }
08410
08411
08412 static int sip_show_subscriptions(int fd, int argc, char *argv[])
08413 {
08414 return __sip_show_channels(fd, argc, argv, 1);
08415 }
08416
08417 static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions)
08418 {
08419 #define FORMAT3 "%-15.15s %-10.10s %-11.11s %-15.15s %-13.13s %-15.15s\n"
08420 #define FORMAT2 "%-15.15s %-10.10s %-11.11s %-11.11s %-4.4s %-7.7s %-15.15s\n"
08421 #define FORMAT "%-15.15s %-10.10s %-11.11s %5.5d/%5.5d %-4.4s %-3.3s %-3.3s %-15.15s\n"
08422 struct sip_pvt *cur;
08423 char iabuf[INET_ADDRSTRLEN];
08424 int numchans = 0;
08425 if (argc != 3)
08426 return RESULT_SHOWUSAGE;
08427 ast_mutex_lock(&iflock);
08428 cur = iflist;
08429 if (!subscriptions)
08430 ast_cli(fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Seq (Tx/Rx)", "Format", "Hold", "Last Message");
08431 else
08432 ast_cli(fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type");
08433 while (cur) {
08434 if (cur->subscribed == NONE && !subscriptions) {
08435 ast_cli(fd, FORMAT, ast_inet_ntoa(iabuf, sizeof(iabuf), cur->sa.sin_addr),
08436 ast_strlen_zero(cur->username) ? ( ast_strlen_zero(cur->cid_num) ? "(None)" : cur->cid_num ) : cur->username,
08437 cur->callid,
08438 cur->ocseq, cur->icseq,
08439 ast_getformatname(cur->owner ? cur->owner->nativeformats : 0),
08440 ast_test_flag(cur, SIP_CALL_ONHOLD) ? "Yes" : "No",
08441 ast_test_flag(cur, SIP_NEEDDESTROY) ? "(d)" : "",
08442 cur->lastmsg );
08443 numchans++;
08444 }
08445 if (cur->subscribed != NONE && subscriptions) {
08446 ast_cli(fd, FORMAT3, ast_inet_ntoa(iabuf, sizeof(iabuf), cur->sa.sin_addr),
08447 ast_strlen_zero(cur->username) ? ( ast_strlen_zero(cur->cid_num) ? "(None)" : cur->cid_num ) : cur->username,
08448 cur->callid, cur->exten, ast_extension_state2str(cur->laststate),
08449 subscription_type2str(cur->subscribed));
08450 numchans++;
08451 }
08452 cur = cur->next;
08453 }
08454 ast_mutex_unlock(&iflock);
08455 if (!subscriptions)
08456 ast_cli(fd, "%d active SIP channel%s\n", numchans, (numchans != 1) ? "s" : "");
08457 else
08458 ast_cli(fd, "%d active SIP subscription%s\n", numchans, (numchans != 1) ? "s" : "");
08459 return RESULT_SUCCESS;
08460 #undef FORMAT
08461 #undef FORMAT2
08462 #undef FORMAT3
08463 }
08464
08465
08466 static char *complete_sipch(char *line, char *word, int pos, int state)
08467 {
08468 int which=0;
08469 struct sip_pvt *cur;
08470 char *c = NULL;
08471
08472 ast_mutex_lock(&iflock);
08473 cur = iflist;
08474 while(cur) {
08475 if (!strncasecmp(word, cur->callid, strlen(word))) {
08476 if (++which > state) {
08477 c = strdup(cur->callid);
08478 break;
08479 }
08480 }
08481 cur = cur->next;
08482 }
08483 ast_mutex_unlock(&iflock);
08484 return c;
08485 }
08486
08487
08488 static char *complete_sip_peer(char *word, int state, int flags2)
08489 {
08490 char *result = NULL;
08491 int wordlen = strlen(word);
08492 int which = 0;
08493
08494 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
08495
08496 if (!strncasecmp(word, iterator->name, wordlen)) {
08497 if (flags2 && !ast_test_flag((&iterator->flags_page2), flags2))
08498 continue;
08499 if (++which > state) {
08500 result = strdup(iterator->name);
08501 }
08502 }
08503 } while(0) );
08504 return result;
08505 }
08506
08507
08508 static char *complete_sip_show_peer(char *line, char *word, int pos, int state)
08509 {
08510 if (pos == 3)
08511 return complete_sip_peer(word, state, 0);
08512
08513 return NULL;
08514 }
08515
08516
08517 static char *complete_sip_debug_peer(char *line, char *word, int pos, int state)
08518 {
08519 if (pos == 3)
08520 return complete_sip_peer(word, state, 0);
08521
08522 return NULL;
08523 }
08524
08525
08526 static char *complete_sip_user(char *word, int state, int flags2)
08527 {
08528 char *result = NULL;
08529 int wordlen = strlen(word);
08530 int which = 0;
08531
08532 ASTOBJ_CONTAINER_TRAVERSE(&userl, !result, do {
08533
08534 if (!strncasecmp(word, iterator->name, wordlen)) {
08535 if (flags2 && !ast_test_flag(&(iterator->flags_page2), flags2))
08536 continue;
08537 if (++which > state) {
08538 result = strdup(iterator->name);
08539 }
08540 }
08541 } while(0) );
08542 return result;
08543 }
08544
08545
08546 static char *complete_sip_show_user(char *line, char *word, int pos, int state)
08547 {
08548 if (pos == 3)
08549 return complete_sip_user(word, state, 0);
08550
08551 return NULL;
08552 }
08553
08554
08555 static char *complete_sipnotify(char *line, char *word, int pos, int state)
08556 {
08557 char *c = NULL;
08558
08559 if (pos == 2) {
08560 int which = 0;
08561 char *cat;
08562
08563
08564
08565 if (!notify_types)
08566 return NULL;
08567
08568 cat = ast_category_browse(notify_types, NULL);
08569 while(cat) {
08570 if (!strncasecmp(word, cat, strlen(word))) {
08571 if (++which > state) {
08572 c = strdup(cat);
08573 break;
08574 }
08575 }
08576 cat = ast_category_browse(notify_types, cat);
08577 }
08578 return c;
08579 }
08580
08581 if (pos > 2)
08582 return complete_sip_peer(word, state, 0);
08583
08584 return NULL;
08585 }
08586
08587
08588 static char *complete_sip_prune_realtime_peer(char *line, char *word, int pos, int state)
08589 {
08590 if (pos == 4)
08591 return complete_sip_peer(word, state, SIP_PAGE2_RTCACHEFRIENDS);
08592 return NULL;
08593 }
08594
08595
08596 static char *complete_sip_prune_realtime_user(char *line, char *word, int pos, int state)
08597 {
08598 if (pos == 4)
08599 return complete_sip_user(word, state, SIP_PAGE2_RTCACHEFRIENDS);
08600
08601 return NULL;
08602 }
08603
08604
08605 static int sip_show_channel(int fd, int argc, char *argv[])
08606 {
08607 struct sip_pvt *cur;
08608 char iabuf[INET_ADDRSTRLEN];
08609 size_t len;
08610 int found = 0;
08611
08612 if (argc != 4)
08613 return RESULT_SHOWUSAGE;
08614 len = strlen(argv[3]);
08615 ast_mutex_lock(&iflock);
08616 cur = iflist;
08617 while(cur) {
08618 if (!strncasecmp(cur->callid, argv[3],len)) {
08619 ast_cli(fd,"\n");
08620 if (cur->subscribed != NONE)
08621 ast_cli(fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
08622 else
08623 ast_cli(fd, " * SIP Call\n");
08624 ast_cli(fd, " Direction: %s\n", ast_test_flag(cur, SIP_OUTGOING)?"Outgoing":"Incoming");
08625 ast_cli(fd, " Call-ID: %s\n", cur->callid);
08626 ast_cli(fd, " Our Codec Capability: %d\n", cur->capability);
08627 ast_cli(fd, " Non-Codec Capability: %d\n", cur->noncodeccapability);
08628 ast_cli(fd, " Their Codec Capability: %d\n", cur->peercapability);
08629 ast_cli(fd, " Joint Codec Capability: %d\n", cur->jointcapability);
08630 ast_cli(fd, " Format %s\n", ast_getformatname(cur->owner ? cur->owner->nativeformats : 0) );
08631 ast_cli(fd, " Theoretical Address: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), cur->sa.sin_addr), ntohs(cur->sa.sin_port));
08632 ast_cli(fd, " Received Address: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), cur->recv.sin_addr), ntohs(cur->recv.sin_port));
08633 ast_cli(fd, " NAT Support: %s\n", nat2str(ast_test_flag(cur, SIP_NAT)));
08634 ast_cli(fd, " Audio IP: %s %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), cur->redirip.sin_addr.s_addr ? cur->redirip.sin_addr : cur->ourip), cur->redirip.sin_addr.s_addr ? "(Outside bridge)" : "(local)" );
08635 ast_cli(fd, " Our Tag: %s\n", cur->tag);
08636 ast_cli(fd, " Their Tag: %s\n", cur->theirtag);
08637 ast_cli(fd, " SIP User agent: %s\n", cur->useragent);
08638 if (!ast_strlen_zero(cur->username))
08639 ast_cli(fd, " Username: %s\n", cur->username);
08640 if (!ast_strlen_zero(cur->peername))
08641 ast_cli(fd, " Peername: %s\n", cur->peername);
08642 if (!ast_strlen_zero(cur->uri))
08643 ast_cli(fd, " Original uri: %s\n", cur->uri);
08644 if (!ast_strlen_zero(cur->cid_num))
08645 ast_cli(fd, " Caller-ID: %s\n", cur->cid_num);
08646 ast_cli(fd, " Need Destroy: %d\n", ast_test_flag(cur, SIP_NEEDDESTROY));
08647 ast_cli(fd, " Last Message: %s\n", cur->lastmsg);
08648 ast_cli(fd, " Promiscuous Redir: %s\n", ast_test_flag(cur, SIP_PROMISCREDIR) ? "Yes" : "No");
08649 ast_cli(fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
08650 ast_cli(fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(cur, SIP_DTMF)));
08651 ast_cli(fd, " SIP Options: ");
08652 if (cur->sipoptions) {
08653 int x;
08654 for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
08655 if (cur->sipoptions & sip_options[x].id)
08656 ast_cli(fd, "%s ", sip_options[x].text);
08657 }
08658 } else
08659 ast_cli(fd, "(none)\n");
08660 ast_cli(fd, "\n\n");
08661 found++;
08662 }
08663 cur = cur->next;
08664 }
08665 ast_mutex_unlock(&iflock);
08666 if (!found)
08667 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
08668 return RESULT_SUCCESS;
08669 }
08670
08671
08672 static int sip_show_history(int fd, int argc, char *argv[])
08673 {
08674 struct sip_pvt *cur;
08675 struct sip_history *hist;
08676 size_t len;
08677 int x;
08678 int found = 0;
08679
08680 if (argc != 4)
08681 return RESULT_SHOWUSAGE;
08682 if (!recordhistory)
08683 ast_cli(fd, "\n***Note: History recording is currently DISABLED. Use 'sip history' to ENABLE.\n");
08684 len = strlen(argv[3]);
08685 ast_mutex_lock(&iflock);
08686 cur = iflist;
08687 while(cur) {
08688 if (!strncasecmp(cur->callid, argv[3], len)) {
08689 ast_cli(fd,"\n");
08690 if (cur->subscribed != NONE)
08691 ast_cli(fd, " * Subscription\n");
08692 else
08693 ast_cli(fd, " * SIP Call\n");
08694 x = 0;
08695 hist = cur->history;
08696 while(hist) {
08697 x++;
08698 ast_cli(fd, "%d. %s\n", x, hist->event);
08699 hist = hist->next;
08700 }
08701 if (!x)
08702 ast_cli(fd, "Call '%s' has no history\n", cur->callid);
08703 found++;
08704 }
08705 cur = cur->next;
08706 }
08707 ast_mutex_unlock(&iflock);
08708 if (!found)
08709 ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
08710 return RESULT_SUCCESS;
08711 }
08712
08713
08714
08715 void sip_dump_history(struct sip_pvt *dialog)
08716 {
08717 int x;
08718 struct sip_history *hist;
08719
08720 if (!dialog)
08721 return;
08722
08723 ast_log(LOG_DEBUG, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
08724 if (dialog->subscribed)
08725 ast_log(LOG_DEBUG, " * Subscription\n");
08726 else
08727 ast_log(LOG_DEBUG, " * SIP Call\n");
08728 x = 0;
08729 hist = dialog->history;
08730 while(hist) {
08731 x++;
08732 ast_log(LOG_DEBUG, " %d. %s\n", x, hist->event);
08733 hist = hist->next;
08734 }
08735 if (!x)
08736 ast_log(LOG_DEBUG, "Call '%s' has no history\n", dialog->callid);
08737 ast_log(LOG_DEBUG, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
08738
08739 }
08740
08741
08742
08743
08744 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
08745 {
08746 char buf[1024];
08747 unsigned int event;
08748 char *c;
08749
08750
08751 if (!strcasecmp(get_header(req, "Content-Type"), "application/dtmf-relay") ||
08752 !strcasecmp(get_header(req, "Content-Type"), "application/vnd.nortelnetworks.digits")) {
08753
08754
08755 if (ast_strlen_zero(c = get_body(req, "Signal")) && ast_strlen_zero(c = get_body(req, "d"))) {
08756 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
08757 transmit_response(p, "200 OK", req);
08758 return;
08759 } else {
08760 ast_copy_string(buf, c, sizeof(buf));
08761 }
08762
08763 if (!p->owner) {
08764 transmit_response(p, "481 Call leg/transaction does not exist", req);
08765 ast_set_flag(p, SIP_NEEDDESTROY);
08766 return;
08767 }
08768
08769 if (ast_strlen_zero(buf)) {
08770 transmit_response(p, "200 OK", req);
08771 return;
08772 }
08773
08774 if (buf[0] == '*')
08775 event = 10;
08776 else if (buf[0] == '#')
08777 event = 11;
08778 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
08779 event = 12 + buf[0] - 'A';
08780 else
08781 event = atoi(buf);
08782 if (event == 16) {
08783
08784 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH, };
08785 ast_queue_frame(p->owner, &f);
08786 if (sipdebug)
08787 ast_verbose("* DTMF-relay event received: FLASH\n");
08788 } else {
08789
08790 struct ast_frame f = { AST_FRAME_DTMF, };
08791 if (event < 10) {
08792 f.subclass = '0' + event;
08793 } else if (event < 11) {
08794 f.subclass = '*';
08795 } else if (event < 12) {
08796 f.subclass = '#';
08797 } else if (event < 16) {
08798 f.subclass = 'A' + (event - 12);
08799 }
08800 ast_queue_frame(p->owner, &f);
08801 if (sipdebug)
08802 ast_verbose("* DTMF-relay event received: %c\n", f.subclass);
08803 }
08804 transmit_response(p, "200 OK", req);
08805 return;
08806 } else if (!strcasecmp(get_header(req, "Content-Type"), "application/media_control+xml")) {
08807
08808 if (p->owner)
08809 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
08810 transmit_response(p, "200 OK", req);
08811 return;
08812 } else if ((c = get_header(req, "X-ClientCode"))) {
08813
08814 if (ast_test_flag(p, SIP_USECLIENTCODE)) {
08815 if (p->owner && p->owner->cdr)
08816 ast_cdr_setuserfield(p->owner, c);
08817 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
08818 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
08819 transmit_response(p, "200 OK", req);
08820 } else {
08821 transmit_response(p, "403 Unauthorized", req);
08822 }
08823 return;
08824 }
08825
08826
08827
08828 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
08829 transmit_response(p, "415 Unsupported media type", req);
08830 return;
08831 }
08832
08833
08834 static int sip_do_debug_ip(int fd, int argc, char *argv[])
08835 {
08836 struct hostent *hp;
08837 struct ast_hostent ahp;
08838 char iabuf[INET_ADDRSTRLEN];
08839 int port = 0;
08840 char *p, *arg;
08841
08842 if (argc != 4)
08843 return RESULT_SHOWUSAGE;
08844 arg = argv[3];
08845 p = strstr(arg, ":");
08846 if (p) {
08847 *p = '\0';
08848 p++;
08849 port = atoi(p);
08850 }
08851 hp = ast_gethostbyname(arg, &ahp);
08852 if (hp == NULL) {
08853 return RESULT_SHOWUSAGE;
08854 }
08855 debugaddr.sin_family = AF_INET;
08856 memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
08857 debugaddr.sin_port = htons(port);
08858 if (port == 0)
08859 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), debugaddr.sin_addr));
08860 else
08861 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), debugaddr.sin_addr), port);
08862 sipdebug |= SIP_DEBUG_CONSOLE;
08863 return RESULT_SUCCESS;
08864 }
08865
08866
08867 static int sip_do_debug_peer(int fd, int argc, char *argv[])
08868 {
08869 struct sip_peer *peer;
08870 char iabuf[INET_ADDRSTRLEN];
08871 if (argc != 4)
08872 return RESULT_SHOWUSAGE;
08873 peer = find_peer(argv[3], NULL, 1);
08874 if (peer) {
08875 if (peer->addr.sin_addr.s_addr) {
08876 debugaddr.sin_family = AF_INET;
08877 memcpy(&debugaddr.sin_addr, &peer->addr.sin_addr, sizeof(debugaddr.sin_addr));
08878 debugaddr.sin_port = peer->addr.sin_port;
08879 ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), debugaddr.sin_addr), ntohs(debugaddr.sin_port));
08880 sipdebug |= SIP_DEBUG_CONSOLE;
08881 } else
08882 ast_cli(fd, "Unable to get IP address of peer '%s'\n", argv[3]);
08883 ASTOBJ_UNREF(peer,sip_destroy_peer);
08884 } else
08885 ast_cli(fd, "No such peer '%s'\n", argv[3]);
08886 return RESULT_SUCCESS;
08887 }
08888
08889
08890 static int sip_do_debug(int fd, int argc, char *argv[])
08891 {
08892 int oldsipdebug = sipdebug & SIP_DEBUG_CONSOLE;
08893 if (argc != 2) {
08894 if (argc != 4)
08895 return RESULT_SHOWUSAGE;
08896 else if (strncmp(argv[2], "ip\0", 3) == 0)
08897 return sip_do_debug_ip(fd, argc, argv);
08898 else if (strncmp(argv[2], "peer\0", 5) == 0)
08899 return sip_do_debug_peer(fd, argc, argv);
08900 else return RESULT_SHOWUSAGE;
08901 }
08902 sipdebug |= SIP_DEBUG_CONSOLE;
08903 memset(&debugaddr, 0, sizeof(debugaddr));
08904 if (oldsipdebug)
08905 ast_cli(fd, "SIP Debugging re-enabled\n");
08906 else
08907 ast_cli(fd, "SIP Debugging enabled\n");
08908 return RESULT_SUCCESS;
08909 }
08910
08911
08912 static int sip_notify(int fd, int argc, char *argv[])
08913 {
08914 struct ast_variable *varlist;
08915 int i;
08916
08917 if (argc < 4)
08918 return RESULT_SHOWUSAGE;
08919
08920 if (!notify_types) {
08921 ast_cli(fd, "No %s file found, or no types listed there\n", notify_config);
08922 return RESULT_FAILURE;
08923 }
08924
08925 varlist = ast_variable_browse(notify_types, argv[2]);
08926
08927 if (!varlist) {
08928 ast_cli(fd, "Unable to find notify type '%s'\n", argv[2]);
08929 return RESULT_FAILURE;
08930 }
08931
08932 for (i = 3; i < argc; i++) {
08933 struct sip_pvt *p;
08934 struct sip_request req;
08935 struct ast_variable *var;
08936
08937 p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY);
08938 if (!p) {
08939 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify\n");
08940 return RESULT_FAILURE;
08941 }
08942
08943 if (create_addr(p, argv[i])) {
08944
08945 sip_destroy(p);
08946 ast_cli(fd, "Could not create address for '%s'\n", argv[i]);
08947 continue;
08948 }
08949
08950 initreqprep(&req, p, SIP_NOTIFY);
08951
08952 for (var = varlist; var; var = var->next)
08953 add_header(&req, var->name, var->value);
08954
08955 add_blank_header(&req);
08956
08957 if (ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip))
08958 memcpy(&p->ourip, &__ourip, sizeof(p->ourip));
08959 build_via(p, p->via, sizeof(p->via));
08960 build_callid(p->callid, sizeof(p->callid), p->ourip, p->fromdomain);
08961 ast_cli(fd, "Sending NOTIFY of type '%s' to '%s'\n", argv[2], argv[i]);
08962 transmit_sip_request(p, &req);
08963 sip_scheddestroy(p, 15000);
08964 }
08965
08966 return RESULT_SUCCESS;
08967 }
08968
08969 static int sip_do_history(int fd, int argc, char *argv[])
08970 {
08971 if (argc != 2) {
08972 return RESULT_SHOWUSAGE;
08973 }
08974 recordhistory = 1;
08975 ast_cli(fd, "SIP History Recording Enabled (use 'sip show history')\n");
08976 return RESULT_SUCCESS;
08977 }
08978
08979
08980 static int sip_no_history(int fd, int argc, char *argv[])
08981 {
08982 if (argc != 3) {
08983 return RESULT_SHOWUSAGE;
08984 }
08985 recordhistory = 0;
08986 ast_cli(fd, "SIP History Recording Disabled\n");
08987 return RESULT_SUCCESS;
08988 }
08989
08990
08991 static int sip_no_debug(int fd, int argc, char *argv[])
08992
08993 {
08994 if (argc != 3)
08995 return RESULT_SHOWUSAGE;
08996 sipdebug &= ~SIP_DEBUG_CONSOLE;
08997 ast_cli(fd, "SIP Debugging Disabled\n");
08998 return RESULT_SUCCESS;
08999 }
09000
09001 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
09002
09003
09004 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader)
09005 {
09006 char digest[1024];
09007 p->authtries++;
09008 memset(digest,0,sizeof(digest));
09009 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
09010
09011
09012 if (sip_debug_test_pvt(p) && p->registry)
09013 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
09014
09015 return -1;
09016 }
09017 if (recordhistory) {
09018 char tmp[80];
09019 snprintf(tmp, sizeof(tmp), "Try: %d", p->authtries);
09020 append_history(p, "RegistryAuth", tmp);
09021 }
09022 if (sip_debug_test_pvt(p) && p->registry)
09023 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
09024 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
09025 }
09026
09027
09028 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *header, char *respheader, int sipmethod, int init)
09029 {
09030 char digest[1024];
09031
09032 if (!p->options) {
09033 p->options = calloc(1, sizeof(*p->options));
09034 if (!p->options) {
09035 ast_log(LOG_ERROR, "Out of memory\n");
09036 return -2;
09037 }
09038 }
09039
09040 p->authtries++;
09041 if (option_debug > 1)
09042 ast_log(LOG_DEBUG, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
09043 memset(digest, 0, sizeof(digest));
09044 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
09045
09046 return -1;
09047 }
09048
09049 p->options->auth = digest;
09050 p->options->authheader = respheader;
09051 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init);
09052 }
09053
09054
09055
09056
09057
09058 static int reply_digest(struct sip_pvt *p, struct sip_request *req,
09059 char *header, int sipmethod, char *digest, int digest_len)
09060 {
09061 char tmp[512];
09062 char *c;
09063 char oldnonce[256];
09064
09065
09066 const struct x {
09067 const char *key;
09068 char *dst;
09069 int dstlen;
09070 } *i, keys[] = {
09071 { "realm=", p->realm, sizeof(p->realm) },
09072 { "nonce=", p->nonce, sizeof(p->nonce) },
09073 { "opaque=", p->opaque, sizeof(p->opaque) },
09074 { "qop=", p->qop, sizeof(p->qop) },
09075 { "domain=", p->domain, sizeof(p->domain) },
09076 { NULL, NULL, 0 },
09077 };
09078
09079 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
09080 if (ast_strlen_zero(tmp))
09081 return -1;
09082 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
09083 ast_log(LOG_WARNING, "missing Digest.\n");
09084 return -1;
09085 }
09086 c = tmp + strlen("Digest ");
09087 for (i = keys; i->key != NULL; i++)
09088 i->dst[0] = '\0';
09089 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
09090 while (c && *(c = ast_skip_blanks(c))) {
09091 for (i = keys; i->key != NULL; i++) {
09092 char *src, *separator;
09093 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
09094 continue;
09095
09096 c += strlen(i->key);
09097 if (*c == '\"') {
09098 src = ++c;
09099 separator = "\"";
09100 } else {
09101 src = c;
09102 separator = ",";
09103 }
09104 strsep(&c, separator);
09105 ast_copy_string(i->dst, src, i->dstlen);
09106 break;
09107 }
09108 if (i->key == NULL)
09109 strsep(&c, ",");
09110 }
09111
09112 if (strcmp(p->nonce, oldnonce))
09113 p->noncecount = 0;
09114
09115
09116 if (p->registry) {
09117 struct sip_registry *r = p->registry;
09118
09119 if (strcmp(r->nonce, p->nonce)) {
09120 ast_copy_string(r->realm, p->realm, sizeof(r->realm));
09121 ast_copy_string(r->nonce, p->nonce, sizeof(r->nonce));
09122 ast_copy_string(r->domain, p->domain, sizeof(r->domain));
09123 ast_copy_string(r->opaque, p->opaque, sizeof(r->opaque));
09124 ast_copy_string(r->qop, p->qop, sizeof(r->qop));
09125 r->noncecount = 0;
09126 }
09127 }
09128 return build_reply_digest(p, sipmethod, digest, digest_len);
09129 }
09130
09131
09132
09133
09134
09135 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
09136 {
09137 char a1[256];
09138 char a2[256];
09139 char a1_hash[256];
09140 char a2_hash[256];
09141 char resp[256];
09142 char resp_hash[256];
09143 char uri[256];
09144 char cnonce[80];
09145 char iabuf[INET_ADDRSTRLEN];
09146 char *username;
09147 char *secret;
09148 char *md5secret;
09149 struct sip_auth *auth = (struct sip_auth *) NULL;
09150
09151 if (!ast_strlen_zero(p->domain))
09152 ast_copy_string(uri, p->domain, sizeof(uri));
09153 else if (!ast_strlen_zero(p->uri))
09154 ast_copy_string(uri, p->uri, sizeof(uri));
09155 else
09156 snprintf(uri, sizeof(uri), "sip:%s@%s",p->username, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr));
09157
09158 snprintf(cnonce, sizeof(cnonce), "%08x", thread_safe_rand());
09159
09160
09161 if ((auth = find_realm_authentication(authl, p->realm))) {
09162 username = auth->username;
09163 secret = auth->secret;
09164 md5secret = auth->md5secret;
09165 if (sipdebug)
09166 ast_log(LOG_DEBUG,"Using realm %s authentication for call %s\n", p->realm, p->callid);
09167 } else {
09168
09169 username = p->authname;
09170 secret = p->peersecret;
09171 md5secret = p->peermd5secret;
09172 }
09173 if (ast_strlen_zero(username))
09174 return -1;
09175
09176
09177
09178 snprintf(a1,sizeof(a1),"%s:%s:%s", username, p->realm, secret);
09179 snprintf(a2,sizeof(a2),"%s:%s", sip_methods[method].text, uri);
09180 if (!ast_strlen_zero(md5secret))
09181 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
09182 else
09183 ast_md5_hash(a1_hash,a1);
09184 ast_md5_hash(a2_hash,a2);
09185
09186 p->noncecount++;
09187 if (!ast_strlen_zero(p->qop))
09188 snprintf(resp,sizeof(resp),"%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
09189 else
09190 snprintf(resp,sizeof(resp),"%s:%s:%s", a1_hash, p->nonce, a2_hash);
09191 ast_md5_hash(resp_hash, resp);
09192
09193 if (!ast_strlen_zero(p->qop))
09194 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);
09195 else
09196 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);
09197
09198 return 0;
09199 }
09200
09201 static char show_domains_usage[] =
09202 "Usage: sip show domains\n"
09203 " Lists all configured SIP local domains.\n"
09204 " Asterisk only responds to SIP messages to local domains.\n";
09205
09206 static char notify_usage[] =
09207 "Usage: sip notify <type> <peer> [<peer>...]\n"
09208 " Send a NOTIFY message to a SIP peer or peers\n"
09209 " Message types are defined in sip_notify.conf\n";
09210
09211 static char show_users_usage[] =
09212 "Usage: sip show users [like <pattern>]\n"
09213 " Lists all known SIP users.\n"
09214 " Optional regular expression pattern is used to filter the user list.\n";
09215
09216 static char show_user_usage[] =
09217 "Usage: sip show user <name> [load]\n"
09218 " Lists all details on one SIP user and the current status.\n"
09219 " Option \"load\" forces lookup of peer in realtime storage.\n";
09220
09221 static char show_inuse_usage[] =
09222 "Usage: sip show inuse [all]\n"
09223 " List all SIP users and peers usage counters and limits.\n"
09224 " Add option \"all\" to show all devices, not only those with a limit.\n";
09225
09226 static char show_channels_usage[] =
09227 "Usage: sip show channels\n"
09228 " Lists all currently active SIP channels.\n";
09229
09230 static char show_channel_usage[] =
09231 "Usage: sip show channel <channel>\n"
09232 " Provides detailed status on a given SIP channel.\n";
09233
09234 static char show_history_usage[] =
09235 "Usage: sip show history <channel>\n"
09236 " Provides detailed dialog history on a given SIP channel.\n";
09237
09238 static char show_peers_usage[] =
09239 "Usage: sip show peers [like <pattern>]\n"
09240 " Lists all known SIP peers.\n"
09241 " Optional regular expression pattern is used to filter the peer list.\n";
09242
09243 static char show_peer_usage[] =
09244 "Usage: sip show peer <name> [load]\n"
09245 " Lists all details on one SIP peer and the current status.\n"
09246 " Option \"load\" forces lookup of peer in realtime storage.\n";
09247
09248 static char prune_realtime_usage[] =
09249 "Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
09250 " Prunes object(s) from the cache.\n"
09251 " Optional regular expression pattern is used to filter the objects.\n";
09252
09253 static char show_reg_usage[] =
09254 "Usage: sip show registry\n"
09255 " Lists all registration requests and status.\n";
09256
09257 static char debug_usage[] =
09258 "Usage: sip debug\n"
09259 " Enables dumping of SIP packets for debugging purposes\n\n"
09260 " sip debug ip <host[:PORT]>\n"
09261 " Enables dumping of SIP packets to and from host.\n\n"
09262 " sip debug peer <peername>\n"
09263 " Enables dumping of SIP packets to and from host.\n"
09264 " Require peer to be registered.\n";
09265
09266 static char no_debug_usage[] =
09267 "Usage: sip no debug\n"
09268 " Disables dumping of SIP packets for debugging purposes\n";
09269
09270 static char no_history_usage[] =
09271 "Usage: sip no history\n"
09272 " Disables recording of SIP dialog history for debugging purposes\n";
09273
09274 static char history_usage[] =
09275 "Usage: sip history\n"
09276 " Enables recording of SIP dialog history for debugging purposes.\n"
09277 "Use 'sip show history' to view the history of a call number.\n";
09278
09279 static char sip_reload_usage[] =
09280 "Usage: sip reload\n"
09281 " Reloads SIP configuration from sip.conf\n";
09282
09283 static char show_subscriptions_usage[] =
09284 "Usage: sip show subscriptions\n"
09285 " Shows active SIP subscriptions for extension states\n";
09286
09287 static char show_objects_usage[] =
09288 "Usage: sip show objects\n"
09289 " Shows status of known SIP objects\n";
09290
09291 static char show_settings_usage[] =
09292 "Usage: sip show settings\n"
09293 " Provides detailed list of the configuration of the SIP channel.\n";
09294
09295
09296
09297
09298 static char *func_header_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
09299 {
09300 struct sip_pvt *p;
09301 char *content;
09302
09303 if (!data) {
09304 ast_log(LOG_WARNING, "This function requires a header name.\n");
09305 return NULL;
09306 }
09307
09308 ast_mutex_lock(&chan->lock);
09309 if (chan->type != channeltype) {
09310 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
09311 ast_mutex_unlock(&chan->lock);
09312 return NULL;
09313 }
09314
09315 p = chan->tech_pvt;
09316
09317
09318 if (!p) {
09319 ast_mutex_unlock(&chan->lock);
09320 return NULL;
09321 }
09322
09323 content = get_header(&p->initreq, data);
09324
09325 if (ast_strlen_zero(content)) {
09326 ast_mutex_unlock(&chan->lock);
09327 return NULL;
09328 }
09329
09330 ast_copy_string(buf, content, len);
09331 ast_mutex_unlock(&chan->lock);
09332
09333 return buf;
09334 }
09335
09336
09337 static struct ast_custom_function sip_header_function = {
09338 .name = "SIP_HEADER",
09339 .synopsis = "Gets the specified SIP header",
09340 .syntax = "SIP_HEADER(<name>)",
09341 .read = func_header_read,
09342 };
09343
09344
09345 static char *func_check_sipdomain(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
09346 {
09347 if (ast_strlen_zero(data)) {
09348 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
09349 return buf;
09350 }
09351 if (check_sip_domain(data, NULL, 0))
09352 ast_copy_string(buf, data, len);
09353 else
09354 buf[0] = '\0';
09355 return buf;
09356 }
09357
09358 static struct ast_custom_function checksipdomain_function = {
09359 .name = "CHECKSIPDOMAIN",
09360 .synopsis = "Checks if domain is a local domain",
09361 .syntax = "CHECKSIPDOMAIN(<domain|IP>)",
09362 .read = func_check_sipdomain,
09363 .desc = "This function checks if the domain in the argument is configured\n"
09364 "as a local SIP domain that this Asterisk server is configured to handle.\n"
09365 "Returns the domain name if it is locally handled, otherwise an empty string.\n"
09366 "Check the domain= configuration in sip.conf\n",
09367 };
09368
09369
09370
09371 static char *function_sippeer(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
09372 {
09373 char *ret = NULL;
09374 struct sip_peer *peer;
09375 char *peername, *colname;
09376 char iabuf[INET_ADDRSTRLEN];
09377
09378 if (!(peername = ast_strdupa(data))) {
09379 ast_log(LOG_ERROR, "Memory Error!\n");
09380 return ret;
09381 }
09382
09383 if ((colname = strchr(peername, ':'))) {
09384 *colname = '\0';
09385 colname++;
09386 } else {
09387 colname = "ip";
09388 }
09389 if (!(peer = find_peer(peername, NULL, 1)))
09390 return ret;
09391
09392 if (!strcasecmp(colname, "ip")) {
09393 ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), peer->addr.sin_addr) : "", len);
09394 } else if (!strcasecmp(colname, "status")) {
09395 peer_status(peer, buf, len);
09396 } else if (!strcasecmp(colname, "language")) {
09397 ast_copy_string(buf, peer->language, len);
09398 } else if (!strcasecmp(colname, "regexten")) {
09399 ast_copy_string(buf, peer->regexten, len);
09400 } else if (!strcasecmp(colname, "limit")) {
09401 snprintf(buf, len, "%d", peer->call_limit);
09402 } else if (!strcasecmp(colname, "curcalls")) {
09403 snprintf(buf, len, "%d", peer->inUse);
09404 } else if (!strcasecmp(colname, "accountcode")) {
09405 ast_copy_string(buf, peer->accountcode, len);
09406 } else if (!strcasecmp(colname, "useragent")) {
09407 ast_copy_string(buf, peer->useragent, len);
09408 } else if (!strcasecmp(colname, "mailbox")) {
09409 ast_copy_string(buf, peer->mailbox, len);
09410 } else if (!strcasecmp(colname, "context")) {
09411 ast_copy_string(buf, peer->context, len);
09412 } else if (!strcasecmp(colname, "expire")) {
09413 snprintf(buf, len, "%d", peer->expire);
09414 } else if (!strcasecmp(colname, "dynamic")) {
09415 ast_copy_string(buf, (ast_test_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC) ? "yes" : "no"), len);
09416 } else if (!strcasecmp(colname, "callerid_name")) {
09417 ast_copy_string(buf, peer->cid_name, len);
09418 } else if (!strcasecmp(colname, "callerid_num")) {
09419 ast_copy_string(buf, peer->cid_num, len);
09420 } else if (!strcasecmp(colname, "codecs")) {
09421 ast_getformatname_multiple(buf, len -1, peer->capability);
09422 } else if (!strncasecmp(colname, "codec[", 6)) {
09423 char *codecnum, *ptr;
09424 int index = 0, codec = 0;
09425
09426 codecnum = strchr(colname, '[');
09427 *codecnum = '\0';
09428 codecnum++;
09429 if ((ptr = strchr(codecnum, ']'))) {
09430 *ptr = '\0';
09431 }
09432 index = atoi(codecnum);
09433 if((codec = ast_codec_pref_index(&peer->prefs, index))) {
09434 ast_copy_string(buf, ast_getformatname(codec), len);
09435 }
09436 }
09437 ret = buf;
09438
09439 ASTOBJ_UNREF(peer, sip_destroy_peer);
09440
09441 return ret;
09442 }
09443
09444
09445 struct ast_custom_function sippeer_function = {
09446 .name = "SIPPEER",
09447 .synopsis = "Gets SIP peer information",
09448 .syntax = "SIPPEER(<peername>[:item])",
09449 .read = function_sippeer,
09450 .desc = "Valid items are:\n"
09451 "- ip (default) The IP address.\n"
09452 "- mailbox The configured mailbox.\n"
09453 "- context The configured context.\n"
09454 "- expire The epoch time of the next expire.\n"
09455 "- dynamic Is it dynamic? (yes/no).\n"
09456 "- callerid_name The configured Caller ID name.\n"
09457 "- callerid_num The configured Caller ID number.\n"
09458 "- codecs The configured codecs.\n"
09459 "- status Status (if qualify=yes).\n"
09460 "- regexten Registration extension\n"
09461 "- limit Call limit (call-limit)\n"
09462 "- curcalls Current amount of calls \n"
09463 " Only available if call-limit is set\n"
09464 "- language Default language for peer\n"
09465 "- accountcode Account code for this peer\n"
09466 "- useragent Current user agent id for peer\n"
09467 "- codec[x] Preferred codec index number 'x' (beginning with zero).\n"
09468 "\n"
09469 };
09470
09471
09472 static char *function_sipchaninfo_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
09473 {
09474 struct sip_pvt *p;
09475 char iabuf[INET_ADDRSTRLEN];
09476
09477 *buf = 0;
09478
09479 if (!data) {
09480 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
09481 return NULL;
09482 }
09483
09484 ast_mutex_lock(&chan->lock);
09485 if (chan->type != channeltype) {
09486 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
09487 ast_mutex_unlock(&chan->lock);
09488 return NULL;
09489 }
09490
09491
09492 p = chan->tech_pvt;
09493
09494
09495 if (!p) {
09496 ast_mutex_unlock(&chan->lock);
09497 return NULL;
09498 }
09499
09500 if (!strcasecmp(data, "peerip")) {
09501 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr) : "", len);
09502 } else if (!strcasecmp(data, "recvip")) {
09503 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr) : "", len);
09504 } else if (!strcasecmp(data, "from")) {
09505 ast_copy_string(buf, p->from, len);
09506 } else if (!strcasecmp(data, "uri")) {
09507 ast_copy_string(buf, p->uri, len);
09508 } else if (!strcasecmp(data, "useragent")) {
09509 ast_copy_string(buf, p->useragent, len);
09510 } else if (!strcasecmp(data, "peername")) {
09511 ast_copy_string(buf, p->peername, len);
09512 } else {
09513 ast_mutex_unlock(&chan->lock);
09514 return NULL;
09515 }
09516 ast_mutex_unlock(&chan->lock);
09517
09518 return buf;
09519 }
09520
09521
09522 static struct ast_custom_function sipchaninfo_function = {
09523 .name = "SIPCHANINFO",
09524 .synopsis = "Gets the specified SIP parameter from the current channel",
09525 .syntax = "SIPCHANINFO(item)",
09526 .read = function_sipchaninfo_read,
09527 .desc = "Valid items are:\n"
09528 "- peerip The IP address of the peer.\n"
09529 "- recvip The source IP address of the peer.\n"
09530 "- from The URI from the From: header.\n"
09531 "- uri The URI from the Contact: header.\n"
09532 "- useragent The useragent.\n"
09533 "- peername The name of the peer.\n"
09534 };
09535
09536
09537
09538
09539 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req)
09540 {
09541 char tmp[256];
09542 char *s, *e;
09543 ast_copy_string(tmp, get_header(req, "Contact"), sizeof(tmp));
09544 s = get_in_brackets(tmp);
09545 e = strchr(s, ';');
09546 if (e)
09547 *e = '\0';
09548 if (ast_test_flag(p, SIP_PROMISCREDIR)) {
09549 if (!strncasecmp(s, "sip:", 4))
09550 s += 4;
09551 e = strchr(s, '/');
09552 if (e)
09553 *e = '\0';
09554 ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
09555 if (p->owner)
09556 snprintf(p->owner->call_forward, sizeof(p->owner->call_forward), "SIP/%s", s);
09557 } else {
09558 e = strchr(tmp, '@');
09559 if (e)
09560 *e = '\0';
09561 e = strchr(tmp, '/');
09562 if (e)
09563 *e = '\0';
09564 if (!strncasecmp(s, "sip:", 4))
09565 s += 4;
09566 ast_log(LOG_DEBUG, "Found 302 Redirect to extension '%s'\n", s);
09567 if (p->owner)
09568 ast_copy_string(p->owner->call_forward, s, sizeof(p->owner->call_forward));
09569 }
09570 }
09571
09572
09573 static void check_pendings(struct sip_pvt *p)
09574 {
09575 if (ast_test_flag(p, SIP_PENDINGBYE)) {
09576
09577 if (!ast_test_flag(p, SIP_CAN_BYE))
09578 transmit_request_with_auth(p, SIP_CANCEL, p->ocseq, 1, 0);
09579
09580
09581 else
09582 transmit_request_with_auth(p, SIP_BYE, 0, 1, 1);
09583 ast_clear_flag(p, SIP_PENDINGBYE);
09584 sip_scheddestroy(p, 32000);
09585 } else if (ast_test_flag(p, SIP_NEEDREINVITE)) {
09586 ast_log(LOG_DEBUG, "Sending pending reinvite on '%s'\n", p->callid);
09587
09588 transmit_reinvite_with_sdp(p);
09589 ast_clear_flag(p, SIP_NEEDREINVITE);
09590 }
09591 }
09592
09593
09594 static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
09595 {
09596 int outgoing = ast_test_flag(p, SIP_OUTGOING);
09597
09598 if (option_debug > 3) {
09599 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
09600 if (reinvite)
09601 ast_log(LOG_DEBUG, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
09602 else
09603 ast_log(LOG_DEBUG, "SIP response %d to standard invite\n", resp);
09604 }
09605
09606 if (ast_test_flag(p, SIP_ALREADYGONE)) {
09607 ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
09608 return;
09609 }
09610
09611
09612
09613
09614 if ((resp > 100) &&
09615 (resp < 200) &&
09616 (resp != 180) &&
09617 (resp != 183))
09618 resp = 183;
09619
09620 switch (resp) {
09621 case 100:
09622 if (!ignore)
09623 sip_cancel_destroy(p);
09624 check_pendings(p);
09625 break;
09626 case 180:
09627 if (!ignore)
09628 sip_cancel_destroy(p);
09629 if (!ignore && p->owner) {
09630 ast_queue_control(p->owner, AST_CONTROL_RINGING);
09631 if (p->owner->_state != AST_STATE_UP)
09632 ast_setstate(p->owner, AST_STATE_RINGING);
09633 }
09634 if (find_sdp(req)) {
09635 process_sdp(p, req);
09636 if (!ignore && p->owner) {
09637
09638 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
09639 }
09640 }
09641 ast_set_flag(p, SIP_CAN_BYE);
09642 check_pendings(p);
09643 break;
09644 case 183:
09645 if (!ignore)
09646 sip_cancel_destroy(p);
09647
09648 if (find_sdp(req)) {
09649 process_sdp(p, req);
09650 if (!ignore && p->owner) {
09651
09652 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
09653 }
09654 }
09655 ast_set_flag(p, SIP_CAN_BYE);
09656 check_pendings(p);
09657 break;
09658 case 200:
09659 if (!ignore)
09660 sip_cancel_destroy(p);
09661 p->authtries = 0;
09662 if (find_sdp(req)) {
09663 process_sdp(p, req);
09664 }
09665
09666
09667
09668
09669 if (outgoing) {
09670 parse_ok_contact(p, req);
09671
09672
09673 build_route(p, req, 1);
09674 }
09675
09676 if (!ignore && p->owner) {
09677 if (p->owner->_state != AST_STATE_UP) {
09678 #ifdef OSP_SUPPORT
09679 time(&p->ospstart);
09680 #endif
09681 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
09682 } else {
09683 struct ast_frame af = { AST_FRAME_NULL, };
09684 ast_queue_frame(p->owner, &af);
09685 }
09686 } else {
09687
09688
09689
09690 if (!ignore)
09691 ast_set_flag(p, SIP_PENDINGBYE);
09692 }
09693
09694 transmit_request(p, SIP_ACK, seqno, 0, 1);
09695 ast_set_flag(p, SIP_CAN_BYE);
09696 check_pendings(p);
09697 break;
09698 case 407:
09699 case 401:
09700
09701 transmit_request(p, SIP_ACK, seqno, 0, 0);
09702 if (p->options)
09703 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
09704
09705
09706 p->theirtag[0]='\0';
09707 if (!ignore) {
09708 char *authenticate = (resp == 401 ? "WWW-Authenticate" : "Proxy-Authenticate");
09709 char *authorization = (resp == 401 ? "Authorization" : "Proxy-Authorization");
09710 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, authenticate, authorization, SIP_INVITE, 1)) {
09711 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
09712 ast_set_flag(p, SIP_NEEDDESTROY);
09713 ast_set_flag(p, SIP_ALREADYGONE);
09714 if (p->owner)
09715 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
09716 }
09717 }
09718 break;
09719 case 403:
09720
09721 transmit_request(p, SIP_ACK, seqno, 0, 0);
09722 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for INVITE to '%s'\n", get_header(&p->initreq, "From"));
09723 if (!ignore && p->owner)
09724 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
09725 ast_set_flag(p, SIP_NEEDDESTROY);
09726 ast_set_flag(p, SIP_ALREADYGONE);
09727 break;
09728 case 404:
09729 transmit_request(p, SIP_ACK, seqno, 0, 0);
09730 if (p->owner && !ignore)
09731 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
09732 ast_set_flag(p, SIP_ALREADYGONE);
09733 break;
09734 case 481:
09735
09736 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
09737 transmit_request(p, SIP_ACK, seqno, 0, 0);
09738 break;
09739 case 491:
09740
09741
09742
09743 break;
09744 case 501:
09745 if (p->owner)
09746 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
09747 break;
09748 }
09749 }
09750
09751
09752 static int handle_response_register(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
09753 {
09754 int expires, expires_ms;
09755 struct sip_registry *r;
09756 r=p->registry;
09757
09758 switch (resp) {
09759 case 401:
09760 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "WWW-Authenticate", "Authorization")) {
09761 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
09762 ast_set_flag(p, SIP_NEEDDESTROY);
09763 }
09764 break;
09765 case 403:
09766 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
09767 if (global_regattempts_max)
09768 p->registry->regattempts = global_regattempts_max+1;
09769 ast_sched_del(sched, r->timeout);
09770 ast_set_flag(p, SIP_NEEDDESTROY);
09771 break;
09772 case 404:
09773 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username,p->registry->hostname);
09774 if (global_regattempts_max)
09775 p->registry->regattempts = global_regattempts_max+1;
09776 ast_set_flag(p, SIP_NEEDDESTROY);
09777 r->call = NULL;
09778 ast_sched_del(sched, r->timeout);
09779 break;
09780 case 407:
09781 if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization")) {
09782 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
09783 ast_set_flag(p, SIP_NEEDDESTROY);
09784 }
09785 break;
09786 case 479:
09787 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username,p->registry->hostname);
09788 if (global_regattempts_max)
09789 p->registry->regattempts = global_regattempts_max+1;
09790 ast_set_flag(p, SIP_NEEDDESTROY);
09791 r->call = NULL;
09792 ast_sched_del(sched, r->timeout);
09793 break;
09794 case 200:
09795 if (!r) {
09796 ast_log(LOG_WARNING, "Got 200 OK on REGISTER that isn't a register\n");
09797 ast_set_flag(p, SIP_NEEDDESTROY);
09798 return 0;
09799 }
09800
09801 r->regstate=REG_STATE_REGISTERED;
09802 manager_event(EVENT_FLAG_SYSTEM, "Registry", "Channel: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
09803 r->regattempts = 0;
09804 ast_log(LOG_DEBUG, "Registration successful\n");
09805 if (r->timeout > -1) {
09806 ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
09807 ast_sched_del(sched, r->timeout);
09808 }
09809 r->timeout=-1;
09810 r->call = NULL;
09811 p->registry = NULL;
09812
09813 sip_scheddestroy(p, 32000);
09814
09815
09816
09817
09818 if (r->expire > -1)
09819 ast_sched_del(sched, r->expire);
09820
09821
09822 expires = 0;
09823 if (!ast_strlen_zero(get_header(req, "Contact"))) {
09824 char *contact = NULL;
09825 char *tmptmp = NULL;
09826 int start = 0;
09827 for(;;) {
09828 contact = __get_header(req, "Contact", &start);
09829
09830 if(!ast_strlen_zero(contact)) {
09831 if( (tmptmp=strstr(contact, p->our_contact))) {
09832 contact=tmptmp;
09833 break;
09834 }
09835 } else
09836 break;
09837 }
09838 tmptmp = strcasestr(contact, "expires=");
09839 if (tmptmp) {
09840 if (sscanf(tmptmp + 8, "%d;", &expires) != 1)
09841 expires = 0;
09842 }
09843
09844 }
09845 if (!expires)
09846 expires=atoi(get_header(req, "expires"));
09847 if (!expires)
09848 expires=default_expiry;
09849
09850 expires_ms = expires * 1000;
09851 if (expires <= EXPIRY_GUARD_LIMIT)
09852 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT),EXPIRY_GUARD_MIN);
09853 else
09854 expires_ms -= EXPIRY_GUARD_SECS * 1000;
09855 if (sipdebug)
09856 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
09857
09858 r->refresh= (int) expires_ms / 1000;
09859
09860
09861 r->expire=ast_sched_add(sched, expires_ms, sip_reregister, r);
09862 ASTOBJ_UNREF(r, sip_registry_destroy);
09863 }
09864 return 1;
09865 }
09866
09867
09868 static int handle_response_peerpoke(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno, int sipmethod)
09869 {
09870 struct sip_peer *peer;
09871 int pingtime;
09872 struct timeval tv;
09873
09874 if (resp != 100) {
09875 int statechanged = 0;
09876 int newstate = 0;
09877 peer = p->peerpoke;
09878 gettimeofday(&tv, NULL);
09879 pingtime = ast_tvdiff_ms(tv, peer->ps);
09880 if (pingtime < 1)
09881 pingtime = 1;
09882 if ((peer->lastms < 0) || (peer->lastms > peer->maxms)) {
09883 if (pingtime <= peer->maxms) {
09884 ast_log(LOG_NOTICE, "Peer '%s' is now REACHABLE! (%dms / %dms)\n", peer->name, pingtime, peer->maxms);
09885 statechanged = 1;
09886 newstate = 1;
09887 }
09888 } else if ((peer->lastms > 0) && (peer->lastms <= peer->maxms)) {
09889 if (pingtime > peer->maxms) {
09890 ast_log(LOG_NOTICE, "Peer '%s' is now TOO LAGGED! (%dms / %dms)\n", peer->name, pingtime, peer->maxms);
09891 statechanged = 1;
09892 newstate = 2;
09893 }
09894 }
09895 if (!peer->lastms)
09896 statechanged = 1;
09897 peer->lastms = pingtime;
09898 peer->call = NULL;
09899 if (statechanged) {
09900 ast_device_state_changed("SIP/%s", peer->name);
09901 if (newstate == 2) {
09902 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Lagged\r\nTime: %d\r\n", peer->name, pingtime);
09903 } else {
09904 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Reachable\r\nTime: %d\r\n", peer->name, pingtime);
09905 }
09906 }
09907
09908 if (peer->pokeexpire > -1)
09909 ast_sched_del(sched, peer->pokeexpire);
09910 if (sipmethod == SIP_INVITE)
09911 transmit_request(p, SIP_ACK, seqno, 0, 0);
09912 ast_set_flag(p, SIP_NEEDDESTROY);
09913
09914
09915 if ((peer->lastms < 0) || (peer->lastms > peer->maxms))
09916 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
09917 else
09918 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_OK, sip_poke_peer_s, peer);
09919 }
09920 return 1;
09921 }
09922
09923
09924 static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_request *req, int ignore, int seqno)
09925 {
09926 char *msg, *c;
09927 struct ast_channel *owner;
09928 char iabuf[INET_ADDRSTRLEN];
09929 int sipmethod;
09930 int res = 1;
09931
09932 c = get_header(req, "Cseq");
09933 msg = strchr(c, ' ');
09934 if (!msg)
09935 msg = "";
09936 else
09937 msg++;
09938 sipmethod = find_sip_method(msg);
09939
09940 owner = p->owner;
09941 if (owner)
09942 owner->hangupcause = hangup_sip2cause(resp);
09943
09944
09945 if ((resp >= 100) && (resp <= 199))
09946 __sip_semi_ack(p, seqno, 0, sipmethod);
09947 else
09948 __sip_ack(p, seqno, 0, sipmethod);
09949
09950
09951 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
09952 gettag(req, "To", p->theirtag, sizeof(p->theirtag));
09953 }
09954 if (p->peerpoke) {
09955
09956
09957
09958
09959 res = handle_response_peerpoke(p, resp, rest, req, ignore, seqno, sipmethod);
09960 } else if (ast_test_flag(p, SIP_OUTGOING)) {
09961
09962 if (p->initid > -1) {
09963
09964 ast_sched_del(sched, p->initid);
09965 p->initid = -1;
09966 }
09967 switch(resp) {
09968 case 100:
09969 if (sipmethod == SIP_INVITE)
09970 handle_response_invite(p, resp, rest, req, ignore, seqno);
09971 break;
09972 case 183:
09973 if (sipmethod == SIP_INVITE)
09974 handle_response_invite(p, resp, rest, req, ignore, seqno);
09975 break;
09976 case 180:
09977 if (sipmethod == SIP_INVITE)
09978 handle_response_invite(p, resp, rest, req, ignore, seqno);
09979 break;
09980 case 200:
09981 p->authtries = 0;
09982 if (sipmethod == SIP_MESSAGE) {
09983
09984 ast_set_flag(p, SIP_NEEDDESTROY);
09985 } else if (sipmethod == SIP_NOTIFY) {
09986
09987 if (p->owner) {
09988 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
09989 ast_queue_hangup(p->owner);
09990 } else {
09991 if (p->subscribed == NONE) {
09992 ast_set_flag(p, SIP_NEEDDESTROY);
09993 }
09994 }
09995 } else if (sipmethod == SIP_INVITE) {
09996 handle_response_invite(p, resp, rest, req, ignore, seqno);
09997 } else if (sipmethod == SIP_REGISTER) {
09998 res = handle_response_register(p, resp, rest, req, ignore, seqno);
09999 } else if (sipmethod == SIP_BYE) {
10000
10001 ast_set_flag(p, SIP_NEEDDESTROY);
10002 }
10003 break;
10004 case 401:
10005 if (sipmethod == SIP_INVITE) {
10006 handle_response_invite(p, resp, rest, req, ignore, seqno);
10007 } else if (p->registry && sipmethod == SIP_REGISTER) {
10008 res = handle_response_register(p, resp, rest, req, ignore, seqno);
10009 } else {
10010 ast_log(LOG_WARNING, "Got authentication request (401) on unknown %s to '%s'\n", sip_methods[sipmethod].text, get_header(req, "To"));
10011 ast_set_flag(p, SIP_NEEDDESTROY);
10012 }
10013 break;
10014 case 403:
10015 if (sipmethod == SIP_INVITE) {
10016 handle_response_invite(p, resp, rest, req, ignore, seqno);
10017 } else if (p->registry && sipmethod == SIP_REGISTER) {
10018 res = handle_response_register(p, resp, rest, req, ignore, seqno);
10019 } else {
10020 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for %s\n", msg);
10021 }
10022 break;
10023 case 404:
10024 if (p->registry && sipmethod == SIP_REGISTER) {
10025 res = handle_response_register(p, resp, rest, req, ignore, seqno);
10026 } else if (sipmethod == SIP_INVITE) {
10027 handle_response_invite(p, resp, rest, req, ignore, seqno);
10028 } else if (owner)
10029 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
10030 break;
10031 case 407:
10032 if (sipmethod == SIP_INVITE) {
10033 handle_response_invite(p, resp, rest, req, ignore, seqno);
10034 } else if (sipmethod == SIP_BYE || sipmethod == SIP_REFER) {
10035 if (ast_strlen_zero(p->authname))
10036 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s:%d but we have no matching peer!\n",
10037 msg, ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr), ntohs(p->recv.sin_port));
10038 ast_set_flag(p, SIP_NEEDDESTROY);
10039 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization", sipmethod, 0)) {
10040 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
10041 ast_set_flag(p, SIP_NEEDDESTROY);
10042 }
10043 } else if (p->registry && sipmethod == SIP_REGISTER) {
10044 res = handle_response_register(p, resp, rest, req, ignore, seqno);
10045 } else
10046 ast_set_flag(p, SIP_NEEDDESTROY);
10047
10048 break;
10049 case 491:
10050 if (sipmethod == SIP_INVITE) {
10051 handle_response_invite(p, resp, rest, req, ignore, seqno);
10052 }
10053 case 501:
10054 if (sipmethod == SIP_INVITE) {
10055 handle_response_invite(p, resp, rest, req, ignore, seqno);
10056 } else
10057 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), msg);
10058 break;
10059 default:
10060 if ((resp >= 300) && (resp < 700)) {
10061 if ((option_verbose > 2) && (resp != 487))
10062 ast_verbose(VERBOSE_PREFIX_3 "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr));
10063 ast_set_flag(p, SIP_ALREADYGONE);
10064 if (p->rtp) {
10065
10066 ast_rtp_stop(p->rtp);
10067 }
10068 if (p->vrtp) {
10069
10070 ast_rtp_stop(p->vrtp);
10071 }
10072
10073 switch(resp) {
10074 case 300:
10075 case 301:
10076 case 302:
10077 case 305:
10078 parse_moved_contact(p, req);
10079
10080 case 486:
10081 case 600:
10082 case 603:
10083 if (p->owner)
10084 ast_queue_control(p->owner, AST_CONTROL_BUSY);
10085 break;
10086 case 487:
10087
10088 if (owner)
10089 ast_queue_hangup(p->owner);
10090 update_call_counter(p, DEC_CALL_LIMIT);
10091 break;
10092 case 482:
10093
10094
10095
10096 ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
10097 if (p->owner)
10098 snprintf(p->owner->call_forward, sizeof(p->owner->call_forward), "Local/%s@%s", p->username, p->context);
10099
10100 case 488:
10101 case 480:
10102 case 404:
10103 case 410:
10104 case 400:
10105 case 500:
10106 case 503:
10107 if (owner)
10108 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
10109 break;
10110 default:
10111
10112 if (owner)
10113 ast_queue_hangup(p->owner);
10114 break;
10115 }
10116
10117 if (sipmethod == SIP_INVITE)
10118 transmit_request(p, SIP_ACK, seqno, 0, 0);
10119 ast_set_flag(p, SIP_ALREADYGONE);
10120 if (!p->owner)
10121 ast_set_flag(p, SIP_NEEDDESTROY);
10122 } else if ((resp >= 100) && (resp < 200)) {
10123 if (sipmethod == SIP_INVITE) {
10124 if (!ignore)
10125 sip_cancel_destroy(p);
10126 if (find_sdp(req))
10127 process_sdp(p, req);
10128 if (p->owner) {
10129
10130 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
10131 }
10132 }
10133 } else
10134 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(iabuf, sizeof(iabuf), p->sa.sin_addr));
10135 }
10136 } else {
10137
10138
10139 if (req->debug)
10140 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
10141 if (resp == 200) {
10142
10143
10144 gettag(req, "To", p->theirtag, sizeof(p->theirtag));
10145 }
10146
10147 switch(resp) {
10148 case 200:
10149 if (sipmethod == SIP_INVITE) {
10150 handle_response_invite(p, resp, rest, req, ignore, seqno);
10151 } else if (sipmethod == SIP_CANCEL) {
10152 ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
10153 } else if (sipmethod == SIP_MESSAGE)
10154
10155 ast_set_flag(p, SIP_NEEDDESTROY);
10156 else if (sipmethod == SIP_BYE)
10157
10158 ast_set_flag(p, SIP_NEEDDESTROY);
10159 break;
10160 case 401:
10161 case 407:
10162 if (sipmethod == SIP_BYE || sipmethod == SIP_REFER) {
10163 char *auth, *auth2;
10164
10165 if (resp == 407) {
10166 auth = "Proxy-Authenticate";
10167 auth2 = "Proxy-Authorization";
10168 } else {
10169 auth = "WWW-Authenticate";
10170 auth2 = "Authorization";
10171 }
10172 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, auth, auth2, sipmethod, 0)) {
10173 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
10174 ast_set_flag(p, SIP_NEEDDESTROY);
10175 }
10176 } else if (sipmethod == SIP_INVITE) {
10177 handle_response_invite(p, resp, rest, req, ignore, seqno);
10178 }
10179 break;
10180 case 481:
10181 if (sipmethod == SIP_INVITE) {
10182
10183 handle_response_invite(p, resp, rest, req, ignore, seqno);
10184 }
10185 break;
10186 default:
10187 if ((resp >= 100) && (resp < 200)) {
10188 if (sipmethod == SIP_INVITE && !ignore)
10189 sip_cancel_destroy(p);
10190
10191 }
10192 if ((resp >= 300) && (resp < 700)) {
10193 if ((option_verbose > 2) && (resp != 487))
10194 ast_verbose(VERBOSE_PREFIX_3 "Incoming call: Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr));
10195 switch(resp) {
10196 case 488:
10197 case 603:
10198 case 500:
10199 case 503:
10200
10201 if (sipmethod == SIP_INVITE && !ignore) {
10202 sip_cancel_destroy(p);
10203 }
10204 break;
10205 }
10206 }
10207 break;
10208 }
10209 }
10210 }
10211
10212 struct sip_dual {
10213 struct ast_channel *chan1;
10214 struct ast_channel *chan2;
10215 struct sip_request req;
10216 };
10217
10218
10219 static void *sip_park_thread(void *stuff)
10220 {
10221 struct ast_channel *chan1, *chan2;
10222 struct sip_dual *d;
10223 struct sip_request req;
10224 int ext;
10225 int res;
10226 d = stuff;
10227 chan1 = d->chan1;
10228 chan2 = d->chan2;
10229 copy_request(&req, &d->req);
10230 free(d);
10231 ast_mutex_lock(&chan1->lock);
10232 ast_do_masquerade(chan1);
10233 ast_mutex_unlock(&chan1->lock);
10234 res = ast_park_call(chan1, chan2, 0, &ext);
10235
10236 ast_hangup(chan2);
10237 ast_log(LOG_DEBUG, "Parked on extension '%d'\n", ext);
10238 return NULL;
10239 }
10240
10241
10242 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req)
10243 {
10244 struct sip_dual *d;
10245 struct ast_channel *chan1m, *chan2m;
10246 pthread_t th;
10247 chan1m = ast_channel_alloc(0);
10248 chan2m = ast_channel_alloc(0);
10249 if ((!chan2m) || (!chan1m)) {
10250 if (chan1m)
10251 ast_hangup(chan1m);
10252 if (chan2m)
10253 ast_hangup(chan2m);
10254 return -1;
10255 }
10256 snprintf(chan1m->name, sizeof(chan1m->name), "Parking/%s", chan1->name);
10257
10258 chan1m->readformat = chan1->readformat;
10259 chan1m->writeformat = chan1->writeformat;
10260 ast_channel_masquerade(chan1m, chan1);
10261
10262 ast_copy_string(chan1m->context, chan1->context, sizeof(chan1m->context));
10263 ast_copy_string(chan1m->exten, chan1->exten, sizeof(chan1m->exten));
10264 chan1m->priority = chan1->priority;
10265
10266
10267
10268 snprintf(chan2m->name, sizeof (chan2m->name), "SIPPeer/%s",chan2->name);
10269
10270 chan2m->readformat = chan2->readformat;
10271 chan2m->writeformat = chan2->writeformat;
10272 ast_channel_masquerade(chan2m, chan2);
10273
10274 ast_copy_string(chan2m->context, chan2->context, sizeof(chan2m->context));
10275 ast_copy_string(chan2m->exten, chan2->exten, sizeof(chan2m->exten));
10276 chan2m->priority = chan2->priority;
10277 ast_mutex_lock(&chan2m->lock);
10278 if (ast_do_masquerade(chan2m)) {
10279 ast_log(LOG_WARNING, "Masquerade failed :(\n");
10280 ast_mutex_unlock(&chan2m->lock);
10281 ast_hangup(chan2m);
10282 return -1;
10283 }
10284 ast_mutex_unlock(&chan2m->lock);
10285 d = malloc(sizeof(struct sip_dual));
10286 if (d) {
10287 memset(d, 0, sizeof(*d));
10288
10289 copy_request(&d->req, req);
10290 d->chan1 = chan1m;
10291 d->chan2 = chan2m;
10292 if (!ast_pthread_create(&th, NULL, sip_park_thread, d))
10293 return 0;
10294 free(d);
10295 }
10296 return -1;
10297 }
10298
10299
10300 static void ast_quiet_chan(struct ast_channel *chan)
10301 {
10302 if (chan && chan->_state == AST_STATE_UP) {
10303 if (chan->generatordata)
10304 ast_deactivate_generator(chan);
10305 }
10306 }
10307
10308
10309 static int attempt_transfer(struct sip_pvt *p1, struct sip_pvt *p2)
10310 {
10311 int res = 0;
10312 struct ast_channel
10313 *chana = NULL,
10314 *chanb = NULL,
10315 *bridgea = NULL,
10316 *bridgeb = NULL,
10317 *peera = NULL,
10318 *peerb = NULL,
10319 *peerc = NULL,
10320 *peerd = NULL;
10321
10322 if (!p1->owner || !p2->owner) {
10323 ast_log(LOG_WARNING, "Transfer attempted without dual ownership?\n");
10324 return -1;
10325 }
10326 chana = p1->owner;
10327 chanb = p2->owner;
10328 bridgea = ast_bridged_channel(chana);
10329 bridgeb = ast_bridged_channel(chanb);
10330
10331 if (bridgea) {
10332 peera = chana;
10333 peerb = chanb;
10334 peerc = bridgea;
10335 peerd = bridgeb;
10336 } else if (bridgeb) {
10337 peera = chanb;
10338 peerb = chana;
10339 peerc = bridgeb;
10340 peerd = bridgea;
10341 }
10342
10343 if (peera && peerb && peerc && (peerb != peerc)) {
10344 ast_quiet_chan(peera);
10345 ast_quiet_chan(peerb);
10346 ast_quiet_chan(peerc);
10347 ast_quiet_chan(peerd);
10348
10349 if (peera->cdr && peerb->cdr) {
10350 peerb->cdr = ast_cdr_append(peerb->cdr, peera->cdr);
10351 } else if (peera->cdr) {
10352 peerb->cdr = peera->cdr;
10353 }
10354 peera->cdr = NULL;
10355
10356 if (peerb->cdr && peerc->cdr) {
10357 peerb->cdr = ast_cdr_append(peerb->cdr, peerc->cdr);
10358 } else if (peerc->cdr) {
10359 peerb->cdr = peerc->cdr;
10360 }
10361 peerc->cdr = NULL;
10362
10363 if (ast_channel_masquerade(peerb, peerc)) {
10364 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
10365 res = -1;
10366 }
10367 return res;
10368 } else {
10369 ast_log(LOG_NOTICE, "Transfer attempted with no appropriate bridged calls to transfer\n");
10370 if (chana)
10371 ast_softhangup_nolock(chana, AST_SOFTHANGUP_DEV);
10372 if (chanb)
10373 ast_softhangup_nolock(chanb, AST_SOFTHANGUP_DEV);
10374 return -1;
10375 }
10376 return 0;
10377 }
10378
10379
10380 static char *gettag(struct sip_request *req, char *header, char *tagbuf, int tagbufsize)
10381 {
10382
10383 char *thetag, *sep;
10384
10385
10386 if (!tagbuf)
10387 return NULL;
10388 tagbuf[0] = '\0';
10389 thetag = get_header(req, header);
10390 thetag = strcasestr(thetag, ";tag=");
10391 if (thetag) {
10392 thetag += 5;
10393 ast_copy_string(tagbuf, thetag, tagbufsize);
10394 sep = strchr(tagbuf, ';');
10395 if (sep)
10396 *sep = '\0';
10397 }
10398 return thetag;
10399 }
10400
10401
10402 static int handle_request_options(struct sip_pvt *p, struct sip_request *req, int debug)
10403 {
10404 int res;
10405
10406 res = get_destination(p, req);
10407 build_contact(p);
10408
10409 if (ast_strlen_zero(p->context))
10410 strcpy(p->context, default_context);
10411 if (res < 0)
10412 transmit_response_with_allow(p, "404 Not Found", req, 0);
10413 else if (res > 0)
10414 transmit_response_with_allow(p, "484 Address Incomplete", req, 0);
10415 else
10416 transmit_response_with_allow(p, "200 OK", req, 0);
10417
10418
10419 if (!p->lastinvite)
10420 ast_set_flag(p, SIP_NEEDDESTROY);
10421
10422 return res;
10423 }
10424
10425
10426 static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, struct sockaddr_in *sin, int *recount, char *e)
10427 {
10428 int res = 1;
10429 struct ast_channel *c=NULL;
10430 int gotdest;
10431 struct ast_frame af = { AST_FRAME_NULL, };
10432 char *supported;
10433 char *required;
10434 unsigned int required_profile = 0;
10435
10436
10437 if (!p->sipoptions) {
10438 supported = get_header(req, "Supported");
10439 if (supported)
10440 parse_sip_options(p, supported);
10441 }
10442 required = get_header(req, "Require");
10443 if (!ast_strlen_zero(required)) {
10444 required_profile = parse_sip_options(NULL, required);
10445 if (required_profile) {
10446
10447 transmit_response_with_unsupported(p, "420 Bad extension", req, required);
10448 if (!p->lastinvite)
10449 ast_set_flag(p, SIP_NEEDDESTROY);
10450 return -1;
10451
10452 }
10453 }
10454
10455
10456
10457
10458 if (ast_test_flag(p, SIP_OUTGOING) && p->owner && (p->owner->_state != AST_STATE_UP)) {
10459
10460
10461
10462 transmit_response(p, "482 Loop Detected", req);
10463
10464 return 0;
10465 }
10466 if (!ignore) {
10467
10468 if (debug)
10469 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
10470 sip_cancel_destroy(p);
10471
10472 ast_clear_flag(p, SIP_OUTGOING);
10473
10474 p->pendinginvite = seqno;
10475 copy_request(&p->initreq, req);
10476 check_via(p, req);
10477 if (p->owner) {
10478
10479 if (find_sdp(req)) {
10480 if (process_sdp(p, req)) {
10481 transmit_response(p, "488 Not acceptable here", req);
10482 if (!p->lastinvite)
10483 ast_set_flag(p, SIP_NEEDDESTROY);
10484 return -1;
10485 }
10486 } else {
10487 p->jointcapability = p->capability;
10488 ast_log(LOG_DEBUG, "Hm.... No sdp for the moment\n");
10489 }
10490 }
10491 } else if (debug)
10492 ast_verbose("Ignoring this INVITE request\n");
10493 if (!p->lastinvite && !ignore && !p->owner) {
10494
10495 res = check_user(p, req, SIP_INVITE, e, 1, sin, ignore);
10496
10497 if (res > 0)
10498 return 0;
10499 if (res < 0) {
10500 if (res == -4) {
10501 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
10502 transmit_fake_auth_response(p, req, p->randdata, sizeof(p->randdata), 1);
10503 } else {
10504 ast_log(LOG_NOTICE, "Failed to authenticate user %s\n", get_header(req, "From"));
10505 if (ignore)
10506 transmit_response(p, "403 Forbidden", req);
10507 else
10508 transmit_response_reliable(p, "403 Forbidden", req, 1);
10509 }
10510 ast_set_flag(p, SIP_NEEDDESTROY);
10511 p->theirtag[0] = '\0';
10512 return 0;
10513 }
10514
10515 if (find_sdp(req)) {
10516 if (process_sdp(p, req)) {
10517 transmit_response(p, "488 Not acceptable here", req);
10518 ast_set_flag(p, SIP_NEEDDESTROY);
10519 return -1;
10520 }
10521 } else {
10522 p->jointcapability = p->capability;
10523 ast_log(LOG_DEBUG, "Hm.... No sdp for the moment\n");
10524 }
10525
10526 if (p->owner)
10527 ast_queue_frame(p->owner, &af);
10528
10529 if (ast_strlen_zero(p->context))
10530 strcpy(p->context, default_context);
10531
10532 ast_log(LOG_DEBUG, "Checking SIP call limits for device %s\n", p->username);
10533 res = update_call_counter(p, INC_CALL_LIMIT);
10534 if (res) {
10535 if (res < 0) {
10536 ast_log(LOG_NOTICE, "Failed to place call for user %s, too many calls\n", p->username);
10537 if (ignore)
10538 transmit_response(p, "480 Temporarily Unavailable (Call limit)", req);
10539 else
10540 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req, 1);
10541 ast_set_flag(p, SIP_NEEDDESTROY);
10542 }
10543 return 0;
10544 }
10545
10546 gotdest = get_destination(p, NULL);
10547
10548 get_rdnis(p, NULL);
10549 extract_uri(p, req);
10550 build_contact(p);
10551
10552 if (gotdest) {
10553 if (gotdest < 0) {
10554 if (ignore)
10555 transmit_response(p, "404 Not Found", req);
10556 else
10557 transmit_response_reliable(p, "404 Not Found", req, 1);
10558 update_call_counter(p, DEC_CALL_LIMIT);
10559 } else {
10560 if (ignore)
10561 transmit_response(p, "484 Address Incomplete", req);
10562 else
10563 transmit_response_reliable(p, "484 Address Incomplete", req, 1);
10564 update_call_counter(p, DEC_CALL_LIMIT);
10565 }
10566 ast_set_flag(p, SIP_NEEDDESTROY);
10567 } else {
10568
10569 if (ast_strlen_zero(p->exten))
10570 ast_copy_string(p->exten, "s", sizeof(p->exten));
10571
10572 make_our_tag(p->tag, sizeof(p->tag));
10573
10574 c = sip_new(p, AST_STATE_DOWN, ast_strlen_zero(p->username) ? NULL : p->username );
10575 *recount = 1;
10576
10577 build_route(p, req, 0);
10578 if (c) {
10579
10580 ast_mutex_lock(&c->lock);
10581 }
10582 }
10583
10584 } else {
10585 if (option_debug > 1 && sipdebug)
10586 ast_log(LOG_DEBUG, "Got a SIP re-invite for call %s\n", p->callid);
10587 c = p->owner;
10588 }
10589 if (!ignore && p)
10590 p->lastinvite = seqno;
10591 if (c) {
10592 #ifdef OSP_SUPPORT
10593 ast_channel_setwhentohangup (c, p->osptimelimit);
10594 #endif
10595 switch(c->_state) {
10596 case AST_STATE_DOWN:
10597 transmit_response(p, "100 Trying", req);
10598 ast_setstate(c, AST_STATE_RING);
10599 if (strcmp(p->exten, ast_pickup_ext())) {
10600 enum ast_pbx_result res;
10601
10602 res = ast_pbx_start(c);
10603
10604 switch (res) {
10605 case AST_PBX_FAILED:
10606 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
10607 if (ignore)
10608 transmit_response(p, "503 Unavailable", req);
10609 else
10610 transmit_response_reliable(p, "503 Unavailable", req, 1);
10611 break;
10612 case AST_PBX_CALL_LIMIT:
10613 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
10614 if (ignore)
10615 transmit_response(p, "480 Temporarily Unavailable", req);
10616 else
10617 transmit_response_reliable(p, "480 Temporarily Unavailable", req, 1);
10618 break;
10619 case AST_PBX_SUCCESS:
10620
10621 break;
10622 }
10623
10624 if (res) {
10625 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
10626
10627 ast_mutex_unlock(&c->lock);
10628 ast_mutex_unlock(&p->lock);
10629 ast_hangup(c);
10630 ast_mutex_lock(&p->lock);
10631 c = NULL;
10632 }
10633 } else {
10634 ast_mutex_unlock(&c->lock);
10635 if (ast_pickup_call(c)) {
10636 ast_log(LOG_NOTICE, "Nothing to pick up\n");
10637 if (ignore)
10638 transmit_response(p, "503 Unavailable", req);
10639 else
10640 transmit_response_reliable(p, "503 Unavailable", req, 1);
10641 ast_set_flag(p, SIP_ALREADYGONE);
10642
10643 ast_mutex_unlock(&p->lock);
10644 ast_hangup(c);
10645 ast_mutex_lock(&p->lock);
10646 c = NULL;
10647 } else {
10648 ast_mutex_unlock(&p->lock);
10649 ast_setstate(c, AST_STATE_DOWN);
10650 ast_hangup(c);
10651 ast_mutex_lock(&p->lock);
10652 c = NULL;
10653 }
10654 }
10655 break;
10656 case AST_STATE_RING:
10657 transmit_response(p, "100 Trying", req);
10658 break;
10659 case AST_STATE_RINGING:
10660 transmit_response(p, "180 Ringing", req);
10661 break;
10662 case AST_STATE_UP:
10663 transmit_response_with_sdp(p, "200 OK", req, 1);
10664 break;
10665 default:
10666 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
10667 transmit_response(p, "100 Trying", req);
10668 }
10669 } else {
10670 if (p && !ast_test_flag(p, SIP_NEEDDESTROY) && !ignore) {
10671 if (!p->jointcapability) {
10672 if (ignore)
10673 transmit_response(p, "488 Not Acceptable Here (codec error)", req);
10674 else
10675 transmit_response_reliable(p, "488 Not Acceptable Here (codec error)", req, 1);
10676 ast_set_flag(p, SIP_NEEDDESTROY);
10677 } else {
10678 ast_log(LOG_NOTICE, "Unable to create/find channel\n");
10679 if (ignore)
10680 transmit_response(p, "503 Unavailable", req);
10681 else
10682 transmit_response_reliable(p, "503 Unavailable", req, 1);
10683 ast_set_flag(p, SIP_NEEDDESTROY);
10684 }
10685 }
10686 }
10687 return res;
10688 }
10689
10690
10691 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, int seqno, int *nounlock)
10692 {
10693 struct ast_channel *c=NULL;
10694 int res;
10695 struct ast_channel *transfer_to;
10696
10697 if (option_debug > 2)
10698 ast_log(LOG_DEBUG, "SIP call transfer received for call %s (REFER)!\n", p->callid);
10699 if (ast_strlen_zero(p->context))
10700 strcpy(p->context, default_context);
10701 res = get_refer_info(p, req);
10702 if (res < 0)
10703 transmit_response(p, "603 Declined", req);
10704 else if (res > 0)
10705 transmit_response(p, "484 Address Incomplete", req);
10706 else {
10707 int nobye = 0;
10708 if (!ignore) {
10709 if (p->refer_call) {
10710 ast_log(LOG_DEBUG,"202 Accepted (supervised)\n");
10711 attempt_transfer(p, p->refer_call);
10712 if (p->refer_call->owner)
10713 ast_mutex_unlock(&p->refer_call->owner->lock);
10714 ast_mutex_unlock(&p->refer_call->lock);
10715 p->refer_call = NULL;
10716 ast_set_flag(p, SIP_GOTREFER);
10717 } else {
10718 ast_log(LOG_DEBUG,"202 Accepted (blind)\n");
10719 c = p->owner;
10720 if (c) {
10721 transfer_to = ast_bridged_channel(c);
10722 if (transfer_to) {
10723 ast_log(LOG_DEBUG, "Got SIP blind transfer, applying to '%s'\n", transfer_to->name);
10724 ast_moh_stop(transfer_to);
10725 if (!strcmp(p->refer_to, ast_parking_ext())) {
10726
10727
10728 *nounlock = 1;
10729 ast_mutex_unlock(&c->lock);
10730 sip_park(transfer_to, c, req);
10731 nobye = 1;
10732 } else {
10733
10734
10735 *nounlock = 1;
10736 ast_mutex_unlock(&c->lock);
10737 ast_async_goto(transfer_to,p->context, p->refer_to,1);
10738 }
10739 } else {
10740 ast_log(LOG_DEBUG, "Got SIP blind transfer but nothing to transfer to.\n");
10741 ast_queue_hangup(p->owner);
10742 }
10743 }
10744 ast_set_flag(p, SIP_GOTREFER);
10745 }
10746 transmit_response(p, "202 Accepted", req);
10747 transmit_notify_with_sipfrag(p, seqno);
10748
10749 if (!nobye) {
10750 transmit_request_with_auth(p, SIP_BYE, 0, 1, 1);
10751 ast_set_flag(p, SIP_ALREADYGONE);
10752 }
10753 }
10754 }
10755 return res;
10756 }
10757
10758
10759 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req, int debug, int ignore)
10760 {
10761
10762 check_via(p, req);
10763 ast_set_flag(p, SIP_ALREADYGONE);
10764 if (p->rtp) {
10765
10766 ast_rtp_stop(p->rtp);
10767 }
10768 if (p->vrtp) {
10769
10770 ast_rtp_stop(p->vrtp);
10771 }
10772 if (p->owner)
10773 ast_queue_hangup(p->owner);
10774 else
10775 ast_set_flag(p, SIP_NEEDDESTROY);
10776 if (p->initreq.len > 0) {
10777 if (!ignore)
10778 transmit_response_reliable(p, "487 Request Terminated", &p->initreq, 1);
10779 transmit_response(p, "200 OK", req);
10780 return 1;
10781 } else {
10782 transmit_response(p, "481 Call Leg Does Not Exist", req);
10783 return 0;
10784 }
10785 }
10786
10787
10788 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req, int debug, int ignore)
10789 {
10790 struct ast_channel *c=NULL;
10791 int res;
10792 struct ast_channel *bridged_to;
10793 char iabuf[INET_ADDRSTRLEN];
10794
10795 if (p->pendinginvite && !ast_test_flag(p, SIP_OUTGOING) && !ignore)
10796 transmit_response_reliable(p, "487 Request Terminated", &p->initreq, 1);
10797
10798 copy_request(&p->initreq, req);
10799 check_via(p, req);
10800 ast_set_flag(p, SIP_ALREADYGONE);
10801 if (p->rtp) {
10802
10803 ast_rtp_stop(p->rtp);
10804 }
10805 if (p->vrtp) {
10806
10807 ast_rtp_stop(p->vrtp);
10808 }
10809 if (!ast_strlen_zero(get_header(req, "Also"))) {
10810 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
10811 ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr));
10812 if (ast_strlen_zero(p->context))
10813 strcpy(p->context, default_context);
10814 res = get_also_info(p, req);
10815 if (!res) {
10816 c = p->owner;
10817 if (c) {
10818 bridged_to = ast_bridged_channel(c);
10819 if (bridged_to) {
10820
10821 ast_moh_stop(bridged_to);
10822 ast_async_goto(bridged_to, p->context, p->refer_to,1);
10823 } else
10824 ast_queue_hangup(p->owner);
10825 }
10826 } else {
10827 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->recv.sin_addr));
10828 if (p->owner)
10829 ast_queue_hangup(p->owner);
10830 }
10831 } else if (p->owner) {
10832 ast_queue_hangup(p->owner);
10833 if (option_debug > 2)
10834 ast_log(LOG_DEBUG, "Received bye, issuing owner hangup\n.");
10835 } else {
10836 ast_set_flag(p, SIP_NEEDDESTROY);
10837 if (option_debug > 2)
10838 ast_log(LOG_DEBUG, "Received bye, no owner, selfdestruct soon.\n.");
10839 }
10840 transmit_response(p, "200 OK", req);
10841
10842 return 1;
10843 }
10844
10845
10846 static int handle_request_message(struct sip_pvt *p, struct sip_request *req, int debug, int ignore)
10847 {
10848 if (!ignore) {
10849 if (debug)
10850 ast_verbose("Receiving message!\n");
10851 receive_message(p, req);
10852 } else {
10853 transmit_response(p, "202 Accepted", req);
10854 }
10855 return 1;
10856 }
10857
10858 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, struct sockaddr_in *sin, int seqno, char *e)
10859 {
10860 int gotdest;
10861 int res = 0;
10862 int firststate = AST_EXTENSION_REMOVED;
10863
10864 if (p->initreq.headers) {
10865
10866 if (p->initreq.method != SIP_SUBSCRIBE) {
10867
10868
10869 transmit_response(p, "403 Forbidden (within dialog)", req);
10870
10871 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);
10872 return 0;
10873 } else {
10874 if (debug)
10875 ast_log(LOG_DEBUG, "Got a re-subscribe on existing subscription %s\n", p->callid);
10876 }
10877 }
10878 if (!ignore && !p->initreq.headers) {
10879
10880 if (debug)
10881 ast_verbose("Using latest SUBSCRIBE request as basis request\n");
10882
10883 ast_clear_flag(p, SIP_OUTGOING);
10884 copy_request(&p->initreq, req);
10885 check_via(p, req);
10886 } else if (debug && ignore)
10887 ast_verbose("Ignoring this SUBSCRIBE request\n");
10888
10889 if (!p->lastinvite) {
10890 char mailboxbuf[256]="";
10891 int found = 0;
10892 char *mailbox = NULL;
10893 int mailboxsize = 0;
10894 char *eventparam;
10895
10896 char *event = get_header(req, "Event");
10897 char *accept = get_header(req, "Accept");
10898
10899
10900 eventparam = strchr(event, ';');
10901 if (eventparam) {
10902 *eventparam = '\0';
10903 eventparam++;
10904 }
10905
10906 if (!strcmp(event, "message-summary") && !strcmp(accept, "application/simple-message-summary")) {
10907 mailbox = mailboxbuf;
10908 mailboxsize = sizeof(mailboxbuf);
10909 }
10910
10911 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, ignore, mailbox, mailboxsize);
10912
10913 if (res > 0)
10914 return 0;
10915 if (res < 0) {
10916 if (res == -4) {
10917 ast_log(LOG_NOTICE, "Sending fake auth rejection for user %s\n", get_header(req, "From"));
10918 transmit_fake_auth_response(p, req, p->randdata, sizeof(p->randdata), 1);
10919 } else {
10920 ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
10921 if (ignore)
10922 transmit_response(p, "403 Forbidden", req);
10923 else
10924 transmit_response_reliable(p, "403 Forbidden", req, 1);
10925 }
10926 ast_set_flag(p, SIP_NEEDDESTROY);
10927 return 0;
10928 }
10929 gotdest = get_destination(p, NULL);
10930
10931
10932
10933
10934
10935 if (!ast_strlen_zero(p->subscribecontext))
10936 ast_copy_string(p->context, p->subscribecontext, sizeof(p->context));
10937 else if (ast_strlen_zero(p->context))
10938 strcpy(p->context, default_context);
10939
10940 build_contact(p);
10941 if (gotdest) {
10942 if (gotdest < 0)
10943 transmit_response(p, "404 Not Found", req);
10944 else
10945 transmit_response(p, "484 Address Incomplete", req);
10946 ast_set_flag(p, SIP_NEEDDESTROY);
10947 } else {
10948
10949
10950 if (ast_strlen_zero(p->tag))
10951 make_our_tag(p->tag, sizeof(p->tag));
10952
10953 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
10954
10955
10956 if (strstr(accept, "application/pidf+xml")) {
10957 p->subscribed = PIDF_XML;
10958 } else if (strstr(accept, "application/dialog-info+xml")) {
10959 p->subscribed = DIALOG_INFO_XML;
10960
10961 } else if (strstr(accept, "application/cpim-pidf+xml")) {
10962 p->subscribed = CPIM_PIDF_XML;
10963 } else if (strstr(accept, "application/xpidf+xml")) {
10964 p->subscribed = XPIDF_XML;
10965 } else if (strstr(p->useragent, "Polycom")) {
10966 p->subscribed = XPIDF_XML;
10967 } else {
10968
10969 transmit_response(p, "489 Bad Event", req);
10970 ast_set_flag(p, SIP_NEEDDESTROY);
10971 return 0;
10972 }
10973 } else if (!strcmp(event, "message-summary") && !strcmp(accept, "application/simple-message-summary")) {
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988 if (!ast_strlen_zero(mailbox)) {
10989 found++;
10990 }
10991
10992 if (found){
10993 transmit_response(p, "200 OK", req);
10994 ast_set_flag(p, SIP_NEEDDESTROY);
10995 } else {
10996 transmit_response(p, "404 Not found", req);
10997 ast_set_flag(p, SIP_NEEDDESTROY);
10998 }
10999 return 0;
11000 } else {
11001 transmit_response(p, "489 Bad Event", req);
11002 if (option_debug > 1)
11003 ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
11004 ast_set_flag(p, SIP_NEEDDESTROY);
11005 return 0;
11006 }
11007 if (p->subscribed != NONE)
11008 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
11009 }
11010 }
11011
11012 if (!ignore && p)
11013 p->lastinvite = seqno;
11014 if (p && !ast_test_flag(p, SIP_NEEDDESTROY)) {
11015 p->expiry = atoi(get_header(req, "Expires"));
11016
11017
11018 if (p->subscribed == DIALOG_INFO_XML) {
11019 if (p->expiry > max_expiry)
11020 p->expiry = max_expiry;
11021 }
11022 if (sipdebug || option_debug > 1)
11023 ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
11024 if (p->autokillid > -1)
11025 sip_cancel_destroy(p);
11026 sip_scheddestroy(p, (p->expiry + 10) * 1000);
11027
11028 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
11029 char iabuf[INET_ADDRSTRLEN];
11030
11031 ast_log(LOG_ERROR, "Got SUBSCRIBE for extension %s@%s from %s, but there is no hint for that extension\n", p->exten, p->context, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr));
11032 transmit_response(p, "404 Not found", req);
11033 ast_set_flag(p, SIP_NEEDDESTROY);
11034 return 0;
11035 } else {
11036 struct sip_pvt *p_old;
11037
11038 transmit_response(p, "200 OK", req);
11039 transmit_state_notify(p, firststate, 1, 1);
11040 append_history(p, "Subscribestatus", ast_extension_state2str(firststate));
11041
11042
11043
11044
11045
11046
11047 ast_mutex_lock(&iflock);
11048 for (p_old = iflist; p_old; p_old = p_old->next) {
11049 if (p_old == p)
11050 continue;
11051 if (p_old->initreq.method != SIP_SUBSCRIBE)
11052 continue;
11053 if (p_old->subscribed == NONE)
11054 continue;
11055 ast_mutex_lock(&p_old->lock);
11056 if (!strcmp(p_old->username, p->username)) {
11057 if (!strcmp(p_old->exten, p->exten) &&
11058 !strcmp(p_old->context, p->context)) {
11059 ast_set_flag(p_old, SIP_NEEDDESTROY);
11060 ast_mutex_unlock(&p_old->lock);
11061 break;
11062 }
11063 }
11064 ast_mutex_unlock(&p_old->lock);
11065 }
11066 ast_mutex_unlock(&iflock);
11067 }
11068 if (!p->expiry)
11069 ast_set_flag(p, SIP_NEEDDESTROY);
11070 }
11071 return 1;
11072 }
11073
11074
11075 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, int debug, int ignore, struct sockaddr_in *sin, char *e)
11076 {
11077 int res = 0;
11078 char iabuf[INET_ADDRSTRLEN];
11079
11080
11081 if (debug)
11082 ast_verbose("Using latest REGISTER request as basis request\n");
11083 copy_request(&p->initreq, req);
11084 check_via(p, req);
11085 if ((res = register_verify(p, sin, req, e, ignore)) < 0)
11086 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n", get_header(req, "To"), ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), (res == -1) ? "Wrong password" : (res == -2 ? "Username/auth name mismatch" : "Not a local SIP domain"));
11087 if (res < 1) {
11088
11089
11090 sip_scheddestroy(p, 15*1000);
11091 }
11092 return res;
11093 }
11094
11095
11096
11097 static int handle_request(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *recount, int *nounlock)
11098 {
11099
11100
11101 struct sip_request resp;
11102 char *cmd;
11103 char *cseq;
11104 char *useragent;
11105 int seqno;
11106 int len;
11107 int ignore=0;
11108 int respid;
11109 int res = 0;
11110 char iabuf[INET_ADDRSTRLEN];
11111 int debug = sip_debug_test_pvt(p);
11112 char *e;
11113 int error = 0;
11114
11115
11116 memset(&resp, 0, sizeof(resp));
11117
11118
11119 cseq = get_header(req, "Cseq");
11120 cmd = req->header[0];
11121
11122
11123 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq)) {
11124 ast_log(LOG_ERROR, "Missing Cseq. Dropping this SIP message, it's incomplete.\n");
11125 error = 1;
11126 }
11127 if (!error && sscanf(cseq, "%d%n", &seqno, &len) != 1) {
11128 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
11129 error = 1;
11130 }
11131 if (error) {
11132 if (!p->initreq.header)
11133 ast_set_flag(p, SIP_NEEDDESTROY);
11134 return -1;
11135 }
11136
11137
11138 cmd = req->rlPart1;
11139 e = req->rlPart2;
11140
11141
11142 useragent = get_header(req, "User-Agent");
11143 if (!ast_strlen_zero(useragent))
11144 ast_copy_string(p->useragent, useragent, sizeof(p->useragent));
11145
11146
11147 if (req->method == SIP_RESPONSE) {
11148
11149 if (!p->initreq.headers) {
11150 ast_log(LOG_DEBUG, "That's odd... Got a response on a call we dont know about. Cseq %d Cmd %s\n", seqno, cmd);
11151 ast_set_flag(p, SIP_NEEDDESTROY);
11152 return 0;
11153 } else if (p->ocseq && (p->ocseq < seqno)) {
11154 ast_log(LOG_DEBUG, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
11155 return -1;
11156 } else if (p->ocseq && (p->ocseq != seqno)) {
11157
11158
11159 ignore=1;
11160 }
11161
11162 e = ast_skip_blanks(e);
11163 if (sscanf(e, "%d %n", &respid, &len) != 1) {
11164 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
11165 } else {
11166
11167 if ((respid == 200) || ((respid >= 300) && (respid <= 399)))
11168 extract_uri(p, req);
11169 handle_response(p, respid, e + len, req, ignore, seqno);
11170 }
11171 return 0;
11172 }
11173
11174
11175
11176
11177
11178 p->method = req->method;
11179 if (option_debug > 2)
11180 ast_log(LOG_DEBUG, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
11181
11182 if (p->icseq && (p->icseq > seqno)) {
11183 if (option_debug)
11184 ast_log(LOG_DEBUG, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
11185 if (req->method != SIP_ACK)
11186 transmit_response(p, "503 Server error", req);
11187 return -1;
11188 } else if (p->icseq && (p->icseq == seqno) && req->method != SIP_ACK &&(p->method != SIP_CANCEL|| ast_test_flag(p, SIP_ALREADYGONE))) {
11189
11190
11191
11192 ignore=2;
11193 if (option_debug > 2)
11194 ast_log(LOG_DEBUG, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
11195 }
11196
11197 if (seqno >= p->icseq)
11198
11199
11200
11201 p->icseq = seqno;
11202
11203
11204 if (ast_strlen_zero(p->theirtag)) {
11205 gettag(req, "From", p->theirtag, sizeof(p->theirtag));
11206 }
11207 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
11208
11209 if (pedanticsipchecking) {
11210
11211
11212
11213
11214 if (!p->initreq.headers && ast_test_flag(req, SIP_PKT_WITH_TOTAG)) {
11215
11216 if (!ignore && req->method == SIP_INVITE) {
11217 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req, 1);
11218
11219 } else if (req->method != SIP_ACK) {
11220 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
11221 ast_set_flag(p, SIP_NEEDDESTROY);
11222 }
11223 return res;
11224 }
11225 }
11226
11227 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
11228 transmit_response(p, "503 Server error", req);
11229 ast_set_flag(p, SIP_NEEDDESTROY);
11230 return -1;
11231 }
11232
11233
11234 switch (p->method) {
11235 case SIP_OPTIONS:
11236 res = handle_request_options(p, req, debug);
11237 break;
11238 case SIP_INVITE:
11239 res = handle_request_invite(p, req, debug, ignore, seqno, sin, recount, e);
11240 break;
11241 case SIP_REFER:
11242 res = handle_request_refer(p, req, debug, ignore, seqno, nounlock);
11243 break;
11244 case SIP_CANCEL:
11245 res = handle_request_cancel(p, req, debug, ignore);
11246 break;
11247 case SIP_BYE:
11248 res = handle_request_bye(p, req, debug, ignore);
11249 break;
11250 case SIP_MESSAGE:
11251 res = handle_request_message(p, req, debug, ignore);
11252 break;
11253 case SIP_SUBSCRIBE:
11254 res = handle_request_subscribe(p, req, debug, ignore, sin, seqno, e);
11255 break;
11256 case SIP_REGISTER:
11257 res = handle_request_register(p, req, debug, ignore, sin, e);
11258 break;
11259 case SIP_INFO:
11260 if (!ignore) {
11261 if (debug)
11262 ast_verbose("Receiving INFO!\n");
11263 handle_request_info(p, req);
11264 } else {
11265 transmit_response(p, "200 OK", req);
11266 }
11267 break;
11268 case SIP_NOTIFY:
11269
11270
11271 transmit_response(p, "200 OK", req);
11272 if (!p->lastinvite)
11273 ast_set_flag(p, SIP_NEEDDESTROY);
11274 break;
11275 case SIP_ACK:
11276
11277 if (seqno == p->pendinginvite) {
11278 p->pendinginvite = 0;
11279 __sip_ack(p, seqno, FLAG_RESPONSE, 0);
11280 if (find_sdp(req)) {
11281 if (process_sdp(p, req))
11282 return -1;
11283 }
11284 check_pendings(p);
11285 }
11286 if (!p->lastinvite && ast_strlen_zero(p->randdata))
11287 ast_set_flag(p, SIP_NEEDDESTROY);
11288 break;
11289 default:
11290 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
11291 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
11292 cmd, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr));
11293
11294 if (!p->initreq.headers)
11295 ast_set_flag(p, SIP_NEEDDESTROY);
11296 break;
11297 }
11298 return res;
11299 }
11300
11301
11302
11303 static int sipsock_read(int *id, int fd, short events, void *ignore)
11304 {
11305 struct sip_request req;
11306 struct sockaddr_in sin = { 0, };
11307 struct sip_pvt *p;
11308 int res;
11309 socklen_t len;
11310 int nounlock;
11311 int recount = 0;
11312 char iabuf[INET_ADDRSTRLEN];
11313 unsigned int lockretry = 100;
11314
11315 len = sizeof(sin);
11316 memset(&req, 0, sizeof(req));
11317 res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
11318 if (res < 0) {
11319 #if !defined(__FreeBSD__)
11320 if (errno == EAGAIN)
11321 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
11322 else
11323 #endif
11324 if (errno != ECONNREFUSED)
11325 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
11326 return 1;
11327 }
11328 if (res == sizeof(req.data)) {
11329 ast_log(LOG_DEBUG, "Received packet exceeds buffer. Data is possibly lost\n");
11330 req.data[sizeof(req.data) - 1] = '\0';
11331 } else
11332 req.data[res] = '\0';
11333 req.len = res;
11334 if(sip_debug_test_addr(&sin))
11335 ast_set_flag(&req, SIP_PKT_DEBUG);
11336 if (pedanticsipchecking)
11337 req.len = lws2sws(req.data, req.len);
11338 if (ast_test_flag(&req, SIP_PKT_DEBUG)) {
11339 ast_verbose("\n<-- SIP read from %s:%d: \n%s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), req.data);
11340 }
11341 parse_request(&req);
11342 req.method = find_sip_method(req.rlPart1);
11343 if (ast_test_flag(&req, SIP_PKT_DEBUG)) {
11344 ast_verbose("--- (%d headers %d lines)", req.headers, req.lines);
11345 if (req.headers + req.lines == 0)
11346 ast_verbose(" Nat keepalive ");
11347 ast_verbose("---\n");
11348 }
11349
11350 if (req.headers < 2) {
11351
11352 return 1;
11353 }
11354
11355
11356
11357 retrylock:
11358 ast_mutex_lock(&netlock);
11359 p = find_call(&req, &sin, req.method);
11360 if (p) {
11361
11362 if (p->owner && ast_mutex_trylock(&p->owner->lock)) {
11363 ast_log(LOG_DEBUG, "Failed to grab lock, trying again...\n");
11364 ast_mutex_unlock(&p->lock);
11365 ast_mutex_unlock(&netlock);
11366
11367 usleep(1);
11368 if (--lockretry)
11369 goto retrylock;
11370 }
11371 if (!lockretry) {
11372 ast_log(LOG_ERROR, "We could NOT get the channel lock for %s! \n", p->owner->name);
11373 ast_log(LOG_ERROR, "SIP MESSAGE JUST IGNORED: %s \n", req.data);
11374 ast_log(LOG_ERROR, "BAD! BAD! BAD!\n");
11375 return 1;
11376 }
11377 memcpy(&p->recv, &sin, sizeof(p->recv));
11378 if (recordhistory) {
11379 char tmp[80];
11380
11381 snprintf(tmp, sizeof(tmp), "%s / %s /%s", req.data, get_header(&req, "CSeq"), req.rlPart2);
11382 append_history(p, "Rx", tmp);
11383 }
11384 nounlock = 0;
11385 if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
11386
11387 ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
11388 }
11389
11390 if (p->owner && !nounlock)
11391 ast_mutex_unlock(&p->owner->lock);
11392 ast_mutex_unlock(&p->lock);
11393 }
11394 ast_mutex_unlock(&netlock);
11395 if (recount)
11396 ast_update_use_count();
11397
11398 return 1;
11399 }
11400
11401
11402 static int sip_send_mwi_to_peer(struct sip_peer *peer)
11403 {
11404
11405 struct sip_pvt *p;
11406 int newmsgs, oldmsgs;
11407
11408
11409 ast_app_messagecount(peer->mailbox, &newmsgs, &oldmsgs);
11410
11411 time(&peer->lastmsgcheck);
11412
11413
11414 if (((newmsgs << 8) | (oldmsgs)) == peer->lastmsgssent) {
11415 return 0;
11416 }
11417
11418 p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY);
11419 if (!p) {
11420 ast_log(LOG_WARNING, "Unable to build sip pvt data for MWI\n");
11421 return -1;
11422 }
11423 peer->lastmsgssent = ((newmsgs << 8) | (oldmsgs));
11424 if (create_addr_from_peer(p, peer)) {
11425
11426 sip_destroy(p);
11427 return 0;
11428 }
11429
11430 if (ast_sip_ouraddrfor(&p->sa.sin_addr,&p->ourip))
11431 memcpy(&p->ourip, &__ourip, sizeof(p->ourip));
11432 build_via(p, p->via, sizeof(p->via));
11433 build_callid(p->callid, sizeof(p->callid), p->ourip, p->fromdomain);
11434
11435 ast_set_flag(p, SIP_OUTGOING);
11436 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
11437 sip_scheddestroy(p, 15000);
11438 return 0;
11439 }
11440
11441
11442 static void *do_monitor(void *data)
11443 {
11444 int res;
11445 struct sip_pvt *sip;
11446 struct sip_peer *peer = NULL;
11447 time_t t;
11448 int fastrestart =0;
11449 int lastpeernum = -1;
11450 int curpeernum;
11451 int reloading;
11452
11453
11454 if (sipsock > -1)
11455 ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
11456
11457
11458
11459
11460 for(;;) {
11461
11462 ast_mutex_lock(&sip_reload_lock);
11463 reloading = sip_reloading;
11464 sip_reloading = 0;
11465 ast_mutex_unlock(&sip_reload_lock);
11466 if (reloading) {
11467 if (option_verbose > 0)
11468 ast_verbose(VERBOSE_PREFIX_1 "Reloading SIP\n");
11469 sip_do_reload();
11470 }
11471
11472 ast_mutex_lock(&iflock);
11473 restartsearch:
11474 time(&t);
11475 sip = iflist;
11476
11477
11478
11479
11480 while(!fastrestart && sip) {
11481 ast_mutex_lock(&sip->lock);
11482 if (sip->rtp && sip->owner && (sip->owner->_state == AST_STATE_UP) && !sip->redirip.sin_addr.s_addr) {
11483 if (sip->lastrtptx && sip->rtpkeepalive && t > sip->lastrtptx + sip->rtpkeepalive) {
11484
11485 time(&sip->lastrtptx);
11486 ast_rtp_sendcng(sip->rtp, 0);
11487 }
11488 if (sip->lastrtprx && (sip->rtptimeout || sip->rtpholdtimeout) && t > sip->lastrtprx + sip->rtptimeout) {
11489
11490 struct sockaddr_in sin;
11491 ast_rtp_get_peer(sip->rtp, &sin);
11492 if (sin.sin_addr.s_addr ||
11493 (sip->rtpholdtimeout &&
11494 (t > sip->lastrtprx + sip->rtpholdtimeout))) {
11495
11496 if (sip->rtptimeout) {
11497 while(sip->owner && ast_mutex_trylock(&sip->owner->lock)) {
11498 ast_mutex_unlock(&sip->lock);
11499 usleep(1);
11500 ast_mutex_lock(&sip->lock);
11501 }
11502 if (sip->owner) {
11503 ast_log(LOG_NOTICE, "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n", sip->owner->name, (long)(t - sip->lastrtprx));
11504
11505 ast_softhangup_nolock(sip->owner, AST_SOFTHANGUP_DEV);
11506 ast_mutex_unlock(&sip->owner->lock);
11507
11508
11509
11510
11511 sip->rtptimeout = 0;
11512 sip->rtpholdtimeout = 0;
11513 }
11514 }
11515 }
11516 }
11517 }
11518 if (ast_test_flag(sip, SIP_NEEDDESTROY) && !sip->packets && !sip->owner) {
11519 ast_mutex_unlock(&sip->lock);
11520 __sip_destroy(sip, 1);
11521 goto restartsearch;
11522 }
11523 ast_mutex_unlock(&sip->lock);
11524 sip = sip->next;
11525 }
11526 ast_mutex_unlock(&iflock);
11527
11528
11529 ast_mutex_lock(&monlock);
11530
11531 ast_mutex_lock(&netlock);
11532
11533 ast_mutex_unlock(&netlock);
11534
11535 ast_mutex_unlock(&monlock);
11536 pthread_testcancel();
11537
11538 res = ast_sched_wait(sched);
11539 if ((res < 0) || (res > 1000))
11540 res = 1000;
11541
11542 if (fastrestart)
11543 res = 1;
11544 res = ast_io_wait(io, res);
11545 if (res > 20)
11546 ast_log(LOG_DEBUG, "chan_sip: ast_io_wait ran %d all at once\n", res);
11547 ast_mutex_lock(&monlock);
11548 if (res >= 0) {
11549 res = ast_sched_runq(sched);
11550 if (res >= 20)
11551 ast_log(LOG_DEBUG, "chan_sip: ast_sched_runq ran %d all at once\n", res);
11552 }
11553
11554
11555 time(&t);
11556 fastrestart = 0;
11557 curpeernum = 0;
11558 peer = NULL;
11559 ASTOBJ_CONTAINER_TRAVERSE(&peerl, !peer, do {
11560 if ((curpeernum > lastpeernum) && !ast_strlen_zero(iterator->mailbox) && ((t - iterator->lastmsgcheck) > global_mwitime)) {
11561 fastrestart = 1;
11562 lastpeernum = curpeernum;
11563 peer = ASTOBJ_REF(iterator);
11564 };
11565 curpeernum++;
11566 } while (0)
11567 );
11568 if (peer) {
11569 ASTOBJ_WRLOCK(peer);
11570 sip_send_mwi_to_peer(peer);
11571 ASTOBJ_UNLOCK(peer);
11572 ASTOBJ_UNREF(peer,sip_destroy_peer);
11573 } else {
11574
11575 lastpeernum = -1;
11576 }
11577 ast_mutex_unlock(&monlock);
11578 }
11579
11580 return NULL;
11581
11582 }
11583
11584
11585 static int restart_monitor(void)
11586 {
11587
11588 if (monitor_thread == AST_PTHREADT_STOP)
11589 return 0;
11590 if (ast_mutex_lock(&monlock)) {
11591 ast_log(LOG_WARNING, "Unable to lock monitor\n");
11592 return -1;
11593 }
11594 if (monitor_thread == pthread_self()) {
11595 ast_mutex_unlock(&monlock);
11596 ast_log(LOG_WARNING, "Cannot kill myself\n");
11597 return -1;
11598 }
11599 if (monitor_thread != AST_PTHREADT_NULL) {
11600
11601 pthread_kill(monitor_thread, SIGURG);
11602 } else {
11603
11604 if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
11605 ast_mutex_unlock(&monlock);
11606 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
11607 return -1;
11608 }
11609 }
11610 ast_mutex_unlock(&monlock);
11611 return 0;
11612 }
11613
11614
11615 static int sip_poke_noanswer(void *data)
11616 {
11617 struct sip_peer *peer = data;
11618
11619 peer->pokeexpire = -1;
11620 if (peer->lastms > -1) {
11621 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
11622 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
11623 }
11624 if (peer->call)
11625 sip_destroy(peer->call);
11626 peer->call = NULL;
11627 peer->lastms = -1;
11628 ast_device_state_changed("SIP/%s", peer->name);
11629
11630 peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
11631 return 0;
11632 }
11633
11634
11635
11636
11637 static int sip_poke_peer(struct sip_peer *peer)
11638 {
11639 struct sip_pvt *p;
11640 if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
11641
11642
11643 if (peer->pokeexpire > -1)
11644 ast_sched_del(sched, peer->pokeexpire);
11645 peer->lastms = 0;
11646 peer->pokeexpire = -1;
11647 peer->call = NULL;
11648 return 0;
11649 }
11650 if (peer->call > 0) {
11651 if (sipdebug)
11652 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
11653 sip_destroy(peer->call);
11654 }
11655 p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS);
11656 if (!peer->call) {
11657 ast_log(LOG_WARNING, "Unable to allocate dialog for poking peer '%s'\n", peer->name);
11658 return -1;
11659 }
11660 memcpy(&p->sa, &peer->addr, sizeof(p->sa));
11661 memcpy(&p->recv, &peer->addr, sizeof(p->sa));
11662 ast_copy_flags(p, peer, SIP_FLAGS_TO_COPY);
11663
11664
11665 if (!ast_strlen_zero(peer->fullcontact)) {
11666 ast_copy_string (p->fullcontact, peer->fullcontact, sizeof(p->fullcontact));
11667 }
11668
11669 if (!ast_strlen_zero(peer->tohost))
11670 ast_copy_string(p->tohost, peer->tohost, sizeof(p->tohost));
11671 else
11672 ast_inet_ntoa(p->tohost, sizeof(p->tohost), peer->addr.sin_addr);
11673
11674
11675 if (ast_sip_ouraddrfor(&p->sa.sin_addr,&p->ourip))
11676 memcpy(&p->ourip, &__ourip, sizeof(p->ourip));
11677 build_via(p, p->via, sizeof(p->via));
11678 build_callid(p->callid, sizeof(p->callid), p->ourip, p->fromdomain);
11679
11680 if (peer->pokeexpire > -1)
11681 ast_sched_del(sched, peer->pokeexpire);
11682 p->peerpoke = peer;
11683 ast_set_flag(p, SIP_OUTGOING);
11684 #ifdef VOCAL_DATA_HACK
11685 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
11686 transmit_invite(p, SIP_INVITE, 0, 2);
11687 #else
11688 transmit_invite(p, SIP_OPTIONS, 0, 2);
11689 #endif
11690 gettimeofday(&peer->ps, NULL);
11691 peer->pokeexpire = ast_sched_add(sched, DEFAULT_MAXMS * 2, sip_poke_noanswer, peer);
11692
11693 return 0;
11694 }
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711 static int sip_devicestate(void *data)
11712 {
11713 char *host;
11714 char *tmp;
11715
11716 struct hostent *hp;
11717 struct ast_hostent ahp;
11718 struct sip_peer *p;
11719
11720 int res = AST_DEVICE_INVALID;
11721
11722 host = ast_strdupa(data);
11723 if ((tmp = strchr(host, '@')))
11724 host = tmp + 1;
11725
11726 if (option_debug > 2)
11727 ast_log(LOG_DEBUG, "Checking device state for peer %s\n", host);
11728
11729 if ((p = find_peer(host, NULL, 1))) {
11730 if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
11731
11732
11733 if (p->maxms && (p->lastms > p->maxms)) {
11734 res = AST_DEVICE_UNAVAILABLE;
11735 } else {
11736
11737
11738 if (p->call_limit && (p->inUse == p->call_limit))
11739 res = AST_DEVICE_BUSY;
11740 else if (p->call_limit && p->inUse)
11741 res = AST_DEVICE_INUSE;
11742 else if (p->call_limit)
11743 res = AST_DEVICE_NOT_INUSE;
11744 else
11745 res = AST_DEVICE_UNKNOWN;
11746 }
11747 } else {
11748
11749 res = AST_DEVICE_UNAVAILABLE;
11750 }
11751 ASTOBJ_UNREF(p,sip_destroy_peer);
11752 } else {
11753 hp = ast_gethostbyname(host, &ahp);
11754 if (hp)
11755 res = AST_DEVICE_UNKNOWN;
11756 }
11757
11758 return res;
11759 }
11760
11761
11762
11763 static struct ast_channel *sip_request_call(const char *type, int format, void *data, int *cause)
11764 {
11765 int oldformat;
11766 struct sip_pvt *p;
11767 struct ast_channel *tmpc = NULL;
11768 char *ext, *host;
11769 char tmp[256];
11770 char *dest = data;
11771
11772 oldformat = format;
11773 format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
11774 if (!format) {
11775 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));
11776 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
11777 return NULL;
11778 }
11779 p = sip_alloc(NULL, NULL, 0, SIP_INVITE);
11780 if (!p) {
11781 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory)\n", (char *)data);
11782 *cause = AST_CAUSE_SWITCH_CONGESTION;
11783 return NULL;
11784 }
11785
11786 p->options = calloc(1, sizeof(*p->options));
11787 if (!p->options) {
11788 sip_destroy(p);
11789 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
11790 *cause = AST_CAUSE_SWITCH_CONGESTION;
11791 return NULL;
11792 }
11793
11794 ast_copy_string(tmp, dest, sizeof(tmp));
11795 host = strchr(tmp, '@');
11796 if (host) {
11797 *host = '\0';
11798 host++;
11799 ext = tmp;
11800 } else {
11801 ext = strchr(tmp, '/');
11802 if (ext) {
11803 *ext++ = '\0';
11804 host = tmp;
11805 }
11806 else {
11807 host = tmp;
11808 ext = NULL;
11809 }
11810 }
11811
11812 if (create_addr(p, host)) {
11813 *cause = AST_CAUSE_UNREGISTERED;
11814 sip_destroy(p);
11815 return NULL;
11816 }
11817 if (ast_strlen_zero(p->peername) && ext)
11818 ast_copy_string(p->peername, ext, sizeof(p->peername));
11819
11820 if (ast_sip_ouraddrfor(&p->sa.sin_addr,&p->ourip))
11821 memcpy(&p->ourip, &__ourip, sizeof(p->ourip));
11822 build_via(p, p->via, sizeof(p->via));
11823 build_callid(p->callid, sizeof(p->callid), p->ourip, p->fromdomain);
11824
11825
11826
11827
11828
11829 if (ext) {
11830 ast_copy_string(p->username, ext, sizeof(p->username));
11831 p->fullcontact[0] = 0;
11832 }
11833 #if 0
11834 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
11835 #endif
11836 p->prefcodec = format;
11837 ast_mutex_lock(&p->lock);
11838 tmpc = sip_new(p, AST_STATE_DOWN, host);
11839 ast_mutex_unlock(&p->lock);
11840 if (!tmpc)
11841 sip_destroy(p);
11842 ast_update_use_count();
11843 restart_monitor();
11844 return tmpc;
11845 }
11846
11847
11848 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
11849 {
11850 int res = 0;
11851
11852 if (!strcasecmp(v->name, "trustrpid")) {
11853 ast_set_flag(mask, SIP_TRUSTRPID);
11854 ast_set2_flag(flags, ast_true(v->value), SIP_TRUSTRPID);
11855 res = 1;
11856 } else if (!strcasecmp(v->name, "sendrpid")) {
11857 ast_set_flag(mask, SIP_SENDRPID);
11858 ast_set2_flag(flags, ast_true(v->value), SIP_SENDRPID);
11859 res = 1;
11860 } else if (!strcasecmp(v->name, "useclientcode")) {
11861 ast_set_flag(mask, SIP_USECLIENTCODE);
11862 ast_set2_flag(flags, ast_true(v->value), SIP_USECLIENTCODE);
11863 res = 1;
11864 } else if (!strcasecmp(v->name, "dtmfmode")) {
11865 ast_set_flag(mask, SIP_DTMF);
11866 ast_clear_flag(flags, SIP_DTMF);
11867 if (!strcasecmp(v->value, "inband"))
11868 ast_set_flag(flags, SIP_DTMF_INBAND);
11869 else if (!strcasecmp(v->value, "rfc2833"))
11870 ast_set_flag(flags, SIP_DTMF_RFC2833);
11871 else if (!strcasecmp(v->value, "info"))
11872 ast_set_flag(flags, SIP_DTMF_INFO);
11873 else if (!strcasecmp(v->value, "auto"))
11874 ast_set_flag(flags, SIP_DTMF_AUTO);
11875 else {
11876 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
11877 ast_set_flag(flags, SIP_DTMF_RFC2833);
11878 }
11879 } else if (!strcasecmp(v->name, "nat")) {
11880 ast_set_flag(mask, SIP_NAT);
11881 ast_clear_flag(flags, SIP_NAT);
11882 if (!strcasecmp(v->value, "never"))
11883 ast_set_flag(flags, SIP_NAT_NEVER);
11884 else if (!strcasecmp(v->value, "route"))
11885 ast_set_flag(flags, SIP_NAT_ROUTE);
11886 else if (ast_true(v->value))
11887 ast_set_flag(flags, SIP_NAT_ALWAYS);
11888 else
11889 ast_set_flag(flags, SIP_NAT_RFC3581);
11890 } else if (!strcasecmp(v->name, "canreinvite")) {
11891 ast_set_flag(mask, SIP_REINVITE);
11892 ast_clear_flag(flags, SIP_REINVITE);
11893 if (!strcasecmp(v->value, "update"))
11894 ast_set_flag(flags, SIP_REINVITE_UPDATE | SIP_CAN_REINVITE);
11895 else
11896 ast_set2_flag(flags, ast_true(v->value), SIP_CAN_REINVITE);
11897 } else if (!strcasecmp(v->name, "insecure")) {
11898 ast_set_flag(mask, SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
11899 ast_clear_flag(flags, SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
11900 if (!strcasecmp(v->value, "very"))
11901 ast_set_flag(flags, SIP_INSECURE_PORT | SIP_INSECURE_INVITE);
11902 else if (ast_true(v->value))
11903 ast_set_flag(flags, SIP_INSECURE_PORT);
11904 else if (!ast_false(v->value)) {
11905 char buf[64];
11906 char *word, *next;
11907
11908 ast_copy_string(buf, v->value, sizeof(buf));
11909 next = buf;
11910 while ((word = strsep(&next, ","))) {
11911 if (!strcasecmp(word, "port"))
11912 ast_set_flag(flags, SIP_INSECURE_PORT);
11913 else if (!strcasecmp(word, "invite"))
11914 ast_set_flag(flags, SIP_INSECURE_INVITE);
11915 else
11916 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", v->value, v->lineno);
11917 }
11918 }
11919 } else if (!strcasecmp(v->name, "progressinband")) {
11920 ast_set_flag(mask, SIP_PROG_INBAND);
11921 ast_clear_flag(flags, SIP_PROG_INBAND);
11922 if (ast_true(v->value))
11923 ast_set_flag(flags, SIP_PROG_INBAND_YES);
11924 else if (strcasecmp(v->value, "never"))
11925 ast_set_flag(flags, SIP_PROG_INBAND_NO);
11926 } else if (!strcasecmp(v->name, "allowguest")) {
11927 #ifdef OSP_SUPPORT
11928 if (!strcasecmp(v->value, "osp"))
11929 global_allowguest = 2;
11930 else
11931 #endif
11932 if (ast_true(v->value))
11933 global_allowguest = 1;
11934 else
11935 global_allowguest = 0;
11936 #ifdef OSP_SUPPORT
11937 } else if (!strcasecmp(v->name, "ospauth")) {
11938 ast_set_flag(mask, SIP_OSPAUTH);
11939 ast_clear_flag(flags, SIP_OSPAUTH);
11940 if (!strcasecmp(v->value, "proxy"))
11941 ast_set_flag(flags, SIP_OSPAUTH_PROXY);
11942 else if (!strcasecmp(v->value, "gateway"))
11943 ast_set_flag(flags, SIP_OSPAUTH_GATEWAY);
11944 else if(!strcasecmp (v->value, "exclusive"))
11945 ast_set_flag(flags, SIP_OSPAUTH_EXCLUSIVE);
11946 #endif
11947 } else if (!strcasecmp(v->name, "promiscredir")) {
11948 ast_set_flag(mask, SIP_PROMISCREDIR);
11949 ast_set2_flag(flags, ast_true(v->value), SIP_PROMISCREDIR);
11950 res = 1;
11951 }
11952
11953 return res;
11954 }
11955
11956
11957 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
11958 {
11959 struct domain *d;
11960
11961 if (ast_strlen_zero(domain)) {
11962 ast_log(LOG_WARNING, "Zero length domain.\n");
11963 return 1;
11964 }
11965
11966 d = calloc(1, sizeof(*d));
11967 if (!d) {
11968 ast_log(LOG_ERROR, "Allocation of domain structure failed, Out of memory\n");
11969 return 0;
11970 }
11971
11972 ast_copy_string(d->domain, domain, sizeof(d->domain));
11973
11974 if (!ast_strlen_zero(context))
11975 ast_copy_string(d->context, context, sizeof(d->context));
11976
11977 d->mode = mode;
11978
11979 AST_LIST_LOCK(&domain_list);
11980 AST_LIST_INSERT_TAIL(&domain_list, d, list);
11981 AST_LIST_UNLOCK(&domain_list);
11982
11983 if (sipdebug)
11984 ast_log(LOG_DEBUG, "Added local SIP domain '%s'\n", domain);
11985
11986 return 1;
11987 }
11988
11989
11990 static int check_sip_domain(const char *domain, char *context, size_t len)
11991 {
11992 struct domain *d;
11993 int result = 0;
11994
11995 AST_LIST_LOCK(&domain_list);
11996 AST_LIST_TRAVERSE(&domain_list, d, list) {
11997 if (strcasecmp(d->domain, domain))
11998 continue;
11999
12000 if (len && !ast_strlen_zero(d->context))
12001 ast_copy_string(context, d->context, len);
12002
12003 result = 1;
12004 break;
12005 }
12006 AST_LIST_UNLOCK(&domain_list);
12007
12008 return result;
12009 }
12010
12011
12012 static void clear_sip_domains(void)
12013 {
12014 struct domain *d;
12015
12016 AST_LIST_LOCK(&domain_list);
12017 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
12018 free(d);
12019 AST_LIST_UNLOCK(&domain_list);
12020 }
12021
12022
12023
12024 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno)
12025 {
12026 char authcopy[256];
12027 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
12028 char *stringp;
12029 struct sip_auth *auth;
12030 struct sip_auth *b = NULL, *a = authlist;
12031
12032 if (ast_strlen_zero(configuration))
12033 return authlist;
12034
12035 ast_log(LOG_DEBUG, "Auth config :: %s\n", configuration);
12036
12037 ast_copy_string(authcopy, configuration, sizeof(authcopy));
12038 stringp = authcopy;
12039
12040 username = stringp;
12041 realm = strrchr(stringp, '@');
12042 if (realm) {
12043 *realm = '\0';
12044 realm++;
12045 }
12046 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
12047 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
12048 return authlist;
12049 }
12050 stringp = username;
12051 username = strsep(&stringp, ":");
12052 if (username) {
12053 secret = strsep(&stringp, ":");
12054 if (!secret) {
12055 stringp = username;
12056 md5secret = strsep(&stringp,"#");
12057 }
12058 }
12059 auth = malloc(sizeof(struct sip_auth));
12060 if (auth) {
12061 memset(auth, 0, sizeof(struct sip_auth));
12062 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
12063 ast_copy_string(auth->username, username, sizeof(auth->username));
12064 if (secret)
12065 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
12066 if (md5secret)
12067 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
12068 } else {
12069 ast_log(LOG_ERROR, "Allocation of auth structure failed, Out of memory\n");
12070 return authlist;
12071 }
12072
12073
12074 if (!authlist) {
12075 return auth;
12076 }
12077 while(a) {
12078 b = a;
12079 a = a->next;
12080 }
12081 b->next = auth;
12082
12083 if (option_verbose > 2)
12084 ast_verbose("Added authentication for realm %s\n", realm);
12085
12086 return authlist;
12087
12088 }
12089
12090
12091 static int clear_realm_authentication(struct sip_auth *authlist)
12092 {
12093 struct sip_auth *a = authlist;
12094 struct sip_auth *b;
12095
12096 while (a) {
12097 b = a;
12098 a = a->next;
12099 free(b);
12100 }
12101
12102 return 1;
12103 }
12104
12105
12106 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, char *realm)
12107 {
12108 struct sip_auth *a = authlist;
12109
12110 while (a) {
12111 if (!strcasecmp(a->realm, realm)){
12112 break;
12113 }
12114 a = a->next;
12115 }
12116
12117 return a;
12118 }
12119
12120
12121 static struct sip_user *build_user(const char *name, struct ast_variable *v, int realtime)
12122 {
12123 struct sip_user *user;
12124 int format;
12125 struct ast_ha *oldha = NULL;
12126 char *varname = NULL, *varval = NULL;
12127 struct ast_variable *tmpvar = NULL;
12128 struct ast_flags userflags = {(0)};
12129 struct ast_flags mask = {(0)};
12130
12131
12132 user = (struct sip_user *)malloc(sizeof(struct sip_user));
12133 if (!user) {
12134 return NULL;
12135 }
12136 memset(user, 0, sizeof(struct sip_user));
12137 suserobjs++;
12138 ASTOBJ_INIT(user);
12139 ast_copy_string(user->name, name, sizeof(user->name));
12140 oldha = user->ha;
12141 user->ha = NULL;
12142 ast_copy_flags(user, &global_flags, SIP_FLAGS_TO_COPY);
12143 user->capability = global_capability;
12144 user->prefs = prefs;
12145
12146 strcpy(user->context, default_context);
12147 strcpy(user->language, default_language);
12148 strcpy(user->musicclass, global_musicclass);
12149 while(v) {
12150 if (handle_common_options(&userflags, &mask, v)) {
12151 v = v->next;
12152 continue;
12153 }
12154
12155 if (!strcasecmp(v->name, "context")) {
12156 ast_copy_string(user->context, v->value, sizeof(user->context));
12157 } else if (!strcasecmp(v->name, "subscribecontext")) {
12158 ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
12159 } else if (!strcasecmp(v->name, "setvar")) {
12160 varname = ast_strdupa(v->value);
12161 if (varname && (varval = strchr(varname,'='))) {
12162 *varval = '\0';
12163 varval++;
12164 if ((tmpvar = ast_variable_new(varname, varval))) {
12165 tmpvar->next = user->chanvars;
12166 user->chanvars = tmpvar;
12167 }
12168 }
12169 } else if (!strcasecmp(v->name, "permit") ||
12170 !strcasecmp(v->name, "deny")) {
12171 user->ha = ast_append_ha(v->name, v->value, user->ha);
12172 } else if (!strcasecmp(v->name, "secret")) {
12173 ast_copy_string(user->secret, v->value, sizeof(user->secret));
12174 } else if (!strcasecmp(v->name, "md5secret")) {
12175 ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
12176 } else if (!strcasecmp(v->name, "callerid")) {
12177 ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
12178 } else if (!strcasecmp(v->name, "callgroup")) {
12179 user->callgroup = ast_get_group(v->value);
12180 } else if (!strcasecmp(v->name, "pickupgroup")) {
12181 user->pickupgroup = ast_get_group(v->value);
12182 } else if (!strcasecmp(v->name, "language")) {
12183 ast_copy_string(user->language, v->value, sizeof(user->language));
12184 } else if (!strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
12185 ast_copy_string(user->musicclass, v->value, sizeof(user->musicclass));
12186 } else if (!strcasecmp(v->name, "accountcode")) {
12187 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
12188 } else if (!strcasecmp(v->name, "call-limit") || !strcasecmp(v->name, "incominglimit")) {
12189 user->call_limit = atoi(v->value);
12190 if (user->call_limit < 0)
12191 user->call_limit = 0;
12192 } else if (!strcasecmp(v->name, "amaflags")) {
12193 format = ast_cdr_amaflags2int(v->value);
12194 if (format < 0) {
12195 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
12196 } else {
12197 user->amaflags = format;
12198 }
12199 } else if (!strcasecmp(v->name, "allow")) {
12200 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
12201 } else if (!strcasecmp(v->name, "disallow")) {
12202 ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
12203 } else if (!strcasecmp(v->name, "callingpres")) {
12204 user->callingpres = ast_parse_caller_presentation(v->value);
12205 if (user->callingpres == -1)
12206 user->callingpres = atoi(v->value);
12207 }
12208
12209
12210
12211 v = v->next;
12212 }
12213 ast_copy_flags(user, &userflags, mask.flags);
12214 ast_free_ha(oldha);
12215 return user;
12216 }
12217
12218
12219 static struct sip_peer *temp_peer(const char *name)
12220 {
12221 struct sip_peer *peer;
12222
12223 peer = malloc(sizeof(*peer));
12224 if (!peer)
12225 return NULL;
12226
12227 memset(peer, 0, sizeof(*peer));
12228 apeerobjs++;
12229 ASTOBJ_INIT(peer);
12230
12231 peer->expire = -1;
12232 peer->pokeexpire = -1;
12233 ast_copy_string(peer->name, name, sizeof(peer->name));
12234 ast_copy_flags(peer, &global_flags, SIP_FLAGS_TO_COPY);
12235 strcpy(peer->context, default_context);
12236 strcpy(peer->subscribecontext, default_subscribecontext);
12237 strcpy(peer->language, default_language);
12238 strcpy(peer->musicclass, global_musicclass);
12239 peer->addr.sin_port = htons(DEFAULT_SIP_PORT);
12240 peer->addr.sin_family = AF_INET;
12241 peer->capability = global_capability;
12242 peer->rtptimeout = global_rtptimeout;
12243 peer->rtpholdtimeout = global_rtpholdtimeout;
12244 peer->rtpkeepalive = global_rtpkeepalive;
12245 ast_set_flag(peer, SIP_SELFDESTRUCT);
12246 ast_set_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC);
12247 peer->prefs = prefs;
12248 reg_source_db(peer);
12249
12250 return peer;
12251 }
12252
12253
12254 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int realtime)
12255 {
12256 struct sip_peer *peer = NULL;
12257 struct ast_ha *oldha = NULL;
12258 int obproxyfound=0;
12259 int found=0;
12260 int format=0;
12261 time_t regseconds;
12262 char *varname = NULL, *varval = NULL;
12263 struct ast_variable *tmpvar = NULL;
12264 struct ast_flags peerflags = {(0)};
12265 struct ast_flags mask = {(0)};
12266
12267
12268 if (!realtime)
12269
12270
12271
12272
12273
12274 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
12275
12276 if (peer) {
12277
12278 found++;
12279 } else {
12280 peer = malloc(sizeof(*peer));
12281 if (peer) {
12282 memset(peer, 0, sizeof(*peer));
12283 if (realtime)
12284 rpeerobjs++;
12285 else
12286 speerobjs++;
12287 ASTOBJ_INIT(peer);
12288 peer->expire = -1;
12289 peer->pokeexpire = -1;
12290 } else {
12291 ast_log(LOG_WARNING, "Can't allocate SIP peer memory\n");
12292 }
12293 }
12294
12295 if (!peer)
12296 return NULL;
12297
12298 peer->lastmsgssent = -1;
12299 if (!found) {
12300 if (name)
12301 ast_copy_string(peer->name, name, sizeof(peer->name));
12302 peer->addr.sin_port = htons(DEFAULT_SIP_PORT);
12303 peer->addr.sin_family = AF_INET;
12304 peer->defaddr.sin_family = AF_INET;
12305 }
12306
12307 if (peer->chanvars) {
12308 ast_variables_destroy(peer->chanvars);
12309 peer->chanvars = NULL;
12310 }
12311 strcpy(peer->context, default_context);
12312 strcpy(peer->subscribecontext, default_subscribecontext);
12313 strcpy(peer->vmexten, global_vmexten);
12314 strcpy(peer->language, default_language);
12315 strcpy(peer->musicclass, global_musicclass);
12316 ast_copy_flags(peer, &global_flags, SIP_USEREQPHONE);
12317 peer->secret[0] = '\0';
12318 peer->md5secret[0] = '\0';
12319 peer->cid_num[0] = '\0';
12320 peer->cid_name[0] = '\0';
12321 peer->fromdomain[0] = '\0';
12322 peer->fromuser[0] = '\0';
12323 peer->regexten[0] = '\0';
12324 peer->mailbox[0] = '\0';
12325 peer->callgroup = 0;
12326 peer->pickupgroup = 0;
12327 peer->rtpkeepalive = global_rtpkeepalive;
12328 peer->maxms = default_qualify;
12329 peer->prefs = prefs;
12330 oldha = peer->ha;
12331 peer->ha = NULL;
12332 peer->addr.sin_family = AF_INET;
12333 ast_copy_flags(peer, &global_flags, SIP_FLAGS_TO_COPY);
12334 peer->capability = global_capability;
12335 peer->rtptimeout = global_rtptimeout;
12336 peer->rtpholdtimeout = global_rtpholdtimeout;
12337 while(v) {
12338 if (handle_common_options(&peerflags, &mask, v)) {
12339 v = v->next;
12340 continue;
12341 }
12342
12343 if (realtime && !strcasecmp(v->name, "regseconds")) {
12344 if (sscanf(v->value, "%ld", (time_t *)®seconds) != 1)
12345 regseconds = 0;
12346 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
12347 inet_aton(v->value, &(peer->addr.sin_addr));
12348 } else if (realtime && !strcasecmp(v->name, "name"))
12349 ast_copy_string(peer->name, v->value, sizeof(peer->name));
12350 else if (realtime && !strcasecmp(v->name, "fullcontact")) {
12351 ast_copy_string(peer->fullcontact, v->value, sizeof(peer->fullcontact));
12352 ast_set_flag((&peer->flags_page2), SIP_PAGE2_RT_FROMCONTACT);
12353 } else if (!strcasecmp(v->name, "secret"))
12354 ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
12355 else if (!strcasecmp(v->name, "md5secret"))
12356 ast_copy_string(peer->md5secret, v->value, sizeof(peer->md5secret));
12357 else if (!strcasecmp(v->name, "auth"))
12358 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
12359 else if (!strcasecmp(v->name, "callerid")) {
12360 ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name), peer->cid_num, sizeof(peer->cid_num));
12361 } else if (!strcasecmp(v->name, "context")) {
12362 ast_copy_string(peer->context, v->value, sizeof(peer->context));
12363 } else if (!strcasecmp(v->name, "subscribecontext")) {
12364 ast_copy_string(peer->subscribecontext, v->value, sizeof(peer->subscribecontext));
12365 } else if (!strcasecmp(v->name, "fromdomain"))
12366 ast_copy_string(peer->fromdomain, v->value, sizeof(peer->fromdomain));
12367 else if (!strcasecmp(v->name, "usereqphone"))
12368 ast_set2_flag(peer, ast_true(v->value), SIP_USEREQPHONE);
12369 else if (!strcasecmp(v->name, "fromuser"))
12370 ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
12371 else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "outboundproxy")) {
12372 if (!strcasecmp(v->value, "dynamic")) {
12373 if (!strcasecmp(v->name, "outboundproxy") || obproxyfound) {
12374 ast_log(LOG_WARNING, "You can't have a dynamic outbound proxy, you big silly head at line %d.\n", v->lineno);
12375 } else {
12376
12377 ast_set_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC);
12378 if (!found) {
12379
12380
12381 memset(&peer->addr.sin_addr, 0, 4);
12382 if (peer->addr.sin_port) {
12383
12384 peer->defaddr.sin_port = peer->addr.sin_port;
12385 peer->addr.sin_port = 0;
12386 }
12387 }
12388 }
12389 } else {
12390
12391 if (peer->expire > -1)
12392 ast_sched_del(sched, peer->expire);
12393 peer->expire = -1;
12394 ast_clear_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC);
12395 if (!obproxyfound || !strcasecmp(v->name, "outboundproxy")) {
12396 if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
12397 ASTOBJ_UNREF(peer, sip_destroy_peer);
12398 return NULL;
12399 }
12400 }
12401 if (!strcasecmp(v->name, "outboundproxy"))
12402 obproxyfound=1;
12403 else {
12404 ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
12405 if (!peer->addr.sin_port)
12406 peer->addr.sin_port = htons(DEFAULT_SIP_PORT);
12407 }
12408 }
12409 } else if (!strcasecmp(v->name, "defaultip")) {
12410 if (ast_get_ip(&peer->defaddr, v->value)) {
12411 ASTOBJ_UNREF(peer, sip_destroy_peer);
12412 return NULL;
12413 }
12414 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
12415 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
12416 } else if (!strcasecmp(v->name, "port")) {
12417 if (!realtime && ast_test_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC))
12418 peer->defaddr.sin_port = htons(atoi(v->value));
12419 else
12420 peer->addr.sin_port = htons(atoi(v->value));
12421 } else if (!strcasecmp(v->name, "callingpres")) {
12422 peer->callingpres = ast_parse_caller_presentation(v->value);
12423 if (peer->callingpres == -1)
12424 peer->callingpres = atoi(v->value);
12425 } else if (!strcasecmp(v->name, "username")) {
12426 ast_copy_string(peer->username, v->value, sizeof(peer->username));
12427 } else if (!strcasecmp(v->name, "language")) {
12428 ast_copy_string(peer->language, v->value, sizeof(peer->language));
12429 } else if (!strcasecmp(v->name, "regexten")) {
12430 ast_copy_string(peer->regexten, v->value, sizeof(peer->regexten));
12431 } else if (!strcasecmp(v->name, "call-limit") || !strcasecmp(v->name, "incominglimit")) {
12432 peer->call_limit = atoi(v->value);
12433 if (peer->call_limit < 0)
12434 peer->call_limit = 0;
12435 } else if (!strcasecmp(v->name, "amaflags")) {
12436 format = ast_cdr_amaflags2int(v->value);
12437 if (format < 0) {
12438 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
12439 } else {
12440 peer->amaflags = format;
12441 }
12442 } else if (!strcasecmp(v->name, "accountcode")) {
12443 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
12444 } else if (!strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
12445 ast_copy_string(peer->musicclass, v->value, sizeof(peer->musicclass));
12446 } else if (!strcasecmp(v->name, "mailbox")) {
12447 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
12448 } else if (!strcasecmp(v->name, "vmexten")) {
12449 ast_copy_string(peer->vmexten, v->value, sizeof(peer->vmexten));
12450 } else if (!strcasecmp(v->name, "callgroup")) {
12451 peer->callgroup = ast_get_group(v->value);
12452 } else if (!strcasecmp(v->name, "pickupgroup")) {
12453 peer->pickupgroup = ast_get_group(v->value);
12454 } else if (!strcasecmp(v->name, "allow")) {
12455 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
12456 } else if (!strcasecmp(v->name, "disallow")) {
12457 ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
12458 } else if (!strcasecmp(v->name, "rtptimeout")) {
12459 if ((sscanf(v->value, "%d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
12460 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
12461 peer->rtptimeout = global_rtptimeout;
12462 }
12463 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
12464 if ((sscanf(v->value, "%d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
12465 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
12466 peer->rtpholdtimeout = global_rtpholdtimeout;
12467 }
12468 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
12469 if ((sscanf(v->value, "%d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
12470 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
12471 peer->rtpkeepalive = global_rtpkeepalive;
12472 }
12473 } else if (!strcasecmp(v->name, "setvar")) {
12474
12475 varname = ast_strdupa(v->value);
12476 if (varname && (varval = strchr(varname,'='))) {
12477 *varval = '\0';
12478 varval++;
12479 if ((tmpvar = ast_variable_new(varname, varval))) {
12480 tmpvar->next = peer->chanvars;
12481 peer->chanvars = tmpvar;
12482 }
12483 }
12484 } else if (!strcasecmp(v->name, "qualify")) {
12485 if (!strcasecmp(v->value, "no")) {
12486 peer->maxms = 0;
12487 } else if (!strcasecmp(v->value, "yes")) {
12488 peer->maxms = DEFAULT_MAXMS;
12489 } else if (sscanf(v->value, "%d", &peer->maxms) != 1) {
12490 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);
12491 peer->maxms = 0;
12492 }
12493 }
12494
12495
12496
12497 v=v->next;
12498 }
12499 if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC) && realtime) {
12500 time_t nowtime;
12501
12502 time(&nowtime);
12503 if ((nowtime - regseconds) > 0) {
12504 destroy_association(peer);
12505 memset(&peer->addr, 0, sizeof(peer->addr));
12506 if (option_debug)
12507 ast_log(LOG_DEBUG, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
12508 }
12509 }
12510 ast_copy_flags(peer, &peerflags, mask.flags);
12511 if (!found && ast_test_flag(&peer->flags_page2, SIP_PAGE2_DYNAMIC) && !ast_test_flag(peer, SIP_REALTIME))
12512 reg_source_db(peer);
12513 ASTOBJ_UNMARK(peer);
12514 ast_free_ha(oldha);
12515 return peer;
12516 }
12517
12518
12519
12520
12521
12522
12523
12524 static int reload_config(void)
12525 {
12526 struct ast_config *cfg;
12527 struct ast_variable *v;
12528 struct sip_peer *peer;
12529 struct sip_user *user;
12530 struct ast_hostent ahp;
12531 char *cat;
12532 char *utype;
12533 struct hostent *hp;
12534 int format;
12535 char iabuf[INET_ADDRSTRLEN];
12536 struct ast_flags dummy;
12537 int auto_sip_domains = 0;
12538 struct sockaddr_in old_bindaddr = bindaddr;
12539
12540 cfg = ast_config_load(config);
12541
12542
12543 if (!cfg) {
12544 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
12545 return -1;
12546 }
12547
12548
12549 memset(&bindaddr, 0, sizeof(bindaddr));
12550 memset(&localaddr, 0, sizeof(localaddr));
12551 memset(&externip, 0, sizeof(externip));
12552 memset(&prefs, 0 , sizeof(prefs));
12553 sipdebug &= ~SIP_DEBUG_CONFIG;
12554
12555
12556 ast_copy_string(default_context, DEFAULT_CONTEXT, sizeof(default_context));
12557 default_subscribecontext[0] = '\0';
12558 default_language[0] = '\0';
12559 default_fromdomain[0] = '\0';
12560 default_qualify = 0;
12561 allow_external_domains = 1;
12562 externhost[0] = '\0';
12563 externexpire = 0;
12564 externrefresh = 10;
12565 ast_copy_string(default_useragent, DEFAULT_USERAGENT, sizeof(default_useragent));
12566 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
12567 global_notifyringing = 1;
12568 global_alwaysauthreject = 0;
12569 ast_copy_string(global_realm, DEFAULT_REALM, sizeof(global_realm));
12570 ast_copy_string(global_musicclass, "default", sizeof(global_musicclass));
12571 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
12572 memset(&outboundproxyip, 0, sizeof(outboundproxyip));
12573 outboundproxyip.sin_port = htons(DEFAULT_SIP_PORT);
12574 outboundproxyip.sin_family = AF_INET;
12575 videosupport = 0;
12576 compactheaders = 0;
12577 dumphistory = 0;
12578 recordhistory = 0;
12579 relaxdtmf = 0;
12580 callevents = 0;
12581 ourport = DEFAULT_SIP_PORT;
12582 global_rtptimeout = 0;
12583 global_rtpholdtimeout = 0;
12584 global_rtpkeepalive = 0;
12585 global_rtautoclear = 120;
12586 pedanticsipchecking = 0;
12587 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
12588 global_regattempts_max = 0;
12589 ast_clear_flag(&global_flags, AST_FLAGS_ALL);
12590 ast_clear_flag(&global_flags_page2, AST_FLAGS_ALL);
12591 ast_set_flag(&global_flags, SIP_DTMF_RFC2833);
12592 ast_set_flag(&global_flags, SIP_NAT_RFC3581);
12593 ast_set_flag(&global_flags, SIP_CAN_REINVITE);
12594 ast_set_flag(&global_flags_page2, SIP_PAGE2_RTUPDATE);
12595 global_mwitime = DEFAULT_MWITIME;
12596 strcpy(global_vmexten, DEFAULT_VMEXTEN);
12597 srvlookup = 0;
12598 autocreatepeer = 0;
12599 regcontext[0] = '\0';
12600 tos = 0;
12601 expiry = DEFAULT_EXPIRY;
12602 global_allowguest = 1;
12603
12604
12605 v = ast_variable_browse(cfg, "general");
12606 while(v) {
12607 if (handle_common_options(&global_flags, &dummy, v)) {
12608 v = v->next;
12609 continue;
12610 }
12611
12612
12613 if (!strcasecmp(v->name, "context")) {
12614 ast_copy_string(default_context, v->value, sizeof(default_context));
12615 } else if (!strcasecmp(v->name, "realm")) {
12616 ast_copy_string(global_realm, v->value, sizeof(global_realm));
12617 } else if (!strcasecmp(v->name, "useragent")) {
12618 ast_copy_string(default_useragent, v->value, sizeof(default_useragent));
12619 ast_log(LOG_DEBUG, "Setting User Agent Name to %s\n",
12620 default_useragent);
12621 } else if (!strcasecmp(v->name, "rtcachefriends")) {
12622 ast_set2_flag((&global_flags_page2), ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
12623 } else if (!strcasecmp(v->name, "rtupdate")) {
12624 ast_set2_flag((&global_flags_page2), ast_true(v->value), SIP_PAGE2_RTUPDATE);
12625 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
12626 ast_set2_flag((&global_flags_page2), ast_true(v->value), SIP_PAGE2_IGNOREREGEXPIRE);
12627 } else if (!strcasecmp(v->name, "rtautoclear")) {
12628 int i = atoi(v->value);
12629 if (i > 0)
12630 global_rtautoclear = i;
12631 else
12632 i = 0;
12633 ast_set2_flag((&global_flags_page2), i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
12634 } else if (!strcasecmp(v->name, "usereqphone")) {
12635 ast_set2_flag((&global_flags), ast_true(v->value), SIP_USEREQPHONE);
12636 } else if (!strcasecmp(v->name, "relaxdtmf")) {
12637 relaxdtmf = ast_true(v->value);
12638 } else if (!strcasecmp(v->name, "checkmwi")) {
12639 if ((sscanf(v->value, "%d", &global_mwitime) != 1) || (global_mwitime < 0)) {
12640 ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d. Using default (10).\n", v->value, v->lineno);
12641 global_mwitime = DEFAULT_MWITIME;
12642 }
12643 } else if (!strcasecmp(v->name, "vmexten")) {
12644 ast_copy_string(global_vmexten, v->value, sizeof(global_vmexten));
12645 } else if (!strcasecmp(v->name, "rtptimeout")) {
12646 if ((sscanf(v->value, "%d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
12647 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
12648 global_rtptimeout = 0;
12649 }
12650 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
12651 if ((sscanf(v->value, "%d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
12652 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
12653 global_rtpholdtimeout = 0;
12654 }
12655 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
12656 if ((sscanf(v->value, "%d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
12657 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
12658 global_rtpkeepalive = 0;
12659 }
12660 } else if (!strcasecmp(v->name, "videosupport")) {
12661 videosupport = ast_true(v->value);
12662 } else if (!strcasecmp(v->name, "compactheaders")) {
12663 compactheaders = ast_true(v->value);
12664 } else if (!strcasecmp(v->name, "notifymimetype")) {
12665 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
12666 } else if (!strcasecmp(v->name, "notifyringing")) {
12667 global_notifyringing = ast_true(v->value);
12668 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
12669 global_alwaysauthreject = ast_true(v->value);
12670 } else if (!strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
12671 ast_copy_string(global_musicclass, v->value, sizeof(global_musicclass));
12672 } else if (!strcasecmp(v->name, "language")) {
12673 ast_copy_string(default_language, v->value, sizeof(default_language));
12674 } else if (!strcasecmp(v->name, "regcontext")) {
12675 ast_copy_string(regcontext, v->value, sizeof(regcontext));
12676
12677 if (!ast_context_find(regcontext))
12678 ast_context_create(NULL, regcontext, channeltype);
12679 } else if (!strcasecmp(v->name, "callerid")) {
12680 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
12681 } else if (!strcasecmp(v->name, "fromdomain")) {
12682 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
12683 } else if (!strcasecmp(v->name, "outboundproxy")) {
12684 if (ast_get_ip_or_srv(&outboundproxyip, v->value, srvlookup ? "_sip._udp" : NULL) < 0)
12685 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", v->value);
12686 } else if (!strcasecmp(v->name, "outboundproxyport")) {
12687
12688 sscanf(v->value, "%d", &format);
12689 outboundproxyip.sin_port = htons(format);
12690 } else if (!strcasecmp(v->name, "autocreatepeer")) {
12691 autocreatepeer = ast_true(v->value);
12692 } else if (!strcasecmp(v->name, "srvlookup")) {
12693 srvlookup = ast_true(v->value);
12694 } else if (!strcasecmp(v->name, "pedantic")) {
12695 pedanticsipchecking = ast_true(v->value);
12696 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
12697 max_expiry = atoi(v->value);
12698 if (max_expiry < 1)
12699 max_expiry = DEFAULT_MAX_EXPIRY;
12700 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
12701 default_expiry = atoi(v->value);
12702 if (default_expiry < 1)
12703 default_expiry = DEFAULT_DEFAULT_EXPIRY;
12704 } else if (!strcasecmp(v->name, "sipdebug")) {
12705 if (ast_true(v->value))
12706 sipdebug |= SIP_DEBUG_CONFIG;
12707 } else if (!strcasecmp(v->name, "dumphistory")) {
12708 dumphistory = ast_true(v->value);
12709 } else if (!strcasecmp(v->name, "recordhistory")) {
12710 recordhistory = ast_true(v->value);
12711 } else if (!strcasecmp(v->name, "registertimeout")) {
12712 global_reg_timeout = atoi(v->value);
12713 if (global_reg_timeout < 1)
12714 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
12715 } else if (!strcasecmp(v->name, "registerattempts")) {
12716 global_regattempts_max = atoi(v->value);
12717 } else if (!strcasecmp(v->name, "bindaddr")) {
12718 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
12719 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
12720 } else {
12721 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
12722 }
12723 } else if (!strcasecmp(v->name, "localnet")) {
12724 struct ast_ha *na;
12725 if (!(na = ast_append_ha("d", v->value, localaddr)))
12726 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
12727 else
12728 localaddr = na;
12729 } else if (!strcasecmp(v->name, "localmask")) {
12730 ast_log(LOG_WARNING, "Use of localmask is no long supported -- use localnet with mask syntax\n");
12731 } else if (!strcasecmp(v->name, "externip")) {
12732 if (!(hp = ast_gethostbyname(v->value, &ahp)))
12733 ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
12734 else
12735 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
12736 externexpire = 0;
12737 } else if (!strcasecmp(v->name, "externhost")) {
12738 ast_copy_string(externhost, v->value, sizeof(externhost));
12739 if (!(hp = ast_gethostbyname(externhost, &ahp)))
12740 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
12741 else
12742 memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
12743 time(&externexpire);
12744 } else if (!strcasecmp(v->name, "externrefresh")) {
12745 if (sscanf(v->value, "%d", &externrefresh) != 1) {
12746 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
12747 externrefresh = 10;
12748 }
12749 } else if (!strcasecmp(v->name, "allow")) {
12750 ast_parse_allow_disallow(&prefs, &global_capability, v->value, 1);
12751 } else if (!strcasecmp(v->name, "disallow")) {
12752 ast_parse_allow_disallow(&prefs, &global_capability, v->value, 0);
12753 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
12754 allow_external_domains = ast_true(v->value);
12755 } else if (!strcasecmp(v->name, "autodomain")) {
12756 auto_sip_domains = ast_true(v->value);
12757 } else if (!strcasecmp(v->name, "domain")) {
12758 char *domain = ast_strdupa(v->value);
12759 char *context = strchr(domain, ',');
12760
12761 if (context)
12762 *context++ = '\0';
12763
12764 if (option_debug && ast_strlen_zero(context))
12765 ast_log(LOG_DEBUG, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
12766 if (ast_strlen_zero(domain))
12767 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
12768 else
12769 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, context ? ast_strip(context) : "");
12770 } else if (!strcasecmp(v->name, "register")) {
12771 sip_register(v->value, v->lineno);
12772 } else if (!strcasecmp(v->name, "tos")) {
12773 if (ast_str2tos(v->value, &tos))
12774 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
12775 } else if (!strcasecmp(v->name, "bindport")) {
12776 if (sscanf(v->value, "%d", &ourport) == 1) {
12777 bindaddr.sin_port = htons(ourport);
12778 } else {
12779 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
12780 }
12781 } else if (!strcasecmp(v->name, "qualify")) {
12782 if (!strcasecmp(v->value, "no")) {
12783 default_qualify = 0;
12784 } else if (!strcasecmp(v->value, "yes")) {
12785 default_qualify = DEFAULT_MAXMS;
12786 } else if (sscanf(v->value, "%d", &default_qualify) != 1) {
12787 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
12788 default_qualify = 0;
12789 }
12790 } else if (!strcasecmp(v->name, "callevents")) {
12791 callevents = ast_true(v->value);
12792 }
12793
12794
12795
12796 v = v->next;
12797 }
12798
12799 if (!allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
12800 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
12801 allow_external_domains = 1;
12802 }
12803
12804
12805 v = ast_variable_browse(cfg, "authentication");
12806 while(v) {
12807
12808 if (!strcasecmp(v->name, "auth")) {
12809 authl = add_realm_authentication(authl, v->value, v->lineno);
12810 }
12811 v = v->next;
12812 }
12813
12814
12815 cat = ast_category_browse(cfg, NULL);
12816 while(cat) {
12817 if (strcasecmp(cat, "general") && strcasecmp(cat, "authentication")) {
12818 utype = ast_variable_retrieve(cfg, cat, "type");
12819 if (utype) {
12820 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
12821 user = build_user(cat, ast_variable_browse(cfg, cat), 0);
12822 if (user) {
12823 ASTOBJ_CONTAINER_LINK(&userl,user);
12824 ASTOBJ_UNREF(user, sip_destroy_user);
12825 }
12826 }
12827 if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
12828 peer = build_peer(cat, ast_variable_browse(cfg, cat), 0);
12829 if (peer) {
12830 ASTOBJ_CONTAINER_LINK(&peerl,peer);
12831 ASTOBJ_UNREF(peer, sip_destroy_peer);
12832 }
12833 } else if (strcasecmp(utype, "user")) {
12834 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
12835 }
12836 } else
12837 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
12838 }
12839 cat = ast_category_browse(cfg, cat);
12840 }
12841 if (ast_find_ourip(&__ourip, bindaddr)) {
12842 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
12843 return 0;
12844 }
12845 if (!ntohs(bindaddr.sin_port))
12846 bindaddr.sin_port = ntohs(DEFAULT_SIP_PORT);
12847 bindaddr.sin_family = AF_INET;
12848 ast_mutex_lock(&netlock);
12849 if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
12850 close(sipsock);
12851 sipsock = -1;
12852 }
12853 if (sipsock < 0) {
12854 sipsock = socket(AF_INET, SOCK_DGRAM, 0);
12855 if (sipsock < 0) {
12856 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
12857 } else {
12858
12859 const int reuseFlag = 1;
12860 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
12861 (const char*)&reuseFlag,
12862 sizeof reuseFlag);
12863
12864 if (bind(sipsock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
12865 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
12866 ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port),
12867 strerror(errno));
12868 close(sipsock);
12869 sipsock = -1;
12870 } else {
12871 if (option_verbose > 1) {
12872 ast_verbose(VERBOSE_PREFIX_2 "SIP Listening on %s:%d\n",
12873 ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port));
12874 ast_verbose(VERBOSE_PREFIX_2 "Using TOS bits %d\n", tos);
12875 }
12876 if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))
12877 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
12878 }
12879 }
12880 }
12881 ast_mutex_unlock(&netlock);
12882
12883
12884
12885
12886
12887 if (auto_sip_domains) {
12888 char temp[MAXHOSTNAMELEN];
12889
12890
12891 if (bindaddr.sin_addr.s_addr) {
12892 ast_inet_ntoa(temp, sizeof(temp), bindaddr.sin_addr);
12893 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
12894 } else {
12895 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
12896 }
12897
12898
12899 if (externip.sin_addr.s_addr) {
12900 ast_inet_ntoa(temp, sizeof(temp), externip.sin_addr);
12901 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
12902 }
12903
12904
12905 if (!ast_strlen_zero(externhost))
12906 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
12907
12908
12909 if (!gethostname(temp, sizeof(temp)))
12910 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
12911 }
12912
12913
12914 ast_config_destroy(cfg);
12915
12916
12917 if (notify_types)
12918 ast_config_destroy(notify_types);
12919 notify_types = ast_config_load(notify_config);
12920
12921 return 0;
12922 }
12923
12924
12925 static struct ast_rtp *sip_get_rtp_peer(struct ast_channel *chan)
12926 {
12927 struct sip_pvt *p;
12928 struct ast_rtp *rtp = NULL;
12929 p = chan->tech_pvt;
12930 if (!p)
12931 return NULL;
12932 ast_mutex_lock(&p->lock);
12933 if (p->rtp && ast_test_flag(p, SIP_CAN_REINVITE))
12934 rtp = p->rtp;
12935 ast_mutex_unlock(&p->lock);
12936 return rtp;
12937 }
12938
12939
12940 static struct ast_rtp *sip_get_vrtp_peer(struct ast_channel *chan)
12941 {
12942 struct sip_pvt *p;
12943 struct ast_rtp *rtp = NULL;
12944 p = chan->tech_pvt;
12945 if (!p)
12946 return NULL;
12947
12948 ast_mutex_lock(&p->lock);
12949 if (p->vrtp && ast_test_flag(p, SIP_CAN_REINVITE))
12950 rtp = p->vrtp;
12951 ast_mutex_unlock(&p->lock);
12952 return rtp;
12953 }
12954
12955
12956 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
12957 {
12958 struct sip_pvt *p;
12959 int changed = 0;
12960
12961 p = chan->tech_pvt;
12962 if (!p)
12963 return -1;
12964 ast_mutex_lock(&p->lock);
12965 if (rtp) {
12966 changed |= ast_rtp_get_peer(rtp, &p->redirip);
12967 } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
12968 memset(&p->redirip, 0, sizeof(p->redirip));
12969 changed = 1;
12970 }
12971 if (vrtp) {
12972 changed |= ast_rtp_get_peer(vrtp, &p->vredirip);
12973 } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
12974 memset(&p->vredirip, 0, sizeof(p->vredirip));
12975 changed = 1;
12976 }
12977 if (codecs && (p->redircodecs != codecs)) {
12978 p->redircodecs = codecs;
12979 changed = 1;
12980 }
12981 if (changed && !ast_test_flag(p, SIP_GOTREFER)) {
12982 if (!p->pendinginvite) {
12983 if (option_debug > 2) {
12984 char iabuf[INET_ADDRSTRLEN];
12985 ast_log(LOG_DEBUG, "Sending reinvite on SIP '%s' - It's audio soon redirected to IP %s\n", p->callid, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp ? p->redirip.sin_addr : p->ourip));
12986 }
12987 transmit_reinvite_with_sdp(p);
12988 } else if (!ast_test_flag(p, SIP_PENDINGBYE)) {
12989 if (option_debug > 2) {
12990 char iabuf[INET_ADDRSTRLEN];
12991 ast_log(LOG_DEBUG, "Deferring reinvite on SIP '%s' - It's audio will be redirected to IP %s\n", p->callid, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp ? p->redirip.sin_addr : p->ourip));
12992 }
12993 ast_set_flag(p, SIP_NEEDREINVITE);
12994 }
12995 }
12996
12997 time(&p->lastrtprx);
12998 time(&p->lastrtptx);
12999 ast_mutex_unlock(&p->lock);
13000 return 0;
13001 }
13002
13003 static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
13004 static char *descrip_dtmfmode = "SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
13005 static char *app_dtmfmode = "SIPDtmfMode";
13006
13007 static char *app_sipaddheader = "SIPAddHeader";
13008 static char *synopsis_sipaddheader = "Add a SIP header to the outbound call";
13009
13010
13011 static char *descrip_sipaddheader = ""
13012 " SIPAddHeader(Header: Content)\n"
13013 "Adds a header to a SIP call placed with DIAL.\n"
13014 "Remember to user the X-header if you are adding non-standard SIP\n"
13015 "headers, like \"X-Asterisk-Accountcode:\". Use this with care.\n"
13016 "Adding the wrong headers may jeopardize the SIP dialog.\n"
13017 "Always returns 0\n";
13018
13019 static char *app_sipgetheader = "SIPGetHeader";
13020 static char *synopsis_sipgetheader = "Get a SIP header from an incoming call";
13021
13022 static char *descrip_sipgetheader = ""
13023 " SIPGetHeader(var=headername[|options]): \n"
13024 "Sets a channel variable to the content of a SIP header\n"
13025 " Options:\n"
13026 " j - Jump to priority n+101 if the requested header isn't found.\n"
13027 " This application sets the following channel variable upon completion:\n"
13028 " SIPGETSTATUS - This variable will contain the status of the attempt\n"
13029 " FOUND | NOTFOUND\n"
13030 " This application has been deprecated in favor of the SIP_HEADER function.\n";
13031
13032
13033 static int sip_dtmfmode(struct ast_channel *chan, void *data)
13034 {
13035 struct sip_pvt *p;
13036 char *mode;
13037 if (data)
13038 mode = (char *)data;
13039 else {
13040 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
13041 return 0;
13042 }
13043 ast_mutex_lock(&chan->lock);
13044 if (chan->type != channeltype) {
13045 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
13046 ast_mutex_unlock(&chan->lock);
13047 return 0;
13048 }
13049 p = chan->tech_pvt;
13050 if (!p) {
13051 ast_mutex_unlock(&chan->lock);
13052 return 0;
13053 }
13054 ast_mutex_lock(&p->lock);
13055 if (!strcasecmp(mode,"info")) {
13056 ast_clear_flag(p, SIP_DTMF);
13057 ast_set_flag(p, SIP_DTMF_INFO);
13058 } else if (!strcasecmp(mode,"rfc2833")) {
13059 ast_clear_flag(p, SIP_DTMF);
13060 ast_set_flag(p, SIP_DTMF_RFC2833);
13061 } else if (!strcasecmp(mode,"inband")) {
13062 ast_clear_flag(p, SIP_DTMF);
13063 ast_set_flag(p, SIP_DTMF_INBAND);
13064 } else
13065 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n",mode);
13066 if (ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) {
13067 if (!p->vad) {
13068 p->vad = ast_dsp_new();
13069 ast_dsp_set_features(p->vad, DSP_FEATURE_DTMF_DETECT);
13070 }
13071 } else {
13072 if (p->vad) {
13073 ast_dsp_free(p->vad);
13074 p->vad = NULL;
13075 }
13076 }
13077 ast_mutex_unlock(&p->lock);
13078 ast_mutex_unlock(&chan->lock);
13079 return 0;
13080 }
13081
13082
13083 static int sip_addheader(struct ast_channel *chan, void *data)
13084 {
13085 int no = 0;
13086 int ok = 0;
13087 char varbuf[128];
13088
13089 if (ast_strlen_zero((char *)data)) {
13090 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
13091 return 0;
13092 }
13093 ast_mutex_lock(&chan->lock);
13094
13095
13096 while (!ok && no <= 50) {
13097 no++;
13098 snprintf(varbuf, sizeof(varbuf), "_SIPADDHEADER%02d", no);
13099 if (ast_strlen_zero(pbx_builtin_getvar_helper(chan, varbuf + 1)))
13100 ok = 1;
13101 }
13102 if (ok) {
13103 pbx_builtin_setvar_helper (chan, varbuf, (char *)data);
13104 if (sipdebug)
13105 ast_log(LOG_DEBUG,"SIP Header added \"%s\" as %s\n", (char *) data, varbuf);
13106 } else {
13107 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
13108 }
13109 ast_mutex_unlock(&chan->lock);
13110 return 0;
13111 }
13112
13113
13114 static int sip_getheader(struct ast_channel *chan, void *data)
13115 {
13116 char *argv, *varname = NULL, *header = NULL, *content, *options = NULL;
13117 static int dep_warning = 0;
13118 struct sip_pvt *p;
13119 int priority_jump = 0;
13120
13121 if (!dep_warning) {
13122 ast_log(LOG_WARNING, "SIPGetHeader is deprecated, use the SIP_HEADER function instead.\n");
13123 dep_warning = 1;
13124 }
13125
13126 argv = ast_strdupa(data);
13127 if (!argv) {
13128 ast_log(LOG_DEBUG, "Memory allocation failed\n");
13129 return 0;
13130 }
13131
13132 if (strchr (argv, '=') ) {
13133 varname = strsep (&argv, "=");
13134 if (strchr(argv, '|')) {
13135 header = strsep (&argv, "|");
13136 options = strsep (&argv, "\0");
13137 } else {
13138 header = strsep (&argv, "\0");
13139 }
13140
13141 }
13142
13143 if (!varname || !header) {
13144 ast_log(LOG_DEBUG, "SIPGetHeader: Ignoring command, Syntax error in argument\n");
13145 return 0;
13146 }
13147
13148 if (options) {
13149 if (strchr(options, 'j'))
13150 priority_jump = 1;
13151 }
13152
13153 ast_mutex_lock(&chan->lock);
13154 if (chan->type != channeltype) {
13155 ast_log(LOG_WARNING, "Call this application only on incoming SIP calls\n");
13156 ast_mutex_unlock(&chan->lock);
13157 return 0;
13158 }
13159
13160 p = chan->tech_pvt;
13161 content = get_header(&p->initreq, header);
13162 if (!ast_strlen_zero(content)) {
13163 pbx_builtin_setvar_helper(chan, varname, content);
13164 if (option_verbose > 2)
13165 ast_verbose(VERBOSE_PREFIX_3 "SIPGetHeader: set variable %s to %s\n", varname, content);
13166 pbx_builtin_setvar_helper(chan, "SIPGETSTATUS", "FOUND");
13167 } else {
13168 ast_log(LOG_WARNING,"SIP Header %s not found for channel variable %s\n", header, varname);
13169 if (priority_jump || option_priority_jumping) {
13170
13171 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
13172 }
13173 pbx_builtin_setvar_helper(chan, "SIPGETSTATUS", "NOTFOUND");
13174 }
13175
13176 ast_mutex_unlock(&chan->lock);
13177 return 0;
13178 }
13179
13180
13181
13182
13183
13184 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
13185 {
13186 char *cdest;
13187 char *extension, *host, *port;
13188 char tmp[80];
13189
13190 cdest = ast_strdupa(dest);
13191 if (!cdest) {
13192 ast_log(LOG_ERROR, "Problem allocating the memory\n");
13193 return 0;
13194 }
13195 extension = strsep(&cdest, "@");
13196 host = strsep(&cdest, ":");
13197 port = strsep(&cdest, ":");
13198 if (!extension) {
13199 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
13200 return 0;
13201 }
13202
13203
13204 if (!host) {
13205 char *localtmp;
13206 ast_copy_string(tmp, get_header(&p->initreq, "To"), sizeof(tmp));
13207 if (!strlen(tmp)) {
13208 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
13209 return 0;
13210 }
13211 if ((localtmp = strstr(tmp, "sip:")) && (localtmp = strchr(localtmp, '@'))) {
13212 char lhost[80], lport[80];
13213 memset(lhost, 0, sizeof(lhost));
13214 memset(lport, 0, sizeof(lport));
13215 localtmp++;
13216
13217 sscanf(localtmp, "%[^<>:; ]:%[^<>:; ]", lhost, lport);
13218 if (!strlen(lhost)) {
13219 ast_log(LOG_ERROR, "Can't find the host address\n");
13220 return 0;
13221 }
13222 host = ast_strdupa(lhost);
13223 if (!host) {
13224 ast_log(LOG_ERROR, "Problem allocating the memory\n");
13225 return 0;
13226 }
13227 if (!ast_strlen_zero(lport)) {
13228 port = ast_strdupa(lport);
13229 if (!port) {
13230 ast_log(LOG_ERROR, "Problem allocating the memory\n");
13231 return 0;
13232 }
13233 }
13234 }
13235 }
13236
13237 snprintf(p->our_contact, sizeof(p->our_contact), "Transfer <sip:%s@%s%s%s>", extension, host, port ? ":" : "", port ? port : "");
13238 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq, 1);
13239
13240
13241 ast_set_flag(p, SIP_ALREADYGONE);
13242
13243
13244 return -1;
13245 }
13246
13247
13248 static int sip_get_codec(struct ast_channel *chan)
13249 {
13250 struct sip_pvt *p = chan->tech_pvt;
13251 return p->peercapability;
13252 }
13253
13254
13255 static struct ast_rtp_protocol sip_rtp = {
13256 type: channeltype,
13257 get_rtp_info: sip_get_rtp_peer,
13258 get_vrtp_info: sip_get_vrtp_peer,
13259 set_rtp_peer: sip_set_rtp_peer,
13260 get_codec: sip_get_codec,
13261 };
13262
13263
13264 static void sip_poke_all_peers(void)
13265 {
13266 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
13267 ASTOBJ_WRLOCK(iterator);
13268 sip_poke_peer(iterator);
13269 ASTOBJ_UNLOCK(iterator);
13270 } while (0)
13271 );
13272 }
13273
13274
13275 static void sip_send_all_registers(void)
13276 {
13277 int ms;
13278 int regspacing;
13279 if (!regobjs)
13280 return;
13281 regspacing = default_expiry * 1000/regobjs;
13282 if (regspacing > 100)
13283 regspacing = 100;
13284 ms = regspacing;
13285 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
13286 ASTOBJ_WRLOCK(iterator);
13287 if (iterator->expire > -1)
13288 ast_sched_del(sched, iterator->expire);
13289 ms += regspacing;
13290 iterator->expire = ast_sched_add(sched, ms, sip_reregister, iterator);
13291 ASTOBJ_UNLOCK(iterator);
13292 } while (0)
13293 );
13294 }
13295
13296
13297 static int sip_do_reload(void)
13298 {
13299 clear_realm_authentication(authl);
13300 clear_sip_domains();
13301 authl = NULL;
13302
13303
13304
13305 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
13306 ASTOBJ_RDLOCK(iterator);
13307 if (iterator->call) {
13308 if (option_debug > 2)
13309 ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
13310
13311 sip_destroy(iterator->call);
13312 }
13313 ASTOBJ_UNLOCK(iterator);
13314 } while(0));
13315
13316 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
13317 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
13318 ASTOBJ_CONTAINER_MARKALL(&peerl);
13319 reload_config();
13320
13321 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
13322
13323 sip_poke_all_peers();
13324 sip_send_all_registers();
13325
13326 return 0;
13327 }
13328
13329
13330 static int sip_reload(int fd, int argc, char *argv[])
13331 {
13332
13333 ast_mutex_lock(&sip_reload_lock);
13334 if (sip_reloading) {
13335 ast_verbose("Previous SIP reload not yet done\n");
13336 } else
13337 sip_reloading = 1;
13338 ast_mutex_unlock(&sip_reload_lock);
13339 restart_monitor();
13340
13341 return 0;
13342 }
13343
13344
13345 int reload(void)
13346 {
13347 return sip_reload(0, 0, NULL);
13348 }
13349
13350 static struct ast_cli_entry my_clis[] = {
13351 { { "sip", "notify", NULL }, sip_notify, "Send a notify packet to a SIP peer", notify_usage, complete_sipnotify },
13352 { { "sip", "show", "objects", NULL }, sip_show_objects, "Show all SIP object allocations", show_objects_usage },
13353 { { "sip", "show", "users", NULL }, sip_show_users, "Show defined SIP users", show_users_usage },
13354 { { "sip", "show", "user", NULL }, sip_show_user, "Show details on specific SIP user", show_user_usage, complete_sip_show_user },
13355 { { "sip", "show", "subscriptions", NULL }, sip_show_subscriptions, "Show active SIP subscriptions", show_subscriptions_usage},
13356 { { "sip", "show", "channels", NULL }, sip_show_channels, "Show active SIP channels", show_channels_usage},
13357 { { "sip", "show", "channel", NULL }, sip_show_channel, "Show detailed SIP channel info", show_channel_usage, complete_sipch },
13358 { { "sip", "show", "history", NULL }, sip_show_history, "Show SIP dialog history", show_history_usage, complete_sipch },
13359 { { "sip", "show", "domains", NULL }, sip_show_domains, "List our local SIP domains.", show_domains_usage },
13360 { { "sip", "show", "settings", NULL }, sip_show_settings, "Show SIP global settings", show_settings_usage },
13361 { { "sip", "debug", NULL }, sip_do_debug, "Enable SIP debugging", debug_usage },
13362 { { "sip", "debug", "ip", NULL }, sip_do_debug, "Enable SIP debugging on IP", debug_usage },
13363 { { "sip", "debug", "peer", NULL }, sip_do_debug, "Enable SIP debugging on Peername", debug_usage, complete_sip_debug_peer },
13364 { { "sip", "show", "peer", NULL }, sip_show_peer, "Show details on specific SIP peer", show_peer_usage, complete_sip_show_peer },
13365 { { "sip", "show", "peers", NULL }, sip_show_peers, "Show defined SIP peers", show_peers_usage },
13366 { { "sip", "prune", "realtime", NULL }, sip_prune_realtime,
13367 "Prune cached Realtime object(s)", prune_realtime_usage },
13368 { { "sip", "prune", "realtime", "peer", NULL }, sip_prune_realtime,
13369 "Prune cached Realtime peer(s)", prune_realtime_usage, complete_sip_prune_realtime_peer },
13370 { { "sip", "prune", "realtime", "user", NULL }, sip_prune_realtime,
13371 "Prune cached Realtime user(s)", prune_realtime_usage, complete_sip_prune_realtime_user },
13372 { { "sip", "show", "inuse", NULL }, sip_show_inuse, "List all inuse/limits", show_inuse_usage },
13373 { { "sip", "show", "registry", NULL }, sip_show_registry, "Show SIP registration status", show_reg_usage },
13374 { { "sip", "history", NULL }, sip_do_history, "Enable SIP history", history_usage },
13375 { { "sip", "no", "history", NULL }, sip_no_history, "Disable SIP history", no_history_usage },
13376 { { "sip", "no", "debug", NULL }, sip_no_debug, "Disable SIP debugging", no_debug_usage },
13377 { { "sip", "reload", NULL }, sip_reload, "Reload SIP configuration", sip_reload_usage },
13378 };
13379
13380
13381 int load_module()
13382 {
13383 ASTOBJ_CONTAINER_INIT(&userl);
13384 ASTOBJ_CONTAINER_INIT(&peerl);
13385 ASTOBJ_CONTAINER_INIT(®l);
13386
13387 sched = sched_context_create();
13388 if (!sched) {
13389 ast_log(LOG_WARNING, "Unable to create schedule context\n");
13390 }
13391
13392 io = io_context_create();
13393 if (!io) {
13394 ast_log(LOG_WARNING, "Unable to create I/O context\n");
13395 }
13396
13397 reload_config();
13398
13399
13400 if (ast_channel_register(&sip_tech)) {
13401 ast_log(LOG_ERROR, "Unable to register channel type %s\n", channeltype);
13402 return -1;
13403 }
13404
13405
13406 ast_cli_register_multiple(my_clis, sizeof(my_clis)/ sizeof(my_clis[0]));
13407
13408
13409 ast_rtp_proto_register(&sip_rtp);
13410
13411
13412 ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
13413
13414
13415 ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
13416 ast_register_application(app_sipgetheader, sip_getheader, synopsis_sipgetheader, descrip_sipgetheader);
13417
13418
13419 ast_custom_function_register(&sip_header_function);
13420 ast_custom_function_register(&sippeer_function);
13421 ast_custom_function_register(&sipchaninfo_function);
13422 ast_custom_function_register(&checksipdomain_function);
13423
13424
13425 ast_manager_register2("SIPpeers", EVENT_FLAG_SYSTEM, manager_sip_show_peers,
13426 "List SIP peers (text format)", mandescr_show_peers);
13427 ast_manager_register2("SIPshowpeer", EVENT_FLAG_SYSTEM, manager_sip_show_peer,
13428 "Show SIP peer (text format)", mandescr_show_peer);
13429
13430 sip_poke_all_peers();
13431 sip_send_all_registers();
13432
13433
13434 restart_monitor();
13435
13436 return 0;
13437 }
13438
13439 int unload_module()
13440 {
13441 struct sip_pvt *p, *pl;
13442
13443
13444 ast_channel_unregister(&sip_tech);
13445
13446 ast_custom_function_unregister(&sipchaninfo_function);
13447 ast_custom_function_unregister(&sippeer_function);
13448 ast_custom_function_unregister(&sip_header_function);
13449 ast_custom_function_unregister(&checksipdomain_function);
13450
13451 ast_unregister_application(app_dtmfmode);
13452 ast_unregister_application(app_sipaddheader);
13453 ast_unregister_application(app_sipgetheader);
13454
13455 ast_cli_unregister_multiple(my_clis, sizeof(my_clis) / sizeof(my_clis[0]));
13456
13457 ast_rtp_proto_unregister(&sip_rtp);
13458
13459 ast_manager_unregister("SIPpeers");
13460 ast_manager_unregister("SIPshowpeer");
13461
13462 if (!ast_mutex_lock(&iflock)) {
13463
13464 p = iflist;
13465 while (p) {
13466 if (p->owner)
13467 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
13468 p = p->next;
13469 }
13470 ast_mutex_unlock(&iflock);
13471 } else {
13472 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
13473 return -1;
13474 }
13475
13476 if (!ast_mutex_lock(&monlock)) {
13477 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
13478 pthread_cancel(monitor_thread);
13479 pthread_kill(monitor_thread, SIGURG);
13480 pthread_join(monitor_thread, NULL);
13481 }
13482 monitor_thread = AST_PTHREADT_STOP;
13483 ast_mutex_unlock(&monlock);
13484 } else {
13485 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
13486 return -1;
13487 }
13488
13489 if (!ast_mutex_lock(&iflock)) {
13490
13491 p = iflist;
13492 while (p) {
13493 pl = p;
13494 p = p->next;
13495
13496 ast_mutex_destroy(&pl->lock);
13497 if (pl->chanvars) {
13498 ast_variables_destroy(pl->chanvars);
13499 pl->chanvars = NULL;
13500 }
13501 free(pl);
13502 }
13503 iflist = NULL;
13504 ast_mutex_unlock(&iflock);
13505 } else {
13506 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
13507 return -1;
13508 }
13509
13510
13511 ast_free_ha(localaddr);
13512
13513 ASTOBJ_CONTAINER_DESTROYALL(&userl, sip_destroy_user);
13514 ASTOBJ_CONTAINER_DESTROY(&userl);
13515 ASTOBJ_CONTAINER_DESTROYALL(&peerl, sip_destroy_peer);
13516 ASTOBJ_CONTAINER_DESTROY(&peerl);
13517 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
13518 ASTOBJ_CONTAINER_DESTROY(®l);
13519
13520 clear_realm_authentication(authl);
13521 clear_sip_domains();
13522 close(sipsock);
13523 sched_context_destroy(sched);
13524
13525 return 0;
13526 }
13527
13528 int usecount()
13529 {
13530 return usecnt;
13531 }
13532
13533 char *key()
13534 {
13535 return ASTERISK_GPL_KEY;
13536 }
13537
13538 char *description()
13539 {
13540 return (char *) desc;
13541 }
13542
13543