dtm.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "dtm.h"
  21. #include "carrier.h"
  22. #include "log.h"
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <sys/mman.h>
  30. #include <sys/types.h>
  31. struct dtm_node_t *dtm_load(char *filename) {
  32. struct dtm_node_t *mroot;
  33. int fd;
  34. int len;
  35. int nodes;
  36. fd = open(filename, O_RDONLY);
  37. if (fd < 0) {
  38. LERR("cannot open file '%s'\n", filename);
  39. return NULL;
  40. }
  41. len=lseek(fd, 0, SEEK_END);
  42. lseek(fd, 0, SEEK_SET);
  43. nodes=len/sizeof(struct dtm_node_t);
  44. LINFO("file contains %ld nodes (size=%ld, rest=%ld)\n", (long int)nodes, (long int)len, (long int)len%sizeof(struct dtm_node_t));
  45. mroot=mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
  46. if (mroot==MAP_FAILED) {
  47. LERR("cannot mmap file '%s', error=%d (%s)\n", filename, errno, strerror(errno));
  48. close(fd);
  49. return NULL;
  50. }
  51. return mroot;
  52. }
  53. int dtm_longest_match(struct dtm_node_t *mroot, const char *number, int numberlen, carrier_t *carrier)
  54. {
  55. dtm_node_index_t node = 0;
  56. int nmatch = -1;
  57. int i=0;
  58. unsigned int digit;
  59. if (mroot[node].carrier > 0) {
  60. nmatch=0;
  61. *carrier = mroot[node].carrier;
  62. }
  63. while (i<numberlen) {
  64. digit = number[i] - '0';
  65. if (digit>9) return nmatch;
  66. node = mroot[node].child[digit];
  67. if (node == NULL_CARRIERID) return nmatch;
  68. i++;
  69. if (node<0) {
  70. nmatch=i;
  71. *carrier = -node;
  72. return nmatch;
  73. }
  74. if (mroot[node].carrier > 0) {
  75. nmatch=i;
  76. *carrier = mroot[node].carrier;
  77. }
  78. }
  79. return nmatch;
  80. }