Kannel: Open Source WAP and SMS gateway  svn-r5335
smsc.c File Reference
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include "gwlib/gwlib.h"
#include "smsc.h"
#include "smsc_p.h"
#include "msg.h"

Go to the source code of this file.

Macros

#define MAX_READ_INTO_BUFFER   (1024)
 

Functions

static void smscenter_lock (SMSCenter *smsc)
 
static void smscenter_unlock (SMSCenter *smsc)
 
SMSCentersmscenter_construct (void)
 
void smscenter_destruct (SMSCenter *smsc)
 
int smscenter_submit_msg (SMSCenter *smsc, Msg *msg)
 
int smscenter_receive_msg (SMSCenter *smsc, Msg **msg)
 
int smscenter_pending_smsmessage (SMSCenter *smsc)
 
int smscenter_read_into_buffer (SMSCenter *smsc)
 
void smscenter_remove_from_buffer (SMSCenter *smsc, size_t n)
 
SMSCentersmsc_open (CfgGroup *grp)
 
int smsc_reopen (SMSCenter *smsc)
 
char * smsc_name (SMSCenter *smsc)
 
int smsc_close (SMSCenter *smsc)
 

Macro Definition Documentation

◆ MAX_READ_INTO_BUFFER

#define MAX_READ_INTO_BUFFER   (1024)

Definition at line 84 of file smsc.c.

Referenced by smscenter_read_into_buffer().

Function Documentation

◆ smsc_close()

int smsc_close ( SMSCenter smsc)

Definition at line 648 of file smsc.c.

References cimd_close(), emi_close(), ois_close(), sema_close(), SMSC_TYPE_CIMD, SMSC_TYPE_EMI_X25, SMSC_TYPE_OIS, SMSC_TYPE_SEMA_X28, smscenter_lock(), and SMSCenter::type.

Referenced by smscwrapper_destroy().

649 {
650  int errors = 0;
651 
652  if (smsc == NULL)
653  return 0;
654 
655  smscenter_lock(smsc);
656 
657  switch (smsc->type) {
658  case SMSC_TYPE_CIMD:
659  if (cimd_close(smsc) == -1)
660  errors = 1;
661  break;
662 
663  case SMSC_TYPE_EMI_X25:
664  if (emi_close(smsc) == -1)
665  errors = 1;
666  break;
667 
668  case SMSC_TYPE_SEMA_X28:
669  if (sema_close(smsc) == -1)
670  errors = 1;
671  break;
672 
673  case SMSC_TYPE_OIS:
674  if (ois_close(smsc) == -1)
675  errors = 1;
676  break;
677 
678  /* add new SMSCes here */
679 
680  default: /* Unknown SMSC type */
681  break;
682  }
683  /*
684  smsc->type = SMSC_TYPE_DELETED;
685  smscenter_unlock(smsc);
686  */
687  if (errors)
688  return -1;
689 
690  return 0;
691 }
int ois_close(SMSCenter *smsc)
Definition: smsc_ois.c:263
int sema_close(SMSCenter *smsc)
Definition: smsc_sema.c:199
static void smscenter_lock(SMSCenter *smsc)
Definition: smsc.c:422
int emi_close(SMSCenter *smsc)
Definition: smsc_emi_x25.c:194
int cimd_close(SMSCenter *smsc)
Definition: smsc_cimd.c:228
int type
Definition: smsc_p.h:93

◆ smsc_name()

char* smsc_name ( SMSCenter smsc)

Definition at line 643 of file smsc.c.

References SMSCenter::name.

Referenced by smsc_wrapper_create().

644 {
645  return smsc->name;
646 }
char name[1024]
Definition: smsc_p.h:96

◆ smsc_open()

SMSCenter* smsc_open ( CfgGroup grp)

Definition at line 444 of file smsc.c.

References allow_ip, SMSCenter::allowed_prefix, SMSCenter::alt_charset, cfg_get, cfg_get_integer(), cimd_open(), SMSCenter::denied_prefix, emi_open(), error(), host, octstr_compare(), octstr_create, octstr_destroy(), octstr_get_cstr, octstr_imm(), octstr_parse_long(), ois_open(), our_port, password, port, SMSCenter::preferred_prefix, sema_open(), SMSC_TYPE_CIMD, SMSC_TYPE_EMI_X25, SMSC_TYPE_OIS, SMSC_TYPE_SEMA_X28, type, and username.

Referenced by smsc_wrapper_create().

445 {
446  SMSCenter *smsc;
447  Octstr *type, *host, *username, *password, *phone, *device;
448  Octstr *preferred_prefix, *allowed_prefix, *denied_prefix;
449  Octstr *alt_chars, *allow_ip;
450  Octstr *sema_smscnua, *sema_homenua, *sema_report;
451  Octstr *sender_prefix;
452 
453  long iwaitreport;
454  long port, receive_port, our_port;
455  long keepalive;
456  long ois_debug;
457  long alt_dcs;
458  int typeno;
459 
460 
461  type = cfg_get(grp, octstr_imm("smsc"));
462  if (type == NULL) {
463  error(0, "Required field 'smsc' missing for smsc group.");
464  return NULL;
465  }
466  if (octstr_compare(type, octstr_imm("cimd")) == 0)
467  typeno = SMSC_TYPE_CIMD;
468  else if (octstr_compare(type, octstr_imm("emi_x25")) == 0)
469  typeno = SMSC_TYPE_EMI_X25;
470  else if (octstr_compare(type, octstr_imm("sema")) == 0)
471  typeno = SMSC_TYPE_SEMA_X28;
472  else if (octstr_compare(type, octstr_imm("ois")) == 0)
473  typeno = SMSC_TYPE_OIS;
474  else {
475  error(0, "Unknown SMSC type '%s'", octstr_get_cstr(type));
477  return NULL;
478  }
479 
480  host = cfg_get(grp, octstr_imm("host"));
481  if (cfg_get_integer(&port, grp, octstr_imm("port")) == -1)
482  port = 0;
483  if (cfg_get_integer(&receive_port, grp, octstr_imm("receive-port")) == -1)
484  receive_port = 0;
485  if (cfg_get_integer(&our_port, grp, octstr_imm("our-port")) == -1)
486  our_port = 0;
487  username = cfg_get(grp, octstr_imm("smsc-username"));
488  password = cfg_get(grp, octstr_imm("smsc-password"));
489  phone = cfg_get(grp, octstr_imm("phone"));
490  device = cfg_get(grp, octstr_imm("device"));
491  preferred_prefix = cfg_get(grp, octstr_imm("preferred-prefix"));
492  allowed_prefix = cfg_get(grp, octstr_imm("allowed-prefix"));
493  denied_prefix = cfg_get(grp, octstr_imm("denied-prefix"));
494  alt_chars = cfg_get(grp, octstr_imm("alt-charset"));
495 
496  allow_ip = cfg_get(grp, octstr_imm("connect-allow-ip"));
497 
498  sema_smscnua = cfg_get(grp, octstr_imm("smsc_nua"));
499  sema_homenua = cfg_get(grp, octstr_imm("home_nua"));
500  sema_report = cfg_get(grp, octstr_imm("wait_report"));
501  if (sema_report == NULL)
502  iwaitreport = 1;
503  else
504  octstr_parse_long(&iwaitreport, sema_report, 0, 0);
505 
506  if (cfg_get_integer(&keepalive, grp, octstr_imm("keepalive")) == -1)
507  keepalive = 0;
508 
509  if (cfg_get_integer(&alt_dcs, grp, octstr_imm("alt-dcs")) == -1)
510  alt_dcs = 0;
511  if (alt_dcs > 1)
512  alt_dcs = 1;
513 
514  if (cfg_get_integer(&ois_debug, grp, octstr_imm("ois-debug-level")) == -1)
515  ois_debug = 0;
516 
517  sender_prefix = cfg_get(grp, octstr_imm("sender-prefix"));
518  if (sender_prefix == NULL)
519  sender_prefix = octstr_create("never");
520 
521  smsc = NULL;
522 
523  switch (typeno) {
524  case SMSC_TYPE_CIMD:
525  if (host == NULL || port == 0 || username == NULL || password == NULL)
526  error(0, "Required field missing for CIMD center.");
527  else
529  port,
532  break;
533 
534  case SMSC_TYPE_EMI_X25:
535  if (phone == NULL || device == NULL || username == NULL ||
536  password == NULL)
537  error(0, "Required field missing for EMI_X25 center.");
538  else
539  smsc = emi_open(octstr_get_cstr(phone),
540  octstr_get_cstr(device),
543  break;
544 
545  case SMSC_TYPE_SEMA_X28:
546  if (device == NULL || sema_smscnua == NULL || sema_homenua == NULL)
547  error(0, "Required field missing for SEMA center.");
548  else
549  smsc = sema_open(octstr_get_cstr(sema_smscnua),
550  octstr_get_cstr(sema_homenua),
551  octstr_get_cstr(device),
552  iwaitreport);
553  break;
554 
555  case SMSC_TYPE_OIS:
556  if (host == NULL || port == 0 || receive_port == 0)
557  error(0, "Required field missing for OIS center.");
558  else
559  smsc = ois_open(receive_port,
561  port,
562  ois_debug);
563  break;
564 
565  /* add new SMSCes here */
566 
567  default: /* Unknown SMSC type */
568  break;
569  }
570 
571  if (smsc != NULL) {
572  if (cfg_get_integer(&smsc->alt_charset, grp,
573  octstr_imm("alt-charset")) == -1)
574  smsc->alt_charset = 0;
575  if (preferred_prefix == NULL)
576  smsc->preferred_prefix = NULL;
577  else
578  smsc->preferred_prefix =
579  gw_strdup(octstr_get_cstr(preferred_prefix));
580  if (allowed_prefix == NULL)
581  smsc->allowed_prefix = NULL;
582  else
583  smsc->allowed_prefix = gw_strdup(octstr_get_cstr(allowed_prefix));
584  if (denied_prefix == NULL)
585  smsc->denied_prefix = NULL;
586  else
587  smsc->denied_prefix = gw_strdup(octstr_get_cstr(denied_prefix));
588  }
589 
594  octstr_destroy(phone);
595  octstr_destroy(device);
596  octstr_destroy(preferred_prefix);
597  octstr_destroy(denied_prefix);
598  octstr_destroy(allowed_prefix);
599  octstr_destroy(alt_chars);
601  octstr_destroy(sema_smscnua);
602  octstr_destroy(sema_homenua);
603  octstr_destroy(sema_report);
604  octstr_destroy(sender_prefix);
605  return smsc;
606 }
void error(int err, const char *fmt,...)
Definition: log.c:648
static long our_port
Definition: radius_acct.c:87
Definition: http.c:2014
char * preferred_prefix
Definition: smsc_p.h:100
int type
Definition: smsc_cimd2.c:215
static Octstr * host
Definition: fakesmsc.c:122
#define cfg_get(grp, varname)
Definition: cfg.h:86
SMSCenter * sema_open(char *smscnua, char *homenua, char *serialdevice, int waitreport)
Definition: smsc_sema.c:113
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
unsigned char * username
Definition: test_cimd2.c:99
static int port
Definition: fakesmsc.c:121
unsigned char * password
Definition: test_cimd2.c:100
SMSCenter * emi_open(char *phonenum, char *serialdevice, char *username, char *password)
Definition: smsc_emi_x25.c:153
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
SMSCenter * cimd_open(char *hostname, int port, char *username, char *password)
Definition: smsc_cimd.c:174
char * allowed_prefix
Definition: smsc_p.h:101
SMSCenter * ois_open(int receiveport, const char *hostname, int port, int debug_level)
Definition: smsc_ois.c:222
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
char * denied_prefix
Definition: smsc_p.h:102
static Octstr * allow_ip
Definition: bb_udp.c:111
Definition: octstr.c:118
int cfg_get_integer(long *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:742
long alt_charset
Definition: smsc_p.h:106
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:749
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871

◆ smsc_reopen()

int smsc_reopen ( SMSCenter smsc)

Definition at line 610 of file smsc.c.

References cimd_reopen(), emi_reopen(), SMSCenter::killed, ois_reopen(), sema_reopen(), SMSC_TYPE_CIMD, SMSC_TYPE_EMI_X25, SMSC_TYPE_OIS, SMSC_TYPE_SEMA_X28, smscenter_lock(), smscenter_unlock(), and SMSCenter::type.

Referenced by reconnect().

611 {
612  int ret;
613 
614  if (smsc->killed)
615  return -2;
616 
617  smscenter_lock(smsc);
618 
619  switch (smsc->type) {
620  case SMSC_TYPE_CIMD:
621  ret = cimd_reopen(smsc);
622  break;
623  case SMSC_TYPE_EMI_X25:
624  ret = emi_reopen(smsc);
625  break;
626  case SMSC_TYPE_SEMA_X28:
627  ret = sema_reopen(smsc);
628  break;
629  case SMSC_TYPE_OIS:
630  ret = ois_reopen(smsc);
631  break;
632  /* add new SMSCes here */
633  default: /* Unknown SMSC type */
634  ret = -2; /* no use */
635  }
636 
637  smscenter_unlock(smsc);
638  return ret;
639 }
int sema_reopen(SMSCenter *smsc)
Definition: smsc_sema.c:162
volatile sig_atomic_t killed
Definition: smsc_p.h:112
int ois_reopen(SMSCenter *smsc)
Definition: smsc_ois.c:287
int cimd_reopen(SMSCenter *smsc)
Definition: smsc_cimd.c:210
static void smscenter_lock(SMSCenter *smsc)
Definition: smsc.c:422
int emi_reopen(SMSCenter *smsc)
Definition: smsc_emi_x25.c:183
int type
Definition: smsc_p.h:93
static void smscenter_unlock(SMSCenter *smsc)
Definition: smsc.c:433

◆ smscenter_construct()

SMSCenter* smscenter_construct ( void  )

Definition at line 101 of file smsc.c.

References SMSCenter::allowed_prefix, SMSCenter::alt_charset, SMSCenter::buffer, SMSCenter::buflen, SMSCenter::bufsize, SMSCenter::cimd_hostname, SMSCenter::cimd_password, SMSCenter::cimd_port, SMSCenter::cimd_username, SMSCenter::denied_prefix, SMSCenter::emi_password, SMSCenter::emi_phonenum, SMSCenter::emi_serialdevice, SMSCenter::emi_username, SMSCenter::hostname, SMSCenter::id, SMSCenter::keepalive, SMSCenter::killed, SMSCenter::mutex, mutex_create, SMSCenter::name, SMSCenter::ois_ack_debt, SMSCenter::ois_alive, SMSCenter::ois_alive2, SMSCenter::ois_buffer, SMSCenter::ois_buflen, SMSCenter::ois_bufsize, SMSCenter::ois_flags, SMSCenter::ois_listening_socket, SMSCenter::ois_received_mo, SMSCenter::ois_socket, SMSCenter::port, SMSCenter::preferred_prefix, SMSCenter::sema_fd, SMSCenter::sema_homenua, SMSCenter::sema_serialdevice, SMSCenter::sema_smscnua, SMSC_TYPE_DELETED, SMSCenter::socket, and SMSCenter::type.

Referenced by cimd_open(), emi_open(), ois_open(), and sema_open().

102 {
103  SMSCenter *smsc;
104  static int next_id = 1;
105 
106  smsc = gw_malloc(sizeof(SMSCenter));
107 
108  smsc->killed = 0;
109  smsc->type = SMSC_TYPE_DELETED;
110  smsc->preferred_prefix = NULL;
111  smsc->allowed_prefix = NULL;
112  smsc->denied_prefix = NULL;
113  smsc->alt_charset = 0;
114  smsc->keepalive = 0;
115 
116  smsc->mutex = mutex_create();
117 
118  sprintf(smsc->name, "Unknown SMSC");
119  smsc->id = next_id++;
120 
121  /* FAKE */
122  smsc->hostname = NULL;
123  smsc->port = -1;
124  smsc->socket = -1;
125 
126  /* CIMD */
127  smsc->cimd_hostname = NULL;
128  smsc->cimd_port = -1;
129  smsc->cimd_username = NULL;
130  smsc->cimd_password = NULL;
131 
132  /* EMI_X25 */
133  smsc->emi_phonenum = NULL;
134  smsc->emi_serialdevice = NULL;
135  smsc->emi_username = NULL;
136  smsc->emi_password = NULL;
137 
138  /* SEMA SMS2000 */
139  smsc->sema_smscnua = NULL;
140  smsc->sema_homenua = NULL;
141  smsc->sema_serialdevice = NULL;
142  smsc->sema_fd = -1;
143 
144  /* SEMA SMS2000 OIS X.25 */
145  smsc->ois_alive = 0;
146  smsc->ois_alive2 = 0;
147  smsc->ois_received_mo = NULL;
148  smsc->ois_ack_debt = 0;
149  smsc->ois_flags = 0;
150  smsc->ois_listening_socket = -1;
151  smsc->ois_socket = -1;
152  smsc->ois_buflen = 0;
153  smsc->ois_bufsize = 0;
154  smsc->ois_buffer = 0;
155 
156  /* add new SMSCes here */
157 
158  /* Memory */
159  smsc->buflen = 0;
160  smsc->bufsize = 10*1024;
161  smsc->buffer = gw_malloc(smsc->bufsize);
162  memset(smsc->buffer, 0, smsc->bufsize);
163 
164  return smsc;
165 }
int ois_ack_debt
Definition: smsc_p.h:174
size_t bufsize
Definition: smsc_p.h:185
volatile sig_atomic_t killed
Definition: smsc_p.h:112
int id
Definition: smsc_p.h:97
char * preferred_prefix
Definition: smsc_p.h:100
char * emi_password
Definition: smsc_p.h:152
size_t ois_bufsize
Definition: smsc_p.h:179
int ois_flags
Definition: smsc_p.h:175
int sema_fd
Definition: smsc_p.h:167
#define mutex_create()
Definition: thread.h:96
size_t buflen
Definition: smsc_p.h:186
Mutex * mutex
Definition: smsc_p.h:109
void * ois_received_mo
Definition: smsc_p.h:173
int port
Definition: smsc_p.h:122
int ois_listening_socket
Definition: smsc_p.h:176
char * allowed_prefix
Definition: smsc_p.h:101
char * emi_phonenum
Definition: smsc_p.h:147
char * emi_username
Definition: smsc_p.h:151
char * sema_smscnua
Definition: smsc_p.h:163
char * emi_serialdevice
Definition: smsc_p.h:148
char * denied_prefix
Definition: smsc_p.h:102
int socket
Definition: smsc_p.h:115
char * sema_serialdevice
Definition: smsc_p.h:165
size_t ois_buflen
Definition: smsc_p.h:180
int cimd_port
Definition: smsc_p.h:138
char * ois_buffer
Definition: smsc_p.h:178
char * cimd_hostname
Definition: smsc_p.h:137
time_t ois_alive2
Definition: smsc_p.h:172
char * sema_homenua
Definition: smsc_p.h:164
int type
Definition: smsc_p.h:93
char * buffer
Definition: smsc_p.h:184
char * cimd_username
Definition: smsc_p.h:139
long alt_charset
Definition: smsc_p.h:106
char name[1024]
Definition: smsc_p.h:96
char * hostname
Definition: smsc_p.h:121
char * cimd_password
Definition: smsc_p.h:140
int ois_socket
Definition: smsc_p.h:177
int keepalive
Definition: smsc_p.h:118
time_t ois_alive
Definition: smsc_p.h:171

◆ smscenter_destruct()

void smscenter_destruct ( SMSCenter smsc)

Definition at line 168 of file smsc.c.

References SMSCenter::buffer, SMSCenter::cimd_hostname, SMSCenter::cimd_password, SMSCenter::cimd_username, SMSCenter::emi_password, SMSCenter::emi_phonenum, SMSCenter::emi_serialdevice, SMSCenter::emi_username, SMSCenter::hostname, SMSCenter::mutex, mutex_destroy(), SMSCenter::ois_buffer, ois_delete_queue(), SMSCenter::sema_homenua, SMSCenter::sema_serialdevice, and SMSCenter::sema_smscnua.

Referenced by cimd_open(), emi_open(), ois_open(), and sema_open().

169 {
170  if (smsc == NULL)
171  return;
172 
173  /* FAKE */
174  gw_free(smsc->hostname);
175 
176  /* CIMD */
177  gw_free(smsc->cimd_hostname);
178  gw_free(smsc->cimd_username);
179  gw_free(smsc->cimd_password);
180 
181  /* EMI_X25 */
182  gw_free(smsc->emi_phonenum);
183  gw_free(smsc->emi_serialdevice);
184  gw_free(smsc->emi_username);
185  gw_free(smsc->emi_password);
186 
187  /* SEMA */
188  gw_free(smsc->sema_smscnua);
189  gw_free(smsc->sema_homenua);
190  gw_free(smsc->sema_serialdevice);
191 
192  /* OIS */
193  ois_delete_queue(smsc);
194  gw_free(smsc->ois_buffer);
195 
196  /* add new SMSCes here */
197 
198  /* Other fields */
199  mutex_destroy(smsc->mutex);
200 
201  /* Memory */
202  gw_free(smsc->buffer);
203  gw_free(smsc);
204 }
char * emi_password
Definition: smsc_p.h:152
Mutex * mutex
Definition: smsc_p.h:109
char * emi_phonenum
Definition: smsc_p.h:147
char * emi_username
Definition: smsc_p.h:151
char * sema_smscnua
Definition: smsc_p.h:163
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
char * emi_serialdevice
Definition: smsc_p.h:148
char * sema_serialdevice
Definition: smsc_p.h:165
char * ois_buffer
Definition: smsc_p.h:178
char * cimd_hostname
Definition: smsc_p.h:137
char * sema_homenua
Definition: smsc_p.h:164
void ois_delete_queue(SMSCenter *smsc)
Definition: smsc_ois.c:423
char * buffer
Definition: smsc_p.h:184
char * cimd_username
Definition: smsc_p.h:139
char * hostname
Definition: smsc_p.h:121
char * cimd_password
Definition: smsc_p.h:140

◆ smscenter_lock()

static void smscenter_lock ( SMSCenter smsc)
static

Definition at line 422 of file smsc.c.

References error(), SMSCenter::mutex, mutex_lock, SMSC_TYPE_DELETED, and SMSCenter::type.

Referenced by smsc_close(), smsc_reopen(), smscenter_pending_smsmessage(), smscenter_receive_msg(), and smscenter_submit_msg().

423 {
424  if (smsc->type == SMSC_TYPE_DELETED)
425  error(0, "smscenter_lock called on DELETED SMSC.");
426  mutex_lock(smsc->mutex);
427 }
void error(int err, const char *fmt,...)
Definition: log.c:648
Mutex * mutex
Definition: smsc_p.h:109
int type
Definition: smsc_p.h:93
#define mutex_lock(m)
Definition: thread.h:130

◆ smscenter_pending_smsmessage()

int smscenter_pending_smsmessage ( SMSCenter smsc)

Definition at line 302 of file smsc.c.

References cimd_pending_smsmessage(), emi_pending_smsmessage(), error(), ois_pending_smsmessage(), sema_pending_smsmessage(), SMSC_TYPE_CIMD, SMSC_TYPE_EMI_X25, SMSC_TYPE_OIS, SMSC_TYPE_SEMA_X28, smscenter_lock(), smscenter_unlock(), and SMSCenter::type.

Referenced by sms_receive().

303 {
304  int ret;
305 
306  smscenter_lock(smsc);
307 
308  switch (smsc->type) {
309 
310  case SMSC_TYPE_CIMD:
311  ret = cimd_pending_smsmessage(smsc);
312  if (ret == -1)
313  goto error;
314  break;
315 
316  case SMSC_TYPE_EMI_X25:
317  ret = emi_pending_smsmessage(smsc);
318  if (ret == -1)
319  goto error;
320  break;
321 
322  case SMSC_TYPE_SEMA_X28:
323  ret = sema_pending_smsmessage(smsc);
324  if (ret == -1)
325  goto error;
326  break;
327 
328  case SMSC_TYPE_OIS:
329  ret = ois_pending_smsmessage(smsc);
330  if (ret == -1)
331  goto error;
332  break;
333 
334  default:
335  goto error;
336  }
337 
338  smscenter_unlock(smsc);
339  return ret;
340 
341 error:
342  error(0, "smscenter_pending_smsmessage is failing");
343  smscenter_unlock(smsc);
344  return -1;
345 }
int ois_pending_smsmessage(SMSCenter *smsc)
Definition: smsc_ois.c:318
void error(int err, const char *fmt,...)
Definition: log.c:648
int emi_pending_smsmessage(SMSCenter *smsc)
Definition: smsc_emi_x25.c:319
int sema_pending_smsmessage(SMSCenter *smsc)
Definition: smsc_sema.c:365
static void smscenter_lock(SMSCenter *smsc)
Definition: smsc.c:422
int type
Definition: smsc_p.h:93
int cimd_pending_smsmessage(SMSCenter *smsc)
Definition: smsc_cimd.c:264
static void smscenter_unlock(SMSCenter *smsc)
Definition: smsc.c:433

◆ smscenter_read_into_buffer()

int smscenter_read_into_buffer ( SMSCenter smsc)

Definition at line 348 of file smsc.c.

References SMSCenter::buffer, SMSCenter::buflen, SMSCenter::bufsize, error(), SMSCenter::hostname, MAX_READ_INTO_BUFFER, SMSCenter::port, and SMSCenter::socket.

Referenced by cimd_open_connection(), cimd_pending_smsmessage(), connect_tcpip(), expect_acknowledge(), and ois_read_into_buffer().

349 {
350  char *p;
351  int ret;
352  fd_set read_fd;
353  struct timeval tv, tvinit;
354  size_t bytes_read;
355 
356  tvinit.tv_sec = 0;
357  tvinit.tv_usec = 1000;
358 
359  bytes_read = 0;
360  for (;;) {
361  FD_ZERO(&read_fd);
362  FD_SET(smsc->socket, &read_fd);
363  tv = tvinit;
364  ret = select(smsc->socket + 1, &read_fd, NULL, NULL, &tv);
365  if (ret == -1) {
366  if (errno == EINTR) goto got_data;
367  if (errno == EAGAIN) goto got_data;
368  error(errno, "Error doing select for socket");
369  goto error;
370  } else if (ret == 0)
371  goto got_data;
372 
373  if (smsc->buflen == smsc->bufsize) {
374  p = gw_realloc(smsc->buffer, smsc->bufsize * 2);
375  smsc->buffer = p;
376  smsc->bufsize *= 2;
377  }
378 
379  ret = read(smsc->socket,
380  smsc->buffer + smsc->buflen,
381  1);
382  if (ret == -1) {
383  error(errno, "Reading from `%s' port `%d' failed.",
384  smsc->hostname, smsc->port);
385  goto error;
386  }
387  if (ret == 0)
388  goto eof;
389  smsc->buflen += ret;
390  bytes_read += ret;
391  if (bytes_read >= MAX_READ_INTO_BUFFER)
392  break;
393  }
394 
395 eof:
396  ret = 0;
397  goto unblock;
398 
399 got_data:
400  ret = 1;
401  goto unblock;
402 
403 error:
404  ret = -1;
405  goto unblock;
406 
407 unblock:
408  return ret;
409 }
void error(int err, const char *fmt,...)
Definition: log.c:648
size_t bufsize
Definition: smsc_p.h:185
size_t buflen
Definition: smsc_p.h:186
int port
Definition: smsc_p.h:122
#define MAX_READ_INTO_BUFFER
Definition: smsc.c:84
int socket
Definition: smsc_p.h:115
char * buffer
Definition: smsc_p.h:184
char * hostname
Definition: smsc_p.h:121

◆ smscenter_receive_msg()

int smscenter_receive_msg ( SMSCenter smsc,
Msg **  msg 
)

Definition at line 251 of file smsc.c.

References cimd_receive_msg(), emi_receive_msg(), error(), msg, ois_receive_msg(), sema_receive_msg(), SMSC_TYPE_CIMD, SMSC_TYPE_EMI_X25, SMSC_TYPE_OIS, SMSC_TYPE_SEMA_X28, smscenter_lock(), smscenter_unlock(), and SMSCenter::type.

Referenced by sms_receive().

252 {
253  int ret;
254 
255  smscenter_lock(smsc);
256 
257  switch (smsc->type) {
258 
259  case SMSC_TYPE_CIMD:
260  ret = cimd_receive_msg(smsc, msg);
261  if (ret == -1)
262  goto error;
263  break;
264 
265  case SMSC_TYPE_EMI_X25:
266  ret = emi_receive_msg(smsc, msg);
267  if (ret == -1)
268  goto error;
269  break;
270 
271  case SMSC_TYPE_OIS:
272  ret = ois_receive_msg(smsc, msg);
273  if (ret == -1)
274  goto error;
275  break;
276 
277 
278  case SMSC_TYPE_SEMA_X28:
279  ret = sema_receive_msg(smsc, msg);
280  if (ret == -1)
281  goto error;
282  break;
283 
284  default:
285  goto error;
286 
287  }
288  smscenter_unlock(smsc);
289 
290  /* If the SMSC didn't set the timestamp, set it here. */
291  if (ret == 1 && msg_type(*msg) == sms && (*msg)->sms.time == 0)
292  time(&(*msg)->sms.time);
293 
294  return ret;
295 
296 error:
297  smscenter_unlock(smsc);
298  return -1;
299 }
void error(int err, const char *fmt,...)
Definition: log.c:648
int sema_receive_msg(SMSCenter *smsc, Msg **msg)
Definition: smsc_sema.c:323
int ois_receive_msg(SMSCenter *smsc, Msg **msg)
Definition: smsc_ois.c:396
int cimd_receive_msg(SMSCenter *smsc, Msg **msg)
Definition: smsc_cimd.c:470
msg_type
Definition: msg.h:73
int emi_receive_msg(SMSCenter *smsc, Msg **tmsg)
Definition: smsc_emi_x25.c:408
static void smscenter_lock(SMSCenter *smsc)
Definition: smsc.c:422
int type
Definition: smsc_p.h:93
static void smscenter_unlock(SMSCenter *smsc)
Definition: smsc.c:433
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ smscenter_remove_from_buffer()

void smscenter_remove_from_buffer ( SMSCenter smsc,
size_t  n 
)

Definition at line 412 of file smsc.c.

References SMSCenter::buffer, and SMSCenter::buflen.

Referenced by cimd_open_connection(), cimd_pending_smsmessage(), cimd_receive_msg(), connect_tcpip(), expect_acknowledge(), ois_close(), ois_extract_line_from_buffer(), and ois_extract_msg_from_buffer().

413 {
414  memmove(smsc->buffer, smsc->buffer + n, smsc->buflen - n);
415  smsc->buflen -= n;
416 }
size_t buflen
Definition: smsc_p.h:186
char * buffer
Definition: smsc_p.h:184

◆ smscenter_submit_msg()

int smscenter_submit_msg ( SMSCenter smsc,
Msg msg 
)

Definition at line 209 of file smsc.c.

References cimd_submit_msg(), emi_submit_msg(), error(), msg, ois_submit_msg(), sema_submit_msg(), SMSC_TYPE_CIMD, SMSC_TYPE_EMI_X25, SMSC_TYPE_OIS, SMSC_TYPE_SEMA_X28, smscenter_lock(), smscenter_unlock(), and SMSCenter::type.

Referenced by sms_send().

210 {
211  smscenter_lock(smsc);
212 
213  switch (smsc->type) {
214 
215  case SMSC_TYPE_CIMD:
216  if (cimd_submit_msg(smsc, msg) == -1)
217  goto error;
218  break;
219 
220  case SMSC_TYPE_EMI_X25:
221  if (emi_submit_msg(smsc, msg) == -1)
222  goto error;
223  break;
224 
225  case SMSC_TYPE_SEMA_X28:
226  if (sema_submit_msg(smsc, msg) == -1)
227  goto error;
228  break;
229 
230  case SMSC_TYPE_OIS:
231  if (ois_submit_msg(smsc, msg) == -1)
232  goto error;
233  break;
234 
235  /* add new SMSCes here */
236 
237  default:
238  goto error;
239  }
240 
241  smscenter_unlock(smsc);
242  return 0;
243 
244 error:
245  smscenter_unlock(smsc);
246  return -1;
247 }
void error(int err, const char *fmt,...)
Definition: log.c:648
int cimd_submit_msg(SMSCenter *smsc, Msg *msg)
Definition: smsc_cimd.c:358
int ois_submit_msg(SMSCenter *smsc, const Msg *msg)
Definition: smsc_ois.c:346
int emi_submit_msg(SMSCenter *smsc, Msg *omsg)
Definition: smsc_emi_x25.c:372
int sema_submit_msg(SMSCenter *smsc, Msg *msg)
Definition: smsc_sema.c:210
static void smscenter_lock(SMSCenter *smsc)
Definition: smsc.c:422
int type
Definition: smsc_p.h:93
static void smscenter_unlock(SMSCenter *smsc)
Definition: smsc.c:433
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ smscenter_unlock()

static void smscenter_unlock ( SMSCenter smsc)
static

Definition at line 433 of file smsc.c.

References SMSCenter::mutex, and mutex_unlock.

Referenced by smsc_reopen(), smscenter_pending_smsmessage(), smscenter_receive_msg(), and smscenter_submit_msg().

434 {
435  mutex_unlock(smsc->mutex);
436 }
#define mutex_unlock(m)
Definition: thread.h:136
Mutex * mutex
Definition: smsc_p.h:109
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.