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

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 peer.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief a stub program to test the natblaster peer functionality
00023  */
00024 
00025 #include <stdio.h>
00026 #include <getopt.h>
00027 #include <stdlib.h>
00028 #include "natblaster_peer.h"
00029 #include "errorcodes.h"
00030 #include "def.h"
00031 #include "berkeleyapi.h"
00032 #include "nethelp.h"
00033 
00034 /** @brief size of buffer to receive a message from the buddy in */
00035 #define BUFSIZE 64
00036 
00037 /**
00038  * @brief gets arguments from the command line
00039  *
00040  * uses errorcodes.h for error codes
00041  *
00042  * @param argc the number of arguments passed in
00043  * @param argv the vector of arguments
00044  * @param helper_ip a pointer to a pointer.  When finished, will point to a
00045  *        string with the helper's dotted-decimal IP address
00046  * @param helper_port pointer to the helper's port (will be filled in)
00047  * @param peer_ip a pointer to a pointer.  When finished, will point to a
00048  *        string with the peer's dotted-decimal IP address
00049  * @param peer_port pointer to the peer's starting port (will be filled in)
00050  * @param buddy_ext_ip a pointer to a pointer.  When finished, will point to a
00051  *        string with the buddy's dotted-decimal external IP address
00052  * @param buddy_int_ip a pointer to a pointer.  When finished, will point to a
00053  *        string with the buddy's dotted-decimal internal IP address
00054  * @param buddy_int_port pointer to the buddy's starting port (will be filled
00055  *        in)
00056  * @param dev a pointer to a pointer.  When finished, will point to a string
00057  *        with the network device to use.  If none is specified, will be NULL
00058  *        on function return
00059  * @param msg a pointer to a pointer.  When finished, will point to a string
00060  *        with the message to send to the buddy.
00061  * @param random a pointer to an flag_t to set to 1 if the peer wants to be
00062  *        random.
00063  *
00064  * @return SUCCESS, neg value on failure
00065  */
00066 int getArgs(int argc, char *argv[], char **helper_ip,
00067             port_t *helper_port, char **peer_ip,
00068                         port_t *peer_port, char **buddy_ext_ip,
00069                         char **buddy_int_ip, port_t *buddy_int_port,
00070                         char **dev, char **msg, flag_t *random);
00071 
00072 /**
00073  * @brief prints the program use
00074  *
00075  * @return void
00076  */
00077 void printUse();
00078 
00079 /**
00080  * @brief entry point for peer stub application
00081  *
00082  * @param argc the number of elements in the argument vector
00083  * @param argv the argument vector
00084  *
00085  * return 0 on success, neagive on failure
00086  */
00087 int main(int argc, char *argv[]) {
00088 
00089         char *helper_addr, *peer_addr, *buddy_ext_addr, *buddy_int_addr;
00090         port_t helper_port, peer_port, buddy_int_port;
00091         char *dev, *msg;
00092         sock_t sd;
00093         char buf[BUFSIZE];
00094         int nread;
00095         flag_t random = FLAG_UNSET;
00096         ip_t helper_num, peer_num, buddy_int_num, buddy_ext_num;
00097 
00098         if(FAILED(getArgs(argc, argv, &helper_addr, &helper_port, &peer_addr,
00099                                           &peer_port, &buddy_ext_addr, &buddy_int_addr,
00100                                           &buddy_int_port, &dev, &msg,&random))) {
00101                 printUse();
00102                 return ERROR_1;
00103         }
00104 
00105         /* put the ports in network byte order */
00106         helper_port    = htons( helper_port    );
00107         peer_port      = htons( peer_port      );
00108         buddy_int_port = htons( buddy_int_port );
00109 
00110         CHECK_FAILED(resolveIP(helper_addr,&helper_num),ERROR_1);
00111         CHECK_FAILED(resolveIP(peer_addr,&peer_num),ERROR_2);
00112         CHECK_FAILED(resolveIP(buddy_int_addr,&buddy_int_num),ERROR_3);
00113         CHECK_FAILED(resolveIP(buddy_ext_addr,&buddy_ext_num),ERROR_4);
00114 
00115         if ( (sd=natblaster_connect(helper_num,helper_port,peer_num,peer_port,
00116                                buddy_ext_num, buddy_int_num, buddy_int_port,
00117                                dev,random))<0) {
00118                 printf("UNSUCCESSFUL!!!\n");
00119                 return ERROR_2;
00120         }
00121 
00122         /* send the buddy a small message */
00123         write(sd, msg, strlen(msg));
00124         /* and recieve the buddy's message */
00125         memset(buf, '\0',sizeof(buf));
00126         if (read(sd, buf, sizeof(buf)-1)>0) {
00127                 printf("Buddy sent message: [%s]\n", buf);
00128                 printf("SUCCESS!!!\n");
00129         }
00130         else {
00131                 /* in pratice this case does not occur */
00132                 printf("no message received :(\n");
00133                 printf("QUASI-SUCCESS...\n");
00134         }
00135         close(sd);
00136 
00137         return (0);
00138 }
00139 
00140 
00141 void printUse() {
00142 
00143         printf("options:\n");
00144         printf("\t--helper_ip      : helper hostname or IP [required]\n");
00145         printf("\t--helper_port    : helper port to connect to [required]\n");
00146         printf("\t--local_ip       : local internal hostname or IP [required]\n");
00147         printf("\t--local_port     : local internal port [required]\n");
00148         printf("\t--buddy_ext_ip   : external hostname or IP of buddy [required]\n");
00149         printf("\t--buddy_int_ip   : internal hostname or IP of buddy [required]\n");
00150         printf("\t--buddy_int_port : internal port of buddy [required]\n");
00151         printf("\t--device         : device to connect on [optional]\n");
00152         printf("\t--message        : message to send to buddy (enclosed in quotes if contains white space)\n");
00153         printf("\t--random         : flag indicating if this peer should pretend to be random\n");
00154 
00155         printf("\n");
00156 
00157         return;
00158 }
00159 
00160 int getArgs(int argc, char *argv[], char **helper_ip,
00161                         port_t *helper_port, char **peer_ip,
00162                         port_t *peer_port, char **buddy_ext_ip,
00163                         char **buddy_int_ip, port_t *buddy_int_port,
00164                         char **dev, char **msg, flag_t *random) {
00165 
00166         char c;
00167         static struct option long_options[] =
00168         {
00169                 {"helper_ip",      required_argument, 0, 'a'},
00170                 {"helper_port",    required_argument, 0, 'b'},
00171                 {"local_ip",       required_argument, 0, 'c'},
00172                 {"local_port",     required_argument, 0, 'd'},
00173                 {"buddy_ext_ip",   required_argument, 0, 'e'},
00174                 {"buddy_int_ip",   required_argument, 0, 'f'},
00175                 {"buddy_int_port", required_argument, 0, 'g'},
00176                 {"device",         required_argument, 0, 'h'},
00177                 {"message",        required_argument, 0, 'i'},
00178                 {"random",         no_argument,       0, 'j'},
00179                 /* for invalid args */
00180                 {0,               0,                 0,  0 }
00181         };
00182                 
00183         CHECK_NOT_NEG(argc,ERROR_NULL_ARG_1);
00184         CHECK_NOT_NULL(argv,ERROR_NULL_ARG_2);
00185         CHECK_NOT_NULL(helper_ip,ERROR_NULL_ARG_3);
00186         CHECK_NOT_NULL(helper_port,ERROR_NULL_ARG_4);
00187         CHECK_NOT_NULL(peer_ip,ERROR_NULL_ARG_5);
00188         CHECK_NOT_NULL(peer_port,ERROR_NULL_ARG_6);
00189         CHECK_NOT_NULL(buddy_ext_ip,ERROR_NULL_ARG_7);
00190         CHECK_NOT_NULL(buddy_int_ip,ERROR_NULL_ARG_8);
00191         CHECK_NOT_NULL(buddy_int_port,ERROR_NULL_ARG_9);
00192         CHECK_NOT_NULL(dev,ERROR_NULL_ARG_10);
00193         CHECK_NOT_NULL(msg,ERROR_NULL_ARG_11);
00194         CHECK_NOT_NULL(random,ERROR_NULL_ARG_12);
00195 
00196         /* set default values */
00197         *helper_ip = *peer_ip = *buddy_ext_ip = NULL;
00198         *buddy_int_ip = *dev = *msg = NULL;
00199         *helper_port = *peer_port = *buddy_int_port = 0 ;
00200 
00201         /* loop over the arguments, and read them in */
00202         while (1)
00203         {
00204                 /* getopt_long stores the option index here. */
00205                 int option_index = 0;
00206 
00207                 c = getopt_long (argc, argv, "a:b:c:d:e:f:g:h:i:j:",
00208                         long_options, &option_index);
00209 
00210                 /* Detect the end of the options. */
00211                 if (c == -1)
00212                         break;
00213 
00214                 switch (c) {
00215                         case 'a' :
00216                                 *helper_ip = optarg;
00217                                 break;
00218                         case 'b' :
00219                                 *helper_port = (unsigned short) atoi(optarg);
00220                                 break;
00221                         case 'c' :
00222                                 *peer_ip = optarg;
00223                                 break;
00224                         case 'd' :
00225                                 *peer_port = (unsigned short) atoi(optarg);
00226                                 break;
00227                         case 'e' :
00228                                 *buddy_ext_ip = optarg;
00229                                 break;
00230                         case 'f':
00231                                 *buddy_int_ip =optarg;
00232                                 break;
00233                         case 'g' :
00234                                 *buddy_int_port = (unsigned short) atoi(optarg);
00235                                 break;
00236                         case 'h' :
00237                                 *dev = optarg;
00238                                 break;
00239                         case 'i':
00240                                 *msg = optarg;
00241                                 break;
00242                         case 'j' :
00243                                 *random = FLAG_SET;
00244                                 break;
00245                         case '?':
00246                                 return ERROR_1;
00247                                 break;
00248                         default:
00249                                 return ERROR_2;
00250                                 break;
00251                 }
00252         }
00253 
00254         if ( (*helper_ip==NULL) || (*buddy_ext_ip==NULL) ||
00255              (buddy_int_ip==NULL) || (*msg==NULL) )
00256                 return ERROR_3;
00257 
00258         if ( (*helper_port==0) || (*peer_port==0) || (*buddy_int_port==0))
00259                 return ERROR_4;
00260 
00261         return SUCCESS;
00262 }
00263 

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