Ver código fonte

changed to custom header definitons

wangyu- 7 anos atrás
pai
commit
10869eb197
4 arquivos alterados com 182 adições e 48 exclusões
  1. 37 4
      common.h
  2. 5 9
      misc.cpp
  3. 30 30
      network.cpp
  4. 110 5
      network.h

+ 37 - 4
common.h

@@ -25,10 +25,10 @@
 #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/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/ip.h>    //Provides declarations for ip header
+//#include <netinet/ip6.h>
 #include <netinet/if_ether.h>
 #include <arpa/inet.h>
 #include <fcntl.h>
@@ -60,6 +60,37 @@
 using  namespace std;
 
 
+#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
+    defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
+    defined(__BIG_ENDIAN__) || \
+    defined(__ARMEB__) || \
+    defined(__THUMBEB__) || \
+    defined(__AARCH64EB__) || \
+    defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
+#define UDP2RAW_BIG_ENDIAN 1
+#endif
+
+
+#if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
+    defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
+    defined(__LITTLE_ENDIAN__) || \
+    defined(__ARMEL__) || \
+    defined(__THUMBEL__) || \
+    defined(__AARCH64EL__) || \
+    defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
+#define UDP2RAW_LITTLE_ENDIAN 1
+#endif
+
+#if defined(UDP2RAW_BIG_ENDIAN) &&defined(UDP2RAW_LITTLE_ENDIAN)
+#error "endian detection conflicts"
+#endif
+
+
+#if !defined(UDP2RAW_BIG_ENDIAN) && !defined(UDP2RAW_LITTLE_ENDIAN)
+#error "endian detection failed"
+#endif
+
+
 typedef unsigned long long u64_t;   //this works on most platform,avoid using the PRId64
 typedef long long i64_t;
 
@@ -276,7 +307,7 @@ struct not_copy_able_t
 const int max_data_len=1800;
 const int buf_len=max_data_len+400;
 
-const int max_address_len=512;
+//const int max_address_len=512;
 
 u64_t get_current_time();
 u64_t pack_u64(u32_t a,u32_t b);
@@ -431,4 +462,6 @@ struct lru_collector_t:not_copy_able_t
 };
 
 
+
+
 #endif /* COMMON_H_ */

+ 5 - 9
misc.cpp

@@ -743,15 +743,11 @@ void process_arg(int argc, char *argv[])  //process all options
 	 log_bare(log_info,"local_addr=%s ",local_addr.get_str());
 	 log_bare(log_info,"remote_addr=%s ",remote_addr.get_str());
 
-	 if(force_source_ip||force_source_port)
-		 log_bare(log_info,"source_addr=%s ",source_addr.get_str());
-
-	 /*log_bare(log_info,"local_ip=%s ",local_ip);
-	 log_bare(log_info,"local_port=%d ",local_port);
-	 log_bare(log_info,"remote_address=%s ",remote_address);
-	 log_bare(log_info,"remote_port=%d ",remote_port);
-	 log_bare(log_info,"source_ip=%s ",source_ip);
-	 log_bare(log_info,"source_port=%d ",source_port);*/
+	 if(force_source_ip)
+		 log_bare(log_info,"source_addr=%s ",source_addr.get_ip());
+	 
+	 if(force_source_port)
+		 log_bare(log_info,"source_port=%d ",source_port);
 
 	 log_bare(log_info,"socket_buf_size=%d ",socket_buf_size);
 

+ 30 - 30
network.cpp

@@ -765,10 +765,10 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
     uint16_t ip_tot_len;
     if(raw_ip_version==AF_INET)
     {
-    	struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
-        memset(iph,0,sizeof(iphdr));
+    	struct my_iphdr *iph = (struct my_iphdr *) send_raw_ip_buf;
+        memset(iph,0,sizeof(my_iphdr));
 
-        iph->ihl = sizeof(iphdr)/4;  //we dont use ip options,so the length is just sizeof(iphdr)
+        iph->ihl = sizeof(my_iphdr)/4;  //we dont use ip options,so the length is just sizeof(iphdr)
         iph->version = 4;
         iph->tos = 0;
 
@@ -791,12 +791,12 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
 		iph->saddr = send_info.new_src_ip.v4;    //Spoof the source ip address
 		iph->daddr = send_info.new_dst_ip.v4;
 
-		ip_tot_len=sizeof (struct iphdr)+payloadlen;
+		ip_tot_len=sizeof (struct my_iphdr)+payloadlen;
 		if(lower_level)iph->tot_len = htons(ip_tot_len);            //this is not necessary ,kernel will always auto fill this  //http://man7.org/linux/man-pages/man7/raw.7.html
 		else
 			iph->tot_len = 0;
 
-		memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
+		memcpy(send_raw_ip_buf+sizeof(my_iphdr) , payload, payloadlen);
 
 		if(lower_level) iph->check =
 				csum ((unsigned short *) send_raw_ip_buf, iph->ihl*4); //this is not necessary ,kernel will always auto fill this
@@ -895,7 +895,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
 	packet_info_t &recv_info=raw_info.recv_info;
 
 
-	iphdr *  iph;
+	my_iphdr *  iph;
 	my_ip6hdr * ip6h;
 	int flag=0;
 	//int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len+1, flag ,(sockaddr*)&saddr , &saddr_size);
@@ -908,7 +908,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
 		mylog(log_trace,"raw_packet_len <1, dropped\n");
 		return -1;
 	}
-	iph = (struct iphdr *) (ip_begin);
+	iph = (struct my_iphdr *) (ip_begin);
 	ip6h= (struct my_ip6hdr *) (ip_begin);
 	if(raw_ip_version==AF_INET)
 	{
@@ -917,7 +917,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
 			mylog(log_trace,"expect ipv4 packet, but got something else: %02x\n",iph->version);
 			return -1;
 		}
-		if(raw_packet_len<(int)sizeof(iphdr))
+		if(raw_packet_len<(int)sizeof(my_iphdr))
 		{
 			mylog(log_trace,"raw_packet_len<sizeof(iphdr)\n");
 			return -1;
@@ -1054,8 +1054,8 @@ int peek_raw(raw_info_t &raw_info)
     			mylog(log_trace,"failed here");
     			return -1;
     		}
-    		struct tcphdr *tcph=(tcphdr *)payload;
-    		if(payload_len<int(sizeof(tcphdr) ))
+    		struct my_tcphdr *tcph=(my_tcphdr *)payload;
+    		if(payload_len<int(sizeof(my_tcphdr) ))
     		{
     			mylog(log_trace,"failed here");
     			return -1;
@@ -1067,8 +1067,8 @@ int peek_raw(raw_info_t &raw_info)
     	case mode_udp:
     	{
     		if(recv_info.protocol!=IPPROTO_UDP) return -1;
-    		struct udphdr *udph=(udphdr *)payload;
-    		if(payload_len<int(sizeof(udphdr)))
+    		struct my_udphdr *udph=(my_udphdr *)payload;
+    		if(payload_len<int(sizeof(my_udphdr)))
     			return -1;
     		recv_info.src_port=ntohs(udph->source);
 			break;
@@ -1082,8 +1082,8 @@ int peek_raw(raw_info_t &raw_info)
     		{
     			if(recv_info.protocol!=IPPROTO_ICMPV6) return -1;
     		}
-    		struct icmphdr *icmph=(icmphdr *)payload;
-    		if(payload_len<int(sizeof(udphdr)))
+    		struct my_icmphdr *icmph=(my_icmphdr *)payload;
+    		if(payload_len<int(sizeof(my_udphdr)))
     			return -1;
     		recv_info.src_port=ntohs(icmph->id);
 			break;
@@ -1098,8 +1098,8 @@ int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
 	const packet_info_t &recv_info=raw_info.recv_info;
 
 	char send_raw_icmp_buf[buf_len];
-	icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
-	memset(icmph,0,sizeof(icmphdr));
+	my_icmphdr *icmph=(struct my_icmphdr *) (send_raw_icmp_buf);
+	memset(icmph,0,sizeof(my_icmphdr));
 	if(raw_ip_version==AF_INET)
 	{
 		if(program_mode==client_mode)
@@ -1127,11 +1127,11 @@ int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
 
 	icmph->seq=htons(send_info.icmp_seq);   /////////////modify
 
-	memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
+	memcpy(send_raw_icmp_buf+sizeof(my_icmphdr),payload,payloadlen);
 
 	if(raw_ip_version==AF_INET)
 	{
-		icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
+		icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(my_icmphdr)+payloadlen);
 	}
 	else
 	{
@@ -1141,13 +1141,13 @@ int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
 		psh->src=send_info.new_src_ip.v6;
 		psh->dst=send_info.new_dst_ip.v6;
 		psh->next_header=IPPROTO_ICMPV6;
-		psh->tcp_length=htons(sizeof(icmphdr)+payloadlen);
+		psh->tcp_length=htons(sizeof(my_icmphdr)+payloadlen);
 		psh->placeholder1 = 0;
 		psh->placeholder2 = 0;
 
-		icmph->check_sum = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
+		icmph->check_sum = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_icmp_buf, sizeof(my_icmphdr)+payloadlen);
 	}
-	if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
+	if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(my_icmphdr)+payloadlen)!=0)
 	{
 		return -1;
 	}
@@ -1239,9 +1239,9 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) {
 	char send_raw_tcp_buf[buf_len];
 	//char *send_raw_tcp_buf=send_raw_tcp_buf0;
 
-	struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf);
+	struct my_tcphdr *tcph = (struct my_tcphdr *) (send_raw_tcp_buf);
 
-	memset(tcph,0,sizeof(tcphdr));
+	memset(tcph,0,sizeof(my_tcphdr));
 
 	//TCP Header
 	tcph->source = htons(send_info.src_port);
@@ -1258,7 +1258,7 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) {
 
 	if (tcph->syn == 1) {
 		tcph->doff = 10;  //tcp header size
-		int i = sizeof(tcphdr);
+		int i = sizeof(my_tcphdr);
 		send_raw_tcp_buf[i++] = 0x02;  //mss
 		send_raw_tcp_buf[i++] = 0x04;
 		send_raw_tcp_buf[i++] = 0x05;
@@ -1294,7 +1294,7 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) {
 		send_raw_tcp_buf[i++] = wscale;
 	} else {
 		tcph->doff = 8;
-		int i = sizeof(tcphdr);
+		int i = sizeof(my_tcphdr);
 
 		send_raw_tcp_buf[i++] = 0x01;
 		send_raw_tcp_buf[i++] = 0x01;
@@ -1570,7 +1570,7 @@ int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
 	}
 
 
-	icmphdr *icmph=(struct icmphdr *) (ip_payload);
+	my_icmphdr *icmph=(struct my_icmphdr *) (ip_payload);
 
 	if(ntohs(icmph->id)!=send_info.src_port)
 	{
@@ -1639,8 +1639,8 @@ int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
 		//mylog(log_info,"send_info.seq=%d\n",send_info.seq);
 	}*/
 
-	payload=ip_payload+sizeof(icmphdr);
-	payloadlen=ip_payloadlen-sizeof(icmphdr);
+	payload=ip_payload+sizeof(my_icmphdr);
+	payloadlen=ip_payloadlen-sizeof(my_icmphdr);
 	mylog(log_trace,"get a packet len=%d\n",payloadlen);
 
     return 0;
@@ -1844,7 +1844,7 @@ int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
 	}
 
 
-	tcphdr * tcph=(struct tcphdr*)ip_payload;
+	my_tcphdr * tcph=(struct my_tcphdr*)ip_payload;
 
     unsigned short tcphdrlen = tcph->doff*4;
 
@@ -1913,7 +1913,7 @@ int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
 
     char *tcp_begin=ip_payload;  //ip packet's data part
 
-    char *tcp_option=ip_payload+sizeof(tcphdr);
+    char *tcp_option=ip_payload+sizeof(my_tcphdr);
     char *option_end=ip_payload+tcphdrlen;
 
     /*

+ 110 - 5
network.h

@@ -30,20 +30,125 @@ extern char g_packet_buf[buf_len];
 extern int g_packet_buf_len;
 extern int g_packet_buf_cnt;
 
+
+struct my_iphdr
+  {
+#ifdef UDP2RAW_LITTLE_ENDIAN
+    unsigned char ihl:4;
+    unsigned char version:4;
+#else
+    unsigned char version:4;
+    unsigned char ihl:4;
+#endif
+    u_int8_t tos;
+    u_int16_t tot_len;
+    u_int16_t id;
+    u_int16_t frag_off;
+    u_int8_t ttl;
+    u_int8_t protocol;
+    u_int16_t check;
+    u_int32_t saddr;
+    u_int32_t daddr;
+    /*The options start here. */
+  };
+
+
+struct my_udphdr
+{
+  /*__extension__*/ union
+  {
+    struct
+    {
+      u_int16_t uh_sport;		/* source port */
+      u_int16_t uh_dport;		/* destination port */
+      u_int16_t uh_ulen;		/* udp length */
+      u_int16_t uh_sum;		/* udp checksum */
+    };
+    struct
+    {
+      u_int16_t source;
+      u_int16_t dest;
+      u_int16_t len;
+      u_int16_t check;
+    };
+  };
+};
+
+
+struct my_tcphdr
+  {
+    /*__extension__*/ union
+    {
+      struct
+      {
+	u_int16_t th_sport;		/* source port */
+	u_int16_t th_dport;		/* destination port */
+	u_int32_t th_seq;		/* sequence number */
+	u_int32_t th_ack;		/* acknowledgement number */
+# 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 */
+	u_int8_t th_x2:4;		/* (unused) */
+# endif
+	u_int8_t th_flags;
+# define TH_FIN	0x01
+# define TH_SYN	0x02
+# define TH_RST	0x04
+# define TH_PUSH	0x08
+# define TH_ACK	0x10
+# define TH_URG	0x20
+	u_int16_t th_win;		/* window */
+	u_int16_t th_sum;		/* checksum */
+	u_int16_t th_urp;		/* urgent pointer */
+      };
+      struct
+      {
+	u_int16_t source;
+	u_int16_t dest;
+	u_int32_t seq;
+	u_int32_t ack_seq;
+# ifdef UDP2RAW_LITTLE_ENDIAN
+	u_int16_t res1:4;
+	u_int16_t doff:4;
+	u_int16_t fin:1;
+	u_int16_t syn:1;
+	u_int16_t rst:1;
+	u_int16_t psh:1;
+	u_int16_t ack:1;
+	u_int16_t urg:1;
+	u_int16_t res2:2;
+# 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;
+      };
+    };
+};
+
 struct my_ip6hdr
   {
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+# ifdef UDP2RAW_LITTLE_ENDIAN
     uint8_t traffic_class_high:4;
     uint8_t version:4;
     uint8_t flow_label_high:4;
     uint8_t traffic_class_low:4;
-#elif __BYTE_ORDER == __BIG_ENDIAN
+#else
     uint8_t version:4;
     uint8_t traffic_class_high:4;
     uint8_t traffic_class_low:4;
     uint8_t flow_label_high:4;
-#else
-# error	"Please fix this"
 #endif
     u_int16_t flow_label_low;
     u_int16_t payload_len;
@@ -54,7 +159,7 @@ struct my_ip6hdr
     struct in6_addr dst;
   };
 
-struct icmphdr
+struct my_icmphdr
 {
 	uint8_t type;
 	uint8_t code;