From 0980d890726341ff8bb2a1afab741a7be19b0dc4 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Fri, 27 Jul 2018 02:26:24 -0500 Subject: [PATCH] sync more updates from mp --- common.cpp | 33 ++++++++++--------- common.h | 94 ++++++++++++++++++++++++++++++++++++++--------------- network.cpp | 16 ++++----- 3 files changed, 93 insertions(+), 50 deletions(-) diff --git a/common.cpp b/common.cpp index 5d19595..1a50414 100644 --- a/common.cpp +++ b/common.cpp @@ -419,25 +419,25 @@ u32_t get_true_random_number_nz() //nz for non-zero } return ret; } + u64_t ntoh64(u64_t a) { - if(__BYTE_ORDER == __LITTLE_ENDIAN) - { - return bswap_64( a); - } - else return a; + #ifdef UDP2RAW_LITTLE_ENDIAN + u32_t h=get_u64_h(a); + u32_t l=get_u64_l(a); + return pack_u64(ntohl(l),ntohl(h)); + //return bswap_64( a); + #else + return a; + #endif } u64_t hton64(u64_t a) { - if(__BYTE_ORDER == __LITTLE_ENDIAN) - { - return bswap_64( a); - } - else return a; - + return ntoh64(a); } + void write_u16(char * p,u16_t w) { *(unsigned char*)(p + 1) = (w & 0xff); @@ -643,10 +643,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); @@ -673,11 +672,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); @@ -703,7 +704,7 @@ bool larger_than_u16(uint16_t a,uint16_t b) { return 1; } - } + }*/ } void myexit(int a) diff --git a/common.h b/common.h index 68b9b69..7cc8fad 100644 --- a/common.h +++ b/common.h @@ -17,38 +17,63 @@ #include #include -#include -#include -#include //for socket ofcourse -#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 -#include -#include + + #include +#if defined(UDP2RAW_MP) + +#if !defined(__CYGWIN__) && !defined(__MINGW32__) +#include +#else +#include +#define NO_LIBNET +#endif +#ifndef NO_LIBNET +#include +#endif +#include + +#else + +#include +#include +#include +#include +#include +#include +#include +#include + +#endif + +#if defined(__MINGW32__) +#include +typedef unsigned char u_int8_t; +typedef unsigned short u_int16_t; +typedef unsigned int u_int32_t; +typedef int socklen_t; +#else +#include +#include +#include +#include +#include +#endif + + #include #include #include @@ -56,7 +81,6 @@ #include #include #include -#include using namespace std; @@ -90,6 +114,27 @@ using namespace std; #error "endian detection failed" #endif +#if defined(__MINGW32__) +#define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const char *)(d),e) +#endif + +char *get_sock_error(); +int get_sock_errno(); + +#if defined(__MINGW32__) +typedef SOCKET my_fd_t; +inline int sock_close(my_fd_t fd) +{ + return closesocket(fd); +} +#else +typedef int my_fd_t; +inline int sock_close(my_fd_t fd) +{ + return close(fd); +} + +#endif typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64 typedef long long i64_t; @@ -301,9 +346,6 @@ struct not_copy_able_t }; - - - const int max_data_len=1800; const int buf_len=max_data_len+400; diff --git a/network.cpp b/network.cpp index 93cb4b3..bfb8ba3 100644 --- a/network.cpp +++ b/network.cpp @@ -1167,14 +1167,14 @@ int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen) char send_raw_udp_buf[buf_len]; - udphdr *udph=(struct udphdr *) (send_raw_udp_buf); + my_udphdr *udph=(struct my_udphdr *) (send_raw_udp_buf); - memset(udph,0,sizeof(udphdr)); + memset(udph,0,sizeof(my_udphdr)); udph->source = htons(send_info.src_port); udph->dest = htons(send_info.dst_port); - int udp_tot_len=payloadlen+sizeof(udphdr); + int udp_tot_len=payloadlen+sizeof(my_udphdr); if(udp_tot_len>65535) @@ -1185,7 +1185,7 @@ int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen) mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len); udph->len=htons(uint16_t(udp_tot_len)); - memcpy(send_raw_udp_buf+sizeof(udphdr),payload,payloadlen); + memcpy(send_raw_udp_buf+sizeof(my_udphdr),payload,payloadlen); @@ -1664,12 +1664,12 @@ int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen) //printf("not udp protocol\n"); return -1; } - if(ip_payloadlenlen))!=ip_payloadlen) { @@ -1744,9 +1744,9 @@ int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen) recv_info.src_port=ntohs(udph->source); recv_info.dst_port=ntohs(udph->dest); - payloadlen = ip_payloadlen-sizeof(udphdr); + payloadlen = ip_payloadlen-sizeof(my_udphdr); - payload=udp_begin+sizeof(udphdr); + payload=udp_begin+sizeof(my_udphdr); return 0; }