geoip_mod.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "../../sr_module.h"
  29. #include "../../dprint.h"
  30. #include "../../ut.h"
  31. #include "../../pvar.h"
  32. #include "../../mod_fix.h"
  33. #include "geoip_pv.h"
  34. MODULE_VERSION
  35. static char *geoip_path = NULL;
  36. static int mod_init(void);
  37. static void mod_destroy(void);
  38. static int w_geoip_match(struct sip_msg* msg, char* str1, char* str2);
  39. static int geoip_match(struct sip_msg *msg, gparam_t *target, gparam_t *pvname);
  40. static pv_export_t mod_pvs[] = {
  41. { {"gip", sizeof("git")-1}, PVT_OTHER, pv_get_geoip, 0,
  42. pv_parse_geoip_name, 0, 0, 0 },
  43. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  44. };
  45. static cmd_export_t cmds[]={
  46. {"geoip_match", (cmd_function)w_geoip_match, 2, fixup_spve_spve,
  47. 0, ANY_ROUTE},
  48. {0, 0, 0, 0, 0, 0}
  49. };
  50. static param_export_t params[]={
  51. {"path", PARAM_STRING, &geoip_path},
  52. {0, 0, 0}
  53. };
  54. struct module_exports exports = {
  55. "geoip",
  56. DEFAULT_DLFLAGS, /* dlopen flags */
  57. cmds,
  58. params,
  59. 0,
  60. 0, /* exported MI functions */
  61. mod_pvs, /* exported pseudo-variables */
  62. 0, /* extra processes */
  63. mod_init, /* module initialization function */
  64. 0, /* response function */
  65. mod_destroy, /* destroy function */
  66. 0 /* per child init function */
  67. };
  68. /**
  69. * init module function
  70. */
  71. static int mod_init(void)
  72. {
  73. if(geoip_path==NULL || strlen(geoip_path)==0)
  74. {
  75. LM_ERR("path to GeoIP database file not set\n");
  76. return -1;
  77. }
  78. if(geoip_init_pv(geoip_path)!=0)
  79. {
  80. LM_ERR("cannot init for database file at: %s\n", geoip_path);
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. /**
  86. * destroy module function
  87. */
  88. static void mod_destroy(void)
  89. {
  90. geoip_destroy_pv();
  91. }
  92. static int w_geoip_match(struct sip_msg* msg, char* str1, char* str2)
  93. {
  94. return geoip_match(msg, (gparam_t*)str1, (gparam_t*)str2);
  95. }
  96. static int geoip_match(struct sip_msg *msg, gparam_t *target, gparam_t *pvname)
  97. {
  98. str tomatch;
  99. str pvclass;
  100. if(msg==NULL)
  101. {
  102. LM_ERR("received null msg\n");
  103. return -1;
  104. }
  105. if(fixup_get_svalue(msg, target, &tomatch)<0)
  106. {
  107. LM_ERR("cannot get the address\n");
  108. return -1;
  109. }
  110. if(fixup_get_svalue(msg, pvname, &pvclass)<0)
  111. {
  112. LM_ERR("cannot get the pv class\n");
  113. return -1;
  114. }
  115. geoip_pv_reset(&pvclass);
  116. return geoip_update_pv(&tomatch, &pvclass);
  117. }