view mtctest/dummy_rtp.c @ 37:a065b4a62a8a

themwi-test-mtc: drain stdin when we receive input meant to request user disconnect
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 10 Jul 2022 00:32:38 -0800
parents e8e82a4bf12b
children
line wrap: on
line source

/*
 * In this module we implement the code that connects to themwi-mgw
 * and obtains a dummy GSM-side RTP endpoint.
 */

#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_ctrl.h"
#include "../include/tmgw_const.h"

struct sockaddr_storage dummy_rtp_endp;

static char tmgw_socket_pathname[] = "/var/gsm/tmgw_socket";

obtain_dummy_rtp()
{
	struct sockaddr_un sa;
	unsigned sa_len;
	int fd, rc;
	struct tmgw_ctrl_req req;
	struct tmgw_ctrl_resp resp;

	fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
	if (fd < 0) {
		perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)");
		exit(1);
	}
	fill_sockaddr_un(tmgw_socket_pathname, &sa, &sa_len);
	rc = connect(fd, (struct sockaddr *) &sa, sa_len);
	if (rc < 0) {
		perror(tmgw_socket_pathname);
		exit(1);
	}
	bzero(&req, sizeof req);
	req.opcode = TMGW_CTRL_OP_CRCX;
	req.ep_id = TMGW_EP_TYPE_DUMMY_GSM;
	rc = send(fd, &req, sizeof req, 0);
	if (rc < 0) {
		perror("send to TMGW socket");
		exit(1);
	}
	rc = recv(fd, &resp, sizeof resp, 0);
	if (rc < 0) {
		perror("recv from TMGW socket");
		exit(1);
	}
	if (rc != sizeof resp) {
		fprintf(stderr,
	"error: response packet from TMGW has wrong length (%d bytes)\n",
			rc);
		exit(1);
	}
	if (resp.res != TMGW_RESP_OK) {
		fprintf(stderr, "TMGW CRCX returned error %u\n", resp.res);
		exit(1);
	}
	bcopy(&resp.gsm_addr, &dummy_rtp_endp, sizeof(struct sockaddr_storage));
	return(0);
}