Skip to main content

ARP/RARP full simulation program

server.c

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "arpa/inet.h"
#include "netinet/in.h"
#define SA struct sockaddr

struct IPmac {
			 char ip[100];
			 char mac[100];
	   };


int main() {
	   int sockfd,len,i;
	   struct sockaddr_in servaddr;
	   char buff[30],temp[30],ip[30],mac[30];
	   int flag=0;

	   struct IPmac in[3]={ 
			 {"10.1.1.8","44:dd:22:11:33"},
			 {"127.0.0.1","33:aa:fe:4e:2d"},
			 {"10.1.8.5","23:a3:5d:33:9d"}
	    };

	   //printing table
	   printf("ip\t\tmac\n");
	   for(i=0;i<3;i++) 
	   {
			 printf("%s\t%s\n",in[i].ip,in[i].mac);
	   }

	   //create socket
	   sockfd = socket(AF_INET,SOCK_DGRAM,0);

	   //fill structure
	   servaddr.sin_family = AF_INET;
	   servaddr.sin_port = htons(9999);
	   servaddr.sin_addr.s_addr = INADDR_ANY;

	   //bind
	   bind(sockfd,(SA*)&servaddr,sizeof(servaddr));

	   //get ip from client
	   len=sizeof(servaddr);
	   recvfrom(sockfd,ip,sizeof(ip),0,(SA*)&servaddr,&len);

	   for(i=0;i<strlen(ip)-1;i++) {
			 temp[i]=ip[i];
	   }
	   temp[i]='\0';
	   printf("received IP :%s\n",temp);

	   //searching in table for equivalent mac
	   for(i=0;i<3;i++) {
			 if(strcmp(temp,in[i].ip)==0) {
				    strcpy(mac,in[i].mac);
				    break;
			 }
	   }

	   printf("mac address is %s\n",mac);
	   sendto(sockfd,mac,sizeof(mac),0,(SA*)&servaddr,len);

	   //rarp simulation
	   //recv mac address
	   bzero(mac,sizeof(mac));
	   recvfrom(sockfd,mac,sizeof(mac),0,(SA*)&servaddr,&len);
	   printf("received mac address :%s",mac);
	   //store in temp
	   bzero(temp,sizeof(temp));
	   for(i=0;i<strlen(mac)-1;i++) {
			 temp[i]=mac[i];
	   }
	   temp[i]='\0';
	   bzero(ip,sizeof(ip));

	   //check in table
	   for(i=0;i<3;i++) {
			 if(strcmp(temp,in[i].mac)==0) {
				    strcpy(ip,in[i].ip);
				    break;
			 }
	   }
	   printf("ip address :%s\n",ip);
	   sendto(sockfd,ip,sizeof(ip),0,(SA*)&servaddr,len);
	   return 0;
}


client.c

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "arpa/inet.h"
#include "netinet/in.h"
#define SA struct sockaddr

int main() {
	   int sockfd,len;
	   char ip[30],mac[30];
	   struct sockaddr_in servaddr;

	   //creating socket
	   sockfd = socket(AF_INET,SOCK_DGRAM,0);

	   //fill structure
	   servaddr.sin_family = AF_INET;
	   servaddr.sin_port = htons(9999);
	   servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	   //send ip address
	   printf("ARP SIMULATION\n");
	   printf("enter ip address :");
	   fgets(ip,sizeof(ip),stdin);
	   sendto(sockfd,ip,sizeof(ip),0,(SA*)&servaddr,sizeof(servaddr));
	   len=sizeof(servaddr);
	   recvfrom(sockfd,mac,sizeof(mac),0,(SA*)&servaddr,&len);
	   printf("MAC address is: %s\n",mac);

	   printf("RARP simulation\n");
	   printf("enter mac address :");
	   bzero(mac,sizeof(mac));
	   fgets(mac,sizeof(mac),stdin);
	   sendto(sockfd,mac,sizeof(mac),0,(SA*)&servaddr,len);
	   recvfrom(sockfd,ip,sizeof(ip),0,(SA*)&servaddr,&len);
	   printf("IP address is: %s\n",ip);

	   return 0;
}

Comments

Post a Comment

Popular posts from this blog

Configure opendns in ubuntu 12.04

Today i have changed my dns server name to opendns. which is alternative to default dns and it provide more secure, faster browsing experience. What is opendns? OpenDNS is the leading provider of Internet security and DNS services Industry leading malware and botnet protection Award winning Web filtering Faster, reliable, more secure DNS Ultra-reliable, globally distributed cloud network Simple, Smart, Easy How to configure opendns in ubuntu 12.04: open terminal and type sudo nano /etc/resolv.conf enter your password and change these lines   nameserver 208.67.222.222  nameserver 208.67.220.220 to use google public dns use 8.8.8.8, 8.8.4.4 instead of 208.67.222.222, 208.67.220.220 simple isn't..! now see your facebook load faster than before. opendns comes with parental control, so never worry about unwanted websites, and your childrens are safe now. to check your dns changed successfully goto welcome.opendns.com

Given a string, reverse only vowels in it; leaving rest of the string as it is

/* Given a string, reverse only vowels in it; leaving rest of the string as it is. Input : abcdef Output : ebcdaf */ import java.io.*; import java.util.*; public class VowelReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); String vowels = ""; String ans = ""; int arr[] = new int[str.length()]; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u') { vowels+=str.charAt(i); arr[i]=1; } } String revVowels = new StringBuffer(vowels).reverse().toString(); int j=0; for(int i=0;i<str.length();i++) { if(arr[i]!=1) { ans+=str.charAt(i); } else { ans+=revVowels.charAt(j); j++; } } System.out.println(ans); } }