Exemple #1
0
/*
	Retrieve Mac address from interface index.
*/
unsigned char * retrieveMacFromInterfaceIndex( int interface_index )
{
	unsigned char source_mac[6];
	struct hwa_info	*hwa, *hwahead;
	struct sockaddr_in * ip_address_structure;
	int k,i;
	char   *ptr,*ptr1;
	
	/* Flood with broadcast address on all interfaces except eth0 and lo and recieved interface */
	for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) 
	{
		if( hwa->if_index == interface_index )
		{	

			ptr = hwa->if_haddr;
			ptr1 = hwa->if_haddr;

			i = IF_HADDR;
			k=0;
			do 
			{	
				source_mac[k] = *ptr++ & 0xff;
				k++;
				//printf("%.2x%s", *ptr1++ & 0xff, (i == 1) ? " " : ":");
			} while (--i > 0);

		
		}
	}

	return source_mac;
}
Exemple #2
0
void server_setup(void) {
  struct hwa_info *h;
  struct hwa_info *h_head;
  struct sockaddr *sa;

  h_head = Get_hw_addrs();
  for (h = h_head; h != NULL; h = h->hwa_next) {
    if (!strcmp(h->if_name, "eth0") && h->ip_addr != NULL) {
      sa = h->ip_addr;
      strcpy(my_ipaddr, (char *)Sock_ntop_host(sa, sizeof(*sa)));
      my_hostname[0] = '\0';
      ip_address_to_hostname(my_ipaddr, my_hostname);
      INFO("My IP Address: %s & hostname: %s\n", my_ipaddr, my_hostname);
      break;
    }
  }
}
Exemple #3
0
void createIfList()
{
	struct hwa_info	*hwa, *hwahead;
	struct sockaddr	*sa;
	char   *ptr, ipaddr[16],hwaddr[18],tmp[4];
	int    i, prflag, ifindex;

	for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) 
    {
        if(strcmp(hwa->if_name,"eth0") != 0)
            continue;
		
		if ( (sa = hwa->ip_addr) != NULL)
            strcpy(ipaddr,(char *)Sock_ntop_host(sa, sizeof(*sa)));
				
		prflag = 0;
		i = 0;
		do {
			if (hwa->if_haddr[i] != '\0') {
				prflag = 1;
				break;
			}
		} while (++i < IF_HADDR);

        strcpy(hwaddr,"");
		if (prflag) {
			ptr = hwa->if_haddr;
			i = IF_HADDR;
			do {
                int j;
				sprintf(tmp,"%.2x%s", *ptr++ & 0xff, (i == 1) ? "" : ":");
                if(i == 1)
                    strncat(hwaddr,tmp,2);
                else
                    strncat(hwaddr,tmp,3);
			} while (--i > 0);
		}

        hwaddr[18] = 0;
		ifindex = hwa->if_index;
		addToIfList(ipaddr, ifindex, hwaddr);
	}

	free_hwa_info(hwahead);
    return;
}
Exemple #4
0
void get_eth0_stuff(){
	struct hwa_info	*hwa,*hwahead;
	struct sockaddr	*sa;

	for(hwahead= hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next){
		if(!strncmp(hwa->if_name, ETH_0_NAME, strlen(ETH_0_NAME))){
			if((sa = hwa->ip_addr) != NULL){	
				memcpy(eth0_mac, hwa->if_haddr,ETH_ALEN);
			}else{
				//DEBUG("\n(sa = hwa->ip_addr) is NULL\n");
			}
			eth0_if = hwa->if_index;
		}
	}
	free_hwa_info(hwahead);
	//printf("local_mac: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",eth0_mac[0],eth0_mac[1],eth0_mac[2],
				//eth0_mac[3],eth0_mac[4],eth0_mac[5]);
	//printf("local if_index %d\n", eth0_if);
}
int
main (int argc, char *argv[])
{
	struct hwa_info	*hwa, *hwahead;
	struct sockaddr	*sa;
	char   *ptr;
	int    i, prflag;

	printf("\n");

	for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) {
		
		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);
	}

	free_hwa_info(hwahead);
	exit(0);
}
Exemple #6
0
void floodARP( int pf_socket, char * target_ip_address)
{
	struct hwa_info	*hwa, *hwahead;
	char * ifname_split;
	unsigned char flood_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
	struct sockaddr	*sa;
	struct arp_frame * populated_arp;
//	printf("inside flood arp\n");
	populated_arp =  create_arp(target_ip_address, 1);
//		printf("populated_arp filled\n");
	//printf("At floodRREQ, size of frame was : %d\n",sizeof(*populated_odr_frame)  );
	/* Flood with broadcast address on all interfaces except eth0 and lo and recieved interface */

		printf("**************************************************************\n");
    	printf("FLOODING ARP REQUEST:\n");
    	printf("**************************************************************\n");
	for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) 
	{
		//printf("get hw addrss loop\n");
		ifname_split = strtok(hwa->if_name, ":"); //get pointer to first token found and store in 0
	
		
		if( strcmp(ifname_split, "eth0")==0)
		{	
			//printf("Entering sendARPframe..\n");
			if ( (sa = hwa->ip_addr) != NULL)
			{
			//printf("         IP addr = %s\n", Sock_ntop_host(sa, sizeof(*sa)));
				
			strcpy(populated_arp->sender_ip_address,Sock_ntop_host(sa, sizeof(*sa)));
			sendARPframe(pf_socket, populated_arp, hwa->if_haddr, flood_mac, hwa->if_index);
			}
			//printf("Leaving SendODR..\n");

		}
	}	
	//printf("Leaving the FloodARP..\n");
	return;
}
Exemple #7
0
local_ent *get_local_list(){

  struct hwa_info *hwa,*hwahead;
  struct sockaddr *sa;
  local_ent *root=NULL;
  local_ent curr;

  for(hwahead=hwa=Get_hw_addrs();hwa!=NULL;hwa=hwa->hwa_next){
    if(strcmp(hwa->if_name,"eth0")==0){
      if((sa=hwa->ip_addr)!=NULL){
	char *temp=Sock_ntop_host(sa,sizeof(*sa));
	strncpy(curr.ip_info.ip,temp,INET_ADDRSTRLEN);
      }

      memcpy(curr.ip_info.hw_addr,hwa->if_haddr,HADDR_SIZE);
      root=insert_into_list(root,&curr);
    }
  }

  free_hwa_info(hwahead);
  return root;
}
Exemple #8
0
/*
	Get source eth0 canonical IP address 
*/
void getOwnCanonicalIPAddress(char* own_canonical_ip_address)
{
	struct hwa_info	*hwa, *hwahead;
	struct sockaddr_in * ip_address_structure;
	

	/* Flood with broadcast address on all interfaces except eth0 and lo and recieved interface */
	for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) 
	{
		if( strcmp(hwa->if_name, "eth0")==0 )
		{	
			//printf("Entered if..\n");
			ip_address_structure = (struct sockaddr_in *)hwa->ip_addr; 		
			inet_ntop( AF_INET, &(ip_address_structure->sin_addr), own_canonical_ip_address, 100 );
			//printf("\nSelf's canonical IP address : %s\n", own_canonical_ip_address);	
			return;	
		}
	}	
	strcpy(own_canonical_ip_address, "Not found..");
	//("\nSelf's canonical IP address : %s\n", own_canonical_ip_address);	
	return;
	//return own_canonical_ip_address;
}
Exemple #9
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);
			} 	
				
		}
	}
	

}
int main(int argc, char **argv)
{
	struct ODRmsg			*msg;
	struct hwa_info			*hwa, *hwahead;
	struct sockaddr_un 		su;
	struct sockaddr		temp;
	struct  sockaddr_un	sckadr;

	struct sockaddr *sa;

	socklen_t				addrlen;
	int 					i, j;
	int 					protocol, maxfdp1;
	fd_set					rset;
	//char					address[16];
	char					rcvline[MAXLINE];
	struct hostent			*host;
	
	if(argc < 2)
	{
		printf("Usage: odr staleness\n");
		return -1;
	}
	
	staleness  = atoi(argv[1]);
	
	gethostname(name, 16);
	host = malloc(sizeof(struct hostent));
	host = gethostbyname(name);
	if(host == NULL)
		printf("host is NULL\n");
	strncpy(canonical, inet_ntoa( *( struct in_addr*)( host -> h_addr_list[0])), 16);
	printf("Local ODR on host %s\n", name);
	printf( "Canonical IP: %s\n", canonical);

	printf("Finding interfaces\n");
	for (i = 0, hwahead = hwa = Get_hw_addrs(); hwa != NULL && i < MAX_INTERFACES; hwa = hwa->hwa_next, i++)
	{
		if(strncmp(hwa->if_name, "eth0", 4) != 0 && strncmp(hwa->if_name, "lo", 2) != 0)
		{
			/*
			for(j = 0; j < 6; j++)
			{
				interfaces[i].if_haddr[j] = hwa->if_haddr[j];
			}*/
			memcpy((void*)interfaces[i].if_haddr, (void*)hwa->if_haddr, 6);
			interfaces[i].if_index = hwa->if_index;
			sa = hwa->ip_addr;
			strncpy(interfaces[i].ip_addr, sock_ntop_host(sa, sizeof(*sa)), 16);
			printf("IP Address: %s\n",  interfaces[i].ip_addr);
			printf("Hardware Address: ");
			printHW(interfaces[i].if_haddr);
			printf("\nIndex: %d\n\n", interfaces[i].if_index);
		}
		else
			i--;
	}
	
	if_nums = i;
	err_msg("%s",ODR_SUNPATH);

	printf("Found %d interfaces\n", if_nums);
	//Creating packet socket
	pfsock = socket(AF_PACKET, SOCK_RAW, htons(ODR_PROTOCOL));
	if(pfsock < 0)
	{
		printf("pfsock: %d %s\n", errno, strerror(errno));
		return -1;
	}
	
	unlink(ODR_SUNPATH);
	bzero(&su, sizeof(su));
	su.sun_family = AF_LOCAL;
	strcpy(su.sun_path, ODR_SUNPATH);
	//strcat(su.sun_path, "\0");
	
	appsock = socket(AF_LOCAL, SOCK_DGRAM, 0);
	if(appsock < 0)
	{
		printf("appsock: %d %s\n", errno, strerror(errno));
		return -1;
	}
	

	if(bind(appsock, (struct sockaddr*) &su, sizeof(su)) < 0)
	{
		printf("bind failed: %d %s\n", errno, strerror(errno));
		return -1;
	}

	FD_ZERO(&rset);
	printf("Bound appsock\n");
	for ( ; ; ) 
	{

		FD_SET(pfsock, &rset);
		FD_SET(appsock, &rset);
		maxfdp1 = max(pfsock, appsock) + 1;
		
		printf("Selecting...\n");
		if(select(maxfdp1, &rset, NULL, NULL, NULL) < 0)
		{
			printf("select error: %d %s\n", errno, strerror(errno));
		}
		
		if(FD_ISSET(appsock, &rset))
		{
			printf("appsock is ready\n");
			//msg_recv(appsock, message, address, port);
		addrlen = sizeof(sckadr);
			
			if(recvfrom(appsock, rcvline, MAXLINE, 0, &temp, &addrlen) < 0)
			{
					printf("appsock recvfrom error: %d %s\n", errno, strerror(errno));
					return -1;
			}
			
			//su = (struct sockaddr_un*) sa;			
			//err_msg("%s",temp.sa_data);
			//err_msg("%d",addrlen);

			processAPPmsg(rcvline, &temp);
		}
		if(FD_ISSET(pfsock, &rset))
		{
			printf("pfsock is ready\n");
			
			if(recvfrom(pfsock, rcvline, MAXLINE, 0, sa, &addrlen) < 0)
			{
					printf("pfsock recvmsg error: %d %s\n", errno, strerror(errno));
					return -1;
			}
			
			printf("Received message: \n");
			printf("From: ");
			printHW(rcvline);
			printHW(rcvline + ETH_ALEN);
			if(!toss())
			{
				msg = rcvline + 14;

				memcpy((void*)neighbor, (void*)rcvline + ETH_ALEN, ETH_ALEN);
				printf(" To: ");
				printHW(neighbor);
				printf("\n");
				processODRmsg(msg, sa);
			}
			else
				printf("Toss it!\n");
		}
	}
}
Exemple #11
0
int main(int argc, char const *argv[])
{
		char *msg_fields[MAXLINE], str[MAXLINE],str1[MAXLINE] ;
		struct hwa_info	*hwa, *hwahead;
		char * ifname_split;
		int i, prflag, k, pf_socket, unix_domain_socket, arplen,unixarplen, ihw, khw, maxfdp,len;
		char   *ptr,*ptr1, *hw_address1, sender_ethernet_address[6], buffer[ETH_FRAME_LEN+1], unix_buffer[ETH_FRAME_LEN+1], tmp_hw_address[6], tmp_ip_address[INET_ADDRSTRLEN]; 
		struct sockaddr	*sa;
		struct sockaddr_un unixaddr, unixarpaddr;
		struct sockaddr_ll arpaddr;
		char * dest_mac1; 
		struct IP_hw_address_mpg *IP_hw_address_mpg_collection, *cache, *entry_from_cache, *own_ip_hw_entry;
		struct arp_frame *recvd_packet, *new_arp_rreq_frame;
		int nready, client[FD_SETSIZE], connfd=-1, j;
		ssize_t	n;
		fd_set	rset, allset;
		printf("set of  <IP address , HW address>  matching pairs for all eth0 interface IP addresses\n");
		printf("****************************************************************************************\n");
		for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) 
		{
			ifname_split = strtok(hwa->if_name, ":"); //get pointer to first token found and store in 0
		
			if( strcmp(ifname_split, "eth0")==0 )
			{	
				printf("%s :%s", hwa->if_name, ((hwa->ip_alias) == IP_ALIAS) ? " (alias)\n" : "\n");
				
				if ( (sa = hwa->ip_addr) != NULL)
				{
					strcpy(tmp_ip_address, Sock_ntop_host(sa, sizeof(*sa)));

					printf("IP: %s\t", tmp_ip_address);
				}	
				prflag = 0;
				i = 0;
				do 
				{

					if (hwa->if_haddr[i] != '\0') 
					{
						prflag = 1;
						break;
					}
				} while (++i < IF_HADDR);
			
			k=0;
			if (prflag) 
			{
				printf("HW addr = ");
				ptr = hwa->if_haddr;
				hw_address1=ptr;
				i = IF_HADDR;
				do {
					tmp_hw_address[k] = *ptr++ & 0xff;
					k++;
					printf("%.2x%s", *hw_address1++ & 0xff, (i == 1) ? " " : ":");
				} while (--i > 0);
				insert_own_to_collection( tmp_ip_address,tmp_hw_address, hwa->if_index );
				printf("\n\n");
			}
			}
		}

 	getOwnCanonicalIPAddress(own_ip_address);

	if((pf_socket = socket(PF_PACKET, SOCK_RAW, htons(USID_PROTO)))==-1)
 	{
 		printf("Error in creation of socket for PF_PACKET\n");
 		perror("socket");
 		return 0;
 	}
 
 	if((unix_domain_socket = socket(AF_UNIX, SOCK_STREAM, 0))==-1)
 	{
 		printf("Error in creation of Unix Domain socket\n");
		perror("socket");
		return;
		
	}


	bzero(&unixaddr, sizeof(unixaddr));
	unixaddr.sun_family = AF_UNIX;
	strcpy(unixaddr.sun_path, UNIXDG_PATH);
	unlink(UNIXDG_PATH);
	 len = strlen(unixaddr.sun_path) + sizeof(unixaddr.sun_family);
    if(bind(unix_domain_socket, (SA *) &unixaddr, len)<0)
    {       fprintf(stderr,"bind() failed. errorno =  %d\n",errno); 
            exit(1);
    }
	
    if(listen(unix_domain_socket, LISTENQ)<0)
    {       fprintf(stderr,"listen() failed. errorno = %d\n",errno);       
            exit(1);
    }

	//printf("listening on : %d\n", unix_domain_socket);
	//unixarplen=sizeof(unixarpaddr);
	//connfd = accept(unix_domain_socket, (struct sockaddr_un *) &unixarpaddr, &unixarplen);
	//printf("accepted : %d\n", unix_domain_socket);
	FD_ZERO(&allset);
	//FD_SET(connfd, &allset);
	FD_SET(unix_domain_socket, &allset);
	FD_SET(pf_socket, &allset);
	maxfdp=max(unix_domain_socket,pf_socket)+1;
	/* end fig01 */


	/* include fig02 */
	for ( ; ; ) 
	{
		
		rset = allset;		/* structure assignment */
		if((nready = select(maxfdp, &rset, NULL, NULL, NULL))<0)
		{	
			if(errno==EINTR)	
				continue;
			else
				err_sys("select error");	
		}		
		if (FD_ISSET(unix_domain_socket, &rset)) 
		{	
			
        	unixarplen=sizeof(unixarpaddr);
	        if((connfd = accept(unix_domain_socket, (struct sockaddr_un *) &unixarpaddr, &unixarplen))>=0)
	        {
	   //     	printf("new conn recieved: \n",connfd);
	        	 if ( (n = readline(connfd, unix_buffer, MAXLINE)) >= 0)
				{          
	        	//(n=recvfrom(unix_domain_socket,unix_buffer, ETH_FRAME_LEN+1, 0, &unixarpaddr, &unixarplen)>0)
	      //  		printf("Received  request from Tour module..%s\n",unix_buffer);
	        		

	        		msg_fields[0] = strtok(unix_buffer, "|"); //get pointer to first token found and store in 0
	                while(msg_fields[i]!= NULL) 
	                {   /* ensure a pointer was found */
	                    i++;
	                    msg_fields[i] = strtok(NULL, "|"); /* continue to tokenize the string */
	                }
                
                	for(j = 0; j <= i-1; j++) {
          
                   		 printf("%s\n", msg_fields[j]); /* print out all of the tokens */
                	}
 					//memset(entry_from_cache,0,sizeof(struct IP_hw_address_mpg));
 			//		printf("check in cache\n");
					print();
                	entry_from_cache=get_ethernet_from_ip(msg_fields[0],NULL,msg_fields[1],msg_fields[2],connfd);
               		print();
               		//get_ethernet_from_ip( char * ip_address,char * hw_address, char * if_index, char * hatype, int connfd )
			      //  printf("out of cache\n");
			        if(entry_from_cache!=NULL && entry_from_cache->hw_address!=NULL)
			        {
			        	printf("hw address obtained from cache\n");
			        	write(connfd,entry_from_cache->hw_address, strlen(entry_from_cache->hw_address));
			        	close(connfd);
			        	/*
						A cache entry has five parts:
						 (i) IP address ;  
						(ii) HW address ;  
						(iii) sll_ifindex (the interface to be used for reaching the matching pair <(i) , (ii)>) ;  
						(iv) sll_hatype ;  and
						 (v) a Unix-domain connection-socket descriptor
			        	*/
						 
			        }else
			        {
			    //    	printf("hw address NOT obtained from cache\n");
			        	floodARP(pf_socket,msg_fields[0]/*target ip address*/ );
			      //  	printf("ARP request sent on pf_socket\n");
			        	
			        	insert_to_cache( msg_fields[0],"", msg_fields[1],(unsigned short)atoi(msg_fields[2]),connfd );
			        	//void insert_to_cache( char * ip_address,char * hw_address, int if_index, char * hatype, int connfd )

			        	FD_SET(connfd, &rset);
			        	maxfdp=connfd+1;

			        }
			        
		        }else
		        {
		        	perror("readline");
		        }


	      
			}else{
				perror("connect");
			}
//---------------------------------------------------------------------------------------------------------
		}
        else if (FD_ISSET(pf_socket, &rset)) 
        {       
 
 			//printf("Receiving packet ...\n");
        	arplen=sizeof(arpaddr);
	        if((n=recvfrom(pf_socket,buffer, ETH_FRAME_LEN+1, 0, &arpaddr, &arplen)>0))
	        {



	        	

	        	if (n == -1)
	        	{ 
	        		printf("Error in recieving data from ARP..\n"); 
	        		exit(0);
	        	}
	        	else
	        	{ 
	        		//printf("Recieved Packet successfully\n" ); 
	        	}

	            recvd_packet = (struct arp_frame *)processRecievedPacket(buffer);

	            ihw = IF_HADDR;
	        	khw=0;
	        	
	        //		printf("Received packet from hw address:\n");
				do 
				{	
					sender_ethernet_address[khw] = arpaddr.sll_addr[khw] & 0xff;
					recvd_packet->sender_ethernet_address[khw] = arpaddr.sll_addr[khw] & 0xff;
					
					//printf("%.2x%s", sender_ethernet_address[khw] & 0xff, (ihw == 1) ? " " : ":");
					khw++;
				} while (--ihw > 0);
			//	printf("\n at interface %d...\n",arpaddr.sll_ifindex);

	            if(recvd_packet->op==1)//request
	            {
	            	///printf("ARP request recieved\n");


	            	        ihw = IF_HADDR;
				        	khw=0;
				        	printf("**************************************************************\n");
				        	printf("ARP REQUEST RECEIVED:\n");
				        	printf("**************************************************************\n");
				        	printf("Sender IP Address : %s \n" , recvd_packet->sender_ip_address);
				        	printf("Sender hw address: ");
							do 
							{	
								
								
								printf("%.2x%s", recvd_packet->sender_ethernet_address[khw] & 0xff, (ihw == 1) ? " " : ":");
								khw++;
							} while (--ihw > 0);	


							printf("\nDestination IP Address : %s \n" , recvd_packet->target_ip_address);
				        	
							printf("\n");
							printf("hard_type: %hu, proto_type: %hu, hard_size: %hu ,proto_size %hu,op : %hu\n", recvd_packet->hard_type, recvd_packet->proto_type, recvd_packet->hard_size,recvd_packet->proto_size,recvd_packet->op);
				        	printf("**************************************************************\n");





	            	 own_ip_hw_entry = get_own_ethernet_from_ip( recvd_packet->target_ip_address );
	            	if(own_ip_hw_entry!=NULL)
	            	{
		      //      	printf("make entry in cache table\n");
		            	if(cache=get_ethernet_from_ip( recvd_packet->sender_ip_address,recvd_packet->sender_ethernet_address, arpaddr.sll_ifindex, recvd_packet->hard_type, -1 )!=NULL)
		            	{
			            //	printf("make entry in cache table123\n");

		            	//	printf("gellooo :%hu\n",ntohs(recvd_packet->hard_type) );
			            //	printf("make entry in cache table456\n");

						//	printf("IFIindex :%d\n",arpaddr.sll_ifindex );

		            		//cache->sll_ifindex=(int)arpaddr.sll_ifindex;
		            		
		            	//	printf("gellooo :%hu\n",recvd_packet->hard_type );
		            		//cache->sll_hatype=ntohs(recvd_packet->hard_type);
		            		

		            	}else{
		            		insert_to_cache( recvd_packet->sender_ip_address,recvd_packet->sender_ethernet_address, arpaddr.sll_ifindex, recvd_packet->hard_type, -1 );	
		            	}

		            	sendARPResponse(pf_socket,recvd_packet->sender_ip_address/*target ip address*/ ,sender_ethernet_address, arpaddr.sll_ifindex/*arp response*/);
	            	
	            	}else
	            	{
	            		cache=get_cache_entry_from_IP(recvd_packet->sender_ip_address);
	            		if(cache!=NULL)
	            		{
	            			cache_update_entry( recvd_packet->sender_ip_address,recvd_packet->sender_ethernet_address,arpaddr.sll_ifindex, recvd_packet->hard_type, -1);
	            		}
	            	}
	            }else if(recvd_packet->op==2)
	            {
	            //	printf("ARP response recieved\n");
	            	cache=get_cache_entry_from_IP(recvd_packet->sender_ip_address);
            		if(cache!=NULL)
            		{
            			if(cache->unix_domain_confd!=-1)
            			{
            	//			printf("conn fd from cache: %d\n",cache->unix_domain_confd );
            				//strcpy(str1,"sender_ethernet_address");
            				//sleep(5);
            				ihw = IF_HADDR;
				        	khw=0;
				        	printf("**************************************************************\n");
				        	printf("ARP RESPONSE RECEIVED:\n");
				        	printf("**************************************************************\n");
				        	printf("Sender IP Address : %s \n" , recvd_packet->sender_ip_address);
				        	printf("Sender hw address: ");
							do 
							{	
								
								
								printf("%.2x%s", sender_ethernet_address[khw] & 0xff, (ihw == 1) ? " " : ":");
								khw++;
							} while (--ihw > 0);	


							printf("\nDestination IP Address : %s \n" , recvd_packet->target_ip_address);
				        	printf("Destination hw address: ");
							ihw = IF_HADDR;
				        	khw=0;
				        	
							do 
							{	
								
								
								printf("%.2x%s", recvd_packet->target_ethernet_address[khw] & 0xff, (ihw == 1) ? " " : ":");
								khw++;
							} while (--ihw > 0);	
							printf("\n");
							printf("hard_type: %hu, proto_type: %hu, hard_size: %hu ,proto_size %hu,op : %hu\n", recvd_packet->hard_type, recvd_packet->proto_type, recvd_packet->hard_size,recvd_packet->proto_size,recvd_packet->op);
				        	printf("**************************************************************\n");

				        	printf("**************************************************************\n");
				        	printf("ARP RESPONSE SENT TO TOUR MODULE :\n");
				        	printf("**************************************************************\n");
				        	printf("hw address for requested IP is : ");
							ihw = IF_HADDR;
				        	khw=0;
				        	
							do 
							{	
								
								
								printf("%.2x%s", sender_ethernet_address[khw] & 0xff, (ihw == 1) ? " " : ":");
								khw++;
							} while (--ihw > 0);	


            				if(write(cache->unix_domain_confd,sender_ethernet_address,100)<0)
            					perror("write");
            				


            				FD_CLR(cache->unix_domain_confd,&rset);
							close(cache->unix_domain_confd);
            			}
            			cache_update_entry( recvd_packet->sender_ip_address,recvd_packet->sender_ethernet_address,arpaddr.sll_ifindex, recvd_packet->hard_type, -1);
	            			//printf("I AM HERE ...4\n");

            		}

	            }

	        }

        }
        else if(FD_ISSET(connfd, &rset))
        {
        //	printf("connfd readable\n");
        	n = readline(connfd, str, MAXLINE);
        	if ( n == 0)
			{ 
		//		printf("Connection terminated\n");
				 cache_delete_entry(connfd );
				close(connfd);
				connfd=-1;
        	}
        	/*else if(n>0)
        	{
        		printf("hw address obtained from ARP\n");
        		cache_update_entry(msg_fields[0],msg_fields[1],msg_fields[2],msg_fields[3],connfd, hw_address_from_arp);
        		writen(connfd,hw_address_from_arp, strlen(hw_address_from_arp));
   				close(connfd);
        	
        	}*/
        }

    }
	return 0;
}
Exemple #12
0
void
if_init()
{
	struct hwa_info *hwa, *hwahead;
	struct sockaddr *sa;
	struct hostent *hptr;
	char *ptr;
	char **pptr;
	int i, j, prflag, n;
	struct arp_cache_entry cache_entry;

	memset(if_hwaddr, 0, sizeof(if_hwaddr));
	memset(if_sock, 0, sizeof(if_sock));
	if_size = 0;
	puts("<-- HW INFO -->");
	for (i = 0; i < NUM_VM; i++){
		sprintf(node_ip[i], "vm%d", i+1);
		hptr = gethostbyname(node_ip[i]);
		for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++){
			Inet_ntop(hptr->h_addrtype, *pptr, node_ip[i], INET_ADDRSTRLEN);
		}
	}
	for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) {
		printf("interface index = %d\n", (n = hwa->if_index));
		printf("%s :%s", hwa->if_name,
				((hwa->ip_alias) == IP_ALIAS) ? " (alias)\n" : "\n");

		if ((hwa->ip_alias) != IP_ALIAS) {
			if_size++;
		}
		if ((sa = hwa->ip_addr) != NULL) {
			printf("\tIP addr = %s\n", Sock_ntop_host(sa, sizeof(*sa)));

			if (hwa->if_haddr != NULL) {
				if (strcmp(hwa->if_haddr, if_hwaddr[my_index])
						== 0 && (hwa->ip_alias) == IP_ALIAS) {
					insert_local_hw_cache(Sock_ntop_host(sa, sizeof(*sa)));
				}
			}
		}
		// get canonical ip
		if (strcmp(hwa->if_name, "eth0") == 0) {
			sprintf(my_ip, "%s", Sock_ntop_host(sa, sizeof(*sa)));

			my_index = hwa->if_index;
			for (my_vm = 0; my_vm < NUM_VM; my_vm++) {
				if (strcmp(my_ip, node_ip[my_vm]) == 0) {
					my_vm++;
					break;
				}
			}
		}

		prflag = i = 0;
		do {
			if (hwa->if_haddr[i] != '\0') {
				prflag = 1;
				break;
			}
		} while (++i < IF_HADDR);

		if (prflag) {
			printf("\tHW addr = ");
			for (i = 0; i < IF_HADDR; i++) {
				if_hwaddr[n][i] = hwa->if_haddr[i];
				printf("%02x ", (int) if_hwaddr[n][i] & 0xff);
			}
			puts("");
		}
	}
	// printf("***info: number of interfaces -- %d\n***", if_size);
	// Create PF_PACKET socket for etho0
	if_sockfd = Socket(AF_PACKET, SOCK_RAW, htons(ARP_PROTOCOL));

	addr.sll_family = PF_PACKET;
	addr.sll_protocol = htons(ARP_PROTOCOL);
	addr.sll_ifindex = my_index;
	addr.sll_hatype = ARPHRD_ETHER;
	addr.sll_pkttype = PACKET_HOST;
	addr.sll_halen = ETH_ALEN;

	for (j = 0; j < ETH_ALEN; j++){
		addr.sll_addr[j] = if_hwaddr[my_index][j];
	}

	Bind(if_sockfd, (SA *)&addr, sizeof(struct sockaddr_ll));
	maxfd= (maxfd, if_sockfd);
	insert_local_hw_cache(my_ip);
	// puts("if_init done\n");
	puts("");
}
Exemple #13
0
int main()
{

	in_addr_t myip, ip_addr,pendip;
	unsigned char mymac[6], src_mac[6], dst_mac[6], mac[6];
	unsigned int my_ifindex;


	struct hwa_info *hwa, *hwahead;
	struct hwaddr hwdetail, *hwptr1;
	struct hwaddr *hwptr = & hwdetail;

	struct sockaddr *srcip;
	struct sockaddr_in *recvptr;
	int pfsocket, listenfd, confd;
	struct sockaddr_ll pfaddr;
	struct sockaddr_ll *pfaddr_ptr = &pfaddr;
	struct sockaddr_un arpaddr, cliaddr;
	struct in_addr Ipad;
	struct in_addr in_addr_temp1, in_addr_temp2, in_addr_temp3;

	struct arp_req_res arpreq, arpres;
	struct arp_req_res *arpreq_ptr, *arpres_ptr, *arpptr;
	struct pending_reply *pendhead, *ptrpend;
	pendhead = ptrpend = NULL;

	fd_set rset;
	int maxfd, i, n,flag=0;
	socklen_t len;

	void *buffer=(void*)malloc(ETH_FRAME_LEN);
	char *recv_buf=(char*)malloc(ETH_FRAME_LEN);
	unsigned char *etherhead=buffer;
	struct ethhdr *eh=(struct ethhdr*)etherhead;
	char sendbuf[512], recvline[512];


	//printf("--------------myip = %d --------------\n", myip);

	pfsocket = Socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP_PROTO));

	for (hwahead = hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) {
		if(strncmp(hwa->if_name,"eth0",4) == 0){
			srcip = hwa->ip_addr;
			struct sockaddr_in *sin = (struct sockaddr_in *)srcip;
			myip = sin -> sin_addr.s_addr;
			my_ifindex = hwa -> if_index;
			for(i=0; i < 6; i++)
			mymac[i] = hwa -> if_haddr[i];


			pfaddr.sll_family = AF_PACKET;
			pfaddr.sll_protocol = htons(ETH_P_ARP_PROTO);
			pfaddr.sll_ifindex = hwa->if_index;
			  //printf("Name : %s\n",hwa->if_name);
			Bind(pfsocket, (struct sockaddr*)&pfaddr, sizeof(pfaddr));
			break;
			}
	}

	prhwaddrs();
	free_hwa_info(hwahead);

	/* create an entry for self in the cache and add it at the head*/
	cache_update(myip, my_ifindex, mymac, -1);
	print_cache();
	/* prepare ARP Request */
	arpreq_ptr = &arpreq;
	arpreq_ptr -> id = 531;
	arpreq_ptr -> hw_type = 1;
	arpreq_ptr -> prot_type = 1842;
	arpreq_ptr -> hw_size = 6;
	arpreq_ptr -> prot_size = 4;
	arpreq_ptr -> op = 1; /* 1 for REQ */
	for(i=0; i < 6; i++)
	  arpreq_ptr -> src_mac[i] = mymac[i];
	arpreq_ptr -> src_ip = myip;
	for(i=0; i < 6; i++)
	  arpreq_ptr -> dst_mac[i] = 0xff; /* dest mac is 0xffffffffffff for broadcast */


	/* prepare ARP Response */
	arpres_ptr = &arpres;
	arpres_ptr -> id = 531;
	arpres_ptr -> hw_type = 1;
	arpres_ptr -> prot_type = 1842;
	arpres_ptr -> hw_size = 6;
	arpres_ptr -> prot_size = 4;
	arpres_ptr -> op = 2; /* 2 for RESP */
	for(i=0; i < 6; i++)


	/* Create unix socket */
	listenfd = Socket(AF_LOCAL, SOCK_STREAM, 0);
	unlink(ARP_PATH);
	bzero(&arpaddr, sizeof(arpaddr));
	arpaddr.sun_family = AF_LOCAL;
	strcpy(arpaddr.sun_path, ARP_PATH);
	
	Bind(listenfd, (struct sockaddr*)&arpaddr, sizeof(arpaddr));
	Listen(listenfd, LISTENQ);

	/*************** debug code *************/
	//printf("--------------myip = %x --------------\n", myip);
	//printf("ntoh myip = %x \n", ntohl((uint32_t)myip));


	Ipad.s_addr = myip;
	printf("myip is %s \n", inet_ntoa( Ipad));
	printf("myIFindex = %d \n", my_ifindex);
	printf("mymac =  ");
	for(i = 0; i<6; i++)
	  printf("%02x:", mymac[i]);
	printf("\n");
	/*************** debug code *************/

	while(1){      
		FD_ZERO(&rset);
		FD_SET(listenfd, &rset);
		FD_SET(pfsocket, &rset);
		maxfd = max(listenfd, pfsocket);
      	//printf("Waiting in select\n");
		select(maxfd+1,&rset,NULL,NULL,NULL);

		if(FD_ISSET(listenfd, &rset)){/* unix socket read*/
		  
			confd = Accept(listenfd, (struct sockaddr *)&cliaddr, &len); 
			len = sizeof(cliaddr);
			bzero(recvline, 512);
			Recvfrom(confd, recvline, 512, 0,(struct sockaddr *)&cliaddr, &len);
			printf("\n---------------Hardware details request received from tour client-----------------\n");
			printf("confd = %d \n", confd);
			printf("H/W details requsted for ");
			recvptr = (struct sockaddr_in*) recvline;
			ip_addr = recvptr -> sin_addr.s_addr;			
			Ipad.s_addr = ip_addr;
			printf("IPaddres = %s \n", inet_ntoa( Ipad));
			printf(".....................................................................................\n\n"); 

			cacheptr = mac_lookup(ip_addr);

			if(cacheptr != NULL){/* cache has entry for this IPaddress. Send HW details on the UXsocket */
				struct uxapi *sndptr, *rptr;
				bzero(sendbuf,512);
		//        sndptr = (struct uxapi *)sendbuf;
		//        rptr = (struct uxapi *)recvline;
		//        memcpy(sndptr, rptr, sizeof( struct uxapi ));
				hwptr = &hwdetail;
				hwptr -> sll_ifindex = cacheptr -> sll_ifindex;
				hwptr -> sll_hatype = 1;
				hwptr -> sll_halen = 8;
				memcpy((void *)hwptr -> sll_addr, (void *)cacheptr -> mac, 8);
				memcpy((struct hwaddr *)sendbuf, hwptr, sizeof(hwdetail));
				hwptr1 = (struct hwaddr *)sendbuf;

				printf("\n------------------------Sending HW details to client-6  ---------------------\n");
				printf("ifindex = %d, hatype = %d, halen = %d, Mac = %02x:%02x:%02x:%02x:%02x:%02x\n",\
				  hwptr1 -> sll_ifindex, hwptr1 -> sll_hatype, hwptr1 -> sll_halen, hwptr1 -> sll_addr[0],\
				  hwptr1 -> sll_addr[1], hwptr1 -> sll_addr[2], hwptr1 -> sll_addr[3], hwptr1 -> sll_addr[4],\
				  hwptr1 -> sll_addr[5]);
				printf("------------------------------------------------------------------------------------\n");

				//sleep(4);
				 writen(confd,sendbuf,sizeof(struct hwaddr));
				//printf("sendout;;;;;;;;;;;");
	
				
			
				//Sendto(confd, sendbuf, 512, 0, (struct sockaddr*)&cliaddr, len);
				close(confd);
			}
			else{//no such entry in the cache
			
					flag =1;//if this flag is set that means we need to write back to tour client
					
					pendip = ip_addr;
					printf("\nNo entry for hardware address. Sending ARP Request \n");
			/* preserve the IP address to send H/W details to client later 
				  ptrpend = malloc(sizeof(struct pending_reply));
				  ptrpend -> srcnodeip
			*/

			/* Send ARP Request with dst_mac set to broadcast of 0xffffffffff */
				    for(i=0; i < 6; i++){
						pfaddr.sll_addr[i] = 0xff;
						dst_mac[i] = 0xff;
						src_mac[i] = mymac[i];
				    }
				    bzero(buffer, ETH_FRAME_LEN);
			/* copy ethernet packet header in to send buffer */
					memcpy((void*)buffer, (void*)dst_mac, ETH_ALEN);
					memcpy((void*)(buffer+ETH_ALEN), (void*)src_mac, ETH_ALEN);
					eh->h_proto=htons(ETH_P_ARP_PROTO);

			/* broadcast ARP REQUEST */
				  arpreq_ptr -> dst_ip = ip_addr; /* Ip address of the node whose HW dsertails are to be gathered */
				  for(i=0; i < 6; i++){
					arpreq_ptr -> dst_mac[i] = 0xff; /* dest mac is 0xffffffffffff for broadcast */
					arpreq_ptr -> src_mac[i] = mymac[i];
				  }
			/* copy arp Request packet in to send buffer */
				  memcpy((void *)(buffer+14), (void *)arpreq_ptr, sizeof( struct arp_req_res));
				  printf("\n------------------ Sending ARP REQUEST packet-2 ---------------------\n");
			//      struct arp_req_res *arpptr;
				  arpptr = (struct arp_req_res *)(buffer+14);

				  printf(" ID = %d hw_type = %d   prot_type = %x   hw_size = %d   prot_size = %d   OP = %d \n",arpptr -> id, arpptr -> hw_type, \
						  arpptr -> prot_type, arpptr -> hw_size, arpptr -> prot_size, arpptr -> op);
				  Ipad.s_addr = arpptr -> src_ip;
				  printf("SrcMac = %02x:%02x:%02x:%02x:%02x:%02x   SrcIp = %s \n", arpptr -> src_mac[0], arpptr -> src_mac[1], \
					arpptr -> src_mac[2], arpptr -> src_mac[3], arpptr -> src_mac[4], arpptr -> src_mac[5], inet_ntoa( Ipad));
				  Ipad.s_addr = arpptr -> dst_ip;
				  printf("DstMac = %02x:%02x:%02x:%02x:%02x:%02x   DstIp = %s \n", arpptr -> dst_mac[0], arpptr -> dst_mac[1], \
					arpptr -> dst_mac[2], arpptr -> dst_mac[3], arpptr -> dst_mac[4], arpptr -> dst_mac[5], inet_ntoa( Ipad) );
				 printf(".......................................................................\n\n");
			 

						Sendto(pfsocket, buffer, ETH_FRAME_LEN, 0, (struct sockaddr*)&pfaddr, sizeof(pfaddr));
				 // print_cache();

			/* The client requested MAC address was not found in the cache. So an ARP Request was sent. 
			  The respective node will respond with ARP response and the cache will be updated.
			  After the cache is updated send the MAC address to the client. The pending response 
			  is indicated by the list */				
	   
			}

		}/* End of unix recieve */	
				
	/***************************************************************************************************/

	/*PF socket recieve*/

		if(FD_ISSET(pfsocket, &rset)){

		bzero(recv_buf, ETH_FRAME_LEN);
			Recvfrom(pfsocket, recv_buf, ETH_FRAME_LEN, 0, NULL, NULL);
		arpptr = (struct arp_req_res *)(recv_buf +14);
		Ipad.s_addr = arpptr -> dst_ip;
		//printf("\tdest of the req is %s\n",inet_ntoa(Ipad));
		if(arpptr -> id != 531)
			continue;
	//    if(arp_ptr -> dst_ip != myip)
	//      continue; /* If the msg is not meant for you, ignore */
	//    if(arp_ptr -> dst_ip != arp_ptr -> src_ip)
	//      continue; /* If it is self generated msg & received on loopback, ignore */
		
			if(arpptr -> op == 2){ /* received ARP response */
					//printf("in the 2\n");
				  cache_update(arpptr -> src_ip, my_ifindex, arpptr -> src_mac, -1);
				  print_cache();

				  printf("\n------------------ Received ARP RESPONSE packet-3 ---------------------\n");
				  printf(" ID = %d hw_type = %d   prot_type = %x   hw_size = %d   prot_size = %d   OP = %d \n", arpptr -> id, arpptr -> hw_type, \
						  arpptr -> prot_type, arpptr -> hw_size, arpptr -> prot_size, arpptr -> op);
				  Ipad.s_addr = arpptr -> src_ip;
				  printf("SrcMac = %02x:%02x:%02x:%02x:%02x:%02x   SrcIp = %s \n", arpptr -> src_mac[0], arpptr -> src_mac[1], \
					arpptr -> src_mac[2], arpptr -> src_mac[3], arpptr -> src_mac[4], arpptr -> src_mac[5], inet_ntoa( Ipad));
				  Ipad.s_addr = arpptr -> dst_ip;
				  printf("DstMac = %02x:%02x:%02x:%02x:%02x:%02x   DstIp = %s \n", arpptr -> dst_mac[0], arpptr -> dst_mac[1], \
					arpptr -> dst_mac[2], arpptr -> dst_mac[3], arpptr -> dst_mac[4], arpptr -> dst_mac[5], inet_ntoa( Ipad) );
				  printf(".......................................................................\n\n");
			}
			
			if(arpptr -> op == 1){ /* received ARP request. Send ARP response */
					//if i am not the destination I will ignore it
					int lin1,lin2;
					//printf("in the 1\n");
					//Ipad.s_addr = arpptr -> dst_ip;
					in_addr_temp1.s_addr = myip;
					
					//in_addr_temp3.s_addr = arpptr -> src_ip;
					 /* If the msg is not meant for you, ignore */
					// printf("dest of req ip is %s\n", inet_ntoa(Ipad));
					// printf("my ip is %s\n",inet_ntoa(in_addr_temp1) );
					 lin1 = atoi(inet_ntoa(Ipad)+11);
					 lin2 = atoi(inet_ntoa(in_addr_temp1)+11);
					 //printf("lin1 is %d, lin2 is %d\n", lin1, lin2); 
					 
					if(lin1 != lin2){//i am not the destination
						if(mac_lookup(arpptr -> src_ip) != NULL){//check the cache,
							printf("\nReceived a broadcast request, I am not the destination, but sender's info is in my cache, updating cache...\n");
							print_cache();
						}
						else {
							printf("\nReceived a broadcast request, I am not the destination and the sender's information is not in my cache, ignore it\n ");
						}
						
					}else{//I am the destination, update cache, and send back
						  printf("\nReceived a broadcast request, I am the destination of req, response with HW addr\n");
						  cache_update(arpptr -> src_ip, my_ifindex, arpptr -> src_mac, -1);
						  print_cache();

						  printf("\n------------------ Received ARP REQUEST packet-4 ---------------------\n");
						  printf("ID =%d hw_type = %d   prot_type = %x   hw_size = %d   prot_size = %d   OP = %d \n",arpptr -> id , arpptr -> hw_type, \
								  arpptr -> prot_type, arpptr -> hw_size, arpptr -> prot_size, arpptr -> op);
						  Ipad.s_addr = arpptr -> src_ip;
						  printf("SrcMac = %02x:%02x:%02x:%02x:%02x:%02x   SrcIp = %s \n", arpptr -> src_mac[0], arpptr -> src_mac[1], \
							arpptr -> src_mac[2], arpptr -> src_mac[3], arpptr -> src_mac[4], arpptr -> src_mac[5], inet_ntoa( Ipad));
						  Ipad.s_addr = arpptr -> dst_ip;
						  printf("DstMac = %02x:%02x:%02x:%02x:%02x:%02x   DstIp = %s \n", arpptr -> dst_mac[0], arpptr -> dst_mac[1], \
							arpptr -> dst_mac[2], arpptr -> dst_mac[3], arpptr -> dst_mac[4], arpptr -> dst_mac[5], inet_ntoa( Ipad) );
						  printf(".......................................................................\n\n");
						
						//in_addr_temp1.s_addr = myip;
					//in_addr_temp2.s_addr = arpptr -> dst_ip;
					//in_addr_temp3.s_addr = arpptr -> src_ip;
					 /* If the msg is not meant for you, ignore */
					// printf("\tdest of the req is %s\n",inet_ntoa(Ipad));
						
						

						  ip_addr = arpptr -> src_ip;
						  for(i=0; i < 6; i++)
							mac[i] = arpptr -> src_mac[i];
						  arpres_ptr = &arpres;
						  arpres_ptr -> dst_ip = ip_addr;
						  arpres_ptr -> src_ip = myip;

						  for(i=0; i < 6; i++){
							arpres_ptr -> dst_mac[i] = mac[i];
							pfaddr.sll_addr[i] = mac[i];
							arpres_ptr -> src_mac[i] = mymac[i];
						  }
						  bzero(buffer, ETH_FRAME_LEN);
						  memcpy((void*)buffer, (void*)arpres_ptr -> dst_mac, ETH_ALEN);
						  memcpy((void*)(buffer+ETH_ALEN), (void*)arpres_ptr -> src_mac, ETH_ALEN);
						  eh->h_proto=htons(ETH_P_ARP_PROTO);
						  memcpy((void*)(buffer+14), (void*)arpres_ptr, sizeof(struct arp_req_res));/* copy the ARP response */

						  arpptr = (struct arp_req_res *)(buffer + 14);
						  printf("\n------------------ Sending ARP RESPONSE packet-5 ---------------------\n");
						  printf("ID = %d hw_type = %d   prot_type = %x   hw_size = %d   prot_size = %d   OP = %d \n",arpptr -> id, arpptr -> hw_type, \
								  arpptr -> prot_type, arpptr -> hw_size, arpptr -> prot_size, arpptr -> op);
						  Ipad.s_addr = arpptr -> src_ip;
						  printf("SrcMac = %02x:%02x:%02x:%02x:%02x:%02x   SrcIp = %s \n", arpptr -> src_mac[0], arpptr -> src_mac[1], \
							arpptr -> src_mac[2], arpptr -> src_mac[3], arpptr -> src_mac[4], arpptr -> src_mac[5], inet_ntoa( Ipad));
						  Ipad.s_addr = arpptr -> dst_ip;
						  printf("DstMac = %02x:%02x:%02x:%02x:%02x:%02x   DstIp = %s \n", arpptr -> dst_mac[0], arpptr -> dst_mac[1], \
							arpptr -> dst_mac[2], arpptr -> dst_mac[3], arpptr -> dst_mac[4], arpptr -> dst_mac[5], inet_ntoa( Ipad) );
						  printf(".......................................................................\n\n");
						  Sendto(pfsocket, buffer, ETH_FRAME_LEN, 0, (struct sockaddr*)&pfaddr, sizeof(pfaddr));
					}
			}/* end of ARP request received */

		  }/* End of PF socket recieve */   
		if(flag == 1){//the flag is 1, and this 
			if((cacheptr = mac_lookup(pendip))!= NULL){
				bzero(sendbuf,512);
				hwptr = &hwdetail;
				hwptr -> sll_ifindex = cacheptr -> sll_ifindex;
				hwptr -> sll_hatype = 1;
				hwptr -> sll_halen = 8;
				memcpy((void *)hwptr -> sll_addr, (void *)cacheptr -> mac, 8);
				memcpy((struct hwaddr *)sendbuf, hwptr, sizeof(hwdetail));
				hwptr1 = (struct hwaddr *)sendbuf;

				//memcpy( sendbuf,cacheptr -> mac, 8 );
				//Sendto(confd, sendbuf, 512, 0, (struct sockaddr*)&cliaddr, len);
				flag =0;	
			
				writen(confd,sendbuf,sizeof(struct hwaddr));
				}

		}
	}/* end of  while */
}/* end of main*/
Exemple #14
0
int main(int argc, char **argv)
{
	getAllIP();

	int count;
	
	for( ; ; )
	{
		printf("\nPlease Choose the Server From VM1 to VM10.\n");
		printf("(1 - 10):");

		int choose;

		scanf("%d", &choose);

		int vm_dest=choose;
		struct hostent *hp;
		
		char ip_src[INET_ADDRSTRLEN];
		char ip_dest[INET_ADDRSTRLEN];

		int result;
		
		char namestr[10];
		
		for(count=0;count<5;count++)
		{
			namestr[count]='\0';
		}

		sprintf(namestr, "vm%d", choose);

		hp=gethostbyname(namestr);

		char **pptr=hp->h_addr_list;
		Inet_ntop(hp->h_addrtype, *pptr, ip_dest, sizeof(ip_dest));
		
		int sockfd;
	
		struct sockaddr_un servaddr;
		struct sockaddr_un cliaddr;

		sockfd=Socket(AF_LOCAL, SOCK_DGRAM, 0);

		char temp_file[]="tmp_XXXXXX";
		mkstemp(temp_file);

		unlink(temp_file);
		
		bzero(&cliaddr, sizeof(cliaddr));
		cliaddr.sun_family=AF_LOCAL;
		strcpy(cliaddr.sun_path, temp_file);
	
		Bind(sockfd, (SA *)&cliaddr, sizeof(cliaddr));

		bzero(&servaddr, sizeof(servaddr));
		servaddr.sun_family=AF_LOCAL;
		strcpy(servaddr.sun_path, ODR_PATH);
	
		//client ip vm
		struct hwa_info *hwa;
		struct hwa_info *hwahead;

		struct sockaddr *sa;
		char *ptr;

		for(hwahead=hwa=Get_hw_addrs(); hwa!=NULL;hwa=hwa->hwa_next)
		{
			//we only care about eth0
			if(strcmp(hwa->if_name, "eth0")==0)
			{
				sa=hwa->ip_addr;
				sprintf(ip_src, "%s", Sock_ntop_host(sa, sizeof(*sa)));
			}
		}
		
		//msg_send
		char message[32];
		for(count=0;count<32;count++)
		{
			message[count]='\0';
		}
		sprintf(message, "%s", "\0");

		//char send_packet[50];
		//Sendto(sockfd, send_packet, 50, 0, (SA *)&servaddr, sizeof(servaddr));
		msg_send(sockfd, (SA *)&servaddr, sizeof(servaddr), ip_src, ip_dest, SERV_WKPORT, message, 0);

		char *new_message=(char *)Malloc(32);
		for(count=0;count<32;count++)
		{
			new_message[count]='\0';
		}
		
		char new_ipsrc[SIZE_IP];
		char new_ipdest[SIZE_IP];
		unsigned short new_portsrc;
		unsigned short new_portdest;
		
		result=msg_recv(sockfd, new_message, new_ipsrc, &new_portsrc, new_ipdest, &new_portdest);
		
		if(result==-1)
		{
			printf("##RECV# client at node vm%d timeout on response from vm%d.\n", getVMNum(ip_src)+1, getVMNum(ip_dest)+1);
			
			msg_send(sockfd, (SA *)&servaddr, sizeof(servaddr), ip_src, ip_dest, SERV_WKPORT, message, 1);

			result=msg_recv(sockfd, new_message, new_ipsrc, &new_portsrc, new_ipdest, &new_portdest);

			if(result==-1)
			{
				printf("##RECV# client at node vm%d timeout on response from vm%d.\n", getVMNum(ip_src)+1, getVMNum(ip_dest)+1);
				unlink(temp_file);
				continue;
			}
		}
		
		unlink(temp_file);
	}
}