view sip-out/uac_resp.c @ 198:cf1ba5d65188

mgw: start using project-global rtp_defs.h
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 29 Mar 2023 20:06:40 -0800
parents 83022d408071
children
line wrap: on
line source

/*
 * In this module we implement our handling of SIP responses in the UAC role.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include "../include/gsm48_const.h"
#include "../include/out_routes.h"
#include "../libsip/parse.h"
#include "../libsip/resp_ident.h"
#include "call.h"

extern struct call *find_call_by_sip_id();

extern unsigned sip_linger_timeout;
extern unsigned sip_linger_bye_out_ok;
extern unsigned sip_linger_bye_out_err;

void
handle_bye_response(call, msg, sin)
	struct call *call;
	struct sip_pkt_rx *msg;
	struct sockaddr_in *sin;
{
	if (msg->status_code != 200)
		syslog(LOG_ERR, "non-200 response to BYE: %.64s",
			msg->status_str);
	if (msg->status_code < 200)
		return;
	if (call->sip_state == SIP_STATE_BYE_SENT) {
		call->sip_state = SIP_STATE_ENDED;
		if (msg->status_code <= 299)
			sip_mark_end_time(call, sip_linger_bye_out_ok);
		else
			sip_mark_end_time(call, sip_linger_bye_out_err);
	}
}

void
handle_cancel_response(call, msg, sin)
	struct call *call;
	struct sip_pkt_rx *msg;
	struct sockaddr_in *sin;
{
	if (msg->status_code != 200)
		syslog(LOG_ERR, "non-200 response to CANCEL: %.64s",
			msg->status_str);
	if (msg->status_code < 200)
		return;
	if (call->sip_state == SIP_STATE_CANCEL_SENT) {
		call->sip_state = SIP_STATE_ACCEPT_200;
		if (msg->status_code <= 299)
			sip_mark_end_time(call, sip_linger_timeout);
		else
			sip_mark_end_time(call, sip_linger_bye_out_err);
	}
}

void
process_sip_response(msg, sin)
	struct sip_pkt_rx *msg;
	struct sockaddr_in *sin;
{
	struct sip_resp_ident rid;
	struct call *call;
	int rc;

	rc = sip_resp_extract_ident(msg, &rid);
	if (rc < 0) {
		syslog(LOG_ERR, "SIP %03u response: bad or missing %s header",
			msg->status_code, rid.error_field);
		return;
	}
	call = find_call_by_sip_id(rid.call_id);
	if (!call) {
		syslog(LOG_ERR, "SIP %03u response: unmatched Call-ID",
			msg->status_code);
		return;
	}
	if (rid.cseq_num == 1 && !strcmp(rid.cseq_method, "INVITE"))
		handle_invite_response(call, msg, sin);
	else if (rid.cseq_num == 1 && !strcmp(rid.cseq_method, "CANCEL"))
		handle_cancel_response(call, msg, sin);
	else if (rid.cseq_num == 2 && !strcmp(rid.cseq_method, "BYE"))
		handle_bye_response(call, msg, sin);
	else
		syslog(LOG_ERR,
			"UAC received %03u response with unknown CSeq %u %.32s",
			msg->status_code, rid.cseq_num, rid.cseq_method);
}