ipv6 for non-raw end works

This commit is contained in:
wangyu- 2018-07-20 04:35:25 -05:00
parent 822a807e58
commit 30eb96608d
2 changed files with 21 additions and 19 deletions

View File

@ -175,17 +175,19 @@ char* address_t::get_ip()
int address_t::from_sockaddr(sockaddr * addr,socklen_t slen) int address_t::from_sockaddr(sockaddr * addr,socklen_t slen)
{ {
memset(&inner,0,sizeof(inner)); clear();
//memset(&inner,0,sizeof(inner));
if(addr->sa_family==AF_INET6) if(addr->sa_family==AF_INET6)
{ {
assert(slen==sizeof(sockaddr_in6)); assert(slen==sizeof(sockaddr_in6));
inner.ipv6= *( (sockaddr_in6*) addr ); //inner.ipv6= *( (sockaddr_in6*) addr );
memcpy(&inner,addr,slen);
} }
else if(addr->sa_family==AF_INET) else if(addr->sa_family==AF_INET)
{ {
assert(slen==sizeof(sockaddr_in)); assert(slen==sizeof(sockaddr_in));
inner.ipv4= *( (sockaddr_in*) addr ); //inner.ipv4= *( (sockaddr_in*) addr );
memcpy(&inner,addr,slen);
} }
else else
{ {

View File

@ -468,7 +468,7 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
if(ret<0) if(ret<0)
{ {
mylog(log_warn,"sento returned %d\n",ret); mylog(log_warn,"sento returned %d,%s,%02x,%s\n",ret,strerror(errno),int(tmp_addr.get_type()),tmp_addr.get_str());
//perror("ret<0"); //perror("ret<0");
} }
//mylog(log_trace,"%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port)); //mylog(log_trace,"%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port));
@ -493,12 +493,12 @@ int client_on_udp_recv(conn_info_t &conn_info)
{ {
int recv_len; int recv_len;
char buf[buf_len]; char buf[buf_len];
struct sockaddr_in udp_new_addr_in={0}; address_t::storage_t udp_new_addr_in={0};
socklen_t udp_new_addr_len = sizeof(sockaddr_in); socklen_t udp_new_addr_len = sizeof(address_t::storage_t);
if ((recv_len = recvfrom(udp_fd, buf, max_data_len+1, 0, if ((recv_len = recvfrom(udp_fd, buf, max_data_len+1, 0,
(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -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_warn,"recv_from error,%s\n",strerror(errno));
myexit(1); //myexit(1);
}; };
if(recv_len==max_data_len+1) if(recv_len==max_data_len+1)
@ -511,8 +511,8 @@ int client_on_udp_recv(conn_info_t &conn_info)
{ {
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); 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);
} }
mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr), //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); // ntohs(udp_new_addr_in.sin_port),recv_len);
/* /*
if(udp_old_addr_in.sin_addr.s_addr==0&&udp_old_addr_in.sin_port==0) if(udp_old_addr_in.sin_addr.s_addr==0&&udp_old_addr_in.sin_port==0)
@ -536,13 +536,13 @@ int client_on_udp_recv(conn_info_t &conn_info)
}*/ }*/
//last_udp_recv_time=get_current_time(); //last_udp_recv_time=get_current_time();
address_t tmp_address; address_t tmp_addr;
tmp_address.from_sockaddr((sockaddr *)&udp_new_addr_in,udp_new_addr_len); tmp_addr.from_sockaddr((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); //u64_t u64=((u64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
u32_t conv; u32_t conv;
//u64_t u64;//////todo //u64_t u64;//////todo
if(!conn_info.blob->conv_manager.c.is_data_used(tmp_address)) if(!conn_info.blob->conv_manager.c.is_data_used(tmp_addr))
{ {
if(conn_info.blob->conv_manager.c.get_size() >=max_conv_num) if(conn_info.blob->conv_manager.c.get_size() >=max_conv_num)
{ {
@ -550,12 +550,12 @@ int client_on_udp_recv(conn_info_t &conn_info)
return -1; return -1;
} }
conv=conn_info.blob->conv_manager.c.get_new_conv(); conv=conn_info.blob->conv_manager.c.get_new_conv();
conn_info.blob->conv_manager.c.insert_conv(conv,tmp_address); 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",tmp_addr.get_str(),conv);
} }
else else
{ {
conv=conn_info.blob->conv_manager.c.find_conv_by_data(tmp_address); conv=conn_info.blob->conv_manager.c.find_conv_by_data(tmp_addr);
} }
conn_info.blob->conv_manager.c.update_active_time(conv); conn_info.blob->conv_manager.c.update_active_time(conv);
@ -1455,13 +1455,13 @@ int client_event_loop()
//g_packet_info.src_ip=source_address_uint32; //g_packet_info.src_ip=source_address_uint32;
//g_packet_info.src_port=source_port; //g_packet_info.src_port=source_port;
udp_fd=socket(remote_addr.get_type(), SOCK_DGRAM, IPPROTO_UDP); udp_fd=socket(local_addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
set_buf_size(udp_fd,socket_buf_size); set_buf_size(udp_fd,socket_buf_size);
int yes = 1; int yes = 1;
//setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); //setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
struct sockaddr_in local_me={0}; //struct sockaddr_in local_me={0};
//socklen_t slen = sizeof(sockaddr_in); //socklen_t slen = sizeof(sockaddr_in);
//memset(&local_me, 0, sizeof(local_me)); //memset(&local_me, 0, sizeof(local_me));