diff liboutrt/readbin.c @ 133:765991f42d86

liboutrt started with binary file reading
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Oct 2022 23:15:26 -0800
parents libnumdb/readbin.c@ffd48df829a7
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboutrt/readbin.c	Thu Oct 06 23:15:26 2022 -0800
@@ -0,0 +1,85 @@
+/*
+ * This library module contains the code that reads /var/gsm/out-routes.bin,
+ * as well as definitions of global variables into which the booty is read.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include "../include/out_routes.h"
+
+char outrt_pathname[] = "/var/gsm/out-routes.bin";
+struct stat outrt_file_stat;
+struct out_routes_header outrt_hdr;
+struct sip_out_dest *outrt_dest_array;
+struct inn_route *outrt_inn_array;
+struct special_num_route *outrt_spec_array;
+
+read_out_routes_db()
+{
+	FILE *inf;
+
+	inf = fopen(outrt_pathname, "r");
+	if (!inf) {
+		syslog(LOG_CRIT, "open %s: %m", outrt_pathname);
+		return(-1);
+	}
+	fstat(fileno(inf), &outrt_file_stat);
+	if (!S_ISREG(outrt_file_stat.st_mode)) {
+		syslog(LOG_CRIT, "invalid %s: not a regular file",
+			outrt_pathname);
+		fclose(inf);
+		return(-1);
+	}
+	if (fread(&outrt_hdr, sizeof outrt_hdr, 1, inf) != 1) {
+read_err:	syslog(LOG_CRIT, "error reading from %s: %m", outrt_pathname);
+		fclose(inf);
+		return(-1);
+	}
+	if (!outrt_hdr.num_dest) {
+		syslog(LOG_CRIT, "%s contains invalid data", outrt_pathname);
+		fclose(inf);
+		return(-1);
+	}
+	outrt_dest_array =
+		malloc(outrt_hdr.num_dest * sizeof(struct sip_out_dest));
+	if (!outrt_dest_array) {
+		syslog(LOG_CRIT, "malloc for SIP dest array: %m");
+		fclose(inf);
+		return(-1);
+	}
+	if (fread(outrt_dest_array, sizeof(struct sip_out_dest),
+		  outrt_hdr.num_dest, inf) != outrt_hdr.num_dest)
+		goto read_err;
+	if (outrt_hdr.num_inn) {
+		outrt_inn_array =
+			malloc(outrt_hdr.num_inn * sizeof(struct inn_route));
+		if (!outrt_inn_array) {
+			syslog(LOG_CRIT, "malloc for E.164 route array: %m");
+			fclose(inf);
+			return(-1);
+		}
+		if (fread(outrt_inn_array, sizeof(struct inn_route),
+			  outrt_hdr.num_inn, inf) != outrt_hdr.num_inn)
+			goto read_err;
+	}
+	if (outrt_hdr.num_special) {
+		outrt_spec_array = malloc(outrt_hdr.num_special *
+					  sizeof(struct special_num_route));
+		if (!outrt_spec_array) {
+			syslog(LOG_CRIT, "malloc for special route array: %m");
+			fclose(inf);
+			return(-1);
+		}
+		if (fread(outrt_spec_array, sizeof(struct special_num_route),
+			  outrt_hdr.num_special, inf) != outrt_hdr.num_special)
+			goto read_err;
+	}
+	fclose(inf);
+	return(0);
+}