view librtpalloc/rtp_alloc_simple.c @ 11:2cdbd574bba6

tone-detect/Makefile: add install
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Mar 2024 12:18:49 -0800
parents 35c0d9f03c0a
children
line wrap: on
line source

/*
 * The library function implemented in this C module provides a
 * simple interface for obtaining a single RTP endpoint from
 * themwi-rtp-mgr.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "../include/tmgw_const.h"
#include "../include/rtp_alloc.h"
#include "rtpmgr_resp.h"
#include "rtp_alloc_simple.h"

static char ctrl_socket_pathname[] = "/var/gsm/rtp_alloc_socket";

static void
close_fds(resp)
	struct rtp_alloc_resp_wrap *resp;
{
	unsigned n;

	for (n = 0; n < resp->num_fd; n++)
		close(resp->fd_buf[n]);
}

rtp_alloc_simple(ep_type, out)
	int ep_type;
	struct rtp_alloc_simple *out;
{
	struct sockaddr_un sa;
	unsigned sa_len;
	int ctrl_fd, rc;
	struct rtp_alloc_req req;
	struct rtp_alloc_resp_wrap resp;
	unsigned expect_num_fd;

	switch (ep_type) {
	case TMGW_EP_TYPE_GSM_ONLY:
	case TMGW_EP_TYPE_PSTN_ONLY:
		expect_num_fd = 2;
		break;
	case TMGW_EP_TYPE_GATEWAY:
		expect_num_fd = 4;
		break;
	default:
		fprintf(stderr,
			"rtp_alloc_simple() error: unknown EP type %d\n",
			ep_type);
		return(-1);
	}
	ctrl_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
	if (ctrl_fd < 0) {
		perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)");
		return(-1);
	}
	fill_sockaddr_un(ctrl_socket_pathname, &sa, &sa_len);
	rc = connect(ctrl_fd, (struct sockaddr *) &sa, sa_len);
	if (rc < 0) {
		perror(ctrl_socket_pathname);
		close(ctrl_fd);
		return(-1);
	}
	bzero(&req, sizeof req);
	req.ep_type = ep_type;
	rc = send(ctrl_fd, &req, sizeof req, 0);
	if (rc < 0) {
		perror("send to RTP allocator socket");
		close(ctrl_fd);
		return(-1);
	}
	rc = collect_rtpmgr_resp(ctrl_fd, 0, &resp);
	if (rc < 0) {
		perror("recvmsg from RTP allocator socket");
		close(ctrl_fd);
		return(-1);
	}
	close(ctrl_fd);
	if (resp.resp_len != sizeof(struct rtp_alloc_resp)) {
		fprintf(stderr,
"error: response packet from themwi-rtp-mgr has wrong length (%u bytes)\n",
			resp.resp_len);
		close_fds(&resp);
		return(-1);
	}
	if (resp.resp.res != RTP_ALLOC_OK) {
		fprintf(stderr, "themwi-rtp-mgr returned error %u\n",
			resp.resp.res);
		close_fds(&resp);
		return(-1);
	}
	if (resp.num_fd != expect_num_fd) {
		fprintf(stderr,
"error: themwi-rtp-mgr returned %u descriptors instead of expected %u\n",
			resp.num_fd, expect_num_fd);
		close_fds(&resp);
		return(-1);
	}
	switch (ep_type) {
	case TMGW_EP_TYPE_GSM_ONLY:
		out->gsm_rtp_fd = resp.fd_buf[0];
		out->gsm_rtcp_fd = resp.fd_buf[1];
		break;
	case TMGW_EP_TYPE_PSTN_ONLY:
		out->pstn_rtp_fd = resp.fd_buf[0];
		out->pstn_rtcp_fd = resp.fd_buf[1];
		break;
	case TMGW_EP_TYPE_GATEWAY:
		out->gsm_rtp_fd = resp.fd_buf[0];
		out->gsm_rtcp_fd = resp.fd_buf[1];
		out->pstn_rtp_fd = resp.fd_buf[2];
		out->pstn_rtcp_fd = resp.fd_buf[3];
	}
	bcopy(&resp.resp.gsm_addr, &out->gsm_addr,
		sizeof(struct sockaddr_storage));
	bcopy(&resp.resp.pstn_addr, &out->pstn_addr,
		sizeof(struct sockaddr_storage));
	return(0);
}