mirror of
				https://github.com/wangyu-/udp2raw.git
				synced 2025-11-04 04:05:35 +08:00 
			
		
		
		
	added ipv6 support,reduce diff with non-mp version
This commit is contained in:
		
							
								
								
									
										276
									
								
								client.cpp
									
									
									
									
									
								
							
							
						
						
									
										276
									
								
								client.cpp
									
									
									
									
									
								
							@@ -44,19 +44,21 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
 | 
			
		||||
		{
 | 
			
		||||
			return 0;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
		struct sockaddr_in remote_addr_in={0};
 | 
			
		||||
 | 
			
		||||
		socklen_t slen = sizeof(sockaddr_in);
 | 
			
		||||
		//memset(&remote_addr_in, 0, sizeof(remote_addr_in));
 | 
			
		||||
		int port=get_true_random_number()%65534+1;
 | 
			
		||||
		remote_addr_in.sin_family = AF_INET;
 | 
			
		||||
		remote_addr_in.sin_port = htons(port);
 | 
			
		||||
		remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
 | 
			
		||||
		remote_addr_in.sin_addr.s_addr = remote_ip_uint32;*/
 | 
			
		||||
		int port=get_true_random_number()%65534+1;
 | 
			
		||||
		address_t tmp_addr=remote_addr;
 | 
			
		||||
		tmp_addr.set_port(port);
 | 
			
		||||
 | 
			
		||||
		if(use_udp_for_detection)
 | 
			
		||||
		{
 | 
			
		||||
			int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 | 
			
		||||
			int new_udp_fd=socket(tmp_addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
 | 
			
		||||
			if(new_udp_fd<0)
 | 
			
		||||
			{
 | 
			
		||||
				mylog(log_warn,"create new_udp_fd error\n");
 | 
			
		||||
@@ -65,7 +67,7 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
 | 
			
		||||
			setnonblocking(new_udp_fd);
 | 
			
		||||
			u64_t tmp=get_true_random_number();
 | 
			
		||||
 | 
			
		||||
			int ret=sendto(new_udp_fd,(char*)(&tmp),sizeof(tmp),0,(struct sockaddr *)&remote_addr_in,sizeof(remote_addr_in));
 | 
			
		||||
			int ret=sendto(new_udp_fd,(char*)(&tmp),sizeof(tmp),0,(struct sockaddr *)&tmp_addr.inner,tmp_addr.get_len());
 | 
			
		||||
			if(ret==-1)
 | 
			
		||||
			{
 | 
			
		||||
				mylog(log_warn,"sendto() failed\n");
 | 
			
		||||
@@ -77,14 +79,14 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
 | 
			
		||||
		{
 | 
			
		||||
			static int last_tcp_fd=-1;
 | 
			
		||||
 | 
			
		||||
			int new_tcp_fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 | 
			
		||||
			int new_tcp_fd=socket(tmp_addr.get_type(), SOCK_STREAM, IPPROTO_TCP);
 | 
			
		||||
			if(new_tcp_fd<0)
 | 
			
		||||
			{
 | 
			
		||||
				mylog(log_warn,"create new_tcp_fd error\n");
 | 
			
		||||
				return -1;
 | 
			
		||||
			}
 | 
			
		||||
			setnonblocking(new_tcp_fd);
 | 
			
		||||
			connect(new_tcp_fd,(struct sockaddr *)&remote_addr_in,sizeof(remote_addr_in));
 | 
			
		||||
			connect(new_tcp_fd,(struct sockaddr *)&tmp_addr.inner,tmp_addr.get_len());
 | 
			
		||||
			if(last_tcp_fd!=-1)
 | 
			
		||||
				sock_close(last_tcp_fd);
 | 
			
		||||
			last_tcp_fd=new_tcp_fd;
 | 
			
		||||
@@ -123,29 +125,48 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		u32_t new_ip=0;
 | 
			
		||||
		if(!force_source_ip&&get_src_adress(new_ip,remote_ip_uint32,remote_port)==0)
 | 
			
		||||
		address_t tmp_addr;
 | 
			
		||||
		//u32_t new_ip=0;
 | 
			
		||||
		if(!force_source_ip)
 | 
			
		||||
		{
 | 
			
		||||
			if(get_src_adress2(tmp_addr,remote_addr)!=0)
 | 
			
		||||
			{
 | 
			
		||||
				mylog(log_warn,"get_src_adress() failed\n");
 | 
			
		||||
				return -1;
 | 
			
		||||
			}
 | 
			
		||||
			//source_addr=new_addr;
 | 
			
		||||
			//source_addr.set_port(0);
 | 
			
		||||
 | 
			
		||||
			mylog(log_info,"source_addr is now %s\n",tmp_addr.get_ip());
 | 
			
		||||
 | 
			
		||||
			/*
 | 
			
		||||
			if(new_ip!=source_ip_uint32)
 | 
			
		||||
			{
 | 
			
		||||
				mylog(log_info,"source ip changed from %s to ",my_ntoa(source_ip_uint32));
 | 
			
		||||
				log_bare(log_info,"%s\n",my_ntoa(new_ip));
 | 
			
		||||
				source_ip_uint32=new_ip;
 | 
			
		||||
				send_info.src_ip=new_ip;
 | 
			
		||||
			}
 | 
			
		||||
			}*/
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			tmp_addr=source_addr;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (source_port == 0)
 | 
			
		||||
		send_info.new_src_ip.from_address_t(tmp_addr);
 | 
			
		||||
 | 
			
		||||
		if (force_source_port == 0)
 | 
			
		||||
		{
 | 
			
		||||
			send_info.src_port = client_bind_to_a_new_port(bind_fd,0);
 | 
			
		||||
			send_info.src_port = client_bind_to_a_new_port2(bind_fd,tmp_addr);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			send_info.src_port = source_port;
 | 
			
		||||
			assert(try_to_list_and_bind(bind_fd,0,source_port)==0);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		if (raw_mode == mode_icmp)
 | 
			
		||||
		{
 | 
			
		||||
			send_info.dst_port = send_info.src_port;
 | 
			
		||||
@@ -165,18 +186,9 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
 | 
			
		||||
			if(use_tcp_dummy_socket)
 | 
			
		||||
			{
 | 
			
		||||
 | 
			
		||||
				struct sockaddr_in remote_addr_in={0};
 | 
			
		||||
				socklen_t slen = sizeof(sockaddr_in);
 | 
			
		||||
				//memset(&remote_addr_in, 0, sizeof(remote_addr_in));
 | 
			
		||||
				remote_addr_in.sin_family = AF_INET;
 | 
			
		||||
				remote_addr_in.sin_port = htons(remote_port);
 | 
			
		||||
				remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
 | 
			
		||||
 | 
			
		||||
				//int new_tcp_fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 | 
			
		||||
 | 
			
		||||
				setnonblocking(bind_fd);
 | 
			
		||||
				int ret=connect(bind_fd,(struct sockaddr *)&remote_addr_in,sizeof(remote_addr_in));
 | 
			
		||||
				mylog(log_info,"ret=%d,errno=%s,%d %d\n",ret,get_sock_error(),bind_fd,remote_port);
 | 
			
		||||
				int ret=connect(bind_fd,(struct sockaddr *)&remote_addr.inner,remote_addr.get_len());
 | 
			
		||||
				mylog(log_info,"ret=%d,errno=%s,%d %s\n",ret,get_sock_error(),bind_fd,remote_addr.get_str());
 | 
			
		||||
				conn_info.state.client_current_state=client_tcp_handshake_dummy;
 | 
			
		||||
				mylog(log_info,"state changed from client_idle to client_tcp_handshake_dummy\n");
 | 
			
		||||
			}
 | 
			
		||||
@@ -389,11 +401,8 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
 | 
			
		||||
 | 
			
		||||
	if(conn_info.state.client_current_state==client_idle )
 | 
			
		||||
	{
 | 
			
		||||
		g_packet_buf_cnt--;
 | 
			
		||||
		discard_raw_packet();
 | 
			
		||||
		//recv(raw_recv_fd, 0,0, 0  );
 | 
			
		||||
		//pthread_mutex_lock(&queue_mutex);
 | 
			
		||||
		//my_queue.pop_front();
 | 
			
		||||
		//pthread_mutex_unlock(&queue_mutex);
 | 
			
		||||
	}
 | 
			
		||||
	else if(conn_info.state.client_current_state==client_tcp_handshake||conn_info.state.client_current_state==client_tcp_handshake_dummy)//received syn ack
 | 
			
		||||
	{
 | 
			
		||||
@@ -402,9 +411,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
 | 
			
		||||
		{
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
 | 
			
		||||
		if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
 | 
			
		||||
		{
 | 
			
		||||
			mylog(log_debug,"unexpected adress %x %x %d %d\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
 | 
			
		||||
			mylog(log_debug,"unexpected adress %s %s %d %d\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(data_len==0&&raw_info.recv_info.syn==1&&raw_info.recv_info.ack==1)
 | 
			
		||||
@@ -444,9 +453,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
 | 
			
		||||
			mylog(log_debug,"recv_bare failed!\n");
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
 | 
			
		||||
		if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
 | 
			
		||||
		{
 | 
			
		||||
			mylog(log_debug,"unexpected adress %x %x %d %d\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
 | 
			
		||||
			mylog(log_debug,"unexpected adress %s %s %d %d\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(data_len<int( 3*sizeof(my_id_t)))
 | 
			
		||||
@@ -505,9 +514,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
 | 
			
		||||
			mylog(log_debug,"recv_safer failed!\n");
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
 | 
			
		||||
		if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
 | 
			
		||||
		{
 | 
			
		||||
			mylog(log_warn,"unexpected adress %x %x %d %d,this shouldnt happen.\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
 | 
			
		||||
			mylog(log_warn,"unexpected adress %s %s %d %d,this shouldnt happen.\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(conn_info.state.client_current_state==client_handshake2)
 | 
			
		||||
@@ -544,8 +553,8 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
 | 
			
		||||
 | 
			
		||||
			conn_info.blob->conv_manager.c.update_active_time(tmp_conv_id);
 | 
			
		||||
 | 
			
		||||
			//u64_t u64=conn_info.blob->conv_manager.c.find_dat_by_conv(tmp_conv_id);
 | 
			
		||||
			address_t addr=conn_info.blob->conv_manager.c.find_data_by_conv(tmp_conv_id);
 | 
			
		||||
			//u64_t u64=conn_info.blob->conv_manager.c.find_data_by_conv(tmp_conv_id);
 | 
			
		||||
			address_t tmp_addr=conn_info.blob->conv_manager.c.find_data_by_conv(tmp_conv_id);
 | 
			
		||||
 | 
			
		||||
			//sockaddr_in tmp_sockaddr={0};
 | 
			
		||||
 | 
			
		||||
@@ -555,15 +564,13 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
 | 
			
		||||
			//tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			int ret=sendto(udp_fd,data+sizeof(u32_t),data_len -(sizeof(u32_t)),0,(struct sockaddr *)&addr.inner,addr.get_len());
 | 
			
		||||
			int ret=sendto(udp_fd,data+sizeof(u32_t),data_len -(sizeof(u32_t)),0,(struct sockaddr *)&tmp_addr.inner,tmp_addr.get_len());
 | 
			
		||||
 | 
			
		||||
			if(ret<0)
 | 
			
		||||
			{
 | 
			
		||||
		    	mylog(log_warn,"sento returned %d\n",ret);
 | 
			
		||||
 | 
			
		||||
		    	mylog(log_warn,"sento returned %d,%s,%02x,%s\n",ret,get_sock_error(),int(tmp_addr.get_type()),tmp_addr.get_str());
 | 
			
		||||
				//perror("ret<0");
 | 
			
		||||
			}
 | 
			
		||||
			//mylog(log_trace,"%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port));
 | 
			
		||||
			//mylog(log_trace,"%d byte sent\n",ret);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
@@ -580,28 +587,22 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
 | 
			
		||||
	}
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void udp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
 | 
			
		||||
int client_on_udp_recv(conn_info_t &conn_info)
 | 
			
		||||
{
 | 
			
		||||
	char buf[buf_len];
 | 
			
		||||
 | 
			
		||||
	conn_info_t & conn_info= *((conn_info_t*)watcher->data);;
 | 
			
		||||
 | 
			
		||||
	int recv_len;
 | 
			
		||||
	address_t addr;
 | 
			
		||||
 | 
			
		||||
	char buf[buf_len];
 | 
			
		||||
	address_t::storage_t udp_new_addr_in={0};
 | 
			
		||||
	socklen_t udp_new_addr_len = sizeof(address_t::storage_t);
 | 
			
		||||
	if ((recv_len = recvfrom(udp_fd, buf, max_data_len, 0,
 | 
			
		||||
	if ((recv_len = recvfrom(udp_fd, buf, max_data_len+1, 0,
 | 
			
		||||
			(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
 | 
			
		||||
		mylog(log_debug,"recv_from error,this shouldnt happen,err=%s,but we can try to continue\n",get_sock_error());
 | 
			
		||||
		return;
 | 
			
		||||
		mylog(log_warn,"recv_from error,%s\n",get_sock_error());
 | 
			
		||||
		//myexit(1);
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	if(recv_len==max_data_len+1)
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
 | 
			
		||||
		return;
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if(recv_len>=mtu_warn)
 | 
			
		||||
@@ -611,26 +612,25 @@ void udp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
 | 
			
		||||
	//mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
 | 
			
		||||
			//ntohs(udp_new_addr_in.sin_port),recv_len);
 | 
			
		||||
 | 
			
		||||
	addr.from_sockaddr((struct sockaddr *) &udp_new_addr_in,udp_new_addr_len);
 | 
			
		||||
 | 
			
		||||
	//u64_t u64=((u64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
 | 
			
		||||
	address_t tmp_addr;
 | 
			
		||||
	tmp_addr.from_sockaddr((sockaddr *)&udp_new_addr_in,udp_new_addr_len);
 | 
			
		||||
	u32_t conv;
 | 
			
		||||
 | 
			
		||||
	if(!conn_info.blob->conv_manager.c.is_data_used(addr))
 | 
			
		||||
	if(!conn_info.blob->conv_manager.c.is_data_used(tmp_addr))
 | 
			
		||||
	{
 | 
			
		||||
		if(conn_info.blob->conv_manager.c.get_size() >=max_conv_num)
 | 
			
		||||
		{
 | 
			
		||||
			mylog(log_warn,"ignored new udp connect bc max_conv_num exceed\n");
 | 
			
		||||
			return;
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
		conv=conn_info.blob->conv_manager.c.get_new_conv();
 | 
			
		||||
		conn_info.blob->conv_manager.c.insert_conv(conv,addr);
 | 
			
		||||
		conn_info.blob->conv_manager.c.insert_conv(conv,tmp_addr);
 | 
			
		||||
		//mylog(log_info,"new packet from %s:%d,conv_id=%x\n",inet_ntoa(udp_new_addr_in.sin_addr),ntohs(udp_new_addr_in.sin_port),conv);
 | 
			
		||||
		mylog(log_info,"new packet from %s,conv_id=%x\n",addr.get_str(),conv);
 | 
			
		||||
		mylog(log_info,"new packet from %s,conv_id=%x\n",tmp_addr.get_str(),conv);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		conv=conn_info.blob->conv_manager.c.find_conv_by_data(addr);
 | 
			
		||||
		conv=conn_info.blob->conv_manager.c.find_conv_by_data(tmp_addr);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conn_info.blob->conv_manager.c.update_active_time(conv);
 | 
			
		||||
@@ -639,7 +639,12 @@ void udp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
 | 
			
		||||
	{
 | 
			
		||||
		send_data_safer(conn_info,buf,recv_len,conv);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
void udp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
 | 
			
		||||
{
 | 
			
		||||
	conn_info_t & conn_info= *((conn_info_t*)watcher->data);
 | 
			
		||||
	client_on_udp_recv(conn_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void raw_recv_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
 | 
			
		||||
@@ -705,11 +710,7 @@ void async_cb(struct ev_loop *loop, struct ev_async *watcher, int revents)
 | 
			
		||||
void clear_timer_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents)
 | 
			
		||||
{
 | 
			
		||||
	conn_info_t & conn_info= *((conn_info_t*)watcher->data);
 | 
			
		||||
	//u64_t value;
 | 
			
		||||
	//read(timer_fd, &value, 8);
 | 
			
		||||
	client_on_timer(conn_info);
 | 
			
		||||
	mylog(log_trace,"epoll_trigger_counter:  %d \n",epoll_trigger_counter);
 | 
			
		||||
	epoll_trigger_counter=0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void fifo_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
 | 
			
		||||
@@ -753,41 +754,85 @@ int client_event_loop()
 | 
			
		||||
	packet_info_t &send_info=conn_info.raw_info.send_info;
 | 
			
		||||
	packet_info_t &recv_info=conn_info.raw_info.recv_info;
 | 
			
		||||
 | 
			
		||||
	if(source_ip_uint32==0)
 | 
			
		||||
	/*
 | 
			
		||||
	if(lower_level)
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_info,"get_src_adress called\n");
 | 
			
		||||
		if(retry_on_error==0)
 | 
			
		||||
		if(lower_level_manual)
 | 
			
		||||
		{
 | 
			
		||||
			if(get_src_adress(source_ip_uint32,remote_ip_uint32,remote_port)!=0)
 | 
			
		||||
			{
 | 
			
		||||
				mylog(log_fatal,"the trick to auto get source ip failed, maybe you dont have internet access\n");
 | 
			
		||||
				myexit(-1);
 | 
			
		||||
			}
 | 
			
		||||
			int index;
 | 
			
		||||
			init_ifindex(if_name,raw_send_fd,index);
 | 
			
		||||
			//init_ifindex(if_name);
 | 
			
		||||
			memset(&send_info.addr_ll, 0, sizeof(send_info.addr_ll));
 | 
			
		||||
			send_info.addr_ll.sll_family = AF_PACKET;
 | 
			
		||||
			send_info.addr_ll.sll_ifindex =index;
 | 
			
		||||
			send_info.addr_ll.sll_halen = ETHER_ADDR_LEN;
 | 
			
		||||
			send_info.addr_ll.sll_protocol = htons(ETH_P_IP);
 | 
			
		||||
			memcpy(&send_info.addr_ll.sll_addr, dest_hw_addr, ETHER_ADDR_LEN);
 | 
			
		||||
			mylog(log_info,"we are running at lower-level (manual) mode\n");
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			int ok=0;
 | 
			
		||||
			while(!ok)
 | 
			
		||||
			{
 | 
			
		||||
				if(get_src_adress(source_ip_uint32,remote_ip_uint32,remote_port)!=0)
 | 
			
		||||
				{
 | 
			
		||||
					mylog(log_warn,"the trick to auto get source ip failed, maybe you dont have internet access, retry in %d seconds\n",retry_on_error_interval);
 | 
			
		||||
					sleep(retry_on_error_interval);
 | 
			
		||||
				}
 | 
			
		||||
				else
 | 
			
		||||
				{
 | 
			
		||||
					ok=1;
 | 
			
		||||
				}
 | 
			
		||||
			u32_t dest_ip;
 | 
			
		||||
			string if_name_string;
 | 
			
		||||
			string hw_string;
 | 
			
		||||
			assert(remote_addr.get_type()==AF_INET);
 | 
			
		||||
 | 
			
		||||
			if(retry_on_error==0)
 | 
			
		||||
			{
 | 
			
		||||
				if(find_lower_level_info(remote_addr.inner.ipv4.sin_addr.s_addr,dest_ip,if_name_string,hw_string)!=0)
 | 
			
		||||
				{
 | 
			
		||||
					mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_addr.get_ip());
 | 
			
		||||
					myexit(-1);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				int ok=0;
 | 
			
		||||
				while(!ok)
 | 
			
		||||
				{
 | 
			
		||||
					if(find_lower_level_info(remote_addr.inner.ipv4.sin_addr.s_addr,dest_ip,if_name_string,hw_string)!=0)
 | 
			
		||||
					{
 | 
			
		||||
						mylog(log_warn,"auto detect lower-level info failed for %s,retry in %d seconds\n",remote_addr.get_ip(),retry_on_error_interval);
 | 
			
		||||
						sleep(retry_on_error_interval);
 | 
			
		||||
					}
 | 
			
		||||
					else
 | 
			
		||||
					{
 | 
			
		||||
						ok=1;
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			mylog(log_info,"we are running at lower-level (auto) mode,%s %s %s\n",my_ntoa(dest_ip),if_name_string.c_str(),hw_string.c_str());
 | 
			
		||||
 | 
			
		||||
			u32_t hw[6];
 | 
			
		||||
			memset(hw, 0, sizeof(hw));
 | 
			
		||||
			sscanf(hw_string.c_str(), "%x:%x:%x:%x:%x:%x",&hw[0], &hw[1], &hw[2],
 | 
			
		||||
					&hw[3], &hw[4], &hw[5]);
 | 
			
		||||
 | 
			
		||||
			mylog(log_warn,
 | 
			
		||||
					"make sure this is correct:   if_name=<%s>  dest_mac_adress=<%02x:%02x:%02x:%02x:%02x:%02x>  \n",
 | 
			
		||||
					if_name_string.c_str(), hw[0], hw[1], hw[2], hw[3], hw[4], hw[5]);
 | 
			
		||||
			for (int i = 0; i < 6; i++) {
 | 
			
		||||
				dest_hw_addr[i] = uint8_t(hw[i]);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			//mylog(log_fatal,"--lower-level auto for client hasnt been implemented\n");
 | 
			
		||||
			int index;
 | 
			
		||||
			init_ifindex(if_name_string.c_str(),raw_send_fd,index);
 | 
			
		||||
 | 
			
		||||
			memset(&send_info.addr_ll, 0, sizeof(send_info.addr_ll));
 | 
			
		||||
			send_info.addr_ll.sll_family = AF_PACKET;
 | 
			
		||||
			send_info.addr_ll.sll_ifindex = index;
 | 
			
		||||
			send_info.addr_ll.sll_halen = ETHER_ADDR_LEN;
 | 
			
		||||
			send_info.addr_ll.sll_protocol = htons(ETH_P_IP);
 | 
			
		||||
			memcpy(&send_info.addr_ll.sll_addr, dest_hw_addr, ETHER_ADDR_LEN);
 | 
			
		||||
			//mylog(log_info,"we are running at lower-level (manual) mode\n");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	//in_addr tmp;
 | 
			
		||||
	//tmp.s_addr=source_ip_uint32;
 | 
			
		||||
	mylog(log_info,"source ip = %s\n",my_ntoa(source_ip_uint32));
 | 
			
		||||
	//printf("done\n");
 | 
			
		||||
	*/
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	if(strcmp(dev,"")==0)
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_info,"--dev have not been set, trying to detect automatically, avaliable deives:\n");
 | 
			
		||||
@@ -814,7 +859,7 @@ int client_event_loop()
 | 
			
		||||
					log_bare(log_debug," [a->addr==NULL]");
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				if(a->addr->sa_family == AF_INET)
 | 
			
		||||
				if(a->addr->sa_family == remote_addr.get_type())
 | 
			
		||||
				{
 | 
			
		||||
					cnt++;
 | 
			
		||||
					log_bare(log_warn," [%s]", inet_ntoa(((struct sockaddr_in*)a->addr)->sin_addr));
 | 
			
		||||
@@ -860,43 +905,31 @@ int client_event_loop()
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_info,"--dev has been manually set, using device:[%s]\n",dev);
 | 
			
		||||
	}
 | 
			
		||||
	}*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if(try_to_list_and_bind(bind_fd,local_ip_uint32,source_port)!=0)
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_fatal,"bind to source_port:%d fail\n ",source_port);
 | 
			
		||||
		myexit(-1);
 | 
			
		||||
	}
 | 
			
		||||
	send_info.src_port=source_port;
 | 
			
		||||
	send_info.src_ip = source_ip_uint32;
 | 
			
		||||
	send_info.src_port=0;
 | 
			
		||||
	memset(&send_info.new_src_ip,0,sizeof(send_info.new_src_ip));
 | 
			
		||||
 | 
			
		||||
	int i, j, k;int ret;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	send_info.dst_ip=remote_ip_uint32;
 | 
			
		||||
	send_info.dst_port=remote_port;
 | 
			
		||||
	send_info.new_dst_ip.from_address_t(remote_addr);
 | 
			
		||||
	send_info.dst_port=remote_addr.get_port();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 | 
			
		||||
    udp_fd=socket(local_addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
 | 
			
		||||
    set_buf_size(udp_fd,socket_buf_size);
 | 
			
		||||
 | 
			
		||||
	int yes = 1;
 | 
			
		||||
 | 
			
		||||
	struct sockaddr_in local_me={0};
 | 
			
		||||
 | 
			
		||||
	socklen_t slen = sizeof(sockaddr_in);
 | 
			
		||||
	local_me.sin_family = AF_INET;
 | 
			
		||||
	local_me.sin_port = htons(local_port);
 | 
			
		||||
	local_me.sin_addr.s_addr = local_ip_uint32;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (::bind(udp_fd, (struct sockaddr*) &local_me, slen) == -1) {
 | 
			
		||||
	if (::bind(udp_fd, (struct sockaddr*) &local_addr.inner, local_addr.get_len()) == -1) {
 | 
			
		||||
		mylog(log_fatal,"socket bind error\n");
 | 
			
		||||
		//perror("socket bind error");
 | 
			
		||||
		myexit(1);
 | 
			
		||||
	}
 | 
			
		||||
	setnonblocking(udp_fd);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	//epollfd = epoll_create1(0);
 | 
			
		||||
 | 
			
		||||
	//const int max_events = 4096;
 | 
			
		||||
@@ -934,32 +967,29 @@ int client_event_loop()
 | 
			
		||||
	//	myexit(-1);
 | 
			
		||||
	//}
 | 
			
		||||
 | 
			
		||||
	//struct ev_io raw_watcher;
 | 
			
		||||
    /*
 | 
			
		||||
	struct ev_io raw_recv_watcher;
 | 
			
		||||
 | 
			
		||||
	//raw_watcher.data=&conn_info;
 | 
			
		||||
   // ev_io_init(&raw_watcher, raw_recv_cb, raw_recv_fd, EV_READ);
 | 
			
		||||
    //ev_io_start(loop, &raw_watcher);
 | 
			
		||||
	raw_recv_watcher.data=&conn_info;
 | 
			
		||||
    ev_io_init(&raw_recv_watcher, raw_recv_cb, raw_recv_fd, EV_READ);
 | 
			
		||||
    ev_io_start(loop, &raw_recv_watcher);
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
	g_default_loop=loop;
 | 
			
		||||
	async_watcher.data=&conn_info;
 | 
			
		||||
	ev_async_init(&async_watcher,async_cb);
 | 
			
		||||
	ev_async_start(loop,&async_watcher);
 | 
			
		||||
 | 
			
		||||
	init_raw_socket();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	int unbind=1;
 | 
			
		||||
	init_raw_socket();//must be put after dev detection
 | 
			
		||||
 | 
			
		||||
	//set_timer(epollfd,timer_fd);
 | 
			
		||||
 | 
			
		||||
	struct ev_timer clear_timer;
 | 
			
		||||
 | 
			
		||||
	clear_timer.data=&conn_info;
 | 
			
		||||
	ev_timer_init(&clear_timer, clear_timer_cb, 0, timer_interval/1000.0);
 | 
			
		||||
	ev_timer_start(loop, &clear_timer);
 | 
			
		||||
 | 
			
		||||
	mylog(log_debug,"send_raw : from %x %d  to %x %d\n",send_info.src_ip,send_info.src_port,send_info.dst_ip,send_info.dst_port);
 | 
			
		||||
	mylog(log_debug,"send_raw : from %s %d  to %s %d\n",send_info.new_src_ip.get_str1(),send_info.src_port,send_info.new_dst_ip.get_str2(),send_info.dst_port);
 | 
			
		||||
 | 
			
		||||
	int fifo_fd=-1;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@
 | 
			
		||||
#include <random>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
 | 
			
		||||
static int random_number_fd=-1;
 | 
			
		||||
//static int random_number_fd=-1;
 | 
			
		||||
int force_socket_buf=0;
 | 
			
		||||
 | 
			
		||||
int address_t::from_str(char *str)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										73
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										73
									
								
								main.cpp
									
									
									
									
									
								
							@@ -34,41 +34,42 @@ int main(int argc, char *argv[])
 | 
			
		||||
 | 
			
		||||
	init_ws();
 | 
			
		||||
 | 
			
		||||
	int i=0;
 | 
			
		||||
 | 
			
		||||
	//libnet_t *l;	/* the libnet context */
 | 
			
		||||
	//char errbuf[LIBNET_ERRBUF_SIZE];
 | 
			
		||||
 | 
			
		||||
	//l = libnet_init(LIBNET_RAW4, NULL, errbuf);
 | 
			
		||||
 | 
			
		||||
	dup2(1, 2);//redirect stderr to stdout
 | 
			
		||||
	//signal(SIGINT, signal_handler);
 | 
			
		||||
	//signal(SIGHUP, signal_handler);
 | 
			
		||||
	//signal(SIGKILL, signal_handler);
 | 
			
		||||
	//signal(SIGTERM, signal_handler);
 | 
			
		||||
	//signal(SIGQUIT, signal_handler);
 | 
			
		||||
 | 
			
		||||
	struct ev_loop* loop=ev_default_loop(0);
 | 
			
		||||
        //printf("%x %x\n",ev_supported_backends(),ev_backend(loop));
 | 
			
		||||
 | 
			
		||||
#if !defined(__MINGW32__)
 | 
			
		||||
    ev_signal signal_watcher_sigpipe;
 | 
			
		||||
    ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
 | 
			
		||||
    ev_signal_start(loop, &signal_watcher_sigpipe);
 | 
			
		||||
#else
 | 
			
		||||
     enable_log_color=0;
 | 
			
		||||
#if defined(__MINGW32__)
 | 
			
		||||
    enable_log_color=0;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    ev_signal signal_watcher_sigterm;
 | 
			
		||||
    ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
 | 
			
		||||
    ev_signal_start(loop, &signal_watcher_sigterm);
 | 
			
		||||
 | 
			
		||||
    ev_signal signal_watcher_sigint;
 | 
			
		||||
    ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
 | 
			
		||||
    ev_signal_start(loop, &signal_watcher_sigint);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	pre_process_arg(argc,argv);
 | 
			
		||||
 | 
			
		||||
	if(program_mode==client_mode)
 | 
			
		||||
	{
 | 
			
		||||
		struct ev_loop* loop=ev_default_loop(0);
 | 
			
		||||
#if !defined(__MINGW32__)
 | 
			
		||||
		ev_signal signal_watcher_sigpipe;
 | 
			
		||||
		ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
 | 
			
		||||
		ev_signal_start(loop, &signal_watcher_sigpipe);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		ev_signal signal_watcher_sigterm;
 | 
			
		||||
		ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
 | 
			
		||||
		ev_signal_start(loop, &signal_watcher_sigterm);
 | 
			
		||||
 | 
			
		||||
		ev_signal signal_watcher_sigint;
 | 
			
		||||
		ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
 | 
			
		||||
		ev_signal_start(loop, &signal_watcher_sigint);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_fatal,"server mode not supported in multi-platform version\n");
 | 
			
		||||
		myexit(-1);
 | 
			
		||||
		/*
 | 
			
		||||
		signal(SIGINT, signal_handler);
 | 
			
		||||
		signal(SIGHUP, signal_handler);
 | 
			
		||||
		signal(SIGKILL, signal_handler);
 | 
			
		||||
		signal(SIGTERM, signal_handler);
 | 
			
		||||
		signal(SIGQUIT, signal_handler);
 | 
			
		||||
		 */
 | 
			
		||||
	}
 | 
			
		||||
#if !defined(__MINGW32__)
 | 
			
		||||
	if(geteuid() != 0)
 | 
			
		||||
	{
 | 
			
		||||
@@ -79,12 +80,8 @@ int main(int argc, char *argv[])
 | 
			
		||||
		mylog(log_warn,"you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
	local_ip_uint32=inet_addr(local_ip);
 | 
			
		||||
	source_ip_uint32=inet_addr(source_ip);
 | 
			
		||||
 | 
			
		||||
	strcpy(remote_ip,remote_address);
 | 
			
		||||
	mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_ip);
 | 
			
		||||
	remote_ip_uint32=inet_addr(remote_ip);
 | 
			
		||||
	mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_addr.get_ip());
 | 
			
		||||
 | 
			
		||||
	//init_random_number_fd();
 | 
			
		||||
	srand(get_true_random_number_nz());
 | 
			
		||||
@@ -104,7 +101,9 @@ int main(int argc, char *argv[])
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_fatal,"server mode not supported in multi-platform version\n");
 | 
			
		||||
		myexit(-1);
 | 
			
		||||
		//server_event_loop();
 | 
			
		||||
		/*
 | 
			
		||||
		server_event_loop();
 | 
			
		||||
		*/
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										53
									
								
								misc.cpp
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								misc.cpp
									
									
									
									
									
								
							@@ -29,17 +29,17 @@ int ttl_value=64;
 | 
			
		||||
 | 
			
		||||
fd_manager_t fd_manager;
 | 
			
		||||
 | 
			
		||||
char remote_address[max_addr_len]="";
 | 
			
		||||
//char remote_address[max_addr_len]="";
 | 
			
		||||
//char remote_address[max_address_len]="";
 | 
			
		||||
char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip
 | 
			
		||||
u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
 | 
			
		||||
int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip  remote_ip,buf for port.source_port=0 indicates --source-port is not enabled
 | 
			
		||||
//char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip
 | 
			
		||||
//u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
 | 
			
		||||
//int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip  remote_ip,buf for port.source_port=0 indicates --source-port is not enabled
 | 
			
		||||
 | 
			
		||||
address_t local_addr,remote_addr,source_addr;
 | 
			
		||||
 | 
			
		||||
my_ip_t bind_addr;
 | 
			
		||||
 | 
			
		||||
//int source_port=-1;
 | 
			
		||||
int source_port=-1;
 | 
			
		||||
 | 
			
		||||
int bind_addr_used=0;
 | 
			
		||||
int force_source_ip=0; //if --source-ip is enabled
 | 
			
		||||
@@ -490,8 +490,9 @@ void process_arg(int argc, char *argv[])  //process all options
 | 
			
		||||
			else if(strcmp(long_options[option_index].name,"source-ip")==0)
 | 
			
		||||
			{
 | 
			
		||||
				mylog(log_debug,"parsing long option :source-ip\n");
 | 
			
		||||
				sscanf(optarg, "%s", source_ip);
 | 
			
		||||
				mylog(log_debug,"source: %s\n",source_ip);
 | 
			
		||||
				//sscanf(optarg, "%s", source_ip);
 | 
			
		||||
				source_addr.from_str_ip_only(optarg);
 | 
			
		||||
				mylog(log_debug,"source: %s\n",source_addr.get_ip());
 | 
			
		||||
				force_source_ip=1;
 | 
			
		||||
			}
 | 
			
		||||
			else if(strcmp(long_options[option_index].name,"source-port")==0)
 | 
			
		||||
@@ -499,6 +500,7 @@ void process_arg(int argc, char *argv[])  //process all options
 | 
			
		||||
				mylog(log_debug,"parsing long option :source-port\n");
 | 
			
		||||
				sscanf(optarg, "%d", &source_port);
 | 
			
		||||
				mylog(log_info,"source: %d\n",source_port);
 | 
			
		||||
				force_source_port=1;
 | 
			
		||||
			}
 | 
			
		||||
			else if(strcmp(long_options[option_index].name,"raw-mode")==0)
 | 
			
		||||
			{
 | 
			
		||||
@@ -785,7 +787,7 @@ void process_arg(int argc, char *argv[])  //process all options
 | 
			
		||||
 | 
			
		||||
	//if(lower_level)
 | 
			
		||||
		//process_lower_level_arg();
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
	 mylog(log_info,"important variables: ");
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"log_level=%d:%s ",log_level,log_text[log_level]);
 | 
			
		||||
@@ -804,6 +806,37 @@ void process_arg(int argc, char *argv[])  //process all options
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"socket_buf_size=%d ",socket_buf_size);
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"\n");
 | 
			
		||||
	 */
 | 
			
		||||
	if(program_mode==client_mode)
 | 
			
		||||
	{
 | 
			
		||||
		raw_ip_version=remote_addr.get_type();
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		raw_ip_version=local_addr.get_type();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	 mylog(log_info,"important variables: ");
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"log_level=%d:%s ",log_level,log_text[log_level]);
 | 
			
		||||
	 log_bare(log_info,"raw_mode=%s ",raw_mode_tostring[raw_mode]);
 | 
			
		||||
	 log_bare(log_info,"cipher_mode=%s ",cipher_mode_tostring[cipher_mode]);
 | 
			
		||||
	 log_bare(log_info,"auth_mode=%s ",auth_mode_tostring[auth_mode]);
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"key=%s ",key_string);
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"local_addr=%s ",local_addr.get_str());
 | 
			
		||||
	 log_bare(log_info,"remote_addr=%s ",remote_addr.get_str());
 | 
			
		||||
 | 
			
		||||
	 if(force_source_ip)
 | 
			
		||||
		 log_bare(log_info,"source_addr=%s ",source_addr.get_ip());
 | 
			
		||||
 | 
			
		||||
	 if(force_source_port)
 | 
			
		||||
		 log_bare(log_info,"source_port=%d ",source_port);
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"socket_buf_size=%d ",socket_buf_size);
 | 
			
		||||
 | 
			
		||||
	 log_bare(log_info,"\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1340,8 +1373,10 @@ int clear_iptables_rule()
 | 
			
		||||
 | 
			
		||||
void iptables_rule()  // handles -a -g --gen-add  --keep-rule --clear --wait-lock
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	if(generate_iptables_rule)
 | 
			
		||||
	{
 | 
			
		||||
#ifdef fixthis
 | 
			
		||||
		if(raw_mode==mode_faketcp && use_tcp_dummy_socket==1)
 | 
			
		||||
		{
 | 
			
		||||
			mylog(log_fatal, "failed,-g doesnt work with easy-faketcp mode\n");
 | 
			
		||||
@@ -1372,6 +1407,7 @@ void iptables_rule()  // handles -a -g --gen-add  --keep-rule --clear --wait-loc
 | 
			
		||||
		printf("\n");
 | 
			
		||||
 | 
			
		||||
		log_bare(log_warn,"for windows vista and above use:\n");
 | 
			
		||||
 | 
			
		||||
		if(raw_mode==mode_faketcp)
 | 
			
		||||
		{
 | 
			
		||||
			printf("netsh advfirewall firewall add rule name=udp2raw protocol=TCP dir=in remoteip=%s/32 remoteport=%d action=block\n",remote_ip,remote_port);
 | 
			
		||||
@@ -1389,6 +1425,7 @@ void iptables_rule()  // handles -a -g --gen-add  --keep-rule --clear --wait-loc
 | 
			
		||||
			printf("netsh advfirewall firewall add rule name=udp2raw protocol=ICMPV4 dir=out remoteip=%s/32 action=block\n",remote_ip);
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		myexit(0);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										112
									
								
								network.cpp
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								network.cpp
									
									
									
									
									
								
							@@ -12,6 +12,7 @@
 | 
			
		||||
int raw_recv_fd=-1;
 | 
			
		||||
int raw_send_fd=-1;
 | 
			
		||||
u32_t link_level_header_len=0;//set it to 14 if SOCK_RAW is used in socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
 | 
			
		||||
int use_tcp_dummy_socket=0;
 | 
			
		||||
 | 
			
		||||
int seq_mode=3;
 | 
			
		||||
int max_seq_mode=4;
 | 
			
		||||
@@ -79,15 +80,9 @@ ev_loop* g_default_loop;
 | 
			
		||||
 | 
			
		||||
pthread_t pcap_recv_thread;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
struct bpf_program g_filter;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int use_tcp_dummy_socket=0;
 | 
			
		||||
 | 
			
		||||
#if 0
 | 
			
		||||
struct sock_filter code_tcp_old[] = {
 | 
			
		||||
		{ 0x28, 0, 0, 0x0000000c },//0
 | 
			
		||||
@@ -567,9 +562,26 @@ int init_raw_socket()
 | 
			
		||||
 | 
			
		||||
	char filter_exp[1000];
 | 
			
		||||
 | 
			
		||||
	assert(source_ip_uint32!=0);
 | 
			
		||||
	address_t tmp_addr;
 | 
			
		||||
	if(get_src_adress2(tmp_addr,remote_addr)!=0)
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_error,"get_src_adress() failed, maybe you dont have internet\n");
 | 
			
		||||
		myexit(-1);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	sprintf(filter_exp,"src %s and dst %s and (tcp or udp or icmp)",my_ntoa(source_ip_uint32),remote_ip);
 | 
			
		||||
	string src=tmp_addr.get_ip();
 | 
			
		||||
	string dst=remote_addr.get_ip();
 | 
			
		||||
	if(raw_ip_version==AF_INET)
 | 
			
		||||
	{
 | 
			
		||||
		//sprintf(filter_exp,"ip and src %s and dst %s and (tcp or udp or icmp)",my_ntoa(source_ip_uint32),dst.c_str());
 | 
			
		||||
		sprintf(filter_exp,"ip and src %s and dst %s and (tcp or udp or icmp)",src.c_str(),dst.c_str());
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
		sprintf(filter_exp,"ip6 and src %s and dst %s and (tcp or udp or icmp6)",src.c_str(),dst.c_str());
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	 if (pcap_compile(pcap_handle, &g_filter, filter_exp, 0, PCAP_NETMASK_UNKNOWN ) == -1) {
 | 
			
		||||
		 printf("Bad filter - %s\n", pcap_geterr(pcap_handle));
 | 
			
		||||
@@ -589,8 +601,6 @@ int init_raw_socket()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	g_ip_id_counter=get_true_random_number()%65535;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
@@ -766,22 +776,46 @@ void init_filter(int port)
 | 
			
		||||
 | 
			
		||||
	 char filter_exp[1000];
 | 
			
		||||
 | 
			
		||||
	if(raw_mode==mode_faketcp)
 | 
			
		||||
	if(raw_ip_version==AF_INET)
 | 
			
		||||
	{
 | 
			
		||||
		sprintf(filter_exp,"tcp and src %s and src port %d and dst port %d",remote_ip,remote_port,port);
 | 
			
		||||
	}
 | 
			
		||||
	else if(raw_mode==mode_udp)
 | 
			
		||||
	{
 | 
			
		||||
		sprintf(filter_exp,"udp and src %s and src port %d and dst port %d",remote_ip,remote_port,port);
 | 
			
		||||
	}
 | 
			
		||||
	else if(raw_mode==mode_icmp)
 | 
			
		||||
	{
 | 
			
		||||
		sprintf(filter_exp,"icmp and src %s",remote_ip);
 | 
			
		||||
		if(raw_mode==mode_faketcp)
 | 
			
		||||
		{
 | 
			
		||||
			sprintf(filter_exp,"ip and tcp and src %s and src port %d and dst port %d",remote_addr.get_ip(),remote_addr.get_port(),port);
 | 
			
		||||
		}
 | 
			
		||||
		else if(raw_mode==mode_udp)
 | 
			
		||||
		{
 | 
			
		||||
			sprintf(filter_exp,"ip and udp and src %s and src port %d and dst port %d",remote_addr.get_ip(),remote_addr.get_port(),port);
 | 
			
		||||
		}
 | 
			
		||||
		else if(raw_mode==mode_icmp)
 | 
			
		||||
		{
 | 
			
		||||
			sprintf(filter_exp,"ip and icmp and src %s",remote_addr.get_ip());
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			mylog(log_fatal,"unknow raw mode\n");
 | 
			
		||||
			myexit(-1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_fatal,"unknow raw mode\n");
 | 
			
		||||
		myexit(-1);
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
		if(raw_mode==mode_faketcp)
 | 
			
		||||
		{
 | 
			
		||||
			sprintf(filter_exp,"ip6 and tcp and src %s and src port %d and dst port %d",remote_addr.get_ip(),remote_addr.get_port(),port);
 | 
			
		||||
		}
 | 
			
		||||
		else if(raw_mode==mode_udp)
 | 
			
		||||
		{
 | 
			
		||||
			sprintf(filter_exp,"ip6 and udp and src %s and src port %d and dst port %d",remote_addr.get_ip(),remote_addr.get_port(),port);
 | 
			
		||||
		}
 | 
			
		||||
		else if(raw_mode==mode_icmp)
 | 
			
		||||
		{
 | 
			
		||||
			sprintf(filter_exp,"ip6 and icmp6 and src %s",remote_addr.get_ip());
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			mylog(log_fatal,"unknow raw mode\n");
 | 
			
		||||
			myexit(-1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mylog(log_info,"filter expression is [%s]\n",filter_exp);
 | 
			
		||||
@@ -1263,10 +1297,12 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
 | 
			
		||||
		iph->daddr = send_info.new_dst_ip.v4;
 | 
			
		||||
 | 
			
		||||
		ip_tot_len=sizeof (struct my_iphdr)+payloadlen;
 | 
			
		||||
		/*
 | 
			
		||||
		if(lower_level)iph->tot_len = htons(ip_tot_len);            //this is not necessary ,kernel will always auto fill this  //http://man7.org/linux/man-pages/man7/raw.7.html
 | 
			
		||||
		else
 | 
			
		||||
			iph->tot_len = 0;
 | 
			
		||||
 | 
			
		||||
		*/
 | 
			
		||||
		iph->tot_len = htons(ip_tot_len);//always fill for mp version
 | 
			
		||||
		memcpy(send_raw_ip_buf+sizeof(my_iphdr) , payload, payloadlen);
 | 
			
		||||
 | 
			
		||||
		/*
 | 
			
		||||
@@ -1276,7 +1312,7 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
 | 
			
		||||
			iph->check=0;
 | 
			
		||||
			*/
 | 
			
		||||
 | 
			
		||||
		csum((unsigned short *) send_raw_ip_buf, iph->ihl*4);//always cal checksum for mp version
 | 
			
		||||
		iph->check =csum((unsigned short *) send_raw_ip_buf, iph->ihl*4);//always cal checksum for mp version
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
@@ -1450,6 +1486,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		//todo flow id
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
		recv_info.new_src_ip.v6=ip6h->src;
 | 
			
		||||
		recv_info.new_dst_ip.v6=ip6h->dst;
 | 
			
		||||
		iphdrlen=40;
 | 
			
		||||
@@ -1491,7 +1528,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
 | 
			
		||||
		//do nothing
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    payload=ip_begin+iphdrlen;
 | 
			
		||||
@@ -1572,6 +1609,7 @@ int peek_raw(raw_info_t &raw_info)
 | 
			
		||||
    			if(recv_info.protocol!=IPPROTO_ICMP) return -1;
 | 
			
		||||
    		}else
 | 
			
		||||
    		{
 | 
			
		||||
    			assert(raw_ip_version==AF_INET6);
 | 
			
		||||
    			if(recv_info.protocol!=IPPROTO_ICMPV6) return -1;
 | 
			
		||||
    		}
 | 
			
		||||
    		struct my_icmphdr *icmph=(my_icmphdr *)payload;
 | 
			
		||||
@@ -1605,6 +1643,7 @@ int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
		if(program_mode==client_mode)
 | 
			
		||||
		{
 | 
			
		||||
			icmph->type=128;
 | 
			
		||||
@@ -1627,6 +1666,8 @@ int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
 | 
			
		||||
		pseudo_header6 v6;
 | 
			
		||||
		struct pseudo_header6 *psh = &v6;
 | 
			
		||||
 | 
			
		||||
@@ -1697,6 +1738,7 @@ int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
		pseudo_header6 v6;
 | 
			
		||||
		struct pseudo_header6 *psh = &v6;
 | 
			
		||||
 | 
			
		||||
@@ -1837,6 +1879,8 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) {
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
 | 
			
		||||
		pseudo_header6 v6;
 | 
			
		||||
		struct pseudo_header6 *psh = &v6;
 | 
			
		||||
 | 
			
		||||
@@ -2054,6 +2098,7 @@ int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
		if(recv_info.protocol!=IPPROTO_ICMPV6)
 | 
			
		||||
		{
 | 
			
		||||
			//printf("not udp protocol\n");
 | 
			
		||||
@@ -2093,6 +2138,7 @@ int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		assert(raw_ip_version==AF_INET6);
 | 
			
		||||
		if(program_mode==client_mode)
 | 
			
		||||
		{
 | 
			
		||||
			if(icmph->type!=129)
 | 
			
		||||
@@ -2854,7 +2900,7 @@ int recv_raw(raw_info_t &raw_info,char *& payload,int & payloadlen)
 | 
			
		||||
	}
 | 
			
		||||
}*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port)  //a trick to get src adress for a dest adress,so that we can use the src address in raw socket as source ip
 | 
			
		||||
{
 | 
			
		||||
	struct sockaddr_in remote_addr_in={0};
 | 
			
		||||
@@ -2893,7 +2939,7 @@ int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port)  //a trick
 | 
			
		||||
    close(new_udp_fd);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
}*/
 | 
			
		||||
 | 
			
		||||
int get_src_adress2(address_t &output_addr,address_t remote_addr)
 | 
			
		||||
{
 | 
			
		||||
@@ -2914,7 +2960,7 @@ int get_src_adress2(address_t &output_addr,address_t remote_addr)
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
int try_to_list_and_bind(int &fd,u32_t local_ip_uint32,int port)  //try to bind to a port,may fail.
 | 
			
		||||
{
 | 
			
		||||
	 int old_bind_fd=fd;
 | 
			
		||||
@@ -2953,7 +2999,7 @@ int try_to_list_and_bind(int &fd,u32_t local_ip_uint32,int port)  //try to bind
 | 
			
		||||
		}
 | 
			
		||||
	 }
 | 
			
		||||
     return 0;
 | 
			
		||||
}
 | 
			
		||||
}*/
 | 
			
		||||
int try_to_list_and_bind2(int &fd,address_t address)  //try to bind to a port,may fail.
 | 
			
		||||
{
 | 
			
		||||
	if(fd!=-1)
 | 
			
		||||
@@ -2986,7 +3032,7 @@ int try_to_list_and_bind2(int &fd,address_t address)  //try to bind to a port,ma
 | 
			
		||||
		mylog(log_debug,"bind fail\n");
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
	if(raw_mode==mode_faketcp)
 | 
			
		||||
	if(raw_mode==mode_faketcp&&!use_tcp_dummy_socket)
 | 
			
		||||
	{
 | 
			
		||||
 | 
			
		||||
		if (listen(fd, SOMAXCONN) != 0) {
 | 
			
		||||
@@ -2996,7 +3042,7 @@ int try_to_list_and_bind2(int &fd,address_t address)  //try to bind to a port,ma
 | 
			
		||||
	}
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
int client_bind_to_a_new_port(int &fd,u32_t local_ip_uint32)//find a free port and bind to it.
 | 
			
		||||
{
 | 
			
		||||
	int raw_send_port=10000+get_true_random_number()%(65535-10000);
 | 
			
		||||
@@ -3011,7 +3057,7 @@ int client_bind_to_a_new_port(int &fd,u32_t local_ip_uint32)//find a free port a
 | 
			
		||||
	myexit(-1);
 | 
			
		||||
	return -1;////for compiler check
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
int client_bind_to_a_new_port2(int &fd,const address_t& address)//find a free port and bind to it.
 | 
			
		||||
{
 | 
			
		||||
	address_t tmp=address;
 | 
			
		||||
@@ -3019,7 +3065,7 @@ int client_bind_to_a_new_port2(int &fd,const address_t& address)//find a free po
 | 
			
		||||
	for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
 | 
			
		||||
	{
 | 
			
		||||
		tmp.set_port(raw_send_port);
 | 
			
		||||
		if (try_to_list_and_bind2(fd,address)==0)
 | 
			
		||||
		if (try_to_list_and_bind2(fd,tmp)==0)
 | 
			
		||||
		{
 | 
			
		||||
			return raw_send_port;
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
 | 
			
		||||
extern int raw_recv_fd;
 | 
			
		||||
extern int raw_send_fd;
 | 
			
		||||
extern int use_tcp_dummy_socket;
 | 
			
		||||
extern int seq_mode;
 | 
			
		||||
extern int max_seq_mode;
 | 
			
		||||
extern int filter_port;
 | 
			
		||||
@@ -44,7 +45,6 @@ extern int send_with_pcap;
 | 
			
		||||
extern int pcap_header_captured;
 | 
			
		||||
extern int pcap_header_buf[buf_len];
 | 
			
		||||
 | 
			
		||||
extern int use_tcp_dummy_socket;
 | 
			
		||||
struct icmphdr
 | 
			
		||||
{
 | 
			
		||||
	uint8_t type;
 | 
			
		||||
@@ -276,10 +276,15 @@ int init_ifindex(const char * if_name,int &index);
 | 
			
		||||
int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw);
 | 
			
		||||
 | 
			
		||||
int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port);  //a trick to get src adress for a dest adress,so that we can use the src address in raw socket as source ip
 | 
			
		||||
int get_src_adress2(address_t &output_addr,address_t remote_addr);
 | 
			
		||||
 | 
			
		||||
int try_to_list_and_bind(int & bind_fd,u32_t local_ip_uint32,int port);  //try to bind to a port,may fail.
 | 
			
		||||
int try_to_list_and_bind2(int &fd,address_t address);
 | 
			
		||||
 | 
			
		||||
int client_bind_to_a_new_port(int & bind_fd,u32_t local_ip_uint32);//find a free port and bind to it.
 | 
			
		||||
int client_bind_to_a_new_port2(int &fd,const address_t& address);
 | 
			
		||||
 | 
			
		||||
int discard_raw_packet();
 | 
			
		||||
 | 
			
		||||
int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user