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 #include <string.h>
00026 #include "helperfsm.h"
00027 #include "helpercon.h"
00028 #include <unistd.h>
00029 #include <pthread.h>
00030 #include "debug.h"
00031 #include "def.h"
00032 #include "util.h"
00033
00034 errorcode create_new_handler(connlist_t *list, observed_data_t *data,
00035 sock_t sd) {
00036
00037
00038 connlist_item_t *item;
00039 pthread_t tid;
00040 helper_fsm_thread_arg_t *arg;
00041
00042
00043 CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00044 CHECK_NOT_NULL(data,ERROR_NULL_ARG_2);
00045
00046
00047 if ( (item=(connlist_item_t*)malloc(sizeof(connlist_item_t))) == NULL)
00048 return ERROR_MALLOC_FAILED_1;
00049
00050
00051 memcpy(&item->obs_data,data,sizeof(observed_data_t));
00052
00053
00054 item->info.socks.peer = sd;
00055
00056
00057 if ( (arg = (helper_fsm_thread_arg_t*) malloc(
00058 sizeof(helper_fsm_thread_arg_t))) == NULL) {
00059 return ERROR_MALLOC_FAILED_2;
00060 }
00061
00062 arg->list = list;
00063 arg->item = item;
00064
00065
00066 if (pthread_create(&tid,NULL,run_helper_fsm_thread,arg)<0) {
00067 safe_free(arg);
00068 return ERROR_PTHREAD_CREATE_FAILED;
00069 }
00070
00071 if (pthread_detach(tid)<0)
00072 return ERROR_PTHREAD_DETACH_FAILED;
00073
00074 return SUCCESS;
00075 }
00076
00077 void *run_helper_fsm_thread(void *arg) {
00078
00079
00080 helper_fsm_thread_arg_t *cast_arg;
00081
00082
00083 CHECK_NOT_NULL(arg,(void*)ERROR_NULL_ARG_1);
00084
00085
00086 cast_arg = (helper_fsm_thread_arg_t*)arg;
00087
00088 if (FAILED(helper_fsm_start(cast_arg->list, cast_arg->item))) {
00089 safe_free(arg);
00090 return (void*)ERROR_1;
00091 }
00092
00093 safe_free(arg);
00094 return (void*) SUCCESS;
00095 }
00096
00097 errorcode get_buddy(connlist_t *list, connlist_item_t *item,
00098 connlist_item_t **found_buddy) {
00099
00100
00101 connlist_item_t *local_item;
00102 int timer = FIND_BUDDY_TIMEOUT;
00103
00104
00105 CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00106 CHECK_NOT_NULL(item,ERROR_NULL_ARG_2);
00107 CHECK_NOT_NULL(found_buddy,ERROR_NULL_ARG_3);
00108
00109
00110
00111 do {
00112
00113 DEBUG(DBG_BUDDY,"BUDDY:Finding buddy\n");
00114 if (FAILED(connlist_find(list,connlist_find_buddy,
00115 &item->info.buddy,&local_item))) {
00116
00117 sleep(1);
00118 timer = DEC_UNTIL_ZERO(timer);
00119 }
00120 else {
00121
00122 break;
00123 }
00124 } while (timer>0);
00125
00126
00127 if (timer==0) {
00128 return ERROR_NOT_FOUND;
00129 }
00130
00131 *found_buddy = local_item;
00132
00133 return SUCCESS;
00134 }
00135
00136 errorcode wait_for_buddy_port_alloc(helper_conn_info_t *buddy) {
00137
00138
00139
00140
00141 CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00142
00143
00144 CHECK_FAILED(wait_for_flag(&buddy->port_alloc.method_set, FLAG_SET,
00145 WAIT_FOR_BUDDY_PORT_ALLOC_TIMEOUT),ERROR_CALLED_FUNCTION);
00146
00147 return SUCCESS;
00148 }
00149
00150 errorcode wait_for_buddy_port_known(helper_conn_info_t *buddy) {
00151
00152
00153
00154
00155 CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00156
00157
00158 CHECK_FAILED(wait_for_flag(&buddy->port_alloc.ext_port_set, FLAG_SET,
00159 WAIT_FOR_BUDDY_PORT_KNOWN_TIMEOUT),ERROR_CALLED_FUNCTION);
00160
00161 return SUCCESS;
00162 }
00163
00164 errorcode wait_for_buddy_bday_port(helper_conn_info_t *buddy) {
00165
00166
00167
00168
00169 CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00170
00171
00172 CHECK_FAILED(wait_for_flag(&buddy->bday.port_set, FLAG_SET,
00173 WAIT_FOR_BUDDY_BDAY_PORT_TIMEOUT),ERROR_CALLED_FUNCTION);
00174
00175 return SUCCESS;
00176 }
00177
00178 errorcode wait_for_buddy_syn_seq_num(helper_conn_info_t *buddy) {
00179
00180
00181
00182
00183 CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00184
00185
00186 CHECK_FAILED(wait_for_flag(&buddy->buddy_syn.seq_num_set, FLAG_SET,
00187 WAIT_FOR_BUDDY_SEQ_NUM_TIMEOUT),ERROR_CALLED_FUNCTION);
00188
00189 return SUCCESS;
00190 }
00191
00192
00193 errorcode wait_for_buddy_syn_flood(helper_conn_info_t *buddy) {
00194
00195
00196
00197
00198 CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00199
00200
00201 CHECK_FAILED(wait_for_flag(&buddy->bday.seq_num_set, FLAG_SET,
00202 WAIT_FOR_BUDDY_SYN_FLOOD_TIMEOUT),ERROR_CALLED_FUNCTION);
00203
00204 return SUCCESS;
00205 }
00206
00207 errorcode find_conn2(connlist_t *list, observed_data_t *find_data,
00208 connlist_item_t **found_item) {
00209
00210
00211 int timer = FIND_CONN2_TIMEOUT;
00212
00213
00214 CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00215 CHECK_NOT_NULL(find_data,ERROR_NULL_ARG_2);
00216
00217
00218 do {
00219 if (FAILED(connlist_find(list,connlist_find_pred_port,
00220 find_data,found_item))) {
00221 sleep(1);
00222 timer = DEC_UNTIL_ZERO(timer);
00223 }
00224 else {
00225 return SUCCESS;
00226 }
00227
00228 } while (timer>0);
00229
00230 return ERROR_TIMEOUT;
00231 }
00232