2017-07-29 20:32:26 +08:00
# include "common.h"
# include "network.h"
2017-09-23 02:40:23 -05:00
# include "connection.h"
2017-09-23 03:35:28 -05:00
# include "misc.h"
2017-07-24 21:18:58 +08:00
# include "log.h"
2017-08-11 12:25:04 +08:00
# include "lib/md5.h"
2017-08-22 11:00:44 -05:00
# include "encrypt.h"
2017-10-30 07:21:27 -05:00
# include "fd_manager.h"
2017-07-26 18:00:45 +08:00
2018-08-29 01:45:57 -05:00
int client_event_loop ( ) ;
int server_event_loop ( ) ;
2017-08-21 20:26:55 +08:00
/*
int test ( )
{
2017-08-26 05:38:33 -05:00
char ip_str [ 100 ] = " 8.8.8.8 " ;
u32_t ip = inet_addr ( ip_str ) ;
u32_t dest_ip ;
string if_name ;
string hw ;
find_lower_level_info ( ip , dest_ip , if_name , hw ) ;
printf ( " %s %s %s \n " , my_ntoa ( dest_ip ) , if_name . c_str ( ) , hw . c_str ( ) ) ;
exit ( 0 ) ;
return 0 ;
2017-08-21 20:26:55 +08:00
} */
2017-07-26 19:47:52 +08:00
int main ( int argc , char * argv [ ] )
{
2017-10-24 09:04:47 -05:00
//printf("%llu\n",u64_t(-1));
2017-08-26 05:38:33 -05:00
//test();
2017-08-26 08:25:38 -05:00
//printf("%s\n",my_ntoa(0x00ffffff));
2017-08-17 23:40:17 +08:00
//auto a=string_to_vec("a b c d ");
//printf("%d\n",(int)a.size());
2017-08-04 12:46:46 +08:00
//printf("%d %d %d %d",larger_than_u32(1,2),larger_than_u32(2,1),larger_than_u32(0xeeaaeebb,2),larger_than_u32(2,0xeeaaeebb));
2017-07-30 16:37:49 +08:00
//assert(0==1);
2017-07-29 20:32:26 +08:00
dup2 ( 1 , 2 ) ; //redirect stderr to stdout
2017-08-04 18:35:51 +08:00
signal ( SIGINT , signal_handler ) ;
signal ( SIGHUP , signal_handler ) ;
signal ( SIGKILL , signal_handler ) ;
signal ( SIGTERM , signal_handler ) ;
signal ( SIGQUIT , signal_handler ) ;
2017-08-23 07:01:21 -05:00
pre_process_arg ( argc , argv ) ;
2017-07-26 19:20:15 +08:00
2017-08-17 23:40:17 +08:00
if ( geteuid ( ) ! = 0 )
{
2018-02-24 17:26:29 -06:00
mylog ( log_warn , " root check failed, it seems like you are using a non-root account. we can try to continue, but it may fail. If you want to run udp2raw as non-root, you have to add iptables rule manually, and grant udp2raw CAP_NET_RAW capability, check README.md in repo for more info. \n " ) ;
2017-12-14 11:25:48 -06:00
}
else
{
mylog ( log_warn , " you can run udp2raw with non-root account for better security. check README.md in repo for more info. \n " ) ;
2017-08-17 23:40:17 +08:00
}
2018-07-17 11:33:02 -05:00
//local_ip_uint32=inet_addr(local_ip);
//source_ip_uint32=inet_addr(source_ip);
2018-06-05 11:26:55 -05:00
# if ENABLE_DNS_RESOLVE
2018-06-05 12:57:25 -05:00
//if(enable_dns_resolve)
//{
2017-10-24 22:58:58 +08:00
struct hostent * he ;
2018-06-05 11:26:55 -05:00
if ( ( he = gethostbyname ( remote_address ) ) = = NULL ) {
2018-06-05 12:57:25 -05:00
mylog ( log_error , " Unable to resolve hostname: %s, error:%s \n " , remote_address , hstrerror ( h_errno ) ) ;
myexit ( 1 ) ; /* error */
2017-10-24 22:58:58 +08:00
}
2017-10-25 00:30:23 +08:00
struct in_addr * * addr_list = ( struct in_addr * * ) he - > h_addr_list ;
2018-06-05 12:57:25 -05:00
assert ( he - > h_addrtype = = AF_INET ) ;
assert ( addr_list ! = NULL ) ;
2017-10-25 00:30:23 +08:00
remote_ip_uint32 = ( * addr_list [ 0 ] ) . s_addr ;
2018-06-05 12:57:25 -05:00
mylog ( log_info , " remote_address[%s] has been resolved to [%s] \n " , remote_address , my_ntoa ( remote_ip_uint32 ) ) ;
2017-10-24 22:58:58 +08:00
2017-07-31 19:01:50 +08:00
2018-06-05 11:26:55 -05:00
strcpy ( remote_ip , my_ntoa ( remote_ip_uint32 ) ) ;
2018-06-05 12:57:25 -05:00
//}
//else
2018-06-05 11:26:55 -05:00
# endif
2017-07-26 19:20:15 +08:00
2018-07-22 08:59:09 -05:00
mylog ( log_info , " remote_ip=[%s], make sure this is a vaild IP address \n " , remote_addr . get_ip ( ) ) ;
2017-08-02 18:57:27 +08:00
//current_time_rough=get_current_time();
2017-07-29 00:22:26 +08:00
2017-07-29 20:32:26 +08:00
init_random_number_fd ( ) ;
srand ( get_true_random_number_nz ( ) ) ;
2017-07-21 20:30:27 +08:00
const_id = get_true_random_number_nz ( ) ;
2017-07-19 06:05:08 +08:00
2017-07-29 20:32:26 +08:00
mylog ( log_info , " const_id:%x \n " , const_id ) ;
2017-07-11 18:01:11 +08:00
2018-06-23 16:19:15 -05:00
my_init_keys ( key_string , program_mode = = client_mode ? 1 : 0 ) ;
2017-07-24 11:31:21 +08:00
2017-08-05 17:31:07 +08:00
iptables_rule ( ) ;
2017-08-21 20:26:55 +08:00
init_raw_socket ( ) ;
2017-07-26 06:29:40 +08:00
if ( program_mode = = client_mode )
2017-07-11 18:01:11 +08:00
{
2017-07-21 20:30:27 +08:00
client_event_loop ( ) ;
2017-07-11 18:01:11 +08:00
}
else
{
2017-07-21 20:30:27 +08:00
server_event_loop ( ) ;
2017-07-11 18:01:11 +08:00
}
return 0 ;
}