view sip-in/prack.c @ 247:c6a3fb913c53

top Makefile: mtctest changed to use libnumdb2
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 22:39:28 -0800
parents c1c94b7fc2e2
children
line wrap: on
line source

/*
 * Here we implement our handling of SIP PRACK, expected from callers
 * when we send them a reliable 180 Ringing response.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <ctype.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 "../libsip/parse.h"
#include "../libsip/uas_basic.h"
#include "../libsip/out_msg.h"
#include "call.h"

extern char *get_single_header();
extern struct call *find_call_by_sip_id();

extern unsigned sip_linger_error;

void
handle_sip_prack(req, ess, sin)
	struct sip_pkt_rx *req;
	struct uas_parse_hdrs *ess;
	struct sockaddr_in *sin;
{
	struct call *call;
	struct sip_msg_out resp;
	char *rack, *orig_method, *cp;
	unsigned rseq, orig_num;
	int rc;

	call = find_call_by_sip_id(ess->call_id);
	if (!call) {
		start_response_out_msg(&resp, "481 Call-ID not found");
error_resp:	rc = add_resp_basic_headers(&resp, ess, req->req_method);
		if (rc < 0)
			return;
		out_msg_finish(&resp);
		sip_tx_packet(&resp, sin);
		return;
	}
	rack = get_single_header(req, "RAck", (char *) 0, (int *) 0);
	if (!rack) {
		start_response_out_msg(&resp, "400 Missing RAck header");
		goto error_resp;
	}
	if (!isdigit(*rack)) {
malformed:	start_response_out_msg(&resp, "400 Malformed RAck header");
		goto error_resp;
	}
	rseq = strtoul(rack, &cp, 10);
	if (!isspace(*cp))
		goto malformed;
	while (isspace(*cp))
		cp++;
	if (!isdigit(*cp))
		goto malformed;
	orig_num = strtoul(cp, &cp, 10);
	if (!isspace(*cp))
		goto malformed;
	while (isspace(*cp))
		cp++;
	if (!isupper(*cp))
		goto malformed;
	orig_method = cp;
	while (isalnum(*cp))
		cp++;
	if (*cp)
		goto malformed;
	if (rseq != 1 || orig_num != call->invite_cseq ||
	    strcmp(orig_method, "INVITE")) {
		start_response_out_msg(&resp,
			"481 RAck fails to match our 100rel response");
		goto error_resp;
	}
	switch (call->sip_state) {
	case SIP_STATE_RINGING_REL:
		call->sip_state = SIP_STATE_RINGING;
		start_response_out_msg(&resp, "200 OK");
		rc = add_resp_basic_headers(&resp, ess, req->req_method);
		if (rc < 0) {
			syslog(LOG_ERR, "PRACK 200 response length exceeded");
			call->sip_state = SIP_STATE_MSG_SIZE_ERR;
			call->overall_state = OVERALL_STATE_TEARDOWN;
			disconnect_mncc(call, GSM48_CAUSE_LOC_PRN_S_LU,
					GSM48_CC_CAUSE_INTERWORKING);
			disconnect_tmgw(call);
			sip_mark_end_time(call, sip_linger_error);
			return;
		}
		out_msg_finish(&resp);
		sip_tx_packet(&resp, sin);
		return;
	case SIP_STATE_RINGING:
	case SIP_STATE_INVITE_200:
		start_response_out_msg(&resp, "200 OK");
		rc = add_resp_basic_headers(&resp, ess, req->req_method);
		if (rc < 0)
			return;
		out_msg_finish(&resp);
		sip_tx_packet(&resp, sin);
		return;
	case SIP_STATE_INVITE_PROC:
	case SIP_STATE_CONNECTED:
	case SIP_STATE_BYE_SENT:
	case SIP_STATE_INVITE_ERR:
	case SIP_STATE_ENDED:
		start_response_out_msg(&resp,
				       "481 No outstanding 100rel response");
		goto error_resp;
	}
}