Explorar o código

add libnet and libpcap

wangyu- %!s(int64=7) %!d(string=hai) anos
pai
achega
8a1d5b58b1
Modificáronse 6 ficheiros con 28 adicións e 10 borrados
  1. 3 0
      common.h
  2. 7 2
      main.cpp
  3. 1 1
      makefile
  4. 6 0
      misc.cpp
  5. 8 6
      network.cpp
  6. 3 1
      network.h

+ 3 - 0
common.h

@@ -47,6 +47,9 @@
 #include <linux/if_packet.h>
 #include <byteswap.h>
 #include <pthread.h>
+#include <pcap.h>
+#include <libnet.h>
+
 
 #include <my_ev.h>
 

+ 7 - 2
main.cpp

@@ -168,7 +168,7 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
 
 				send_handshake(raw_info,conn_info.my_id,0,const_id);
 				if(raw_mode==mode_icmp)
-					send_info.icmp_seq++;
+					send_info.my_icmp_seq++;
 			}
 
 			conn_info.last_hb_sent_time=get_current_time();
@@ -209,7 +209,7 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
 
 				send_handshake(raw_info,conn_info.my_id,conn_info.oppsite_id,const_id);
 				if(raw_mode==mode_icmp)
-					send_info.icmp_seq++;
+					send_info.my_icmp_seq++;
 			}
 			conn_info.last_hb_sent_time=get_current_time();
 			mylog(log_info,"(re)sent handshake2\n");
@@ -768,6 +768,11 @@ int client_event_loop()
 
 int main(int argc, char *argv[])
 {
+	libnet_t *l;	/* the libnet context */
+	char errbuf[LIBNET_ERRBUF_SIZE];
+
+	l = libnet_init(LIBNET_RAW4, NULL, errbuf);
+
 	dup2(1, 2);//redirect stderr to stdout
 	signal(SIGINT, signal_handler);
 	signal(SIGHUP, signal_handler);

+ 1 - 1
makefile

@@ -10,7 +10,7 @@ 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
+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` 
 
 SOURCES= $(COMMON) lib/aes_faster_c/aes.c lib/aes_faster_c/wrapper.c
 SOURCES_TINY_AES= $(COMMON) lib/aes.c

+ 6 - 0
misc.cpp

@@ -279,6 +279,7 @@ void process_arg(int argc, char *argv[])  //process all options
 		{"max-rst-to-show", required_argument,    0, 1},
 		{"max-rst-allowed", required_argument,    0, 1},
 		{"set-ttl", required_argument,    0, 1},
+		{"dev", required_argument,    0, 1},
 		{"dns-resolve", no_argument,    0, 1},
 		{NULL, 0, 0, 0}
 	  };
@@ -666,6 +667,11 @@ void process_arg(int argc, char *argv[])  //process all options
 				enable_dns_resolve=1;
 				mylog(log_info,"dns-resolve enabled\n");
 			}
+			else if(strcmp(long_options[option_index].name,"dev")==0)  // currently not used
+			{
+				sscanf(optarg,"%s",dev);
+				mylog(log_info,"dev=[%s]\n",dev);
+			}
 			else
 			{
 				mylog(log_warn,"ignored unknown long option ,option_index:%d code:<%x>\n",option_index, optopt);

+ 8 - 6
network.cpp

@@ -28,6 +28,8 @@ int lower_level_manual=0;
 int ifindex=-1;
 char if_name[100]="";
 
+char dev[100]="";
+
 unsigned short g_ip_id_counter=0;
 
 unsigned char dest_hw_addr[sizeof(sockaddr_ll::sll_addr)]=
@@ -164,7 +166,7 @@ packet_info_t::packet_info_t()
 	else if (raw_mode == mode_icmp)
 	{
 		protocol = IPPROTO_ICMP;
-		icmp_seq=0;
+		my_icmp_seq=0;
 	}
 
 }
@@ -815,7 +817,7 @@ int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
 	icmph->id=htons(send_info.src_port);
 
 
-	icmph->seq=htons(send_info.icmp_seq);   /////////////modify
+	icmph->seq=htons(send_info.my_icmp_seq);   /////////////modify
 
 	memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
 
@@ -1205,7 +1207,7 @@ int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
 	}
 
 	recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
-	recv_info.icmp_seq=ntohs(icmph->seq);
+	recv_info.my_icmp_seq=ntohs(icmph->seq);
 
 
 	if(program_mode==client_mode)
@@ -1754,7 +1756,7 @@ int after_send_raw0(raw_info_t &raw_info)
 	{
 		if(program_mode==client_mode)
 		{
-			send_info.icmp_seq++;
+			send_info.my_icmp_seq++;
 		}
 	}
 	return 0;
@@ -1789,8 +1791,8 @@ int after_recv_raw0(raw_info_t &raw_info)
 	{
 		if(program_mode==server_mode)
 		{
-			if(larger_than_u16(recv_info.icmp_seq,send_info.icmp_seq))
-				send_info.icmp_seq = recv_info.icmp_seq;  //TODO only update if its larger
+			if(larger_than_u16(recv_info.my_icmp_seq,send_info.my_icmp_seq))
+				send_info.my_icmp_seq = recv_info.my_icmp_seq;  //TODO only update if its larger
 		}
 	}
 	return 0;

+ 3 - 1
network.h

@@ -21,6 +21,8 @@ extern int lower_level_manual;
 extern char if_name[100];
 extern unsigned char dest_hw_addr[];
 
+extern char dev[100];
+
 extern int random_drop;
 
 extern int ifindex;
@@ -62,7 +64,7 @@ struct packet_info_t  //todo change this to union
 	u32_t ts,ts_ack;
 
 
-	uint16_t icmp_seq;
+	uint16_t my_icmp_seq;
 
 	bool has_ts;