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

directconn.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 directconn.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief functions to help in attempt to make direct connection
00023  */
00024 
00025 #include "directconn.h"
00026 #include "directconn_private.h"
00027 #include <stdlib.h>
00028 #include <pthread.h>
00029 #include "util.h"
00030 #include "debug.h"
00031 #include "berkeleyapi.h"
00032 #include <unistd.h>
00033 
00034 errorcode start_direct_conn(peer_conn_info_t *info) {
00035 
00036         /* declare local variables */
00037         direct_conn_connect_arg_t *arg;
00038         pthread_t tid;
00039 
00040         /* error check arguments */
00041         CHECK_NOT_NULL(info,ERROR_NULL_ARG_1);
00042 
00043         /* do function */
00044 
00045         /* create argument */
00046         if ( (arg = (direct_conn_connect_arg_t*) malloc (
00047                         sizeof(direct_conn_connect_arg_t))) == NULL)
00048                 return ERROR_1;
00049         arg->info = info;
00050 
00051         /* start the direct connection thread... */
00052         if (pthread_create(&tid,NULL,run_direct_conn_connect,arg)<0) {
00053                 safe_free(arg);
00054                 return ERROR_PTHREAD_CREATE_FAILED;
00055         }
00056         /* ...and the detach it! */
00057         if (pthread_detach(tid)<0)
00058                 return ERROR_PTHREAD_DETACH_FAILED;
00059 
00060         return SUCCESS;
00061 }
00062 
00063 void *run_direct_conn_connect(void *arg) {
00064 
00065         /* declare local variables */
00066         direct_conn_connect_arg_t *cast_arg;
00067         struct sockaddr_in server;
00068         int ttl;
00069 
00070         /* error check arguments */
00071         CHECK_NOT_NULL(arg,(void*)ERROR_NULL_ARG_1);
00072 
00073         /* do function */
00074 
00075         sleep(1);/* HACK!!! to slow things down */
00076 
00077         cast_arg = (direct_conn_connect_arg_t*)arg;
00078 
00079         DEBUG(DBG_DIR_CONN,"DIR_CONN:starting connection\n");
00080 
00081         /* put the server info in the sockaddr_in struct */
00082         server.sin_family      = AF_INET;
00083         server.sin_port        = cast_arg->info->buddy.ext_port;
00084         server.sin_addr.s_addr = cast_arg->info->buddy.ext_ip;
00085 
00086         /* set the TTL too low */
00087         ttl = TTL_TOO_LOW;
00088         setsockopt(cast_arg->info->socks.buddy, IPPROTO_IP, IP_TTL,
00089                         &ttl, sizeof(ttl));
00090 
00091         /* connect to the server */
00092         if( connect(cast_arg->info->socks.buddy, (struct sockaddr *)&server,
00093                         sizeof(server)) < 0) {
00094                 cast_arg->info->direct_conn_status = FLAG_FAILED;
00095                 safe_free(arg);
00096                 DEBUG(DBG_DIR_CONN,"DIR_CONN:Direction connection failed\n");
00097                 return (void*)ERROR_TCP_CONNECT;
00098         }
00099 
00100         /* set the TTL back high */
00101         ttl = TTL_OK;
00102         setsockopt(cast_arg->info->socks.buddy, IPPROTO_IP, IP_TTL,
00103                         &ttl, sizeof(ttl));
00104 
00105         DEBUG(DBG_DIR_CONN,"DIR_CONN:direct connection made!\n");
00106 
00107         cast_arg->info->direct_conn_status = FLAG_SUCCESS;
00108         safe_free(arg);
00109         return (void*)SUCCESS;
00110 }
00111 

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