view sip-in/mncc_sock.c @ 147:94b5831c017f

sip-manual-out code: split bye_in.c from uas.c
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Oct 2022 23:01:32 -0800
parents 245dc4837b56
children
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_ERR, "mtcall socket disconnected");
		close(gsm_socket);
		gsm_is_connected = 0;
		shutdown_gsm_conn();
		return;
	}
	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);
}