00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include "gwlib/gwlib.h"
00065 #include "wtp_tid.h"
00066
00067
00068
00069
00070 enum {
00071 no_cache = -1,
00072 iniatilised = -2,
00073 not_iniatilised = -3,
00074 cached = 0
00075 };
00076
00077
00078
00079
00080
00081
00082 static List *tid_cache = NULL;
00083
00084
00085
00086
00087 static WTPCached_tid *cache_item_create_empty(void);
00088 static void cache_item_destroy(void *item);
00089
00090
00091
00092 static void add_tid(WTPRespMachine *resp_machine, long tid);
00093 static void set_tid_by_item(WTPCached_tid *item, long tid);
00094 static int tid_in_window(long rcv_tid, long last_tid);
00095 static WTPCached_tid *tid_cached(WTPRespMachine *resp_machine);
00096
00097
00098
00099
00100
00101
00102 void wtp_tid_cache_init(void)
00103 {
00104 tid_cache = gwlist_create();
00105 }
00106
00107 void wtp_tid_cache_shutdown(void)
00108 {
00109 debug("wap.wtp_tid", 0, "%ld items left in the tid cache",
00110 gwlist_len(tid_cache));
00111 gwlist_destroy(tid_cache, cache_item_destroy);
00112 }
00113
00114
00115
00116
00117
00118
00119 int wtp_tid_is_valid(WAPEvent *event, WTPRespMachine *resp_machine)
00120 {
00121 long rcv_tid = -1,
00122 last_tid = -1;
00123
00124 WTPCached_tid *item = NULL;
00125
00126 #if 0
00127 debug("wap.wtp.tid", 0, "starting validation");
00128 #endif
00129 rcv_tid = resp_machine->tid;
00130
00131 if (!event->u.RcvInvoke.tid_new) {
00132
00133
00134
00135 if ((item = tid_cached(resp_machine)) == NULL) {
00136 if (event->u.RcvInvoke.no_cache_supported)
00137 return no_cached_tid;
00138 else {
00139 #if 0
00140 debug("wap.wtp.tid", 0, "empty cache");
00141 #endif
00142 add_tid(resp_machine, rcv_tid);
00143 return ok;
00144 }
00145 }
00146
00147
00148
00149
00150 last_tid = item->tid;
00151
00152 if (tid_in_window(rcv_tid, last_tid) == 0){
00153 info(0, "WTP_TID: tid out of the window");
00154 return fail;
00155 } else {
00156 #if 0
00157 debug("wap.wtp.tid", 0, "tid in the window");
00158 #endif
00159 set_tid_by_item(item, rcv_tid);
00160 return ok;
00161 }
00162
00163 } else {
00164 info(0, "WTP_TID: tid_new flag on");
00165 rcv_tid = 0;
00166
00167 if (item == NULL) {
00168 add_tid(resp_machine, rcv_tid);
00169 } else {
00170 set_tid_by_item(item, rcv_tid);
00171 }
00172
00173 return fail;
00174 }
00175
00176
00177
00178 return fail;
00179 }
00180
00181
00182
00183
00184
00185 void wtp_tid_set_by_machine(WTPRespMachine *resp_machine, long tid)
00186 {
00187 WTPCached_tid *item = NULL;
00188
00189 item = tid_cached(resp_machine);
00190 set_tid_by_item(item, tid);
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 static int tid_in_window(long rcv_tid, long last_tid)
00205 {
00206 #if 0
00207 debug("wap.wtp.tid", 0, "tids were rcv_tid, %ld and last_tid, %ld"
00208 " and test window %ld", rcv_tid, last_tid, WTP_TID_WINDOW_SIZE);
00209 #endif
00210 if (last_tid == rcv_tid) {
00211 return 0;
00212 }
00213
00214 if (rcv_tid > last_tid) {
00215 if (abs(rcv_tid - last_tid) <= WTP_TID_WINDOW_SIZE) {
00216 return 1;
00217 } else {
00218 return 0;
00219 }
00220 }
00221
00222 if (rcv_tid < last_tid) {
00223 if (abs(rcv_tid - last_tid) >= WTP_TID_WINDOW_SIZE){
00224 return 1;
00225 } else {
00226 return 0;
00227 }
00228 }
00229
00230
00231
00232
00233 return 0;
00234 }
00235
00236 static WTPCached_tid *cache_item_create_empty(void)
00237 {
00238 WTPCached_tid *item = NULL;
00239
00240 item = gw_malloc(sizeof(*item));
00241 item->addr_tuple = NULL;
00242 item->tid = 0;
00243
00244 return item;
00245 }
00246
00247 static void cache_item_destroy(void *p)
00248 {
00249 WTPCached_tid *item;
00250
00251 item = p;
00252 wap_addr_tuple_destroy(item->addr_tuple);
00253 gw_free(item);
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263 static int tid_is_cached(void *a, void *b)
00264 {
00265 WAPAddrTuple *initiator_profile;
00266 WTPCached_tid *item;
00267
00268 item = a;
00269 initiator_profile = b;
00270
00271 return wap_addr_tuple_same(item->addr_tuple, initiator_profile);
00272 }
00273
00274 static WTPCached_tid *tid_cached(WTPRespMachine *resp_machine)
00275 {
00276 WTPCached_tid *item = NULL;
00277
00278 item = gwlist_search(tid_cache, resp_machine->addr_tuple, tid_is_cached);
00279
00280 return item;
00281 }
00282
00283
00284
00285
00286
00287 static void add_tid(WTPRespMachine *resp_machine, long tid)
00288 {
00289 WTPCached_tid *new_item = NULL;
00290
00291 new_item = cache_item_create_empty();
00292 new_item->addr_tuple = wap_addr_tuple_duplicate(resp_machine->addr_tuple);
00293 new_item->tid = tid;
00294
00295 gwlist_append(tid_cache, new_item);
00296 }
00297
00298
00299
00300
00301 static void set_tid_by_item(WTPCached_tid *item, long tid)
00302 {
00303 gwlist_lock(tid_cache);
00304 item->tid = tid;
00305 gwlist_unlock(tid_cache);
00306 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.