Skip to content

Commit

Permalink
handle truncated packet
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu- committed Feb 20, 2018
1 parent 3138b2c commit 7cb65ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct ip_port_t

typedef u64_t fd64_t;

const int max_data_len=1600;
const int max_data_len=1800;
const int buf_len=max_data_len+400;

u64_t get_current_time();
Expand Down
16 changes: 14 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1294,12 +1294,18 @@ int client_event_loop()
int recv_len;
struct sockaddr_in udp_new_addr_in={0};
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+1, 0,
(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
mylog(log_error,"recv_from error,this shouldnt happen at client\n");
myexit(1);
};

if(recv_len==max_data_len+1)
{
mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
continue;
}

if(recv_len>=mtu_warn)
{
mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",recv_len,mtu_warn);
Expand Down Expand Up @@ -1639,10 +1645,16 @@ int server_event_loop()

int fd=fd_manager.to_fd(fd64);

int recv_len=recv(fd,buf,max_data_len,0);
int recv_len=recv(fd,buf,max_data_len+1,0);

mylog(log_trace,"received a packet from udp_fd,len:%d\n",recv_len);

if(recv_len==max_data_len+1)
{
mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
continue;
}

if(recv_len<0)
{
mylog(log_debug,"udp fd,recv_len<0 continue,%s\n",strerror(errno));
Expand Down
8 changes: 7 additions & 1 deletion network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,13 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
struct sockaddr_ll saddr={0};
socklen_t saddr_size = sizeof(saddr);
int flag=0;
int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len, flag ,(sockaddr*)&saddr , &saddr_size);
int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len+1, flag ,(sockaddr*)&saddr , &saddr_size);

if(recv_len==max_data_len+1)
{
mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
return -1;
}

if(recv_len<0)
{
Expand Down

0 comments on commit 7cb65ec

Please sign in to comment.