Kannel: Open Source WAP and SMS gateway  svn-r5335
smsc_loopback.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_loopback.c - loopback SMSC interface
59  *
60  * This SMSC type is the MT wise counterpart of the 'reroute' functionality
61  * of the smsc group when MOs are re-routed as MT. This SMSC type re-routes
62  * therefore MTs as MOs back again.
63  *
64  * Stipe Tolj <stolj at kannel dot org>
65  */
66 
67 #include "gwlib/gwlib.h"
68 #include "smscconn.h"
69 #include "smscconn_p.h"
70 #include "bb_smscconn_cb.h"
71 #include "dlr.h"
72 
73 #define O_DESTROY(a) { octstr_destroy(a); a = NULL; }
74 
75 
76 static int msg_cb(SMSCConn *conn, Msg *msg)
77 {
78  Msg *sms;
79 
80  /* create duplicates first */
81  sms = msg_duplicate(msg);
82 
83  /* store temporary DLR data for SMSC ACK */
84  if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask) && !uuid_is_null(sms->sms.id)) {
85  Octstr *mid;
86  char id[UUID_STR_LEN + 1];
87 
88  uuid_unparse(sms->sms.id, id);
89  mid = octstr_create(id);
90 
91  dlr_add(conn->id, mid, sms, 0);
92 
93  octstr_destroy(mid);
94  }
95 
96  /*
97  * Inform abstraction layer of sent event,
98  * it will also take care of DLR SMSC events.
99  */
100  bb_smscconn_sent(conn, sms, NULL);
101 
102  /* now change msg type to reflect flow type */
103  sms = msg_duplicate(msg);
104  sms->sms.sms_type = mo;
105 
106  /* Since this is a new MO now, make sure that the
107  * values we have still from the MT are cleaned. */
108  uuid_clear(sms->sms.id);
109  uuid_generate(sms->sms.id);
110  O_DESTROY(sms->sms.boxc_id);
111  sms->sms.dlr_mask = MSG_PARAM_UNDEFINED;
112  O_DESTROY(sms->sms.dlr_url);
113 
114  /*
115  * If there is a reroute-smsc-id in the config group,
116  * then let's use that value, otherwise assign the
117  * smsc-id canonical name for the MO.
118  * This re-assignment of the smsc-id provides some
119  * MO routing capabilities, i.e. via smsbox-route group.
120  */
121  octstr_destroy(sms->sms.smsc_id);
122 
123  if (conn->reroute_to_smsc) {
124  sms->sms.smsc_id = octstr_duplicate(conn->reroute_to_smsc);
125  } else {
126  sms->sms.smsc_id = octstr_duplicate(conn->id);
127  }
128 
129  /* now pass back again as MO to the abstraction layer */
130  bb_smscconn_receive(conn, sms);
131 
132  return 0;
133 }
134 
135 
136 static int shutdown_cb(SMSCConn *conn, int finish_sending)
137 {
138  debug("smsc.loopback", 0, "Shutting down SMSCConn %s",
139  octstr_get_cstr(conn->name));
140 
142  conn->status = SMSCCONN_DEAD;
144 
145  return 0;
146 }
147 
148 
149 static void start_cb(SMSCConn *conn)
150 {
151  conn->status = SMSCCONN_ACTIVE;
152  conn->connect_time = time(NULL);
153 }
154 
155 
156 static long queued_cb(SMSCConn *conn)
157 {
158  long ret = 0;
159 
160  conn->load = ret;
161  return ret;
162 }
163 
164 
166 {
167  conn->data = NULL;
168  conn->name = octstr_format("LOOPBACK:%S", conn->id);
169 
170  conn->status = SMSCCONN_CONNECTING;
171  conn->connect_time = time(NULL);
172 
173  conn->shutdown = shutdown_cb;
174  conn->queued = queued_cb;
175  conn->start_conn = start_cb;
176  conn->send_msg = msg_cb;
177 
178  return 0;
179 }
Octstr * name
Definition: smscconn_p.h:173
Msg * msg_duplicate(Msg *msg)
Definition: msg.c:111
static int msg_cb(SMSCConn *conn, Msg *msg)
Definition: smsc_loopback.c:76
static int shutdown_cb(SMSCConn *conn, int finish_sending)
Definition: msg.h:106
void bb_smscconn_killed(void)
Definition: bb_smscconn.c:199
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:250
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
void uuid_generate(uuid_t out)
Definition: gw_uuid.c:393
static Cfg * cfg
Definition: opensmppbox.c:95
static void start_cb(SMSCConn *conn)
void dlr_add(const Octstr *smsc, const Octstr *ts, Msg *msg, int use_dst)
Definition: dlr.c:330
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void(* start_conn)(SMSCConn *conn)
Definition: smscconn_p.h:246
smscconn_killed_t why_killed
Definition: smscconn_p.h:153
#define O_DESTROY(a)
Definition: smsc_loopback.c:73
Definition: msg.h:79
Octstr * reroute_to_smsc
Definition: smscconn_p.h:209
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:477
int uuid_is_null(const uuid_t uu)
Definition: gw_uuid.c:413
time_t connect_time
Definition: smscconn_p.h:155
#define octstr_duplicate(ostr)
Definition: octstr.h:187
int smsc_loopback_create(SMSCConn *conn, CfgGroup *cfg)
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
#define UUID_STR_LEN
Definition: gw_uuid.h:19
Definition: octstr.c:118
void bb_smscconn_sent(SMSCConn *conn, Msg *sms, Octstr *reply)
Definition: bb_smscconn.c:281
int(* shutdown)(SMSCConn *conn, int finish_sending)
Definition: smscconn_p.h:230
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
Definition: cfg.c:73
#define MSG_PARAM_UNDEFINED
Definition: msg.h:71
smscconn_status_t status
Definition: smscconn_p.h:151
long(* queued)(SMSCConn *conn)
Definition: smscconn_p.h:241
int(* send_msg)(SMSCConn *conn, Msg *msg)
Definition: smscconn_p.h:236
static long queued_cb(SMSCConn *conn)
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
void uuid_clear(uuid_t uu)
Definition: gw_uuid.c:105
int load
Definition: smscconn_p.h:152
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.