KeyManager.cpp
上传用户:nbcables
上传日期:2007-01-11
资源大小:1243k
文件大小:2k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <time.h>
  3. #include "keymanager.h"
  4. CKeyManager::CKeyManager(void)
  5. {
  6. m_pkey_info =NULL;
  7. m_key_info_count =0;
  8. InitializeCriticalSection(&m_cs_key);
  9. }
  10. CKeyManager::~CKeyManager(void)
  11. {
  12. RemoveAllKey();
  13. DeleteCriticalSection(&m_cs_key);
  14. }
  15. // 添加一个请求连接数据
  16. int CKeyManager::AddKey(int sd, unsigned char *key, int key_len)
  17. {
  18. KEY_INFO **ppkey =&m_pkey_info;
  19. EnterCriticalSection(&m_cs_key);
  20. while(1)
  21. {
  22. if(*ppkey ==NULL)
  23. {
  24. *ppkey =(KEY_INFO *)malloc(sizeof(KEY_INFO));
  25. memset(*ppkey, 0, sizeof(KEY_INFO));
  26. (*ppkey)->sd =sd;
  27. memcpy((*ppkey)->key, key, key_len);
  28. (*ppkey)->key_len =key_len;
  29. break;
  30. }
  31. ppkey =&((*ppkey)->pnext);
  32. }
  33. LeaveCriticalSection(&m_cs_key);
  34. return 0;
  35. }
  36. // 删除一个请求连接数据
  37. int CKeyManager::RemoveKey(int sd)
  38. {
  39. KEY_INFO *pkey =m_pkey_info, *pprev_info =NULL, **ppkey =&m_pkey_info;
  40. EnterCriticalSection(&m_cs_key);
  41. while(pkey)
  42. {
  43. if(pkey->sd ==sd)
  44. {
  45. if(pprev_info)
  46. pprev_info->pnext =pkey->pnext;
  47. free(pkey);
  48. *ppkey =NULL;
  49. break;
  50. }
  51. pprev_info =pkey;
  52. pkey =pkey->pnext;
  53. ppkey =&pkey->pnext;
  54. }
  55. LeaveCriticalSection(&m_cs_key);
  56. return 0;
  57. }
  58. void CKeyManager::RemoveAllKey()
  59. {
  60. KEY_INFO *pkey =m_pkey_info, *pprev_key =NULL;
  61. EnterCriticalSection(&m_cs_key);
  62. while(pkey)
  63. {
  64. pprev_key =pkey;
  65. pkey =pkey->pnext;
  66. free(pprev_key);
  67. }
  68. m_pkey_info =NULL;
  69. LeaveCriticalSection(&m_cs_key);
  70. }
  71. int CKeyManager::GenKey(unsigned char * key)
  72. {
  73. srand((unsigned int)time(NULL));
  74. for(int i =0; i<16; i++)   // 3des key size =16
  75. key[i] =rand()%255+1;
  76. return i;
  77. }
  78. KEY_INFO *CKeyManager::GetKey(int sd)
  79. {
  80. KEY_INFO *pkey =m_pkey_info;
  81. EnterCriticalSection(&m_cs_key);
  82. while(pkey)
  83. {
  84. if(pkey->sd ==sd)
  85. return pkey;
  86. pkey =pkey->pnext;
  87. }
  88. LeaveCriticalSection(&m_cs_key);
  89. return pkey;
  90. }