00001 /* $Id$ */ 00002 00003 #ifdef HAVE_CONFIG_H 00004 #include "config.h" 00005 #endif 00006 00007 #include "flow_hash.h" 00008 /** 00009 * @file flow_hash.c 00010 * @author Chris Green <cmg@sourcefire.com> 00011 * @date Thu Jun 19 11:42:49 2003 00012 * 00013 * @brief hash function for FLOW keys 00014 * 00015 * We can save a bit of work in the hash stage by having a hash 00016 * function that understands FLOWS better than hash(sizeof(FLOWKEY)) 00017 */ 00018 00019 00020 /** 00021 * hash function that implements sfhashfcn for only the portions 00022 * a Flowkey that are relevant 00023 * 00024 * @param p 00025 * @param d 00026 * @param n 00027 * 00028 * @return the hash 00029 */ 00030 unsigned flowkey_hashfcn1( SFHASHFCN * p, unsigned char * d, int n) 00031 { 00032 unsigned hash = p->seed; 00033 FLOWKEY *keyp = (FLOWKEY *) d; 00034 00035 hash *= p->scale; 00036 hash += ((char *) &keyp->init_address)[0]; 00037 hash *= p->scale; 00038 hash += ((char *) &keyp->init_address)[1]; 00039 hash *= p->scale; 00040 hash += ((char *) &keyp->init_address)[2]; 00041 hash *= p->scale; 00042 hash += ((char *) &keyp->init_address)[3]; 00043 00044 hash *= p->scale; 00045 hash += ((char *) &keyp->init_port)[0]; 00046 hash *= p->scale; 00047 hash += ((char *) &keyp->init_port)[1]; 00048 00049 hash *= p->scale; 00050 hash += ((char *) &keyp->resp_address)[0]; 00051 hash *= p->scale; 00052 hash += ((char *) &keyp->resp_address)[1]; 00053 hash *= p->scale; 00054 hash += ((char *) &keyp->resp_address)[2]; 00055 hash *= p->scale; 00056 hash += ((char *) &keyp->resp_address)[3]; 00057 00058 hash *= p->scale; 00059 hash += ((char *) &keyp->resp_port)[0]; 00060 hash *= p->scale; 00061 hash += ((char *) &keyp->resp_port)[1]; 00062 00063 hash *= p->scale; 00064 hash += keyp->protocol; 00065 00066 return hash ^ p->hardener; 00067 } 00068 00069 /** 00070 00071 * One that performs less calculations because it doesn't treat each 00072 * byte of the entity as unique. This is probably less resistant to 00073 * collisions but the hardener stages should be randomly chosen so that 00074 * complexity attacks shouldn't succeed without a lot of prior knowledge 00075 * 00076 * @param p 00077 * @param d 00078 * @param n 00079 * 00080 * @return the hash 00081 */ 00082 unsigned flowkey_hashfcn2( SFHASHFCN * p, unsigned char * d, int n) 00083 { 00084 unsigned hash = p->seed; 00085 FLOWKEY *keyp = (FLOWKEY *) d; 00086 00087 hash *= p->scale; 00088 hash += keyp->init_address; 00089 00090 hash *= p->scale; 00091 hash += keyp->init_port; 00092 00093 hash *= p->scale; 00094 hash += keyp->resp_address; 00095 00096 hash *= p->scale; 00097 hash += keyp->resp_port; 00098 00099 hash *= p->scale; 00100 hash += keyp->protocol; 00101 00102 return hash ^ p->hardener; 00103 }