Ejemplo n.º 1
0
int
main(int argc, char **argv)
{
	int					sendfd, recvfd;
	const int			on = 1;
	socklen_t			salen;
	struct sockaddr		*sasend, *sarecv;

	if (argc != 3)
		err_quit("usage: sendrecv <IP-multicast-address> <port#>");

	sendfd = Udp_client(argv[1], argv[2], (void **) &sasend, &salen);

	recvfd = Socket(sasend->sa_family, SOCK_DGRAM, 0);

	Setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

	sarecv = Malloc(salen);
	memcpy(sarecv, sasend, salen);
	Bind(recvfd, sarecv, salen);

	Mcast_join(recvfd, sasend, salen, NULL, 0);
	Mcast_set_loop(sendfd, 0);

	if (Fork() == 0)
		recv_all(recvfd, salen);		/* child -> receives */

	send_all(sendfd, sasend, salen);	/* parent -> sends */
}
Ejemplo n.º 2
0
int
main(int argc, char **argv)
{
	
	int n, i, size, prflag;
	int pos = 3;
	int maxfd;
	int rt_sockfd,recv_ping_sockfd;
	int send_mulcst_fd, recv_mulcst_fd;
	const int on = 1;
	
	char buff_recv[BUFFSIZE];
	char tour_array[TOURNUM][IPLEN];
	char *ptr;

	fd_set  rset;
	struct timeval tval, tval1, tval2, tval3, tval4;
	struct sockaddr_ll saddrll;

	Signal(SIGALRM, sig_alrm);
	//Signal(SIGCHLD, sig_chld);
	uname(&myname);
	
	if (argc == 1){
		fputs("This is NOT source node\n", stdout);
		//_is_src_node = 0;
		source_flag = 0;
	}else{
		fputs("I'm source node, start to send tour infomation \n", stdout);
		//_is_src_node = 1;
		source_flag = 1;
	}

	
	//if (uname(&myname) < 0)
		//err_sys("uname error");

	for ( hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) {

		if(strcmp(hwa->if_name,"eth0")==0){
		
			/* in eth0 we create pfsocket and bind it to hw addr, this pf socket is for sned ping info */
			//pf_sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_IP));
			send_ping_pf_sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_IP));
			//send_ping_pf_sockfd use to send ping to source node
			saddrll.sll_family      = PF_PACKET;
			saddrll.sll_ifindex     = hwa->if_index;
			saddrll.sll_protocol    = htons(ETH_P_IP); 
			bind(send_ping_pf_sockfd, (struct sockaddr *) &saddrll, sizeof(saddrll));
			
			
			printf("%s :%s", hwa->if_name, ((hwa->ip_alias) == IP_ALIAS) ? " (alias)\n" : "\n");
		
			if ( (sa = hwa->ip_addr) != NULL)
				printf("IP addr = %s\n", Sock_ntop_host(sa, sizeof(*sa)));
				
			prflag = 0;
			i = 0;
			do {
				if (hwa->if_haddr[i] != '\0') {
					prflag = 1;
					break;
				}
			} while (++i < IF_HADDR);

			if (prflag) {
				printf("         HW addr = ");
				ptr = hwa->if_haddr;
				i = IF_HADDR;
				do {
					printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":");
				} while (--i > 0);
			}
			printf("\n         Interface Index = %d\n\n", hwa->if_index);	
			break;
		}
		
	}
	
	
	/* creat two raw sockets for route travesal and receive ping*/

	rt_sockfd = Socket(AF_INET, SOCK_RAW, 254);
	setsockopt(rt_sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));
	
	recv_ping_sockfd = Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
	setuid(getuid());           /* don't need special permissions any more */
	size = 60 * 1024;           /* OK if setsockopt fails */
	setsockopt (recv_ping_sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
		
	if (source_flag == 1)
	{//this node is source 

		struct icmp *icmp;
		struct ip* ip;
		int node_num;
		
		node_num = argc + 3;
		strcpy(tour_array[0], Sock_ntop_host(sa, sizeof(*sa)));//first one is source code

		sprintf(tour_array[1],"%d",argc - 1);
		sprintf(tour_array[2],"%d",node_num);
		sprintf(tour_array[3],"%d",4);
		for (i = 4; i < node_num; i++)
			strcpy(tour_array[i], getipbyvm(argv[i-3]));

//////////////////////////before send we let the source node join the multicast

		send_mulcst_fd = Udp_client(MC_ADDR_LIN, MC_PORT_LIN, (void **) &sasend, &salen);
		recv_mulcst_fd = Socket(AF_INET, SOCK_DGRAM, 0);
		Setsockopt(recv_mulcst_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
		sarecv = Malloc(salen);
		memcpy(sarecv, sasend, salen);
	
		Bind(recv_mulcst_fd, sarecv, salen);
		Mcast_join(recv_mulcst_fd, sasend, salen, NULL, 0);
		Mcast_set_loop(send_mulcst_fd, 0);
///////////////////////////////////////////////	this is the send tour part
	
		rt_send(rt_sockfd, tour_array);
	
		for(;;){
			if(first_mulcst_flag == 1){
				
				printf("wait for first multicast info\n");
				recv_all(recv_mulcst_fd, salen);
				first_mulcst_flag = 0;
				char buf[MAXLINE];
				//sleep(1);
				snprintf(buf, sizeof(buf), "<<<<< Node: %s I am a member of the group.>>>>>\n", myname.nodename);
				send_all(send_mulcst_fd, sasend, salen, buf);
				
			}else{
				for(;;){
					recv_all(recv_mulcst_fd, salen);
					printf("Waiting for 5 seconds and exit\n");
					
				}
			}
		}
			
	
			
		
		
		
	}else{//not source node
		pthread_t tid, tid2;
		char source_name[IPLEN];
		
		for( ;; )
		{
			FD_ZERO(&rset);
			
			FD_SET(rt_sockfd, &rset);
			
			
			maxfd = rt_sockfd;
			if(rt_recved_flag == 1){
				FD_SET(recv_ping_sockfd, &rset);
				maxfd = max(recv_ping_sockfd, maxfd);
			}
			
			if (ns_first_flag == 0)
			{
				FD_SET(recv_mulcst_fd,&rset);
				//maxfd = (maxfd > recv_mulcst_fd) ? maxfd : recv_mulcst_fd;
				maxfd = max(maxfd, recv_mulcst_fd);
			}
			
			//printf("before select\n");
			
			select(maxfd + 1, &rset, NULL, NULL, NULL);
			
			if (FD_ISSET(rt_sockfd, &rset)) 
			{    
				printf("receive route travelsal paket\n");
				n = rt_recv(rt_sockfd, tour_array);
				memcpy(dest_ip_addr, tour_array[0], IPLEN);
				if (n < 0) {
					if (errno == EINTR)
						continue;
				else
					err_sys("receive tour packet error");
				}
				
			    get_vmname(tour_array[0], source_name);
				
				
				 if (ns_first_flag == 1)
				{
					ns_first_flag = 0;
					rt_recved_flag = 1;
					
					// join the multicast first
					send_mulcst_fd = Udp_client(MC_ADDR_LIN, MC_PORT_LIN, (void **) &sasend, &salen);
					recv_mulcst_fd = Socket(AF_INET, SOCK_DGRAM, 0);
					Setsockopt(recv_mulcst_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
					sarecv = Malloc(salen);
					memcpy(sarecv, sasend, salen);
					Bind(recv_mulcst_fd, sarecv, salen);
					Mcast_join(recv_mulcst_fd, sasend, salen, NULL, 0);
					Mcast_set_loop(send_mulcst_fd, 0);
					
					//create a thread for ping
					Pthread_create(&tid, NULL, &ping, NULL);
						
				
				} 
				if(last_node_flag == 0){
					rt_send(rt_sockfd, tour_array);
				}else{
					//create a thread to handle last operations
					Pthread_create(&tid2, NULL, &ls_send_mul, &send_mulcst_fd);
				}
			}
			
			if (FD_ISSET(recv_mulcst_fd, &rset)) 
			{//recv multicast info
				if (first_mulcst_flag == 1 )
				{
					first_mulcst_flag = 0;
					ping_over_flag = 1;//
					//printf("ping_over_flag is %d\n", ping_over_flag);
					recv_all(recv_mulcst_fd, salen);
					char buf[MAXLINE];
					snprintf(buf, sizeof(buf), "<<<<< Node: %s I am a member of the group.>>>>>\n", myname.nodename);
					send_all(send_mulcst_fd, sasend, salen, buf); 
					//printf("gonna go to alarm with pof changed\n");
					alarm(0);
				
				}else{
						
						for(;;){
							recv_all(recv_mulcst_fd, salen);
						}
						printf("Waiting for 5 seconds and exit\n");
					}
				
			}
			
			 if (FD_ISSET(recv_ping_sockfd, &rset)) 
			{//recv ping reply
				//printf("received ping reply\n");
				recvfrom(recv_ping_sockfd, buff_recv, MAXLINE, 0, NULL, NULL);
				Gettimeofday (&tval, NULL);
				proc_v4 (buff_recv, n, &tval);
				if (ping_over_flag == 1)
					alarm(0);
			} 	
				
		}
	}
	

}
Ejemplo n.º 3
0
int main(int argc, char **argv){
	const int  on = 1;
	unsigned char ttl = 1;
	time_t 	   rawtime;
	time_t 	   lasttime;
	int 	   maxfd;
	fd_set 	   rset;
	ssize_t	   how_much_read;
	char	   msg[MAXLINE];

	rt 			   = Socket(AF_INET, SOCK_RAW, MY_IP_PROTO);
	Setsockopt(rt, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));

	pg			   = Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
	request_sock   = Socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
	multicast_sock = Socket(AF_INET, SOCK_DGRAM, 0);
	multicast_recv = Socket(AF_INET, SOCK_DGRAM, 0);
	Mcast_set_loop(multicast_sock,0);
	Setsockopt(multicast_sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
	Setsockopt(multicast_recv, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));

	gethostname(our_hostname, sizeof(our_hostname));
	//printf("My our_hostname: %s\n", our_hostname);
	get_eth0_stuff();
	if(argc > 1)
	{
		//DEBUG("%s is the start of the tour!\n",our_hostname);

		int   i = 1;		
		tour  tour_pkt;
		
		// separate func
		bzero(&tour_pkt, sizeof(tour_pkt));

		fill_buff_with_ip_of_hostname(our_hostname);
		//printf("fill buff ok\n");
		inet_aton(ip_static_buff, &(tour_pkt.payload[0]));
		join_mcast(MULTICAST_ADDRESS, MULTICAST_PORT);
		#if 0
		// tokenize by ' ' and send each char* to gethostbyname
		char 		*token, *prev_token;
		const char  s[2] = " ";

		/* get the first token */
		token = strtok(argv[1], s);

		/* walk through other tokens */
		while(token != NULL)
		{
			DEBUG("token = %s\n", token );

			fill_buff_with_ip_of_hostname(token);
			inet_aton(ip_static_buff, &tour_pkt.payload[i]);

			prev_token  = token;
			token 		= strtok(NULL, s);
			if(!strcmp(prev_token, token)){
				perror("The same node should not appear consequentively in the tour list – i.e., the next node on the tour cannot be the current node itself");
				exit(0);
			}

			i++;
		}
		// separate func
		#endif
		while(i< argc)
		{
			//printf("argv[%d] is %s\n",i, argv[i]);
			fill_buff_with_ip_of_hostname(argv[i]);
			//printf("fill buff ok\n");
			inet_aton(ip_static_buff, &(tour_pkt.payload[i]));
			i++;
		}
		tour_pkt.index = 0; // We're the origin
		tour_pkt.total = i-1;
		//printf("tour_pkt total %d\n", tour_pkt.total);
		send_raw_tour_packet(&tour_pkt);

	}else{
		//DEBUG("We're NOT the start of the tour!");
		#if 0
		while(1)
		{
			int len = 0;
			len = Recv(rt, msg, sizeof(msg), 0);
			printf("received len %d\n",len);
			int j = 0;
			for(;j<len; j++)
			{
				printf("%.2x ", msg[j]);
				if((i-9) % 10 == 0) printf("\n");
				fflush(stdout);
			}
		}
		#endif
	}
	#if 0
	fill_buff_with_ip_of_hostname("vm2");
	inet_aton(ip_static_buff, (struct in_addr *)&dest_of_echo_req);
	printf("destofechoreq %x\n", dest_of_echo_req.s_addr);
	struct sockaddr_in IPaddr;
	struct hwaddr 	   HWaddr;
	bzero(&IPaddr, sizeof(IPaddr));
	bzero(&HWaddr, sizeof(HWaddr));
	IPaddr.sin_addr = dest_of_echo_req;
	//Areq((struct sockaddr *)&IPaddr, sizeof(struct sockaddr_in), &HWaddr);
	//printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",HWaddr.sll_addr[0],HWaddr.sll_addr[1],HWaddr.sll_addr[2],
	//			HWaddr.sll_addr[3],HWaddr.sll_addr[4],HWaddr.sll_addr[5]);
	void sig_alrm(int signo);
	if(argc > 1){
	Signal(SIGALRM, sig_alrm);
	sig_alrm(SIGALRM);
	}
	//send_icmp_request(HWaddr.sll_addr);
	int n  = 0;
	struct timeval tval;
	
	while(1)
	{
		n = recv(pg, msg, sizeof(msg), 0);
		if(n<0)
		{
			if (errno == EINTR)
				continue;
		}
		printf("receive ping packet\n");
		//int j = 0;
		//for (; j < n; j++)
		//{
		//	printf("%.2x ", msg[j]);
		//	if((j-9) % 10 == 0) printf("\n");
		//	fflush(stdout);
		//}
		Gettimeofday(&tval, NULL);
		proc_v4(msg,n,NULL, &tval);
	}
	#endif
	#if 1
    //act_open.sa_flags = 0;
    //sigemptyset(&act_open.sa_mask);
    //sigaddset(&act_open.sa_mask, SIGALRM);
    //act_open.sa_handler = terminate;
	Signal(SIGALRM, sig_alrm);
	FD_ZERO(&rset);
	maxfd = max(pg, multicast_recv);
	maxfd = max(rt, maxfd);

	while(1){
		FD_SET(multicast_recv, &rset);
		FD_SET(rt, &rset);
		FD_SET(pg, &rset);

		if(select(maxfd+1, &rset, NULL, NULL, NULL)<0)
		{
			if(errno == EINTR)
			{
				continue;
			}
		}

		if(FD_ISSET(rt, &rset)){
			//DEBUG("possibly totally wrong here\n");
			//DEBUG("possibly totally wrong here\n");
			//DEBUG("possibly totally wrong here\n");
			alarm(0);
			bzero(msg, sizeof(msg));
			how_much_read = Recv(rt, msg, sizeof(msg), 0);
		

			//assert(how_much_read == sizeof(sizeof(my_ip_header)+sizeof(tour)));
			//printf("rt receive pkt len %d\n", how_much_read);
			my_ip_header  *head     =  (my_ip_header *)msg;
			tour 		  *tour_pkt =  (tour *)(msg+sizeof(my_ip_header));
			
			if(ntohs(head->id) != MY_IP_ID){
				//DEBUG("head->id != MY_IP_ID\n");
				continue;
			}
			char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&head->saddr));
			time(&rawtime);
			printf("<%s> recieved source routing packet from <%s>\n", ctime (&rawtime), name);
			
			if(!already_here){
				already_here = 1;
				// IP_MULTICAST_TTL - If not otherwise specified, has a default value of 1.
				join_mcast(inet_ntoa(tour_pkt->multicast_addr), ntohs(tour_pkt->multicast_port));
				stop_pinging = 0;
			}
				// start icmp echo reqs
			for(i = 0; i < prev_sender_list.index; i++)
			{
				if(!memcmp(&prev_sender_list.previous_senders[i], &tour_pkt->payload[(tour_pkt->index)], sizeof(struct in_addr)))
					we_dont_need_to_send_echo_req = 1;
				else{
					we_dont_need_to_send_echo_req = 0;
					break;
				}
			}

			if(!we_dont_need_to_send_echo_req){
				//DEBUG("We Need To Send Echo Request\n");

				prev_sender_list.previous_senders[prev_sender_list.index] = tour_pkt->payload[(tour_pkt->index)];
				prev_sender_list.index++;

				dest_of_echo_req = tour_pkt->payload[(tour_pkt->index)];

				char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&tour_pkt->payload[(tour_pkt->index)]));
				printf("PING %s (%s): %d data bytes\n",name,inet_ntoa(dest_of_echo_req), icmpdata);
				sig_alrm(SIGALRM);
			}
			else
			{
				char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&tour_pkt->payload[(tour_pkt->index)]));
				printf("We have ping %s before\n",name);
			}
			

			// INCREMENTING THE INDEX
			tour_pkt->index = (tour_pkt->index)+1;
			if((tour_pkt->index) == MAX_VISITS){
				perror("MAX_VISITS has been reached.");
				exit(0);
			}

			// Now blank
			//bzero(&for_comparisons, sizeof(for_comparisons));
			// ghiTODO this may not work ghiTODO
			//if(!memcmp(&tour_pkt->payload[ntohl(tour_pkt->index)], &for_comparisons, sizeof(for_comparisons))){
			//printf("rt current pkt index %d, total index %d\n",tour_pkt->index, tour_pkt->total);
			if(tour_pkt->index == tour_pkt->total)
			{
				//printf("We're the final destination.\n");
				// ping the saddr
				final_destination = 1;
				char buffer[100];
				snprintf(buffer, 100, "<<<<< This is node %s.  Tour has ended.  Group members please identify yourselves. >>>>>",our_hostname);
				printf("Node %s.  Sending: <%s>.  ",our_hostname, buffer);
				send_multicast_message(buffer,strlen(buffer)+1);
				//printf("send mulitcast msg ok\n");

			}else{
				//printf("We're NOT the final destination, keep touring.\n");
				// pass it along
				send_raw_tour_packet(tour_pkt);
			}


		}else if(FD_ISSET(multicast_recv, &rset)){
			//DEBUG("Receiving multicast message.\n");
			printf("");
			bzero(msg, sizeof(msg));
			how_much_read = Recv(multicast_recv, msg, sizeof(msg), 0);
			//printf("multicast msg len %d\n", how_much_read);
			msg[how_much_read]='\0';
			if(!first_term){
				printf("Node %s.  Received <%s>.  ",our_hostname,msg);
				// then should immediately stop ping 
				//DEBUG("how_much_read is %lu\n", how_much_read);
				stop_pinging = 1;
				alarm(0);
				bzero(msg, sizeof(msg));
				snprintf(msg, sizeof(msg), "<<<<< Node %s I am a member of the group. >>>>>", our_hostname);
				printf("Node %s. Sending <%s>.  ", our_hostname, msg);
				fflush(stdout);
				first_term = 1;
				send_multicast_message(msg,strlen(msg)+1);
			}
			else
			{
				Signal(SIGALRM, terminate);
				alarm(5);
				printf("");
				printf("Node %s.  Received <%s>.  ",our_hostname,msg);
				fflush(stdout);
			}

			//send_multicast_message(msg);
		}else if(FD_ISSET(pg, &rset)){
			//DEBUG("Receiving echo reply message.\n");
			struct timeval tval;
			bzero(msg, sizeof(msg));
			how_much_read = Recv(pg, msg, sizeof(msg), 0);
			Gettimeofday(&tval, NULL);
			proc_v4(msg,how_much_read,NULL, &tval);
			//assert(how_much_read == sizeof(sizeof(my_eth_header)+sizeof(my_ip_header)+sizeof(my_icmp_header)));
			//DEBUG("how_much_read is %lu\n", how_much_read);
			//DEBUG("Node %s recieved %s\n", our_hostname, msg);
			// my_eth_header   *eh    =  (my_eth_header  *)msg;
			// my_ip_header    *iph   =  (my_ip_header   *)msg+sizeof(my_eth_header);
			// my_icmp_header  *icmph =  (my_icmp_header *)msg+sizeof(my_eth_header)+sizeof(my_ip_header);
			//my_ip_header *iph = (my_ip_header *)(msg+sizeof(my_eth_header));
			//if(!memcmp(&iph->saddr, &dest_of_echo_req, sizeof(dest_of_echo_req))){
			//	DEBUG("ghiTODO\n");
			//	count_of_replies++;
			//	DEBUG("ghiTODO\n");
				//DEBUG("ghiTODO\n");
				//DEBUG("ghiTODO\n");
			//	if(final_destination && count_of_replies >= 5){
			//		stop_pinging = 1;
			//		bzero(msg, sizeof(msg));
			///		snprintf(msg, sizeof(msg), "<<<<< This is node %s Tour has ended. Group members please identify yourselves.>>>>>", our_hostname);
			//		printf("Node %s sending %s", our_hostname, msg);
			//		send_multicast_message(msg, strlen(msg)+1);
			//		alarm(5);
			//	}
			//}else{
				//DEBUG("The following is FALSE !memcmp(&iph->saddr, &dest_of_echo_req, sizeof(dest_of_echo_req))");
			//}
		}
		#if 0
		time(&rawtime);
		if(lasttime+1 <= rawtime){
			if(!stop_pinging){	
				struct sockaddr_in IPaddr;
				struct hwaddr 	   HWaddr;
				bzero(&IPaddr, sizeof(IPaddr));
				bzero(&HWaddr, sizeof(HWaddr));
				IPaddr.sin_addr = dest_of_echo_req;
				Areq((struct sockaddr *)&IPaddr, sizeof(struct sockaddr_in), &HWaddr);
				send_raw_echo_request_message(HWaddr.sll_addr);
			}

		}else{
			lasttime = rawtime;
		}
		#endif

	}
	#endif
}