Browse code

- Updated to cached version - removed free_uri - Fixed db_query parameters

Jan Janak authored on 15/01/2003 00:13:59
Showing 10 changed files
... ...
@@ -1,10 +1,24 @@
1 1
 #
2
-# README of domain module -- Juha Heinanen Dec 27, 2002
2
+# README of domain module -- Juha Heinanen Dec 29, 2002
3 3
 #
4 4
 
5 5
 Domain module implements checks that based on domain table determine if
6 6
 a host part of an URI is "local" or not.  A "local" domain is one that
7
-the proxy is responsible of.
7
+the proxy is responsible for.
8
+
9
+Domain module operates in caching or non-caching mode depending on value
10
+of module parameter db_mode.  In caching mode domain module reads the
11
+contents of domain table into cache memory when the module is loaded.
12
+After that domain table is re-read only when module is given
13
+domain_reload fifo command.  Any changes in domain table must thus be
14
+followed by domain_reload command in order to reflect them in module
15
+behavior.  In non-caching mode domain module always queries domain table
16
+in the database.
17
+
18
+Caching is implemented using a hash table.  The size of the hash table
19
+is given by HASH_SIZE constant defined in domain_mod.h.  Its "factory
20
+default" value is 128.
21
+
8 22
 
9 23
 Depends on:
10 24
 ----------
... ...
@@ -14,27 +28,50 @@ mysql
14 28
 Exported parameters:
15 29
 -------------------
16 30
 
31
+Name:	 db_url
32
+Type:    string
33
+Default: "sql://serro:47serro11@localhost/ser"
34
+Desc:    URL for accessing the database.
35
+
36
+Name:	 db_mode
37
+Type:    int
38
+Default: 0
39
+Desc:    Database usage mode: 0 = non-caching, 1 = caching
40
+
17 41
 Name:	 domain_table
18 42
 Type:    string
19
-Default: "realm"
20
-Desc:    Name of table containing names of local realms that the proxy
43
+Default: "domain"
44
+Desc:    Name of table containing names of local domains that the proxy
21 45
 	 is responsible for.  Local users must have in their sip uri a
22
-	 host part that is equal to one of these realms.
46
+	 host part that is equal to one of these domains.
23 47
 
24 48
 Name:	 domain_domain_column
25 49
 Type:	 string
26
-Default: "realm"
27
-Desc:	 Name of column containing realms in realm table
50
+Default: "domain"
51
+Desc:	 Name of column containing domains in domain table
28 52
 
29 53
 Exported Functions:
30 54
 ------------------
31 55
 
32 56
 Name:	is_from_local
33 57
 Params:	-
34
-Desc:	Checks based on realm table if host part of From header uri is
35
-	one of the local realms that the proxy is responsible for
58
+Desc:	Checks based on domain table if host part of From header uri is
59
+	one of the local domains that the proxy is responsible for
36 60
 
37 61
 Name:	is_uri_host_local
38 62
 Params:	-
39
-Desc:	Checks based on realm table if request uri belongs to one of the
40
-	local realms
63
+Desc:	Checks based on domain table if request uri belongs to one of the
64
+	local domains
65
+
66
+Fifo Commands:
67
+-------------
68
+
69
+Name:	domain_reload
70
+Params: -
71
+Desc:	Causes domain module to re-read the contents of domain table
72
+	into cache memory.
73
+
74
+Name:	domain_dump
75
+Params: -
76
+Desc:	Causes domain module to dump hash indexes and domain names in
77
+	its cache memory.
... ...
@@ -28,6 +28,7 @@
28 28
 
29 29
 
30 30
 #include "domain_mod.h"
31
+#include "hash.h"
31 32
 #include "../../db/db.h"
32 33
 #include "../../parser/parse_uri.h"
33 34
 #include "../../parser/parse_from.h"
... ...
@@ -38,37 +39,43 @@
38 39
  */
39 40
 int is_domain_local(str* _host)
40 41
 {
41
-	db_key_t keys[] = {domain_domain_col};
42
-	db_val_t vals[1];
43
-	db_key_t cols[] = {domain_domain_col};
44
-	db_res_t* res;
42
+	if (db_mode == 0) {
43
+		db_key_t keys[] = {domain_domain_col};
44
+		db_val_t vals[1];
45
+		db_key_t cols[] = {domain_domain_col};
46
+		db_res_t* res;
45 47
 
46
-	if (db_use_table(db_handle, domain_table) < 0) {
47
-		LOG(L_ERR, "is_local(): Error while trying to use domain table\n");
48
-	}
48
+		if (db_use_table(db_handle, domain_table) < 0) {
49
+			LOG(L_ERR, "is_local(): Error while trying to use domain table\n");
50
+			return -1;
51
+		}
49 52
 
50
-	VAL_TYPE(vals) = DB_STR;
51
-	VAL_NULL(vals) = 0;
52
-    
53
-	VAL_STR(vals).s = _host->s;
54
-	VAL_STR(vals).len = _host->len;
53
+		VAL_TYPE(vals) = DB_STR;
54
+		VAL_NULL(vals) = 0;
55
+		
56
+		VAL_STR(vals).s = _host->s;
57
+		VAL_STR(vals).len = _host->len;
55 58
 
56
-	if (db_query(db_handle, keys, vals, cols, 1, 1, 0, &res) < 0) {
57
-		LOG(L_ERR, "is_local(): Error while querying database\n");
58
-		return -1;
59
-	}
59
+		if (db_query(db_handle, keys, 0, vals, cols, 1, 1, 0, &res) < 0) {
60
+			LOG(L_ERR, "is_local(): Error while querying database\n");
61
+			return -1;
62
+		}
60 63
 
61
-	if (RES_ROW_N(res) == 0) {
62
-		DBG("is_local(): Realm \'%.*s\' is not local\n", 
63
-		    _host->len, _host->s);
64
-		db_free_query(db_handle, res);
65
-		return -1;
64
+		if (RES_ROW_N(res) == 0) {
65
+			DBG("is_local(): Realm \'%.*s\' is not local\n", 
66
+			    _host->len, _host->s);
67
+			db_free_query(db_handle, res);
68
+			return -1;
69
+		} else {
70
+			DBG("is_local(): Realm \'%.*s\' is local\n", 
71
+			    _host->len, _host->s);
72
+			db_free_query(db_handle, res);
73
+			return 1;
74
+		}
66 75
 	} else {
67
-		DBG("is_local(): Realm \'%.*s\' is local\n", 
68
-		    _host->len, _host->s);
69
-		db_free_query(db_handle, res);
70
-		return 1;
76
+		return hash_table_lookup (_host->s, _host->len);
71 77
 	}
78
+			
72 79
 }
73 80
 
74 81
 /*
... ...
@@ -85,7 +92,7 @@ int is_from_local(struct sip_msg* _msg, char* _s1, char* _s2)
85 92
 	}
86 93
 
87 94
 	if (!(_msg->from->parsed)) {
88
-		if (parse_from_header(_msg->from) == -1) {
95
+		if (parse_from_header(_msg) == -1) {
89 96
 			LOG(L_ERR, "is_from_local(): Can't parse from header\n");
90 97
 			return -1;
91 98
 		}
... ...
@@ -97,7 +104,6 @@ int is_from_local(struct sip_msg* _msg, char* _s1, char* _s2)
97 104
 	}
98 105
 
99 106
 	ret = is_domain_local(&(uri.host));
100
-	free_uri(&uri);
101 107
 	return ret;
102 108
 }
103 109
 
... ...
@@ -1,8 +1,9 @@
1 1
 domain.o domain.d : domain.c domain_mod.h ../../db/db.h ../../db/db_key.h \
2
-  ../../db/db_val.h ../../str.h ../../db/db_con.h ../../db/db_row.h \
3
-  ../../db/db_res.h ../../parser/parse_uri.h ../../parser/msg_parser.h \
4
-  ../../data_lump.h ../../flags.h ../../ip_addr.h ../../dprint.h \
5
-  ../../md5utils.h ../../config.h ../../types.h ../../parser/parse_def.h \
6
-  ../../parser/parse_cseq.h ../../parser/parse_to.h \
7
-  ../../parser/parse_via.h ../../parser/parse_fline.h ../../parser/hf.h \
8
-  ../../parser/parse_from.h ../../ut.h
2
+  ../../db/db_op.h ../../db/db_val.h ../../str.h ../../db/db_con.h \
3
+  ../../db/db_row.h ../../db/db_res.h hash.h ../../parser/parse_uri.h \
4
+  ../../parser/msg_parser.h ../../data_lump.h ../../flags.h \
5
+  ../../ip_addr.h ../../dprint.h ../../md5utils.h ../../config.h \
6
+  ../../types.h ../../parser/parse_def.h ../../parser/parse_cseq.h \
7
+  ../../parser/parse_to.h ../../parser/parse_via.h \
8
+  ../../parser/parse_fline.h ../../parser/hf.h ../../parser/parse_from.h \
9
+  ../../ut.h
... ...
@@ -31,7 +31,7 @@
31 31
 #include <stdio.h>
32 32
 #include "../../sr_module.h"
33 33
 #include "domain.h"
34
-
34
+#include "fifo.h"
35 35
 
36 36
 /*
37 37
  * Module management function prototypes
... ...
@@ -40,20 +40,26 @@ static int mod_init(void);
40 40
 static void destroy(void);
41 41
 static int child_init(int rank);
42 42
 
43
+
43 44
 /*
44 45
  * Module parameter variables
45 46
  */
46 47
 char* db_url = "sql://serro:47serro11@localhost/ser";
48
+int db_mode = 0;                      /* Database usage mode: 0 = no cache, 1 = cache */
47 49
 char* domain_table = "domain";        /* Name of domain table */
48 50
 char* domain_domain_col = "domain";   /* Name of domain column */
49 51
 
50
-
51 52
 /*
52 53
  * Other module variables
53 54
  */
54
-db_con_t* db_handle;   /* Database connection handle */
55
-
55
+db_con_t* db_handle = NULL;                  /* Database connection handle */
56
+struct domain_list *hash_table_1[HASH_SIZE]; /* Hash table for domains */
57
+struct domain_list *hash_table_2[HASH_SIZE]; /* Hash table for domains */
58
+struct domain_list **current_hash_table;
56 59
 
60
+/*
61
+ * Module interface
62
+ */
57 63
 struct module_exports exports = {
58 64
 	"domain", 
59 65
 	(char*[]) {"is_from_local", "is_uri_host_local"},
... ...
@@ -61,10 +67,10 @@ struct module_exports exports = {
61 67
 	(int[]) {0, 0},
62 68
 	(fixup_function[]) {0, 0},
63 69
 	2, /* number of functions*/
64
-	(char*[]){"db_url", "domain_table", "domain_domain_column"},
65
-	(modparam_t[]){STR_PARAM, STR_PARAM, STR_PARAM},
66
-	(void*[]){&db_url, &domain_table, &domain_domain_col},
67
-	3,
70
+	(char*[]){"db_url", "db_mode", "domain_table", "domain_domain_column"},
71
+	(modparam_t[]){STR_PARAM, INT_PARAM, STR_PARAM, STR_PARAM},
72
+	(void*[]){&db_url, &db_mode, &domain_table, &domain_domain_col},
73
+	4,
68 74
 	
69 75
 	mod_init,  /* module initialization function */
70 76
 	NULL,      /* response function*/
... ...
@@ -76,30 +82,58 @@ struct module_exports exports = {
76 82
 
77 83
 static int mod_init(void)
78 84
 {
79
-	printf("Domain module - initializing\n");
85
+	int i;
86
+
87
+	fprintf(stderr, "domain - initializing\n");
80 88
 	
81
-	/* Check if database modulke has been laoded */
89
+	/* Check if database module has been laoded */
82 90
 	if (bind_dbmod()) {
83 91
 		LOG(L_ERR, "domain:mod_init(): Unable to bind database module\n");
84 92
 		return -1;
85 93
 	}
86 94
 
95
+	/* Check if cache needs to be loaded from domain table */
96
+	if (db_mode == 1) {
97
+		db_handle = db_init(db_url);
98
+		if (!db_handle) {
99
+			LOG(L_ERR, "domain:mod_init(): Unable to connect database\n");
100
+			return -1;
101
+		}
102
+		
103
+		/* Initialize fifo interface */
104
+		(void)init_domain_fifo();
105
+
106
+		/* Initializing hash tables and hash table variable */
107
+		for (i = 0; i < HASH_SIZE; i++) {
108
+			hash_table_1[i] = hash_table_2[i] = (struct domain_list *)0;
109
+		}
110
+		current_hash_table = hash_table_1;
111
+		if (reload_domain_table() == -1) {
112
+			LOG(L_CRIT, "domain:mod_init(): Domain table reload failed\n");
113
+			return -1;
114
+		}
115
+	}
116
+
87 117
 	return 0;
88 118
 }
89 119
 
90 120
 
91 121
 static int child_init(int rank)
92 122
 {
93
-	if (db_url == NULL) {
94
-		LOG(L_ERR, "domain:init_child(): Use db_url parameter\n");
95
-		return -1;
123
+	/* Check if database is needed by child */
124
+	if (db_mode == 0) {
125
+		if (db_url == NULL) {
126
+			LOG(L_ERR, "domain:child_init(): Use db_url parameter\n");
127
+			return -1;
128
+		}
129
+
130
+		db_handle = db_init(db_url);
131
+		if (!db_handle) {
132
+			LOG(L_ERR, "domain:child_init(): Unable to connect database\n");
133
+			return -1;
134
+		}
96 135
 	}
97 136
 
98
-	db_handle = db_init(db_url);
99
-	if (!db_handle) {
100
-		LOG(L_ERR, "auth:init_child(): Unable to connect database\n");
101
-		return -1;
102
-	}
103 137
 	return 0;
104 138
 
105 139
 }
... ...
@@ -107,5 +141,5 @@ static int child_init(int rank)
107 141
 
108 142
 static void destroy(void)
109 143
 {
110
-	db_close(db_handle);
144
+	if (db_handle) db_close(db_handle);
111 145
 }
... ...
@@ -1,8 +1,8 @@
1 1
 domain_mod.o domain_mod.d : domain_mod.c domain_mod.h ../../db/db.h ../../db/db_key.h \
2
-  ../../db/db_val.h ../../str.h ../../db/db_con.h ../../db/db_row.h \
3
-  ../../db/db_res.h ../../sr_module.h ../../parser/msg_parser.h \
4
-  ../../data_lump.h ../../flags.h ../../ip_addr.h ../../dprint.h \
5
-  ../../md5utils.h ../../config.h ../../types.h ../../parser/parse_def.h \
6
-  ../../parser/parse_cseq.h ../../parser/parse_to.h \
7
-  ../../parser/parse_via.h ../../parser/parse_fline.h ../../parser/hf.h \
8
-  domain.h
2
+  ../../db/db_op.h ../../db/db_val.h ../../str.h ../../db/db_con.h \
3
+  ../../db/db_row.h ../../db/db_res.h ../../sr_module.h \
4
+  ../../parser/msg_parser.h ../../data_lump.h ../../flags.h \
5
+  ../../ip_addr.h ../../dprint.h ../../md5utils.h ../../config.h \
6
+  ../../types.h ../../parser/parse_def.h ../../parser/parse_cseq.h \
7
+  ../../parser/parse_to.h ../../parser/parse_via.h \
8
+  ../../parser/parse_fline.h ../../parser/hf.h domain.h fifo.h
... ...
@@ -35,10 +35,23 @@
35 35
 
36 36
 
37 37
 /*
38
- * Module parameters variables
38
+ * Constants
39
+ */
40
+#define HASH_SIZE 128
41
+
42
+/*
43
+ * Type definitions
39 44
  */
45
+struct domain_list {
46
+	char *domain;
47
+	struct domain_list *next;
48
+};
40 49
 
50
+/*
51
+ * Module parameters variables
52
+ */
41 53
 extern char* db_url;              /* Database URL */
54
+extern int db_mode;               /* Database usage mode: 0 = no cache, 1 = cache */
42 55
 extern char* domain_table;        /* Domain table name */
43 56
 extern char* domain_domain_col;   /* Domain column name */
44 57
 
... ...
@@ -46,8 +59,10 @@ extern char* domain_domain_col;   /* Domain column name */
46 59
 /*
47 60
  * Other module variables
48 61
  */
49
- 
50 62
 extern db_con_t* db_handle; /* Database connection handle */
63
+extern struct domain_list *hash_table_1[HASH_SIZE]; /* Hash table for domains */
64
+extern struct domain_list *hash_table_2[HASH_SIZE]; /* Hash table for domains */
65
+extern struct domain_list **current_hash_table;
51 66
 
52 67
 
53 68
 #endif /* DOMAIN_MOD_H */
54 69
new file mode 100644
... ...
@@ -0,0 +1,155 @@
1
+/* fifo.c v 0.1 2002/12/28
2
+ *
3
+ * Domain fifo functions
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#include "domain_mod.h"
31
+#include "hash.h"
32
+#include "fifo.h"
33
+#include "../../fifo_server.h"
34
+#include "../../dprint.h"
35
+#include "../../db/db.h"
36
+
37
+
38
+/*
39
+ * Reload domain table to new hash table and when done, make new hash table
40
+ * current one.
41
+ */
42
+int reload_domain_table ( void )
43
+{
44
+/*	db_key_t keys[] = {domain_domain_col}; */
45
+	db_val_t vals[1];
46
+	db_key_t cols[] = {domain_domain_col};
47
+	db_res_t* res;
48
+	db_row_t* row;
49
+	db_val_t* val;
50
+
51
+	struct domain_list **new_hash_table;
52
+	int i;
53
+
54
+	if (db_use_table(db_handle, domain_table) < 0) {
55
+		LOG(L_ERR, "reload_domain_table(): Error while trying to use domain table\n");
56
+		return -1;
57
+	}
58
+
59
+	VAL_TYPE(vals) = DB_STR;
60
+	VAL_NULL(vals) = 0;
61
+    
62
+	if (db_query(db_handle, NULL, 0, NULL, cols, 0, 1, 0, &res) < 0) {
63
+		LOG(L_ERR, "reload_domain_table(): Error while querying database\n");
64
+		return -1;
65
+	}
66
+
67
+	/* Choose new hash table and free its old contents */
68
+	if (current_hash_table == hash_table_1) {
69
+		hash_table_free(hash_table_2);
70
+		new_hash_table = hash_table_2;
71
+	} else {
72
+		hash_table_free(hash_table_1);
73
+		new_hash_table = hash_table_1;
74
+	}
75
+
76
+	row = RES_ROWS(res);
77
+
78
+	DBG("Number of rows in domain table: %d\n", RES_ROW_N(res));
79
+		
80
+	for (i = 0; i < RES_ROW_N(res); i++) {
81
+		val = ROW_VALUES(row + i);
82
+		if ((ROW_N(row) == 1) && (VAL_TYPE(val) == DB_STRING)) {
83
+			
84
+			DBG("Value: %s inserted into domain hash table\n", VAL_STRING(val));
85
+
86
+			if (hash_table_install(new_hash_table, (char *)(VAL_STRING(val))) == -1) {
87
+				LOG(L_ERR, "domain_reload(): Hash table problem\n");
88
+				db_free_query(db_handle, res);
89
+				return -1;
90
+			}
91
+		} else {
92
+			LOG(L_ERR, "domain_reload(): Database problem\n");
93
+			db_free_query(db_handle, res);
94
+			return -1;
95
+		}
96
+	}
97
+	db_free_query(db_handle, res);
98
+
99
+	current_hash_table = new_hash_table;
100
+	
101
+	return 1;
102
+}
103
+
104
+
105
+/*
106
+ * Fifo function to reload domain table
107
+ */
108
+static int domain_reload ( FILE* pipe, char* response_file )
109
+{
110
+	if (reload_domain_table () == 1) {
111
+		fifo_reply (response_file, "200 OK\n");
112
+		return 1;
113
+	} else {
114
+		fifo_reply (response_file, "400 Domain table reload failed\n");
115
+		return -1;
116
+	}
117
+}
118
+
119
+
120
+/*
121
+ * Fifo function to print domains from current hash table
122
+ */
123
+static int domain_dump ( FILE* pipe, char* response_file )
124
+{
125
+	FILE *reply_file;
126
+	
127
+	reply_file=open_reply_pipe(response_file);
128
+	if (reply_file==0) {
129
+		LOG(L_ERR, "domain_dump(): Opening of response file failed\n");
130
+		return -1;
131
+	}
132
+	fputs( "200 OK\n", reply_file );
133
+	hash_table_print( current_hash_table, reply_file );
134
+	fclose(reply_file);
135
+	return 1;
136
+}
137
+
138
+
139
+/*
140
+ * Register domain fifo functions
141
+ */
142
+int init_domain_fifo( void ) 
143
+{
144
+	if (register_fifo_cmd(domain_reload, DOMAIN_RELOAD, 0) < 0) {
145
+		LOG(L_CRIT, "Cannot register domain_reload\n");
146
+		return -1;
147
+	}
148
+
149
+	if (register_fifo_cmd(domain_dump, DOMAIN_DUMP, 0) < 0) {
150
+		LOG(L_CRIT, "Cannot register domain_dump");
151
+		return -1;
152
+	}
153
+
154
+	return 1;
155
+}
0 156
new file mode 100644
... ...
@@ -0,0 +1,42 @@
1
+/* fifo.h v 0.1 2002/12/28
2
+ *
3
+ * Header file for domain fifo functions
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#ifndef _FIFO_H
31
+#define _FIFO_H
32
+
33
+
34
+#define DOMAIN_RELOAD "domain_reload"
35
+#define DOMAIN_DUMP "domain_dump"
36
+
37
+
38
+int init_domain_fifo( void );
39
+int reload_domain_table( void );
40
+
41
+
42
+#endif
0 43
new file mode 100644
... ...
@@ -0,0 +1,119 @@
1
+/* hash.c v 0.1 2002/12/28
2
+ *
3
+ * Hash functions for cached domain table
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#include "domain_mod.h"
31
+#include "../../dprint.h"
32
+#include <stdlib.h>
33
+#include <string.h>
34
+#include <stdio.h>
35
+
36
+
37
+/* String hash function */
38
+unsigned int hash (char *v)
39
+{
40
+  char *p;
41
+  unsigned int h=0;
42
+
43
+  for (p = v; *p != '\0'; p += 1) {
44
+	  h = ( h << 5 ) - h + *p;
45
+  }
46
+
47
+  return h % HASH_SIZE;
48
+}
49
+
50
+/* Add domain to hash table */
51
+int hash_table_install (struct domain_list **hash_table, char *domain)
52
+{
53
+	struct domain_list *np;
54
+	unsigned int hash_val;
55
+
56
+	np = (struct domain_list *) malloc(sizeof(*np));
57
+	if (np == NULL) {
58
+		LOG(L_CRIT, "hash_install(): Cannot allocate memory for table entry\n");
59
+		return -1;
60
+	}
61
+	np->domain = strdup(domain);
62
+	if (np->domain == NULL) {
63
+		LOG(L_CRIT, "hash_install(): Cannot allocate memory for domain string\n");
64
+		return -1;
65
+	}
66
+
67
+	hash_val = hash(np->domain);
68
+	np->next = hash_table[hash_val];
69
+	hash_table[hash_val] = np;
70
+
71
+	return 1;
72
+}
73
+
74
+
75
+/* Check if domain exsist in hash table */
76
+int hash_table_lookup (char *domain, int len)
77
+{
78
+	struct domain_list *np;
79
+
80
+	for (np = current_hash_table[hash(domain)]; np != NULL; np = np->next)
81
+		if (strncasecmp(domain, np->domain, len) == 0) return 1;
82
+	return -1;
83
+
84
+}
85
+
86
+
87
+/* Print domains stored in hash table */
88
+void hash_table_print (struct domain_list **hash_table, FILE *reply_file)
89
+{
90
+	int i;
91
+	struct domain_list *np;
92
+
93
+	for (i = 0; i < HASH_SIZE; i++) {
94
+		np = hash_table[i];
95
+		while (np) {
96
+			fprintf(reply_file, "%4d %s\n", i, np->domain);
97
+			np = np->next;
98
+		}
99
+	}
100
+}
101
+
102
+
103
+/* Free contents of hash table */
104
+void hash_table_free (struct domain_list **hash_table)
105
+{
106
+	int i;
107
+	struct domain_list *np, *next;
108
+
109
+	for (i = 0; i < HASH_SIZE; i++) {
110
+		np = hash_table[i];
111
+		while (np) {
112
+			free(np->domain);
113
+			next = np->next;
114
+			free(np);
115
+			np = next;
116
+		}
117
+		hash_table[i] = NULL;
118
+	}
119
+}
0 120
new file mode 100644
... ...
@@ -0,0 +1,40 @@
1
+/* hash.h v 0.1 2002/12/28
2
+ *
3
+ * Header file for hash table functions
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#ifndef _HASH_H
31
+#define _HASH_H
32
+
33
+#include <stdio.h>
34
+
35
+extern int hash_table_install (struct domain_list **hash_table, char *domain);
36
+int hash_table_lookup (char *domain, int len);
37
+extern void hash_table_print (struct domain_list **hash_table, FILE *reply_file);
38
+extern void hash_table_free (struct domain_list **hash_table);
39
+
40
+#endif