Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsp_unit.c File Reference
#include <string.h>
#include "gwlib/gwlib.h"
#include "wsp.h"
#include "wsp_pdu.h"
#include "wsp_headers.h"
#include "wap_events.h"
#include "wsp_strings.h"
#include "wap.h"

Go to the source code of this file.

Enumerations

enum  { limbo, running, terminating }
 

Functions

static void main_thread (void *)
 
static WAPEventpack_into_result_datagram (WAPEvent *event)
 
static WAPEventpack_into_push_datagram (WAPEvent *event)
 
void wsp_unit_init (wap_dispatch_func_t *datagram_dispatch, wap_dispatch_func_t *application_dispatch)
 
void wsp_unit_shutdown (void)
 
void wsp_unit_dispatch_event (WAPEvent *event)
 
static WAPEventunpack_datagram (WAPEvent *datagram)
 

Variables

static enum { ... }  run_status = limbo
 
static wap_dispatch_func_tdispatch_to_wdp
 
static wap_dispatch_func_tdispatch_to_appl
 
static Listqueue = NULL
 

Enumeration Type Documentation

anonymous enum
Enumerator
limbo 
running 
terminating 

Definition at line 84 of file wsp_unit.c.

Definition: wsp_unit.c:84
static enum @92 run_status

Function Documentation

static void main_thread ( void *  arg)
static

Definition at line 217 of file wsp_unit.c.

References debug(), dispatch_to_appl, dispatch_to_wdp, gwlist_consume(), pack_into_push_datagram(), pack_into_result_datagram(), run_status, running, WAPEvent::type, unpack_datagram(), wap_event_assert(), wap_event_destroy(), and warning().

Referenced by wsp_unit_init(), and wsp_unit_shutdown().

218 {
219  WAPEvent *e;
220  WAPEvent *newevent;
221 
222  while (run_status == running && (e = gwlist_consume(queue)) != NULL) {
223  debug("wap.wsp.unit", 0, "WSP (UNIT): event arrived");
224  wap_event_assert(e);
225  switch (e->type) {
226  case T_DUnitdata_Ind:
227  newevent = unpack_datagram(e);
228  if (newevent != NULL)
229  dispatch_to_appl(newevent);
230  break;
231 
232  case S_Unit_MethodResult_Req:
233  newevent = pack_into_result_datagram(e);
234  if (newevent != NULL)
235  dispatch_to_wdp(newevent);
236  break;
237 
238  case S_Unit_Push_Req:
239  newevent = pack_into_push_datagram(e);
240  if (newevent != NULL)
241  dispatch_to_wdp(newevent);
242  debug("wsp.unit", 0, "WSP (UNIT): delivering to wdp");
243  break;
244 
245  default:
246  warning(0, "WSP UNIT: Unknown event type %d", e->type);
247  break;
248  }
250  }
251 }
void wap_event_assert(WAPEvent *event)
Definition: wap_events.c:220
static WAPEvent * unpack_datagram(WAPEvent *datagram)
Definition: wsp_unit.c:131
static wap_dispatch_func_t * dispatch_to_appl
Definition: wsp_unit.c:87
void warning(int err, const char *fmt,...)
Definition: log.c:624
static wap_dispatch_func_t * dispatch_to_wdp
Definition: wsp_unit.c:86
void * gwlist_consume(List *list)
Definition: list.c:427
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
WAPEventName type
Definition: wap_events.h:88
static WAPEvent * pack_into_push_datagram(WAPEvent *event)
Definition: wsp_unit.c:298
static enum @92 run_status
static WAPEvent * pack_into_result_datagram(WAPEvent *event)
Definition: wsp_unit.c:261
static List * queue
Definition: wsp_unit.c:89
void wap_event_destroy(WAPEvent *event)
Definition: wap_events.c:102
static WAPEvent * pack_into_push_datagram ( WAPEvent event)
static

Definition at line 298 of file wsp_unit.c.

References debug(), gw_assert(), http_header_add(), octstr_duplicate, octstr_insert_data(), WAPEvent::type, WAPEvent::u, wsp_pdu::u, wap_addr_tuple_duplicate(), wap_event_create, WSP_1_3, wsp_headers_pack(), wsp_pdu_create(), wsp_pdu_destroy(), wsp_pdu_dump(), and wsp_pdu_pack().

Referenced by main_thread().

299 {
300  WAPEvent *datagram;
301  WSP_PDU *pdu;
302  Octstr *ospdu;
303  unsigned char push_id;
304 
305  gw_assert(event->type == S_Unit_Push_Req);
306  debug("wap.wsp.unit", 0, "WSP_UNIT: Connectionless push accepted");
307 
308  http_header_add(event->u.S_Unit_Push_Req.push_headers,
309  "Encoding-Version", "1.3");
310 
311  pdu = wsp_pdu_create(Push);
312  pdu->u.Push.headers = wsp_headers_pack(
313  event->u.S_Unit_Push_Req.push_headers, 1, WSP_1_3);
314  pdu->u.Push.data = octstr_duplicate(
315  event->u.S_Unit_Push_Req.push_body);
316  wsp_pdu_dump(pdu, 0);
317  ospdu = wsp_pdu_pack(pdu);
318  wsp_pdu_destroy(pdu);
319  if (ospdu == NULL)
320  return NULL;
321 
322  push_id = event->u.S_Unit_Push_Req.push_id;
323  octstr_insert_data(ospdu, 0, (char *)&push_id, 1);
324 
325  datagram = wap_event_create(T_DUnitdata_Req);
326 
327  datagram->u.T_DUnitdata_Req.addr_tuple =
328  wap_addr_tuple_duplicate(event->u.S_Unit_Push_Req.addr_tuple);
329  datagram->u.T_DUnitdata_Req.address_type =
330  event->u.S_Unit_Push_Req.address_type;
331  if (event->u.S_Unit_Push_Req.smsc_id != NULL)
332  datagram->u.T_DUnitdata_Req.smsc_id =
333  octstr_duplicate(event->u.S_Unit_Push_Req.smsc_id);
334  else
335  datagram->u.T_DUnitdata_Req.smsc_id = NULL;
336  if (event->u.S_Unit_Push_Req.dlr_url != NULL)
337  datagram->u.T_DUnitdata_Req.dlr_url =
338  octstr_duplicate(event->u.S_Unit_Push_Req.dlr_url);
339  else
340  datagram->u.T_DUnitdata_Req.dlr_url = NULL;
341  datagram->u.T_DUnitdata_Req.dlr_mask = event->u.S_Unit_Push_Req.dlr_mask;
342  if (event->u.S_Unit_Push_Req.smsbox_id != NULL)
343  datagram->u.T_DUnitdata_Req.smsbox_id =
344  octstr_duplicate(event->u.S_Unit_Push_Req.smsbox_id);
345  else
346  datagram->u.T_DUnitdata_Req.smsbox_id = NULL;
347  datagram->u.T_DUnitdata_Req.service_name =
348  octstr_duplicate(event->u.S_Unit_Push_Req.service_name);
349 
350  datagram->u.T_DUnitdata_Req.user_data = ospdu;
351 
352  return datagram;
353 }
union wsp_pdu::@89 u
Definition: wsp.h:73
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
Octstr * wsp_headers_pack(List *headers, int separate_content_type, int wsp_version)
Definition: wsp_headers.c:2963
Octstr * wsp_pdu_pack(WSP_PDU *pdu)
Definition: wsp_pdu.c:271
void octstr_insert_data(Octstr *ostr, long pos, const char *data, long len)
Definition: octstr.c:1459
WAPAddrTuple * wap_addr_tuple_duplicate(WAPAddrTuple *tuple)
Definition: wap_addr.c:125
WSP_PDU * wsp_pdu_create(int type)
Definition: wsp_pdu.c:68
#define octstr_duplicate(ostr)
Definition: octstr.h:187
#define wap_event_create(type)
Definition: wap_events.h:107
gw_assert(wtls_machine->packet_to_send!=NULL)
void wsp_pdu_destroy(WSP_PDU *pdu)
Definition: wsp_pdu.c:102
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
WAPEventName type
Definition: wap_events.h:88
void wsp_pdu_dump(WSP_PDU *pdu, int level)
Definition: wsp_pdu.c:324
union WAPEvent::@87 u
static WAPEvent * pack_into_result_datagram ( WAPEvent event)
static

Definition at line 261 of file wsp_unit.c.

References gw_assert(), http_header_add(), octstr_duplicate, octstr_insert_data(), WAPEvent::type, WAPEvent::u, wsp_pdu::u, wap_addr_tuple_duplicate(), wap_event_create, WSP_1_3, wsp_convert_http_status_to_wsp_status(), wsp_headers_pack(), wsp_pdu_create(), wsp_pdu_destroy(), and wsp_pdu_pack().

Referenced by main_thread().

262 {
263  WAPEvent *datagram;
264  struct S_Unit_MethodResult_Req *p;
265  WSP_PDU *pdu;
266  Octstr *ospdu;
267  unsigned char tid;
268 
269  gw_assert(event->type == S_Unit_MethodResult_Req);
270  p = &event->u.S_Unit_MethodResult_Req;
271 
272  http_header_add(p->response_headers, "Encoding-Version", "1.3");
273 
274  pdu = wsp_pdu_create(Reply);
275  pdu->u.Reply.status = wsp_convert_http_status_to_wsp_status(p->status);
276  pdu->u.Reply.headers = wsp_headers_pack(p->response_headers, 1, WSP_1_3);
277  pdu->u.Reply.data = octstr_duplicate(p->response_body);
278  ospdu = wsp_pdu_pack(pdu);
279  wsp_pdu_destroy(pdu);
280  if (ospdu == NULL)
281  return NULL;
282 
283  tid = p->transaction_id;
284  octstr_insert_data(ospdu, 0, (char *)&tid, 1);
285 
286  datagram = wap_event_create(T_DUnitdata_Req);
287  datagram->u.T_DUnitdata_Req.addr_tuple =
288  wap_addr_tuple_duplicate(p->addr_tuple);
289  datagram->u.T_DUnitdata_Req.user_data = ospdu;
290 
291  return datagram;
292 }
long wsp_convert_http_status_to_wsp_status(long http_status)
Definition: wsp.c:77
union wsp_pdu::@89 u
Definition: wsp.h:73
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
Octstr * wsp_headers_pack(List *headers, int separate_content_type, int wsp_version)
Definition: wsp_headers.c:2963
Octstr * wsp_pdu_pack(WSP_PDU *pdu)
Definition: wsp_pdu.c:271
void octstr_insert_data(Octstr *ostr, long pos, const char *data, long len)
Definition: octstr.c:1459
WAPAddrTuple * wap_addr_tuple_duplicate(WAPAddrTuple *tuple)
Definition: wap_addr.c:125
WSP_PDU * wsp_pdu_create(int type)
Definition: wsp_pdu.c:68
#define octstr_duplicate(ostr)
Definition: octstr.h:187
#define wap_event_create(type)
Definition: wap_events.h:107
gw_assert(wtls_machine->packet_to_send!=NULL)
void wsp_pdu_destroy(WSP_PDU *pdu)
Definition: wsp_pdu.c:102
Definition: octstr.c:118
WAPEventName type
Definition: wap_events.h:88
union WAPEvent::@87 u
static WAPEvent* unpack_datagram ( WAPEvent datagram)
static

Definition at line 131 of file wsp_unit.c.

References debug(), error(), GET_METHODS, gw_assert(), method, method_name, octstr_delete(), octstr_destroy(), octstr_duplicate, octstr_format(), octstr_get_char(), octstr_len(), POST_METHODS, wsp_pdu::type, WAPEvent::type, WAPEvent::u, wsp_pdu::u, wap_addr_tuple_duplicate(), wap_event_create, wap_event_destroy(), warning(), wsp_headers_unpack(), wsp_pdu_destroy(), and wsp_pdu_unpack().

Referenced by main_thread().

132 {
133  WAPEvent *event;
134  Octstr *os;
135  WSP_PDU *pdu;
136  long tid_byte;
137  int method;
139 
140  gw_assert(datagram->type == T_DUnitdata_Ind);
141 
142  os = NULL;
143  pdu = NULL;
144  event = NULL;
145 
146  os = octstr_duplicate(datagram->u.T_DUnitdata_Ind.user_data);
147  if (os && octstr_len(os) == 0) {
148  warning(0, "WSP UNIT: Empty datagram.");
149  goto error;
150  }
151 
152  tid_byte = octstr_get_char(os, 0);
153  octstr_delete(os, 0, 1);
154 
155  pdu = wsp_pdu_unpack(os);
156  if (pdu == NULL)
157  goto error;
158 
159  if (pdu->type != Get && pdu->type != Post) {
160  warning(0, "WSP UNIT: Unsupported PDU type %d", pdu->type);
161  goto error;
162  }
163 
164  event = wap_event_create(S_Unit_MethodInvoke_Ind);
165  event->u.S_Unit_MethodInvoke_Ind.addr_tuple = wap_addr_tuple_duplicate(
166  datagram->u.T_DUnitdata_Ind.addr_tuple);
167  event->u.S_Unit_MethodInvoke_Ind.transaction_id = tid_byte;
168 
169  switch (pdu->type) {
170  case Get:
171  debug("wap.wsp", 0, "Connectionless Get request received.");
172  method = GET_METHODS + pdu->u.Get.subtype;
173  event->u.S_Unit_MethodInvoke_Ind.request_uri =
174  octstr_duplicate(pdu->u.Get.uri);
175  event->u.S_Unit_MethodInvoke_Ind.request_headers =
176  wsp_headers_unpack(pdu->u.Get.headers, 0);
177  event->u.S_Unit_MethodInvoke_Ind.request_body = NULL;
178  break;
179 
180  case Post:
181  debug("wap.wsp", 0, "Connectionless Post request received.");
182  method = POST_METHODS + pdu->u.Post.subtype;
183  event->u.S_Unit_MethodInvoke_Ind.request_uri =
184  octstr_duplicate(pdu->u.Post.uri);
185  event->u.S_Unit_MethodInvoke_Ind.request_headers =
186  wsp_headers_unpack(pdu->u.Post.headers, 1);
187  event->u.S_Unit_MethodInvoke_Ind.request_body =
188  octstr_duplicate(pdu->u.Post.data);
189  break;
190 
191  default:
192  warning(0, "WSP UNIT: Unsupported PDU type %d", pdu->type);
193  goto error;
194  }
195 
196  method_name = wsp_method_to_string(method);
197  if (method_name == NULL)
198  method_name = octstr_format("UNKNOWN%02X", method);
199  event->u.S_Unit_MethodInvoke_Ind.method = method_name;
200 
201  octstr_destroy(os);
202  wsp_pdu_destroy(pdu);
203  return event;
204 
205 error:
206  octstr_destroy(os);
207  wsp_pdu_destroy(pdu);
208  wap_event_destroy(event);
209  return NULL;
210 }
void error(int err, const char *fmt,...)
Definition: log.c:612
union wsp_pdu::@89 u
static Octstr * method_name
Definition: test_http.c:86
WAPAddrTuple * wap_addr_tuple_duplicate(WAPAddrTuple *tuple)
Definition: wap_addr.c:125
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
#define octstr_duplicate(ostr)
Definition: octstr.h:187
#define wap_event_create(type)
Definition: wap_events.h:107
void warning(int err, const char *fmt,...)
Definition: log.c:624
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
static int method
Definition: test_http.c:76
gw_assert(wtls_machine->packet_to_send!=NULL)
void wsp_pdu_destroy(WSP_PDU *pdu)
Definition: wsp_pdu.c:102
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
List * wsp_headers_unpack(Octstr *headers, int content_type_present)
Definition: wsp_headers.c:1331
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
int type
Definition: wsp_pdu.h:87
WAPEventName type
Definition: wap_events.h:88
union WAPEvent::@87 u
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:404
WSP_PDU * wsp_pdu_unpack(Octstr *data)
Definition: wsp_pdu.c:178
void wap_event_destroy(WAPEvent *event)
Definition: wap_events.c:102
void wsp_unit_dispatch_event ( WAPEvent event)

Definition at line 124 of file wsp_unit.c.

References gwlist_produce(), and wap_event_assert().

Referenced by main(), return_unit_reply(), and wap_dispatch_datagram().

125 {
126  wap_event_assert(event);
127  gwlist_produce(queue, event);
128 }
void gwlist_produce(List *list, void *item)
Definition: list.c:411
void wap_event_assert(WAPEvent *event)
Definition: wap_events.c:220
static List * queue
Definition: wsp_unit.c:89
void wsp_unit_init ( wap_dispatch_func_t datagram_dispatch,
wap_dispatch_func_t application_dispatch 
)

Definition at line 100 of file wsp_unit.c.

References dispatch_to_appl, dispatch_to_wdp, gwlist_add_producer(), gwlist_create, gwthread_create, main_thread(), run_status, running, and wsp_strings_init().

Referenced by main().

102 {
103  queue = gwlist_create();
105  dispatch_to_wdp = datagram_dispatch;
106  dispatch_to_appl = application_dispatch;
110 }
static void main_thread(void *)
Definition: wsp_unit.c:217
static wap_dispatch_func_t * dispatch_to_appl
Definition: wsp_unit.c:87
#define gwthread_create(func, arg)
Definition: gwthread.h:90
static wap_dispatch_func_t * dispatch_to_wdp
Definition: wsp_unit.c:86
void wsp_strings_init(void)
Definition: wsp_strings.c:269
#define gwlist_create()
Definition: list.h:136
static enum @92 run_status
void gwlist_add_producer(List *list)
Definition: list.c:383
static List * queue
Definition: wsp_unit.c:89
void wsp_unit_shutdown ( void  )

Definition at line 113 of file wsp_unit.c.

References gw_assert(), gwlist_destroy(), gwlist_remove_producer(), gwthread_join_every(), main_thread(), run_status, running, terminating, wap_event_destroy_item(), and wsp_strings_shutdown().

Referenced by main().

114 {
121 }
static void main_thread(void *)
Definition: wsp_unit.c:217
void gwthread_join_every(gwthread_func_t *func)
void gwlist_remove_producer(List *list)
Definition: list.c:401
void wap_event_destroy_item(void *event)
Definition: wap_events.c:130
gw_assert(wtls_machine->packet_to_send!=NULL)
void wsp_strings_shutdown(void)
Definition: wsp_strings.c:286
static enum @92 run_status
static List * queue
Definition: wsp_unit.c:89
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

Variable Documentation

wap_dispatch_func_t* dispatch_to_appl
static

Definition at line 87 of file wsp_unit.c.

Referenced by main_thread(), and wsp_unit_init().

wap_dispatch_func_t* dispatch_to_wdp
static

Definition at line 86 of file wsp_unit.c.

Referenced by main_thread(), and wsp_unit_init().

List* queue = NULL
static

Definition at line 89 of file wsp_unit.c.

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