# HG changeset patch # User Mychaela Falconia # Date 1702428784 0 # Node ID 6534965175dd6c4f2580df7807c8bb4edc999431 # Parent 159dd90eeafefe2a031e0db20cd22dcccbd10771 libnumdb ported over diff -r 159dd90eeafe -r 6534965175dd libnumdb/Makefile --- /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 diff -r 159dd90eeafe -r 6534965175dd libnumdb/check_nanp.c --- /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 +#include +#include + +#include +#include +#include + +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); +} diff -r 159dd90eeafe -r 6534965175dd libnumdb/check_short.c --- /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 +#include +#include + +#include +#include +#include + +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); +} diff -r 159dd90eeafe -r 6534965175dd libnumdb/readbin.c --- /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 +#include +#include +#include +#include +#include + +#include +#include + +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); +} diff -r 159dd90eeafe -r 6534965175dd libnumdb/refresh.c --- /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 +#include +#include +#include +#include +#include + +#include +#include + +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); +}