From 57af53afa8b4447782cbf468b53ff3245087637e Mon Sep 17 00:00:00 2001 From: wangyu- Date: Tue, 28 Aug 2018 15:51:53 -0500 Subject: [PATCH] deleted unused codes,reduce diff with non-mp version --- common.cpp | 460 ++++++++++++++++++++++++++++++++++++++++++++++++- common.h | 305 +++++++++++++++++++++++++++++--- connection.cpp | 167 ------------------ main.cpp | 2 +- misc.cpp | 5 +- misc.h | 5 +- 6 files changed, 746 insertions(+), 198 deletions(-) diff --git a/common.cpp b/common.cpp index f914f3b..1155198 100644 --- a/common.cpp +++ b/common.cpp @@ -13,6 +13,339 @@ #include static int random_number_fd=-1; +int force_socket_buf=0; + +int address_t::from_str(char *str) +{ + clear(); + + char ip_addr_str[100];u32_t port; + mylog(log_info,"parsing address: %s\n",str); + int is_ipv6=0; + if(sscanf(str, "[%[^]]]:%u", ip_addr_str,&port)==2) + { + mylog(log_info,"its an ipv6 adress\n"); + inner.ipv6.sin6_family=AF_INET6; + is_ipv6=1; + } + else if(sscanf(str, "%[^:]:%u", ip_addr_str,&port)==2) + { + mylog(log_info,"its an ipv4 adress\n"); + inner.ipv4.sin_family=AF_INET; + } + else + { + mylog(log_error,"failed to parse\n"); + myexit(-1); + } + + mylog(log_info,"ip_address is {%s}, port is {%u}\n",ip_addr_str,port); + + if(port>65535) + { + mylog(log_error,"invalid port: %d\n",port); + myexit(-1); + } + + int ret=-100; + if(is_ipv6) + { + ret=inet_pton(AF_INET6, ip_addr_str,&(inner.ipv6.sin6_addr)); + inner.ipv6.sin6_port=htons(port); + if(ret==0) // 0 if address type doesnt match + { + mylog(log_error,"ip_addr %s is not an ipv6 address, %d\n",ip_addr_str,ret); + myexit(-1); + } + else if(ret==1) // inet_pton returns 1 on success + { + //okay + } + else + { + mylog(log_error,"ip_addr %s is invalid, %d\n",ip_addr_str,ret); + myexit(-1); + } + } + else + { + ret=inet_pton(AF_INET, ip_addr_str,&(inner.ipv4.sin_addr)); + inner.ipv4.sin_port=htons(port); + + if(ret==0) + { + mylog(log_error,"ip_addr %s is not an ipv4 address, %d\n",ip_addr_str,ret); + myexit(-1); + } + else if(ret==1) + { + //okay + } + else + { + mylog(log_error,"ip_addr %s is invalid, %d\n",ip_addr_str,ret); + myexit(-1); + } + } + + return 0; +} + +int address_t::from_str_ip_only(char * str) +{ + clear(); + + u32_t type; + + if(strchr(str,':')==NULL) + type=AF_INET; + else + type=AF_INET6; + + ((sockaddr*)&inner)->sa_family=type; + + int ret; + if(type==AF_INET) + { + ret=inet_pton(type, str,&inner.ipv4.sin_addr); + } + else + { + ret=inet_pton(type, str,&inner.ipv6.sin6_addr); + } + + if(ret==0) // 0 if address type doesnt match + { + mylog(log_error,"confusion in parsing %s, %d\n",str,ret); + myexit(-1); + } + else if(ret==1) // inet_pton returns 1 on success + { + //okay + } + else + { + mylog(log_error,"ip_addr %s is invalid, %d\n",str,ret); + myexit(-1); + } + return 0; +} + +char * address_t::get_str() +{ + static char res[max_addr_len]; + to_str(res); + return res; +} +void address_t::to_str(char * s) +{ + //static char res[max_addr_len]; + char ip_addr[max_addr_len]; + u32_t port; + const char * ret=0; + if(get_type()==AF_INET6) + { + ret=inet_ntop(AF_INET6, &inner.ipv6.sin6_addr, ip_addr,max_addr_len); + port=inner.ipv6.sin6_port; + } + else if(get_type()==AF_INET) + { + ret=inet_ntop(AF_INET, &inner.ipv4.sin_addr, ip_addr,max_addr_len); + port=inner.ipv4.sin_port; + } + else + { + assert(0==1); + } + + if(ret==0) //NULL on failure + { + mylog(log_error,"inet_ntop failed\n"); + myexit(-1); + } + + port=ntohs(port); + + ip_addr[max_addr_len-1]=0; + if(get_type()==AF_INET6) + { + sprintf(s,"[%s]:%u",ip_addr,(u32_t)port); + }else + { + sprintf(s,"%s:%u",ip_addr,(u32_t)port); + } + + //return res; +} + +char* address_t::get_ip() +{ + char ip_addr[max_addr_len]; + static char s[max_addr_len]; + const char * ret=0; + if(get_type()==AF_INET6) + { + ret=inet_ntop(AF_INET6, &inner.ipv6.sin6_addr, ip_addr,max_addr_len); + } + else if(get_type()==AF_INET) + { + ret=inet_ntop(AF_INET, &inner.ipv4.sin_addr, ip_addr,max_addr_len); + } + else + { + assert(0==1); + } + + if(ret==0) //NULL on failure + { + mylog(log_error,"inet_ntop failed\n"); + myexit(-1); + } + + ip_addr[max_addr_len-1]=0; + if(get_type()==AF_INET6) + { + sprintf(s,"%s",ip_addr); + }else + { + sprintf(s,"%s",ip_addr); + } + + return s; +} + +int address_t::from_sockaddr(sockaddr * addr,socklen_t slen) +{ + clear(); + //memset(&inner,0,sizeof(inner)); + if(addr->sa_family==AF_INET6) + { + assert(slen==sizeof(sockaddr_in6)); + //inner.ipv6= *( (sockaddr_in6*) addr ); + memcpy(&inner,addr,slen); + } + else if(addr->sa_family==AF_INET) + { + assert(slen==sizeof(sockaddr_in)); + //inner.ipv4= *( (sockaddr_in*) addr ); + memcpy(&inner,addr,slen); + } + else + { + assert(0==1); + } + return 0; +} + +int address_t::new_connected_udp_fd() +{ + + int new_udp_fd; + new_udp_fd = socket(get_type(), SOCK_DGRAM, IPPROTO_UDP); + if (new_udp_fd < 0) { + mylog(log_warn, "create udp_fd error\n"); + return -1; + } + setnonblocking(new_udp_fd); + set_buf_size(new_udp_fd,socket_buf_size); + + mylog(log_debug, "created new udp_fd %d\n", new_udp_fd); + int ret = connect(new_udp_fd, (struct sockaddr *) &inner, get_len()); + if (ret != 0) { + mylog(log_warn, "udp fd connect fail %d %s\n",ret,strerror(errno) ); + //sock_close(new_udp_fd); + close(new_udp_fd); + return -1; + } + + return new_udp_fd; +} + +bool my_ip_t::equal (const my_ip_t &b) const +{ + //extern int raw_ip_version; + if(raw_ip_version==AF_INET) + { + return v4==b.v4; + }else if(raw_ip_version==AF_INET6) + { + return memcmp(&v6,&b.v6,sizeof(v6))==0; + } + assert(0==1); + return 0; +} +char * my_ip_t::get_str1() const +{ + static char res[max_addr_len]; + if(raw_ip_version==AF_INET6) + { + assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0); + } + else + { + assert(raw_ip_version==AF_INET); + assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0); + } + return res; +} +char * my_ip_t::get_str2() const +{ + static char res[max_addr_len]; + if(raw_ip_version==AF_INET6) + { + assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0); + } + else + { + assert(raw_ip_version==AF_INET); + assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0); + } + return res; +} + +int my_ip_t::from_address_t(address_t tmp_addr) +{ + if(tmp_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET) + { + v4=tmp_addr.inner.ipv4.sin_addr.s_addr; + } + else if(tmp_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET6) + { + v6=tmp_addr.inner.ipv6.sin6_addr; + } + else + { + assert(0==1); + } + return 0; +} + +/* +int my_ip_t::from_str(char * str) +{ + u32_t type; + if(strchr(str,':')==NULL) + type=AF_INET; + else + type=AF_INET6; + int ret; + ret=inet_pton(type, str,this); + if(ret==0) // 0 if address type doesnt match + { + mylog(log_error,"confusion in parsing %s, %d\n",str,ret); + myexit(-1); + } + else if(ret==1) // inet_pton returns 1 on success + { + //okay + } + else + { + mylog(log_error,"ip_addr %s is invalid, %d\n",str,ret); + myexit(-1); + } + return 0; +}*/ int init_ws() { @@ -127,6 +460,20 @@ char * my_ntoa(u32_t ip) return inet_ntoa(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); + } + setnonblocking(random_number_fd); +}*/ + #if !defined(__MINGW32__) struct random_fd_t { @@ -261,6 +608,48 @@ u64_t hton64(u64_t a) return ntoh64(a); } +void write_u16(char * p,u16_t w) +{ + *(unsigned char*)(p + 1) = (w & 0xff); + *(unsigned char*)(p + 0) = (w >> 8); +} +u16_t read_u16(char * p) +{ + u16_t res; + res = *(const unsigned char*)(p + 0); + res = *(const unsigned char*)(p + 1) + (res << 8); + return res; +} + +void write_u32(char * p,u32_t l) +{ + *(unsigned char*)(p + 3) = (unsigned char)((l >> 0) & 0xff); + *(unsigned char*)(p + 2) = (unsigned char)((l >> 8) & 0xff); + *(unsigned char*)(p + 1) = (unsigned char)((l >> 16) & 0xff); + *(unsigned char*)(p + 0) = (unsigned char)((l >> 24) & 0xff); +} +u32_t read_u32(char * p) +{ + u32_t res; + res = *(const unsigned char*)(p + 0); + res = *(const unsigned char*)(p + 1) + (res << 8); + res = *(const unsigned char*)(p + 2) + (res << 8); + res = *(const unsigned char*)(p + 3) + (res << 8); + return res; +} + +void write_u64(char * s,u64_t a) +{ + assert(0==1); +} +u64_t read_u64(char * s) +{ + assert(0==1); + return 0; +} + + + void setnonblocking(int sock) { #if !defined(__MINGW32__) int opts; @@ -313,7 +702,40 @@ unsigned short csum(const unsigned short *ptr,int nbytes) {//works both for big return(answer); } -int set_buf_size(int fd,int socket_buf_size,int force_socket_buf) +unsigned short csum_with_header(char* header,int hlen,const unsigned short *ptr,int nbytes) {//works both for big and little endian + + long sum; + unsigned short oddbyte; + short answer; + + assert(hlen%2==0); + + sum=0; + unsigned short * tmp= (unsigned short *)header; + for(int i=0;i1) { + 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,int socket_buf_size) { if(force_socket_buf) { @@ -408,10 +830,9 @@ int hex_to_u32_with_endian(const string & a,u32_t &output) return -1; } bool larger_than_u32(u32_t a,u32_t b) -//TODO -//looks like this can simply be done by return ((i32_t)(a-b) >0) { - + return ((i32_t(a-b)) >0); +/* u32_t smaller,bigger; smaller=min(a,b);//smaller in normal sense bigger=max(a,b); @@ -438,11 +859,13 @@ bool larger_than_u32(u32_t a,u32_t b) return 1; } } +*/ } bool larger_than_u16(uint16_t a,uint16_t b) { - + return ((i16_t(a-b)) >0); +/* uint16_t smaller,bigger; smaller=min(a,b);//smaller in normal sense bigger=max(a,b); @@ -469,6 +892,7 @@ bool larger_than_u16(uint16_t a,uint16_t b) return 1; } } +*/ } void myexit(int a) @@ -803,3 +1227,29 @@ void print_binary_chars(const char * a,int len) log_bare(log_debug,"\n"); } +u32_t djb2(unsigned char *str,int len) +{ + u32_t hash = 5381; + int c; + int i=0; + while(c = *str++,i++!=len) + { + hash = ((hash << 5) + hash)^c; /* (hash * 33) ^ c */ + } + + hash=htonl(hash); + return hash; + } + +u32_t sdbm(unsigned char *str,int len) +{ + u32_t hash = 0; + int c; + int i=0; + while(c = *str++,i++!=len) + { + hash = c + (hash << 6) + (hash << 16) - hash; + } + //hash=htonl(hash); + return hash; + } diff --git a/common.h b/common.h index 1a9aa26..70a02fc 100644 --- a/common.h +++ b/common.h @@ -17,32 +17,14 @@ #include #include -//#include -//#include - #include #include //for exit(0); #include //For errno - the error number -//#include // for gethostbyname() -//#include //Provides declarations for tcp header -//#include -//#include //Provides declarations for ip header -//#include -//#include #include -//#include -//#include -//#include -//#include #include #include -//#include -//#include -//#include #include #include -//#include -//#include #include #if !defined(__CYGWIN__) && !defined(__MINGW32__) @@ -65,7 +47,7 @@ typedef unsigned short u_int16_t; typedef unsigned int u_int32_t; typedef int socklen_t; #else -#include //for socket ofcourse +#include #include #include #include @@ -79,6 +61,7 @@ typedef int socklen_t; #include #include #include +#include using namespace std; #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \ @@ -141,6 +124,9 @@ typedef long long i64_t; typedef unsigned int u32_t; typedef int i32_t; +typedef unsigned short u16_t; +typedef short i16_t; + typedef u32_t my_id_t; typedef u64_t iv_t; @@ -149,6 +135,11 @@ typedef u64_t padding_t; typedef u64_t anti_replay_seq_t; +typedef u64_t my_time_t; + +const int max_addr_len=100; + +extern int force_socket_buf; struct ip_port_t { @@ -162,11 +153,187 @@ struct ip_port_t typedef u64_t fd64_t; +u32_t djb2(unsigned char *str,int len); +u32_t sdbm(unsigned char *str,int len); + +struct address_t //TODO scope id +{ + struct hash_function + { + u32_t operator()(const address_t &key) const + { + return sdbm((unsigned char*)&key.inner,sizeof(key.inner)); + } + }; + + union storage_t //sockaddr_storage is too huge, we dont use it. + { + sockaddr_in ipv4; + sockaddr_in6 ipv6; + }; + storage_t inner; + + address_t() + { + clear(); + } + void clear() + { + memset(&inner,0,sizeof(inner)); + } + int from_ip_port(u32_t ip, int port) + { + clear(); + inner.ipv4.sin_family=AF_INET; + inner.ipv4.sin_port=htons(port); + inner.ipv4.sin_addr.s_addr=ip; + return 0; + } + + int from_ip_port_new(int type, void * ip, int port) + { + clear(); + if(type==AF_INET) + { + inner.ipv4.sin_family=AF_INET; + inner.ipv4.sin_port=htons(port); + inner.ipv4.sin_addr.s_addr=*((u32_t *)ip); + } + else if(type==AF_INET6) + { + inner.ipv6.sin6_family=AF_INET6; + inner.ipv6.sin6_port=htons(port); + inner.ipv6.sin6_addr=*((in6_addr*)ip); + } + return 0; + } + + int from_str(char * str); + + int from_str_ip_only(char * str); + + int from_sockaddr(sockaddr *,socklen_t); + + char* get_str(); + void to_str(char *); + + inline u32_t get_type() + { + u32_t ret=((sockaddr*)&inner)->sa_family; + assert(ret==AF_INET||ret==AF_INET6); + return ret; + } + + inline u32_t get_len() + { + u32_t type=get_type(); + switch(type) + { + case AF_INET: + return sizeof(sockaddr_in); + case AF_INET6: + return sizeof(sockaddr_in6); + default: + assert(0==1); + } + return -1; + } + + inline u32_t get_port() + { + u32_t type=get_type(); + switch(type) + { + case AF_INET: + return ntohs(inner.ipv4.sin_port); + case AF_INET6: + return ntohs(inner.ipv6.sin6_port); + default: + assert(0==1); + } + return -1; + } + + inline void set_port(int port) + { + u32_t type=get_type(); + switch(type) + { + case AF_INET: + inner.ipv4.sin_port=htons(port); + break; + case AF_INET6: + inner.ipv6.sin6_port=htons(port); + break; + default: + assert(0==1); + } + return ; + } + + bool operator == (const address_t &b) const + { + //return this->data==b.data; + return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0; + } + + int new_connected_udp_fd(); + + char* get_ip(); +}; + +namespace std { +template <> + struct hash + { + std::size_t operator()(const address_t& key) const + { + + //return address_t::hash_function(k); + return sdbm((unsigned char*)&key.inner,sizeof(key.inner)); + } + }; +} + +union my_ip_t //just a simple version of address_t,stores ip only +{ + u32_t v4; + in6_addr v6; + + bool equal (const my_ip_t &b) const; + + //int from_str(char * str); + char * get_str1() const; + char * get_str2() const; + + int from_address_t(address_t a); + +}; + +struct not_copy_able_t +{ + not_copy_able_t() + { + + } + not_copy_able_t(const not_copy_able_t &other) + { + assert(0==1); + } + const not_copy_able_t & operator=(const not_copy_able_t &other) + { + assert(0==1); + return other; + } +}; + const int max_data_len=1800; const int buf_len=max_data_len+400; -const int max_address_len=512; + const int queue_len=2000; +//const int max_address_len=512; + struct queue_t { char data[queue_len][buf_len]; @@ -225,10 +392,18 @@ u32_t get_true_random_number(); u32_t get_true_random_number_nz(); u64_t ntoh64(u64_t a); u64_t hton64(u64_t a); + +void write_u16(char *,u16_t a);// network order +u16_t read_u16(char *); +void write_u32(char *,u32_t a);// network order +u32_t read_u32(char *); +void write_u64(char *,u64_t a); +u64_t read_u64(char *); + bool larger_than_u16(uint16_t a,uint16_t b); bool larger_than_u32(u32_t a,u32_t b); void setnonblocking(int sock); -int set_buf_size(int fd,int socket_buf_size,int force_socket_buf); +int set_buf_size(int fd,int socket_buf_size); void myexit(int a); @@ -263,4 +438,92 @@ int create_fifo(char * file); void print_binary_chars(const char * a,int len); +template +struct lru_collector_t:not_copy_able_t +{ + //typedef void* key_t; +//#define key_t void* + struct lru_pair_t + { + key_t key; + my_time_t ts; + }; + + unordered_map::iterator> mp; + + list q; + int update(key_t key) + { + assert(mp.find(key)!=mp.end()); + auto it=mp[key]; + q.erase(it); + + my_time_t value=get_current_time(); + if(!q.empty()) + { + assert(value >=q.front().ts); + } + lru_pair_t tmp; tmp.key=key; tmp.ts=value; + q.push_front( tmp); + mp[key]=q.begin(); + + return 0; + } + int new_key(key_t key) + { + assert(mp.find(key)==mp.end()); + + my_time_t value=get_current_time(); + if(!q.empty()) + { + assert(value >=q.front().ts); + } + lru_pair_t tmp; tmp.key=key; tmp.ts=value; + q.push_front( tmp); + mp[key]=q.begin(); + + return 0; + } + int size() + { + return q.size(); + } + int empty() + { + return q.empty(); + } + void clear() + { + mp.clear(); q.clear(); + } + my_time_t ts_of(key_t key) + { + assert(mp.find(key)!=mp.end()); + return mp[key]->ts; + } + + my_time_t peek_back(key_t &key) + { + assert(!q.empty()); + auto it=q.end(); it--; + key=it->key; + return it->ts; + } + void erase(key_t key) + { + assert(mp.find(key)!=mp.end()); + q.erase(mp[key]); + mp.erase(key); + } + /* + void erase_back() + { + assert(!q.empty()); + auto it=q.end(); it--; + key_t key=it->key; + erase(key); + }*/ +}; + + #endif /* COMMON_H_ */ diff --git a/connection.cpp b/connection.cpp index 077d600..454344a 100644 --- a/connection.cpp +++ b/connection.cpp @@ -16,8 +16,6 @@ const int disable_conv_clear=0;//a udp connection in the multiplexer is called c const int disable_conn_clear=0;//a raw connection is called conn. -conn_manager_t conn_manager; - anti_replay_seq_t anti_replay_t::get_new_seq_for_send() { return anti_replay_seq++; @@ -312,171 +310,6 @@ conv_manager_t::~conv_manager_t() } - conn_manager_t::conn_manager_t() - { - ready_num=0; - mp.reserve(10007); - clear_it=mp.begin(); - // timer_fd_mp.reserve(10007); - const_id_mp.reserve(10007); - // udp_fd_mp.reserve(100007); - last_clear_time=0; - //current_ready_ip=0; - // current_ready_port=0; - } - int conn_manager_t::exist(u32_t ip,uint16_t port) - { - u64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - if(mp.find(u64)!=mp.end()) - { - return 1; - } - return 0; - } - /* - int insert(uint32_t ip,uint16_t port) - { - uint64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - mp[u64]; - return 0; - }*/ - conn_info_t *& conn_manager_t::find_insert_p(u32_t ip,uint16_t port) //be aware,the adress may change after rehash - { - u64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - unordered_map::iterator it=mp.find(u64); - if(it==mp.end()) - { - mp[u64]=new conn_info_t; - } - return mp[u64]; - } - conn_info_t & conn_manager_t::find_insert(u32_t ip,uint16_t port) //be aware,the adress may change after rehash - { - u64_t u64=0; - u64=ip; - u64<<=32u; - u64|=port; - unordered_map::iterator it=mp.find(u64); - if(it==mp.end()) - { - mp[u64]=new conn_info_t; - } - return *mp[u64]; - } - int conn_manager_t::erase(unordered_map::iterator erase_it) - { - if(erase_it->second->state.server_current_state==server_ready) - { - ready_num--; - assert(i32_t(ready_num)!=-1); - assert(erase_it->second!=0); - - assert(erase_it->second->timer_fd64 !=0); - - assert(fd_manager.exist(erase_it->second->timer_fd64)); - - assert(erase_it->second->oppsite_const_id!=0); - assert(const_id_mp.find(erase_it->second->oppsite_const_id)!=const_id_mp.end()); - - - //assert(timer_fd_mp.find(erase_it->second->timer_fd)!=timer_fd_mp.end()); - - const_id_mp.erase(erase_it->second->oppsite_const_id); - - fd_manager.fd64_close(erase_it->second->timer_fd64); - - erase_it->second->timer_fd64=0; - //timer_fd_mp.erase(erase_it->second->timer_fd); - //close(erase_it->second->timer_fd);// close will auto delte it from epoll - delete(erase_it->second); - mp.erase(erase_it->first); - } - else - { - assert(erase_it->second->blob==0); - assert(erase_it->second->timer_fd64 ==0); - - - assert(erase_it->second->oppsite_const_id==0); - delete(erase_it->second); - mp.erase(erase_it->first); - } - return 0; - } -int conn_manager_t::clear_inactive() -{ - if(get_current_time()-last_clear_time>conn_clear_interval) - { - last_clear_time=get_current_time(); - return clear_inactive0(); - } - return 0; -} -int conn_manager_t::clear_inactive0() -{ - unordered_map::iterator it; - unordered_map::iterator old_it; - - if(disable_conn_clear) return 0; - - //map::iterator it; - int cnt=0; - it=clear_it; - int size=mp.size(); - int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch - - mylog(log_trace,"mp.size() %d\n", size); - - num_to_clean=min(num_to_clean,(int)mp.size()); - u64_t current_time=get_current_time(); - - for(;;) - { - if(cnt>=num_to_clean) break; - if(mp.begin()==mp.end()) break; - - if(it==mp.end()) - { - it=mp.begin(); - } - - if(it->second->state.server_current_state==server_ready &¤t_time - it->second->last_hb_recv_time <=server_conn_timeout) - { - it++; - } - else if(it->second->state.server_current_state!=server_ready&& current_time - it->second->last_state_time <=server_handshake_timeout ) - { - it++; - } - else if(it->second->blob!=0&&it->second->blob->conv_manager.get_size() >0) - { - assert(it->second->state.server_current_state==server_ready); - it++; - } - else - { - mylog(log_info,"[%s:%d]inactive conn cleared \n",my_ntoa(it->second->raw_info.recv_info.src_ip),it->second->raw_info.recv_info.src_port); - old_it=it; - it++; - erase(old_it); - } - cnt++; - } - clear_it=it; - return 0; -} - - - int send_bare(raw_info_t &raw_info,const char* data,int len)//send function with encryption but no anti replay,this is used when client and server verifys each other //you have to design the protocol carefully, so that you wont be affect by relay attack diff --git a/main.cpp b/main.cpp index c4fd371..c5df706 100755 --- a/main.cpp +++ b/main.cpp @@ -880,7 +880,7 @@ int client_event_loop() udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - set_buf_size(udp_fd,socket_buf_size,force_socket_buf); + set_buf_size(udp_fd,socket_buf_size); int yes = 1; diff --git a/misc.cpp b/misc.cpp index edf1bf8..38e2d1d 100644 --- a/misc.cpp +++ b/misc.cpp @@ -28,7 +28,7 @@ int ttl_value=64; fd_manager_t fd_manager; -char remote_address[max_address_len]=""; +char remote_address[max_addr_len]=""; char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line. int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled @@ -71,6 +71,7 @@ int iptables_rule_keep_index=0; program_mode_t program_mode=unset_mode;//0 unset; 1client 2server raw_mode_t raw_mode=mode_faketcp; +u32_t raw_ip_version=(u32_t)-1; unordered_map raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}}; int about_to_exit=0; @@ -80,7 +81,7 @@ int about_to_exit=0; int socket_buf_size=1024*1024; -int force_socket_buf=0; +//int force_socket_buf=0; diff --git a/misc.h b/misc.h index 19bcde4..862156e 100644 --- a/misc.h +++ b/misc.h @@ -73,7 +73,7 @@ union current_state_t client_current_state_t client_current_state; }; -extern char remote_address[max_address_len]; +extern char remote_address[max_addr_len]; extern char local_ip[100], remote_ip[100],source_ip[100];//local_ip is for -l option,remote_ip for -r option,source for --source-ip extern u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line. extern int local_port , remote_port,source_port;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled @@ -106,6 +106,7 @@ extern char fifo_file[1000]; extern raw_mode_t raw_mode; +extern u32_t raw_ip_version; extern program_mode_t program_mode; extern unordered_map raw_mode_tostring ; @@ -113,7 +114,7 @@ extern unordered_map raw_mode_tostring ; extern int about_to_exit; extern int socket_buf_size; -extern int force_socket_buf; +//extern int force_socket_buf; extern pthread_t keep_thread; extern int keep_thread_running;