view sip-in/mgw_sock.c @ 165:9419fe55824f

themwi-sip-out complete to the point of compiling and linking
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 Oct 2022 17:05:45 -0800
parents 372209628038
children
line wrap: on
line source

/*
 * In this module we implement our local socket interface to themwi-mgw.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <syslog.h>
#include "../include/tmgw_ctrl.h"

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

int mgw_socket, mgw_is_connected;

connect_tmgw_socket()
{
	struct sockaddr_un sa;
	unsigned sa_len;
	int rc;

	if (mgw_is_connected)
		return(0);
	mgw_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
	if (mgw_socket < 0) {
		syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
		return(-1);
	}
	fill_sockaddr_un(tmgw_socket_pathname, &sa, &sa_len);
	rc = connect(mgw_socket, (struct sockaddr *) &sa, sa_len);
	if (rc < 0) {
		syslog(LOG_ERR, "connect to %s: %m", tmgw_socket_pathname);
		close(mgw_socket);
		return(-1);
	}
	update_max_fd(mgw_socket);
	mgw_is_connected = 1;
	return(0);
}

void
mgw_socket_select()
{
	struct tmgw_ctrl_resp msg;
	int rc;

	rc = recv(mgw_socket, &msg, sizeof msg, 0);
	if (rc <= 0) {
		syslog(LOG_ERR, "themwi-mgw socket disconnected");
		close(mgw_socket);
		mgw_is_connected = 0;
		shutdown_mgw_conn();
		return;
	}
	if (rc != sizeof(struct tmgw_ctrl_resp)) {
		syslog(LOG_CRIT,
			"response packet from TMGW has wrong length: %d bytes",
			rc);
		exit(1);
	}
	process_tmgw_response(&msg);
}

send_req_to_tmgw(msg)
	struct tmgw_ctrl_req *msg;
{
	return send(mgw_socket, msg, sizeof(struct tmgw_ctrl_req), 0);
}