📄 chan_sip.c
字号:
/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2006, Digium, Inc. * * Mark Spencer <markster@digium.com> * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. *//*! * \file * \brief Implementation of Session Initiation Protocol * * \author Mark Spencer <markster@digium.com> * * See Also: * \arg \ref AstCREDITS * * Implementation of RFC 3261 - without S/MIME, TCP and TLS support * Configuration file \link Config_sip sip.conf \endlink * * * \todo SIP over TCP * \todo SIP over TLS * \todo Better support of forking * \todo VIA branch tag transaction checking * \todo Transaction support * * \ingroup channel_drivers * * \par Overview of the handling of SIP sessions * The SIP channel handles several types of SIP sessions, or dialogs, * not all of them being "telephone calls". * - Incoming calls that will be sent to the PBX core * - Outgoing calls, generated by the PBX * - SIP subscriptions and notifications of states and voicemail messages * - SIP registrations, both inbound and outbound * - SIP peer management (peerpoke, OPTIONS) * - SIP text messages * * In the SIP channel, there's a list of active SIP dialogs, which includes * all of these when they are active. "sip show channels" in the CLI will * show most of these, excluding subscriptions which are shown by * "sip show subscriptions" * * \par incoming packets * Incoming packets are received in the monitoring thread, then handled by * sipsock_read(). This function parses the packet and matches an existing * dialog or starts a new SIP dialog. * * sipsock_read sends the packet to handle_request(), that parses a bit more. * if it's a response to an outbound request, it's sent to handle_response(). * If it is a request, handle_request sends it to one of a list of functions * depending on the request type - INVITE, OPTIONS, REFER, BYE, CANCEL etc * sipsock_read locks the ast_channel if it exists (an active call) and * unlocks it after we have processed the SIP message. * * A new INVITE is sent to handle_request_invite(), that will end up * starting a new channel in the PBX, the new channel after that executing * in a separate channel thread. This is an incoming "call". * When the call is answered, either by a bridged channel or the PBX itself * the sip_answer() function is called. * * The actual media - Video or Audio - is mostly handled by the RTP subsystem * in rtp.c * * \par Outbound calls * Outbound calls are set up by the PBX through the sip_request_call() * function. After that, they are activated by sip_call(). * * \par Hanging up * The PBX issues a hangup on both incoming and outgoing calls through * the sip_hangup() function * * \par Deprecated stuff * This is deprecated and will be removed after the 1.4 release * - the SIPUSERAGENT dialplan variable * - the ALERT_INFO dialplan variable *//*** MODULEINFO <depend>res_features</depend> ***/#include "asterisk.h"ASTERISK_FILE_VERSION(__FILE__, "$Revision: 141809 $")#include <stdio.h>#include <ctype.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <net/if.h>#include <errno.h>#include <stdlib.h>#include <fcntl.h>#include <netdb.h>#include <signal.h>#include <sys/signal.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <arpa/inet.h>#include <netinet/ip.h>#include <regex.h>#include "asterisk/lock.h"#include "asterisk/channel.h"#include "asterisk/config.h"#include "asterisk/logger.h"#include "asterisk/module.h"#include "asterisk/pbx.h"#include "asterisk/options.h"#include "asterisk/sched.h"#include "asterisk/io.h"#include "asterisk/rtp.h"#include "asterisk/udptl.h"#include "asterisk/acl.h"#include "asterisk/manager.h"#include "asterisk/callerid.h"#include "asterisk/cli.h"#include "asterisk/app.h"#include "asterisk/musiconhold.h"#include "asterisk/dsp.h"#include "asterisk/features.h"#include "asterisk/srv.h"#include "asterisk/astdb.h"#include "asterisk/causes.h"#include "asterisk/utils.h"#include "asterisk/file.h"#include "asterisk/astobj.h"#include "asterisk/devicestate.h"#include "asterisk/linkedlists.h"#include "asterisk/stringfields.h"#include "asterisk/monitor.h"#include "asterisk/localtime.h"#include "asterisk/abstract_jb.h"#include "asterisk/compiler.h"#include "asterisk/threadstorage.h"#include "asterisk/translate.h"#ifndef FALSE#define FALSE 0#endif#ifndef TRUE#define TRUE 1#endif#define SIPBUFSIZE 512#define XMIT_ERROR -2#define VIDEO_CODEC_MASK 0x1fc0000 /*!< Video codecs from H.261 thru AST_FORMAT_MAX_VIDEO */#ifndef IPTOS_MINCOST#define IPTOS_MINCOST 0x02#endif/* #define VOCAL_DATA_HACK */#define DEFAULT_DEFAULT_EXPIRY 120#define DEFAULT_MIN_EXPIRY 60#define DEFAULT_MAX_EXPIRY 3600#define DEFAULT_REGISTRATION_TIMEOUT 20#define DEFAULT_MAX_FORWARDS "70"/* guard limit must be larger than guard secs *//* guard min must be < 1000, and should be >= 250 */#define EXPIRY_GUARD_SECS 15 /*!< How long before expiry do we reregister */#define EXPIRY_GUARD_LIMIT 30 /*!< Below here, we use EXPIRY_GUARD_PCT instead of EXPIRY_GUARD_SECS */#define EXPIRY_GUARD_MIN 500 /*!< This is the minimum guard time applied. If GUARD_PCT turns out to be lower than this, it will use this time instead. This is in milliseconds. */#define EXPIRY_GUARD_PCT 0.20 /*!< Percentage of expires timeout to use when below EXPIRY_GUARD_LIMIT */#define DEFAULT_EXPIRY 900 /*!< Expire slowly */static int min_expiry = DEFAULT_MIN_EXPIRY; /*!< Minimum accepted registration time */static int max_expiry = DEFAULT_MAX_EXPIRY; /*!< Maximum accepted registration time */static int default_expiry = DEFAULT_DEFAULT_EXPIRY;static int expiry = DEFAULT_EXPIRY;#ifndef MAX#define MAX(a,b) ((a) > (b) ? (a) : (b))#endif#define CALLERID_UNKNOWN "Unknown"#define DEFAULT_MAXMS 2000 /*!< Qualification: Must be faster than 2 seconds by default */#define DEFAULT_FREQ_OK 60 * 1000 /*!< Qualification: How often to check for the host to be up */#define DEFAULT_FREQ_NOTOK 10 * 1000 /*!< Qualification: How often to check, if the host is down... */#define DEFAULT_RETRANS 1000 /*!< How frequently to retransmit Default: 2 * 500 ms in RFC 3261 */#define MAX_RETRANS 6 /*!< Try only 6 times for retransmissions, a total of 7 transmissions */#define SIP_TRANS_TIMEOUT 32000 /*!< SIP request timeout (rfc 3261) 64*T1 \todo Use known T1 for timeout (peerpoke) */#define DEFAULT_TRANS_TIMEOUT -1 /* Use default SIP transaction timeout */#define MAX_AUTHTRIES 3 /*!< Try authentication three times, then fail */#define SIP_MAX_HEADERS 64 /*!< Max amount of SIP headers to read */#define SIP_MAX_LINES 64 /*!< Max amount of lines in SIP attachment (like SDP) */#define SIP_MAX_PACKET 4096 /*!< Also from RFC 3261 (2543), should sub headers tho */#define SDP_MAX_RTPMAP_CODECS 32 /*!< Maximum number of codecs allowed in received SDP */#define INITIAL_CSEQ 101 /*!< our initial sip sequence number *//*! \brief Global jitterbuffer configuration - by default, jb is disabled */static struct ast_jb_conf default_jbconf ={ .flags = 0, .max_size = -1, .resync_threshold = -1, .impl = ""};static struct ast_jb_conf global_jbconf;static const char config[] = "sip.conf";static const char notify_config[] = "sip_notify.conf";#define RTP 1#define NO_RTP 0/*! \brief Authorization scheme for call transfers \note Not a bitfield flag, since there are plans for other modes, like "only allow transfers for authenticated devices" */enum transfermodes { TRANSFER_OPENFORALL, /*!< Allow all SIP transfers */ TRANSFER_CLOSED, /*!< Allow no SIP transfers */};enum sip_result { AST_SUCCESS = 0, AST_FAILURE = -1,};/*! \brief States for the INVITE transaction, not the dialog \note this is for the INVITE that sets up the dialog*/enum invitestates { INV_NONE = 0, /*!< No state at all, maybe not an INVITE dialog */ INV_CALLING = 1, /*!< Invite sent, no answer */ INV_PROCEEDING = 2, /*!< We got/sent 1xx message */ INV_EARLY_MEDIA = 3, /*!< We got 18x message with to-tag back */ INV_COMPLETED = 4, /*!< Got final response with error. Wait for ACK, then CONFIRMED */ INV_CONFIRMED = 5, /*!< Confirmed response - we've got an ack (Incoming calls only) */ INV_TERMINATED = 6, /*!< Transaction done - either successful (AST_STATE_UP) or failed, but done The only way out of this is a BYE from one side */ INV_CANCELLED = 7, /*!< Transaction cancelled by client or server in non-terminated state */};/* Do _NOT_ make any changes to this enum, or the array following it; if you think you are doing the right thing, you are probably not doing the right thing. If you think there are changes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -