Thu Oct 9 06:44:50 2008

Asterisk developer's documentation


chan_sip.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  * \brief Implementation of Session Initiation Protocol
00022  * 
00023  * Implementation of RFC 3261 - without S/MIME, TCP and TLS support
00024  * Configuration file \link Config_sip sip.conf \endlink
00025  *
00026  * \todo SIP over TCP
00027  * \todo SIP over TLS
00028  * \todo Better support of forking
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: 120109 $")
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/devicestate.h"
00082 #include "asterisk/linkedlists.h"
00083 
00084 #ifdef OSP_SUPPORT
00085 #include "asterisk/astosp.h"
00086 #endif
00087 
00088 #ifndef DEFAULT_USERAGENT
00089 #define DEFAULT_USERAGENT "Asterisk PBX"
00090 #endif
00091  
00092 #define VIDEO_CODEC_MASK   0x1fc0000 /* Video codecs from H.261 thru AST_FORMAT_MAX_VIDEO */
00093 #ifndef IPTOS_MINCOST
00094 #define IPTOS_MINCOST      0x02
00095 #endif
00096 
00097 /* #define VOCAL_DATA_HACK */
00098 
00099 #define SIPDUMPER
00100 #define DEFAULT_DEFAULT_EXPIRY  120
00101 #define DEFAULT_MAX_EXPIRY 3600
00102 #define DEFAULT_REGISTRATION_TIMEOUT   20
00103 #define DEFAULT_MAX_FORWARDS  "70"
00104 
00105 /* guard limit must be larger than guard secs */
00106 /* guard min must be < 1000, and should be >= 250 */
00107 #define EXPIRY_GUARD_SECS  15 /* How long before expiry do we reregister */
00108 #define EXPIRY_GUARD_LIMIT 30 /* Below here, we use EXPIRY_GUARD_PCT instead of 
00109                   EXPIRY_GUARD_SECS */
00110 #define EXPIRY_GUARD_MIN   500   /* This is the minimum guard time applied. If 
00111                   GUARD_PCT turns out to be lower than this, it 
00112                   will use this time instead.
00113                   This is in milliseconds. */
00114 #define EXPIRY_GUARD_PCT   0.20  /* Percentage of expires timeout to use when 
00115                   below EXPIRY_GUARD_LIMIT */
00116 
00117 #define SIP_LEN_CONTACT    256
00118 
00119 static int max_expiry = DEFAULT_MAX_EXPIRY;
00120 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00121 
00122 #ifndef MAX
00123 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00124 #endif
00125 
00126 #define CALLERID_UNKNOWN   "Unknown"
00127 
00128 
00129 
00130 #define DEFAULT_MAXMS      2000     /* Must be faster than 2 seconds by default */
00131 #define DEFAULT_FREQ_OK    60 * 1000   /* How often to check for the host to be up */
00132 #define DEFAULT_FREQ_NOTOK 10 * 1000   /* How often to check, if the host is down... */
00133 
00134 #define DEFAULT_RETRANS    1000     /* How frequently to retransmit */
00135                   /* 2 * 500 ms in RFC 3261 */
00136 #define MAX_RETRANS     6     /* Try only 6 times for retransmissions, a total of 7 transmissions */
00137 #define MAX_AUTHTRIES      3     /* Try authentication three times, then fail */
00138 
00139 
00140 #define DEBUG_READ   0        /* Recieved data  */
00141 #define DEBUG_SEND   1        /* Transmit data  */
00142 
00143 static const char desc[] = "Session Initiation Protocol (SIP)";
00144 static const char channeltype[] = "SIP";
00145 static const char config[] = "sip.conf";
00146 static const char notify_config[] = "sip_notify.conf";
00147 
00148 #define RTP    1
00149 #define NO_RTP 0
00150 
00151 /* Do _NOT_ make any changes to this enum, or the array following it;
00152    if you think you are doing the right thing, you are probably
00153    not doing the right thing. If you think there are changes
00154    needed, get someone else to review them first _before_
00155    submitting a patch. If these two lists do not match properly
00156    bad things will happen.
00157 */
00158 
00159 enum subscriptiontype { 
00160    NONE = 0,
00161    XPIDF_XML,
00162    DIALOG_INFO_XML,
00163    CPIM_PIDF_XML,
00164    PIDF_XML
00165 };
00166 
00167 static const struct cfsubscription_types {
00168    enum subscriptiontype type;
00169    const char * const event;
00170    const char * const mediatype;
00171    const char * const text;
00172 } subscription_types[] = {
00173    { NONE,            "-",        "unknown",                   "unknown" },
00174    /* IETF draft: draft-ietf-sipping-dialog-package-05.txt */
00175    { DIALOG_INFO_XML, "dialog",   "application/dialog-info+xml", "dialog-info+xml" },
00176    { CPIM_PIDF_XML,   "presence", "application/cpim-pidf+xml",   "cpim-pidf+xml" },  /* RFC 3863 */
00177    { PIDF_XML,        "presence", "application/pidf+xml",        "pidf+xml" },       /* RFC 3863 */
00178    { XPIDF_XML,       "presence", "application/xpidf+xml",       "xpidf+xml" }       /* Pre-RFC 3863 with MS additions */
00179 };
00180 
00181 enum sipmethod {
00182    SIP_UNKNOWN,
00183    SIP_RESPONSE,
00184    SIP_REGISTER,
00185    SIP_OPTIONS,
00186    SIP_NOTIFY,
00187    SIP_INVITE,
00188    SIP_ACK,
00189    SIP_PRACK,
00190    SIP_BYE,
00191    SIP_REFER,
00192    SIP_SUBSCRIBE,
00193    SIP_MESSAGE,
00194    SIP_UPDATE,
00195    SIP_INFO,
00196    SIP_CANCEL,
00197    SIP_PUBLISH,
00198 } sip_method_list;
00199 
00200 enum sip_auth_type {
00201    PROXY_AUTH,
00202    WWW_AUTH,
00203 };
00204 
00205 /*! XXX Note that sip_methods[i].id == i must hold or the code breaks */
00206 static const struct  cfsip_methods { 
00207    enum sipmethod id;
00208    int need_rtp;     /*!< when this is the 'primary' use for a pvt structure, does it need RTP? */
00209    char * const text;
00210    int can_create;   /*!< 0=can't create, 1 can create, 2 can create, but not supported */
00211 } sip_methods[] = {
00212    { SIP_UNKNOWN,  RTP,    "-UNKNOWN-", 2 },
00213    { SIP_RESPONSE,    NO_RTP, "SIP/2.0", 0 },
00214    { SIP_REGISTER,    NO_RTP, "REGISTER", 1 },
00215    { SIP_OPTIONS,  NO_RTP, "OPTIONS", 1 },
00216    { SIP_NOTIFY,   NO_RTP, "NOTIFY", 2 },
00217    { SIP_INVITE,   RTP,    "INVITE", 1 },
00218    { SIP_ACK,   NO_RTP, "ACK", 0 },
00219    { SIP_PRACK,    NO_RTP, "PRACK", 2 },
00220    { SIP_BYE,   NO_RTP, "BYE", 0 },
00221    { SIP_REFER,    NO_RTP, "REFER", 2 },
00222    { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", 1 },
00223    { SIP_MESSAGE,  NO_RTP, "MESSAGE", 1 },
00224    { SIP_UPDATE,   NO_RTP, "UPDATE", 0 },
00225    { SIP_INFO,  NO_RTP, "INFO", 0 },
00226    { SIP_CANCEL,   NO_RTP, "CANCEL", 0 },
00227    { SIP_PUBLISH,  NO_RTP, "PUBLISH", 1 }
00228 };
00229 
00230 /*! \brief Structure for conversion between compressed SIP and "normal" SIP */
00231 static const struct cfalias {
00232    char * const fullname;
00233    char * const shortname;
00234 } aliases[] = {
00235    { "Content-Type", "c" },
00236    { "Content-Encoding", "e" },
00237    { "From", "f" },
00238    { "Call-ID", "i" },
00239    { "Contact", "m" },
00240    { "Content-Length", "l" },
00241    { "Subject", "s" },
00242    { "To", "t" },
00243    { "Supported", "k" },
00244    { "Refer-To", "r" },
00245    { "Referred-By", "b" },
00246    { "Allow-Events", "u" },
00247    { "Event", "o" },
00248    { "Via", "v" },
00249    { "Accept-Contact",      "a" },
00250    { "Reject-Contact",      "j" },
00251    { "Request-Disposition", "d" },
00252    { "Session-Expires",     "x" },
00253 };
00254 
00255 /*!  Define SIP option tags, used in Require: and Supported: headers 
00256    We need to be aware of these properties in the phones to use 
00257    the replace: header. We should not do that without knowing
00258    that the other end supports it... 
00259    This is nothing we can configure, we learn by the dialog
00260    Supported: header on the REGISTER (peer) or the INVITE
00261    (other devices)
00262    We are not using many of these today, but will in the future.
00263    This is documented in RFC 3261
00264 */
00265 #define SUPPORTED    1
00266 #define NOT_SUPPORTED      0
00267 
00268 #define SIP_OPT_REPLACES   (1 << 0)
00269 #define SIP_OPT_100REL     (1 << 1)
00270 #define SIP_OPT_TIMER      (1 << 2)
00271 #define SIP_OPT_EARLY_SESSION (1 << 3)
00272 #define SIP_OPT_JOIN    (1 << 4)
00273 #define SIP_OPT_PATH    (1 << 5)
00274 #define SIP_OPT_PREF    (1 << 6)
00275 #define SIP_OPT_PRECONDITION  (1 << 7)
00276 #define SIP_OPT_PRIVACY    (1 << 8)
00277 #define SIP_OPT_SDP_ANAT   (1 << 9)
00278 #define SIP_OPT_SEC_AGREE  (1 << 10)
00279 #define SIP_OPT_EVENTLIST  (1 << 11)
00280 #define SIP_OPT_GRUU    (1 << 12)
00281 #define SIP_OPT_TARGET_DIALOG (1 << 13)
00282 
00283 /*! \brief List of well-known SIP options. If we get this in a require,
00284    we should check the list and answer accordingly. */
00285 static const struct cfsip_options {
00286    int id;        /*!< Bitmap ID */
00287    int supported;    /*!< Supported by Asterisk ? */
00288    char * const text;   /*!< Text id, as in standard */
00289 } sip_options[] = {
00290    /* Replaces: header for transfer */
00291    { SIP_OPT_REPLACES,  SUPPORTED,  "replaces" },  
00292    /* RFC3262: PRACK 100% reliability */
00293    { SIP_OPT_100REL, NOT_SUPPORTED, "100rel" }, 
00294    /* SIP Session Timers */
00295    { SIP_OPT_TIMER,  NOT_SUPPORTED, "timer" },
00296    /* RFC3959: SIP Early session support */
00297    { SIP_OPT_EARLY_SESSION, NOT_SUPPORTED,   "early-session" },
00298    /* SIP Join header support */
00299    { SIP_OPT_JOIN,      NOT_SUPPORTED, "join" },
00300    /* RFC3327: Path support */
00301    { SIP_OPT_PATH,      NOT_SUPPORTED, "path" },
00302    /* RFC3840: Callee preferences */
00303    { SIP_OPT_PREF,      NOT_SUPPORTED, "pref" },
00304    /* RFC3312: Precondition support */
00305    { SIP_OPT_PRECONDITION, NOT_SUPPORTED, "precondition" },
00306    /* RFC3323: Privacy with proxies*/
00307    { SIP_OPT_PRIVACY,   NOT_SUPPORTED, "privacy" },
00308    /* RFC4092: Usage of the SDP ANAT Semantics in the SIP */
00309    { SIP_OPT_SDP_ANAT,  NOT_SUPPORTED, "sdp-anat" },
00310    /* RFC3329: Security agreement mechanism */
00311    { SIP_OPT_SEC_AGREE, NOT_SUPPORTED, "sec_agree" },
00312    /* SIMPLE events:  draft-ietf-simple-event-list-07.txt */
00313    { SIP_OPT_EVENTLIST, NOT_SUPPORTED, "eventlist" },
00314    /* GRUU: Globally Routable User Agent URI's */
00315    { SIP_OPT_GRUU,      NOT_SUPPORTED, "gruu" },
00316    /* Target-dialog: draft-ietf-sip-target-dialog-00.txt */
00317    { SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED, "target-dialog" },
00318 };
00319 
00320 
00321 /*! \brief SIP Methods we support */
00322 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
00323 
00324 /*! \brief SIP Extensions we support */
00325 #define SUPPORTED_EXTENSIONS "replaces" 
00326 
00327 #define DEFAULT_SIP_PORT   5060  /*!< From RFC 3261 (former 2543) */
00328 #define SIP_MAX_PACKET     4096  /*!< Also from RFC 3261 (2543), should sub headers tho */
00329 
00330 static char default_useragent[AST_MAX_EXTENSION] = DEFAULT_USERAGENT;
00331 
00332 #define DEFAULT_CONTEXT "default"
00333 static char default_context[AST_MAX_CONTEXT] = DEFAULT_CONTEXT;
00334 static char default_subscribecontext[AST_MAX_CONTEXT];
00335 
00336 #define DEFAULT_VMEXTEN "asterisk"
00337 static char global_vmexten[AST_MAX_EXTENSION] = DEFAULT_VMEXTEN;
00338 
00339 static char default_language[MAX_LANGUAGE] = "";
00340 
00341 #define DEFAULT_CALLERID "asterisk"
00342 static char default_callerid[AST_MAX_EXTENSION] = DEFAULT_CALLERID;
00343 
00344 static char default_fromdomain[AST_MAX_EXTENSION] = "";
00345 
00346 #define DEFAULT_NOTIFYMIME "application/simple-message-summary"
00347 static char default_notifymime[AST_MAX_EXTENSION] = DEFAULT_NOTIFYMIME;
00348 
00349 static int global_notifyringing = 1;   /*!< Send notifications on ringing */
00350 
00351 static int global_alwaysauthreject = 0;   /*!< Send 401 Unauthorized for all failing requests */
00352 
00353 static int default_qualify = 0;     /*!< Default Qualify= setting */
00354 
00355 static struct ast_flags global_flags = {0};     /*!< global SIP_ flags */
00356 static struct ast_flags global_flags_page2 = {0};  /*!< more global SIP_ flags */
00357 
00358 static int srvlookup = 0;     /*!< SRV Lookup on or off. Default is off, RFC behavior is on */
00359 
00360 static int pedanticsipchecking = 0; /*!< Extra checking ?  Default off */
00361 
00362 static int autocreatepeer = 0;      /*!< Auto creation of peers at registration? Default off. */
00363 
00364 static int relaxdtmf = 0;
00365 
00366 static int global_rtptimeout = 0;
00367 
00368 static int global_rtpholdtimeout = 0;
00369 
00370 static int global_rtpkeepalive = 0;
00371 
00372 static int global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;  
00373 static int global_regattempts_max = 0;
00374 
00375 /* Object counters */
00376 static int suserobjs = 0;
00377 static int ruserobjs = 0;
00378 static int speerobjs = 0;
00379 static int rpeerobjs = 0;
00380 static int apeerobjs = 0;
00381 static int regobjs = 0;
00382 
00383 static int global_allowguest = 1;    /*!< allow unauthenticated users/peers to connect? */
00384 
00385 #define DEFAULT_MWITIME 10
00386 static int global_mwitime = DEFAULT_MWITIME; /*!< Time between MWI checks for peers */
00387 
00388 static int usecnt =0;
00389 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00390 
00391 AST_MUTEX_DEFINE_STATIC(rand_lock);
00392 
00393 /*! \brief Protect the interface list (of sip_pvt's) */
00394 AST_MUTEX_DEFINE_STATIC(iflock);
00395 
00396 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
00397    when it's doing something critical. */
00398 AST_MUTEX_DEFINE_STATIC(netlock);
00399 
00400 AST_MUTEX_DEFINE_STATIC(monlock);
00401 
00402 /*! \brief This is the thread for the monitor which checks for input on the channels
00403    which are not currently in use.  */
00404 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00405 
00406 static int restart_monitor(void);
00407 
00408 /*! \brief Codecs that we support by default: */
00409 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
00410 
00411 static struct in_addr __ourip;
00412 static struct sockaddr_in outboundproxyip;
00413 static int ourport;
00414 
00415 #define SIP_DEBUG_CONFIG 1 << 0
00416 #define SIP_DEBUG_CONSOLE 1 << 1
00417 static int sipdebug = 0;
00418 static struct sockaddr_in debugaddr;
00419 
00420 static int tos = 0;
00421 
00422 static int videosupport = 0;
00423 
00424 static int compactheaders = 0;            /*!< send compact sip headers */
00425 
00426 static int recordhistory = 0;          /*!< Record SIP history. Off by default */
00427 static int dumphistory = 0;            /*!< Dump history to verbose before destroying SIP dialog */
00428 
00429 static char global_musicclass[MAX_MUSICCLASS] = "";   /*!< Global music on hold class */
00430 #define DEFAULT_REALM   "asterisk"
00431 static char global_realm[MAXHOSTNAMELEN] = DEFAULT_REALM;   /*!< Default realm */
00432 static char regcontext[AST_MAX_CONTEXT] = "";      /*!< Context for auto-extensions */
00433 
00434 #define DEFAULT_EXPIRY 900          /*!< Expire slowly */
00435 static int expiry = DEFAULT_EXPIRY;
00436 
00437 #define DEFAULT_T1MIN   100            /*!< Minimial T1 roundtrip time - ms */
00438 
00439 static struct sched_context *sched;
00440 static struct io_context *io;
00441 static int *sipsock_read_id;
00442 
00443 #define SIP_MAX_HEADERS    64       /*!< Max amount of SIP headers to read */
00444 #define SIP_MAX_LINES      64       /*!< Max amount of lines in SIP attachment (like SDP) */
00445 
00446 #define DEC_CALL_LIMIT  0
00447 #define INC_CALL_LIMIT  1
00448 
00449 static struct ast_codec_pref prefs;
00450 
00451 
00452 /*! \brief sip_request: The data grabbed from the UDP socket */
00453 struct sip_request {
00454    char *rlPart1;       /*!< SIP Method Name or "SIP/2.0" protocol version */
00455    char *rlPart2;       /*!< The Request URI or Response Status */
00456    int len;    /*!< Length */
00457    int headers;      /*!< # of SIP Headers */
00458    int method;    /*!< Method of this request */
00459    char *header[SIP_MAX_HEADERS];
00460    int lines;     /*!< Body Content */
00461    char *line[SIP_MAX_LINES];
00462    char data[SIP_MAX_PACKET];
00463    int debug;     /*!< Debug flag for this packet */
00464    unsigned int flags;  /*!< SIP_PKT Flags for this packet */
00465    unsigned int sdp_start; /*!< the line number where the SDP begins */
00466    unsigned int sdp_end;   /*!< the line number where the SDP ends */
00467 };
00468 
00469 struct sip_pkt;
00470 
00471 /*! \brief Parameters to the transmit_invite function */
00472 struct sip_invite_param {
00473    char *distinctive_ring; /*!< Distinctive ring header */
00474    char *osptoken;      /*!< OSP token for this call */
00475    int addsipheaders;   /*!< Add extra SIP headers */
00476    char *uri_options;   /*!< URI options to add to the URI */
00477    char *vxml_url;      /*!< VXML url for Cisco phones */
00478    char *auth;    /*!< Authentication */
00479    char *authheader; /*!< Auth header */
00480    enum sip_auth_type auth_type; /*!< Authentication type */
00481 };
00482 
00483 struct sip_route {
00484    struct sip_route *next;
00485    char hop[0];
00486 };
00487 
00488 enum domain_mode {
00489    SIP_DOMAIN_AUTO,  /*!< This domain is auto-configured */
00490    SIP_DOMAIN_CONFIG,   /*!< This domain is from configuration */
00491 };
00492 
00493 struct domain {
00494    char domain[MAXHOSTNAMELEN];     /*!< SIP domain we are responsible for */
00495    char context[AST_MAX_EXTENSION]; /*!< Incoming context for this domain */
00496    enum domain_mode mode;        /*!< How did we find this domain? */
00497    AST_LIST_ENTRY(domain) list;     /*!< List mechanics */
00498 };
00499 
00500 static AST_LIST_HEAD_STATIC(domain_list, domain);  /*!< The SIP domain list */
00501 
00502 int allow_external_domains;      /*!< Accept calls to external SIP domains? */
00503 
00504 /*! \brief sip_history: Structure for saving transactions within a SIP dialog */
00505 struct sip_history {
00506    char event[80];
00507    struct sip_history *next;
00508 };
00509 
00510 /*! \brief sip_auth: Creadentials for authentication to other SIP services */
00511 struct sip_auth {
00512    char realm[AST_MAX_EXTENSION];  /*!< Realm in which these credentials are valid */
00513    char username[256];             /*!< Username */
00514    char secret[256];               /*!< Secret */
00515    char md5secret[256];            /*!< MD5Secret */
00516    struct sip_auth *next;          /*!< Next auth structure in list */
00517 };
00518 
00519 #define SIP_ALREADYGONE    (1 << 0) /*!< Whether or not we've already been destroyed by our peer */
00520 #define SIP_NEEDDESTROY    (1 << 1) /*!< if we need to be destroyed */
00521 #define SIP_NOVIDEO     (1 << 2) /*!< Didn't get video in invite, don't offer */
00522 #define SIP_RINGING     (1 << 3) /*!< Have sent 180 ringing */
00523 #define SIP_PROGRESS_SENT  (1 << 4) /*!< Have sent 183 message progress */
00524 #define SIP_NEEDREINVITE   (1 << 5) /*!< Do we need to send another reinvite? */
00525 #define SIP_PENDINGBYE     (1 << 6) /*!< Need to send bye after we ack? */
00526 #define SIP_GOTREFER    (1 << 7) /*!< Got a refer? */
00527 #define SIP_PROMISCREDIR   (1 << 8) /*!< Promiscuous redirection */
00528 #define SIP_TRUSTRPID      (1 << 9) /*!< Trust RPID headers? */
00529 #define SIP_USEREQPHONE    (1 << 10)   /*!< Add user=phone to numeric URI. Default off */
00530 #define SIP_REALTIME    (1 << 11)   /*!< Flag for realtime users */
00531 #define SIP_USECLIENTCODE  (1 << 12)   /*!< Trust X-ClientCode info message */
00532 #define SIP_OUTGOING    (1 << 13)   /*!< Is this an outgoing call? */
00533 #define SIP_SELFDESTRUCT   (1 << 14)   /*!< This is an autocreated peer */
00534 #define SIP_CAN_BYE     (1 << 15)   /*!< Can we send BYE for this dialog? */
00535 /* --- Choices for DTMF support in SIP channel */
00536 #define SIP_DTMF     (3 << 16)   /*!< three settings, uses two bits */
00537 #define SIP_DTMF_RFC2833   (0 << 16)   /*!< RTP DTMF */
00538 #define SIP_DTMF_INBAND    (1 << 16)   /*!< Inband audio, only for ULAW/ALAW */
00539 #define SIP_DTMF_INFO      (2 << 16)   /*!< SIP Info messages */
00540 #define SIP_DTMF_AUTO      (3 << 16)   /*!< AUTO switch between rfc2833 and in-band DTMF */
00541 /* NAT settings */
00542 #define SIP_NAT         (3 << 18)   /*!< four settings, uses two bits */
00543 #define SIP_NAT_NEVER      (0 << 18)   /*!< No nat support */
00544 #define SIP_NAT_RFC3581    (1 << 18)
00545 #define SIP_NAT_ROUTE      (2 << 18)
00546 #define SIP_NAT_ALWAYS     (3 << 18)
00547 /* re-INVITE related settings */
00548 #define SIP_REINVITE    (3 << 20)   /*!< two bits used */
00549 #define SIP_CAN_REINVITE   (1 << 20)   /*!< allow peers to be reinvited to send media directly p2p */
00550 #define SIP_REINVITE_UPDATE   (2 << 20)   /*!< use UPDATE (RFC3311) when reinviting this peer */
00551 /* "insecure" settings */
00552 #define SIP_INSECURE_PORT  (1 << 22)   /*!< don't require matching port for incoming requests */
00553 #define SIP_INSECURE_INVITE   (1 << 23)   /*!< don't require authentication for incoming INVITEs */
00554 /* Sending PROGRESS in-band settings */
00555 #define SIP_PROG_INBAND    (3 << 24)   /*!< three settings, uses two bits */
00556 #define SIP_PROG_INBAND_NEVER (0 << 24)
00557 #define SIP_PROG_INBAND_NO (1 << 24)
00558 #define SIP_PROG_INBAND_YES   (2 << 24)
00559 /* Open Settlement Protocol authentication */
00560 #define SIP_OSPAUTH     (3 << 26)   /*!< four settings, uses two bits */
00561 #define SIP_OSPAUTH_NO     (0 << 26)
00562 #define SIP_OSPAUTH_GATEWAY   (1 << 26)
00563 #define SIP_OSPAUTH_PROXY  (2 << 26)
00564 #define SIP_OSPAUTH_EXCLUSIVE (3 << 26)
00565 /* Call states */
00566 #define SIP_CALL_ONHOLD    (1 << 28)    
00567 #define SIP_CALL_LIMIT     (1 << 29)
00568 /* Remote Party-ID Support */
00569 #define SIP_SENDRPID    (1 << 30)
00570 #define SIP_INC_COUNT      (1 << 31)   /* Did this connection increment the counter of in-use calls? */
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 /* a new page of flags for peer */
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) /*!< Is this a dynamic peer? */
00584 
00585 /* SIP packet flags */
00586 #define SIP_PKT_DEBUG      (1 << 0) /*!< Debug this packet */
00587 #define SIP_PKT_WITH_TOTAG (1 << 1) /*!< This packet has a to-tag */
00588 
00589 static int global_rtautoclear;
00590 
00591 /*! \brief sip_pvt: PVT structures are used for each SIP conversation, ie. a call  */
00592 static struct sip_pvt {
00593    ast_mutex_t lock;       /*!< Channel private lock */
00594    int method;          /*!< SIP method of this packet */
00595    char callid[128];       /*!< Global CallID */
00596    char randdata[80];         /*!< Random data */
00597    struct ast_codec_pref prefs;     /*!< codec prefs */
00598    unsigned int ocseq;        /*!< Current outgoing seqno */
00599    unsigned int icseq;        /*!< Current incoming seqno */
00600    ast_group_t callgroup;        /*!< Call group */
00601    ast_group_t pickupgroup;      /*!< Pickup group */
00602    int lastinvite;            /*!< Last Cseq of invite */
00603    unsigned int flags;        /*!< SIP_ flags */   
00604    int timer_t1;           /*!< SIP timer T1, ms rtt */
00605    unsigned int sipoptions;      /*!< Supported SIP sipoptions on the other end */
00606    int capability;            /*!< Special capability (codec) */
00607    int jointcapability;       /*!< Supported capability at both ends (codecs ) */
00608    int peercapability;        /*!< Supported peer capability */
00609    int prefcodec;          /*!< Preferred codec (outbound only) */
00610    int noncodeccapability;
00611    int jointnoncodeccapability;
00612    int callingpres;        /*!< Calling presentation */
00613    int authtries;          /*!< Times we've tried to authenticate */
00614    int expiry;          /*!< How long we take to expire */
00615    int branch;          /*!< One random number */
00616    char tag[11];           /*!< Another random number */
00617    int sessionid;          /*!< SDP Session ID */
00618    int sessionversion;        /*!< SDP Session Version */
00619    struct sockaddr_in sa;        /*!< Our peer */
00620    struct sockaddr_in redirip;      /*!< Where our RTP should be going if not to us */
00621    struct sockaddr_in vredirip;     /*!< Where our Video RTP should be going if not to us */
00622    int redircodecs;        /*!< Redirect codecs */
00623    struct sockaddr_in recv;      /*!< Received as */
00624    struct in_addr ourip;         /*!< Our IP */
00625    struct ast_channel *owner;    /*!< Who owns us */
00626    char exten[AST_MAX_EXTENSION];      /*!< Extension where to start */
00627    char refer_to[AST_MAX_EXTENSION];   /*!< Place to store REFER-TO extension */
00628    char referred_by[AST_MAX_EXTENSION];   /*!< Place to store REFERRED-BY extension */
00629    char refer_contact[SIP_LEN_CONTACT];   /*!< Place to store Contact info from a REFER extension */
00630    struct sip_pvt *refer_call;      /*!< Call we are referring */
00631    struct sip_route *route;      /*!< Head of linked list of routing steps (fm Record-Route) */
00632    int route_persistant;         /*!< Is this the "real" route? */
00633    char from[256];            /*!< The From: header */
00634    char useragent[256];       /*!< User agent in SIP request */
00635    char context[AST_MAX_CONTEXT];      /*!< Context for this call */
00636    char subscribecontext[AST_MAX_CONTEXT];   /*!< Subscribecontext */
00637    char fromdomain[MAXHOSTNAMELEN]; /*!< Domain to show in the from field */
00638    char fromuser[AST_MAX_EXTENSION];   /*!< User to show in the user field */
00639    char fromname[AST_MAX_EXTENSION];   /*!< Name to show in the user field */
00640    char tohost[MAXHOSTNAMELEN];     /*!< Host we should put in the "to" field */
00641    char language[MAX_LANGUAGE];     /*!< Default language for this call */
00642    char musicclass[MAX_MUSICCLASS]; /*!< Music on Hold class */
00643    char rdnis[256];        /*!< Referring DNIS */
00644    char theirtag[256];        /*!< Their tag */
00645    char username[256];        /*!< [user] name */
00646    char peername[256];        /*!< [peer] name, not set if [user] */
00647    char authname[256];        /*!< Who we use for authentication */
00648    char uri[256];          /*!< Original requested URI */
00649    char okcontacturi[SIP_LEN_CONTACT]; /*!< URI from the 200 OK on INVITE */
00650    char peersecret[256];         /*!< Password */
00651    char peermd5secret[256];
00652    struct sip_auth *peerauth;    /*!< Realm authentication */
00653    char cid_num[256];         /*!< Caller*ID */
00654    char cid_name[256];        /*!< Caller*ID */
00655    char via[256];          /*!< Via: header */
00656    char fullcontact[SIP_LEN_CONTACT];  /*!< The Contact: that the UA registers with us */
00657    char accountcode[AST_MAX_ACCOUNT_CODE];   /*!< Account code */
00658    char our_contact[SIP_LEN_CONTACT];  /*!< Our contact header */
00659    char *rpid;          /*!< Our RPID header */
00660    char *rpid_from;        /*!< Our RPID From header */
00661    char realm[MAXHOSTNAMELEN];      /*!< Authorization realm */
00662    char nonce[256];        /*!< Authorization nonce */
00663    int noncecount;            /*!< Nonce-count */
00664    char opaque[256];       /*!< Opaque nonsense */
00665    char qop[80];           /*!< Quality of Protection, since SIP wasn't complicated enough yet. */
00666    char domain[MAXHOSTNAMELEN];     /*!< Authorization domain */
00667    char lastmsg[256];         /*!< Last Message sent/received */
00668    int amaflags;           /*!< AMA Flags */
00669    int pendinginvite;         /*!< Any pending invite */
00670 #ifdef OSP_SUPPORT
00671    int osphandle;          /*!< OSP Handle for call */
00672    time_t ospstart;        /*!< OSP Start time */
00673    unsigned int osptimelimit;    /*!< OSP call duration limit */
00674 #endif
00675    struct sip_request initreq;      /*!< Initial request */
00676    
00677    int maxtime;            /*!< Max time for first response */
00678    int initid;          /*!< Auto-congest ID if appropriate */
00679    int autokillid;            /*!< Auto-kill ID */
00680    time_t lastrtprx;       /*!< Last RTP received */
00681    time_t lastrtptx;       /*!< Last RTP sent */
00682    int rtptimeout;            /*!< RTP timeout time */
00683    int rtpholdtimeout;        /*!< RTP timeout when on hold */
00684    int rtpkeepalive;       /*!< Send RTP packets for keepalive */
00685    enum subscriptiontype subscribed;   /*!< Is this call a subscription?  */
00686    int stateid;
00687    int laststate;                          /*!< Last known extension state */
00688    int dialogver;
00689    
00690    struct ast_dsp *vad;       /*!< Voice Activation Detection dsp */
00691    
00692    struct sip_peer *peerpoke;    /*!< If this calls is to poke a peer, which one */
00693    struct sip_registry *registry;      /*!< If this is a REGISTER call, to which registry */
00694    struct ast_rtp *rtp;       /*!< RTP Session */
00695    struct ast_rtp *vrtp;         /*!< Video RTP session */
00696    struct sip_pkt *packets;      /*!< Packets scheduled for re-transmission */
00697    struct sip_history *history;     /*!< History of this SIP dialog */