Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
brunet.c File Reference
#include "gwlib/gwlib.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

static int brunet_init ( SMSCConn conn,
CfgGroup cfg 
)
static

Definition at line 304 of file brunet.c.

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

305 {
306  ConnData *conndata = conn->data;
307 
308  if (conndata->username == NULL) {
309  error(0, "HTTP[%s]: 'username' (=CustomerId) required for Brunet http smsc",
310  octstr_get_cstr(conn->id));
311  return -1;
312  }
313 
314  return 0;
315 }
void error(int err, const char *fmt,...)
Definition: log.c:612
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * username
Definition: smsc_http.c:154
static Dict* brunet_parse_body ( Octstr body)
static

Definition at line 174 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().

175 {
176  Dict *param = NULL;
177  List *words = NULL;
178  long len;
179  Octstr *word;
180 
181  words = octstr_split_words(body);
182  if ((len = gwlist_len(words)) > 0) {
183  param = dict_create(4, (void(*)(void *)) octstr_destroy);
184  while ((word = gwlist_extract_first(words)) != NULL) {
185  List *l = octstr_split(word, octstr_imm("="));
186  Octstr *key = gwlist_extract_first(l);
187  Octstr *value = gwlist_extract_first(l);
188  if (octstr_len(key))
189  dict_put(param, key, value);
190  octstr_destroy(key);
191  octstr_destroy(word);
192  gwlist_destroy(l, (void(*)(void *)) octstr_destroy);
193  }
194  }
195  gwlist_destroy(words, (void(*)(void *)) octstr_destroy);
196 
197  return param;
198 }
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:281
void * gwlist_extract_first(List *list)
Definition: list.c:305
Definition: dict.c:116
List * octstr_split_words(const Octstr *ostr)
Definition: octstr.c:1600
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
List * octstr_split(const Octstr *os, const Octstr *sep)
Definition: octstr.c:1638
Definition: list.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
static void brunet_parse_reply ( SMSCConn conn,
Msg msg,
int  status,
List headers,
Octstr body 
)
static

Definition at line 201 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, octstr_case_compare(), octstr_duplicate, octstr_get_cstr, octstr_imm(), SMSCCONN_FAILED_MALFORMED, and SMSCCONN_FAILED_REJECTED.

203 {
204  if (status == HTTP_OK || status == HTTP_ACCEPTED) {
205  Dict *param;
206  Octstr *status;
207 
208  if ((param = brunet_parse_body(body)) != NULL &&
209  (status = dict_get(param, octstr_imm("Status"))) != NULL &&
210  octstr_case_compare(status, octstr_imm("0")) == 0) {
211  Octstr *msg_id;
212 
213  /* pass the MessageId for this MT to the logging facility */
214  if ((msg_id = dict_get(param, octstr_imm("MessageId"))) != NULL)
215  msg->sms.binfo = octstr_duplicate(msg_id);
216 
217  bb_smscconn_sent(conn, msg, NULL);
218 
219  } else {
220  error(0, "HTTP[%s]: Message was malformed. SMSC response `%s'.",
221  octstr_get_cstr(conn->id), octstr_get_cstr(body));
222  bb_smscconn_send_failed(conn, msg,
224  }
225  dict_destroy(param);
226 
227  } else {
228  error(0, "HTTP[%s]: Message was rejected. SMSC response `%s'.",
229  octstr_get_cstr(conn->id), octstr_get_cstr(body));
230  bb_smscconn_send_failed(conn, msg,
232  }
233 }
void error(int err, const char *fmt,...)
Definition: log.c:612
Octstr * id
Definition: smscconn_p.h:174
static Dict * brunet_parse_body(Octstr *body)
Definition: brunet.c:174
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
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:901
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:279
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:328
static void brunet_receive_sms ( SMSCConn conn,
HTTPClient client,
List headers,
Octstr body,
List cgivars 
)
static

Definition at line 236 of file brunet.c.

References bb_smscconn_receive(), 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.

238 {
239  ConnData *conndata = conn->data;
240  Octstr *user, *from, *to, *text, *udh;
241  Octstr *retmsg;
242  int mclass, mwi, coding, validity, deferred;
243  List *reply_headers;
244  int ret;
245 
246  mclass = mwi = coding = validity = deferred = 0;
247 
248  user = http_cgi_variable(cgivars, "CustomerId");
249  from = http_cgi_variable(cgivars, "MsIsdn");
250  to = http_cgi_variable(cgivars, "Recipient");
251  text = http_cgi_variable(cgivars, "SMMO");
252  udh = http_cgi_variable(cgivars, "XSer");
253 
254  debug("smsc.http.brunet", 0, "HTTP[%s]: Received a request",
255  octstr_get_cstr(conn->id));
256 
257  if (user == NULL || octstr_compare(user, conndata->username) != 0) {
258  error(0, "HTTP[%s]: Authorization failure. CustomerId was <%s>.",
259  octstr_get_cstr(conn->id), octstr_get_cstr(user));
260  retmsg = octstr_create("Authorization failed for MO submission.");
261  }
262  else if (from == NULL || to == NULL || text == NULL) {
263  error(0, "HTTP[%s]: Insufficient args.",
264  octstr_get_cstr(conn->id));
265  retmsg = octstr_create("Insufficient arguments, rejected.");
266  }
267  else {
268  Msg *msg;
269  msg = msg_create(sms);
270 
271  debug("smsc.http.brunet", 0, "HTTP[%s]: Received new MO SMS.",
272  octstr_get_cstr(conn->id));
273 
274  msg->sms.sender = octstr_duplicate(from);
275  msg->sms.receiver = octstr_duplicate(to);
276  msg->sms.msgdata = octstr_duplicate(text);
277  msg->sms.udhdata = octstr_duplicate(udh);
278 
279  msg->sms.smsc_id = octstr_duplicate(conn->id);
280  msg->sms.time = time(NULL); /* XXX maybe extract from DateReceived */
281  msg->sms.mclass = mclass;
282  msg->sms.mwi = mwi;
283  msg->sms.coding = coding;
284  msg->sms.validity = time(NULL) + validity * 60;
285  msg->sms.deferred = time(NULL) + deferred * 60;
286 
287  ret = bb_smscconn_receive(conn, msg);
288  if (ret == -1)
289  retmsg = octstr_create("Status=1");
290  else
291  retmsg = octstr_create("Status=0");
292  }
293 
294  reply_headers = gwlist_create();
295  http_header_add(reply_headers, "Content-Type", "text/plain");
296  debug("smsc.http.brunet", 0, "HTTP[%s]: Sending reply `%s'.",
297  octstr_get_cstr(conn->id), octstr_get_cstr(retmsg));
298  http_send_reply(client, HTTP_OK, reply_headers, retmsg);
299 
300  octstr_destroy(retmsg);
301  http_destroy_headers(reply_headers);
302 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
#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:2813
void http_destroy_headers(List *headers)
Definition: http.c:2856
static Octstr * from
Definition: mtbatch.c:95
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2671
Definition: http.h:142
Definition: msg.h:79
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:478
char * text
Definition: smsc_cimd2.c:921
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#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:690
#define gwlist_create()
Definition: list.h:136
Octstr * username
Definition: smsc_http.c:154
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
static int brunet_send_sms ( SMSCConn conn,
Msg sms 
)
static

Definition at line 71 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().

72 {
73  ConnData *conndata = conn->data;
74  Octstr *url, *tid, *xser;
75  List *headers;
76  char id[UUID_STR_LEN + 1];
77  int dcs;
78 
79  /*
80  * Construct TransactionId.
81  * Beware that brunet needs an "clean" octstr representation,
82  * without the dashes in the string. So remove them.
83  */
84  uuid_unparse(sms->sms.id, id);
85  tid = octstr_create(id);
86  octstr_replace(tid, octstr_imm("-"), octstr_imm(""));
87 
88  /* form the basic URL */
89  url = octstr_format("%S?MsIsdn=%E&Originator=%E",
90  conndata->send_url, sms->sms.receiver, sms->sms.sender);
91 
92  /*
93  * We use &binfo=<foobar> from sendsms interface to encode
94  * additional paramters. If a mandatory value is not set,
95  * a default value is applied
96  */
97  if (octstr_len(sms->sms.binfo)) {
98  octstr_url_decode(sms->sms.binfo);
99  octstr_format_append(url, "&%S", sms->sms.binfo);
100  }
101  /* CustomerId */
102  if (octstr_search(url, octstr_create("CustomerId="), 0) == -1) {
103  octstr_format_append(url, "&CustomerId=%S", conndata->username);
104  }
105  /* TransactionId */
106  if (octstr_search(url, octstr_create("TransactionId="), 0) == -1) {
107  octstr_format_append(url, "&TransactionId=%S", tid);
108  }
109  /* SMSCount */
110  if (octstr_search(url, octstr_create("SMSCount="), 0) == -1) {
111  octstr_format_append(url, "&%s", "SMSCount=1");
112  }
113  /* ActionType */
114  if (octstr_search(url, octstr_create("ActionType="), 0) == -1) {
115  octstr_format_append(url, "&%s", "ActionType=A");
116  }
117  /* ServiceDeliveryType */
118  if (octstr_search(url, octstr_create("ServiceDeliveryType="), 0) == -1) {
119  octstr_format_append(url, "&%s", "ServiceDeliveryType=P");
120  }
121 
122  /* if coding is not set and UDH exists, assume DC_8BIT
123  * else default to DC_7BIT */
124  if (sms->sms.coding == DC_UNDEF)
125  sms->sms.coding = octstr_len(sms->sms.udhdata) > 0 ? DC_8BIT : DC_7BIT;
126 
127  if (sms->sms.coding == DC_8BIT)
128  octstr_format_append(url, "&MessageType=B&Text=%H", sms->sms.msgdata);
129  else
130  octstr_format_append(url, "&MessageType=S&Text=%E", sms->sms.msgdata);
131 
132  dcs = fields_to_dcs(sms,
133  (sms->sms.alt_dcs != SMS_PARAM_UNDEFINED ? sms->sms.alt_dcs : 0));
134 
135  /* XSer processing */
136  xser = octstr_create("");
137  /* XSer DCS values */
138  if (dcs != 0 && dcs != 4)
139  octstr_format_append(xser, "0201%02x", dcs & 0xff);
140  /* add UDH header */
141  if (octstr_len(sms->sms.udhdata)) {
142  octstr_format_append(xser, "01%02x%H", octstr_len(sms->sms.udhdata),
143  sms->sms.udhdata);
144  }
145  if (octstr_len(xser) > 0)
146  octstr_format_append(url, "&XSer=%S", xser);
147  octstr_destroy(xser);
148 
149 
150  headers = http_create_empty_headers();
151  debug("smsc.http.brunet", 0, "HTTP[%s]: Sending request <%s>",
152  octstr_get_cstr(conn->id), octstr_get_cstr(url));
153 
154  /*
155  * Brunet requires an SSL-enabled HTTP client call, this is handled
156  * transparently by the Kannel HTTP layer module.
157  */
158  http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers,
159  NULL, 0, sms, NULL);
160 
161  octstr_destroy(url);
162  octstr_destroy(tid);
163  http_destroy_headers(headers);
164 
165  return 0;
166 }
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
Definition: octstr.c:2647
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
HTTPCaller * http_ref
Definition: smsc_http.c:143
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:561
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void http_destroy_headers(List *headers)
Definition: http.c:2856
void http_start_request(HTTPCaller *caller, int method, Octstr *url, List *headers, Octstr *body, int follow, void *id, Octstr *certkeyfile)
Definition: http.c:1745
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
List * http_create_empty_headers(void)
Definition: http.c:2849
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
#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
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2505
Octstr * username
Definition: smsc_http.c:154
#define DC_UNDEF
Definition: sms.h:109
Octstr * send_url
Definition: smsc_http.c:150
static Octstr * url
Definition: test_xmlrpc.c:84
Definition: list.c:102
#define DC_7BIT
Definition: sms.h:110

Variable Documentation

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:201
static void brunet_receive_sms(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: brunet.c:236
static int brunet_init(SMSCConn *conn, CfgGroup *cfg)
Definition: brunet.c:304
static int brunet_send_sms(SMSCConn *conn, Msg *sms)
Definition: brunet.c:71

Definition at line 317 of file brunet.c.

Referenced by smsc_http_create().

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