This commit is contained in:
wangyu 2017-08-13 08:18:23 +08:00
parent 2af94823e3
commit 32166d65ae
3 changed files with 35 additions and 17 deletions

View File

@ -1417,10 +1417,13 @@ int server_on_raw_recv_multi()
{ {
recv(raw_recv_fd, 0,0, 0 );// recv(raw_recv_fd, 0,0, 0 );//
//struct sockaddr saddr; //struct sockaddr saddr;
//socklen_t saddr_size; //socklen_t saddr_size=sizeof(saddr);
///recvfrom(raw_recv_fd, 0,0, 0 ,&saddr , &saddr_size);// ///recvfrom(raw_recv_fd, 0,0, 0 ,&saddr , &saddr_size);//
mylog(log_trace,"peek_raw failed\n"); mylog(log_trace,"peek_raw failed\n");
return -1; return -1;
}else
{
mylog(log_trace,"peek_raw success\n");
} }
u32_t ip=peek_info.src_ip;uint16_t port=peek_info.src_port; u32_t ip=peek_info.src_ip;uint16_t port=peek_info.src_port;
@ -1544,9 +1547,11 @@ int server_on_raw_recv_multi()
if(conn_info.state.server_current_state==server_ready) if(conn_info.state.server_current_state==server_ready)
{ {
char type; char type;
//mylog(log_info,"before recv_safer\n");
if (recv_safer(conn_info,type, data, data_len) != 0) { if (recv_safer(conn_info,type, data, data_len) != 0) {
return -1; return -1;
} }
//mylog(log_info,"after recv_safer\n");
return server_on_raw_recv_ready(conn_info,ip_port,type,data,data_len); return server_on_raw_recv_ready(conn_info,ip_port,type,data,data_len);
} }
return 0; return 0;
@ -2034,8 +2039,9 @@ int client_event_loop()
int recv_len; int recv_len;
struct sockaddr_in udp_new_addr_in; struct sockaddr_in udp_new_addr_in;
socklen_t udp_new_addr_len = sizeof(sockaddr_in);
if ((recv_len = recvfrom(udp_fd, buf, max_data_len, 0, if ((recv_len = recvfrom(udp_fd, buf, max_data_len, 0,
(struct sockaddr *) &udp_new_addr_in, &slen)) == -1) { (struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
mylog(log_error,"recv_from error,this shouldnt happen at client\n"); mylog(log_error,"recv_from error,this shouldnt happen at client\n");
myexit(1); myexit(1);
}; };
@ -2313,7 +2319,8 @@ int server_event_loop()
if(recv_len<0) if(recv_len<0)
{ {
mylog(log_debug,"udp fd,recv_len<0 continue\n"); mylog(log_debug,"udp fd,recv_len<0 continue,%s\n",strerror(errno));
continue; continue;
} }

View File

@ -20,6 +20,10 @@ ar71xx:
${cc_ar71xx} -o udp2raw_ar71xx -I. ${SOURCES} ${FLAGS} -lrt -lgcc_eh -static -O3 ${cc_ar71xx} -o udp2raw_ar71xx -I. ${SOURCES} ${FLAGS} -lrt -lgcc_eh -static -O3
bcm2708: bcm2708:
${cc_bcm2708} -o udp2raw_bcm2708 -I. ${SOURCES} ${FLAGS} -lrt -static -O3 ${cc_bcm2708} -o udp2raw_bcm2708 -I. ${SOURCES} ${FLAGS} -lrt -static -O3
amd64:
${cc_local} -o udp2raw_amd64 -I. ${SOURCES} ${FLAGS} -lrt -static -O3
x86:
${cc_local} -o udp2raw_x86 -I. ${SOURCES} ${FLAGS} -lrt -m32 -static -O3
cross: cross:
${cc_cross} -o udp2raw_cross -I. ${SOURCES} ${FLAGS} -lrt -static -lgcc_eh -O3 ${cc_cross} -o udp2raw_cross -I. ${SOURCES} ${FLAGS} -lrt -static -lgcc_eh -O3
@ -27,9 +31,7 @@ cross:
cross2: cross2:
${cc_cross} -o udp2raw_cross -I. ${SOURCES} ${FLAGS} -lrt -O3 ${cc_cross} -o udp2raw_cross -I. ${SOURCES} ${FLAGS} -lrt -O3
release: ar71xx bcm2708 release: amd64 x86 ar71xx bcm2708
${cc_local} -o udp2raw_amd64 -I. ${SOURCES} ${FLAGS} -lrt -static -O3
${cc_local} -o udp2raw_x86 -I. ${SOURCES} ${FLAGS} -lrt -m32 -static -O3
tar -zcvf ${TAR} tar -zcvf ${TAR}
clean: clean:

View File

@ -24,6 +24,8 @@ int lower_level=0;
int ifindex=-1; int ifindex=-1;
char if_name[100]=""; char if_name[100]="";
unsigned short g_ip_id_counter=0;
unsigned char oppsite_hw_addr[6]= unsigned char oppsite_hw_addr[6]=
{0xff,0xff,0xff,0xff,0xff,0xff}; {0xff,0xff,0xff,0xff,0xff,0xff};
//{0x00,0x23,0x45,0x67,0x89,0xb9}; //{0x00,0x23,0x45,0x67,0x89,0xb9};
@ -162,6 +164,7 @@ packet_info_t::packet_info_t()
int init_raw_socket() int init_raw_socket()
{ {
g_ip_id_counter=get_true_random_number()%65535;
if(lower_level==0) if(lower_level==0)
{ {
raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP); raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
@ -306,7 +309,6 @@ int init_ifindex(char * if_name)
return 0; return 0;
} }
int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen) int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
{ {
const packet_info_t &send_info=raw_info.send_info; const packet_info_t &send_info=raw_info.send_info;
@ -316,19 +318,18 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
struct iphdr *iph = (struct iphdr *) send_raw_ip_buf; struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
memset(iph,0,sizeof(iphdr)); memset(iph,0,sizeof(iphdr));
static unsigned short ip_id=1;
iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr) iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
iph->version = 4; iph->version = 4;
iph->tos = 0; iph->tos = 0;
if(lower_level) if(lower_level)
{ {
iph->id=0; //iph->id=0;
//iph->id = htons (ip_id++); //Id of this packet iph->id = htons (g_ip_id_counter++); //Id of this packet
} }
else else
iph->id = 0; //Id of this packet ,kernel will auto fill this if id is zero ,or really?????// todo //seems like there is a problem iph->id = htons (g_ip_id_counter++); //Id of this packet
//iph->id = 0; //Id of this packet ,kernel will auto fill this if id is zero ,or really?????// todo //seems like there is a problem
iph->frag_off = htons(0x4000); //DF set,others are zero iph->frag_off = htons(0x4000); //DF set,others are zero
// iph->frag_off = htons(0x0000); //DF set,others are zero // iph->frag_off = htons(0x0000); //DF set,others are zero
@ -390,12 +391,14 @@ int peek_raw(packet_info_t &peek_info)
{ static char peek_raw_buf[buf_len]; { static char peek_raw_buf[buf_len];
char *ip_begin=peek_raw_buf+link_level_header_len; char *ip_begin=peek_raw_buf+link_level_header_len;
struct sockaddr saddr; struct sockaddr saddr;
socklen_t saddr_size; socklen_t saddr_size=sizeof(saddr);
int recv_len = recvfrom(raw_recv_fd, peek_raw_buf,max_data_len, MSG_PEEK ,&saddr , &saddr_size);//change max_data_len to something smaller,we only need header here int recv_len = recvfrom(raw_recv_fd, peek_raw_buf,max_data_len, MSG_PEEK ,&saddr , &saddr_size);//change max_data_len to something smaller,we only need header here
iphdr * iph = (struct iphdr *) (ip_begin); iphdr * iph = (struct iphdr *) (ip_begin);
//mylog(log_info,"recv_len %d\n",recv_len); //mylog(log_info,"recv_len %d\n",recv_len);
if(recv_len<int(sizeof(iphdr))) if(recv_len<int(sizeof(iphdr)))
{ {
mylog(log_trace,"failed here %d \n",recv_len,int(sizeof(iphdr)));
mylog(log_trace,"%s\n ",strerror(errno));
return -1; return -1;
} }
peek_info.src_ip=iph->saddr; peek_info.src_ip=iph->saddr;
@ -407,10 +410,17 @@ int peek_raw(packet_info_t &peek_info)
{ {
case mode_faketcp: case mode_faketcp:
{ {
if(iph->protocol!=IPPROTO_TCP) return -1; if(iph->protocol!=IPPROTO_TCP)
{
mylog(log_trace,"failed here");
return -1;
}
struct tcphdr *tcph=(tcphdr *)payload; struct tcphdr *tcph=(tcphdr *)payload;
if(recv_len<int( iphdrlen+sizeof(tcphdr) )) if(recv_len<int( iphdrlen+sizeof(tcphdr) ))
{
mylog(log_trace,"failed here");
return -1; return -1;
}
peek_info.src_port=ntohs(tcph->source); peek_info.src_port=ntohs(tcph->source);
peek_info.syn=tcph->syn; peek_info.syn=tcph->syn;
break; break;
@ -446,10 +456,8 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
iphdr * iph; iphdr * iph;
struct sockaddr saddr; struct sockaddr saddr;
socklen_t saddr_size; socklen_t saddr_size = sizeof(saddr);
saddr_size = sizeof(saddr);
int flag=0; int flag=0;
int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len, flag ,&saddr , &saddr_size); int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len, flag ,&saddr , &saddr_size);
if(recv_len<0) if(recv_len<0)
@ -481,6 +489,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
if(bind_address_uint32!=0 &&recv_info.dst_ip!=bind_address_uint32) if(bind_address_uint32!=0 &&recv_info.dst_ip!=bind_address_uint32)
{ {
mylog(log_trace,"bind adress doenst match, dropped\n");
//printf(" bind adress doenst match, dropped\n"); //printf(" bind adress doenst match, dropped\n");
return -1; return -1;
} }