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 "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
00037 direct_conn_connect_arg_t *arg;
00038 pthread_t tid;
00039
00040
00041 CHECK_NOT_NULL(info,ERROR_NULL_ARG_1);
00042
00043
00044
00045
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
00052 if (pthread_create(&tid,NULL,run_direct_conn_connect,arg)<0) {
00053 safe_free(arg);
00054 return ERROR_PTHREAD_CREATE_FAILED;
00055 }
00056
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
00066 direct_conn_connect_arg_t *cast_arg;
00067 struct sockaddr_in server;
00068 int ttl;
00069
00070
00071 CHECK_NOT_NULL(arg,(void*)ERROR_NULL_ARG_1);
00072
00073
00074
00075 sleep(1);
00076
00077 cast_arg = (direct_conn_connect_arg_t*)arg;
00078
00079 DEBUG(DBG_DIR_CONN,"DIR_CONN:starting connection\n");
00080
00081
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
00087 ttl = TTL_TOO_LOW;
00088 setsockopt(cast_arg->info->socks.buddy, IPPROTO_IP, IP_TTL,
00089 &ttl, sizeof(ttl));
00090
00091
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
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