Split printing functions to their own translation unit.
[platform/upstream/crda.git] / regdbdump.c
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <arpa/inet.h> /* ntohl */
8
9 #include "regdb.h"
10 #include "reglib.h"
11
12 int main(int argc, char **argv)
13 {
14         int fd;
15         struct stat stat;
16         __u8 *db;
17         struct regdb_file_header *header;
18         struct regdb_file_reg_country *countries;
19         int dblen, siglen, num_countries, i, r = 0;
20         struct ieee80211_regdomain *rd = NULL;
21
22         if (argc != 2) {
23                 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
24                 return 2;
25         }
26
27         fd = open(argv[1], O_RDONLY);
28         if (fd < 0) {
29                 perror("failed to open db file");
30                 return 2;
31         }
32
33         if (fstat(fd, &stat)) {
34                 perror("failed to fstat db file");
35                 return 2;
36         }
37
38         dblen = stat.st_size;
39
40         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
41         if (db == MAP_FAILED) {
42                 perror("failed to mmap db file");
43                 return 2;
44         }
45
46         header = crda_get_file_ptr(db, dblen, sizeof(*header), 0);
47
48         if (ntohl(header->magic) != REGDB_MAGIC) {
49                 fprintf(stderr, "Invalid database magic\n");
50                 return 2;
51         }
52
53         if (ntohl(header->version) != REGDB_VERSION) {
54                 fprintf(stderr, "Invalid database version\n");
55                 return 2;
56         }
57
58         siglen = ntohl(header->signature_length);
59         /* adjust dblen so later sanity checks don't run into the signature */
60         dblen -= siglen;
61
62         if (dblen <= (int)sizeof(*header)) {
63                 fprintf(stderr, "Invalid signature length %d\n", siglen);
64                 return 2;
65         }
66
67         /* verify signature */
68         if (!crda_verify_db_signature(db, dblen, siglen))
69                 return -EINVAL;
70
71         num_countries = ntohl(header->reg_country_num);
72         countries = crda_get_file_ptr(db, dblen,
73                         sizeof(struct regdb_file_reg_country) * num_countries,
74                         header->reg_country_ptr);
75
76         for (i = 0; i < num_countries; i++) {
77                 struct regdb_file_reg_country *country = countries + i;
78
79                 rd = country2rd(db, dblen, country);
80                 if (!rd) {
81                         r = -ENOMEM;
82                         fprintf(stderr, "Could not covert country "
83                         "(%.2s) to rd\n", country->alpha2);
84                         goto out;
85                 }
86
87                 print_regdom(rd);
88                 free(rd);
89                 rd = NULL;
90
91         }
92 out:
93         return r;
94 }