12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include "Banns.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- BannList* bannsNew()
- {
- BannList* list = malloc(sizeof(BannList));
- list->root = NULL;
- return list;
- }
- void bannsRemove(BannList* bl)
- {
- Bann* b = bl->root;
- while(b != NULL)
- {
- Bann* next = b->next;
- free(b);
- b = next;
- }
- bl->root = NULL;
- free(bl);
- }
- void bannsAdd(BannList* bl, int ip)
- {
- Bann* b = malloc(sizeof(Bann));
- b->ip = ip;
- time(&b->timeStamp);
- b->timeStamp += 120; // two minutes
- b->next = bl->root;
- bl->root = b;
- }
- int bannsIsBanned(BannList* bl, int ip)
- {
- time_t t;
- time(&t);
-
- Bann* prev = NULL;
- Bann* b = bl->root;
-
- while(b != NULL)
- {
- if(b->ip == ip)
- {
- if(t < b->timeStamp)
- {
- return 1;
- }
- else
- {
- if(prev == NULL)
- {
- Bann* deleteMe = bl->root;
- bl->root = bl->root->next;
- free(deleteMe);
- }
- else
- {
- prev->next = b->next;
- free(b);
- }
- return 0;
- }
- }
-
- prev = b;
- b = b->next;
- }
- return 0;
- }
|