mirror of
				https://github.com/wangyu-/udp2raw.git
				synced 2025-10-31 10:15:33 +08:00 
			
		
		
		
	use csum_with_header in recv_raw_udp
This commit is contained in:
		
							
								
								
									
										1
									
								
								misc.cpp
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								misc.cpp
									
									
									
									
									
								
							| @@ -74,6 +74,7 @@ int iptables_rule_keep_index=0; | |||||||
|  |  | ||||||
| program_mode_t program_mode=unset_mode;//0 unset; 1client 2server | program_mode_t program_mode=unset_mode;//0 unset; 1client 2server | ||||||
| raw_mode_t raw_mode=mode_faketcp; | raw_mode_t raw_mode=mode_faketcp; | ||||||
|  | ip_version_t ip_version=version_unset; | ||||||
| unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}}; | unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}}; | ||||||
|  |  | ||||||
| int about_to_exit=0; | int about_to_exit=0; | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								misc.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								misc.h
									
									
									
									
									
								
							| @@ -65,6 +65,7 @@ enum server_current_state_t {server_idle=0,server_handshake1,server_ready};  //s | |||||||
| enum client_current_state_t {client_idle=0,client_tcp_handshake,client_handshake1,client_handshake2,client_ready};//client state machine | enum client_current_state_t {client_idle=0,client_tcp_handshake,client_handshake1,client_handshake2,client_ready};//client state machine | ||||||
|  |  | ||||||
| enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end}; | enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end}; | ||||||
|  | enum ip_version_t{version_unset=0,version_ipv4,version_ipv6,version_end}; | ||||||
| enum program_mode_t {unset_mode=0,client_mode,server_mode}; | enum program_mode_t {unset_mode=0,client_mode,server_mode}; | ||||||
|  |  | ||||||
| union current_state_t | union current_state_t | ||||||
| @@ -110,6 +111,7 @@ extern char fifo_file[1000]; | |||||||
|  |  | ||||||
|  |  | ||||||
| extern raw_mode_t raw_mode; | extern raw_mode_t raw_mode; | ||||||
|  | extern ip_version_t ip_version; | ||||||
|  |  | ||||||
| extern program_mode_t program_mode; | extern program_mode_t program_mode; | ||||||
| extern unordered_map<int, const char*> raw_mode_tostring ; | extern unordered_map<int, const char*> raw_mode_tostring ; | ||||||
|   | |||||||
							
								
								
									
										30
									
								
								network.cpp
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								network.cpp
									
									
									
									
									
								
							| @@ -222,6 +222,9 @@ int init_raw_socket() | |||||||
|  |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |     int opt = 0; | ||||||
|  |     assert(setsockopt(raw_send_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt))==0);// raw_send_fd is for send only, set its recv buffer to zero | ||||||
|  |  | ||||||
| 	if(force_socket_buf) | 	if(force_socket_buf) | ||||||
| 	{ | 	{ | ||||||
| 		if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) | 		if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) | ||||||
| @@ -909,11 +912,11 @@ int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen) | |||||||
|  |  | ||||||
| 	char send_raw_udp_buf[buf_len]; | 	char send_raw_udp_buf[buf_len]; | ||||||
|  |  | ||||||
| 	udphdr *udph=(struct udphdr *) (send_raw_udp_buf | 	udphdr *udph=(struct udphdr *) (send_raw_udp_buf); | ||||||
| 			+ sizeof(struct pseudo_header)); |  | ||||||
|  |  | ||||||
| 	memset(udph,0,sizeof(udphdr)); | 	memset(udph,0,sizeof(udphdr)); | ||||||
| 	struct pseudo_header *psh = (struct pseudo_header *) (send_raw_udp_buf); | 	struct pseudo_header tmp_header={0}; | ||||||
|  | 	struct pseudo_header *psh = &tmp_header; | ||||||
|  |  | ||||||
| 	udph->source = htons(send_info.src_port); | 	udph->source = htons(send_info.src_port); | ||||||
| 	udph->dest = htons(send_info.dst_port); | 	udph->dest = htons(send_info.dst_port); | ||||||
| @@ -928,7 +931,7 @@ int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen) | |||||||
| 	mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len); | 	mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len); | ||||||
| 	udph->len=htons(uint16_t(udp_tot_len)); | 	udph->len=htons(uint16_t(udp_tot_len)); | ||||||
|  |  | ||||||
| 	memcpy(send_raw_udp_buf+sizeof(struct pseudo_header)+sizeof(udphdr),payload,payloadlen); | 	memcpy(send_raw_udp_buf+sizeof(udphdr),payload,payloadlen); | ||||||
|  |  | ||||||
| 	psh->source_address = send_info.src_ip; | 	psh->source_address = send_info.src_ip; | ||||||
| 	psh->dest_address = send_info.dst_ip; | 	psh->dest_address = send_info.dst_ip; | ||||||
| @@ -936,11 +939,11 @@ int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen) | |||||||
| 	psh->protocol = IPPROTO_UDP; | 	psh->protocol = IPPROTO_UDP; | ||||||
| 	psh->tcp_length = htons(uint16_t(udp_tot_len)); | 	psh->tcp_length = htons(uint16_t(udp_tot_len)); | ||||||
|  |  | ||||||
| 	int csum_size = sizeof(struct pseudo_header) +udp_tot_len  ; | 	int csum_size = udp_tot_len  ; | ||||||
|  |  | ||||||
| 	udph->check = csum( (unsigned short*) send_raw_udp_buf, csum_size); | 	udph->check = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) send_raw_udp_buf, csum_size); | ||||||
|  |  | ||||||
| 	if(send_raw_ip(raw_info,send_raw_udp_buf+ sizeof(struct pseudo_header),udp_tot_len)!=0) | 	if(send_raw_ip(raw_info,send_raw_udp_buf,udp_tot_len)!=0) | ||||||
| 	{ | 	{ | ||||||
| 		return -1; | 		return -1; | ||||||
| 	} | 	} | ||||||
| @@ -1316,7 +1319,7 @@ int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen) | |||||||
| { | { | ||||||
| 	const packet_info_t &send_info=raw_info.send_info; | 	const packet_info_t &send_info=raw_info.send_info; | ||||||
| 	packet_info_t &recv_info=raw_info.recv_info; | 	packet_info_t &recv_info=raw_info.recv_info; | ||||||
| 	static char recv_raw_udp_buf[buf_len]; | 	//static char recv_raw_udp_buf[buf_len]; | ||||||
| 	char * ip_payload; | 	char * ip_payload; | ||||||
| 	int ip_payloadlen; | 	int ip_payloadlen; | ||||||
|  |  | ||||||
| @@ -1350,9 +1353,10 @@ int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen) | |||||||
|     	return -1; |     	return -1; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen); |     //memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen); | ||||||
|  |  | ||||||
|     struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ; |     pseudo_header tmp_header={0}; | ||||||
|  |     struct pseudo_header *psh=&tmp_header ; | ||||||
|  |  | ||||||
|     psh->source_address = recv_info.src_ip; |     psh->source_address = recv_info.src_ip; | ||||||
|     psh->dest_address = recv_info.dst_ip; |     psh->dest_address = recv_info.dst_ip; | ||||||
| @@ -1360,8 +1364,8 @@ int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen) | |||||||
|     psh->protocol = IPPROTO_UDP; |     psh->protocol = IPPROTO_UDP; | ||||||
|     psh->tcp_length = htons(ip_payloadlen); |     psh->tcp_length = htons(ip_payloadlen); | ||||||
|  |  | ||||||
|     int csum_len=sizeof(struct pseudo_header)+ip_payloadlen; |     int csum_len=ip_payloadlen; | ||||||
|     uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len); |     uint16_t udp_chk = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) ip_payload, csum_len); | ||||||
|  |  | ||||||
|     if(udp_chk!=0) |     if(udp_chk!=0) | ||||||
|     { |     { | ||||||
| @@ -1371,7 +1375,7 @@ int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen) | |||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header); |     char *udp_begin=ip_payload; | ||||||
|  |  | ||||||
|     recv_info.src_port=ntohs(udph->source); |     recv_info.src_port=ntohs(udph->source); | ||||||
|     recv_info.dst_port=ntohs(udph->dest); |     recv_info.dst_port=ntohs(udph->dest); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user