annotate liboutrt/readbin.c @ 133:765991f42d86

liboutrt started with binary file reading
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Oct 2022 23:15:26 -0800
parents libnumdb/readbin.c@ffd48df829a7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
2 * This library module contains the code that reads /var/gsm/out-routes.bin,
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * as well as definitions of global variables into which the booty is read.
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/stat.h>
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
8 #include <sys/socket.h>
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
9 #include <netinet/in.h>
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdint.h>
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <syslog.h>
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
14 #include "../include/out_routes.h"
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
16 char outrt_pathname[] = "/var/gsm/out-routes.bin";
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
17 struct stat outrt_file_stat;
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
18 struct out_routes_header outrt_hdr;
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
19 struct sip_out_dest *outrt_dest_array;
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
20 struct inn_route *outrt_inn_array;
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
21 struct special_num_route *outrt_spec_array;
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
23 read_out_routes_db()
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 FILE *inf;
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
27 inf = fopen(outrt_pathname, "r");
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (!inf) {
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
29 syslog(LOG_CRIT, "open %s: %m", outrt_pathname);
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 return(-1);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
32 fstat(fileno(inf), &outrt_file_stat);
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
33 if (!S_ISREG(outrt_file_stat.st_mode)) {
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 syslog(LOG_CRIT, "invalid %s: not a regular file",
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
35 outrt_pathname);
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
36 fclose(inf);
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
37 return(-1);
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
38 }
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
39 if (fread(&outrt_hdr, sizeof outrt_hdr, 1, inf) != 1) {
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
40 read_err: syslog(LOG_CRIT, "error reading from %s: %m", outrt_pathname);
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 fclose(inf);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 return(-1);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
44 if (!outrt_hdr.num_dest) {
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
45 syslog(LOG_CRIT, "%s contains invalid data", outrt_pathname);
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 fclose(inf);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 return(-1);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
49 outrt_dest_array =
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
50 malloc(outrt_hdr.num_dest * sizeof(struct sip_out_dest));
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
51 if (!outrt_dest_array) {
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
52 syslog(LOG_CRIT, "malloc for SIP dest array: %m");
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
53 fclose(inf);
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
54 return(-1);
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
55 }
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
56 if (fread(outrt_dest_array, sizeof(struct sip_out_dest),
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
57 outrt_hdr.num_dest, inf) != outrt_hdr.num_dest)
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
58 goto read_err;
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
59 if (outrt_hdr.num_inn) {
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
60 outrt_inn_array =
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
61 malloc(outrt_hdr.num_inn * sizeof(struct inn_route));
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
62 if (!outrt_inn_array) {
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
63 syslog(LOG_CRIT, "malloc for E.164 route array: %m");
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 fclose(inf);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return(-1);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
67 if (fread(outrt_inn_array, sizeof(struct inn_route),
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
68 outrt_hdr.num_inn, inf) != outrt_hdr.num_inn)
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 goto read_err;
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
71 if (outrt_hdr.num_special) {
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
72 outrt_spec_array = malloc(outrt_hdr.num_special *
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
73 sizeof(struct special_num_route));
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
74 if (!outrt_spec_array) {
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
75 syslog(LOG_CRIT, "malloc for special route array: %m");
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 fclose(inf);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 return(-1);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
133
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
79 if (fread(outrt_spec_array, sizeof(struct special_num_route),
765991f42d86 liboutrt started with binary file reading
Mychaela Falconia <falcon@freecalypso.org>
parents: 8
diff changeset
80 outrt_hdr.num_special, inf) != outrt_hdr.num_special)
8
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 goto read_err;
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 fclose(inf);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 return(0);
ffd48df829a7 beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }