mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-02-07 23:59:36 +08:00
new option --no-pcap-mutex
This commit is contained in:
parent
7980103bd5
commit
003f31bfef
6
misc.cpp
6
misc.cpp
@ -297,6 +297,7 @@ void process_arg(int argc, char *argv[]) //process all options
|
||||
{"dns-resolve", no_argument, 0, 1},
|
||||
{"pcap-send", no_argument, 0, 1},
|
||||
{"easy-tcp", no_argument, 0, 1},
|
||||
{"no-pcap-mutex", no_argument, 0, 1},
|
||||
{NULL, 0, 0, 0}
|
||||
};
|
||||
|
||||
@ -756,6 +757,11 @@ void process_arg(int argc, char *argv[]) //process all options
|
||||
send_with_pcap=1;
|
||||
mylog(log_info,"--pcap-send enabled, now pcap will be used for sending packet instead of libnet\n");
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"no-pcap-mutex")==0)
|
||||
{
|
||||
use_pcap_mutex=9;
|
||||
mylog(log_warn,"--no-pcap-mutex enabled, we will assume the underlying pcap calls are threadsafe\n");
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"easy-tcp")==0)
|
||||
{
|
||||
use_tcp_dummy_socket=1;
|
||||
|
16
network.cpp
16
network.cpp
@ -73,6 +73,7 @@ queue_t my_queue;
|
||||
|
||||
pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t pcap_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
int use_pcap_mutex=1;
|
||||
|
||||
ev_async async_watcher;
|
||||
|
||||
@ -326,9 +327,9 @@ void *pcap_recv_thread_entry(void *none)
|
||||
|
||||
while(1)
|
||||
{
|
||||
pthread_mutex_lock(&pcap_mutex);
|
||||
if(use_pcap_mutex) pthread_mutex_lock(&pcap_mutex);
|
||||
int ret=pcap_loop(pcap_handle, -1, my_packet_handler, NULL); //use -1 instead of 0 as cnt, since 0 is undefined in old versions
|
||||
pthread_mutex_unlock(&pcap_mutex);
|
||||
if(use_pcap_mutex) pthread_mutex_unlock(&pcap_mutex);
|
||||
if(ret==-1)
|
||||
mylog(log_warn,"pcap_loop exited with value %d\n",ret);
|
||||
else
|
||||
@ -845,11 +846,17 @@ void init_filter(int port)
|
||||
//pthread_mutex_lock(&pcap_mutex);//not sure if mutex is needed here
|
||||
|
||||
long long tmp_cnt=0;
|
||||
if(use_pcap_mutex)
|
||||
{
|
||||
while(pthread_mutex_trylock(&pcap_mutex)!=0)
|
||||
{
|
||||
tmp_cnt++;
|
||||
pcap_breakloop(pcap_handle);
|
||||
if(tmp_cnt%500==0)
|
||||
if(tmp_cnt==100)
|
||||
{
|
||||
mylog(log_warn,"%lld attempts of pcap_breakloop()\n", tmp_cnt);
|
||||
}
|
||||
if(tmp_cnt%1000==0)
|
||||
{
|
||||
mylog(log_warn,"%lld attempts of pcap_breakloop()\n", tmp_cnt);
|
||||
if(tmp_cnt>5000)
|
||||
@ -859,6 +866,7 @@ void init_filter(int port)
|
||||
}
|
||||
ev_sleep(0.001);
|
||||
}
|
||||
}
|
||||
|
||||
mylog(log_info,"breakloop() succeed after %lld attempt(s)\n", tmp_cnt);
|
||||
|
||||
@ -884,7 +892,7 @@ void init_filter(int port)
|
||||
}
|
||||
|
||||
|
||||
pthread_mutex_unlock(&pcap_mutex);
|
||||
if(use_pcap_mutex) pthread_mutex_unlock(&pcap_mutex);
|
||||
/*
|
||||
if(disable_bpf_filter) return;
|
||||
//if(raw_mode==mode_icmp) return ;
|
||||
|
Loading…
x
Reference in New Issue
Block a user