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

peercon.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 peercon.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief functions to assist the peer in making a direct connection
00023  */
00024 
00025 #include "peercon.h"
00026 #include "util.h"
00027 #include "peerdef.h"
00028 #include <time.h>
00029 #include <stdlib.h>
00030 #include "spoof.h"
00031 #include "sniff.h"
00032 #include "debug.h"
00033 
00034 errorcode wait_for_direct_conn(flag_t *check_flag) {
00035 
00036         /* declare local variables */
00037 
00038         /* error check arguments */
00039         CHECK_NOT_NULL(check_flag,ERROR_NULL_ARG_1);
00040 
00041         /* do function */
00042         CHECK_FAILED(wait_for_flag(check_flag,FLAG_FAILED|FLAG_SUCCESS,
00043                 DIRECT_CONNECTION_TIMEOUT),ERROR_1);
00044 
00045         return SUCCESS;
00046 }
00047 
00048 errorcode findDevice(char **dev) {
00049 
00050         /* declare local variables */
00051         char errbuf[PCAP_ERRBUF_SIZE];
00052 
00053         /* error check arguments */
00054         CHECK_NOT_NULL(dev,ERROR_NULL_ARG_1);
00055 
00056         /* do function */
00057         *dev = pcap_lookupdev(errbuf);
00058 
00059         if (*dev==NULL)
00060                 return ERROR_NO_DEV_FOUND;
00061 
00062         return SUCCESS;
00063 }
00064 
00065 errorcode flood_syns(tcp_packet_info_t tcp_skeleton, char *device) {
00066 
00067         /* declare local variables */
00068         int i;
00069 
00070         /* error check arguments */
00071         CHECK_NOT_NULL(device,ERROR_NULL_ARG_1);
00072 
00073         /* do function */
00074 
00075         /* fill in missing tcp_skeleton fields */
00076         tcp_skeleton.syn_flag = FLAG_SET;
00077         tcp_skeleton.ack_flag = FLAG_UNSET;
00078         tcp_skeleton.window   = WINDOW_DEFAULT;
00079         tcp_skeleton.ack_num  = 0;
00080 
00081         DEBUG(DBG_BDAY,"DBAY:d_port = %u\n",DBG_PORT(tcp_skeleton.d_port));
00082 
00083         /* seed the random number generator */
00084         srand(time(NULL));
00085 
00086         for (i=0;i<SYN_FLOOD_COUNT;i++) {
00087                 tcp_skeleton.s_port = (port_t)rand();
00088                 CHECK_FAILED(spoof(&tcp_skeleton,device,NULL,0,TTL_TOO_LOW),
00089                         ERROR_CALLED_FUNCTION);
00090         }
00091 
00092         return SUCCESS;
00093 }
00094 
00095 errorcode start_find_synack(peer_conn_info_t *info) {
00096 
00097         /* declare local variables */
00098 
00099         /* error check arguments */
00100         CHECK_NOT_NULL(info,ERROR_NULL_ARG_1);
00101 
00102         /* do function */
00103         info->bday.stop_synack_find = FLAG_UNSET;
00104         info->bday.find_synack_done = FLAG_UNSET;
00105         info->bday.port_set         = FLAG_UNSET;
00106         if (pthread_create(&info->bday.find_synack_tid,NULL,
00107                                                 run_find_synack,info)<0)
00108                 return ERROR_PTHREAD_CREATE_FAILED;
00109 
00110         return SUCCESS;
00111 }
00112 
00113 void *run_find_synack(void* arg) {
00114 
00115         /* declare local variables */
00116         peer_conn_info_t *cast_arg;
00117         errorcode ret;
00118 
00119         /* error check arguments */
00120         CHECK_NOT_NULL(arg,(void*)ERROR_NULL_ARG_1);
00121 
00122         /* do function*/
00123         cast_arg = (peer_conn_info_t*) arg;
00124         ret = capture_flooded_synack(cast_arg);
00125 
00126         cast_arg->bday.find_synack_done = FLAG_SET;
00127 
00128         if (FAILED(ret))
00129                 return (void*)ERROR_CALLED_FUNCTION;
00130         else
00131                 return (void*)SUCCESS;
00132 }
00133 
00134 errorcode wait_and_join_find_synack(peer_conn_info_t *info) {
00135 
00136         /* declare local variables */
00137         errorcode wait_ret;
00138         errorcode thread_ret;
00139 
00140         /* error check arguments */
00141         CHECK_NOT_NULL(info,ERROR_NULL_ARG_1);
00142 
00143         /* do function */
00144         DBG_TIME("waiting for SYNACK flood listening thread to finished");
00145         /* wait for the find synack thread to finish */
00146         wait_ret = wait_for_flag(&info->bday.find_synack_done,FLAG_SET,
00147                 FIND_SYN_ACK_TIMEOUT);
00148 
00149         /* force the thread to finish even if it wasn't done */
00150         info->bday.stop_synack_find = FLAG_SET;
00151 
00152         /* join on the thread */
00153         if (pthread_join(info->bday.find_synack_tid,(void**)&thread_ret)<0)
00154                 return ERROR_PTHREAD_JOIN;
00155         DBG_TIME("finished waiting for SYNACK flood listening thread to finish");
00156 
00157         /* make sure no timeout happened, and that the thread was successful */
00158         if (FAILED(wait_ret))
00159                 return ERROR_1;
00160         if (FAILED(thread_ret))
00161                 return ERROR_2;
00162 
00163         return SUCCESS;
00164 }
00165 
00166 errorcode synack_flood(peer_conn_info_t *info, seq_num_t seq_num) {
00167 
00168         /* declare local variables */
00169         int i;
00170         tcp_packet_info_t skeleton;
00171         port_t port;
00172 
00173         /* error check arguments */
00174         CHECK_NOT_NULL(info,ERROR_NULL_ARG_1);
00175 
00176         /* do function */
00177 
00178         /* fill in the tcp packet skeleton */
00179         skeleton.d_addr   = info->buddy.ext_ip;
00180         skeleton.s_addr   = info->peer.ip;
00181         skeleton.s_port   = info->peer.port;
00182         skeleton.seq_num  = (seq_num_t)rand();
00183         skeleton.ack_num  = SEQ_NUM_ADD(seq_num,1);
00184         skeleton.ack_flag = FLAG_SET;
00185         skeleton.syn_flag = FLAG_SET;
00186         skeleton.window   = WINDOW_DEFAULT;
00187 
00188         /* seed the random number generator */
00189         srand(time(NULL));
00190 
00191         /* do the flood */
00192         for (i=0;i<SYN_ACK_FLOOD_COUNT;i++) {
00193                 skeleton.d_port = port = (port_t)rand();
00194                 CHECK_FAILED(spoof(&skeleton, info->device,&port,sizeof(port),
00195                         TTL_OK),ERROR_1);
00196         }
00197 
00198         return SUCCESS;
00199 }
00200 

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