view sip-manual-out/main.c @ 128:5685412bd6aa

sip-in: pass DTMF start & stop to themwi-mgw
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 01 Oct 2022 23:07:01 -0800
parents a36b731bfef9
children 258932879f8b
line wrap: on
line source

/*
 * This is the main module for sip-manual-out test program.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../libsip/out_msg.h"
#include "../libsip/sdp.h"

extern int sip_socket;
extern struct in_addr sip_bind_ip, sip_dest_ip;
extern unsigned sip_bind_port, sip_dest_port;
extern char sip_dest_domain[];
extern struct sockaddr_in dummy_rtp_endp;

struct sockaddr_in sip_dest_sin;
char from_uri[128], to_uri[128], call_id[128];
struct timeval cur_event_time;
unsigned max_forwards = 70;
int declare_100rel_supp;
int pcma_codec_pref, pcma_codec_force;

send_invite_req()
{
	struct sip_msg_out msg;
	struct sdp_gen sdp;
	int rc;

	rc = start_request_out_msg(&msg, "INVITE", to_uri);
	if (rc < 0) {
msg_size_err:	fprintf(stderr, "composing INVITE req: msg size error\n");
		exit(1);
	}
	rc = add_req_boilerplate(&msg, "1 INVITE", 0);
	if (rc < 0)
		goto msg_size_err;
	rc = add_contact_header(&msg);
	if (rc < 0)
		goto msg_size_err;
	if (declare_100rel_supp) {
		rc = out_msg_add_header(&msg, "Supported", "100rel");
		if (rc < 0)
			goto msg_size_err;
	}
	rc = out_msg_add_header(&msg, "Content-Type", "application/sdp");
	if (rc < 0)
		goto msg_size_err;
	bzero(&sdp, sizeof sdp);
	sdp.conn_ip = dummy_rtp_endp.sin_addr;
	sdp.conn_port = ntohs(dummy_rtp_endp.sin_port);
	if (pcma_codec_force)
		sdp.codec_mask = SDP_CODEC_MASK_PCMA;
	else {
		sdp.codec_mask = SDP_CODEC_MASK_BOTH;
		if (pcma_codec_pref)
			sdp.codec_mask |= SDP_CODEC_MASK_PCMA_PREF;
	}
	sdp.session_id = sdp.conn_port << 16;
	sdp.owner_ip = sip_bind_ip;
	rc = out_msg_finish_sdp(&msg, &sdp);
	if (rc < 0)
		goto msg_size_err;
	sip_tx_packet(&msg, &sip_dest_sin);
}

static void
preliminary_proc(argc, argv)
	char **argv;
{
	extern int optind;
	extern char *optarg;
	char *logfile;
	int opt, rc;

	logfile = 0;
	while ((opt = getopt(argc, argv, "aAl:m:r")) != EOF) {
		switch (opt) {
		case 'a':
			pcma_codec_pref = 1;
			continue;
		case 'A':
			pcma_codec_force = 1;
			continue;
		case 'l':
			logfile = optarg;
			continue;
		case 'm':
			max_forwards = atoi(optarg);
			continue;
		case 'r':
			declare_100rel_supp = 1;
			continue;
		default:
		usage:
			fprintf(stderr,
			"usage: %s [options] dest-conf from-num to-num\n",
				argv[0]);
			exit(1);
		}
	}
	if (argc != optind + 3)
		goto usage;
	read_config_file(argv[optind]);
	open_sip_udp_socket();
	obtain_dummy_rtp();
	sip_dest_sin.sin_family = AF_INET;
	sip_dest_sin.sin_addr = sip_dest_ip;
	sip_dest_sin.sin_port = htons(sip_dest_port);
	sprintf(from_uri, "<sip:%s@%s>;tag=out%u", argv[optind+1],
		inet_ntoa(sip_bind_ip), ntohs(dummy_rtp_endp.sin_port));
	sprintf(to_uri, "sip:%s@%s", argv[optind+2], sip_dest_domain);
	if (logfile) {
		rc = open_sip_log_file(logfile);
		if (rc < 0)
			exit(1);	/* error msg already printed */
	}
}

main(argc, argv)
	char **argv;
{
	fd_set fds;
	int rc;

	preliminary_proc(argc, argv);
	gettimeofday(&cur_event_time, 0);
	sprintf(call_id, "%08u_%u@%s",
		(unsigned)(cur_event_time.tv_sec % 100000000),
		ntohs(dummy_rtp_endp.sin_port), inet_ntoa(sip_bind_ip));
	send_invite_req();
	/* main select loop */
	for (;;) {
		FD_ZERO(&fds);
		FD_SET(0, &fds);
		FD_SET(sip_socket, &fds);
		rc = select(sip_socket+1, &fds, 0, 0, 0);
		if (rc < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			exit(1);
		}
		gettimeofday(&cur_event_time, 0);
		if (FD_ISSET(0, &fds))
			select_stdin();
		if (FD_ISSET(sip_socket, &fds))
			sip_socket_select();
	}
}