Kannel: Open Source WAP and SMS gateway  svn-r5262
smsc_http.c File Reference
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <limits.h>
#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.

Macros

#define DEFAULT_CHARSET   "UTF-8"
 
#define DEFAULT_UCS2_CHARSET   "UTF-16BE"
 

Functions

static void conndata_destroy (ConnData *conndata)
 
static void httpsmsc_receiver (void *arg)
 
static void httpsmsc_sender (void *arg)
 
static void httpsmsc_send_cb (void *arg)
 
static int httpsmsc_send (SMSCConn *conn, Msg *msg)
 
static long httpsmsc_queued (SMSCConn *conn)
 
static int httpsmsc_shutdown (SMSCConn *conn, int finish_sending)
 
int smsc_http_create (SMSCConn *conn, CfgGroup *cfg)
 

Macro Definition Documentation

◆ DEFAULT_CHARSET

#define DEFAULT_CHARSET   "UTF-8"

Definition at line 133 of file smsc_http.c.

Referenced by httpsmsc_send().

◆ DEFAULT_UCS2_CHARSET

#define DEFAULT_UCS2_CHARSET   "UTF-16BE"

Definition at line 134 of file smsc_http.c.

Referenced by httpsmsc_send().

Function Documentation

◆ conndata_destroy()

static void conndata_destroy ( ConnData conndata)
static

Definition at line 136 of file smsc_http.c.

References conndata::allow_ip, conndata::alt_charset, counter_destroy(), conndata::dlr_url, gwlist_destroy(), http_caller_destroy(), conndata::http_ref, conndata::max_pending_sends, conndata::msg_to_send, octstr_destroy(), conndata::open_sends, conndata::password, conndata::proxy, semaphore_destroy(), conndata::send_url, conndata::system_id, and conndata::username.

Referenced by httpsmsc_receiver(), httpsmsc_sender(), and smsc_http_create().

137 {
138  if (conndata == NULL)
139  return;
140  if (conndata->http_ref)
141  http_caller_destroy(conndata->http_ref);
142  octstr_destroy(conndata->allow_ip);
143  octstr_destroy(conndata->send_url);
144  octstr_destroy(conndata->dlr_url);
145  octstr_destroy(conndata->username);
146  octstr_destroy(conndata->password);
147  octstr_destroy(conndata->proxy);
148  octstr_destroy(conndata->system_id);
149  octstr_destroy(conndata->alt_charset);
150  counter_destroy(conndata->open_sends);
151  gwlist_destroy(conndata->msg_to_send, NULL);
152  if (conndata->max_pending_sends)
154 
155  gw_free(conndata);
156 }
Octstr * dlr_url
Definition: smsc_http_p.h:94
Octstr * alt_charset
Definition: smsc_http_p.h:104
void counter_destroy(Counter *counter)
Definition: counter.c:110
void semaphore_destroy(Semaphore *semaphore)
Definition: gw-semaphore.c:104
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
Octstr * allow_ip
Definition: smsc_http_p.h:92
Octstr * proxy
Definition: smsc_http_p.h:103
Octstr * system_id
Definition: smsc_http_p.h:99
Counter * open_sends
Definition: smsc_http_p.h:95
List * msg_to_send
Definition: smsc_http_p.h:105
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Semaphore * max_pending_sends
Definition: smsc_http_p.h:96
Octstr * password
Definition: smsc_http_p.h:98
Octstr * username
Definition: smsc_http_p.h:97
void http_caller_destroy(HTTPCaller *caller)
Definition: http.c:907
Octstr * send_url
Definition: smsc_http_p.h:93
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ httpsmsc_queued()

static long httpsmsc_queued ( SMSCConn conn)
static

Definition at line 451 of file smsc_http.c.

References smscconn::data, gwlist_len(), smscconn::load, conndata::msg_to_send, SMSCCONN_DEAD, and smscconn::status.

Referenced by smsc_http_create().

452 {
453  ConnData *conndata = conn->data;
454 
455  conn->load = (conndata ? (conn->status != SMSCCONN_DEAD ?
456  gwlist_len(conndata->msg_to_send) : 0) : 0);
457 
458  return conn->load;
459 }
long gwlist_len(List *list)
Definition: list.c:166
void * data
Definition: smscconn_p.h:249
List * msg_to_send
Definition: smsc_http_p.h:105
smscconn_status_t status
Definition: smscconn_p.h:151
int load
Definition: smscconn_p.h:152

◆ httpsmsc_receiver()

static void httpsmsc_receiver ( void *  arg)
static

Definition at line 162 of file smsc_http.c.

References conndata::allow_ip, bb_smscconn_killed(), conndata::callbacks, client(), conndata_destroy(), connect_denied(), counter_value(), smscconn::data, debug(), smsc_http_fn_callbacks::destroy, smscconn::flow_mutex, gwthread_join(), gwthread_wakeup(), http_accept_request(), http_caller_signal_shutdown(), http_cgivar_dump_into(), http_close_client(), http_close_port(), http_destroy_cgiargs(), http_destroy_headers(), conndata::http_ref, smscconn::id, info(), smscconn::log_idx, log_thread_to(), mutex_lock, mutex_unlock, octstr_append_char(), octstr_destroy(), octstr_get_cstr, conndata::open_sends, conndata::port, smsc_http_fn_callbacks::receive_sms, conndata::send_cb_thread, conndata::sender_thread, conndata::shutdown, SMSCCONN_DEAD, smscconn::status, and url.

Referenced by smsc_http_create().

163 {
164  SMSCConn *conn = arg;
165  ConnData *conndata = conn->data;
167  Octstr *ip, *url, *body;
168  List *headers, *cgivars;
169 
170  /* Make sure we log into our own log-file if defined */
171  log_thread_to(conn->log_idx);
172 
173  while (conndata->shutdown == 0) {
174  /* reset */
175  ip = url = body = NULL;
176  headers = cgivars = NULL;
177 
178  /* XXX if conn->is_stopped, do not receive new messages.. */
179 
180  client = http_accept_request(conndata->port, &ip, &url,
181  &headers, &body, &cgivars);
182  if (client == NULL)
183  break;
184 
185  if (cgivars != NULL) {
186  octstr_append_char(url, '?');
187  http_cgivar_dump_into(cgivars, url);
188  }
189 
190  debug("smsc.http", 0, "HTTP[%s]: Got request `%s'",
191  octstr_get_cstr(conn->id), octstr_get_cstr(url));
192 
193  if (connect_denied(conndata->allow_ip, ip)) {
194  info(0, "HTTP[%s]: Connection `%s' tried from denied "
195  "host %s, ignored", octstr_get_cstr(conn->id),
196  octstr_get_cstr(url), octstr_get_cstr(ip));
197  http_close_client(client);
198  } else
199  conndata->callbacks->receive_sms(conn, client, headers, body, cgivars);
200 
201  debug("smsc.http", 0, "HTTP[%s]: Destroying client information",
202  octstr_get_cstr(conn->id));
203  octstr_destroy(url);
204  octstr_destroy(ip);
205  octstr_destroy(body);
206  http_destroy_headers(headers);
207  http_destroy_cgiargs(cgivars);
208  }
209  debug("smsc.http", 0, "HTTP[%s]: httpsmsc_receiver dying",
210  octstr_get_cstr(conn->id));
211 
212  conndata->shutdown = 1;
213  http_close_port(conndata->port);
214 
215  /* unblock http_receive_result() if there are no open sends */
216  if (counter_value(conndata->open_sends) == 0)
218 
219  if (conndata->sender_thread != -1) {
220  gwthread_wakeup(conndata->sender_thread);
221  gwthread_join(conndata->sender_thread);
222  }
223  if (conndata->send_cb_thread != -1) {
224  gwthread_wakeup(conndata->send_cb_thread);
225  gwthread_join(conndata->send_cb_thread);
226  }
227 
228  mutex_lock(conn->flow_mutex);
229  conn->status = SMSCCONN_DEAD;
230  mutex_unlock(conn->flow_mutex);
231 
232  if (conndata->callbacks != NULL && conndata->callbacks->destroy != NULL)
233  conndata->callbacks->destroy(conn);
234  conn->data = NULL;
235  conndata_destroy(conndata);
237 }
void info(int err, const char *fmt,...)
Definition: log.c:672
static void conndata_destroy(ConnData *conndata)
Definition: smsc_http.c:136
void http_caller_signal_shutdown(HTTPCaller *caller)
Definition: http.c:913
#define mutex_unlock(m)
Definition: thread.h:136
void http_close_client(HTTPClient *client)
Definition: http.c:2735
void bb_smscconn_killed(void)
Definition: bb_smscconn.c:199
Octstr * id
Definition: smscconn_p.h:174
void gwthread_join(long thread)
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
void octstr_append_char(Octstr *ostr, int ch)
Definition: octstr.c:1515
int log_idx
Definition: smscconn_p.h:197
Octstr * allow_ip
Definition: smsc_http_p.h:92
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void(* destroy)(SMSCConn *conn)
Definition: smsc_http_p.h:75
int connect_denied(Octstr *allow_ip, Octstr *ip)
Definition: utils.c:833
void log_thread_to(int idx)
Definition: log.c:759
void http_destroy_headers(List *headers)
Definition: http.c:2856
volatile int shutdown
Definition: smsc_http_p.h:90
void http_destroy_cgiargs(List *args)
Definition: http.c:2795
long port
Definition: smsc_http_p.h:91
Counter * open_sends
Definition: smsc_http_p.h:95
HTTPClient * http_accept_request(int port, Octstr **client_ip, Octstr **url, List **headers, Octstr **body, List **cgivars)
Definition: http.c:2575
void http_close_port(int port)
Definition: http.c:2505
Mutex * flow_mutex
Definition: smscconn_p.h:157
long sender_thread
Definition: smsc_http_p.h:89
void http_cgivar_dump_into(List *cgiargs, Octstr *os)
Definition: http.c:3436
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
unsigned long counter_value(Counter *counter)
Definition: counter.c:145
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void gwthread_wakeup(long thread)
smscconn_status_t status
Definition: smscconn_p.h:151
void(* receive_sms)(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: smsc_http_p.h:78
static Octstr * url
Definition: test_xmlrpc.c:84
#define mutex_lock(m)
Definition: thread.h:130
struct smsc_http_fn_callbacks * callbacks
Definition: smsc_http_p.h:108
Definition: list.c:102
long send_cb_thread
Definition: smsc_http_p.h:88

◆ httpsmsc_send()

static int httpsmsc_send ( SMSCConn conn,
Msg msg 
)
static

Definition at line 403 of file smsc_http.c.

References conndata::alt_charset, conndata::callbacks, charset_convert(), smscconn::data, DC_7BIT, DC_UCS2, DC_UNDEF, DEFAULT_CHARSET, DEFAULT_UCS2_CHARSET, error(), gwlist_produce(), msg_duplicate(), conndata::msg_to_send, octstr_get_cstr, and smsc_http_fn_callbacks::send_sms.

Referenced by smsc_http_create().

404 {
405  ConnData *conndata = conn->data;
406  Msg *sms;
407 
408 
409  /* don't crash if no send_sms handle defined */
410  if (!conndata || !conndata->callbacks->send_sms)
411  return -1;
412 
413  sms = msg_duplicate(msg);
414  /* convert character encoding if required */
415  if (conndata->alt_charset) {
416  /*
417  * Converted now to the target character set based on the
418  * one we got in the msg, which is either UTF-8 (our normal
419  * inter-box encoding), but may also be UCS-2, so beware.
420  * In addition, IF we convert to an "extra" encoding here
421  * we also revert the .coding vaue to DC_UNDEF, in order
422  * that all API specific code doesn't indicate an encoding
423  * which is no longer inside the payload here.
424  */
425  if (sms->sms.coding == DC_7BIT) {
426  if (charset_convert(sms->sms.msgdata, DEFAULT_CHARSET,
427  octstr_get_cstr(conndata->alt_charset)) == 0) {
428  sms->sms.coding = DC_UNDEF;
429  } else {
430  error(0, "Failed to convert msgdata from charset <%s> to <%s>, will send as is.",
432  }
433  }
434  else if (sms->sms.coding == DC_UCS2) {
435  if (charset_convert(sms->sms.msgdata, DEFAULT_UCS2_CHARSET,
436  octstr_get_cstr(conndata->alt_charset)) == 0) {
437  sms->sms.coding = DC_UNDEF;
438  } else {
439  error(0, "Failed to convert msgdata from charset <%s> to <%s>, will send as is.",
441  }
442  }
443  }
444 
445  gwlist_produce(conndata->msg_to_send, sms);
446 
447  return 0;
448 }
void error(int err, const char *fmt,...)
Definition: log.c:648
#define DEFAULT_CHARSET
Definition: smsc_http.c:133
Msg * msg_duplicate(Msg *msg)
Definition: msg.c:111
Octstr * alt_charset
Definition: smsc_http_p.h:104
int(* send_sms)(SMSCConn *conn, Msg *msg)
Definition: smsc_http_p.h:76
void gwlist_produce(List *list, void *item)
Definition: list.c:411
void * data
Definition: smscconn_p.h:249
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Definition: msg.h:79
List * msg_to_send
Definition: smsc_http_p.h:105
#define DEFAULT_UCS2_CHARSET
Definition: smsc_http.c:134
#define DC_UNDEF
Definition: sms.h:109
struct smsc_http_fn_callbacks * callbacks
Definition: smsc_http_p.h:108
#define DC_UCS2
Definition: sms.h:112
int charset_convert(Octstr *string, char *charset_from, char *charset_to)
Definition: charset.c:589
#define DC_7BIT
Definition: sms.h:110

◆ httpsmsc_send_cb()

static void httpsmsc_send_cb ( void *  arg)
static

Definition at line 313 of file smsc_http.c.

References bb_smscconn_connected(), bb_smscconn_send_failed(), conndata::callbacks, smscconn::connect_time, counter_decrease(), counter_value(), smscconn::data, debug(), error(), smscconn::flow_mutex, gwthread_sleep(), http_destroy_headers(), http_receive_result, conndata::http_ref, smscconn::id, smscconn::log_idx, log_thread_to(), conndata::max_pending_sends, msg, mutex_lock, mutex_unlock, octstr_destroy(), octstr_get_cstr, conndata::open_sends, smsc_http_fn_callbacks::parse_reply, smscconn::reconnect_delay, semaphore_up(), conndata::shutdown, SMSCCONN_ACTIVE, SMSCCONN_FAILED_SHUTDOWN, SMSCCONN_FAILED_TEMPORARILY, SMSCCONN_RECONNECTING, smscconn::status, and warning().

Referenced by smsc_http_create().

314 {
315  SMSCConn *conn = arg;
316  ConnData *conndata = conn->data;
317  Msg *msg;
318  int status;
319  List *headers;
320  Octstr *final_url, *body;
321 
322  /* Make sure we log into our own log-file if defined */
323  log_thread_to(conn->log_idx);
324 
325  while(conndata->shutdown == 0 || counter_value(conndata->open_sends)) {
326 
327  msg = http_receive_result(conndata->http_ref, &status,
328  &final_url, &headers, &body);
329 
330  if (msg == NULL)
331  break; /* they told us to die, by unlocking */
332 
333  counter_decrease(conndata->open_sends);
334  if (conndata->max_pending_sends)
335  semaphore_up(conndata->max_pending_sends);
336 
337  /* Handle various states here. */
338 
339  /* request failed and we are not in shutdown mode */
340  if (status == -1 && conndata->shutdown == 0) {
341  error(0, "HTTP[%s]: Couldn't connect to SMS center."
342  "(retrying in %ld seconds) %ld.",
343  octstr_get_cstr(conn->id), conn->reconnect_delay, counter_value(conndata->open_sends));
344  mutex_lock(conn->flow_mutex);
346  mutex_unlock(conn->flow_mutex);
347  /* XXX how should we know whether it's temp. error ?? */
349  /*
350  * Just sleep reconnect delay and set conn to ACTIVE again;
351  * otherwise if no pending request are here, we leave conn in
352  * RECONNECTING state for ever and no routing (trials) take place.
353  */
354  if (counter_value(conndata->open_sends) == 0) {
356  /* and now enable routing again */
357  mutex_lock(conn->flow_mutex);
358  conn->status = SMSCCONN_ACTIVE;
359  time(&conn->connect_time);
360  mutex_unlock(conn->flow_mutex);
361  /* tell bearerbox core that we are connected again */
362  bb_smscconn_connected(conn);
363  }
364  continue;
365  }
366  /* request failed and we *are* in shutdown mode, drop the message */
367  else if (status == -1 && conndata->shutdown == 1) {
369  }
370  /* request succeeded */
371  else {
372  /* we received a response, so this link is considered online again */
373  if (conn->status != SMSCCONN_ACTIVE) {
374  mutex_lock(conn->flow_mutex);
375  conn->status = SMSCCONN_ACTIVE;
376  time(&conn->connect_time);
377  mutex_unlock(conn->flow_mutex);
378  /* tell bearerbox core that we are connected again */
379  bb_smscconn_connected(conn);
380  }
381  conndata->callbacks->parse_reply(conn, msg, status, headers, body);
382  }
383 
384  http_destroy_headers(headers);
385  octstr_destroy(final_url);
386  octstr_destroy(body);
387  }
388  debug("smsc.http", 0, "HTTP[%s]: httpsmsc_send_cb dying",
389  octstr_get_cstr(conn->id));
390  conndata->shutdown = 1;
391 
392  if (counter_value(conndata->open_sends)) {
393  warning(0, "HTTP[%s]: Shutdown while <%ld> requests are pending.",
394  octstr_get_cstr(conn->id), counter_value(conndata->open_sends));
395  }
396 }
void error(int err, const char *fmt,...)
Definition: log.c:648
void bb_smscconn_connected(SMSCConn *conn)
Definition: bb_smscconn.c:192
#define mutex_unlock(m)
Definition: thread.h:136
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
int log_idx
Definition: smscconn_p.h:197
void(* parse_reply)(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: smsc_http_p.h:77
unsigned long counter_decrease(Counter *counter)
Definition: counter.c:155
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
long reconnect_delay
Definition: smscconn_p.h:199
void log_thread_to(int idx)
Definition: log.c:759
void http_destroy_headers(List *headers)
Definition: http.c:2856
volatile int shutdown
Definition: smsc_http_p.h:90
Counter * open_sends
Definition: smsc_http_p.h:95
Definition: msg.h:79
time_t connect_time
Definition: smscconn_p.h:155
Mutex * flow_mutex
Definition: smscconn_p.h:157
void warning(int err, const char *fmt,...)
Definition: log.c:660
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
unsigned long counter_value(Counter *counter)
Definition: counter.c:145
void gwthread_sleep(double seconds)
Semaphore * max_pending_sends
Definition: smsc_http_p.h:96
#define http_receive_result(caller, status, final_url, headers, body)
Definition: http.h:383
void semaphore_up(Semaphore *semaphore)
Definition: gw-semaphore.c:118
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
smscconn_status_t status
Definition: smscconn_p.h:151
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:328
#define mutex_lock(m)
Definition: thread.h:130
struct smsc_http_fn_callbacks * callbacks
Definition: smsc_http_p.h:108
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ httpsmsc_sender()

static void httpsmsc_sender ( void *  arg)
static

Definition at line 243 of file smsc_http.c.

References bb_smscconn_killed(), bb_smscconn_send_failed(), conndata::callbacks, conndata_destroy(), counter_decrease(), counter_increase(), counter_value(), smscconn::data, delay, smsc_http_fn_callbacks::destroy, smscconn::flow_mutex, gwlist_consume(), gwlist_extract_first(), gwthread_join(), gwthread_sleep(), gwthread_wakeup(), http_caller_signal_shutdown(), conndata::http_ref, smscconn::log_idx, log_thread_to(), conndata::max_pending_sends, msg, conndata::msg_to_send, mutex_lock, mutex_unlock, conndata::open_sends, conndata::port, semaphore_down(), semaphore_up(), conndata::send_cb_thread, smsc_http_fn_callbacks::send_sms, conndata::shutdown, SMSCCONN_DEAD, SMSCCONN_FAILED_SHUTDOWN, smscconn::status, and smscconn::throughput.

Referenced by smsc_http_create().

244 {
245  SMSCConn *conn = arg;
246  ConnData *conndata = conn->data;
247  Msg *msg;
248  double delay = 0;
249 
250  /* Make sure we log into our own log-file if defined */
251  log_thread_to(conn->log_idx);
252 
253  if (conn->throughput) {
254  delay = 1.0 / conn->throughput;
255  }
256 
257  while (conndata->shutdown == 0) {
258  /* check if we can send ; otherwise block on semaphore */
259  if (conndata->max_pending_sends)
261 
262  if (conndata->shutdown) {
263  if (conndata->max_pending_sends)
264  semaphore_up(conndata->max_pending_sends);
265  break;
266  }
267 
268  msg = gwlist_consume(conndata->msg_to_send);
269  if (msg == NULL)
270  break;
271 
272  /* obey throughput speed limit, if any */
273  if (conn->throughput > 0) {
274  gwthread_sleep(delay);
275  }
276  counter_increase(conndata->open_sends);
277  if (conndata->callbacks->send_sms(conn, msg) == -1) {
278  counter_decrease(conndata->open_sends);
279  if (conndata->max_pending_sends)
280  semaphore_up(conndata->max_pending_sends);
281  }
282  }
283 
284  /* put outstanding sends back into global queue */
285  while((msg = gwlist_extract_first(conndata->msg_to_send)))
287 
288  /* if there no receiver shutdown */
289  if (conndata->port <= 0) {
290  /* unblock http_receive_result() if there are no open sends */
291  if (counter_value(conndata->open_sends) == 0)
293 
294  if (conndata->send_cb_thread != -1) {
295  gwthread_wakeup(conndata->send_cb_thread);
296  gwthread_join(conndata->send_cb_thread);
297  }
298  mutex_lock(conn->flow_mutex);
299  conn->status = SMSCCONN_DEAD;
300  mutex_unlock(conn->flow_mutex);
301 
302  if (conndata->callbacks != NULL && conndata->callbacks->destroy != NULL)
303  conndata->callbacks->destroy(conn);
304  conn->data = NULL;
305  conndata_destroy(conndata);
307  }
308 }
static void conndata_destroy(ConnData *conndata)
Definition: smsc_http.c:136
int(* send_sms)(SMSCConn *conn, Msg *msg)
Definition: smsc_http_p.h:76
void http_caller_signal_shutdown(HTTPCaller *caller)
Definition: http.c:913
#define mutex_unlock(m)
Definition: thread.h:136
void bb_smscconn_killed(void)
Definition: bb_smscconn.c:199
void gwthread_join(long thread)
void * data
Definition: smscconn_p.h:249
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
int log_idx
Definition: smscconn_p.h:197
unsigned long counter_decrease(Counter *counter)
Definition: counter.c:155
void(* destroy)(SMSCConn *conn)
Definition: smsc_http_p.h:75
unsigned long counter_increase(Counter *counter)
Definition: counter.c:123
void log_thread_to(int idx)
Definition: log.c:759
void semaphore_down(Semaphore *semaphore)
Definition: gw-semaphore.c:132
volatile int shutdown
Definition: smsc_http_p.h:90
long port
Definition: smsc_http_p.h:91
Counter * open_sends
Definition: smsc_http_p.h:95
Definition: msg.h:79
List * msg_to_send
Definition: smsc_http_p.h:105
void * gwlist_extract_first(List *list)
Definition: list.c:305
double throughput
Definition: smscconn_p.h:203
Mutex * flow_mutex
Definition: smscconn_p.h:157
static double delay
Definition: mtbatch.c:99
unsigned long counter_value(Counter *counter)
Definition: counter.c:145
void gwthread_sleep(double seconds)
Semaphore * max_pending_sends
Definition: smsc_http_p.h:96
void semaphore_up(Semaphore *semaphore)
Definition: gw-semaphore.c:118
void * gwlist_consume(List *list)
Definition: list.c:427
void gwthread_wakeup(long thread)
smscconn_status_t status
Definition: smscconn_p.h:151
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:328
#define mutex_lock(m)
Definition: thread.h:130
struct smsc_http_fn_callbacks * callbacks
Definition: smsc_http_p.h:108
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
long send_cb_thread
Definition: smsc_http_p.h:88

◆ httpsmsc_shutdown()

static int httpsmsc_shutdown ( SMSCConn conn,
int  finish_sending 
)
static

Definition at line 462 of file smsc_http.c.

References smscconn::data, debug(), smscconn::flow_mutex, gwlist_remove_producer(), gwthread_wakeup(), http_close_port(), smscconn::id, conndata::msg_to_send, mutex_lock, mutex_unlock, octstr_get_cstr, conndata::port, conndata::receive_thread, conndata::sender_thread, conndata::shutdown, SMSCCONN_KILLED_SHUTDOWN, and smscconn::why_killed.

Referenced by smsc_http_create().

463 {
464  ConnData *conndata = conn->data;
465 
466  if (conndata == NULL)
467  return 0;
468 
469  debug("httpsmsc_shutdown", 0, "HTTP[%s]: Shutting down",
470  octstr_get_cstr(conn->id));
471 
472  mutex_lock(conn->flow_mutex);
474 
475  conndata->shutdown = 1;
476 
477  if (conndata->port > 0)
478  http_close_port(conndata->port);
480  if (conndata->receive_thread != -1)
481  gwthread_wakeup(conndata->receive_thread);
482  if (conndata->sender_thread != -1)
483  gwthread_wakeup(conndata->sender_thread);
484  mutex_unlock(conn->flow_mutex);
485 
486  return 0;
487 }
long receive_thread
Definition: smsc_http_p.h:87
#define mutex_unlock(m)
Definition: thread.h:136
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
smscconn_killed_t why_killed
Definition: smscconn_p.h:153
volatile int shutdown
Definition: smsc_http_p.h:90
long port
Definition: smsc_http_p.h:91
List * msg_to_send
Definition: smsc_http_p.h:105
void http_close_port(int port)
Definition: http.c:2505
void gwlist_remove_producer(List *list)
Definition: list.c:401
Mutex * flow_mutex
Definition: smscconn_p.h:157
long sender_thread
Definition: smsc_http_p.h:89
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void gwthread_wakeup(long thread)
#define mutex_lock(m)
Definition: thread.h:130

◆ smsc_http_create()

int smsc_http_create ( SMSCConn conn,
CfgGroup cfg 
)

Definition at line 490 of file smsc_http.c.

References conndata::allow_ip, conndata::alt_charset, conndata::callbacks, cfg_get, cfg_get_bool(), cfg_get_integer(), conndata_destroy(), smscconn::connect_time, counter_create(), conndata::data, smscconn::data, smsc_http_fn_callbacks::destroy, conndata::dlr_url, error(), gw_dlopen_get_symbol(), gwlist_add_producer(), gwlist_create, gwthread_create, http_caller_create(), http_open_port(), conndata::http_ref, httpsmsc_queued(), httpsmsc_receiver(), httpsmsc_send(), httpsmsc_send_cb(), httpsmsc_sender(), httpsmsc_shutdown(), smscconn::id, info(), smsc_http_fn_callbacks::init, conndata::max_pending_sends, conndata::msg_to_send, smscconn::name, conndata::no_coding, conndata::no_sender, conndata::no_sep, octstr_destroy(), octstr_format(), octstr_get_cstr, octstr_imm(), conndata::open_sends, panic, conndata::password, conndata::port, conndata::proxy, smscconn::queued, conndata::receive_thread, semaphore_create(), conndata::send_cb_thread, smscconn::send_msg, conndata::send_url, conndata::sender_thread, conndata::shutdown, smscconn::shutdown, SMSCCONN_ACTIVE, SMSCCONN_ACTIVE_RECV, SMSCCONN_DEAD, SMSCCONN_KILLED_CANNOT_CONNECT, ssl, smscconn::status, conndata::system_id, type, conndata::username, warning(), and smscconn::why_killed.

Referenced by smscconn_create().

491 {
492  ConnData *conndata = NULL;
493  Octstr *type = NULL, *callbackname = NULL;
494  int ssl = 0; /* indicate if SSL-enabled server should be used */
495  long max_ps;
496 
497  if ((type = cfg_get(cfg, octstr_imm("system-type"))) == NULL) {
498  error(0, "HTTP[%s]: 'system-type' missing in smsc 'http' record.",
499  octstr_get_cstr(conn->id));
500  octstr_destroy(type);
501  return -1;
502  }
503 
504  conndata = gw_malloc(sizeof(ConnData));
505  /* reset conndata */
506  memset(conndata, 0, sizeof(ConnData));
507 
508  conn->data = conndata;
509  conndata->http_ref = NULL;
510  conndata->data = NULL;
511 
512  if (cfg_get_integer(&conndata->port, cfg, octstr_imm("port")) == -1) {
513  warning(0, "HTTP[%s]: 'port' not set in smsc 'http' group.",
514  octstr_get_cstr(conn->id));
515  conndata->port = -1;
516  }
517 
518  conndata->allow_ip = cfg_get(cfg, octstr_imm("connect-allow-ip"));
519  conndata->send_url = cfg_get(cfg, octstr_imm("send-url"));
520  conndata->username = cfg_get(cfg, octstr_imm("smsc-username"));
521  conndata->password = cfg_get(cfg, octstr_imm("smsc-password"));
522  conndata->system_id = cfg_get(cfg, octstr_imm("system-id"));
523  cfg_get_bool(&conndata->no_sender, cfg, octstr_imm("no-sender"));
524  cfg_get_bool(&conndata->no_coding, cfg, octstr_imm("no-coding"));
525  cfg_get_bool(&conndata->no_sep, cfg, octstr_imm("no-sep"));
526  conndata->proxy = cfg_get(cfg, octstr_imm("system-id"));
527  cfg_get_bool(&ssl, cfg, octstr_imm("use-ssl"));
528  conndata->dlr_url = cfg_get(cfg, octstr_imm("dlr-url"));
529  conndata->alt_charset = cfg_get(cfg, octstr_imm("alt-charset"));
530 
531  if (cfg_get_integer(&max_ps, cfg, octstr_imm("max-pending-submits")) == -1 || max_ps < 1)
532  max_ps = 10;
533 
534  conndata->max_pending_sends = semaphore_create(max_ps);
535 
536  if (conndata->port <= 0 && conndata->send_url == NULL) {
537  error(0, "Sender and receiver disabled. Dummy SMSC not allowed.");
538  goto error;
539  }
540  if (conndata->send_url == NULL)
541  panic(0, "HTTP[%s]: Sending not allowed. No 'send-url' specified.",
542  octstr_get_cstr(conn->id));
543 
544  /* callback struct is always in the format: smsc_http_XXX_callback where XXX is our type */
545  callbackname = octstr_format("smsc_http_%S_callback", type);
546  /* try to find the struct */
547  if (gw_dlopen_get_symbol(NULL, octstr_get_cstr(callbackname), (void**)&conndata->callbacks) == -1 || conndata->callbacks == NULL) {
548  error(0, "HTTP[%s]: system-type '%s' unknown smsc 'http' record.",
549  octstr_get_cstr(conn->id), octstr_get_cstr(type));
550  goto error;
551  }
552 
553  if (conndata->callbacks != NULL && conndata->callbacks->init != NULL && conndata->callbacks->init(conn, cfg)) {
554  error(0, "HTTP[%s]: submodule '%s' init failed.", octstr_get_cstr(conn->id), octstr_get_cstr(type));
555  goto error;
556  }
557 
558  conndata->open_sends = counter_create();
559  conndata->msg_to_send = gwlist_create();
560  gwlist_add_producer(conndata->msg_to_send);
561  conndata->http_ref = http_caller_create();
562 
563  conn->name = octstr_format("HTTP%s:%S:%d", (ssl?"S":""), type, conndata->port);
564 
565  if (conndata->send_url != NULL) {
566  conn->status = SMSCCONN_ACTIVE;
567  } else {
569  }
570 
571  conn->connect_time = time(NULL);
572 
573  conn->shutdown = httpsmsc_shutdown;
574  conn->queued = httpsmsc_queued;
575  conn->send_msg = httpsmsc_send;
576 
577  conndata->shutdown = 0;
578 
579  /* start receiver thread */
580  if (conndata->port > 0) {
581  if (http_open_port(conndata->port, ssl) == -1)
582  goto error;
583  if ((conndata->receive_thread = gwthread_create(httpsmsc_receiver, conn)) == -1)
584  goto error;
585  } else
586  conndata->receive_thread = -1;
587 
588  /* start sender threads */
589  if (conndata->send_url) {
590  if ((conndata->send_cb_thread =
591  gwthread_create(httpsmsc_send_cb, conn)) == -1)
592  goto error;
593  if ((conndata->sender_thread =
594  gwthread_create(httpsmsc_sender, conn)) == -1)
595  goto error;
596  }
597  else {
598  conndata->send_cb_thread = conndata->sender_thread = -1;
599  }
600 
601  info(0, "HTTP[%s]: Initiated and ready", octstr_get_cstr(conn->id));
602 
603  octstr_destroy(callbackname);
604  octstr_destroy(type);
605  return 0;
606 
607 error:
608  error(0, "HTTP[%s]: Failed to create HTTP SMSC connection",
609  octstr_get_cstr(conn->id));
610 
611  if (conndata->callbacks != NULL && conndata->callbacks->destroy != NULL)
612  conndata->callbacks->destroy(conn);
613  conn->data = NULL;
614  conndata_destroy(conndata);
616  conn->status = SMSCCONN_DEAD;
617  octstr_destroy(callbackname);
618  octstr_destroy(type);
619  return -1;
620 }
Octstr * name
Definition: smscconn_p.h:173
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
Octstr * dlr_url
Definition: smsc_http_p.h:94
void * data
Definition: smsc_http_p.h:111
static void httpsmsc_receiver(void *arg)
Definition: smsc_http.c:162
static void conndata_destroy(ConnData *conndata)
Definition: smsc_http.c:136
Octstr * alt_charset
Definition: smsc_http_p.h:104
long receive_thread
Definition: smsc_http_p.h:87
int gw_dlopen_get_symbol(const char *lib_path, const char *name, void **result)
Definition: gw-dlopen.c:70
int ssl
int no_sep
Definition: smsc_http_p.h:102
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
int type
Definition: smsc_cimd2.c:215
Octstr * allow_ip
Definition: smsc_http_p.h:92
#define cfg_get(grp, varname)
Definition: cfg.h:86
Octstr * proxy
Definition: smsc_http_p.h:103
Octstr * system_id
Definition: smsc_http_p.h:99
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void(* destroy)(SMSCConn *conn)
Definition: smsc_http_p.h:75
smscconn_killed_t why_killed
Definition: smscconn_p.h:153
volatile int shutdown
Definition: smsc_http_p.h:90
long port
Definition: smsc_http_p.h:91
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
Counter * open_sends
Definition: smsc_http_p.h:95
int no_coding
Definition: smsc_http_p.h:101
List * msg_to_send
Definition: smsc_http_p.h:105
Counter * counter_create(void)
Definition: counter.c:94
static void httpsmsc_send_cb(void *arg)
Definition: smsc_http.c:313
time_t connect_time
Definition: smscconn_p.h:155
long sender_thread
Definition: smsc_http_p.h:89
void warning(int err, const char *fmt,...)
Definition: log.c:660
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define gwthread_create(func, arg)
Definition: gwthread.h:90
int no_sender
Definition: smsc_http_p.h:100
static int httpsmsc_shutdown(SMSCConn *conn, int finish_sending)
Definition: smsc_http.c:462
int http_open_port(int port, int ssl)
Definition: http.c:2499
Semaphore * max_pending_sends
Definition: smsc_http_p.h:96
int cfg_get_bool(int *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:756
int(* init)(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_http_p.h:74
Definition: octstr.c:118
static long httpsmsc_queued(SMSCConn *conn)
Definition: smsc_http.c:451
int(* shutdown)(SMSCConn *conn, int finish_sending)
Definition: smscconn_p.h:229
Octstr * password
Definition: smsc_http_p.h:98
int cfg_get_integer(long *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:739
#define panic
Definition: log.h:87
smscconn_status_t status
Definition: smscconn_p.h:151
HTTPCaller * http_caller_create(void)
Definition: http.c:897
#define gwlist_create()
Definition: list.h:136
long(* queued)(SMSCConn *conn)
Definition: smscconn_p.h:240
int(* send_msg)(SMSCConn *conn, Msg *msg)
Definition: smscconn_p.h:235
Octstr * username
Definition: smsc_http_p.h:97
static int httpsmsc_send(SMSCConn *conn, Msg *msg)
Definition: smsc_http.c:403
Octstr * send_url
Definition: smsc_http_p.h:93
void gwlist_add_producer(List *list)
Definition: list.c:383
static void httpsmsc_sender(void *arg)
Definition: smsc_http.c:243
struct smsc_http_fn_callbacks * callbacks
Definition: smsc_http_p.h:108
Semaphore * semaphore_create(long n)
Definition: gw-semaphore.c:81
long send_cb_thread
Definition: smsc_http_p.h:88
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.