Skip to content
Snippets Groups Projects
chan_sip.c 432 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    
    /*! \brief  sip_call: Initiate SIP call from PBX 
     *      used from the dial() application      */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int sip_call(struct ast_channel *ast, char *dest, int timeout)
    {
    	int res;
    	struct sip_pvt *p;
    
    	const char *osphandle = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct varshead *headp;
    	struct ast_var_t *current;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
    		return -1;
    	}
    
    	/* Check whether there is vxml_url, distinctive ring variables */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	headp=&ast->varshead;
    	AST_LIST_TRAVERSE(headp,current,entries) {
    
    		/* Check whether there is a VXML_URL variable */
    
    		if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
    			p->options->vxml_url = ast_var_value(current);
    
                   } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
                           p->options->uri_options = ast_var_value(current);
    
    		} else if (!p->options->distinctive_ring && !strcasecmp(ast_var_name(current), "ALERT_INFO")) {
    
    			/* Check whether there is a ALERT_INFO variable */
    
    			p->options->distinctive_ring = ast_var_value(current);
    		} else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
    
    			/* Check whether there is a variable with a name starting with SIPADDHEADER */
    
    		else if (!p->options->osptoken && !strcasecmp(ast_var_name(current), "OSPTOKEN")) {
    			p->options->osptoken = ast_var_value(current);
    
    		} else if (!osphandle && !strcasecmp(ast_var_name(current), "OSPHANDLE")) {
    
    			osphandle = ast_var_value(current);
    		}
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    	res = 0;
    
    	ast_set_flag(p, SIP_OUTGOING);
    
    	if (!p->options->osptoken || !osphandle || (sscanf(osphandle, "%d", &p->osphandle) != 1)) {
    
    		/* Force Disable OSP support */
    
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Disabling OSP support for this call. osptoken = %s, osphandle = %s\n", p->options->osptoken, osphandle);
    
    		osphandle = NULL;
    		p->osphandle = -1;
    	}
    #endif
    
    	ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
    
    	res = update_call_counter(p, INC_CALL_LIMIT);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if ( res != -1 ) {
    
    		p->callingpres = ast->cid.cid_pres;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		p->jointcapability = p->capability;
    
    		transmit_invite(p, SIP_INVITE, 1, 2);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (p->maxtime) {
    			/* Initialize auto-congest time */
    
    			p->initid = ast_sched_add(sched, p->maxtime * 4, auto_congest, p);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;
    }
    
    
    /*! \brief  sip_registry_destroy: Destroy registry object */
    
    /*	Objects created with the register= statement in static configuration */
    
    static void sip_registry_destroy(struct sip_registry *reg)
    {
    	/* Really delete */
    	if (reg->call) {
    		/* Clear registry before destroying to ensure
    		   we don't get reentered trying to grab the registry lock */
    		reg->call->registry = NULL;
    		sip_destroy(reg->call);
    	}
    	if (reg->expire > -1)
    		ast_sched_del(sched, reg->expire);
    	if (reg->timeout > -1)
    		ast_sched_del(sched, reg->timeout);
    
    	ast_string_field_free_all(reg);
    
    /*! \brief   __sip_destroy: Execute destrucion of SIP dialog structure, release memory */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static void __sip_destroy(struct sip_pvt *p, int lockowner)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct sip_pvt *cur, *prev = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct sip_pkt *cp;
    
    		ast_verbose("Destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
    
    	if (p->stateid > -1)
    		ast_extension_state_del(p->stateid, NULL);
    
    	if (p->initid > -1)
    		ast_sched_del(sched, p->initid);
    	if (p->autokillid > -1)
    		ast_sched_del(sched, p->autokillid);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (p->rtp) {
    		ast_rtp_destroy(p->rtp);
    	}
    
    	if (p->vrtp) {
    		ast_rtp_destroy(p->vrtp);
    	}
    
    	if (p->route) {
    		free_old_route(p->route);
    		p->route = NULL;
    	}
    
    		if (p->registry->call == p)
    			p->registry->call = NULL;
    
    		ASTOBJ_UNREF(p->registry, sip_registry_destroy);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Unlink us from the owner if we have one */
    	if (p->owner) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (lockowner)
    
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (lockowner)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (p->history) {
    		while(!AST_LIST_EMPTY(p->history)) {
    			struct sip_history *hist = AST_LIST_FIRST(p->history);
    			AST_LIST_REMOVE_HEAD(p->history, list);
    			free(hist);
    		}
    		free(p->history);
    		p->history = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	cur = iflist;
    	while(cur) {
    		if (cur == p) {
    			if (prev)
    				prev->next = cur->next;
    			else
    				iflist = cur->next;
    			break;
    		}
    		prev = cur;
    		cur = cur->next;
    	}
    	if (!cur) {
    
    		ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
    		return;
    	} 
    	if (p->initid > -1)
    		ast_sched_del(sched, p->initid);
    
    	while((cp = p->packets)) {
    		p->packets = p->packets->next;
    
    		if (cp->retransid > -1) {
    
    			ast_sched_del(sched, cp->retransid);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (p->chanvars) {
    		ast_variables_destroy(p->chanvars);
    		p->chanvars = NULL;
    	}
    
    	ast_mutex_destroy(&p->lock);
    
    /*! \brief  update_call_counter: Handle call_limit for SIP users 
    
     * Setting a call-limit will cause calls above the limit not to be accepted.
     *
     * Remember that for a type=friend, there's one limit for the user and
     * another for the peer, not a combined call limit.
     * This will cause unexpected behaviour in subscriptions, since a "friend"
     * is *two* devices in Asterisk, not one.
     *
     * Thought: For realtime, we should propably update storage with inuse counter... 
     */
    
    static int update_call_counter(struct sip_pvt *fup, int event)
    
    	int *inuse, *call_limit;
    	int outgoing = ast_test_flag(fup, SIP_OUTGOING);
    	struct sip_user *u = NULL;
    	struct sip_peer *p = NULL;
    
    	if (option_debug > 2)
    		ast_log(LOG_DEBUG, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
    
    	/* Test if we need to check call limits, in order to avoid 
    	   realtime lookups if we do not need it */
    	if (!ast_test_flag(fup, SIP_CALL_LIMIT))
    		return 0;
    
    
    	ast_copy_string(name, fup->username, sizeof(name));
    
    	if (!outgoing)	/* Only check users for incoming calls */
    		u = find_user(name, 1);
    
    
    		if (!p)
    			p = find_peer(fup->peername, NULL, 1);
    
    			ast_copy_string(name, fup->peername, sizeof(name));
    
    			if (option_debug > 1)
    				ast_log(LOG_DEBUG, "%s is not a local user, no call limit\n", name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	switch(event) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* incoming and outgoing affects the inUse counter */
    
    			         if (ast_test_flag(fup, SIP_INC_COUNT))
    
    			} else {
    
    				ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
    			}
    
    		case INC_CALL_LIMIT:
    			if (*call_limit > 0 ) {
    				if (*inuse >= *call_limit) {
    					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);
    
    						ASTOBJ_UNREF(u, sip_destroy_user);
    
    						ASTOBJ_UNREF(p, sip_destroy_peer);
    
    	                ast_set_flag(fup, SIP_INC_COUNT);
    
    				ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
    			}
    
    			break;
    		default:
    
    			ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
    
    		ASTOBJ_UNREF(u, sip_destroy_user);
    
    		ASTOBJ_UNREF(p, sip_destroy_peer);
    
    /*! \brief  sip_destroy: Destroy SIP call structure */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static void sip_destroy(struct sip_pvt *p)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	__sip_destroy(p, 1);
    
    static int transmit_response_reliable(struct sip_pvt *p, char *msg, struct sip_request *req, int fatal);
    
    /*! \brief  hangup_sip2cause: Convert SIP hangup causes to Asterisk hangup causes */
    
    /* Possible values taken from causes.h */
    
    
    	switch(cause) {
    
    		case 401:	/* Unauthorized */
    			return AST_CAUSE_CALL_REJECTED;
    
    		case 403:	/* Not found */
    
    			return AST_CAUSE_CALL_REJECTED;
    
    		case 404:	/* Not found */
    
    			return AST_CAUSE_UNALLOCATED;
    
    		case 405:	/* Method not allowed */
    			return AST_CAUSE_INTERWORKING;
    		case 407:	/* Proxy authentication required */
    			return AST_CAUSE_CALL_REJECTED;
    
    		case 408:	/* No reaction */
    
    			return AST_CAUSE_NO_USER_RESPONSE;
    
    		case 409:	/* Conflict */
    			return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
    		case 410:	/* Gone */
    			return AST_CAUSE_UNALLOCATED;
    		case 411:	/* Length required */
    			return AST_CAUSE_INTERWORKING;
    		case 413:	/* Request entity too large */
    			return AST_CAUSE_INTERWORKING;
    		case 414:	/* Request URI too large */
    			return AST_CAUSE_INTERWORKING;
    		case 415:	/* Unsupported media type */
    			return AST_CAUSE_INTERWORKING;
    		case 420:	/* Bad extension */
    			return AST_CAUSE_NO_ROUTE_DESTINATION;
    
    		case 480:	/* No answer */
    
    			return AST_CAUSE_FAILURE;
    
    		case 481:	/* No answer */
    			return AST_CAUSE_INTERWORKING;
    		case 482:	/* Loop detected */
    			return AST_CAUSE_INTERWORKING;
    
    		case 483:	/* Too many hops */
    
    			return AST_CAUSE_NO_ANSWER;
    
    		case 484:	/* Address incomplete */
    			return AST_CAUSE_INVALID_NUMBER_FORMAT;
    		case 485:	/* Ambigous */
    			return AST_CAUSE_UNALLOCATED;
    
    		case 486:	/* Busy everywhere */
    
    		case 487:	/* Request terminated */
    			return AST_CAUSE_INTERWORKING;
    
    		case 488:	/* No codecs approved */
    			return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
    
    		case 491:	/* Request pending */
    			return AST_CAUSE_INTERWORKING;
    		case 493:	/* Undecipherable */
    			return AST_CAUSE_INTERWORKING;
    
    		case 500:	/* Server internal failure */
    			return AST_CAUSE_FAILURE;
    		case 501:	/* Call rejected */
    			return AST_CAUSE_FACILITY_REJECTED;
    		case 502:	
    			return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
    		case 503:	/* Service unavailable */
    			return AST_CAUSE_CONGESTION;
    
    		case 504:	/* Gateway timeout */
    			return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
    		case 505:	/* SIP version not supported */
    			return AST_CAUSE_INTERWORKING;
    		case 600:	/* Busy everywhere */
    			return AST_CAUSE_USER_BUSY;
    		case 603:	/* Decline */
    			return AST_CAUSE_CALL_REJECTED;
    		case 604:	/* Does not exist anywhere */
    			return AST_CAUSE_UNALLOCATED;
    		case 606:	/* Not acceptable */
    			return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
    
    /*! \brief  hangup_cause2sip: Convert Asterisk hangup causes to SIP codes 
    \verbatim
     Possible values from causes.h
    
            AST_CAUSE_NOTDEFINED    AST_CAUSE_NORMAL        AST_CAUSE_BUSY
            AST_CAUSE_FAILURE       AST_CAUSE_CONGESTION    AST_CAUSE_UNALLOCATED
    
    	In addition to these, a lot of PRI codes is defined in causes.h 
    	...should we take care of them too ?
    	
    	Quote RFC 3398
    
       ISUP Cause value                        SIP response
       ----------------                        ------------
       1  unallocated number                   404 Not Found
       2  no route to network                  404 Not found
       3  no route to destination              404 Not found
       16 normal call clearing                 --- (*)
       17 user busy                            486 Busy here
       18 no user responding                   408 Request Timeout
       19 no answer from the user              480 Temporarily unavailable
       20 subscriber absent                    480 Temporarily unavailable
       21 call rejected                        403 Forbidden (+)
       22 number changed (w/o diagnostic)      410 Gone
       22 number changed (w/ diagnostic)       301 Moved Permanently
       23 redirection to new destination       410 Gone
       26 non-selected user clearing           404 Not Found (=)
       27 destination out of order             502 Bad Gateway
       28 address incomplete                   484 Address incomplete
       29 facility rejected                    501 Not implemented
       31 normal unspecified                   480 Temporarily unavailable
    
    \endverbatim
    
    static char *hangup_cause2sip(int cause)
    {
    	switch(cause)
    	{
    
    		case AST_CAUSE_UNALLOCATED:		/* 1 */
    		case AST_CAUSE_NO_ROUTE_DESTINATION:	/* 3 IAX2: Can't find extension in context */
    		case AST_CAUSE_NO_ROUTE_TRANSIT_NET:	/* 2 */
    			return "404 Not Found";
                    case AST_CAUSE_CONGESTION:		/* 34 */
                    case AST_CAUSE_SWITCH_CONGESTION:	/* 42 */
                            return "503 Service Unavailable";
    		case AST_CAUSE_NO_USER_RESPONSE:	/* 18 */
    			return "408 Request Timeout";
    		case AST_CAUSE_NO_ANSWER:		/* 19 */
    			return "480 Temporarily unavailable";
    		case AST_CAUSE_CALL_REJECTED:		/* 21 */
    			return "403 Forbidden";
    		case AST_CAUSE_NUMBER_CHANGED:		/* 22 */
    			return "410 Gone";
    		case AST_CAUSE_NORMAL_UNSPECIFIED:	/* 31 */
    			return "480 Temporarily unavailable";
    		case AST_CAUSE_INVALID_NUMBER_FORMAT:
    			return "484 Address incomplete";
    		case AST_CAUSE_USER_BUSY:
    			return "486 Busy here";
    
    		case AST_CAUSE_FAILURE:
                    	return "500 Server internal failure";
    
    		case AST_CAUSE_FACILITY_REJECTED:	/* 29 */
    			return "501 Not Implemented";
    		case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
    
    			return "503 Service Unavailable";
    
    		/* Used in chan_iax2 */
    		case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
    
    			return "502 Bad Gateway";
    
    		case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:	/* Can't find codec to connect to host */
    
    			return "488 Not Acceptable Here";
    
    			ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
    
     * Part of PBX interface, called from ast_hangup */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int sip_hangup(struct ast_channel *ast)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int needcancel = 0;
    
    	struct ast_flags locflags = {0};
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return 0;
    	}
    
    Olle Johansson's avatar
    Olle Johansson committed
    	if (option_debug && sipdebug)
    
    		ast_log(LOG_DEBUG, "Hangup call %s, SIP callid %s)\n", ast->name, p->callid);
    
    
    #ifdef OSP_SUPPORT
    	if ((p->osphandle > -1) && (ast->_state == AST_STATE_UP)) {
    		ast_osp_terminate(p->osphandle, AST_CAUSE_NORMAL, p->ospstart, time(NULL) - p->ospstart);
    	}
    #endif	
    
    Olle Johansson's avatar
    Olle Johansson committed
    	if (option_debug && sipdebug)
    		ast_log(LOG_DEBUG, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Determine how to disconnect */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (p->owner != ast) {
    
    		ast_log(LOG_WARNING, "Huh?  We aren't the owner? Can't hangup call.\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return 0;
    	}
    
    	/* If the call is not UP, we need to send CANCEL instead of BYE */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		needcancel = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Disconnect */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (p->vad) {
    		ast_dsp_free(p->vad);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->owner = NULL;
    
    	ast_mutex_lock(&usecnt_lock);
    	usecnt--;
    	ast_mutex_unlock(&usecnt_lock);
    	ast_update_use_count();
    
    
    	ast_set_flag(&locflags, SIP_NEEDDESTROY);	
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Start the process if it's not already started */
    
    	if (!ast_test_flag(p, SIP_ALREADYGONE) && !ast_strlen_zero(p->initreq.data)) {
    
    			if (ast_test_flag(p, SIP_OUTGOING)) {
    
    				transmit_request_with_auth(p, SIP_CANCEL, p->ocseq, 1, 0);
    
    				/* Actually don't destroy us yet, wait for the 487 on our original 
    
    				   INVITE, but do set an autodestruct just in case we never get it. */
    
    				ast_clear_flag(&locflags, SIP_NEEDDESTROY);
    
    				/* stop retransmitting an INVITE that has not received a response */
    				__sip_pretend_ack(p);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				if ( p->initid != -1 ) {
    					/* channel still up - reverse dec of inUse counter
    					   only if the channel is not auto-congested */
    
    Mark Spencer's avatar
    Mark Spencer committed
    				}
    
    				char *res;
    				if (ast->hangupcause && ((res = hangup_cause2sip(ast->hangupcause)))) {
    
    					transmit_response_reliable(p, res, &p->initreq, 1);
    
    					transmit_response_reliable(p, "603 Declined", &p->initreq, 1);
    
    			if (!p->pendinginvite) {
    				/* Send a hangup */
    
    				transmit_request_with_auth(p, SIP_BYE, 0, 1, 1);
    
    				/* Note we will need a BYE when this all settles out
    				   but we can't send one while we have "INVITE" outstanding. */
    
    				ast_set_flag(p, SIP_PENDINGBYE);	
    				ast_clear_flag(p, SIP_NEEDREINVITE);	
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    	ast_copy_flags(p, (&locflags), SIP_NEEDDESTROY);	
    
    /*! \brief  sip_answer: Answer SIP call , send 200 OK on Invite 
     * Part of PBX interface */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int sip_answer(struct ast_channel *ast)
    {
    
    	int res = 0,fmt;
    
    	ast_mutex_lock(&p->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (ast->_state != AST_STATE_UP) {
    
    #ifdef OSP_SUPPORT	
    		time(&p->ospstart);
    #endif
    
    	
    		codec=pbx_builtin_getvar_helper(p->owner,"SIP_CODEC");
    		if (codec) {
    			fmt=ast_getformatbyname(codec);
    			if (fmt) {
    
    				ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC) variable\n",codec);
    
    				if (p->jointcapability & fmt) {
    					p->jointcapability &= fmt;
    					p->capability &= fmt;
    				} else
    					ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
    
    			} else ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n",codec);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_setstate(ast, AST_STATE_UP);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (option_debug)
    			ast_log(LOG_DEBUG, "sip_answer(%s)\n", ast->name);
    
    		res = transmit_response_with_sdp(p, "200 OK", &p->initreq, 1);
    
    	ast_mutex_unlock(&p->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;
    }
    
    
    /*! \brief  sip_write: Send frame to media channel (rtp) */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res = 0;
    
    	switch (frame->frametype) {
    	case AST_FRAME_VOICE:
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (!(frame->subclass & ast->nativeformats)) {
    			ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
    				frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    				/* If channel is not up, activate early media session */
    
    				if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
    
    					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
    
    					ast_set_flag(p, SIP_PROGRESS_SENT);	
    
    				time(&p->lastrtptx);
    
    				res =  ast_rtp_write(p->rtp, frame);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    		break;
    	case AST_FRAME_VIDEO:
    
    				/* Activate video early media */
    
    				if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
    
    					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
    
    					ast_set_flag(p, SIP_PROGRESS_SENT);	
    
    				time(&p->lastrtptx);
    
    				res =  ast_rtp_write(p->vrtp, frame);
    			}
    
    		break;
    	case AST_FRAME_IMAGE:
    
    		ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
    		return 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;
    
    /*! \brief  sip_fixup: Fix up a channel:  If a channel is consumed, this is called.
    
            Basically update any ->owner links */
    
    static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
    
    	struct sip_pvt *p = newchan->tech_pvt;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (p->owner != oldchan) {
    		ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    	}
    	p->owner = newchan;
    
    /*! \brief  sip_senddigit: Send DTMF character on SIP channel */
    
    /*    within one call, we're able to transmit in many methods simultaneously */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int sip_senddigit(struct ast_channel *ast, char digit)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res = 0;
    	ast_mutex_lock(&p->lock);
    
    	switch (ast_test_flag(p, SIP_DTMF)) {
    	case SIP_DTMF_INFO:
    
    		transmit_info_with_digit(p, digit);
    
    		break;
    	case SIP_DTMF_RFC2833:
    		if (p->rtp)
    			ast_rtp_senddigit(p->rtp, digit);
    		break;
    	case SIP_DTMF_INBAND:
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_unlock(&p->lock);
    	return res;
    
    /*! \brief  sip_transfer: Transfer SIP call */
    
    static int sip_transfer(struct ast_channel *ast, const char *dest)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_lock(&p->lock);
    
    	if (ast->_state == AST_STATE_RING)
    		res = sip_sipredirect(p, dest);
    	else
    		res = transmit_refer(p, dest);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_unlock(&p->lock);
    
    /*! \brief  sip_indicate: Play indication to user 
     * With SIP a lot of indications is sent as messages, letting the device play
    
       the indication - busy signal, congestion etc */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int sip_indicate(struct ast_channel *ast, int condition)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res = 0;
    
    	ast_mutex_lock(&p->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	switch(condition) {
    	case AST_CONTROL_RINGING:
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (ast->_state == AST_STATE_RING) {
    
    			if (!ast_test_flag(p, SIP_PROGRESS_SENT) ||
    			    (ast_test_flag(p, SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {				
    
    				/* Send 180 ringing if out-of-band seems reasonable */
    				transmit_response(p, "180 Ringing", &p->initreq);
    
    				ast_set_flag(p, SIP_RINGING);
    
    				if (ast_test_flag(p, SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
    
    				/* Well, if it's not reasonable, just send in-band */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = -1;
    		break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	case AST_CONTROL_BUSY:
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (ast->_state != AST_STATE_UP) {
    
    			transmit_response(p, "486 Busy Here", &p->initreq);
    
    			ast_set_flag(p, SIP_ALREADYGONE);	
    
    			ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = -1;
    		break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	case AST_CONTROL_CONGESTION:
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (ast->_state != AST_STATE_UP) {
    
    			transmit_response(p, "503 Service Unavailable", &p->initreq);
    
    			ast_set_flag(p, SIP_ALREADYGONE);	
    
    			ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = -1;
    		break;
    
    	case AST_CONTROL_PROCEEDING:
    
    		if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
    			transmit_response(p, "100 Trying", &p->initreq);
    			break;
    		}
    		res = -1;
    		break;
    	case AST_CONTROL_PROGRESS:
    
    		if ((ast->_state != AST_STATE_UP) && !ast_test_flag(p, SIP_PROGRESS_SENT) && !ast_test_flag(p, SIP_OUTGOING)) {
    
    			transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
    
    			ast_set_flag(p, SIP_PROGRESS_SENT);	
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = -1;
    		break;
    
    	case AST_CONTROL_HOLD:	/* The other part of the bridge are put on hold */
    
    			ast_log(LOG_DEBUG, "Bridged channel now on hold - %s\n", p->callid);
    
    	case AST_CONTROL_UNHOLD:	/* The other part of the bridge are back from hold */
    
    			ast_log(LOG_DEBUG, "Bridged channel is back from hold, let's talk! : %s\n", p->callid);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	case AST_CONTROL_VIDUPDATE:	/* Request a video frame update */
    		if (p->vrtp && !ast_test_flag(p, SIP_NOVIDEO)) {
    			transmit_info_with_vidupdate(p);
    			res = 0;
    		} else
    			res = -1;
    		break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	case -1:
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = -1;
    		break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	default:
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = -1;
    		break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_unlock(&p->lock);
    	return res;
    
    /*! \brief  sip_new: Initiate a call in the SIP channel */
    
    /*      called from sip_request_call (calls from the pbx ) */
    
    static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct ast_channel *tmp;
    
    	struct ast_variable *v = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int fmt;
    
    #ifdef OSP_SUPPORT
    	char iabuf[INET_ADDRSTRLEN];
    	char peer[MAXHOSTNAMELEN];
    #endif	
    
    	ast_mutex_unlock(&i->lock);
    	/* Don't hold a sip pvt lock while we allocate a channel */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	tmp = ast_channel_alloc(1);
    
    	if (!tmp) {
    		ast_log(LOG_WARNING, "Unable to allocate SIP channel structure\n");
    		return NULL;
    	}
    	tmp->tech = &sip_tech;
    	/* Select our native format based on codec preference until we receive
    	   something from another device to the contrary. */
    	if (i->jointcapability)
    
    	else if (i->capability)
    
    	tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
    
    	fmt = ast_best_codec(tmp->nativeformats);
    
    	if (title)
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    		snprintf(tmp->name, sizeof(tmp->name), "SIP/%s-%04x", title, thread_safe_rand() & 0xffff);
    
    	else if (strchr(i->fromdomain,':'))
    		snprintf(tmp->name, sizeof(tmp->name), "SIP/%s-%08x", strchr(i->fromdomain,':')+1, (int)(long)(i));
    	else
    		snprintf(tmp->name, sizeof(tmp->name), "SIP/%s-%08x", i->fromdomain, (int)(long)(i));
    
    	tmp->type = channeltype;
    	if (ast_test_flag(i, SIP_DTMF) ==  SIP_DTMF_INBAND) {
    		i->vad = ast_dsp_new();
    		ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
    		if (relaxdtmf)
    			ast_dsp_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
    	}
    
    	if (i->rtp) {
    		tmp->fds[0] = ast_rtp_fd(i->rtp);
    		tmp->fds[1] = ast_rtcp_fd(i->rtp);
    	}
    
    	if (i->vrtp) {
    		tmp->fds[2] = ast_rtp_fd(i->vrtp);
    		tmp->fds[3] = ast_rtcp_fd(i->vrtp);
    	}
    	if (state == AST_STATE_RING)
    		tmp->rings = 1;
    	tmp->adsicpe = AST_ADSI_UNAVAILABLE;
    	tmp->writeformat = fmt;
    	tmp->rawwriteformat = fmt;
    	tmp->readformat = fmt;
    	tmp->rawreadformat = fmt;
    	tmp->tech_pvt = i;
    
    	tmp->callgroup = i->callgroup;
    	tmp->pickupgroup = i->pickupgroup;
    	tmp->cid.cid_pres = i->callingpres;
    	if (!ast_strlen_zero(i->accountcode))
    		ast_copy_string(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode));
    	if (i->amaflags)
    		tmp->amaflags = i->amaflags;
    	if (!ast_strlen_zero(i->language))
    		ast_copy_string(tmp->language, i->language, sizeof(tmp->language));
    	if (!ast_strlen_zero(i->musicclass))
    		ast_copy_string(tmp->musicclass, i->musicclass, sizeof(tmp->musicclass));
    	i->owner = tmp;
    	ast_mutex_lock(&usecnt_lock);
    	usecnt++;
    	ast_mutex_unlock(&usecnt_lock);
    	ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
    	ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
    	if (!ast_strlen_zero(i->cid_num)) 
    
    		tmp->cid.cid_num = ast_strdup(i->cid_num);
    
    	if (!ast_strlen_zero(i->cid_name))
    
    		tmp->cid.cid_name = ast_strdup(i->cid_name);
    
    	if (!ast_strlen_zero(i->rdnis))
    
    		tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
    
    	if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
    
    		tmp->cid.cid_dnid = ast_strdup(i->exten);
    
    	tmp->priority = 1;
    	if (!ast_strlen_zero(i->uri)) {
    		pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
    	}
    	if (!ast_strlen_zero(i->domain)) {
    		pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
    	}
    	if (!ast_strlen_zero(i->useragent)) {
    		pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
    	}
    	if (!ast_strlen_zero(i->callid)) {
    		pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
    	}
    
    #ifdef OSP_SUPPORT
    	snprintf(peer, sizeof(peer), "[%s]:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), i->sa.sin_addr), ntohs(i->sa.sin_port));
    	pbx_builtin_setvar_helper(tmp, "OSPPEER", peer);
    #endif
    
    	ast_setstate(tmp, state);
    	if (state != AST_STATE_DOWN) {
    
    			ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
    			ast_hangup(tmp);
    			tmp = NULL;
    
    	}
    	/* Set channel variables for this call from configuration */
    	for (v = i->chanvars ; v ; v = v->next)
    		pbx_builtin_setvar_helper(tmp,v->name,v->value);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return tmp;
    }
    
    
    /*! \brief  get_sdp_by_line: Reads one line of SIP message body */
    
    static char* get_sdp_by_line(char* line, char *name, int nameLen)
    {
    	if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=') {
    
    		return ast_skip_blanks(line + nameLen + 1);
    
    /*! \brief  get_sdp: Gets all kind of SIP message bodies, including SDP,
    
       but the name wrongly applies _only_ sdp */
    
    static char *get_sdp(struct sip_request *req, char *name) 
    {
    	int x;
    	int len = strlen(name);
    	char *r;
    
    	for (x=0; x<req->lines; x++) {
    		r = get_sdp_by_line(req->line[x], name, len);
    		if (r[0] != '\0')
    			return r;
    	}
    	return "";
    
    static void sdpLineNum_iterator_init(int* iterator) 
    {
    	*iterator = 0;
    
    }
    
    static char* get_sdp_iterate(int* iterator,
    
    			     struct sip_request *req, char *name)
    {
    	int len = strlen(name);
    	char *r;
    
    	while (*iterator < req->lines) {
    		r = get_sdp_by_line(req->line[(*iterator)++], name, len);
    		if (r[0] != '\0')
    			return r;
    	}
    	return "";
    
    static char *find_alias(const char *name, char *_default)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	int x;
    
    	for (x=0;x<sizeof(aliases) / sizeof(aliases[0]); x++) 
    		if (!strcasecmp(aliases[x].fullname, name))
    			return aliases[x].shortname;
    	return _default;
    }
    
    static char *__get_header(struct sip_request *req, char *name, int *start)
    {
    	int pass;
    
    	/*
    	 * Technically you can place arbitrary whitespace both before and after the ':' in
    	 * a header, although RFC3261 clearly says you shouldn't before, and place just
    	 * one afterwards.  If you shouldn't do it, what absolute idiot decided it was 
    	 * a good idea to say you can do it, and if you can do it, why in the hell would.
    	 * you say you shouldn't.
    	 * Anyways, pedanticsipchecking controls whether we allow spaces before ':',
    	 * and we always allow spaces after that for compatibility.
    	 */
    	for (pass = 0; name && pass < 2;pass++) {
    		int x, len = strlen(name);
    
    		for (x=*start; x<req->headers; x++) {
    
    			if (!strncasecmp(req->header[x], name, len)) {
    
    				char *r = req->header[x] + len;	/* skip name */
    				if (pedanticsipchecking)
    					r = ast_skip_blanks(r);
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    					*start = x+1;
    
    					return ast_skip_blanks(r+1);
    
    		if (pass == 0) /* Try aliases */
    			name = find_alias(name, NULL);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	/* Don't return NULL, so get_header is always a valid pointer */
    	return "";
    }
    
    
    /*! \brief  get_header: Get header from SIP request */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static char *get_header(struct sip_request *req, char *name)
    {
    	int start = 0;
    	return __get_header(req, name, &start);
    }