diff --git a/common.cpp b/common.cpp index ba746d4..c390cbf 100644 --- a/common.cpp +++ b/common.cpp @@ -93,20 +93,19 @@ inline int is_big_endian() } u64_t ntoh64(u64_t a) { - static int big_endian=is_big_endian(); - if(!big_endian) - { + #ifdef UDP2RAW_BIG_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; + #else + return a; + #endif } u64_t hton64(u64_t a) { - return ntoh64(u64_t a); + return ntoh64(a); } void setnonblocking(int sock) { @@ -131,9 +130,9 @@ void setnonblocking(int sock) { Generic checksum calculation function */ unsigned short csum(const unsigned short *ptr,int nbytes) {//works both for big and little endian - register long sum; + long sum; unsigned short oddbyte; - register short answer; + short answer; sum=0; while(nbytes>1) { diff --git a/common.h b/common.h index 50b281f..4edd884 100644 --- a/common.h +++ b/common.h @@ -61,6 +61,25 @@ #include using namespace std; +#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \ + defined(__BIG_ENDIAN__) || \ + defined(__ARMEB__) || \ + defined(__THUMBEB__) || \ + defined(__AARCH64EB__) || \ + defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__) +#define UDP2RAW_BIG_ENDIAN 1 +#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \ + defined(__LITTLE_ENDIAN__) || \ + defined(__ARMEL__) || \ + defined(__THUMBEL__) || \ + defined(__AARCH64EL__) || \ + defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) +#define UDP2RAW_LITTLE_ENDIAN 1 +// It's a little-endian target architecture +#else +#error "I don't know what architecture this is!" +#endif + typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64 typedef long long i64_t; diff --git a/lib/aes.c b/lib/aes.cpp similarity index 100% rename from lib/aes.c rename to lib/aes.cpp diff --git a/lib/aes_faster_c/aes.c b/lib/aes_faster_c/aes.cpp similarity index 100% rename from lib/aes_faster_c/aes.c rename to lib/aes_faster_c/aes.cpp diff --git a/lib/aes_faster_c/wrapper.c b/lib/aes_faster_c/wrapper.cpp similarity index 100% rename from lib/aes_faster_c/wrapper.c rename to lib/aes_faster_c/wrapper.cpp diff --git a/lib/md5.c b/lib/md5.cpp similarity index 100% rename from lib/md5.c rename to lib/md5.cpp diff --git a/lib/sha1.c b/lib/sha1.cpp similarity index 100% rename from lib/sha1.c rename to lib/sha1.cpp diff --git a/makefile b/makefile index 859a6f4..7733551 100755 --- a/makefile +++ b/makefile @@ -10,10 +10,10 @@ cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++ #cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers ${OPT} -COMMON=main.cpp lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp connection.cpp misc.cpp fd_manager.cpp -lpthread my_ev.cpp -isystem libev -lpcap -D_DEFAULT_SOURCE `libnet-config --defines` `libnet-config --libs` +COMMON=main.cpp lib/md5.cpp encrypt.cpp log.cpp network.cpp common.cpp connection.cpp misc.cpp fd_manager.cpp -lpthread my_ev.cpp -isystem libev -lpcap -D_DEFAULT_SOURCE `libnet-config --defines` `libnet-config --libs` -SOURCES= $(COMMON) lib/aes_faster_c/aes.c lib/aes_faster_c/wrapper.c -SOURCES_TINY_AES= $(COMMON) lib/aes.c +SOURCES= $(COMMON) lib/aes_faster_c/aes.cpp lib/aes_faster_c/wrapper.cpp +SOURCES_TINY_AES= $(COMMON) lib/aes.cpp SOURCES_AES_ACC=$(COMMON) $(wildcard lib/aes_acc/aes*.c) NAME=udp2raw @@ -23,6 +23,9 @@ TAR=${NAME}_binaries.tar.gz `echo ${TARGETS}|sed -r 's/([^ ]+)/udp2raw_\1/g'` ve all:git_version rm -f ${NAME} ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb -static -O3 +mac:git_version + rm -f ${NAME} + ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -ggdb -O2 fast: git_version rm -f ${NAME} ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb diff --git a/network.cpp b/network.cpp index 6db3832..cd76a7e 100644 --- a/network.cpp +++ b/network.cpp @@ -2060,7 +2060,7 @@ int try_to_list_and_bind(int &fd,u32_t local_ip_uint32,int port) //try to bind temp_bind_addr.sin_port = htons(port); temp_bind_addr.sin_addr.s_addr = local_ip_uint32; - if (bind(fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0) + if (::bind(fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0) { mylog(log_debug,"bind fail\n"); return -1; diff --git a/network.h b/network.h index 1cae436..c922d6b 100644 --- a/network.h +++ b/network.h @@ -50,15 +50,13 @@ struct icmphdr struct my_iphdr { -//#if __BYTE_ORDER == __LITTLE_ENDIAN +#ifdef UDP2RAW_LITTLE_ENDIAN unsigned int ihl:4; unsigned int version:4; -//#elif __BYTE_ORDER == __BIG_ENDIAN -// unsigned int version:4; -// unsigned int ihl:4; -//#else -//# error "Please fix " -//#endif +#else + unsigned int version:4; + unsigned int ihl:4; +#endif u_int8_t tos; u_int16_t tot_len; u_int16_t id; @@ -74,7 +72,7 @@ struct my_iphdr struct my_udphdr { - __extension__ union + /*__extension__*/ union { struct { @@ -96,7 +94,7 @@ struct my_udphdr struct my_tcphdr { - __extension__ union + /*__extension__*/ union { struct { @@ -104,14 +102,13 @@ struct my_tcphdr u_int16_t th_dport; /* destination port */ u_int32_t th_seq; /* sequence number */ u_int32_t th_ack; /* acknowledgement number */ -//# if __BYTE_ORDER == __LITTLE_ENDIAN +# ifdef UDP2RAW_LITTLE_ENDIAN u_int8_t th_x2:4; /* (unused) */ + u_int8_t tc_off:4; /* data offset */ +# else u_int8_t th_off:4; /* data offset */ -//# endif -//# if __BYTE_ORDER == __BIG_ENDIAN -// u_int8_t th_off:4; /* data offset */ -// u_int8_t th_x2:4; /* (unused) */ -//# endif + u_int8_t th_x2:4; /* (unused) */ +# endif u_int8_t th_flags; # define TH_FIN 0x01 # define TH_SYN 0x02 @@ -129,7 +126,7 @@ struct my_tcphdr u_int16_t dest; u_int32_t seq; u_int32_t ack_seq; -//# if __BYTE_ORDER == __LITTLE_ENDIAN +# ifdef UDP2RAW_LITTLE_ENDIAN u_int16_t res1:4; u_int16_t doff:4; u_int16_t fin:1; @@ -139,19 +136,17 @@ struct my_tcphdr u_int16_t ack:1; u_int16_t urg:1; u_int16_t res2:2; -//# elif __BYTE_ORDER == __BIG_ENDIAN -// u_int16_t doff:4; -// u_int16_t res1:4; -// u_int16_t res2:2; -// u_int16_t urg:1; -// u_int16_t ack:1; -// u_int16_t psh:1; -// u_int16_t rst:1; -// u_int16_t syn:1; -// u_int16_t fin:1; -//# else -//# error "Adjust your defines" -//# endif +# else + u_int16_t doff:4; + u_int16_t res1:4; + u_int16_t res2:2; + u_int16_t urg:1; + u_int16_t ack:1; + u_int16_t psh:1; + u_int16_t rst:1; + u_int16_t syn:1; + u_int16_t fin:1; +# endif u_int16_t window; u_int16_t check; u_int16_t urg_ptr;