|
@@ -7,26 +7,10 @@
|
|
|
|
|
|
#include "common.h"
|
|
|
#include "log.h"
|
|
|
+#include "misc.h"
|
|
|
|
|
|
|
|
|
-
|
|
|
-int about_to_exit=0;
|
|
|
-
|
|
|
-raw_mode_t raw_mode=mode_faketcp;
|
|
|
-unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
|
|
|
-int socket_buf_size=1024*1024;
|
|
|
-int force_socket_buf=0;
|
|
|
-
|
|
|
static int random_number_fd=-1;
|
|
|
-string iptables_pattern="";
|
|
|
-int iptables_rule_added=0;
|
|
|
-int iptables_rule_keeped=0;
|
|
|
-int iptables_rule_keep_index=0;
|
|
|
-//int iptables_rule_no_clear=0;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
|
|
|
|
|
|
u64_t get_current_time()
|
|
|
{
|
|
@@ -58,167 +42,6 @@ char * my_ntoa(u32_t ip)
|
|
|
return inet_ntoa(a);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-/*
|
|
|
-int add_iptables_rule(const char * s)
|
|
|
-{
|
|
|
-
|
|
|
- iptables_pattern=s;
|
|
|
-
|
|
|
- string rule="iptables -I INPUT ";
|
|
|
- rule+=iptables_pattern;
|
|
|
- rule+=" -j DROP";
|
|
|
-
|
|
|
- char *output;
|
|
|
- if(run_command(rule.c_str(),output)==0)
|
|
|
- {
|
|
|
- mylog(log_warn,"auto added iptables rule by: %s\n",rule.c_str());
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- mylog(log_fatal,"auto added iptables failed by: %s\n",rule.c_str());
|
|
|
- //mylog(log_fatal,"reason : %s\n",strerror(errno));
|
|
|
- myexit(-1);
|
|
|
- }
|
|
|
- iptables_rule_added=1;
|
|
|
- return 0;
|
|
|
-}*/
|
|
|
-string chain[2];
|
|
|
-string rule_keep[2];
|
|
|
-string rule_keep_add[2];
|
|
|
-string rule_keep_del[2];
|
|
|
-u64_t keep_rule_last_time=0;
|
|
|
-
|
|
|
-pthread_t keep_thread;
|
|
|
-int keep_thread_running=0;
|
|
|
-int iptables_gen_add(const char * s,u32_t const_id)
|
|
|
-{
|
|
|
- string dummy="";
|
|
|
- iptables_pattern=s;
|
|
|
- chain[0] =dummy+ "udp2rawDwrW_C";
|
|
|
- rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
|
|
|
- rule_keep_add[0]=dummy+"iptables -I INPUT "+rule_keep[0];
|
|
|
-
|
|
|
- char *output;
|
|
|
- run_command(dummy+"iptables -N "+chain[0],output,show_none);
|
|
|
- run_command(dummy+"iptables -F "+chain[0],output);
|
|
|
- run_command(dummy+"iptables -I "+chain[0] + " -j DROP",output);
|
|
|
-
|
|
|
- rule_keep_del[0]=dummy+"iptables -D INPUT "+rule_keep[0];
|
|
|
-
|
|
|
- run_command(rule_keep_del[0],output,show_none);
|
|
|
- run_command(rule_keep_del[0],output,show_none);
|
|
|
-
|
|
|
- if(run_command(rule_keep_add[0],output)!=0)
|
|
|
- {
|
|
|
- mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[0].c_str());
|
|
|
- myexit(-1);
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-int iptables_rule_init(const char * s,u32_t const_id,int keep)
|
|
|
-{
|
|
|
- iptables_pattern=s;
|
|
|
- iptables_rule_added=1;
|
|
|
- iptables_rule_keeped=keep;
|
|
|
-
|
|
|
- string dummy="";
|
|
|
- char const_id_str[100];
|
|
|
- sprintf(const_id_str, "%x", const_id);
|
|
|
-
|
|
|
- chain[0] =dummy+ "udp2rawDwrW_"+const_id_str+"_C0";
|
|
|
- chain[1] =dummy+ "udp2rawDwrW_"+const_id_str+"_C1";
|
|
|
-
|
|
|
- rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
|
|
|
- rule_keep[1]=dummy+ iptables_pattern+" -j " +chain[1];
|
|
|
-
|
|
|
- rule_keep_add[0]=dummy+"iptables -I INPUT "+rule_keep[0];
|
|
|
- rule_keep_add[1]=dummy+"iptables -I INPUT "+rule_keep[1];
|
|
|
-
|
|
|
- rule_keep_del[0]=dummy+"iptables -D INPUT "+rule_keep[0];
|
|
|
- rule_keep_del[1]=dummy+"iptables -D INPUT "+rule_keep[1];
|
|
|
-
|
|
|
- keep_rule_last_time=get_current_time();
|
|
|
-
|
|
|
- char *output;
|
|
|
-
|
|
|
- for(int i=0;i<=iptables_rule_keeped;i++)
|
|
|
- {
|
|
|
- run_command(dummy+"iptables -N "+chain[i],output);
|
|
|
- run_command(dummy+"iptables -F "+chain[i],output);
|
|
|
- run_command(dummy+"iptables -I "+chain[i] + " -j DROP",output);
|
|
|
-
|
|
|
- if(run_command(rule_keep_add[i],output)!=0)
|
|
|
- {
|
|
|
- mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[i].c_str());
|
|
|
- myexit(-1);
|
|
|
- }
|
|
|
- }
|
|
|
- mylog(log_warn,"auto added iptables rules\n");
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int keep_iptables_rule() //magic to work on a machine without grep/iptables --check/-m commment
|
|
|
-{
|
|
|
- /*
|
|
|
- if(iptables_rule_keeped==0) return 0;
|
|
|
-
|
|
|
-
|
|
|
- uint64_t tmp_current_time=get_current_time();
|
|
|
- if(tmp_current_time-keep_rule_last_time<=iptables_rule_keep_interval)
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- keep_rule_last_time=tmp_current_time;
|
|
|
- }*/
|
|
|
-
|
|
|
- mylog(log_debug,"keep_iptables_rule begin %llu\n",get_current_time());
|
|
|
- iptables_rule_keep_index+=1;
|
|
|
- iptables_rule_keep_index%=2;
|
|
|
-
|
|
|
- string dummy="";
|
|
|
- char *output;
|
|
|
-
|
|
|
- int i=iptables_rule_keep_index;
|
|
|
-
|
|
|
- run_command(dummy + "iptables -N " + chain[i], output,show_none);
|
|
|
-
|
|
|
- if (run_command(dummy + "iptables -F " + chain[i], output,show_none) != 0)
|
|
|
- mylog(log_warn, "iptables -F failed %d\n",i);
|
|
|
-
|
|
|
- if (run_command(dummy + "iptables -I " + chain[i] + " -j DROP",output,show_none) != 0)
|
|
|
- mylog(log_warn, "iptables -I failed %d\n",i);
|
|
|
-
|
|
|
- if (run_command(rule_keep_del[i], output,show_none) != 0)
|
|
|
- mylog(log_warn, "rule_keep_del failed %d\n",i);
|
|
|
-
|
|
|
- run_command(rule_keep_del[i], output,show_none); //do it twice,incase it fails for unknown random reason
|
|
|
-
|
|
|
- if(run_command(rule_keep_add[i], output,show_log)!=0)
|
|
|
- mylog(log_warn, "rule_keep_del failed %d\n",i);
|
|
|
-
|
|
|
- mylog(log_debug,"keep_iptables_rule end %llu\n",get_current_time());
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int clear_iptables_rule()
|
|
|
-{
|
|
|
- char *output;
|
|
|
- string dummy="";
|
|
|
- if(!iptables_rule_added) return 0;
|
|
|
-
|
|
|
- for(int i=0;i<=iptables_rule_keeped;i++ )
|
|
|
- {
|
|
|
- run_command(rule_keep_del[i],output);
|
|
|
- run_command(dummy+"iptables -F "+chain[i],output);
|
|
|
- run_command(dummy+"iptables -X "+chain[i],output);
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
void init_random_number_fd()
|
|
|
{
|
|
|
|
|
@@ -326,8 +149,7 @@ unsigned short csum(const unsigned short *ptr,int nbytes) {//works both for big
|
|
|
return(answer);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-int set_buf_size(int fd)
|
|
|
+int set_buf_size(int fd,int socket_buf_size,int force_socket_buf)
|
|
|
{
|
|
|
if(force_socket_buf)
|
|
|
{
|
|
@@ -358,30 +180,6 @@ int set_buf_size(int fd)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-void myexit(int a)
|
|
|
-{
|
|
|
- if(enable_log_color)
|
|
|
- printf("%s\n",RESET);
|
|
|
- if(keep_thread_running)
|
|
|
- {
|
|
|
- if(pthread_cancel(keep_thread))
|
|
|
- {
|
|
|
- mylog(log_warn,"pthread_cancel failed\n");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- mylog(log_info,"pthread_cancel success\n");
|
|
|
- }
|
|
|
- }
|
|
|
- clear_iptables_rule();
|
|
|
- exit(a);
|
|
|
-}
|
|
|
-void signal_handler(int sig)
|
|
|
-{
|
|
|
- about_to_exit=1;
|
|
|
- // myexit(0);
|
|
|
-}
|
|
|
-
|
|
|
int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len)
|
|
|
{
|
|
|
static char buf[buf_len];
|
|
@@ -399,7 +197,6 @@ int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3)
|
|
|
{
|
|
|
if(len<int(sizeof(id_t)*3)) return -1;
|
|
@@ -502,6 +299,26 @@ bool larger_than_u16(uint16_t a,uint16_t b)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+void myexit(int a)
|
|
|
+{
|
|
|
+ if(enable_log_color)
|
|
|
+ printf("%s\n",RESET);
|
|
|
+ if(keep_thread_running)
|
|
|
+ {
|
|
|
+ if(pthread_cancel(keep_thread))
|
|
|
+ {
|
|
|
+ mylog(log_warn,"pthread_cancel failed\n");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ mylog(log_info,"pthread_cancel success\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ clear_iptables_rule();
|
|
|
+ exit(a);
|
|
|
+}
|
|
|
+
|
|
|
vector<string> string_to_vec(const char * s,const char * sp) {
|
|
|
vector<string> res;
|
|
|
string str=s;
|