00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifdef HAVE_CONFIG_H
00017 #include "config.h"
00018 #endif
00019
00020 #include "unique_tracker.h"
00021 #include "sfxhash.h"
00022
00023 typedef struct _UT_KEY
00024 {
00025 u_int32_t sip;
00026 u_int32_t dip;
00027 u_int16_t dport;
00028 char protocol;
00029 } UT_KEY;
00030
00031 static UT_KEY s_ut_key;
00032 static int s_debug = 0;
00033
00034
00035 static void ut_init_entry(void);
00036
00037
00038 int ut_init(UNIQUE_TRACKER *utp, unsigned int rows, int memcap)
00039 {
00040 if(!utp)
00041 return FLOW_ENULL;
00042
00043 ut_init_entry();
00044
00045 memset(utp, 0, sizeof(UNIQUE_TRACKER));
00046
00047
00048 utp->ipv4_table = sfxhash_new(rows,
00049 sizeof(UT_KEY),
00050 0,
00051 memcap,
00052 1,
00053 NULL,
00054 NULL,
00055 1);
00056
00057 if(utp->ipv4_table == NULL)
00058 {
00059 if(s_debug)
00060 flow_printf("ran out of memory!\n");
00061 return FLOW_ENOMEM;
00062 }
00063
00064 return FLOW_SUCCESS;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074 int ut_destroy(UNIQUE_TRACKER *utp)
00075 {
00076 if(!utp)
00077 return FLOW_ENULL;
00078
00079 if(!utp->ipv4_table)
00080 return FLOW_SUCCESS;
00081
00082 sfxhash_delete(utp->ipv4_table);
00083
00084 return FLOW_SUCCESS;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 int ut_check(UNIQUE_TRACKER *utp, FLOWKEY *keyp, UT_TYPE *retval)
00099 {
00100 int ret;
00101 UT_KEY *utkeyp = &s_ut_key;
00102
00103 if(!retval || !utp || !utp->ipv4_table)
00104 return FLOW_ENULL;
00105
00106 utkeyp->protocol = keyp->protocol;
00107 utkeyp->sip = keyp->init_address;
00108 utkeyp->dip = keyp->resp_address;
00109 utkeyp->dport = keyp->resp_port;
00110
00111 ret = sfxhash_add(utp->ipv4_table, utkeyp, NULL);
00112
00113 switch(ret)
00114 {
00115 case SFXHASH_NOMEM:
00116
00117
00118
00119 case SFXHASH_OK:
00120 *retval = UT_NEW;
00121 break;
00122 case SFXHASH_INTABLE:
00123 *retval = UT_OLD;
00124 break;
00125 }
00126
00127 return FLOW_SUCCESS;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 static void ut_init_entry(void)
00139 {
00140 static int init_once = 1;
00141
00142 if(init_once)
00143 {
00144 init_once = 0;
00145 memset(&s_ut_key, 0, sizeof(UT_KEY));
00146 }
00147 }
00148
00149
00150
00151
00152
00153
00154 void unique_tracker_dump(UNIQUE_TRACKER *ssp)
00155 {
00156 SFXHASH_NODE *nodep;
00157 char buf[32 + 1];
00158
00159 if(ssp && ssp->ipv4_table)
00160 {
00161 for( nodep = sfxhash_ghead(ssp->ipv4_table);
00162 nodep != NULL;
00163 nodep = sfxhash_gnext(nodep) )
00164 {
00165 UT_KEY *kp = (UT_KEY *) nodep->key;
00166
00167 snprintf(buf, 32, "%15s", inet_ntoa(*(struct in_addr *)&kp->sip));
00168 buf[32] = '\0';
00169
00170 flow_printf("%s -> (proto:%d %s:%d)\n",
00171 buf,
00172 kp->protocol,
00173 inet_ntoa(*(struct in_addr *)&kp->dip),
00174 kp->dport);
00175 }
00176 }
00177 else
00178 {
00179 flow_printf("nothing to dump!\n");
00180 }
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190 void ut_stats(UNIQUE_TRACKER *utp, int dumpall)
00191 {
00192 unsigned total = sfxhash_find_total(utp->ipv4_table);
00193 unsigned fail = sfxhash_find_fail(utp->ipv4_table);
00194 unsigned success = sfxhash_find_success(utp->ipv4_table);
00195
00196 flow_printf("UNIQUE_TRACKER STATS\n");
00197 flow_printf(" Memcap: %u Overhead Bytes: %u\n",
00198 ut_memcap(utp), ut_overhead_bytes(utp));
00199
00200 flow_printf(" Finds: %u (Sucessful: %u(%%%lf) Unsucessful: %u(%%%lf))\n",
00201 total,
00202 success, calc_percent(success,total),
00203 fail, calc_percent(fail,total));
00204
00205 flow_printf(" Nodes: %u\n", sfxhash_count(utp->ipv4_table));
00206
00207 flow_printf(" Recovered Nodes: %u\n", sfxhash_anr_count(utp->ipv4_table));
00208
00209 if(dumpall)
00210 unique_tracker_dump(utp);
00211
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 int ut_memcap(UNIQUE_TRACKER *utp)
00223 {
00224 if(utp != NULL && utp->ipv4_table != NULL)
00225 return utp->ipv4_table->mc.memcap;
00226
00227 return -1;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237 int ut_row_count(UNIQUE_TRACKER *utp)
00238 {
00239 if(utp != NULL && utp->ipv4_table != NULL)
00240 return utp->ipv4_table->nrows;
00241
00242 return -1;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 int ut_overhead_bytes(UNIQUE_TRACKER *sbp)
00253 {
00254 if(sbp != NULL && sbp->ipv4_table != NULL)
00255 return sfxhash_overhead_bytes(sbp->ipv4_table);
00256
00257 return -1;
00258 }
00259