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 <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
00035 #define BUFSIZE 64
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
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
00074
00075
00076
00077 void printUse();
00078
00079
00080
00081
00082
00083
00084
00085
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
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
00123 write(sd, msg, strlen(msg));
00124
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
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
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
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
00202 while (1)
00203 {
00204
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
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