Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
shared.c File Reference
#include <errno.h>
#include <sys/unistd.h>
#include <libxml/xmlversion.h>
#include "gwlib/gwlib.h"
#include "shared.h"

Go to the source code of this file.

Functions

Connectionconnect_to_bearerbox_real (Octstr *host, int port, int ssl, Octstr *our_host)
 
void connect_to_bearerbox (Octstr *host, int port, int ssl, Octstr *our_host)
 
void close_connection_to_bearerbox_real (Connection *conn)
 
void close_connection_to_bearerbox (void)
 
void write_to_bearerbox_real (Connection *conn, Msg *pmsg)
 
void write_to_bearerbox (Msg *pmsg)
 
int deliver_to_bearerbox_real (Connection *conn, Msg *msg)
 
int deliver_to_bearerbox (Msg *msg)
 
int read_from_bearerbox_real (Connection *conn, Msg **msg, double seconds)
 
int read_from_bearerbox (Msg **msg, double seconds)
 
Octstrparse_date (Octstr *date)
 
int restart_box (char **argv)
 

Variables

enum program_status program_status = starting_up
 
static Connectionbb_conn
 

Function Documentation

void close_connection_to_bearerbox ( void  )

Definition at line 122 of file shared.c.

References close_connection_to_bearerbox_real().

Referenced by main(), and run_connects().

123 {
125  bb_conn = NULL;
126 }
void close_connection_to_bearerbox_real(Connection *conn)
Definition: shared.c:116
static Connection * bb_conn
Definition: shared.c:80
void close_connection_to_bearerbox_real ( Connection conn)

Definition at line 116 of file shared.c.

References conn_destroy().

Referenced by close_connection_to_bearerbox().

117 {
118  conn_destroy(conn);
119 }
void conn_destroy(Connection *conn)
Definition: conn.c:619
void connect_to_bearerbox ( Octstr host,
int  port,
int  ssl,
Octstr our_host 
)

Definition at line 108 of file shared.c.

References connect_to_bearerbox_real(), and panic.

Referenced by main(), and run_connects().

109 {
110  bb_conn = connect_to_bearerbox_real(host, port, ssl, our_host);
111  if (bb_conn == NULL)
112  panic(0, "Couldn't connect to the bearerbox.");
113 }
Definition: http.c:1998
Connection * connect_to_bearerbox_real(Octstr *host, int port, int ssl, Octstr *our_host)
Definition: shared.c:83
int ssl
#define panic
Definition: log.h:87
static Connection * bb_conn
Definition: shared.c:80
Connection* connect_to_bearerbox_real ( Octstr host,
int  port,
int  ssl,
Octstr our_host 
)

Definition at line 83 of file shared.c.

References conn_open_tcp(), info(), and octstr_get_cstr.

Referenced by connect_to_bearerbox().

84 {
85  Connection *conn;
86 
87 #ifdef HAVE_LIBSSL
88  if (ssl)
89  conn = conn_open_ssl(host, port, NULL, our_host);
90  /* XXX add certkeyfile to be given to conn_open_ssl */
91  else
92 #endif /* HAVE_LIBSSL */
93  conn = conn_open_tcp(host, port, our_host);
94  if (conn == NULL)
95  return NULL;
96 
97  if (ssl)
98  info(0, "Connected to bearerbox at %s port %d using SSL.",
99  octstr_get_cstr(host), port);
100  else
101  info(0, "Connected to bearerbox at %s port %d.",
102  octstr_get_cstr(host), port);
103 
104  return conn;
105 }
void info(int err, const char *fmt,...)
Definition: log.c:636
Definition: http.c:1998
Connection * conn_open_tcp(Octstr *host, int port, Octstr *our_host)
Definition: conn.c:488
int ssl
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
int deliver_to_bearerbox ( Msg msg)

Definition at line 166 of file shared.c.

References deliver_to_bearerbox_real().

Referenced by send_message().

167 {
168  return deliver_to_bearerbox_real(bb_conn, msg);
169 }
int deliver_to_bearerbox_real(Connection *conn, Msg *msg)
Definition: shared.c:148
static Connection * bb_conn
Definition: shared.c:80
int deliver_to_bearerbox_real ( Connection conn,
Msg msg 
)

Definition at line 148 of file shared.c.

References conn_write_withlen(), error(), msg_destroy(), msg_pack(), and octstr_destroy().

Referenced by deliver_to_bearerbox().

149 {
150 
151  Octstr *pack;
152 
153  pack = msg_pack(msg);
154  if (conn_write_withlen(conn, pack) == -1) {
155  error(0, "Couldn't deliver Msg to bearerbox.");
156  octstr_destroy(pack);
157  return -1;
158  }
159 
160  octstr_destroy(pack);
161  msg_destroy(msg);
162  return 0;
163 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void msg_destroy(Msg *msg)
Definition: msg.c:132
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
int conn_write_withlen(Connection *conn, Octstr *data)
Definition: conn.c:1067
Definition: octstr.c:118
Octstr * msg_pack(Msg *msg)
Definition: msg.c:181
Octstr* parse_date ( Octstr date)

Definition at line 232 of file shared.c.

References date(), error(), octstr_get_char(), octstr_parse_long(), and warning().

Referenced by parse_progress_note_value(), parse_push_message_value(), parse_push_response_value(), and tokenize_date().

233 {
234  long date_value;
235 
236  if (octstr_get_char(date, 4) != '-')
237  goto error;
238  if (octstr_get_char(date, 7) != '-')
239  goto error;
240  if (octstr_get_char(date, 10) != 'T')
241  goto error;
242  if (octstr_get_char(date, 13) != ':')
243  goto error;
244  if (octstr_get_char(date, 16) != ':')
245  goto error;
246  if (octstr_get_char(date, 19) != 'Z')
247  goto error;
248 
249  if (octstr_parse_long(&date_value, date, 0, 10) < 0)
250  goto error;
251  if (octstr_parse_long(&date_value, date, 5, 10) < 0)
252  goto error;
253  if (date_value < 1 || date_value > 12)
254  goto error;
255  if (octstr_parse_long(&date_value, date, 8, 10) < 0)
256  goto error;
257  if (date_value < 1 || date_value > 31)
258  goto error;
259  if (octstr_parse_long(&date_value, date, 11, 10) < 0)
260  goto error;
261  if (date_value < 0 || date_value > 23)
262  goto error;
263  if (octstr_parse_long(&date_value, date, 14, 10) < 0)
264  goto error;
265  if (date_value < 0 || date_value > 59)
266  goto error;
267  if (date_value < 0 || date_value > 59)
268  goto error;
269  if (octstr_parse_long(&date_value, date, 17, 10) < 0)
270  goto error;
271 
272  return date;
273 
274 error:
275  warning(0, "parse_date: not an ISO date");
276  return NULL;
277 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void warning(int err, const char *fmt,...)
Definition: log.c:624
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:747
static int date(int hex)
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:404
int read_from_bearerbox ( Msg **  msg,
double  seconds 
)

Definition at line 220 of file shared.c.

References read_from_bearerbox_real().

Referenced by main(), and read_messages_from_bearerbox().

221 {
222  return read_from_bearerbox_real(bb_conn, msg, seconds);
223 }
int read_from_bearerbox_real(Connection *conn, Msg **msg, double seconds)
Definition: shared.c:172
static Connection * bb_conn
Definition: shared.c:80
int read_from_bearerbox_real ( Connection conn,
Msg **  msg,
double  seconds 
)

Definition at line 172 of file shared.c.

References conn_eof(), conn_error(), conn_read_withlen(), conn_wait(), error(), msg_unpack, octstr_destroy(), and shutting_down.

Referenced by read_from_bearerbox().

173 {
174  int ret;
175  Octstr *pack;
176 
177  pack = NULL;
178  *msg = NULL;
179  while (program_status != shutting_down) {
180  pack = conn_read_withlen(conn);
181  gw_claim_area(pack);
182  if (pack != NULL)
183  break;
184 
185  if (conn_error(conn)) {
186  error(0, "Error reading from bearerbox, disconnecting.");
187  return -1;
188  }
189  if (conn_eof(conn)) {
190  error(0, "Connection closed by the bearerbox.");
191  return -1;
192  }
193 
194  ret = conn_wait(conn, seconds);
195  if (ret < 0) {
196  error(0, "Connection to bearerbox broke.");
197  return -1;
198  }
199  else if (ret == 1) {
200  /* debug("gwlib.gwlib", 0, "Connection to bearerbox timed out after %.2f seconds.", seconds); */
201  return 1;
202  }
203  }
204 
205  if (pack == NULL)
206  return -1;
207 
208  *msg = msg_unpack(pack);
209  octstr_destroy(pack);
210 
211  if (*msg == NULL) {
212  error(0, "Failed to unpack data!");
213  return -1;
214  }
215 
216  return 0;
217 }
void error(int err, const char *fmt,...)
Definition: log.c:612
#define msg_unpack(os)
Definition: msg.h:183
program_status
Definition: shared.h:79
int conn_eof(Connection *conn)
Definition: conn.c:697
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Octstr * conn_read_withlen(Connection *conn)
Definition: conn.c:1161
Definition: octstr.c:118
int conn_wait(Connection *conn, double seconds)
Definition: conn.c:896
int conn_error(Connection *conn)
Definition: conn.c:708
int restart_box ( char **  argv)

Definition at line 279 of file shared.c.

References error(), and gwthread_sleep().

Referenced by main().

280 {
281  int rc;
282 
283  if (!(rc = fork())) {
284  /*
285  * Sleep a while in order to get father
286  * process time to cleanup things
287  */
288  gwthread_sleep(1.0);
289  if (execvp(argv[0],argv) == -1)
290  error(errno, "Unable to start new process.");
291  } else if (rc == -1)
292  error(errno, "Could not restart, exiting...");
293 
294  return rc == -1 ? -1 : 0;
295 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void gwthread_sleep(double seconds)
void write_to_bearerbox ( Msg pmsg)

Definition at line 142 of file shared.c.

References write_to_bearerbox_real().

Referenced by dispatch_datagram(), identify_to_bearerbox(), main(), obey_request_thread(), run_connects(), and send_message().

143 {
145 }
void write_to_bearerbox_real(Connection *conn, Msg *pmsg)
Definition: shared.c:129
static Connection * bb_conn
Definition: shared.c:80
void write_to_bearerbox_real ( Connection conn,
Msg pmsg 
)

Definition at line 129 of file shared.c.

References conn_write_withlen(), error(), msg_destroy(), msg_pack(), and octstr_destroy().

Referenced by write_to_bearerbox().

130 {
131  Octstr *pack;
132 
133  pack = msg_pack(pmsg);
134  if (conn_write_withlen(conn, pack) == -1)
135  error(0, "Couldn't write Msg to bearerbox.");
136 
137  msg_destroy(pmsg);
138  octstr_destroy(pack);
139 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void msg_destroy(Msg *msg)
Definition: msg.c:132
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
int conn_write_withlen(Connection *conn, Octstr *data)
Definition: conn.c:1067
Definition: octstr.c:118
Octstr * msg_pack(Msg *msg)
Definition: msg.c:181

Variable Documentation

Connection* bb_conn
static

Definition at line 80 of file shared.c.

Definition at line 71 of file shared.c.

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