ds_backend.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /**
  2. * $Id$
  3. *
  4. * dispatcher module
  5. *
  6. * Copyright (C) 2004-2006 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include "../../trim.h"
  34. #include "../../dprint.h"
  35. #include "../../mem/mem.h"
  36. #include "../../mem/shm_mem.h"
  37. #include "../../parser/parse_uri.h"
  38. #include "../../parser/parse_from.h"
  39. #include "../../dset.h"
  40. #include "dispatcher.h"
  41. /******************************************************************************
  42. *
  43. * ds_destroy_lists()
  44. *
  45. * free all memory occupied by dispatcher module
  46. *
  47. *****************************************************************************/
  48. int ds_destroy_lists()
  49. {
  50. extern int *ds_activelist;
  51. extern char ***ds_setp_a, ***ds_setp_b;
  52. extern int *ds_setlen_a, *ds_setlen_b;
  53. int set, node;
  54. /* assume the whole structure has been allocated if ds_activelist
  55. * is non-NULL
  56. * hint: when using ser -c memory wasn't allocated
  57. */
  58. if (ds_activelist == NULL)
  59. return 0;
  60. /* free sets and nodes */
  61. for (set = 0; set < DS_MAX_SETS; set++) {
  62. for (node = 0; node < DS_MAX_NODES; node++) {
  63. shm_free(ds_setp_a[set][node]);
  64. shm_free(ds_setp_b[set][node]);
  65. }
  66. shm_free(ds_setp_a[set]);
  67. shm_free(ds_setp_b[set]);
  68. }
  69. /* free counters */
  70. shm_free(ds_setlen_a);
  71. shm_free(ds_setlen_b);
  72. /* eventually, free ds_activelist */
  73. shm_free(ds_activelist);
  74. return 0;
  75. }
  76. /******************************************************************************
  77. *
  78. * ds_get_hash()
  79. *
  80. * obtain hash from given strings
  81. *
  82. *****************************************************************************/
  83. unsigned int ds_get_hash(str *x, str *y)
  84. {
  85. char* p;
  86. register unsigned v;
  87. register unsigned h;
  88. if(!x && !y)
  89. return 0;
  90. h=0;
  91. if(x)
  92. {
  93. p=x->s;
  94. if (x->len>=4){
  95. for (;p<=(x->s+x->len-4); p+=4){
  96. v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
  97. h+=v^(v>>3);
  98. }
  99. };
  100. v=0;
  101. for (;p<(x->s+x->len); p++)
  102. {
  103. v<<=8;
  104. v+=*p;
  105. }
  106. h+=v^(v>>3);
  107. }
  108. if(y)
  109. {
  110. p=y->s;
  111. if (y->len>=4){
  112. for (;p<=(y->s+y->len-4); p+=4){
  113. v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
  114. h+=v^(v>>3);
  115. }
  116. };
  117. v=0;
  118. for (;p<(y->s+y->len); p++)
  119. {
  120. v<<=8;
  121. v+=*p;
  122. }
  123. h+=v^(v>>3);
  124. }
  125. h=((h)+(h>>11))+((h>>13)+(h>>23));
  126. return (h)?h:1;
  127. }
  128. /*
  129. * gets the part of the uri we will use as a key for hashing
  130. * params: key1 - will be filled with first part of the key
  131. * (uri user or "" if no user)
  132. * key2 - will be filled with the second part of the key
  133. * (uri host:port)
  134. * uri - str with the whole uri
  135. * parsed_uri - struct sip_uri pointer with the parsed uri
  136. * (it must point inside uri). It can be null
  137. * (in this case the uri will be parsed internally).
  138. * flags - if & DS_HASH_USER_ONLY, only the user part of the uri
  139. * will be used
  140. * returns: -1 on error, 0 on success
  141. */
  142. static inline int get_uri_hash_keys(str* key1, str* key2,
  143. str* uri, struct sip_uri* parsed_uri, int flags)
  144. {
  145. struct sip_uri tmp_p_uri; /* used only if parsed_uri==0 */
  146. if (parsed_uri==0){
  147. if (parse_uri(uri->s, uri->len, &tmp_p_uri)<0){
  148. LOG(L_ERR, "DISPATCHER: get_uri_hash_keys: invalid uri %.*s\n",
  149. uri->len, uri->len?uri->s:"");
  150. goto error;
  151. }
  152. parsed_uri=&tmp_p_uri;
  153. }
  154. /* uri sanity checks */
  155. if (parsed_uri->host.s==0){
  156. LOG(L_ERR, "DISPATCHER: get_uri_hash_keys: invalid uri, no host"
  157. "present: %.*s\n", uri->len, uri->len?uri->s:"");
  158. goto error;
  159. }
  160. /* we want: user@host:port if port !=5060
  161. * user@host if port==5060
  162. * user if the user flag is set*/
  163. *key1=parsed_uri->user;
  164. key2->s=0;
  165. key2->len=0;
  166. if ((!(flags & (DS_HASH_USER_ONLY | DS_HASH_USER_OR_HOST))) ||
  167. ((key1->s==0) && (flags & DS_HASH_USER_OR_HOST))){
  168. /* key2=host */
  169. *key2=parsed_uri->host;
  170. /* add port if needed */
  171. if (parsed_uri->port.s!=0){ /* uri has a port */
  172. /* skip port if == 5060 or sips and == 5061 */
  173. if (parsed_uri->port_no !=
  174. ((parsed_uri->type==SIPS_URI_T)?SIPS_PORT:SIP_PORT))
  175. key2->len+=parsed_uri->port.len+1 /* ':' */;
  176. }
  177. }
  178. if (key1->s==0 && (flags & DS_HASH_USER_ONLY)){
  179. LOG(L_WARN, "DISPATCHER: get_uri_hash_keys: empty username in:"
  180. " %.*s\n", uri->len, uri->len?uri->s:"");
  181. }
  182. return 0;
  183. error:
  184. return -1;
  185. }
  186. /**
  187. *
  188. */
  189. int ds_hash_fromuri(struct sip_msg *msg, unsigned int *hash)
  190. {
  191. str from;
  192. str key1;
  193. str key2;
  194. if(msg==NULL || hash == NULL)
  195. {
  196. LOG(L_ERR, "DISPATCHER:ds_hash_fromuri: bad parameters\n");
  197. return -1;
  198. }
  199. if(parse_from_header(msg)==-1)
  200. {
  201. LOG(L_ERR, "DISPATCHER:ds_hash_fromuri:ERROR cannot parse From hdr\n");
  202. return -1;
  203. }
  204. if(msg->from==NULL || get_from(msg)==NULL)
  205. {
  206. LOG(L_ERR, "DISPATCHER:ds_hash_fromuri:ERROR cannot get From uri\n");
  207. return -1;
  208. }
  209. from = get_from(msg)->uri;
  210. trim(&from);
  211. if (get_uri_hash_keys(&key1, &key2, &from, 0, ds_flags)<0)
  212. return -1;
  213. *hash = ds_get_hash(&key1, &key2);
  214. return 0;
  215. }
  216. /**
  217. *
  218. */
  219. int ds_hash_touri(struct sip_msg *msg, unsigned int *hash)
  220. {
  221. str to;
  222. str key1;
  223. str key2;
  224. if(msg==NULL || hash == NULL)
  225. {
  226. LOG(L_ERR, "DISPATCHER:ds_hash_touri: bad parameters\n");
  227. return -1;
  228. }
  229. if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
  230. (msg->to==0)))
  231. {
  232. LOG(L_ERR, "DISPATCHER:ds_hash_touri:ERROR cannot parse To hdr\n");
  233. return -1;
  234. }
  235. to = get_to(msg)->uri;
  236. trim(&to);
  237. if (get_uri_hash_keys(&key1, &key2, &to, 0, ds_flags)<0)
  238. return -1;
  239. *hash = ds_get_hash(&key1, &key2);
  240. return 0;
  241. }
  242. /**
  243. *
  244. */
  245. int ds_hash_callid(struct sip_msg *msg, unsigned int *hash)
  246. {
  247. str cid;
  248. if(msg==NULL || hash == NULL)
  249. {
  250. LOG(L_ERR, "DISPATCHER:ds_hash_callid: bad parameters\n");
  251. return -1;
  252. }
  253. if(msg->callid==NULL && ((parse_headers(msg, HDR_CALLID_F, 0)==-1) ||
  254. (msg->callid==NULL)) )
  255. {
  256. LOG(L_ERR, "DISPATCHER:ds_hash_callid:ERROR cannot parse Call-Id\n");
  257. return -1;
  258. }
  259. cid.s = msg->callid->body.s;
  260. cid.len = msg->callid->body.len;
  261. trim(&cid);
  262. *hash = ds_get_hash(&cid, NULL);
  263. return 0;
  264. }
  265. int ds_hash_ruri(struct sip_msg *msg, unsigned int *hash)
  266. {
  267. str* uri;
  268. str key1;
  269. str key2;
  270. if(msg==NULL || hash == NULL)
  271. {
  272. LOG(L_ERR, "DISPATCHER:ds_hash_ruri: bad parameters\n");
  273. return -1;
  274. }
  275. if (parse_sip_msg_uri(msg)<0){
  276. LOG(L_ERR, "DISPATCHER: ds_hash_ruri: ERROR: bad request uri\n");
  277. return -1;
  278. }
  279. uri=GET_RURI(msg);
  280. if (get_uri_hash_keys(&key1, &key2, uri, &msg->parsed_uri, ds_flags)<0)
  281. return -1;
  282. *hash = ds_get_hash(&key1, &key2);
  283. return 0;
  284. }
  285. /* from dispatcher */
  286. static int set_new_uri_simple(struct sip_msg *msg, str *uri)
  287. {
  288. if (msg->new_uri.s)
  289. {
  290. pkg_free(msg->new_uri.s);
  291. msg->new_uri.len=0;
  292. }
  293. msg->parsed_uri_ok=0;
  294. msg->new_uri.s = (char*)pkg_malloc(uri->len+1);
  295. if (msg->new_uri.s==0)
  296. {
  297. ERR("no more pkg memory\n");
  298. return -1;
  299. }
  300. memcpy(msg->new_uri.s, uri->s, uri->len);
  301. msg->new_uri.s[uri->len]=0;
  302. msg->new_uri.len=uri->len;
  303. ruri_mark_new();
  304. return 0;
  305. }
  306. /* from dispatcher */
  307. static int set_new_uri_with_user(struct sip_msg *msg, str *uri, str *user)
  308. {
  309. struct sip_uri dst;
  310. int start_len, stop_len;
  311. if (parse_uri(uri->s, uri->len, &dst) < 0) {
  312. ERR("can't parse destination URI\n");
  313. return -1;
  314. }
  315. if ((!dst.host.s) || (dst.host.len <= 0)) {
  316. ERR("destination URI host not set\n");
  317. return -1;
  318. }
  319. if (dst.user.s && (dst.user.len > 0)) {
  320. DBG("user already exists\n");
  321. /* don't replace the user */
  322. return set_new_uri_simple(msg, uri);
  323. }
  324. if (msg->new_uri.s)
  325. {
  326. pkg_free(msg->new_uri.s);
  327. msg->new_uri.len=0;
  328. }
  329. start_len = dst.host.s - uri->s;
  330. stop_len = uri->len - start_len;
  331. msg->parsed_uri_ok=0;
  332. msg->new_uri.s = (char*)pkg_malloc(uri->len+1+user->len+1);
  333. if (msg->new_uri.s==0)
  334. {
  335. ERR("no more pkg memory\n");
  336. return -1;
  337. }
  338. memcpy(msg->new_uri.s, uri->s, start_len);
  339. memcpy(msg->new_uri.s + start_len, user->s, user->len);
  340. *(msg->new_uri.s + start_len + user->len) = '@';
  341. memcpy(msg->new_uri.s + start_len + user->len + 1, dst.host.s, stop_len);
  342. msg->new_uri.len=uri->len + user->len + 1;
  343. msg->new_uri.s[msg->new_uri.len]=0;
  344. ruri_mark_new();
  345. return 0;
  346. }
  347. static int set_new_uri(struct sip_msg *msg, str *uri)
  348. {
  349. struct to_body* to;
  350. struct sip_uri to_uri;
  351. /* we need to leave original user */
  352. to = get_to(msg);
  353. if (to) {
  354. if (parse_uri(to->uri.s, to->uri.len, &to_uri) >= 0) {
  355. if (to_uri.user.s && (to_uri.user.len > 0)) {
  356. return set_new_uri_with_user(msg, uri, &to_uri.user);
  357. }
  358. }
  359. }
  360. return set_new_uri_simple(msg, uri);
  361. }
  362. /******************************************************************************
  363. *
  364. * ds_select_dst_impl()
  365. *
  366. * use requested algorithm to calculate hash and pull an URI off the
  367. * active dispatcher list.
  368. * set dst_uri or new_uri accordingly
  369. * Attention: if specific hash algorithms fail the module falls back
  370. * to a hash on the Call-ID
  371. *
  372. *****************************************************************************/
  373. int ds_select_dst_impl(struct sip_msg *msg, char *set_, char *alg_, int set_new)
  374. {
  375. extern int *ds_activelist;
  376. extern char ***ds_setp_a, ***ds_setp_b;
  377. extern int *ds_setlen_a, *ds_setlen_b;
  378. int set, alg;
  379. unsigned int hash;
  380. str uri;
  381. if(msg==NULL)
  382. {
  383. LOG(L_ERR, "DISPATCHER:ds_select_dst: bad parameters\n");
  384. return -1;
  385. }
  386. if ( get_int_fparam(&set, msg, (fparam_t*)set_) < 0 ) {
  387. LOG(L_ERR, "DISPATCHER:ds_select_dst: bad set value (%d)\n", set);
  388. return -1;
  389. }
  390. if ( get_int_fparam(&alg, msg, (fparam_t*)alg_) < 0 ) {
  391. LOG(L_ERR, "DISPATCHER:ds_select_dst: bad algorithm (%d)\n", alg);
  392. return -1;
  393. }
  394. if ((set < 0) || (set >= DS_MAX_SETS)) {
  395. LOG(L_ERR, "DISPATCHER:ds_select_dst: bad set offset (%d)\n", set);
  396. return -1;
  397. }
  398. if ((alg < 0) || (alg > 4)) {
  399. LOG(L_ERR, "DISPATCHER:ds_select_dst: invalid algorithm\n");
  400. return -1;
  401. }
  402. if (((*ds_activelist == 0) ? ds_setlen_a[set] : ds_setlen_b[set]) <= 0) {
  403. LOG(L_ERR, "DISPATCHER:ds_select_dst: empty destination set\n");
  404. return -1;
  405. }
  406. if (msg->dst_uri.s != NULL || msg->dst_uri.len > 0) {
  407. if (msg->dst_uri.s)
  408. pkg_free(msg->dst_uri.s);
  409. msg->dst_uri.s = NULL;
  410. msg->dst_uri.len = 0;
  411. }
  412. /* get hash */
  413. hash = 0;
  414. switch (alg) {
  415. /* see bottom for case '0' */
  416. case 1: /* hash from uri */
  417. if (ds_hash_fromuri(msg, &hash) != 0) {
  418. if (ds_hash_callid(msg, &hash) != 0) {
  419. LOG(L_ERR, "DISPATCHER:ds_select_dst: cannot determine from uri hash\n");
  420. return -1;
  421. }
  422. }
  423. break;
  424. case 2: /* hash to uri */
  425. if (ds_hash_touri(msg, &hash) != 0) {
  426. if (ds_hash_callid(msg, &hash) != 0) {
  427. LOG(L_ERR, "DISPATCHER:ds_select_dst: cannot determine from uri hash\n");
  428. return -1;
  429. }
  430. }
  431. break;
  432. case 3: /* hash Request uri */
  433. if (ds_hash_ruri(msg, &hash) != 0) {
  434. if (ds_hash_callid(msg, &hash) != 0) {
  435. LOG(L_ERR, "DISPATCHER:ds_select_dst: cannot determine from uri hash\n");
  436. return -1;
  437. }
  438. }
  439. break;
  440. case 4: /* Call ID hash, shifted right once to skip low bit
  441. * This should allow for even distribution when using
  442. * Call ID hash twice (i.e. fe + be)
  443. */
  444. if (ds_hash_callid(msg, &hash) != 0) {
  445. LOG(L_ERR,
  446. "DISPATCHER:ds_select_dst: cannot determine callid hash\n");
  447. hash = 0; /* bad default, just to be sure */
  448. return -1;
  449. }
  450. hash = hash >> 4; /* should be enough for even more backends */
  451. break;
  452. case 0: /* hash call id */ /* fall-through */
  453. default:
  454. if (ds_hash_callid(msg, &hash) != 0) {
  455. LOG(L_ERR,
  456. "DISPATCHER:ds_select_dst: cannot determine callid hash\n");
  457. hash = 0; /* bad default, just to be sure */
  458. return -1;
  459. }
  460. break; /* make gcc happy */
  461. }
  462. DBG("DISPATCHER:ds_select_dst: hash: [%u]\n", hash);
  463. /* node list offset from hash */
  464. if (*ds_activelist == 0) {
  465. hash = hash % ds_setlen_a[set];
  466. uri.s = ds_setp_a[set][hash];
  467. uri.len = strlen(ds_setp_a[set][hash]);
  468. } else {
  469. hash = hash % ds_setlen_b[set];
  470. uri.s = ds_setp_b[set][hash];
  471. uri.len = strlen(ds_setp_b[set][hash]);
  472. }
  473. if (!set_new) {
  474. if (set_dst_uri(msg, &uri) < 0) {
  475. LOG(L_ERR,
  476. "DISPATCHER:dst_select_dst: Error while setting dst_uri\n");
  477. return -1;
  478. }
  479. /* dst_uri changed, so it makes sense to re-use the current uri for
  480. forking */
  481. ruri_mark_new(); /* re-use uri for serial forking */
  482. DBG("DISPATCHER:ds_select_dst: selected [%d-%d-%d] <%.*s>\n",
  483. alg, set, hash, msg->dst_uri.len, msg->dst_uri.s);
  484. } else {
  485. if (set_new_uri(msg, &uri) < 0) {
  486. LOG(L_ERR,
  487. "DISPATCHER:dst_select_dst: Error while setting new_uri\n");
  488. return -1;
  489. }
  490. DBG("DISPATCHER:ds_select_dst: selected [%d-%d-%d] <%.*s>\n",
  491. alg, set, hash, msg->new_uri.len, msg->dst_uri.s);
  492. }
  493. return 1;
  494. }
  495. /**
  496. * from dispatcher
  497. */
  498. int ds_select_dst(struct sip_msg *msg, char *set, char *alg)
  499. {
  500. return ds_select_dst_impl(msg, set, alg, 0);
  501. }
  502. /**
  503. * from dispatcher
  504. */
  505. int ds_select_new(struct sip_msg *msg, char *set, char *alg)
  506. {
  507. return ds_select_dst_impl(msg, set, alg, 1);
  508. }
  509. /******************************************************************************
  510. *
  511. * ds_init_memory()
  512. *
  513. * init memory structure
  514. *
  515. * - ds_activelist: active list (0 or 1)
  516. * - ds_setp_a/ds_setp_b: each active config has up to DS_MAX_SETS so
  517. * called sets. Each set references to a node list with DS_MAX_NODES
  518. * slots that hold SIP URIs up to DS_MAX_URILEN-1 Bytes
  519. *
  520. *****************************************************************************/
  521. #define MALLOC_ERR LOG(L_ERR, \
  522. "ERROR:DISPATCHER:init_dispatcher_mem: shm_malloc() failed\n");
  523. int ds_init_memory() {
  524. extern int *ds_activelist;
  525. extern char ***ds_setp_a, ***ds_setp_b;
  526. extern int *ds_setlen_a, *ds_setlen_b;
  527. int set, node;
  528. /* active list */
  529. ds_activelist = (int *) shm_malloc(sizeof(int));
  530. if (ds_activelist == NULL) {
  531. MALLOC_ERR;
  532. return -1;
  533. }
  534. *ds_activelist = 0;
  535. ds_setp_a = (char ***) shm_malloc(sizeof(char **) * DS_MAX_SETS);
  536. if (ds_setp_a == NULL) {
  537. MALLOC_ERR;
  538. return -1;
  539. }
  540. /* attach node list to each set */
  541. for (set = 0; set < DS_MAX_SETS; set++) {
  542. ds_setp_a[set] = (char **) shm_malloc(sizeof(char *) * DS_MAX_NODES);
  543. if (ds_setp_a[set] == NULL) {
  544. MALLOC_ERR;
  545. return -1;
  546. }
  547. /* init each node */
  548. for (node = 0; node < DS_MAX_NODES; node++) {
  549. ds_setp_a[set][node] = (char *)
  550. shm_malloc(sizeof(char) * DS_MAX_URILEN);
  551. if (ds_setp_a[set][node] == NULL) {
  552. MALLOC_ERR;
  553. return -1;
  554. }
  555. *ds_setp_a[set][node] = '\0';
  556. }
  557. }
  558. ds_setp_b = (char ***) shm_malloc(sizeof(char **) * DS_MAX_SETS);
  559. if (ds_setp_b == NULL) {
  560. MALLOC_ERR;
  561. return -1;
  562. }
  563. /* attach node list to each set */
  564. for (set = 0; set < DS_MAX_SETS; set++) {
  565. ds_setp_b[set] = (char **) shm_malloc(sizeof(char *) * DS_MAX_NODES);
  566. if (ds_setp_b[set] == NULL) {
  567. MALLOC_ERR;
  568. return -1;
  569. }
  570. /* init each node */
  571. for (node = 0; node < DS_MAX_NODES; node++) {
  572. ds_setp_b[set][node] = (char *)
  573. shm_malloc(sizeof(char) * DS_MAX_URILEN);
  574. if (ds_setp_b[set][node] == NULL) {
  575. MALLOC_ERR;
  576. return -1;
  577. }
  578. *ds_setp_b[set][node] = '\0';
  579. }
  580. }
  581. /* set length counters */
  582. ds_setlen_a = (int *) shm_malloc(sizeof(int) * DS_MAX_SETS);
  583. if (ds_setlen_a == NULL) {
  584. MALLOC_ERR;
  585. return -1;
  586. }
  587. ds_setlen_b = (int *) shm_malloc(sizeof(int) * DS_MAX_SETS);
  588. if (ds_setlen_b == NULL) {
  589. MALLOC_ERR;
  590. return -1;
  591. }
  592. for (set = 0; set < DS_MAX_SETS; set++) {
  593. ds_setlen_a[set] = 0;
  594. ds_setlen_b[set] = 0;
  595. }
  596. return 0;
  597. }
  598. /******************************************************************************
  599. *
  600. * ds_clean_list()
  601. *
  602. * empty the in-active config so we can reload a new one
  603. * this does not free() memory!
  604. *
  605. *****************************************************************************/
  606. void ds_clean_list(void) {
  607. extern int *ds_activelist;
  608. extern char ***ds_setp_a, ***ds_setp_b;
  609. extern int *ds_setlen_a, *ds_setlen_b;
  610. int set, node;
  611. for (set = 0; set < DS_MAX_SETS; set++) {
  612. for (node = 0; node < DS_MAX_NODES; node++) {
  613. if (*ds_activelist == 0) {
  614. *ds_setp_b[set][node] = '\0';
  615. } else {
  616. *ds_setp_a[set][node] = '\0';
  617. }
  618. }
  619. if (*ds_activelist == 0) {
  620. ds_setlen_b[set] = 0;
  621. } else {
  622. ds_setlen_a[set] = 0;
  623. }
  624. }
  625. return;
  626. }
  627. /******************************************************************************
  628. *
  629. * ds_load_list()
  630. *
  631. * (re)load dispatcher module config file using the dispatcher module
  632. * parser.
  633. * Save config in the in-active shared memory section
  634. * Attention: We do not bail out with an error if we try to store more
  635. * sets/nodes than we have room for since we might be running
  636. * live and don't want SER to stop processing packets, do we?
  637. *
  638. *****************************************************************************/
  639. int ds_load_list (char *lfile) {
  640. /* storage */
  641. extern int *ds_activelist;
  642. extern char ***ds_setp_a, ***ds_setp_b;
  643. extern int *ds_setlen_a, *ds_setlen_b;
  644. /* parser related */
  645. char line[MAX_LINE_LEN], *p;
  646. int set;
  647. str uri;
  648. struct sip_uri puri;
  649. FILE *f = NULL;
  650. DBG("ds_load_list() invoked\n");
  651. /* clean up temporary list before saving updated config */
  652. (void) ds_clean_list();
  653. if (lfile == NULL || strlen(lfile) <= 0) {
  654. LOG(L_ERR, "DISPATCHER:ds_load_list: cannot open list file [%s]\n",
  655. lfile);
  656. return -1;
  657. }
  658. f = fopen(lfile, "r");
  659. if (f == NULL) {
  660. LOG(L_ERR, "DISPATCHER:ds_load_list: cannot open list file [%s]\n",
  661. lfile);
  662. return -1;
  663. }
  664. p = fgets(line, MAX_LINE_LEN-1, f);
  665. while (p)
  666. {
  667. /* eat all white spaces */
  668. while (*p && (*p==' ' || *p=='\t' || *p=='\r' || *p=='\n'))
  669. p++;
  670. if (*p=='\0' || *p=='#')
  671. goto next_line;
  672. /* get set id */
  673. set = 0;
  674. while(*p>='0' && *p<='9')
  675. {
  676. set = set*10+ (*p-'0');
  677. p++;
  678. }
  679. /* eat all white spaces */
  680. while(*p && (*p==' ' || *p=='\t' || *p=='\r' || *p=='\n'))
  681. p++;
  682. if(*p=='\0' || *p=='#')
  683. {
  684. LOG(L_ERR, "DISPATCHER:ds_load_list: bad line [%s]\n", line);
  685. goto error;
  686. }
  687. /* get uri */
  688. uri.s = p;
  689. while(*p && *p!=' ' && *p!='\t' && *p!='\r' && *p!='\n' && *p!='#')
  690. p++;
  691. uri.len = p-uri.s;
  692. /* check uri */
  693. if(parse_uri(uri.s, uri.len, &puri)!=0)
  694. {
  695. LOG(L_ERR, "DISPATCHER:ds_load_list: bad uri [%.*s]\n",
  696. uri.len, uri.s);
  697. goto next_line;
  698. }
  699. /* now we have set id and get uri -> save to shared mem */
  700. if ((set > DS_MAX_SETS-1) || (uri.len > DS_MAX_URILEN))
  701. {
  702. LOG(L_ERR, "DISPATCHER:ds_load_list: increase DS_MAX_SETS or DS_MAX_URILEN ...\n");
  703. goto next_line;
  704. }
  705. /* save correct line from config file */
  706. DBG("content: set %d, str: %.*s\n", set, uri.len, uri.s);
  707. if (*ds_activelist == 0) {
  708. DBG("[%d] active nodes in this set so far: %d\n",
  709. *ds_activelist, ds_setlen_b[set]);
  710. if (ds_setlen_b[set] >= (DS_MAX_NODES-1)) {
  711. LOG(L_ERR, "DISPATCHER:ds_load_list: increase DS_MAX_NODES!\n");
  712. goto next_line;
  713. }
  714. snprintf((char *)ds_setp_b[set][ds_setlen_b[set]], DS_MAX_URILEN-1,
  715. "%.*s", uri.len, uri.s);
  716. ds_setlen_b[set]++;
  717. DBG("[%d] active nodes in this set now: %d\n",
  718. *ds_activelist, ds_setlen_b[set]);
  719. DBG("node now contains: %s\n", (char *)ds_setp_b[set][ds_setlen_b[set]-1]);
  720. } else {
  721. DBG("[%d] active nodes in this set so far: %d\n",
  722. *ds_activelist, ds_setlen_a[set]);
  723. if (ds_setlen_a[set] >= (DS_MAX_NODES-1)) {
  724. LOG(L_ERR, "DISPATCHER:ds_load_list: increase DS_MAX_NODES!\n");
  725. goto next_line;
  726. }
  727. snprintf((char *)ds_setp_a[set][ds_setlen_a[set]], DS_MAX_URILEN-1,
  728. "%.*s", uri.len, uri.s);
  729. ds_setlen_a[set]++;
  730. DBG("[%d] active nodes in this set now: %d\n",
  731. *ds_activelist, ds_setlen_a[set]);
  732. DBG("node now contains: %s\n", (char *)ds_setp_a[set][ds_setlen_a[set]-1]);
  733. }
  734. next_line:
  735. p = fgets(line, MAX_LINE_LEN-1, f);
  736. }
  737. if (f != NULL)
  738. fclose(f);
  739. /* see if there are any active sets at all */
  740. if (*ds_activelist == 0) {
  741. int found = 0;
  742. int i;
  743. for (i = 0; i < DS_MAX_SETS; i++) {
  744. if (ds_setlen_b[i] > 0)
  745. found++;
  746. }
  747. if (!found)
  748. return -1;
  749. } else {
  750. int found = 0;
  751. int i;
  752. for (i = 0; i < DS_MAX_SETS; i++) {
  753. if (ds_setlen_a[i] > 0)
  754. found++;
  755. }
  756. if (!found)
  757. return -1;
  758. }
  759. return 0;
  760. error:
  761. if (f != NULL)
  762. fclose(f);
  763. return -1;
  764. }