FreeCalypso > hg > themwi-system-sw
comparison 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 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 132:aa278d75d757 | 133:765991f42d86 | 
|---|---|
| 1 /* | |
| 2 * This library module contains the code that reads /var/gsm/out-routes.bin, | |
| 3 * as well as definitions of global variables into which the booty is read. | |
| 4 */ | |
| 5 | |
| 6 #include <sys/types.h> | |
| 7 #include <sys/stat.h> | |
| 8 #include <sys/socket.h> | |
| 9 #include <netinet/in.h> | |
| 10 #include <stdio.h> | |
| 11 #include <stdint.h> | |
| 12 #include <stdlib.h> | |
| 13 #include <syslog.h> | |
| 14 #include "../include/out_routes.h" | |
| 15 | |
| 16 char outrt_pathname[] = "/var/gsm/out-routes.bin"; | |
| 17 struct stat outrt_file_stat; | |
| 18 struct out_routes_header outrt_hdr; | |
| 19 struct sip_out_dest *outrt_dest_array; | |
| 20 struct inn_route *outrt_inn_array; | |
| 21 struct special_num_route *outrt_spec_array; | |
| 22 | |
| 23 read_out_routes_db() | |
| 24 { | |
| 25 FILE *inf; | |
| 26 | |
| 27 inf = fopen(outrt_pathname, "r"); | |
| 28 if (!inf) { | |
| 29 syslog(LOG_CRIT, "open %s: %m", outrt_pathname); | |
| 30 return(-1); | |
| 31 } | |
| 32 fstat(fileno(inf), &outrt_file_stat); | |
| 33 if (!S_ISREG(outrt_file_stat.st_mode)) { | |
| 34 syslog(LOG_CRIT, "invalid %s: not a regular file", | |
| 35 outrt_pathname); | |
| 36 fclose(inf); | |
| 37 return(-1); | |
| 38 } | |
| 39 if (fread(&outrt_hdr, sizeof outrt_hdr, 1, inf) != 1) { | |
| 40 read_err: syslog(LOG_CRIT, "error reading from %s: %m", outrt_pathname); | |
| 41 fclose(inf); | |
| 42 return(-1); | |
| 43 } | |
| 44 if (!outrt_hdr.num_dest) { | |
| 45 syslog(LOG_CRIT, "%s contains invalid data", outrt_pathname); | |
| 46 fclose(inf); | |
| 47 return(-1); | |
| 48 } | |
| 49 outrt_dest_array = | |
| 50 malloc(outrt_hdr.num_dest * sizeof(struct sip_out_dest)); | |
| 51 if (!outrt_dest_array) { | |
| 52 syslog(LOG_CRIT, "malloc for SIP dest array: %m"); | |
| 53 fclose(inf); | |
| 54 return(-1); | |
| 55 } | |
| 56 if (fread(outrt_dest_array, sizeof(struct sip_out_dest), | |
| 57 outrt_hdr.num_dest, inf) != outrt_hdr.num_dest) | |
| 58 goto read_err; | |
| 59 if (outrt_hdr.num_inn) { | |
| 60 outrt_inn_array = | |
| 61 malloc(outrt_hdr.num_inn * sizeof(struct inn_route)); | |
| 62 if (!outrt_inn_array) { | |
| 63 syslog(LOG_CRIT, "malloc for E.164 route array: %m"); | |
| 64 fclose(inf); | |
| 65 return(-1); | |
| 66 } | |
| 67 if (fread(outrt_inn_array, sizeof(struct inn_route), | |
| 68 outrt_hdr.num_inn, inf) != outrt_hdr.num_inn) | |
| 69 goto read_err; | |
| 70 } | |
| 71 if (outrt_hdr.num_special) { | |
| 72 outrt_spec_array = malloc(outrt_hdr.num_special * | |
| 73 sizeof(struct special_num_route)); | |
| 74 if (!outrt_spec_array) { | |
| 75 syslog(LOG_CRIT, "malloc for special route array: %m"); | |
| 76 fclose(inf); | |
| 77 return(-1); | |
| 78 } | |
| 79 if (fread(outrt_spec_array, sizeof(struct special_num_route), | |
| 80 outrt_hdr.num_special, inf) != outrt_hdr.num_special) | |
| 81 goto read_err; | |
| 82 } | |
| 83 fclose(inf); | |
| 84 return(0); | |
| 85 } | 
