Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

helpercon.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright 2005 Daniel Ferullo                                             *
00003  *                                                                           *
00004  * Licensed under the Apache License, Version 2.0 (the "License");           *
00005  * you may not use this file except in compliance with the License.          *
00006  * You may obtain a copy of the License at                                   *
00007  *                                                                           *
00008  *    http://www.apache.org/licenses/LICENSE-2.0                             *
00009  *                                                                           *
00010  * Unless required by applicable law or agreed to in writing, software       *
00011  * distributed under the License is distributed on an "AS IS" BASIS,         *
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00013  * See the License for the specific language governing permissions and       *
00014  * limitations under the License.                                            *
00015  *                                                                           *
00016  *****************************************************************************/
00017 
00018 /**
00019  * @file helpercon.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief contains defintions for functions to help with a peer connection
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         /* declare variables */
00038         connlist_item_t *item;
00039         pthread_t tid;
00040         helper_fsm_thread_arg_t *arg;
00041 
00042         /* error check arguments */
00043         CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00044         CHECK_NOT_NULL(data,ERROR_NULL_ARG_2);
00045 
00046         /* do function */
00047         if ( (item=(connlist_item_t*)malloc(sizeof(connlist_item_t))) == NULL)
00048                 return ERROR_MALLOC_FAILED_1;
00049 
00050         /* copy the observed data */
00051         memcpy(&item->obs_data,data,sizeof(observed_data_t));
00052 
00053         /* set the sd for the peer connection */
00054         item->info.socks.peer = sd;
00055 
00056         /* create new thread */
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         /* create a thread with the default attributes... */
00066         if (pthread_create(&tid,NULL,run_helper_fsm_thread,arg)<0) {
00067                 safe_free(arg);
00068                 return ERROR_PTHREAD_CREATE_FAILED;
00069         }
00070         /* .. and then detach it! */
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         /* declare variables */
00080         helper_fsm_thread_arg_t *cast_arg;
00081 
00082         /* check arguments */
00083         CHECK_NOT_NULL(arg,(void*)ERROR_NULL_ARG_1);
00084 
00085         /* do function */
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         /* declare local varibles */
00101         connlist_item_t *local_item;
00102         int timer = FIND_BUDDY_TIMEOUT;
00103 
00104         /* error check arguments */
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         /* do function */
00110         /* loop looking for the buddy */
00111         do {
00112                 /* look for the item */
00113                 DEBUG(DBG_BUDDY,"BUDDY:Finding buddy\n");
00114                 if (FAILED(connlist_find(list,connlist_find_buddy,
00115                                 &item->info.buddy,&local_item))) {
00116                         /* if it wasn't found, pause */
00117                         sleep(1);
00118                         timer = DEC_UNTIL_ZERO(timer);
00119                 }
00120                 else {
00121                         /* if it was found, break out */
00122                         break;
00123                 }
00124         } while (timer>0);
00125 
00126         /* if the timer is 0, then the item wasn't found, otherwise it was */
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         /* declare local variables */
00139 
00140         /* error check arguments */
00141         CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00142 
00143         /* do function */
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         /* declare local variables */
00153 
00154         /* error check arguments */
00155         CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00156 
00157         /* do function */
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         /* declare local variables */
00167 
00168         /* error check arguments */
00169         CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00170 
00171         /* do function */
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         /* declare local variables */
00181 
00182         /* error check arguments */
00183         CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00184 
00185         /* do function */
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         /* declare local variables */
00196 
00197         /* error check arguments */
00198         CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_1);
00199 
00200         /* do function */
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         /* declare local variables */
00211         int timer = FIND_CONN2_TIMEOUT;
00212 
00213         /* error check arguments */
00214         CHECK_NOT_NULL(list,ERROR_NULL_ARG_1);
00215         CHECK_NOT_NULL(find_data,ERROR_NULL_ARG_2);
00216 
00217         /* do function */
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 

Generated on Wed Mar 30 23:20:47 2005 for NATBLASTER by  doxygen 1.3.9.1