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

natblaster_peer.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 natblaster_peer.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief contains natblaster entrypoint for peer
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         /* the return type is "int", but I return "errorcode"s because I know that
00041          * they are the same real type and that all the errorcodes are negative.
00042          * This makes debugging easier. */
00043 
00044         /* first find a device if needed */
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         /* now put the info in a conn_info structure */
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         /* buddy sock gets filled in below */
00079 
00080         /* bind the desired port for a connection to buddy */
00081         CHECK_FAILED(bindSocket(info.peer.port,&info.socks.buddy),ERROR_1);
00082 
00083 
00084         /* bind socket for helper connection (2 before buddy port); */
00085         if (random==FLAG_UNSET)
00086                 info.helper_conn.persistent_port = PORT_ADD(peer_port,-2);
00087         else /* bind a "random" port (not the conventional port) */
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         /* bind port for second helper connection */
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                 /* close the sockets */
00099                 close(info.socks.helper);
00100                 close(info.socks.helper_pred);
00101                 close(info.socks.buddy);
00102                 return ERROR_4;
00103         }
00104 
00105         /* close helper sockets */
00106         close(info.socks.helper);
00107         close(info.socks.helper_pred);
00108 
00109         return info.socks.buddy;
00110 }
00111 

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