view utils/themwi-check-own.c @ 7:dc1554b7dfb8

themwi-check-own compiles in the new model
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Dec 2023 03:33:51 +0000
parents 1e548c7a24a1
children
line wrap: on
line source

/*
 * This utility performs a lookup in ThemWi number database to see
 * if a given NANP number is owned by us.
 */

#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>
#include <themwi/nanp/number_utils.h>

int main(int argc, char **argv)
{
	char nanp[11];
	const struct owned_number_rec *rec;

	if (argc != 2) {
usage:		fprintf(stderr, "usage: %s 10-digit-number\n", argv[0]);
		exit(1);
	}
	if (grok_number_string(argv[1], 1) != 10)
		goto usage;
	dehyphen_number_string(argv[1], nanp);
	if (!is_nanp_valid_prefix(nanp)) {
		fprintf(stderr, "error: number violates NANP rules\n");
		exit(1);
	}
	openlog("themwi-check-own", LOG_PERROR, LOG_USER);
	if (read_number_db() < 0) {
		fprintf(stderr, "error reading number database\n");
		exit(1);
	}
	rec = numdb_lookup_nanp(nanp);
	printf("+1-%.3s-%.3s-%s is ", nanp, nanp+3, nanp+6);
	if (!rec) {
		puts("NOT owned by us");
		exit(1);
	}
	fputs("locally owned, ", stdout);
	switch (rec->usage & NUMBER_USAGE_MASK) {
	case NUMBER_USAGE_TYPE_RSVD:
		puts("reserved");
		break;
	case NUMBER_USAGE_TYPE_GSM_SUB:
		puts("assigned to a GSM subscriber");
		break;
	case NUMBER_USAGE_TYPE_ALIAS:
		printf("mapped to +1-%03u-%03u-%04u\n", rec->remap[0],
			rec->remap[1], rec->remap[2]);
		break;
	default:
		printf("unknown usage byte 0x%02X\n", rec->usage);
	}
	exit(0);
}