changeset 107:372209628038

sip-in: handle themwi-mgw shutdown without terminating
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 27 Sep 2022 23:45:40 -0800
parents 245dc4837b56
children 0d6435808bcd
files sip-in/invite.c sip-in/main.c sip-in/mgw_sock.c
diffstat 3 files changed, 22 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/sip-in/invite.c	Tue Sep 27 23:44:07 2022 -0800
+++ b/sip-in/invite.c	Tue Sep 27 23:45:40 2022 -0800
@@ -243,9 +243,12 @@
 	/* SIP INVITE validation done - check if GSM service is up */
 	rc = connect_gsm_mtcall();
 	if (rc < 0) {
-		start_response_out_msg(&resp, "480 GSM service is offline");
+gsm_offline:	start_response_out_msg(&resp, "480 GSM service is offline");
 		goto error_resp;
 	}
+	rc = connect_tmgw_socket();
+	if (rc < 0)
+		goto gsm_offline;
 	/* stateful processing begins */
 	call = malloc(sizeof(struct call) + strlen(ess->call_id) +
 		      strlen(ess->from) + req_uri_len + strlen(ess->via) + 19);
--- a/sip-in/main.c	Tue Sep 27 23:44:07 2022 -0800
+++ b/sip-in/main.c	Tue Sep 27 23:45:40 2022 -0800
@@ -15,8 +15,9 @@
 
 extern unsigned cfg_retrans_timeout;
 
-extern int mgw_socket, sip_socket;
 extern int gsm_socket, gsm_is_connected;
+extern int mgw_socket, mgw_is_connected;
+extern int sip_socket;
 
 static int max_fd;
 
@@ -41,10 +42,6 @@
 		fprintf(stderr, "error reading number database\n");
 		exit(1);
 	}
-	if (open_tmgw_socket() < 0) {
-		fprintf(stderr, "error connecting to themwi-mgw socket\n");
-		exit(1);
-	}
 	if (open_sip_udp_socket() < 0) {
 		fprintf(stderr, "error opening SIP UDP socket\n");
 		exit(1);
@@ -58,10 +55,11 @@
 	/* main select loop */
 	for (;;) {
 		FD_ZERO(&fds);
-		FD_SET(mgw_socket, &fds);
 		FD_SET(sip_socket, &fds);
 		if (gsm_is_connected)
 			FD_SET(gsm_socket, &fds);
+		if (mgw_is_connected)
+			FD_SET(mgw_socket, &fds);
 		need_retrans = 0;
 		scan_call_list_for_timeouts(&need_retrans);
 		if (need_retrans) {
@@ -83,9 +81,9 @@
 		}
 		if (gsm_is_connected && FD_ISSET(gsm_socket, &fds))
 			gsm_socket_select();
+		if (mgw_is_connected && FD_ISSET(mgw_socket, &fds))
+			mgw_socket_select();
 		if (FD_ISSET(sip_socket, &fds))
 			sip_socket_select();
-		if (FD_ISSET(mgw_socket, &fds))
-			mgw_socket_select();
 	}
 }
--- a/sip-in/mgw_sock.c	Tue Sep 27 23:44:07 2022 -0800
+++ b/sip-in/mgw_sock.c	Tue Sep 27 23:45:40 2022 -0800
@@ -13,14 +13,16 @@
 
 static char tmgw_socket_pathname[] = "/var/gsm/tmgw_socket";
 
-int mgw_socket;
+int mgw_socket, mgw_is_connected;
 
-open_tmgw_socket()
+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");
@@ -30,9 +32,11 @@
 	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);
 }
 
@@ -43,9 +47,12 @@
 	int rc;
 
 	rc = recv(mgw_socket, &msg, sizeof msg, 0);
-	if (rc < 0) {
-		syslog(LOG_CRIT, "error reading from TMGW socket: %m");
-		exit(1);
+	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,