dispatcher.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * $Id$
  3. *
  4. * dispatcher module -- stateless load balancing
  5. *
  6. * Copyright (C) 2004-2006 FhG Fokus
  7. * Copyright (C) 2005-2008 Hendrik Scholz <[email protected]>
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <sys/types.h>
  34. #include <unistd.h>
  35. #include "../../sr_module.h"
  36. #include "../../dprint.h"
  37. #include "../../error.h"
  38. #include "../../ut.h"
  39. #include "../../mem/mem.h"
  40. #include "dispatcher.h"
  41. #include "ds_rpc.h"
  42. MODULE_VERSION
  43. /** parameters */
  44. char *dslistfile = CFG_DIR"dispatcher.list";
  45. int force_dst = 0;
  46. int ds_flags = 0;
  47. char *ds_activelist;
  48. char ***ds_setp_a, ***ds_setp_b;
  49. int **ds_setlen_a, **ds_setlen_b;
  50. /** module functions */
  51. static int mod_init(void);
  52. static int child_init(int);
  53. static int w_ds_select_dst(struct sip_msg*, char*, char*);
  54. static int w_ds_select_new(struct sip_msg*, char*, char*);
  55. static void destroy(void);
  56. static cmd_export_t cmds[]={
  57. {"ds_select_dst", w_ds_select_dst, 2, fixup_var_int_12, REQUEST_ROUTE|FAILURE_ROUTE},
  58. {"ds_select_new", w_ds_select_new, 2, fixup_var_int_12, REQUEST_ROUTE|FAILURE_ROUTE},
  59. {0,0,0,0,0}
  60. };
  61. static param_export_t params[]={
  62. {"list_file", PARAM_STRING, &dslistfile},
  63. {"force_dst", PARAM_INT, &force_dst},
  64. {"flags", PARAM_INT, &ds_flags},
  65. {0,0,0}
  66. };
  67. /** module exports */
  68. struct module_exports exports= {
  69. "dispatcher",
  70. cmds,
  71. rpc_methods, /* RPC methods */
  72. params,
  73. mod_init, /* module initialization function */
  74. (response_function) 0,
  75. (destroy_function) destroy,
  76. 0,
  77. child_init /* per-child init function */
  78. };
  79. /******************************************************************************
  80. * init module function
  81. *
  82. * - init memory
  83. * - clear both 'memory pages'
  84. * - load dispatcher lists from file
  85. * - activate list
  86. *
  87. ******************************************************************************/
  88. static int mod_init(void)
  89. {
  90. DBG("DISPATCHER: initializing ...\n");
  91. if(ds_init_memory() != 0) {
  92. LOG(L_ERR, "DISPATCHER:mod_init:ERROR -- memory allocation error\n");
  93. return -1;
  94. }
  95. /* clean both lists */
  96. ds_clean_list();
  97. DS_SWITCH_ACTIVE_LIST
  98. ds_clean_list();
  99. if(ds_load_list(dslistfile)!=0)
  100. {
  101. LOG(L_ERR, "DISPATCHER:mod_init:ERROR -- couldn't load list file\n");
  102. return -1;
  103. }
  104. /* switch active list since we had the offline one prepared */
  105. DS_SWITCH_ACTIVE_LIST
  106. return 0;
  107. }
  108. /**
  109. * Initialize children
  110. */
  111. static int child_init(int rank)
  112. {
  113. DBG("DISPATCHER:init_child #%d / pid <%d>\n", rank, getpid());
  114. return 0;
  115. }
  116. /**
  117. *
  118. */
  119. static int w_ds_select_dst(struct sip_msg* msg, char* set, char* alg)
  120. {
  121. if(msg==NULL)
  122. return -1;
  123. return ds_select_dst(msg, set, alg);
  124. }
  125. /**
  126. *
  127. */
  128. static int w_ds_select_new(struct sip_msg* msg, char* set, char* alg)
  129. {
  130. if(msg==NULL)
  131. return -1;
  132. return ds_select_new(msg, set, alg);
  133. }
  134. /**
  135. * destroy function
  136. */
  137. static void destroy(void)
  138. {
  139. DBG("DISPATCHER: destroy module ...\n");
  140. ds_destroy_lists();
  141. }