hash.h

  1. #ifndef HASH_H 
  2. #define HASH_H              1 
  3.  
  4. #include <stdio.h> 
  5. #include <time.h> 
  6. #include <string.h> 
  7. #include <malloc.h> 
  8. #include <list.h> 
  9.  
  10. #define NMN_USER_HASH_LIST_MAX      1024 
  11.  
  12. struct user { 
  13.     struct hlist_node   sibling; 
  14.     char            username[32]; 
  15. /* 
  16.  *  char            userpwd[NMN_VALUE_MAX_LEN]; 
  17.  */ 
  18.     time_t          starttime; 
  19.     time_t          endtime; 
  20. }; 
  21.  
  22. extern int user_count; 
  23. extern int hash_test[NMN_USER_HASH_LIST_MAX]; 
  24. extern struct hlist_head hash_listhead[NMN_USER_HASH_LIST_MAX]; 
  25. /* 
  26. int str2user(struct user *user_entry, const char *username, const char *userpwd); 
  27. int user2str(char *buf, int buf_len, const struct user *user_entry); 
  28. //*/ 
  29. int read_users_list(const char *file_path); 
  30. struct user *user_hash_get(const char *username); 
  31. //#undef HASH_LIST_MAX 
  32.  
  33.  
  34.  
  35. #endif /* HASH_H */ 

hash.c

  1. #include <hash.h> 
  2. struct hlist_head hash_listhead[NMN_USER_HASH_LIST_MAX]; 
  3. int user_count; 
  4. static unsigned int bkdrhash(const char *str) 
  5.     unsigned int seed = 1313; //13 131 1313 13131 131313 etc... 
  6.     unsigned int hash = 0; 
  7.     while(*str) { 
  8.         hash = hash * seed + (unsigned int)(*str++); 
  9.     } 
  10.     return hash & 0x7FFFFFFF; 
  11. static int get_name_hash_index(const char *username) 
  12.     unsigned int key = bkdrhash(username); 
  13.     int index = key & (NMN_USER_HASH_LIST_MAX-1); 
  14.     return index; 
  15. static int user_hash_push(struct user *entry) 
  16.     int index = get_name_hash_index(entry->username); 
  17.     hlist_add_head(&entry->sibling, &hash_listhead[index]); 
  18.     return 0; 
  19. /* 
  20. static int user_cmp(const struct user *a, const struct user *b) 
  21. { 
  22.     return strcmp(a->username, b->username); 
  23. } 
  24. //*/ 
  25. struct user *user_hash_get(const char *username) 
  26.     struct user *tpos; 
  27.     struct user *ret = NULL; 
  28.     struct hlist_node *pos; 
  29.     int index = get_name_hash_index(username); 
  30.     hlist_for_each_entry(tpos, pos, &hash_listhead[index], sibling) { 
  31.         fprintf(stdout, "username=%s...\n", tpos->username); 
  32.         if(0==strcmp(username, tpos->username)) { 
  33.             ret = tpos; 
  34.             break
  35.         } 
  36.     } 
  37.     return ret; 
  38. /* 
  39. static int line2user(struct user *entry, const char *line) 
  40. { 
  41.     sscanf(line, "%s Cleartext-Password := \"%[^\"]%*[ #\"]%ld-%ld", 
  42.                  entry->username, entry->userpwd, &entry->starttime, &entry->endtime); 
  43.     return 0; 
  44. } 
  45. //*/ 
  46. /* without userpwd */ 
  47. static int line2user(struct user *entry, const char *line) 
  48.     return sscanf(line, "%s Cleartext-Password := \"%*[^\"]%*[ #\"]%ld-%ld"
  49.                  entry->username, &entry->starttime, &entry->endtime); 
  50. int read_users_list(const char *dir_path) 
  51.     int ret = 0; 
  52.     user_count = 0; 
  53.     char file_path[1024]; 
  54.     snprintf(file_path, sizeof(file_path), "%s", dir_path); 
  55.     FILE *fp = fopen(file_path, "r"); 
  56.     if(NULL==fp) { 
  57.         return -1; 
  58.     } 
  59.     char line[1024]; 
  60.     struct user *entry; 
  61.     while(NULL != (fgets(line, sizeof(line), fp))) { 
  62.         if(('#'==*line) || (!strstr(line, "Cleartext-Password"))) { 
  63.             continue
  64.         } 
  65.         entry = (struct user *)malloc(sizeof(struct user)); 
  66.         if(!entry) { 
  67.             ret = -1; 
  68.             break
  69.         } 
  70.         line2user(entry, line); 
  71.         user_hash_push(entry); 
  72.         ++user_count; 
  73.     } 
  74.     fclose(fp); 
  75.     return ret; 
  76. /* no free hash list method */