view sip-in/mncc_sock.c @ 64:1f863c63f96b

sip-in: beginning of disconnect handling
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 18 Sep 2022 15:29:30 -0800
parents e5aee661e3b2
children 9213ec8b434b
line wrap: on
line source

/*
 * In this module we implement our connection to the MNCC daemon's
 * mtcall socket.
 */

#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/mncc.h"

static char mtcall_socket_pathname[] = "/var/gsm/mtcall_socket";

int gsm_socket, gsm_is_connected;

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

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

void
gsm_socket_select()
{
	union mncc_msg msg;
	int rc;

	rc = recv(gsm_socket, &msg, sizeof msg, 0);
	if (rc < 0) {
		syslog(LOG_CRIT, "error reading from mtcall socket: %m");
		/* GSM disconnect handling to be implemented */
		exit(1);
	}
	if (rc < 4) {
		syslog(LOG_CRIT, "short read from mtcall socket: %d bytes", rc);
		exit(1);
	}
	msg_from_mncc(&msg, rc);
}

send_mncc_to_gsm(msg, msglen)
	union mncc_msg *msg;
	unsigned msglen;
{
	return send(gsm_socket, msg, msglen, 0);
}