Loading ...
Sorry, an error occurred while loading the content.

broadcast using packet socket

Expand Messages
  • arp_project
    Hello linux-savvies ! I am simulating ARP in linux(RH 7.0) using packet sockets. I am stuck right at the first step ! I m not able to broadcast ARP requests.
    Message 1 of 1 , Dec 31, 2001
    View Source
    • 0 Attachment
      Hello linux-savvies !

      I am simulating ARP in linux(RH 7.0) using packet sockets. I
      am stuck right at the first step ! I'm not able to broadcast ARP
      requests. The sendto() function that I'm using gives me no error but
      the packet is *NOT* broadcasted. /tcpdump/ running on other systems
      of the network do not get any packet sent by my application.

      So, those who know about packet sockets - plz help me out ! I've
      pasted the ARP-request broadcast() function below.

      Hoping for an early reply,
      sandeep


      --------------------------------------------------

      void broadcast()
      {
      /* begin declarations */
      struct sockaddr_ll myaddr;
      struct sockaddr_ll sll;
      int sockd,i,error,s,ret;
      struct ifreq tempifreq;
      int sock_pkt;
      int on;
      struct arp_pkt ap;
      char src_addr[20];
      /* end declarations */

      /* fill myaddr structure for binding the packet socket*/
      memset(&myaddr,'\0',sizeof(myaddr));
      myaddr.sll_family=AF_PACKET;
      myaddr.sll_protocol=htons(ETH_P_ARP);
      myaddr.sll_ifindex=2; /* as returned by ioctl() */

      /* create packet socket */
      if((sock_pkt=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP)))
      <0) /* no luck with ETH_P_ALL either */
      {
      perror("error creating socket\n");
      }

      sll.sll_family=AF_PACKET;
      sll.sll_protocol=htons(ETH_P_ARP);
      sll.sll_hatype=ARPHRD_ETHER;
      sll.sll_pkttype=PACKET_BROADCAST;
      sll.sll_halen=ETH_ALEN;

      for(i=0;i<ETH_ALEN;i++)
      {
      sll.sll_addr[i]= 0xff; /* broadcast addr */
      }


      /* index of the network interface - ethernet in this case */
      strcpy(tempifreq.ifr_name,"eth0");
      if( ioctl(sock_pkt,SIOCGIFINDEX,&tempifreq) <0)
      perror("ioctl");
      sll.sll_ifindex=tempifreq.ifr_ifindex;
      printf("sll.sll_ifindex = %d\n",sll.sll_ifindex); /* prints
      2 */


      /* set to PROMISC mode - Do I need to do this ? Anyway no
      luck without it either */
      tempifreq.ifr_flags|=IFF_PROMISC;
      if(ioctl(sock_pkt,SIOCSIFFLAGS,&tempifreq)==-1)
      {
      perror("ioctl");
      }


      /* create <Ethernet Header + ether_arp> packet... */
      for(i=0;i<ETH_ALEN;i++)
      ap.eh.ether_dhost[i]=0xff; /* broadcast address */
      for(i=0;i<ETH_ALEN;i++)
      ap.eh.ether_shost[i]=*(ptr1+i); /* ptr1 has the src
      ethernet addr */
      ap.eh.ether_type=ETHERTYPE_ARP;

      ap.ea.arp_hrd=ARPHRD_ETHER;
      ap.ea.arp_pro=ETHERTYPE_IP;
      ap.ea.arp_hln=ETH_ALEN;
      ap.ea.arp_pln=4;
      ap.ea.arp_op=ARPOP_REQUEST;

      for(i=0;i<ETH_ALEN;i++)
      ap.ea.arp_sha[i]=*(ptr1+i);
      for(i=0;i<ETH_ALEN;i++)
      ap.ea.arp_tha[i]=0; /* destination addr unknown and
      WANTED! */

      strcpy(src_addr,"192.x.xxx.x"); /* ip addr hard-coded : will
      change this later */


      /* convert ip addresses to network format and fill in the
      header fields */
      if((inet_pton(AF_INET,(const char*)&src_addr,&ap.ea.arp_spa)
      <0))
      perror("source protocol addr\n");

      if((inet_pton(AF_INET,(const char*)&ipaddr,&ap.ea.arp_tpa)
      <0)) /* the global variable ipaddr has the ip address whose hardware
      address is needed */
      perror("dest protocol addr\n");


      /* bind socket - again... have tried without binding the
      socket... but ... no luck */
      bind(sock_pkt,(struct sockaddr *)&myaddr,sizeof(myaddr));

      /* finally, send the packet - have tried this with write()
      too...but... :( */
      if((error=sendto(sock_pkt,&ap,sizeof(ap),0,(struct sockaddr*)
      &sll,sizeof(sll)))<0)
      {
      perror("error sending packet(eth_hdr+eth_arp)\n");

      switch(error)
      {
      case EMSGSIZE:
      printf("msg too long\n");
      break;
      case ENOTSOCK:
      printf("argument s is not a socket\n");
      break;
      case EFAULT:
      printf("invalid user space address\n");
      break;
      case EBADF:
      printf("invalid descriptor\n");
      break;
      case EAGAIN:
      printf("socket is non-blocking\n");
      break;
      case ENOBUFS:
      printf("output queue for interface is
      full\n");
      break;
      case EINTR:
      printf("interrupt occurred\n");
      break;
      case ENOMEM:
      printf("no mem available\n");
      break;
      case EINVAL:
      printf("invalid arg passed\n");
      break;
      case EPIPE:
      printf("local end shut down\n");
      break;
      default:
      printf("some other err\n");
      break;
      }
      }
      else
      printf("broadcast sent\n"); /* always prints this
      and gives no error */
      /* but no packet
      is sent #actually# */
      return;
      }

      ------------------------------------------
    Your message has been successfully submitted and would be delivered to recipients shortly.