mirror of
				https://github.com/wangyu-/udp2raw.git
				synced 2025-10-31 10:15:33 +08:00 
			
		
		
		
	split to multi files
This commit is contained in:
		
							
								
								
									
										200
									
								
								common.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										200
									
								
								common.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,200 @@ | |||||||
|  | /* | ||||||
|  |  * comm.cpp | ||||||
|  |  * | ||||||
|  |  *  Created on: Jul 29, 2017 | ||||||
|  |  *      Author: wangyu | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "common.h" | ||||||
|  | #include "log.h" | ||||||
|  |  | ||||||
|  |  | ||||||
|  | raw_mode_t raw_mode=mode_faketcp; | ||||||
|  | map<int, string> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}}; | ||||||
|  | int socket_buf_size=1024*1024; | ||||||
|  | static int random_number_fd=-1; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | uint64_t get_current_time() | ||||||
|  | { | ||||||
|  | 	timespec tmp_time; | ||||||
|  | 	clock_gettime(CLOCK_MONOTONIC, &tmp_time); | ||||||
|  | 	return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000l); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | uint64_t pack_u64(uint32_t a,uint32_t b) | ||||||
|  | { | ||||||
|  | 	uint64_t ret=a; | ||||||
|  | 	ret<<=32u; | ||||||
|  | 	ret+=b; | ||||||
|  | 	return ret; | ||||||
|  | } | ||||||
|  | uint32_t get_u64_h(uint64_t a) | ||||||
|  | { | ||||||
|  | 	return a>>32u; | ||||||
|  | } | ||||||
|  | uint32_t get_u64_l(uint64_t a) | ||||||
|  | { | ||||||
|  | 	return (a<<32u)>>32u; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | char * my_ntoa(uint32_t ip) | ||||||
|  | { | ||||||
|  | 	in_addr a; | ||||||
|  | 	a.s_addr=ip; | ||||||
|  | 	return inet_ntoa(a); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void myexit(int a) | ||||||
|  | { | ||||||
|  |     if(enable_log_color) | ||||||
|  |     	 printf(RESET); | ||||||
|  | 	exit(a); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void init_random_number_fd() | ||||||
|  | { | ||||||
|  | 	random_number_fd=open("/dev/urandom",O_RDONLY); | ||||||
|  | 	if(random_number_fd==-1) | ||||||
|  | 	{ | ||||||
|  | 		mylog(log_fatal,"error open /dev/urandom\n"); | ||||||
|  | 		myexit(-1); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | uint64_t get_true_random_number_64() | ||||||
|  | { | ||||||
|  | 	uint64_t ret; | ||||||
|  | 	read(random_number_fd,&ret,sizeof(ret)); | ||||||
|  | 	return ret; | ||||||
|  | } | ||||||
|  | uint32_t get_true_random_number_0() | ||||||
|  | { | ||||||
|  | 	uint32_t ret; | ||||||
|  | 	read(random_number_fd,&ret,sizeof(ret)); | ||||||
|  | 	return ret; | ||||||
|  | } | ||||||
|  | uint32_t get_true_random_number_nz() //nz for non-zero | ||||||
|  | { | ||||||
|  | 	uint32_t ret=0; | ||||||
|  | 	while(ret==0) | ||||||
|  | 	{ | ||||||
|  | 		ret=get_true_random_number_0(); | ||||||
|  | 	} | ||||||
|  | 	return ret; | ||||||
|  | } | ||||||
|  | uint64_t ntoh64(uint64_t a) | ||||||
|  | { | ||||||
|  | 	if(__BYTE_ORDER == __LITTLE_ENDIAN) | ||||||
|  | 	{ | ||||||
|  | 		return __bswap_64( a); | ||||||
|  | 	} | ||||||
|  | 	else return a; | ||||||
|  |  | ||||||
|  | } | ||||||
|  | uint64_t hton64(uint64_t a) | ||||||
|  | { | ||||||
|  | 	if(__BYTE_ORDER == __LITTLE_ENDIAN) | ||||||
|  | 	{ | ||||||
|  | 		return __bswap_64( a); | ||||||
|  | 	} | ||||||
|  | 	else return a; | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void setnonblocking(int sock) { | ||||||
|  | 	int opts; | ||||||
|  | 	opts = fcntl(sock, F_GETFL); | ||||||
|  |  | ||||||
|  | 	if (opts < 0) { | ||||||
|  |     	mylog(log_fatal,"fcntl(sock,GETFL)\n"); | ||||||
|  | 		//perror("fcntl(sock,GETFL)"); | ||||||
|  | 		myexit(1); | ||||||
|  | 	} | ||||||
|  | 	opts = opts | O_NONBLOCK; | ||||||
|  | 	if (fcntl(sock, F_SETFL, opts) < 0) { | ||||||
|  |     	mylog(log_fatal,"fcntl(sock,SETFL,opts)\n"); | ||||||
|  | 		//perror("fcntl(sock,SETFL,opts)"); | ||||||
|  | 		myexit(1); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |     Generic checksum calculation function | ||||||
|  | */ | ||||||
|  | unsigned short csum(const unsigned short *ptr,int nbytes) { | ||||||
|  |     register long sum; | ||||||
|  |     unsigned short oddbyte; | ||||||
|  |     register short answer; | ||||||
|  |  | ||||||
|  |     sum=0; | ||||||
|  |     while(nbytes>1) { | ||||||
|  |         sum+=*ptr++; | ||||||
|  |         nbytes-=2; | ||||||
|  |     } | ||||||
|  |     if(nbytes==1) { | ||||||
|  |         oddbyte=0; | ||||||
|  |         *((u_char*)&oddbyte)=*(u_char*)ptr; | ||||||
|  |         sum+=oddbyte; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     sum = (sum>>16)+(sum & 0xffff); | ||||||
|  |     sum = sum + (sum>>16); | ||||||
|  |     answer=(short)~sum; | ||||||
|  |  | ||||||
|  |     return(answer); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int set_buf_size(int fd) | ||||||
|  | { | ||||||
|  |     if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) | ||||||
|  |     { | ||||||
|  |     	mylog(log_fatal,"SO_SNDBUFFORCE fail\n"); | ||||||
|  |     	myexit(1); | ||||||
|  |     } | ||||||
|  |     if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0) | ||||||
|  |     { | ||||||
|  |     	mylog(log_fatal,"SO_RCVBUFFORCE fail\n"); | ||||||
|  |     	myexit(1); | ||||||
|  |     } | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void  INThandler(int sig) | ||||||
|  | { | ||||||
|  |      if(enable_log_color) | ||||||
|  |     	 printf(RESET); | ||||||
|  |      myexit(0); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len) | ||||||
|  | { | ||||||
|  | 	static char buf[buf_len]; | ||||||
|  | 	data=buf; | ||||||
|  | 	id_t tmp=htonl(id1); | ||||||
|  | 	memcpy(buf,&tmp,sizeof(tmp)); | ||||||
|  |  | ||||||
|  | 	tmp=htonl(id2); | ||||||
|  | 	memcpy(buf+sizeof(tmp),&tmp,sizeof(tmp)); | ||||||
|  |  | ||||||
|  | 	tmp=htonl(id3); | ||||||
|  | 	memcpy(buf+sizeof(tmp)*2,&tmp,sizeof(tmp)); | ||||||
|  |  | ||||||
|  | 	len=sizeof(id_t)*3; | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3) | ||||||
|  | { | ||||||
|  | 	if(len<sizeof(id_t)*3) return -1; | ||||||
|  | 	id1=ntohl(  *((id_t*)(data+0)) ); | ||||||
|  | 	id2=ntohl(  *((id_t*)(data+sizeof(id_t))) ); | ||||||
|  | 	id3=ntohl(  *((id_t*)(data+sizeof(id_t)*2)) ); | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | program_mode_t program_mode=unset_mode;//0 unset; 1client 2server | ||||||
							
								
								
									
										137
									
								
								common.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								common.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,137 @@ | |||||||
|  | /* | ||||||
|  |  * common.h | ||||||
|  |  * | ||||||
|  |  *  Created on: Jul 29, 2017 | ||||||
|  |  *      Author: wangyu | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #ifndef COMMON_H_ | ||||||
|  | #define COMMON_H_ | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #include<stdio.h> | ||||||
|  | #include<string.h> | ||||||
|  | #include<sys/socket.h> | ||||||
|  | #include<arpa/inet.h> | ||||||
|  | #include<stdlib.h> | ||||||
|  | #include<getopt.h> | ||||||
|  | #include <unistd.h> | ||||||
|  | #include<errno.h> | ||||||
|  |  | ||||||
|  | #include <fcntl.h> | ||||||
|  |  | ||||||
|  | #include <sys/epoll.h> | ||||||
|  | #include <sys/wait.h> | ||||||
|  |  | ||||||
|  | #include<map> | ||||||
|  | #include<string> | ||||||
|  | #include<vector> | ||||||
|  |  | ||||||
|  | #include <sys/socket.h>    //for socket ofcourse | ||||||
|  | #include <sys/types.h> | ||||||
|  | #include <stdlib.h> //for exit(0); | ||||||
|  | #include <errno.h> //For errno - the error number | ||||||
|  | #include <netinet/tcp.h>   //Provides declarations for tcp header | ||||||
|  | #include <netinet/udp.h> | ||||||
|  | #include <netinet/ip.h>    //Provides declarations for ip header | ||||||
|  | #include <netinet/if_ether.h> | ||||||
|  | #include <arpa/inet.h> | ||||||
|  | #include <fcntl.h> | ||||||
|  | #include <byteswap.h> | ||||||
|  | #include <sys/socket.h> | ||||||
|  | #include <sys/types.h> | ||||||
|  | #include <arpa/inet.h> | ||||||
|  | #include <linux/if_ether.h> | ||||||
|  | #include <linux/filter.h> | ||||||
|  |  | ||||||
|  | #include <sys/time.h> | ||||||
|  | #include <time.h> | ||||||
|  |  | ||||||
|  | #include <sys/timerfd.h> | ||||||
|  | #include <set> | ||||||
|  | #include <encrypt.h> | ||||||
|  | #include <inttypes.h> | ||||||
|  |  | ||||||
|  | #include <sys/ioctl.h> | ||||||
|  | #include <netinet/in.h> | ||||||
|  | #include <net/if.h> | ||||||
|  | #include <arpa/inet.h> | ||||||
|  | #include <stdarg.h> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #include <unordered_map> | ||||||
|  |  | ||||||
|  | using  namespace std; | ||||||
|  |  | ||||||
|  | const int max_data_len=65535; | ||||||
|  | const int buf_len=max_data_len+200; | ||||||
|  |  | ||||||
|  | const int handshake_timeout=2000; | ||||||
|  | const int server_handshake_timeout=10000; | ||||||
|  |  | ||||||
|  | const int heartbeat_timeout=10000; | ||||||
|  | const int udp_timeout=3000; | ||||||
|  |  | ||||||
|  | const int heartbeat_interval=1000; | ||||||
|  |  | ||||||
|  | const int timer_interval=500; | ||||||
|  |  | ||||||
|  | const int RETRY_TIME=3; | ||||||
|  |  | ||||||
|  | const int anti_replay_window_size=1000; | ||||||
|  |  | ||||||
|  | const int max_conv_num=10000; | ||||||
|  | const int conv_timeout=120000; //60 second | ||||||
|  | const int conv_clear_ratio=10; | ||||||
|  |  | ||||||
|  | const int max_handshake_conn_num=10000; | ||||||
|  | const int max_ready_conn_num=1000; | ||||||
|  |  | ||||||
|  | const int conn_timeout=60000; | ||||||
|  | const int conn_clear_ratio=10; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | enum raw_mode_t{mode_faketcp=1,mode_udp,mode_icmp,mode_end}; | ||||||
|  | extern raw_mode_t raw_mode; | ||||||
|  | enum program_mode_t {unset_mode=0,client_mode,server_mode}; | ||||||
|  | extern program_mode_t program_mode; | ||||||
|  | extern map<int, string> raw_mode_tostring ; | ||||||
|  | extern int socket_buf_size; | ||||||
|  |  | ||||||
|  | typedef uint32_t id_t; | ||||||
|  |  | ||||||
|  | typedef uint64_t iv_t; | ||||||
|  |  | ||||||
|  | typedef uint64_t anti_replay_seq_t; | ||||||
|  |  | ||||||
|  | uint64_t get_current_time(); | ||||||
|  | uint64_t pack_u64(uint32_t a,uint32_t b); | ||||||
|  |  | ||||||
|  | uint32_t get_u64_h(uint64_t a); | ||||||
|  |  | ||||||
|  | uint32_t get_u64_l(uint64_t a); | ||||||
|  |  | ||||||
|  | char * my_ntoa(uint32_t ip); | ||||||
|  |  | ||||||
|  | void myexit(int a); | ||||||
|  | void init_random_number_fd(); | ||||||
|  | uint64_t get_true_random_number_64(); | ||||||
|  | uint32_t get_true_random_number_0(); | ||||||
|  | uint32_t get_true_random_number_nz(); | ||||||
|  | uint64_t ntoh64(uint64_t a); | ||||||
|  | uint64_t hton64(uint64_t a); | ||||||
|  |  | ||||||
|  | void setnonblocking(int sock); | ||||||
|  | int set_buf_size(int fd); | ||||||
|  |  | ||||||
|  | unsigned short csum(const unsigned short *ptr,int nbytes); | ||||||
|  |  | ||||||
|  | void  INThandler(int sig); | ||||||
|  | int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len); | ||||||
|  | int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif /* COMMON_H_ */ | ||||||
							
								
								
									
										14
									
								
								encrypt.cpp
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								encrypt.cpp
									
									
									
									
									
								
							| @@ -12,8 +12,6 @@ | |||||||
|  |  | ||||||
| static int8_t zero_iv[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   0,0,0,0};//this prog use zero iv,you should make sure first block of data contains a random/nonce data | static int8_t zero_iv[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   0,0,0,0};//this prog use zero iv,you should make sure first block of data contains a random/nonce data | ||||||
|  |  | ||||||
| extern const int max_data_len; |  | ||||||
| extern const int buf_len;//=65535; |  | ||||||
|  |  | ||||||
| map<int, string> auth_mode_tostring = {{auth_none, "none"}, {auth_md5, "md5"}, {auth_crc32, "crc32"},{auth_sum,"sum"}}; | map<int, string> auth_mode_tostring = {{auth_none, "none"}, {auth_md5, "md5"}, {auth_crc32, "crc32"},{auth_sum,"sum"}}; | ||||||
| map<int, string> cipher_mode_tostring={{cipher_none,"none"},{cipher_aes128cbc,"aes128cbc"},{cipher_xor,"xor"}}; | map<int, string> cipher_mode_tostring={{cipher_none,"none"},{cipher_aes128cbc,"aes128cbc"},{cipher_xor,"xor"}}; | ||||||
| @@ -209,8 +207,9 @@ int auth_cal(const char *data,char * output,int &len) | |||||||
| 	case auth_md5:return auth_md5_cal(data, output, len); | 	case auth_md5:return auth_md5_cal(data, output, len); | ||||||
| 	case auth_sum:return auth_sum_cal(data, output, len); | 	case auth_sum:return auth_sum_cal(data, output, len); | ||||||
| 	case auth_none:return auth_none_cal(data, output, len); | 	case auth_none:return auth_none_cal(data, output, len); | ||||||
|  | 	default:	return auth_md5_cal(data,output,len);//default | ||||||
| 	} | 	} | ||||||
| 	return auth_md5_cal(data,output,len);//default |  | ||||||
| } | } | ||||||
| int auth_verify(const char *data,int &len) | int auth_verify(const char *data,int &len) | ||||||
| { | { | ||||||
| @@ -221,8 +220,9 @@ int auth_verify(const char *data,int &len) | |||||||
| 	case auth_md5:return auth_md5_verify(data, len); | 	case auth_md5:return auth_md5_verify(data, len); | ||||||
| 	case auth_sum:return auth_sum_verify(data, len); | 	case auth_sum:return auth_sum_verify(data, len); | ||||||
| 	case auth_none:return auth_none_verify(data, len); | 	case auth_none:return auth_none_verify(data, len); | ||||||
|  | 	default:	return auth_md5_verify(data,len);//default | ||||||
| 	} | 	} | ||||||
| 	return auth_md5_verify(data,len);//default |  | ||||||
| } | } | ||||||
| int cipher_encrypt(const char *data,char *output,int &len,char * key) | int cipher_encrypt(const char *data,char *output,int &len,char * key) | ||||||
| { | { | ||||||
| @@ -232,8 +232,9 @@ int cipher_encrypt(const char *data,char *output,int &len,char * key) | |||||||
| 	case cipher_aes128cbc:return cipher_aes128cbc_encrypt(data,output,len, key); | 	case cipher_aes128cbc:return cipher_aes128cbc_encrypt(data,output,len, key); | ||||||
| 	case cipher_xor:return cipher_xor_encrypt(data,output,len, key); | 	case cipher_xor:return cipher_xor_encrypt(data,output,len, key); | ||||||
| 	case cipher_none:return cipher_none_encrypt(data,output,len, key); | 	case cipher_none:return cipher_none_encrypt(data,output,len, key); | ||||||
|  | 	default:return cipher_aes128cbc_encrypt(data,output,len, key); | ||||||
| 	} | 	} | ||||||
| 	return cipher_aes128cbc_encrypt(data,output,len, key); |  | ||||||
| } | } | ||||||
| int cipher_decrypt(const char *data,char *output,int &len,char * key) | int cipher_decrypt(const char *data,char *output,int &len,char * key) | ||||||
| { | { | ||||||
| @@ -243,8 +244,9 @@ int cipher_decrypt(const char *data,char *output,int &len,char * key) | |||||||
| 		case cipher_aes128cbc:return cipher_aes128cbc_decrypt(data,output,len, key); | 		case cipher_aes128cbc:return cipher_aes128cbc_decrypt(data,output,len, key); | ||||||
| 		case cipher_xor:return cipher_xor_decrypt(data,output,len, key); | 		case cipher_xor:return cipher_xor_decrypt(data,output,len, key); | ||||||
| 		case cipher_none:return cipher_none_decrypt(data,output,len, key); | 		case cipher_none:return cipher_none_decrypt(data,output,len, key); | ||||||
|  | 		default:	return cipher_aes128cbc_decrypt(data,output,len,key); | ||||||
| 	} | 	} | ||||||
| 	return cipher_aes128cbc_decrypt(data,output,len,key); |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								encrypt.h
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								encrypt.h
									
									
									
									
									
								
							| @@ -1,13 +1,10 @@ | |||||||
| #ifndef _ENCRYPTION_H_ | #ifndef _ENCRYPTION_H_ | ||||||
| #define _ENCRYPTION_H_ | #define _ENCRYPTION_H_ | ||||||
| #include <aes.h> | #include "aes.h" | ||||||
| #include <md5.h> | #include "md5.h" | ||||||
| #include <string.h> |  | ||||||
| #include <stdint.h> | #include "common.h" | ||||||
| #include <stdlib.h> |  | ||||||
| #include <stdio.h> |  | ||||||
| #include <map> |  | ||||||
| #include <string> |  | ||||||
| using namespace std; | using namespace std; | ||||||
| int my_encrypt(const char *data,char *output,int &len,char * key); | int my_encrypt(const char *data,char *output,int &len,char * key); | ||||||
| int my_decrypt(const char *data,char *output,int &len,char * key); | int my_decrypt(const char *data,char *output,int &len,char * key); | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								log.cpp
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								log.cpp
									
									
									
									
									
								
							| @@ -23,7 +23,7 @@ void log0(const char * file,const char * function,int line,int level,const char* | |||||||
| 		printf(log_color[level]); | 		printf(log_color[level]); | ||||||
|  |  | ||||||
| 	strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info); | 	strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info); | ||||||
| 	printf("[%s][%s]",buffer,log_text[level],file,line); | 	printf("[%s][%s]",buffer,log_text[level]); | ||||||
|  |  | ||||||
| 	if(enable_log_position)printf("[%s,func:%s,line:%d]",file,function,line); | 	if(enable_log_position)printf("[%s,func:%s,line:%d]",file,function,line); | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								makefile
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								makefile
									
									
									
									
									
								
							| @@ -1,8 +1,9 @@ | |||||||
| ccmips=mips-openwrt-linux-g++ | ccmips=mips-openwrt-linux-g++ | ||||||
|  | FLAGS=-Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare  | ||||||
| all: | all: | ||||||
| 	killall udp2raw||true | 	sudo killall udp2raw||true | ||||||
| 	sleep 1 | 	sleep 0.2 | ||||||
| 	g++ main.cpp -o udp2raw -static -lrt -ggdb -I. aes.c md5.c encrypt.cpp log.cpp  -std=c++11  -O3 | 	g++ main.cpp -o udp2raw -static  -ggdb -I. aes.c md5.c encrypt.cpp log.cpp network.cpp common.cpp -lrt -std=c++11  -O3  ${FLAGS} | ||||||
| 	${ccmips} main.cpp -o udp2raw_mips  -lrt   -I. aes.c md5.c encrypt.cpp log.cpp -std=c++11 -O3 | #${ccmips} main.cpp -o udp2raw_mips  -lrt   -I. aes.c md5.c encrypt.cpp log.cpp -std=c++11 -O3 | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										1213
									
								
								network.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1213
									
								
								network.cpp
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										117
									
								
								network.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								network.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,117 @@ | |||||||
|  | /* | ||||||
|  |  * network.h | ||||||
|  |  * | ||||||
|  |  *  Created on: Jul 29, 2017 | ||||||
|  |  *      Author: wangyu | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #ifndef NETWORK_H_ | ||||||
|  | #define NETWORK_H_ | ||||||
|  |  | ||||||
|  | extern int raw_recv_fd; | ||||||
|  | extern int raw_send_fd; | ||||||
|  | extern int seq_mode; | ||||||
|  | extern int filter_port; | ||||||
|  | extern uint32_t bind_address_uint32; | ||||||
|  | extern int disable_bpf_filter; | ||||||
|  |  | ||||||
|  | struct icmphdr | ||||||
|  | { | ||||||
|  | 	uint8_t type; | ||||||
|  | 	uint8_t code; | ||||||
|  | 	uint16_t check_sum; | ||||||
|  | 	uint16_t id; | ||||||
|  | 	uint16_t seq; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | struct pseudo_header { | ||||||
|  |     u_int32_t source_address; | ||||||
|  |     u_int32_t dest_address; | ||||||
|  |     u_int8_t placeholder; | ||||||
|  |     u_int8_t protocol; | ||||||
|  |     u_int16_t tcp_length; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | struct packet_info_t | ||||||
|  | { | ||||||
|  | 	uint8_t protocol; | ||||||
|  | 	//ip_part: | ||||||
|  | 	uint32_t src_ip; | ||||||
|  | 	uint16_t src_port; | ||||||
|  |  | ||||||
|  | 	uint32_t dst_ip; | ||||||
|  | 	uint16_t dst_port; | ||||||
|  |  | ||||||
|  | 	//tcp_part: | ||||||
|  | 	bool syn,ack,psh,rst; | ||||||
|  |  | ||||||
|  | 	uint32_t seq,ack_seq; | ||||||
|  |  | ||||||
|  | 	uint32_t ts,ts_ack; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	uint16_t icmp_seq; | ||||||
|  |  | ||||||
|  | 	bool has_ts; | ||||||
|  | 	packet_info_t() | ||||||
|  | 	{ | ||||||
|  | 		if(raw_mode==mode_faketcp) | ||||||
|  | 		{ | ||||||
|  | 			protocol=IPPROTO_TCP; | ||||||
|  | 		} | ||||||
|  | 		else if(raw_mode==mode_udp) | ||||||
|  | 		{ | ||||||
|  | 			protocol=IPPROTO_UDP; | ||||||
|  | 		} | ||||||
|  | 		else if(raw_mode==mode_icmp) | ||||||
|  | 		{ | ||||||
|  | 			protocol=IPPROTO_ICMP; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | struct raw_info_t | ||||||
|  | { | ||||||
|  | //	int peek; | ||||||
|  | 	packet_info_t send_info; | ||||||
|  | 	packet_info_t recv_info; | ||||||
|  | 	raw_info_t() | ||||||
|  | 	{ | ||||||
|  | 	//	peek=0; | ||||||
|  | 	} | ||||||
|  | };//g_raw_info; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | int init_raw_socket(); | ||||||
|  |  | ||||||
|  | void init_filter(int port); | ||||||
|  |  | ||||||
|  | void remove_filter(); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen); | ||||||
|  |  | ||||||
|  | int peek_raw(uint32_t &ip,uint16_t &port); | ||||||
|  |  | ||||||
|  | int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen); | ||||||
|  |  | ||||||
|  | int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen); | ||||||
|  |  | ||||||
|  | int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen); | ||||||
|  |  | ||||||
|  | int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen); | ||||||
|  |  | ||||||
|  | int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen); | ||||||
|  |  | ||||||
|  | int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen); | ||||||
|  |  | ||||||
|  | int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen); | ||||||
|  |  | ||||||
|  | int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen); | ||||||
|  |  | ||||||
|  | int recv_raw(raw_info_t &raw_info,char * &payload,int &payloadlen); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif /* NETWORK_H_ */ | ||||||
		Reference in New Issue
	
	Block a user