/src/ec_hook.c

https://github.com/wertarbyte/ettercap · C · 157 lines · 84 code · 41 blank · 32 comment · 22 complexity · d74473e0794b9e7577a18c1bfdcab09e MD5 · raw file

  1. /*
  2. ettercap -- hook points handling
  3. Copyright (C) ALoR & NaGA
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include <ec.h>
  17. #include <ec_hook.h>
  18. #include <ec_packet.h>
  19. #include <pthread.h>
  20. struct hook_list {
  21. int point;
  22. void (*func)(struct packet_object *po);
  23. LIST_ENTRY (hook_list) next;
  24. };
  25. /* global data */
  26. /* the list for the HOOK_* */
  27. static LIST_HEAD(, hook_list) hook_list_head;
  28. /* the list for the PACKET_* */
  29. static LIST_HEAD(, hook_list) hook_pck_list_head;
  30. pthread_mutex_t hook_mutex = PTHREAD_MUTEX_INITIALIZER;
  31. #define HOOK_LOCK do{ pthread_mutex_lock(&hook_mutex); } while(0)
  32. #define HOOK_UNLOCK do{ pthread_mutex_unlock(&hook_mutex); } while(0)
  33. pthread_mutex_t hook_pck_mutex = PTHREAD_MUTEX_INITIALIZER;
  34. #define HOOK_PCK_LOCK do{ pthread_mutex_lock(&hook_pck_mutex); } while(0)
  35. #define HOOK_PCK_UNLOCK do{ pthread_mutex_unlock(&hook_pck_mutex); } while(0)
  36. /* protos... */
  37. void hook_point(int point, struct packet_object *po);
  38. void hook_add(int point, void (*func)(struct packet_object *po) );
  39. int hook_del(int point, void (*func)(struct packet_object *po) );
  40. /*******************************************/
  41. /* execute the functions registered in that hook point */
  42. void hook_point(int point, struct packet_object *po)
  43. {
  44. struct hook_list *current;
  45. /* the hook is for a HOOK_PACKET_* type */
  46. if (point > HOOK_PACKET_BASE) {
  47. HOOK_PCK_LOCK;
  48. LIST_FOREACH(current, &hook_pck_list_head, next)
  49. if (current->point == point)
  50. current->func(po);
  51. HOOK_PCK_UNLOCK;
  52. } else {
  53. HOOK_LOCK;
  54. LIST_FOREACH(current, &hook_list_head, next)
  55. if (current->point == point)
  56. current->func(po);
  57. HOOK_UNLOCK;
  58. }
  59. return;
  60. }
  61. /* add a function to an hook point */
  62. void hook_add(int point, void (*func)(struct packet_object *po) )
  63. {
  64. struct hook_list *newelem;
  65. SAFE_CALLOC(newelem, 1, sizeof(struct hook_list));
  66. newelem->point = point;
  67. newelem->func = func;
  68. /* the hook is for a HOOK_PACKET_* type */
  69. if (point > HOOK_PACKET_BASE) {
  70. HOOK_PCK_LOCK;
  71. LIST_INSERT_HEAD(&hook_pck_list_head, newelem, next);
  72. HOOK_PCK_UNLOCK;
  73. } else {
  74. HOOK_LOCK;
  75. LIST_INSERT_HEAD(&hook_list_head, newelem, next);
  76. HOOK_UNLOCK;
  77. }
  78. }
  79. /* remove a function from an hook point */
  80. int hook_del(int point, void (*func)(struct packet_object *po) )
  81. {
  82. struct hook_list *current;
  83. /* the hook is for a HOOK_PACKET_* type */
  84. if (point > HOOK_PACKET_BASE) {
  85. HOOK_PCK_LOCK;
  86. LIST_FOREACH(current, &hook_pck_list_head, next) {
  87. if (current->point == point && current->func == func) {
  88. LIST_REMOVE(current, next);
  89. SAFE_FREE(current);
  90. HOOK_PCK_UNLOCK;
  91. DEBUG_MSG("hook_del -- %d [%p]", point, func);
  92. return ESUCCESS;
  93. }
  94. }
  95. HOOK_PCK_UNLOCK;
  96. } else {
  97. HOOK_LOCK;
  98. LIST_FOREACH(current, &hook_list_head, next) {
  99. if (current->point == point && current->func == func) {
  100. LIST_REMOVE(current, next);
  101. SAFE_FREE(current);
  102. HOOK_UNLOCK;
  103. DEBUG_MSG("hook_del -- %d [%p]", point, func);
  104. return ESUCCESS;
  105. }
  106. }
  107. HOOK_UNLOCK;
  108. }
  109. return -ENOTFOUND;
  110. }
  111. /* EOF */
  112. // vim:ts=3:expandtab