carrier.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2009 1&1 Internet AG
  3. *
  4. * This file is part of sip-router, a free SIP server.
  5. *
  6. * sip-router is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * sip-router is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #define _GNU_SOURCE
  21. #include "carrier.h"
  22. #include "log.h"
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. char *cnames[MAX_CARRIERID+1];
  28. void init_carrier_names() {
  29. int i;
  30. for (i=0; i<=MAX_CARRIERID; i++) cnames[i]=NULL;
  31. cnames[OTHER_CARRIERID] = "other carriers merged by this tool";
  32. }
  33. int load_carrier_names(char *filename) {
  34. FILE * fp;
  35. char * line = NULL;
  36. size_t len = 0;
  37. ssize_t read;
  38. char *p;
  39. int n;
  40. char idstr[4];
  41. long int id;
  42. int ret=0;
  43. idstr[3]=0;
  44. fp = fopen(filename, "r");
  45. if (fp == NULL) {
  46. LERR("cannot open file '%s'\n", filename);
  47. return -1;
  48. }
  49. n=1;
  50. while ((read = getline(&line, &len, fp)) != -1) {
  51. p=line;
  52. len=strlen(p);
  53. if (len<=5) {
  54. LWARNING("invalid line %ld\n", (long int)n);
  55. ret=-1;
  56. goto nextline;
  57. }
  58. idstr[0] = p[1];
  59. idstr[1] = p[2];
  60. idstr[2] = p[3];
  61. p+=5;
  62. len-=5;
  63. id = strtol(idstr, NULL, 10);
  64. if (!IS_VALID_PDB_CARRIERID(id)) {
  65. LWARNING("invalid carrier id '%s'\n", idstr);
  66. ret=-1;
  67. goto nextline;
  68. }
  69. cnames[id]=malloc(len+1);
  70. if (cnames[id]==NULL) {
  71. LERR("out of memory (needed %ld bytes)\n", (long int)len);
  72. ret=-1;
  73. exit(-1);
  74. }
  75. strncpy(cnames[id], p, len - 1);
  76. cnames[id][len - 1]=0;
  77. nextline:
  78. n++;
  79. }
  80. if (line) free(line);
  81. fclose(fp);
  82. return ret;
  83. }
  84. char *carrierid2name(carrier_t carrier) {
  85. char *s;
  86. if (!IS_VALID_CARRIERID(carrier)) s="invalid carrier id";
  87. else s=cnames[carrier];
  88. if (s==NULL) s="unknown carrier";
  89. return s;
  90. }