Banns.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "Banns.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. BannList* bannsNew()
  6. {
  7. BannList* list = malloc(sizeof(BannList));
  8. list->root = NULL;
  9. return list;
  10. }
  11. void bannsRemove(BannList* bl)
  12. {
  13. Bann* b = bl->root;
  14. while(b != NULL)
  15. {
  16. Bann* next = b->next;
  17. free(b);
  18. b = next;
  19. }
  20. bl->root = NULL;
  21. free(bl);
  22. }
  23. void bannsAdd(BannList* bl, int ip)
  24. {
  25. Bann* b = malloc(sizeof(Bann));
  26. b->ip = ip;
  27. time(&b->timeStamp);
  28. b->timeStamp += 120; // two minutes
  29. b->next = bl->root;
  30. bl->root = b;
  31. }
  32. int bannsIsBanned(BannList* bl, int ip)
  33. {
  34. time_t t;
  35. time(&t);
  36. Bann* prev = NULL;
  37. Bann* b = bl->root;
  38. while(b != NULL)
  39. {
  40. if(b->ip == ip)
  41. {
  42. if(t < b->timeStamp)
  43. {
  44. return 1;
  45. }
  46. else
  47. {
  48. if(prev == NULL)
  49. {
  50. Bann* deleteMe = bl->root;
  51. bl->root = bl->root->next;
  52. free(deleteMe);
  53. }
  54. else
  55. {
  56. prev->next = b->next;
  57. free(b);
  58. }
  59. return 0;
  60. }
  61. }
  62. prev = b;
  63. b = b->next;
  64. }
  65. return 0;
  66. }