@@ -52,38 +52,47 @@ static int _def_config(void)
5252
5353 sys_str2addr (" 0.0.0.0[0]" , (struct sockaddr *)>est_conf.client_addr , true );
5454 sys_str2addr (" 0.0.0.0[0]" , (struct sockaddr *)>est_conf.server_addr , true );
55- sys_str2addr (" 0 .0.0.0 [8888]" , (struct sockaddr *)>est_conf.remote_addr , true );
55+ sys_str2addr (" 192 .0.2.1 [8888]" , (struct sockaddr *)>est_conf.remote_unreachable_addr , true );
5656
5757 gtest_conf.port = 55555 ;
5858
5959 return rc;
6060}
6161
62- static void set_def_remote_address ()
62+ static void set_def_remote_address (bool user_defined_unreachable )
6363{
64- gtest_conf.def_gw_exists = sys_gateway ((struct sockaddr *)>est_conf.remote_addr ,
65- gtest_conf.server_addr .addr .sa_family );
66- if (gtest_conf.server_addr .addr .sa_family == AF_INET6) {
67- if (!gtest_conf.def_gw_exists ) {
68- sys_str2addr (" ::[8888]" , (struct sockaddr *)>est_conf.remote_addr , true );
69- } else {
70- gtest_conf.remote_addr .addr6 .sin6_port = htons (8888 );
71- }
64+ sa_family_t family = gtest_conf.server_addr .addr .sa_family ;
65+
66+ // Set default unreachable address for IPv6 if user didn't provide one
67+ if (family == AF_INET6 && !user_defined_unreachable) {
68+ // RFC 3849: 2001:db8::/32 is IPv6 documentation prefix (equivalent to 192.0.2.0/24 for
69+ // IPv4)
70+ sys_str2addr (" 2001:db8::1[8888]" , (struct sockaddr *)>est_conf.remote_unreachable_addr ,
71+ true );
72+ }
73+
74+ // If user provided an address without a port, default to 8888
75+ if (user_defined_unreachable &&
76+ sys_get_port ((struct sockaddr *)>est_conf.remote_unreachable_addr ) == 0 ) {
77+ sys_set_port ((struct sockaddr *)>est_conf.remote_unreachable_addr , 8888 );
7278 }
7379}
7480
7581static int _set_config (int argc, char **argv)
7682{
7783 int rc = 0 ;
7884 static struct option long_options[] = {
79- {" addr" , required_argument, 0 , ' a' }, {" if" , required_argument, 0 , ' i' },
80- {" remote" , required_argument, 0 , ' r' }, {" port" , required_argument, 0 , ' p' },
81- {" random" , required_argument, 0 , ' s' }, {" debug" , required_argument, 0 , ' d' },
85+ {" addr" , required_argument, 0 , ' a' },
86+ {" if" , required_argument, 0 , ' i' },
87+ {" remote-non-routable" , required_argument, 0 , ' r' },
88+ {" port" , required_argument, 0 , ' p' },
89+ {" random" , required_argument, 0 , ' s' },
90+ {" debug" , required_argument, 0 , ' d' },
8291 {" help" , no_argument, 0 , ' h' },
8392 };
8493 int op;
8594 int option_index;
86- bool user_defined_remote = false ;
95+ bool user_defined_unreachable = false ;
8796
8897 while ((op = getopt_long (argc, argv, " a:i:r:p:d:h" , long_options, &option_index)) != -1 ) {
8998 switch (op) {
@@ -147,12 +156,12 @@ static int _set_config(int argc, char **argv)
147156 }
148157 } break ;
149158 case ' r' : {
150- rc = sys_get_addr (optarg, (struct sockaddr *)>est_conf.remote_addr );
159+ rc = sys_get_addr (optarg, (struct sockaddr *)>est_conf.remote_unreachable_addr );
151160 if (rc < 0 ) {
152161 rc = -EINVAL;
153162 log_fatal (" Failed to resolve ip address %s\n " , optarg);
154163 } else {
155- user_defined_remote = true ;
164+ user_defined_unreachable = true ;
156165 }
157166 } break ;
158167 case ' p' :
@@ -195,16 +204,15 @@ static int _set_config(int argc, char **argv)
195204 srand (gtest_conf.random_seed );
196205 sys_set_port ((struct sockaddr *)>est_conf.server_addr , gtest_conf.port );
197206
198- if (!user_defined_remote) {
199- set_def_remote_address ();
200- }
207+ set_def_remote_address (user_defined_unreachable);
201208
202209 log_info (" CONFIGURATION:\n " );
203210 log_info (" log level: %d\n " , gtest_conf.log_level );
204211 log_info (" seed: %d\n " , gtest_conf.random_seed );
205212 log_info (" client ip: %s\n " , sys_addr2str ((struct sockaddr *)>est_conf.client_addr ));
206213 log_info (" server ip: %s\n " , sys_addr2str ((struct sockaddr *)>est_conf.server_addr ));
207- log_info (" remote ip: %s\n " , sys_addr2str ((struct sockaddr *)>est_conf.remote_addr ));
214+ log_info (" remote unreachable ip: %s\n " ,
215+ sys_addr2str ((struct sockaddr *)>est_conf.remote_unreachable_addr ));
208216 log_info (" port: %d\n " , gtest_conf.port );
209217 }
210218
@@ -214,13 +222,13 @@ static int _set_config(int argc, char **argv)
214222static void _usage (void )
215223{
216224 printf (" Usage: gtest [options]\n "
217- " \t --addr,-a <ip,ip> IP address client,server\n "
218- " \t --if,-i <ip,ip> Interface client,server\n "
219- " \t --remote,-r <ip> IP address remote \n "
220- " \t --port,-p <num> Listen/connect to port <num> (default %d).\n "
221- " \t --random,-s <count> Seed (default %d).\n "
222- " \t --debug,-d <level> Output verbose level (default: %d).\n "
223- " \t --help,-h Print help and exit\n " ,
225+ " \t --addr,-a <ip,ip> IP address client,server\n "
226+ " \t --if,-i <ip,ip> Interface client,server\n "
227+ " \t --remote-unreachable ,-r <ip> IP address not reachable \n "
228+ " \t --port,-p <num> Listen/connect to port <num> (default %d).\n "
229+ " \t --random,-s <count> Seed (default %d).\n "
230+ " \t --debug,-d <level> Output verbose level (default: %d).\n "
231+ " \t --help,-h Print help and exit\n " ,
224232
225233 gtest_conf.port , gtest_conf.random_seed , gtest_conf.log_level );
226234 exit (0 );
0 commit comments