Kannel: Open Source WAP and SMS gateway  svn-r5335
smscconn.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * SMSC Connection
59  *
60  * Interface for main bearerbox to SMS center connection modules
61  *
62  * Kalle Marjola 2000 for project Kannel
63  */
64 
65 #include <signal.h>
66 #include <time.h>
67 
68 #include "gwlib/gwlib.h"
69 #include "gwlib/gw-regex.h"
70 #include "smscconn.h"
71 #include "smscconn_p.h"
72 #include "bb_smscconn_cb.h"
73 #include "sms.h"
74 
76 
77 /*
78  * Some defaults
79  */
80 #define SMSCCONN_RECONNECT_DELAY 10.0
81 
82 
83 /*
84  * Add reroute information to the connection data. Where the priority
85  * is in the order: reroute, reroute-smsc-id, reroute-receiver.
86  */
87 static void init_reroute(SMSCConn *conn, CfgGroup *grp)
88 {
89  Octstr *rule;
90  long i;
91 
92  if (cfg_get_bool(&conn->reroute_dlr, grp, octstr_imm("reroute-dlr")) == -1)
93  conn->reroute_dlr = 0;
94  info(0, "DLR rerouting for smsc id <%s> %s.", octstr_get_cstr(conn->id), (conn->reroute_dlr?"enabled":"disabled"));
95 
96  if (cfg_get_bool(&conn->reroute, grp, octstr_imm("reroute")) != -1) {
97  debug("smscconn",0,"Adding general internal routing for smsc id <%s>",
98  octstr_get_cstr(conn->id));
99  return;
100  }
101 
102  if ((conn->reroute_to_smsc = cfg_get(grp, octstr_imm("reroute-smsc-id"))) != NULL) {
103  /* reroute all messages to a specific smsc-id */
104  debug("smscconn",0,"Adding internal routing: smsc id <%s> to smsc id <%s>",
106  return;
107  }
108 
109  if ((rule = cfg_get(grp, octstr_imm("reroute-receiver"))) != NULL) {
110  List *routes;
111 
112  /* create hash dictionary for this smsc-id */
113  conn->reroute_by_receiver = dict_create(100, (void(*)(void *)) octstr_destroy);
114 
115  routes = octstr_split(rule, octstr_imm(";"));
116  for (i = 0; i < gwlist_len(routes); i++) {
117  Octstr *item = gwlist_get(routes, i);
118  Octstr *smsc, *receiver;
119  List *receivers;
120 
121  /* first word is the smsc-id, all other are the receivers */
122  receivers = octstr_split(item, octstr_imm(","));
123  smsc = gwlist_extract_first(receivers);
124  if (smsc)
125  octstr_strip_blanks(smsc);
126 
127  while((receiver = gwlist_extract_first(receivers))) {
129  debug("smscconn",0,"Adding internal routing for smsc id <%s>: "
130  "receiver <%s> to smsc id <%s>",
132  octstr_get_cstr(smsc));
134  panic(0, "Could not set internal routing for smsc id <%s>: "
135  "receiver <%s> to smsc id <%s>, because receiver has already routing entry!",
137  octstr_get_cstr(smsc));
139  }
140  octstr_destroy(smsc);
142  }
143  octstr_destroy(rule);
145  }
146 
147  if ((rule = cfg_get(grp, octstr_imm("reroute-receiver-regex"))) != NULL) {
148  List *routes;
149 
150  /* create list with regex patterns and smsc-id */
152 
153  routes = octstr_split(rule, octstr_imm(";"));
154  for (i = 0; i < gwlist_len(routes); i++) {
155  Octstr *item = gwlist_get(routes, i);
156  Octstr *smsc, *receiver;
157  List *receivers;
158  regex_t *re;
159  pattern_route *r;
160 
161  /* first word is the smsc-id, all other are receiver regex */
162  receivers = octstr_split(item, octstr_imm(","));
163  smsc = gwlist_extract_first(receivers);
164  if (smsc)
165  octstr_strip_blanks(smsc);
166 
167  while ((receiver = gwlist_extract_first(receivers))) {
169 
170  if ((re = gw_regex_comp(receiver, REG_EXTENDED)) == NULL) {
171  grp_dump(grp);
172  error(0, "Could not compile regex pattern '%s', skipping.", octstr_get_cstr(receiver));
174  continue;
175  }
176 
177  r = gw_malloc(sizeof(pattern_route));
178  r->id = octstr_duplicate(smsc);
179  r->re = re;
180 
181  debug("smscconn",0,"Adding internal routing for smsc id <%s>: "
182  "receiver-regex <%s> to smsc id <%s>",
184  octstr_get_cstr(smsc));
187  }
188  octstr_destroy(smsc);
190  }
191  octstr_destroy(rule);
193  }
194 }
195 
196 
197 unsigned int smscconn_instances(CfgGroup *grp)
198 {
199  long i;
200 
201  if (cfg_get_integer(&i, grp, octstr_imm("instances")) == -1)
202  i = 1;
203  else if (i < 0)
204  i = 0;
205 
206  return i;
207 }
208 
209 
210 SMSCConn *smscconn_create(CfgGroup *grp, int start_as_stopped)
211 {
212  SMSCConn *conn;
213  Octstr *smsc_type;
214  int ret;
215  Octstr *allowed_smsc_id_regex;
216  Octstr *denied_smsc_id_regex;
217  Octstr *allowed_prefix_regex;
218  Octstr *denied_prefix_regex;
219  Octstr *preferred_prefix_regex;
220  Octstr *tmp;
221 
222  if (grp == NULL)
223  return NULL;
224 
225  conn = gw_malloc(sizeof(*conn));
226  memset(conn, 0, sizeof(*conn));
227 
228  conn->why_killed = SMSCCONN_ALIVE;
229  conn->status = SMSCCONN_CONNECTING;
230  conn->connect_time = -1;
231  conn->is_stopped = start_as_stopped;
232 
233 #define OCTSTR(os) octstr_imm(#os)
234 
235  /* checksum of the whole config, without instance multiplier */
236  conn->chksum = cfg_get_group_checksum(grp,
237  NULL
238  );
239 
240  /* checksum of the connection related part, without routing
241  * and without instance multiplier */
243  OCTSTR(denied-smsc-id),
244  OCTSTR(allowed-smsc-id),
245  OCTSTR(preferred-smsc-id),
246  OCTSTR(allowed-prefix),
247  OCTSTR(denied-prefix),
248  OCTSTR(preferred-prefix),
249  OCTSTR(unified-prefix),
250  OCTSTR(reroute),
251  OCTSTR(reroute-smsc-id),
252  OCTSTR(reroute-receiver),
253  OCTSTR(reroute-receiver-regex),
254  OCTSTR(reroute-dlr),
255  OCTSTR(allowed-smsc-id-regex),
256  OCTSTR(denied-smsc-id-regex),
257  OCTSTR(preferred-smsc-id-regex),
258  OCTSTR(allowed-prefix-regex),
259  OCTSTR(denied-prefix-regex),
260  OCTSTR(preferred-prefix-regex),
261  NULL
262  );
263 
264  conn->received = counter_create();
265  conn->received_dlr = counter_create();
266  conn->sent = counter_create();
267  conn->sent_dlr = counter_create();
268  conn->failed = counter_create();
269  conn->flow_mutex = mutex_create();
270 
271  conn->outgoing_sms_load = load_create();
272  /* add 60,300,-1 entries */
276 
277  conn->incoming_sms_load = load_create();
278  /* add 60,300,-1 entries */
282 
283  conn->incoming_dlr_load = load_create();
284  /* add 60,300,-1 entries to dlr */
288 
289  conn->outgoing_dlr_load = load_create();
290  /* add 60,300,-1 entries to dlr */
294 
295 
296 #define GET_OPTIONAL_VAL(x, n) x = cfg_get(grp, octstr_imm(n))
297 #define SPLIT_OPTIONAL_VAL(x, n) \
298  do { \
299  Octstr *tmp = cfg_get(grp, octstr_imm(n)); \
300  if (tmp) x = octstr_split(tmp, octstr_imm(";")); \
301  else x = NULL; \
302  octstr_destroy(tmp); \
303  }while(0)
304 
305  GET_OPTIONAL_VAL(conn->id, "smsc-id");
306  SPLIT_OPTIONAL_VAL(conn->allowed_smsc_id, "allowed-smsc-id");
307  SPLIT_OPTIONAL_VAL(conn->denied_smsc_id, "denied-smsc-id");
308  SPLIT_OPTIONAL_VAL(conn->preferred_smsc_id, "preferred-smsc-id");
309  GET_OPTIONAL_VAL(conn->allowed_prefix, "allowed-prefix");
310  GET_OPTIONAL_VAL(conn->denied_prefix, "denied-prefix");
311  GET_OPTIONAL_VAL(conn->preferred_prefix, "preferred-prefix");
312  GET_OPTIONAL_VAL(conn->unified_prefix, "unified-prefix");
313  GET_OPTIONAL_VAL(conn->our_host, "our-host");
314  GET_OPTIONAL_VAL(conn->log_file, "log-file");
315  cfg_get_bool(&conn->alt_dcs, grp, octstr_imm("alt-dcs"));
316 
317  GET_OPTIONAL_VAL(allowed_smsc_id_regex, "allowed-smsc-id-regex");
318  if (allowed_smsc_id_regex != NULL)
319  if ((conn->allowed_smsc_id_regex = gw_regex_comp(allowed_smsc_id_regex, REG_EXTENDED)) == NULL)
320  panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_smsc_id_regex));
321  GET_OPTIONAL_VAL(denied_smsc_id_regex, "denied-smsc-id-regex");
322  if (denied_smsc_id_regex != NULL)
323  if ((conn->denied_smsc_id_regex = gw_regex_comp(denied_smsc_id_regex, REG_EXTENDED)) == NULL)
324  panic(0, "Could not compile pattern '%s'", octstr_get_cstr(denied_smsc_id_regex));
325  GET_OPTIONAL_VAL(allowed_prefix_regex, "allowed-prefix-regex");
326  if (allowed_prefix_regex != NULL)
327  if ((conn->allowed_prefix_regex = gw_regex_comp(allowed_prefix_regex, REG_EXTENDED)) == NULL)
328  panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_prefix_regex));
329  GET_OPTIONAL_VAL(denied_prefix_regex, "denied-prefix-regex");
330  if (denied_prefix_regex != NULL)
331  if ((conn->denied_prefix_regex = gw_regex_comp(denied_prefix_regex, REG_EXTENDED)) == NULL)
332  panic(0, "Could not compile pattern '%s'", octstr_get_cstr(denied_prefix_regex));
333  GET_OPTIONAL_VAL(preferred_prefix_regex, "preferred-prefix-regex");
334  if (preferred_prefix_regex != NULL)
335  if ((conn->preferred_prefix_regex = gw_regex_comp(preferred_prefix_regex, REG_EXTENDED)) == NULL)
336  panic(0, "Could not compile pattern '%s'", octstr_get_cstr(preferred_prefix_regex));
337 
338  if ((tmp = cfg_get(grp, octstr_imm("throughput"))) != NULL) {
339  if (octstr_parse_double(&conn->throughput, tmp, 0) == -1)
340  conn->throughput = 0;
341  octstr_destroy(tmp);
342  info(0, "Set throughput to %.3f for smsc id <%s>", conn->throughput, octstr_get_cstr(conn->id));
343  }
344  /* Sets the admin_id. Equals to connection id if empty */
345  GET_OPTIONAL_VAL(conn->admin_id, "smsc-admin-id");
346  if (conn->admin_id == NULL)
347  conn->admin_id = octstr_duplicate(conn->id);
348 
349  /* configure the internal rerouting rules for this smsc id */
350  init_reroute(conn, grp);
351 
352  if (cfg_get_integer(&conn->log_level, grp, octstr_imm("log-level")) == -1)
353  conn->log_level = 0;
354 
355  if (cfg_get_integer(&conn->max_sms_octets, grp, octstr_imm("max-sms-octets")) == -1)
357 
358  if (cfg_get_bool(&conn->dead_start, grp, octstr_imm("dead-start")) == -1)
359  conn->dead_start = 0; /* default to connect at start-up time */
360 
361  /* open a smsc-id specific log-file in exlusive mode */
362  if (conn->log_file)
363  conn->log_idx = log_open(octstr_get_cstr(conn->log_file),
364  conn->log_level, GW_EXCL);
365 #undef GET_OPTIONAL_VAL
366 #undef SPLIT_OPTIONAL_VAL
367 
368  if (conn->allowed_smsc_id && conn->denied_smsc_id)
369  warning(0, "Both 'allowed-smsc-id' and 'denied-smsc-id' set, deny-list "
370  "automatically ignored");
371  if (conn->allowed_smsc_id_regex && conn->denied_smsc_id_regex)
372  warning(0, "Both 'allowed-smsc-id_regex' and 'denied-smsc-id_regex' set, deny-regex "
373  "automatically ignored");
374 
375  if (cfg_get_integer(&conn->reconnect_delay, grp,
376  octstr_imm("reconnect-delay")) == -1)
378 
379  smsc_type = cfg_get(grp, octstr_imm("smsc"));
380  if (smsc_type == NULL) {
381  error(0, "Required field 'smsc' missing for smsc group.");
382  smscconn_destroy(conn);
383  octstr_destroy(smsc_type);
384  return NULL;
385  }
386 
387  if (octstr_compare(smsc_type, octstr_imm("fake")) == 0)
388  ret = smsc_fake_create(conn, grp);
389  else if (octstr_compare(smsc_type, octstr_imm("cimd2")) == 0)
390  ret = smsc_cimd2_create(conn, grp);
391  else if (octstr_compare(smsc_type, octstr_imm("emi")) == 0)
392  ret = smsc_emi2_create(conn, grp);
393  else if (octstr_compare(smsc_type, octstr_imm("http")) == 0)
394  ret = smsc_http_create(conn, grp);
395  else if (octstr_compare(smsc_type, octstr_imm("smpp")) == 0)
396  ret = smsc_smpp_create(conn, grp);
397  else if (octstr_compare(smsc_type, octstr_imm("at")) == 0)
398  ret = smsc_at2_create(conn,grp);
399  else if (octstr_compare(smsc_type, octstr_imm("cgw")) == 0)
400  ret = smsc_cgw_create(conn,grp);
401  else if (octstr_compare(smsc_type, octstr_imm("smasi")) == 0)
402  ret = smsc_smasi_create(conn, grp);
403  else if (octstr_compare(smsc_type, octstr_imm("oisd")) == 0)
404  ret = smsc_oisd_create(conn, grp);
405  else if (octstr_compare(smsc_type, octstr_imm("loopback")) == 0)
406  ret = smsc_loopback_create(conn, grp);
407 #ifdef HAVE_GSOAP
408  else if (octstr_compare(smsc_type, octstr_imm("parlayx")) == 0)
409  ret = smsc_soap_parlayx_create(conn, grp);
410 #endif
411  else
412  ret = smsc_wrapper_create(conn, grp);
413 
414  octstr_destroy(smsc_type);
415  if (ret == -1) {
416  smscconn_destroy(conn);
417  return NULL;
418  }
419  gw_assert(conn->send_msg != NULL);
420 
421  bb_smscconn_ready(conn);
422 
423  return conn;
424 }
425 
426 
427 void smscconn_shutdown(SMSCConn *conn, int finish_sending)
428 {
429  gw_assert(conn != NULL);
430 
431  mutex_lock(conn->flow_mutex);
432  if (conn->status == SMSCCONN_DEAD) {
433  mutex_unlock(conn->flow_mutex);
434  return;
435  }
436 
437  /* Call SMSC specific destroyer */
438  if (conn->shutdown) {
439  /*
440  * we must unlock here, because module manipulate their state
441  * and will try to lock this mutex.Otherwise we have deadlock!
442  */
443  mutex_unlock(conn->flow_mutex);
444  conn->shutdown(conn, finish_sending);
445  }
446  else {
448  mutex_unlock(conn->flow_mutex);
449  }
450 
451  return;
452 }
453 
454 
455 static void pattern_route_destroy(void *arg)
456 {
457  pattern_route *r = arg;
458  gw_regex_destroy(r->re);
459  octstr_destroy(r->id);
460  gw_free(r);
461 }
462 
463 
465 {
466  if (conn == NULL)
467  return 0;
468  if (conn->status != SMSCCONN_DEAD)
469  return -1;
470  mutex_lock(conn->flow_mutex);
471 
472  counter_destroy(conn->received);
474  counter_destroy(conn->sent);
475  counter_destroy(conn->sent_dlr);
476  counter_destroy(conn->failed);
477 
482 
483  octstr_destroy(conn->name);
484  octstr_destroy(conn->id);
485  octstr_destroy(conn->admin_id);
493  octstr_destroy(conn->our_host);
494  octstr_destroy(conn->log_file);
495  octstr_destroy(conn->chksum);
497 
498  if (conn->denied_smsc_id_regex != NULL) gw_regex_destroy(conn->denied_smsc_id_regex);
499  if (conn->allowed_smsc_id_regex != NULL) gw_regex_destroy(conn->allowed_smsc_id_regex);
500  if (conn->preferred_prefix_regex != NULL) gw_regex_destroy(conn->preferred_prefix_regex);
501  if (conn->denied_prefix_regex != NULL) gw_regex_destroy(conn->denied_prefix_regex);
502  if (conn->allowed_prefix_regex != NULL) gw_regex_destroy(conn->allowed_prefix_regex);
503 
507 
508  mutex_unlock(conn->flow_mutex);
509  mutex_destroy(conn->flow_mutex);
510 
511  gw_free(conn);
512  return 0;
513 }
514 
515 
517 {
518  gw_assert(conn != NULL);
519  mutex_lock(conn->flow_mutex);
520  if (conn->status == SMSCCONN_DEAD || conn->is_stopped != 0
521  || conn->why_killed != SMSCCONN_ALIVE)
522  {
523  mutex_unlock(conn->flow_mutex);
524  return -1;
525  }
526  conn->is_stopped = 1;
527  mutex_unlock(conn->flow_mutex);
528 
529  if (conn->stop_conn)
530  conn->stop_conn(conn);
531 
532  return 0;
533 }
534 
535 
537 {
538  gw_assert(conn != NULL);
539  mutex_lock(conn->flow_mutex);
540  if (conn->status == SMSCCONN_DEAD || conn->is_stopped == 0) {
541  mutex_unlock(conn->flow_mutex);
542  return;
543  }
544  conn->is_stopped = 0;
545  mutex_unlock(conn->flow_mutex);
546 
547  if (conn->start_conn)
548  conn->start_conn(conn);
549 }
550 
551 
553 {
554  gw_assert(conn != NULL);
555  return conn->name;
556 }
557 
558 
560 {
561  gw_assert(conn != NULL);
562  return conn->id;
563 }
564 
565 
567 {
568  gw_assert(conn != NULL);
569  return conn->admin_id;
570 }
571 
572 
574 {
575  gw_assert(conn != NULL);
576  gw_assert(msg != NULL && msg_type(msg) == sms);
577 
578  /* dead transmitter or active receiver connections are not feasible */
579  if (conn->status == SMSCCONN_DEAD || conn->status == SMSCCONN_ACTIVE_RECV ||
580  conn->why_killed != SMSCCONN_ALIVE)
581  return -1;
582 
583  /* if allowed-smsc-id set, then only allow this SMSC if message
584  * smsc-id matches any of its allowed SMSCes
585  */
586  if (conn->allowed_smsc_id && (msg->sms.smsc_id == NULL ||
587  gwlist_search(conn->allowed_smsc_id, msg->sms.smsc_id, octstr_item_match) == NULL)) {
588  return -1;
589  }
590  /* ..if no allowed-smsc-id set but denied-smsc-id and message smsc-id
591  * is set, deny message if smsc-ids match */
592  else if (conn->denied_smsc_id && msg->sms.smsc_id != NULL &&
593  gwlist_search(conn->denied_smsc_id, msg->sms.smsc_id, octstr_item_match) != NULL) {
594  return -1;
595  }
596 
597  if (conn->allowed_smsc_id_regex) {
598  if (msg->sms.smsc_id == NULL)
599  return -1;
600 
601  if (gw_regex_match_pre(conn->allowed_smsc_id_regex, msg->sms.smsc_id) == 0)
602  return -1;
603  }
604  else if (conn->denied_smsc_id_regex && msg->sms.smsc_id != NULL) {
605  if (gw_regex_match_pre(conn->denied_smsc_id_regex, msg->sms.smsc_id) == 1)
606  return -1;
607  }
608 
609  /* Have allowed */
610  if (conn->allowed_prefix && !conn->denied_prefix &&
611  (does_prefix_match(conn->allowed_prefix, msg->sms.receiver) != 1))
612  return -1;
613 
614  if (conn->allowed_prefix_regex && !conn->denied_prefix_regex &&
615  gw_regex_match_pre(conn->allowed_prefix_regex, msg->sms.receiver) == 0)
616  return -1;
617 
618  /* Have denied */
619  if (conn->denied_prefix && !conn->allowed_prefix &&
620  (does_prefix_match(conn->denied_prefix, msg->sms.receiver) == 1))
621  return -1;
622 
623  if (conn->denied_prefix_regex && !conn->allowed_prefix_regex &&
624  gw_regex_match_pre(conn->denied_prefix_regex, msg->sms.receiver) == 1)
625  return -1;
626 
627  /* Have allowed and denied */
628  if (conn->denied_prefix && conn->allowed_prefix &&
629  (does_prefix_match(conn->allowed_prefix, msg->sms.receiver) != 1) &&
630  (does_prefix_match(conn->denied_prefix, msg->sms.receiver) == 1))
631  return -1;
632 
633  if (conn->allowed_prefix_regex && conn->denied_prefix_regex &&
634  gw_regex_match_pre(conn->allowed_prefix_regex, msg->sms.receiver) == 0 &&
635  gw_regex_match_pre(conn->denied_prefix_regex, msg->sms.receiver) == 1)
636  return -1;
637 
638  /* then see if it is preferred one */
639  if (conn->preferred_smsc_id && msg->sms.smsc_id != NULL &&
640  gwlist_search(conn->preferred_smsc_id, msg->sms.smsc_id, octstr_item_match) != NULL)
641  return 1;
642 
643  if (conn->preferred_prefix &&
644  does_prefix_match(conn->preferred_prefix, msg->sms.receiver) == 1)
645  return 1;
646 
647  if (conn->preferred_prefix_regex &&
648  gw_regex_match_pre(conn->preferred_prefix_regex, msg->sms.receiver) == 1)
649  return 1;
650 
651  return 0;
652 }
653 
654 
656 {
657  int ret = -1;
658  List *parts = NULL;
659 
660  gw_assert(conn != NULL);
661  mutex_lock(conn->flow_mutex);
662  if (conn->status == SMSCCONN_DEAD || conn->why_killed != SMSCCONN_ALIVE) {
663  mutex_unlock(conn->flow_mutex);
664  return -1;
665  }
666 
667  /* if this a retry of splitted message, don't unify prefix and don't try to split */
668  if (msg->sms.split_parts == NULL) {
669  /* normalize the destination number for this smsc */
670  char *uf = conn->unified_prefix ? octstr_get_cstr(conn->unified_prefix) : NULL;
671  normalize_number(uf, &(msg->sms.receiver));
672 
673  /* split msg */
674  parts = sms_split(msg, NULL, NULL, NULL, NULL, 1,
675  counter_increase(split_msg_counter) & 0xff, 0xff, conn->max_sms_octets);
676  if (gwlist_len(parts) == 1) {
677  /* don't create split_parts of sms fit into one */
679  parts = NULL;
680  }
681  }
682 
683  if (parts == NULL)
684  ret = conn->send_msg(conn, msg);
685  else {
686  long i, parts_len = gwlist_len(parts);
687  struct split_parts *split = gw_malloc(sizeof(*split));
688  /* must duplicate, because smsc2_route will destroy this msg */
689  split->orig = msg_duplicate(msg);
690  split->parts_left = counter_create();
691  split->status = SMSCCONN_SUCCESS;
692  counter_set(split->parts_left, parts_len);
693  split->smsc_conn = conn;
694  debug("bb.sms.splits", 0, "new split_parts created %p", split);
695  for (i = 0; i < parts_len; i++) {
696  msg = gwlist_get(parts, i);
697  msg->sms.split_parts = split;
698  ret = conn->send_msg(conn, msg);
699  if (ret < 0) {
700  if (i == 0) {
701  counter_destroy(split->parts_left);
703  gw_free(split);
704  mutex_unlock(conn->flow_mutex);
705  return ret;
706  }
707  /*
708  * Some parts were sent. So handle this within
709  * bb_smscconn_XXX().
710  */
712  counter_increase_with(split->parts_left, -(parts_len - i));
713  warning(0, "Could not send all parts of a split message");
714  break;
715  }
716  }
718  }
719  mutex_unlock(conn->flow_mutex);
720  return ret;
721 }
722 
723 
725 {
726  gw_assert(conn != NULL);
727 
728  return conn->status;
729 }
730 
731 
732 int smscconn_info(SMSCConn *conn, StatusInfo *infotable)
733 {
734  if (conn == NULL || infotable == NULL)
735  return -1;
736 
737  mutex_lock(conn->flow_mutex);
738 
739  infotable->status = conn->status;
740  infotable->killed = conn->why_killed;
741  infotable->is_stopped = conn->is_stopped;
742  infotable->online = time(NULL) - conn->connect_time;
743 
744  infotable->sent = counter_value(conn->sent);
745  infotable->received = counter_value(conn->received);
746  infotable->sent_dlr = counter_value(conn->sent_dlr);
747  infotable->received_dlr = counter_value(conn->received_dlr);
748  infotable->failed = counter_value(conn->failed);
749 
750  if (conn->queued)
751  infotable->queued = conn->queued(conn);
752  else
753  infotable->queued = -1;
754 
755  infotable->load = conn->load;
756 
757  mutex_unlock(conn->flow_mutex);
758 
759  return 0;
760 }
761 
762 
764 {
765  Octstr *tmp;
766 
767  gw_assert(conn != NULL);
768 
769  if (grp == NULL)
770  return;
771 
772 #define GET_OPTIONAL_VAL(x, n) \
773  octstr_destroy(x); \
774  x = cfg_get(grp, octstr_imm(n))
775 #define SPLIT_OPTIONAL_VAL(x, n) \
776  gwlist_destroy(x, octstr_destroy_item); \
777  if ((tmp = cfg_get(grp, octstr_imm(n))) != NULL) \
778  x = octstr_split(tmp, octstr_imm(";")); \
779  else \
780  x = NULL; \
781  octstr_destroy(tmp);
782 #define GET_OPTIONAL_REGEX(x, n) \
783  gw_regex_destroy(x); \
784  if ((tmp = cfg_get(grp, octstr_imm(n))) != NULL) { \
785  if ((x = gw_regex_comp(tmp, REG_EXTENDED)) == NULL) \
786  error(0, "Could not compile pattern '%s'", octstr_get_cstr(tmp)); \
787  octstr_destroy(tmp); \
788  }
789 
790  SPLIT_OPTIONAL_VAL(conn->allowed_smsc_id, "allowed-smsc-id");
791  SPLIT_OPTIONAL_VAL(conn->denied_smsc_id, "denied-smsc-id");
792  SPLIT_OPTIONAL_VAL(conn->preferred_smsc_id, "preferred-smsc-id");
793  GET_OPTIONAL_VAL(conn->allowed_prefix, "allowed-prefix");
794  GET_OPTIONAL_VAL(conn->denied_prefix, "denied-prefix");
795  GET_OPTIONAL_VAL(conn->preferred_prefix, "preferred-prefix");
796  GET_OPTIONAL_VAL(conn->unified_prefix, "unified-prefix");
797  GET_OPTIONAL_REGEX(conn->allowed_smsc_id_regex, "allowed-smsc-id-regex");
798  GET_OPTIONAL_REGEX(conn->denied_smsc_id_regex, "denied-smsc-id-regex");
799  GET_OPTIONAL_REGEX(conn->preferred_smsc_id_regex, "preferred-smsc-id-regex");
800  GET_OPTIONAL_REGEX(conn->allowed_prefix_regex, "allowed-prefix-regex");
801  GET_OPTIONAL_REGEX(conn->denied_prefix_regex, "denied-prefix-regex");
802  GET_OPTIONAL_REGEX(conn->preferred_prefix_regex, "preferred-prefix-regex");
803 
807  init_reroute(conn, grp);
808 
809  /*
810  * Since we applied changes, we need to re-compute
811  * at least the overall checksum. The other one
812  * is not changed as the routing values are excluded.
813  * also the checksum hash values.
814  */
815  octstr_destroy(conn->chksum);
816  conn->chksum = cfg_get_group_checksum(grp, NULL);
817 }
Load * incoming_sms_load
Definition: smscconn_p.h:216
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
Octstr * name
Definition: smscconn_p.h:173
long online
Definition: smscconn.h:122
void error(int err, const char *fmt,...)
Definition: log.c:648
int smsc_oisd_create(SMSCConn *conn, CfgGroup *grp)
Definition: smsc_oisd.c:1498
void info(int err, const char *fmt,...)
Definition: log.c:672
#define MAX_SMS_OCTETS
Definition: sms.h:129
regex_t * re
Definition: smscconn_p.h:255
Msg * msg_duplicate(Msg *msg)
Definition: msg.c:111
int smsc_cgw_create(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_cgw.c:431
regex_t * allowed_smsc_id_regex
Definition: smscconn_p.h:180
unsigned long received_dlr
Definition: smscconn.h:117
void * gwlist_search(List *list, void *pattern, int(*cmp)(void *, void *))
Definition: list.c:486
long max_sms_octets
Definition: smscconn_p.h:213
void bb_smscconn_ready(SMSCConn *conn)
Definition: bb_smscconn.c:185
int smscconn_destroy(SMSCConn *conn)
Definition: smscconn.c:464
int smsc_cimd2_create(SMSCConn *conn, CfgGroup *grp)
Definition: smsc_cimd2.c:2321
gw_assert(wtls_machine->packet_to_send !=NULL)
void counter_destroy(Counter *counter)
Definition: counter.c:110
#define mutex_unlock(m)
Definition: thread.h:136
void gwlist_append(List *list, void *item)
Definition: list.c:179
int alt_dcs
Definition: smscconn_p.h:201
int dead_start
Definition: smscconn_p.h:211
void smscconn_shutdown(SMSCConn *conn, int finish_sending)
Definition: smscconn.c:427
List * reroute_by_receiver_regex
Definition: smscconn_p.h:208
Octstr * id
Definition: smscconn_p.h:174
long gwlist_len(List *list)
Definition: list.c:166
Counter * failed
Definition: smscconn_p.h:166
#define mutex_create()
Definition: thread.h:96
void * gwlist_get(List *list, long pos)
Definition: list.c:292
void(* stop_conn)(SMSCConn *conn)
Definition: smscconn_p.h:247
Octstr * denied_prefix
Definition: smscconn_p.h:186
int load_add_interval(Load *load, int interval)
Definition: load.c:111
msg_type
Definition: msg.h:73
Octstr * cfg_get_group_checksum(CfgGroup *grp,...)
Definition: cfg.c:666
int log_idx
Definition: smscconn_p.h:197
int smsc_emi2_create(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_emi.c:1629
#define cfg_get(grp, varname)
Definition: cfg.h:86
const Octstr * smscconn_name(SMSCConn *conn)
Definition: smscconn.c:552
Octstr * allowed_prefix
Definition: smscconn_p.h:184
unsigned long counter_set(Counter *counter, unsigned long n)
Definition: counter.c:167
Octstr * id
Definition: smscconn_p.h:256
static void pattern_route_destroy(void *arg)
Definition: smscconn.c:455
Dict * reroute_by_receiver
Definition: smscconn_p.h:207
smscconn_status_t status
Definition: smscconn.h:113
Octstr * our_host
Definition: smscconn_p.h:192
int smsc_wrapper_create(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_wrapper.c:397
long log_level
Definition: smscconn_p.h:196
List * denied_smsc_id
Definition: smscconn_p.h:178
void octstr_strip_blanks(Octstr *text)
Definition: octstr.c:1346
smscconn_killed_t killed
Definition: smscconn.h:114
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
List * sms_split(Msg *orig, Octstr *header, Octstr *footer, Octstr *nonlast_suffix, Octstr *split_chars, int catenate, unsigned long msg_sequence, int max_messages, int max_octets)
Definition: sms.c:309
void(* start_conn)(SMSCConn *conn)
Definition: smscconn_p.h:246
int smsc_http_create(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_http.c:489
#define OCTSTR(os)
unsigned long counter_increase(Counter *counter)
Definition: counter.c:123
Definition: log.h:74
long reconnect_delay
Definition: smscconn_p.h:199
unsigned int smscconn_instances(CfgGroup *grp)
Definition: smscconn.c:197
int smsc_at2_create(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_at.c:1519
unsigned long sent_dlr
Definition: smscconn.h:119
long octstr_parse_double(double *nump, Octstr *ostr, long pos)
Definition: octstr.c:782
long queued
Definition: smscconn.h:121
regex_t * preferred_prefix_regex
Definition: smscconn_p.h:189
Octstr * log_file
Definition: smscconn_p.h:195
#define SPLIT_OPTIONAL_VAL(x, n)
Load * outgoing_sms_load
Definition: smscconn_p.h:215
const Octstr * smscconn_admin_id(SMSCConn *conn)
Definition: smscconn.c:566
int smsc_smasi_create(SMSCConn *conn, CfgGroup *config)
Definition: smsc_smasi.c:1129
smscconn_killed_t why_killed
Definition: smscconn_p.h:153
regex_t * allowed_prefix_regex
Definition: smscconn_p.h:185
void smscconn_reconfig(SMSCConn *conn, CfgGroup *grp)
Definition: smscconn.c:763
void msg_destroy_item(void *msg)
Definition: msg.c:147
Counter * sent
Definition: smscconn_p.h:164
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Counter * received_dlr
Definition: smscconn_p.h:163
Definition: msg.h:79
int smscconn_status(SMSCConn *conn)
Definition: smscconn.c:724
regex_t * preferred_smsc_id_regex
Definition: smscconn_p.h:182
int does_prefix_match(Octstr *prefix, Octstr *number)
Definition: utils.c:850
Counter * counter_create(void)
Definition: counter.c:94
Octstr * reroute_to_smsc
Definition: smscconn_p.h:209
unsigned long sent
Definition: smscconn.h:118
void * gwlist_extract_first(List *list)
Definition: list.c:305
Octstr * chksum_conn
Definition: smscconn_p.h:172
void grp_dump(CfgGroup *grp)
Definition: cfg.c:811
Octstr * admin_id
Definition: smscconn_p.h:176
List * preferred_smsc_id
Definition: smscconn_p.h:179
Octstr * unified_prefix
Definition: smscconn_p.h:190
unsigned long counter_increase_with(Counter *counter, unsigned long value)
Definition: counter.c:134
Octstr * chksum
Definition: smscconn_p.h:171
int smscconn_stop(SMSCConn *conn)
Definition: smscconn.c:516
unsigned long failed
Definition: smscconn.h:120
int is_stopped
Definition: smscconn.h:115
time_t connect_time
Definition: smscconn_p.h:155
int reroute
Definition: smscconn_p.h:206
const Octstr * smscconn_id(SMSCConn *conn)
Definition: smscconn.c:559
#define octstr_duplicate(ostr)
Definition: octstr.h:187
double throughput
Definition: smscconn_p.h:203
int smsc_loopback_create(SMSCConn *conn, CfgGroup *cfg)
int octstr_item_match(void *item, void *pattern)
Definition: octstr.c:1661
Mutex * flow_mutex
Definition: smscconn_p.h:157
static void init_reroute(SMSCConn *conn, CfgGroup *grp)
Definition: smscconn.c:87
Octstr * preferred_prefix
Definition: smscconn_p.h:188
regex_t * denied_prefix_regex
Definition: smscconn_p.h:187
void warning(int err, const char *fmt,...)
Definition: log.c:660
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
void octstr_destroy_item(void *os)
Definition: octstr.c:336
unsigned long counter_value(Counter *counter)
Definition: counter.c:145
#define load_create()
Definition: load.h:78
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
#define GET_OPTIONAL_VAL(x, n)
int load
Definition: smscconn.h:123
#define SMSCCONN_RECONNECT_DELAY
Definition: smscconn.c:80
volatile sig_atomic_t is_stopped
Definition: smscconn_p.h:169
int smscconn_send(SMSCConn *conn, Msg *msg)
Definition: smscconn.c:655
List * allowed_smsc_id
Definition: smscconn_p.h:177
int smsc_smpp_create(SMSCConn *conn, CfgGroup *grp)
Definition: smsc_smpp.c:2599
Counter * received
Definition: smscconn_p.h:162
int smsc_fake_create(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_fake.c:540
Counter * sent_dlr
Definition: smscconn_p.h:165
int log_open(char *filename, int level, enum excl_state excl)
Definition: log.c:375
void dict_destroy(Dict *dict)
Definition: dict.c:215
int cfg_get_bool(int *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:759
Definition: octstr.c:118
int smscconn_usable(SMSCConn *conn, Msg *msg)
Definition: smscconn.c:573
int(* shutdown)(SMSCConn *conn, int finish_sending)
Definition: smscconn_p.h:230
Load * outgoing_dlr_load
Definition: smscconn_p.h:218
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
int cfg_get_integer(long *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:742
#define panic
Definition: log.h:87
static List * split
Definition: test_http.c:88
Definition: cfg.c:73
void load_destroy(Load *load)
Definition: load.c:145
smscconn_status_t status
Definition: smscconn_p.h:151
Load * incoming_dlr_load
Definition: smscconn_p.h:217
#define gwlist_create()
Definition: list.h:136
int normalize_number(char *dial_prefixes, Octstr **number)
Definition: utils.c:882
long(* queued)(SMSCConn *conn)
Definition: smscconn_p.h:241
SMSCConn * smscconn_create(CfgGroup *grp, int start_as_stopped)
Definition: smscconn.c:210
int(* send_msg)(SMSCConn *conn, Msg *msg)
Definition: smscconn_p.h:236
int dict_put_once(Dict *dict, Octstr *key, void *value)
Definition: dict.c:271
regex_t * denied_smsc_id_regex
Definition: smscconn_p.h:181
int smscconn_info(SMSCConn *conn, StatusInfo *infotable)
Definition: smscconn.c:732
#define mutex_lock(m)
Definition: thread.h:130
unsigned long received
Definition: smscconn.h:116
int reroute_dlr
Definition: smscconn_p.h:210
#define GET_OPTIONAL_REGEX(x, n)
List * octstr_split(const Octstr *os, const Octstr *sep)
Definition: octstr.c:1640
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
void smscconn_start(SMSCConn *conn)
Definition: smscconn.c:536
Counter * split_msg_counter
Definition: bb_smscconn.c:159
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871
int load
Definition: smscconn_p.h:152
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.