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 "natblaster_peer.h"
00026
00027 #include "debug.h"
00028 #include "errorcodes.h"
00029 #include "nethelp.h"
00030 #include "peerdef.h"
00031 #include "peerfsm.h"
00032 #include "peercon.h"
00033
00034 int natblaster_connect(ip_t helper_ip, port_t helper_port, ip_t peer_ip,
00035 port_t peer_port, ip_t buddy_ext_ip, ip_t buddy_int_ip,
00036 port_t buddy_int_port, char *device, flag_t random) {
00037
00038 peer_conn_info_t info;
00039
00040
00041
00042
00043
00044
00045 if (device==NULL)
00046 CHECK_FAILED(findDevice(&device),ERROR_NO_DEV_FOUND);
00047
00048 DEBUG(DBG_VERBOSE, "VERBOSE:connecting to Buddy....%s:%uint\n",
00049 DBG_IP(buddy_int_ip), DBG_PORT(buddy_int_port));
00050 DEBUG(DBG_VERBOSE," %sext\n", DBG_IP(buddy_ext_ip));
00051 DEBUG(DBG_VERBOSE, "VERBOSE:from local address.....%s:%u\n",
00052 DBG_IP(peer_ip), DBG_PORT(peer_port));
00053 DEBUG(DBG_VERBOSE, "VERBOSE:with helper............%s:%u\n",
00054 DBG_IP(helper_ip), DBG_PORT(helper_port));
00055 DEBUG(DBG_VERBOSE, "VERBOSE:using Device...........%s\n",
00056 device);
00057 DEBUG(DBG_VERBOSE, "VERBOSE: this peer is %srandom\n",
00058 (random==FLAG_SET ? "" : "not "));
00059
00060
00061 info.helper.ip = helper_ip;
00062 info.helper.port = helper_port;
00063 info.peer.ip = peer_ip;
00064 info.peer.port = peer_port;
00065 info.peer.set = FLAG_SET;
00066 info.buddy.int_ip = buddy_int_ip;
00067 info.buddy.int_port = buddy_int_port;
00068 info.buddy.ext_ip = buddy_ext_ip;
00069 info.buddy.identifier = FLAG_SET;
00070 info.buddy.ext_port = PORT_UNKNOWN;
00071 info.buddy.ext_port_set = FLAG_UNSET;
00072 info.socks.helper = SOCKET_UNKNOWN;
00073 info.socks.helper_pred = SOCKET_UNKNOWN;
00074 info.socks.buddy = SOCKET_UNKNOWN;
00075 info.device = device;
00076 info.direct_conn_status = FLAG_UNSET;
00077 info.bday.stop_synack_find = FLAG_UNSET;
00078
00079
00080
00081 CHECK_FAILED(bindSocket(info.peer.port,&info.socks.buddy),ERROR_1);
00082
00083
00084
00085 if (random==FLAG_UNSET)
00086 info.helper_conn.persistent_port = PORT_ADD(peer_port,-2);
00087 else
00088 info.helper_conn.persistent_port = PORT_ADD(peer_port,-3);
00089 CHECK_FAILED(bindSocket(info.helper_conn.persistent_port,
00090 &info.socks.helper),ERROR_2);
00091
00092
00093 info.helper_conn.prediction_port = PORT_ADD(peer_port,-1);
00094 CHECK_FAILED(bindSocket(info.helper_conn.prediction_port,
00095 &info.socks.helper_pred),ERROR_3);
00096
00097 if (FAILED(peer_fsm_start(&info))) {
00098
00099 close(info.socks.helper);
00100 close(info.socks.helper_pred);
00101 close(info.socks.buddy);
00102 return ERROR_4;
00103 }
00104
00105
00106 close(info.socks.helper);
00107 close(info.socks.helper_pred);
00108
00109 return info.socks.buddy;
00110 }
00111