Kannel: Open Source WAP and SMS gateway  svn-r5262
brunet.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  * Brunet - A german aggregator (mainly doing T-Mobil D1 connections)
59  *
60  * o bruHTT v1.3L (for MO traffic)
61  * o bruHTP v2.1 (date 22.04.2003) (for MT traffic)
62  *
63  * Stipe Tolj <stolj@wapme.de>
64  * Tobias Weber <weber@wapme.de>
65  */
66 
67 #include "gwlib/gwlib.h"
68 #include "smscconn.h"
69 #include "smscconn_p.h"
70 #include "bb_smscconn_cb.h"
71 #include "msg.h"
72 #include "sms.h"
73 #include "dlr.h"
74 #include "urltrans.h"
75 #include "meta_data.h"
76 
77 #include "../smsc_http_p.h"
78 
79 
80 /* MT related function */
81 static int brunet_send_sms(SMSCConn *conn, Msg *sms)
82 {
83  ConnData *conndata = conn->data;
84  Octstr *url, *tid, *xser;
85  List *headers;
86  char id[UUID_STR_LEN + 1];
87  int dcs;
88 
89  /*
90  * Construct TransactionId.
91  * Beware that brunet needs an "clean" octstr representation,
92  * without the dashes in the string. So remove them.
93  */
94  uuid_unparse(sms->sms.id, id);
95  tid = octstr_create(id);
96  octstr_replace(tid, octstr_imm("-"), octstr_imm(""));
97 
98  /* form the basic URL */
99  url = octstr_format("%S?MsIsdn=%E&Originator=%E",
100  conndata->send_url, sms->sms.receiver, sms->sms.sender);
101 
102  /*
103  * We use &binfo=<foobar> from sendsms interface to encode
104  * additional paramters. If a mandatory value is not set,
105  * a default value is applied
106  */
107  if (octstr_len(sms->sms.binfo)) {
108  octstr_url_decode(sms->sms.binfo);
109  octstr_format_append(url, "&%S", sms->sms.binfo);
110  }
111  /* CustomerId */
112  if (octstr_search(url, octstr_create("CustomerId="), 0) == -1) {
113  octstr_format_append(url, "&CustomerId=%S", conndata->username);
114  }
115  /* TransactionId */
116  if (octstr_search(url, octstr_create("TransactionId="), 0) == -1) {
117  octstr_format_append(url, "&TransactionId=%S", tid);
118  }
119  /* SMSCount */
120  if (octstr_search(url, octstr_create("SMSCount="), 0) == -1) {
121  octstr_format_append(url, "&%s", "SMSCount=1");
122  }
123  /* ActionType */
124  if (octstr_search(url, octstr_create("ActionType="), 0) == -1) {
125  octstr_format_append(url, "&%s", "ActionType=A");
126  }
127  /* ServiceDeliveryType */
128  if (octstr_search(url, octstr_create("ServiceDeliveryType="), 0) == -1) {
129  octstr_format_append(url, "&%s", "ServiceDeliveryType=P");
130  }
131 
132  /* if coding is not set and UDH exists, assume DC_8BIT
133  * else default to DC_7BIT */
134  if (sms->sms.coding == DC_UNDEF)
135  sms->sms.coding = octstr_len(sms->sms.udhdata) > 0 ? DC_8BIT : DC_7BIT;
136 
137  if (sms->sms.coding == DC_8BIT)
138  octstr_format_append(url, "&MessageType=B&Text=%H", sms->sms.msgdata);
139  else
140  octstr_format_append(url, "&MessageType=S&Text=%E", sms->sms.msgdata);
141 
142  dcs = fields_to_dcs(sms,
143  (sms->sms.alt_dcs != SMS_PARAM_UNDEFINED ? sms->sms.alt_dcs : 0));
144 
145  /* XSer processing */
146  xser = octstr_create("");
147  /* XSer DCS values */
148  if (dcs != 0 && dcs != 4)
149  octstr_format_append(xser, "0201%02x", dcs & 0xff);
150  /* add UDH header */
151  if (octstr_len(sms->sms.udhdata)) {
152  octstr_format_append(xser, "01%02x%H", octstr_len(sms->sms.udhdata),
153  sms->sms.udhdata);
154  }
155  if (octstr_len(xser) > 0)
156  octstr_format_append(url, "&XSer=%S", xser);
157  octstr_destroy(xser);
158 
159 
160  headers = http_create_empty_headers();
161  debug("smsc.http.brunet", 0, "HTTP[%s]: Sending request <%s>",
162  octstr_get_cstr(conn->id), octstr_get_cstr(url));
163 
164  /*
165  * Brunet requires an SSL-enabled HTTP client call, this is handled
166  * transparently by the Kannel HTTP layer module.
167  */
168  http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers,
169  NULL, 0, sms, NULL);
170 
171  octstr_destroy(url);
172  octstr_destroy(tid);
173  http_destroy_headers(headers);
174 
175  return 0;
176 }
177 
178 
179 /*
180  * Parse a line in the format: <name=value name=value ...>
181  * and return a Dict with the name as key and the value as value,
182  * otherwise return NULL if a parsing error occures.
183  */
185 {
186  Dict *param = NULL;
187  List *words = NULL;
188  long len;
189  Octstr *word;
190 
191  words = octstr_split_words(body);
192  if ((len = gwlist_len(words)) > 0) {
193  param = dict_create(4, (void(*)(void *)) octstr_destroy);
194  while ((word = gwlist_extract_first(words)) != NULL) {
195  List *l = octstr_split(word, octstr_imm("="));
196  Octstr *key = gwlist_extract_first(l);
197  Octstr *value = gwlist_extract_first(l);
198  if (octstr_len(key))
199  dict_put(param, key, value);
200  octstr_destroy(key);
201  octstr_destroy(word);
202  gwlist_destroy(l, (void(*)(void *)) octstr_destroy);
203  }
204  }
205  gwlist_destroy(words, (void(*)(void *)) octstr_destroy);
206 
207  return param;
208 }
209 
210 
211 static void brunet_parse_reply(SMSCConn *conn, Msg *msg, int status,
212  List *headers, Octstr *body)
213 {
214  if (status == HTTP_OK || status == HTTP_ACCEPTED) {
215  Dict *param;
216  Octstr *status;
217 
218  if ((param = brunet_parse_body(body)) != NULL &&
219  (status = dict_get(param, octstr_imm("Status"))) != NULL &&
220  octstr_case_compare(status, octstr_imm("0")) == 0) {
221  Octstr *msg_id;
222 
223  /* pass the MessageId for this MT to the logging facility */
224  if ((msg_id = dict_get(param, octstr_imm("MessageId"))) != NULL)
225  msg->sms.binfo = octstr_duplicate(msg_id);
226 
227  bb_smscconn_sent(conn, msg, NULL);
228 
229  } else {
230  error(0, "HTTP[%s]: Message was malformed. SMSC response `%s'.",
231  octstr_get_cstr(conn->id), octstr_get_cstr(body));
232  bb_smscconn_send_failed(conn, msg,
234  }
235  dict_destroy(param);
236 
237  } else {
238  error(0, "HTTP[%s]: Message was rejected. SMSC response `%s'.",
239  octstr_get_cstr(conn->id), octstr_get_cstr(body));
240  bb_smscconn_send_failed(conn, msg,
242  }
243 }
244 
245 /* MO related function */
247  List *headers, Octstr *body, List *cgivars)
248 {
249  ConnData *conndata = conn->data;
250  Octstr *user, *from, *to, *text, *udh;
251  Octstr *retmsg;
252  int mclass, mwi, coding, validity, deferred;
253  List *reply_headers;
254  int ret;
255 
256  mclass = mwi = coding = validity = deferred = 0;
257 
258  user = http_cgi_variable(cgivars, "CustomerId");
259  from = http_cgi_variable(cgivars, "MsIsdn");
260  to = http_cgi_variable(cgivars, "Recipient");
261  text = http_cgi_variable(cgivars, "SMMO");
262  udh = http_cgi_variable(cgivars, "XSer");
263 
264  debug("smsc.http.brunet", 0, "HTTP[%s]: Received a request",
265  octstr_get_cstr(conn->id));
266 
267  if (user == NULL || octstr_compare(user, conndata->username) != 0) {
268  error(0, "HTTP[%s]: Authorization failure. CustomerId was <%s>.",
269  octstr_get_cstr(conn->id), octstr_get_cstr(user));
270  retmsg = octstr_create("Authorization failed for MO submission.");
271  }
272  else if (from == NULL || to == NULL || text == NULL) {
273  error(0, "HTTP[%s]: Insufficient args.",
274  octstr_get_cstr(conn->id));
275  retmsg = octstr_create("Insufficient arguments, rejected.");
276  }
277  else {
278  Msg *msg;
279  msg = msg_create(sms);
280 
281  debug("smsc.http.brunet", 0, "HTTP[%s]: Received new MO SMS.",
282  octstr_get_cstr(conn->id));
283 
284  msg->sms.sender = octstr_duplicate(from);
285  msg->sms.receiver = octstr_duplicate(to);
286  msg->sms.msgdata = octstr_duplicate(text);
287  msg->sms.udhdata = octstr_duplicate(udh);
288 
289  msg->sms.smsc_id = octstr_duplicate(conn->id);
290  msg->sms.time = time(NULL); /* XXX maybe extract from DateReceived */
291  msg->sms.mclass = mclass;
292  msg->sms.mwi = mwi;
293  msg->sms.coding = coding;
294  msg->sms.validity = time(NULL) + validity * 60;
295  msg->sms.deferred = time(NULL) + deferred * 60;
296 
297  ret = bb_smscconn_receive(conn, msg);
298  if (ret == -1)
299  retmsg = octstr_create("Status=1");
300  else
301  retmsg = octstr_create("Status=0");
302  }
303 
304  reply_headers = gwlist_create();
305  http_header_add(reply_headers, "Content-Type", "text/plain");
306  debug("smsc.http.brunet", 0, "HTTP[%s]: Sending reply `%s'.",
307  octstr_get_cstr(conn->id), octstr_get_cstr(retmsg));
308  http_send_reply(client, HTTP_OK, reply_headers, retmsg);
309 
310  octstr_destroy(retmsg);
311  http_destroy_headers(reply_headers);
312 }
313 
314 static int brunet_init(SMSCConn *conn, CfgGroup *cfg)
315 {
316  ConnData *conndata = conn->data;
317 
318  if (conndata->username == NULL) {
319  error(0, "HTTP[%s]: 'username' (=CustomerId) required for Brunet http smsc",
320  octstr_get_cstr(conn->id));
321  return -1;
322  }
323 
324  return 0;
325 }
326 
328  .init = brunet_init,
329  .send_sms = brunet_send_sms,
330  .parse_reply = brunet_parse_reply,
331  .receive_sms = brunet_receive_sms,
332 };
333 
334 
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
void error(int err, const char *fmt,...)
Definition: log.c:648
static void brunet_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: brunet.c:211
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
Definition: octstr.c:2647
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
void dict_put(Dict *dict, Octstr *key, void *value)
Definition: dict.c:240
Octstr * id
Definition: smscconn_p.h:174
long gwlist_len(List *list)
Definition: list.c:166
void * data
Definition: smscconn_p.h:249
static void client(int port)
Definition: test_udp.c:77
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
int octstr_url_decode(Octstr *ostr)
Definition: octstr.c:1744
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1068
#define DC_8BIT
Definition: sms.h:111
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
#define msg_create(type)
Definition: msg.h:136
static Dict * brunet_parse_body(Octstr *body)
Definition: brunet.c:184
static Cfg * cfg
Definition: opensmppbox.c:95
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
void http_destroy_headers(List *headers)
Definition: http.c:2856
struct smsc_http_fn_callbacks smsc_http_brunet_callback
Definition: brunet.c:327
static Octstr * from
Definition: mtbatch.c:95
void http_start_request(HTTPCaller *caller, int method, Octstr *url, List *headers, Octstr *body, int follow, void *id, Octstr *certkeyfile)
Definition: http.c:1745
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2672
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
Definition: http.h:142
Definition: msg.h:79
void * gwlist_extract_first(List *list)
Definition: list.c:305
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:478
char * text
Definition: smsc_cimd2.c:921
List * http_create_empty_headers(void)
Definition: http.c:2849
static void brunet_receive_sms(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: brunet.c:246
Definition: dict.c:116
#define octstr_duplicate(ostr)
Definition: octstr.h:187
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
Definition: octstr.c:901
List * octstr_split_words(const Octstr *ostr)
Definition: octstr.c:1600
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
int fields_to_dcs(Msg *msg, int mode)
Definition: sms.c:73
static int brunet_init(SMSCConn *conn, CfgGroup *cfg)
Definition: brunet.c:314
#define SMS_PARAM_UNDEFINED
Definition: sms.h:91
#define UUID_STR_LEN
Definition: gw_uuid.h:19
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
void dict_destroy(Dict *dict)
Definition: dict.c:215
int(* init)(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_http_p.h:74
Definition: octstr.c:118
void bb_smscconn_sent(SMSCConn *conn, Msg *sms, Octstr *reply)
Definition: bb_smscconn.c:279
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
Definition: cfg.c:73
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2505
#define gwlist_create()
Definition: list.h:136
static int brunet_send_sms(SMSCConn *conn, Msg *sms)
Definition: brunet.c:81
Octstr * username
Definition: smsc_http_p.h:97
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:328
#define DC_UNDEF
Definition: sms.h:109
Octstr * send_url
Definition: smsc_http_p.h:93
static Octstr * url
Definition: test_xmlrpc.c:84
List * octstr_split(const Octstr *os, const Octstr *sep)
Definition: octstr.c:1638
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
#define DC_7BIT
Definition: sms.h:110
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
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.