view sip-in/bye_out.c @ 92:a9137bdb6047

sip-in: make BYE Max-Forwards configurable
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 23 Sep 2022 19:18:07 -0800
parents ff4b76a107a1
children 2c22b40408fb
line wrap: on
line source

/*
 * In this module we implement our UAC functionality of sending BYE.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include "../libsip/parse.h"
#include "../libsip/out_msg.h"
#include "call.h"

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

extern struct in_addr sip_bind_ip;
extern unsigned sip_bind_port;
extern unsigned max_forwards;

fill_bye_out_msg(msg, call)
	struct sip_msg_out *msg;
	struct call *call;
{
	char strbuf[80];
	int rc;

	rc = start_request_out_msg_urilen(msg, "BYE", call->from_uri,
					  call->from_uri_len);
	if (rc < 0)
		return rc;
	sprintf(strbuf, "SIP/2.0/UDP %s:%u",
		inet_ntoa(sip_bind_ip), sip_bind_port);
	rc = out_msg_add_header(msg, "Via", strbuf);
	if (rc < 0)
		return rc;
	rc = out_msg_add_header(msg, "From", call->invite_to);
	if (rc < 0)
		return rc;
	rc = out_msg_add_header(msg, "To", call->invite_from);
	if (rc < 0)
		return rc;
	rc = out_msg_add_header(msg, "Call-ID", call->sip_call_id);
	if (rc < 0)
		return rc;
	rc = out_msg_add_header(msg, "CSeq", "1 BYE");
	if (rc < 0)
		return rc;
	sprintf(strbuf, "%u", max_forwards);
	rc = out_msg_add_header(msg, "Max-Forwards", strbuf);
	if (rc < 0)
		return rc;
	out_msg_finish(msg);
	return 0;
}

void
initiate_bye(call)
	struct call *call;
{
	struct sip_msg_out msg;
	int rc;

	rc = fill_bye_out_msg(&msg, call);
	if (rc < 0) {
		syslog(LOG_ERR, "outgoing BYE request msg length exceeded");
		call->sip_state = SIP_STATE_MSG_SIZE_ERR;
		/* TODO: transition from TEARDOWN to DEAD_SIP */
		return;
	}
	sip_tx_packet(&msg, &call->udp_sin);
	call->sip_state = SIP_STATE_BYE_SENT;
	call->sip_tx_count = 1;
}

void
process_sip_response(msg, sin)
	struct sip_pkt_rx *msg;
	struct sockaddr_in *sin;
{
	char *call_id_hdr, *cseq_hdr;
	struct call *call;

	call_id_hdr = get_single_header(msg, "Call-ID", "i", (int *) 0);
	if (!call_id_hdr)
		return;
	call = find_call_by_sip_id(call_id_hdr);
	if (!call)
		return;
	cseq_hdr = get_single_header(msg, "CSeq", (char *) 0, (int *) 0);
	if (!cseq_hdr)
		return;
	if (strcmp(cseq_hdr, "1 BYE")) {
		syslog(LOG_ERR,
			"UAC received response with unknown CSeq %.32s",
			cseq_hdr);
		return;
	}
	if (msg->status_code < 200)
		return;
	if (call->sip_state == SIP_STATE_BYE_SENT)
		call->sip_state = SIP_STATE_ENDED;
}