Browse code

Changed domain field in hash table from char* to str

Juha Heinanen authored on 01/04/2003 17:51:54
Showing 6 changed files
... ...
@@ -45,7 +45,7 @@ Desc:    Name of table containing names of local domains that the proxy
45 45
 	 is responsible for.  Local users must have in their sip uri a
46 46
 	 host part that is equal to one of these domains.
47 47
 
48
-Name:	 domain_column
48
+Name:	 domain_col
49 49
 Type:	 string
50 50
 Default: "domain"
51 51
 Desc:	 Name of column containing domains in domain table
... ...
@@ -35,6 +35,7 @@
35 35
 #include "../../parser/parse_from.h"
36 36
 #include "../../ut.h"
37 37
 
38
+
38 39
 /*
39 40
  * Check that From header is properly parsed and if so,
40 41
  * return pointer to parsed From header.  Otherwise return NULL.
... ...
@@ -57,6 +58,7 @@ inline struct to_body *get_parsed_from_body(struct sip_msg *_msg)
57 58
  */
58 59
 int is_domain_local(str* _host)
59 60
 {
61
+	LOG(L_ERR, "is_local(): \'%.*s\'\n", _host->len, _host->s);
60 62
 	if (db_mode == 0) {
61 63
 		db_key_t keys[1];
62 64
 		db_val_t vals[1];
... ...
@@ -94,7 +96,7 @@ int is_domain_local(str* _host)
94 96
 			return 1;
95 97
 		}
96 98
 	} else {
97
-		return hash_table_lookup (_host->s, _host->len);
99
+		return hash_table_lookup (_host);
98 100
 	}
99 101
 			
100 102
 }
... ...
@@ -52,17 +52,17 @@ static int child_init(int rank);
52 52
  * Module parameter variables
53 53
  */
54 54
 char* db_url = "sql://serro:47serro11@localhost/ser";
55
-int db_mode = 0;                      /* Database usage mode: 0 = no cache, 1 = cache */
56
-char* domain_table = "domain";        /* Name of domain table */
57
-char* domain_col = "domain";          /* Name of domain column */
55
+int db_mode = 0;			/* Database usage mode: 0 = no cache, 1 = cache */
56
+char* domain_table = "domain";          /* Name of domain table */
57
+char* domain_col = "domain";            /* Name of domain column */
58 58
 
59 59
 /*
60 60
  * Other module variables
61 61
  */
62
-db_con_t* db_handle = NULL;                  /* Database connection handle */
63
-struct domain_list ***hash_table;            /* Pointer to current hash table pointer */
64
-struct domain_list **hash_table_1;           /* Pointer to hash table 1 */
65
-struct domain_list **hash_table_2;           /* Pointer to hash table 2 */
62
+db_con_t* db_handle = NULL;		/* Database connection handle */
63
+struct domain_list ***hash_table;	/* Pointer to current hash table pointer */
64
+struct domain_list **hash_table_1;	/* Pointer to hash table 1 */
65
+struct domain_list **hash_table_2;	/* Pointer to hash table 2 */
66 66
 
67 67
 
68 68
 /*
... ...
@@ -79,10 +79,10 @@ static cmd_export_t cmds[] = {
79 79
  * Exported parameters
80 80
  */
81 81
 static param_export_t params[] = {
82
-	{"db_url",               STR_PARAM, &db_url      },
83
-	{"db_mode",              INT_PARAM, &db_mode     },
84
-	{"domain_table",         STR_PARAM, &domain_table},
85
-	{"domain_column",        STR_PARAM, &domain_col  },
82
+	{"db_url",		STR_PARAM, &db_url	},
83
+	{"db_mode",             INT_PARAM, &db_mode     },
84
+	{"domain_table",        STR_PARAM, &domain_table},
85
+	{"domain_col",          STR_PARAM, &domain_col  },
86 86
 	{0, 0, 0}
87 87
 };
88 88
 
... ...
@@ -44,23 +44,23 @@
44 44
  * Type definitions
45 45
  */
46 46
 struct domain_list {
47
-	char *domain;
47
+	str domain;
48 48
 	struct domain_list *next;
49 49
 };
50 50
 
51 51
 /*
52 52
  * Module parameters variables
53 53
  */
54
-extern char* db_url;        /* Database URL */
55
-extern int db_mode;         /* Database usage mode: 0 = no cache, 1 = cache */
56
-extern char* domain_table;  /* Domain table name */
57
-extern char* domain_col;    /* Domain column name */
54
+extern char* db_url;            /* Database URL */
55
+extern int db_mode;             /* Database usage mode: 0 = no cache, 1 = cache */
56
+extern char* domain_table;	/* Domain table name */
57
+extern char* domain_col;	/* Domain column name */
58 58
 
59 59
 
60 60
 /*
61 61
  * Other module variables
62 62
  */
63
-extern db_con_t* db_handle; /* Database connection handle */
63
+extern db_con_t* db_handle;	/* Database connection handle */
64 64
 extern struct domain_list **hash_table_1; /* Hash table for domains */
65 65
 extern struct domain_list **hash_table_2; /* Hash table for domains */
66 66
 extern struct domain_list ***hash_table;  /* Current hash table */
... ...
@@ -37,18 +37,24 @@
37 37
 
38 38
 
39 39
 /* String hash function */
40
-unsigned int hash (char *v)
40
+unsigned int hash (str *domain)
41 41
 {
42 42
   char *p;
43 43
   unsigned int h=0;
44
+  unsigned int len;
45
+  unsigned int i;
44 46
 
45
-  for (p = v; *p != '\0'; p += 1) {
46
-	  h = ( h << 5 ) - h + *p;
47
+  p = domain->s;
48
+  len = domain->len;
49
+
50
+  for (i = 0; i < len; i++) {
51
+	  h = ( h << 5 ) - h + *(p + i);
47 52
   }
48 53
 
49 54
   return h % HASH_SIZE;
50 55
 }
51 56
 
57
+
52 58
 /* Add domain to hash table */
53 59
 int hash_table_install (struct domain_list **hash_table, char *domain)
54 60
 {
... ...
@@ -61,14 +67,15 @@ int hash_table_install (struct domain_list **hash_table, char *domain)
61 67
 		return -1;
62 68
 	}
63 69
 
64
-	np->domain = (char *) shm_malloc(strlen(domain) + 1);
65
-	if (np->domain == NULL) {
70
+	np->domain.len = strlen(domain);
71
+	np->domain.s = (char *) shm_malloc(np->domain.len);
72
+	if (np->domain.s == NULL) {
66 73
 		LOG(L_CRIT, "hash_install(): Cannot allocate memory for domain string\n");
67 74
 		return -1;
68 75
 	}
69
-	(void) strcpy(np->domain, domain);
76
+	(void) strncpy(np->domain.s, domain, np->domain.len);
70 77
 
71
-	hash_val = hash(np->domain);
78
+	hash_val = hash(&(np->domain));
72 79
 	np->next = hash_table[hash_val];
73 80
 	hash_table[hash_val] = np;
74 81
 
... ...
@@ -77,14 +84,19 @@ int hash_table_install (struct domain_list **hash_table, char *domain)
77 84
 
78 85
 
79 86
 /* Check if domain exsist in hash table */
80
-int hash_table_lookup (char *domain, int len)
87
+int hash_table_lookup (str *domain)
81 88
 {
82 89
 	struct domain_list *np;
83 90
 
84
-	for (np = (*hash_table)[hash(domain)]; np != NULL; np = np->next)
85
-		if (strncasecmp(domain, np->domain, len) == 0) return 1;
86
-	return -1;
91
+	LOG(L_ERR, "looking hash entry %d for domain \'%.*s\'\n", hash(domain), domain->len, domain->s);
87 92
 
93
+	for (np = (*hash_table)[hash(domain)]; np != NULL; np = np->next) {
94
+		if ((np->domain.len == domain->len) && 
95
+		    (strncasecmp(np->domain.s, domain->s, domain->len) == 0)) {
96
+			return 1;
97
+		}
98
+	}
99
+	return -1;
88 100
 }
89 101
 
90 102
 
... ...
@@ -97,7 +109,7 @@ void hash_table_print (struct domain_list **hash_table, FILE *reply_file)
97 109
 	for (i = 0; i < HASH_SIZE; i++) {
98 110
 		np = hash_table[i];
99 111
 		while (np) {
100
-			fprintf(reply_file, "%4d %s\n", i, np->domain);
112
+			fprintf(reply_file, "%4d %.*s\n", i, np->domain.len, np->domain.s);
101 113
 			np = np->next;
102 114
 		}
103 115
 	}
... ...
@@ -113,7 +125,7 @@ void hash_table_free (struct domain_list **hash_table)
113 125
 	for (i = 0; i < HASH_SIZE; i++) {
114 126
 		np = hash_table[i];
115 127
 		while (np) {
116
-			shm_free(np->domain);
128
+			shm_free(np->domain.s);
117 129
 			next = np->next;
118 130
 			shm_free(np);
119 131
 			np = next;
... ...
@@ -34,7 +34,7 @@
34 34
 #include <stdio.h>
35 35
 
36 36
 extern int hash_table_install (struct domain_list **hash_table, char *domain);
37
-int hash_table_lookup (char *domain, int len);
37
+int hash_table_lookup (str *domain);
38 38
 extern void hash_table_print (struct domain_list **hash_table, FILE *reply_file);
39 39
 extern void hash_table_free (struct domain_list **hash_table);
40 40