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 #include "connlist.h"
00027 #include <pthread.h>
00028 #include "debug.h"
00029
00030 #include <unistd.h>
00031
00032 errorcode connlist_init(connlist_t *list) {
00033
00034 CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00035
00036 CHECK_FAILED(list_init(&list->list),ERROR_INIT);
00037
00038
00039
00040
00041
00042
00043 if (pthread_mutex_init(&(list->mutex),NULL)<0)
00044 return ERROR_2;
00045
00046 return SUCCESS;
00047 }
00048
00049 errorcode connlist_add(connlist_t *list, connlist_item_t *item) {
00050
00051 CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00052 CHECK_NOT_NULL(item,ERROR_NULL_ARG_2);
00053
00054
00055 DEBUG(DBG_THREAD,"THREAD:LOCKING MUTEX\n");
00056 if (pthread_mutex_lock(&(list->mutex))<0)
00057 return ERROR_MUTEX_LOCK;
00058 DEBUG(DBG_THREAD,"THREAD:LOCKED MUTEX\n");
00059
00060 item->watchers = 1;
00061
00062
00063 if (FAILED(list_add(&list->list,item))) {
00064 if(pthread_mutex_unlock(&(list->mutex))<0)
00065 return ERROR_MUTEX_UNLOCK_1;
00066 DEBUG(DBG_THREAD,"THREAD:UNLOCKED MUTEX\n");
00067 return ERROR_LIST_ADD;
00068 }
00069
00070
00071 DEBUG(DBG_THREAD,"THREAD:UNLOCKING MUTEX\n");
00072 if (pthread_mutex_unlock(&(list->mutex))<0)
00073 return ERROR_MUTEX_UNLOCK_2;
00074 DEBUG(DBG_THREAD,"THREAD:UNLOCKED MUTEX\n");
00075
00076 return SUCCESS;
00077 }
00078
00079 errorcode connlist_find(connlist_t *list, int (*func)(void*,void*), void *arg,
00080 connlist_item_t **found_item){
00081
00082
00083 connlist_item_t *cast_item;
00084
00085
00086 CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00087 CHECK_NOT_NULL(func,ERROR_NULL_ARG_2);
00088 CHECK_NOT_NULL(found_item,ERROR_NULL_ARG_4);
00089
00090
00091
00092
00093 DEBUG(DBG_THREAD,"THREAD:LOCKING MUTEX\n");
00094 if (pthread_mutex_lock(&(list->mutex))<0)
00095 return ERROR_MUTEX_LOCK;
00096 DEBUG(DBG_THREAD,"THREAD:LOCKED MUTEX %p\n",&(list->mutex));
00097
00098
00099 if (FAILED(list_find(&list->list,func,arg,(void**)&cast_item))) {
00100 if(pthread_mutex_unlock(&(list->mutex))<0)
00101 return ERROR_MUTEX_UNLOCK_1;
00102 DEBUG(DBG_THREAD,"THREAD:UNLOCKED MUTEX\n");
00103 return ERROR_LIST_FIND;
00104 }
00105
00106 *found_item = cast_item;
00107
00108
00109 cast_item->watchers += 1;
00110
00111
00112 DEBUG(DBG_THREAD,"THREAD:UNLOCKING MUTEX\n");
00113 if (pthread_mutex_unlock(&(list->mutex))<0)
00114 return ERROR_MUTEX_UNLOCK_2;
00115 DEBUG(DBG_THREAD,"THREAD:UNLOCKED MUTEX\n");
00116
00117 DEBUG(DBG_PORT_PRED,"PORT_PRED:found item\n");
00118 return SUCCESS;
00119 }
00120
00121 int connlist_find_pred_port(void *this_item, void *find_item) {
00122
00123
00124 connlist_item_t *cast_item;
00125 observed_data_t *cast_data;
00126
00127
00128 CHECK_NOT_NULL(find_item,LIST_FATAL);
00129 CHECK_NOT_NULL(this_item,LIST_FATAL);
00130
00131
00132 cast_item = (connlist_item_t*) this_item;
00133 cast_data = (observed_data_t*) find_item;
00134
00135 DEBUG(DBG_PORT_PRED,"PORT_PRED:item %s:%u\n",
00136 DBG_IP(cast_item->obs_data.ip),
00137 DBG_PORT(cast_item->obs_data.port));
00138 DEBUG(DBG_PORT_PRED,"PORT_PRED:data %s:%u\n",
00139 DBG_IP(cast_data->ip),DBG_PORT(cast_data->port));
00140
00141
00142 if ( (cast_item->obs_data.ip == cast_data->ip) &&
00143 (cast_item->obs_data.port == cast_data->port) ) {
00144 DEBUG(DBG_PORT_PRED,"PORT_PRED:matched item\n");
00145 return LIST_FOUND;
00146 }
00147
00148 return LIST_NOT_FOUND;
00149 }
00150
00151 int connlist_find_buddy(void *this_item, void *find_item) {
00152
00153
00154 connlist_item_t *cast_item;
00155 buddy_info_t *cast_buddy;
00156
00157
00158 CHECK_NOT_NULL(find_item,LIST_FATAL);
00159 CHECK_NOT_NULL(this_item,LIST_FATAL);
00160
00161
00162 cast_item = (connlist_item_t*) this_item;
00163 cast_buddy = (buddy_info_t*) find_item;
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 if ( (cast_buddy->int_port==cast_item->info.peer.port) &&
00179 (cast_buddy->int_ip==cast_item->info.peer.ip) &&
00180 (cast_buddy->ext_ip==cast_item->obs_data.ip) ) {
00181 return LIST_FOUND;
00182 }
00183
00184 return LIST_NOT_FOUND;
00185 }
00186
00187 errorcode connlist_forget(connlist_t *list,
00188 int (*func)(void*,void*), connlist_item_t *item) {
00189
00190
00191
00192
00193 CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00194 CHECK_NOT_NULL(item,ERROR_NULL_ARG_2);
00195
00196
00197 DEBUG(DBG_THREAD,"THREAD:LOCKING MUTEX\n");
00198 if (pthread_mutex_lock(&(list->mutex))<0)
00199 return ERROR_MUTEX_LOCK;
00200 DEBUG(DBG_THREAD,"THREAD:LOCKED MUTEX\n");
00201
00202
00203 item->watchers -= 1;
00204
00205 if (item->watchers == 0) {
00206
00207 if (FAILED(list_remove(&list->list,func,item))) {
00208 if(pthread_mutex_unlock(&(list->mutex))<0)
00209 return ERROR_MUTEX_UNLOCK_1;
00210 DEBUG(DBG_THREAD,"THREAD:UNLOCKED MUTEX\n");
00211 return ERROR_LIST_ADD;
00212 }
00213 }
00214
00215
00216 DEBUG(DBG_THREAD,"THREAD:UNLOCKING MUTEX\n");
00217 if (pthread_mutex_unlock(&(list->mutex))<0)
00218 return ERROR_MUTEX_UNLOCK_2;
00219 DEBUG(DBG_THREAD,"THREAD:UNLOCKED MUTEX\n");
00220 return SUCCESS;
00221 }
00222
00223 int connlist_item_match(void *this_item, void *find_item) {
00224
00225
00226
00227
00228 CHECK_NOT_NULL(this_item,LIST_FATAL);
00229 CHECK_NOT_NULL(find_item,LIST_FATAL);
00230
00231
00232 if (this_item == find_item)
00233 return LIST_FOUND;
00234
00235 return LIST_NOT_FOUND;
00236 }
00237
00238 int connlist_count(connlist_t *list) {
00239
00240
00241 int count;
00242
00243
00244 CHECK_NOT_NULL(list,-2);
00245
00246
00247
00248 DEBUG(DBG_THREAD,"THREAD:LOCKING MUTEX\n");
00249 if (pthread_mutex_lock(&(list->mutex))<0)
00250 return -3;
00251 DEBUG(DBG_THREAD,"THREAD:LOCKED MUTEX\n");
00252
00253 count = list_count(&list->list);
00254
00255
00256 DEBUG(DBG_THREAD,"THREAD:UNLOCKING MUTEX\n");
00257 if (pthread_mutex_unlock(&(list->mutex))<0)
00258 return -4;
00259 DEBUG(DBG_THREAD,"THREAD:UNLOCKED MUTEX\n");
00260
00261 return count;
00262 }