Kannel: Open Source WAP and SMS gateway  svn-r5335
kannel.c File Reference
#include "gwlib/gwlib.h"
#include "smscconn.h"
#include "smscconn_p.h"
#include "bb_smscconn_cb.h"
#include "msg.h"
#include "sms.h"
#include "dlr.h"
#include "urltrans.h"
#include "meta_data.h"
#include "../smsc_http_p.h"

Go to the source code of this file.

Enumerations

enum  { HEX_NOT_UPPERCASE = 0 }
 

Functions

static int kannel_send_sms (SMSCConn *conn, Msg *sms)
 
static void kannel_parse_reply (SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
 
static void kannel_receive_sms (SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
 

Variables

struct smsc_http_fn_callbacks smsc_http_kannel_callback
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
HEX_NOT_UPPERCASE 

Definition at line 110 of file kannel.c.

Function Documentation

◆ kannel_parse_reply()

static void kannel_parse_reply ( SMSCConn conn,
Msg msg,
int  status,
List headers,
Octstr body 
)
static

Definition at line 216 of file kannel.c.

References bb_smscconn_send_failed(), bb_smscconn_sent(), dlr_add(), DLR_IS_ENABLED_DEVICE, HTTP_ACCEPTED, HTTP_OK, smscconn::id, msg, octstr_case_compare(), octstr_create, octstr_destroy(), octstr_duplicate, octstr_imm(), octstr_ncompare(), SMSCCONN_FAILED_MALFORMED, UUID_STR_LEN, and uuid_unparse().

218 {
219  /* Test on three cases:
220  * 1. an smsbox reply of an remote kannel instance
221  * 2. an smsc_http response (if used for MT to MO looping)
222  * 3. an smsbox reply of partly successful sendings */
223  if ((status == HTTP_OK || status == HTTP_ACCEPTED)
224  && (octstr_case_compare(body, octstr_imm("0: Accepted for delivery")) == 0 ||
225  octstr_case_compare(body, octstr_imm("Sent.")) == 0 ||
226  octstr_case_compare(body, octstr_imm("Ok.")) == 0 ||
227  octstr_ncompare(body, octstr_imm("Result: OK"),10) == 0)) {
228  char id[UUID_STR_LEN + 1];
229  Octstr *mid;
230 
231  /* create Octstr from UUID */
232  uuid_unparse(msg->sms.id, id);
233  mid = octstr_create(id);
234 
235  /* add to our own DLR storage */
236  if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask))
237  dlr_add(conn->id, mid, msg, 0);
238 
239  octstr_destroy(mid);
240 
241  bb_smscconn_sent(conn, msg, NULL);
242  } else {
245  }
246 }
Octstr * id
Definition: smscconn_p.h:174
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
void dlr_add(const Octstr *smsc, const Octstr *ts, Msg *msg, int use_dst)
Definition: dlr.c:330
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Definition: http.h:142
int octstr_ncompare(const Octstr *ostr1, const Octstr *ostr2, long n)
Definition: octstr.c:952
#define octstr_duplicate(ostr)
Definition: octstr.h:187
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
Definition: octstr.c:903
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
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:329
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ kannel_receive_sms()

static void kannel_receive_sms ( SMSCConn conn,
HTTPClient client,
List headers,
Octstr body,
List cgivars 
)
static

Definition at line 249 of file kannel.c.

References account, bb_smscconn_receive(), charset, client(), coding, smscconn::data, debug(), dlr_find(), error(), from, gwlist_create, HTTP_ACCEPTED, http_cgi_variable(), http_destroy_headers(), http_header_add(), http_send_reply(), smscconn::id, MAX_SMS_OCTETS, meta_data_set_value(), METADATA_DLR_GROUP, METADATA_DLR_GROUP_ERRORCODE, msg, msg_create, octstr_compare(), octstr_create, octstr_destroy(), octstr_duplicate, octstr_get_char(), octstr_get_cstr, octstr_imm(), octstr_len(), conndata::password, report_mo, sms_charset_processing(), SMS_PARAM_UNDEFINED, text, and conndata::username.

251 {
252  ConnData *conndata = conn->data;
253  Octstr *user, *pass, *from, *to, *text, *udh, *account, *binfo, *charset;
254  Octstr *dlrmid, *dlrerr;
255  Octstr *tmp_string, *retmsg;
256  int mclass, mwi, coding, validity, deferred, dlrmask;
257  List *reply_headers;
258  int ret;
259 
260  mclass = mwi = coding = validity =
261  deferred = dlrmask = SMS_PARAM_UNDEFINED;
262 
263  user = http_cgi_variable(cgivars, "username");
264  pass = http_cgi_variable(cgivars, "password");
265  from = http_cgi_variable(cgivars, "from");
266  to = http_cgi_variable(cgivars, "to");
267  text = http_cgi_variable(cgivars, "text");
268  udh = http_cgi_variable(cgivars, "udh");
269  charset = http_cgi_variable(cgivars, "charset");
270  account = http_cgi_variable(cgivars, "account");
271  binfo = http_cgi_variable(cgivars, "binfo");
272  dlrmid = http_cgi_variable(cgivars, "dlr-mid");
273  tmp_string = http_cgi_variable(cgivars, "flash");
274  if (tmp_string) {
275  sscanf(octstr_get_cstr(tmp_string),"%d", &mclass);
276  }
277  tmp_string = http_cgi_variable(cgivars, "mclass");
278  if (tmp_string) {
279  sscanf(octstr_get_cstr(tmp_string),"%d", &mclass);
280  }
281  tmp_string = http_cgi_variable(cgivars, "mwi");
282  if (tmp_string) {
283  sscanf(octstr_get_cstr(tmp_string),"%d", &mwi);
284  }
285  tmp_string = http_cgi_variable(cgivars, "coding");
286  if (tmp_string) {
287  sscanf(octstr_get_cstr(tmp_string),"%d", &coding);
288  }
289  tmp_string = http_cgi_variable(cgivars, "validity");
290  if (tmp_string) {
291  sscanf(octstr_get_cstr(tmp_string),"%d", &validity);
292  }
293  tmp_string = http_cgi_variable(cgivars, "deferred");
294  if (tmp_string) {
295  sscanf(octstr_get_cstr(tmp_string),"%d", &deferred);
296  }
297  tmp_string = http_cgi_variable(cgivars, "dlr-mask");
298  if (tmp_string) {
299  sscanf(octstr_get_cstr(tmp_string),"%d", &dlrmask);
300  }
301  dlrerr = http_cgi_variable(cgivars, "dlr-err");
302 
303  debug("smsc.http.kannel", 0, "HTTP[%s]: Received an HTTP request",
304  octstr_get_cstr(conn->id));
305 
306  if (user == NULL || pass == NULL ||
307  octstr_compare(user, conndata->username) != 0 ||
308  octstr_compare(pass, conndata->password) != 0) {
309 
310  error(0, "HTTP[%s]: Authorization failure",
311  octstr_get_cstr(conn->id));
312  retmsg = octstr_create("Authorization failed for sendsms");
313  } else if (dlrmask != 0 && dlrmid != NULL) {
314  /* we got a DLR, and we don't require additional values */
315  Msg *dlrmsg;
316 
317  dlrmsg = dlr_find(conn->id,
318  dlrmid, /* message id */
319  to, /* destination */
320  dlrmask, 0);
321 
322  if (dlrmsg != NULL) {
323  dlrmsg->sms.sms_type = report_mo;
324  dlrmsg->sms.msgdata = octstr_duplicate(text);
325  dlrmsg->sms.account = octstr_duplicate(conndata->username);
326 
327  debug("smsc.http.kannel", 0, "HTTP[%s]: Received DLR for DLR-URL <%s>",
328  octstr_get_cstr(conn->id), octstr_get_cstr(dlrmsg->sms.dlr_url));
329 
330  if (dlrerr != NULL) {
331  /* pass errorcode as is */
332  if (dlrmsg->sms.meta_data == NULL)
333  dlrmsg->sms.meta_data = octstr_create("");
334 
335  meta_data_set_value(dlrmsg->sms.meta_data, METADATA_DLR_GROUP,
337  }
338 
339  ret = bb_smscconn_receive(conn, dlrmsg);
340  if (ret == -1)
341  retmsg = octstr_create("Not accepted");
342  else
343  retmsg = octstr_create("Sent.");
344  } else {
345  error(0,"HTTP[%s]: Got DLR but could not find message or was not interested "
346  "in it id<%s> dst<%s>, type<%d>",
347  octstr_get_cstr(conn->id), octstr_get_cstr(dlrmid),
348  octstr_get_cstr(to), dlrmask);
349  retmsg = octstr_create("Unknown DLR, not accepted");
350  }
351  }
352  else if (from == NULL || to == NULL || text == NULL) {
353 
354  error(0, "HTTP[%s]: Insufficient args",
355  octstr_get_cstr(conn->id));
356  retmsg = octstr_create("Insufficient args, rejected");
357  }
358  else if (udh != NULL && (octstr_len(udh) != octstr_get_char(udh, 0) + 1)) {
359  error(0, "HTTP[%s]: UDH field misformed, rejected",
360  octstr_get_cstr(conn->id));
361  retmsg = octstr_create("UDH field misformed, rejected");
362  }
363  else if (udh != NULL && octstr_len(udh) > MAX_SMS_OCTETS) {
364  error(0, "HTTP[%s]: UDH field is too long, rejected",
365  octstr_get_cstr(conn->id));
366  retmsg = octstr_create("UDH field is too long, rejected");
367  }
368  else {
369  /* we got a normal MO SMS */
370  Msg *msg;
371  msg = msg_create(sms);
372 
373  debug("smsc.http.kannel", 0, "HTTP[%s]: Constructing new SMS",
374  octstr_get_cstr(conn->id));
375 
376  msg->sms.service = octstr_duplicate(user);
377  msg->sms.sender = octstr_duplicate(from);
378  msg->sms.receiver = octstr_duplicate(to);
379  msg->sms.msgdata = octstr_duplicate(text);
380  msg->sms.udhdata = octstr_duplicate(udh);
381 
382  msg->sms.smsc_id = octstr_duplicate(conn->id);
383  msg->sms.time = time(NULL);
384  msg->sms.mclass = mclass;
385  msg->sms.mwi = mwi;
386  msg->sms.coding = coding;
387  msg->sms.validity = (validity == SMS_PARAM_UNDEFINED ? validity : time(NULL) + validity * 60);
388  msg->sms.deferred = (deferred == SMS_PARAM_UNDEFINED ? deferred : time(NULL) + deferred * 60);
389  msg->sms.account = octstr_duplicate(account);
390  msg->sms.binfo = octstr_duplicate(binfo);
391 
392  /* re-encode content if necessary */
393  if (sms_charset_processing(charset, msg->sms.msgdata, msg->sms.coding) == -1) {
394  error(0, "HTTP[%s]: Charset or body misformed, rejected",
395  octstr_get_cstr(conn->id));
396  retmsg = octstr_create("Charset or body misformed, rejected");
397  }
398  else {
399 
400  ret = bb_smscconn_receive(conn, msg);
401  if (ret == -1)
402  retmsg = octstr_create("Not accepted");
403  else
404  retmsg = octstr_create("Sent.");
405  }
406  }
407 
408  reply_headers = gwlist_create();
409  http_header_add(reply_headers, "Content-Type", "text/plain");
410  debug("smsc.http.kannel", 0, "HTTP[%s]: Sending reply",
411  octstr_get_cstr(conn->id));
412  http_send_reply(client, HTTP_ACCEPTED, reply_headers, retmsg);
413 
414  octstr_destroy(retmsg);
415  http_destroy_headers(reply_headers);
416 }
Octstr * to
Definition: generic.c:110
void error(int err, const char *fmt,...)
Definition: log.c:648
#define MAX_SMS_OCTETS
Definition: sms.h:129
static int coding
Definition: mtbatch.c:102
Octstr * validity
Definition: generic.c:125
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2886
Definition: msg.h:109
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:250
static void client(int port)
Definition: test_udp.c:77
#define METADATA_DLR_GROUP_ERRORCODE
Definition: meta_data.h:71
int meta_data_set_value(Octstr *data, const char *group, const Octstr *key, const Octstr *value, int replace)
Definition: meta_data.c:324
Octstr * deferred
Definition: generic.c:126
#define msg_create(type)
Definition: msg.h:136
Msg * dlr_find(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int typ, int use_dst)
Definition: dlr.c:387
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * charset
Definition: test_ota.c:68
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2836
void http_destroy_headers(List *headers)
Definition: http.c:2879
static Octstr * from
Definition: mtbatch.c:95
Octstr * binfo
Definition: generic.c:115
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2695
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Definition: msg.h:79
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:477
char * text
Definition: smsc_cimd2.c:921
#define METADATA_DLR_GROUP
Definition: meta_data.h:68
#define octstr_duplicate(ostr)
Definition: octstr.h:187
Octstr * mclass
Definition: generic.c:122
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
int sms_charset_processing(Octstr *charset, Octstr *body, int coding)
Definition: sms.c:419
#define octstr_create(cstr)
Definition: octstr.h:125
#define SMS_PARAM_UNDEFINED
Definition: sms.h:91
Octstr * udh
Definition: generic.c:112
Octstr * mwi
Definition: generic.c:123
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
Octstr * password
Definition: smsc_http_p.h:98
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
#define gwlist_create()
Definition: list.h:136
Octstr * username
Definition: smsc_http_p.h:97
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406
Definition: list.c:102
static Octstr * account
Definition: mtbatch.c:94
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871

◆ kannel_send_sms()

static int kannel_send_sms ( SMSCConn conn,
Msg sms 
)
static

Definition at line 113 of file kannel.c.

References smscconn::data, DC_7BIT, DC_UCS2, DC_UNDEF, debug(), DLR_NOTHING, DLR_UNDEFINED, conndata::dlr_url, gwlist_create, HEX_NOT_UPPERCASE, http_destroy_headers(), HTTP_METHOD_GET, conndata::http_ref, http_start_request(), smscconn::id, MC_UNDEF, MWI_UNDEF, conndata::no_coding, conndata::no_sender, conndata::no_sep, octstr_append_cstr(), octstr_binary_to_hex(), octstr_create, octstr_destroy(), octstr_duplicate, octstr_format(), octstr_format_append(), octstr_get_cstr, octstr_imm(), octstr_len(), conndata::password, conndata::send_url, SMS_PARAM_UNDEFINED, url, conndata::username, UUID_STR_LEN, and uuid_unparse().

114 {
115  ConnData *conndata = conn->data;
116  Octstr *url;
117  List *headers;
118 
119  if (!conndata->no_sep) {
120  url = octstr_format("%S?"
121  "username=%E&password=%E&to=%E&text=%E",
124  sms->sms.receiver, sms->sms.msgdata);
125  } else {
126  Octstr *msgdata = octstr_duplicate(sms->sms.msgdata);
127 
129  url = octstr_format("%S?"
130  "username=%E&password=%E&to=%E&text=%S",
133  sms->sms.receiver, msgdata);
134  octstr_destroy(msgdata);
135  }
136 
137  if (octstr_len(sms->sms.udhdata)) {
138  if (!conndata->no_sep) {
139  octstr_format_append(url, "&udh=%E", sms->sms.udhdata);
140  } else {
141  Octstr *udhdata = octstr_duplicate(sms->sms.udhdata);
142 
144  octstr_format_append(url, "&udh=%S", udhdata);
145  octstr_destroy(udhdata);
146  }
147  }
148 
149  if (!conndata->no_sender)
150  octstr_format_append(url, "&from=%E", sms->sms.sender);
151  if (sms->sms.mclass != MC_UNDEF)
152  octstr_format_append(url, "&mclass=%d", sms->sms.mclass);
153  if (!conndata->no_coding && sms->sms.coding != DC_UNDEF)
154  octstr_format_append(url, "&coding=%d", sms->sms.coding);
155 
156  /* Obey that smsbox's sendsms HTTP interface is still expecting
157  * WINDOWS-1252 as default charset, while all other internal parts
158  * use UTF-8 as internal encoding. This means, when we pass a SMS
159  * into a next Kannel instance, we need to let the smsbox know which
160  * charset we have in use.
161  * XXX TODO: change smsbox interface to use UTF-8 as default
162  * in next major release. */
163  if (sms->sms.coding == DC_7BIT)
164  octstr_append_cstr(url, "&charset=UTF-8");
165  else if (sms->sms.coding == DC_UCS2)
166  octstr_append_cstr(url, "&charset=UTF-16BE");
167 
168  if (sms->sms.mwi != MWI_UNDEF)
169  octstr_format_append(url, "&mwi=%d", sms->sms.mwi);
170  if (sms->sms.account) /* prepend account with local username */
171  octstr_format_append(url, "&account=%E:%E", sms->sms.service, sms->sms.account);
172  if (sms->sms.binfo) /* prepend billing info */
173  octstr_format_append(url, "&binfo=%S", sms->sms.binfo);
174  if (sms->sms.smsc_id) /* proxy the smsc-id to the next instance */
175  octstr_format_append(url, "&smsc=%S", sms->sms.smsc_id);
176  if (conndata->dlr_url) {
177  char id[UUID_STR_LEN + 1];
178  Octstr *mid;
179 
180  /* create Octstr from UUID */
181  uuid_unparse(sms->sms.id, id);
182  mid = octstr_create(id);
183 
184  octstr_format_append(url, "&dlr-url=%E", conndata->dlr_url);
185 
186  /* encapsulate the original DLR-URL, escape code for DLR mask
187  * and message id */
188  octstr_format_append(url, "%E%E%E%E%E",
189  octstr_imm("&dlr-url="), sms->sms.dlr_url != NULL ? sms->sms.dlr_url : octstr_imm(""),
190  octstr_imm("&dlr-mask=%d"),
191  octstr_imm("&dlr-mid="), mid);
192 
193  octstr_destroy(mid);
194  } else if (sms->sms.dlr_url != NULL)
195  octstr_format_append(url, "&dlr-url=%E", sms->sms.dlr_url);
196  if (sms->sms.dlr_mask != DLR_UNDEFINED && sms->sms.dlr_mask != DLR_NOTHING)
197  octstr_format_append(url, "&dlr-mask=%d", sms->sms.dlr_mask);
198 
199  if (sms->sms.validity != SMS_PARAM_UNDEFINED)
200  octstr_format_append(url, "&validity=%ld", (sms->sms.validity - time(NULL)) / 60);
201  if (sms->sms.deferred != SMS_PARAM_UNDEFINED)
202  octstr_format_append(url, "&deferred=%ld", (sms->sms.deferred - time(NULL)) / 60);
203 
204  headers = gwlist_create();
205  debug("smsc.http.kannel", 0, "HTTP[%s]: Start request",
206  octstr_get_cstr(conn->id));
208  NULL, 0, sms, NULL);
209 
211  http_destroy_headers(headers);
212 
213  return 0;
214 }
#define MWI_UNDEF
Definition: sms.h:99
Octstr * dlr_url
Definition: smsc_http_p.h:94
int no_sep
Definition: smsc_http_p.h:101
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:250
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
#define DLR_NOTHING
Definition: dlr.h:71
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1511
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void octstr_binary_to_hex(Octstr *ostr, int uppercase)
Definition: octstr.c:465
void http_destroy_headers(List *headers)
Definition: http.c:2879
void http_start_request(HTTPCaller *caller, int method, Octstr *url, List *headers, Octstr *body, int follow, void *id, Octstr *certkeyfile)
Definition: http.c:1760
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
int no_coding
Definition: smsc_http_p.h:100
#define octstr_duplicate(ostr)
Definition: octstr.h:187
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 SMS_PARAM_UNDEFINED
Definition: sms.h:91
int no_sender
Definition: smsc_http_p.h:99
#define UUID_STR_LEN
Definition: gw_uuid.h:19
#define DLR_UNDEFINED
Definition: dlr.h:70
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
#define MC_UNDEF
Definition: sms.h:93
Definition: octstr.c:118
Octstr * password
Definition: smsc_http_p.h:98
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2507
#define gwlist_create()
Definition: list.h:136
Octstr * username
Definition: smsc_http_p.h:97
#define DC_UNDEF
Definition: sms.h:109
Octstr * send_url
Definition: smsc_http_p.h:93
static Octstr * url
Definition: test_xmlrpc.c:84
Definition: list.c:102
#define DC_UCS2
Definition: sms.h:112
#define DC_7BIT
Definition: sms.h:110

Variable Documentation

◆ smsc_http_kannel_callback

struct smsc_http_fn_callbacks smsc_http_kannel_callback
Initial value:
= {
.send_sms = kannel_send_sms,
.parse_reply = kannel_parse_reply,
.receive_sms = kannel_receive_sms,
}
static void kannel_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: kannel.c:216
static void kannel_receive_sms(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: kannel.c:249
static int kannel_send_sms(SMSCConn *conn, Msg *sms)
Definition: kannel.c:113

Definition at line 418 of file kannel.c.

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.