view sip-manual-out/main.c @ 119:056616f7e8ab

sip-manual-out: convert command line to getopt style
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 29 Sep 2022 14:03:06 -0800
parents 51e2f72dc5ab
children c62d0f28da6f
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;

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");
	if (rc < 0)
		goto msg_size_err;
	rc = add_contact_header(&msg);
	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);
	sdp.codec_mask = SDP_CODEC_MASK_BOTH;
	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, "l:")) != EOF) {
		switch (opt) {
		case 'l':
			logfile = optarg;
			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(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(sip_socket, &fds))
			sip_socket_select();
	}
}