From 92fcdbb31a8f7273afd09b36b8c983554b51f076 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Wed, 29 Aug 2018 20:00:37 -0500 Subject: [PATCH] reduce diff --- common.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ common.h | 21 +++++++-------------- network.cpp | 5 +++-- 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/common.cpp b/common.cpp index dbb2bb1..dd78c67 100644 --- a/common.cpp +++ b/common.cpp @@ -347,6 +347,52 @@ int my_ip_t::from_str(char * str) }*/ #if defined(__MINGW32__) +int inet_pton(int af, const char *src, void *dst) +{ + struct sockaddr_storage ss; + int size = sizeof(ss); + char src_copy[max_addr_len+1]; + + ZeroMemory(&ss, sizeof(ss)); + /* stupid non-const API */ + strncpy (src_copy, src, max_addr_len+1); + src_copy[max_addr_len] = 0; + + if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) { + switch(af) { + case AF_INET: + *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; + return 1; + case AF_INET6: + *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; + return 1; + } + } + return 0; +} + +const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) +{ + struct sockaddr_storage ss; + unsigned long s = size; + + ZeroMemory(&ss, sizeof(ss)); + ss.ss_family = af; + + switch(af) { + case AF_INET: + ((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src; + break; + case AF_INET6: + ((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src; + break; + default: + return NULL; + } + /* cannot direclty use &size because of strict aliasing rules */ + return (WSAAddressToString((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s) == 0)? + dst : NULL; +} char *get_sock_error() { static char buf[1000]; diff --git a/common.h b/common.h index 04422a9..a589e2b 100644 --- a/common.h +++ b/common.h @@ -17,20 +17,14 @@ #include #include -//#include -//#include - #include #include //for exit(0); #include //For errno - the error number #include #include #include - #include #include - - #include #if defined(UDP2RAW_MP) @@ -41,6 +35,7 @@ #include #define NO_LIBNET #endif + #ifndef NO_LIBNET #include #endif @@ -70,8 +65,8 @@ typedef int socklen_t; #include #include #include -#include #include +#include #endif @@ -84,7 +79,6 @@ typedef int socklen_t; #include using namespace std; - #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \ defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \ defined(__BIG_ENDIAN__) || \ @@ -115,7 +109,10 @@ using namespace std; #error "endian detection failed" #endif + #if defined(__MINGW32__) +int inet_pton(int af, const char *src, void *dst); +const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); #define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const char *)(d),e) #endif @@ -137,6 +134,7 @@ inline int sock_close(my_fd_t fd) #endif + typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64 typedef long long i64_t; @@ -370,12 +368,10 @@ 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_uu64(char *); +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); @@ -416,7 +412,6 @@ int create_fifo(char * file); void print_binary_chars(const char * a,int len); - template struct lru_collector_t:not_copy_able_t { @@ -505,6 +500,4 @@ struct lru_collector_t:not_copy_able_t }; - - #endif /* COMMON_H_ */ diff --git a/network.cpp b/network.cpp index 26bf6ae..0c225ac 100644 --- a/network.cpp +++ b/network.cpp @@ -460,6 +460,7 @@ void remove_filter() //exit(-1); } } + int init_ifindex(const char * if_name,int fd,int &index) { struct ifreq ifr; @@ -1327,7 +1328,7 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { tcph->urg = 0; //tcph->window = htons((uint16_t)(1024)); - tcph->window = htons((uint16_t) (receive_window_lower_bound + random() % receive_window_random_range)); + tcph->window = htons((uint16_t) (receive_window_lower_bound + get_true_random_number() % receive_window_random_range)); tcph->check = 0; //leave checksum 0 now, filled later by pseudo header tcph->urg_ptr = 0; @@ -2266,7 +2267,7 @@ int after_send_raw0(raw_info_t &raw_info) send_info.seq += raw_info.send_info.data_len; //////////////////modify } else if (seq_mode == 2) { - if (random() % 5 == 3) + if (get_true_random_number() % 5 == 3) send_info.seq += raw_info.send_info.data_len; //////////////////modify } else if(seq_mode==3||seq_mode==4)