hash_func.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*!
  28. * \file
  29. * \brief SIP-router core ::
  30. * \ingroup core
  31. * Module: \ref core
  32. */
  33. #ifndef _CRC_H_
  34. #define _CRC_H_
  35. extern unsigned long int crc_32_tab[];
  36. extern unsigned short int ccitt_tab[];
  37. extern unsigned short int crc_16_tab[];
  38. #endif
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42. #include "hash_func.h"
  43. #include "dprint.h"
  44. #include "crc.h"
  45. #include "ut.h"
  46. unsigned int new_hash( str call_id, str cseq_nr )
  47. {
  48. unsigned int hash_code = 0;
  49. int i,j, k, third;
  50. int ci_len, cs_len;
  51. char *ci, *cs;
  52. /* trim EoLs */
  53. /*
  54. ci_len = call_id.len;
  55. while (ci_len && ((c=call_id.s[ci_len-1])==0 || c=='\r' || c=='\n'))
  56. ci_len--;
  57. cs_len = cseq_nr.len;
  58. while (cs_len && ((c=cseq_nr.s[cs_len-1])==0 || c=='\r' || c=='\n'))
  59. cs_len--;
  60. */
  61. trim_len( ci_len, ci, call_id );
  62. trim_len( cs_len, cs, cseq_nr );
  63. /* run the cycle from the end ... we are interested in the
  64. most-right digits ... and just take the %10 value of it
  65. */
  66. third=(ci_len-1)/3;
  67. for ( i=ci_len-1, j=2*third, k=third;
  68. k>0 ; i--, j--, k-- ) {
  69. hash_code+=crc_16_tab[(unsigned char)(*(ci+i)) /*+7*/ ]+
  70. ccitt_tab[(unsigned char)*(ci+k)+63]+
  71. ccitt_tab[(unsigned char)*(ci+j)+13];
  72. }
  73. for( i=0 ; i<cs_len ; i++ )
  74. //hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243];
  75. hash_code+=ccitt_tab[(unsigned char)*(cs+i)+123];
  76. /* hash_code conditioning */
  77. #ifdef _BUG
  78. /* not flat ... % 111b has shorter period than
  79. & 111b by one and results in different distribution;
  80. ( 7 % 7 == 0, 7 %7 == 1 )
  81. % is used as a part of the hash function too, not only
  82. for rounding; & is not flat; whoever comes up with
  83. a nicer flat hash function which does not take
  84. costly division is welcome; feel free to verify
  85. distribution using hashtest()
  86. */
  87. hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
  88. #endif
  89. hash_code=hash_code%(TABLE_ENTRIES-1)+1;
  90. return hash_code;
  91. }
  92. #if 0
  93. int new_hash2( str call_id, str cseq_nr )
  94. {
  95. #define h_inc h+=v^(v>>3)
  96. char* p;
  97. register unsigned v;
  98. register unsigned h;
  99. h=0;
  100. for (p=call_id.s; p<=(call_id.s+call_id.len-4); p+=4){
  101. v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
  102. h_inc;
  103. }
  104. v=0;
  105. for (;p<(call_id.s+call_id.len); p++){ v<<=8; v+=*p;}
  106. h_inc;
  107. for (p=cseq_nr.s; p<=(cseq_nr.s+cseq_nr.len-4); p+=4){
  108. v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
  109. h_inc;
  110. }
  111. v=0;
  112. for (;p<(cseq_nr.s+cseq_nr.len); p++){ v<<=8; v+=*p;}
  113. h_inc;
  114. h=((h)+(h>>11))+((h>>13)+(h>>23));
  115. return (h)&(TABLE_ENTRIES-1);
  116. }
  117. #endif
  118. void hashtest_cycle( int hits[TABLE_ENTRIES+5], char *ip )
  119. {
  120. long int i,j,k, l;
  121. int hashv;
  122. static char buf1[1024];
  123. static char buf2[1024];
  124. str call_id;
  125. str cseq;
  126. call_id.s=buf1;
  127. cseq.s=buf2;
  128. for (i=987654328;i<987654328+10;i++)
  129. for (j=85296341;j<85296341+10;j++)
  130. for (k=987654;k<=987654+10;k++)
  131. for (l=101;l<201;l++) {
  132. call_id.len=sprintf( buf1, "%d-%d-%d@%s",(int)i,(int)j,
  133. (int)k, ip );
  134. cseq.len=sprintf( buf2, "%d", (int)l );
  135. /* printf("%s\t%s\n", buf1, buf2 ); */
  136. hashv=hash( call_id, cseq );
  137. hits[ hashv ]++;
  138. }
  139. }
  140. void hashtest(void)
  141. {
  142. int hits[TABLE_ENTRIES+5];
  143. int i;
  144. memset( hits, 0, sizeof hits );
  145. hashtest_cycle( hits, "192.168.99.100" );
  146. hashtest_cycle( hits, "172.168.99.100" );
  147. hashtest_cycle( hits, "142.168.99.100" );
  148. for (i=0; i<TABLE_ENTRIES+5; i++)
  149. printf("[%d. %d]\n", i, hits[i] );
  150. exit(0);
  151. }