Kannel: Open Source WAP and SMS gateway  svn-r5285
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, 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)
153 
154  gw_free(conndata);
155 }
Octstr * dlr_url
Definition: smsc_http_p.h:94
Octstr * alt_charset
Definition: smsc_http_p.h:102
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 * system_id
Definition: smsc_http_p.h:106
Counter * open_sends
Definition: smsc_http_p.h:95
List * msg_to_send
Definition: smsc_http_p.h:103
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
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 450 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().

451 {
452  ConnData *conndata = conn->data;
453 
454  conn->load = (conndata ? (conn->status != SMSCCONN_DEAD ?
455  gwlist_len(conndata->msg_to_send) : 0) : 0);
456 
457  return conn->load;
458 }
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:103
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 161 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().

162 {
163  SMSCConn *conn = arg;
164  ConnData *conndata = conn->data;
166  Octstr *ip, *url, *body;
167  List *headers, *cgivars;
168 
169  /* Make sure we log into our own log-file if defined */
170  log_thread_to(conn->log_idx);
171 
172  while (conndata->shutdown == 0) {
173  /* reset */
174  ip = url = body = NULL;
175  headers = cgivars = NULL;
176 
177  /* XXX if conn->is_stopped, do not receive new messages.. */
178 
180  &headers, &body, &cgivars);
181  if (client == NULL)
182  break;
183 
184  if (cgivars != NULL) {
185  octstr_append_char(url, '?');
186  http_cgivar_dump_into(cgivars, url);
187  }
188 
189  debug("smsc.http", 0, "HTTP[%s]: Got request `%s'",
191 
192  if (connect_denied(conndata->allow_ip, ip)) {
193  info(0, "HTTP[%s]: Connection `%s' tried from denied "
194  "host %s, ignored", octstr_get_cstr(conn->id),
197  } else
198  conndata->callbacks->receive_sms(conn, client, headers, body, cgivars);
199 
200  debug("smsc.http", 0, "HTTP[%s]: Destroying client information",
201  octstr_get_cstr(conn->id));
203  octstr_destroy(ip);
204  octstr_destroy(body);
205  http_destroy_headers(headers);
206  http_destroy_cgiargs(cgivars);
207  }
208  debug("smsc.http", 0, "HTTP[%s]: httpsmsc_receiver dying",
209  octstr_get_cstr(conn->id));
210 
211  conndata->shutdown = 1;
213 
214  /* unblock http_receive_result() if there are no open sends */
215  if (counter_value(conndata->open_sends) == 0)
217 
218  if (conndata->sender_thread != -1) {
221  }
222  if (conndata->send_cb_thread != -1) {
225  }
226 
227  mutex_lock(conn->flow_mutex);
228  conn->status = SMSCCONN_DEAD;
229  mutex_unlock(conn->flow_mutex);
230 
231  if (conndata->callbacks != NULL && conndata->callbacks->destroy != NULL)
232  conndata->callbacks->destroy(conn);
233  conn->data = NULL;
236 }
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:2757
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:1517
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:2878
volatile int shutdown
Definition: smsc_http_p.h:90
void http_destroy_cgiargs(List *args)
Definition: http.c:2817
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:2570
void http_close_port(int port)
Definition: http.c:2514
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:3458
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
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:110
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 402 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, msg_duplicate(), conndata::msg_to_send, octstr_get_cstr, and smsc_http_fn_callbacks::send_sms.

Referenced by smsc_http_create().

403 {
404  ConnData *conndata = conn->data;
405  Msg *sms;
406 
407 
408  /* don't crash if no send_sms handle defined */
409  if (!conndata || !conndata->callbacks->send_sms)
410  return -1;
411 
412  sms = msg_duplicate(msg);
413  /* convert character encoding if required */
414  if (conndata->alt_charset) {
415  /*
416  * Converted now to the target character set based on the
417  * one we got in the msg, which is either UTF-8 (our normal
418  * inter-box encoding), but may also be UCS-2, so beware.
419  * In addition, IF we convert to an "extra" encoding here
420  * we also revert the .coding vaue to DC_UNDEF, in order
421  * that all API specific code doesn't indicate an encoding
422  * which is no longer inside the payload here.
423  */
424  if (sms->sms.coding == DC_7BIT) {
425  if (charset_convert(sms->sms.msgdata, DEFAULT_CHARSET,
427  sms->sms.coding = DC_UNDEF;
428  } else {
429  error(0, "Failed to convert msgdata from charset <%s> to <%s>, will send as is.",
431  }
432  }
433  else if (sms->sms.coding == DC_UCS2) {
434  if (charset_convert(sms->sms.msgdata, DEFAULT_UCS2_CHARSET,
436  sms->sms.coding = DC_UNDEF;
437  } else {
438  error(0, "Failed to convert msgdata from charset <%s> to <%s>, will send as is.",
440  }
441  }
442  }
443 
445 
446  return 0;
447 }
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:102
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:103
#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:110
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
#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 312 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().

313 {
314  SMSCConn *conn = arg;
315  ConnData *conndata = conn->data;
316  Msg *msg;
317  int status;
318  List *headers;
319  Octstr *final_url, *body;
320 
321  /* Make sure we log into our own log-file if defined */
322  log_thread_to(conn->log_idx);
323 
325 
327  &final_url, &headers, &body);
328 
329  if (msg == NULL)
330  break; /* they told us to die, by unlocking */
331 
335 
336  /* Handle various states here. */
337 
338  /* request failed and we are not in shutdown mode */
339  if (status == -1 && conndata->shutdown == 0) {
340  error(0, "HTTP[%s]: Couldn't connect to SMS center."
341  "(retrying in %ld seconds) %ld.",
343  mutex_lock(conn->flow_mutex);
345  mutex_unlock(conn->flow_mutex);
346  /* XXX how should we know whether it's temp. error ?? */
348  /*
349  * Just sleep reconnect delay and set conn to ACTIVE again;
350  * otherwise if no pending request are here, we leave conn in
351  * RECONNECTING state for ever and no routing (trials) take place.
352  */
353  if (counter_value(conndata->open_sends) == 0) {
355  /* and now enable routing again */
356  mutex_lock(conn->flow_mutex);
357  conn->status = SMSCCONN_ACTIVE;
358  time(&conn->connect_time);
359  mutex_unlock(conn->flow_mutex);
360  /* tell bearerbox core that we are connected again */
361  bb_smscconn_connected(conn);
362  }
363  continue;
364  }
365  /* request failed and we *are* in shutdown mode, drop the message */
366  else if (status == -1 && conndata->shutdown == 1) {
368  }
369  /* request succeeded */
370  else {
371  /* we received a response, so this link is considered online again */
372  if (conn->status != SMSCCONN_ACTIVE) {
373  mutex_lock(conn->flow_mutex);
374  conn->status = SMSCCONN_ACTIVE;
375  time(&conn->connect_time);
376  mutex_unlock(conn->flow_mutex);
377  /* tell bearerbox core that we are connected again */
378  bb_smscconn_connected(conn);
379  }
380  conndata->callbacks->parse_reply(conn, msg, status, headers, body);
381  }
382 
383  http_destroy_headers(headers);
384  octstr_destroy(final_url);
385  octstr_destroy(body);
386  }
387  debug("smsc.http", 0, "HTTP[%s]: httpsmsc_send_cb dying",
388  octstr_get_cstr(conn->id));
389  conndata->shutdown = 1;
390 
392  warning(0, "HTTP[%s]: Shutdown while <%ld> requests are pending.",
394  }
395 }
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:2878
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:324
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:385
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:110
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ httpsmsc_sender()

static void httpsmsc_sender ( void *  arg)
static

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

243 {
244  SMSCConn *conn = arg;
245  ConnData *conndata = conn->data;
246  Msg *msg;
247  double delay = 0;
248 
249  /* Make sure we log into our own log-file if defined */
250  log_thread_to(conn->log_idx);
251 
252  if (conn->throughput) {
253  delay = 1.0 / conn->throughput;
254  }
255 
256  while (conndata->shutdown == 0) {
257  /* check if we can send ; otherwise block on semaphore */
260 
261  if (conndata->shutdown) {
264  break;
265  }
266 
268  if (msg == NULL)
269  break;
270 
271  /* obey throughput speed limit, if any */
272  if (conn->throughput > 0) {
274  }
276  if (conndata->callbacks->send_sms(conn, msg) == -1) {
280  }
281  }
282 
283  /* put outstanding sends back into global queue */
286 
287  /* if there no receiver shutdown */
288  if (conndata->port <= 0) {
289  /* unblock http_receive_result() if there are no open sends */
290  if (counter_value(conndata->open_sends) == 0)
292 
293  if (conndata->send_cb_thread != -1) {
296  }
297  mutex_lock(conn->flow_mutex);
298  conn->status = SMSCCONN_DEAD;
299  mutex_unlock(conn->flow_mutex);
300 
301  if (conndata->callbacks != NULL && conndata->callbacks->destroy != NULL)
302  conndata->callbacks->destroy(conn);
303  conn->data = NULL;
306  }
307 }
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:103
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:110
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 461 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().

462 {
463  ConnData *conndata = conn->data;
464 
465  if (conndata == NULL)
466  return 0;
467 
468  debug("httpsmsc_shutdown", 0, "HTTP[%s]: Shutting down",
469  octstr_get_cstr(conn->id));
470 
471  mutex_lock(conn->flow_mutex);
473 
474  conndata->shutdown = 1;
475 
476  if (conndata->port > 0)
479  if (conndata->receive_thread != -1)
481  if (conndata->sender_thread != -1)
483  mutex_unlock(conn->flow_mutex);
484 
485  return 0;
486 }
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:103
void http_close_port(int port)
Definition: http.c:2514
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 489 of file smsc_http.c.

References conndata::allow_ip, conndata::alt_charset, conndata::callbacks, cfg, 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::mobile_originated, 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, conndata::password, conndata::port, 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().

490 {
491  ConnData *conndata = NULL;
492  Octstr *type = NULL, *callbackname = NULL;
493  int ssl = 0; /* indicate if SSL-enabled server should be used */
494  long max_ps;
495 
496  if ((type = cfg_get(cfg, octstr_imm("system-type"))) == NULL) {
497  error(0, "HTTP[%s]: 'system-type' missing in smsc 'http' record.",
498  octstr_get_cstr(conn->id));
500  return -1;
501  }
502 
503  conndata = gw_malloc(sizeof(ConnData));
504  /* reset conndata */
505  memset(conndata, 0, sizeof(ConnData));
506 
507  conn->data = conndata;
508  conndata->http_ref = NULL;
509  conndata->data = NULL;
510 
511  if (cfg_get_integer(&conndata->port, cfg, octstr_imm("port")) == -1) {
512  warning(0, "HTTP[%s]: 'port' not set in smsc 'http' group.",
513  octstr_get_cstr(conn->id));
514  conndata->port = -1;
515  }
516 
517  conndata->allow_ip = cfg_get(cfg, octstr_imm("connect-allow-ip"));
518  conndata->send_url = cfg_get(cfg, octstr_imm("send-url"));
519  conndata->username = cfg_get(cfg, octstr_imm("smsc-username"));
520  conndata->password = cfg_get(cfg, octstr_imm("smsc-password"));
521  conndata->system_id = cfg_get(cfg, octstr_imm("system-id"));
522  cfg_get_bool(&conndata->no_sender, cfg, octstr_imm("no-sender"));
523  cfg_get_bool(&conndata->no_coding, cfg, octstr_imm("no-coding"));
524  cfg_get_bool(&conndata->no_sep, cfg, octstr_imm("no-sep"));
525  cfg_get_bool(&conndata->mobile_originated, cfg, octstr_imm("mobile-originated"));
526  cfg_get_bool(&ssl, cfg, octstr_imm("use-ssl"));
527  conndata->dlr_url = cfg_get(cfg, octstr_imm("dlr-url"));
528  conndata->alt_charset = cfg_get(cfg, octstr_imm("alt-charset"));
529 
530  if (cfg_get_integer(&max_ps, cfg, octstr_imm("max-pending-submits")) == -1 || max_ps < 1)
531  max_ps = 10;
533 
534  if (conndata->port <= 0 && conndata->send_url == NULL) {
535  error(0, "HTTP[%s]: Sender and receiver disabled. Dummy SMSC not allowed.",
536  octstr_get_cstr(conn->id));
537  goto error;
538  }
539  if (conndata->send_url == NULL) {
540  error(0, "HTTP[%s]: Sending not allowed. No 'send-url' specified.",
541  octstr_get_cstr(conn->id));
542  goto error;
543  }
544 
545  /* callback struct is always in the format: smsc_http_XXX_callback where XXX is our type */
546  callbackname = octstr_format("smsc_http_%S_callback", type);
547  /* try to find the struct */
548  if (gw_dlopen_get_symbol(NULL, octstr_get_cstr(callbackname), (void**)&conndata->callbacks) == -1 || conndata->callbacks == NULL) {
549  error(0, "HTTP[%s]: system-type '%s' unknown smsc 'http' record.",
551  goto error;
552  }
553 
554  if (conndata->callbacks != NULL && conndata->callbacks->init != NULL && conndata->callbacks->init(conn, cfg)) {
555  error(0, "HTTP[%s]: submodule '%s' init failed.", octstr_get_cstr(conn->id), octstr_get_cstr(type));
556  goto error;
557  }
558 
563 
564  conn->name = octstr_format("HTTP%s:%S:%d", (ssl?"S":""), type, conndata->port);
565 
566  if (conndata->send_url != NULL) {
567  conn->status = SMSCCONN_ACTIVE;
568  } else {
570  }
571 
572  conn->connect_time = time(NULL);
573 
574  conn->shutdown = httpsmsc_shutdown;
575  conn->queued = httpsmsc_queued;
576  conn->send_msg = httpsmsc_send;
577 
578  conndata->shutdown = 0;
579 
580  /* start receiver thread */
581  if (conndata->port > 0) {
582  if (http_open_port(conndata->port, ssl) == -1)
583  goto error;
585  goto error;
586  } else
587  conndata->receive_thread = -1;
588 
589  /* start sender threads */
590  if (conndata->send_url) {
591  if ((conndata->send_cb_thread =
592  gwthread_create(httpsmsc_send_cb, conn)) == -1)
593  goto error;
594  if ((conndata->sender_thread =
595  gwthread_create(httpsmsc_sender, conn)) == -1)
596  goto error;
597  }
598  else {
600  }
601 
602  info(0, "HTTP[%s]: Initiated and ready", octstr_get_cstr(conn->id));
603 
604  octstr_destroy(callbackname);
606  return 0;
607 
608 error:
609  error(0, "HTTP[%s]: Failed to create HTTP SMSC connection",
610  octstr_get_cstr(conn->id));
611 
612  if (conndata->callbacks != NULL && conndata->callbacks->destroy != NULL)
613  conndata->callbacks->destroy(conn);
614  conn->data = NULL;
617  conn->status = SMSCCONN_DEAD;
618  octstr_destroy(callbackname);
620  return -1;
621 }
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:113
static void httpsmsc_receiver(void *arg)
Definition: smsc_http.c:161
static void conndata_destroy(ConnData *conndata)
Definition: smsc_http.c:136
Octstr * alt_charset
Definition: smsc_http_p.h:102
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:101
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 * system_id
Definition: smsc_http_p.h:106
static Cfg * cfg
Definition: opensmppbox.c:95
#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:283
Counter * open_sends
Definition: smsc_http_p.h:95
int no_coding
Definition: smsc_http_p.h:100
List * msg_to_send
Definition: smsc_http_p.h:103
Counter * counter_create(void)
Definition: counter.c:94
static void httpsmsc_send_cb(void *arg)
Definition: smsc_http.c:312
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:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define gwthread_create(func, arg)
Definition: gwthread.h:90
int no_sender
Definition: smsc_http_p.h:99
static int httpsmsc_shutdown(SMSCConn *conn, int finish_sending)
Definition: smsc_http.c:461
int http_open_port(int port, int ssl)
Definition: http.c:2508
Semaphore * max_pending_sends
Definition: smsc_http_p.h:96
int cfg_get_bool(int *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:757
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:450
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:740
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:402
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:242
struct smsc_http_fn_callbacks * callbacks
Definition: smsc_http_p.h:110
Semaphore * semaphore_create(long n)
Definition: gw-semaphore.c:81
long send_cb_thread
Definition: smsc_http_p.h:88
int mobile_originated
Definition: smsc_http_p.h:107
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.