Kannel: Open Source WAP and SMS gateway  svn-r5335
brunet.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.

Functions

static int brunet_send_sms (SMSCConn *conn, Msg *sms)
 
static Dictbrunet_parse_body (Octstr *body)
 
static void brunet_parse_reply (SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
 
static void brunet_receive_sms (SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
 
static int brunet_init (SMSCConn *conn, CfgGroup *cfg)
 

Variables

struct smsc_http_fn_callbacks smsc_http_brunet_callback
 

Function Documentation

◆ brunet_init()

static int brunet_init ( SMSCConn conn,
CfgGroup cfg 
)
static

Definition at line 314 of file brunet.c.

References smscconn::data, error(), smscconn::id, octstr_get_cstr, and conndata::username.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:648
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:250
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * username
Definition: smsc_http_p.h:97

◆ brunet_parse_body()

static Dict* brunet_parse_body ( Octstr body)
static

Definition at line 184 of file brunet.c.

References dict_create(), dict_put(), gwlist_destroy(), gwlist_extract_first(), gwlist_len(), octstr_destroy(), octstr_imm(), octstr_len(), octstr_split(), and octstr_split_words().

Referenced by brunet_parse_reply().

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 }
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
void dict_put(Dict *dict, Octstr *key, void *value)
Definition: dict.c:240
long gwlist_len(List *list)
Definition: list.c:166
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
void * gwlist_extract_first(List *list)
Definition: list.c:305
Definition: dict.c:116
List * octstr_split_words(const Octstr *ostr)
Definition: octstr.c:1602
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
List * octstr_split(const Octstr *os, const Octstr *sep)
Definition: octstr.c:1640
Definition: list.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ brunet_parse_reply()

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

Definition at line 211 of file brunet.c.

References bb_smscconn_send_failed(), bb_smscconn_sent(), brunet_parse_body(), dict_destroy(), dict_get(), error(), HTTP_ACCEPTED, HTTP_OK, smscconn::id, msg, octstr_case_compare(), octstr_duplicate, octstr_get_cstr, octstr_imm(), SMSCCONN_FAILED_MALFORMED, and SMSCCONN_FAILED_REJECTED.

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));
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));
242  }
243 }
void error(int err, const char *fmt,...)
Definition: log.c:648
Octstr * id
Definition: smscconn_p.h:174
static Dict * brunet_parse_body(Octstr *body)
Definition: brunet.c:184
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Definition: http.h:142
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
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:903
void dict_destroy(Dict *dict)
Definition: dict.c:215
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
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ brunet_receive_sms()

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

Definition at line 246 of file brunet.c.

References bb_smscconn_receive(), client(), coding, smscconn::data, debug(), error(), from, gwlist_create, http_cgi_variable(), http_destroy_headers(), http_header_add(), HTTP_OK, http_send_reply(), smscconn::id, msg, msg_create, octstr_compare(), octstr_create, octstr_destroy(), octstr_duplicate, octstr_get_cstr, text, and conndata::username.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:648
static int coding
Definition: mtbatch.c:102
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2886
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 msg_create(type)
Definition: msg.h:136
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
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
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2695
Definition: http.h:142
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 octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
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
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871

◆ brunet_send_sms()

static int brunet_send_sms ( SMSCConn conn,
Msg sms 
)
static

Definition at line 81 of file brunet.c.

References smscconn::data, DC_7BIT, DC_8BIT, DC_UNDEF, debug(), fields_to_dcs(), http_create_empty_headers(), http_destroy_headers(), HTTP_METHOD_GET, conndata::http_ref, http_start_request(), smscconn::id, octstr_create, octstr_destroy(), octstr_format(), octstr_format_append(), octstr_get_cstr, octstr_imm(), octstr_len(), octstr_replace(), octstr_search(), octstr_url_decode(), conndata::send_url, SMS_PARAM_UNDEFINED, url, conndata::username, UUID_STR_LEN, and uuid_unparse().

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>",
163 
164  /*
165  * Brunet requires an SSL-enabled HTTP client call, this is handled
166  * transparently by the Kannel HTTP layer module.
167  */
169  NULL, 0, sms, NULL);
170 
172  octstr_destroy(tid);
173  http_destroy_headers(headers);
174 
175  return 0;
176 }
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
Definition: octstr.c:2649
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:250
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
int octstr_url_decode(Octstr *ostr)
Definition: octstr.c:1746
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1070
#define DC_8BIT
Definition: sms.h:111
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
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
List * http_create_empty_headers(void)
Definition: http.c:2872
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
int fields_to_dcs(Msg *msg, int mode)
Definition: sms.c:73
#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:342
Definition: octstr.c:118
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
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_7BIT
Definition: sms.h:110

Variable Documentation

◆ smsc_http_brunet_callback

struct smsc_http_fn_callbacks smsc_http_brunet_callback
Initial value:
= {
.init = brunet_init,
.send_sms = brunet_send_sms,
.parse_reply = brunet_parse_reply,
.receive_sms = brunet_receive_sms,
}
static void brunet_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: brunet.c:211
static void brunet_receive_sms(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: brunet.c:246
static int brunet_init(SMSCConn *conn, CfgGroup *cfg)
Definition: brunet.c:314
static int brunet_send_sms(SMSCConn *conn, Msg *sms)
Definition: brunet.c:81

Definition at line 327 of file brunet.c.

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