add small C program to dump back binary database
authorJohannes Berg <johannes@sipsolutions.net>
Thu, 22 May 2008 12:42:54 +0000 (14:42 +0200)
committerJohannes Berg <johannes@sipsolutions.net>
Thu, 22 May 2008 12:42:54 +0000 (14:42 +0200)
.gitignore
daemon/Makefile [new file with mode: 0644]
daemon/dump.c [new file with mode: 0644]

index a8be90a..8919b0f 100644 (file)
@@ -1,3 +1,4 @@
 *~
 regulatory.sqlite
 regulatory.bin
+daemon/dump
diff --git a/daemon/Makefile b/daemon/Makefile
new file mode 100644 (file)
index 0000000..ee62b4a
--- /dev/null
@@ -0,0 +1,8 @@
+CFLAGS += -Wall -g3
+
+all:   dump
+
+dump:  dump.c regdb.h
+
+clean:
+       @rm -f dump *~
diff --git a/daemon/dump.c b/daemon/dump.c
new file mode 100644 (file)
index 0000000..99c6d64
--- /dev/null
@@ -0,0 +1,97 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <arpa/inet.h> /* ntohl */
+
+#include "regdb.h"
+
+static void print_reg_rule(__u8 *db, struct regdb_file_reg_rule *rule)
+{
+       struct regdb_file_freq_range *freq;
+       struct regdb_file_power_rule *power;
+
+       freq = (void *)(db + ntohl(rule->freq_range_ptr));
+       power = (void *)(db + ntohl(rule->power_rule_ptr));
+
+       printf("\t%d..%d kHz (maxbw: %d kHz, mod: 0x%x, restrict: 0x%x)\n",
+              ntohl(freq->start_freq),
+              ntohl(freq->end_freq),
+              ntohl(freq->max_bandwidth),
+              ntohl(freq->modulation_cap),
+              ntohl(freq->misc_restrictions));
+
+       printf("\t -> env: '%.1s', ag: %d, ir_ptmp: %d, ir_ptp: %d, eirp_pmtp: %d, eirp_ptp: %d\n",
+              &power->environment_cap,
+              ntohl(power->max_antenna_gain),
+              ntohl(power->max_ir_ptmp),
+              ntohl(power->max_ir_ptp),
+              ntohl(power->max_eirp_pmtp),
+              ntohl(power->max_eirp_ptp));
+}
+
+int main(int argc, char **argv)
+{
+       int fd;
+       struct stat stat;
+       __u8 *db;
+       struct regdb_file_header *header;
+       struct regdb_file_reg_country *countries;
+       int num_countries, i, j;
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
+               return 2;
+       }
+
+       fd = open(argv[1], O_RDONLY);
+       if (fd < 0) {
+               perror("failed to open db file");
+               return 2;
+       }
+
+       if (fstat(fd, &stat)) {
+               perror("failed to fstat db file");
+               return 2;
+       }
+
+       /* XXX: check file length to be at least >= sizeof(struct regdb_file_header) */
+
+       db = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
+       if (db == MAP_FAILED) {
+               perror("failed to mmap db file");
+               return 2;
+       }
+
+       header = (void *)db;
+
+       if (ntohl(header->magic) != REGDB_MAGIC) {
+               fprintf(stderr, "Invalid database magic\n");
+               return 2;
+       }
+
+       if (ntohl(header->version) != REGDB_VERSION) {
+               fprintf(stderr, "Invalid database version\n");
+               return 2;
+       }
+
+       num_countries = ntohl(header->reg_country_num);
+       countries = (void *)(db + ntohl(header->reg_country_ptr));
+
+       /* XXX: check countries pointer to be in file */
+
+       for (i = 0; i < num_countries; i++) {
+               struct regdb_file_reg_rules_collection *rcoll;
+               struct regdb_file_reg_country *country = countries + i;
+
+               printf("Country %.2s\n", country->alpha2);
+               rcoll = (void *)db + ntohl(country->reg_collection_ptr);
+               for (j = 0; j < ntohl(rcoll->reg_rule_num); j++)
+                       print_reg_rule(db, (void *)(db + ntohl(rcoll->reg_rule_ptrs[j])));
+       }
+
+       return 0;
+}