wtls-secmgr.c
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:4k
源码类别:

手机WAP编程

开发平台:

WINDOWS

  1. /*
  2.  * gw/wtls-secmgr.c - wapbox wtls security manager
  3.  *
  4.  * The security manager's interface consists of two functions:
  5.  *
  6.  *      wtls_secmgr_start()
  7.  *              This starts the security manager thread.
  8.  *
  9.  *      wtls_secmgr_dispatch(event)
  10.  *              This adds a new event to the security manager's event
  11.  *              queue.
  12.  *
  13.  * The wtls security manager is a thread that reads events from its event
  14.  * queue, and feeds back events to the WTLS layer. Here is where various
  15.  * approvals or rejections are made to requested security settings.
  16.  *
  17.  */
  18. #include <string.h>
  19. #include "gwlib/gwlib.h"
  20. #if (HAVE_WTLS_OPENSSL)
  21. #include "wtls.h"
  22. /*
  23.  * Give the status the module:
  24.  *
  25.  *      limbo
  26.  *              not running at all
  27.  *      running
  28.  *              operating normally
  29.  *      terminating
  30.  *              waiting for operations to terminate, returning to limbo
  31.  */
  32. static enum { limbo, running, terminating } run_status = limbo;
  33. /*
  34.  * The queue of incoming events.
  35.  */
  36. static List *secmgr_queue = NULL;
  37. /*
  38.  * Private functions.
  39.  */
  40. static void main_thread(void *);
  41. /***********************************************************************
  42.  * The public interface to the application layer.
  43.  */
  44. void wtls_secmgr_init(void) {
  45.         gw_assert(run_status == limbo);
  46.         secmgr_queue = list_create();
  47.         list_add_producer(secmgr_queue);
  48.         run_status = running;
  49.         gwthread_create(main_thread, NULL);
  50. }
  51. void wtls_secmgr_shutdown(void) {
  52.         gw_assert(run_status == running);
  53.         list_remove_producer(secmgr_queue);
  54.         run_status = terminating;
  55.         
  56.         gwthread_join_every(main_thread);
  57.         
  58.         list_destroy(secmgr_queue, wap_event_destroy_item);
  59. }
  60. void wtls_secmgr_dispatch(WAPEvent *event) {
  61.         gw_assert(run_status == running);
  62.         list_produce(secmgr_queue, event);
  63. }
  64. long wtls_secmgr_get_load(void) {
  65.         gw_assert(run_status == running);
  66.         return list_len(secmgr_queue);
  67. }
  68. /***********************************************************************
  69.  * Private functions.
  70.  */
  71. static void main_thread(void *arg) {
  72.         WAPEvent *ind, *res, *req, *term;
  73.         
  74.         while (run_status == running && (ind = list_consume(secmgr_queue)) != NULL) {
  75.                 switch (ind->type) {
  76.                 case SEC_Create_Ind:
  77.                         /* Process the cipherlist */
  78.                         /* Process the MAClist */
  79.                         /* Process the PKIlist */
  80.                         /* Dispatch a SEC_Create_Res */
  81.                         res = wap_event_create(SEC_Create_Res);
  82.                         res->u.SEC_Create_Res.addr_tuple =
  83.                                 wap_addr_tuple_duplicate(ind->u.SEC_Create_Ind.addr_tuple);
  84.                         wtls_dispatch_event(res);
  85.                         debug("wtls_secmgr : main_thread", 0,"Dispatching SEC_Create_Res event");
  86.                         /* Dispatch a SEC_Exchange_Req or maybe a SEC_Commit_Req */
  87.                         req = wap_event_create(SEC_Exchange_Req);
  88.                         req->u.SEC_Exchange_Req.addr_tuple =
  89.                                 wap_addr_tuple_duplicate(ind->u.SEC_Create_Ind.addr_tuple);
  90.                         wtls_dispatch_event(req);
  91.                         debug("wtls_secmgr : main_thread", 0,"Dispatching SEC_Exchange_Req event");
  92.                         wap_event_destroy(ind);
  93.                         break;
  94.                 case SEC_Terminate_Req:
  95.                         /* Dispatch a SEC_Terminate_Req */
  96.                         term = wap_event_create(SEC_Terminate_Req);
  97.                         term->u.SEC_Terminate_Req.addr_tuple =
  98.                                 wap_addr_tuple_duplicate(ind->u.SEC_Create_Ind.addr_tuple);
  99.                         term->u.SEC_Terminate_Req.alert_desc = 0;
  100.                         term->u.SEC_Terminate_Req.alert_level = 3;
  101.                        wtls_dispatch_event(term);
  102.                 default:
  103.                         panic(0, "WTLS-secmgr: Can't handle %s event",
  104.                               wap_event_name(ind->type));
  105.                         break;
  106.                 }
  107.         }
  108. }
  109. #endif