浏览代码

sync more updates from mp

wangyu- 7 年之前
父节点
当前提交
0980d89072
共有 3 个文件被更改,包括 92 次插入49 次删除
  1. 17 16
      common.cpp
  2. 67 25
      common.h
  3. 8 8
      network.cpp

+ 17 - 16
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)

+ 67 - 25
common.h

@@ -17,37 +17,62 @@
 
 #include<unistd.h>
 #include<errno.h>
-#include <sys/epoll.h>
-#include <sys/wait.h>
-#include <sys/socket.h>    //for socket ofcourse
-#include <sys/types.h>
+//#include <sys/epoll.h>
+//#include <sys/wait.h>
+
 #include <sys/stat.h>
 #include <stdlib.h> //for exit(0);
 #include <errno.h> //For errno - the error number
-#include <netdb.h> // for gethostbyname()
-//#include <netinet/tcp.h>   //Provides declarations for tcp header
-#include <netinet/udp.h>
-//#include <netinet/ip.h>    //Provides declarations for ip header
-//#include <netinet/ip6.h>
-#include <netinet/if_ether.h>
-#include <arpa/inet.h>
 #include <fcntl.h>
-#include <byteswap.h>
-#include <arpa/inet.h>
-#include <linux/if_ether.h>
-#include <linux/filter.h>
 #include <sys/time.h>
 #include <time.h>
+
+#include <stdarg.h>
+#include <assert.h>
+
+
+#include <pthread.h>
+
+#if defined(UDP2RAW_MP)
+
+#if !defined(__CYGWIN__) && !defined(__MINGW32__)
+#include <pcap.h>
+#else
+#include <pcap_wrapper.h>
+#define NO_LIBNET
+#endif
+#ifndef NO_LIBNET
+#include <libnet.h>
+#endif
+#include <my_ev.h>
+
+#else
+
+#include <linux/if_ether.h>
+#include <linux/filter.h>
+#include <linux/if_packet.h>
+#include <sys/epoll.h>
+#include <sys/wait.h>
+#include <netinet/if_ether.h>
+#include <net/if.h>
 #include <sys/timerfd.h>
+
+#endif
+
+#if defined(__MINGW32__)
+#include <winsock2.h>
+typedef unsigned char u_int8_t;
+typedef unsigned short u_int16_t;
+typedef unsigned int u_int32_t;
+typedef int socklen_t;
+#else
+#include <sys/socket.h>
+#include <sys/types.h>
 #include <sys/ioctl.h>
 #include <netinet/in.h>
-#include <net/if.h>
 #include <arpa/inet.h>
-#include <stdarg.h>
-#include <assert.h>
-#include <linux/if_packet.h>
-#include <byteswap.h>
-#include <pthread.h>
+#endif
+
 
 #include<unordered_map>
 #include <fstream>
@@ -56,7 +81,6 @@
 #include <map>
 #include <set>
 #include <list>
-#include <type_traits>
 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;
 

+ 8 - 8
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_payloadlen<int( sizeof(udphdr) ))
+	if(ip_payloadlen<int( sizeof(my_udphdr) ))
 	{
 		mylog(log_debug,"too short to hold udpheader\n");
 		return -1;
 	}
-	udphdr *udph=(struct udphdr*)ip_payload;
+	my_udphdr *udph=(struct my_udphdr*)ip_payload;
 
 	if(int(ntohs(udph->len))!=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;
 }