-1

I want to send data to my friend. I don't know his IP address. But I know his MAC address. So I have to send data using Ethernet frames.

I wrote a program, but it is not working. I didn't understand where is the problem? Where I did a mistake?

client side frame sending:

main()
{
  int size ;
  char req[10];
  printf("enter to send\n");
  gets(req);
  int fd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP));
  if (fd==-1) 
  {
    perror("socket");
  }

  struct ifreq ifr;
  char if_name[5]="eth0";
  int if_name_len=strlen(if_name);
  if (if_name_len<sizeof(ifr.ifr_name)) 
  {
     memcpy(ifr.ifr_name,if_name,if_name_len);
     ifr.ifr_name[if_name_len]=0;
  } 
  else 
  {
     printf("interface name is too long");
  }

  if (ioctl(fd,SIOCGIFINDEX,&ifr)==-1) 
  {
     perror("ioctl");
  }
  int ifindex=ifr.ifr_ifindex;
  printf("ethernet interface id:%x\n",ifindex);

  const unsigned char ether_broadcast_addr[]={0x00,0x1d,0x09,0x7a,0xDf,0x5e};

  struct sockaddr_ll addr={0};

  addr.sll_family=PF_PACKET;
  addr.sll_ifindex=ifindex;
  addr.sll_halen=ETHER_ADDR_LEN;
  //addr.sll_protocol=htons(ETH_P_IP);
  memcpy(&addr.sll_addr,ether_broadcast_addr,ETHER_ADDR_LEN);
  addr.sll_hatype=772;
  addr.sll_pkttype=PACKET_OUTGOING;

  printf("size=%d\n",size);
  if (size=sendto(fd,req,sizeof(req),0,(struct sockaddr*)&addr,sizeof(addr))==-1) 
  {
     perror("sendto");
  }
  printf("send size=%d\n",size);

}

frame receive code at server side:

main()
{

  int size;
  char req[20];
  int fd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
  if (fd==-1) 
  {
     perror("socket");
  }

  struct ifreq ifr;
  char if_name[5]="eth0";
  int if_name_len=strlen(if_name);
  if (if_name_len<sizeof(ifr.ifr_name)) 
  {
     memcpy(ifr.ifr_name,if_name,if_name_len);
     ifr.ifr_name[if_name_len]=0;
  } 
  else 
  {
     printf("interface name is too long");
  }

  if (ioctl(fd,SIOCGIFINDEX,&ifr)==-1) 
  {
     perror("ioctl");
  }
  int ifindex=ifr.ifr_ifindex;
  printf("ethernet interface id:%x\n",ifindex);

  const unsigned char ether_broadcast_addr[]={0x00,0x1d,0x09,0x7a,0xDf,0x5e};

  struct sockaddr_ll addr={0};

  addr.sll_family=PF_PACKET;
  addr.sll_ifindex=ifindex;
  addr.sll_halen=ETHER_ADDR_LEN;
  addr.sll_protocol=htons(ETH_P_ALL);
  memcpy(&addr.sll_addr,ether_broadcast_addr,ETHER_ADDR_LEN);

  addr.sll_pkttype=PACKET_OUTGOING;
  addr.sll_hatype=772;

  bind (fd, (struct sockaddr*)&addr, sizeof(addr));

  printf("size=%d\n",size);
  socklen_t a=sizeof(addr);
  size=recvfrom(fd,req,sizeof(req),0,(struct sockaddr*)&addr,&a);
  if(size==-1)  
  {
    perror("recvfrom");
  }
  printf("size=%d\n",size);
  printf("recv msg=%s\n",req);
}
rajk
  • 9
  • 3
  • 4
    'Not working' does not help us at all. What happens that should not happen? What does not happen that should happen? What did you find out during your own debugging? What happens when you check the line with a network analyser like Wireshark? – Martin James Feb 26 '14 at 11:07
  • Would it be quicker to figure out the IP address? – Ed Heal Feb 27 '14 at 01:06

1 Answers1

0

while your program may work in sending a data frame onto the network it will not get anywhere from there.MAC addresses are non Hierarchical which means the address space is flat. Knowing a MAC address tells you nothing on how to reach that address.

Having someones MAC address is the same as having someones name. You may know their name but you can not call them with our their phone number. Now if you have their full name you can look up their phone number. Networking is similar.

Network data (Packets) are sent from you computer to your default gateway which is a DSL or cable modem that connects to your ISP. When it receives the packet it looks at the IP address to decide where to send it. Since your frame (data without an IP address is called a frame) does not have an IP address the gateway will just delete it. To be more precise since it does not have an destination or source IP address it never really gets to the gateway. It is transmitted on the network and nothing is done with it.