1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,150 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ */ |
|
4 |
+ |
|
5 |
+ |
|
6 |
+#ifndef _CRC_H_ |
|
7 |
+#define _CRC_H_ |
|
8 |
+ |
|
9 |
+extern unsigned long int crc_32_tab[]; |
|
10 |
+extern unsigned short int ccitt_tab[]; |
|
11 |
+extern unsigned short int crc_16_tab[]; |
|
12 |
+ |
|
13 |
+#endif |
|
14 |
+ |
|
15 |
+#include <stdio.h> |
|
16 |
+#include <string.h> |
|
17 |
+#include <stdlib.h> |
|
18 |
+#include "hash_func.h" |
|
19 |
+#include "dprint.h" |
|
20 |
+#include "crc.h" |
|
21 |
+#include "ut.h" |
|
22 |
+ |
|
23 |
+int old_hash( str call_id, str cseq_nr ) |
|
24 |
+{ |
|
25 |
+ int hash_code = 0; |
|
26 |
+ int i; |
|
27 |
+ |
|
28 |
+#if 0 /*def i386*/ |
|
29 |
+ int ci_len, cs_len; |
|
30 |
+ char *ci, *cs; |
|
31 |
+ |
|
32 |
+ trim_len( ci_len, ci, call_id ); |
|
33 |
+ trim_len( cs_len, cs, cseq_nr ); |
|
34 |
+ |
|
35 |
+ int dummy1; |
|
36 |
+ if (call_id.len>=4){ |
|
37 |
+ asm( |
|
38 |
+ "1: \n\r" |
|
39 |
+ "addl (%1), %0 \n\r" |
|
40 |
+ "add $4, %1 \n\r" |
|
41 |
+ "cmp %2, %1 \n\r" |
|
42 |
+ "jl 1b \n\r" |
|
43 |
+ : "=r"(hash_code), "=r"(dummy1) |
|
44 |
+ : "0" (hash_code), "1"(ci), |
|
45 |
+ "r"( (ci_len & (~3)) +ci) |
|
46 |
+ ); |
|
47 |
+ } |
|
48 |
+#else |
|
49 |
+ if ( call_id.len>0 ) |
|
50 |
+ for( i=0 ; i<call_id.len ; hash_code+=call_id.s[i++] ); |
|
51 |
+#endif |
|
52 |
+ |
|
53 |
+#if 0 /*def i386*/ |
|
54 |
+ |
|
55 |
+ int dummy2; |
|
56 |
+ if (cseq_nr.len>=4){ |
|
57 |
+ asm( |
|
58 |
+ "1: \n\r" |
|
59 |
+ "addl (%1), %0 \n\r" |
|
60 |
+ "add $4, %1 \n\r" |
|
61 |
+ "cmp %2, %1 \n\r" |
|
62 |
+ "jl 1b \n\r" |
|
63 |
+ : "=r"(hash_code), "=r"(dummy2) |
|
64 |
+ : "0" (hash_code), "1"(cs), |
|
65 |
+ "r"((cs_len & (~3) )+ cs) |
|
66 |
+ ); |
|
67 |
+ } |
|
68 |
+#else |
|
69 |
+ if ( cseq_nr.len>0 ) |
|
70 |
+ for( i=0 ; i<cseq_nr.len ; hash_code+=cseq_nr.s[i++] ); |
|
71 |
+#endif |
|
72 |
+ return hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */ |
|
73 |
+} |
|
74 |
+ |
|
75 |
+int new_hash( str call_id, str cseq_nr ) |
|
76 |
+{ |
|
77 |
+ int hash_code = 0; |
|
78 |
+ int i,j, k, third; |
|
79 |
+ int ci_len, cs_len; |
|
80 |
+ char *ci, *cs; |
|
81 |
+ |
|
82 |
+ /* trim EoLs */ |
|
83 |
+/* |
|
84 |
+ ci_len = call_id.len; |
|
85 |
+ while (ci_len && ((c=call_id.s[ci_len-1])==0 || c=='\r' || c=='\n')) |
|
86 |
+ ci_len--; |
|
87 |
+ cs_len = cseq_nr.len; |
|
88 |
+ while (cs_len && ((c=cseq_nr.s[cs_len-1])==0 || c=='\r' || c=='\n')) |
|
89 |
+ cs_len--; |
|
90 |
+*/ |
|
91 |
+ trim_len( ci_len, ci, call_id ); |
|
92 |
+ trim_len( cs_len, cs, cseq_nr ); |
|
93 |
+ |
|
94 |
+ /* run the cycle from the end ... we are interested in the |
|
95 |
+ most-right digits ... and just take the %10 value of it |
|
96 |
+ */ |
|
97 |
+ third=(ci_len-1)/3; |
|
98 |
+ for ( i=ci_len-1, j=2*third, k=third; |
|
99 |
+ k>0 ; i--, j--, k-- ) { |
|
100 |
+ hash_code+=crc_16_tab[(unsigned char)(*(ci+i)) /*+7*/ ]+ |
|
101 |
+ ccitt_tab[*(ci+k)+63]+ |
|
102 |
+ ccitt_tab[*(ci+j)+13]; |
|
103 |
+ } |
|
104 |
+ for( i=0 ; i<cs_len ; i++ ) |
|
105 |
+ //hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243]; |
|
106 |
+ hash_code+=ccitt_tab[*(cs+i)+123]; |
|
107 |
+ |
|
108 |
+ hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */ |
|
109 |
+ return hash_code; |
|
110 |
+} |
|
111 |
+ |
|
112 |
+void hashtest_cycle( int hits[TABLE_ENTRIES], char *ip ) |
|
113 |
+{ |
|
114 |
+ long int i,j,k, l; |
|
115 |
+ int hashv; |
|
116 |
+ static char buf1[1024]; |
|
117 |
+ static char buf2[1024]; |
|
118 |
+ str call_id; |
|
119 |
+ str cseq; |
|
120 |
+ |
|
121 |
+ call_id.s=buf1; |
|
122 |
+ cseq.s=buf2; |
|
123 |
+ |
|
124 |
+ for (i=987654328;i<987654328+10;i++) |
|
125 |
+ for (j=85296341;j<85296341+10;j++) |
|
126 |
+ for (k=987654;k<=987654+10;k++) |
|
127 |
+ for (l=101;l<201;l++) { |
|
128 |
+ call_id.len=sprintf( buf1, "%d-%d-%d@%s",(int)i,(int)j, |
|
129 |
+ (int)k, ip ); |
|
130 |
+ cseq.len=sprintf( buf2, "%d", (int)l ); |
|
131 |
+ printf("%s\t%s\n", buf1, buf2 ); |
|
132 |
+ hashv=hash( call_id, cseq ); |
|
133 |
+ hits[ hashv ]++; |
|
134 |
+ } |
|
135 |
+} |
|
136 |
+ |
|
137 |
+void hashtest() |
|
138 |
+{ |
|
139 |
+ int hits[TABLE_ENTRIES]; |
|
140 |
+ int i; |
|
141 |
+ |
|
142 |
+ memset( hits, 0, sizeof hits ); |
|
143 |
+ hashtest_cycle( hits, "192.168.99.100" ); |
|
144 |
+ hashtest_cycle( hits, "172.168.99.100" ); |
|
145 |
+ hashtest_cycle( hits, "142.168.99.100" ); |
|
146 |
+ for (i=0; i<TABLE_ENTRIES; i++) |
|
147 |
+ printf("[%d. %d]\n", i, hits[i] ); |
|
148 |
+ exit(0); |
|
149 |
+} |
|
150 |
+ |
0 | 151 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,20 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ */ |
|
4 |
+ |
|
5 |
+ |
|
6 |
+#ifndef _HASH_H |
|
7 |
+#define _HASH_H |
|
8 |
+ |
|
9 |
+#include "str.h" |
|
10 |
+ |
|
11 |
+/* always use a power of 2 for hash table size */ |
|
12 |
+#define T_TABLE_POWER 12 |
|
13 |
+#define TABLE_ENTRIES (1 << (T_TABLE_POWER)) |
|
14 |
+ |
|
15 |
+int new_hash( str call_id, str cseq_nr ); |
|
16 |
+int old_hash( str call_id, str cseq_nr ); |
|
17 |
+ |
|
18 |
+#define hash( cid, cseq) new_hash( cid, cseq ) |
|
19 |
+ |
|
20 |
+#endif |