changeset 1:6534965175dd

libnumdb ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Dec 2023 00:53:04 +0000
parents 159dd90eeafe
children 1773886ef54e
files libnumdb/Makefile libnumdb/check_nanp.c libnumdb/check_short.c libnumdb/readbin.c libnumdb/refresh.c
diffstat 5 files changed, 222 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb/Makefile	Wed Dec 13 00:53:04 2023 +0000
@@ -0,0 +1,13 @@
+CC=	gcc
+CFLAGS=	-O2 -I../build-inc
+OBJS=	check_nanp.o check_short.o readbin.o refresh.o
+LIB=	libnumdb.a
+
+all:	${LIB}
+
+${LIB}:	${OBJS}
+	ar rcu $@ ${OBJS}
+	ranlib $@
+
+clean:
+	rm -f *.[oa] errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb/check_nanp.c	Wed Dec 13 00:53:04 2023 +0000
@@ -0,0 +1,47 @@
+/*
+ * The library function implemented in this module consults ThemWi number db
+ * to see if a given NANP number is owned by us or not.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <themwi/nanp/number_db_v2.h>
+#include <themwi/nanp/number_lookup.h>
+#include <themwi/nanp/number_utils.h>
+
+extern struct numdb_file_hdr numdb_hdr;
+extern struct owned_number_rec *numdb_owned_numbers;
+
+static int compare_owned_num(const void *p1v, const void *p2v)
+{
+	const uint16_t *p1 = p1v, *p2 = p2v;
+
+	if (p1[0] < p2[0])
+		return(-1);
+	if (p1[0] > p2[0])
+		return(1);
+	if (p1[1] < p2[1])
+		return(-1);
+	if (p1[1] > p2[1])
+		return(1);
+	if (p1[2] < p2[2])
+		return(-1);
+	if (p1[2] > p2[2])
+		return(1);
+	return(0);
+}
+
+const struct owned_number_rec *numdb_lookup_nanp(const char *numstr)
+{
+	uint16_t key[3];
+
+	if (!numdb_owned_numbers || !numdb_hdr.owned_number_count)
+		return(0);
+	key[0] = digits3_to_uint16(numstr);
+	key[1] = digits3_to_uint16(numstr + 3);
+	key[2] = digits4_to_uint16(numstr + 6);
+	return bsearch(key, numdb_owned_numbers, numdb_hdr.owned_number_count,
+			sizeof(struct owned_number_rec), compare_owned_num);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb/check_short.c	Wed Dec 13 00:53:04 2023 +0000
@@ -0,0 +1,37 @@
+/*
+ * The library function implemented in this module looks up 4-digit short
+ * dialing numbers in ThemWi number db to determine their disposition.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <themwi/nanp/number_db_v2.h>
+#include <themwi/nanp/number_lookup.h>
+#include <themwi/nanp/number_utils.h>
+
+extern struct numdb_file_hdr numdb_hdr;
+extern struct short_number_rec *numdb_short_numbers;
+
+static int compare_short_num(const void *p1v, const void *p2v)
+{
+	const uint16_t *p1 = p1v, *p2 = p2v;
+
+	if (*p1 < *p2)
+		return(-1);
+	if (*p1 > *p2)
+		return(1);
+	return(0);
+}
+
+const struct short_number_rec *numdb_lookup_short(const char *numstr)
+{
+	uint16_t key;
+
+	if (!numdb_short_numbers || !numdb_hdr.short_number_count)
+		return(0);
+	key = digits4_to_uint16(numstr);
+	return bsearch(&key, numdb_short_numbers, numdb_hdr.short_number_count,
+			sizeof(struct short_number_rec), compare_short_num);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb/readbin.c	Wed Dec 13 00:53:04 2023 +0000
@@ -0,0 +1,71 @@
+/*
+ * This library module contains the code that reads /var/gsm/number-db2.bin,
+ * as well as definitions of global variables into which the booty is read.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <syslog.h>
+
+#include <themwi/nanp/number_db_v2.h>
+#include <themwi/nanp/number_lookup.h>
+
+const char numdb_pathname[] = "/var/gsm/number-db2.bin";
+struct stat numdb_file_stat;
+struct numdb_file_hdr numdb_hdr;
+struct owned_number_rec *numdb_owned_numbers;
+struct short_number_rec *numdb_short_numbers;
+
+int read_number_db(void)
+{
+	FILE *inf;
+
+	inf = fopen(numdb_pathname, "r");
+	if (!inf) {
+		syslog(LOG_CRIT, "open %s: %m", numdb_pathname);
+		return(-1);
+	}
+	fstat(fileno(inf), &numdb_file_stat);
+	if (!S_ISREG(numdb_file_stat.st_mode)) {
+		syslog(LOG_CRIT, "invalid %s: not a regular file",
+			numdb_pathname);
+		fclose(inf);
+		return(-1);
+	}
+	if (fread(&numdb_hdr, sizeof numdb_hdr, 1, inf) != 1) {
+read_err:	syslog(LOG_CRIT, "error reading from %s: %m", numdb_pathname);
+		fclose(inf);
+		return(-1);
+	}
+	if (numdb_hdr.owned_number_count) {
+		numdb_owned_numbers = malloc(numdb_hdr.owned_number_count *
+					     sizeof(struct owned_number_rec));
+		if (!numdb_owned_numbers) {
+			syslog(LOG_CRIT, "malloc for owned number db: %m");
+			fclose(inf);
+			return(-1);
+		}
+		if (fread(numdb_owned_numbers, sizeof(struct owned_number_rec),
+			  numdb_hdr.owned_number_count, inf) !=
+		    numdb_hdr.owned_number_count)
+			goto read_err;
+	}
+	if (numdb_hdr.short_number_count) {
+		numdb_short_numbers = malloc(numdb_hdr.short_number_count *
+					     sizeof(struct short_number_rec));
+		if (!numdb_short_numbers) {
+			syslog(LOG_CRIT, "malloc for short number db: %m");
+			fclose(inf);
+			return(-1);
+		}
+		if (fread(numdb_short_numbers, sizeof(struct short_number_rec),
+			  numdb_hdr.short_number_count, inf) !=
+		    numdb_hdr.short_number_count)
+			goto read_err;
+	}
+	fclose(inf);
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb/refresh.c	Wed Dec 13 00:53:04 2023 +0000
@@ -0,0 +1,54 @@
+/*
+ * Long-running ThemWi daemon processes need to be able to pick up updates
+ * to the number database without being restarted.  Whenever they need to
+ * consult the number db when handling a new call setup or equivalent,
+ * they will call refresh_number_db(), which does a stat on the file,
+ * followed by a re-read if the file has changed.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <syslog.h>
+
+#include <themwi/nanp/number_db_v2.h>
+#include <themwi/nanp/number_lookup.h>
+
+extern const char numdb_pathname[];
+extern struct stat numdb_file_stat;
+extern struct owned_number_rec *numdb_owned_numbers;
+extern struct short_number_rec *numdb_short_numbers;
+
+int refresh_number_db(void)
+{
+	int rc;
+	struct stat st;
+
+	rc = stat(numdb_pathname, &st);
+	if (rc < 0) {
+		syslog(LOG_CRIT, "unable to stat %s for refresh: %m",
+			numdb_pathname);
+		return(-1);
+	}
+	if (st.st_mtime == numdb_file_stat.st_mtime &&
+	    st.st_ctime == numdb_file_stat.st_ctime &&
+	    st.st_size  == numdb_file_stat.st_size)
+		return(0);
+	if (numdb_owned_numbers) {
+		free(numdb_owned_numbers);
+		numdb_owned_numbers = 0;
+	}
+	if (numdb_short_numbers) {
+		free(numdb_short_numbers);
+		numdb_short_numbers = 0;
+	}
+	rc = read_number_db();
+	if (rc < 0) {
+		syslog(LOG_CRIT, "error reading %s on refresh!",
+			numdb_pathname);
+		exit(1);
+	}
+	return(1);
+}