Kannel: Open Source WAP and SMS gateway  svn-r5335
msg.c File Reference
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include "msg.h"
#include "gwlib/gwlib.h"
#include "msg-decl.h"

Go to the source code of this file.

Macros

#define INTEGER(name)   p->name = MSG_PARAM_UNDEFINED;
 
#define OCTSTR(name)   p->name = NULL;
 
#define UUID(name)   uuid_generate(p->name);
 
#define VOID(name)   p->name = NULL;
 
#define MSG(type, stmt)   { struct type *p = &msg->type; stmt }
 
#define INTEGER(name)   p->name = q->name;
 
#define OCTSTR(name)
 
#define UUID(name)   uuid_copy(p->name, q->name);
 
#define VOID(name)   p->name = q->name;
 
#define MSG(type, stmt)
 
#define INTEGER(name)   p->name = 0;
 
#define OCTSTR(name)   octstr_destroy(p->name);
 
#define UUID(name)   uuid_clear(p->name);
 
#define VOID(name)
 
#define MSG(type, stmt)   { struct type *p = &msg->type; stmt }
 
#define INTEGER(name)   debug("gw.msg", 0, "%*s %s.%s: %ld", level, "", t, #name, (long) p->name);
 
#define OCTSTR(name)
 
#define UUID(name)
 
#define VOID(name)   debug("gw.msg", 0, "%*s %s.%s: %p", level, "", t, #name, p->name);
 
#define MSG(tt, stmt)
 
#define INTEGER(name)   append_integer(os, p->name);
 
#define OCTSTR(name)   append_string(os, p->name);
 
#define UUID(name)   append_uuid(os, p->name);
 
#define VOID(name)
 
#define MSG(type, stmt)   case type: { struct type *p = &msg->type; stmt } break;
 
#define INTEGER(name)   if (parse_integer(&(p->name), os, &off) == -1) goto error;
 
#define OCTSTR(name)   if (parse_string(&(p->name), os, &off) == -1) goto error;
 
#define UUID(name)   if (parse_uuid(p->name, os, &off) == -1) goto error;
 
#define VOID(name)
 
#define MSG(type, stmt)   case type: { struct type *p = &(msg->type); stmt } break;
 
#define MSG(t, stmt)   case t: return #t;
 

Functions

static void append_integer (Octstr *os, long i)
 
static void append_string (Octstr *os, Octstr *field)
 
static void append_uuid (Octstr *os, uuid_t id)
 
static int parse_integer (long *i, Octstr *packed, int *off)
 
static int parse_string (Octstr **os, Octstr *packed, int *off)
 
static int parse_uuid (uuid_t id, Octstr *packed, int *off)
 
static char * type_as_str (Msg *msg)
 
Msgmsg_create_real (enum msg_type type, const char *file, long line, const char *func)
 
Msgmsg_duplicate (Msg *msg)
 
void msg_destroy (Msg *msg)
 
void msg_destroy_item (void *msg)
 
void msg_dump (Msg *msg, int level)
 
enum msg_type msg_type (Msg *msg)
 
Octstrmsg_pack (Msg *msg)
 
Msgmsg_unpack_real (Octstr *os, const char *file, long line, const char *func)
 
Msgmsg_unpack_wrapper (Octstr *os)
 

Macro Definition Documentation

◆ INTEGER [1/6]

#define INTEGER (   name)    p->name = MSG_PARAM_UNDEFINED;

◆ INTEGER [2/6]

#define INTEGER (   name)    p->name = q->name;

◆ INTEGER [3/6]

#define INTEGER (   name)    p->name = 0;

◆ INTEGER [4/6]

#define INTEGER (   name)    debug("gw.msg", 0, "%*s %s.%s: %ld", level, "", t, #name, (long) p->name);

◆ INTEGER [5/6]

#define INTEGER (   name)    append_integer(os, p->name);

◆ INTEGER [6/6]

#define INTEGER (   name)    if (parse_integer(&(p->name), os, &off) == -1) goto error;

◆ MSG [1/7]

#define MSG (   type,
  stmt 
)    { struct type *p = &msg->type; stmt }

◆ MSG [2/7]

#define MSG (   type,
  stmt 
)
Value:
{ \
struct type *p = &new->type; \
struct type *q = &msg->type; \
stmt }
int type
Definition: smsc_cimd2.c:215
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ MSG [3/7]

#define MSG (   type,
  stmt 
)    { struct type *p = &msg->type; stmt }

◆ MSG [4/7]

#define MSG (   tt,
  stmt 
)
Value:
if (tt == msg->type) \
{ char *t = #tt; struct tt *p = &msg->tt; stmt }
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ MSG [5/7]

#define MSG (   type,
  stmt 
)    case type: { struct type *p = &msg->type; stmt } break;

◆ MSG [6/7]

#define MSG (   type,
  stmt 
)    case type: { struct type *p = &(msg->type); stmt } break;

◆ MSG [7/7]

#define MSG (   t,
  stmt 
)    case t: return #t;

◆ OCTSTR [1/6]

#define OCTSTR (   name)    p->name = NULL;

◆ OCTSTR [2/6]

#define OCTSTR (   name)
Value:
if (q->name == NULL) p->name = NULL; \
else p->name = octstr_duplicate(q->name);
#define octstr_duplicate(ostr)
Definition: octstr.h:187

◆ OCTSTR [3/6]

#define OCTSTR (   name)    octstr_destroy(p->name);

◆ OCTSTR [4/6]

#define OCTSTR (   name)
Value:
debug("gw.msg", 0, "%*s %s.%s:", level, "", t, #name); \
octstr_dump(p->name, level + 1);
char * name
Definition: smsc_cimd2.c:212
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726

◆ OCTSTR [5/6]

#define OCTSTR (   name)    append_string(os, p->name);

◆ OCTSTR [6/6]

#define OCTSTR (   name)    if (parse_string(&(p->name), os, &off) == -1) goto error;

◆ UUID [1/6]

#define UUID (   name)    uuid_generate(p->name);

◆ UUID [2/6]

#define UUID (   name)    uuid_copy(p->name, q->name);

◆ UUID [3/6]

#define UUID (   name)    uuid_clear(p->name);

◆ UUID [4/6]

#define UUID (   name)
Value:
uuid_unparse(p->name, buf); \
debug("gw.msg", 0 , "%*s %s.%s: %s", level, "", t, #name, buf);
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
char * name
Definition: smsc_cimd2.c:212

◆ UUID [5/6]

#define UUID (   name)    append_uuid(os, p->name);

◆ UUID [6/6]

#define UUID (   name)    if (parse_uuid(p->name, os, &off) == -1) goto error;

◆ VOID [1/6]

#define VOID (   name)    p->name = NULL;

◆ VOID [2/6]

#define VOID (   name)    p->name = q->name;

◆ VOID [3/6]

#define VOID (   name)

◆ VOID [4/6]

#define VOID (   name)    debug("gw.msg", 0, "%*s %s.%s: %p", level, "", t, #name, p->name);

◆ VOID [5/6]

#define VOID (   name)

◆ VOID [6/6]

#define VOID (   name)

Function Documentation

◆ append_integer()

static void append_integer ( Octstr os,
long  i 
)
static

Definition at line 267 of file msg.c.

References encode_network_long(), and octstr_append_data().

Referenced by append_string(), append_uuid(), and msg_pack().

268 {
269  unsigned char buf[4];
270 
271  encode_network_long(buf, i);
272  octstr_append_data(os, (char *)buf, 4);
273 }
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1497
void encode_network_long(unsigned char *data, unsigned long value)
Definition: utils.c:940

◆ append_string()

static void append_string ( Octstr os,
Octstr field 
)
static

Definition at line 275 of file msg.c.

References append_integer(), octstr_insert(), and octstr_len().

276 {
277  if (field == NULL)
278  append_integer(os, -1);
279  else {
280  append_integer(os, octstr_len(field));
281  octstr_insert(os, field, octstr_len(os));
282  }
283 }
static void append_integer(Octstr *os, long i)
Definition: msg.c:267
void octstr_insert(Octstr *ostr1, const Octstr *ostr2, long pos)
Definition: octstr.c:1303
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342

◆ append_uuid()

static void append_uuid ( Octstr os,
uuid_t  id 
)
static

Definition at line 285 of file msg.c.

References append_integer(), octstr_append_cstr(), UUID_STR_LEN, and uuid_unparse().

286 {
287  char buf[UUID_STR_LEN + 1];
288 
289  uuid_unparse(id, buf);
291  octstr_append_cstr(os, buf);
292 }
static void append_integer(Octstr *os, long i)
Definition: msg.c:267
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1511
#define UUID_STR_LEN
Definition: gw_uuid.h:19

◆ msg_create_real()

Msg* msg_create_real ( enum msg_type  type,
const char *  file,
long  line,
const char *  func 
)

Definition at line 93 of file msg.c.

References file, msg, and type.

Referenced by msg_unpack_real().

95 {
96  Msg *msg;
97 
98  msg = gw_malloc_trace(sizeof(Msg), file, line, func);
99 
100  msg->type = type;
101 #define INTEGER(name) p->name = MSG_PARAM_UNDEFINED;
102 #define OCTSTR(name) p->name = NULL;
103 #define UUID(name) uuid_generate(p->name);
104 #define VOID(name) p->name = NULL;
105 #define MSG(type, stmt) { struct type *p = &msg->type; stmt }
106 #include "msg-decl.h"
107 
108  return msg;
109 }
int type
Definition: smsc_cimd2.c:215
FILE * file
Definition: log.c:169
Definition: msg.h:79
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_destroy()

void msg_destroy ( Msg msg)

Definition at line 132 of file msg.c.

References msg.

Referenced by bb_remove_message(), bb_smscconn_receive(), bb_smscconn_receive_internal(), bb_smscconn_send_failed(), bb_smscconn_sent(), bearerbox_to_smpp(), bearerbox_to_smsbox(), bearerbox_to_sql(), boxc_receiver(), boxc_sender(), boxc_sent_pop(), catenate_msg(), cimd2_accept_message(), concat_handling_check_and_handle(), concat_handling_clear_old_parts(), data_sm_to_msg(), deliver_sms_to_queue(), deliver_to_bearerbox_real(), destroy_concatMsg(), dispatch_datagram(), dispatch_into_queue(), dlr_spool_add(), dlr_spool_get(), emi_receive_msg(), empty_msg_lists(), generic_receive_sms(), handle_pdu(), handle_split(), http_queue_thread(), identify_to_bearerbox(), main(), main_connection_loop(), msg_destroy_item(), msg_to_bb(), msg_to_pdu(), msg_unpack_real(), obey_request_thread(), ois_deliver_sm_invoke(), oisd_accept_message(), pdu_to_msg(), read_messages_from_bearerbox(), retry_consumer_thread(), run_batch(), run_requests(), send_message(), send_pdu(), smpp_msg_destroy(), sms_receive(), sms_router(), sms_split(), smsbox_req_handle(), smsbox_req_sendota(), smsbox_sendota_post(), smsbox_thread(), smsbox_to_bearerbox(), smsc2_rout(), sql_list(), sql_single(), status_cb(), store_file_load(), store_file_save_ack(), store_spool_save_ack(), store_to_dict(), timer_consumer_thread(), udp_sender(), url_result_thread(), wdp_to_wapboxes(), wrapper_sender(), and write_to_bearerbox_real().

133 {
134  if (msg == NULL)
135  return;
136 
137 #define INTEGER(name) p->name = 0;
138 #define OCTSTR(name) octstr_destroy(p->name);
139 #define UUID(name) uuid_clear(p->name);
140 #define VOID(name)
141 #define MSG(type, stmt) { struct type *p = &msg->type; stmt }
142 #include "msg-decl.h"
143 
144  gw_free(msg);
145 }
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_destroy_item()

void msg_destroy_item ( void *  msg)

Definition at line 147 of file msg.c.

References msg, and msg_destroy().

Referenced by boxc_create(), cimd2_destroy(), empty_msg_lists(), handle_pdu(), msg_to_pdu(), oisd_destroy(), run_batch(), smasi_destroy(), smpp_destroy(), smscconn_send(), sql_list(), and store_file_init().

148 {
149  msg_destroy(msg);
150 }
void msg_destroy(Msg *msg)
Definition: msg.c:132
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_dump()

void msg_dump ( Msg msg,
int  level 
)

Definition at line 152 of file msg.c.

References debug(), msg, type_as_str(), and UUID_STR_LEN.

Referenced by bearerbox_to_smpp(), boxc_sent_pop(), concat_handling_check_and_handle(), dump(), handle_pdu(), main(), obey_request_thread(), print_msg(), read_messages_from_bearerbox(), send_message(), and store_file_load().

153 {
154  char buf[UUID_STR_LEN + 1];
155 
156  debug("gw.msg", 0, "%*sMsg object at %p:", level, "", (void *) msg);
157  debug("gw.msg", 0, "%*s type: %s", level, "", type_as_str(msg));
158 #define INTEGER(name) \
159  debug("gw.msg", 0, "%*s %s.%s: %ld", level, "", t, #name, (long) p->name);
160 #define OCTSTR(name) \
161  debug("gw.msg", 0, "%*s %s.%s:", level, "", t, #name); \
162  octstr_dump(p->name, level + 1);
163 #define UUID(name) \
164  uuid_unparse(p->name, buf); \
165  debug("gw.msg", 0 , "%*s %s.%s: %s", level, "", t, #name, buf);
166 #define VOID(name) \
167  debug("gw.msg", 0, "%*s %s.%s: %p", level, "", t, #name, p->name);
168 #define MSG(tt, stmt) \
169  if (tt == msg->type) \
170  { char *t = #tt; struct tt *p = &msg->tt; stmt }
171 #include "msg-decl.h"
172  debug("gw.msg", 0, "Msg object ends.");
173 }
static char * type_as_str(Msg *msg)
Definition: msg.c:353
#define UUID_STR_LEN
Definition: gw_uuid.h:19
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_duplicate()

Msg* msg_duplicate ( Msg msg)

Definition at line 111 of file msg.c.

References msg, and msg_create.

Referenced by add_msg_cb(), at2_add_msg_cb(), bb_smscconn_receive_internal(), bearerbox_to_smsbox(), boxc_sent_push(), catenate_msg(), cgw_add_msg_cb(), cimd2_add_msg_cb(), concat_handling_check_and_handle(), concat_handling_clear_old_parts(), generic_receive_sms(), handle_pdu(), httpsmsc_send(), main(), main_connection_loop(), msg_cb(), oisd_add_msg_cb(), run_batch(), send_message(), send_msg_cb(), sms_split(), smsbox_to_bearerbox(), smsc2_rout(), smscconn_send(), soap_add_msg_cb(), store_to_dict(), wrapper_add_msg(), and wrapper_sender().

112 {
113  Msg *new;
114 
115  new = msg_create(msg->type);
116 
117 #define INTEGER(name) p->name = q->name;
118 #define OCTSTR(name) \
119  if (q->name == NULL) p->name = NULL; \
120  else p->name = octstr_duplicate(q->name);
121 #define UUID(name) uuid_copy(p->name, q->name);
122 #define VOID(name) p->name = q->name;
123 #define MSG(type, stmt) { \
124  struct type *p = &new->type; \
125  struct type *q = &msg->type; \
126  stmt }
127 #include "msg-decl.h"
128 
129  return new;
130 }
#define msg_create(type)
Definition: msg.h:136
Definition: msg.h:79
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_pack()

Octstr* msg_pack ( Msg msg)

Definition at line 181 of file msg.c.

References append_integer(), msg, octstr_create, and panic.

Referenced by deliver_to_bearerbox_real(), init_bearerbox(), init_smppbox(), main(), send_msg(), send_pdu(), smsbox_thread(), and write_to_bearerbox_real().

182 {
183  Octstr *os;
184 
185  os = octstr_create("");
186  append_integer(os, msg->type);
187 
188 #define INTEGER(name) append_integer(os, p->name);
189 #define OCTSTR(name) append_string(os, p->name);
190 #define UUID(name) append_uuid(os, p->name);
191 #define VOID(name)
192 #define MSG(type, stmt) \
193  case type: { struct type *p = &msg->type; stmt } break;
194 
195  switch (msg->type) {
196 #include "msg-decl.h"
197  default:
198  panic(0, "Internal error: unknown message type: %d",
199  msg->type);
200  }
201 
202  return os;
203 }
static void append_integer(Octstr *os, long i)
Definition: msg.c:267
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
#define panic
Definition: log.h:87
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_type()

enum msg_type msg_type ( Msg msg)

Definition at line 176 of file msg.c.

References msg.

177 {
178  return msg->type;
179 }
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_unpack_real()

Msg* msg_unpack_real ( Octstr os,
const char *  file,
long  line,
const char *  func 
)

Definition at line 206 of file msg.c.

References error(), file, msg, msg_create_real(), msg_destroy(), and parse_integer().

207 {
208  Msg *msg;
209  int off;
210  long i;
211 
212  msg = msg_create_real(0, file, line, func);
213  if (msg == NULL)
214  goto error;
215 
216  off = 0;
217 
218  if (parse_integer(&i, os, &off) == -1)
219  goto error;
220  msg->type = i;
221 
222 #define INTEGER(name) \
223  if (parse_integer(&(p->name), os, &off) == -1) goto error;
224 #define OCTSTR(name) \
225  if (parse_string(&(p->name), os, &off) == -1) goto error;
226 #define UUID(name) \
227  if (parse_uuid(p->name, os, &off) == -1) goto error;
228 #define VOID(name)
229 #define MSG(type, stmt) \
230  case type: { struct type *p = &(msg->type); stmt } break;
231 
232  switch (msg->type) {
233 #include "msg-decl.h"
234  default:
235  error(0, "Internal error: unknown message type: %d",
236  msg->type);
237  msg->type = 0;
238  msg_destroy(msg);
239  return NULL;
240  }
241 
242  return msg;
243 
244 error:
245  if (msg != NULL) msg_destroy(msg);
246  error(0, "Msg packet was invalid.");
247  return NULL;
248 }
void error(int err, const char *fmt,...)
Definition: log.c:648
Msg * msg_create_real(enum msg_type type, const char *file, long line, const char *func)
Definition: msg.c:93
FILE * file
Definition: log.c:169
Definition: msg.h:79
void msg_destroy(Msg *msg)
Definition: msg.c:132
static int parse_integer(long *i, Octstr *packed, int *off)
Definition: msg.c:294
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ msg_unpack_wrapper()

Msg* msg_unpack_wrapper ( Octstr os)
inline

Definition at line 256 of file msg.c.

References msg_unpack.

Referenced by init_bearerbox(), init_smppbox(), and main().

257 {
258  return msg_unpack(os);
259 }
#define msg_unpack(os)
Definition: msg.h:183

◆ parse_integer()

static int parse_integer ( long *  i,
Octstr packed,
int *  off 
)
static

Definition at line 294 of file msg.c.

References decode_network_long(), error(), gw_assert(), octstr_get_many_chars(), and octstr_len().

Referenced by msg_unpack_real(), and parse_string().

295 {
296  unsigned char buf[4];
297 
298  gw_assert(*off >= 0);
299  if (*off + 4 > octstr_len(packed)) {
300  error(0, "Packet too short while unpacking Msg.");
301  return -1;
302  }
303 
304  octstr_get_many_chars((char *)buf, packed, *off, 4);
305  *i = decode_network_long(buf);
306  *off += 4;
307  return 0;
308 }
void error(int err, const char *fmt,...)
Definition: log.c:648
gw_assert(wtls_machine->packet_to_send !=NULL)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
long decode_network_long(unsigned char *data)
Definition: utils.c:935
void octstr_get_many_chars(char *buf, Octstr *ostr, long pos, long len)
Definition: octstr.c:425

◆ parse_string()

static int parse_string ( Octstr **  os,
Octstr packed,
int *  off 
)
static

Definition at line 311 of file msg.c.

References octstr_copy, and parse_integer().

Referenced by parse_uuid().

312 {
313  long len;
314 
315  if (parse_integer(&len, packed, off) == -1)
316  return -1;
317 
318  if (len == -1) {
319  *os = NULL;
320  return 0;
321  }
322 
323  /* XXX check that len is ok */
324 
325  *os = octstr_copy(packed, *off, len);
326  if (*os == NULL)
327  return -1;
328  *off += len;
329 
330  return 0;
331 }
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
static int parse_integer(long *i, Octstr *packed, int *off)
Definition: msg.c:294

◆ parse_uuid()

static int parse_uuid ( uuid_t  id,
Octstr packed,
int *  off 
)
static

Definition at line 334 of file msg.c.

References octstr_destroy(), octstr_get_cstr, parse_string(), and uuid_parse().

335 {
336  Octstr *tmp = NULL;
337 
338  if (parse_string(&tmp, packed, off) == -1) {
339  octstr_destroy(tmp);
340  return -1;
341  }
342 
343  if (uuid_parse(octstr_get_cstr(tmp), id) == -1) {
344  octstr_destroy(tmp);
345  return -1;
346  }
347 
348  octstr_destroy(tmp);
349 
350  return 0;
351 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
int uuid_parse(const char *in, uuid_t uu)
Definition: gw_uuid.c:476
static int parse_string(Octstr **os, Octstr *packed, int *off)
Definition: msg.c:311
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
Definition: octstr.c:118

◆ type_as_str()

static char * type_as_str ( Msg msg)
static

Definition at line 353 of file msg.c.

References msg.

Referenced by msg_dump().

354 {
355  switch (msg->type) {
356 #define MSG(t, stmt) case t: return #t;
357 #include "msg-decl.h"
358  default:
359  return "unknown type";
360  }
361 }
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.