diff sip-out/mgw_sock.c @ 154:e54b0a9e322f

beginning of themwi-sip-out
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 11 Oct 2022 23:04:01 -0800
parents sip-in/mgw_sock.c@372209628038
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-out/mgw_sock.c	Tue Oct 11 23:04:01 2022 -0800
@@ -0,0 +1,70 @@
+/*
+ * 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);
+}